* dwarf2read.c (struct dwo_file): Add/tweak comments.
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2013 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "bfd.h"
33 #include "elf-bfd.h"
34 #include "symtab.h"
35 #include "gdbtypes.h"
36 #include "objfiles.h"
37 #include "dwarf2.h"
38 #include "buildsym.h"
39 #include "demangle.h"
40 #include "gdb-demangle.h"
41 #include "expression.h"
42 #include "filenames.h" /* for DOSish file names */
43 #include "macrotab.h"
44 #include "language.h"
45 #include "complaints.h"
46 #include "bcache.h"
47 #include "dwarf2expr.h"
48 #include "dwarf2loc.h"
49 #include "cp-support.h"
50 #include "hashtab.h"
51 #include "command.h"
52 #include "gdbcmd.h"
53 #include "block.h"
54 #include "addrmap.h"
55 #include "typeprint.h"
56 #include "jv-lang.h"
57 #include "psympriv.h"
58 #include "exceptions.h"
59 #include "gdb_stat.h"
60 #include "completer.h"
61 #include "vec.h"
62 #include "c-lang.h"
63 #include "go-lang.h"
64 #include "valprint.h"
65 #include "gdbcore.h" /* for gnutarget */
66 #include "gdb/gdb-index.h"
67 #include <ctype.h>
68 #include "gdb_bfd.h"
69 #include "f-lang.h"
70 #include "source.h"
71 #include "filestuff.h"
72
73 #include <fcntl.h>
74 #include "gdb_string.h"
75 #include "gdb_assert.h"
76 #include <sys/types.h>
77
78 typedef struct symbol *symbolp;
79 DEF_VEC_P (symbolp);
80
81 /* When non-zero, print basic high level tracing messages.
82 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
83 static int dwarf2_read_debug = 0;
84
85 /* When non-zero, dump DIEs after they are read in. */
86 static unsigned int dwarf2_die_debug = 0;
87
88 /* When non-zero, cross-check physname against demangler. */
89 static int check_physname = 0;
90
91 /* When non-zero, do not reject deprecated .gdb_index sections. */
92 static int use_deprecated_index_sections = 0;
93
94 static const struct objfile_data *dwarf2_objfile_data_key;
95
96 /* The "aclass" indices for various kinds of computed DWARF symbols. */
97
98 static int dwarf2_locexpr_index;
99 static int dwarf2_loclist_index;
100 static int dwarf2_locexpr_block_index;
101 static int dwarf2_loclist_block_index;
102
103 struct dwarf2_section_info
104 {
105 asection *asection;
106 const gdb_byte *buffer;
107 bfd_size_type size;
108 /* True if we have tried to read this section. */
109 int readin;
110 };
111
112 typedef struct dwarf2_section_info dwarf2_section_info_def;
113 DEF_VEC_O (dwarf2_section_info_def);
114
115 /* All offsets in the index are of this type. It must be
116 architecture-independent. */
117 typedef uint32_t offset_type;
118
119 DEF_VEC_I (offset_type);
120
121 /* Ensure only legit values are used. */
122 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
123 do { \
124 gdb_assert ((unsigned int) (value) <= 1); \
125 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
126 } while (0)
127
128 /* Ensure only legit values are used. */
129 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
130 do { \
131 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
132 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
133 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
134 } while (0)
135
136 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
137 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
138 do { \
139 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
140 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
141 } while (0)
142
143 /* A description of the mapped index. The file format is described in
144 a comment by the code that writes the index. */
145 struct mapped_index
146 {
147 /* Index data format version. */
148 int version;
149
150 /* The total length of the buffer. */
151 off_t total_size;
152
153 /* A pointer to the address table data. */
154 const gdb_byte *address_table;
155
156 /* Size of the address table data in bytes. */
157 offset_type address_table_size;
158
159 /* The symbol table, implemented as a hash table. */
160 const offset_type *symbol_table;
161
162 /* Size in slots, each slot is 2 offset_types. */
163 offset_type symbol_table_slots;
164
165 /* A pointer to the constant pool. */
166 const char *constant_pool;
167 };
168
169 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
170 DEF_VEC_P (dwarf2_per_cu_ptr);
171
172 /* Collection of data recorded per objfile.
173 This hangs off of dwarf2_objfile_data_key. */
174
175 struct dwarf2_per_objfile
176 {
177 struct dwarf2_section_info info;
178 struct dwarf2_section_info abbrev;
179 struct dwarf2_section_info line;
180 struct dwarf2_section_info loc;
181 struct dwarf2_section_info macinfo;
182 struct dwarf2_section_info macro;
183 struct dwarf2_section_info str;
184 struct dwarf2_section_info ranges;
185 struct dwarf2_section_info addr;
186 struct dwarf2_section_info frame;
187 struct dwarf2_section_info eh_frame;
188 struct dwarf2_section_info gdb_index;
189
190 VEC (dwarf2_section_info_def) *types;
191
192 /* Back link. */
193 struct objfile *objfile;
194
195 /* Table of all the compilation units. This is used to locate
196 the target compilation unit of a particular reference. */
197 struct dwarf2_per_cu_data **all_comp_units;
198
199 /* The number of compilation units in ALL_COMP_UNITS. */
200 int n_comp_units;
201
202 /* The number of .debug_types-related CUs. */
203 int n_type_units;
204
205 /* The .debug_types-related CUs (TUs).
206 This is stored in malloc space because we may realloc it. */
207 struct signatured_type **all_type_units;
208
209 /* The number of entries in all_type_unit_groups. */
210 int n_type_unit_groups;
211
212 /* Table of type unit groups.
213 This exists to make it easy to iterate over all CUs and TU groups. */
214 struct type_unit_group **all_type_unit_groups;
215
216 /* Table of struct type_unit_group objects.
217 The hash key is the DW_AT_stmt_list value. */
218 htab_t type_unit_groups;
219
220 /* A table mapping .debug_types signatures to its signatured_type entry.
221 This is NULL if the .debug_types section hasn't been read in yet. */
222 htab_t signatured_types;
223
224 /* Type unit statistics, to see how well the scaling improvements
225 are doing. */
226 struct tu_stats
227 {
228 int nr_uniq_abbrev_tables;
229 int nr_symtabs;
230 int nr_symtab_sharers;
231 int nr_stmt_less_type_units;
232 } tu_stats;
233
234 /* A chain of compilation units that are currently read in, so that
235 they can be freed later. */
236 struct dwarf2_per_cu_data *read_in_chain;
237
238 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
239 This is NULL if the table hasn't been allocated yet. */
240 htab_t dwo_files;
241
242 /* Non-zero if we've check for whether there is a DWP file. */
243 int dwp_checked;
244
245 /* The DWP file if there is one, or NULL. */
246 struct dwp_file *dwp_file;
247
248 /* The shared '.dwz' file, if one exists. This is used when the
249 original data was compressed using 'dwz -m'. */
250 struct dwz_file *dwz_file;
251
252 /* A flag indicating wether this objfile has a section loaded at a
253 VMA of 0. */
254 int has_section_at_zero;
255
256 /* True if we are using the mapped index,
257 or we are faking it for OBJF_READNOW's sake. */
258 unsigned char using_index;
259
260 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
261 struct mapped_index *index_table;
262
263 /* When using index_table, this keeps track of all quick_file_names entries.
264 TUs typically share line table entries with a CU, so we maintain a
265 separate table of all line table entries to support the sharing.
266 Note that while there can be way more TUs than CUs, we've already
267 sorted all the TUs into "type unit groups", grouped by their
268 DW_AT_stmt_list value. Therefore the only sharing done here is with a
269 CU and its associated TU group if there is one. */
270 htab_t quick_file_names_table;
271
272 /* Set during partial symbol reading, to prevent queueing of full
273 symbols. */
274 int reading_partial_symbols;
275
276 /* Table mapping type DIEs to their struct type *.
277 This is NULL if not allocated yet.
278 The mapping is done via (CU/TU + DIE offset) -> type. */
279 htab_t die_type_hash;
280
281 /* The CUs we recently read. */
282 VEC (dwarf2_per_cu_ptr) *just_read_cus;
283 };
284
285 static struct dwarf2_per_objfile *dwarf2_per_objfile;
286
287 /* Default names of the debugging sections. */
288
289 /* Note that if the debugging section has been compressed, it might
290 have a name like .zdebug_info. */
291
292 static const struct dwarf2_debug_sections dwarf2_elf_names =
293 {
294 { ".debug_info", ".zdebug_info" },
295 { ".debug_abbrev", ".zdebug_abbrev" },
296 { ".debug_line", ".zdebug_line" },
297 { ".debug_loc", ".zdebug_loc" },
298 { ".debug_macinfo", ".zdebug_macinfo" },
299 { ".debug_macro", ".zdebug_macro" },
300 { ".debug_str", ".zdebug_str" },
301 { ".debug_ranges", ".zdebug_ranges" },
302 { ".debug_types", ".zdebug_types" },
303 { ".debug_addr", ".zdebug_addr" },
304 { ".debug_frame", ".zdebug_frame" },
305 { ".eh_frame", NULL },
306 { ".gdb_index", ".zgdb_index" },
307 23
308 };
309
310 /* List of DWO/DWP sections. */
311
312 static const struct dwop_section_names
313 {
314 struct dwarf2_section_names abbrev_dwo;
315 struct dwarf2_section_names info_dwo;
316 struct dwarf2_section_names line_dwo;
317 struct dwarf2_section_names loc_dwo;
318 struct dwarf2_section_names macinfo_dwo;
319 struct dwarf2_section_names macro_dwo;
320 struct dwarf2_section_names str_dwo;
321 struct dwarf2_section_names str_offsets_dwo;
322 struct dwarf2_section_names types_dwo;
323 struct dwarf2_section_names cu_index;
324 struct dwarf2_section_names tu_index;
325 }
326 dwop_section_names =
327 {
328 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
329 { ".debug_info.dwo", ".zdebug_info.dwo" },
330 { ".debug_line.dwo", ".zdebug_line.dwo" },
331 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
332 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
333 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
334 { ".debug_str.dwo", ".zdebug_str.dwo" },
335 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
336 { ".debug_types.dwo", ".zdebug_types.dwo" },
337 { ".debug_cu_index", ".zdebug_cu_index" },
338 { ".debug_tu_index", ".zdebug_tu_index" },
339 };
340
341 /* local data types */
342
343 /* The data in a compilation unit header, after target2host
344 translation, looks like this. */
345 struct comp_unit_head
346 {
347 unsigned int length;
348 short version;
349 unsigned char addr_size;
350 unsigned char signed_addr_p;
351 sect_offset abbrev_offset;
352
353 /* Size of file offsets; either 4 or 8. */
354 unsigned int offset_size;
355
356 /* Size of the length field; either 4 or 12. */
357 unsigned int initial_length_size;
358
359 /* Offset to the first byte of this compilation unit header in the
360 .debug_info section, for resolving relative reference dies. */
361 sect_offset offset;
362
363 /* Offset to first die in this cu from the start of the cu.
364 This will be the first byte following the compilation unit header. */
365 cu_offset first_die_offset;
366 };
367
368 /* Type used for delaying computation of method physnames.
369 See comments for compute_delayed_physnames. */
370 struct delayed_method_info
371 {
372 /* The type to which the method is attached, i.e., its parent class. */
373 struct type *type;
374
375 /* The index of the method in the type's function fieldlists. */
376 int fnfield_index;
377
378 /* The index of the method in the fieldlist. */
379 int index;
380
381 /* The name of the DIE. */
382 const char *name;
383
384 /* The DIE associated with this method. */
385 struct die_info *die;
386 };
387
388 typedef struct delayed_method_info delayed_method_info;
389 DEF_VEC_O (delayed_method_info);
390
391 /* Internal state when decoding a particular compilation unit. */
392 struct dwarf2_cu
393 {
394 /* The objfile containing this compilation unit. */
395 struct objfile *objfile;
396
397 /* The header of the compilation unit. */
398 struct comp_unit_head header;
399
400 /* Base address of this compilation unit. */
401 CORE_ADDR base_address;
402
403 /* Non-zero if base_address has been set. */
404 int base_known;
405
406 /* The language we are debugging. */
407 enum language language;
408 const struct language_defn *language_defn;
409
410 const char *producer;
411
412 /* The generic symbol table building routines have separate lists for
413 file scope symbols and all all other scopes (local scopes). So
414 we need to select the right one to pass to add_symbol_to_list().
415 We do it by keeping a pointer to the correct list in list_in_scope.
416
417 FIXME: The original dwarf code just treated the file scope as the
418 first local scope, and all other local scopes as nested local
419 scopes, and worked fine. Check to see if we really need to
420 distinguish these in buildsym.c. */
421 struct pending **list_in_scope;
422
423 /* The abbrev table for this CU.
424 Normally this points to the abbrev table in the objfile.
425 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
426 struct abbrev_table *abbrev_table;
427
428 /* Hash table holding all the loaded partial DIEs
429 with partial_die->offset.SECT_OFF as hash. */
430 htab_t partial_dies;
431
432 /* Storage for things with the same lifetime as this read-in compilation
433 unit, including partial DIEs. */
434 struct obstack comp_unit_obstack;
435
436 /* When multiple dwarf2_cu structures are living in memory, this field
437 chains them all together, so that they can be released efficiently.
438 We will probably also want a generation counter so that most-recently-used
439 compilation units are cached... */
440 struct dwarf2_per_cu_data *read_in_chain;
441
442 /* Backlink to our per_cu entry. */
443 struct dwarf2_per_cu_data *per_cu;
444
445 /* How many compilation units ago was this CU last referenced? */
446 int last_used;
447
448 /* A hash table of DIE cu_offset for following references with
449 die_info->offset.sect_off as hash. */
450 htab_t die_hash;
451
452 /* Full DIEs if read in. */
453 struct die_info *dies;
454
455 /* A set of pointers to dwarf2_per_cu_data objects for compilation
456 units referenced by this one. Only set during full symbol processing;
457 partial symbol tables do not have dependencies. */
458 htab_t dependencies;
459
460 /* Header data from the line table, during full symbol processing. */
461 struct line_header *line_header;
462
463 /* A list of methods which need to have physnames computed
464 after all type information has been read. */
465 VEC (delayed_method_info) *method_list;
466
467 /* To be copied to symtab->call_site_htab. */
468 htab_t call_site_htab;
469
470 /* Non-NULL if this CU came from a DWO file.
471 There is an invariant here that is important to remember:
472 Except for attributes copied from the top level DIE in the "main"
473 (or "stub") file in preparation for reading the DWO file
474 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
475 Either there isn't a DWO file (in which case this is NULL and the point
476 is moot), or there is and either we're not going to read it (in which
477 case this is NULL) or there is and we are reading it (in which case this
478 is non-NULL). */
479 struct dwo_unit *dwo_unit;
480
481 /* The DW_AT_addr_base attribute if present, zero otherwise
482 (zero is a valid value though).
483 Note this value comes from the stub CU/TU's DIE. */
484 ULONGEST addr_base;
485
486 /* The DW_AT_ranges_base attribute if present, zero otherwise
487 (zero is a valid value though).
488 Note this value comes from the stub CU/TU's DIE.
489 Also note that the value is zero in the non-DWO case so this value can
490 be used without needing to know whether DWO files are in use or not.
491 N.B. This does not apply to DW_AT_ranges appearing in
492 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
493 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
494 DW_AT_ranges_base *would* have to be applied, and we'd have to care
495 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
496 ULONGEST ranges_base;
497
498 /* Mark used when releasing cached dies. */
499 unsigned int mark : 1;
500
501 /* This CU references .debug_loc. See the symtab->locations_valid field.
502 This test is imperfect as there may exist optimized debug code not using
503 any location list and still facing inlining issues if handled as
504 unoptimized code. For a future better test see GCC PR other/32998. */
505 unsigned int has_loclist : 1;
506
507 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
508 if all the producer_is_* fields are valid. This information is cached
509 because profiling CU expansion showed excessive time spent in
510 producer_is_gxx_lt_4_6. */
511 unsigned int checked_producer : 1;
512 unsigned int producer_is_gxx_lt_4_6 : 1;
513 unsigned int producer_is_gcc_lt_4_3 : 1;
514 unsigned int producer_is_icc : 1;
515
516 /* When set, the file that we're processing is known to have
517 debugging info for C++ namespaces. GCC 3.3.x did not produce
518 this information, but later versions do. */
519
520 unsigned int processing_has_namespace_info : 1;
521 };
522
523 /* Persistent data held for a compilation unit, even when not
524 processing it. We put a pointer to this structure in the
525 read_symtab_private field of the psymtab. */
526
527 struct dwarf2_per_cu_data
528 {
529 /* The start offset and length of this compilation unit.
530 NOTE: Unlike comp_unit_head.length, this length includes
531 initial_length_size.
532 If the DIE refers to a DWO file, this is always of the original die,
533 not the DWO file. */
534 sect_offset offset;
535 unsigned int length;
536
537 /* Flag indicating this compilation unit will be read in before
538 any of the current compilation units are processed. */
539 unsigned int queued : 1;
540
541 /* This flag will be set when reading partial DIEs if we need to load
542 absolutely all DIEs for this compilation unit, instead of just the ones
543 we think are interesting. It gets set if we look for a DIE in the
544 hash table and don't find it. */
545 unsigned int load_all_dies : 1;
546
547 /* Non-zero if this CU is from .debug_types.
548 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
549 this is non-zero. */
550 unsigned int is_debug_types : 1;
551
552 /* Non-zero if this CU is from the .dwz file. */
553 unsigned int is_dwz : 1;
554
555 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
556 This flag is only valid if is_debug_types is true.
557 We can't read a CU directly from a DWO file: There are required
558 attributes in the stub. */
559 unsigned int reading_dwo_directly : 1;
560
561 /* Non-zero if the TU has been read.
562 This is used to assist the "Stay in DWO Optimization" for Fission:
563 When reading a DWO, it's faster to read TUs from the DWO instead of
564 fetching them from random other DWOs (due to comdat folding).
565 If the TU has already been read, the optimization is unnecessary
566 (and unwise - we don't want to change where gdb thinks the TU lives
567 "midflight").
568 This flag is only valid if is_debug_types is true. */
569 unsigned int tu_read : 1;
570
571 /* The section this CU/TU lives in.
572 If the DIE refers to a DWO file, this is always the original die,
573 not the DWO file. */
574 struct dwarf2_section_info *section;
575
576 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
577 of the CU cache it gets reset to NULL again. */
578 struct dwarf2_cu *cu;
579
580 /* The corresponding objfile.
581 Normally we can get the objfile from dwarf2_per_objfile.
582 However we can enter this file with just a "per_cu" handle. */
583 struct objfile *objfile;
584
585 /* When using partial symbol tables, the 'psymtab' field is active.
586 Otherwise the 'quick' field is active. */
587 union
588 {
589 /* The partial symbol table associated with this compilation unit,
590 or NULL for unread partial units. */
591 struct partial_symtab *psymtab;
592
593 /* Data needed by the "quick" functions. */
594 struct dwarf2_per_cu_quick_data *quick;
595 } v;
596
597 /* The CUs we import using DW_TAG_imported_unit. This is filled in
598 while reading psymtabs, used to compute the psymtab dependencies,
599 and then cleared. Then it is filled in again while reading full
600 symbols, and only deleted when the objfile is destroyed.
601
602 This is also used to work around a difference between the way gold
603 generates .gdb_index version <=7 and the way gdb does. Arguably this
604 is a gold bug. For symbols coming from TUs, gold records in the index
605 the CU that includes the TU instead of the TU itself. This breaks
606 dw2_lookup_symbol: It assumes that if the index says symbol X lives
607 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
608 will find X. Alas TUs live in their own symtab, so after expanding CU Y
609 we need to look in TU Z to find X. Fortunately, this is akin to
610 DW_TAG_imported_unit, so we just use the same mechanism: For
611 .gdb_index version <=7 this also records the TUs that the CU referred
612 to. Concurrently with this change gdb was modified to emit version 8
613 indices so we only pay a price for gold generated indices.
614 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
615 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
616 };
617
618 /* Entry in the signatured_types hash table. */
619
620 struct signatured_type
621 {
622 /* The "per_cu" object of this type.
623 This struct is used iff per_cu.is_debug_types.
624 N.B.: This is the first member so that it's easy to convert pointers
625 between them. */
626 struct dwarf2_per_cu_data per_cu;
627
628 /* The type's signature. */
629 ULONGEST signature;
630
631 /* Offset in the TU of the type's DIE, as read from the TU header.
632 If this TU is a DWO stub and the definition lives in a DWO file
633 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
634 cu_offset type_offset_in_tu;
635
636 /* Offset in the section of the type's DIE.
637 If the definition lives in a DWO file, this is the offset in the
638 .debug_types.dwo section.
639 The value is zero until the actual value is known.
640 Zero is otherwise not a valid section offset. */
641 sect_offset type_offset_in_section;
642
643 /* Type units are grouped by their DW_AT_stmt_list entry so that they
644 can share them. This points to the containing symtab. */
645 struct type_unit_group *type_unit_group;
646
647 /* The type.
648 The first time we encounter this type we fully read it in and install it
649 in the symbol tables. Subsequent times we only need the type. */
650 struct type *type;
651
652 /* Containing DWO unit.
653 This field is valid iff per_cu.reading_dwo_directly. */
654 struct dwo_unit *dwo_unit;
655 };
656
657 typedef struct signatured_type *sig_type_ptr;
658 DEF_VEC_P (sig_type_ptr);
659
660 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
661 This includes type_unit_group and quick_file_names. */
662
663 struct stmt_list_hash
664 {
665 /* The DWO unit this table is from or NULL if there is none. */
666 struct dwo_unit *dwo_unit;
667
668 /* Offset in .debug_line or .debug_line.dwo. */
669 sect_offset line_offset;
670 };
671
672 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
673 an object of this type. */
674
675 struct type_unit_group
676 {
677 /* dwarf2read.c's main "handle" on a TU symtab.
678 To simplify things we create an artificial CU that "includes" all the
679 type units using this stmt_list so that the rest of the code still has
680 a "per_cu" handle on the symtab.
681 This PER_CU is recognized by having no section. */
682 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
683 struct dwarf2_per_cu_data per_cu;
684
685 /* The TUs that share this DW_AT_stmt_list entry.
686 This is added to while parsing type units to build partial symtabs,
687 and is deleted afterwards and not used again. */
688 VEC (sig_type_ptr) *tus;
689
690 /* The primary symtab.
691 Type units in a group needn't all be defined in the same source file,
692 so we create an essentially anonymous symtab as the primary symtab. */
693 struct symtab *primary_symtab;
694
695 /* The data used to construct the hash key. */
696 struct stmt_list_hash hash;
697
698 /* The number of symtabs from the line header.
699 The value here must match line_header.num_file_names. */
700 unsigned int num_symtabs;
701
702 /* The symbol tables for this TU (obtained from the files listed in
703 DW_AT_stmt_list).
704 WARNING: The order of entries here must match the order of entries
705 in the line header. After the first TU using this type_unit_group, the
706 line header for the subsequent TUs is recreated from this. This is done
707 because we need to use the same symtabs for each TU using the same
708 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
709 there's no guarantee the line header doesn't have duplicate entries. */
710 struct symtab **symtabs;
711 };
712
713 /* These sections are what may appear in a DWO file. */
714
715 struct dwo_sections
716 {
717 struct dwarf2_section_info abbrev;
718 struct dwarf2_section_info line;
719 struct dwarf2_section_info loc;
720 struct dwarf2_section_info macinfo;
721 struct dwarf2_section_info macro;
722 struct dwarf2_section_info str;
723 struct dwarf2_section_info str_offsets;
724 /* In the case of a virtual DWO file, these two are unused. */
725 struct dwarf2_section_info info;
726 VEC (dwarf2_section_info_def) *types;
727 };
728
729 /* CUs/TUs in DWP/DWO files. */
730
731 struct dwo_unit
732 {
733 /* Backlink to the containing struct dwo_file. */
734 struct dwo_file *dwo_file;
735
736 /* The "id" that distinguishes this CU/TU.
737 .debug_info calls this "dwo_id", .debug_types calls this "signature".
738 Since signatures came first, we stick with it for consistency. */
739 ULONGEST signature;
740
741 /* The section this CU/TU lives in, in the DWO file. */
742 struct dwarf2_section_info *section;
743
744 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
745 sect_offset offset;
746 unsigned int length;
747
748 /* For types, offset in the type's DIE of the type defined by this TU. */
749 cu_offset type_offset_in_tu;
750 };
751
752 /* Data for one DWO file.
753
754 This includes virtual DWO files (a virtual DWO file is a DWO file as it
755 appears in a DWP file). DWP files don't really have DWO files per se -
756 comdat folding of types "loses" the DWO file they came from, and from
757 a high level view DWP files appear to contain a mass of random types.
758 However, to maintain consistency with the non-DWP case we pretend DWP
759 files contain virtual DWO files, and we assign each TU with one virtual
760 DWO file (generally based on the line and abbrev section offsets -
761 a heuristic that seems to work in practice). */
762
763 struct dwo_file
764 {
765 /* The DW_AT_GNU_dwo_name attribute.
766 For virtual DWO files the name is constructed from the section offsets
767 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
768 from related CU+TUs. */
769 const char *dwo_name;
770
771 /* The DW_AT_comp_dir attribute. */
772 const char *comp_dir;
773
774 /* The bfd, when the file is open. Otherwise this is NULL.
775 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
776 bfd *dbfd;
777
778 /* Section info for this file. */
779 struct dwo_sections sections;
780
781 /* The CU in the file.
782 We only support one because having more than one requires hacking the
783 dwo_name of each to match, which is highly unlikely to happen.
784 Doing this means all TUs can share comp_dir: We also assume that
785 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
786 struct dwo_unit *cu;
787
788 /* Table of TUs in the file.
789 Each element is a struct dwo_unit. */
790 htab_t tus;
791 };
792
793 /* These sections are what may appear in a DWP file. */
794
795 struct dwp_sections
796 {
797 struct dwarf2_section_info str;
798 struct dwarf2_section_info cu_index;
799 struct dwarf2_section_info tu_index;
800 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
801 by section number. We don't need to record them here. */
802 };
803
804 /* These sections are what may appear in a virtual DWO file. */
805
806 struct virtual_dwo_sections
807 {
808 struct dwarf2_section_info abbrev;
809 struct dwarf2_section_info line;
810 struct dwarf2_section_info loc;
811 struct dwarf2_section_info macinfo;
812 struct dwarf2_section_info macro;
813 struct dwarf2_section_info str_offsets;
814 /* Each DWP hash table entry records one CU or one TU.
815 That is recorded here, and copied to dwo_unit.section. */
816 struct dwarf2_section_info info_or_types;
817 };
818
819 /* Contents of DWP hash tables. */
820
821 struct dwp_hash_table
822 {
823 uint32_t nr_units, nr_slots;
824 const gdb_byte *hash_table, *unit_table, *section_pool;
825 };
826
827 /* Data for one DWP file. */
828
829 struct dwp_file
830 {
831 /* Name of the file. */
832 const char *name;
833
834 /* The bfd. */
835 bfd *dbfd;
836
837 /* Section info for this file. */
838 struct dwp_sections sections;
839
840 /* Table of CUs in the file. */
841 const struct dwp_hash_table *cus;
842
843 /* Table of TUs in the file. */
844 const struct dwp_hash_table *tus;
845
846 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
847 htab_t loaded_cutus;
848
849 /* Table to map ELF section numbers to their sections. */
850 unsigned int num_sections;
851 asection **elf_sections;
852 };
853
854 /* This represents a '.dwz' file. */
855
856 struct dwz_file
857 {
858 /* A dwz file can only contain a few sections. */
859 struct dwarf2_section_info abbrev;
860 struct dwarf2_section_info info;
861 struct dwarf2_section_info str;
862 struct dwarf2_section_info line;
863 struct dwarf2_section_info macro;
864 struct dwarf2_section_info gdb_index;
865
866 /* The dwz's BFD. */
867 bfd *dwz_bfd;
868 };
869
870 /* Struct used to pass misc. parameters to read_die_and_children, et
871 al. which are used for both .debug_info and .debug_types dies.
872 All parameters here are unchanging for the life of the call. This
873 struct exists to abstract away the constant parameters of die reading. */
874
875 struct die_reader_specs
876 {
877 /* die_section->asection->owner. */
878 bfd* abfd;
879
880 /* The CU of the DIE we are parsing. */
881 struct dwarf2_cu *cu;
882
883 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
884 struct dwo_file *dwo_file;
885
886 /* The section the die comes from.
887 This is either .debug_info or .debug_types, or the .dwo variants. */
888 struct dwarf2_section_info *die_section;
889
890 /* die_section->buffer. */
891 const gdb_byte *buffer;
892
893 /* The end of the buffer. */
894 const gdb_byte *buffer_end;
895
896 /* The value of the DW_AT_comp_dir attribute. */
897 const char *comp_dir;
898 };
899
900 /* Type of function passed to init_cutu_and_read_dies, et.al. */
901 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
902 const gdb_byte *info_ptr,
903 struct die_info *comp_unit_die,
904 int has_children,
905 void *data);
906
907 /* The line number information for a compilation unit (found in the
908 .debug_line section) begins with a "statement program header",
909 which contains the following information. */
910 struct line_header
911 {
912 unsigned int total_length;
913 unsigned short version;
914 unsigned int header_length;
915 unsigned char minimum_instruction_length;
916 unsigned char maximum_ops_per_instruction;
917 unsigned char default_is_stmt;
918 int line_base;
919 unsigned char line_range;
920 unsigned char opcode_base;
921
922 /* standard_opcode_lengths[i] is the number of operands for the
923 standard opcode whose value is i. This means that
924 standard_opcode_lengths[0] is unused, and the last meaningful
925 element is standard_opcode_lengths[opcode_base - 1]. */
926 unsigned char *standard_opcode_lengths;
927
928 /* The include_directories table. NOTE! These strings are not
929 allocated with xmalloc; instead, they are pointers into
930 debug_line_buffer. If you try to free them, `free' will get
931 indigestion. */
932 unsigned int num_include_dirs, include_dirs_size;
933 const char **include_dirs;
934
935 /* The file_names table. NOTE! These strings are not allocated
936 with xmalloc; instead, they are pointers into debug_line_buffer.
937 Don't try to free them directly. */
938 unsigned int num_file_names, file_names_size;
939 struct file_entry
940 {
941 const char *name;
942 unsigned int dir_index;
943 unsigned int mod_time;
944 unsigned int length;
945 int included_p; /* Non-zero if referenced by the Line Number Program. */
946 struct symtab *symtab; /* The associated symbol table, if any. */
947 } *file_names;
948
949 /* The start and end of the statement program following this
950 header. These point into dwarf2_per_objfile->line_buffer. */
951 const gdb_byte *statement_program_start, *statement_program_end;
952 };
953
954 /* When we construct a partial symbol table entry we only
955 need this much information. */
956 struct partial_die_info
957 {
958 /* Offset of this DIE. */
959 sect_offset offset;
960
961 /* DWARF-2 tag for this DIE. */
962 ENUM_BITFIELD(dwarf_tag) tag : 16;
963
964 /* Assorted flags describing the data found in this DIE. */
965 unsigned int has_children : 1;
966 unsigned int is_external : 1;
967 unsigned int is_declaration : 1;
968 unsigned int has_type : 1;
969 unsigned int has_specification : 1;
970 unsigned int has_pc_info : 1;
971 unsigned int may_be_inlined : 1;
972
973 /* Flag set if the SCOPE field of this structure has been
974 computed. */
975 unsigned int scope_set : 1;
976
977 /* Flag set if the DIE has a byte_size attribute. */
978 unsigned int has_byte_size : 1;
979
980 /* Flag set if any of the DIE's children are template arguments. */
981 unsigned int has_template_arguments : 1;
982
983 /* Flag set if fixup_partial_die has been called on this die. */
984 unsigned int fixup_called : 1;
985
986 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
987 unsigned int is_dwz : 1;
988
989 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
990 unsigned int spec_is_dwz : 1;
991
992 /* The name of this DIE. Normally the value of DW_AT_name, but
993 sometimes a default name for unnamed DIEs. */
994 const char *name;
995
996 /* The linkage name, if present. */
997 const char *linkage_name;
998
999 /* The scope to prepend to our children. This is generally
1000 allocated on the comp_unit_obstack, so will disappear
1001 when this compilation unit leaves the cache. */
1002 const char *scope;
1003
1004 /* Some data associated with the partial DIE. The tag determines
1005 which field is live. */
1006 union
1007 {
1008 /* The location description associated with this DIE, if any. */
1009 struct dwarf_block *locdesc;
1010 /* The offset of an import, for DW_TAG_imported_unit. */
1011 sect_offset offset;
1012 } d;
1013
1014 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1015 CORE_ADDR lowpc;
1016 CORE_ADDR highpc;
1017
1018 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1019 DW_AT_sibling, if any. */
1020 /* NOTE: This member isn't strictly necessary, read_partial_die could
1021 return DW_AT_sibling values to its caller load_partial_dies. */
1022 const gdb_byte *sibling;
1023
1024 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1025 DW_AT_specification (or DW_AT_abstract_origin or
1026 DW_AT_extension). */
1027 sect_offset spec_offset;
1028
1029 /* Pointers to this DIE's parent, first child, and next sibling,
1030 if any. */
1031 struct partial_die_info *die_parent, *die_child, *die_sibling;
1032 };
1033
1034 /* This data structure holds the information of an abbrev. */
1035 struct abbrev_info
1036 {
1037 unsigned int number; /* number identifying abbrev */
1038 enum dwarf_tag tag; /* dwarf tag */
1039 unsigned short has_children; /* boolean */
1040 unsigned short num_attrs; /* number of attributes */
1041 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1042 struct abbrev_info *next; /* next in chain */
1043 };
1044
1045 struct attr_abbrev
1046 {
1047 ENUM_BITFIELD(dwarf_attribute) name : 16;
1048 ENUM_BITFIELD(dwarf_form) form : 16;
1049 };
1050
1051 /* Size of abbrev_table.abbrev_hash_table. */
1052 #define ABBREV_HASH_SIZE 121
1053
1054 /* Top level data structure to contain an abbreviation table. */
1055
1056 struct abbrev_table
1057 {
1058 /* Where the abbrev table came from.
1059 This is used as a sanity check when the table is used. */
1060 sect_offset offset;
1061
1062 /* Storage for the abbrev table. */
1063 struct obstack abbrev_obstack;
1064
1065 /* Hash table of abbrevs.
1066 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1067 It could be statically allocated, but the previous code didn't so we
1068 don't either. */
1069 struct abbrev_info **abbrevs;
1070 };
1071
1072 /* Attributes have a name and a value. */
1073 struct attribute
1074 {
1075 ENUM_BITFIELD(dwarf_attribute) name : 16;
1076 ENUM_BITFIELD(dwarf_form) form : 15;
1077
1078 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1079 field should be in u.str (existing only for DW_STRING) but it is kept
1080 here for better struct attribute alignment. */
1081 unsigned int string_is_canonical : 1;
1082
1083 union
1084 {
1085 const char *str;
1086 struct dwarf_block *blk;
1087 ULONGEST unsnd;
1088 LONGEST snd;
1089 CORE_ADDR addr;
1090 ULONGEST signature;
1091 }
1092 u;
1093 };
1094
1095 /* This data structure holds a complete die structure. */
1096 struct die_info
1097 {
1098 /* DWARF-2 tag for this DIE. */
1099 ENUM_BITFIELD(dwarf_tag) tag : 16;
1100
1101 /* Number of attributes */
1102 unsigned char num_attrs;
1103
1104 /* True if we're presently building the full type name for the
1105 type derived from this DIE. */
1106 unsigned char building_fullname : 1;
1107
1108 /* Abbrev number */
1109 unsigned int abbrev;
1110
1111 /* Offset in .debug_info or .debug_types section. */
1112 sect_offset offset;
1113
1114 /* The dies in a compilation unit form an n-ary tree. PARENT
1115 points to this die's parent; CHILD points to the first child of
1116 this node; and all the children of a given node are chained
1117 together via their SIBLING fields. */
1118 struct die_info *child; /* Its first child, if any. */
1119 struct die_info *sibling; /* Its next sibling, if any. */
1120 struct die_info *parent; /* Its parent, if any. */
1121
1122 /* An array of attributes, with NUM_ATTRS elements. There may be
1123 zero, but it's not common and zero-sized arrays are not
1124 sufficiently portable C. */
1125 struct attribute attrs[1];
1126 };
1127
1128 /* Get at parts of an attribute structure. */
1129
1130 #define DW_STRING(attr) ((attr)->u.str)
1131 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1132 #define DW_UNSND(attr) ((attr)->u.unsnd)
1133 #define DW_BLOCK(attr) ((attr)->u.blk)
1134 #define DW_SND(attr) ((attr)->u.snd)
1135 #define DW_ADDR(attr) ((attr)->u.addr)
1136 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1137
1138 /* Blocks are a bunch of untyped bytes. */
1139 struct dwarf_block
1140 {
1141 size_t size;
1142
1143 /* Valid only if SIZE is not zero. */
1144 const gdb_byte *data;
1145 };
1146
1147 #ifndef ATTR_ALLOC_CHUNK
1148 #define ATTR_ALLOC_CHUNK 4
1149 #endif
1150
1151 /* Allocate fields for structs, unions and enums in this size. */
1152 #ifndef DW_FIELD_ALLOC_CHUNK
1153 #define DW_FIELD_ALLOC_CHUNK 4
1154 #endif
1155
1156 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1157 but this would require a corresponding change in unpack_field_as_long
1158 and friends. */
1159 static int bits_per_byte = 8;
1160
1161 /* The routines that read and process dies for a C struct or C++ class
1162 pass lists of data member fields and lists of member function fields
1163 in an instance of a field_info structure, as defined below. */
1164 struct field_info
1165 {
1166 /* List of data member and baseclasses fields. */
1167 struct nextfield
1168 {
1169 struct nextfield *next;
1170 int accessibility;
1171 int virtuality;
1172 struct field field;
1173 }
1174 *fields, *baseclasses;
1175
1176 /* Number of fields (including baseclasses). */
1177 int nfields;
1178
1179 /* Number of baseclasses. */
1180 int nbaseclasses;
1181
1182 /* Set if the accesibility of one of the fields is not public. */
1183 int non_public_fields;
1184
1185 /* Member function fields array, entries are allocated in the order they
1186 are encountered in the object file. */
1187 struct nextfnfield
1188 {
1189 struct nextfnfield *next;
1190 struct fn_field fnfield;
1191 }
1192 *fnfields;
1193
1194 /* Member function fieldlist array, contains name of possibly overloaded
1195 member function, number of overloaded member functions and a pointer
1196 to the head of the member function field chain. */
1197 struct fnfieldlist
1198 {
1199 const char *name;
1200 int length;
1201 struct nextfnfield *head;
1202 }
1203 *fnfieldlists;
1204
1205 /* Number of entries in the fnfieldlists array. */
1206 int nfnfields;
1207
1208 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1209 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1210 struct typedef_field_list
1211 {
1212 struct typedef_field field;
1213 struct typedef_field_list *next;
1214 }
1215 *typedef_field_list;
1216 unsigned typedef_field_list_count;
1217 };
1218
1219 /* One item on the queue of compilation units to read in full symbols
1220 for. */
1221 struct dwarf2_queue_item
1222 {
1223 struct dwarf2_per_cu_data *per_cu;
1224 enum language pretend_language;
1225 struct dwarf2_queue_item *next;
1226 };
1227
1228 /* The current queue. */
1229 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1230
1231 /* Loaded secondary compilation units are kept in memory until they
1232 have not been referenced for the processing of this many
1233 compilation units. Set this to zero to disable caching. Cache
1234 sizes of up to at least twenty will improve startup time for
1235 typical inter-CU-reference binaries, at an obvious memory cost. */
1236 static int dwarf2_max_cache_age = 5;
1237 static void
1238 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1239 struct cmd_list_element *c, const char *value)
1240 {
1241 fprintf_filtered (file, _("The upper bound on the age of cached "
1242 "dwarf2 compilation units is %s.\n"),
1243 value);
1244 }
1245 \f
1246 /* local function prototypes */
1247
1248 static void dwarf2_locate_sections (bfd *, asection *, void *);
1249
1250 static void dwarf2_find_base_address (struct die_info *die,
1251 struct dwarf2_cu *cu);
1252
1253 static struct partial_symtab *create_partial_symtab
1254 (struct dwarf2_per_cu_data *per_cu, const char *name);
1255
1256 static void dwarf2_build_psymtabs_hard (struct objfile *);
1257
1258 static void scan_partial_symbols (struct partial_die_info *,
1259 CORE_ADDR *, CORE_ADDR *,
1260 int, struct dwarf2_cu *);
1261
1262 static void add_partial_symbol (struct partial_die_info *,
1263 struct dwarf2_cu *);
1264
1265 static void add_partial_namespace (struct partial_die_info *pdi,
1266 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1267 int need_pc, struct dwarf2_cu *cu);
1268
1269 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1270 CORE_ADDR *highpc, int need_pc,
1271 struct dwarf2_cu *cu);
1272
1273 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1274 struct dwarf2_cu *cu);
1275
1276 static void add_partial_subprogram (struct partial_die_info *pdi,
1277 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1278 int need_pc, struct dwarf2_cu *cu);
1279
1280 static void dwarf2_read_symtab (struct partial_symtab *,
1281 struct objfile *);
1282
1283 static void psymtab_to_symtab_1 (struct partial_symtab *);
1284
1285 static struct abbrev_info *abbrev_table_lookup_abbrev
1286 (const struct abbrev_table *, unsigned int);
1287
1288 static struct abbrev_table *abbrev_table_read_table
1289 (struct dwarf2_section_info *, sect_offset);
1290
1291 static void abbrev_table_free (struct abbrev_table *);
1292
1293 static void abbrev_table_free_cleanup (void *);
1294
1295 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1296 struct dwarf2_section_info *);
1297
1298 static void dwarf2_free_abbrev_table (void *);
1299
1300 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1301
1302 static struct partial_die_info *load_partial_dies
1303 (const struct die_reader_specs *, const gdb_byte *, int);
1304
1305 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1306 struct partial_die_info *,
1307 struct abbrev_info *,
1308 unsigned int,
1309 const gdb_byte *);
1310
1311 static struct partial_die_info *find_partial_die (sect_offset, int,
1312 struct dwarf2_cu *);
1313
1314 static void fixup_partial_die (struct partial_die_info *,
1315 struct dwarf2_cu *);
1316
1317 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1318 struct attribute *, struct attr_abbrev *,
1319 const gdb_byte *);
1320
1321 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1322
1323 static int read_1_signed_byte (bfd *, const gdb_byte *);
1324
1325 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1326
1327 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1328
1329 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1330
1331 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1332 unsigned int *);
1333
1334 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1335
1336 static LONGEST read_checked_initial_length_and_offset
1337 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1338 unsigned int *, unsigned int *);
1339
1340 static LONGEST read_offset (bfd *, const gdb_byte *,
1341 const struct comp_unit_head *,
1342 unsigned int *);
1343
1344 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1345
1346 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1347 sect_offset);
1348
1349 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1350
1351 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1352
1353 static const char *read_indirect_string (bfd *, const gdb_byte *,
1354 const struct comp_unit_head *,
1355 unsigned int *);
1356
1357 static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1358
1359 static ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);
1360
1361 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1362
1363 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1364 const gdb_byte *,
1365 unsigned int *);
1366
1367 static const char *read_str_index (const struct die_reader_specs *reader,
1368 struct dwarf2_cu *cu, ULONGEST str_index);
1369
1370 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1371
1372 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1373 struct dwarf2_cu *);
1374
1375 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1376 unsigned int);
1377
1378 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1379 struct dwarf2_cu *cu);
1380
1381 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1382
1383 static struct die_info *die_specification (struct die_info *die,
1384 struct dwarf2_cu **);
1385
1386 static void free_line_header (struct line_header *lh);
1387
1388 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1389 struct dwarf2_cu *cu);
1390
1391 static void dwarf_decode_lines (struct line_header *, const char *,
1392 struct dwarf2_cu *, struct partial_symtab *,
1393 int);
1394
1395 static void dwarf2_start_subfile (const char *, const char *, const char *);
1396
1397 static void dwarf2_start_symtab (struct dwarf2_cu *,
1398 const char *, const char *, CORE_ADDR);
1399
1400 static struct symbol *new_symbol (struct die_info *, struct type *,
1401 struct dwarf2_cu *);
1402
1403 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1404 struct dwarf2_cu *, struct symbol *);
1405
1406 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1407 struct dwarf2_cu *);
1408
1409 static void dwarf2_const_value_attr (const struct attribute *attr,
1410 struct type *type,
1411 const char *name,
1412 struct obstack *obstack,
1413 struct dwarf2_cu *cu, LONGEST *value,
1414 const gdb_byte **bytes,
1415 struct dwarf2_locexpr_baton **baton);
1416
1417 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1418
1419 static int need_gnat_info (struct dwarf2_cu *);
1420
1421 static struct type *die_descriptive_type (struct die_info *,
1422 struct dwarf2_cu *);
1423
1424 static void set_descriptive_type (struct type *, struct die_info *,
1425 struct dwarf2_cu *);
1426
1427 static struct type *die_containing_type (struct die_info *,
1428 struct dwarf2_cu *);
1429
1430 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1431 struct dwarf2_cu *);
1432
1433 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1434
1435 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1436
1437 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1438
1439 static char *typename_concat (struct obstack *obs, const char *prefix,
1440 const char *suffix, int physname,
1441 struct dwarf2_cu *cu);
1442
1443 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1444
1445 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1446
1447 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1448
1449 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1450
1451 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1452
1453 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1454 struct dwarf2_cu *, struct partial_symtab *);
1455
1456 static int dwarf2_get_pc_bounds (struct die_info *,
1457 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1458 struct partial_symtab *);
1459
1460 static void get_scope_pc_bounds (struct die_info *,
1461 CORE_ADDR *, CORE_ADDR *,
1462 struct dwarf2_cu *);
1463
1464 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1465 CORE_ADDR, struct dwarf2_cu *);
1466
1467 static void dwarf2_add_field (struct field_info *, struct die_info *,
1468 struct dwarf2_cu *);
1469
1470 static void dwarf2_attach_fields_to_type (struct field_info *,
1471 struct type *, struct dwarf2_cu *);
1472
1473 static void dwarf2_add_member_fn (struct field_info *,
1474 struct die_info *, struct type *,
1475 struct dwarf2_cu *);
1476
1477 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1478 struct type *,
1479 struct dwarf2_cu *);
1480
1481 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1482
1483 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1484
1485 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1486
1487 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1488
1489 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1490
1491 static struct type *read_module_type (struct die_info *die,
1492 struct dwarf2_cu *cu);
1493
1494 static const char *namespace_name (struct die_info *die,
1495 int *is_anonymous, struct dwarf2_cu *);
1496
1497 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1498
1499 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1500
1501 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1502 struct dwarf2_cu *);
1503
1504 static struct die_info *read_die_and_siblings_1
1505 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1506 struct die_info *);
1507
1508 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1509 const gdb_byte *info_ptr,
1510 const gdb_byte **new_info_ptr,
1511 struct die_info *parent);
1512
1513 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1514 struct die_info **, const gdb_byte *,
1515 int *, int);
1516
1517 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1518 struct die_info **, const gdb_byte *,
1519 int *);
1520
1521 static void process_die (struct die_info *, struct dwarf2_cu *);
1522
1523 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1524 struct obstack *);
1525
1526 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1527
1528 static const char *dwarf2_full_name (const char *name,
1529 struct die_info *die,
1530 struct dwarf2_cu *cu);
1531
1532 static const char *dwarf2_physname (const char *name, struct die_info *die,
1533 struct dwarf2_cu *cu);
1534
1535 static struct die_info *dwarf2_extension (struct die_info *die,
1536 struct dwarf2_cu **);
1537
1538 static const char *dwarf_tag_name (unsigned int);
1539
1540 static const char *dwarf_attr_name (unsigned int);
1541
1542 static const char *dwarf_form_name (unsigned int);
1543
1544 static char *dwarf_bool_name (unsigned int);
1545
1546 static const char *dwarf_type_encoding_name (unsigned int);
1547
1548 static struct die_info *sibling_die (struct die_info *);
1549
1550 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1551
1552 static void dump_die_for_error (struct die_info *);
1553
1554 static void dump_die_1 (struct ui_file *, int level, int max_level,
1555 struct die_info *);
1556
1557 /*static*/ void dump_die (struct die_info *, int max_level);
1558
1559 static void store_in_ref_table (struct die_info *,
1560 struct dwarf2_cu *);
1561
1562 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1563
1564 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1565
1566 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1567 const struct attribute *,
1568 struct dwarf2_cu **);
1569
1570 static struct die_info *follow_die_ref (struct die_info *,
1571 const struct attribute *,
1572 struct dwarf2_cu **);
1573
1574 static struct die_info *follow_die_sig (struct die_info *,
1575 const struct attribute *,
1576 struct dwarf2_cu **);
1577
1578 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1579 struct dwarf2_cu *);
1580
1581 static struct type *get_DW_AT_signature_type (struct die_info *,
1582 const struct attribute *,
1583 struct dwarf2_cu *);
1584
1585 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1586
1587 static void read_signatured_type (struct signatured_type *);
1588
1589 static struct type_unit_group *get_type_unit_group
1590 (struct dwarf2_cu *, const struct attribute *);
1591
1592 static void build_type_unit_groups (die_reader_func_ftype *, void *);
1593
1594 /* memory allocation interface */
1595
1596 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1597
1598 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1599
1600 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1601 const char *, int);
1602
1603 static int attr_form_is_block (const struct attribute *);
1604
1605 static int attr_form_is_section_offset (const struct attribute *);
1606
1607 static int attr_form_is_constant (const struct attribute *);
1608
1609 static int attr_form_is_ref (const struct attribute *);
1610
1611 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1612 struct dwarf2_loclist_baton *baton,
1613 const struct attribute *attr);
1614
1615 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1616 struct symbol *sym,
1617 struct dwarf2_cu *cu,
1618 int is_block);
1619
1620 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1621 const gdb_byte *info_ptr,
1622 struct abbrev_info *abbrev);
1623
1624 static void free_stack_comp_unit (void *);
1625
1626 static hashval_t partial_die_hash (const void *item);
1627
1628 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1629
1630 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1631 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1632
1633 static void init_one_comp_unit (struct dwarf2_cu *cu,
1634 struct dwarf2_per_cu_data *per_cu);
1635
1636 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1637 struct die_info *comp_unit_die,
1638 enum language pretend_language);
1639
1640 static void free_heap_comp_unit (void *);
1641
1642 static void free_cached_comp_units (void *);
1643
1644 static void age_cached_comp_units (void);
1645
1646 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1647
1648 static struct type *set_die_type (struct die_info *, struct type *,
1649 struct dwarf2_cu *);
1650
1651 static void create_all_comp_units (struct objfile *);
1652
1653 static int create_all_type_units (struct objfile *);
1654
1655 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1656 enum language);
1657
1658 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1659 enum language);
1660
1661 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1662 enum language);
1663
1664 static void dwarf2_add_dependence (struct dwarf2_cu *,
1665 struct dwarf2_per_cu_data *);
1666
1667 static void dwarf2_mark (struct dwarf2_cu *);
1668
1669 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1670
1671 static struct type *get_die_type_at_offset (sect_offset,
1672 struct dwarf2_per_cu_data *);
1673
1674 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1675
1676 static void dwarf2_release_queue (void *dummy);
1677
1678 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1679 enum language pretend_language);
1680
1681 static void process_queue (void);
1682
1683 static void find_file_and_directory (struct die_info *die,
1684 struct dwarf2_cu *cu,
1685 const char **name, const char **comp_dir);
1686
1687 static char *file_full_name (int file, struct line_header *lh,
1688 const char *comp_dir);
1689
1690 static const gdb_byte *read_and_check_comp_unit_head
1691 (struct comp_unit_head *header,
1692 struct dwarf2_section_info *section,
1693 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1694 int is_debug_types_section);
1695
1696 static void init_cutu_and_read_dies
1697 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1698 int use_existing_cu, int keep,
1699 die_reader_func_ftype *die_reader_func, void *data);
1700
1701 static void init_cutu_and_read_dies_simple
1702 (struct dwarf2_per_cu_data *this_cu,
1703 die_reader_func_ftype *die_reader_func, void *data);
1704
1705 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1706
1707 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1708
1709 static struct dwo_unit *lookup_dwo_unit_in_dwp
1710 (struct dwp_file *dwp_file, const char *comp_dir,
1711 ULONGEST signature, int is_debug_types);
1712
1713 static struct dwp_file *get_dwp_file (void);
1714
1715 static struct dwo_unit *lookup_dwo_comp_unit
1716 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1717
1718 static struct dwo_unit *lookup_dwo_type_unit
1719 (struct signatured_type *, const char *, const char *);
1720
1721 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1722
1723 static void free_dwo_file_cleanup (void *);
1724
1725 static void process_cu_includes (void);
1726
1727 static void check_producer (struct dwarf2_cu *cu);
1728 \f
1729 /* Various complaints about symbol reading that don't abort the process. */
1730
1731 static void
1732 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1733 {
1734 complaint (&symfile_complaints,
1735 _("statement list doesn't fit in .debug_line section"));
1736 }
1737
1738 static void
1739 dwarf2_debug_line_missing_file_complaint (void)
1740 {
1741 complaint (&symfile_complaints,
1742 _(".debug_line section has line data without a file"));
1743 }
1744
1745 static void
1746 dwarf2_debug_line_missing_end_sequence_complaint (void)
1747 {
1748 complaint (&symfile_complaints,
1749 _(".debug_line section has line "
1750 "program sequence without an end"));
1751 }
1752
1753 static void
1754 dwarf2_complex_location_expr_complaint (void)
1755 {
1756 complaint (&symfile_complaints, _("location expression too complex"));
1757 }
1758
1759 static void
1760 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1761 int arg3)
1762 {
1763 complaint (&symfile_complaints,
1764 _("const value length mismatch for '%s', got %d, expected %d"),
1765 arg1, arg2, arg3);
1766 }
1767
1768 static void
1769 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1770 {
1771 complaint (&symfile_complaints,
1772 _("debug info runs off end of %s section"
1773 " [in module %s]"),
1774 section->asection->name,
1775 bfd_get_filename (section->asection->owner));
1776 }
1777
1778 static void
1779 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1780 {
1781 complaint (&symfile_complaints,
1782 _("macro debug info contains a "
1783 "malformed macro definition:\n`%s'"),
1784 arg1);
1785 }
1786
1787 static void
1788 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1789 {
1790 complaint (&symfile_complaints,
1791 _("invalid attribute class or form for '%s' in '%s'"),
1792 arg1, arg2);
1793 }
1794 \f
1795 #if WORDS_BIGENDIAN
1796
1797 /* Convert VALUE between big- and little-endian. */
1798 static offset_type
1799 byte_swap (offset_type value)
1800 {
1801 offset_type result;
1802
1803 result = (value & 0xff) << 24;
1804 result |= (value & 0xff00) << 8;
1805 result |= (value & 0xff0000) >> 8;
1806 result |= (value & 0xff000000) >> 24;
1807 return result;
1808 }
1809
1810 #define MAYBE_SWAP(V) byte_swap (V)
1811
1812 #else
1813 #define MAYBE_SWAP(V) (V)
1814 #endif /* WORDS_BIGENDIAN */
1815
1816 /* The suffix for an index file. */
1817 #define INDEX_SUFFIX ".gdb-index"
1818
1819 /* Try to locate the sections we need for DWARF 2 debugging
1820 information and return true if we have enough to do something.
1821 NAMES points to the dwarf2 section names, or is NULL if the standard
1822 ELF names are used. */
1823
1824 int
1825 dwarf2_has_info (struct objfile *objfile,
1826 const struct dwarf2_debug_sections *names)
1827 {
1828 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1829 if (!dwarf2_per_objfile)
1830 {
1831 /* Initialize per-objfile state. */
1832 struct dwarf2_per_objfile *data
1833 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1834
1835 memset (data, 0, sizeof (*data));
1836 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1837 dwarf2_per_objfile = data;
1838
1839 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1840 (void *) names);
1841 dwarf2_per_objfile->objfile = objfile;
1842 }
1843 return (dwarf2_per_objfile->info.asection != NULL
1844 && dwarf2_per_objfile->abbrev.asection != NULL);
1845 }
1846
1847 /* When loading sections, we look either for uncompressed section or for
1848 compressed section names. */
1849
1850 static int
1851 section_is_p (const char *section_name,
1852 const struct dwarf2_section_names *names)
1853 {
1854 if (names->normal != NULL
1855 && strcmp (section_name, names->normal) == 0)
1856 return 1;
1857 if (names->compressed != NULL
1858 && strcmp (section_name, names->compressed) == 0)
1859 return 1;
1860 return 0;
1861 }
1862
1863 /* This function is mapped across the sections and remembers the
1864 offset and size of each of the debugging sections we are interested
1865 in. */
1866
1867 static void
1868 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1869 {
1870 const struct dwarf2_debug_sections *names;
1871 flagword aflag = bfd_get_section_flags (abfd, sectp);
1872
1873 if (vnames == NULL)
1874 names = &dwarf2_elf_names;
1875 else
1876 names = (const struct dwarf2_debug_sections *) vnames;
1877
1878 if ((aflag & SEC_HAS_CONTENTS) == 0)
1879 {
1880 }
1881 else if (section_is_p (sectp->name, &names->info))
1882 {
1883 dwarf2_per_objfile->info.asection = sectp;
1884 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1885 }
1886 else if (section_is_p (sectp->name, &names->abbrev))
1887 {
1888 dwarf2_per_objfile->abbrev.asection = sectp;
1889 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1890 }
1891 else if (section_is_p (sectp->name, &names->line))
1892 {
1893 dwarf2_per_objfile->line.asection = sectp;
1894 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1895 }
1896 else if (section_is_p (sectp->name, &names->loc))
1897 {
1898 dwarf2_per_objfile->loc.asection = sectp;
1899 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1900 }
1901 else if (section_is_p (sectp->name, &names->macinfo))
1902 {
1903 dwarf2_per_objfile->macinfo.asection = sectp;
1904 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1905 }
1906 else if (section_is_p (sectp->name, &names->macro))
1907 {
1908 dwarf2_per_objfile->macro.asection = sectp;
1909 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1910 }
1911 else if (section_is_p (sectp->name, &names->str))
1912 {
1913 dwarf2_per_objfile->str.asection = sectp;
1914 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1915 }
1916 else if (section_is_p (sectp->name, &names->addr))
1917 {
1918 dwarf2_per_objfile->addr.asection = sectp;
1919 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1920 }
1921 else if (section_is_p (sectp->name, &names->frame))
1922 {
1923 dwarf2_per_objfile->frame.asection = sectp;
1924 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1925 }
1926 else if (section_is_p (sectp->name, &names->eh_frame))
1927 {
1928 dwarf2_per_objfile->eh_frame.asection = sectp;
1929 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1930 }
1931 else if (section_is_p (sectp->name, &names->ranges))
1932 {
1933 dwarf2_per_objfile->ranges.asection = sectp;
1934 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1935 }
1936 else if (section_is_p (sectp->name, &names->types))
1937 {
1938 struct dwarf2_section_info type_section;
1939
1940 memset (&type_section, 0, sizeof (type_section));
1941 type_section.asection = sectp;
1942 type_section.size = bfd_get_section_size (sectp);
1943
1944 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1945 &type_section);
1946 }
1947 else if (section_is_p (sectp->name, &names->gdb_index))
1948 {
1949 dwarf2_per_objfile->gdb_index.asection = sectp;
1950 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1951 }
1952
1953 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1954 && bfd_section_vma (abfd, sectp) == 0)
1955 dwarf2_per_objfile->has_section_at_zero = 1;
1956 }
1957
1958 /* A helper function that decides whether a section is empty,
1959 or not present. */
1960
1961 static int
1962 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1963 {
1964 return info->asection == NULL || info->size == 0;
1965 }
1966
1967 /* Read the contents of the section INFO.
1968 OBJFILE is the main object file, but not necessarily the file where
1969 the section comes from. E.g., for DWO files INFO->asection->owner
1970 is the bfd of the DWO file.
1971 If the section is compressed, uncompress it before returning. */
1972
1973 static void
1974 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1975 {
1976 asection *sectp = info->asection;
1977 bfd *abfd;
1978 gdb_byte *buf, *retbuf;
1979 unsigned char header[4];
1980
1981 if (info->readin)
1982 return;
1983 info->buffer = NULL;
1984 info->readin = 1;
1985
1986 if (dwarf2_section_empty_p (info))
1987 return;
1988
1989 abfd = sectp->owner;
1990
1991 /* If the section has relocations, we must read it ourselves.
1992 Otherwise we attach it to the BFD. */
1993 if ((sectp->flags & SEC_RELOC) == 0)
1994 {
1995 info->buffer = gdb_bfd_map_section (sectp, &info->size);
1996 return;
1997 }
1998
1999 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
2000 info->buffer = buf;
2001
2002 /* When debugging .o files, we may need to apply relocations; see
2003 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2004 We never compress sections in .o files, so we only need to
2005 try this when the section is not compressed. */
2006 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2007 if (retbuf != NULL)
2008 {
2009 info->buffer = retbuf;
2010 return;
2011 }
2012
2013 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2014 || bfd_bread (buf, info->size, abfd) != info->size)
2015 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
2016 bfd_get_filename (abfd));
2017 }
2018
2019 /* A helper function that returns the size of a section in a safe way.
2020 If you are positive that the section has been read before using the
2021 size, then it is safe to refer to the dwarf2_section_info object's
2022 "size" field directly. In other cases, you must call this
2023 function, because for compressed sections the size field is not set
2024 correctly until the section has been read. */
2025
2026 static bfd_size_type
2027 dwarf2_section_size (struct objfile *objfile,
2028 struct dwarf2_section_info *info)
2029 {
2030 if (!info->readin)
2031 dwarf2_read_section (objfile, info);
2032 return info->size;
2033 }
2034
2035 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2036 SECTION_NAME. */
2037
2038 void
2039 dwarf2_get_section_info (struct objfile *objfile,
2040 enum dwarf2_section_enum sect,
2041 asection **sectp, const gdb_byte **bufp,
2042 bfd_size_type *sizep)
2043 {
2044 struct dwarf2_per_objfile *data
2045 = objfile_data (objfile, dwarf2_objfile_data_key);
2046 struct dwarf2_section_info *info;
2047
2048 /* We may see an objfile without any DWARF, in which case we just
2049 return nothing. */
2050 if (data == NULL)
2051 {
2052 *sectp = NULL;
2053 *bufp = NULL;
2054 *sizep = 0;
2055 return;
2056 }
2057 switch (sect)
2058 {
2059 case DWARF2_DEBUG_FRAME:
2060 info = &data->frame;
2061 break;
2062 case DWARF2_EH_FRAME:
2063 info = &data->eh_frame;
2064 break;
2065 default:
2066 gdb_assert_not_reached ("unexpected section");
2067 }
2068
2069 dwarf2_read_section (objfile, info);
2070
2071 *sectp = info->asection;
2072 *bufp = info->buffer;
2073 *sizep = info->size;
2074 }
2075
2076 /* A helper function to find the sections for a .dwz file. */
2077
2078 static void
2079 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2080 {
2081 struct dwz_file *dwz_file = arg;
2082
2083 /* Note that we only support the standard ELF names, because .dwz
2084 is ELF-only (at the time of writing). */
2085 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2086 {
2087 dwz_file->abbrev.asection = sectp;
2088 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2089 }
2090 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2091 {
2092 dwz_file->info.asection = sectp;
2093 dwz_file->info.size = bfd_get_section_size (sectp);
2094 }
2095 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2096 {
2097 dwz_file->str.asection = sectp;
2098 dwz_file->str.size = bfd_get_section_size (sectp);
2099 }
2100 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2101 {
2102 dwz_file->line.asection = sectp;
2103 dwz_file->line.size = bfd_get_section_size (sectp);
2104 }
2105 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2106 {
2107 dwz_file->macro.asection = sectp;
2108 dwz_file->macro.size = bfd_get_section_size (sectp);
2109 }
2110 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2111 {
2112 dwz_file->gdb_index.asection = sectp;
2113 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2114 }
2115 }
2116
2117 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2118 there is no .gnu_debugaltlink section in the file. Error if there
2119 is such a section but the file cannot be found. */
2120
2121 static struct dwz_file *
2122 dwarf2_get_dwz_file (void)
2123 {
2124 bfd *dwz_bfd;
2125 char *data;
2126 struct cleanup *cleanup;
2127 const char *filename;
2128 struct dwz_file *result;
2129 unsigned long buildid;
2130
2131 if (dwarf2_per_objfile->dwz_file != NULL)
2132 return dwarf2_per_objfile->dwz_file;
2133
2134 bfd_set_error (bfd_error_no_error);
2135 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2136 &buildid);
2137 if (data == NULL)
2138 {
2139 if (bfd_get_error () == bfd_error_no_error)
2140 return NULL;
2141 error (_("could not read '.gnu_debugaltlink' section: %s"),
2142 bfd_errmsg (bfd_get_error ()));
2143 }
2144 cleanup = make_cleanup (xfree, data);
2145
2146 filename = (const char *) data;
2147 if (!IS_ABSOLUTE_PATH (filename))
2148 {
2149 char *abs = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2150 char *rel;
2151
2152 make_cleanup (xfree, abs);
2153 abs = ldirname (abs);
2154 make_cleanup (xfree, abs);
2155
2156 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2157 make_cleanup (xfree, rel);
2158 filename = rel;
2159 }
2160
2161 /* The format is just a NUL-terminated file name, followed by the
2162 build-id. For now, though, we ignore the build-id. */
2163 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2164 if (dwz_bfd == NULL)
2165 error (_("could not read '%s': %s"), filename,
2166 bfd_errmsg (bfd_get_error ()));
2167
2168 if (!bfd_check_format (dwz_bfd, bfd_object))
2169 {
2170 gdb_bfd_unref (dwz_bfd);
2171 error (_("file '%s' was not usable: %s"), filename,
2172 bfd_errmsg (bfd_get_error ()));
2173 }
2174
2175 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2176 struct dwz_file);
2177 result->dwz_bfd = dwz_bfd;
2178
2179 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2180
2181 do_cleanups (cleanup);
2182
2183 dwarf2_per_objfile->dwz_file = result;
2184 return result;
2185 }
2186 \f
2187 /* DWARF quick_symbols_functions support. */
2188
2189 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2190 unique line tables, so we maintain a separate table of all .debug_line
2191 derived entries to support the sharing.
2192 All the quick functions need is the list of file names. We discard the
2193 line_header when we're done and don't need to record it here. */
2194 struct quick_file_names
2195 {
2196 /* The data used to construct the hash key. */
2197 struct stmt_list_hash hash;
2198
2199 /* The number of entries in file_names, real_names. */
2200 unsigned int num_file_names;
2201
2202 /* The file names from the line table, after being run through
2203 file_full_name. */
2204 const char **file_names;
2205
2206 /* The file names from the line table after being run through
2207 gdb_realpath. These are computed lazily. */
2208 const char **real_names;
2209 };
2210
2211 /* When using the index (and thus not using psymtabs), each CU has an
2212 object of this type. This is used to hold information needed by
2213 the various "quick" methods. */
2214 struct dwarf2_per_cu_quick_data
2215 {
2216 /* The file table. This can be NULL if there was no file table
2217 or it's currently not read in.
2218 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2219 struct quick_file_names *file_names;
2220
2221 /* The corresponding symbol table. This is NULL if symbols for this
2222 CU have not yet been read. */
2223 struct symtab *symtab;
2224
2225 /* A temporary mark bit used when iterating over all CUs in
2226 expand_symtabs_matching. */
2227 unsigned int mark : 1;
2228
2229 /* True if we've tried to read the file table and found there isn't one.
2230 There will be no point in trying to read it again next time. */
2231 unsigned int no_file_data : 1;
2232 };
2233
2234 /* Utility hash function for a stmt_list_hash. */
2235
2236 static hashval_t
2237 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2238 {
2239 hashval_t v = 0;
2240
2241 if (stmt_list_hash->dwo_unit != NULL)
2242 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2243 v += stmt_list_hash->line_offset.sect_off;
2244 return v;
2245 }
2246
2247 /* Utility equality function for a stmt_list_hash. */
2248
2249 static int
2250 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2251 const struct stmt_list_hash *rhs)
2252 {
2253 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2254 return 0;
2255 if (lhs->dwo_unit != NULL
2256 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2257 return 0;
2258
2259 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2260 }
2261
2262 /* Hash function for a quick_file_names. */
2263
2264 static hashval_t
2265 hash_file_name_entry (const void *e)
2266 {
2267 const struct quick_file_names *file_data = e;
2268
2269 return hash_stmt_list_entry (&file_data->hash);
2270 }
2271
2272 /* Equality function for a quick_file_names. */
2273
2274 static int
2275 eq_file_name_entry (const void *a, const void *b)
2276 {
2277 const struct quick_file_names *ea = a;
2278 const struct quick_file_names *eb = b;
2279
2280 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2281 }
2282
2283 /* Delete function for a quick_file_names. */
2284
2285 static void
2286 delete_file_name_entry (void *e)
2287 {
2288 struct quick_file_names *file_data = e;
2289 int i;
2290
2291 for (i = 0; i < file_data->num_file_names; ++i)
2292 {
2293 xfree ((void*) file_data->file_names[i]);
2294 if (file_data->real_names)
2295 xfree ((void*) file_data->real_names[i]);
2296 }
2297
2298 /* The space for the struct itself lives on objfile_obstack,
2299 so we don't free it here. */
2300 }
2301
2302 /* Create a quick_file_names hash table. */
2303
2304 static htab_t
2305 create_quick_file_names_table (unsigned int nr_initial_entries)
2306 {
2307 return htab_create_alloc (nr_initial_entries,
2308 hash_file_name_entry, eq_file_name_entry,
2309 delete_file_name_entry, xcalloc, xfree);
2310 }
2311
2312 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2313 have to be created afterwards. You should call age_cached_comp_units after
2314 processing PER_CU->CU. dw2_setup must have been already called. */
2315
2316 static void
2317 load_cu (struct dwarf2_per_cu_data *per_cu)
2318 {
2319 if (per_cu->is_debug_types)
2320 load_full_type_unit (per_cu);
2321 else
2322 load_full_comp_unit (per_cu, language_minimal);
2323
2324 gdb_assert (per_cu->cu != NULL);
2325
2326 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2327 }
2328
2329 /* Read in the symbols for PER_CU. */
2330
2331 static void
2332 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2333 {
2334 struct cleanup *back_to;
2335
2336 /* Skip type_unit_groups, reading the type units they contain
2337 is handled elsewhere. */
2338 if (IS_TYPE_UNIT_GROUP (per_cu))
2339 return;
2340
2341 back_to = make_cleanup (dwarf2_release_queue, NULL);
2342
2343 if (dwarf2_per_objfile->using_index
2344 ? per_cu->v.quick->symtab == NULL
2345 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2346 {
2347 queue_comp_unit (per_cu, language_minimal);
2348 load_cu (per_cu);
2349
2350 /* If we just loaded a CU from a DWO, and we're working with an index
2351 that may badly handle TUs, load all the TUs in that DWO as well.
2352 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2353 if (!per_cu->is_debug_types
2354 && per_cu->cu->dwo_unit != NULL
2355 && dwarf2_per_objfile->index_table != NULL
2356 && dwarf2_per_objfile->index_table->version <= 7
2357 /* DWP files aren't supported yet. */
2358 && get_dwp_file () == NULL)
2359 queue_and_load_all_dwo_tus (per_cu);
2360 }
2361
2362 process_queue ();
2363
2364 /* Age the cache, releasing compilation units that have not
2365 been used recently. */
2366 age_cached_comp_units ();
2367
2368 do_cleanups (back_to);
2369 }
2370
2371 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2372 the objfile from which this CU came. Returns the resulting symbol
2373 table. */
2374
2375 static struct symtab *
2376 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2377 {
2378 gdb_assert (dwarf2_per_objfile->using_index);
2379 if (!per_cu->v.quick->symtab)
2380 {
2381 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2382 increment_reading_symtab ();
2383 dw2_do_instantiate_symtab (per_cu);
2384 process_cu_includes ();
2385 do_cleanups (back_to);
2386 }
2387 return per_cu->v.quick->symtab;
2388 }
2389
2390 /* Return the CU given its index.
2391
2392 This is intended for loops like:
2393
2394 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2395 + dwarf2_per_objfile->n_type_units); ++i)
2396 {
2397 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2398
2399 ...;
2400 }
2401 */
2402
2403 static struct dwarf2_per_cu_data *
2404 dw2_get_cu (int index)
2405 {
2406 if (index >= dwarf2_per_objfile->n_comp_units)
2407 {
2408 index -= dwarf2_per_objfile->n_comp_units;
2409 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2410 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2411 }
2412
2413 return dwarf2_per_objfile->all_comp_units[index];
2414 }
2415
2416 /* Return the primary CU given its index.
2417 The difference between this function and dw2_get_cu is in the handling
2418 of type units (TUs). Here we return the type_unit_group object.
2419
2420 This is intended for loops like:
2421
2422 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2423 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2424 {
2425 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2426
2427 ...;
2428 }
2429 */
2430
2431 static struct dwarf2_per_cu_data *
2432 dw2_get_primary_cu (int index)
2433 {
2434 if (index >= dwarf2_per_objfile->n_comp_units)
2435 {
2436 index -= dwarf2_per_objfile->n_comp_units;
2437 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2438 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
2439 }
2440
2441 return dwarf2_per_objfile->all_comp_units[index];
2442 }
2443
2444 /* A helper for create_cus_from_index that handles a given list of
2445 CUs. */
2446
2447 static void
2448 create_cus_from_index_list (struct objfile *objfile,
2449 const gdb_byte *cu_list, offset_type n_elements,
2450 struct dwarf2_section_info *section,
2451 int is_dwz,
2452 int base_offset)
2453 {
2454 offset_type i;
2455
2456 for (i = 0; i < n_elements; i += 2)
2457 {
2458 struct dwarf2_per_cu_data *the_cu;
2459 ULONGEST offset, length;
2460
2461 gdb_static_assert (sizeof (ULONGEST) >= 8);
2462 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2463 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2464 cu_list += 2 * 8;
2465
2466 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2467 struct dwarf2_per_cu_data);
2468 the_cu->offset.sect_off = offset;
2469 the_cu->length = length;
2470 the_cu->objfile = objfile;
2471 the_cu->section = section;
2472 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2473 struct dwarf2_per_cu_quick_data);
2474 the_cu->is_dwz = is_dwz;
2475 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2476 }
2477 }
2478
2479 /* Read the CU list from the mapped index, and use it to create all
2480 the CU objects for this objfile. */
2481
2482 static void
2483 create_cus_from_index (struct objfile *objfile,
2484 const gdb_byte *cu_list, offset_type cu_list_elements,
2485 const gdb_byte *dwz_list, offset_type dwz_elements)
2486 {
2487 struct dwz_file *dwz;
2488
2489 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2490 dwarf2_per_objfile->all_comp_units
2491 = obstack_alloc (&objfile->objfile_obstack,
2492 dwarf2_per_objfile->n_comp_units
2493 * sizeof (struct dwarf2_per_cu_data *));
2494
2495 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2496 &dwarf2_per_objfile->info, 0, 0);
2497
2498 if (dwz_elements == 0)
2499 return;
2500
2501 dwz = dwarf2_get_dwz_file ();
2502 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2503 cu_list_elements / 2);
2504 }
2505
2506 /* Create the signatured type hash table from the index. */
2507
2508 static void
2509 create_signatured_type_table_from_index (struct objfile *objfile,
2510 struct dwarf2_section_info *section,
2511 const gdb_byte *bytes,
2512 offset_type elements)
2513 {
2514 offset_type i;
2515 htab_t sig_types_hash;
2516
2517 dwarf2_per_objfile->n_type_units = elements / 3;
2518 dwarf2_per_objfile->all_type_units
2519 = xmalloc (dwarf2_per_objfile->n_type_units
2520 * sizeof (struct signatured_type *));
2521
2522 sig_types_hash = allocate_signatured_type_table (objfile);
2523
2524 for (i = 0; i < elements; i += 3)
2525 {
2526 struct signatured_type *sig_type;
2527 ULONGEST offset, type_offset_in_tu, signature;
2528 void **slot;
2529
2530 gdb_static_assert (sizeof (ULONGEST) >= 8);
2531 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2532 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2533 BFD_ENDIAN_LITTLE);
2534 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2535 bytes += 3 * 8;
2536
2537 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2538 struct signatured_type);
2539 sig_type->signature = signature;
2540 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2541 sig_type->per_cu.is_debug_types = 1;
2542 sig_type->per_cu.section = section;
2543 sig_type->per_cu.offset.sect_off = offset;
2544 sig_type->per_cu.objfile = objfile;
2545 sig_type->per_cu.v.quick
2546 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2547 struct dwarf2_per_cu_quick_data);
2548
2549 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2550 *slot = sig_type;
2551
2552 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2553 }
2554
2555 dwarf2_per_objfile->signatured_types = sig_types_hash;
2556 }
2557
2558 /* Read the address map data from the mapped index, and use it to
2559 populate the objfile's psymtabs_addrmap. */
2560
2561 static void
2562 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2563 {
2564 const gdb_byte *iter, *end;
2565 struct obstack temp_obstack;
2566 struct addrmap *mutable_map;
2567 struct cleanup *cleanup;
2568 CORE_ADDR baseaddr;
2569
2570 obstack_init (&temp_obstack);
2571 cleanup = make_cleanup_obstack_free (&temp_obstack);
2572 mutable_map = addrmap_create_mutable (&temp_obstack);
2573
2574 iter = index->address_table;
2575 end = iter + index->address_table_size;
2576
2577 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2578
2579 while (iter < end)
2580 {
2581 ULONGEST hi, lo, cu_index;
2582 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2583 iter += 8;
2584 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2585 iter += 8;
2586 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2587 iter += 4;
2588
2589 if (lo > hi)
2590 {
2591 complaint (&symfile_complaints,
2592 _(".gdb_index address table has invalid range (%s - %s)"),
2593 hex_string (lo), hex_string (hi));
2594 continue;
2595 }
2596
2597 if (cu_index >= dwarf2_per_objfile->n_comp_units)
2598 {
2599 complaint (&symfile_complaints,
2600 _(".gdb_index address table has invalid CU number %u"),
2601 (unsigned) cu_index);
2602 continue;
2603 }
2604
2605 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2606 dw2_get_cu (cu_index));
2607 }
2608
2609 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2610 &objfile->objfile_obstack);
2611 do_cleanups (cleanup);
2612 }
2613
2614 /* The hash function for strings in the mapped index. This is the same as
2615 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2616 implementation. This is necessary because the hash function is tied to the
2617 format of the mapped index file. The hash values do not have to match with
2618 SYMBOL_HASH_NEXT.
2619
2620 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2621
2622 static hashval_t
2623 mapped_index_string_hash (int index_version, const void *p)
2624 {
2625 const unsigned char *str = (const unsigned char *) p;
2626 hashval_t r = 0;
2627 unsigned char c;
2628
2629 while ((c = *str++) != 0)
2630 {
2631 if (index_version >= 5)
2632 c = tolower (c);
2633 r = r * 67 + c - 113;
2634 }
2635
2636 return r;
2637 }
2638
2639 /* Find a slot in the mapped index INDEX for the object named NAME.
2640 If NAME is found, set *VEC_OUT to point to the CU vector in the
2641 constant pool and return 1. If NAME cannot be found, return 0. */
2642
2643 static int
2644 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2645 offset_type **vec_out)
2646 {
2647 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2648 offset_type hash;
2649 offset_type slot, step;
2650 int (*cmp) (const char *, const char *);
2651
2652 if (current_language->la_language == language_cplus
2653 || current_language->la_language == language_java
2654 || current_language->la_language == language_fortran)
2655 {
2656 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2657 not contain any. */
2658 const char *paren = strchr (name, '(');
2659
2660 if (paren)
2661 {
2662 char *dup;
2663
2664 dup = xmalloc (paren - name + 1);
2665 memcpy (dup, name, paren - name);
2666 dup[paren - name] = 0;
2667
2668 make_cleanup (xfree, dup);
2669 name = dup;
2670 }
2671 }
2672
2673 /* Index version 4 did not support case insensitive searches. But the
2674 indices for case insensitive languages are built in lowercase, therefore
2675 simulate our NAME being searched is also lowercased. */
2676 hash = mapped_index_string_hash ((index->version == 4
2677 && case_sensitivity == case_sensitive_off
2678 ? 5 : index->version),
2679 name);
2680
2681 slot = hash & (index->symbol_table_slots - 1);
2682 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2683 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2684
2685 for (;;)
2686 {
2687 /* Convert a slot number to an offset into the table. */
2688 offset_type i = 2 * slot;
2689 const char *str;
2690 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2691 {
2692 do_cleanups (back_to);
2693 return 0;
2694 }
2695
2696 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2697 if (!cmp (name, str))
2698 {
2699 *vec_out = (offset_type *) (index->constant_pool
2700 + MAYBE_SWAP (index->symbol_table[i + 1]));
2701 do_cleanups (back_to);
2702 return 1;
2703 }
2704
2705 slot = (slot + step) & (index->symbol_table_slots - 1);
2706 }
2707 }
2708
2709 /* A helper function that reads the .gdb_index from SECTION and fills
2710 in MAP. FILENAME is the name of the file containing the section;
2711 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2712 ok to use deprecated sections.
2713
2714 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2715 out parameters that are filled in with information about the CU and
2716 TU lists in the section.
2717
2718 Returns 1 if all went well, 0 otherwise. */
2719
2720 static int
2721 read_index_from_section (struct objfile *objfile,
2722 const char *filename,
2723 int deprecated_ok,
2724 struct dwarf2_section_info *section,
2725 struct mapped_index *map,
2726 const gdb_byte **cu_list,
2727 offset_type *cu_list_elements,
2728 const gdb_byte **types_list,
2729 offset_type *types_list_elements)
2730 {
2731 const gdb_byte *addr;
2732 offset_type version;
2733 offset_type *metadata;
2734 int i;
2735
2736 if (dwarf2_section_empty_p (section))
2737 return 0;
2738
2739 /* Older elfutils strip versions could keep the section in the main
2740 executable while splitting it for the separate debug info file. */
2741 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
2742 return 0;
2743
2744 dwarf2_read_section (objfile, section);
2745
2746 addr = section->buffer;
2747 /* Version check. */
2748 version = MAYBE_SWAP (*(offset_type *) addr);
2749 /* Versions earlier than 3 emitted every copy of a psymbol. This
2750 causes the index to behave very poorly for certain requests. Version 3
2751 contained incomplete addrmap. So, it seems better to just ignore such
2752 indices. */
2753 if (version < 4)
2754 {
2755 static int warning_printed = 0;
2756 if (!warning_printed)
2757 {
2758 warning (_("Skipping obsolete .gdb_index section in %s."),
2759 filename);
2760 warning_printed = 1;
2761 }
2762 return 0;
2763 }
2764 /* Index version 4 uses a different hash function than index version
2765 5 and later.
2766
2767 Versions earlier than 6 did not emit psymbols for inlined
2768 functions. Using these files will cause GDB not to be able to
2769 set breakpoints on inlined functions by name, so we ignore these
2770 indices unless the user has done
2771 "set use-deprecated-index-sections on". */
2772 if (version < 6 && !deprecated_ok)
2773 {
2774 static int warning_printed = 0;
2775 if (!warning_printed)
2776 {
2777 warning (_("\
2778 Skipping deprecated .gdb_index section in %s.\n\
2779 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2780 to use the section anyway."),
2781 filename);
2782 warning_printed = 1;
2783 }
2784 return 0;
2785 }
2786 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2787 of the TU (for symbols coming from TUs). It's just a performance bug, and
2788 we can't distinguish gdb-generated indices from gold-generated ones, so
2789 nothing to do here. */
2790
2791 /* Indexes with higher version than the one supported by GDB may be no
2792 longer backward compatible. */
2793 if (version > 8)
2794 return 0;
2795
2796 map->version = version;
2797 map->total_size = section->size;
2798
2799 metadata = (offset_type *) (addr + sizeof (offset_type));
2800
2801 i = 0;
2802 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2803 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2804 / 8);
2805 ++i;
2806
2807 *types_list = addr + MAYBE_SWAP (metadata[i]);
2808 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2809 - MAYBE_SWAP (metadata[i]))
2810 / 8);
2811 ++i;
2812
2813 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2814 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2815 - MAYBE_SWAP (metadata[i]));
2816 ++i;
2817
2818 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2819 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2820 - MAYBE_SWAP (metadata[i]))
2821 / (2 * sizeof (offset_type)));
2822 ++i;
2823
2824 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2825
2826 return 1;
2827 }
2828
2829
2830 /* Read the index file. If everything went ok, initialize the "quick"
2831 elements of all the CUs and return 1. Otherwise, return 0. */
2832
2833 static int
2834 dwarf2_read_index (struct objfile *objfile)
2835 {
2836 struct mapped_index local_map, *map;
2837 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2838 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2839 struct dwz_file *dwz;
2840
2841 if (!read_index_from_section (objfile, objfile_name (objfile),
2842 use_deprecated_index_sections,
2843 &dwarf2_per_objfile->gdb_index, &local_map,
2844 &cu_list, &cu_list_elements,
2845 &types_list, &types_list_elements))
2846 return 0;
2847
2848 /* Don't use the index if it's empty. */
2849 if (local_map.symbol_table_slots == 0)
2850 return 0;
2851
2852 /* If there is a .dwz file, read it so we can get its CU list as
2853 well. */
2854 dwz = dwarf2_get_dwz_file ();
2855 if (dwz != NULL)
2856 {
2857 struct mapped_index dwz_map;
2858 const gdb_byte *dwz_types_ignore;
2859 offset_type dwz_types_elements_ignore;
2860
2861 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2862 1,
2863 &dwz->gdb_index, &dwz_map,
2864 &dwz_list, &dwz_list_elements,
2865 &dwz_types_ignore,
2866 &dwz_types_elements_ignore))
2867 {
2868 warning (_("could not read '.gdb_index' section from %s; skipping"),
2869 bfd_get_filename (dwz->dwz_bfd));
2870 return 0;
2871 }
2872 }
2873
2874 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2875 dwz_list_elements);
2876
2877 if (types_list_elements)
2878 {
2879 struct dwarf2_section_info *section;
2880
2881 /* We can only handle a single .debug_types when we have an
2882 index. */
2883 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2884 return 0;
2885
2886 section = VEC_index (dwarf2_section_info_def,
2887 dwarf2_per_objfile->types, 0);
2888
2889 create_signatured_type_table_from_index (objfile, section, types_list,
2890 types_list_elements);
2891 }
2892
2893 create_addrmap_from_index (objfile, &local_map);
2894
2895 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2896 *map = local_map;
2897
2898 dwarf2_per_objfile->index_table = map;
2899 dwarf2_per_objfile->using_index = 1;
2900 dwarf2_per_objfile->quick_file_names_table =
2901 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2902
2903 return 1;
2904 }
2905
2906 /* A helper for the "quick" functions which sets the global
2907 dwarf2_per_objfile according to OBJFILE. */
2908
2909 static void
2910 dw2_setup (struct objfile *objfile)
2911 {
2912 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2913 gdb_assert (dwarf2_per_objfile);
2914 }
2915
2916 /* die_reader_func for dw2_get_file_names. */
2917
2918 static void
2919 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2920 const gdb_byte *info_ptr,
2921 struct die_info *comp_unit_die,
2922 int has_children,
2923 void *data)
2924 {
2925 struct dwarf2_cu *cu = reader->cu;
2926 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2927 struct objfile *objfile = dwarf2_per_objfile->objfile;
2928 struct dwarf2_per_cu_data *lh_cu;
2929 struct line_header *lh;
2930 struct attribute *attr;
2931 int i;
2932 const char *name, *comp_dir;
2933 void **slot;
2934 struct quick_file_names *qfn;
2935 unsigned int line_offset;
2936
2937 gdb_assert (! this_cu->is_debug_types);
2938
2939 /* Our callers never want to match partial units -- instead they
2940 will match the enclosing full CU. */
2941 if (comp_unit_die->tag == DW_TAG_partial_unit)
2942 {
2943 this_cu->v.quick->no_file_data = 1;
2944 return;
2945 }
2946
2947 lh_cu = this_cu;
2948 lh = NULL;
2949 slot = NULL;
2950 line_offset = 0;
2951
2952 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2953 if (attr)
2954 {
2955 struct quick_file_names find_entry;
2956
2957 line_offset = DW_UNSND (attr);
2958
2959 /* We may have already read in this line header (TU line header sharing).
2960 If we have we're done. */
2961 find_entry.hash.dwo_unit = cu->dwo_unit;
2962 find_entry.hash.line_offset.sect_off = line_offset;
2963 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2964 &find_entry, INSERT);
2965 if (*slot != NULL)
2966 {
2967 lh_cu->v.quick->file_names = *slot;
2968 return;
2969 }
2970
2971 lh = dwarf_decode_line_header (line_offset, cu);
2972 }
2973 if (lh == NULL)
2974 {
2975 lh_cu->v.quick->no_file_data = 1;
2976 return;
2977 }
2978
2979 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2980 qfn->hash.dwo_unit = cu->dwo_unit;
2981 qfn->hash.line_offset.sect_off = line_offset;
2982 gdb_assert (slot != NULL);
2983 *slot = qfn;
2984
2985 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2986
2987 qfn->num_file_names = lh->num_file_names;
2988 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2989 lh->num_file_names * sizeof (char *));
2990 for (i = 0; i < lh->num_file_names; ++i)
2991 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2992 qfn->real_names = NULL;
2993
2994 free_line_header (lh);
2995
2996 lh_cu->v.quick->file_names = qfn;
2997 }
2998
2999 /* A helper for the "quick" functions which attempts to read the line
3000 table for THIS_CU. */
3001
3002 static struct quick_file_names *
3003 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3004 {
3005 /* This should never be called for TUs. */
3006 gdb_assert (! this_cu->is_debug_types);
3007 /* Nor type unit groups. */
3008 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3009
3010 if (this_cu->v.quick->file_names != NULL)
3011 return this_cu->v.quick->file_names;
3012 /* If we know there is no line data, no point in looking again. */
3013 if (this_cu->v.quick->no_file_data)
3014 return NULL;
3015
3016 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3017
3018 if (this_cu->v.quick->no_file_data)
3019 return NULL;
3020 return this_cu->v.quick->file_names;
3021 }
3022
3023 /* A helper for the "quick" functions which computes and caches the
3024 real path for a given file name from the line table. */
3025
3026 static const char *
3027 dw2_get_real_path (struct objfile *objfile,
3028 struct quick_file_names *qfn, int index)
3029 {
3030 if (qfn->real_names == NULL)
3031 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3032 qfn->num_file_names, sizeof (char *));
3033
3034 if (qfn->real_names[index] == NULL)
3035 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
3036
3037 return qfn->real_names[index];
3038 }
3039
3040 static struct symtab *
3041 dw2_find_last_source_symtab (struct objfile *objfile)
3042 {
3043 int index;
3044
3045 dw2_setup (objfile);
3046 index = dwarf2_per_objfile->n_comp_units - 1;
3047 return dw2_instantiate_symtab (dw2_get_cu (index));
3048 }
3049
3050 /* Traversal function for dw2_forget_cached_source_info. */
3051
3052 static int
3053 dw2_free_cached_file_names (void **slot, void *info)
3054 {
3055 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3056
3057 if (file_data->real_names)
3058 {
3059 int i;
3060
3061 for (i = 0; i < file_data->num_file_names; ++i)
3062 {
3063 xfree ((void*) file_data->real_names[i]);
3064 file_data->real_names[i] = NULL;
3065 }
3066 }
3067
3068 return 1;
3069 }
3070
3071 static void
3072 dw2_forget_cached_source_info (struct objfile *objfile)
3073 {
3074 dw2_setup (objfile);
3075
3076 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3077 dw2_free_cached_file_names, NULL);
3078 }
3079
3080 /* Helper function for dw2_map_symtabs_matching_filename that expands
3081 the symtabs and calls the iterator. */
3082
3083 static int
3084 dw2_map_expand_apply (struct objfile *objfile,
3085 struct dwarf2_per_cu_data *per_cu,
3086 const char *name, const char *real_path,
3087 int (*callback) (struct symtab *, void *),
3088 void *data)
3089 {
3090 struct symtab *last_made = objfile->symtabs;
3091
3092 /* Don't visit already-expanded CUs. */
3093 if (per_cu->v.quick->symtab)
3094 return 0;
3095
3096 /* This may expand more than one symtab, and we want to iterate over
3097 all of them. */
3098 dw2_instantiate_symtab (per_cu);
3099
3100 return iterate_over_some_symtabs (name, real_path, callback, data,
3101 objfile->symtabs, last_made);
3102 }
3103
3104 /* Implementation of the map_symtabs_matching_filename method. */
3105
3106 static int
3107 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3108 const char *real_path,
3109 int (*callback) (struct symtab *, void *),
3110 void *data)
3111 {
3112 int i;
3113 const char *name_basename = lbasename (name);
3114
3115 dw2_setup (objfile);
3116
3117 /* The rule is CUs specify all the files, including those used by
3118 any TU, so there's no need to scan TUs here. */
3119
3120 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3121 {
3122 int j;
3123 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3124 struct quick_file_names *file_data;
3125
3126 /* We only need to look at symtabs not already expanded. */
3127 if (per_cu->v.quick->symtab)
3128 continue;
3129
3130 file_data = dw2_get_file_names (per_cu);
3131 if (file_data == NULL)
3132 continue;
3133
3134 for (j = 0; j < file_data->num_file_names; ++j)
3135 {
3136 const char *this_name = file_data->file_names[j];
3137 const char *this_real_name;
3138
3139 if (compare_filenames_for_search (this_name, name))
3140 {
3141 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3142 callback, data))
3143 return 1;
3144 continue;
3145 }
3146
3147 /* Before we invoke realpath, which can get expensive when many
3148 files are involved, do a quick comparison of the basenames. */
3149 if (! basenames_may_differ
3150 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3151 continue;
3152
3153 this_real_name = dw2_get_real_path (objfile, file_data, j);
3154 if (compare_filenames_for_search (this_real_name, name))
3155 {
3156 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3157 callback, data))
3158 return 1;
3159 continue;
3160 }
3161
3162 if (real_path != NULL)
3163 {
3164 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3165 gdb_assert (IS_ABSOLUTE_PATH (name));
3166 if (this_real_name != NULL
3167 && FILENAME_CMP (real_path, this_real_name) == 0)
3168 {
3169 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3170 callback, data))
3171 return 1;
3172 continue;
3173 }
3174 }
3175 }
3176 }
3177
3178 return 0;
3179 }
3180
3181 /* Struct used to manage iterating over all CUs looking for a symbol. */
3182
3183 struct dw2_symtab_iterator
3184 {
3185 /* The internalized form of .gdb_index. */
3186 struct mapped_index *index;
3187 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3188 int want_specific_block;
3189 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3190 Unused if !WANT_SPECIFIC_BLOCK. */
3191 int block_index;
3192 /* The kind of symbol we're looking for. */
3193 domain_enum domain;
3194 /* The list of CUs from the index entry of the symbol,
3195 or NULL if not found. */
3196 offset_type *vec;
3197 /* The next element in VEC to look at. */
3198 int next;
3199 /* The number of elements in VEC, or zero if there is no match. */
3200 int length;
3201 };
3202
3203 /* Initialize the index symtab iterator ITER.
3204 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3205 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3206
3207 static void
3208 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3209 struct mapped_index *index,
3210 int want_specific_block,
3211 int block_index,
3212 domain_enum domain,
3213 const char *name)
3214 {
3215 iter->index = index;
3216 iter->want_specific_block = want_specific_block;
3217 iter->block_index = block_index;
3218 iter->domain = domain;
3219 iter->next = 0;
3220
3221 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3222 iter->length = MAYBE_SWAP (*iter->vec);
3223 else
3224 {
3225 iter->vec = NULL;
3226 iter->length = 0;
3227 }
3228 }
3229
3230 /* Return the next matching CU or NULL if there are no more. */
3231
3232 static struct dwarf2_per_cu_data *
3233 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3234 {
3235 for ( ; iter->next < iter->length; ++iter->next)
3236 {
3237 offset_type cu_index_and_attrs =
3238 MAYBE_SWAP (iter->vec[iter->next + 1]);
3239 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3240 struct dwarf2_per_cu_data *per_cu;
3241 int want_static = iter->block_index != GLOBAL_BLOCK;
3242 /* This value is only valid for index versions >= 7. */
3243 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3244 gdb_index_symbol_kind symbol_kind =
3245 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3246 /* Only check the symbol attributes if they're present.
3247 Indices prior to version 7 don't record them,
3248 and indices >= 7 may elide them for certain symbols
3249 (gold does this). */
3250 int attrs_valid =
3251 (iter->index->version >= 7
3252 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3253
3254 /* Don't crash on bad data. */
3255 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3256 + dwarf2_per_objfile->n_type_units))
3257 {
3258 complaint (&symfile_complaints,
3259 _(".gdb_index entry has bad CU index"
3260 " [in module %s]"),
3261 objfile_name (dwarf2_per_objfile->objfile));
3262 continue;
3263 }
3264
3265 per_cu = dw2_get_cu (cu_index);
3266
3267 /* Skip if already read in. */
3268 if (per_cu->v.quick->symtab)
3269 continue;
3270
3271 if (attrs_valid
3272 && iter->want_specific_block
3273 && want_static != is_static)
3274 continue;
3275
3276 /* Only check the symbol's kind if it has one. */
3277 if (attrs_valid)
3278 {
3279 switch (iter->domain)
3280 {
3281 case VAR_DOMAIN:
3282 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3283 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3284 /* Some types are also in VAR_DOMAIN. */
3285 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3286 continue;
3287 break;
3288 case STRUCT_DOMAIN:
3289 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3290 continue;
3291 break;
3292 case LABEL_DOMAIN:
3293 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3294 continue;
3295 break;
3296 default:
3297 break;
3298 }
3299 }
3300
3301 ++iter->next;
3302 return per_cu;
3303 }
3304
3305 return NULL;
3306 }
3307
3308 static struct symtab *
3309 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3310 const char *name, domain_enum domain)
3311 {
3312 struct symtab *stab_best = NULL;
3313 struct mapped_index *index;
3314
3315 dw2_setup (objfile);
3316
3317 index = dwarf2_per_objfile->index_table;
3318
3319 /* index is NULL if OBJF_READNOW. */
3320 if (index)
3321 {
3322 struct dw2_symtab_iterator iter;
3323 struct dwarf2_per_cu_data *per_cu;
3324
3325 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3326
3327 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3328 {
3329 struct symbol *sym = NULL;
3330 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3331
3332 /* Some caution must be observed with overloaded functions
3333 and methods, since the index will not contain any overload
3334 information (but NAME might contain it). */
3335 if (stab->primary)
3336 {
3337 struct blockvector *bv = BLOCKVECTOR (stab);
3338 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3339
3340 sym = lookup_block_symbol (block, name, domain);
3341 }
3342
3343 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3344 {
3345 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3346 return stab;
3347
3348 stab_best = stab;
3349 }
3350
3351 /* Keep looking through other CUs. */
3352 }
3353 }
3354
3355 return stab_best;
3356 }
3357
3358 static void
3359 dw2_print_stats (struct objfile *objfile)
3360 {
3361 int i, total, count;
3362
3363 dw2_setup (objfile);
3364 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
3365 count = 0;
3366 for (i = 0; i < total; ++i)
3367 {
3368 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3369
3370 if (!per_cu->v.quick->symtab)
3371 ++count;
3372 }
3373 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3374 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3375 }
3376
3377 /* This dumps minimal information about the index.
3378 It is called via "mt print objfiles".
3379 One use is to verify .gdb_index has been loaded by the
3380 gdb.dwarf2/gdb-index.exp testcase. */
3381
3382 static void
3383 dw2_dump (struct objfile *objfile)
3384 {
3385 dw2_setup (objfile);
3386 gdb_assert (dwarf2_per_objfile->using_index);
3387 printf_filtered (".gdb_index:");
3388 if (dwarf2_per_objfile->index_table != NULL)
3389 {
3390 printf_filtered (" version %d\n",
3391 dwarf2_per_objfile->index_table->version);
3392 }
3393 else
3394 printf_filtered (" faked for \"readnow\"\n");
3395 printf_filtered ("\n");
3396 }
3397
3398 static void
3399 dw2_relocate (struct objfile *objfile,
3400 const struct section_offsets *new_offsets,
3401 const struct section_offsets *delta)
3402 {
3403 /* There's nothing to relocate here. */
3404 }
3405
3406 static void
3407 dw2_expand_symtabs_for_function (struct objfile *objfile,
3408 const char *func_name)
3409 {
3410 struct mapped_index *index;
3411
3412 dw2_setup (objfile);
3413
3414 index = dwarf2_per_objfile->index_table;
3415
3416 /* index is NULL if OBJF_READNOW. */
3417 if (index)
3418 {
3419 struct dw2_symtab_iterator iter;
3420 struct dwarf2_per_cu_data *per_cu;
3421
3422 /* Note: It doesn't matter what we pass for block_index here. */
3423 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3424 func_name);
3425
3426 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3427 dw2_instantiate_symtab (per_cu);
3428 }
3429 }
3430
3431 static void
3432 dw2_expand_all_symtabs (struct objfile *objfile)
3433 {
3434 int i;
3435
3436 dw2_setup (objfile);
3437
3438 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3439 + dwarf2_per_objfile->n_type_units); ++i)
3440 {
3441 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3442
3443 dw2_instantiate_symtab (per_cu);
3444 }
3445 }
3446
3447 static void
3448 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3449 const char *fullname)
3450 {
3451 int i;
3452
3453 dw2_setup (objfile);
3454
3455 /* We don't need to consider type units here.
3456 This is only called for examining code, e.g. expand_line_sal.
3457 There can be an order of magnitude (or more) more type units
3458 than comp units, and we avoid them if we can. */
3459
3460 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3461 {
3462 int j;
3463 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3464 struct quick_file_names *file_data;
3465
3466 /* We only need to look at symtabs not already expanded. */
3467 if (per_cu->v.quick->symtab)
3468 continue;
3469
3470 file_data = dw2_get_file_names (per_cu);
3471 if (file_data == NULL)
3472 continue;
3473
3474 for (j = 0; j < file_data->num_file_names; ++j)
3475 {
3476 const char *this_fullname = file_data->file_names[j];
3477
3478 if (filename_cmp (this_fullname, fullname) == 0)
3479 {
3480 dw2_instantiate_symtab (per_cu);
3481 break;
3482 }
3483 }
3484 }
3485 }
3486
3487 static void
3488 dw2_map_matching_symbols (struct objfile *objfile,
3489 const char * name, domain_enum namespace,
3490 int global,
3491 int (*callback) (struct block *,
3492 struct symbol *, void *),
3493 void *data, symbol_compare_ftype *match,
3494 symbol_compare_ftype *ordered_compare)
3495 {
3496 /* Currently unimplemented; used for Ada. The function can be called if the
3497 current language is Ada for a non-Ada objfile using GNU index. As Ada
3498 does not look for non-Ada symbols this function should just return. */
3499 }
3500
3501 static void
3502 dw2_expand_symtabs_matching
3503 (struct objfile *objfile,
3504 int (*file_matcher) (const char *, void *, int basenames),
3505 int (*name_matcher) (const char *, void *),
3506 enum search_domain kind,
3507 void *data)
3508 {
3509 int i;
3510 offset_type iter;
3511 struct mapped_index *index;
3512
3513 dw2_setup (objfile);
3514
3515 /* index_table is NULL if OBJF_READNOW. */
3516 if (!dwarf2_per_objfile->index_table)
3517 return;
3518 index = dwarf2_per_objfile->index_table;
3519
3520 if (file_matcher != NULL)
3521 {
3522 struct cleanup *cleanup;
3523 htab_t visited_found, visited_not_found;
3524
3525 visited_found = htab_create_alloc (10,
3526 htab_hash_pointer, htab_eq_pointer,
3527 NULL, xcalloc, xfree);
3528 cleanup = make_cleanup_htab_delete (visited_found);
3529 visited_not_found = htab_create_alloc (10,
3530 htab_hash_pointer, htab_eq_pointer,
3531 NULL, xcalloc, xfree);
3532 make_cleanup_htab_delete (visited_not_found);
3533
3534 /* The rule is CUs specify all the files, including those used by
3535 any TU, so there's no need to scan TUs here. */
3536
3537 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3538 {
3539 int j;
3540 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3541 struct quick_file_names *file_data;
3542 void **slot;
3543
3544 per_cu->v.quick->mark = 0;
3545
3546 /* We only need to look at symtabs not already expanded. */
3547 if (per_cu->v.quick->symtab)
3548 continue;
3549
3550 file_data = dw2_get_file_names (per_cu);
3551 if (file_data == NULL)
3552 continue;
3553
3554 if (htab_find (visited_not_found, file_data) != NULL)
3555 continue;
3556 else if (htab_find (visited_found, file_data) != NULL)
3557 {
3558 per_cu->v.quick->mark = 1;
3559 continue;
3560 }
3561
3562 for (j = 0; j < file_data->num_file_names; ++j)
3563 {
3564 const char *this_real_name;
3565
3566 if (file_matcher (file_data->file_names[j], data, 0))
3567 {
3568 per_cu->v.quick->mark = 1;
3569 break;
3570 }
3571
3572 /* Before we invoke realpath, which can get expensive when many
3573 files are involved, do a quick comparison of the basenames. */
3574 if (!basenames_may_differ
3575 && !file_matcher (lbasename (file_data->file_names[j]),
3576 data, 1))
3577 continue;
3578
3579 this_real_name = dw2_get_real_path (objfile, file_data, j);
3580 if (file_matcher (this_real_name, data, 0))
3581 {
3582 per_cu->v.quick->mark = 1;
3583 break;
3584 }
3585 }
3586
3587 slot = htab_find_slot (per_cu->v.quick->mark
3588 ? visited_found
3589 : visited_not_found,
3590 file_data, INSERT);
3591 *slot = file_data;
3592 }
3593
3594 do_cleanups (cleanup);
3595 }
3596
3597 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3598 {
3599 offset_type idx = 2 * iter;
3600 const char *name;
3601 offset_type *vec, vec_len, vec_idx;
3602
3603 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3604 continue;
3605
3606 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3607
3608 if (! (*name_matcher) (name, data))
3609 continue;
3610
3611 /* The name was matched, now expand corresponding CUs that were
3612 marked. */
3613 vec = (offset_type *) (index->constant_pool
3614 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3615 vec_len = MAYBE_SWAP (vec[0]);
3616 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3617 {
3618 struct dwarf2_per_cu_data *per_cu;
3619 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3620 gdb_index_symbol_kind symbol_kind =
3621 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3622 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3623 /* Only check the symbol attributes if they're present.
3624 Indices prior to version 7 don't record them,
3625 and indices >= 7 may elide them for certain symbols
3626 (gold does this). */
3627 int attrs_valid =
3628 (index->version >= 7
3629 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3630
3631 /* Only check the symbol's kind if it has one. */
3632 if (attrs_valid)
3633 {
3634 switch (kind)
3635 {
3636 case VARIABLES_DOMAIN:
3637 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3638 continue;
3639 break;
3640 case FUNCTIONS_DOMAIN:
3641 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3642 continue;
3643 break;
3644 case TYPES_DOMAIN:
3645 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3646 continue;
3647 break;
3648 default:
3649 break;
3650 }
3651 }
3652
3653 /* Don't crash on bad data. */
3654 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3655 + dwarf2_per_objfile->n_type_units))
3656 {
3657 complaint (&symfile_complaints,
3658 _(".gdb_index entry has bad CU index"
3659 " [in module %s]"), objfile_name (objfile));
3660 continue;
3661 }
3662
3663 per_cu = dw2_get_cu (cu_index);
3664 if (file_matcher == NULL || per_cu->v.quick->mark)
3665 dw2_instantiate_symtab (per_cu);
3666 }
3667 }
3668 }
3669
3670 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3671 symtab. */
3672
3673 static struct symtab *
3674 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3675 {
3676 int i;
3677
3678 if (BLOCKVECTOR (symtab) != NULL
3679 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3680 return symtab;
3681
3682 if (symtab->includes == NULL)
3683 return NULL;
3684
3685 for (i = 0; symtab->includes[i]; ++i)
3686 {
3687 struct symtab *s = symtab->includes[i];
3688
3689 s = recursively_find_pc_sect_symtab (s, pc);
3690 if (s != NULL)
3691 return s;
3692 }
3693
3694 return NULL;
3695 }
3696
3697 static struct symtab *
3698 dw2_find_pc_sect_symtab (struct objfile *objfile,
3699 struct minimal_symbol *msymbol,
3700 CORE_ADDR pc,
3701 struct obj_section *section,
3702 int warn_if_readin)
3703 {
3704 struct dwarf2_per_cu_data *data;
3705 struct symtab *result;
3706
3707 dw2_setup (objfile);
3708
3709 if (!objfile->psymtabs_addrmap)
3710 return NULL;
3711
3712 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3713 if (!data)
3714 return NULL;
3715
3716 if (warn_if_readin && data->v.quick->symtab)
3717 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3718 paddress (get_objfile_arch (objfile), pc));
3719
3720 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3721 gdb_assert (result != NULL);
3722 return result;
3723 }
3724
3725 static void
3726 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3727 void *data, int need_fullname)
3728 {
3729 int i;
3730 struct cleanup *cleanup;
3731 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3732 NULL, xcalloc, xfree);
3733
3734 cleanup = make_cleanup_htab_delete (visited);
3735 dw2_setup (objfile);
3736
3737 /* The rule is CUs specify all the files, including those used by
3738 any TU, so there's no need to scan TUs here.
3739 We can ignore file names coming from already-expanded CUs. */
3740
3741 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3742 {
3743 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3744
3745 if (per_cu->v.quick->symtab)
3746 {
3747 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3748 INSERT);
3749
3750 *slot = per_cu->v.quick->file_names;
3751 }
3752 }
3753
3754 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3755 {
3756 int j;
3757 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3758 struct quick_file_names *file_data;
3759 void **slot;
3760
3761 /* We only need to look at symtabs not already expanded. */
3762 if (per_cu->v.quick->symtab)
3763 continue;
3764
3765 file_data = dw2_get_file_names (per_cu);
3766 if (file_data == NULL)
3767 continue;
3768
3769 slot = htab_find_slot (visited, file_data, INSERT);
3770 if (*slot)
3771 {
3772 /* Already visited. */
3773 continue;
3774 }
3775 *slot = file_data;
3776
3777 for (j = 0; j < file_data->num_file_names; ++j)
3778 {
3779 const char *this_real_name;
3780
3781 if (need_fullname)
3782 this_real_name = dw2_get_real_path (objfile, file_data, j);
3783 else
3784 this_real_name = NULL;
3785 (*fun) (file_data->file_names[j], this_real_name, data);
3786 }
3787 }
3788
3789 do_cleanups (cleanup);
3790 }
3791
3792 static int
3793 dw2_has_symbols (struct objfile *objfile)
3794 {
3795 return 1;
3796 }
3797
3798 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3799 {
3800 dw2_has_symbols,
3801 dw2_find_last_source_symtab,
3802 dw2_forget_cached_source_info,
3803 dw2_map_symtabs_matching_filename,
3804 dw2_lookup_symbol,
3805 dw2_print_stats,
3806 dw2_dump,
3807 dw2_relocate,
3808 dw2_expand_symtabs_for_function,
3809 dw2_expand_all_symtabs,
3810 dw2_expand_symtabs_with_fullname,
3811 dw2_map_matching_symbols,
3812 dw2_expand_symtabs_matching,
3813 dw2_find_pc_sect_symtab,
3814 dw2_map_symbol_filenames
3815 };
3816
3817 /* Initialize for reading DWARF for this objfile. Return 0 if this
3818 file will use psymtabs, or 1 if using the GNU index. */
3819
3820 int
3821 dwarf2_initialize_objfile (struct objfile *objfile)
3822 {
3823 /* If we're about to read full symbols, don't bother with the
3824 indices. In this case we also don't care if some other debug
3825 format is making psymtabs, because they are all about to be
3826 expanded anyway. */
3827 if ((objfile->flags & OBJF_READNOW))
3828 {
3829 int i;
3830
3831 dwarf2_per_objfile->using_index = 1;
3832 create_all_comp_units (objfile);
3833 create_all_type_units (objfile);
3834 dwarf2_per_objfile->quick_file_names_table =
3835 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3836
3837 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3838 + dwarf2_per_objfile->n_type_units); ++i)
3839 {
3840 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3841
3842 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3843 struct dwarf2_per_cu_quick_data);
3844 }
3845
3846 /* Return 1 so that gdb sees the "quick" functions. However,
3847 these functions will be no-ops because we will have expanded
3848 all symtabs. */
3849 return 1;
3850 }
3851
3852 if (dwarf2_read_index (objfile))
3853 return 1;
3854
3855 return 0;
3856 }
3857
3858 \f
3859
3860 /* Build a partial symbol table. */
3861
3862 void
3863 dwarf2_build_psymtabs (struct objfile *objfile)
3864 {
3865 volatile struct gdb_exception except;
3866
3867 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3868 {
3869 init_psymbol_list (objfile, 1024);
3870 }
3871
3872 TRY_CATCH (except, RETURN_MASK_ERROR)
3873 {
3874 /* This isn't really ideal: all the data we allocate on the
3875 objfile's obstack is still uselessly kept around. However,
3876 freeing it seems unsafe. */
3877 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3878
3879 dwarf2_build_psymtabs_hard (objfile);
3880 discard_cleanups (cleanups);
3881 }
3882 if (except.reason < 0)
3883 exception_print (gdb_stderr, except);
3884 }
3885
3886 /* Return the total length of the CU described by HEADER. */
3887
3888 static unsigned int
3889 get_cu_length (const struct comp_unit_head *header)
3890 {
3891 return header->initial_length_size + header->length;
3892 }
3893
3894 /* Return TRUE if OFFSET is within CU_HEADER. */
3895
3896 static inline int
3897 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3898 {
3899 sect_offset bottom = { cu_header->offset.sect_off };
3900 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
3901
3902 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3903 }
3904
3905 /* Find the base address of the compilation unit for range lists and
3906 location lists. It will normally be specified by DW_AT_low_pc.
3907 In DWARF-3 draft 4, the base address could be overridden by
3908 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3909 compilation units with discontinuous ranges. */
3910
3911 static void
3912 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3913 {
3914 struct attribute *attr;
3915
3916 cu->base_known = 0;
3917 cu->base_address = 0;
3918
3919 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3920 if (attr)
3921 {
3922 cu->base_address = DW_ADDR (attr);
3923 cu->base_known = 1;
3924 }
3925 else
3926 {
3927 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3928 if (attr)
3929 {
3930 cu->base_address = DW_ADDR (attr);
3931 cu->base_known = 1;
3932 }
3933 }
3934 }
3935
3936 /* Read in the comp unit header information from the debug_info at info_ptr.
3937 NOTE: This leaves members offset, first_die_offset to be filled in
3938 by the caller. */
3939
3940 static const gdb_byte *
3941 read_comp_unit_head (struct comp_unit_head *cu_header,
3942 const gdb_byte *info_ptr, bfd *abfd)
3943 {
3944 int signed_addr;
3945 unsigned int bytes_read;
3946
3947 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3948 cu_header->initial_length_size = bytes_read;
3949 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3950 info_ptr += bytes_read;
3951 cu_header->version = read_2_bytes (abfd, info_ptr);
3952 info_ptr += 2;
3953 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3954 &bytes_read);
3955 info_ptr += bytes_read;
3956 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3957 info_ptr += 1;
3958 signed_addr = bfd_get_sign_extend_vma (abfd);
3959 if (signed_addr < 0)
3960 internal_error (__FILE__, __LINE__,
3961 _("read_comp_unit_head: dwarf from non elf file"));
3962 cu_header->signed_addr_p = signed_addr;
3963
3964 return info_ptr;
3965 }
3966
3967 /* Helper function that returns the proper abbrev section for
3968 THIS_CU. */
3969
3970 static struct dwarf2_section_info *
3971 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3972 {
3973 struct dwarf2_section_info *abbrev;
3974
3975 if (this_cu->is_dwz)
3976 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3977 else
3978 abbrev = &dwarf2_per_objfile->abbrev;
3979
3980 return abbrev;
3981 }
3982
3983 /* Subroutine of read_and_check_comp_unit_head and
3984 read_and_check_type_unit_head to simplify them.
3985 Perform various error checking on the header. */
3986
3987 static void
3988 error_check_comp_unit_head (struct comp_unit_head *header,
3989 struct dwarf2_section_info *section,
3990 struct dwarf2_section_info *abbrev_section)
3991 {
3992 bfd *abfd = section->asection->owner;
3993 const char *filename = bfd_get_filename (abfd);
3994
3995 if (header->version != 2 && header->version != 3 && header->version != 4)
3996 error (_("Dwarf Error: wrong version in compilation unit header "
3997 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3998 filename);
3999
4000 if (header->abbrev_offset.sect_off
4001 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
4002 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
4003 "(offset 0x%lx + 6) [in module %s]"),
4004 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
4005 filename);
4006
4007 /* Cast to unsigned long to use 64-bit arithmetic when possible to
4008 avoid potential 32-bit overflow. */
4009 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
4010 > section->size)
4011 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
4012 "(offset 0x%lx + 0) [in module %s]"),
4013 (long) header->length, (long) header->offset.sect_off,
4014 filename);
4015 }
4016
4017 /* Read in a CU/TU header and perform some basic error checking.
4018 The contents of the header are stored in HEADER.
4019 The result is a pointer to the start of the first DIE. */
4020
4021 static const gdb_byte *
4022 read_and_check_comp_unit_head (struct comp_unit_head *header,
4023 struct dwarf2_section_info *section,
4024 struct dwarf2_section_info *abbrev_section,
4025 const gdb_byte *info_ptr,
4026 int is_debug_types_section)
4027 {
4028 const gdb_byte *beg_of_comp_unit = info_ptr;
4029 bfd *abfd = section->asection->owner;
4030
4031 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4032
4033 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4034
4035 /* If we're reading a type unit, skip over the signature and
4036 type_offset fields. */
4037 if (is_debug_types_section)
4038 info_ptr += 8 /*signature*/ + header->offset_size;
4039
4040 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4041
4042 error_check_comp_unit_head (header, section, abbrev_section);
4043
4044 return info_ptr;
4045 }
4046
4047 /* Read in the types comp unit header information from .debug_types entry at
4048 types_ptr. The result is a pointer to one past the end of the header. */
4049
4050 static const gdb_byte *
4051 read_and_check_type_unit_head (struct comp_unit_head *header,
4052 struct dwarf2_section_info *section,
4053 struct dwarf2_section_info *abbrev_section,
4054 const gdb_byte *info_ptr,
4055 ULONGEST *signature,
4056 cu_offset *type_offset_in_tu)
4057 {
4058 const gdb_byte *beg_of_comp_unit = info_ptr;
4059 bfd *abfd = section->asection->owner;
4060
4061 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4062
4063 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4064
4065 /* If we're reading a type unit, skip over the signature and
4066 type_offset fields. */
4067 if (signature != NULL)
4068 *signature = read_8_bytes (abfd, info_ptr);
4069 info_ptr += 8;
4070 if (type_offset_in_tu != NULL)
4071 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4072 header->offset_size);
4073 info_ptr += header->offset_size;
4074
4075 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4076
4077 error_check_comp_unit_head (header, section, abbrev_section);
4078
4079 return info_ptr;
4080 }
4081
4082 /* Fetch the abbreviation table offset from a comp or type unit header. */
4083
4084 static sect_offset
4085 read_abbrev_offset (struct dwarf2_section_info *section,
4086 sect_offset offset)
4087 {
4088 bfd *abfd = section->asection->owner;
4089 const gdb_byte *info_ptr;
4090 unsigned int length, initial_length_size, offset_size;
4091 sect_offset abbrev_offset;
4092
4093 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4094 info_ptr = section->buffer + offset.sect_off;
4095 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4096 offset_size = initial_length_size == 4 ? 4 : 8;
4097 info_ptr += initial_length_size + 2 /*version*/;
4098 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4099 return abbrev_offset;
4100 }
4101
4102 /* Allocate a new partial symtab for file named NAME and mark this new
4103 partial symtab as being an include of PST. */
4104
4105 static void
4106 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
4107 struct objfile *objfile)
4108 {
4109 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4110
4111 if (!IS_ABSOLUTE_PATH (subpst->filename))
4112 {
4113 /* It shares objfile->objfile_obstack. */
4114 subpst->dirname = pst->dirname;
4115 }
4116
4117 subpst->section_offsets = pst->section_offsets;
4118 subpst->textlow = 0;
4119 subpst->texthigh = 0;
4120
4121 subpst->dependencies = (struct partial_symtab **)
4122 obstack_alloc (&objfile->objfile_obstack,
4123 sizeof (struct partial_symtab *));
4124 subpst->dependencies[0] = pst;
4125 subpst->number_of_dependencies = 1;
4126
4127 subpst->globals_offset = 0;
4128 subpst->n_global_syms = 0;
4129 subpst->statics_offset = 0;
4130 subpst->n_static_syms = 0;
4131 subpst->symtab = NULL;
4132 subpst->read_symtab = pst->read_symtab;
4133 subpst->readin = 0;
4134
4135 /* No private part is necessary for include psymtabs. This property
4136 can be used to differentiate between such include psymtabs and
4137 the regular ones. */
4138 subpst->read_symtab_private = NULL;
4139 }
4140
4141 /* Read the Line Number Program data and extract the list of files
4142 included by the source file represented by PST. Build an include
4143 partial symtab for each of these included files. */
4144
4145 static void
4146 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4147 struct die_info *die,
4148 struct partial_symtab *pst)
4149 {
4150 struct line_header *lh = NULL;
4151 struct attribute *attr;
4152
4153 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4154 if (attr)
4155 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
4156 if (lh == NULL)
4157 return; /* No linetable, so no includes. */
4158
4159 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4160 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
4161
4162 free_line_header (lh);
4163 }
4164
4165 static hashval_t
4166 hash_signatured_type (const void *item)
4167 {
4168 const struct signatured_type *sig_type = item;
4169
4170 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4171 return sig_type->signature;
4172 }
4173
4174 static int
4175 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4176 {
4177 const struct signatured_type *lhs = item_lhs;
4178 const struct signatured_type *rhs = item_rhs;
4179
4180 return lhs->signature == rhs->signature;
4181 }
4182
4183 /* Allocate a hash table for signatured types. */
4184
4185 static htab_t
4186 allocate_signatured_type_table (struct objfile *objfile)
4187 {
4188 return htab_create_alloc_ex (41,
4189 hash_signatured_type,
4190 eq_signatured_type,
4191 NULL,
4192 &objfile->objfile_obstack,
4193 hashtab_obstack_allocate,
4194 dummy_obstack_deallocate);
4195 }
4196
4197 /* A helper function to add a signatured type CU to a table. */
4198
4199 static int
4200 add_signatured_type_cu_to_table (void **slot, void *datum)
4201 {
4202 struct signatured_type *sigt = *slot;
4203 struct signatured_type ***datap = datum;
4204
4205 **datap = sigt;
4206 ++*datap;
4207
4208 return 1;
4209 }
4210
4211 /* Create the hash table of all entries in the .debug_types
4212 (or .debug_types.dwo) section(s).
4213 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4214 otherwise it is NULL.
4215
4216 The result is a pointer to the hash table or NULL if there are no types.
4217
4218 Note: This function processes DWO files only, not DWP files. */
4219
4220 static htab_t
4221 create_debug_types_hash_table (struct dwo_file *dwo_file,
4222 VEC (dwarf2_section_info_def) *types)
4223 {
4224 struct objfile *objfile = dwarf2_per_objfile->objfile;
4225 htab_t types_htab = NULL;
4226 int ix;
4227 struct dwarf2_section_info *section;
4228 struct dwarf2_section_info *abbrev_section;
4229
4230 if (VEC_empty (dwarf2_section_info_def, types))
4231 return NULL;
4232
4233 abbrev_section = (dwo_file != NULL
4234 ? &dwo_file->sections.abbrev
4235 : &dwarf2_per_objfile->abbrev);
4236
4237 if (dwarf2_read_debug)
4238 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4239 dwo_file ? ".dwo" : "",
4240 bfd_get_filename (abbrev_section->asection->owner));
4241
4242 for (ix = 0;
4243 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4244 ++ix)
4245 {
4246 bfd *abfd;
4247 const gdb_byte *info_ptr, *end_ptr;
4248
4249 dwarf2_read_section (objfile, section);
4250 info_ptr = section->buffer;
4251
4252 if (info_ptr == NULL)
4253 continue;
4254
4255 /* We can't set abfd until now because the section may be empty or
4256 not present, in which case section->asection will be NULL. */
4257 abfd = section->asection->owner;
4258
4259 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4260 because we don't need to read any dies: the signature is in the
4261 header. */
4262
4263 end_ptr = info_ptr + section->size;
4264 while (info_ptr < end_ptr)
4265 {
4266 sect_offset offset;
4267 cu_offset type_offset_in_tu;
4268 ULONGEST signature;
4269 struct signatured_type *sig_type;
4270 struct dwo_unit *dwo_tu;
4271 void **slot;
4272 const gdb_byte *ptr = info_ptr;
4273 struct comp_unit_head header;
4274 unsigned int length;
4275
4276 offset.sect_off = ptr - section->buffer;
4277
4278 /* We need to read the type's signature in order to build the hash
4279 table, but we don't need anything else just yet. */
4280
4281 ptr = read_and_check_type_unit_head (&header, section,
4282 abbrev_section, ptr,
4283 &signature, &type_offset_in_tu);
4284
4285 length = get_cu_length (&header);
4286
4287 /* Skip dummy type units. */
4288 if (ptr >= info_ptr + length
4289 || peek_abbrev_code (abfd, ptr) == 0)
4290 {
4291 info_ptr += length;
4292 continue;
4293 }
4294
4295 if (types_htab == NULL)
4296 {
4297 if (dwo_file)
4298 types_htab = allocate_dwo_unit_table (objfile);
4299 else
4300 types_htab = allocate_signatured_type_table (objfile);
4301 }
4302
4303 if (dwo_file)
4304 {
4305 sig_type = NULL;
4306 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4307 struct dwo_unit);
4308 dwo_tu->dwo_file = dwo_file;
4309 dwo_tu->signature = signature;
4310 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4311 dwo_tu->section = section;
4312 dwo_tu->offset = offset;
4313 dwo_tu->length = length;
4314 }
4315 else
4316 {
4317 /* N.B.: type_offset is not usable if this type uses a DWO file.
4318 The real type_offset is in the DWO file. */
4319 dwo_tu = NULL;
4320 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4321 struct signatured_type);
4322 sig_type->signature = signature;
4323 sig_type->type_offset_in_tu = type_offset_in_tu;
4324 sig_type->per_cu.objfile = objfile;
4325 sig_type->per_cu.is_debug_types = 1;
4326 sig_type->per_cu.section = section;
4327 sig_type->per_cu.offset = offset;
4328 sig_type->per_cu.length = length;
4329 }
4330
4331 slot = htab_find_slot (types_htab,
4332 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4333 INSERT);
4334 gdb_assert (slot != NULL);
4335 if (*slot != NULL)
4336 {
4337 sect_offset dup_offset;
4338
4339 if (dwo_file)
4340 {
4341 const struct dwo_unit *dup_tu = *slot;
4342
4343 dup_offset = dup_tu->offset;
4344 }
4345 else
4346 {
4347 const struct signatured_type *dup_tu = *slot;
4348
4349 dup_offset = dup_tu->per_cu.offset;
4350 }
4351
4352 complaint (&symfile_complaints,
4353 _("debug type entry at offset 0x%x is duplicate to"
4354 " the entry at offset 0x%x, signature %s"),
4355 offset.sect_off, dup_offset.sect_off,
4356 hex_string (signature));
4357 }
4358 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4359
4360 if (dwarf2_read_debug)
4361 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
4362 offset.sect_off,
4363 hex_string (signature));
4364
4365 info_ptr += length;
4366 }
4367 }
4368
4369 return types_htab;
4370 }
4371
4372 /* Create the hash table of all entries in the .debug_types section,
4373 and initialize all_type_units.
4374 The result is zero if there is an error (e.g. missing .debug_types section),
4375 otherwise non-zero. */
4376
4377 static int
4378 create_all_type_units (struct objfile *objfile)
4379 {
4380 htab_t types_htab;
4381 struct signatured_type **iter;
4382
4383 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4384 if (types_htab == NULL)
4385 {
4386 dwarf2_per_objfile->signatured_types = NULL;
4387 return 0;
4388 }
4389
4390 dwarf2_per_objfile->signatured_types = types_htab;
4391
4392 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4393 dwarf2_per_objfile->all_type_units
4394 = xmalloc (dwarf2_per_objfile->n_type_units
4395 * sizeof (struct signatured_type *));
4396 iter = &dwarf2_per_objfile->all_type_units[0];
4397 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4398 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4399 == dwarf2_per_objfile->n_type_units);
4400
4401 return 1;
4402 }
4403
4404 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
4405 Fill in SIG_ENTRY with DWO_ENTRY. */
4406
4407 static void
4408 fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
4409 struct signatured_type *sig_entry,
4410 struct dwo_unit *dwo_entry)
4411 {
4412 /* Make sure we're not clobbering something we don't expect to. */
4413 gdb_assert (! sig_entry->per_cu.queued);
4414 gdb_assert (sig_entry->per_cu.cu == NULL);
4415 gdb_assert (sig_entry->per_cu.v.quick != NULL);
4416 gdb_assert (sig_entry->per_cu.v.quick->symtab == NULL);
4417 gdb_assert (sig_entry->signature == dwo_entry->signature);
4418 gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
4419 gdb_assert (sig_entry->type_unit_group == NULL);
4420 gdb_assert (sig_entry->dwo_unit == NULL);
4421
4422 sig_entry->per_cu.section = dwo_entry->section;
4423 sig_entry->per_cu.offset = dwo_entry->offset;
4424 sig_entry->per_cu.length = dwo_entry->length;
4425 sig_entry->per_cu.reading_dwo_directly = 1;
4426 sig_entry->per_cu.objfile = objfile;
4427 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
4428 sig_entry->dwo_unit = dwo_entry;
4429 }
4430
4431 /* Subroutine of lookup_signatured_type.
4432 If we haven't read the TU yet, create the signatured_type data structure
4433 for a TU to be read in directly from a DWO file, bypassing the stub.
4434 This is the "Stay in DWO Optimization": When there is no DWP file and we're
4435 using .gdb_index, then when reading a CU we want to stay in the DWO file
4436 containing that CU. Otherwise we could end up reading several other DWO
4437 files (due to comdat folding) to process the transitive closure of all the
4438 mentioned TUs, and that can be slow. The current DWO file will have every
4439 type signature that it needs.
4440 We only do this for .gdb_index because in the psymtab case we already have
4441 to read all the DWOs to build the type unit groups. */
4442
4443 static struct signatured_type *
4444 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4445 {
4446 struct objfile *objfile = dwarf2_per_objfile->objfile;
4447 struct dwo_file *dwo_file;
4448 struct dwo_unit find_dwo_entry, *dwo_entry;
4449 struct signatured_type find_sig_entry, *sig_entry;
4450
4451 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4452
4453 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
4454 dwo_unit of the TU itself. */
4455 dwo_file = cu->dwo_unit->dwo_file;
4456
4457 /* We only ever need to read in one copy of a signatured type.
4458 Just use the global signatured_types array. If this is the first time
4459 we're reading this type, replace the recorded data from .gdb_index with
4460 this TU. */
4461
4462 if (dwarf2_per_objfile->signatured_types == NULL)
4463 return NULL;
4464 find_sig_entry.signature = sig;
4465 sig_entry = htab_find (dwarf2_per_objfile->signatured_types, &find_sig_entry);
4466 if (sig_entry == NULL)
4467 return NULL;
4468
4469 /* We can get here with the TU already read, *or* in the process of being
4470 read. Don't reassign it if that's the case. Also note that if the TU is
4471 already being read, it may not have come from a DWO, the program may be
4472 a mix of Fission-compiled code and non-Fission-compiled code. */
4473 /* Have we already tried to read this TU? */
4474 if (sig_entry->per_cu.tu_read)
4475 return sig_entry;
4476
4477 /* Ok, this is the first time we're reading this TU. */
4478 if (dwo_file->tus == NULL)
4479 return NULL;
4480 find_dwo_entry.signature = sig;
4481 dwo_entry = htab_find (dwo_file->tus, &find_dwo_entry);
4482 if (dwo_entry == NULL)
4483 return NULL;
4484
4485 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4486 sig_entry->per_cu.tu_read = 1;
4487 return sig_entry;
4488 }
4489
4490 /* Subroutine of lookup_dwp_signatured_type.
4491 Add an entry for signature SIG to dwarf2_per_objfile->signatured_types. */
4492
4493 static struct signatured_type *
4494 add_type_unit (ULONGEST sig)
4495 {
4496 struct objfile *objfile = dwarf2_per_objfile->objfile;
4497 int n_type_units = dwarf2_per_objfile->n_type_units;
4498 struct signatured_type *sig_type;
4499 void **slot;
4500
4501 ++n_type_units;
4502 dwarf2_per_objfile->all_type_units =
4503 xrealloc (dwarf2_per_objfile->all_type_units,
4504 n_type_units * sizeof (struct signatured_type *));
4505 dwarf2_per_objfile->n_type_units = n_type_units;
4506 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4507 struct signatured_type);
4508 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
4509 sig_type->signature = sig;
4510 sig_type->per_cu.is_debug_types = 1;
4511 sig_type->per_cu.v.quick =
4512 OBSTACK_ZALLOC (&objfile->objfile_obstack,
4513 struct dwarf2_per_cu_quick_data);
4514 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4515 sig_type, INSERT);
4516 gdb_assert (*slot == NULL);
4517 *slot = sig_type;
4518 /* The rest of sig_type must be filled in by the caller. */
4519 return sig_type;
4520 }
4521
4522 /* Subroutine of lookup_signatured_type.
4523 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
4524 then try the DWP file.
4525 Normally this "can't happen", but if there's a bug in signature
4526 generation and/or the DWP file is built incorrectly, it can happen.
4527 Using the type directly from the DWP file means we don't have the stub
4528 which has some useful attributes (e.g., DW_AT_comp_dir), but they're
4529 not critical. [Eventually the stub may go away for type units anyway.] */
4530
4531 static struct signatured_type *
4532 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4533 {
4534 struct objfile *objfile = dwarf2_per_objfile->objfile;
4535 struct dwp_file *dwp_file = get_dwp_file ();
4536 struct dwo_unit *dwo_entry;
4537 struct signatured_type find_sig_entry, *sig_entry;
4538
4539 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4540 gdb_assert (dwp_file != NULL);
4541
4542 if (dwarf2_per_objfile->signatured_types != NULL)
4543 {
4544 find_sig_entry.signature = sig;
4545 sig_entry = htab_find (dwarf2_per_objfile->signatured_types,
4546 &find_sig_entry);
4547 if (sig_entry != NULL)
4548 return sig_entry;
4549 }
4550
4551 /* This is the "shouldn't happen" case.
4552 Try the DWP file and hope for the best. */
4553 if (dwp_file->tus == NULL)
4554 return NULL;
4555 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
4556 sig, 1 /* is_debug_types */);
4557 if (dwo_entry == NULL)
4558 return NULL;
4559
4560 sig_entry = add_type_unit (sig);
4561 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4562
4563 /* The caller will signal a complaint if we return NULL.
4564 Here we don't return NULL but we still want to complain. */
4565 complaint (&symfile_complaints,
4566 _("Bad type signature %s referenced by %s at 0x%x,"
4567 " coping by using copy in DWP [in module %s]"),
4568 hex_string (sig),
4569 cu->per_cu->is_debug_types ? "TU" : "CU",
4570 cu->per_cu->offset.sect_off,
4571 objfile_name (objfile));
4572
4573 return sig_entry;
4574 }
4575
4576 /* Lookup a signature based type for DW_FORM_ref_sig8.
4577 Returns NULL if signature SIG is not present in the table.
4578 It is up to the caller to complain about this. */
4579
4580 static struct signatured_type *
4581 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4582 {
4583 if (cu->dwo_unit
4584 && dwarf2_per_objfile->using_index)
4585 {
4586 /* We're in a DWO/DWP file, and we're using .gdb_index.
4587 These cases require special processing. */
4588 if (get_dwp_file () == NULL)
4589 return lookup_dwo_signatured_type (cu, sig);
4590 else
4591 return lookup_dwp_signatured_type (cu, sig);
4592 }
4593 else
4594 {
4595 struct signatured_type find_entry, *entry;
4596
4597 if (dwarf2_per_objfile->signatured_types == NULL)
4598 return NULL;
4599 find_entry.signature = sig;
4600 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4601 return entry;
4602 }
4603 }
4604 \f
4605 /* Low level DIE reading support. */
4606
4607 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4608
4609 static void
4610 init_cu_die_reader (struct die_reader_specs *reader,
4611 struct dwarf2_cu *cu,
4612 struct dwarf2_section_info *section,
4613 struct dwo_file *dwo_file)
4614 {
4615 gdb_assert (section->readin && section->buffer != NULL);
4616 reader->abfd = section->asection->owner;
4617 reader->cu = cu;
4618 reader->dwo_file = dwo_file;
4619 reader->die_section = section;
4620 reader->buffer = section->buffer;
4621 reader->buffer_end = section->buffer + section->size;
4622 reader->comp_dir = NULL;
4623 }
4624
4625 /* Subroutine of init_cutu_and_read_dies to simplify it.
4626 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
4627 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
4628 already.
4629
4630 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
4631 from it to the DIE in the DWO. If NULL we are skipping the stub.
4632 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
4633 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
4634 attribute of the referencing CU. Exactly one of STUB_COMP_UNIT_DIE and
4635 COMP_DIR must be non-NULL.
4636 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
4637 are filled in with the info of the DIE from the DWO file.
4638 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
4639 provided an abbrev table to use.
4640 The result is non-zero if a valid (non-dummy) DIE was found. */
4641
4642 static int
4643 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
4644 struct dwo_unit *dwo_unit,
4645 int abbrev_table_provided,
4646 struct die_info *stub_comp_unit_die,
4647 const char *stub_comp_dir,
4648 struct die_reader_specs *result_reader,
4649 const gdb_byte **result_info_ptr,
4650 struct die_info **result_comp_unit_die,
4651 int *result_has_children)
4652 {
4653 struct objfile *objfile = dwarf2_per_objfile->objfile;
4654 struct dwarf2_cu *cu = this_cu->cu;
4655 struct dwarf2_section_info *section;
4656 bfd *abfd;
4657 const gdb_byte *begin_info_ptr, *info_ptr;
4658 const char *comp_dir_string;
4659 ULONGEST signature; /* Or dwo_id. */
4660 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4661 int i,num_extra_attrs;
4662 struct dwarf2_section_info *dwo_abbrev_section;
4663 struct attribute *attr;
4664 struct attribute comp_dir_attr;
4665 struct die_info *comp_unit_die;
4666
4667 /* Both can't be provided. */
4668 gdb_assert (! (stub_comp_unit_die && stub_comp_dir));
4669
4670 /* These attributes aren't processed until later:
4671 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4672 However, the attribute is found in the stub which we won't have later.
4673 In order to not impose this complication on the rest of the code,
4674 we read them here and copy them to the DWO CU/TU die. */
4675
4676 stmt_list = NULL;
4677 low_pc = NULL;
4678 high_pc = NULL;
4679 ranges = NULL;
4680 comp_dir = NULL;
4681
4682 if (stub_comp_unit_die != NULL)
4683 {
4684 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4685 DWO file. */
4686 if (! this_cu->is_debug_types)
4687 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
4688 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
4689 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
4690 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
4691 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
4692
4693 /* There should be a DW_AT_addr_base attribute here (if needed).
4694 We need the value before we can process DW_FORM_GNU_addr_index. */
4695 cu->addr_base = 0;
4696 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
4697 if (attr)
4698 cu->addr_base = DW_UNSND (attr);
4699
4700 /* There should be a DW_AT_ranges_base attribute here (if needed).
4701 We need the value before we can process DW_AT_ranges. */
4702 cu->ranges_base = 0;
4703 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
4704 if (attr)
4705 cu->ranges_base = DW_UNSND (attr);
4706 }
4707 else if (stub_comp_dir != NULL)
4708 {
4709 /* Reconstruct the comp_dir attribute to simplify the code below. */
4710 comp_dir = (struct attribute *)
4711 obstack_alloc (&cu->comp_unit_obstack, sizeof (*comp_dir));
4712 comp_dir->name = DW_AT_comp_dir;
4713 comp_dir->form = DW_FORM_string;
4714 DW_STRING_IS_CANONICAL (comp_dir) = 0;
4715 DW_STRING (comp_dir) = stub_comp_dir;
4716 }
4717
4718 /* Set up for reading the DWO CU/TU. */
4719 cu->dwo_unit = dwo_unit;
4720 section = dwo_unit->section;
4721 dwarf2_read_section (objfile, section);
4722 abfd = section->asection->owner;
4723 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4724 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4725 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
4726
4727 if (this_cu->is_debug_types)
4728 {
4729 ULONGEST header_signature;
4730 cu_offset type_offset_in_tu;
4731 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
4732
4733 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4734 dwo_abbrev_section,
4735 info_ptr,
4736 &header_signature,
4737 &type_offset_in_tu);
4738 /* This is not an assert because it can be caused by bad debug info. */
4739 if (sig_type->signature != header_signature)
4740 {
4741 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
4742 " TU at offset 0x%x [in module %s]"),
4743 hex_string (sig_type->signature),
4744 hex_string (header_signature),
4745 dwo_unit->offset.sect_off,
4746 bfd_get_filename (abfd));
4747 }
4748 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4749 /* For DWOs coming from DWP files, we don't know the CU length
4750 nor the type's offset in the TU until now. */
4751 dwo_unit->length = get_cu_length (&cu->header);
4752 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4753
4754 /* Establish the type offset that can be used to lookup the type.
4755 For DWO files, we don't know it until now. */
4756 sig_type->type_offset_in_section.sect_off =
4757 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4758 }
4759 else
4760 {
4761 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4762 dwo_abbrev_section,
4763 info_ptr, 0);
4764 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4765 /* For DWOs coming from DWP files, we don't know the CU length
4766 until now. */
4767 dwo_unit->length = get_cu_length (&cu->header);
4768 }
4769
4770 /* Replace the CU's original abbrev table with the DWO's.
4771 Reminder: We can't read the abbrev table until we've read the header. */
4772 if (abbrev_table_provided)
4773 {
4774 /* Don't free the provided abbrev table, the caller of
4775 init_cutu_and_read_dies owns it. */
4776 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4777 /* Ensure the DWO abbrev table gets freed. */
4778 make_cleanup (dwarf2_free_abbrev_table, cu);
4779 }
4780 else
4781 {
4782 dwarf2_free_abbrev_table (cu);
4783 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4784 /* Leave any existing abbrev table cleanup as is. */
4785 }
4786
4787 /* Read in the die, but leave space to copy over the attributes
4788 from the stub. This has the benefit of simplifying the rest of
4789 the code - all the work to maintain the illusion of a single
4790 DW_TAG_{compile,type}_unit DIE is done here. */
4791 num_extra_attrs = ((stmt_list != NULL)
4792 + (low_pc != NULL)
4793 + (high_pc != NULL)
4794 + (ranges != NULL)
4795 + (comp_dir != NULL));
4796 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
4797 result_has_children, num_extra_attrs);
4798
4799 /* Copy over the attributes from the stub to the DIE we just read in. */
4800 comp_unit_die = *result_comp_unit_die;
4801 i = comp_unit_die->num_attrs;
4802 if (stmt_list != NULL)
4803 comp_unit_die->attrs[i++] = *stmt_list;
4804 if (low_pc != NULL)
4805 comp_unit_die->attrs[i++] = *low_pc;
4806 if (high_pc != NULL)
4807 comp_unit_die->attrs[i++] = *high_pc;
4808 if (ranges != NULL)
4809 comp_unit_die->attrs[i++] = *ranges;
4810 if (comp_dir != NULL)
4811 comp_unit_die->attrs[i++] = *comp_dir;
4812 comp_unit_die->num_attrs += num_extra_attrs;
4813
4814 if (dwarf2_die_debug)
4815 {
4816 fprintf_unfiltered (gdb_stdlog,
4817 "Read die from %s@0x%x of %s:\n",
4818 bfd_section_name (abfd, section->asection),
4819 (unsigned) (begin_info_ptr - section->buffer),
4820 bfd_get_filename (abfd));
4821 dump_die (comp_unit_die, dwarf2_die_debug);
4822 }
4823
4824 /* Save the comp_dir attribute. If there is no DWP file then we'll read
4825 TUs by skipping the stub and going directly to the entry in the DWO file.
4826 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
4827 to get it via circuitous means. Blech. */
4828 if (comp_dir != NULL)
4829 result_reader->comp_dir = DW_STRING (comp_dir);
4830
4831 /* Skip dummy compilation units. */
4832 if (info_ptr >= begin_info_ptr + dwo_unit->length
4833 || peek_abbrev_code (abfd, info_ptr) == 0)
4834 return 0;
4835
4836 *result_info_ptr = info_ptr;
4837 return 1;
4838 }
4839
4840 /* Subroutine of init_cutu_and_read_dies to simplify it.
4841 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
4842 Returns NULL if the specified DWO unit cannot be found. */
4843
4844 static struct dwo_unit *
4845 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
4846 struct die_info *comp_unit_die)
4847 {
4848 struct dwarf2_cu *cu = this_cu->cu;
4849 struct attribute *attr;
4850 ULONGEST signature;
4851 struct dwo_unit *dwo_unit;
4852 const char *comp_dir, *dwo_name;
4853
4854 gdb_assert (cu != NULL);
4855
4856 /* Yeah, we look dwo_name up again, but it simplifies the code. */
4857 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4858 gdb_assert (attr != NULL);
4859 dwo_name = DW_STRING (attr);
4860 comp_dir = NULL;
4861 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4862 if (attr)
4863 comp_dir = DW_STRING (attr);
4864
4865 if (this_cu->is_debug_types)
4866 {
4867 struct signatured_type *sig_type;
4868
4869 /* Since this_cu is the first member of struct signatured_type,
4870 we can go from a pointer to one to a pointer to the other. */
4871 sig_type = (struct signatured_type *) this_cu;
4872 signature = sig_type->signature;
4873 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4874 }
4875 else
4876 {
4877 struct attribute *attr;
4878
4879 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4880 if (! attr)
4881 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
4882 " [in module %s]"),
4883 dwo_name, objfile_name (this_cu->objfile));
4884 signature = DW_UNSND (attr);
4885 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4886 signature);
4887 }
4888
4889 return dwo_unit;
4890 }
4891
4892 /* Subroutine of init_cutu_and_read_dies to simplify it.
4893 Read a TU directly from a DWO file, bypassing the stub. */
4894
4895 static void
4896 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu, int keep,
4897 die_reader_func_ftype *die_reader_func,
4898 void *data)
4899 {
4900 struct dwarf2_cu *cu;
4901 struct signatured_type *sig_type;
4902 struct cleanup *cleanups, *free_cu_cleanup;
4903 struct die_reader_specs reader;
4904 const gdb_byte *info_ptr;
4905 struct die_info *comp_unit_die;
4906 int has_children;
4907
4908 /* Verify we can do the following downcast, and that we have the
4909 data we need. */
4910 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
4911 sig_type = (struct signatured_type *) this_cu;
4912 gdb_assert (sig_type->dwo_unit != NULL);
4913
4914 cleanups = make_cleanup (null_cleanup, NULL);
4915
4916 gdb_assert (this_cu->cu == NULL);
4917 cu = xmalloc (sizeof (*cu));
4918 init_one_comp_unit (cu, this_cu);
4919 /* If an error occurs while loading, release our storage. */
4920 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4921
4922 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
4923 0 /* abbrev_table_provided */,
4924 NULL /* stub_comp_unit_die */,
4925 sig_type->dwo_unit->dwo_file->comp_dir,
4926 &reader, &info_ptr,
4927 &comp_unit_die, &has_children) == 0)
4928 {
4929 /* Dummy die. */
4930 do_cleanups (cleanups);
4931 return;
4932 }
4933
4934 /* All the "real" work is done here. */
4935 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4936
4937 /* This duplicates some code in init_cutu_and_read_dies,
4938 but the alternative is making the latter more complex.
4939 This function is only for the special case of using DWO files directly:
4940 no point in overly complicating the general case just to handle this. */
4941 if (keep)
4942 {
4943 /* We've successfully allocated this compilation unit. Let our
4944 caller clean it up when finished with it. */
4945 discard_cleanups (free_cu_cleanup);
4946
4947 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4948 So we have to manually free the abbrev table. */
4949 dwarf2_free_abbrev_table (cu);
4950
4951 /* Link this CU into read_in_chain. */
4952 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4953 dwarf2_per_objfile->read_in_chain = this_cu;
4954 }
4955 else
4956 do_cleanups (free_cu_cleanup);
4957
4958 do_cleanups (cleanups);
4959 }
4960
4961 /* Initialize a CU (or TU) and read its DIEs.
4962 If the CU defers to a DWO file, read the DWO file as well.
4963
4964 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4965 Otherwise the table specified in the comp unit header is read in and used.
4966 This is an optimization for when we already have the abbrev table.
4967
4968 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4969 Otherwise, a new CU is allocated with xmalloc.
4970
4971 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4972 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4973
4974 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4975 linker) then DIE_READER_FUNC will not get called. */
4976
4977 static void
4978 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
4979 struct abbrev_table *abbrev_table,
4980 int use_existing_cu, int keep,
4981 die_reader_func_ftype *die_reader_func,
4982 void *data)
4983 {
4984 struct objfile *objfile = dwarf2_per_objfile->objfile;
4985 struct dwarf2_section_info *section = this_cu->section;
4986 bfd *abfd = section->asection->owner;
4987 struct dwarf2_cu *cu;
4988 const gdb_byte *begin_info_ptr, *info_ptr;
4989 struct die_reader_specs reader;
4990 struct die_info *comp_unit_die;
4991 int has_children;
4992 struct attribute *attr;
4993 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4994 struct signatured_type *sig_type = NULL;
4995 struct dwarf2_section_info *abbrev_section;
4996 /* Non-zero if CU currently points to a DWO file and we need to
4997 reread it. When this happens we need to reread the skeleton die
4998 before we can reread the DWO file (this only applies to CUs, not TUs). */
4999 int rereading_dwo_cu = 0;
5000
5001 if (dwarf2_die_debug)
5002 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5003 this_cu->is_debug_types ? "type" : "comp",
5004 this_cu->offset.sect_off);
5005
5006 if (use_existing_cu)
5007 gdb_assert (keep);
5008
5009 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5010 file (instead of going through the stub), short-circuit all of this. */
5011 if (this_cu->reading_dwo_directly)
5012 {
5013 /* Narrow down the scope of possibilities to have to understand. */
5014 gdb_assert (this_cu->is_debug_types);
5015 gdb_assert (abbrev_table == NULL);
5016 gdb_assert (!use_existing_cu);
5017 init_tu_and_read_dwo_dies (this_cu, keep, die_reader_func, data);
5018 return;
5019 }
5020
5021 cleanups = make_cleanup (null_cleanup, NULL);
5022
5023 /* This is cheap if the section is already read in. */
5024 dwarf2_read_section (objfile, section);
5025
5026 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
5027
5028 abbrev_section = get_abbrev_section_for_cu (this_cu);
5029
5030 if (use_existing_cu && this_cu->cu != NULL)
5031 {
5032 cu = this_cu->cu;
5033
5034 /* If this CU is from a DWO file we need to start over, we need to
5035 refetch the attributes from the skeleton CU.
5036 This could be optimized by retrieving those attributes from when we
5037 were here the first time: the previous comp_unit_die was stored in
5038 comp_unit_obstack. But there's no data yet that we need this
5039 optimization. */
5040 if (cu->dwo_unit != NULL)
5041 rereading_dwo_cu = 1;
5042 }
5043 else
5044 {
5045 /* If !use_existing_cu, this_cu->cu must be NULL. */
5046 gdb_assert (this_cu->cu == NULL);
5047
5048 cu = xmalloc (sizeof (*cu));
5049 init_one_comp_unit (cu, this_cu);
5050
5051 /* If an error occurs while loading, release our storage. */
5052 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5053 }
5054
5055 /* Get the header. */
5056 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
5057 {
5058 /* We already have the header, there's no need to read it in again. */
5059 info_ptr += cu->header.first_die_offset.cu_off;
5060 }
5061 else
5062 {
5063 if (this_cu->is_debug_types)
5064 {
5065 ULONGEST signature;
5066 cu_offset type_offset_in_tu;
5067
5068 info_ptr = read_and_check_type_unit_head (&cu->header, section,
5069 abbrev_section, info_ptr,
5070 &signature,
5071 &type_offset_in_tu);
5072
5073 /* Since per_cu is the first member of struct signatured_type,
5074 we can go from a pointer to one to a pointer to the other. */
5075 sig_type = (struct signatured_type *) this_cu;
5076 gdb_assert (sig_type->signature == signature);
5077 gdb_assert (sig_type->type_offset_in_tu.cu_off
5078 == type_offset_in_tu.cu_off);
5079 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5080
5081 /* LENGTH has not been set yet for type units if we're
5082 using .gdb_index. */
5083 this_cu->length = get_cu_length (&cu->header);
5084
5085 /* Establish the type offset that can be used to lookup the type. */
5086 sig_type->type_offset_in_section.sect_off =
5087 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
5088 }
5089 else
5090 {
5091 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5092 abbrev_section,
5093 info_ptr, 0);
5094
5095 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5096 gdb_assert (this_cu->length == get_cu_length (&cu->header));
5097 }
5098 }
5099
5100 /* Skip dummy compilation units. */
5101 if (info_ptr >= begin_info_ptr + this_cu->length
5102 || peek_abbrev_code (abfd, info_ptr) == 0)
5103 {
5104 do_cleanups (cleanups);
5105 return;
5106 }
5107
5108 /* If we don't have them yet, read the abbrevs for this compilation unit.
5109 And if we need to read them now, make sure they're freed when we're
5110 done. Note that it's important that if the CU had an abbrev table
5111 on entry we don't free it when we're done: Somewhere up the call stack
5112 it may be in use. */
5113 if (abbrev_table != NULL)
5114 {
5115 gdb_assert (cu->abbrev_table == NULL);
5116 gdb_assert (cu->header.abbrev_offset.sect_off
5117 == abbrev_table->offset.sect_off);
5118 cu->abbrev_table = abbrev_table;
5119 }
5120 else if (cu->abbrev_table == NULL)
5121 {
5122 dwarf2_read_abbrevs (cu, abbrev_section);
5123 make_cleanup (dwarf2_free_abbrev_table, cu);
5124 }
5125 else if (rereading_dwo_cu)
5126 {
5127 dwarf2_free_abbrev_table (cu);
5128 dwarf2_read_abbrevs (cu, abbrev_section);
5129 }
5130
5131 /* Read the top level CU/TU die. */
5132 init_cu_die_reader (&reader, cu, section, NULL);
5133 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5134
5135 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5136 from the DWO file.
5137 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5138 DWO CU, that this test will fail (the attribute will not be present). */
5139 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5140 if (attr)
5141 {
5142 struct dwo_unit *dwo_unit;
5143 struct die_info *dwo_comp_unit_die;
5144
5145 if (has_children)
5146 {
5147 complaint (&symfile_complaints,
5148 _("compilation unit with DW_AT_GNU_dwo_name"
5149 " has children (offset 0x%x) [in module %s]"),
5150 this_cu->offset.sect_off, bfd_get_filename (abfd));
5151 }
5152 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
5153 if (dwo_unit != NULL)
5154 {
5155 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5156 abbrev_table != NULL,
5157 comp_unit_die, NULL,
5158 &reader, &info_ptr,
5159 &dwo_comp_unit_die, &has_children) == 0)
5160 {
5161 /* Dummy die. */
5162 do_cleanups (cleanups);
5163 return;
5164 }
5165 comp_unit_die = dwo_comp_unit_die;
5166 }
5167 else
5168 {
5169 /* Yikes, we couldn't find the rest of the DIE, we only have
5170 the stub. A complaint has already been logged. There's
5171 not much more we can do except pass on the stub DIE to
5172 die_reader_func. We don't want to throw an error on bad
5173 debug info. */
5174 }
5175 }
5176
5177 /* All of the above is setup for this call. Yikes. */
5178 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5179
5180 /* Done, clean up. */
5181 if (free_cu_cleanup != NULL)
5182 {
5183 if (keep)
5184 {
5185 /* We've successfully allocated this compilation unit. Let our
5186 caller clean it up when finished with it. */
5187 discard_cleanups (free_cu_cleanup);
5188
5189 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5190 So we have to manually free the abbrev table. */
5191 dwarf2_free_abbrev_table (cu);
5192
5193 /* Link this CU into read_in_chain. */
5194 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5195 dwarf2_per_objfile->read_in_chain = this_cu;
5196 }
5197 else
5198 do_cleanups (free_cu_cleanup);
5199 }
5200
5201 do_cleanups (cleanups);
5202 }
5203
5204 /* Read CU/TU THIS_CU in section SECTION,
5205 but do not follow DW_AT_GNU_dwo_name if present.
5206 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
5207 to have already done the lookup to find the DWO/DWP file).
5208
5209 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
5210 THIS_CU->is_debug_types, but nothing else.
5211
5212 We fill in THIS_CU->length.
5213
5214 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5215 linker) then DIE_READER_FUNC will not get called.
5216
5217 THIS_CU->cu is always freed when done.
5218 This is done in order to not leave THIS_CU->cu in a state where we have
5219 to care whether it refers to the "main" CU or the DWO CU. */
5220
5221 static void
5222 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
5223 struct dwarf2_section_info *abbrev_section,
5224 struct dwo_file *dwo_file,
5225 die_reader_func_ftype *die_reader_func,
5226 void *data)
5227 {
5228 struct objfile *objfile = dwarf2_per_objfile->objfile;
5229 struct dwarf2_section_info *section = this_cu->section;
5230 bfd *abfd = section->asection->owner;
5231 struct dwarf2_cu cu;
5232 const gdb_byte *begin_info_ptr, *info_ptr;
5233 struct die_reader_specs reader;
5234 struct cleanup *cleanups;
5235 struct die_info *comp_unit_die;
5236 int has_children;
5237
5238 if (dwarf2_die_debug)
5239 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5240 this_cu->is_debug_types ? "type" : "comp",
5241 this_cu->offset.sect_off);
5242
5243 gdb_assert (this_cu->cu == NULL);
5244
5245 /* This is cheap if the section is already read in. */
5246 dwarf2_read_section (objfile, section);
5247
5248 init_one_comp_unit (&cu, this_cu);
5249
5250 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5251
5252 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
5253 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5254 abbrev_section, info_ptr,
5255 this_cu->is_debug_types);
5256
5257 this_cu->length = get_cu_length (&cu.header);
5258
5259 /* Skip dummy compilation units. */
5260 if (info_ptr >= begin_info_ptr + this_cu->length
5261 || peek_abbrev_code (abfd, info_ptr) == 0)
5262 {
5263 do_cleanups (cleanups);
5264 return;
5265 }
5266
5267 dwarf2_read_abbrevs (&cu, abbrev_section);
5268 make_cleanup (dwarf2_free_abbrev_table, &cu);
5269
5270 init_cu_die_reader (&reader, &cu, section, dwo_file);
5271 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5272
5273 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5274
5275 do_cleanups (cleanups);
5276 }
5277
5278 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5279 does not lookup the specified DWO file.
5280 This cannot be used to read DWO files.
5281
5282 THIS_CU->cu is always freed when done.
5283 This is done in order to not leave THIS_CU->cu in a state where we have
5284 to care whether it refers to the "main" CU or the DWO CU.
5285 We can revisit this if the data shows there's a performance issue. */
5286
5287 static void
5288 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5289 die_reader_func_ftype *die_reader_func,
5290 void *data)
5291 {
5292 init_cutu_and_read_dies_no_follow (this_cu,
5293 get_abbrev_section_for_cu (this_cu),
5294 NULL,
5295 die_reader_func, data);
5296 }
5297 \f
5298 /* Type Unit Groups.
5299
5300 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5301 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5302 so that all types coming from the same compilation (.o file) are grouped
5303 together. A future step could be to put the types in the same symtab as
5304 the CU the types ultimately came from. */
5305
5306 static hashval_t
5307 hash_type_unit_group (const void *item)
5308 {
5309 const struct type_unit_group *tu_group = item;
5310
5311 return hash_stmt_list_entry (&tu_group->hash);
5312 }
5313
5314 static int
5315 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
5316 {
5317 const struct type_unit_group *lhs = item_lhs;
5318 const struct type_unit_group *rhs = item_rhs;
5319
5320 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
5321 }
5322
5323 /* Allocate a hash table for type unit groups. */
5324
5325 static htab_t
5326 allocate_type_unit_groups_table (void)
5327 {
5328 return htab_create_alloc_ex (3,
5329 hash_type_unit_group,
5330 eq_type_unit_group,
5331 NULL,
5332 &dwarf2_per_objfile->objfile->objfile_obstack,
5333 hashtab_obstack_allocate,
5334 dummy_obstack_deallocate);
5335 }
5336
5337 /* Type units that don't have DW_AT_stmt_list are grouped into their own
5338 partial symtabs. We combine several TUs per psymtab to not let the size
5339 of any one psymtab grow too big. */
5340 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5341 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
5342
5343 /* Helper routine for get_type_unit_group.
5344 Create the type_unit_group object used to hold one or more TUs. */
5345
5346 static struct type_unit_group *
5347 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
5348 {
5349 struct objfile *objfile = dwarf2_per_objfile->objfile;
5350 struct dwarf2_per_cu_data *per_cu;
5351 struct type_unit_group *tu_group;
5352
5353 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5354 struct type_unit_group);
5355 per_cu = &tu_group->per_cu;
5356 per_cu->objfile = objfile;
5357
5358 if (dwarf2_per_objfile->using_index)
5359 {
5360 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5361 struct dwarf2_per_cu_quick_data);
5362 }
5363 else
5364 {
5365 unsigned int line_offset = line_offset_struct.sect_off;
5366 struct partial_symtab *pst;
5367 char *name;
5368
5369 /* Give the symtab a useful name for debug purposes. */
5370 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5371 name = xstrprintf ("<type_units_%d>",
5372 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5373 else
5374 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5375
5376 pst = create_partial_symtab (per_cu, name);
5377 pst->anonymous = 1;
5378
5379 xfree (name);
5380 }
5381
5382 tu_group->hash.dwo_unit = cu->dwo_unit;
5383 tu_group->hash.line_offset = line_offset_struct;
5384
5385 return tu_group;
5386 }
5387
5388 /* Look up the type_unit_group for type unit CU, and create it if necessary.
5389 STMT_LIST is a DW_AT_stmt_list attribute. */
5390
5391 static struct type_unit_group *
5392 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
5393 {
5394 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5395 struct type_unit_group *tu_group;
5396 void **slot;
5397 unsigned int line_offset;
5398 struct type_unit_group type_unit_group_for_lookup;
5399
5400 if (dwarf2_per_objfile->type_unit_groups == NULL)
5401 {
5402 dwarf2_per_objfile->type_unit_groups =
5403 allocate_type_unit_groups_table ();
5404 }
5405
5406 /* Do we need to create a new group, or can we use an existing one? */
5407
5408 if (stmt_list)
5409 {
5410 line_offset = DW_UNSND (stmt_list);
5411 ++tu_stats->nr_symtab_sharers;
5412 }
5413 else
5414 {
5415 /* Ugh, no stmt_list. Rare, but we have to handle it.
5416 We can do various things here like create one group per TU or
5417 spread them over multiple groups to split up the expansion work.
5418 To avoid worst case scenarios (too many groups or too large groups)
5419 we, umm, group them in bunches. */
5420 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5421 | (tu_stats->nr_stmt_less_type_units
5422 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5423 ++tu_stats->nr_stmt_less_type_units;
5424 }
5425
5426 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5427 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5428 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5429 &type_unit_group_for_lookup, INSERT);
5430 if (*slot != NULL)
5431 {
5432 tu_group = *slot;
5433 gdb_assert (tu_group != NULL);
5434 }
5435 else
5436 {
5437 sect_offset line_offset_struct;
5438
5439 line_offset_struct.sect_off = line_offset;
5440 tu_group = create_type_unit_group (cu, line_offset_struct);
5441 *slot = tu_group;
5442 ++tu_stats->nr_symtabs;
5443 }
5444
5445 return tu_group;
5446 }
5447
5448 /* Struct used to sort TUs by their abbreviation table offset. */
5449
5450 struct tu_abbrev_offset
5451 {
5452 struct signatured_type *sig_type;
5453 sect_offset abbrev_offset;
5454 };
5455
5456 /* Helper routine for build_type_unit_groups, passed to qsort. */
5457
5458 static int
5459 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5460 {
5461 const struct tu_abbrev_offset * const *a = ap;
5462 const struct tu_abbrev_offset * const *b = bp;
5463 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5464 unsigned int boff = (*b)->abbrev_offset.sect_off;
5465
5466 return (aoff > boff) - (aoff < boff);
5467 }
5468
5469 /* A helper function to add a type_unit_group to a table. */
5470
5471 static int
5472 add_type_unit_group_to_table (void **slot, void *datum)
5473 {
5474 struct type_unit_group *tu_group = *slot;
5475 struct type_unit_group ***datap = datum;
5476
5477 **datap = tu_group;
5478 ++*datap;
5479
5480 return 1;
5481 }
5482
5483 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
5484 each one passing FUNC,DATA.
5485
5486 The efficiency is because we sort TUs by the abbrev table they use and
5487 only read each abbrev table once. In one program there are 200K TUs
5488 sharing 8K abbrev tables.
5489
5490 The main purpose of this function is to support building the
5491 dwarf2_per_objfile->type_unit_groups table.
5492 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5493 can collapse the search space by grouping them by stmt_list.
5494 The savings can be significant, in the same program from above the 200K TUs
5495 share 8K stmt_list tables.
5496
5497 FUNC is expected to call get_type_unit_group, which will create the
5498 struct type_unit_group if necessary and add it to
5499 dwarf2_per_objfile->type_unit_groups. */
5500
5501 static void
5502 build_type_unit_groups (die_reader_func_ftype *func, void *data)
5503 {
5504 struct objfile *objfile = dwarf2_per_objfile->objfile;
5505 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5506 struct cleanup *cleanups;
5507 struct abbrev_table *abbrev_table;
5508 sect_offset abbrev_offset;
5509 struct tu_abbrev_offset *sorted_by_abbrev;
5510 struct type_unit_group **iter;
5511 int i;
5512
5513 /* It's up to the caller to not call us multiple times. */
5514 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5515
5516 if (dwarf2_per_objfile->n_type_units == 0)
5517 return;
5518
5519 /* TUs typically share abbrev tables, and there can be way more TUs than
5520 abbrev tables. Sort by abbrev table to reduce the number of times we
5521 read each abbrev table in.
5522 Alternatives are to punt or to maintain a cache of abbrev tables.
5523 This is simpler and efficient enough for now.
5524
5525 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5526 symtab to use). Typically TUs with the same abbrev offset have the same
5527 stmt_list value too so in practice this should work well.
5528
5529 The basic algorithm here is:
5530
5531 sort TUs by abbrev table
5532 for each TU with same abbrev table:
5533 read abbrev table if first user
5534 read TU top level DIE
5535 [IWBN if DWO skeletons had DW_AT_stmt_list]
5536 call FUNC */
5537
5538 if (dwarf2_read_debug)
5539 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5540
5541 /* Sort in a separate table to maintain the order of all_type_units
5542 for .gdb_index: TU indices directly index all_type_units. */
5543 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5544 dwarf2_per_objfile->n_type_units);
5545 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5546 {
5547 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5548
5549 sorted_by_abbrev[i].sig_type = sig_type;
5550 sorted_by_abbrev[i].abbrev_offset =
5551 read_abbrev_offset (sig_type->per_cu.section,
5552 sig_type->per_cu.offset);
5553 }
5554 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5555 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5556 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5557
5558 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5559 called any number of times, so we don't reset tu_stats here. */
5560
5561 abbrev_offset.sect_off = ~(unsigned) 0;
5562 abbrev_table = NULL;
5563 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5564
5565 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5566 {
5567 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5568
5569 /* Switch to the next abbrev table if necessary. */
5570 if (abbrev_table == NULL
5571 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5572 {
5573 if (abbrev_table != NULL)
5574 {
5575 abbrev_table_free (abbrev_table);
5576 /* Reset to NULL in case abbrev_table_read_table throws
5577 an error: abbrev_table_free_cleanup will get called. */
5578 abbrev_table = NULL;
5579 }
5580 abbrev_offset = tu->abbrev_offset;
5581 abbrev_table =
5582 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5583 abbrev_offset);
5584 ++tu_stats->nr_uniq_abbrev_tables;
5585 }
5586
5587 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5588 func, data);
5589 }
5590
5591 /* type_unit_groups can be NULL if there is an error in the debug info.
5592 Just create an empty table so the rest of gdb doesn't have to watch
5593 for this error case. */
5594 if (dwarf2_per_objfile->type_unit_groups == NULL)
5595 {
5596 dwarf2_per_objfile->type_unit_groups =
5597 allocate_type_unit_groups_table ();
5598 dwarf2_per_objfile->n_type_unit_groups = 0;
5599 }
5600
5601 /* Create a vector of pointers to primary type units to make it easy to
5602 iterate over them and CUs. See dw2_get_primary_cu. */
5603 dwarf2_per_objfile->n_type_unit_groups =
5604 htab_elements (dwarf2_per_objfile->type_unit_groups);
5605 dwarf2_per_objfile->all_type_unit_groups =
5606 obstack_alloc (&objfile->objfile_obstack,
5607 dwarf2_per_objfile->n_type_unit_groups
5608 * sizeof (struct type_unit_group *));
5609 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5610 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5611 add_type_unit_group_to_table, &iter);
5612 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5613 == dwarf2_per_objfile->n_type_unit_groups);
5614
5615 do_cleanups (cleanups);
5616
5617 if (dwarf2_read_debug)
5618 {
5619 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5620 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5621 dwarf2_per_objfile->n_type_units);
5622 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5623 tu_stats->nr_uniq_abbrev_tables);
5624 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5625 tu_stats->nr_symtabs);
5626 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5627 tu_stats->nr_symtab_sharers);
5628 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5629 tu_stats->nr_stmt_less_type_units);
5630 }
5631 }
5632 \f
5633 /* Partial symbol tables. */
5634
5635 /* Create a psymtab named NAME and assign it to PER_CU.
5636
5637 The caller must fill in the following details:
5638 dirname, textlow, texthigh. */
5639
5640 static struct partial_symtab *
5641 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5642 {
5643 struct objfile *objfile = per_cu->objfile;
5644 struct partial_symtab *pst;
5645
5646 pst = start_psymtab_common (objfile, objfile->section_offsets,
5647 name, 0,
5648 objfile->global_psymbols.next,
5649 objfile->static_psymbols.next);
5650
5651 pst->psymtabs_addrmap_supported = 1;
5652
5653 /* This is the glue that links PST into GDB's symbol API. */
5654 pst->read_symtab_private = per_cu;
5655 pst->read_symtab = dwarf2_read_symtab;
5656 per_cu->v.psymtab = pst;
5657
5658 return pst;
5659 }
5660
5661 /* The DATA object passed to process_psymtab_comp_unit_reader has this
5662 type. */
5663
5664 struct process_psymtab_comp_unit_data
5665 {
5666 /* True if we are reading a DW_TAG_partial_unit. */
5667
5668 int want_partial_unit;
5669
5670 /* The "pretend" language that is used if the CU doesn't declare a
5671 language. */
5672
5673 enum language pretend_language;
5674 };
5675
5676 /* die_reader_func for process_psymtab_comp_unit. */
5677
5678 static void
5679 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
5680 const gdb_byte *info_ptr,
5681 struct die_info *comp_unit_die,
5682 int has_children,
5683 void *data)
5684 {
5685 struct dwarf2_cu *cu = reader->cu;
5686 struct objfile *objfile = cu->objfile;
5687 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5688 struct attribute *attr;
5689 CORE_ADDR baseaddr;
5690 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5691 struct partial_symtab *pst;
5692 int has_pc_info;
5693 const char *filename;
5694 struct process_psymtab_comp_unit_data *info = data;
5695
5696 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
5697 return;
5698
5699 gdb_assert (! per_cu->is_debug_types);
5700
5701 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
5702
5703 cu->list_in_scope = &file_symbols;
5704
5705 /* Allocate a new partial symbol table structure. */
5706 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5707 if (attr == NULL || !DW_STRING (attr))
5708 filename = "";
5709 else
5710 filename = DW_STRING (attr);
5711
5712 pst = create_partial_symtab (per_cu, filename);
5713
5714 /* This must be done before calling dwarf2_build_include_psymtabs. */
5715 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5716 if (attr != NULL)
5717 pst->dirname = DW_STRING (attr);
5718
5719 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5720
5721 dwarf2_find_base_address (comp_unit_die, cu);
5722
5723 /* Possibly set the default values of LOWPC and HIGHPC from
5724 `DW_AT_ranges'. */
5725 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5726 &best_highpc, cu, pst);
5727 if (has_pc_info == 1 && best_lowpc < best_highpc)
5728 /* Store the contiguous range if it is not empty; it can be empty for
5729 CUs with no code. */
5730 addrmap_set_empty (objfile->psymtabs_addrmap,
5731 best_lowpc + baseaddr,
5732 best_highpc + baseaddr - 1, pst);
5733
5734 /* Check if comp unit has_children.
5735 If so, read the rest of the partial symbols from this comp unit.
5736 If not, there's no more debug_info for this comp unit. */
5737 if (has_children)
5738 {
5739 struct partial_die_info *first_die;
5740 CORE_ADDR lowpc, highpc;
5741
5742 lowpc = ((CORE_ADDR) -1);
5743 highpc = ((CORE_ADDR) 0);
5744
5745 first_die = load_partial_dies (reader, info_ptr, 1);
5746
5747 scan_partial_symbols (first_die, &lowpc, &highpc,
5748 ! has_pc_info, cu);
5749
5750 /* If we didn't find a lowpc, set it to highpc to avoid
5751 complaints from `maint check'. */
5752 if (lowpc == ((CORE_ADDR) -1))
5753 lowpc = highpc;
5754
5755 /* If the compilation unit didn't have an explicit address range,
5756 then use the information extracted from its child dies. */
5757 if (! has_pc_info)
5758 {
5759 best_lowpc = lowpc;
5760 best_highpc = highpc;
5761 }
5762 }
5763 pst->textlow = best_lowpc + baseaddr;
5764 pst->texthigh = best_highpc + baseaddr;
5765
5766 pst->n_global_syms = objfile->global_psymbols.next -
5767 (objfile->global_psymbols.list + pst->globals_offset);
5768 pst->n_static_syms = objfile->static_psymbols.next -
5769 (objfile->static_psymbols.list + pst->statics_offset);
5770 sort_pst_symbols (objfile, pst);
5771
5772 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5773 {
5774 int i;
5775 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5776 struct dwarf2_per_cu_data *iter;
5777
5778 /* Fill in 'dependencies' here; we fill in 'users' in a
5779 post-pass. */
5780 pst->number_of_dependencies = len;
5781 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5782 len * sizeof (struct symtab *));
5783 for (i = 0;
5784 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5785 i, iter);
5786 ++i)
5787 pst->dependencies[i] = iter->v.psymtab;
5788
5789 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5790 }
5791
5792 /* Get the list of files included in the current compilation unit,
5793 and build a psymtab for each of them. */
5794 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5795
5796 if (dwarf2_read_debug)
5797 {
5798 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5799
5800 fprintf_unfiltered (gdb_stdlog,
5801 "Psymtab for %s unit @0x%x: %s - %s"
5802 ", %d global, %d static syms\n",
5803 per_cu->is_debug_types ? "type" : "comp",
5804 per_cu->offset.sect_off,
5805 paddress (gdbarch, pst->textlow),
5806 paddress (gdbarch, pst->texthigh),
5807 pst->n_global_syms, pst->n_static_syms);
5808 }
5809 }
5810
5811 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5812 Process compilation unit THIS_CU for a psymtab. */
5813
5814 static void
5815 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5816 int want_partial_unit,
5817 enum language pretend_language)
5818 {
5819 struct process_psymtab_comp_unit_data info;
5820
5821 /* If this compilation unit was already read in, free the
5822 cached copy in order to read it in again. This is
5823 necessary because we skipped some symbols when we first
5824 read in the compilation unit (see load_partial_dies).
5825 This problem could be avoided, but the benefit is unclear. */
5826 if (this_cu->cu != NULL)
5827 free_one_cached_comp_unit (this_cu);
5828
5829 gdb_assert (! this_cu->is_debug_types);
5830 info.want_partial_unit = want_partial_unit;
5831 info.pretend_language = pretend_language;
5832 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5833 process_psymtab_comp_unit_reader,
5834 &info);
5835
5836 /* Age out any secondary CUs. */
5837 age_cached_comp_units ();
5838 }
5839
5840 /* Reader function for build_type_psymtabs. */
5841
5842 static void
5843 build_type_psymtabs_reader (const struct die_reader_specs *reader,
5844 const gdb_byte *info_ptr,
5845 struct die_info *type_unit_die,
5846 int has_children,
5847 void *data)
5848 {
5849 struct objfile *objfile = dwarf2_per_objfile->objfile;
5850 struct dwarf2_cu *cu = reader->cu;
5851 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5852 struct signatured_type *sig_type;
5853 struct type_unit_group *tu_group;
5854 struct attribute *attr;
5855 struct partial_die_info *first_die;
5856 CORE_ADDR lowpc, highpc;
5857 struct partial_symtab *pst;
5858
5859 gdb_assert (data == NULL);
5860 gdb_assert (per_cu->is_debug_types);
5861 sig_type = (struct signatured_type *) per_cu;
5862
5863 if (! has_children)
5864 return;
5865
5866 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
5867 tu_group = get_type_unit_group (cu, attr);
5868
5869 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
5870
5871 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5872 cu->list_in_scope = &file_symbols;
5873 pst = create_partial_symtab (per_cu, "");
5874 pst->anonymous = 1;
5875
5876 first_die = load_partial_dies (reader, info_ptr, 1);
5877
5878 lowpc = (CORE_ADDR) -1;
5879 highpc = (CORE_ADDR) 0;
5880 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5881
5882 pst->n_global_syms = objfile->global_psymbols.next -
5883 (objfile->global_psymbols.list + pst->globals_offset);
5884 pst->n_static_syms = objfile->static_psymbols.next -
5885 (objfile->static_psymbols.list + pst->statics_offset);
5886 sort_pst_symbols (objfile, pst);
5887 }
5888
5889 /* Traversal function for build_type_psymtabs. */
5890
5891 static int
5892 build_type_psymtab_dependencies (void **slot, void *info)
5893 {
5894 struct objfile *objfile = dwarf2_per_objfile->objfile;
5895 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5896 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5897 struct partial_symtab *pst = per_cu->v.psymtab;
5898 int len = VEC_length (sig_type_ptr, tu_group->tus);
5899 struct signatured_type *iter;
5900 int i;
5901
5902 gdb_assert (len > 0);
5903 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
5904
5905 pst->number_of_dependencies = len;
5906 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5907 len * sizeof (struct psymtab *));
5908 for (i = 0;
5909 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
5910 ++i)
5911 {
5912 gdb_assert (iter->per_cu.is_debug_types);
5913 pst->dependencies[i] = iter->per_cu.v.psymtab;
5914 iter->type_unit_group = tu_group;
5915 }
5916
5917 VEC_free (sig_type_ptr, tu_group->tus);
5918
5919 return 1;
5920 }
5921
5922 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5923 Build partial symbol tables for the .debug_types comp-units. */
5924
5925 static void
5926 build_type_psymtabs (struct objfile *objfile)
5927 {
5928 if (! create_all_type_units (objfile))
5929 return;
5930
5931 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5932
5933 /* Now that all TUs have been processed we can fill in the dependencies. */
5934 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5935 build_type_psymtab_dependencies, NULL);
5936 }
5937
5938 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
5939
5940 static void
5941 psymtabs_addrmap_cleanup (void *o)
5942 {
5943 struct objfile *objfile = o;
5944
5945 objfile->psymtabs_addrmap = NULL;
5946 }
5947
5948 /* Compute the 'user' field for each psymtab in OBJFILE. */
5949
5950 static void
5951 set_partial_user (struct objfile *objfile)
5952 {
5953 int i;
5954
5955 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5956 {
5957 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5958 struct partial_symtab *pst = per_cu->v.psymtab;
5959 int j;
5960
5961 if (pst == NULL)
5962 continue;
5963
5964 for (j = 0; j < pst->number_of_dependencies; ++j)
5965 {
5966 /* Set the 'user' field only if it is not already set. */
5967 if (pst->dependencies[j]->user == NULL)
5968 pst->dependencies[j]->user = pst;
5969 }
5970 }
5971 }
5972
5973 /* Build the partial symbol table by doing a quick pass through the
5974 .debug_info and .debug_abbrev sections. */
5975
5976 static void
5977 dwarf2_build_psymtabs_hard (struct objfile *objfile)
5978 {
5979 struct cleanup *back_to, *addrmap_cleanup;
5980 struct obstack temp_obstack;
5981 int i;
5982
5983 if (dwarf2_read_debug)
5984 {
5985 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5986 objfile_name (objfile));
5987 }
5988
5989 dwarf2_per_objfile->reading_partial_symbols = 1;
5990
5991 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5992
5993 /* Any cached compilation units will be linked by the per-objfile
5994 read_in_chain. Make sure to free them when we're done. */
5995 back_to = make_cleanup (free_cached_comp_units, NULL);
5996
5997 build_type_psymtabs (objfile);
5998
5999 create_all_comp_units (objfile);
6000
6001 /* Create a temporary address map on a temporary obstack. We later
6002 copy this to the final obstack. */
6003 obstack_init (&temp_obstack);
6004 make_cleanup_obstack_free (&temp_obstack);
6005 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6006 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
6007
6008 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6009 {
6010 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
6011
6012 process_psymtab_comp_unit (per_cu, 0, language_minimal);
6013 }
6014
6015 set_partial_user (objfile);
6016
6017 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6018 &objfile->objfile_obstack);
6019 discard_cleanups (addrmap_cleanup);
6020
6021 do_cleanups (back_to);
6022
6023 if (dwarf2_read_debug)
6024 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
6025 objfile_name (objfile));
6026 }
6027
6028 /* die_reader_func for load_partial_comp_unit. */
6029
6030 static void
6031 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
6032 const gdb_byte *info_ptr,
6033 struct die_info *comp_unit_die,
6034 int has_children,
6035 void *data)
6036 {
6037 struct dwarf2_cu *cu = reader->cu;
6038
6039 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
6040
6041 /* Check if comp unit has_children.
6042 If so, read the rest of the partial symbols from this comp unit.
6043 If not, there's no more debug_info for this comp unit. */
6044 if (has_children)
6045 load_partial_dies (reader, info_ptr, 0);
6046 }
6047
6048 /* Load the partial DIEs for a secondary CU into memory.
6049 This is also used when rereading a primary CU with load_all_dies. */
6050
6051 static void
6052 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6053 {
6054 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6055 load_partial_comp_unit_reader, NULL);
6056 }
6057
6058 static void
6059 read_comp_units_from_section (struct objfile *objfile,
6060 struct dwarf2_section_info *section,
6061 unsigned int is_dwz,
6062 int *n_allocated,
6063 int *n_comp_units,
6064 struct dwarf2_per_cu_data ***all_comp_units)
6065 {
6066 const gdb_byte *info_ptr;
6067 bfd *abfd = section->asection->owner;
6068
6069 if (dwarf2_read_debug)
6070 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
6071 section->asection->name, bfd_get_filename (abfd));
6072
6073 dwarf2_read_section (objfile, section);
6074
6075 info_ptr = section->buffer;
6076
6077 while (info_ptr < section->buffer + section->size)
6078 {
6079 unsigned int length, initial_length_size;
6080 struct dwarf2_per_cu_data *this_cu;
6081 sect_offset offset;
6082
6083 offset.sect_off = info_ptr - section->buffer;
6084
6085 /* Read just enough information to find out where the next
6086 compilation unit is. */
6087 length = read_initial_length (abfd, info_ptr, &initial_length_size);
6088
6089 /* Save the compilation unit for later lookup. */
6090 this_cu = obstack_alloc (&objfile->objfile_obstack,
6091 sizeof (struct dwarf2_per_cu_data));
6092 memset (this_cu, 0, sizeof (*this_cu));
6093 this_cu->offset = offset;
6094 this_cu->length = length + initial_length_size;
6095 this_cu->is_dwz = is_dwz;
6096 this_cu->objfile = objfile;
6097 this_cu->section = section;
6098
6099 if (*n_comp_units == *n_allocated)
6100 {
6101 *n_allocated *= 2;
6102 *all_comp_units = xrealloc (*all_comp_units,
6103 *n_allocated
6104 * sizeof (struct dwarf2_per_cu_data *));
6105 }
6106 (*all_comp_units)[*n_comp_units] = this_cu;
6107 ++*n_comp_units;
6108
6109 info_ptr = info_ptr + this_cu->length;
6110 }
6111 }
6112
6113 /* Create a list of all compilation units in OBJFILE.
6114 This is only done for -readnow and building partial symtabs. */
6115
6116 static void
6117 create_all_comp_units (struct objfile *objfile)
6118 {
6119 int n_allocated;
6120 int n_comp_units;
6121 struct dwarf2_per_cu_data **all_comp_units;
6122 struct dwz_file *dwz;
6123
6124 n_comp_units = 0;
6125 n_allocated = 10;
6126 all_comp_units = xmalloc (n_allocated
6127 * sizeof (struct dwarf2_per_cu_data *));
6128
6129 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6130 &n_allocated, &n_comp_units, &all_comp_units);
6131
6132 dwz = dwarf2_get_dwz_file ();
6133 if (dwz != NULL)
6134 read_comp_units_from_section (objfile, &dwz->info, 1,
6135 &n_allocated, &n_comp_units,
6136 &all_comp_units);
6137
6138 dwarf2_per_objfile->all_comp_units
6139 = obstack_alloc (&objfile->objfile_obstack,
6140 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6141 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6142 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6143 xfree (all_comp_units);
6144 dwarf2_per_objfile->n_comp_units = n_comp_units;
6145 }
6146
6147 /* Process all loaded DIEs for compilation unit CU, starting at
6148 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
6149 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
6150 DW_AT_ranges). If NEED_PC is set, then this function will set
6151 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
6152 and record the covered ranges in the addrmap. */
6153
6154 static void
6155 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
6156 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6157 {
6158 struct partial_die_info *pdi;
6159
6160 /* Now, march along the PDI's, descending into ones which have
6161 interesting children but skipping the children of the other ones,
6162 until we reach the end of the compilation unit. */
6163
6164 pdi = first_die;
6165
6166 while (pdi != NULL)
6167 {
6168 fixup_partial_die (pdi, cu);
6169
6170 /* Anonymous namespaces or modules have no name but have interesting
6171 children, so we need to look at them. Ditto for anonymous
6172 enums. */
6173
6174 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
6175 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6176 || pdi->tag == DW_TAG_imported_unit)
6177 {
6178 switch (pdi->tag)
6179 {
6180 case DW_TAG_subprogram:
6181 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6182 break;
6183 case DW_TAG_constant:
6184 case DW_TAG_variable:
6185 case DW_TAG_typedef:
6186 case DW_TAG_union_type:
6187 if (!pdi->is_declaration)
6188 {
6189 add_partial_symbol (pdi, cu);
6190 }
6191 break;
6192 case DW_TAG_class_type:
6193 case DW_TAG_interface_type:
6194 case DW_TAG_structure_type:
6195 if (!pdi->is_declaration)
6196 {
6197 add_partial_symbol (pdi, cu);
6198 }
6199 break;
6200 case DW_TAG_enumeration_type:
6201 if (!pdi->is_declaration)
6202 add_partial_enumeration (pdi, cu);
6203 break;
6204 case DW_TAG_base_type:
6205 case DW_TAG_subrange_type:
6206 /* File scope base type definitions are added to the partial
6207 symbol table. */
6208 add_partial_symbol (pdi, cu);
6209 break;
6210 case DW_TAG_namespace:
6211 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
6212 break;
6213 case DW_TAG_module:
6214 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
6215 break;
6216 case DW_TAG_imported_unit:
6217 {
6218 struct dwarf2_per_cu_data *per_cu;
6219
6220 /* For now we don't handle imported units in type units. */
6221 if (cu->per_cu->is_debug_types)
6222 {
6223 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6224 " supported in type units [in module %s]"),
6225 objfile_name (cu->objfile));
6226 }
6227
6228 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
6229 pdi->is_dwz,
6230 cu->objfile);
6231
6232 /* Go read the partial unit, if needed. */
6233 if (per_cu->v.psymtab == NULL)
6234 process_psymtab_comp_unit (per_cu, 1, cu->language);
6235
6236 VEC_safe_push (dwarf2_per_cu_ptr,
6237 cu->per_cu->imported_symtabs, per_cu);
6238 }
6239 break;
6240 default:
6241 break;
6242 }
6243 }
6244
6245 /* If the die has a sibling, skip to the sibling. */
6246
6247 pdi = pdi->die_sibling;
6248 }
6249 }
6250
6251 /* Functions used to compute the fully scoped name of a partial DIE.
6252
6253 Normally, this is simple. For C++, the parent DIE's fully scoped
6254 name is concatenated with "::" and the partial DIE's name. For
6255 Java, the same thing occurs except that "." is used instead of "::".
6256 Enumerators are an exception; they use the scope of their parent
6257 enumeration type, i.e. the name of the enumeration type is not
6258 prepended to the enumerator.
6259
6260 There are two complexities. One is DW_AT_specification; in this
6261 case "parent" means the parent of the target of the specification,
6262 instead of the direct parent of the DIE. The other is compilers
6263 which do not emit DW_TAG_namespace; in this case we try to guess
6264 the fully qualified name of structure types from their members'
6265 linkage names. This must be done using the DIE's children rather
6266 than the children of any DW_AT_specification target. We only need
6267 to do this for structures at the top level, i.e. if the target of
6268 any DW_AT_specification (if any; otherwise the DIE itself) does not
6269 have a parent. */
6270
6271 /* Compute the scope prefix associated with PDI's parent, in
6272 compilation unit CU. The result will be allocated on CU's
6273 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6274 field. NULL is returned if no prefix is necessary. */
6275 static const char *
6276 partial_die_parent_scope (struct partial_die_info *pdi,
6277 struct dwarf2_cu *cu)
6278 {
6279 const char *grandparent_scope;
6280 struct partial_die_info *parent, *real_pdi;
6281
6282 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6283 then this means the parent of the specification DIE. */
6284
6285 real_pdi = pdi;
6286 while (real_pdi->has_specification)
6287 real_pdi = find_partial_die (real_pdi->spec_offset,
6288 real_pdi->spec_is_dwz, cu);
6289
6290 parent = real_pdi->die_parent;
6291 if (parent == NULL)
6292 return NULL;
6293
6294 if (parent->scope_set)
6295 return parent->scope;
6296
6297 fixup_partial_die (parent, cu);
6298
6299 grandparent_scope = partial_die_parent_scope (parent, cu);
6300
6301 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6302 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6303 Work around this problem here. */
6304 if (cu->language == language_cplus
6305 && parent->tag == DW_TAG_namespace
6306 && strcmp (parent->name, "::") == 0
6307 && grandparent_scope == NULL)
6308 {
6309 parent->scope = NULL;
6310 parent->scope_set = 1;
6311 return NULL;
6312 }
6313
6314 if (pdi->tag == DW_TAG_enumerator)
6315 /* Enumerators should not get the name of the enumeration as a prefix. */
6316 parent->scope = grandparent_scope;
6317 else if (parent->tag == DW_TAG_namespace
6318 || parent->tag == DW_TAG_module
6319 || parent->tag == DW_TAG_structure_type
6320 || parent->tag == DW_TAG_class_type
6321 || parent->tag == DW_TAG_interface_type
6322 || parent->tag == DW_TAG_union_type
6323 || parent->tag == DW_TAG_enumeration_type)
6324 {
6325 if (grandparent_scope == NULL)
6326 parent->scope = parent->name;
6327 else
6328 parent->scope = typename_concat (&cu->comp_unit_obstack,
6329 grandparent_scope,
6330 parent->name, 0, cu);
6331 }
6332 else
6333 {
6334 /* FIXME drow/2004-04-01: What should we be doing with
6335 function-local names? For partial symbols, we should probably be
6336 ignoring them. */
6337 complaint (&symfile_complaints,
6338 _("unhandled containing DIE tag %d for DIE at %d"),
6339 parent->tag, pdi->offset.sect_off);
6340 parent->scope = grandparent_scope;
6341 }
6342
6343 parent->scope_set = 1;
6344 return parent->scope;
6345 }
6346
6347 /* Return the fully scoped name associated with PDI, from compilation unit
6348 CU. The result will be allocated with malloc. */
6349
6350 static char *
6351 partial_die_full_name (struct partial_die_info *pdi,
6352 struct dwarf2_cu *cu)
6353 {
6354 const char *parent_scope;
6355
6356 /* If this is a template instantiation, we can not work out the
6357 template arguments from partial DIEs. So, unfortunately, we have
6358 to go through the full DIEs. At least any work we do building
6359 types here will be reused if full symbols are loaded later. */
6360 if (pdi->has_template_arguments)
6361 {
6362 fixup_partial_die (pdi, cu);
6363
6364 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6365 {
6366 struct die_info *die;
6367 struct attribute attr;
6368 struct dwarf2_cu *ref_cu = cu;
6369
6370 /* DW_FORM_ref_addr is using section offset. */
6371 attr.name = 0;
6372 attr.form = DW_FORM_ref_addr;
6373 attr.u.unsnd = pdi->offset.sect_off;
6374 die = follow_die_ref (NULL, &attr, &ref_cu);
6375
6376 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6377 }
6378 }
6379
6380 parent_scope = partial_die_parent_scope (pdi, cu);
6381 if (parent_scope == NULL)
6382 return NULL;
6383 else
6384 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
6385 }
6386
6387 static void
6388 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
6389 {
6390 struct objfile *objfile = cu->objfile;
6391 CORE_ADDR addr = 0;
6392 const char *actual_name = NULL;
6393 CORE_ADDR baseaddr;
6394 char *built_actual_name;
6395
6396 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6397
6398 built_actual_name = partial_die_full_name (pdi, cu);
6399 if (built_actual_name != NULL)
6400 actual_name = built_actual_name;
6401
6402 if (actual_name == NULL)
6403 actual_name = pdi->name;
6404
6405 switch (pdi->tag)
6406 {
6407 case DW_TAG_subprogram:
6408 if (pdi->is_external || cu->language == language_ada)
6409 {
6410 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6411 of the global scope. But in Ada, we want to be able to access
6412 nested procedures globally. So all Ada subprograms are stored
6413 in the global scope. */
6414 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
6415 mst_text, objfile); */
6416 add_psymbol_to_list (actual_name, strlen (actual_name),
6417 built_actual_name != NULL,
6418 VAR_DOMAIN, LOC_BLOCK,
6419 &objfile->global_psymbols,
6420 0, pdi->lowpc + baseaddr,
6421 cu->language, objfile);
6422 }
6423 else
6424 {
6425 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
6426 mst_file_text, objfile); */
6427 add_psymbol_to_list (actual_name, strlen (actual_name),
6428 built_actual_name != NULL,
6429 VAR_DOMAIN, LOC_BLOCK,
6430 &objfile->static_psymbols,
6431 0, pdi->lowpc + baseaddr,
6432 cu->language, objfile);
6433 }
6434 break;
6435 case DW_TAG_constant:
6436 {
6437 struct psymbol_allocation_list *list;
6438
6439 if (pdi->is_external)
6440 list = &objfile->global_psymbols;
6441 else
6442 list = &objfile->static_psymbols;
6443 add_psymbol_to_list (actual_name, strlen (actual_name),
6444 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
6445 list, 0, 0, cu->language, objfile);
6446 }
6447 break;
6448 case DW_TAG_variable:
6449 if (pdi->d.locdesc)
6450 addr = decode_locdesc (pdi->d.locdesc, cu);
6451
6452 if (pdi->d.locdesc
6453 && addr == 0
6454 && !dwarf2_per_objfile->has_section_at_zero)
6455 {
6456 /* A global or static variable may also have been stripped
6457 out by the linker if unused, in which case its address
6458 will be nullified; do not add such variables into partial
6459 symbol table then. */
6460 }
6461 else if (pdi->is_external)
6462 {
6463 /* Global Variable.
6464 Don't enter into the minimal symbol tables as there is
6465 a minimal symbol table entry from the ELF symbols already.
6466 Enter into partial symbol table if it has a location
6467 descriptor or a type.
6468 If the location descriptor is missing, new_symbol will create
6469 a LOC_UNRESOLVED symbol, the address of the variable will then
6470 be determined from the minimal symbol table whenever the variable
6471 is referenced.
6472 The address for the partial symbol table entry is not
6473 used by GDB, but it comes in handy for debugging partial symbol
6474 table building. */
6475
6476 if (pdi->d.locdesc || pdi->has_type)
6477 add_psymbol_to_list (actual_name, strlen (actual_name),
6478 built_actual_name != NULL,
6479 VAR_DOMAIN, LOC_STATIC,
6480 &objfile->global_psymbols,
6481 0, addr + baseaddr,
6482 cu->language, objfile);
6483 }
6484 else
6485 {
6486 /* Static Variable. Skip symbols without location descriptors. */
6487 if (pdi->d.locdesc == NULL)
6488 {
6489 xfree (built_actual_name);
6490 return;
6491 }
6492 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
6493 mst_file_data, objfile); */
6494 add_psymbol_to_list (actual_name, strlen (actual_name),
6495 built_actual_name != NULL,
6496 VAR_DOMAIN, LOC_STATIC,
6497 &objfile->static_psymbols,
6498 0, addr + baseaddr,
6499 cu->language, objfile);
6500 }
6501 break;
6502 case DW_TAG_typedef:
6503 case DW_TAG_base_type:
6504 case DW_TAG_subrange_type:
6505 add_psymbol_to_list (actual_name, strlen (actual_name),
6506 built_actual_name != NULL,
6507 VAR_DOMAIN, LOC_TYPEDEF,
6508 &objfile->static_psymbols,
6509 0, (CORE_ADDR) 0, cu->language, objfile);
6510 break;
6511 case DW_TAG_namespace:
6512 add_psymbol_to_list (actual_name, strlen (actual_name),
6513 built_actual_name != NULL,
6514 VAR_DOMAIN, LOC_TYPEDEF,
6515 &objfile->global_psymbols,
6516 0, (CORE_ADDR) 0, cu->language, objfile);
6517 break;
6518 case DW_TAG_class_type:
6519 case DW_TAG_interface_type:
6520 case DW_TAG_structure_type:
6521 case DW_TAG_union_type:
6522 case DW_TAG_enumeration_type:
6523 /* Skip external references. The DWARF standard says in the section
6524 about "Structure, Union, and Class Type Entries": "An incomplete
6525 structure, union or class type is represented by a structure,
6526 union or class entry that does not have a byte size attribute
6527 and that has a DW_AT_declaration attribute." */
6528 if (!pdi->has_byte_size && pdi->is_declaration)
6529 {
6530 xfree (built_actual_name);
6531 return;
6532 }
6533
6534 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6535 static vs. global. */
6536 add_psymbol_to_list (actual_name, strlen (actual_name),
6537 built_actual_name != NULL,
6538 STRUCT_DOMAIN, LOC_TYPEDEF,
6539 (cu->language == language_cplus
6540 || cu->language == language_java)
6541 ? &objfile->global_psymbols
6542 : &objfile->static_psymbols,
6543 0, (CORE_ADDR) 0, cu->language, objfile);
6544
6545 break;
6546 case DW_TAG_enumerator:
6547 add_psymbol_to_list (actual_name, strlen (actual_name),
6548 built_actual_name != NULL,
6549 VAR_DOMAIN, LOC_CONST,
6550 (cu->language == language_cplus
6551 || cu->language == language_java)
6552 ? &objfile->global_psymbols
6553 : &objfile->static_psymbols,
6554 0, (CORE_ADDR) 0, cu->language, objfile);
6555 break;
6556 default:
6557 break;
6558 }
6559
6560 xfree (built_actual_name);
6561 }
6562
6563 /* Read a partial die corresponding to a namespace; also, add a symbol
6564 corresponding to that namespace to the symbol table. NAMESPACE is
6565 the name of the enclosing namespace. */
6566
6567 static void
6568 add_partial_namespace (struct partial_die_info *pdi,
6569 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6570 int need_pc, struct dwarf2_cu *cu)
6571 {
6572 /* Add a symbol for the namespace. */
6573
6574 add_partial_symbol (pdi, cu);
6575
6576 /* Now scan partial symbols in that namespace. */
6577
6578 if (pdi->has_children)
6579 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6580 }
6581
6582 /* Read a partial die corresponding to a Fortran module. */
6583
6584 static void
6585 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6586 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6587 {
6588 /* Now scan partial symbols in that module. */
6589
6590 if (pdi->has_children)
6591 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6592 }
6593
6594 /* Read a partial die corresponding to a subprogram and create a partial
6595 symbol for that subprogram. When the CU language allows it, this
6596 routine also defines a partial symbol for each nested subprogram
6597 that this subprogram contains.
6598
6599 DIE my also be a lexical block, in which case we simply search
6600 recursively for suprograms defined inside that lexical block.
6601 Again, this is only performed when the CU language allows this
6602 type of definitions. */
6603
6604 static void
6605 add_partial_subprogram (struct partial_die_info *pdi,
6606 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6607 int need_pc, struct dwarf2_cu *cu)
6608 {
6609 if (pdi->tag == DW_TAG_subprogram)
6610 {
6611 if (pdi->has_pc_info)
6612 {
6613 if (pdi->lowpc < *lowpc)
6614 *lowpc = pdi->lowpc;
6615 if (pdi->highpc > *highpc)
6616 *highpc = pdi->highpc;
6617 if (need_pc)
6618 {
6619 CORE_ADDR baseaddr;
6620 struct objfile *objfile = cu->objfile;
6621
6622 baseaddr = ANOFFSET (objfile->section_offsets,
6623 SECT_OFF_TEXT (objfile));
6624 addrmap_set_empty (objfile->psymtabs_addrmap,
6625 pdi->lowpc + baseaddr,
6626 pdi->highpc - 1 + baseaddr,
6627 cu->per_cu->v.psymtab);
6628 }
6629 }
6630
6631 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6632 {
6633 if (!pdi->is_declaration)
6634 /* Ignore subprogram DIEs that do not have a name, they are
6635 illegal. Do not emit a complaint at this point, we will
6636 do so when we convert this psymtab into a symtab. */
6637 if (pdi->name)
6638 add_partial_symbol (pdi, cu);
6639 }
6640 }
6641
6642 if (! pdi->has_children)
6643 return;
6644
6645 if (cu->language == language_ada)
6646 {
6647 pdi = pdi->die_child;
6648 while (pdi != NULL)
6649 {
6650 fixup_partial_die (pdi, cu);
6651 if (pdi->tag == DW_TAG_subprogram
6652 || pdi->tag == DW_TAG_lexical_block)
6653 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6654 pdi = pdi->die_sibling;
6655 }
6656 }
6657 }
6658
6659 /* Read a partial die corresponding to an enumeration type. */
6660
6661 static void
6662 add_partial_enumeration (struct partial_die_info *enum_pdi,
6663 struct dwarf2_cu *cu)
6664 {
6665 struct partial_die_info *pdi;
6666
6667 if (enum_pdi->name != NULL)
6668 add_partial_symbol (enum_pdi, cu);
6669
6670 pdi = enum_pdi->die_child;
6671 while (pdi)
6672 {
6673 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
6674 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6675 else
6676 add_partial_symbol (pdi, cu);
6677 pdi = pdi->die_sibling;
6678 }
6679 }
6680
6681 /* Return the initial uleb128 in the die at INFO_PTR. */
6682
6683 static unsigned int
6684 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6685 {
6686 unsigned int bytes_read;
6687
6688 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6689 }
6690
6691 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6692 Return the corresponding abbrev, or NULL if the number is zero (indicating
6693 an empty DIE). In either case *BYTES_READ will be set to the length of
6694 the initial number. */
6695
6696 static struct abbrev_info *
6697 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
6698 struct dwarf2_cu *cu)
6699 {
6700 bfd *abfd = cu->objfile->obfd;
6701 unsigned int abbrev_number;
6702 struct abbrev_info *abbrev;
6703
6704 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6705
6706 if (abbrev_number == 0)
6707 return NULL;
6708
6709 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
6710 if (!abbrev)
6711 {
6712 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6713 abbrev_number, bfd_get_filename (abfd));
6714 }
6715
6716 return abbrev;
6717 }
6718
6719 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6720 Returns a pointer to the end of a series of DIEs, terminated by an empty
6721 DIE. Any children of the skipped DIEs will also be skipped. */
6722
6723 static const gdb_byte *
6724 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
6725 {
6726 struct dwarf2_cu *cu = reader->cu;
6727 struct abbrev_info *abbrev;
6728 unsigned int bytes_read;
6729
6730 while (1)
6731 {
6732 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6733 if (abbrev == NULL)
6734 return info_ptr + bytes_read;
6735 else
6736 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
6737 }
6738 }
6739
6740 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6741 INFO_PTR should point just after the initial uleb128 of a DIE, and the
6742 abbrev corresponding to that skipped uleb128 should be passed in
6743 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6744 children. */
6745
6746 static const gdb_byte *
6747 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
6748 struct abbrev_info *abbrev)
6749 {
6750 unsigned int bytes_read;
6751 struct attribute attr;
6752 bfd *abfd = reader->abfd;
6753 struct dwarf2_cu *cu = reader->cu;
6754 const gdb_byte *buffer = reader->buffer;
6755 const gdb_byte *buffer_end = reader->buffer_end;
6756 const gdb_byte *start_info_ptr = info_ptr;
6757 unsigned int form, i;
6758
6759 for (i = 0; i < abbrev->num_attrs; i++)
6760 {
6761 /* The only abbrev we care about is DW_AT_sibling. */
6762 if (abbrev->attrs[i].name == DW_AT_sibling)
6763 {
6764 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
6765 if (attr.form == DW_FORM_ref_addr)
6766 complaint (&symfile_complaints,
6767 _("ignoring absolute DW_AT_sibling"));
6768 else
6769 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
6770 }
6771
6772 /* If it isn't DW_AT_sibling, skip this attribute. */
6773 form = abbrev->attrs[i].form;
6774 skip_attribute:
6775 switch (form)
6776 {
6777 case DW_FORM_ref_addr:
6778 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6779 and later it is offset sized. */
6780 if (cu->header.version == 2)
6781 info_ptr += cu->header.addr_size;
6782 else
6783 info_ptr += cu->header.offset_size;
6784 break;
6785 case DW_FORM_GNU_ref_alt:
6786 info_ptr += cu->header.offset_size;
6787 break;
6788 case DW_FORM_addr:
6789 info_ptr += cu->header.addr_size;
6790 break;
6791 case DW_FORM_data1:
6792 case DW_FORM_ref1:
6793 case DW_FORM_flag:
6794 info_ptr += 1;
6795 break;
6796 case DW_FORM_flag_present:
6797 break;
6798 case DW_FORM_data2:
6799 case DW_FORM_ref2:
6800 info_ptr += 2;
6801 break;
6802 case DW_FORM_data4:
6803 case DW_FORM_ref4:
6804 info_ptr += 4;
6805 break;
6806 case DW_FORM_data8:
6807 case DW_FORM_ref8:
6808 case DW_FORM_ref_sig8:
6809 info_ptr += 8;
6810 break;
6811 case DW_FORM_string:
6812 read_direct_string (abfd, info_ptr, &bytes_read);
6813 info_ptr += bytes_read;
6814 break;
6815 case DW_FORM_sec_offset:
6816 case DW_FORM_strp:
6817 case DW_FORM_GNU_strp_alt:
6818 info_ptr += cu->header.offset_size;
6819 break;
6820 case DW_FORM_exprloc:
6821 case DW_FORM_block:
6822 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6823 info_ptr += bytes_read;
6824 break;
6825 case DW_FORM_block1:
6826 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6827 break;
6828 case DW_FORM_block2:
6829 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6830 break;
6831 case DW_FORM_block4:
6832 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6833 break;
6834 case DW_FORM_sdata:
6835 case DW_FORM_udata:
6836 case DW_FORM_ref_udata:
6837 case DW_FORM_GNU_addr_index:
6838 case DW_FORM_GNU_str_index:
6839 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
6840 break;
6841 case DW_FORM_indirect:
6842 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6843 info_ptr += bytes_read;
6844 /* We need to continue parsing from here, so just go back to
6845 the top. */
6846 goto skip_attribute;
6847
6848 default:
6849 error (_("Dwarf Error: Cannot handle %s "
6850 "in DWARF reader [in module %s]"),
6851 dwarf_form_name (form),
6852 bfd_get_filename (abfd));
6853 }
6854 }
6855
6856 if (abbrev->has_children)
6857 return skip_children (reader, info_ptr);
6858 else
6859 return info_ptr;
6860 }
6861
6862 /* Locate ORIG_PDI's sibling.
6863 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
6864
6865 static const gdb_byte *
6866 locate_pdi_sibling (const struct die_reader_specs *reader,
6867 struct partial_die_info *orig_pdi,
6868 const gdb_byte *info_ptr)
6869 {
6870 /* Do we know the sibling already? */
6871
6872 if (orig_pdi->sibling)
6873 return orig_pdi->sibling;
6874
6875 /* Are there any children to deal with? */
6876
6877 if (!orig_pdi->has_children)
6878 return info_ptr;
6879
6880 /* Skip the children the long way. */
6881
6882 return skip_children (reader, info_ptr);
6883 }
6884
6885 /* Expand this partial symbol table into a full symbol table. SELF is
6886 not NULL. */
6887
6888 static void
6889 dwarf2_read_symtab (struct partial_symtab *self,
6890 struct objfile *objfile)
6891 {
6892 if (self->readin)
6893 {
6894 warning (_("bug: psymtab for %s is already read in."),
6895 self->filename);
6896 }
6897 else
6898 {
6899 if (info_verbose)
6900 {
6901 printf_filtered (_("Reading in symbols for %s..."),
6902 self->filename);
6903 gdb_flush (gdb_stdout);
6904 }
6905
6906 /* Restore our global data. */
6907 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
6908
6909 /* If this psymtab is constructed from a debug-only objfile, the
6910 has_section_at_zero flag will not necessarily be correct. We
6911 can get the correct value for this flag by looking at the data
6912 associated with the (presumably stripped) associated objfile. */
6913 if (objfile->separate_debug_objfile_backlink)
6914 {
6915 struct dwarf2_per_objfile *dpo_backlink
6916 = objfile_data (objfile->separate_debug_objfile_backlink,
6917 dwarf2_objfile_data_key);
6918
6919 dwarf2_per_objfile->has_section_at_zero
6920 = dpo_backlink->has_section_at_zero;
6921 }
6922
6923 dwarf2_per_objfile->reading_partial_symbols = 0;
6924
6925 psymtab_to_symtab_1 (self);
6926
6927 /* Finish up the debug error message. */
6928 if (info_verbose)
6929 printf_filtered (_("done.\n"));
6930 }
6931
6932 process_cu_includes ();
6933 }
6934 \f
6935 /* Reading in full CUs. */
6936
6937 /* Add PER_CU to the queue. */
6938
6939 static void
6940 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6941 enum language pretend_language)
6942 {
6943 struct dwarf2_queue_item *item;
6944
6945 per_cu->queued = 1;
6946 item = xmalloc (sizeof (*item));
6947 item->per_cu = per_cu;
6948 item->pretend_language = pretend_language;
6949 item->next = NULL;
6950
6951 if (dwarf2_queue == NULL)
6952 dwarf2_queue = item;
6953 else
6954 dwarf2_queue_tail->next = item;
6955
6956 dwarf2_queue_tail = item;
6957 }
6958
6959 /* If PER_CU is not yet queued, add it to the queue.
6960 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
6961 dependency.
6962 The result is non-zero if PER_CU was queued, otherwise the result is zero
6963 meaning either PER_CU is already queued or it is already loaded.
6964
6965 N.B. There is an invariant here that if a CU is queued then it is loaded.
6966 The caller is required to load PER_CU if we return non-zero. */
6967
6968 static int
6969 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
6970 struct dwarf2_per_cu_data *per_cu,
6971 enum language pretend_language)
6972 {
6973 /* We may arrive here during partial symbol reading, if we need full
6974 DIEs to process an unusual case (e.g. template arguments). Do
6975 not queue PER_CU, just tell our caller to load its DIEs. */
6976 if (dwarf2_per_objfile->reading_partial_symbols)
6977 {
6978 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6979 return 1;
6980 return 0;
6981 }
6982
6983 /* Mark the dependence relation so that we don't flush PER_CU
6984 too early. */
6985 if (dependent_cu != NULL)
6986 dwarf2_add_dependence (dependent_cu, per_cu);
6987
6988 /* If it's already on the queue, we have nothing to do. */
6989 if (per_cu->queued)
6990 return 0;
6991
6992 /* If the compilation unit is already loaded, just mark it as
6993 used. */
6994 if (per_cu->cu != NULL)
6995 {
6996 per_cu->cu->last_used = 0;
6997 return 0;
6998 }
6999
7000 /* Add it to the queue. */
7001 queue_comp_unit (per_cu, pretend_language);
7002
7003 return 1;
7004 }
7005
7006 /* Process the queue. */
7007
7008 static void
7009 process_queue (void)
7010 {
7011 struct dwarf2_queue_item *item, *next_item;
7012
7013 if (dwarf2_read_debug)
7014 {
7015 fprintf_unfiltered (gdb_stdlog,
7016 "Expanding one or more symtabs of objfile %s ...\n",
7017 objfile_name (dwarf2_per_objfile->objfile));
7018 }
7019
7020 /* The queue starts out with one item, but following a DIE reference
7021 may load a new CU, adding it to the end of the queue. */
7022 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7023 {
7024 if (dwarf2_per_objfile->using_index
7025 ? !item->per_cu->v.quick->symtab
7026 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7027 {
7028 struct dwarf2_per_cu_data *per_cu = item->per_cu;
7029 char buf[100];
7030
7031 if (per_cu->is_debug_types)
7032 {
7033 struct signatured_type *sig_type =
7034 (struct signatured_type *) per_cu;
7035
7036 sprintf (buf, "TU %s at offset 0x%x",
7037 hex_string (sig_type->signature), per_cu->offset.sect_off);
7038 }
7039 else
7040 sprintf (buf, "CU at offset 0x%x", per_cu->offset.sect_off);
7041
7042 if (dwarf2_read_debug)
7043 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
7044
7045 if (per_cu->is_debug_types)
7046 process_full_type_unit (per_cu, item->pretend_language);
7047 else
7048 process_full_comp_unit (per_cu, item->pretend_language);
7049
7050 if (dwarf2_read_debug)
7051 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
7052 }
7053
7054 item->per_cu->queued = 0;
7055 next_item = item->next;
7056 xfree (item);
7057 }
7058
7059 dwarf2_queue_tail = NULL;
7060
7061 if (dwarf2_read_debug)
7062 {
7063 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
7064 objfile_name (dwarf2_per_objfile->objfile));
7065 }
7066 }
7067
7068 /* Free all allocated queue entries. This function only releases anything if
7069 an error was thrown; if the queue was processed then it would have been
7070 freed as we went along. */
7071
7072 static void
7073 dwarf2_release_queue (void *dummy)
7074 {
7075 struct dwarf2_queue_item *item, *last;
7076
7077 item = dwarf2_queue;
7078 while (item)
7079 {
7080 /* Anything still marked queued is likely to be in an
7081 inconsistent state, so discard it. */
7082 if (item->per_cu->queued)
7083 {
7084 if (item->per_cu->cu != NULL)
7085 free_one_cached_comp_unit (item->per_cu);
7086 item->per_cu->queued = 0;
7087 }
7088
7089 last = item;
7090 item = item->next;
7091 xfree (last);
7092 }
7093
7094 dwarf2_queue = dwarf2_queue_tail = NULL;
7095 }
7096
7097 /* Read in full symbols for PST, and anything it depends on. */
7098
7099 static void
7100 psymtab_to_symtab_1 (struct partial_symtab *pst)
7101 {
7102 struct dwarf2_per_cu_data *per_cu;
7103 int i;
7104
7105 if (pst->readin)
7106 return;
7107
7108 for (i = 0; i < pst->number_of_dependencies; i++)
7109 if (!pst->dependencies[i]->readin
7110 && pst->dependencies[i]->user == NULL)
7111 {
7112 /* Inform about additional files that need to be read in. */
7113 if (info_verbose)
7114 {
7115 /* FIXME: i18n: Need to make this a single string. */
7116 fputs_filtered (" ", gdb_stdout);
7117 wrap_here ("");
7118 fputs_filtered ("and ", gdb_stdout);
7119 wrap_here ("");
7120 printf_filtered ("%s...", pst->dependencies[i]->filename);
7121 wrap_here (""); /* Flush output. */
7122 gdb_flush (gdb_stdout);
7123 }
7124 psymtab_to_symtab_1 (pst->dependencies[i]);
7125 }
7126
7127 per_cu = pst->read_symtab_private;
7128
7129 if (per_cu == NULL)
7130 {
7131 /* It's an include file, no symbols to read for it.
7132 Everything is in the parent symtab. */
7133 pst->readin = 1;
7134 return;
7135 }
7136
7137 dw2_do_instantiate_symtab (per_cu);
7138 }
7139
7140 /* Trivial hash function for die_info: the hash value of a DIE
7141 is its offset in .debug_info for this objfile. */
7142
7143 static hashval_t
7144 die_hash (const void *item)
7145 {
7146 const struct die_info *die = item;
7147
7148 return die->offset.sect_off;
7149 }
7150
7151 /* Trivial comparison function for die_info structures: two DIEs
7152 are equal if they have the same offset. */
7153
7154 static int
7155 die_eq (const void *item_lhs, const void *item_rhs)
7156 {
7157 const struct die_info *die_lhs = item_lhs;
7158 const struct die_info *die_rhs = item_rhs;
7159
7160 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
7161 }
7162
7163 /* die_reader_func for load_full_comp_unit.
7164 This is identical to read_signatured_type_reader,
7165 but is kept separate for now. */
7166
7167 static void
7168 load_full_comp_unit_reader (const struct die_reader_specs *reader,
7169 const gdb_byte *info_ptr,
7170 struct die_info *comp_unit_die,
7171 int has_children,
7172 void *data)
7173 {
7174 struct dwarf2_cu *cu = reader->cu;
7175 enum language *language_ptr = data;
7176
7177 gdb_assert (cu->die_hash == NULL);
7178 cu->die_hash =
7179 htab_create_alloc_ex (cu->header.length / 12,
7180 die_hash,
7181 die_eq,
7182 NULL,
7183 &cu->comp_unit_obstack,
7184 hashtab_obstack_allocate,
7185 dummy_obstack_deallocate);
7186
7187 if (has_children)
7188 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7189 &info_ptr, comp_unit_die);
7190 cu->dies = comp_unit_die;
7191 /* comp_unit_die is not stored in die_hash, no need. */
7192
7193 /* We try not to read any attributes in this function, because not
7194 all CUs needed for references have been loaded yet, and symbol
7195 table processing isn't initialized. But we have to set the CU language,
7196 or we won't be able to build types correctly.
7197 Similarly, if we do not read the producer, we can not apply
7198 producer-specific interpretation. */
7199 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
7200 }
7201
7202 /* Load the DIEs associated with PER_CU into memory. */
7203
7204 static void
7205 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7206 enum language pretend_language)
7207 {
7208 gdb_assert (! this_cu->is_debug_types);
7209
7210 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7211 load_full_comp_unit_reader, &pretend_language);
7212 }
7213
7214 /* Add a DIE to the delayed physname list. */
7215
7216 static void
7217 add_to_method_list (struct type *type, int fnfield_index, int index,
7218 const char *name, struct die_info *die,
7219 struct dwarf2_cu *cu)
7220 {
7221 struct delayed_method_info mi;
7222 mi.type = type;
7223 mi.fnfield_index = fnfield_index;
7224 mi.index = index;
7225 mi.name = name;
7226 mi.die = die;
7227 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7228 }
7229
7230 /* A cleanup for freeing the delayed method list. */
7231
7232 static void
7233 free_delayed_list (void *ptr)
7234 {
7235 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7236 if (cu->method_list != NULL)
7237 {
7238 VEC_free (delayed_method_info, cu->method_list);
7239 cu->method_list = NULL;
7240 }
7241 }
7242
7243 /* Compute the physnames of any methods on the CU's method list.
7244
7245 The computation of method physnames is delayed in order to avoid the
7246 (bad) condition that one of the method's formal parameters is of an as yet
7247 incomplete type. */
7248
7249 static void
7250 compute_delayed_physnames (struct dwarf2_cu *cu)
7251 {
7252 int i;
7253 struct delayed_method_info *mi;
7254 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7255 {
7256 const char *physname;
7257 struct fn_fieldlist *fn_flp
7258 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7259 physname = dwarf2_physname (mi->name, mi->die, cu);
7260 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
7261 }
7262 }
7263
7264 /* Go objects should be embedded in a DW_TAG_module DIE,
7265 and it's not clear if/how imported objects will appear.
7266 To keep Go support simple until that's worked out,
7267 go back through what we've read and create something usable.
7268 We could do this while processing each DIE, and feels kinda cleaner,
7269 but that way is more invasive.
7270 This is to, for example, allow the user to type "p var" or "b main"
7271 without having to specify the package name, and allow lookups
7272 of module.object to work in contexts that use the expression
7273 parser. */
7274
7275 static void
7276 fixup_go_packaging (struct dwarf2_cu *cu)
7277 {
7278 char *package_name = NULL;
7279 struct pending *list;
7280 int i;
7281
7282 for (list = global_symbols; list != NULL; list = list->next)
7283 {
7284 for (i = 0; i < list->nsyms; ++i)
7285 {
7286 struct symbol *sym = list->symbol[i];
7287
7288 if (SYMBOL_LANGUAGE (sym) == language_go
7289 && SYMBOL_CLASS (sym) == LOC_BLOCK)
7290 {
7291 char *this_package_name = go_symbol_package_name (sym);
7292
7293 if (this_package_name == NULL)
7294 continue;
7295 if (package_name == NULL)
7296 package_name = this_package_name;
7297 else
7298 {
7299 if (strcmp (package_name, this_package_name) != 0)
7300 complaint (&symfile_complaints,
7301 _("Symtab %s has objects from two different Go packages: %s and %s"),
7302 (SYMBOL_SYMTAB (sym)
7303 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
7304 : objfile_name (cu->objfile)),
7305 this_package_name, package_name);
7306 xfree (this_package_name);
7307 }
7308 }
7309 }
7310 }
7311
7312 if (package_name != NULL)
7313 {
7314 struct objfile *objfile = cu->objfile;
7315 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
7316 package_name,
7317 strlen (package_name));
7318 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
7319 saved_package_name, objfile);
7320 struct symbol *sym;
7321
7322 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7323
7324 sym = allocate_symbol (objfile);
7325 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
7326 SYMBOL_SET_NAMES (sym, saved_package_name,
7327 strlen (saved_package_name), 0, objfile);
7328 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7329 e.g., "main" finds the "main" module and not C's main(). */
7330 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
7331 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
7332 SYMBOL_TYPE (sym) = type;
7333
7334 add_symbol_to_list (sym, &global_symbols);
7335
7336 xfree (package_name);
7337 }
7338 }
7339
7340 /* Return the symtab for PER_CU. This works properly regardless of
7341 whether we're using the index or psymtabs. */
7342
7343 static struct symtab *
7344 get_symtab (struct dwarf2_per_cu_data *per_cu)
7345 {
7346 return (dwarf2_per_objfile->using_index
7347 ? per_cu->v.quick->symtab
7348 : per_cu->v.psymtab->symtab);
7349 }
7350
7351 /* A helper function for computing the list of all symbol tables
7352 included by PER_CU. */
7353
7354 static void
7355 recursively_compute_inclusions (VEC (symtab_ptr) **result,
7356 htab_t all_children, htab_t all_type_symtabs,
7357 struct dwarf2_per_cu_data *per_cu,
7358 struct symtab *immediate_parent)
7359 {
7360 void **slot;
7361 int ix;
7362 struct symtab *symtab;
7363 struct dwarf2_per_cu_data *iter;
7364
7365 slot = htab_find_slot (all_children, per_cu, INSERT);
7366 if (*slot != NULL)
7367 {
7368 /* This inclusion and its children have been processed. */
7369 return;
7370 }
7371
7372 *slot = per_cu;
7373 /* Only add a CU if it has a symbol table. */
7374 symtab = get_symtab (per_cu);
7375 if (symtab != NULL)
7376 {
7377 /* If this is a type unit only add its symbol table if we haven't
7378 seen it yet (type unit per_cu's can share symtabs). */
7379 if (per_cu->is_debug_types)
7380 {
7381 slot = htab_find_slot (all_type_symtabs, symtab, INSERT);
7382 if (*slot == NULL)
7383 {
7384 *slot = symtab;
7385 VEC_safe_push (symtab_ptr, *result, symtab);
7386 if (symtab->user == NULL)
7387 symtab->user = immediate_parent;
7388 }
7389 }
7390 else
7391 {
7392 VEC_safe_push (symtab_ptr, *result, symtab);
7393 if (symtab->user == NULL)
7394 symtab->user = immediate_parent;
7395 }
7396 }
7397
7398 for (ix = 0;
7399 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
7400 ++ix)
7401 {
7402 recursively_compute_inclusions (result, all_children,
7403 all_type_symtabs, iter, symtab);
7404 }
7405 }
7406
7407 /* Compute the symtab 'includes' fields for the symtab related to
7408 PER_CU. */
7409
7410 static void
7411 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7412 {
7413 gdb_assert (! per_cu->is_debug_types);
7414
7415 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
7416 {
7417 int ix, len;
7418 struct dwarf2_per_cu_data *per_cu_iter;
7419 struct symtab *symtab_iter;
7420 VEC (symtab_ptr) *result_symtabs = NULL;
7421 htab_t all_children, all_type_symtabs;
7422 struct symtab *symtab = get_symtab (per_cu);
7423
7424 /* If we don't have a symtab, we can just skip this case. */
7425 if (symtab == NULL)
7426 return;
7427
7428 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7429 NULL, xcalloc, xfree);
7430 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7431 NULL, xcalloc, xfree);
7432
7433 for (ix = 0;
7434 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
7435 ix, per_cu_iter);
7436 ++ix)
7437 {
7438 recursively_compute_inclusions (&result_symtabs, all_children,
7439 all_type_symtabs, per_cu_iter,
7440 symtab);
7441 }
7442
7443 /* Now we have a transitive closure of all the included symtabs. */
7444 len = VEC_length (symtab_ptr, result_symtabs);
7445 symtab->includes
7446 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7447 (len + 1) * sizeof (struct symtab *));
7448 for (ix = 0;
7449 VEC_iterate (symtab_ptr, result_symtabs, ix, symtab_iter);
7450 ++ix)
7451 symtab->includes[ix] = symtab_iter;
7452 symtab->includes[len] = NULL;
7453
7454 VEC_free (symtab_ptr, result_symtabs);
7455 htab_delete (all_children);
7456 htab_delete (all_type_symtabs);
7457 }
7458 }
7459
7460 /* Compute the 'includes' field for the symtabs of all the CUs we just
7461 read. */
7462
7463 static void
7464 process_cu_includes (void)
7465 {
7466 int ix;
7467 struct dwarf2_per_cu_data *iter;
7468
7469 for (ix = 0;
7470 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7471 ix, iter);
7472 ++ix)
7473 {
7474 if (! iter->is_debug_types)
7475 compute_symtab_includes (iter);
7476 }
7477
7478 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
7479 }
7480
7481 /* Generate full symbol information for PER_CU, whose DIEs have
7482 already been loaded into memory. */
7483
7484 static void
7485 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7486 enum language pretend_language)
7487 {
7488 struct dwarf2_cu *cu = per_cu->cu;
7489 struct objfile *objfile = per_cu->objfile;
7490 CORE_ADDR lowpc, highpc;
7491 struct symtab *symtab;
7492 struct cleanup *back_to, *delayed_list_cleanup;
7493 CORE_ADDR baseaddr;
7494 struct block *static_block;
7495
7496 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7497
7498 buildsym_init ();
7499 back_to = make_cleanup (really_free_pendings, NULL);
7500 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7501
7502 cu->list_in_scope = &file_symbols;
7503
7504 cu->language = pretend_language;
7505 cu->language_defn = language_def (cu->language);
7506
7507 /* Do line number decoding in read_file_scope () */
7508 process_die (cu->dies, cu);
7509
7510 /* For now fudge the Go package. */
7511 if (cu->language == language_go)
7512 fixup_go_packaging (cu);
7513
7514 /* Now that we have processed all the DIEs in the CU, all the types
7515 should be complete, and it should now be safe to compute all of the
7516 physnames. */
7517 compute_delayed_physnames (cu);
7518 do_cleanups (delayed_list_cleanup);
7519
7520 /* Some compilers don't define a DW_AT_high_pc attribute for the
7521 compilation unit. If the DW_AT_high_pc is missing, synthesize
7522 it, by scanning the DIE's below the compilation unit. */
7523 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
7524
7525 static_block
7526 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0, 1);
7527
7528 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7529 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7530 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7531 addrmap to help ensure it has an accurate map of pc values belonging to
7532 this comp unit. */
7533 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7534
7535 symtab = end_symtab_from_static_block (static_block, objfile,
7536 SECT_OFF_TEXT (objfile), 0);
7537
7538 if (symtab != NULL)
7539 {
7540 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
7541
7542 /* Set symtab language to language from DW_AT_language. If the
7543 compilation is from a C file generated by language preprocessors, do
7544 not set the language if it was already deduced by start_subfile. */
7545 if (!(cu->language == language_c && symtab->language != language_c))
7546 symtab->language = cu->language;
7547
7548 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7549 produce DW_AT_location with location lists but it can be possibly
7550 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7551 there were bugs in prologue debug info, fixed later in GCC-4.5
7552 by "unwind info for epilogues" patch (which is not directly related).
7553
7554 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7555 needed, it would be wrong due to missing DW_AT_producer there.
7556
7557 Still one can confuse GDB by using non-standard GCC compilation
7558 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7559 */
7560 if (cu->has_loclist && gcc_4_minor >= 5)
7561 symtab->locations_valid = 1;
7562
7563 if (gcc_4_minor >= 5)
7564 symtab->epilogue_unwind_valid = 1;
7565
7566 symtab->call_site_htab = cu->call_site_htab;
7567 }
7568
7569 if (dwarf2_per_objfile->using_index)
7570 per_cu->v.quick->symtab = symtab;
7571 else
7572 {
7573 struct partial_symtab *pst = per_cu->v.psymtab;
7574 pst->symtab = symtab;
7575 pst->readin = 1;
7576 }
7577
7578 /* Push it for inclusion processing later. */
7579 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7580
7581 do_cleanups (back_to);
7582 }
7583
7584 /* Generate full symbol information for type unit PER_CU, whose DIEs have
7585 already been loaded into memory. */
7586
7587 static void
7588 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7589 enum language pretend_language)
7590 {
7591 struct dwarf2_cu *cu = per_cu->cu;
7592 struct objfile *objfile = per_cu->objfile;
7593 struct symtab *symtab;
7594 struct cleanup *back_to, *delayed_list_cleanup;
7595 struct signatured_type *sig_type;
7596
7597 gdb_assert (per_cu->is_debug_types);
7598 sig_type = (struct signatured_type *) per_cu;
7599
7600 buildsym_init ();
7601 back_to = make_cleanup (really_free_pendings, NULL);
7602 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7603
7604 cu->list_in_scope = &file_symbols;
7605
7606 cu->language = pretend_language;
7607 cu->language_defn = language_def (cu->language);
7608
7609 /* The symbol tables are set up in read_type_unit_scope. */
7610 process_die (cu->dies, cu);
7611
7612 /* For now fudge the Go package. */
7613 if (cu->language == language_go)
7614 fixup_go_packaging (cu);
7615
7616 /* Now that we have processed all the DIEs in the CU, all the types
7617 should be complete, and it should now be safe to compute all of the
7618 physnames. */
7619 compute_delayed_physnames (cu);
7620 do_cleanups (delayed_list_cleanup);
7621
7622 /* TUs share symbol tables.
7623 If this is the first TU to use this symtab, complete the construction
7624 of it with end_expandable_symtab. Otherwise, complete the addition of
7625 this TU's symbols to the existing symtab. */
7626 if (sig_type->type_unit_group->primary_symtab == NULL)
7627 {
7628 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7629 sig_type->type_unit_group->primary_symtab = symtab;
7630
7631 if (symtab != NULL)
7632 {
7633 /* Set symtab language to language from DW_AT_language. If the
7634 compilation is from a C file generated by language preprocessors,
7635 do not set the language if it was already deduced by
7636 start_subfile. */
7637 if (!(cu->language == language_c && symtab->language != language_c))
7638 symtab->language = cu->language;
7639 }
7640 }
7641 else
7642 {
7643 augment_type_symtab (objfile,
7644 sig_type->type_unit_group->primary_symtab);
7645 symtab = sig_type->type_unit_group->primary_symtab;
7646 }
7647
7648 if (dwarf2_per_objfile->using_index)
7649 per_cu->v.quick->symtab = symtab;
7650 else
7651 {
7652 struct partial_symtab *pst = per_cu->v.psymtab;
7653 pst->symtab = symtab;
7654 pst->readin = 1;
7655 }
7656
7657 do_cleanups (back_to);
7658 }
7659
7660 /* Process an imported unit DIE. */
7661
7662 static void
7663 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7664 {
7665 struct attribute *attr;
7666
7667 /* For now we don't handle imported units in type units. */
7668 if (cu->per_cu->is_debug_types)
7669 {
7670 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7671 " supported in type units [in module %s]"),
7672 objfile_name (cu->objfile));
7673 }
7674
7675 attr = dwarf2_attr (die, DW_AT_import, cu);
7676 if (attr != NULL)
7677 {
7678 struct dwarf2_per_cu_data *per_cu;
7679 struct symtab *imported_symtab;
7680 sect_offset offset;
7681 int is_dwz;
7682
7683 offset = dwarf2_get_ref_die_offset (attr);
7684 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7685 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
7686
7687 /* If necessary, add it to the queue and load its DIEs. */
7688 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7689 load_full_comp_unit (per_cu, cu->language);
7690
7691 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7692 per_cu);
7693 }
7694 }
7695
7696 /* Process a die and its children. */
7697
7698 static void
7699 process_die (struct die_info *die, struct dwarf2_cu *cu)
7700 {
7701 switch (die->tag)
7702 {
7703 case DW_TAG_padding:
7704 break;
7705 case DW_TAG_compile_unit:
7706 case DW_TAG_partial_unit:
7707 read_file_scope (die, cu);
7708 break;
7709 case DW_TAG_type_unit:
7710 read_type_unit_scope (die, cu);
7711 break;
7712 case DW_TAG_subprogram:
7713 case DW_TAG_inlined_subroutine:
7714 read_func_scope (die, cu);
7715 break;
7716 case DW_TAG_lexical_block:
7717 case DW_TAG_try_block:
7718 case DW_TAG_catch_block:
7719 read_lexical_block_scope (die, cu);
7720 break;
7721 case DW_TAG_GNU_call_site:
7722 read_call_site_scope (die, cu);
7723 break;
7724 case DW_TAG_class_type:
7725 case DW_TAG_interface_type:
7726 case DW_TAG_structure_type:
7727 case DW_TAG_union_type:
7728 process_structure_scope (die, cu);
7729 break;
7730 case DW_TAG_enumeration_type:
7731 process_enumeration_scope (die, cu);
7732 break;
7733
7734 /* These dies have a type, but processing them does not create
7735 a symbol or recurse to process the children. Therefore we can
7736 read them on-demand through read_type_die. */
7737 case DW_TAG_subroutine_type:
7738 case DW_TAG_set_type:
7739 case DW_TAG_array_type:
7740 case DW_TAG_pointer_type:
7741 case DW_TAG_ptr_to_member_type:
7742 case DW_TAG_reference_type:
7743 case DW_TAG_string_type:
7744 break;
7745
7746 case DW_TAG_base_type:
7747 case DW_TAG_subrange_type:
7748 case DW_TAG_typedef:
7749 /* Add a typedef symbol for the type definition, if it has a
7750 DW_AT_name. */
7751 new_symbol (die, read_type_die (die, cu), cu);
7752 break;
7753 case DW_TAG_common_block:
7754 read_common_block (die, cu);
7755 break;
7756 case DW_TAG_common_inclusion:
7757 break;
7758 case DW_TAG_namespace:
7759 cu->processing_has_namespace_info = 1;
7760 read_namespace (die, cu);
7761 break;
7762 case DW_TAG_module:
7763 cu->processing_has_namespace_info = 1;
7764 read_module (die, cu);
7765 break;
7766 case DW_TAG_imported_declaration:
7767 case DW_TAG_imported_module:
7768 cu->processing_has_namespace_info = 1;
7769 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7770 || cu->language != language_fortran))
7771 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7772 dwarf_tag_name (die->tag));
7773 read_import_statement (die, cu);
7774 break;
7775
7776 case DW_TAG_imported_unit:
7777 process_imported_unit_die (die, cu);
7778 break;
7779
7780 default:
7781 new_symbol (die, NULL, cu);
7782 break;
7783 }
7784 }
7785 \f
7786 /* DWARF name computation. */
7787
7788 /* A helper function for dwarf2_compute_name which determines whether DIE
7789 needs to have the name of the scope prepended to the name listed in the
7790 die. */
7791
7792 static int
7793 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7794 {
7795 struct attribute *attr;
7796
7797 switch (die->tag)
7798 {
7799 case DW_TAG_namespace:
7800 case DW_TAG_typedef:
7801 case DW_TAG_class_type:
7802 case DW_TAG_interface_type:
7803 case DW_TAG_structure_type:
7804 case DW_TAG_union_type:
7805 case DW_TAG_enumeration_type:
7806 case DW_TAG_enumerator:
7807 case DW_TAG_subprogram:
7808 case DW_TAG_member:
7809 return 1;
7810
7811 case DW_TAG_variable:
7812 case DW_TAG_constant:
7813 /* We only need to prefix "globally" visible variables. These include
7814 any variable marked with DW_AT_external or any variable that
7815 lives in a namespace. [Variables in anonymous namespaces
7816 require prefixing, but they are not DW_AT_external.] */
7817
7818 if (dwarf2_attr (die, DW_AT_specification, cu))
7819 {
7820 struct dwarf2_cu *spec_cu = cu;
7821
7822 return die_needs_namespace (die_specification (die, &spec_cu),
7823 spec_cu);
7824 }
7825
7826 attr = dwarf2_attr (die, DW_AT_external, cu);
7827 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7828 && die->parent->tag != DW_TAG_module)
7829 return 0;
7830 /* A variable in a lexical block of some kind does not need a
7831 namespace, even though in C++ such variables may be external
7832 and have a mangled name. */
7833 if (die->parent->tag == DW_TAG_lexical_block
7834 || die->parent->tag == DW_TAG_try_block
7835 || die->parent->tag == DW_TAG_catch_block
7836 || die->parent->tag == DW_TAG_subprogram)
7837 return 0;
7838 return 1;
7839
7840 default:
7841 return 0;
7842 }
7843 }
7844
7845 /* Retrieve the last character from a mem_file. */
7846
7847 static void
7848 do_ui_file_peek_last (void *object, const char *buffer, long length)
7849 {
7850 char *last_char_p = (char *) object;
7851
7852 if (length > 0)
7853 *last_char_p = buffer[length - 1];
7854 }
7855
7856 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
7857 compute the physname for the object, which include a method's:
7858 - formal parameters (C++/Java),
7859 - receiver type (Go),
7860 - return type (Java).
7861
7862 The term "physname" is a bit confusing.
7863 For C++, for example, it is the demangled name.
7864 For Go, for example, it's the mangled name.
7865
7866 For Ada, return the DIE's linkage name rather than the fully qualified
7867 name. PHYSNAME is ignored..
7868
7869 The result is allocated on the objfile_obstack and canonicalized. */
7870
7871 static const char *
7872 dwarf2_compute_name (const char *name,
7873 struct die_info *die, struct dwarf2_cu *cu,
7874 int physname)
7875 {
7876 struct objfile *objfile = cu->objfile;
7877
7878 if (name == NULL)
7879 name = dwarf2_name (die, cu);
7880
7881 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7882 compute it by typename_concat inside GDB. */
7883 if (cu->language == language_ada
7884 || (cu->language == language_fortran && physname))
7885 {
7886 /* For Ada unit, we prefer the linkage name over the name, as
7887 the former contains the exported name, which the user expects
7888 to be able to reference. Ideally, we want the user to be able
7889 to reference this entity using either natural or linkage name,
7890 but we haven't started looking at this enhancement yet. */
7891 struct attribute *attr;
7892
7893 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7894 if (attr == NULL)
7895 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7896 if (attr && DW_STRING (attr))
7897 return DW_STRING (attr);
7898 }
7899
7900 /* These are the only languages we know how to qualify names in. */
7901 if (name != NULL
7902 && (cu->language == language_cplus || cu->language == language_java
7903 || cu->language == language_fortran))
7904 {
7905 if (die_needs_namespace (die, cu))
7906 {
7907 long length;
7908 const char *prefix;
7909 struct ui_file *buf;
7910
7911 prefix = determine_prefix (die, cu);
7912 buf = mem_fileopen ();
7913 if (*prefix != '\0')
7914 {
7915 char *prefixed_name = typename_concat (NULL, prefix, name,
7916 physname, cu);
7917
7918 fputs_unfiltered (prefixed_name, buf);
7919 xfree (prefixed_name);
7920 }
7921 else
7922 fputs_unfiltered (name, buf);
7923
7924 /* Template parameters may be specified in the DIE's DW_AT_name, or
7925 as children with DW_TAG_template_type_param or
7926 DW_TAG_value_type_param. If the latter, add them to the name
7927 here. If the name already has template parameters, then
7928 skip this step; some versions of GCC emit both, and
7929 it is more efficient to use the pre-computed name.
7930
7931 Something to keep in mind about this process: it is very
7932 unlikely, or in some cases downright impossible, to produce
7933 something that will match the mangled name of a function.
7934 If the definition of the function has the same debug info,
7935 we should be able to match up with it anyway. But fallbacks
7936 using the minimal symbol, for instance to find a method
7937 implemented in a stripped copy of libstdc++, will not work.
7938 If we do not have debug info for the definition, we will have to
7939 match them up some other way.
7940
7941 When we do name matching there is a related problem with function
7942 templates; two instantiated function templates are allowed to
7943 differ only by their return types, which we do not add here. */
7944
7945 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7946 {
7947 struct attribute *attr;
7948 struct die_info *child;
7949 int first = 1;
7950
7951 die->building_fullname = 1;
7952
7953 for (child = die->child; child != NULL; child = child->sibling)
7954 {
7955 struct type *type;
7956 LONGEST value;
7957 const gdb_byte *bytes;
7958 struct dwarf2_locexpr_baton *baton;
7959 struct value *v;
7960
7961 if (child->tag != DW_TAG_template_type_param
7962 && child->tag != DW_TAG_template_value_param)
7963 continue;
7964
7965 if (first)
7966 {
7967 fputs_unfiltered ("<", buf);
7968 first = 0;
7969 }
7970 else
7971 fputs_unfiltered (", ", buf);
7972
7973 attr = dwarf2_attr (child, DW_AT_type, cu);
7974 if (attr == NULL)
7975 {
7976 complaint (&symfile_complaints,
7977 _("template parameter missing DW_AT_type"));
7978 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7979 continue;
7980 }
7981 type = die_type (child, cu);
7982
7983 if (child->tag == DW_TAG_template_type_param)
7984 {
7985 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
7986 continue;
7987 }
7988
7989 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7990 if (attr == NULL)
7991 {
7992 complaint (&symfile_complaints,
7993 _("template parameter missing "
7994 "DW_AT_const_value"));
7995 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7996 continue;
7997 }
7998
7999 dwarf2_const_value_attr (attr, type, name,
8000 &cu->comp_unit_obstack, cu,
8001 &value, &bytes, &baton);
8002
8003 if (TYPE_NOSIGN (type))
8004 /* GDB prints characters as NUMBER 'CHAR'. If that's
8005 changed, this can use value_print instead. */
8006 c_printchar (value, type, buf);
8007 else
8008 {
8009 struct value_print_options opts;
8010
8011 if (baton != NULL)
8012 v = dwarf2_evaluate_loc_desc (type, NULL,
8013 baton->data,
8014 baton->size,
8015 baton->per_cu);
8016 else if (bytes != NULL)
8017 {
8018 v = allocate_value (type);
8019 memcpy (value_contents_writeable (v), bytes,
8020 TYPE_LENGTH (type));
8021 }
8022 else
8023 v = value_from_longest (type, value);
8024
8025 /* Specify decimal so that we do not depend on
8026 the radix. */
8027 get_formatted_print_options (&opts, 'd');
8028 opts.raw = 1;
8029 value_print (v, buf, &opts);
8030 release_value (v);
8031 value_free (v);
8032 }
8033 }
8034
8035 die->building_fullname = 0;
8036
8037 if (!first)
8038 {
8039 /* Close the argument list, with a space if necessary
8040 (nested templates). */
8041 char last_char = '\0';
8042 ui_file_put (buf, do_ui_file_peek_last, &last_char);
8043 if (last_char == '>')
8044 fputs_unfiltered (" >", buf);
8045 else
8046 fputs_unfiltered (">", buf);
8047 }
8048 }
8049
8050 /* For Java and C++ methods, append formal parameter type
8051 information, if PHYSNAME. */
8052
8053 if (physname && die->tag == DW_TAG_subprogram
8054 && (cu->language == language_cplus
8055 || cu->language == language_java))
8056 {
8057 struct type *type = read_type_die (die, cu);
8058
8059 c_type_print_args (type, buf, 1, cu->language,
8060 &type_print_raw_options);
8061
8062 if (cu->language == language_java)
8063 {
8064 /* For java, we must append the return type to method
8065 names. */
8066 if (die->tag == DW_TAG_subprogram)
8067 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
8068 0, 0, &type_print_raw_options);
8069 }
8070 else if (cu->language == language_cplus)
8071 {
8072 /* Assume that an artificial first parameter is
8073 "this", but do not crash if it is not. RealView
8074 marks unnamed (and thus unused) parameters as
8075 artificial; there is no way to differentiate
8076 the two cases. */
8077 if (TYPE_NFIELDS (type) > 0
8078 && TYPE_FIELD_ARTIFICIAL (type, 0)
8079 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
8080 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8081 0))))
8082 fputs_unfiltered (" const", buf);
8083 }
8084 }
8085
8086 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
8087 &length);
8088 ui_file_delete (buf);
8089
8090 if (cu->language == language_cplus)
8091 {
8092 const char *cname
8093 = dwarf2_canonicalize_name (name, cu,
8094 &objfile->objfile_obstack);
8095
8096 if (cname != NULL)
8097 name = cname;
8098 }
8099 }
8100 }
8101
8102 return name;
8103 }
8104
8105 /* Return the fully qualified name of DIE, based on its DW_AT_name.
8106 If scope qualifiers are appropriate they will be added. The result
8107 will be allocated on the objfile_obstack, or NULL if the DIE does
8108 not have a name. NAME may either be from a previous call to
8109 dwarf2_name or NULL.
8110
8111 The output string will be canonicalized (if C++/Java). */
8112
8113 static const char *
8114 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8115 {
8116 return dwarf2_compute_name (name, die, cu, 0);
8117 }
8118
8119 /* Construct a physname for the given DIE in CU. NAME may either be
8120 from a previous call to dwarf2_name or NULL. The result will be
8121 allocated on the objfile_objstack or NULL if the DIE does not have a
8122 name.
8123
8124 The output string will be canonicalized (if C++/Java). */
8125
8126 static const char *
8127 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8128 {
8129 struct objfile *objfile = cu->objfile;
8130 struct attribute *attr;
8131 const char *retval, *mangled = NULL, *canon = NULL;
8132 struct cleanup *back_to;
8133 int need_copy = 1;
8134
8135 /* In this case dwarf2_compute_name is just a shortcut not building anything
8136 on its own. */
8137 if (!die_needs_namespace (die, cu))
8138 return dwarf2_compute_name (name, die, cu, 1);
8139
8140 back_to = make_cleanup (null_cleanup, NULL);
8141
8142 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8143 if (!attr)
8144 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8145
8146 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8147 has computed. */
8148 if (attr && DW_STRING (attr))
8149 {
8150 char *demangled;
8151
8152 mangled = DW_STRING (attr);
8153
8154 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8155 type. It is easier for GDB users to search for such functions as
8156 `name(params)' than `long name(params)'. In such case the minimal
8157 symbol names do not match the full symbol names but for template
8158 functions there is never a need to look up their definition from their
8159 declaration so the only disadvantage remains the minimal symbol
8160 variant `long name(params)' does not have the proper inferior type.
8161 */
8162
8163 if (cu->language == language_go)
8164 {
8165 /* This is a lie, but we already lie to the caller new_symbol_full.
8166 new_symbol_full assumes we return the mangled name.
8167 This just undoes that lie until things are cleaned up. */
8168 demangled = NULL;
8169 }
8170 else
8171 {
8172 demangled = gdb_demangle (mangled,
8173 (DMGL_PARAMS | DMGL_ANSI
8174 | (cu->language == language_java
8175 ? DMGL_JAVA | DMGL_RET_POSTFIX
8176 : DMGL_RET_DROP)));
8177 }
8178 if (demangled)
8179 {
8180 make_cleanup (xfree, demangled);
8181 canon = demangled;
8182 }
8183 else
8184 {
8185 canon = mangled;
8186 need_copy = 0;
8187 }
8188 }
8189
8190 if (canon == NULL || check_physname)
8191 {
8192 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8193
8194 if (canon != NULL && strcmp (physname, canon) != 0)
8195 {
8196 /* It may not mean a bug in GDB. The compiler could also
8197 compute DW_AT_linkage_name incorrectly. But in such case
8198 GDB would need to be bug-to-bug compatible. */
8199
8200 complaint (&symfile_complaints,
8201 _("Computed physname <%s> does not match demangled <%s> "
8202 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
8203 physname, canon, mangled, die->offset.sect_off,
8204 objfile_name (objfile));
8205
8206 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8207 is available here - over computed PHYSNAME. It is safer
8208 against both buggy GDB and buggy compilers. */
8209
8210 retval = canon;
8211 }
8212 else
8213 {
8214 retval = physname;
8215 need_copy = 0;
8216 }
8217 }
8218 else
8219 retval = canon;
8220
8221 if (need_copy)
8222 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
8223
8224 do_cleanups (back_to);
8225 return retval;
8226 }
8227
8228 /* Read the import statement specified by the given die and record it. */
8229
8230 static void
8231 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
8232 {
8233 struct objfile *objfile = cu->objfile;
8234 struct attribute *import_attr;
8235 struct die_info *imported_die, *child_die;
8236 struct dwarf2_cu *imported_cu;
8237 const char *imported_name;
8238 const char *imported_name_prefix;
8239 const char *canonical_name;
8240 const char *import_alias;
8241 const char *imported_declaration = NULL;
8242 const char *import_prefix;
8243 VEC (const_char_ptr) *excludes = NULL;
8244 struct cleanup *cleanups;
8245
8246 import_attr = dwarf2_attr (die, DW_AT_import, cu);
8247 if (import_attr == NULL)
8248 {
8249 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8250 dwarf_tag_name (die->tag));
8251 return;
8252 }
8253
8254 imported_cu = cu;
8255 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
8256 imported_name = dwarf2_name (imported_die, imported_cu);
8257 if (imported_name == NULL)
8258 {
8259 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
8260
8261 The import in the following code:
8262 namespace A
8263 {
8264 typedef int B;
8265 }
8266
8267 int main ()
8268 {
8269 using A::B;
8270 B b;
8271 return b;
8272 }
8273
8274 ...
8275 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
8276 <52> DW_AT_decl_file : 1
8277 <53> DW_AT_decl_line : 6
8278 <54> DW_AT_import : <0x75>
8279 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
8280 <59> DW_AT_name : B
8281 <5b> DW_AT_decl_file : 1
8282 <5c> DW_AT_decl_line : 2
8283 <5d> DW_AT_type : <0x6e>
8284 ...
8285 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
8286 <76> DW_AT_byte_size : 4
8287 <77> DW_AT_encoding : 5 (signed)
8288
8289 imports the wrong die ( 0x75 instead of 0x58 ).
8290 This case will be ignored until the gcc bug is fixed. */
8291 return;
8292 }
8293
8294 /* Figure out the local name after import. */
8295 import_alias = dwarf2_name (die, cu);
8296
8297 /* Figure out where the statement is being imported to. */
8298 import_prefix = determine_prefix (die, cu);
8299
8300 /* Figure out what the scope of the imported die is and prepend it
8301 to the name of the imported die. */
8302 imported_name_prefix = determine_prefix (imported_die, imported_cu);
8303
8304 if (imported_die->tag != DW_TAG_namespace
8305 && imported_die->tag != DW_TAG_module)
8306 {
8307 imported_declaration = imported_name;
8308 canonical_name = imported_name_prefix;
8309 }
8310 else if (strlen (imported_name_prefix) > 0)
8311 canonical_name = obconcat (&objfile->objfile_obstack,
8312 imported_name_prefix, "::", imported_name,
8313 (char *) NULL);
8314 else
8315 canonical_name = imported_name;
8316
8317 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
8318
8319 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
8320 for (child_die = die->child; child_die && child_die->tag;
8321 child_die = sibling_die (child_die))
8322 {
8323 /* DWARF-4: A Fortran use statement with a “rename list” may be
8324 represented by an imported module entry with an import attribute
8325 referring to the module and owned entries corresponding to those
8326 entities that are renamed as part of being imported. */
8327
8328 if (child_die->tag != DW_TAG_imported_declaration)
8329 {
8330 complaint (&symfile_complaints,
8331 _("child DW_TAG_imported_declaration expected "
8332 "- DIE at 0x%x [in module %s]"),
8333 child_die->offset.sect_off, objfile_name (objfile));
8334 continue;
8335 }
8336
8337 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
8338 if (import_attr == NULL)
8339 {
8340 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8341 dwarf_tag_name (child_die->tag));
8342 continue;
8343 }
8344
8345 imported_cu = cu;
8346 imported_die = follow_die_ref_or_sig (child_die, import_attr,
8347 &imported_cu);
8348 imported_name = dwarf2_name (imported_die, imported_cu);
8349 if (imported_name == NULL)
8350 {
8351 complaint (&symfile_complaints,
8352 _("child DW_TAG_imported_declaration has unknown "
8353 "imported name - DIE at 0x%x [in module %s]"),
8354 child_die->offset.sect_off, objfile_name (objfile));
8355 continue;
8356 }
8357
8358 VEC_safe_push (const_char_ptr, excludes, imported_name);
8359
8360 process_die (child_die, cu);
8361 }
8362
8363 cp_add_using_directive (import_prefix,
8364 canonical_name,
8365 import_alias,
8366 imported_declaration,
8367 excludes,
8368 0,
8369 &objfile->objfile_obstack);
8370
8371 do_cleanups (cleanups);
8372 }
8373
8374 /* Cleanup function for handle_DW_AT_stmt_list. */
8375
8376 static void
8377 free_cu_line_header (void *arg)
8378 {
8379 struct dwarf2_cu *cu = arg;
8380
8381 free_line_header (cu->line_header);
8382 cu->line_header = NULL;
8383 }
8384
8385 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
8386 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
8387 this, it was first present in GCC release 4.3.0. */
8388
8389 static int
8390 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
8391 {
8392 if (!cu->checked_producer)
8393 check_producer (cu);
8394
8395 return cu->producer_is_gcc_lt_4_3;
8396 }
8397
8398 static void
8399 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
8400 const char **name, const char **comp_dir)
8401 {
8402 struct attribute *attr;
8403
8404 *name = NULL;
8405 *comp_dir = NULL;
8406
8407 /* Find the filename. Do not use dwarf2_name here, since the filename
8408 is not a source language identifier. */
8409 attr = dwarf2_attr (die, DW_AT_name, cu);
8410 if (attr)
8411 {
8412 *name = DW_STRING (attr);
8413 }
8414
8415 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
8416 if (attr)
8417 *comp_dir = DW_STRING (attr);
8418 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
8419 && IS_ABSOLUTE_PATH (*name))
8420 {
8421 char *d = ldirname (*name);
8422
8423 *comp_dir = d;
8424 if (d != NULL)
8425 make_cleanup (xfree, d);
8426 }
8427 if (*comp_dir != NULL)
8428 {
8429 /* Irix 6.2 native cc prepends <machine>.: to the compilation
8430 directory, get rid of it. */
8431 char *cp = strchr (*comp_dir, ':');
8432
8433 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
8434 *comp_dir = cp + 1;
8435 }
8436
8437 if (*name == NULL)
8438 *name = "<unknown>";
8439 }
8440
8441 /* Handle DW_AT_stmt_list for a compilation unit.
8442 DIE is the DW_TAG_compile_unit die for CU.
8443 COMP_DIR is the compilation directory.
8444 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
8445
8446 static void
8447 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
8448 const char *comp_dir) /* ARI: editCase function */
8449 {
8450 struct attribute *attr;
8451
8452 gdb_assert (! cu->per_cu->is_debug_types);
8453
8454 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8455 if (attr)
8456 {
8457 unsigned int line_offset = DW_UNSND (attr);
8458 struct line_header *line_header
8459 = dwarf_decode_line_header (line_offset, cu);
8460
8461 if (line_header)
8462 {
8463 cu->line_header = line_header;
8464 make_cleanup (free_cu_line_header, cu);
8465 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
8466 }
8467 }
8468 }
8469
8470 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
8471
8472 static void
8473 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
8474 {
8475 struct objfile *objfile = dwarf2_per_objfile->objfile;
8476 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
8477 CORE_ADDR lowpc = ((CORE_ADDR) -1);
8478 CORE_ADDR highpc = ((CORE_ADDR) 0);
8479 struct attribute *attr;
8480 const char *name = NULL;
8481 const char *comp_dir = NULL;
8482 struct die_info *child_die;
8483 bfd *abfd = objfile->obfd;
8484 CORE_ADDR baseaddr;
8485
8486 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8487
8488 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
8489
8490 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8491 from finish_block. */
8492 if (lowpc == ((CORE_ADDR) -1))
8493 lowpc = highpc;
8494 lowpc += baseaddr;
8495 highpc += baseaddr;
8496
8497 find_file_and_directory (die, cu, &name, &comp_dir);
8498
8499 prepare_one_comp_unit (cu, die, cu->language);
8500
8501 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8502 standardised yet. As a workaround for the language detection we fall
8503 back to the DW_AT_producer string. */
8504 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8505 cu->language = language_opencl;
8506
8507 /* Similar hack for Go. */
8508 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8509 set_cu_language (DW_LANG_Go, cu);
8510
8511 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
8512
8513 /* Decode line number information if present. We do this before
8514 processing child DIEs, so that the line header table is available
8515 for DW_AT_decl_file. */
8516 handle_DW_AT_stmt_list (die, cu, comp_dir);
8517
8518 /* Process all dies in compilation unit. */
8519 if (die->child != NULL)
8520 {
8521 child_die = die->child;
8522 while (child_die && child_die->tag)
8523 {
8524 process_die (child_die, cu);
8525 child_die = sibling_die (child_die);
8526 }
8527 }
8528
8529 /* Decode macro information, if present. Dwarf 2 macro information
8530 refers to information in the line number info statement program
8531 header, so we can only read it if we've read the header
8532 successfully. */
8533 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8534 if (attr && cu->line_header)
8535 {
8536 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8537 complaint (&symfile_complaints,
8538 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8539
8540 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
8541 }
8542 else
8543 {
8544 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8545 if (attr && cu->line_header)
8546 {
8547 unsigned int macro_offset = DW_UNSND (attr);
8548
8549 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
8550 }
8551 }
8552
8553 do_cleanups (back_to);
8554 }
8555
8556 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8557 Create the set of symtabs used by this TU, or if this TU is sharing
8558 symtabs with another TU and the symtabs have already been created
8559 then restore those symtabs in the line header.
8560 We don't need the pc/line-number mapping for type units. */
8561
8562 static void
8563 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
8564 {
8565 struct objfile *objfile = dwarf2_per_objfile->objfile;
8566 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8567 struct type_unit_group *tu_group;
8568 int first_time;
8569 struct line_header *lh;
8570 struct attribute *attr;
8571 unsigned int i, line_offset;
8572 struct signatured_type *sig_type;
8573
8574 gdb_assert (per_cu->is_debug_types);
8575 sig_type = (struct signatured_type *) per_cu;
8576
8577 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8578
8579 /* If we're using .gdb_index (includes -readnow) then
8580 per_cu->type_unit_group may not have been set up yet. */
8581 if (sig_type->type_unit_group == NULL)
8582 sig_type->type_unit_group = get_type_unit_group (cu, attr);
8583 tu_group = sig_type->type_unit_group;
8584
8585 /* If we've already processed this stmt_list there's no real need to
8586 do it again, we could fake it and just recreate the part we need
8587 (file name,index -> symtab mapping). If data shows this optimization
8588 is useful we can do it then. */
8589 first_time = tu_group->primary_symtab == NULL;
8590
8591 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8592 debug info. */
8593 lh = NULL;
8594 if (attr != NULL)
8595 {
8596 line_offset = DW_UNSND (attr);
8597 lh = dwarf_decode_line_header (line_offset, cu);
8598 }
8599 if (lh == NULL)
8600 {
8601 if (first_time)
8602 dwarf2_start_symtab (cu, "", NULL, 0);
8603 else
8604 {
8605 gdb_assert (tu_group->symtabs == NULL);
8606 restart_symtab (0);
8607 }
8608 /* Note: The primary symtab will get allocated at the end. */
8609 return;
8610 }
8611
8612 cu->line_header = lh;
8613 make_cleanup (free_cu_line_header, cu);
8614
8615 if (first_time)
8616 {
8617 dwarf2_start_symtab (cu, "", NULL, 0);
8618
8619 tu_group->num_symtabs = lh->num_file_names;
8620 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
8621
8622 for (i = 0; i < lh->num_file_names; ++i)
8623 {
8624 const char *dir = NULL;
8625 struct file_entry *fe = &lh->file_names[i];
8626
8627 if (fe->dir_index)
8628 dir = lh->include_dirs[fe->dir_index - 1];
8629 dwarf2_start_subfile (fe->name, dir, NULL);
8630
8631 /* Note: We don't have to watch for the main subfile here, type units
8632 don't have DW_AT_name. */
8633
8634 if (current_subfile->symtab == NULL)
8635 {
8636 /* NOTE: start_subfile will recognize when it's been passed
8637 a file it has already seen. So we can't assume there's a
8638 simple mapping from lh->file_names to subfiles,
8639 lh->file_names may contain dups. */
8640 current_subfile->symtab = allocate_symtab (current_subfile->name,
8641 objfile);
8642 }
8643
8644 fe->symtab = current_subfile->symtab;
8645 tu_group->symtabs[i] = fe->symtab;
8646 }
8647 }
8648 else
8649 {
8650 restart_symtab (0);
8651
8652 for (i = 0; i < lh->num_file_names; ++i)
8653 {
8654 struct file_entry *fe = &lh->file_names[i];
8655
8656 fe->symtab = tu_group->symtabs[i];
8657 }
8658 }
8659
8660 /* The main symtab is allocated last. Type units don't have DW_AT_name
8661 so they don't have a "real" (so to speak) symtab anyway.
8662 There is later code that will assign the main symtab to all symbols
8663 that don't have one. We need to handle the case of a symbol with a
8664 missing symtab (DW_AT_decl_file) anyway. */
8665 }
8666
8667 /* Process DW_TAG_type_unit.
8668 For TUs we want to skip the first top level sibling if it's not the
8669 actual type being defined by this TU. In this case the first top
8670 level sibling is there to provide context only. */
8671
8672 static void
8673 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8674 {
8675 struct die_info *child_die;
8676
8677 prepare_one_comp_unit (cu, die, language_minimal);
8678
8679 /* Initialize (or reinitialize) the machinery for building symtabs.
8680 We do this before processing child DIEs, so that the line header table
8681 is available for DW_AT_decl_file. */
8682 setup_type_unit_groups (die, cu);
8683
8684 if (die->child != NULL)
8685 {
8686 child_die = die->child;
8687 while (child_die && child_die->tag)
8688 {
8689 process_die (child_die, cu);
8690 child_die = sibling_die (child_die);
8691 }
8692 }
8693 }
8694 \f
8695 /* DWO/DWP files.
8696
8697 http://gcc.gnu.org/wiki/DebugFission
8698 http://gcc.gnu.org/wiki/DebugFissionDWP
8699
8700 To simplify handling of both DWO files ("object" files with the DWARF info)
8701 and DWP files (a file with the DWOs packaged up into one file), we treat
8702 DWP files as having a collection of virtual DWO files. */
8703
8704 static hashval_t
8705 hash_dwo_file (const void *item)
8706 {
8707 const struct dwo_file *dwo_file = item;
8708 hashval_t hash;
8709
8710 hash = htab_hash_string (dwo_file->dwo_name);
8711 if (dwo_file->comp_dir != NULL)
8712 hash += htab_hash_string (dwo_file->comp_dir);
8713 return hash;
8714 }
8715
8716 static int
8717 eq_dwo_file (const void *item_lhs, const void *item_rhs)
8718 {
8719 const struct dwo_file *lhs = item_lhs;
8720 const struct dwo_file *rhs = item_rhs;
8721
8722 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
8723 return 0;
8724 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
8725 return lhs->comp_dir == rhs->comp_dir;
8726 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
8727 }
8728
8729 /* Allocate a hash table for DWO files. */
8730
8731 static htab_t
8732 allocate_dwo_file_hash_table (void)
8733 {
8734 struct objfile *objfile = dwarf2_per_objfile->objfile;
8735
8736 return htab_create_alloc_ex (41,
8737 hash_dwo_file,
8738 eq_dwo_file,
8739 NULL,
8740 &objfile->objfile_obstack,
8741 hashtab_obstack_allocate,
8742 dummy_obstack_deallocate);
8743 }
8744
8745 /* Lookup DWO file DWO_NAME. */
8746
8747 static void **
8748 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
8749 {
8750 struct dwo_file find_entry;
8751 void **slot;
8752
8753 if (dwarf2_per_objfile->dwo_files == NULL)
8754 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8755
8756 memset (&find_entry, 0, sizeof (find_entry));
8757 find_entry.dwo_name = dwo_name;
8758 find_entry.comp_dir = comp_dir;
8759 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8760
8761 return slot;
8762 }
8763
8764 static hashval_t
8765 hash_dwo_unit (const void *item)
8766 {
8767 const struct dwo_unit *dwo_unit = item;
8768
8769 /* This drops the top 32 bits of the id, but is ok for a hash. */
8770 return dwo_unit->signature;
8771 }
8772
8773 static int
8774 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8775 {
8776 const struct dwo_unit *lhs = item_lhs;
8777 const struct dwo_unit *rhs = item_rhs;
8778
8779 /* The signature is assumed to be unique within the DWO file.
8780 So while object file CU dwo_id's always have the value zero,
8781 that's OK, assuming each object file DWO file has only one CU,
8782 and that's the rule for now. */
8783 return lhs->signature == rhs->signature;
8784 }
8785
8786 /* Allocate a hash table for DWO CUs,TUs.
8787 There is one of these tables for each of CUs,TUs for each DWO file. */
8788
8789 static htab_t
8790 allocate_dwo_unit_table (struct objfile *objfile)
8791 {
8792 /* Start out with a pretty small number.
8793 Generally DWO files contain only one CU and maybe some TUs. */
8794 return htab_create_alloc_ex (3,
8795 hash_dwo_unit,
8796 eq_dwo_unit,
8797 NULL,
8798 &objfile->objfile_obstack,
8799 hashtab_obstack_allocate,
8800 dummy_obstack_deallocate);
8801 }
8802
8803 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
8804
8805 struct create_dwo_cu_data
8806 {
8807 struct dwo_file *dwo_file;
8808 struct dwo_unit dwo_unit;
8809 };
8810
8811 /* die_reader_func for create_dwo_cu. */
8812
8813 static void
8814 create_dwo_cu_reader (const struct die_reader_specs *reader,
8815 const gdb_byte *info_ptr,
8816 struct die_info *comp_unit_die,
8817 int has_children,
8818 void *datap)
8819 {
8820 struct dwarf2_cu *cu = reader->cu;
8821 struct objfile *objfile = dwarf2_per_objfile->objfile;
8822 sect_offset offset = cu->per_cu->offset;
8823 struct dwarf2_section_info *section = cu->per_cu->section;
8824 struct create_dwo_cu_data *data = datap;
8825 struct dwo_file *dwo_file = data->dwo_file;
8826 struct dwo_unit *dwo_unit = &data->dwo_unit;
8827 struct attribute *attr;
8828
8829 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8830 if (attr == NULL)
8831 {
8832 complaint (&symfile_complaints,
8833 _("Dwarf Error: debug entry at offset 0x%x is missing"
8834 " its dwo_id [in module %s]"),
8835 offset.sect_off, dwo_file->dwo_name);
8836 return;
8837 }
8838
8839 dwo_unit->dwo_file = dwo_file;
8840 dwo_unit->signature = DW_UNSND (attr);
8841 dwo_unit->section = section;
8842 dwo_unit->offset = offset;
8843 dwo_unit->length = cu->per_cu->length;
8844
8845 if (dwarf2_read_debug)
8846 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
8847 offset.sect_off, hex_string (dwo_unit->signature));
8848 }
8849
8850 /* Create the dwo_unit for the lone CU in DWO_FILE.
8851 Note: This function processes DWO files only, not DWP files. */
8852
8853 static struct dwo_unit *
8854 create_dwo_cu (struct dwo_file *dwo_file)
8855 {
8856 struct objfile *objfile = dwarf2_per_objfile->objfile;
8857 struct dwarf2_section_info *section = &dwo_file->sections.info;
8858 bfd *abfd;
8859 htab_t cu_htab;
8860 const gdb_byte *info_ptr, *end_ptr;
8861 struct create_dwo_cu_data create_dwo_cu_data;
8862 struct dwo_unit *dwo_unit;
8863
8864 dwarf2_read_section (objfile, section);
8865 info_ptr = section->buffer;
8866
8867 if (info_ptr == NULL)
8868 return NULL;
8869
8870 /* We can't set abfd until now because the section may be empty or
8871 not present, in which case section->asection will be NULL. */
8872 abfd = section->asection->owner;
8873
8874 if (dwarf2_read_debug)
8875 {
8876 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
8877 bfd_section_name (abfd, section->asection),
8878 bfd_get_filename (abfd));
8879 }
8880
8881 create_dwo_cu_data.dwo_file = dwo_file;
8882 dwo_unit = NULL;
8883
8884 end_ptr = info_ptr + section->size;
8885 while (info_ptr < end_ptr)
8886 {
8887 struct dwarf2_per_cu_data per_cu;
8888
8889 memset (&create_dwo_cu_data.dwo_unit, 0,
8890 sizeof (create_dwo_cu_data.dwo_unit));
8891 memset (&per_cu, 0, sizeof (per_cu));
8892 per_cu.objfile = objfile;
8893 per_cu.is_debug_types = 0;
8894 per_cu.offset.sect_off = info_ptr - section->buffer;
8895 per_cu.section = section;
8896
8897 init_cutu_and_read_dies_no_follow (&per_cu,
8898 &dwo_file->sections.abbrev,
8899 dwo_file,
8900 create_dwo_cu_reader,
8901 &create_dwo_cu_data);
8902
8903 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
8904 {
8905 /* If we've already found one, complain. We only support one
8906 because having more than one requires hacking the dwo_name of
8907 each to match, which is highly unlikely to happen. */
8908 if (dwo_unit != NULL)
8909 {
8910 complaint (&symfile_complaints,
8911 _("Multiple CUs in DWO file %s [in module %s]"),
8912 dwo_file->dwo_name, objfile_name (objfile));
8913 break;
8914 }
8915
8916 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8917 *dwo_unit = create_dwo_cu_data.dwo_unit;
8918 }
8919
8920 info_ptr += per_cu.length;
8921 }
8922
8923 return dwo_unit;
8924 }
8925
8926 /* DWP file .debug_{cu,tu}_index section format:
8927 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8928
8929 DWP Version 1:
8930
8931 Both index sections have the same format, and serve to map a 64-bit
8932 signature to a set of section numbers. Each section begins with a header,
8933 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8934 indexes, and a pool of 32-bit section numbers. The index sections will be
8935 aligned at 8-byte boundaries in the file.
8936
8937 The index section header consists of:
8938
8939 V, 32 bit version number
8940 -, 32 bits unused
8941 N, 32 bit number of compilation units or type units in the index
8942 M, 32 bit number of slots in the hash table
8943
8944 Numbers are recorded using the byte order of the application binary.
8945
8946 We assume that N and M will not exceed 2^32 - 1.
8947
8948 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8949
8950 The hash table begins at offset 16 in the section, and consists of an array
8951 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8952 order of the application binary). Unused slots in the hash table are 0.
8953 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8954
8955 The parallel table begins immediately after the hash table
8956 (at offset 16 + 8 * M from the beginning of the section), and consists of an
8957 array of 32-bit indexes (using the byte order of the application binary),
8958 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8959 table contains a 32-bit index into the pool of section numbers. For unused
8960 hash table slots, the corresponding entry in the parallel table will be 0.
8961
8962 Given a 64-bit compilation unit signature or a type signature S, an entry
8963 in the hash table is located as follows:
8964
8965 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8966 the low-order k bits all set to 1.
8967
8968 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8969
8970 3) If the hash table entry at index H matches the signature, use that
8971 entry. If the hash table entry at index H is unused (all zeroes),
8972 terminate the search: the signature is not present in the table.
8973
8974 4) Let H = (H + H') modulo M. Repeat at Step 3.
8975
8976 Because M > N and H' and M are relatively prime, the search is guaranteed
8977 to stop at an unused slot or find the match.
8978
8979 The pool of section numbers begins immediately following the hash table
8980 (at offset 16 + 12 * M from the beginning of the section). The pool of
8981 section numbers consists of an array of 32-bit words (using the byte order
8982 of the application binary). Each item in the array is indexed starting
8983 from 0. The hash table entry provides the index of the first section
8984 number in the set. Additional section numbers in the set follow, and the
8985 set is terminated by a 0 entry (section number 0 is not used in ELF).
8986
8987 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8988 section must be the first entry in the set, and the .debug_abbrev.dwo must
8989 be the second entry. Other members of the set may follow in any order. */
8990
8991 /* Create a hash table to map DWO IDs to their CU/TU entry in
8992 .debug_{info,types}.dwo in DWP_FILE.
8993 Returns NULL if there isn't one.
8994 Note: This function processes DWP files only, not DWO files. */
8995
8996 static struct dwp_hash_table *
8997 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8998 {
8999 struct objfile *objfile = dwarf2_per_objfile->objfile;
9000 bfd *dbfd = dwp_file->dbfd;
9001 const gdb_byte *index_ptr, *index_end;
9002 struct dwarf2_section_info *index;
9003 uint32_t version, nr_units, nr_slots;
9004 struct dwp_hash_table *htab;
9005
9006 if (is_debug_types)
9007 index = &dwp_file->sections.tu_index;
9008 else
9009 index = &dwp_file->sections.cu_index;
9010
9011 if (dwarf2_section_empty_p (index))
9012 return NULL;
9013 dwarf2_read_section (objfile, index);
9014
9015 index_ptr = index->buffer;
9016 index_end = index_ptr + index->size;
9017
9018 version = read_4_bytes (dbfd, index_ptr);
9019 index_ptr += 8; /* Skip the unused word. */
9020 nr_units = read_4_bytes (dbfd, index_ptr);
9021 index_ptr += 4;
9022 nr_slots = read_4_bytes (dbfd, index_ptr);
9023 index_ptr += 4;
9024
9025 if (version != 1)
9026 {
9027 error (_("Dwarf Error: unsupported DWP file version (%s)"
9028 " [in module %s]"),
9029 pulongest (version), dwp_file->name);
9030 }
9031 if (nr_slots != (nr_slots & -nr_slots))
9032 {
9033 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
9034 " is not power of 2 [in module %s]"),
9035 pulongest (nr_slots), dwp_file->name);
9036 }
9037
9038 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
9039 htab->nr_units = nr_units;
9040 htab->nr_slots = nr_slots;
9041 htab->hash_table = index_ptr;
9042 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
9043 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
9044
9045 return htab;
9046 }
9047
9048 /* Update SECTIONS with the data from SECTP.
9049
9050 This function is like the other "locate" section routines that are
9051 passed to bfd_map_over_sections, but in this context the sections to
9052 read comes from the DWP hash table, not the full ELF section table.
9053
9054 The result is non-zero for success, or zero if an error was found. */
9055
9056 static int
9057 locate_virtual_dwo_sections (asection *sectp,
9058 struct virtual_dwo_sections *sections)
9059 {
9060 const struct dwop_section_names *names = &dwop_section_names;
9061
9062 if (section_is_p (sectp->name, &names->abbrev_dwo))
9063 {
9064 /* There can be only one. */
9065 if (sections->abbrev.asection != NULL)
9066 return 0;
9067 sections->abbrev.asection = sectp;
9068 sections->abbrev.size = bfd_get_section_size (sectp);
9069 }
9070 else if (section_is_p (sectp->name, &names->info_dwo)
9071 || section_is_p (sectp->name, &names->types_dwo))
9072 {
9073 /* There can be only one. */
9074 if (sections->info_or_types.asection != NULL)
9075 return 0;
9076 sections->info_or_types.asection = sectp;
9077 sections->info_or_types.size = bfd_get_section_size (sectp);
9078 }
9079 else if (section_is_p (sectp->name, &names->line_dwo))
9080 {
9081 /* There can be only one. */
9082 if (sections->line.asection != NULL)
9083 return 0;
9084 sections->line.asection = sectp;
9085 sections->line.size = bfd_get_section_size (sectp);
9086 }
9087 else if (section_is_p (sectp->name, &names->loc_dwo))
9088 {
9089 /* There can be only one. */
9090 if (sections->loc.asection != NULL)
9091 return 0;
9092 sections->loc.asection = sectp;
9093 sections->loc.size = bfd_get_section_size (sectp);
9094 }
9095 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9096 {
9097 /* There can be only one. */
9098 if (sections->macinfo.asection != NULL)
9099 return 0;
9100 sections->macinfo.asection = sectp;
9101 sections->macinfo.size = bfd_get_section_size (sectp);
9102 }
9103 else if (section_is_p (sectp->name, &names->macro_dwo))
9104 {
9105 /* There can be only one. */
9106 if (sections->macro.asection != NULL)
9107 return 0;
9108 sections->macro.asection = sectp;
9109 sections->macro.size = bfd_get_section_size (sectp);
9110 }
9111 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9112 {
9113 /* There can be only one. */
9114 if (sections->str_offsets.asection != NULL)
9115 return 0;
9116 sections->str_offsets.asection = sectp;
9117 sections->str_offsets.size = bfd_get_section_size (sectp);
9118 }
9119 else
9120 {
9121 /* No other kind of section is valid. */
9122 return 0;
9123 }
9124
9125 return 1;
9126 }
9127
9128 /* Create a dwo_unit object for the DWO with signature SIGNATURE.
9129 HTAB is the hash table from the DWP file.
9130 SECTION_INDEX is the index of the DWO in HTAB.
9131 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU. */
9132
9133 static struct dwo_unit *
9134 create_dwo_in_dwp (struct dwp_file *dwp_file,
9135 const struct dwp_hash_table *htab,
9136 uint32_t section_index,
9137 const char *comp_dir,
9138 ULONGEST signature, int is_debug_types)
9139 {
9140 struct objfile *objfile = dwarf2_per_objfile->objfile;
9141 bfd *dbfd = dwp_file->dbfd;
9142 const char *kind = is_debug_types ? "TU" : "CU";
9143 struct dwo_file *dwo_file;
9144 struct dwo_unit *dwo_unit;
9145 struct virtual_dwo_sections sections;
9146 void **dwo_file_slot;
9147 char *virtual_dwo_name;
9148 struct dwarf2_section_info *cutu;
9149 struct cleanup *cleanups;
9150 int i;
9151
9152 if (dwarf2_read_debug)
9153 {
9154 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP file: %s\n",
9155 kind,
9156 pulongest (section_index), hex_string (signature),
9157 dwp_file->name);
9158 }
9159
9160 /* Fetch the sections of this DWO.
9161 Put a limit on the number of sections we look for so that bad data
9162 doesn't cause us to loop forever. */
9163
9164 #define MAX_NR_DWO_SECTIONS \
9165 (1 /* .debug_info or .debug_types */ \
9166 + 1 /* .debug_abbrev */ \
9167 + 1 /* .debug_line */ \
9168 + 1 /* .debug_loc */ \
9169 + 1 /* .debug_str_offsets */ \
9170 + 1 /* .debug_macro */ \
9171 + 1 /* .debug_macinfo */ \
9172 + 1 /* trailing zero */)
9173
9174 memset (&sections, 0, sizeof (sections));
9175 cleanups = make_cleanup (null_cleanup, 0);
9176
9177 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
9178 {
9179 asection *sectp;
9180 uint32_t section_nr =
9181 read_4_bytes (dbfd,
9182 htab->section_pool
9183 + (section_index + i) * sizeof (uint32_t));
9184
9185 if (section_nr == 0)
9186 break;
9187 if (section_nr >= dwp_file->num_sections)
9188 {
9189 error (_("Dwarf Error: bad DWP hash table, section number too large"
9190 " [in module %s]"),
9191 dwp_file->name);
9192 }
9193
9194 sectp = dwp_file->elf_sections[section_nr];
9195 if (! locate_virtual_dwo_sections (sectp, &sections))
9196 {
9197 error (_("Dwarf Error: bad DWP hash table, invalid section found"
9198 " [in module %s]"),
9199 dwp_file->name);
9200 }
9201 }
9202
9203 if (i < 2
9204 || sections.info_or_types.asection == NULL
9205 || sections.abbrev.asection == NULL)
9206 {
9207 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
9208 " [in module %s]"),
9209 dwp_file->name);
9210 }
9211 if (i == MAX_NR_DWO_SECTIONS)
9212 {
9213 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
9214 " [in module %s]"),
9215 dwp_file->name);
9216 }
9217
9218 /* It's easier for the rest of the code if we fake a struct dwo_file and
9219 have dwo_unit "live" in that. At least for now.
9220
9221 The DWP file can be made up of a random collection of CUs and TUs.
9222 However, for each CU + set of TUs that came from the same original DWO
9223 file, we can combine them back into a virtual DWO file to save space
9224 (fewer struct dwo_file objects to allocate). Remember that for really
9225 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
9226
9227 virtual_dwo_name =
9228 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
9229 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
9230 sections.line.asection ? sections.line.asection->id : 0,
9231 sections.loc.asection ? sections.loc.asection->id : 0,
9232 (sections.str_offsets.asection
9233 ? sections.str_offsets.asection->id
9234 : 0));
9235 make_cleanup (xfree, virtual_dwo_name);
9236 /* Can we use an existing virtual DWO file? */
9237 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
9238 /* Create one if necessary. */
9239 if (*dwo_file_slot == NULL)
9240 {
9241 if (dwarf2_read_debug)
9242 {
9243 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
9244 virtual_dwo_name);
9245 }
9246 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
9247 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
9248 virtual_dwo_name,
9249 strlen (virtual_dwo_name));
9250 dwo_file->comp_dir = comp_dir;
9251 dwo_file->sections.abbrev = sections.abbrev;
9252 dwo_file->sections.line = sections.line;
9253 dwo_file->sections.loc = sections.loc;
9254 dwo_file->sections.macinfo = sections.macinfo;
9255 dwo_file->sections.macro = sections.macro;
9256 dwo_file->sections.str_offsets = sections.str_offsets;
9257 /* The "str" section is global to the entire DWP file. */
9258 dwo_file->sections.str = dwp_file->sections.str;
9259 /* The info or types section is assigned below to dwo_unit,
9260 there's no need to record it in dwo_file.
9261 Also, we can't simply record type sections in dwo_file because
9262 we record a pointer into the vector in dwo_unit. As we collect more
9263 types we'll grow the vector and eventually have to reallocate space
9264 for it, invalidating all copies of pointers into the previous
9265 contents. */
9266 *dwo_file_slot = dwo_file;
9267 }
9268 else
9269 {
9270 if (dwarf2_read_debug)
9271 {
9272 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
9273 virtual_dwo_name);
9274 }
9275 dwo_file = *dwo_file_slot;
9276 }
9277 do_cleanups (cleanups);
9278
9279 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9280 dwo_unit->dwo_file = dwo_file;
9281 dwo_unit->signature = signature;
9282 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
9283 sizeof (struct dwarf2_section_info));
9284 *dwo_unit->section = sections.info_or_types;
9285 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
9286
9287 return dwo_unit;
9288 }
9289
9290 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
9291 Returns NULL if the signature isn't found. */
9292
9293 static struct dwo_unit *
9294 lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
9295 ULONGEST signature, int is_debug_types)
9296 {
9297 const struct dwp_hash_table *dwp_htab =
9298 is_debug_types ? dwp_file->tus : dwp_file->cus;
9299 bfd *dbfd = dwp_file->dbfd;
9300 uint32_t mask = dwp_htab->nr_slots - 1;
9301 uint32_t hash = signature & mask;
9302 uint32_t hash2 = ((signature >> 32) & mask) | 1;
9303 unsigned int i;
9304 void **slot;
9305 struct dwo_unit find_dwo_cu, *dwo_cu;
9306
9307 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
9308 find_dwo_cu.signature = signature;
9309 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
9310
9311 if (*slot != NULL)
9312 return *slot;
9313
9314 /* Use a for loop so that we don't loop forever on bad debug info. */
9315 for (i = 0; i < dwp_htab->nr_slots; ++i)
9316 {
9317 ULONGEST signature_in_table;
9318
9319 signature_in_table =
9320 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
9321 if (signature_in_table == signature)
9322 {
9323 uint32_t unit_index =
9324 read_4_bytes (dbfd,
9325 dwp_htab->unit_table + hash * sizeof (uint32_t));
9326
9327 *slot = create_dwo_in_dwp (dwp_file, dwp_htab, unit_index,
9328 comp_dir, signature, is_debug_types);
9329 return *slot;
9330 }
9331 if (signature_in_table == 0)
9332 return NULL;
9333 hash = (hash + hash2) & mask;
9334 }
9335
9336 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
9337 " [in module %s]"),
9338 dwp_file->name);
9339 }
9340
9341 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
9342 Open the file specified by FILE_NAME and hand it off to BFD for
9343 preliminary analysis. Return a newly initialized bfd *, which
9344 includes a canonicalized copy of FILE_NAME.
9345 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
9346 SEARCH_CWD is true if the current directory is to be searched.
9347 It will be searched before debug-file-directory.
9348 If unable to find/open the file, return NULL.
9349 NOTE: This function is derived from symfile_bfd_open. */
9350
9351 static bfd *
9352 try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
9353 {
9354 bfd *sym_bfd;
9355 int desc, flags;
9356 char *absolute_name;
9357 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
9358 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
9359 to debug_file_directory. */
9360 char *search_path;
9361 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
9362
9363 if (search_cwd)
9364 {
9365 if (*debug_file_directory != '\0')
9366 search_path = concat (".", dirname_separator_string,
9367 debug_file_directory, NULL);
9368 else
9369 search_path = xstrdup (".");
9370 }
9371 else
9372 search_path = xstrdup (debug_file_directory);
9373
9374 flags = OPF_RETURN_REALPATH;
9375 if (is_dwp)
9376 flags |= OPF_SEARCH_IN_PATH;
9377 desc = openp (search_path, flags, file_name,
9378 O_RDONLY | O_BINARY, &absolute_name);
9379 xfree (search_path);
9380 if (desc < 0)
9381 return NULL;
9382
9383 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
9384 xfree (absolute_name);
9385 if (sym_bfd == NULL)
9386 return NULL;
9387 bfd_set_cacheable (sym_bfd, 1);
9388
9389 if (!bfd_check_format (sym_bfd, bfd_object))
9390 {
9391 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
9392 return NULL;
9393 }
9394
9395 return sym_bfd;
9396 }
9397
9398 /* Try to open DWO file FILE_NAME.
9399 COMP_DIR is the DW_AT_comp_dir attribute.
9400 The result is the bfd handle of the file.
9401 If there is a problem finding or opening the file, return NULL.
9402 Upon success, the canonicalized path of the file is stored in the bfd,
9403 same as symfile_bfd_open. */
9404
9405 static bfd *
9406 open_dwo_file (const char *file_name, const char *comp_dir)
9407 {
9408 bfd *abfd;
9409
9410 if (IS_ABSOLUTE_PATH (file_name))
9411 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
9412
9413 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
9414
9415 if (comp_dir != NULL)
9416 {
9417 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
9418
9419 /* NOTE: If comp_dir is a relative path, this will also try the
9420 search path, which seems useful. */
9421 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/, 1 /*search_cwd*/);
9422 xfree (path_to_try);
9423 if (abfd != NULL)
9424 return abfd;
9425 }
9426
9427 /* That didn't work, try debug-file-directory, which, despite its name,
9428 is a list of paths. */
9429
9430 if (*debug_file_directory == '\0')
9431 return NULL;
9432
9433 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
9434 }
9435
9436 /* This function is mapped across the sections and remembers the offset and
9437 size of each of the DWO debugging sections we are interested in. */
9438
9439 static void
9440 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
9441 {
9442 struct dwo_sections *dwo_sections = dwo_sections_ptr;
9443 const struct dwop_section_names *names = &dwop_section_names;
9444
9445 if (section_is_p (sectp->name, &names->abbrev_dwo))
9446 {
9447 dwo_sections->abbrev.asection = sectp;
9448 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
9449 }
9450 else if (section_is_p (sectp->name, &names->info_dwo))
9451 {
9452 dwo_sections->info.asection = sectp;
9453 dwo_sections->info.size = bfd_get_section_size (sectp);
9454 }
9455 else if (section_is_p (sectp->name, &names->line_dwo))
9456 {
9457 dwo_sections->line.asection = sectp;
9458 dwo_sections->line.size = bfd_get_section_size (sectp);
9459 }
9460 else if (section_is_p (sectp->name, &names->loc_dwo))
9461 {
9462 dwo_sections->loc.asection = sectp;
9463 dwo_sections->loc.size = bfd_get_section_size (sectp);
9464 }
9465 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9466 {
9467 dwo_sections->macinfo.asection = sectp;
9468 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
9469 }
9470 else if (section_is_p (sectp->name, &names->macro_dwo))
9471 {
9472 dwo_sections->macro.asection = sectp;
9473 dwo_sections->macro.size = bfd_get_section_size (sectp);
9474 }
9475 else if (section_is_p (sectp->name, &names->str_dwo))
9476 {
9477 dwo_sections->str.asection = sectp;
9478 dwo_sections->str.size = bfd_get_section_size (sectp);
9479 }
9480 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9481 {
9482 dwo_sections->str_offsets.asection = sectp;
9483 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
9484 }
9485 else if (section_is_p (sectp->name, &names->types_dwo))
9486 {
9487 struct dwarf2_section_info type_section;
9488
9489 memset (&type_section, 0, sizeof (type_section));
9490 type_section.asection = sectp;
9491 type_section.size = bfd_get_section_size (sectp);
9492 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
9493 &type_section);
9494 }
9495 }
9496
9497 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
9498 by PER_CU. This is for the non-DWP case.
9499 The result is NULL if DWO_NAME can't be found. */
9500
9501 static struct dwo_file *
9502 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
9503 const char *dwo_name, const char *comp_dir)
9504 {
9505 struct objfile *objfile = dwarf2_per_objfile->objfile;
9506 struct dwo_file *dwo_file;
9507 bfd *dbfd;
9508 struct cleanup *cleanups;
9509
9510 dbfd = open_dwo_file (dwo_name, comp_dir);
9511 if (dbfd == NULL)
9512 {
9513 if (dwarf2_read_debug)
9514 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
9515 return NULL;
9516 }
9517 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
9518 dwo_file->dwo_name = dwo_name;
9519 dwo_file->comp_dir = comp_dir;
9520 dwo_file->dbfd = dbfd;
9521
9522 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
9523
9524 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
9525
9526 dwo_file->cu = create_dwo_cu (dwo_file);
9527
9528 dwo_file->tus = create_debug_types_hash_table (dwo_file,
9529 dwo_file->sections.types);
9530
9531 discard_cleanups (cleanups);
9532
9533 if (dwarf2_read_debug)
9534 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9535
9536 return dwo_file;
9537 }
9538
9539 /* This function is mapped across the sections and remembers the offset and
9540 size of each of the DWP debugging sections we are interested in. */
9541
9542 static void
9543 dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
9544 {
9545 struct dwp_file *dwp_file = dwp_file_ptr;
9546 const struct dwop_section_names *names = &dwop_section_names;
9547 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9548
9549 /* Record the ELF section number for later lookup: this is what the
9550 .debug_cu_index,.debug_tu_index tables use. */
9551 gdb_assert (elf_section_nr < dwp_file->num_sections);
9552 dwp_file->elf_sections[elf_section_nr] = sectp;
9553
9554 /* Look for specific sections that we need. */
9555 if (section_is_p (sectp->name, &names->str_dwo))
9556 {
9557 dwp_file->sections.str.asection = sectp;
9558 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9559 }
9560 else if (section_is_p (sectp->name, &names->cu_index))
9561 {
9562 dwp_file->sections.cu_index.asection = sectp;
9563 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9564 }
9565 else if (section_is_p (sectp->name, &names->tu_index))
9566 {
9567 dwp_file->sections.tu_index.asection = sectp;
9568 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9569 }
9570 }
9571
9572 /* Hash function for dwp_file loaded CUs/TUs. */
9573
9574 static hashval_t
9575 hash_dwp_loaded_cutus (const void *item)
9576 {
9577 const struct dwo_unit *dwo_unit = item;
9578
9579 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9580 return dwo_unit->signature;
9581 }
9582
9583 /* Equality function for dwp_file loaded CUs/TUs. */
9584
9585 static int
9586 eq_dwp_loaded_cutus (const void *a, const void *b)
9587 {
9588 const struct dwo_unit *dua = a;
9589 const struct dwo_unit *dub = b;
9590
9591 return dua->signature == dub->signature;
9592 }
9593
9594 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
9595
9596 static htab_t
9597 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9598 {
9599 return htab_create_alloc_ex (3,
9600 hash_dwp_loaded_cutus,
9601 eq_dwp_loaded_cutus,
9602 NULL,
9603 &objfile->objfile_obstack,
9604 hashtab_obstack_allocate,
9605 dummy_obstack_deallocate);
9606 }
9607
9608 /* Try to open DWP file FILE_NAME.
9609 The result is the bfd handle of the file.
9610 If there is a problem finding or opening the file, return NULL.
9611 Upon success, the canonicalized path of the file is stored in the bfd,
9612 same as symfile_bfd_open. */
9613
9614 static bfd *
9615 open_dwp_file (const char *file_name)
9616 {
9617 bfd *abfd;
9618
9619 abfd = try_open_dwop_file (file_name, 1 /*is_dwp*/, 1 /*search_cwd*/);
9620 if (abfd != NULL)
9621 return abfd;
9622
9623 /* Work around upstream bug 15652.
9624 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
9625 [Whether that's a "bug" is debatable, but it is getting in our way.]
9626 We have no real idea where the dwp file is, because gdb's realpath-ing
9627 of the executable's path may have discarded the needed info.
9628 [IWBN if the dwp file name was recorded in the executable, akin to
9629 .gnu_debuglink, but that doesn't exist yet.]
9630 Strip the directory from FILE_NAME and search again. */
9631 if (*debug_file_directory != '\0')
9632 {
9633 /* Don't implicitly search the current directory here.
9634 If the user wants to search "." to handle this case,
9635 it must be added to debug-file-directory. */
9636 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
9637 0 /*search_cwd*/);
9638 }
9639
9640 return NULL;
9641 }
9642
9643 /* Initialize the use of the DWP file for the current objfile.
9644 By convention the name of the DWP file is ${objfile}.dwp.
9645 The result is NULL if it can't be found. */
9646
9647 static struct dwp_file *
9648 open_and_init_dwp_file (void)
9649 {
9650 struct objfile *objfile = dwarf2_per_objfile->objfile;
9651 struct dwp_file *dwp_file;
9652 char *dwp_name;
9653 bfd *dbfd;
9654 struct cleanup *cleanups;
9655
9656 /* Try to find first .dwp for the binary file before any symbolic links
9657 resolving. */
9658 dwp_name = xstrprintf ("%s.dwp", objfile->original_name);
9659 cleanups = make_cleanup (xfree, dwp_name);
9660
9661 dbfd = open_dwp_file (dwp_name);
9662 if (dbfd == NULL
9663 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
9664 {
9665 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
9666 dwp_name = xstrprintf ("%s.dwp", objfile_name (objfile));
9667 make_cleanup (xfree, dwp_name);
9668 dbfd = open_dwp_file (dwp_name);
9669 }
9670
9671 if (dbfd == NULL)
9672 {
9673 if (dwarf2_read_debug)
9674 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9675 do_cleanups (cleanups);
9676 return NULL;
9677 }
9678 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9679 dwp_file->name = bfd_get_filename (dbfd);
9680 dwp_file->dbfd = dbfd;
9681 do_cleanups (cleanups);
9682
9683 /* +1: section 0 is unused */
9684 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9685 dwp_file->elf_sections =
9686 OBSTACK_CALLOC (&objfile->objfile_obstack,
9687 dwp_file->num_sections, asection *);
9688
9689 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9690
9691 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9692
9693 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9694
9695 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9696
9697 if (dwarf2_read_debug)
9698 {
9699 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9700 fprintf_unfiltered (gdb_stdlog,
9701 " %s CUs, %s TUs\n",
9702 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
9703 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
9704 }
9705
9706 return dwp_file;
9707 }
9708
9709 /* Wrapper around open_and_init_dwp_file, only open it once. */
9710
9711 static struct dwp_file *
9712 get_dwp_file (void)
9713 {
9714 if (! dwarf2_per_objfile->dwp_checked)
9715 {
9716 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
9717 dwarf2_per_objfile->dwp_checked = 1;
9718 }
9719 return dwarf2_per_objfile->dwp_file;
9720 }
9721
9722 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9723 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9724 or in the DWP file for the objfile, referenced by THIS_UNIT.
9725 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9726 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9727
9728 This is called, for example, when wanting to read a variable with a
9729 complex location. Therefore we don't want to do file i/o for every call.
9730 Therefore we don't want to look for a DWO file on every call.
9731 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9732 then we check if we've already seen DWO_NAME, and only THEN do we check
9733 for a DWO file.
9734
9735 The result is a pointer to the dwo_unit object or NULL if we didn't find it
9736 (dwo_id mismatch or couldn't find the DWO/DWP file). */
9737
9738 static struct dwo_unit *
9739 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9740 const char *dwo_name, const char *comp_dir,
9741 ULONGEST signature, int is_debug_types)
9742 {
9743 struct objfile *objfile = dwarf2_per_objfile->objfile;
9744 const char *kind = is_debug_types ? "TU" : "CU";
9745 void **dwo_file_slot;
9746 struct dwo_file *dwo_file;
9747 struct dwp_file *dwp_file;
9748
9749 /* First see if there's a DWP file.
9750 If we have a DWP file but didn't find the DWO inside it, don't
9751 look for the original DWO file. It makes gdb behave differently
9752 depending on whether one is debugging in the build tree. */
9753
9754 dwp_file = get_dwp_file ();
9755 if (dwp_file != NULL)
9756 {
9757 const struct dwp_hash_table *dwp_htab =
9758 is_debug_types ? dwp_file->tus : dwp_file->cus;
9759
9760 if (dwp_htab != NULL)
9761 {
9762 struct dwo_unit *dwo_cutu =
9763 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
9764 signature, is_debug_types);
9765
9766 if (dwo_cutu != NULL)
9767 {
9768 if (dwarf2_read_debug)
9769 {
9770 fprintf_unfiltered (gdb_stdlog,
9771 "Virtual DWO %s %s found: @%s\n",
9772 kind, hex_string (signature),
9773 host_address_to_string (dwo_cutu));
9774 }
9775 return dwo_cutu;
9776 }
9777 }
9778 }
9779 else
9780 {
9781 /* No DWP file, look for the DWO file. */
9782
9783 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
9784 if (*dwo_file_slot == NULL)
9785 {
9786 /* Read in the file and build a table of the CUs/TUs it contains. */
9787 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
9788 }
9789 /* NOTE: This will be NULL if unable to open the file. */
9790 dwo_file = *dwo_file_slot;
9791
9792 if (dwo_file != NULL)
9793 {
9794 struct dwo_unit *dwo_cutu = NULL;
9795
9796 if (is_debug_types && dwo_file->tus)
9797 {
9798 struct dwo_unit find_dwo_cutu;
9799
9800 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9801 find_dwo_cutu.signature = signature;
9802 dwo_cutu = htab_find (dwo_file->tus, &find_dwo_cutu);
9803 }
9804 else if (!is_debug_types && dwo_file->cu)
9805 {
9806 if (signature == dwo_file->cu->signature)
9807 dwo_cutu = dwo_file->cu;
9808 }
9809
9810 if (dwo_cutu != NULL)
9811 {
9812 if (dwarf2_read_debug)
9813 {
9814 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9815 kind, dwo_name, hex_string (signature),
9816 host_address_to_string (dwo_cutu));
9817 }
9818 return dwo_cutu;
9819 }
9820 }
9821 }
9822
9823 /* We didn't find it. This could mean a dwo_id mismatch, or
9824 someone deleted the DWO/DWP file, or the search path isn't set up
9825 correctly to find the file. */
9826
9827 if (dwarf2_read_debug)
9828 {
9829 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9830 kind, dwo_name, hex_string (signature));
9831 }
9832
9833 /* This is a warning and not a complaint because it can be caused by
9834 pilot error (e.g., user accidentally deleting the DWO). */
9835 warning (_("Could not find DWO %s %s(%s) referenced by %s at offset 0x%x"
9836 " [in module %s]"),
9837 kind, dwo_name, hex_string (signature),
9838 this_unit->is_debug_types ? "TU" : "CU",
9839 this_unit->offset.sect_off, objfile_name (objfile));
9840 return NULL;
9841 }
9842
9843 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9844 See lookup_dwo_cutu_unit for details. */
9845
9846 static struct dwo_unit *
9847 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9848 const char *dwo_name, const char *comp_dir,
9849 ULONGEST signature)
9850 {
9851 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9852 }
9853
9854 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9855 See lookup_dwo_cutu_unit for details. */
9856
9857 static struct dwo_unit *
9858 lookup_dwo_type_unit (struct signatured_type *this_tu,
9859 const char *dwo_name, const char *comp_dir)
9860 {
9861 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9862 }
9863
9864 /* Traversal function for queue_and_load_all_dwo_tus. */
9865
9866 static int
9867 queue_and_load_dwo_tu (void **slot, void *info)
9868 {
9869 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
9870 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
9871 ULONGEST signature = dwo_unit->signature;
9872 struct signatured_type *sig_type =
9873 lookup_dwo_signatured_type (per_cu->cu, signature);
9874
9875 if (sig_type != NULL)
9876 {
9877 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
9878
9879 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
9880 a real dependency of PER_CU on SIG_TYPE. That is detected later
9881 while processing PER_CU. */
9882 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
9883 load_full_type_unit (sig_cu);
9884 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
9885 }
9886
9887 return 1;
9888 }
9889
9890 /* Queue all TUs contained in the DWO of PER_CU to be read in.
9891 The DWO may have the only definition of the type, though it may not be
9892 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
9893 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
9894
9895 static void
9896 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
9897 {
9898 struct dwo_unit *dwo_unit;
9899 struct dwo_file *dwo_file;
9900
9901 gdb_assert (!per_cu->is_debug_types);
9902 gdb_assert (get_dwp_file () == NULL);
9903 gdb_assert (per_cu->cu != NULL);
9904
9905 dwo_unit = per_cu->cu->dwo_unit;
9906 gdb_assert (dwo_unit != NULL);
9907
9908 dwo_file = dwo_unit->dwo_file;
9909 if (dwo_file->tus != NULL)
9910 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
9911 }
9912
9913 /* Free all resources associated with DWO_FILE.
9914 Close the DWO file and munmap the sections.
9915 All memory should be on the objfile obstack. */
9916
9917 static void
9918 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
9919 {
9920 int ix;
9921 struct dwarf2_section_info *section;
9922
9923 /* Note: dbfd is NULL for virtual DWO files. */
9924 gdb_bfd_unref (dwo_file->dbfd);
9925
9926 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9927 }
9928
9929 /* Wrapper for free_dwo_file for use in cleanups. */
9930
9931 static void
9932 free_dwo_file_cleanup (void *arg)
9933 {
9934 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9935 struct objfile *objfile = dwarf2_per_objfile->objfile;
9936
9937 free_dwo_file (dwo_file, objfile);
9938 }
9939
9940 /* Traversal function for free_dwo_files. */
9941
9942 static int
9943 free_dwo_file_from_slot (void **slot, void *info)
9944 {
9945 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9946 struct objfile *objfile = (struct objfile *) info;
9947
9948 free_dwo_file (dwo_file, objfile);
9949
9950 return 1;
9951 }
9952
9953 /* Free all resources associated with DWO_FILES. */
9954
9955 static void
9956 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9957 {
9958 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
9959 }
9960 \f
9961 /* Read in various DIEs. */
9962
9963 /* qsort helper for inherit_abstract_dies. */
9964
9965 static int
9966 unsigned_int_compar (const void *ap, const void *bp)
9967 {
9968 unsigned int a = *(unsigned int *) ap;
9969 unsigned int b = *(unsigned int *) bp;
9970
9971 return (a > b) - (b > a);
9972 }
9973
9974 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9975 Inherit only the children of the DW_AT_abstract_origin DIE not being
9976 already referenced by DW_AT_abstract_origin from the children of the
9977 current DIE. */
9978
9979 static void
9980 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9981 {
9982 struct die_info *child_die;
9983 unsigned die_children_count;
9984 /* CU offsets which were referenced by children of the current DIE. */
9985 sect_offset *offsets;
9986 sect_offset *offsets_end, *offsetp;
9987 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9988 struct die_info *origin_die;
9989 /* Iterator of the ORIGIN_DIE children. */
9990 struct die_info *origin_child_die;
9991 struct cleanup *cleanups;
9992 struct attribute *attr;
9993 struct dwarf2_cu *origin_cu;
9994 struct pending **origin_previous_list_in_scope;
9995
9996 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9997 if (!attr)
9998 return;
9999
10000 /* Note that following die references may follow to a die in a
10001 different cu. */
10002
10003 origin_cu = cu;
10004 origin_die = follow_die_ref (die, attr, &origin_cu);
10005
10006 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
10007 symbols in. */
10008 origin_previous_list_in_scope = origin_cu->list_in_scope;
10009 origin_cu->list_in_scope = cu->list_in_scope;
10010
10011 if (die->tag != origin_die->tag
10012 && !(die->tag == DW_TAG_inlined_subroutine
10013 && origin_die->tag == DW_TAG_subprogram))
10014 complaint (&symfile_complaints,
10015 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
10016 die->offset.sect_off, origin_die->offset.sect_off);
10017
10018 child_die = die->child;
10019 die_children_count = 0;
10020 while (child_die && child_die->tag)
10021 {
10022 child_die = sibling_die (child_die);
10023 die_children_count++;
10024 }
10025 offsets = xmalloc (sizeof (*offsets) * die_children_count);
10026 cleanups = make_cleanup (xfree, offsets);
10027
10028 offsets_end = offsets;
10029 child_die = die->child;
10030 while (child_die && child_die->tag)
10031 {
10032 /* For each CHILD_DIE, find the corresponding child of
10033 ORIGIN_DIE. If there is more than one layer of
10034 DW_AT_abstract_origin, follow them all; there shouldn't be,
10035 but GCC versions at least through 4.4 generate this (GCC PR
10036 40573). */
10037 struct die_info *child_origin_die = child_die;
10038 struct dwarf2_cu *child_origin_cu = cu;
10039
10040 while (1)
10041 {
10042 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
10043 child_origin_cu);
10044 if (attr == NULL)
10045 break;
10046 child_origin_die = follow_die_ref (child_origin_die, attr,
10047 &child_origin_cu);
10048 }
10049
10050 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
10051 counterpart may exist. */
10052 if (child_origin_die != child_die)
10053 {
10054 if (child_die->tag != child_origin_die->tag
10055 && !(child_die->tag == DW_TAG_inlined_subroutine
10056 && child_origin_die->tag == DW_TAG_subprogram))
10057 complaint (&symfile_complaints,
10058 _("Child DIE 0x%x and its abstract origin 0x%x have "
10059 "different tags"), child_die->offset.sect_off,
10060 child_origin_die->offset.sect_off);
10061 if (child_origin_die->parent != origin_die)
10062 complaint (&symfile_complaints,
10063 _("Child DIE 0x%x and its abstract origin 0x%x have "
10064 "different parents"), child_die->offset.sect_off,
10065 child_origin_die->offset.sect_off);
10066 else
10067 *offsets_end++ = child_origin_die->offset;
10068 }
10069 child_die = sibling_die (child_die);
10070 }
10071 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
10072 unsigned_int_compar);
10073 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
10074 if (offsetp[-1].sect_off == offsetp->sect_off)
10075 complaint (&symfile_complaints,
10076 _("Multiple children of DIE 0x%x refer "
10077 "to DIE 0x%x as their abstract origin"),
10078 die->offset.sect_off, offsetp->sect_off);
10079
10080 offsetp = offsets;
10081 origin_child_die = origin_die->child;
10082 while (origin_child_die && origin_child_die->tag)
10083 {
10084 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
10085 while (offsetp < offsets_end
10086 && offsetp->sect_off < origin_child_die->offset.sect_off)
10087 offsetp++;
10088 if (offsetp >= offsets_end
10089 || offsetp->sect_off > origin_child_die->offset.sect_off)
10090 {
10091 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
10092 process_die (origin_child_die, origin_cu);
10093 }
10094 origin_child_die = sibling_die (origin_child_die);
10095 }
10096 origin_cu->list_in_scope = origin_previous_list_in_scope;
10097
10098 do_cleanups (cleanups);
10099 }
10100
10101 static void
10102 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
10103 {
10104 struct objfile *objfile = cu->objfile;
10105 struct context_stack *new;
10106 CORE_ADDR lowpc;
10107 CORE_ADDR highpc;
10108 struct die_info *child_die;
10109 struct attribute *attr, *call_line, *call_file;
10110 const char *name;
10111 CORE_ADDR baseaddr;
10112 struct block *block;
10113 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
10114 VEC (symbolp) *template_args = NULL;
10115 struct template_symbol *templ_func = NULL;
10116
10117 if (inlined_func)
10118 {
10119 /* If we do not have call site information, we can't show the
10120 caller of this inlined function. That's too confusing, so
10121 only use the scope for local variables. */
10122 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
10123 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
10124 if (call_line == NULL || call_file == NULL)
10125 {
10126 read_lexical_block_scope (die, cu);
10127 return;
10128 }
10129 }
10130
10131 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10132
10133 name = dwarf2_name (die, cu);
10134
10135 /* Ignore functions with missing or empty names. These are actually
10136 illegal according to the DWARF standard. */
10137 if (name == NULL)
10138 {
10139 complaint (&symfile_complaints,
10140 _("missing name for subprogram DIE at %d"),
10141 die->offset.sect_off);
10142 return;
10143 }
10144
10145 /* Ignore functions with missing or invalid low and high pc attributes. */
10146 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
10147 {
10148 attr = dwarf2_attr (die, DW_AT_external, cu);
10149 if (!attr || !DW_UNSND (attr))
10150 complaint (&symfile_complaints,
10151 _("cannot get low and high bounds "
10152 "for subprogram DIE at %d"),
10153 die->offset.sect_off);
10154 return;
10155 }
10156
10157 lowpc += baseaddr;
10158 highpc += baseaddr;
10159
10160 /* If we have any template arguments, then we must allocate a
10161 different sort of symbol. */
10162 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
10163 {
10164 if (child_die->tag == DW_TAG_template_type_param
10165 || child_die->tag == DW_TAG_template_value_param)
10166 {
10167 templ_func = allocate_template_symbol (objfile);
10168 templ_func->base.is_cplus_template_function = 1;
10169 break;
10170 }
10171 }
10172
10173 new = push_context (0, lowpc);
10174 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
10175 (struct symbol *) templ_func);
10176
10177 /* If there is a location expression for DW_AT_frame_base, record
10178 it. */
10179 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
10180 if (attr)
10181 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
10182
10183 cu->list_in_scope = &local_symbols;
10184
10185 if (die->child != NULL)
10186 {
10187 child_die = die->child;
10188 while (child_die && child_die->tag)
10189 {
10190 if (child_die->tag == DW_TAG_template_type_param
10191 || child_die->tag == DW_TAG_template_value_param)
10192 {
10193 struct symbol *arg = new_symbol (child_die, NULL, cu);
10194
10195 if (arg != NULL)
10196 VEC_safe_push (symbolp, template_args, arg);
10197 }
10198 else
10199 process_die (child_die, cu);
10200 child_die = sibling_die (child_die);
10201 }
10202 }
10203
10204 inherit_abstract_dies (die, cu);
10205
10206 /* If we have a DW_AT_specification, we might need to import using
10207 directives from the context of the specification DIE. See the
10208 comment in determine_prefix. */
10209 if (cu->language == language_cplus
10210 && dwarf2_attr (die, DW_AT_specification, cu))
10211 {
10212 struct dwarf2_cu *spec_cu = cu;
10213 struct die_info *spec_die = die_specification (die, &spec_cu);
10214
10215 while (spec_die)
10216 {
10217 child_die = spec_die->child;
10218 while (child_die && child_die->tag)
10219 {
10220 if (child_die->tag == DW_TAG_imported_module)
10221 process_die (child_die, spec_cu);
10222 child_die = sibling_die (child_die);
10223 }
10224
10225 /* In some cases, GCC generates specification DIEs that
10226 themselves contain DW_AT_specification attributes. */
10227 spec_die = die_specification (spec_die, &spec_cu);
10228 }
10229 }
10230
10231 new = pop_context ();
10232 /* Make a block for the local symbols within. */
10233 block = finish_block (new->name, &local_symbols, new->old_blocks,
10234 lowpc, highpc, objfile);
10235
10236 /* For C++, set the block's scope. */
10237 if ((cu->language == language_cplus || cu->language == language_fortran)
10238 && cu->processing_has_namespace_info)
10239 block_set_scope (block, determine_prefix (die, cu),
10240 &objfile->objfile_obstack);
10241
10242 /* If we have address ranges, record them. */
10243 dwarf2_record_block_ranges (die, block, baseaddr, cu);
10244
10245 /* Attach template arguments to function. */
10246 if (! VEC_empty (symbolp, template_args))
10247 {
10248 gdb_assert (templ_func != NULL);
10249
10250 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
10251 templ_func->template_arguments
10252 = obstack_alloc (&objfile->objfile_obstack,
10253 (templ_func->n_template_arguments
10254 * sizeof (struct symbol *)));
10255 memcpy (templ_func->template_arguments,
10256 VEC_address (symbolp, template_args),
10257 (templ_func->n_template_arguments * sizeof (struct symbol *)));
10258 VEC_free (symbolp, template_args);
10259 }
10260
10261 /* In C++, we can have functions nested inside functions (e.g., when
10262 a function declares a class that has methods). This means that
10263 when we finish processing a function scope, we may need to go
10264 back to building a containing block's symbol lists. */
10265 local_symbols = new->locals;
10266 using_directives = new->using_directives;
10267
10268 /* If we've finished processing a top-level function, subsequent
10269 symbols go in the file symbol list. */
10270 if (outermost_context_p ())
10271 cu->list_in_scope = &file_symbols;
10272 }
10273
10274 /* Process all the DIES contained within a lexical block scope. Start
10275 a new scope, process the dies, and then close the scope. */
10276
10277 static void
10278 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
10279 {
10280 struct objfile *objfile = cu->objfile;
10281 struct context_stack *new;
10282 CORE_ADDR lowpc, highpc;
10283 struct die_info *child_die;
10284 CORE_ADDR baseaddr;
10285
10286 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10287
10288 /* Ignore blocks with missing or invalid low and high pc attributes. */
10289 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
10290 as multiple lexical blocks? Handling children in a sane way would
10291 be nasty. Might be easier to properly extend generic blocks to
10292 describe ranges. */
10293 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
10294 return;
10295 lowpc += baseaddr;
10296 highpc += baseaddr;
10297
10298 push_context (0, lowpc);
10299 if (die->child != NULL)
10300 {
10301 child_die = die->child;
10302 while (child_die && child_die->tag)
10303 {
10304 process_die (child_die, cu);
10305 child_die = sibling_die (child_die);
10306 }
10307 }
10308 new = pop_context ();
10309
10310 if (local_symbols != NULL || using_directives != NULL)
10311 {
10312 struct block *block
10313 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
10314 highpc, objfile);
10315
10316 /* Note that recording ranges after traversing children, as we
10317 do here, means that recording a parent's ranges entails
10318 walking across all its children's ranges as they appear in
10319 the address map, which is quadratic behavior.
10320
10321 It would be nicer to record the parent's ranges before
10322 traversing its children, simply overriding whatever you find
10323 there. But since we don't even decide whether to create a
10324 block until after we've traversed its children, that's hard
10325 to do. */
10326 dwarf2_record_block_ranges (die, block, baseaddr, cu);
10327 }
10328 local_symbols = new->locals;
10329 using_directives = new->using_directives;
10330 }
10331
10332 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
10333
10334 static void
10335 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
10336 {
10337 struct objfile *objfile = cu->objfile;
10338 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10339 CORE_ADDR pc, baseaddr;
10340 struct attribute *attr;
10341 struct call_site *call_site, call_site_local;
10342 void **slot;
10343 int nparams;
10344 struct die_info *child_die;
10345
10346 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10347
10348 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10349 if (!attr)
10350 {
10351 complaint (&symfile_complaints,
10352 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
10353 "DIE 0x%x [in module %s]"),
10354 die->offset.sect_off, objfile_name (objfile));
10355 return;
10356 }
10357 pc = DW_ADDR (attr) + baseaddr;
10358
10359 if (cu->call_site_htab == NULL)
10360 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
10361 NULL, &objfile->objfile_obstack,
10362 hashtab_obstack_allocate, NULL);
10363 call_site_local.pc = pc;
10364 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
10365 if (*slot != NULL)
10366 {
10367 complaint (&symfile_complaints,
10368 _("Duplicate PC %s for DW_TAG_GNU_call_site "
10369 "DIE 0x%x [in module %s]"),
10370 paddress (gdbarch, pc), die->offset.sect_off,
10371 objfile_name (objfile));
10372 return;
10373 }
10374
10375 /* Count parameters at the caller. */
10376
10377 nparams = 0;
10378 for (child_die = die->child; child_die && child_die->tag;
10379 child_die = sibling_die (child_die))
10380 {
10381 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10382 {
10383 complaint (&symfile_complaints,
10384 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
10385 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10386 child_die->tag, child_die->offset.sect_off,
10387 objfile_name (objfile));
10388 continue;
10389 }
10390
10391 nparams++;
10392 }
10393
10394 call_site = obstack_alloc (&objfile->objfile_obstack,
10395 (sizeof (*call_site)
10396 + (sizeof (*call_site->parameter)
10397 * (nparams - 1))));
10398 *slot = call_site;
10399 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
10400 call_site->pc = pc;
10401
10402 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
10403 {
10404 struct die_info *func_die;
10405
10406 /* Skip also over DW_TAG_inlined_subroutine. */
10407 for (func_die = die->parent;
10408 func_die && func_die->tag != DW_TAG_subprogram
10409 && func_die->tag != DW_TAG_subroutine_type;
10410 func_die = func_die->parent);
10411
10412 /* DW_AT_GNU_all_call_sites is a superset
10413 of DW_AT_GNU_all_tail_call_sites. */
10414 if (func_die
10415 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
10416 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
10417 {
10418 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
10419 not complete. But keep CALL_SITE for look ups via call_site_htab,
10420 both the initial caller containing the real return address PC and
10421 the final callee containing the current PC of a chain of tail
10422 calls do not need to have the tail call list complete. But any
10423 function candidate for a virtual tail call frame searched via
10424 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
10425 determined unambiguously. */
10426 }
10427 else
10428 {
10429 struct type *func_type = NULL;
10430
10431 if (func_die)
10432 func_type = get_die_type (func_die, cu);
10433 if (func_type != NULL)
10434 {
10435 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
10436
10437 /* Enlist this call site to the function. */
10438 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
10439 TYPE_TAIL_CALL_LIST (func_type) = call_site;
10440 }
10441 else
10442 complaint (&symfile_complaints,
10443 _("Cannot find function owning DW_TAG_GNU_call_site "
10444 "DIE 0x%x [in module %s]"),
10445 die->offset.sect_off, objfile_name (objfile));
10446 }
10447 }
10448
10449 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
10450 if (attr == NULL)
10451 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
10452 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
10453 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
10454 /* Keep NULL DWARF_BLOCK. */;
10455 else if (attr_form_is_block (attr))
10456 {
10457 struct dwarf2_locexpr_baton *dlbaton;
10458
10459 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
10460 dlbaton->data = DW_BLOCK (attr)->data;
10461 dlbaton->size = DW_BLOCK (attr)->size;
10462 dlbaton->per_cu = cu->per_cu;
10463
10464 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
10465 }
10466 else if (attr_form_is_ref (attr))
10467 {
10468 struct dwarf2_cu *target_cu = cu;
10469 struct die_info *target_die;
10470
10471 target_die = follow_die_ref (die, attr, &target_cu);
10472 gdb_assert (target_cu->objfile == objfile);
10473 if (die_is_declaration (target_die, target_cu))
10474 {
10475 const char *target_physname = NULL;
10476 struct attribute *target_attr;
10477
10478 /* Prefer the mangled name; otherwise compute the demangled one. */
10479 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
10480 if (target_attr == NULL)
10481 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
10482 target_cu);
10483 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
10484 target_physname = DW_STRING (target_attr);
10485 else
10486 target_physname = dwarf2_physname (NULL, target_die, target_cu);
10487 if (target_physname == NULL)
10488 complaint (&symfile_complaints,
10489 _("DW_AT_GNU_call_site_target target DIE has invalid "
10490 "physname, for referencing DIE 0x%x [in module %s]"),
10491 die->offset.sect_off, objfile_name (objfile));
10492 else
10493 SET_FIELD_PHYSNAME (call_site->target, target_physname);
10494 }
10495 else
10496 {
10497 CORE_ADDR lowpc;
10498
10499 /* DW_AT_entry_pc should be preferred. */
10500 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
10501 complaint (&symfile_complaints,
10502 _("DW_AT_GNU_call_site_target target DIE has invalid "
10503 "low pc, for referencing DIE 0x%x [in module %s]"),
10504 die->offset.sect_off, objfile_name (objfile));
10505 else
10506 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
10507 }
10508 }
10509 else
10510 complaint (&symfile_complaints,
10511 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
10512 "block nor reference, for DIE 0x%x [in module %s]"),
10513 die->offset.sect_off, objfile_name (objfile));
10514
10515 call_site->per_cu = cu->per_cu;
10516
10517 for (child_die = die->child;
10518 child_die && child_die->tag;
10519 child_die = sibling_die (child_die))
10520 {
10521 struct call_site_parameter *parameter;
10522 struct attribute *loc, *origin;
10523
10524 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10525 {
10526 /* Already printed the complaint above. */
10527 continue;
10528 }
10529
10530 gdb_assert (call_site->parameter_count < nparams);
10531 parameter = &call_site->parameter[call_site->parameter_count];
10532
10533 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10534 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10535 register is contained in DW_AT_GNU_call_site_value. */
10536
10537 loc = dwarf2_attr (child_die, DW_AT_location, cu);
10538 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
10539 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
10540 {
10541 sect_offset offset;
10542
10543 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
10544 offset = dwarf2_get_ref_die_offset (origin);
10545 if (!offset_in_cu_p (&cu->header, offset))
10546 {
10547 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10548 binding can be done only inside one CU. Such referenced DIE
10549 therefore cannot be even moved to DW_TAG_partial_unit. */
10550 complaint (&symfile_complaints,
10551 _("DW_AT_abstract_origin offset is not in CU for "
10552 "DW_TAG_GNU_call_site child DIE 0x%x "
10553 "[in module %s]"),
10554 child_die->offset.sect_off, objfile_name (objfile));
10555 continue;
10556 }
10557 parameter->u.param_offset.cu_off = (offset.sect_off
10558 - cu->header.offset.sect_off);
10559 }
10560 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
10561 {
10562 complaint (&symfile_complaints,
10563 _("No DW_FORM_block* DW_AT_location for "
10564 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10565 child_die->offset.sect_off, objfile_name (objfile));
10566 continue;
10567 }
10568 else
10569 {
10570 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10571 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
10572 if (parameter->u.dwarf_reg != -1)
10573 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10574 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
10575 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
10576 &parameter->u.fb_offset))
10577 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10578 else
10579 {
10580 complaint (&symfile_complaints,
10581 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
10582 "for DW_FORM_block* DW_AT_location is supported for "
10583 "DW_TAG_GNU_call_site child DIE 0x%x "
10584 "[in module %s]"),
10585 child_die->offset.sect_off, objfile_name (objfile));
10586 continue;
10587 }
10588 }
10589
10590 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10591 if (!attr_form_is_block (attr))
10592 {
10593 complaint (&symfile_complaints,
10594 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
10595 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10596 child_die->offset.sect_off, objfile_name (objfile));
10597 continue;
10598 }
10599 parameter->value = DW_BLOCK (attr)->data;
10600 parameter->value_size = DW_BLOCK (attr)->size;
10601
10602 /* Parameters are not pre-cleared by memset above. */
10603 parameter->data_value = NULL;
10604 parameter->data_value_size = 0;
10605 call_site->parameter_count++;
10606
10607 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10608 if (attr)
10609 {
10610 if (!attr_form_is_block (attr))
10611 complaint (&symfile_complaints,
10612 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
10613 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10614 child_die->offset.sect_off, objfile_name (objfile));
10615 else
10616 {
10617 parameter->data_value = DW_BLOCK (attr)->data;
10618 parameter->data_value_size = DW_BLOCK (attr)->size;
10619 }
10620 }
10621 }
10622 }
10623
10624 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
10625 Return 1 if the attributes are present and valid, otherwise, return 0.
10626 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
10627
10628 static int
10629 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
10630 CORE_ADDR *high_return, struct dwarf2_cu *cu,
10631 struct partial_symtab *ranges_pst)
10632 {
10633 struct objfile *objfile = cu->objfile;
10634 struct comp_unit_head *cu_header = &cu->header;
10635 bfd *obfd = objfile->obfd;
10636 unsigned int addr_size = cu_header->addr_size;
10637 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10638 /* Base address selection entry. */
10639 CORE_ADDR base;
10640 int found_base;
10641 unsigned int dummy;
10642 const gdb_byte *buffer;
10643 CORE_ADDR marker;
10644 int low_set;
10645 CORE_ADDR low = 0;
10646 CORE_ADDR high = 0;
10647 CORE_ADDR baseaddr;
10648
10649 found_base = cu->base_known;
10650 base = cu->base_address;
10651
10652 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
10653 if (offset >= dwarf2_per_objfile->ranges.size)
10654 {
10655 complaint (&symfile_complaints,
10656 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10657 offset);
10658 return 0;
10659 }
10660 buffer = dwarf2_per_objfile->ranges.buffer + offset;
10661
10662 /* Read in the largest possible address. */
10663 marker = read_address (obfd, buffer, cu, &dummy);
10664 if ((marker & mask) == mask)
10665 {
10666 /* If we found the largest possible address, then
10667 read the base address. */
10668 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10669 buffer += 2 * addr_size;
10670 offset += 2 * addr_size;
10671 found_base = 1;
10672 }
10673
10674 low_set = 0;
10675
10676 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10677
10678 while (1)
10679 {
10680 CORE_ADDR range_beginning, range_end;
10681
10682 range_beginning = read_address (obfd, buffer, cu, &dummy);
10683 buffer += addr_size;
10684 range_end = read_address (obfd, buffer, cu, &dummy);
10685 buffer += addr_size;
10686 offset += 2 * addr_size;
10687
10688 /* An end of list marker is a pair of zero addresses. */
10689 if (range_beginning == 0 && range_end == 0)
10690 /* Found the end of list entry. */
10691 break;
10692
10693 /* Each base address selection entry is a pair of 2 values.
10694 The first is the largest possible address, the second is
10695 the base address. Check for a base address here. */
10696 if ((range_beginning & mask) == mask)
10697 {
10698 /* If we found the largest possible address, then
10699 read the base address. */
10700 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10701 found_base = 1;
10702 continue;
10703 }
10704
10705 if (!found_base)
10706 {
10707 /* We have no valid base address for the ranges
10708 data. */
10709 complaint (&symfile_complaints,
10710 _("Invalid .debug_ranges data (no base address)"));
10711 return 0;
10712 }
10713
10714 if (range_beginning > range_end)
10715 {
10716 /* Inverted range entries are invalid. */
10717 complaint (&symfile_complaints,
10718 _("Invalid .debug_ranges data (inverted range)"));
10719 return 0;
10720 }
10721
10722 /* Empty range entries have no effect. */
10723 if (range_beginning == range_end)
10724 continue;
10725
10726 range_beginning += base;
10727 range_end += base;
10728
10729 /* A not-uncommon case of bad debug info.
10730 Don't pollute the addrmap with bad data. */
10731 if (range_beginning + baseaddr == 0
10732 && !dwarf2_per_objfile->has_section_at_zero)
10733 {
10734 complaint (&symfile_complaints,
10735 _(".debug_ranges entry has start address of zero"
10736 " [in module %s]"), objfile_name (objfile));
10737 continue;
10738 }
10739
10740 if (ranges_pst != NULL)
10741 addrmap_set_empty (objfile->psymtabs_addrmap,
10742 range_beginning + baseaddr,
10743 range_end - 1 + baseaddr,
10744 ranges_pst);
10745
10746 /* FIXME: This is recording everything as a low-high
10747 segment of consecutive addresses. We should have a
10748 data structure for discontiguous block ranges
10749 instead. */
10750 if (! low_set)
10751 {
10752 low = range_beginning;
10753 high = range_end;
10754 low_set = 1;
10755 }
10756 else
10757 {
10758 if (range_beginning < low)
10759 low = range_beginning;
10760 if (range_end > high)
10761 high = range_end;
10762 }
10763 }
10764
10765 if (! low_set)
10766 /* If the first entry is an end-of-list marker, the range
10767 describes an empty scope, i.e. no instructions. */
10768 return 0;
10769
10770 if (low_return)
10771 *low_return = low;
10772 if (high_return)
10773 *high_return = high;
10774 return 1;
10775 }
10776
10777 /* Get low and high pc attributes from a die. Return 1 if the attributes
10778 are present and valid, otherwise, return 0. Return -1 if the range is
10779 discontinuous, i.e. derived from DW_AT_ranges information. */
10780
10781 static int
10782 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
10783 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10784 struct partial_symtab *pst)
10785 {
10786 struct attribute *attr;
10787 struct attribute *attr_high;
10788 CORE_ADDR low = 0;
10789 CORE_ADDR high = 0;
10790 int ret = 0;
10791
10792 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10793 if (attr_high)
10794 {
10795 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10796 if (attr)
10797 {
10798 low = DW_ADDR (attr);
10799 if (attr_high->form == DW_FORM_addr
10800 || attr_high->form == DW_FORM_GNU_addr_index)
10801 high = DW_ADDR (attr_high);
10802 else
10803 high = low + DW_UNSND (attr_high);
10804 }
10805 else
10806 /* Found high w/o low attribute. */
10807 return 0;
10808
10809 /* Found consecutive range of addresses. */
10810 ret = 1;
10811 }
10812 else
10813 {
10814 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10815 if (attr != NULL)
10816 {
10817 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10818 We take advantage of the fact that DW_AT_ranges does not appear
10819 in DW_TAG_compile_unit of DWO files. */
10820 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10821 unsigned int ranges_offset = (DW_UNSND (attr)
10822 + (need_ranges_base
10823 ? cu->ranges_base
10824 : 0));
10825
10826 /* Value of the DW_AT_ranges attribute is the offset in the
10827 .debug_ranges section. */
10828 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
10829 return 0;
10830 /* Found discontinuous range of addresses. */
10831 ret = -1;
10832 }
10833 }
10834
10835 /* read_partial_die has also the strict LOW < HIGH requirement. */
10836 if (high <= low)
10837 return 0;
10838
10839 /* When using the GNU linker, .gnu.linkonce. sections are used to
10840 eliminate duplicate copies of functions and vtables and such.
10841 The linker will arbitrarily choose one and discard the others.
10842 The AT_*_pc values for such functions refer to local labels in
10843 these sections. If the section from that file was discarded, the
10844 labels are not in the output, so the relocs get a value of 0.
10845 If this is a discarded function, mark the pc bounds as invalid,
10846 so that GDB will ignore it. */
10847 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
10848 return 0;
10849
10850 *lowpc = low;
10851 if (highpc)
10852 *highpc = high;
10853 return ret;
10854 }
10855
10856 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
10857 its low and high PC addresses. Do nothing if these addresses could not
10858 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10859 and HIGHPC to the high address if greater than HIGHPC. */
10860
10861 static void
10862 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10863 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10864 struct dwarf2_cu *cu)
10865 {
10866 CORE_ADDR low, high;
10867 struct die_info *child = die->child;
10868
10869 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
10870 {
10871 *lowpc = min (*lowpc, low);
10872 *highpc = max (*highpc, high);
10873 }
10874
10875 /* If the language does not allow nested subprograms (either inside
10876 subprograms or lexical blocks), we're done. */
10877 if (cu->language != language_ada)
10878 return;
10879
10880 /* Check all the children of the given DIE. If it contains nested
10881 subprograms, then check their pc bounds. Likewise, we need to
10882 check lexical blocks as well, as they may also contain subprogram
10883 definitions. */
10884 while (child && child->tag)
10885 {
10886 if (child->tag == DW_TAG_subprogram
10887 || child->tag == DW_TAG_lexical_block)
10888 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10889 child = sibling_die (child);
10890 }
10891 }
10892
10893 /* Get the low and high pc's represented by the scope DIE, and store
10894 them in *LOWPC and *HIGHPC. If the correct values can't be
10895 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10896
10897 static void
10898 get_scope_pc_bounds (struct die_info *die,
10899 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10900 struct dwarf2_cu *cu)
10901 {
10902 CORE_ADDR best_low = (CORE_ADDR) -1;
10903 CORE_ADDR best_high = (CORE_ADDR) 0;
10904 CORE_ADDR current_low, current_high;
10905
10906 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
10907 {
10908 best_low = current_low;
10909 best_high = current_high;
10910 }
10911 else
10912 {
10913 struct die_info *child = die->child;
10914
10915 while (child && child->tag)
10916 {
10917 switch (child->tag) {
10918 case DW_TAG_subprogram:
10919 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
10920 break;
10921 case DW_TAG_namespace:
10922 case DW_TAG_module:
10923 /* FIXME: carlton/2004-01-16: Should we do this for
10924 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10925 that current GCC's always emit the DIEs corresponding
10926 to definitions of methods of classes as children of a
10927 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10928 the DIEs giving the declarations, which could be
10929 anywhere). But I don't see any reason why the
10930 standards says that they have to be there. */
10931 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10932
10933 if (current_low != ((CORE_ADDR) -1))
10934 {
10935 best_low = min (best_low, current_low);
10936 best_high = max (best_high, current_high);
10937 }
10938 break;
10939 default:
10940 /* Ignore. */
10941 break;
10942 }
10943
10944 child = sibling_die (child);
10945 }
10946 }
10947
10948 *lowpc = best_low;
10949 *highpc = best_high;
10950 }
10951
10952 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
10953 in DIE. */
10954
10955 static void
10956 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10957 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10958 {
10959 struct objfile *objfile = cu->objfile;
10960 struct attribute *attr;
10961 struct attribute *attr_high;
10962
10963 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10964 if (attr_high)
10965 {
10966 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10967 if (attr)
10968 {
10969 CORE_ADDR low = DW_ADDR (attr);
10970 CORE_ADDR high;
10971 if (attr_high->form == DW_FORM_addr
10972 || attr_high->form == DW_FORM_GNU_addr_index)
10973 high = DW_ADDR (attr_high);
10974 else
10975 high = low + DW_UNSND (attr_high);
10976
10977 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10978 }
10979 }
10980
10981 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10982 if (attr)
10983 {
10984 bfd *obfd = objfile->obfd;
10985 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10986 We take advantage of the fact that DW_AT_ranges does not appear
10987 in DW_TAG_compile_unit of DWO files. */
10988 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10989
10990 /* The value of the DW_AT_ranges attribute is the offset of the
10991 address range list in the .debug_ranges section. */
10992 unsigned long offset = (DW_UNSND (attr)
10993 + (need_ranges_base ? cu->ranges_base : 0));
10994 const gdb_byte *buffer;
10995
10996 /* For some target architectures, but not others, the
10997 read_address function sign-extends the addresses it returns.
10998 To recognize base address selection entries, we need a
10999 mask. */
11000 unsigned int addr_size = cu->header.addr_size;
11001 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
11002
11003 /* The base address, to which the next pair is relative. Note
11004 that this 'base' is a DWARF concept: most entries in a range
11005 list are relative, to reduce the number of relocs against the
11006 debugging information. This is separate from this function's
11007 'baseaddr' argument, which GDB uses to relocate debugging
11008 information from a shared library based on the address at
11009 which the library was loaded. */
11010 CORE_ADDR base = cu->base_address;
11011 int base_known = cu->base_known;
11012
11013 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
11014 if (offset >= dwarf2_per_objfile->ranges.size)
11015 {
11016 complaint (&symfile_complaints,
11017 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
11018 offset);
11019 return;
11020 }
11021 buffer = dwarf2_per_objfile->ranges.buffer + offset;
11022
11023 for (;;)
11024 {
11025 unsigned int bytes_read;
11026 CORE_ADDR start, end;
11027
11028 start = read_address (obfd, buffer, cu, &bytes_read);
11029 buffer += bytes_read;
11030 end = read_address (obfd, buffer, cu, &bytes_read);
11031 buffer += bytes_read;
11032
11033 /* Did we find the end of the range list? */
11034 if (start == 0 && end == 0)
11035 break;
11036
11037 /* Did we find a base address selection entry? */
11038 else if ((start & base_select_mask) == base_select_mask)
11039 {
11040 base = end;
11041 base_known = 1;
11042 }
11043
11044 /* We found an ordinary address range. */
11045 else
11046 {
11047 if (!base_known)
11048 {
11049 complaint (&symfile_complaints,
11050 _("Invalid .debug_ranges data "
11051 "(no base address)"));
11052 return;
11053 }
11054
11055 if (start > end)
11056 {
11057 /* Inverted range entries are invalid. */
11058 complaint (&symfile_complaints,
11059 _("Invalid .debug_ranges data "
11060 "(inverted range)"));
11061 return;
11062 }
11063
11064 /* Empty range entries have no effect. */
11065 if (start == end)
11066 continue;
11067
11068 start += base + baseaddr;
11069 end += base + baseaddr;
11070
11071 /* A not-uncommon case of bad debug info.
11072 Don't pollute the addrmap with bad data. */
11073 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
11074 {
11075 complaint (&symfile_complaints,
11076 _(".debug_ranges entry has start address of zero"
11077 " [in module %s]"), objfile_name (objfile));
11078 continue;
11079 }
11080
11081 record_block_range (block, start, end - 1);
11082 }
11083 }
11084 }
11085 }
11086
11087 /* Check whether the producer field indicates either of GCC < 4.6, or the
11088 Intel C/C++ compiler, and cache the result in CU. */
11089
11090 static void
11091 check_producer (struct dwarf2_cu *cu)
11092 {
11093 const char *cs;
11094 int major, minor, release;
11095
11096 if (cu->producer == NULL)
11097 {
11098 /* For unknown compilers expect their behavior is DWARF version
11099 compliant.
11100
11101 GCC started to support .debug_types sections by -gdwarf-4 since
11102 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
11103 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
11104 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
11105 interpreted incorrectly by GDB now - GCC PR debug/48229. */
11106 }
11107 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
11108 {
11109 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
11110
11111 cs = &cu->producer[strlen ("GNU ")];
11112 while (*cs && !isdigit (*cs))
11113 cs++;
11114 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
11115 {
11116 /* Not recognized as GCC. */
11117 }
11118 else
11119 {
11120 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
11121 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
11122 }
11123 }
11124 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
11125 cu->producer_is_icc = 1;
11126 else
11127 {
11128 /* For other non-GCC compilers, expect their behavior is DWARF version
11129 compliant. */
11130 }
11131
11132 cu->checked_producer = 1;
11133 }
11134
11135 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
11136 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
11137 during 4.6.0 experimental. */
11138
11139 static int
11140 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
11141 {
11142 if (!cu->checked_producer)
11143 check_producer (cu);
11144
11145 return cu->producer_is_gxx_lt_4_6;
11146 }
11147
11148 /* Return the default accessibility type if it is not overriden by
11149 DW_AT_accessibility. */
11150
11151 static enum dwarf_access_attribute
11152 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
11153 {
11154 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
11155 {
11156 /* The default DWARF 2 accessibility for members is public, the default
11157 accessibility for inheritance is private. */
11158
11159 if (die->tag != DW_TAG_inheritance)
11160 return DW_ACCESS_public;
11161 else
11162 return DW_ACCESS_private;
11163 }
11164 else
11165 {
11166 /* DWARF 3+ defines the default accessibility a different way. The same
11167 rules apply now for DW_TAG_inheritance as for the members and it only
11168 depends on the container kind. */
11169
11170 if (die->parent->tag == DW_TAG_class_type)
11171 return DW_ACCESS_private;
11172 else
11173 return DW_ACCESS_public;
11174 }
11175 }
11176
11177 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
11178 offset. If the attribute was not found return 0, otherwise return
11179 1. If it was found but could not properly be handled, set *OFFSET
11180 to 0. */
11181
11182 static int
11183 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
11184 LONGEST *offset)
11185 {
11186 struct attribute *attr;
11187
11188 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
11189 if (attr != NULL)
11190 {
11191 *offset = 0;
11192
11193 /* Note that we do not check for a section offset first here.
11194 This is because DW_AT_data_member_location is new in DWARF 4,
11195 so if we see it, we can assume that a constant form is really
11196 a constant and not a section offset. */
11197 if (attr_form_is_constant (attr))
11198 *offset = dwarf2_get_attr_constant_value (attr, 0);
11199 else if (attr_form_is_section_offset (attr))
11200 dwarf2_complex_location_expr_complaint ();
11201 else if (attr_form_is_block (attr))
11202 *offset = decode_locdesc (DW_BLOCK (attr), cu);
11203 else
11204 dwarf2_complex_location_expr_complaint ();
11205
11206 return 1;
11207 }
11208
11209 return 0;
11210 }
11211
11212 /* Add an aggregate field to the field list. */
11213
11214 static void
11215 dwarf2_add_field (struct field_info *fip, struct die_info *die,
11216 struct dwarf2_cu *cu)
11217 {
11218 struct objfile *objfile = cu->objfile;
11219 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11220 struct nextfield *new_field;
11221 struct attribute *attr;
11222 struct field *fp;
11223 const char *fieldname = "";
11224
11225 /* Allocate a new field list entry and link it in. */
11226 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
11227 make_cleanup (xfree, new_field);
11228 memset (new_field, 0, sizeof (struct nextfield));
11229
11230 if (die->tag == DW_TAG_inheritance)
11231 {
11232 new_field->next = fip->baseclasses;
11233 fip->baseclasses = new_field;
11234 }
11235 else
11236 {
11237 new_field->next = fip->fields;
11238 fip->fields = new_field;
11239 }
11240 fip->nfields++;
11241
11242 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11243 if (attr)
11244 new_field->accessibility = DW_UNSND (attr);
11245 else
11246 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
11247 if (new_field->accessibility != DW_ACCESS_public)
11248 fip->non_public_fields = 1;
11249
11250 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11251 if (attr)
11252 new_field->virtuality = DW_UNSND (attr);
11253 else
11254 new_field->virtuality = DW_VIRTUALITY_none;
11255
11256 fp = &new_field->field;
11257
11258 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
11259 {
11260 LONGEST offset;
11261
11262 /* Data member other than a C++ static data member. */
11263
11264 /* Get type of field. */
11265 fp->type = die_type (die, cu);
11266
11267 SET_FIELD_BITPOS (*fp, 0);
11268
11269 /* Get bit size of field (zero if none). */
11270 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
11271 if (attr)
11272 {
11273 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
11274 }
11275 else
11276 {
11277 FIELD_BITSIZE (*fp) = 0;
11278 }
11279
11280 /* Get bit offset of field. */
11281 if (handle_data_member_location (die, cu, &offset))
11282 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
11283 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
11284 if (attr)
11285 {
11286 if (gdbarch_bits_big_endian (gdbarch))
11287 {
11288 /* For big endian bits, the DW_AT_bit_offset gives the
11289 additional bit offset from the MSB of the containing
11290 anonymous object to the MSB of the field. We don't
11291 have to do anything special since we don't need to
11292 know the size of the anonymous object. */
11293 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
11294 }
11295 else
11296 {
11297 /* For little endian bits, compute the bit offset to the
11298 MSB of the anonymous object, subtract off the number of
11299 bits from the MSB of the field to the MSB of the
11300 object, and then subtract off the number of bits of
11301 the field itself. The result is the bit offset of
11302 the LSB of the field. */
11303 int anonymous_size;
11304 int bit_offset = DW_UNSND (attr);
11305
11306 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11307 if (attr)
11308 {
11309 /* The size of the anonymous object containing
11310 the bit field is explicit, so use the
11311 indicated size (in bytes). */
11312 anonymous_size = DW_UNSND (attr);
11313 }
11314 else
11315 {
11316 /* The size of the anonymous object containing
11317 the bit field must be inferred from the type
11318 attribute of the data member containing the
11319 bit field. */
11320 anonymous_size = TYPE_LENGTH (fp->type);
11321 }
11322 SET_FIELD_BITPOS (*fp,
11323 (FIELD_BITPOS (*fp)
11324 + anonymous_size * bits_per_byte
11325 - bit_offset - FIELD_BITSIZE (*fp)));
11326 }
11327 }
11328
11329 /* Get name of field. */
11330 fieldname = dwarf2_name (die, cu);
11331 if (fieldname == NULL)
11332 fieldname = "";
11333
11334 /* The name is already allocated along with this objfile, so we don't
11335 need to duplicate it for the type. */
11336 fp->name = fieldname;
11337
11338 /* Change accessibility for artificial fields (e.g. virtual table
11339 pointer or virtual base class pointer) to private. */
11340 if (dwarf2_attr (die, DW_AT_artificial, cu))
11341 {
11342 FIELD_ARTIFICIAL (*fp) = 1;
11343 new_field->accessibility = DW_ACCESS_private;
11344 fip->non_public_fields = 1;
11345 }
11346 }
11347 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
11348 {
11349 /* C++ static member. */
11350
11351 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
11352 is a declaration, but all versions of G++ as of this writing
11353 (so through at least 3.2.1) incorrectly generate
11354 DW_TAG_variable tags. */
11355
11356 const char *physname;
11357
11358 /* Get name of field. */
11359 fieldname = dwarf2_name (die, cu);
11360 if (fieldname == NULL)
11361 return;
11362
11363 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11364 if (attr
11365 /* Only create a symbol if this is an external value.
11366 new_symbol checks this and puts the value in the global symbol
11367 table, which we want. If it is not external, new_symbol
11368 will try to put the value in cu->list_in_scope which is wrong. */
11369 && dwarf2_flag_true_p (die, DW_AT_external, cu))
11370 {
11371 /* A static const member, not much different than an enum as far as
11372 we're concerned, except that we can support more types. */
11373 new_symbol (die, NULL, cu);
11374 }
11375
11376 /* Get physical name. */
11377 physname = dwarf2_physname (fieldname, die, cu);
11378
11379 /* The name is already allocated along with this objfile, so we don't
11380 need to duplicate it for the type. */
11381 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
11382 FIELD_TYPE (*fp) = die_type (die, cu);
11383 FIELD_NAME (*fp) = fieldname;
11384 }
11385 else if (die->tag == DW_TAG_inheritance)
11386 {
11387 LONGEST offset;
11388
11389 /* C++ base class field. */
11390 if (handle_data_member_location (die, cu, &offset))
11391 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
11392 FIELD_BITSIZE (*fp) = 0;
11393 FIELD_TYPE (*fp) = die_type (die, cu);
11394 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
11395 fip->nbaseclasses++;
11396 }
11397 }
11398
11399 /* Add a typedef defined in the scope of the FIP's class. */
11400
11401 static void
11402 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
11403 struct dwarf2_cu *cu)
11404 {
11405 struct objfile *objfile = cu->objfile;
11406 struct typedef_field_list *new_field;
11407 struct attribute *attr;
11408 struct typedef_field *fp;
11409 char *fieldname = "";
11410
11411 /* Allocate a new field list entry and link it in. */
11412 new_field = xzalloc (sizeof (*new_field));
11413 make_cleanup (xfree, new_field);
11414
11415 gdb_assert (die->tag == DW_TAG_typedef);
11416
11417 fp = &new_field->field;
11418
11419 /* Get name of field. */
11420 fp->name = dwarf2_name (die, cu);
11421 if (fp->name == NULL)
11422 return;
11423
11424 fp->type = read_type_die (die, cu);
11425
11426 new_field->next = fip->typedef_field_list;
11427 fip->typedef_field_list = new_field;
11428 fip->typedef_field_list_count++;
11429 }
11430
11431 /* Create the vector of fields, and attach it to the type. */
11432
11433 static void
11434 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
11435 struct dwarf2_cu *cu)
11436 {
11437 int nfields = fip->nfields;
11438
11439 /* Record the field count, allocate space for the array of fields,
11440 and create blank accessibility bitfields if necessary. */
11441 TYPE_NFIELDS (type) = nfields;
11442 TYPE_FIELDS (type) = (struct field *)
11443 TYPE_ALLOC (type, sizeof (struct field) * nfields);
11444 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
11445
11446 if (fip->non_public_fields && cu->language != language_ada)
11447 {
11448 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11449
11450 TYPE_FIELD_PRIVATE_BITS (type) =
11451 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11452 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
11453
11454 TYPE_FIELD_PROTECTED_BITS (type) =
11455 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11456 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
11457
11458 TYPE_FIELD_IGNORE_BITS (type) =
11459 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11460 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
11461 }
11462
11463 /* If the type has baseclasses, allocate and clear a bit vector for
11464 TYPE_FIELD_VIRTUAL_BITS. */
11465 if (fip->nbaseclasses && cu->language != language_ada)
11466 {
11467 int num_bytes = B_BYTES (fip->nbaseclasses);
11468 unsigned char *pointer;
11469
11470 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11471 pointer = TYPE_ALLOC (type, num_bytes);
11472 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
11473 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
11474 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
11475 }
11476
11477 /* Copy the saved-up fields into the field vector. Start from the head of
11478 the list, adding to the tail of the field array, so that they end up in
11479 the same order in the array in which they were added to the list. */
11480 while (nfields-- > 0)
11481 {
11482 struct nextfield *fieldp;
11483
11484 if (fip->fields)
11485 {
11486 fieldp = fip->fields;
11487 fip->fields = fieldp->next;
11488 }
11489 else
11490 {
11491 fieldp = fip->baseclasses;
11492 fip->baseclasses = fieldp->next;
11493 }
11494
11495 TYPE_FIELD (type, nfields) = fieldp->field;
11496 switch (fieldp->accessibility)
11497 {
11498 case DW_ACCESS_private:
11499 if (cu->language != language_ada)
11500 SET_TYPE_FIELD_PRIVATE (type, nfields);
11501 break;
11502
11503 case DW_ACCESS_protected:
11504 if (cu->language != language_ada)
11505 SET_TYPE_FIELD_PROTECTED (type, nfields);
11506 break;
11507
11508 case DW_ACCESS_public:
11509 break;
11510
11511 default:
11512 /* Unknown accessibility. Complain and treat it as public. */
11513 {
11514 complaint (&symfile_complaints, _("unsupported accessibility %d"),
11515 fieldp->accessibility);
11516 }
11517 break;
11518 }
11519 if (nfields < fip->nbaseclasses)
11520 {
11521 switch (fieldp->virtuality)
11522 {
11523 case DW_VIRTUALITY_virtual:
11524 case DW_VIRTUALITY_pure_virtual:
11525 if (cu->language == language_ada)
11526 error (_("unexpected virtuality in component of Ada type"));
11527 SET_TYPE_FIELD_VIRTUAL (type, nfields);
11528 break;
11529 }
11530 }
11531 }
11532 }
11533
11534 /* Return true if this member function is a constructor, false
11535 otherwise. */
11536
11537 static int
11538 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
11539 {
11540 const char *fieldname;
11541 const char *typename;
11542 int len;
11543
11544 if (die->parent == NULL)
11545 return 0;
11546
11547 if (die->parent->tag != DW_TAG_structure_type
11548 && die->parent->tag != DW_TAG_union_type
11549 && die->parent->tag != DW_TAG_class_type)
11550 return 0;
11551
11552 fieldname = dwarf2_name (die, cu);
11553 typename = dwarf2_name (die->parent, cu);
11554 if (fieldname == NULL || typename == NULL)
11555 return 0;
11556
11557 len = strlen (fieldname);
11558 return (strncmp (fieldname, typename, len) == 0
11559 && (typename[len] == '\0' || typename[len] == '<'));
11560 }
11561
11562 /* Add a member function to the proper fieldlist. */
11563
11564 static void
11565 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
11566 struct type *type, struct dwarf2_cu *cu)
11567 {
11568 struct objfile *objfile = cu->objfile;
11569 struct attribute *attr;
11570 struct fnfieldlist *flp;
11571 int i;
11572 struct fn_field *fnp;
11573 const char *fieldname;
11574 struct nextfnfield *new_fnfield;
11575 struct type *this_type;
11576 enum dwarf_access_attribute accessibility;
11577
11578 if (cu->language == language_ada)
11579 error (_("unexpected member function in Ada type"));
11580
11581 /* Get name of member function. */
11582 fieldname = dwarf2_name (die, cu);
11583 if (fieldname == NULL)
11584 return;
11585
11586 /* Look up member function name in fieldlist. */
11587 for (i = 0; i < fip->nfnfields; i++)
11588 {
11589 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
11590 break;
11591 }
11592
11593 /* Create new list element if necessary. */
11594 if (i < fip->nfnfields)
11595 flp = &fip->fnfieldlists[i];
11596 else
11597 {
11598 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
11599 {
11600 fip->fnfieldlists = (struct fnfieldlist *)
11601 xrealloc (fip->fnfieldlists,
11602 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
11603 * sizeof (struct fnfieldlist));
11604 if (fip->nfnfields == 0)
11605 make_cleanup (free_current_contents, &fip->fnfieldlists);
11606 }
11607 flp = &fip->fnfieldlists[fip->nfnfields];
11608 flp->name = fieldname;
11609 flp->length = 0;
11610 flp->head = NULL;
11611 i = fip->nfnfields++;
11612 }
11613
11614 /* Create a new member function field and chain it to the field list
11615 entry. */
11616 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
11617 make_cleanup (xfree, new_fnfield);
11618 memset (new_fnfield, 0, sizeof (struct nextfnfield));
11619 new_fnfield->next = flp->head;
11620 flp->head = new_fnfield;
11621 flp->length++;
11622
11623 /* Fill in the member function field info. */
11624 fnp = &new_fnfield->fnfield;
11625
11626 /* Delay processing of the physname until later. */
11627 if (cu->language == language_cplus || cu->language == language_java)
11628 {
11629 add_to_method_list (type, i, flp->length - 1, fieldname,
11630 die, cu);
11631 }
11632 else
11633 {
11634 const char *physname = dwarf2_physname (fieldname, die, cu);
11635 fnp->physname = physname ? physname : "";
11636 }
11637
11638 fnp->type = alloc_type (objfile);
11639 this_type = read_type_die (die, cu);
11640 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
11641 {
11642 int nparams = TYPE_NFIELDS (this_type);
11643
11644 /* TYPE is the domain of this method, and THIS_TYPE is the type
11645 of the method itself (TYPE_CODE_METHOD). */
11646 smash_to_method_type (fnp->type, type,
11647 TYPE_TARGET_TYPE (this_type),
11648 TYPE_FIELDS (this_type),
11649 TYPE_NFIELDS (this_type),
11650 TYPE_VARARGS (this_type));
11651
11652 /* Handle static member functions.
11653 Dwarf2 has no clean way to discern C++ static and non-static
11654 member functions. G++ helps GDB by marking the first
11655 parameter for non-static member functions (which is the this
11656 pointer) as artificial. We obtain this information from
11657 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
11658 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
11659 fnp->voffset = VOFFSET_STATIC;
11660 }
11661 else
11662 complaint (&symfile_complaints, _("member function type missing for '%s'"),
11663 dwarf2_full_name (fieldname, die, cu));
11664
11665 /* Get fcontext from DW_AT_containing_type if present. */
11666 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11667 fnp->fcontext = die_containing_type (die, cu);
11668
11669 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11670 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
11671
11672 /* Get accessibility. */
11673 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11674 if (attr)
11675 accessibility = DW_UNSND (attr);
11676 else
11677 accessibility = dwarf2_default_access_attribute (die, cu);
11678 switch (accessibility)
11679 {
11680 case DW_ACCESS_private:
11681 fnp->is_private = 1;
11682 break;
11683 case DW_ACCESS_protected:
11684 fnp->is_protected = 1;
11685 break;
11686 }
11687
11688 /* Check for artificial methods. */
11689 attr = dwarf2_attr (die, DW_AT_artificial, cu);
11690 if (attr && DW_UNSND (attr) != 0)
11691 fnp->is_artificial = 1;
11692
11693 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11694
11695 /* Get index in virtual function table if it is a virtual member
11696 function. For older versions of GCC, this is an offset in the
11697 appropriate virtual table, as specified by DW_AT_containing_type.
11698 For everyone else, it is an expression to be evaluated relative
11699 to the object address. */
11700
11701 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
11702 if (attr)
11703 {
11704 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
11705 {
11706 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11707 {
11708 /* Old-style GCC. */
11709 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11710 }
11711 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11712 || (DW_BLOCK (attr)->size > 1
11713 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11714 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11715 {
11716 struct dwarf_block blk;
11717 int offset;
11718
11719 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11720 ? 1 : 2);
11721 blk.size = DW_BLOCK (attr)->size - offset;
11722 blk.data = DW_BLOCK (attr)->data + offset;
11723 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11724 if ((fnp->voffset % cu->header.addr_size) != 0)
11725 dwarf2_complex_location_expr_complaint ();
11726 else
11727 fnp->voffset /= cu->header.addr_size;
11728 fnp->voffset += 2;
11729 }
11730 else
11731 dwarf2_complex_location_expr_complaint ();
11732
11733 if (!fnp->fcontext)
11734 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11735 }
11736 else if (attr_form_is_section_offset (attr))
11737 {
11738 dwarf2_complex_location_expr_complaint ();
11739 }
11740 else
11741 {
11742 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11743 fieldname);
11744 }
11745 }
11746 else
11747 {
11748 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11749 if (attr && DW_UNSND (attr))
11750 {
11751 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11752 complaint (&symfile_complaints,
11753 _("Member function \"%s\" (offset %d) is virtual "
11754 "but the vtable offset is not specified"),
11755 fieldname, die->offset.sect_off);
11756 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11757 TYPE_CPLUS_DYNAMIC (type) = 1;
11758 }
11759 }
11760 }
11761
11762 /* Create the vector of member function fields, and attach it to the type. */
11763
11764 static void
11765 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
11766 struct dwarf2_cu *cu)
11767 {
11768 struct fnfieldlist *flp;
11769 int i;
11770
11771 if (cu->language == language_ada)
11772 error (_("unexpected member functions in Ada type"));
11773
11774 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11775 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11776 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11777
11778 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11779 {
11780 struct nextfnfield *nfp = flp->head;
11781 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11782 int k;
11783
11784 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11785 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11786 fn_flp->fn_fields = (struct fn_field *)
11787 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11788 for (k = flp->length; (k--, nfp); nfp = nfp->next)
11789 fn_flp->fn_fields[k] = nfp->fnfield;
11790 }
11791
11792 TYPE_NFN_FIELDS (type) = fip->nfnfields;
11793 }
11794
11795 /* Returns non-zero if NAME is the name of a vtable member in CU's
11796 language, zero otherwise. */
11797 static int
11798 is_vtable_name (const char *name, struct dwarf2_cu *cu)
11799 {
11800 static const char vptr[] = "_vptr";
11801 static const char vtable[] = "vtable";
11802
11803 /* Look for the C++ and Java forms of the vtable. */
11804 if ((cu->language == language_java
11805 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11806 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11807 && is_cplus_marker (name[sizeof (vptr) - 1])))
11808 return 1;
11809
11810 return 0;
11811 }
11812
11813 /* GCC outputs unnamed structures that are really pointers to member
11814 functions, with the ABI-specified layout. If TYPE describes
11815 such a structure, smash it into a member function type.
11816
11817 GCC shouldn't do this; it should just output pointer to member DIEs.
11818 This is GCC PR debug/28767. */
11819
11820 static void
11821 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
11822 {
11823 struct type *pfn_type, *domain_type, *new_type;
11824
11825 /* Check for a structure with no name and two children. */
11826 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11827 return;
11828
11829 /* Check for __pfn and __delta members. */
11830 if (TYPE_FIELD_NAME (type, 0) == NULL
11831 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11832 || TYPE_FIELD_NAME (type, 1) == NULL
11833 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11834 return;
11835
11836 /* Find the type of the method. */
11837 pfn_type = TYPE_FIELD_TYPE (type, 0);
11838 if (pfn_type == NULL
11839 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11840 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
11841 return;
11842
11843 /* Look for the "this" argument. */
11844 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11845 if (TYPE_NFIELDS (pfn_type) == 0
11846 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
11847 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
11848 return;
11849
11850 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
11851 new_type = alloc_type (objfile);
11852 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
11853 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11854 TYPE_VARARGS (pfn_type));
11855 smash_to_methodptr_type (type, new_type);
11856 }
11857
11858 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11859 (icc). */
11860
11861 static int
11862 producer_is_icc (struct dwarf2_cu *cu)
11863 {
11864 if (!cu->checked_producer)
11865 check_producer (cu);
11866
11867 return cu->producer_is_icc;
11868 }
11869
11870 /* Called when we find the DIE that starts a structure or union scope
11871 (definition) to create a type for the structure or union. Fill in
11872 the type's name and general properties; the members will not be
11873 processed until process_structure_scope.
11874
11875 NOTE: we need to call these functions regardless of whether or not the
11876 DIE has a DW_AT_name attribute, since it might be an anonymous
11877 structure or union. This gets the type entered into our set of
11878 user defined types.
11879
11880 However, if the structure is incomplete (an opaque struct/union)
11881 then suppress creating a symbol table entry for it since gdb only
11882 wants to find the one with the complete definition. Note that if
11883 it is complete, we just call new_symbol, which does it's own
11884 checking about whether the struct/union is anonymous or not (and
11885 suppresses creating a symbol table entry itself). */
11886
11887 static struct type *
11888 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
11889 {
11890 struct objfile *objfile = cu->objfile;
11891 struct type *type;
11892 struct attribute *attr;
11893 const char *name;
11894
11895 /* If the definition of this type lives in .debug_types, read that type.
11896 Don't follow DW_AT_specification though, that will take us back up
11897 the chain and we want to go down. */
11898 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11899 if (attr)
11900 {
11901 type = get_DW_AT_signature_type (die, attr, cu);
11902
11903 /* The type's CU may not be the same as CU.
11904 Ensure TYPE is recorded with CU in die_type_hash. */
11905 return set_die_type (die, type, cu);
11906 }
11907
11908 type = alloc_type (objfile);
11909 INIT_CPLUS_SPECIFIC (type);
11910
11911 name = dwarf2_name (die, cu);
11912 if (name != NULL)
11913 {
11914 if (cu->language == language_cplus
11915 || cu->language == language_java)
11916 {
11917 const char *full_name = dwarf2_full_name (name, die, cu);
11918
11919 /* dwarf2_full_name might have already finished building the DIE's
11920 type. If so, there is no need to continue. */
11921 if (get_die_type (die, cu) != NULL)
11922 return get_die_type (die, cu);
11923
11924 TYPE_TAG_NAME (type) = full_name;
11925 if (die->tag == DW_TAG_structure_type
11926 || die->tag == DW_TAG_class_type)
11927 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11928 }
11929 else
11930 {
11931 /* The name is already allocated along with this objfile, so
11932 we don't need to duplicate it for the type. */
11933 TYPE_TAG_NAME (type) = name;
11934 if (die->tag == DW_TAG_class_type)
11935 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11936 }
11937 }
11938
11939 if (die->tag == DW_TAG_structure_type)
11940 {
11941 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11942 }
11943 else if (die->tag == DW_TAG_union_type)
11944 {
11945 TYPE_CODE (type) = TYPE_CODE_UNION;
11946 }
11947 else
11948 {
11949 TYPE_CODE (type) = TYPE_CODE_CLASS;
11950 }
11951
11952 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11953 TYPE_DECLARED_CLASS (type) = 1;
11954
11955 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11956 if (attr)
11957 {
11958 TYPE_LENGTH (type) = DW_UNSND (attr);
11959 }
11960 else
11961 {
11962 TYPE_LENGTH (type) = 0;
11963 }
11964
11965 if (producer_is_icc (cu))
11966 {
11967 /* ICC does not output the required DW_AT_declaration
11968 on incomplete types, but gives them a size of zero. */
11969 }
11970 else
11971 TYPE_STUB_SUPPORTED (type) = 1;
11972
11973 if (die_is_declaration (die, cu))
11974 TYPE_STUB (type) = 1;
11975 else if (attr == NULL && die->child == NULL
11976 && producer_is_realview (cu->producer))
11977 /* RealView does not output the required DW_AT_declaration
11978 on incomplete types. */
11979 TYPE_STUB (type) = 1;
11980
11981 /* We need to add the type field to the die immediately so we don't
11982 infinitely recurse when dealing with pointers to the structure
11983 type within the structure itself. */
11984 set_die_type (die, type, cu);
11985
11986 /* set_die_type should be already done. */
11987 set_descriptive_type (type, die, cu);
11988
11989 return type;
11990 }
11991
11992 /* Finish creating a structure or union type, including filling in
11993 its members and creating a symbol for it. */
11994
11995 static void
11996 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11997 {
11998 struct objfile *objfile = cu->objfile;
11999 struct die_info *child_die = die->child;
12000 struct type *type;
12001
12002 type = get_die_type (die, cu);
12003 if (type == NULL)
12004 type = read_structure_type (die, cu);
12005
12006 if (die->child != NULL && ! die_is_declaration (die, cu))
12007 {
12008 struct field_info fi;
12009 struct die_info *child_die;
12010 VEC (symbolp) *template_args = NULL;
12011 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
12012
12013 memset (&fi, 0, sizeof (struct field_info));
12014
12015 child_die = die->child;
12016
12017 while (child_die && child_die->tag)
12018 {
12019 if (child_die->tag == DW_TAG_member
12020 || child_die->tag == DW_TAG_variable)
12021 {
12022 /* NOTE: carlton/2002-11-05: A C++ static data member
12023 should be a DW_TAG_member that is a declaration, but
12024 all versions of G++ as of this writing (so through at
12025 least 3.2.1) incorrectly generate DW_TAG_variable
12026 tags for them instead. */
12027 dwarf2_add_field (&fi, child_die, cu);
12028 }
12029 else if (child_die->tag == DW_TAG_subprogram)
12030 {
12031 /* C++ member function. */
12032 dwarf2_add_member_fn (&fi, child_die, type, cu);
12033 }
12034 else if (child_die->tag == DW_TAG_inheritance)
12035 {
12036 /* C++ base class field. */
12037 dwarf2_add_field (&fi, child_die, cu);
12038 }
12039 else if (child_die->tag == DW_TAG_typedef)
12040 dwarf2_add_typedef (&fi, child_die, cu);
12041 else if (child_die->tag == DW_TAG_template_type_param
12042 || child_die->tag == DW_TAG_template_value_param)
12043 {
12044 struct symbol *arg = new_symbol (child_die, NULL, cu);
12045
12046 if (arg != NULL)
12047 VEC_safe_push (symbolp, template_args, arg);
12048 }
12049
12050 child_die = sibling_die (child_die);
12051 }
12052
12053 /* Attach template arguments to type. */
12054 if (! VEC_empty (symbolp, template_args))
12055 {
12056 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12057 TYPE_N_TEMPLATE_ARGUMENTS (type)
12058 = VEC_length (symbolp, template_args);
12059 TYPE_TEMPLATE_ARGUMENTS (type)
12060 = obstack_alloc (&objfile->objfile_obstack,
12061 (TYPE_N_TEMPLATE_ARGUMENTS (type)
12062 * sizeof (struct symbol *)));
12063 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
12064 VEC_address (symbolp, template_args),
12065 (TYPE_N_TEMPLATE_ARGUMENTS (type)
12066 * sizeof (struct symbol *)));
12067 VEC_free (symbolp, template_args);
12068 }
12069
12070 /* Attach fields and member functions to the type. */
12071 if (fi.nfields)
12072 dwarf2_attach_fields_to_type (&fi, type, cu);
12073 if (fi.nfnfields)
12074 {
12075 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
12076
12077 /* Get the type which refers to the base class (possibly this
12078 class itself) which contains the vtable pointer for the current
12079 class from the DW_AT_containing_type attribute. This use of
12080 DW_AT_containing_type is a GNU extension. */
12081
12082 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
12083 {
12084 struct type *t = die_containing_type (die, cu);
12085
12086 TYPE_VPTR_BASETYPE (type) = t;
12087 if (type == t)
12088 {
12089 int i;
12090
12091 /* Our own class provides vtbl ptr. */
12092 for (i = TYPE_NFIELDS (t) - 1;
12093 i >= TYPE_N_BASECLASSES (t);
12094 --i)
12095 {
12096 const char *fieldname = TYPE_FIELD_NAME (t, i);
12097
12098 if (is_vtable_name (fieldname, cu))
12099 {
12100 TYPE_VPTR_FIELDNO (type) = i;
12101 break;
12102 }
12103 }
12104
12105 /* Complain if virtual function table field not found. */
12106 if (i < TYPE_N_BASECLASSES (t))
12107 complaint (&symfile_complaints,
12108 _("virtual function table pointer "
12109 "not found when defining class '%s'"),
12110 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
12111 "");
12112 }
12113 else
12114 {
12115 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
12116 }
12117 }
12118 else if (cu->producer
12119 && strncmp (cu->producer,
12120 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
12121 {
12122 /* The IBM XLC compiler does not provide direct indication
12123 of the containing type, but the vtable pointer is
12124 always named __vfp. */
12125
12126 int i;
12127
12128 for (i = TYPE_NFIELDS (type) - 1;
12129 i >= TYPE_N_BASECLASSES (type);
12130 --i)
12131 {
12132 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
12133 {
12134 TYPE_VPTR_FIELDNO (type) = i;
12135 TYPE_VPTR_BASETYPE (type) = type;
12136 break;
12137 }
12138 }
12139 }
12140 }
12141
12142 /* Copy fi.typedef_field_list linked list elements content into the
12143 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
12144 if (fi.typedef_field_list)
12145 {
12146 int i = fi.typedef_field_list_count;
12147
12148 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12149 TYPE_TYPEDEF_FIELD_ARRAY (type)
12150 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
12151 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
12152
12153 /* Reverse the list order to keep the debug info elements order. */
12154 while (--i >= 0)
12155 {
12156 struct typedef_field *dest, *src;
12157
12158 dest = &TYPE_TYPEDEF_FIELD (type, i);
12159 src = &fi.typedef_field_list->field;
12160 fi.typedef_field_list = fi.typedef_field_list->next;
12161 *dest = *src;
12162 }
12163 }
12164
12165 do_cleanups (back_to);
12166
12167 if (HAVE_CPLUS_STRUCT (type))
12168 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
12169 }
12170
12171 quirk_gcc_member_function_pointer (type, objfile);
12172
12173 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
12174 snapshots) has been known to create a die giving a declaration
12175 for a class that has, as a child, a die giving a definition for a
12176 nested class. So we have to process our children even if the
12177 current die is a declaration. Normally, of course, a declaration
12178 won't have any children at all. */
12179
12180 while (child_die != NULL && child_die->tag)
12181 {
12182 if (child_die->tag == DW_TAG_member
12183 || child_die->tag == DW_TAG_variable
12184 || child_die->tag == DW_TAG_inheritance
12185 || child_die->tag == DW_TAG_template_value_param
12186 || child_die->tag == DW_TAG_template_type_param)
12187 {
12188 /* Do nothing. */
12189 }
12190 else
12191 process_die (child_die, cu);
12192
12193 child_die = sibling_die (child_die);
12194 }
12195
12196 /* Do not consider external references. According to the DWARF standard,
12197 these DIEs are identified by the fact that they have no byte_size
12198 attribute, and a declaration attribute. */
12199 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
12200 || !die_is_declaration (die, cu))
12201 new_symbol (die, type, cu);
12202 }
12203
12204 /* Given a DW_AT_enumeration_type die, set its type. We do not
12205 complete the type's fields yet, or create any symbols. */
12206
12207 static struct type *
12208 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
12209 {
12210 struct objfile *objfile = cu->objfile;
12211 struct type *type;
12212 struct attribute *attr;
12213 const char *name;
12214
12215 /* If the definition of this type lives in .debug_types, read that type.
12216 Don't follow DW_AT_specification though, that will take us back up
12217 the chain and we want to go down. */
12218 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
12219 if (attr)
12220 {
12221 type = get_DW_AT_signature_type (die, attr, cu);
12222
12223 /* The type's CU may not be the same as CU.
12224 Ensure TYPE is recorded with CU in die_type_hash. */
12225 return set_die_type (die, type, cu);
12226 }
12227
12228 type = alloc_type (objfile);
12229
12230 TYPE_CODE (type) = TYPE_CODE_ENUM;
12231 name = dwarf2_full_name (NULL, die, cu);
12232 if (name != NULL)
12233 TYPE_TAG_NAME (type) = name;
12234
12235 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12236 if (attr)
12237 {
12238 TYPE_LENGTH (type) = DW_UNSND (attr);
12239 }
12240 else
12241 {
12242 TYPE_LENGTH (type) = 0;
12243 }
12244
12245 /* The enumeration DIE can be incomplete. In Ada, any type can be
12246 declared as private in the package spec, and then defined only
12247 inside the package body. Such types are known as Taft Amendment
12248 Types. When another package uses such a type, an incomplete DIE
12249 may be generated by the compiler. */
12250 if (die_is_declaration (die, cu))
12251 TYPE_STUB (type) = 1;
12252
12253 return set_die_type (die, type, cu);
12254 }
12255
12256 /* Given a pointer to a die which begins an enumeration, process all
12257 the dies that define the members of the enumeration, and create the
12258 symbol for the enumeration type.
12259
12260 NOTE: We reverse the order of the element list. */
12261
12262 static void
12263 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
12264 {
12265 struct type *this_type;
12266
12267 this_type = get_die_type (die, cu);
12268 if (this_type == NULL)
12269 this_type = read_enumeration_type (die, cu);
12270
12271 if (die->child != NULL)
12272 {
12273 struct die_info *child_die;
12274 struct symbol *sym;
12275 struct field *fields = NULL;
12276 int num_fields = 0;
12277 int unsigned_enum = 1;
12278 const char *name;
12279 int flag_enum = 1;
12280 ULONGEST mask = 0;
12281
12282 child_die = die->child;
12283 while (child_die && child_die->tag)
12284 {
12285 if (child_die->tag != DW_TAG_enumerator)
12286 {
12287 process_die (child_die, cu);
12288 }
12289 else
12290 {
12291 name = dwarf2_name (child_die, cu);
12292 if (name)
12293 {
12294 sym = new_symbol (child_die, this_type, cu);
12295 if (SYMBOL_VALUE (sym) < 0)
12296 {
12297 unsigned_enum = 0;
12298 flag_enum = 0;
12299 }
12300 else if ((mask & SYMBOL_VALUE (sym)) != 0)
12301 flag_enum = 0;
12302 else
12303 mask |= SYMBOL_VALUE (sym);
12304
12305 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
12306 {
12307 fields = (struct field *)
12308 xrealloc (fields,
12309 (num_fields + DW_FIELD_ALLOC_CHUNK)
12310 * sizeof (struct field));
12311 }
12312
12313 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
12314 FIELD_TYPE (fields[num_fields]) = NULL;
12315 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
12316 FIELD_BITSIZE (fields[num_fields]) = 0;
12317
12318 num_fields++;
12319 }
12320 }
12321
12322 child_die = sibling_die (child_die);
12323 }
12324
12325 if (num_fields)
12326 {
12327 TYPE_NFIELDS (this_type) = num_fields;
12328 TYPE_FIELDS (this_type) = (struct field *)
12329 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
12330 memcpy (TYPE_FIELDS (this_type), fields,
12331 sizeof (struct field) * num_fields);
12332 xfree (fields);
12333 }
12334 if (unsigned_enum)
12335 TYPE_UNSIGNED (this_type) = 1;
12336 if (flag_enum)
12337 TYPE_FLAG_ENUM (this_type) = 1;
12338 }
12339
12340 /* If we are reading an enum from a .debug_types unit, and the enum
12341 is a declaration, and the enum is not the signatured type in the
12342 unit, then we do not want to add a symbol for it. Adding a
12343 symbol would in some cases obscure the true definition of the
12344 enum, giving users an incomplete type when the definition is
12345 actually available. Note that we do not want to do this for all
12346 enums which are just declarations, because C++0x allows forward
12347 enum declarations. */
12348 if (cu->per_cu->is_debug_types
12349 && die_is_declaration (die, cu))
12350 {
12351 struct signatured_type *sig_type;
12352
12353 sig_type = (struct signatured_type *) cu->per_cu;
12354 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
12355 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
12356 return;
12357 }
12358
12359 new_symbol (die, this_type, cu);
12360 }
12361
12362 /* Extract all information from a DW_TAG_array_type DIE and put it in
12363 the DIE's type field. For now, this only handles one dimensional
12364 arrays. */
12365
12366 static struct type *
12367 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
12368 {
12369 struct objfile *objfile = cu->objfile;
12370 struct die_info *child_die;
12371 struct type *type;
12372 struct type *element_type, *range_type, *index_type;
12373 struct type **range_types = NULL;
12374 struct attribute *attr;
12375 int ndim = 0;
12376 struct cleanup *back_to;
12377 const char *name;
12378
12379 element_type = die_type (die, cu);
12380
12381 /* The die_type call above may have already set the type for this DIE. */
12382 type = get_die_type (die, cu);
12383 if (type)
12384 return type;
12385
12386 /* Irix 6.2 native cc creates array types without children for
12387 arrays with unspecified length. */
12388 if (die->child == NULL)
12389 {
12390 index_type = objfile_type (objfile)->builtin_int;
12391 range_type = create_range_type (NULL, index_type, 0, -1);
12392 type = create_array_type (NULL, element_type, range_type);
12393 return set_die_type (die, type, cu);
12394 }
12395
12396 back_to = make_cleanup (null_cleanup, NULL);
12397 child_die = die->child;
12398 while (child_die && child_die->tag)
12399 {
12400 if (child_die->tag == DW_TAG_subrange_type)
12401 {
12402 struct type *child_type = read_type_die (child_die, cu);
12403
12404 if (child_type != NULL)
12405 {
12406 /* The range type was succesfully read. Save it for the
12407 array type creation. */
12408 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
12409 {
12410 range_types = (struct type **)
12411 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
12412 * sizeof (struct type *));
12413 if (ndim == 0)
12414 make_cleanup (free_current_contents, &range_types);
12415 }
12416 range_types[ndim++] = child_type;
12417 }
12418 }
12419 child_die = sibling_die (child_die);
12420 }
12421
12422 /* Dwarf2 dimensions are output from left to right, create the
12423 necessary array types in backwards order. */
12424
12425 type = element_type;
12426
12427 if (read_array_order (die, cu) == DW_ORD_col_major)
12428 {
12429 int i = 0;
12430
12431 while (i < ndim)
12432 type = create_array_type (NULL, type, range_types[i++]);
12433 }
12434 else
12435 {
12436 while (ndim-- > 0)
12437 type = create_array_type (NULL, type, range_types[ndim]);
12438 }
12439
12440 /* Understand Dwarf2 support for vector types (like they occur on
12441 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
12442 array type. This is not part of the Dwarf2/3 standard yet, but a
12443 custom vendor extension. The main difference between a regular
12444 array and the vector variant is that vectors are passed by value
12445 to functions. */
12446 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
12447 if (attr)
12448 make_vector_type (type);
12449
12450 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
12451 implementation may choose to implement triple vectors using this
12452 attribute. */
12453 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12454 if (attr)
12455 {
12456 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
12457 TYPE_LENGTH (type) = DW_UNSND (attr);
12458 else
12459 complaint (&symfile_complaints,
12460 _("DW_AT_byte_size for array type smaller "
12461 "than the total size of elements"));
12462 }
12463
12464 name = dwarf2_name (die, cu);
12465 if (name)
12466 TYPE_NAME (type) = name;
12467
12468 /* Install the type in the die. */
12469 set_die_type (die, type, cu);
12470
12471 /* set_die_type should be already done. */
12472 set_descriptive_type (type, die, cu);
12473
12474 do_cleanups (back_to);
12475
12476 return type;
12477 }
12478
12479 static enum dwarf_array_dim_ordering
12480 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
12481 {
12482 struct attribute *attr;
12483
12484 attr = dwarf2_attr (die, DW_AT_ordering, cu);
12485
12486 if (attr) return DW_SND (attr);
12487
12488 /* GNU F77 is a special case, as at 08/2004 array type info is the
12489 opposite order to the dwarf2 specification, but data is still
12490 laid out as per normal fortran.
12491
12492 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
12493 version checking. */
12494
12495 if (cu->language == language_fortran
12496 && cu->producer && strstr (cu->producer, "GNU F77"))
12497 {
12498 return DW_ORD_row_major;
12499 }
12500
12501 switch (cu->language_defn->la_array_ordering)
12502 {
12503 case array_column_major:
12504 return DW_ORD_col_major;
12505 case array_row_major:
12506 default:
12507 return DW_ORD_row_major;
12508 };
12509 }
12510
12511 /* Extract all information from a DW_TAG_set_type DIE and put it in
12512 the DIE's type field. */
12513
12514 static struct type *
12515 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
12516 {
12517 struct type *domain_type, *set_type;
12518 struct attribute *attr;
12519
12520 domain_type = die_type (die, cu);
12521
12522 /* The die_type call above may have already set the type for this DIE. */
12523 set_type = get_die_type (die, cu);
12524 if (set_type)
12525 return set_type;
12526
12527 set_type = create_set_type (NULL, domain_type);
12528
12529 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12530 if (attr)
12531 TYPE_LENGTH (set_type) = DW_UNSND (attr);
12532
12533 return set_die_type (die, set_type, cu);
12534 }
12535
12536 /* A helper for read_common_block that creates a locexpr baton.
12537 SYM is the symbol which we are marking as computed.
12538 COMMON_DIE is the DIE for the common block.
12539 COMMON_LOC is the location expression attribute for the common
12540 block itself.
12541 MEMBER_LOC is the location expression attribute for the particular
12542 member of the common block that we are processing.
12543 CU is the CU from which the above come. */
12544
12545 static void
12546 mark_common_block_symbol_computed (struct symbol *sym,
12547 struct die_info *common_die,
12548 struct attribute *common_loc,
12549 struct attribute *member_loc,
12550 struct dwarf2_cu *cu)
12551 {
12552 struct objfile *objfile = dwarf2_per_objfile->objfile;
12553 struct dwarf2_locexpr_baton *baton;
12554 gdb_byte *ptr;
12555 unsigned int cu_off;
12556 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
12557 LONGEST offset = 0;
12558
12559 gdb_assert (common_loc && member_loc);
12560 gdb_assert (attr_form_is_block (common_loc));
12561 gdb_assert (attr_form_is_block (member_loc)
12562 || attr_form_is_constant (member_loc));
12563
12564 baton = obstack_alloc (&objfile->objfile_obstack,
12565 sizeof (struct dwarf2_locexpr_baton));
12566 baton->per_cu = cu->per_cu;
12567 gdb_assert (baton->per_cu);
12568
12569 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
12570
12571 if (attr_form_is_constant (member_loc))
12572 {
12573 offset = dwarf2_get_attr_constant_value (member_loc, 0);
12574 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
12575 }
12576 else
12577 baton->size += DW_BLOCK (member_loc)->size;
12578
12579 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
12580 baton->data = ptr;
12581
12582 *ptr++ = DW_OP_call4;
12583 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
12584 store_unsigned_integer (ptr, 4, byte_order, cu_off);
12585 ptr += 4;
12586
12587 if (attr_form_is_constant (member_loc))
12588 {
12589 *ptr++ = DW_OP_addr;
12590 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
12591 ptr += cu->header.addr_size;
12592 }
12593 else
12594 {
12595 /* We have to copy the data here, because DW_OP_call4 will only
12596 use a DW_AT_location attribute. */
12597 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
12598 ptr += DW_BLOCK (member_loc)->size;
12599 }
12600
12601 *ptr++ = DW_OP_plus;
12602 gdb_assert (ptr - baton->data == baton->size);
12603
12604 SYMBOL_LOCATION_BATON (sym) = baton;
12605 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
12606 }
12607
12608 /* Create appropriate locally-scoped variables for all the
12609 DW_TAG_common_block entries. Also create a struct common_block
12610 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
12611 is used to sepate the common blocks name namespace from regular
12612 variable names. */
12613
12614 static void
12615 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
12616 {
12617 struct attribute *attr;
12618
12619 attr = dwarf2_attr (die, DW_AT_location, cu);
12620 if (attr)
12621 {
12622 /* Support the .debug_loc offsets. */
12623 if (attr_form_is_block (attr))
12624 {
12625 /* Ok. */
12626 }
12627 else if (attr_form_is_section_offset (attr))
12628 {
12629 dwarf2_complex_location_expr_complaint ();
12630 attr = NULL;
12631 }
12632 else
12633 {
12634 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12635 "common block member");
12636 attr = NULL;
12637 }
12638 }
12639
12640 if (die->child != NULL)
12641 {
12642 struct objfile *objfile = cu->objfile;
12643 struct die_info *child_die;
12644 size_t n_entries = 0, size;
12645 struct common_block *common_block;
12646 struct symbol *sym;
12647
12648 for (child_die = die->child;
12649 child_die && child_die->tag;
12650 child_die = sibling_die (child_die))
12651 ++n_entries;
12652
12653 size = (sizeof (struct common_block)
12654 + (n_entries - 1) * sizeof (struct symbol *));
12655 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12656 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12657 common_block->n_entries = 0;
12658
12659 for (child_die = die->child;
12660 child_die && child_die->tag;
12661 child_die = sibling_die (child_die))
12662 {
12663 /* Create the symbol in the DW_TAG_common_block block in the current
12664 symbol scope. */
12665 sym = new_symbol (child_die, NULL, cu);
12666 if (sym != NULL)
12667 {
12668 struct attribute *member_loc;
12669
12670 common_block->contents[common_block->n_entries++] = sym;
12671
12672 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12673 cu);
12674 if (member_loc)
12675 {
12676 /* GDB has handled this for a long time, but it is
12677 not specified by DWARF. It seems to have been
12678 emitted by gfortran at least as recently as:
12679 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12680 complaint (&symfile_complaints,
12681 _("Variable in common block has "
12682 "DW_AT_data_member_location "
12683 "- DIE at 0x%x [in module %s]"),
12684 child_die->offset.sect_off,
12685 objfile_name (cu->objfile));
12686
12687 if (attr_form_is_section_offset (member_loc))
12688 dwarf2_complex_location_expr_complaint ();
12689 else if (attr_form_is_constant (member_loc)
12690 || attr_form_is_block (member_loc))
12691 {
12692 if (attr)
12693 mark_common_block_symbol_computed (sym, die, attr,
12694 member_loc, cu);
12695 }
12696 else
12697 dwarf2_complex_location_expr_complaint ();
12698 }
12699 }
12700 }
12701
12702 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12703 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
12704 }
12705 }
12706
12707 /* Create a type for a C++ namespace. */
12708
12709 static struct type *
12710 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
12711 {
12712 struct objfile *objfile = cu->objfile;
12713 const char *previous_prefix, *name;
12714 int is_anonymous;
12715 struct type *type;
12716
12717 /* For extensions, reuse the type of the original namespace. */
12718 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12719 {
12720 struct die_info *ext_die;
12721 struct dwarf2_cu *ext_cu = cu;
12722
12723 ext_die = dwarf2_extension (die, &ext_cu);
12724 type = read_type_die (ext_die, ext_cu);
12725
12726 /* EXT_CU may not be the same as CU.
12727 Ensure TYPE is recorded with CU in die_type_hash. */
12728 return set_die_type (die, type, cu);
12729 }
12730
12731 name = namespace_name (die, &is_anonymous, cu);
12732
12733 /* Now build the name of the current namespace. */
12734
12735 previous_prefix = determine_prefix (die, cu);
12736 if (previous_prefix[0] != '\0')
12737 name = typename_concat (&objfile->objfile_obstack,
12738 previous_prefix, name, 0, cu);
12739
12740 /* Create the type. */
12741 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12742 objfile);
12743 TYPE_NAME (type) = name;
12744 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12745
12746 return set_die_type (die, type, cu);
12747 }
12748
12749 /* Read a C++ namespace. */
12750
12751 static void
12752 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12753 {
12754 struct objfile *objfile = cu->objfile;
12755 int is_anonymous;
12756
12757 /* Add a symbol associated to this if we haven't seen the namespace
12758 before. Also, add a using directive if it's an anonymous
12759 namespace. */
12760
12761 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
12762 {
12763 struct type *type;
12764
12765 type = read_type_die (die, cu);
12766 new_symbol (die, type, cu);
12767
12768 namespace_name (die, &is_anonymous, cu);
12769 if (is_anonymous)
12770 {
12771 const char *previous_prefix = determine_prefix (die, cu);
12772
12773 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12774 NULL, NULL, 0, &objfile->objfile_obstack);
12775 }
12776 }
12777
12778 if (die->child != NULL)
12779 {
12780 struct die_info *child_die = die->child;
12781
12782 while (child_die && child_die->tag)
12783 {
12784 process_die (child_die, cu);
12785 child_die = sibling_die (child_die);
12786 }
12787 }
12788 }
12789
12790 /* Read a Fortran module as type. This DIE can be only a declaration used for
12791 imported module. Still we need that type as local Fortran "use ... only"
12792 declaration imports depend on the created type in determine_prefix. */
12793
12794 static struct type *
12795 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12796 {
12797 struct objfile *objfile = cu->objfile;
12798 const char *module_name;
12799 struct type *type;
12800
12801 module_name = dwarf2_name (die, cu);
12802 if (!module_name)
12803 complaint (&symfile_complaints,
12804 _("DW_TAG_module has no name, offset 0x%x"),
12805 die->offset.sect_off);
12806 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12807
12808 /* determine_prefix uses TYPE_TAG_NAME. */
12809 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12810
12811 return set_die_type (die, type, cu);
12812 }
12813
12814 /* Read a Fortran module. */
12815
12816 static void
12817 read_module (struct die_info *die, struct dwarf2_cu *cu)
12818 {
12819 struct die_info *child_die = die->child;
12820
12821 while (child_die && child_die->tag)
12822 {
12823 process_die (child_die, cu);
12824 child_die = sibling_die (child_die);
12825 }
12826 }
12827
12828 /* Return the name of the namespace represented by DIE. Set
12829 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12830 namespace. */
12831
12832 static const char *
12833 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
12834 {
12835 struct die_info *current_die;
12836 const char *name = NULL;
12837
12838 /* Loop through the extensions until we find a name. */
12839
12840 for (current_die = die;
12841 current_die != NULL;
12842 current_die = dwarf2_extension (die, &cu))
12843 {
12844 name = dwarf2_name (current_die, cu);
12845 if (name != NULL)
12846 break;
12847 }
12848
12849 /* Is it an anonymous namespace? */
12850
12851 *is_anonymous = (name == NULL);
12852 if (*is_anonymous)
12853 name = CP_ANONYMOUS_NAMESPACE_STR;
12854
12855 return name;
12856 }
12857
12858 /* Extract all information from a DW_TAG_pointer_type DIE and add to
12859 the user defined type vector. */
12860
12861 static struct type *
12862 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
12863 {
12864 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
12865 struct comp_unit_head *cu_header = &cu->header;
12866 struct type *type;
12867 struct attribute *attr_byte_size;
12868 struct attribute *attr_address_class;
12869 int byte_size, addr_class;
12870 struct type *target_type;
12871
12872 target_type = die_type (die, cu);
12873
12874 /* The die_type call above may have already set the type for this DIE. */
12875 type = get_die_type (die, cu);
12876 if (type)
12877 return type;
12878
12879 type = lookup_pointer_type (target_type);
12880
12881 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
12882 if (attr_byte_size)
12883 byte_size = DW_UNSND (attr_byte_size);
12884 else
12885 byte_size = cu_header->addr_size;
12886
12887 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
12888 if (attr_address_class)
12889 addr_class = DW_UNSND (attr_address_class);
12890 else
12891 addr_class = DW_ADDR_none;
12892
12893 /* If the pointer size or address class is different than the
12894 default, create a type variant marked as such and set the
12895 length accordingly. */
12896 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
12897 {
12898 if (gdbarch_address_class_type_flags_p (gdbarch))
12899 {
12900 int type_flags;
12901
12902 type_flags = gdbarch_address_class_type_flags
12903 (gdbarch, byte_size, addr_class);
12904 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12905 == 0);
12906 type = make_type_with_address_space (type, type_flags);
12907 }
12908 else if (TYPE_LENGTH (type) != byte_size)
12909 {
12910 complaint (&symfile_complaints,
12911 _("invalid pointer size %d"), byte_size);
12912 }
12913 else
12914 {
12915 /* Should we also complain about unhandled address classes? */
12916 }
12917 }
12918
12919 TYPE_LENGTH (type) = byte_size;
12920 return set_die_type (die, type, cu);
12921 }
12922
12923 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12924 the user defined type vector. */
12925
12926 static struct type *
12927 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
12928 {
12929 struct type *type;
12930 struct type *to_type;
12931 struct type *domain;
12932
12933 to_type = die_type (die, cu);
12934 domain = die_containing_type (die, cu);
12935
12936 /* The calls above may have already set the type for this DIE. */
12937 type = get_die_type (die, cu);
12938 if (type)
12939 return type;
12940
12941 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12942 type = lookup_methodptr_type (to_type);
12943 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12944 {
12945 struct type *new_type = alloc_type (cu->objfile);
12946
12947 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12948 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12949 TYPE_VARARGS (to_type));
12950 type = lookup_methodptr_type (new_type);
12951 }
12952 else
12953 type = lookup_memberptr_type (to_type, domain);
12954
12955 return set_die_type (die, type, cu);
12956 }
12957
12958 /* Extract all information from a DW_TAG_reference_type DIE and add to
12959 the user defined type vector. */
12960
12961 static struct type *
12962 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
12963 {
12964 struct comp_unit_head *cu_header = &cu->header;
12965 struct type *type, *target_type;
12966 struct attribute *attr;
12967
12968 target_type = die_type (die, cu);
12969
12970 /* The die_type call above may have already set the type for this DIE. */
12971 type = get_die_type (die, cu);
12972 if (type)
12973 return type;
12974
12975 type = lookup_reference_type (target_type);
12976 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12977 if (attr)
12978 {
12979 TYPE_LENGTH (type) = DW_UNSND (attr);
12980 }
12981 else
12982 {
12983 TYPE_LENGTH (type) = cu_header->addr_size;
12984 }
12985 return set_die_type (die, type, cu);
12986 }
12987
12988 static struct type *
12989 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
12990 {
12991 struct type *base_type, *cv_type;
12992
12993 base_type = die_type (die, cu);
12994
12995 /* The die_type call above may have already set the type for this DIE. */
12996 cv_type = get_die_type (die, cu);
12997 if (cv_type)
12998 return cv_type;
12999
13000 /* In case the const qualifier is applied to an array type, the element type
13001 is so qualified, not the array type (section 6.7.3 of C99). */
13002 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
13003 {
13004 struct type *el_type, *inner_array;
13005
13006 base_type = copy_type (base_type);
13007 inner_array = base_type;
13008
13009 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
13010 {
13011 TYPE_TARGET_TYPE (inner_array) =
13012 copy_type (TYPE_TARGET_TYPE (inner_array));
13013 inner_array = TYPE_TARGET_TYPE (inner_array);
13014 }
13015
13016 el_type = TYPE_TARGET_TYPE (inner_array);
13017 TYPE_TARGET_TYPE (inner_array) =
13018 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
13019
13020 return set_die_type (die, base_type, cu);
13021 }
13022
13023 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
13024 return set_die_type (die, cv_type, cu);
13025 }
13026
13027 static struct type *
13028 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
13029 {
13030 struct type *base_type, *cv_type;
13031
13032 base_type = die_type (die, cu);
13033
13034 /* The die_type call above may have already set the type for this DIE. */
13035 cv_type = get_die_type (die, cu);
13036 if (cv_type)
13037 return cv_type;
13038
13039 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
13040 return set_die_type (die, cv_type, cu);
13041 }
13042
13043 /* Handle DW_TAG_restrict_type. */
13044
13045 static struct type *
13046 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
13047 {
13048 struct type *base_type, *cv_type;
13049
13050 base_type = die_type (die, cu);
13051
13052 /* The die_type call above may have already set the type for this DIE. */
13053 cv_type = get_die_type (die, cu);
13054 if (cv_type)
13055 return cv_type;
13056
13057 cv_type = make_restrict_type (base_type);
13058 return set_die_type (die, cv_type, cu);
13059 }
13060
13061 /* Extract all information from a DW_TAG_string_type DIE and add to
13062 the user defined type vector. It isn't really a user defined type,
13063 but it behaves like one, with other DIE's using an AT_user_def_type
13064 attribute to reference it. */
13065
13066 static struct type *
13067 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
13068 {
13069 struct objfile *objfile = cu->objfile;
13070 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13071 struct type *type, *range_type, *index_type, *char_type;
13072 struct attribute *attr;
13073 unsigned int length;
13074
13075 attr = dwarf2_attr (die, DW_AT_string_length, cu);
13076 if (attr)
13077 {
13078 length = DW_UNSND (attr);
13079 }
13080 else
13081 {
13082 /* Check for the DW_AT_byte_size attribute. */
13083 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13084 if (attr)
13085 {
13086 length = DW_UNSND (attr);
13087 }
13088 else
13089 {
13090 length = 1;
13091 }
13092 }
13093
13094 index_type = objfile_type (objfile)->builtin_int;
13095 range_type = create_range_type (NULL, index_type, 1, length);
13096 char_type = language_string_char_type (cu->language_defn, gdbarch);
13097 type = create_string_type (NULL, char_type, range_type);
13098
13099 return set_die_type (die, type, cu);
13100 }
13101
13102 /* Assuming that DIE corresponds to a function, returns nonzero
13103 if the function is prototyped. */
13104
13105 static int
13106 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
13107 {
13108 struct attribute *attr;
13109
13110 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
13111 if (attr && (DW_UNSND (attr) != 0))
13112 return 1;
13113
13114 /* The DWARF standard implies that the DW_AT_prototyped attribute
13115 is only meaninful for C, but the concept also extends to other
13116 languages that allow unprototyped functions (Eg: Objective C).
13117 For all other languages, assume that functions are always
13118 prototyped. */
13119 if (cu->language != language_c
13120 && cu->language != language_objc
13121 && cu->language != language_opencl)
13122 return 1;
13123
13124 /* RealView does not emit DW_AT_prototyped. We can not distinguish
13125 prototyped and unprototyped functions; default to prototyped,
13126 since that is more common in modern code (and RealView warns
13127 about unprototyped functions). */
13128 if (producer_is_realview (cu->producer))
13129 return 1;
13130
13131 return 0;
13132 }
13133
13134 /* Handle DIES due to C code like:
13135
13136 struct foo
13137 {
13138 int (*funcp)(int a, long l);
13139 int b;
13140 };
13141
13142 ('funcp' generates a DW_TAG_subroutine_type DIE). */
13143
13144 static struct type *
13145 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
13146 {
13147 struct objfile *objfile = cu->objfile;
13148 struct type *type; /* Type that this function returns. */
13149 struct type *ftype; /* Function that returns above type. */
13150 struct attribute *attr;
13151
13152 type = die_type (die, cu);
13153
13154 /* The die_type call above may have already set the type for this DIE. */
13155 ftype = get_die_type (die, cu);
13156 if (ftype)
13157 return ftype;
13158
13159 ftype = lookup_function_type (type);
13160
13161 if (prototyped_function_p (die, cu))
13162 TYPE_PROTOTYPED (ftype) = 1;
13163
13164 /* Store the calling convention in the type if it's available in
13165 the subroutine die. Otherwise set the calling convention to
13166 the default value DW_CC_normal. */
13167 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
13168 if (attr)
13169 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
13170 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
13171 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
13172 else
13173 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
13174
13175 /* We need to add the subroutine type to the die immediately so
13176 we don't infinitely recurse when dealing with parameters
13177 declared as the same subroutine type. */
13178 set_die_type (die, ftype, cu);
13179
13180 if (die->child != NULL)
13181 {
13182 struct type *void_type = objfile_type (objfile)->builtin_void;
13183 struct die_info *child_die;
13184 int nparams, iparams;
13185
13186 /* Count the number of parameters.
13187 FIXME: GDB currently ignores vararg functions, but knows about
13188 vararg member functions. */
13189 nparams = 0;
13190 child_die = die->child;
13191 while (child_die && child_die->tag)
13192 {
13193 if (child_die->tag == DW_TAG_formal_parameter)
13194 nparams++;
13195 else if (child_die->tag == DW_TAG_unspecified_parameters)
13196 TYPE_VARARGS (ftype) = 1;
13197 child_die = sibling_die (child_die);
13198 }
13199
13200 /* Allocate storage for parameters and fill them in. */
13201 TYPE_NFIELDS (ftype) = nparams;
13202 TYPE_FIELDS (ftype) = (struct field *)
13203 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
13204
13205 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
13206 even if we error out during the parameters reading below. */
13207 for (iparams = 0; iparams < nparams; iparams++)
13208 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
13209
13210 iparams = 0;
13211 child_die = die->child;
13212 while (child_die && child_die->tag)
13213 {
13214 if (child_die->tag == DW_TAG_formal_parameter)
13215 {
13216 struct type *arg_type;
13217
13218 /* DWARF version 2 has no clean way to discern C++
13219 static and non-static member functions. G++ helps
13220 GDB by marking the first parameter for non-static
13221 member functions (which is the this pointer) as
13222 artificial. We pass this information to
13223 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
13224
13225 DWARF version 3 added DW_AT_object_pointer, which GCC
13226 4.5 does not yet generate. */
13227 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
13228 if (attr)
13229 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
13230 else
13231 {
13232 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
13233
13234 /* GCC/43521: In java, the formal parameter
13235 "this" is sometimes not marked with DW_AT_artificial. */
13236 if (cu->language == language_java)
13237 {
13238 const char *name = dwarf2_name (child_die, cu);
13239
13240 if (name && !strcmp (name, "this"))
13241 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
13242 }
13243 }
13244 arg_type = die_type (child_die, cu);
13245
13246 /* RealView does not mark THIS as const, which the testsuite
13247 expects. GCC marks THIS as const in method definitions,
13248 but not in the class specifications (GCC PR 43053). */
13249 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
13250 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
13251 {
13252 int is_this = 0;
13253 struct dwarf2_cu *arg_cu = cu;
13254 const char *name = dwarf2_name (child_die, cu);
13255
13256 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
13257 if (attr)
13258 {
13259 /* If the compiler emits this, use it. */
13260 if (follow_die_ref (die, attr, &arg_cu) == child_die)
13261 is_this = 1;
13262 }
13263 else if (name && strcmp (name, "this") == 0)
13264 /* Function definitions will have the argument names. */
13265 is_this = 1;
13266 else if (name == NULL && iparams == 0)
13267 /* Declarations may not have the names, so like
13268 elsewhere in GDB, assume an artificial first
13269 argument is "this". */
13270 is_this = 1;
13271
13272 if (is_this)
13273 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
13274 arg_type, 0);
13275 }
13276
13277 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
13278 iparams++;
13279 }
13280 child_die = sibling_die (child_die);
13281 }
13282 }
13283
13284 return ftype;
13285 }
13286
13287 static struct type *
13288 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
13289 {
13290 struct objfile *objfile = cu->objfile;
13291 const char *name = NULL;
13292 struct type *this_type, *target_type;
13293
13294 name = dwarf2_full_name (NULL, die, cu);
13295 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
13296 TYPE_FLAG_TARGET_STUB, NULL, objfile);
13297 TYPE_NAME (this_type) = name;
13298 set_die_type (die, this_type, cu);
13299 target_type = die_type (die, cu);
13300 if (target_type != this_type)
13301 TYPE_TARGET_TYPE (this_type) = target_type;
13302 else
13303 {
13304 /* Self-referential typedefs are, it seems, not allowed by the DWARF
13305 spec and cause infinite loops in GDB. */
13306 complaint (&symfile_complaints,
13307 _("Self-referential DW_TAG_typedef "
13308 "- DIE at 0x%x [in module %s]"),
13309 die->offset.sect_off, objfile_name (objfile));
13310 TYPE_TARGET_TYPE (this_type) = NULL;
13311 }
13312 return this_type;
13313 }
13314
13315 /* Find a representation of a given base type and install
13316 it in the TYPE field of the die. */
13317
13318 static struct type *
13319 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
13320 {
13321 struct objfile *objfile = cu->objfile;
13322 struct type *type;
13323 struct attribute *attr;
13324 int encoding = 0, size = 0;
13325 const char *name;
13326 enum type_code code = TYPE_CODE_INT;
13327 int type_flags = 0;
13328 struct type *target_type = NULL;
13329
13330 attr = dwarf2_attr (die, DW_AT_encoding, cu);
13331 if (attr)
13332 {
13333 encoding = DW_UNSND (attr);
13334 }
13335 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13336 if (attr)
13337 {
13338 size = DW_UNSND (attr);
13339 }
13340 name = dwarf2_name (die, cu);
13341 if (!name)
13342 {
13343 complaint (&symfile_complaints,
13344 _("DW_AT_name missing from DW_TAG_base_type"));
13345 }
13346
13347 switch (encoding)
13348 {
13349 case DW_ATE_address:
13350 /* Turn DW_ATE_address into a void * pointer. */
13351 code = TYPE_CODE_PTR;
13352 type_flags |= TYPE_FLAG_UNSIGNED;
13353 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
13354 break;
13355 case DW_ATE_boolean:
13356 code = TYPE_CODE_BOOL;
13357 type_flags |= TYPE_FLAG_UNSIGNED;
13358 break;
13359 case DW_ATE_complex_float:
13360 code = TYPE_CODE_COMPLEX;
13361 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
13362 break;
13363 case DW_ATE_decimal_float:
13364 code = TYPE_CODE_DECFLOAT;
13365 break;
13366 case DW_ATE_float:
13367 code = TYPE_CODE_FLT;
13368 break;
13369 case DW_ATE_signed:
13370 break;
13371 case DW_ATE_unsigned:
13372 type_flags |= TYPE_FLAG_UNSIGNED;
13373 if (cu->language == language_fortran
13374 && name
13375 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
13376 code = TYPE_CODE_CHAR;
13377 break;
13378 case DW_ATE_signed_char:
13379 if (cu->language == language_ada || cu->language == language_m2
13380 || cu->language == language_pascal
13381 || cu->language == language_fortran)
13382 code = TYPE_CODE_CHAR;
13383 break;
13384 case DW_ATE_unsigned_char:
13385 if (cu->language == language_ada || cu->language == language_m2
13386 || cu->language == language_pascal
13387 || cu->language == language_fortran)
13388 code = TYPE_CODE_CHAR;
13389 type_flags |= TYPE_FLAG_UNSIGNED;
13390 break;
13391 case DW_ATE_UTF:
13392 /* We just treat this as an integer and then recognize the
13393 type by name elsewhere. */
13394 break;
13395
13396 default:
13397 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
13398 dwarf_type_encoding_name (encoding));
13399 break;
13400 }
13401
13402 type = init_type (code, size, type_flags, NULL, objfile);
13403 TYPE_NAME (type) = name;
13404 TYPE_TARGET_TYPE (type) = target_type;
13405
13406 if (name && strcmp (name, "char") == 0)
13407 TYPE_NOSIGN (type) = 1;
13408
13409 return set_die_type (die, type, cu);
13410 }
13411
13412 /* Read the given DW_AT_subrange DIE. */
13413
13414 static struct type *
13415 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
13416 {
13417 struct type *base_type, *orig_base_type;
13418 struct type *range_type;
13419 struct attribute *attr;
13420 LONGEST low, high;
13421 int low_default_is_valid;
13422 const char *name;
13423 LONGEST negative_mask;
13424
13425 orig_base_type = die_type (die, cu);
13426 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
13427 whereas the real type might be. So, we use ORIG_BASE_TYPE when
13428 creating the range type, but we use the result of check_typedef
13429 when examining properties of the type. */
13430 base_type = check_typedef (orig_base_type);
13431
13432 /* The die_type call above may have already set the type for this DIE. */
13433 range_type = get_die_type (die, cu);
13434 if (range_type)
13435 return range_type;
13436
13437 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
13438 omitting DW_AT_lower_bound. */
13439 switch (cu->language)
13440 {
13441 case language_c:
13442 case language_cplus:
13443 low = 0;
13444 low_default_is_valid = 1;
13445 break;
13446 case language_fortran:
13447 low = 1;
13448 low_default_is_valid = 1;
13449 break;
13450 case language_d:
13451 case language_java:
13452 case language_objc:
13453 low = 0;
13454 low_default_is_valid = (cu->header.version >= 4);
13455 break;
13456 case language_ada:
13457 case language_m2:
13458 case language_pascal:
13459 low = 1;
13460 low_default_is_valid = (cu->header.version >= 4);
13461 break;
13462 default:
13463 low = 0;
13464 low_default_is_valid = 0;
13465 break;
13466 }
13467
13468 /* FIXME: For variable sized arrays either of these could be
13469 a variable rather than a constant value. We'll allow it,
13470 but we don't know how to handle it. */
13471 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
13472 if (attr)
13473 low = dwarf2_get_attr_constant_value (attr, low);
13474 else if (!low_default_is_valid)
13475 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
13476 "- DIE at 0x%x [in module %s]"),
13477 die->offset.sect_off, objfile_name (cu->objfile));
13478
13479 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
13480 if (attr)
13481 {
13482 if (attr_form_is_block (attr) || attr_form_is_ref (attr))
13483 {
13484 /* GCC encodes arrays with unspecified or dynamic length
13485 with a DW_FORM_block1 attribute or a reference attribute.
13486 FIXME: GDB does not yet know how to handle dynamic
13487 arrays properly, treat them as arrays with unspecified
13488 length for now.
13489
13490 FIXME: jimb/2003-09-22: GDB does not really know
13491 how to handle arrays of unspecified length
13492 either; we just represent them as zero-length
13493 arrays. Choose an appropriate upper bound given
13494 the lower bound we've computed above. */
13495 high = low - 1;
13496 }
13497 else
13498 high = dwarf2_get_attr_constant_value (attr, 1);
13499 }
13500 else
13501 {
13502 attr = dwarf2_attr (die, DW_AT_count, cu);
13503 if (attr)
13504 {
13505 int count = dwarf2_get_attr_constant_value (attr, 1);
13506 high = low + count - 1;
13507 }
13508 else
13509 {
13510 /* Unspecified array length. */
13511 high = low - 1;
13512 }
13513 }
13514
13515 /* Dwarf-2 specifications explicitly allows to create subrange types
13516 without specifying a base type.
13517 In that case, the base type must be set to the type of
13518 the lower bound, upper bound or count, in that order, if any of these
13519 three attributes references an object that has a type.
13520 If no base type is found, the Dwarf-2 specifications say that
13521 a signed integer type of size equal to the size of an address should
13522 be used.
13523 For the following C code: `extern char gdb_int [];'
13524 GCC produces an empty range DIE.
13525 FIXME: muller/2010-05-28: Possible references to object for low bound,
13526 high bound or count are not yet handled by this code. */
13527 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
13528 {
13529 struct objfile *objfile = cu->objfile;
13530 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13531 int addr_size = gdbarch_addr_bit (gdbarch) /8;
13532 struct type *int_type = objfile_type (objfile)->builtin_int;
13533
13534 /* Test "int", "long int", and "long long int" objfile types,
13535 and select the first one having a size above or equal to the
13536 architecture address size. */
13537 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13538 base_type = int_type;
13539 else
13540 {
13541 int_type = objfile_type (objfile)->builtin_long;
13542 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13543 base_type = int_type;
13544 else
13545 {
13546 int_type = objfile_type (objfile)->builtin_long_long;
13547 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13548 base_type = int_type;
13549 }
13550 }
13551 }
13552
13553 negative_mask =
13554 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
13555 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
13556 low |= negative_mask;
13557 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
13558 high |= negative_mask;
13559
13560 range_type = create_range_type (NULL, orig_base_type, low, high);
13561
13562 /* Mark arrays with dynamic length at least as an array of unspecified
13563 length. GDB could check the boundary but before it gets implemented at
13564 least allow accessing the array elements. */
13565 if (attr && attr_form_is_block (attr))
13566 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13567
13568 /* Ada expects an empty array on no boundary attributes. */
13569 if (attr == NULL && cu->language != language_ada)
13570 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13571
13572 name = dwarf2_name (die, cu);
13573 if (name)
13574 TYPE_NAME (range_type) = name;
13575
13576 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13577 if (attr)
13578 TYPE_LENGTH (range_type) = DW_UNSND (attr);
13579
13580 set_die_type (die, range_type, cu);
13581
13582 /* set_die_type should be already done. */
13583 set_descriptive_type (range_type, die, cu);
13584
13585 return range_type;
13586 }
13587
13588 static struct type *
13589 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
13590 {
13591 struct type *type;
13592
13593 /* For now, we only support the C meaning of an unspecified type: void. */
13594
13595 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
13596 TYPE_NAME (type) = dwarf2_name (die, cu);
13597
13598 return set_die_type (die, type, cu);
13599 }
13600
13601 /* Read a single die and all its descendents. Set the die's sibling
13602 field to NULL; set other fields in the die correctly, and set all
13603 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
13604 location of the info_ptr after reading all of those dies. PARENT
13605 is the parent of the die in question. */
13606
13607 static struct die_info *
13608 read_die_and_children (const struct die_reader_specs *reader,
13609 const gdb_byte *info_ptr,
13610 const gdb_byte **new_info_ptr,
13611 struct die_info *parent)
13612 {
13613 struct die_info *die;
13614 const gdb_byte *cur_ptr;
13615 int has_children;
13616
13617 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
13618 if (die == NULL)
13619 {
13620 *new_info_ptr = cur_ptr;
13621 return NULL;
13622 }
13623 store_in_ref_table (die, reader->cu);
13624
13625 if (has_children)
13626 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
13627 else
13628 {
13629 die->child = NULL;
13630 *new_info_ptr = cur_ptr;
13631 }
13632
13633 die->sibling = NULL;
13634 die->parent = parent;
13635 return die;
13636 }
13637
13638 /* Read a die, all of its descendents, and all of its siblings; set
13639 all of the fields of all of the dies correctly. Arguments are as
13640 in read_die_and_children. */
13641
13642 static struct die_info *
13643 read_die_and_siblings_1 (const struct die_reader_specs *reader,
13644 const gdb_byte *info_ptr,
13645 const gdb_byte **new_info_ptr,
13646 struct die_info *parent)
13647 {
13648 struct die_info *first_die, *last_sibling;
13649 const gdb_byte *cur_ptr;
13650
13651 cur_ptr = info_ptr;
13652 first_die = last_sibling = NULL;
13653
13654 while (1)
13655 {
13656 struct die_info *die
13657 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
13658
13659 if (die == NULL)
13660 {
13661 *new_info_ptr = cur_ptr;
13662 return first_die;
13663 }
13664
13665 if (!first_die)
13666 first_die = die;
13667 else
13668 last_sibling->sibling = die;
13669
13670 last_sibling = die;
13671 }
13672 }
13673
13674 /* Read a die, all of its descendents, and all of its siblings; set
13675 all of the fields of all of the dies correctly. Arguments are as
13676 in read_die_and_children.
13677 This the main entry point for reading a DIE and all its children. */
13678
13679 static struct die_info *
13680 read_die_and_siblings (const struct die_reader_specs *reader,
13681 const gdb_byte *info_ptr,
13682 const gdb_byte **new_info_ptr,
13683 struct die_info *parent)
13684 {
13685 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
13686 new_info_ptr, parent);
13687
13688 if (dwarf2_die_debug)
13689 {
13690 fprintf_unfiltered (gdb_stdlog,
13691 "Read die from %s@0x%x of %s:\n",
13692 bfd_section_name (reader->abfd,
13693 reader->die_section->asection),
13694 (unsigned) (info_ptr - reader->die_section->buffer),
13695 bfd_get_filename (reader->abfd));
13696 dump_die (die, dwarf2_die_debug);
13697 }
13698
13699 return die;
13700 }
13701
13702 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13703 attributes.
13704 The caller is responsible for filling in the extra attributes
13705 and updating (*DIEP)->num_attrs.
13706 Set DIEP to point to a newly allocated die with its information,
13707 except for its child, sibling, and parent fields.
13708 Set HAS_CHILDREN to tell whether the die has children or not. */
13709
13710 static const gdb_byte *
13711 read_full_die_1 (const struct die_reader_specs *reader,
13712 struct die_info **diep, const gdb_byte *info_ptr,
13713 int *has_children, int num_extra_attrs)
13714 {
13715 unsigned int abbrev_number, bytes_read, i;
13716 sect_offset offset;
13717 struct abbrev_info *abbrev;
13718 struct die_info *die;
13719 struct dwarf2_cu *cu = reader->cu;
13720 bfd *abfd = reader->abfd;
13721
13722 offset.sect_off = info_ptr - reader->buffer;
13723 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13724 info_ptr += bytes_read;
13725 if (!abbrev_number)
13726 {
13727 *diep = NULL;
13728 *has_children = 0;
13729 return info_ptr;
13730 }
13731
13732 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
13733 if (!abbrev)
13734 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13735 abbrev_number,
13736 bfd_get_filename (abfd));
13737
13738 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
13739 die->offset = offset;
13740 die->tag = abbrev->tag;
13741 die->abbrev = abbrev_number;
13742
13743 /* Make the result usable.
13744 The caller needs to update num_attrs after adding the extra
13745 attributes. */
13746 die->num_attrs = abbrev->num_attrs;
13747
13748 for (i = 0; i < abbrev->num_attrs; ++i)
13749 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13750 info_ptr);
13751
13752 *diep = die;
13753 *has_children = abbrev->has_children;
13754 return info_ptr;
13755 }
13756
13757 /* Read a die and all its attributes.
13758 Set DIEP to point to a newly allocated die with its information,
13759 except for its child, sibling, and parent fields.
13760 Set HAS_CHILDREN to tell whether the die has children or not. */
13761
13762 static const gdb_byte *
13763 read_full_die (const struct die_reader_specs *reader,
13764 struct die_info **diep, const gdb_byte *info_ptr,
13765 int *has_children)
13766 {
13767 const gdb_byte *result;
13768
13769 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13770
13771 if (dwarf2_die_debug)
13772 {
13773 fprintf_unfiltered (gdb_stdlog,
13774 "Read die from %s@0x%x of %s:\n",
13775 bfd_section_name (reader->abfd,
13776 reader->die_section->asection),
13777 (unsigned) (info_ptr - reader->die_section->buffer),
13778 bfd_get_filename (reader->abfd));
13779 dump_die (*diep, dwarf2_die_debug);
13780 }
13781
13782 return result;
13783 }
13784 \f
13785 /* Abbreviation tables.
13786
13787 In DWARF version 2, the description of the debugging information is
13788 stored in a separate .debug_abbrev section. Before we read any
13789 dies from a section we read in all abbreviations and install them
13790 in a hash table. */
13791
13792 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13793
13794 static struct abbrev_info *
13795 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13796 {
13797 struct abbrev_info *abbrev;
13798
13799 abbrev = (struct abbrev_info *)
13800 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13801 memset (abbrev, 0, sizeof (struct abbrev_info));
13802 return abbrev;
13803 }
13804
13805 /* Add an abbreviation to the table. */
13806
13807 static void
13808 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13809 unsigned int abbrev_number,
13810 struct abbrev_info *abbrev)
13811 {
13812 unsigned int hash_number;
13813
13814 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13815 abbrev->next = abbrev_table->abbrevs[hash_number];
13816 abbrev_table->abbrevs[hash_number] = abbrev;
13817 }
13818
13819 /* Look up an abbrev in the table.
13820 Returns NULL if the abbrev is not found. */
13821
13822 static struct abbrev_info *
13823 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13824 unsigned int abbrev_number)
13825 {
13826 unsigned int hash_number;
13827 struct abbrev_info *abbrev;
13828
13829 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13830 abbrev = abbrev_table->abbrevs[hash_number];
13831
13832 while (abbrev)
13833 {
13834 if (abbrev->number == abbrev_number)
13835 return abbrev;
13836 abbrev = abbrev->next;
13837 }
13838 return NULL;
13839 }
13840
13841 /* Read in an abbrev table. */
13842
13843 static struct abbrev_table *
13844 abbrev_table_read_table (struct dwarf2_section_info *section,
13845 sect_offset offset)
13846 {
13847 struct objfile *objfile = dwarf2_per_objfile->objfile;
13848 bfd *abfd = section->asection->owner;
13849 struct abbrev_table *abbrev_table;
13850 const gdb_byte *abbrev_ptr;
13851 struct abbrev_info *cur_abbrev;
13852 unsigned int abbrev_number, bytes_read, abbrev_name;
13853 unsigned int abbrev_form;
13854 struct attr_abbrev *cur_attrs;
13855 unsigned int allocated_attrs;
13856
13857 abbrev_table = XMALLOC (struct abbrev_table);
13858 abbrev_table->offset = offset;
13859 obstack_init (&abbrev_table->abbrev_obstack);
13860 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13861 (ABBREV_HASH_SIZE
13862 * sizeof (struct abbrev_info *)));
13863 memset (abbrev_table->abbrevs, 0,
13864 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
13865
13866 dwarf2_read_section (objfile, section);
13867 abbrev_ptr = section->buffer + offset.sect_off;
13868 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13869 abbrev_ptr += bytes_read;
13870
13871 allocated_attrs = ATTR_ALLOC_CHUNK;
13872 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
13873
13874 /* Loop until we reach an abbrev number of 0. */
13875 while (abbrev_number)
13876 {
13877 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
13878
13879 /* read in abbrev header */
13880 cur_abbrev->number = abbrev_number;
13881 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13882 abbrev_ptr += bytes_read;
13883 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13884 abbrev_ptr += 1;
13885
13886 /* now read in declarations */
13887 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13888 abbrev_ptr += bytes_read;
13889 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13890 abbrev_ptr += bytes_read;
13891 while (abbrev_name)
13892 {
13893 if (cur_abbrev->num_attrs == allocated_attrs)
13894 {
13895 allocated_attrs += ATTR_ALLOC_CHUNK;
13896 cur_attrs
13897 = xrealloc (cur_attrs, (allocated_attrs
13898 * sizeof (struct attr_abbrev)));
13899 }
13900
13901 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13902 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
13903 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13904 abbrev_ptr += bytes_read;
13905 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13906 abbrev_ptr += bytes_read;
13907 }
13908
13909 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
13910 (cur_abbrev->num_attrs
13911 * sizeof (struct attr_abbrev)));
13912 memcpy (cur_abbrev->attrs, cur_attrs,
13913 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13914
13915 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
13916
13917 /* Get next abbreviation.
13918 Under Irix6 the abbreviations for a compilation unit are not
13919 always properly terminated with an abbrev number of 0.
13920 Exit loop if we encounter an abbreviation which we have
13921 already read (which means we are about to read the abbreviations
13922 for the next compile unit) or if the end of the abbreviation
13923 table is reached. */
13924 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
13925 break;
13926 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13927 abbrev_ptr += bytes_read;
13928 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
13929 break;
13930 }
13931
13932 xfree (cur_attrs);
13933 return abbrev_table;
13934 }
13935
13936 /* Free the resources held by ABBREV_TABLE. */
13937
13938 static void
13939 abbrev_table_free (struct abbrev_table *abbrev_table)
13940 {
13941 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13942 xfree (abbrev_table);
13943 }
13944
13945 /* Same as abbrev_table_free but as a cleanup.
13946 We pass in a pointer to the pointer to the table so that we can
13947 set the pointer to NULL when we're done. It also simplifies
13948 build_type_unit_groups. */
13949
13950 static void
13951 abbrev_table_free_cleanup (void *table_ptr)
13952 {
13953 struct abbrev_table **abbrev_table_ptr = table_ptr;
13954
13955 if (*abbrev_table_ptr != NULL)
13956 abbrev_table_free (*abbrev_table_ptr);
13957 *abbrev_table_ptr = NULL;
13958 }
13959
13960 /* Read the abbrev table for CU from ABBREV_SECTION. */
13961
13962 static void
13963 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13964 struct dwarf2_section_info *abbrev_section)
13965 {
13966 cu->abbrev_table =
13967 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13968 }
13969
13970 /* Release the memory used by the abbrev table for a compilation unit. */
13971
13972 static void
13973 dwarf2_free_abbrev_table (void *ptr_to_cu)
13974 {
13975 struct dwarf2_cu *cu = ptr_to_cu;
13976
13977 if (cu->abbrev_table != NULL)
13978 abbrev_table_free (cu->abbrev_table);
13979 /* Set this to NULL so that we SEGV if we try to read it later,
13980 and also because free_comp_unit verifies this is NULL. */
13981 cu->abbrev_table = NULL;
13982 }
13983 \f
13984 /* Returns nonzero if TAG represents a type that we might generate a partial
13985 symbol for. */
13986
13987 static int
13988 is_type_tag_for_partial (int tag)
13989 {
13990 switch (tag)
13991 {
13992 #if 0
13993 /* Some types that would be reasonable to generate partial symbols for,
13994 that we don't at present. */
13995 case DW_TAG_array_type:
13996 case DW_TAG_file_type:
13997 case DW_TAG_ptr_to_member_type:
13998 case DW_TAG_set_type:
13999 case DW_TAG_string_type:
14000 case DW_TAG_subroutine_type:
14001 #endif
14002 case DW_TAG_base_type:
14003 case DW_TAG_class_type:
14004 case DW_TAG_interface_type:
14005 case DW_TAG_enumeration_type:
14006 case DW_TAG_structure_type:
14007 case DW_TAG_subrange_type:
14008 case DW_TAG_typedef:
14009 case DW_TAG_union_type:
14010 return 1;
14011 default:
14012 return 0;
14013 }
14014 }
14015
14016 /* Load all DIEs that are interesting for partial symbols into memory. */
14017
14018 static struct partial_die_info *
14019 load_partial_dies (const struct die_reader_specs *reader,
14020 const gdb_byte *info_ptr, int building_psymtab)
14021 {
14022 struct dwarf2_cu *cu = reader->cu;
14023 struct objfile *objfile = cu->objfile;
14024 struct partial_die_info *part_die;
14025 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
14026 struct abbrev_info *abbrev;
14027 unsigned int bytes_read;
14028 unsigned int load_all = 0;
14029 int nesting_level = 1;
14030
14031 parent_die = NULL;
14032 last_die = NULL;
14033
14034 gdb_assert (cu->per_cu != NULL);
14035 if (cu->per_cu->load_all_dies)
14036 load_all = 1;
14037
14038 cu->partial_dies
14039 = htab_create_alloc_ex (cu->header.length / 12,
14040 partial_die_hash,
14041 partial_die_eq,
14042 NULL,
14043 &cu->comp_unit_obstack,
14044 hashtab_obstack_allocate,
14045 dummy_obstack_deallocate);
14046
14047 part_die = obstack_alloc (&cu->comp_unit_obstack,
14048 sizeof (struct partial_die_info));
14049
14050 while (1)
14051 {
14052 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
14053
14054 /* A NULL abbrev means the end of a series of children. */
14055 if (abbrev == NULL)
14056 {
14057 if (--nesting_level == 0)
14058 {
14059 /* PART_DIE was probably the last thing allocated on the
14060 comp_unit_obstack, so we could call obstack_free
14061 here. We don't do that because the waste is small,
14062 and will be cleaned up when we're done with this
14063 compilation unit. This way, we're also more robust
14064 against other users of the comp_unit_obstack. */
14065 return first_die;
14066 }
14067 info_ptr += bytes_read;
14068 last_die = parent_die;
14069 parent_die = parent_die->die_parent;
14070 continue;
14071 }
14072
14073 /* Check for template arguments. We never save these; if
14074 they're seen, we just mark the parent, and go on our way. */
14075 if (parent_die != NULL
14076 && cu->language == language_cplus
14077 && (abbrev->tag == DW_TAG_template_type_param
14078 || abbrev->tag == DW_TAG_template_value_param))
14079 {
14080 parent_die->has_template_arguments = 1;
14081
14082 if (!load_all)
14083 {
14084 /* We don't need a partial DIE for the template argument. */
14085 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
14086 continue;
14087 }
14088 }
14089
14090 /* We only recurse into c++ subprograms looking for template arguments.
14091 Skip their other children. */
14092 if (!load_all
14093 && cu->language == language_cplus
14094 && parent_die != NULL
14095 && parent_die->tag == DW_TAG_subprogram)
14096 {
14097 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
14098 continue;
14099 }
14100
14101 /* Check whether this DIE is interesting enough to save. Normally
14102 we would not be interested in members here, but there may be
14103 later variables referencing them via DW_AT_specification (for
14104 static members). */
14105 if (!load_all
14106 && !is_type_tag_for_partial (abbrev->tag)
14107 && abbrev->tag != DW_TAG_constant
14108 && abbrev->tag != DW_TAG_enumerator
14109 && abbrev->tag != DW_TAG_subprogram
14110 && abbrev->tag != DW_TAG_lexical_block
14111 && abbrev->tag != DW_TAG_variable
14112 && abbrev->tag != DW_TAG_namespace
14113 && abbrev->tag != DW_TAG_module
14114 && abbrev->tag != DW_TAG_member
14115 && abbrev->tag != DW_TAG_imported_unit)
14116 {
14117 /* Otherwise we skip to the next sibling, if any. */
14118 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
14119 continue;
14120 }
14121
14122 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
14123 info_ptr);
14124
14125 /* This two-pass algorithm for processing partial symbols has a
14126 high cost in cache pressure. Thus, handle some simple cases
14127 here which cover the majority of C partial symbols. DIEs
14128 which neither have specification tags in them, nor could have
14129 specification tags elsewhere pointing at them, can simply be
14130 processed and discarded.
14131
14132 This segment is also optional; scan_partial_symbols and
14133 add_partial_symbol will handle these DIEs if we chain
14134 them in normally. When compilers which do not emit large
14135 quantities of duplicate debug information are more common,
14136 this code can probably be removed. */
14137
14138 /* Any complete simple types at the top level (pretty much all
14139 of them, for a language without namespaces), can be processed
14140 directly. */
14141 if (parent_die == NULL
14142 && part_die->has_specification == 0
14143 && part_die->is_declaration == 0
14144 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
14145 || part_die->tag == DW_TAG_base_type
14146 || part_die->tag == DW_TAG_subrange_type))
14147 {
14148 if (building_psymtab && part_die->name != NULL)
14149 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
14150 VAR_DOMAIN, LOC_TYPEDEF,
14151 &objfile->static_psymbols,
14152 0, (CORE_ADDR) 0, cu->language, objfile);
14153 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
14154 continue;
14155 }
14156
14157 /* The exception for DW_TAG_typedef with has_children above is
14158 a workaround of GCC PR debug/47510. In the case of this complaint
14159 type_name_no_tag_or_error will error on such types later.
14160
14161 GDB skipped children of DW_TAG_typedef by the shortcut above and then
14162 it could not find the child DIEs referenced later, this is checked
14163 above. In correct DWARF DW_TAG_typedef should have no children. */
14164
14165 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
14166 complaint (&symfile_complaints,
14167 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
14168 "- DIE at 0x%x [in module %s]"),
14169 part_die->offset.sect_off, objfile_name (objfile));
14170
14171 /* If we're at the second level, and we're an enumerator, and
14172 our parent has no specification (meaning possibly lives in a
14173 namespace elsewhere), then we can add the partial symbol now
14174 instead of queueing it. */
14175 if (part_die->tag == DW_TAG_enumerator
14176 && parent_die != NULL
14177 && parent_die->die_parent == NULL
14178 && parent_die->tag == DW_TAG_enumeration_type
14179 && parent_die->has_specification == 0)
14180 {
14181 if (part_die->name == NULL)
14182 complaint (&symfile_complaints,
14183 _("malformed enumerator DIE ignored"));
14184 else if (building_psymtab)
14185 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
14186 VAR_DOMAIN, LOC_CONST,
14187 (cu->language == language_cplus
14188 || cu->language == language_java)
14189 ? &objfile->global_psymbols
14190 : &objfile->static_psymbols,
14191 0, (CORE_ADDR) 0, cu->language, objfile);
14192
14193 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
14194 continue;
14195 }
14196
14197 /* We'll save this DIE so link it in. */
14198 part_die->die_parent = parent_die;
14199 part_die->die_sibling = NULL;
14200 part_die->die_child = NULL;
14201
14202 if (last_die && last_die == parent_die)
14203 last_die->die_child = part_die;
14204 else if (last_die)
14205 last_die->die_sibling = part_die;
14206
14207 last_die = part_die;
14208
14209 if (first_die == NULL)
14210 first_die = part_die;
14211
14212 /* Maybe add the DIE to the hash table. Not all DIEs that we
14213 find interesting need to be in the hash table, because we
14214 also have the parent/sibling/child chains; only those that we
14215 might refer to by offset later during partial symbol reading.
14216
14217 For now this means things that might have be the target of a
14218 DW_AT_specification, DW_AT_abstract_origin, or
14219 DW_AT_extension. DW_AT_extension will refer only to
14220 namespaces; DW_AT_abstract_origin refers to functions (and
14221 many things under the function DIE, but we do not recurse
14222 into function DIEs during partial symbol reading) and
14223 possibly variables as well; DW_AT_specification refers to
14224 declarations. Declarations ought to have the DW_AT_declaration
14225 flag. It happens that GCC forgets to put it in sometimes, but
14226 only for functions, not for types.
14227
14228 Adding more things than necessary to the hash table is harmless
14229 except for the performance cost. Adding too few will result in
14230 wasted time in find_partial_die, when we reread the compilation
14231 unit with load_all_dies set. */
14232
14233 if (load_all
14234 || abbrev->tag == DW_TAG_constant
14235 || abbrev->tag == DW_TAG_subprogram
14236 || abbrev->tag == DW_TAG_variable
14237 || abbrev->tag == DW_TAG_namespace
14238 || part_die->is_declaration)
14239 {
14240 void **slot;
14241
14242 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
14243 part_die->offset.sect_off, INSERT);
14244 *slot = part_die;
14245 }
14246
14247 part_die = obstack_alloc (&cu->comp_unit_obstack,
14248 sizeof (struct partial_die_info));
14249
14250 /* For some DIEs we want to follow their children (if any). For C
14251 we have no reason to follow the children of structures; for other
14252 languages we have to, so that we can get at method physnames
14253 to infer fully qualified class names, for DW_AT_specification,
14254 and for C++ template arguments. For C++, we also look one level
14255 inside functions to find template arguments (if the name of the
14256 function does not already contain the template arguments).
14257
14258 For Ada, we need to scan the children of subprograms and lexical
14259 blocks as well because Ada allows the definition of nested
14260 entities that could be interesting for the debugger, such as
14261 nested subprograms for instance. */
14262 if (last_die->has_children
14263 && (load_all
14264 || last_die->tag == DW_TAG_namespace
14265 || last_die->tag == DW_TAG_module
14266 || last_die->tag == DW_TAG_enumeration_type
14267 || (cu->language == language_cplus
14268 && last_die->tag == DW_TAG_subprogram
14269 && (last_die->name == NULL
14270 || strchr (last_die->name, '<') == NULL))
14271 || (cu->language != language_c
14272 && (last_die->tag == DW_TAG_class_type
14273 || last_die->tag == DW_TAG_interface_type
14274 || last_die->tag == DW_TAG_structure_type
14275 || last_die->tag == DW_TAG_union_type))
14276 || (cu->language == language_ada
14277 && (last_die->tag == DW_TAG_subprogram
14278 || last_die->tag == DW_TAG_lexical_block))))
14279 {
14280 nesting_level++;
14281 parent_die = last_die;
14282 continue;
14283 }
14284
14285 /* Otherwise we skip to the next sibling, if any. */
14286 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
14287
14288 /* Back to the top, do it again. */
14289 }
14290 }
14291
14292 /* Read a minimal amount of information into the minimal die structure. */
14293
14294 static const gdb_byte *
14295 read_partial_die (const struct die_reader_specs *reader,
14296 struct partial_die_info *part_die,
14297 struct abbrev_info *abbrev, unsigned int abbrev_len,
14298 const gdb_byte *info_ptr)
14299 {
14300 struct dwarf2_cu *cu = reader->cu;
14301 struct objfile *objfile = cu->objfile;
14302 const gdb_byte *buffer = reader->buffer;
14303 unsigned int i;
14304 struct attribute attr;
14305 int has_low_pc_attr = 0;
14306 int has_high_pc_attr = 0;
14307 int high_pc_relative = 0;
14308
14309 memset (part_die, 0, sizeof (struct partial_die_info));
14310
14311 part_die->offset.sect_off = info_ptr - buffer;
14312
14313 info_ptr += abbrev_len;
14314
14315 if (abbrev == NULL)
14316 return info_ptr;
14317
14318 part_die->tag = abbrev->tag;
14319 part_die->has_children = abbrev->has_children;
14320
14321 for (i = 0; i < abbrev->num_attrs; ++i)
14322 {
14323 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
14324
14325 /* Store the data if it is of an attribute we want to keep in a
14326 partial symbol table. */
14327 switch (attr.name)
14328 {
14329 case DW_AT_name:
14330 switch (part_die->tag)
14331 {
14332 case DW_TAG_compile_unit:
14333 case DW_TAG_partial_unit:
14334 case DW_TAG_type_unit:
14335 /* Compilation units have a DW_AT_name that is a filename, not
14336 a source language identifier. */
14337 case DW_TAG_enumeration_type:
14338 case DW_TAG_enumerator:
14339 /* These tags always have simple identifiers already; no need
14340 to canonicalize them. */
14341 part_die->name = DW_STRING (&attr);
14342 break;
14343 default:
14344 part_die->name
14345 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
14346 &objfile->objfile_obstack);
14347 break;
14348 }
14349 break;
14350 case DW_AT_linkage_name:
14351 case DW_AT_MIPS_linkage_name:
14352 /* Note that both forms of linkage name might appear. We
14353 assume they will be the same, and we only store the last
14354 one we see. */
14355 if (cu->language == language_ada)
14356 part_die->name = DW_STRING (&attr);
14357 part_die->linkage_name = DW_STRING (&attr);
14358 break;
14359 case DW_AT_low_pc:
14360 has_low_pc_attr = 1;
14361 part_die->lowpc = DW_ADDR (&attr);
14362 break;
14363 case DW_AT_high_pc:
14364 has_high_pc_attr = 1;
14365 if (attr.form == DW_FORM_addr
14366 || attr.form == DW_FORM_GNU_addr_index)
14367 part_die->highpc = DW_ADDR (&attr);
14368 else
14369 {
14370 high_pc_relative = 1;
14371 part_die->highpc = DW_UNSND (&attr);
14372 }
14373 break;
14374 case DW_AT_location:
14375 /* Support the .debug_loc offsets. */
14376 if (attr_form_is_block (&attr))
14377 {
14378 part_die->d.locdesc = DW_BLOCK (&attr);
14379 }
14380 else if (attr_form_is_section_offset (&attr))
14381 {
14382 dwarf2_complex_location_expr_complaint ();
14383 }
14384 else
14385 {
14386 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14387 "partial symbol information");
14388 }
14389 break;
14390 case DW_AT_external:
14391 part_die->is_external = DW_UNSND (&attr);
14392 break;
14393 case DW_AT_declaration:
14394 part_die->is_declaration = DW_UNSND (&attr);
14395 break;
14396 case DW_AT_type:
14397 part_die->has_type = 1;
14398 break;
14399 case DW_AT_abstract_origin:
14400 case DW_AT_specification:
14401 case DW_AT_extension:
14402 part_die->has_specification = 1;
14403 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
14404 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
14405 || cu->per_cu->is_dwz);
14406 break;
14407 case DW_AT_sibling:
14408 /* Ignore absolute siblings, they might point outside of
14409 the current compile unit. */
14410 if (attr.form == DW_FORM_ref_addr)
14411 complaint (&symfile_complaints,
14412 _("ignoring absolute DW_AT_sibling"));
14413 else
14414 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
14415 break;
14416 case DW_AT_byte_size:
14417 part_die->has_byte_size = 1;
14418 break;
14419 case DW_AT_calling_convention:
14420 /* DWARF doesn't provide a way to identify a program's source-level
14421 entry point. DW_AT_calling_convention attributes are only meant
14422 to describe functions' calling conventions.
14423
14424 However, because it's a necessary piece of information in
14425 Fortran, and because DW_CC_program is the only piece of debugging
14426 information whose definition refers to a 'main program' at all,
14427 several compilers have begun marking Fortran main programs with
14428 DW_CC_program --- even when those functions use the standard
14429 calling conventions.
14430
14431 So until DWARF specifies a way to provide this information and
14432 compilers pick up the new representation, we'll support this
14433 practice. */
14434 if (DW_UNSND (&attr) == DW_CC_program
14435 && cu->language == language_fortran)
14436 {
14437 set_main_name (part_die->name);
14438
14439 /* As this DIE has a static linkage the name would be difficult
14440 to look up later. */
14441 language_of_main = language_fortran;
14442 }
14443 break;
14444 case DW_AT_inline:
14445 if (DW_UNSND (&attr) == DW_INL_inlined
14446 || DW_UNSND (&attr) == DW_INL_declared_inlined)
14447 part_die->may_be_inlined = 1;
14448 break;
14449
14450 case DW_AT_import:
14451 if (part_die->tag == DW_TAG_imported_unit)
14452 {
14453 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
14454 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
14455 || cu->per_cu->is_dwz);
14456 }
14457 break;
14458
14459 default:
14460 break;
14461 }
14462 }
14463
14464 if (high_pc_relative)
14465 part_die->highpc += part_die->lowpc;
14466
14467 if (has_low_pc_attr && has_high_pc_attr)
14468 {
14469 /* When using the GNU linker, .gnu.linkonce. sections are used to
14470 eliminate duplicate copies of functions and vtables and such.
14471 The linker will arbitrarily choose one and discard the others.
14472 The AT_*_pc values for such functions refer to local labels in
14473 these sections. If the section from that file was discarded, the
14474 labels are not in the output, so the relocs get a value of 0.
14475 If this is a discarded function, mark the pc bounds as invalid,
14476 so that GDB will ignore it. */
14477 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
14478 {
14479 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14480
14481 complaint (&symfile_complaints,
14482 _("DW_AT_low_pc %s is zero "
14483 "for DIE at 0x%x [in module %s]"),
14484 paddress (gdbarch, part_die->lowpc),
14485 part_die->offset.sect_off, objfile_name (objfile));
14486 }
14487 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
14488 else if (part_die->lowpc >= part_die->highpc)
14489 {
14490 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14491
14492 complaint (&symfile_complaints,
14493 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
14494 "for DIE at 0x%x [in module %s]"),
14495 paddress (gdbarch, part_die->lowpc),
14496 paddress (gdbarch, part_die->highpc),
14497 part_die->offset.sect_off, objfile_name (objfile));
14498 }
14499 else
14500 part_die->has_pc_info = 1;
14501 }
14502
14503 return info_ptr;
14504 }
14505
14506 /* Find a cached partial DIE at OFFSET in CU. */
14507
14508 static struct partial_die_info *
14509 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
14510 {
14511 struct partial_die_info *lookup_die = NULL;
14512 struct partial_die_info part_die;
14513
14514 part_die.offset = offset;
14515 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
14516 offset.sect_off);
14517
14518 return lookup_die;
14519 }
14520
14521 /* Find a partial DIE at OFFSET, which may or may not be in CU,
14522 except in the case of .debug_types DIEs which do not reference
14523 outside their CU (they do however referencing other types via
14524 DW_FORM_ref_sig8). */
14525
14526 static struct partial_die_info *
14527 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
14528 {
14529 struct objfile *objfile = cu->objfile;
14530 struct dwarf2_per_cu_data *per_cu = NULL;
14531 struct partial_die_info *pd = NULL;
14532
14533 if (offset_in_dwz == cu->per_cu->is_dwz
14534 && offset_in_cu_p (&cu->header, offset))
14535 {
14536 pd = find_partial_die_in_comp_unit (offset, cu);
14537 if (pd != NULL)
14538 return pd;
14539 /* We missed recording what we needed.
14540 Load all dies and try again. */
14541 per_cu = cu->per_cu;
14542 }
14543 else
14544 {
14545 /* TUs don't reference other CUs/TUs (except via type signatures). */
14546 if (cu->per_cu->is_debug_types)
14547 {
14548 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
14549 " external reference to offset 0x%lx [in module %s].\n"),
14550 (long) cu->header.offset.sect_off, (long) offset.sect_off,
14551 bfd_get_filename (objfile->obfd));
14552 }
14553 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
14554 objfile);
14555
14556 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
14557 load_partial_comp_unit (per_cu);
14558
14559 per_cu->cu->last_used = 0;
14560 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14561 }
14562
14563 /* If we didn't find it, and not all dies have been loaded,
14564 load them all and try again. */
14565
14566 if (pd == NULL && per_cu->load_all_dies == 0)
14567 {
14568 per_cu->load_all_dies = 1;
14569
14570 /* This is nasty. When we reread the DIEs, somewhere up the call chain
14571 THIS_CU->cu may already be in use. So we can't just free it and
14572 replace its DIEs with the ones we read in. Instead, we leave those
14573 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
14574 and clobber THIS_CU->cu->partial_dies with the hash table for the new
14575 set. */
14576 load_partial_comp_unit (per_cu);
14577
14578 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14579 }
14580
14581 if (pd == NULL)
14582 internal_error (__FILE__, __LINE__,
14583 _("could not find partial DIE 0x%x "
14584 "in cache [from module %s]\n"),
14585 offset.sect_off, bfd_get_filename (objfile->obfd));
14586 return pd;
14587 }
14588
14589 /* See if we can figure out if the class lives in a namespace. We do
14590 this by looking for a member function; its demangled name will
14591 contain namespace info, if there is any. */
14592
14593 static void
14594 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
14595 struct dwarf2_cu *cu)
14596 {
14597 /* NOTE: carlton/2003-10-07: Getting the info this way changes
14598 what template types look like, because the demangler
14599 frequently doesn't give the same name as the debug info. We
14600 could fix this by only using the demangled name to get the
14601 prefix (but see comment in read_structure_type). */
14602
14603 struct partial_die_info *real_pdi;
14604 struct partial_die_info *child_pdi;
14605
14606 /* If this DIE (this DIE's specification, if any) has a parent, then
14607 we should not do this. We'll prepend the parent's fully qualified
14608 name when we create the partial symbol. */
14609
14610 real_pdi = struct_pdi;
14611 while (real_pdi->has_specification)
14612 real_pdi = find_partial_die (real_pdi->spec_offset,
14613 real_pdi->spec_is_dwz, cu);
14614
14615 if (real_pdi->die_parent != NULL)
14616 return;
14617
14618 for (child_pdi = struct_pdi->die_child;
14619 child_pdi != NULL;
14620 child_pdi = child_pdi->die_sibling)
14621 {
14622 if (child_pdi->tag == DW_TAG_subprogram
14623 && child_pdi->linkage_name != NULL)
14624 {
14625 char *actual_class_name
14626 = language_class_name_from_physname (cu->language_defn,
14627 child_pdi->linkage_name);
14628 if (actual_class_name != NULL)
14629 {
14630 struct_pdi->name
14631 = obstack_copy0 (&cu->objfile->objfile_obstack,
14632 actual_class_name,
14633 strlen (actual_class_name));
14634 xfree (actual_class_name);
14635 }
14636 break;
14637 }
14638 }
14639 }
14640
14641 /* Adjust PART_DIE before generating a symbol for it. This function
14642 may set the is_external flag or change the DIE's name. */
14643
14644 static void
14645 fixup_partial_die (struct partial_die_info *part_die,
14646 struct dwarf2_cu *cu)
14647 {
14648 /* Once we've fixed up a die, there's no point in doing so again.
14649 This also avoids a memory leak if we were to call
14650 guess_partial_die_structure_name multiple times. */
14651 if (part_die->fixup_called)
14652 return;
14653
14654 /* If we found a reference attribute and the DIE has no name, try
14655 to find a name in the referred to DIE. */
14656
14657 if (part_die->name == NULL && part_die->has_specification)
14658 {
14659 struct partial_die_info *spec_die;
14660
14661 spec_die = find_partial_die (part_die->spec_offset,
14662 part_die->spec_is_dwz, cu);
14663
14664 fixup_partial_die (spec_die, cu);
14665
14666 if (spec_die->name)
14667 {
14668 part_die->name = spec_die->name;
14669
14670 /* Copy DW_AT_external attribute if it is set. */
14671 if (spec_die->is_external)
14672 part_die->is_external = spec_die->is_external;
14673 }
14674 }
14675
14676 /* Set default names for some unnamed DIEs. */
14677
14678 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
14679 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
14680
14681 /* If there is no parent die to provide a namespace, and there are
14682 children, see if we can determine the namespace from their linkage
14683 name. */
14684 if (cu->language == language_cplus
14685 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
14686 && part_die->die_parent == NULL
14687 && part_die->has_children
14688 && (part_die->tag == DW_TAG_class_type
14689 || part_die->tag == DW_TAG_structure_type
14690 || part_die->tag == DW_TAG_union_type))
14691 guess_partial_die_structure_name (part_die, cu);
14692
14693 /* GCC might emit a nameless struct or union that has a linkage
14694 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14695 if (part_die->name == NULL
14696 && (part_die->tag == DW_TAG_class_type
14697 || part_die->tag == DW_TAG_interface_type
14698 || part_die->tag == DW_TAG_structure_type
14699 || part_die->tag == DW_TAG_union_type)
14700 && part_die->linkage_name != NULL)
14701 {
14702 char *demangled;
14703
14704 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
14705 if (demangled)
14706 {
14707 const char *base;
14708
14709 /* Strip any leading namespaces/classes, keep only the base name.
14710 DW_AT_name for named DIEs does not contain the prefixes. */
14711 base = strrchr (demangled, ':');
14712 if (base && base > demangled && base[-1] == ':')
14713 base++;
14714 else
14715 base = demangled;
14716
14717 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14718 base, strlen (base));
14719 xfree (demangled);
14720 }
14721 }
14722
14723 part_die->fixup_called = 1;
14724 }
14725
14726 /* Read an attribute value described by an attribute form. */
14727
14728 static const gdb_byte *
14729 read_attribute_value (const struct die_reader_specs *reader,
14730 struct attribute *attr, unsigned form,
14731 const gdb_byte *info_ptr)
14732 {
14733 struct dwarf2_cu *cu = reader->cu;
14734 bfd *abfd = reader->abfd;
14735 struct comp_unit_head *cu_header = &cu->header;
14736 unsigned int bytes_read;
14737 struct dwarf_block *blk;
14738
14739 attr->form = form;
14740 switch (form)
14741 {
14742 case DW_FORM_ref_addr:
14743 if (cu->header.version == 2)
14744 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14745 else
14746 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14747 &cu->header, &bytes_read);
14748 info_ptr += bytes_read;
14749 break;
14750 case DW_FORM_GNU_ref_alt:
14751 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14752 info_ptr += bytes_read;
14753 break;
14754 case DW_FORM_addr:
14755 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14756 info_ptr += bytes_read;
14757 break;
14758 case DW_FORM_block2:
14759 blk = dwarf_alloc_block (cu);
14760 blk->size = read_2_bytes (abfd, info_ptr);
14761 info_ptr += 2;
14762 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14763 info_ptr += blk->size;
14764 DW_BLOCK (attr) = blk;
14765 break;
14766 case DW_FORM_block4:
14767 blk = dwarf_alloc_block (cu);
14768 blk->size = read_4_bytes (abfd, info_ptr);
14769 info_ptr += 4;
14770 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14771 info_ptr += blk->size;
14772 DW_BLOCK (attr) = blk;
14773 break;
14774 case DW_FORM_data2:
14775 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14776 info_ptr += 2;
14777 break;
14778 case DW_FORM_data4:
14779 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14780 info_ptr += 4;
14781 break;
14782 case DW_FORM_data8:
14783 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14784 info_ptr += 8;
14785 break;
14786 case DW_FORM_sec_offset:
14787 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14788 info_ptr += bytes_read;
14789 break;
14790 case DW_FORM_string:
14791 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
14792 DW_STRING_IS_CANONICAL (attr) = 0;
14793 info_ptr += bytes_read;
14794 break;
14795 case DW_FORM_strp:
14796 if (!cu->per_cu->is_dwz)
14797 {
14798 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14799 &bytes_read);
14800 DW_STRING_IS_CANONICAL (attr) = 0;
14801 info_ptr += bytes_read;
14802 break;
14803 }
14804 /* FALLTHROUGH */
14805 case DW_FORM_GNU_strp_alt:
14806 {
14807 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14808 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14809 &bytes_read);
14810
14811 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14812 DW_STRING_IS_CANONICAL (attr) = 0;
14813 info_ptr += bytes_read;
14814 }
14815 break;
14816 case DW_FORM_exprloc:
14817 case DW_FORM_block:
14818 blk = dwarf_alloc_block (cu);
14819 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14820 info_ptr += bytes_read;
14821 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14822 info_ptr += blk->size;
14823 DW_BLOCK (attr) = blk;
14824 break;
14825 case DW_FORM_block1:
14826 blk = dwarf_alloc_block (cu);
14827 blk->size = read_1_byte (abfd, info_ptr);
14828 info_ptr += 1;
14829 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14830 info_ptr += blk->size;
14831 DW_BLOCK (attr) = blk;
14832 break;
14833 case DW_FORM_data1:
14834 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14835 info_ptr += 1;
14836 break;
14837 case DW_FORM_flag:
14838 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14839 info_ptr += 1;
14840 break;
14841 case DW_FORM_flag_present:
14842 DW_UNSND (attr) = 1;
14843 break;
14844 case DW_FORM_sdata:
14845 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14846 info_ptr += bytes_read;
14847 break;
14848 case DW_FORM_udata:
14849 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14850 info_ptr += bytes_read;
14851 break;
14852 case DW_FORM_ref1:
14853 DW_UNSND (attr) = (cu->header.offset.sect_off
14854 + read_1_byte (abfd, info_ptr));
14855 info_ptr += 1;
14856 break;
14857 case DW_FORM_ref2:
14858 DW_UNSND (attr) = (cu->header.offset.sect_off
14859 + read_2_bytes (abfd, info_ptr));
14860 info_ptr += 2;
14861 break;
14862 case DW_FORM_ref4:
14863 DW_UNSND (attr) = (cu->header.offset.sect_off
14864 + read_4_bytes (abfd, info_ptr));
14865 info_ptr += 4;
14866 break;
14867 case DW_FORM_ref8:
14868 DW_UNSND (attr) = (cu->header.offset.sect_off
14869 + read_8_bytes (abfd, info_ptr));
14870 info_ptr += 8;
14871 break;
14872 case DW_FORM_ref_sig8:
14873 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
14874 info_ptr += 8;
14875 break;
14876 case DW_FORM_ref_udata:
14877 DW_UNSND (attr) = (cu->header.offset.sect_off
14878 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
14879 info_ptr += bytes_read;
14880 break;
14881 case DW_FORM_indirect:
14882 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14883 info_ptr += bytes_read;
14884 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
14885 break;
14886 case DW_FORM_GNU_addr_index:
14887 if (reader->dwo_file == NULL)
14888 {
14889 /* For now flag a hard error.
14890 Later we can turn this into a complaint. */
14891 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14892 dwarf_form_name (form),
14893 bfd_get_filename (abfd));
14894 }
14895 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14896 info_ptr += bytes_read;
14897 break;
14898 case DW_FORM_GNU_str_index:
14899 if (reader->dwo_file == NULL)
14900 {
14901 /* For now flag a hard error.
14902 Later we can turn this into a complaint if warranted. */
14903 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14904 dwarf_form_name (form),
14905 bfd_get_filename (abfd));
14906 }
14907 {
14908 ULONGEST str_index =
14909 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14910
14911 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14912 DW_STRING_IS_CANONICAL (attr) = 0;
14913 info_ptr += bytes_read;
14914 }
14915 break;
14916 default:
14917 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
14918 dwarf_form_name (form),
14919 bfd_get_filename (abfd));
14920 }
14921
14922 /* Super hack. */
14923 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
14924 attr->form = DW_FORM_GNU_ref_alt;
14925
14926 /* We have seen instances where the compiler tried to emit a byte
14927 size attribute of -1 which ended up being encoded as an unsigned
14928 0xffffffff. Although 0xffffffff is technically a valid size value,
14929 an object of this size seems pretty unlikely so we can relatively
14930 safely treat these cases as if the size attribute was invalid and
14931 treat them as zero by default. */
14932 if (attr->name == DW_AT_byte_size
14933 && form == DW_FORM_data4
14934 && DW_UNSND (attr) >= 0xffffffff)
14935 {
14936 complaint
14937 (&symfile_complaints,
14938 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14939 hex_string (DW_UNSND (attr)));
14940 DW_UNSND (attr) = 0;
14941 }
14942
14943 return info_ptr;
14944 }
14945
14946 /* Read an attribute described by an abbreviated attribute. */
14947
14948 static const gdb_byte *
14949 read_attribute (const struct die_reader_specs *reader,
14950 struct attribute *attr, struct attr_abbrev *abbrev,
14951 const gdb_byte *info_ptr)
14952 {
14953 attr->name = abbrev->name;
14954 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
14955 }
14956
14957 /* Read dwarf information from a buffer. */
14958
14959 static unsigned int
14960 read_1_byte (bfd *abfd, const gdb_byte *buf)
14961 {
14962 return bfd_get_8 (abfd, buf);
14963 }
14964
14965 static int
14966 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
14967 {
14968 return bfd_get_signed_8 (abfd, buf);
14969 }
14970
14971 static unsigned int
14972 read_2_bytes (bfd *abfd, const gdb_byte *buf)
14973 {
14974 return bfd_get_16 (abfd, buf);
14975 }
14976
14977 static int
14978 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
14979 {
14980 return bfd_get_signed_16 (abfd, buf);
14981 }
14982
14983 static unsigned int
14984 read_4_bytes (bfd *abfd, const gdb_byte *buf)
14985 {
14986 return bfd_get_32 (abfd, buf);
14987 }
14988
14989 static int
14990 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
14991 {
14992 return bfd_get_signed_32 (abfd, buf);
14993 }
14994
14995 static ULONGEST
14996 read_8_bytes (bfd *abfd, const gdb_byte *buf)
14997 {
14998 return bfd_get_64 (abfd, buf);
14999 }
15000
15001 static CORE_ADDR
15002 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
15003 unsigned int *bytes_read)
15004 {
15005 struct comp_unit_head *cu_header = &cu->header;
15006 CORE_ADDR retval = 0;
15007
15008 if (cu_header->signed_addr_p)
15009 {
15010 switch (cu_header->addr_size)
15011 {
15012 case 2:
15013 retval = bfd_get_signed_16 (abfd, buf);
15014 break;
15015 case 4:
15016 retval = bfd_get_signed_32 (abfd, buf);
15017 break;
15018 case 8:
15019 retval = bfd_get_signed_64 (abfd, buf);
15020 break;
15021 default:
15022 internal_error (__FILE__, __LINE__,
15023 _("read_address: bad switch, signed [in module %s]"),
15024 bfd_get_filename (abfd));
15025 }
15026 }
15027 else
15028 {
15029 switch (cu_header->addr_size)
15030 {
15031 case 2:
15032 retval = bfd_get_16 (abfd, buf);
15033 break;
15034 case 4:
15035 retval = bfd_get_32 (abfd, buf);
15036 break;
15037 case 8:
15038 retval = bfd_get_64 (abfd, buf);
15039 break;
15040 default:
15041 internal_error (__FILE__, __LINE__,
15042 _("read_address: bad switch, "
15043 "unsigned [in module %s]"),
15044 bfd_get_filename (abfd));
15045 }
15046 }
15047
15048 *bytes_read = cu_header->addr_size;
15049 return retval;
15050 }
15051
15052 /* Read the initial length from a section. The (draft) DWARF 3
15053 specification allows the initial length to take up either 4 bytes
15054 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
15055 bytes describe the length and all offsets will be 8 bytes in length
15056 instead of 4.
15057
15058 An older, non-standard 64-bit format is also handled by this
15059 function. The older format in question stores the initial length
15060 as an 8-byte quantity without an escape value. Lengths greater
15061 than 2^32 aren't very common which means that the initial 4 bytes
15062 is almost always zero. Since a length value of zero doesn't make
15063 sense for the 32-bit format, this initial zero can be considered to
15064 be an escape value which indicates the presence of the older 64-bit
15065 format. As written, the code can't detect (old format) lengths
15066 greater than 4GB. If it becomes necessary to handle lengths
15067 somewhat larger than 4GB, we could allow other small values (such
15068 as the non-sensical values of 1, 2, and 3) to also be used as
15069 escape values indicating the presence of the old format.
15070
15071 The value returned via bytes_read should be used to increment the
15072 relevant pointer after calling read_initial_length().
15073
15074 [ Note: read_initial_length() and read_offset() are based on the
15075 document entitled "DWARF Debugging Information Format", revision
15076 3, draft 8, dated November 19, 2001. This document was obtained
15077 from:
15078
15079 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
15080
15081 This document is only a draft and is subject to change. (So beware.)
15082
15083 Details regarding the older, non-standard 64-bit format were
15084 determined empirically by examining 64-bit ELF files produced by
15085 the SGI toolchain on an IRIX 6.5 machine.
15086
15087 - Kevin, July 16, 2002
15088 ] */
15089
15090 static LONGEST
15091 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
15092 {
15093 LONGEST length = bfd_get_32 (abfd, buf);
15094
15095 if (length == 0xffffffff)
15096 {
15097 length = bfd_get_64 (abfd, buf + 4);
15098 *bytes_read = 12;
15099 }
15100 else if (length == 0)
15101 {
15102 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
15103 length = bfd_get_64 (abfd, buf);
15104 *bytes_read = 8;
15105 }
15106 else
15107 {
15108 *bytes_read = 4;
15109 }
15110
15111 return length;
15112 }
15113
15114 /* Cover function for read_initial_length.
15115 Returns the length of the object at BUF, and stores the size of the
15116 initial length in *BYTES_READ and stores the size that offsets will be in
15117 *OFFSET_SIZE.
15118 If the initial length size is not equivalent to that specified in
15119 CU_HEADER then issue a complaint.
15120 This is useful when reading non-comp-unit headers. */
15121
15122 static LONGEST
15123 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
15124 const struct comp_unit_head *cu_header,
15125 unsigned int *bytes_read,
15126 unsigned int *offset_size)
15127 {
15128 LONGEST length = read_initial_length (abfd, buf, bytes_read);
15129
15130 gdb_assert (cu_header->initial_length_size == 4
15131 || cu_header->initial_length_size == 8
15132 || cu_header->initial_length_size == 12);
15133
15134 if (cu_header->initial_length_size != *bytes_read)
15135 complaint (&symfile_complaints,
15136 _("intermixed 32-bit and 64-bit DWARF sections"));
15137
15138 *offset_size = (*bytes_read == 4) ? 4 : 8;
15139 return length;
15140 }
15141
15142 /* Read an offset from the data stream. The size of the offset is
15143 given by cu_header->offset_size. */
15144
15145 static LONGEST
15146 read_offset (bfd *abfd, const gdb_byte *buf,
15147 const struct comp_unit_head *cu_header,
15148 unsigned int *bytes_read)
15149 {
15150 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
15151
15152 *bytes_read = cu_header->offset_size;
15153 return offset;
15154 }
15155
15156 /* Read an offset from the data stream. */
15157
15158 static LONGEST
15159 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
15160 {
15161 LONGEST retval = 0;
15162
15163 switch (offset_size)
15164 {
15165 case 4:
15166 retval = bfd_get_32 (abfd, buf);
15167 break;
15168 case 8:
15169 retval = bfd_get_64 (abfd, buf);
15170 break;
15171 default:
15172 internal_error (__FILE__, __LINE__,
15173 _("read_offset_1: bad switch [in module %s]"),
15174 bfd_get_filename (abfd));
15175 }
15176
15177 return retval;
15178 }
15179
15180 static const gdb_byte *
15181 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
15182 {
15183 /* If the size of a host char is 8 bits, we can return a pointer
15184 to the buffer, otherwise we have to copy the data to a buffer
15185 allocated on the temporary obstack. */
15186 gdb_assert (HOST_CHAR_BIT == 8);
15187 return buf;
15188 }
15189
15190 static const char *
15191 read_direct_string (bfd *abfd, const gdb_byte *buf,
15192 unsigned int *bytes_read_ptr)
15193 {
15194 /* If the size of a host char is 8 bits, we can return a pointer
15195 to the string, otherwise we have to copy the string to a buffer
15196 allocated on the temporary obstack. */
15197 gdb_assert (HOST_CHAR_BIT == 8);
15198 if (*buf == '\0')
15199 {
15200 *bytes_read_ptr = 1;
15201 return NULL;
15202 }
15203 *bytes_read_ptr = strlen ((const char *) buf) + 1;
15204 return (const char *) buf;
15205 }
15206
15207 static const char *
15208 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
15209 {
15210 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
15211 if (dwarf2_per_objfile->str.buffer == NULL)
15212 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
15213 bfd_get_filename (abfd));
15214 if (str_offset >= dwarf2_per_objfile->str.size)
15215 error (_("DW_FORM_strp pointing outside of "
15216 ".debug_str section [in module %s]"),
15217 bfd_get_filename (abfd));
15218 gdb_assert (HOST_CHAR_BIT == 8);
15219 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
15220 return NULL;
15221 return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
15222 }
15223
15224 /* Read a string at offset STR_OFFSET in the .debug_str section from
15225 the .dwz file DWZ. Throw an error if the offset is too large. If
15226 the string consists of a single NUL byte, return NULL; otherwise
15227 return a pointer to the string. */
15228
15229 static const char *
15230 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
15231 {
15232 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
15233
15234 if (dwz->str.buffer == NULL)
15235 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
15236 "section [in module %s]"),
15237 bfd_get_filename (dwz->dwz_bfd));
15238 if (str_offset >= dwz->str.size)
15239 error (_("DW_FORM_GNU_strp_alt pointing outside of "
15240 ".debug_str section [in module %s]"),
15241 bfd_get_filename (dwz->dwz_bfd));
15242 gdb_assert (HOST_CHAR_BIT == 8);
15243 if (dwz->str.buffer[str_offset] == '\0')
15244 return NULL;
15245 return (const char *) (dwz->str.buffer + str_offset);
15246 }
15247
15248 static const char *
15249 read_indirect_string (bfd *abfd, const gdb_byte *buf,
15250 const struct comp_unit_head *cu_header,
15251 unsigned int *bytes_read_ptr)
15252 {
15253 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
15254
15255 return read_indirect_string_at_offset (abfd, str_offset);
15256 }
15257
15258 static ULONGEST
15259 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
15260 unsigned int *bytes_read_ptr)
15261 {
15262 ULONGEST result;
15263 unsigned int num_read;
15264 int i, shift;
15265 unsigned char byte;
15266
15267 result = 0;
15268 shift = 0;
15269 num_read = 0;
15270 i = 0;
15271 while (1)
15272 {
15273 byte = bfd_get_8 (abfd, buf);
15274 buf++;
15275 num_read++;
15276 result |= ((ULONGEST) (byte & 127) << shift);
15277 if ((byte & 128) == 0)
15278 {
15279 break;
15280 }
15281 shift += 7;
15282 }
15283 *bytes_read_ptr = num_read;
15284 return result;
15285 }
15286
15287 static LONGEST
15288 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
15289 unsigned int *bytes_read_ptr)
15290 {
15291 LONGEST result;
15292 int i, shift, num_read;
15293 unsigned char byte;
15294
15295 result = 0;
15296 shift = 0;
15297 num_read = 0;
15298 i = 0;
15299 while (1)
15300 {
15301 byte = bfd_get_8 (abfd, buf);
15302 buf++;
15303 num_read++;
15304 result |= ((LONGEST) (byte & 127) << shift);
15305 shift += 7;
15306 if ((byte & 128) == 0)
15307 {
15308 break;
15309 }
15310 }
15311 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
15312 result |= -(((LONGEST) 1) << shift);
15313 *bytes_read_ptr = num_read;
15314 return result;
15315 }
15316
15317 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
15318 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
15319 ADDR_SIZE is the size of addresses from the CU header. */
15320
15321 static CORE_ADDR
15322 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
15323 {
15324 struct objfile *objfile = dwarf2_per_objfile->objfile;
15325 bfd *abfd = objfile->obfd;
15326 const gdb_byte *info_ptr;
15327
15328 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
15329 if (dwarf2_per_objfile->addr.buffer == NULL)
15330 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
15331 objfile_name (objfile));
15332 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
15333 error (_("DW_FORM_addr_index pointing outside of "
15334 ".debug_addr section [in module %s]"),
15335 objfile_name (objfile));
15336 info_ptr = (dwarf2_per_objfile->addr.buffer
15337 + addr_base + addr_index * addr_size);
15338 if (addr_size == 4)
15339 return bfd_get_32 (abfd, info_ptr);
15340 else
15341 return bfd_get_64 (abfd, info_ptr);
15342 }
15343
15344 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
15345
15346 static CORE_ADDR
15347 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
15348 {
15349 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
15350 }
15351
15352 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
15353
15354 static CORE_ADDR
15355 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
15356 unsigned int *bytes_read)
15357 {
15358 bfd *abfd = cu->objfile->obfd;
15359 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
15360
15361 return read_addr_index (cu, addr_index);
15362 }
15363
15364 /* Data structure to pass results from dwarf2_read_addr_index_reader
15365 back to dwarf2_read_addr_index. */
15366
15367 struct dwarf2_read_addr_index_data
15368 {
15369 ULONGEST addr_base;
15370 int addr_size;
15371 };
15372
15373 /* die_reader_func for dwarf2_read_addr_index. */
15374
15375 static void
15376 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
15377 const gdb_byte *info_ptr,
15378 struct die_info *comp_unit_die,
15379 int has_children,
15380 void *data)
15381 {
15382 struct dwarf2_cu *cu = reader->cu;
15383 struct dwarf2_read_addr_index_data *aidata =
15384 (struct dwarf2_read_addr_index_data *) data;
15385
15386 aidata->addr_base = cu->addr_base;
15387 aidata->addr_size = cu->header.addr_size;
15388 }
15389
15390 /* Given an index in .debug_addr, fetch the value.
15391 NOTE: This can be called during dwarf expression evaluation,
15392 long after the debug information has been read, and thus per_cu->cu
15393 may no longer exist. */
15394
15395 CORE_ADDR
15396 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
15397 unsigned int addr_index)
15398 {
15399 struct objfile *objfile = per_cu->objfile;
15400 struct dwarf2_cu *cu = per_cu->cu;
15401 ULONGEST addr_base;
15402 int addr_size;
15403
15404 /* This is intended to be called from outside this file. */
15405 dw2_setup (objfile);
15406
15407 /* We need addr_base and addr_size.
15408 If we don't have PER_CU->cu, we have to get it.
15409 Nasty, but the alternative is storing the needed info in PER_CU,
15410 which at this point doesn't seem justified: it's not clear how frequently
15411 it would get used and it would increase the size of every PER_CU.
15412 Entry points like dwarf2_per_cu_addr_size do a similar thing
15413 so we're not in uncharted territory here.
15414 Alas we need to be a bit more complicated as addr_base is contained
15415 in the DIE.
15416
15417 We don't need to read the entire CU(/TU).
15418 We just need the header and top level die.
15419
15420 IWBN to use the aging mechanism to let us lazily later discard the CU.
15421 For now we skip this optimization. */
15422
15423 if (cu != NULL)
15424 {
15425 addr_base = cu->addr_base;
15426 addr_size = cu->header.addr_size;
15427 }
15428 else
15429 {
15430 struct dwarf2_read_addr_index_data aidata;
15431
15432 /* Note: We can't use init_cutu_and_read_dies_simple here,
15433 we need addr_base. */
15434 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
15435 dwarf2_read_addr_index_reader, &aidata);
15436 addr_base = aidata.addr_base;
15437 addr_size = aidata.addr_size;
15438 }
15439
15440 return read_addr_index_1 (addr_index, addr_base, addr_size);
15441 }
15442
15443 /* Given a DW_FORM_GNU_str_index, fetch the string.
15444 This is only used by the Fission support. */
15445
15446 static const char *
15447 read_str_index (const struct die_reader_specs *reader,
15448 struct dwarf2_cu *cu, ULONGEST str_index)
15449 {
15450 struct objfile *objfile = dwarf2_per_objfile->objfile;
15451 const char *dwo_name = objfile_name (objfile);
15452 bfd *abfd = objfile->obfd;
15453 struct dwo_sections *sections = &reader->dwo_file->sections;
15454 const gdb_byte *info_ptr;
15455 ULONGEST str_offset;
15456 static const char form_name[] = "DW_FORM_GNU_str_index";
15457
15458 dwarf2_read_section (objfile, &sections->str);
15459 dwarf2_read_section (objfile, &sections->str_offsets);
15460 if (sections->str.buffer == NULL)
15461 error (_("%s used without .debug_str.dwo section"
15462 " in CU at offset 0x%lx [in module %s]"),
15463 form_name, (long) cu->header.offset.sect_off, dwo_name);
15464 if (sections->str_offsets.buffer == NULL)
15465 error (_("%s used without .debug_str_offsets.dwo section"
15466 " in CU at offset 0x%lx [in module %s]"),
15467 form_name, (long) cu->header.offset.sect_off, dwo_name);
15468 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
15469 error (_("%s pointing outside of .debug_str_offsets.dwo"
15470 " section in CU at offset 0x%lx [in module %s]"),
15471 form_name, (long) cu->header.offset.sect_off, dwo_name);
15472 info_ptr = (sections->str_offsets.buffer
15473 + str_index * cu->header.offset_size);
15474 if (cu->header.offset_size == 4)
15475 str_offset = bfd_get_32 (abfd, info_ptr);
15476 else
15477 str_offset = bfd_get_64 (abfd, info_ptr);
15478 if (str_offset >= sections->str.size)
15479 error (_("Offset from %s pointing outside of"
15480 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
15481 form_name, (long) cu->header.offset.sect_off, dwo_name);
15482 return (const char *) (sections->str.buffer + str_offset);
15483 }
15484
15485 /* Return the length of an LEB128 number in BUF. */
15486
15487 static int
15488 leb128_size (const gdb_byte *buf)
15489 {
15490 const gdb_byte *begin = buf;
15491 gdb_byte byte;
15492
15493 while (1)
15494 {
15495 byte = *buf++;
15496 if ((byte & 128) == 0)
15497 return buf - begin;
15498 }
15499 }
15500
15501 static void
15502 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
15503 {
15504 switch (lang)
15505 {
15506 case DW_LANG_C89:
15507 case DW_LANG_C99:
15508 case DW_LANG_C:
15509 case DW_LANG_UPC:
15510 cu->language = language_c;
15511 break;
15512 case DW_LANG_C_plus_plus:
15513 cu->language = language_cplus;
15514 break;
15515 case DW_LANG_D:
15516 cu->language = language_d;
15517 break;
15518 case DW_LANG_Fortran77:
15519 case DW_LANG_Fortran90:
15520 case DW_LANG_Fortran95:
15521 cu->language = language_fortran;
15522 break;
15523 case DW_LANG_Go:
15524 cu->language = language_go;
15525 break;
15526 case DW_LANG_Mips_Assembler:
15527 cu->language = language_asm;
15528 break;
15529 case DW_LANG_Java:
15530 cu->language = language_java;
15531 break;
15532 case DW_LANG_Ada83:
15533 case DW_LANG_Ada95:
15534 cu->language = language_ada;
15535 break;
15536 case DW_LANG_Modula2:
15537 cu->language = language_m2;
15538 break;
15539 case DW_LANG_Pascal83:
15540 cu->language = language_pascal;
15541 break;
15542 case DW_LANG_ObjC:
15543 cu->language = language_objc;
15544 break;
15545 case DW_LANG_Cobol74:
15546 case DW_LANG_Cobol85:
15547 default:
15548 cu->language = language_minimal;
15549 break;
15550 }
15551 cu->language_defn = language_def (cu->language);
15552 }
15553
15554 /* Return the named attribute or NULL if not there. */
15555
15556 static struct attribute *
15557 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
15558 {
15559 for (;;)
15560 {
15561 unsigned int i;
15562 struct attribute *spec = NULL;
15563
15564 for (i = 0; i < die->num_attrs; ++i)
15565 {
15566 if (die->attrs[i].name == name)
15567 return &die->attrs[i];
15568 if (die->attrs[i].name == DW_AT_specification
15569 || die->attrs[i].name == DW_AT_abstract_origin)
15570 spec = &die->attrs[i];
15571 }
15572
15573 if (!spec)
15574 break;
15575
15576 die = follow_die_ref (die, spec, &cu);
15577 }
15578
15579 return NULL;
15580 }
15581
15582 /* Return the named attribute or NULL if not there,
15583 but do not follow DW_AT_specification, etc.
15584 This is for use in contexts where we're reading .debug_types dies.
15585 Following DW_AT_specification, DW_AT_abstract_origin will take us
15586 back up the chain, and we want to go down. */
15587
15588 static struct attribute *
15589 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
15590 {
15591 unsigned int i;
15592
15593 for (i = 0; i < die->num_attrs; ++i)
15594 if (die->attrs[i].name == name)
15595 return &die->attrs[i];
15596
15597 return NULL;
15598 }
15599
15600 /* Return non-zero iff the attribute NAME is defined for the given DIE,
15601 and holds a non-zero value. This function should only be used for
15602 DW_FORM_flag or DW_FORM_flag_present attributes. */
15603
15604 static int
15605 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
15606 {
15607 struct attribute *attr = dwarf2_attr (die, name, cu);
15608
15609 return (attr && DW_UNSND (attr));
15610 }
15611
15612 static int
15613 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
15614 {
15615 /* A DIE is a declaration if it has a DW_AT_declaration attribute
15616 which value is non-zero. However, we have to be careful with
15617 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
15618 (via dwarf2_flag_true_p) follows this attribute. So we may
15619 end up accidently finding a declaration attribute that belongs
15620 to a different DIE referenced by the specification attribute,
15621 even though the given DIE does not have a declaration attribute. */
15622 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
15623 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
15624 }
15625
15626 /* Return the die giving the specification for DIE, if there is
15627 one. *SPEC_CU is the CU containing DIE on input, and the CU
15628 containing the return value on output. If there is no
15629 specification, but there is an abstract origin, that is
15630 returned. */
15631
15632 static struct die_info *
15633 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
15634 {
15635 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
15636 *spec_cu);
15637
15638 if (spec_attr == NULL)
15639 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
15640
15641 if (spec_attr == NULL)
15642 return NULL;
15643 else
15644 return follow_die_ref (die, spec_attr, spec_cu);
15645 }
15646
15647 /* Free the line_header structure *LH, and any arrays and strings it
15648 refers to.
15649 NOTE: This is also used as a "cleanup" function. */
15650
15651 static void
15652 free_line_header (struct line_header *lh)
15653 {
15654 if (lh->standard_opcode_lengths)
15655 xfree (lh->standard_opcode_lengths);
15656
15657 /* Remember that all the lh->file_names[i].name pointers are
15658 pointers into debug_line_buffer, and don't need to be freed. */
15659 if (lh->file_names)
15660 xfree (lh->file_names);
15661
15662 /* Similarly for the include directory names. */
15663 if (lh->include_dirs)
15664 xfree (lh->include_dirs);
15665
15666 xfree (lh);
15667 }
15668
15669 /* Add an entry to LH's include directory table. */
15670
15671 static void
15672 add_include_dir (struct line_header *lh, const char *include_dir)
15673 {
15674 /* Grow the array if necessary. */
15675 if (lh->include_dirs_size == 0)
15676 {
15677 lh->include_dirs_size = 1; /* for testing */
15678 lh->include_dirs = xmalloc (lh->include_dirs_size
15679 * sizeof (*lh->include_dirs));
15680 }
15681 else if (lh->num_include_dirs >= lh->include_dirs_size)
15682 {
15683 lh->include_dirs_size *= 2;
15684 lh->include_dirs = xrealloc (lh->include_dirs,
15685 (lh->include_dirs_size
15686 * sizeof (*lh->include_dirs)));
15687 }
15688
15689 lh->include_dirs[lh->num_include_dirs++] = include_dir;
15690 }
15691
15692 /* Add an entry to LH's file name table. */
15693
15694 static void
15695 add_file_name (struct line_header *lh,
15696 const char *name,
15697 unsigned int dir_index,
15698 unsigned int mod_time,
15699 unsigned int length)
15700 {
15701 struct file_entry *fe;
15702
15703 /* Grow the array if necessary. */
15704 if (lh->file_names_size == 0)
15705 {
15706 lh->file_names_size = 1; /* for testing */
15707 lh->file_names = xmalloc (lh->file_names_size
15708 * sizeof (*lh->file_names));
15709 }
15710 else if (lh->num_file_names >= lh->file_names_size)
15711 {
15712 lh->file_names_size *= 2;
15713 lh->file_names = xrealloc (lh->file_names,
15714 (lh->file_names_size
15715 * sizeof (*lh->file_names)));
15716 }
15717
15718 fe = &lh->file_names[lh->num_file_names++];
15719 fe->name = name;
15720 fe->dir_index = dir_index;
15721 fe->mod_time = mod_time;
15722 fe->length = length;
15723 fe->included_p = 0;
15724 fe->symtab = NULL;
15725 }
15726
15727 /* A convenience function to find the proper .debug_line section for a
15728 CU. */
15729
15730 static struct dwarf2_section_info *
15731 get_debug_line_section (struct dwarf2_cu *cu)
15732 {
15733 struct dwarf2_section_info *section;
15734
15735 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15736 DWO file. */
15737 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15738 section = &cu->dwo_unit->dwo_file->sections.line;
15739 else if (cu->per_cu->is_dwz)
15740 {
15741 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15742
15743 section = &dwz->line;
15744 }
15745 else
15746 section = &dwarf2_per_objfile->line;
15747
15748 return section;
15749 }
15750
15751 /* Read the statement program header starting at OFFSET in
15752 .debug_line, or .debug_line.dwo. Return a pointer
15753 to a struct line_header, allocated using xmalloc.
15754
15755 NOTE: the strings in the include directory and file name tables of
15756 the returned object point into the dwarf line section buffer,
15757 and must not be freed. */
15758
15759 static struct line_header *
15760 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
15761 {
15762 struct cleanup *back_to;
15763 struct line_header *lh;
15764 const gdb_byte *line_ptr;
15765 unsigned int bytes_read, offset_size;
15766 int i;
15767 const char *cur_dir, *cur_file;
15768 struct dwarf2_section_info *section;
15769 bfd *abfd;
15770
15771 section = get_debug_line_section (cu);
15772 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15773 if (section->buffer == NULL)
15774 {
15775 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15776 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15777 else
15778 complaint (&symfile_complaints, _("missing .debug_line section"));
15779 return 0;
15780 }
15781
15782 /* We can't do this until we know the section is non-empty.
15783 Only then do we know we have such a section. */
15784 abfd = section->asection->owner;
15785
15786 /* Make sure that at least there's room for the total_length field.
15787 That could be 12 bytes long, but we're just going to fudge that. */
15788 if (offset + 4 >= section->size)
15789 {
15790 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15791 return 0;
15792 }
15793
15794 lh = xmalloc (sizeof (*lh));
15795 memset (lh, 0, sizeof (*lh));
15796 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15797 (void *) lh);
15798
15799 line_ptr = section->buffer + offset;
15800
15801 /* Read in the header. */
15802 lh->total_length =
15803 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15804 &bytes_read, &offset_size);
15805 line_ptr += bytes_read;
15806 if (line_ptr + lh->total_length > (section->buffer + section->size))
15807 {
15808 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15809 do_cleanups (back_to);
15810 return 0;
15811 }
15812 lh->statement_program_end = line_ptr + lh->total_length;
15813 lh->version = read_2_bytes (abfd, line_ptr);
15814 line_ptr += 2;
15815 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15816 line_ptr += offset_size;
15817 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15818 line_ptr += 1;
15819 if (lh->version >= 4)
15820 {
15821 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15822 line_ptr += 1;
15823 }
15824 else
15825 lh->maximum_ops_per_instruction = 1;
15826
15827 if (lh->maximum_ops_per_instruction == 0)
15828 {
15829 lh->maximum_ops_per_instruction = 1;
15830 complaint (&symfile_complaints,
15831 _("invalid maximum_ops_per_instruction "
15832 "in `.debug_line' section"));
15833 }
15834
15835 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15836 line_ptr += 1;
15837 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15838 line_ptr += 1;
15839 lh->line_range = read_1_byte (abfd, line_ptr);
15840 line_ptr += 1;
15841 lh->opcode_base = read_1_byte (abfd, line_ptr);
15842 line_ptr += 1;
15843 lh->standard_opcode_lengths
15844 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
15845
15846 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15847 for (i = 1; i < lh->opcode_base; ++i)
15848 {
15849 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15850 line_ptr += 1;
15851 }
15852
15853 /* Read directory table. */
15854 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15855 {
15856 line_ptr += bytes_read;
15857 add_include_dir (lh, cur_dir);
15858 }
15859 line_ptr += bytes_read;
15860
15861 /* Read file name table. */
15862 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15863 {
15864 unsigned int dir_index, mod_time, length;
15865
15866 line_ptr += bytes_read;
15867 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15868 line_ptr += bytes_read;
15869 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15870 line_ptr += bytes_read;
15871 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15872 line_ptr += bytes_read;
15873
15874 add_file_name (lh, cur_file, dir_index, mod_time, length);
15875 }
15876 line_ptr += bytes_read;
15877 lh->statement_program_start = line_ptr;
15878
15879 if (line_ptr > (section->buffer + section->size))
15880 complaint (&symfile_complaints,
15881 _("line number info header doesn't "
15882 "fit in `.debug_line' section"));
15883
15884 discard_cleanups (back_to);
15885 return lh;
15886 }
15887
15888 /* Subroutine of dwarf_decode_lines to simplify it.
15889 Return the file name of the psymtab for included file FILE_INDEX
15890 in line header LH of PST.
15891 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15892 If space for the result is malloc'd, it will be freed by a cleanup.
15893 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15894
15895 The function creates dangling cleanup registration. */
15896
15897 static const char *
15898 psymtab_include_file_name (const struct line_header *lh, int file_index,
15899 const struct partial_symtab *pst,
15900 const char *comp_dir)
15901 {
15902 const struct file_entry fe = lh->file_names [file_index];
15903 const char *include_name = fe.name;
15904 const char *include_name_to_compare = include_name;
15905 const char *dir_name = NULL;
15906 const char *pst_filename;
15907 char *copied_name = NULL;
15908 int file_is_pst;
15909
15910 if (fe.dir_index)
15911 dir_name = lh->include_dirs[fe.dir_index - 1];
15912
15913 if (!IS_ABSOLUTE_PATH (include_name)
15914 && (dir_name != NULL || comp_dir != NULL))
15915 {
15916 /* Avoid creating a duplicate psymtab for PST.
15917 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15918 Before we do the comparison, however, we need to account
15919 for DIR_NAME and COMP_DIR.
15920 First prepend dir_name (if non-NULL). If we still don't
15921 have an absolute path prepend comp_dir (if non-NULL).
15922 However, the directory we record in the include-file's
15923 psymtab does not contain COMP_DIR (to match the
15924 corresponding symtab(s)).
15925
15926 Example:
15927
15928 bash$ cd /tmp
15929 bash$ gcc -g ./hello.c
15930 include_name = "hello.c"
15931 dir_name = "."
15932 DW_AT_comp_dir = comp_dir = "/tmp"
15933 DW_AT_name = "./hello.c" */
15934
15935 if (dir_name != NULL)
15936 {
15937 char *tem = concat (dir_name, SLASH_STRING,
15938 include_name, (char *)NULL);
15939
15940 make_cleanup (xfree, tem);
15941 include_name = tem;
15942 include_name_to_compare = include_name;
15943 }
15944 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15945 {
15946 char *tem = concat (comp_dir, SLASH_STRING,
15947 include_name, (char *)NULL);
15948
15949 make_cleanup (xfree, tem);
15950 include_name_to_compare = tem;
15951 }
15952 }
15953
15954 pst_filename = pst->filename;
15955 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15956 {
15957 copied_name = concat (pst->dirname, SLASH_STRING,
15958 pst_filename, (char *)NULL);
15959 pst_filename = copied_name;
15960 }
15961
15962 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
15963
15964 if (copied_name != NULL)
15965 xfree (copied_name);
15966
15967 if (file_is_pst)
15968 return NULL;
15969 return include_name;
15970 }
15971
15972 /* Ignore this record_line request. */
15973
15974 static void
15975 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15976 {
15977 return;
15978 }
15979
15980 /* Subroutine of dwarf_decode_lines to simplify it.
15981 Process the line number information in LH. */
15982
15983 static void
15984 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15985 struct dwarf2_cu *cu, struct partial_symtab *pst)
15986 {
15987 const gdb_byte *line_ptr, *extended_end;
15988 const gdb_byte *line_end;
15989 unsigned int bytes_read, extended_len;
15990 unsigned char op_code, extended_op, adj_opcode;
15991 CORE_ADDR baseaddr;
15992 struct objfile *objfile = cu->objfile;
15993 bfd *abfd = objfile->obfd;
15994 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15995 const int decode_for_pst_p = (pst != NULL);
15996 struct subfile *last_subfile = NULL;
15997 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15998 = record_line;
15999
16000 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16001
16002 line_ptr = lh->statement_program_start;
16003 line_end = lh->statement_program_end;
16004
16005 /* Read the statement sequences until there's nothing left. */
16006 while (line_ptr < line_end)
16007 {
16008 /* state machine registers */
16009 CORE_ADDR address = 0;
16010 unsigned int file = 1;
16011 unsigned int line = 1;
16012 unsigned int column = 0;
16013 int is_stmt = lh->default_is_stmt;
16014 int basic_block = 0;
16015 int end_sequence = 0;
16016 CORE_ADDR addr;
16017 unsigned char op_index = 0;
16018
16019 if (!decode_for_pst_p && lh->num_file_names >= file)
16020 {
16021 /* Start a subfile for the current file of the state machine. */
16022 /* lh->include_dirs and lh->file_names are 0-based, but the
16023 directory and file name numbers in the statement program
16024 are 1-based. */
16025 struct file_entry *fe = &lh->file_names[file - 1];
16026 const char *dir = NULL;
16027
16028 if (fe->dir_index)
16029 dir = lh->include_dirs[fe->dir_index - 1];
16030
16031 dwarf2_start_subfile (fe->name, dir, comp_dir);
16032 }
16033
16034 /* Decode the table. */
16035 while (!end_sequence)
16036 {
16037 op_code = read_1_byte (abfd, line_ptr);
16038 line_ptr += 1;
16039 if (line_ptr > line_end)
16040 {
16041 dwarf2_debug_line_missing_end_sequence_complaint ();
16042 break;
16043 }
16044
16045 if (op_code >= lh->opcode_base)
16046 {
16047 /* Special operand. */
16048 adj_opcode = op_code - lh->opcode_base;
16049 address += (((op_index + (adj_opcode / lh->line_range))
16050 / lh->maximum_ops_per_instruction)
16051 * lh->minimum_instruction_length);
16052 op_index = ((op_index + (adj_opcode / lh->line_range))
16053 % lh->maximum_ops_per_instruction);
16054 line += lh->line_base + (adj_opcode % lh->line_range);
16055 if (lh->num_file_names < file || file == 0)
16056 dwarf2_debug_line_missing_file_complaint ();
16057 /* For now we ignore lines not starting on an
16058 instruction boundary. */
16059 else if (op_index == 0)
16060 {
16061 lh->file_names[file - 1].included_p = 1;
16062 if (!decode_for_pst_p && is_stmt)
16063 {
16064 if (last_subfile != current_subfile)
16065 {
16066 addr = gdbarch_addr_bits_remove (gdbarch, address);
16067 if (last_subfile)
16068 (*p_record_line) (last_subfile, 0, addr);
16069 last_subfile = current_subfile;
16070 }
16071 /* Append row to matrix using current values. */
16072 addr = gdbarch_addr_bits_remove (gdbarch, address);
16073 (*p_record_line) (current_subfile, line, addr);
16074 }
16075 }
16076 basic_block = 0;
16077 }
16078 else switch (op_code)
16079 {
16080 case DW_LNS_extended_op:
16081 extended_len = read_unsigned_leb128 (abfd, line_ptr,
16082 &bytes_read);
16083 line_ptr += bytes_read;
16084 extended_end = line_ptr + extended_len;
16085 extended_op = read_1_byte (abfd, line_ptr);
16086 line_ptr += 1;
16087 switch (extended_op)
16088 {
16089 case DW_LNE_end_sequence:
16090 p_record_line = record_line;
16091 end_sequence = 1;
16092 break;
16093 case DW_LNE_set_address:
16094 address = read_address (abfd, line_ptr, cu, &bytes_read);
16095
16096 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
16097 {
16098 /* This line table is for a function which has been
16099 GCd by the linker. Ignore it. PR gdb/12528 */
16100
16101 long line_offset
16102 = line_ptr - get_debug_line_section (cu)->buffer;
16103
16104 complaint (&symfile_complaints,
16105 _(".debug_line address at offset 0x%lx is 0 "
16106 "[in module %s]"),
16107 line_offset, objfile_name (objfile));
16108 p_record_line = noop_record_line;
16109 }
16110
16111 op_index = 0;
16112 line_ptr += bytes_read;
16113 address += baseaddr;
16114 break;
16115 case DW_LNE_define_file:
16116 {
16117 const char *cur_file;
16118 unsigned int dir_index, mod_time, length;
16119
16120 cur_file = read_direct_string (abfd, line_ptr,
16121 &bytes_read);
16122 line_ptr += bytes_read;
16123 dir_index =
16124 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16125 line_ptr += bytes_read;
16126 mod_time =
16127 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16128 line_ptr += bytes_read;
16129 length =
16130 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16131 line_ptr += bytes_read;
16132 add_file_name (lh, cur_file, dir_index, mod_time, length);
16133 }
16134 break;
16135 case DW_LNE_set_discriminator:
16136 /* The discriminator is not interesting to the debugger;
16137 just ignore it. */
16138 line_ptr = extended_end;
16139 break;
16140 default:
16141 complaint (&symfile_complaints,
16142 _("mangled .debug_line section"));
16143 return;
16144 }
16145 /* Make sure that we parsed the extended op correctly. If e.g.
16146 we expected a different address size than the producer used,
16147 we may have read the wrong number of bytes. */
16148 if (line_ptr != extended_end)
16149 {
16150 complaint (&symfile_complaints,
16151 _("mangled .debug_line section"));
16152 return;
16153 }
16154 break;
16155 case DW_LNS_copy:
16156 if (lh->num_file_names < file || file == 0)
16157 dwarf2_debug_line_missing_file_complaint ();
16158 else
16159 {
16160 lh->file_names[file - 1].included_p = 1;
16161 if (!decode_for_pst_p && is_stmt)
16162 {
16163 if (last_subfile != current_subfile)
16164 {
16165 addr = gdbarch_addr_bits_remove (gdbarch, address);
16166 if (last_subfile)
16167 (*p_record_line) (last_subfile, 0, addr);
16168 last_subfile = current_subfile;
16169 }
16170 addr = gdbarch_addr_bits_remove (gdbarch, address);
16171 (*p_record_line) (current_subfile, line, addr);
16172 }
16173 }
16174 basic_block = 0;
16175 break;
16176 case DW_LNS_advance_pc:
16177 {
16178 CORE_ADDR adjust
16179 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16180
16181 address += (((op_index + adjust)
16182 / lh->maximum_ops_per_instruction)
16183 * lh->minimum_instruction_length);
16184 op_index = ((op_index + adjust)
16185 % lh->maximum_ops_per_instruction);
16186 line_ptr += bytes_read;
16187 }
16188 break;
16189 case DW_LNS_advance_line:
16190 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
16191 line_ptr += bytes_read;
16192 break;
16193 case DW_LNS_set_file:
16194 {
16195 /* The arrays lh->include_dirs and lh->file_names are
16196 0-based, but the directory and file name numbers in
16197 the statement program are 1-based. */
16198 struct file_entry *fe;
16199 const char *dir = NULL;
16200
16201 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16202 line_ptr += bytes_read;
16203 if (lh->num_file_names < file || file == 0)
16204 dwarf2_debug_line_missing_file_complaint ();
16205 else
16206 {
16207 fe = &lh->file_names[file - 1];
16208 if (fe->dir_index)
16209 dir = lh->include_dirs[fe->dir_index - 1];
16210 if (!decode_for_pst_p)
16211 {
16212 last_subfile = current_subfile;
16213 dwarf2_start_subfile (fe->name, dir, comp_dir);
16214 }
16215 }
16216 }
16217 break;
16218 case DW_LNS_set_column:
16219 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16220 line_ptr += bytes_read;
16221 break;
16222 case DW_LNS_negate_stmt:
16223 is_stmt = (!is_stmt);
16224 break;
16225 case DW_LNS_set_basic_block:
16226 basic_block = 1;
16227 break;
16228 /* Add to the address register of the state machine the
16229 address increment value corresponding to special opcode
16230 255. I.e., this value is scaled by the minimum
16231 instruction length since special opcode 255 would have
16232 scaled the increment. */
16233 case DW_LNS_const_add_pc:
16234 {
16235 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
16236
16237 address += (((op_index + adjust)
16238 / lh->maximum_ops_per_instruction)
16239 * lh->minimum_instruction_length);
16240 op_index = ((op_index + adjust)
16241 % lh->maximum_ops_per_instruction);
16242 }
16243 break;
16244 case DW_LNS_fixed_advance_pc:
16245 address += read_2_bytes (abfd, line_ptr);
16246 op_index = 0;
16247 line_ptr += 2;
16248 break;
16249 default:
16250 {
16251 /* Unknown standard opcode, ignore it. */
16252 int i;
16253
16254 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
16255 {
16256 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16257 line_ptr += bytes_read;
16258 }
16259 }
16260 }
16261 }
16262 if (lh->num_file_names < file || file == 0)
16263 dwarf2_debug_line_missing_file_complaint ();
16264 else
16265 {
16266 lh->file_names[file - 1].included_p = 1;
16267 if (!decode_for_pst_p)
16268 {
16269 addr = gdbarch_addr_bits_remove (gdbarch, address);
16270 (*p_record_line) (current_subfile, 0, addr);
16271 }
16272 }
16273 }
16274 }
16275
16276 /* Decode the Line Number Program (LNP) for the given line_header
16277 structure and CU. The actual information extracted and the type
16278 of structures created from the LNP depends on the value of PST.
16279
16280 1. If PST is NULL, then this procedure uses the data from the program
16281 to create all necessary symbol tables, and their linetables.
16282
16283 2. If PST is not NULL, this procedure reads the program to determine
16284 the list of files included by the unit represented by PST, and
16285 builds all the associated partial symbol tables.
16286
16287 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
16288 It is used for relative paths in the line table.
16289 NOTE: When processing partial symtabs (pst != NULL),
16290 comp_dir == pst->dirname.
16291
16292 NOTE: It is important that psymtabs have the same file name (via strcmp)
16293 as the corresponding symtab. Since COMP_DIR is not used in the name of the
16294 symtab we don't use it in the name of the psymtabs we create.
16295 E.g. expand_line_sal requires this when finding psymtabs to expand.
16296 A good testcase for this is mb-inline.exp. */
16297
16298 static void
16299 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
16300 struct dwarf2_cu *cu, struct partial_symtab *pst,
16301 int want_line_info)
16302 {
16303 struct objfile *objfile = cu->objfile;
16304 const int decode_for_pst_p = (pst != NULL);
16305 struct subfile *first_subfile = current_subfile;
16306
16307 if (want_line_info)
16308 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
16309
16310 if (decode_for_pst_p)
16311 {
16312 int file_index;
16313
16314 /* Now that we're done scanning the Line Header Program, we can
16315 create the psymtab of each included file. */
16316 for (file_index = 0; file_index < lh->num_file_names; file_index++)
16317 if (lh->file_names[file_index].included_p == 1)
16318 {
16319 const char *include_name =
16320 psymtab_include_file_name (lh, file_index, pst, comp_dir);
16321 if (include_name != NULL)
16322 dwarf2_create_include_psymtab (include_name, pst, objfile);
16323 }
16324 }
16325 else
16326 {
16327 /* Make sure a symtab is created for every file, even files
16328 which contain only variables (i.e. no code with associated
16329 line numbers). */
16330 int i;
16331
16332 for (i = 0; i < lh->num_file_names; i++)
16333 {
16334 const char *dir = NULL;
16335 struct file_entry *fe;
16336
16337 fe = &lh->file_names[i];
16338 if (fe->dir_index)
16339 dir = lh->include_dirs[fe->dir_index - 1];
16340 dwarf2_start_subfile (fe->name, dir, comp_dir);
16341
16342 /* Skip the main file; we don't need it, and it must be
16343 allocated last, so that it will show up before the
16344 non-primary symtabs in the objfile's symtab list. */
16345 if (current_subfile == first_subfile)
16346 continue;
16347
16348 if (current_subfile->symtab == NULL)
16349 current_subfile->symtab = allocate_symtab (current_subfile->name,
16350 objfile);
16351 fe->symtab = current_subfile->symtab;
16352 }
16353 }
16354 }
16355
16356 /* Start a subfile for DWARF. FILENAME is the name of the file and
16357 DIRNAME the name of the source directory which contains FILENAME
16358 or NULL if not known. COMP_DIR is the compilation directory for the
16359 linetable's compilation unit or NULL if not known.
16360 This routine tries to keep line numbers from identical absolute and
16361 relative file names in a common subfile.
16362
16363 Using the `list' example from the GDB testsuite, which resides in
16364 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
16365 of /srcdir/list0.c yields the following debugging information for list0.c:
16366
16367 DW_AT_name: /srcdir/list0.c
16368 DW_AT_comp_dir: /compdir
16369 files.files[0].name: list0.h
16370 files.files[0].dir: /srcdir
16371 files.files[1].name: list0.c
16372 files.files[1].dir: /srcdir
16373
16374 The line number information for list0.c has to end up in a single
16375 subfile, so that `break /srcdir/list0.c:1' works as expected.
16376 start_subfile will ensure that this happens provided that we pass the
16377 concatenation of files.files[1].dir and files.files[1].name as the
16378 subfile's name. */
16379
16380 static void
16381 dwarf2_start_subfile (const char *filename, const char *dirname,
16382 const char *comp_dir)
16383 {
16384 char *copy = NULL;
16385
16386 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
16387 `start_symtab' will always pass the contents of DW_AT_comp_dir as
16388 second argument to start_subfile. To be consistent, we do the
16389 same here. In order not to lose the line information directory,
16390 we concatenate it to the filename when it makes sense.
16391 Note that the Dwarf3 standard says (speaking of filenames in line
16392 information): ``The directory index is ignored for file names
16393 that represent full path names''. Thus ignoring dirname in the
16394 `else' branch below isn't an issue. */
16395
16396 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
16397 {
16398 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
16399 filename = copy;
16400 }
16401
16402 start_subfile (filename, comp_dir);
16403
16404 if (copy != NULL)
16405 xfree (copy);
16406 }
16407
16408 /* Start a symtab for DWARF.
16409 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
16410
16411 static void
16412 dwarf2_start_symtab (struct dwarf2_cu *cu,
16413 const char *name, const char *comp_dir, CORE_ADDR low_pc)
16414 {
16415 start_symtab (name, comp_dir, low_pc);
16416 record_debugformat ("DWARF 2");
16417 record_producer (cu->producer);
16418
16419 /* We assume that we're processing GCC output. */
16420 processing_gcc_compilation = 2;
16421
16422 cu->processing_has_namespace_info = 0;
16423 }
16424
16425 static void
16426 var_decode_location (struct attribute *attr, struct symbol *sym,
16427 struct dwarf2_cu *cu)
16428 {
16429 struct objfile *objfile = cu->objfile;
16430 struct comp_unit_head *cu_header = &cu->header;
16431
16432 /* NOTE drow/2003-01-30: There used to be a comment and some special
16433 code here to turn a symbol with DW_AT_external and a
16434 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
16435 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
16436 with some versions of binutils) where shared libraries could have
16437 relocations against symbols in their debug information - the
16438 minimal symbol would have the right address, but the debug info
16439 would not. It's no longer necessary, because we will explicitly
16440 apply relocations when we read in the debug information now. */
16441
16442 /* A DW_AT_location attribute with no contents indicates that a
16443 variable has been optimized away. */
16444 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
16445 {
16446 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
16447 return;
16448 }
16449
16450 /* Handle one degenerate form of location expression specially, to
16451 preserve GDB's previous behavior when section offsets are
16452 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
16453 then mark this symbol as LOC_STATIC. */
16454
16455 if (attr_form_is_block (attr)
16456 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
16457 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
16458 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
16459 && (DW_BLOCK (attr)->size
16460 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
16461 {
16462 unsigned int dummy;
16463
16464 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
16465 SYMBOL_VALUE_ADDRESS (sym) =
16466 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
16467 else
16468 SYMBOL_VALUE_ADDRESS (sym) =
16469 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
16470 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
16471 fixup_symbol_section (sym, objfile);
16472 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
16473 SYMBOL_SECTION (sym));
16474 return;
16475 }
16476
16477 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
16478 expression evaluator, and use LOC_COMPUTED only when necessary
16479 (i.e. when the value of a register or memory location is
16480 referenced, or a thread-local block, etc.). Then again, it might
16481 not be worthwhile. I'm assuming that it isn't unless performance
16482 or memory numbers show me otherwise. */
16483
16484 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
16485
16486 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
16487 cu->has_loclist = 1;
16488 }
16489
16490 /* Given a pointer to a DWARF information entry, figure out if we need
16491 to make a symbol table entry for it, and if so, create a new entry
16492 and return a pointer to it.
16493 If TYPE is NULL, determine symbol type from the die, otherwise
16494 used the passed type.
16495 If SPACE is not NULL, use it to hold the new symbol. If it is
16496 NULL, allocate a new symbol on the objfile's obstack. */
16497
16498 static struct symbol *
16499 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
16500 struct symbol *space)
16501 {
16502 struct objfile *objfile = cu->objfile;
16503 struct symbol *sym = NULL;
16504 const char *name;
16505 struct attribute *attr = NULL;
16506 struct attribute *attr2 = NULL;
16507 CORE_ADDR baseaddr;
16508 struct pending **list_to_add = NULL;
16509
16510 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
16511
16512 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16513
16514 name = dwarf2_name (die, cu);
16515 if (name)
16516 {
16517 const char *linkagename;
16518 int suppress_add = 0;
16519
16520 if (space)
16521 sym = space;
16522 else
16523 sym = allocate_symbol (objfile);
16524 OBJSTAT (objfile, n_syms++);
16525
16526 /* Cache this symbol's name and the name's demangled form (if any). */
16527 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
16528 linkagename = dwarf2_physname (name, die, cu);
16529 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
16530
16531 /* Fortran does not have mangling standard and the mangling does differ
16532 between gfortran, iFort etc. */
16533 if (cu->language == language_fortran
16534 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
16535 symbol_set_demangled_name (&(sym->ginfo),
16536 dwarf2_full_name (name, die, cu),
16537 NULL);
16538
16539 /* Default assumptions.
16540 Use the passed type or decode it from the die. */
16541 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16542 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
16543 if (type != NULL)
16544 SYMBOL_TYPE (sym) = type;
16545 else
16546 SYMBOL_TYPE (sym) = die_type (die, cu);
16547 attr = dwarf2_attr (die,
16548 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
16549 cu);
16550 if (attr)
16551 {
16552 SYMBOL_LINE (sym) = DW_UNSND (attr);
16553 }
16554
16555 attr = dwarf2_attr (die,
16556 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
16557 cu);
16558 if (attr)
16559 {
16560 int file_index = DW_UNSND (attr);
16561
16562 if (cu->line_header == NULL
16563 || file_index > cu->line_header->num_file_names)
16564 complaint (&symfile_complaints,
16565 _("file index out of range"));
16566 else if (file_index > 0)
16567 {
16568 struct file_entry *fe;
16569
16570 fe = &cu->line_header->file_names[file_index - 1];
16571 SYMBOL_SYMTAB (sym) = fe->symtab;
16572 }
16573 }
16574
16575 switch (die->tag)
16576 {
16577 case DW_TAG_label:
16578 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
16579 if (attr)
16580 {
16581 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
16582 }
16583 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
16584 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
16585 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
16586 add_symbol_to_list (sym, cu->list_in_scope);
16587 break;
16588 case DW_TAG_subprogram:
16589 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16590 finish_block. */
16591 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
16592 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16593 if ((attr2 && (DW_UNSND (attr2) != 0))
16594 || cu->language == language_ada)
16595 {
16596 /* Subprograms marked external are stored as a global symbol.
16597 Ada subprograms, whether marked external or not, are always
16598 stored as a global symbol, because we want to be able to
16599 access them globally. For instance, we want to be able
16600 to break on a nested subprogram without having to
16601 specify the context. */
16602 list_to_add = &global_symbols;
16603 }
16604 else
16605 {
16606 list_to_add = cu->list_in_scope;
16607 }
16608 break;
16609 case DW_TAG_inlined_subroutine:
16610 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16611 finish_block. */
16612 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
16613 SYMBOL_INLINED (sym) = 1;
16614 list_to_add = cu->list_in_scope;
16615 break;
16616 case DW_TAG_template_value_param:
16617 suppress_add = 1;
16618 /* Fall through. */
16619 case DW_TAG_constant:
16620 case DW_TAG_variable:
16621 case DW_TAG_member:
16622 /* Compilation with minimal debug info may result in
16623 variables with missing type entries. Change the
16624 misleading `void' type to something sensible. */
16625 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
16626 SYMBOL_TYPE (sym)
16627 = objfile_type (objfile)->nodebug_data_symbol;
16628
16629 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16630 /* In the case of DW_TAG_member, we should only be called for
16631 static const members. */
16632 if (die->tag == DW_TAG_member)
16633 {
16634 /* dwarf2_add_field uses die_is_declaration,
16635 so we do the same. */
16636 gdb_assert (die_is_declaration (die, cu));
16637 gdb_assert (attr);
16638 }
16639 if (attr)
16640 {
16641 dwarf2_const_value (attr, sym, cu);
16642 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16643 if (!suppress_add)
16644 {
16645 if (attr2 && (DW_UNSND (attr2) != 0))
16646 list_to_add = &global_symbols;
16647 else
16648 list_to_add = cu->list_in_scope;
16649 }
16650 break;
16651 }
16652 attr = dwarf2_attr (die, DW_AT_location, cu);
16653 if (attr)
16654 {
16655 var_decode_location (attr, sym, cu);
16656 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16657
16658 /* Fortran explicitly imports any global symbols to the local
16659 scope by DW_TAG_common_block. */
16660 if (cu->language == language_fortran && die->parent
16661 && die->parent->tag == DW_TAG_common_block)
16662 attr2 = NULL;
16663
16664 if (SYMBOL_CLASS (sym) == LOC_STATIC
16665 && SYMBOL_VALUE_ADDRESS (sym) == 0
16666 && !dwarf2_per_objfile->has_section_at_zero)
16667 {
16668 /* When a static variable is eliminated by the linker,
16669 the corresponding debug information is not stripped
16670 out, but the variable address is set to null;
16671 do not add such variables into symbol table. */
16672 }
16673 else if (attr2 && (DW_UNSND (attr2) != 0))
16674 {
16675 /* Workaround gfortran PR debug/40040 - it uses
16676 DW_AT_location for variables in -fPIC libraries which may
16677 get overriden by other libraries/executable and get
16678 a different address. Resolve it by the minimal symbol
16679 which may come from inferior's executable using copy
16680 relocation. Make this workaround only for gfortran as for
16681 other compilers GDB cannot guess the minimal symbol
16682 Fortran mangling kind. */
16683 if (cu->language == language_fortran && die->parent
16684 && die->parent->tag == DW_TAG_module
16685 && cu->producer
16686 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
16687 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
16688
16689 /* A variable with DW_AT_external is never static,
16690 but it may be block-scoped. */
16691 list_to_add = (cu->list_in_scope == &file_symbols
16692 ? &global_symbols : cu->list_in_scope);
16693 }
16694 else
16695 list_to_add = cu->list_in_scope;
16696 }
16697 else
16698 {
16699 /* We do not know the address of this symbol.
16700 If it is an external symbol and we have type information
16701 for it, enter the symbol as a LOC_UNRESOLVED symbol.
16702 The address of the variable will then be determined from
16703 the minimal symbol table whenever the variable is
16704 referenced. */
16705 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16706
16707 /* Fortran explicitly imports any global symbols to the local
16708 scope by DW_TAG_common_block. */
16709 if (cu->language == language_fortran && die->parent
16710 && die->parent->tag == DW_TAG_common_block)
16711 {
16712 /* SYMBOL_CLASS doesn't matter here because
16713 read_common_block is going to reset it. */
16714 if (!suppress_add)
16715 list_to_add = cu->list_in_scope;
16716 }
16717 else if (attr2 && (DW_UNSND (attr2) != 0)
16718 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
16719 {
16720 /* A variable with DW_AT_external is never static, but it
16721 may be block-scoped. */
16722 list_to_add = (cu->list_in_scope == &file_symbols
16723 ? &global_symbols : cu->list_in_scope);
16724
16725 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
16726 }
16727 else if (!die_is_declaration (die, cu))
16728 {
16729 /* Use the default LOC_OPTIMIZED_OUT class. */
16730 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
16731 if (!suppress_add)
16732 list_to_add = cu->list_in_scope;
16733 }
16734 }
16735 break;
16736 case DW_TAG_formal_parameter:
16737 /* If we are inside a function, mark this as an argument. If
16738 not, we might be looking at an argument to an inlined function
16739 when we do not have enough information to show inlined frames;
16740 pretend it's a local variable in that case so that the user can
16741 still see it. */
16742 if (context_stack_depth > 0
16743 && context_stack[context_stack_depth - 1].name != NULL)
16744 SYMBOL_IS_ARGUMENT (sym) = 1;
16745 attr = dwarf2_attr (die, DW_AT_location, cu);
16746 if (attr)
16747 {
16748 var_decode_location (attr, sym, cu);
16749 }
16750 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16751 if (attr)
16752 {
16753 dwarf2_const_value (attr, sym, cu);
16754 }
16755
16756 list_to_add = cu->list_in_scope;
16757 break;
16758 case DW_TAG_unspecified_parameters:
16759 /* From varargs functions; gdb doesn't seem to have any
16760 interest in this information, so just ignore it for now.
16761 (FIXME?) */
16762 break;
16763 case DW_TAG_template_type_param:
16764 suppress_add = 1;
16765 /* Fall through. */
16766 case DW_TAG_class_type:
16767 case DW_TAG_interface_type:
16768 case DW_TAG_structure_type:
16769 case DW_TAG_union_type:
16770 case DW_TAG_set_type:
16771 case DW_TAG_enumeration_type:
16772 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16773 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
16774
16775 {
16776 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
16777 really ever be static objects: otherwise, if you try
16778 to, say, break of a class's method and you're in a file
16779 which doesn't mention that class, it won't work unless
16780 the check for all static symbols in lookup_symbol_aux
16781 saves you. See the OtherFileClass tests in
16782 gdb.c++/namespace.exp. */
16783
16784 if (!suppress_add)
16785 {
16786 list_to_add = (cu->list_in_scope == &file_symbols
16787 && (cu->language == language_cplus
16788 || cu->language == language_java)
16789 ? &global_symbols : cu->list_in_scope);
16790
16791 /* The semantics of C++ state that "struct foo {
16792 ... }" also defines a typedef for "foo". A Java
16793 class declaration also defines a typedef for the
16794 class. */
16795 if (cu->language == language_cplus
16796 || cu->language == language_java
16797 || cu->language == language_ada)
16798 {
16799 /* The symbol's name is already allocated along
16800 with this objfile, so we don't need to
16801 duplicate it for the type. */
16802 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16803 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16804 }
16805 }
16806 }
16807 break;
16808 case DW_TAG_typedef:
16809 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16810 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16811 list_to_add = cu->list_in_scope;
16812 break;
16813 case DW_TAG_base_type:
16814 case DW_TAG_subrange_type:
16815 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16816 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16817 list_to_add = cu->list_in_scope;
16818 break;
16819 case DW_TAG_enumerator:
16820 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16821 if (attr)
16822 {
16823 dwarf2_const_value (attr, sym, cu);
16824 }
16825 {
16826 /* NOTE: carlton/2003-11-10: See comment above in the
16827 DW_TAG_class_type, etc. block. */
16828
16829 list_to_add = (cu->list_in_scope == &file_symbols
16830 && (cu->language == language_cplus
16831 || cu->language == language_java)
16832 ? &global_symbols : cu->list_in_scope);
16833 }
16834 break;
16835 case DW_TAG_namespace:
16836 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16837 list_to_add = &global_symbols;
16838 break;
16839 case DW_TAG_common_block:
16840 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
16841 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16842 add_symbol_to_list (sym, cu->list_in_scope);
16843 break;
16844 default:
16845 /* Not a tag we recognize. Hopefully we aren't processing
16846 trash data, but since we must specifically ignore things
16847 we don't recognize, there is nothing else we should do at
16848 this point. */
16849 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
16850 dwarf_tag_name (die->tag));
16851 break;
16852 }
16853
16854 if (suppress_add)
16855 {
16856 sym->hash_next = objfile->template_symbols;
16857 objfile->template_symbols = sym;
16858 list_to_add = NULL;
16859 }
16860
16861 if (list_to_add != NULL)
16862 add_symbol_to_list (sym, list_to_add);
16863
16864 /* For the benefit of old versions of GCC, check for anonymous
16865 namespaces based on the demangled name. */
16866 if (!cu->processing_has_namespace_info
16867 && cu->language == language_cplus)
16868 cp_scan_for_anonymous_namespaces (sym, objfile);
16869 }
16870 return (sym);
16871 }
16872
16873 /* A wrapper for new_symbol_full that always allocates a new symbol. */
16874
16875 static struct symbol *
16876 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16877 {
16878 return new_symbol_full (die, type, cu, NULL);
16879 }
16880
16881 /* Given an attr with a DW_FORM_dataN value in host byte order,
16882 zero-extend it as appropriate for the symbol's type. The DWARF
16883 standard (v4) is not entirely clear about the meaning of using
16884 DW_FORM_dataN for a constant with a signed type, where the type is
16885 wider than the data. The conclusion of a discussion on the DWARF
16886 list was that this is unspecified. We choose to always zero-extend
16887 because that is the interpretation long in use by GCC. */
16888
16889 static gdb_byte *
16890 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
16891 struct dwarf2_cu *cu, LONGEST *value, int bits)
16892 {
16893 struct objfile *objfile = cu->objfile;
16894 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16895 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
16896 LONGEST l = DW_UNSND (attr);
16897
16898 if (bits < sizeof (*value) * 8)
16899 {
16900 l &= ((LONGEST) 1 << bits) - 1;
16901 *value = l;
16902 }
16903 else if (bits == sizeof (*value) * 8)
16904 *value = l;
16905 else
16906 {
16907 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16908 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16909 return bytes;
16910 }
16911
16912 return NULL;
16913 }
16914
16915 /* Read a constant value from an attribute. Either set *VALUE, or if
16916 the value does not fit in *VALUE, set *BYTES - either already
16917 allocated on the objfile obstack, or newly allocated on OBSTACK,
16918 or, set *BATON, if we translated the constant to a location
16919 expression. */
16920
16921 static void
16922 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
16923 const char *name, struct obstack *obstack,
16924 struct dwarf2_cu *cu,
16925 LONGEST *value, const gdb_byte **bytes,
16926 struct dwarf2_locexpr_baton **baton)
16927 {
16928 struct objfile *objfile = cu->objfile;
16929 struct comp_unit_head *cu_header = &cu->header;
16930 struct dwarf_block *blk;
16931 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16932 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16933
16934 *value = 0;
16935 *bytes = NULL;
16936 *baton = NULL;
16937
16938 switch (attr->form)
16939 {
16940 case DW_FORM_addr:
16941 case DW_FORM_GNU_addr_index:
16942 {
16943 gdb_byte *data;
16944
16945 if (TYPE_LENGTH (type) != cu_header->addr_size)
16946 dwarf2_const_value_length_mismatch_complaint (name,
16947 cu_header->addr_size,
16948 TYPE_LENGTH (type));
16949 /* Symbols of this form are reasonably rare, so we just
16950 piggyback on the existing location code rather than writing
16951 a new implementation of symbol_computed_ops. */
16952 *baton = obstack_alloc (obstack, sizeof (struct dwarf2_locexpr_baton));
16953 (*baton)->per_cu = cu->per_cu;
16954 gdb_assert ((*baton)->per_cu);
16955
16956 (*baton)->size = 2 + cu_header->addr_size;
16957 data = obstack_alloc (obstack, (*baton)->size);
16958 (*baton)->data = data;
16959
16960 data[0] = DW_OP_addr;
16961 store_unsigned_integer (&data[1], cu_header->addr_size,
16962 byte_order, DW_ADDR (attr));
16963 data[cu_header->addr_size + 1] = DW_OP_stack_value;
16964 }
16965 break;
16966 case DW_FORM_string:
16967 case DW_FORM_strp:
16968 case DW_FORM_GNU_str_index:
16969 case DW_FORM_GNU_strp_alt:
16970 /* DW_STRING is already allocated on the objfile obstack, point
16971 directly to it. */
16972 *bytes = (const gdb_byte *) DW_STRING (attr);
16973 break;
16974 case DW_FORM_block1:
16975 case DW_FORM_block2:
16976 case DW_FORM_block4:
16977 case DW_FORM_block:
16978 case DW_FORM_exprloc:
16979 blk = DW_BLOCK (attr);
16980 if (TYPE_LENGTH (type) != blk->size)
16981 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16982 TYPE_LENGTH (type));
16983 *bytes = blk->data;
16984 break;
16985
16986 /* The DW_AT_const_value attributes are supposed to carry the
16987 symbol's value "represented as it would be on the target
16988 architecture." By the time we get here, it's already been
16989 converted to host endianness, so we just need to sign- or
16990 zero-extend it as appropriate. */
16991 case DW_FORM_data1:
16992 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
16993 break;
16994 case DW_FORM_data2:
16995 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
16996 break;
16997 case DW_FORM_data4:
16998 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
16999 break;
17000 case DW_FORM_data8:
17001 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
17002 break;
17003
17004 case DW_FORM_sdata:
17005 *value = DW_SND (attr);
17006 break;
17007
17008 case DW_FORM_udata:
17009 *value = DW_UNSND (attr);
17010 break;
17011
17012 default:
17013 complaint (&symfile_complaints,
17014 _("unsupported const value attribute form: '%s'"),
17015 dwarf_form_name (attr->form));
17016 *value = 0;
17017 break;
17018 }
17019 }
17020
17021
17022 /* Copy constant value from an attribute to a symbol. */
17023
17024 static void
17025 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
17026 struct dwarf2_cu *cu)
17027 {
17028 struct objfile *objfile = cu->objfile;
17029 struct comp_unit_head *cu_header = &cu->header;
17030 LONGEST value;
17031 const gdb_byte *bytes;
17032 struct dwarf2_locexpr_baton *baton;
17033
17034 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
17035 SYMBOL_PRINT_NAME (sym),
17036 &objfile->objfile_obstack, cu,
17037 &value, &bytes, &baton);
17038
17039 if (baton != NULL)
17040 {
17041 SYMBOL_LOCATION_BATON (sym) = baton;
17042 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
17043 }
17044 else if (bytes != NULL)
17045 {
17046 SYMBOL_VALUE_BYTES (sym) = bytes;
17047 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
17048 }
17049 else
17050 {
17051 SYMBOL_VALUE (sym) = value;
17052 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
17053 }
17054 }
17055
17056 /* Return the type of the die in question using its DW_AT_type attribute. */
17057
17058 static struct type *
17059 die_type (struct die_info *die, struct dwarf2_cu *cu)
17060 {
17061 struct attribute *type_attr;
17062
17063 type_attr = dwarf2_attr (die, DW_AT_type, cu);
17064 if (!type_attr)
17065 {
17066 /* A missing DW_AT_type represents a void type. */
17067 return objfile_type (cu->objfile)->builtin_void;
17068 }
17069
17070 return lookup_die_type (die, type_attr, cu);
17071 }
17072
17073 /* True iff CU's producer generates GNAT Ada auxiliary information
17074 that allows to find parallel types through that information instead
17075 of having to do expensive parallel lookups by type name. */
17076
17077 static int
17078 need_gnat_info (struct dwarf2_cu *cu)
17079 {
17080 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
17081 of GNAT produces this auxiliary information, without any indication
17082 that it is produced. Part of enhancing the FSF version of GNAT
17083 to produce that information will be to put in place an indicator
17084 that we can use in order to determine whether the descriptive type
17085 info is available or not. One suggestion that has been made is
17086 to use a new attribute, attached to the CU die. For now, assume
17087 that the descriptive type info is not available. */
17088 return 0;
17089 }
17090
17091 /* Return the auxiliary type of the die in question using its
17092 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
17093 attribute is not present. */
17094
17095 static struct type *
17096 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
17097 {
17098 struct attribute *type_attr;
17099
17100 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
17101 if (!type_attr)
17102 return NULL;
17103
17104 return lookup_die_type (die, type_attr, cu);
17105 }
17106
17107 /* If DIE has a descriptive_type attribute, then set the TYPE's
17108 descriptive type accordingly. */
17109
17110 static void
17111 set_descriptive_type (struct type *type, struct die_info *die,
17112 struct dwarf2_cu *cu)
17113 {
17114 struct type *descriptive_type = die_descriptive_type (die, cu);
17115
17116 if (descriptive_type)
17117 {
17118 ALLOCATE_GNAT_AUX_TYPE (type);
17119 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
17120 }
17121 }
17122
17123 /* Return the containing type of the die in question using its
17124 DW_AT_containing_type attribute. */
17125
17126 static struct type *
17127 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
17128 {
17129 struct attribute *type_attr;
17130
17131 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
17132 if (!type_attr)
17133 error (_("Dwarf Error: Problem turning containing type into gdb type "
17134 "[in module %s]"), objfile_name (cu->objfile));
17135
17136 return lookup_die_type (die, type_attr, cu);
17137 }
17138
17139 /* Return an error marker type to use for the ill formed type in DIE/CU. */
17140
17141 static struct type *
17142 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
17143 {
17144 struct objfile *objfile = dwarf2_per_objfile->objfile;
17145 char *message, *saved;
17146
17147 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
17148 objfile_name (objfile),
17149 cu->header.offset.sect_off,
17150 die->offset.sect_off);
17151 saved = obstack_copy0 (&objfile->objfile_obstack,
17152 message, strlen (message));
17153 xfree (message);
17154
17155 return init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
17156 }
17157
17158 /* Look up the type of DIE in CU using its type attribute ATTR.
17159 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
17160 DW_AT_containing_type.
17161 If there is no type substitute an error marker. */
17162
17163 static struct type *
17164 lookup_die_type (struct die_info *die, const struct attribute *attr,
17165 struct dwarf2_cu *cu)
17166 {
17167 struct objfile *objfile = cu->objfile;
17168 struct type *this_type;
17169
17170 gdb_assert (attr->name == DW_AT_type
17171 || attr->name == DW_AT_GNAT_descriptive_type
17172 || attr->name == DW_AT_containing_type);
17173
17174 /* First see if we have it cached. */
17175
17176 if (attr->form == DW_FORM_GNU_ref_alt)
17177 {
17178 struct dwarf2_per_cu_data *per_cu;
17179 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17180
17181 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
17182 this_type = get_die_type_at_offset (offset, per_cu);
17183 }
17184 else if (attr_form_is_ref (attr))
17185 {
17186 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17187
17188 this_type = get_die_type_at_offset (offset, cu->per_cu);
17189 }
17190 else if (attr->form == DW_FORM_ref_sig8)
17191 {
17192 ULONGEST signature = DW_SIGNATURE (attr);
17193
17194 return get_signatured_type (die, signature, cu);
17195 }
17196 else
17197 {
17198 complaint (&symfile_complaints,
17199 _("Dwarf Error: Bad type attribute %s in DIE"
17200 " at 0x%x [in module %s]"),
17201 dwarf_attr_name (attr->name), die->offset.sect_off,
17202 objfile_name (objfile));
17203 return build_error_marker_type (cu, die);
17204 }
17205
17206 /* If not cached we need to read it in. */
17207
17208 if (this_type == NULL)
17209 {
17210 struct die_info *type_die = NULL;
17211 struct dwarf2_cu *type_cu = cu;
17212
17213 if (attr_form_is_ref (attr))
17214 type_die = follow_die_ref (die, attr, &type_cu);
17215 if (type_die == NULL)
17216 return build_error_marker_type (cu, die);
17217 /* If we find the type now, it's probably because the type came
17218 from an inter-CU reference and the type's CU got expanded before
17219 ours. */
17220 this_type = read_type_die (type_die, type_cu);
17221 }
17222
17223 /* If we still don't have a type use an error marker. */
17224
17225 if (this_type == NULL)
17226 return build_error_marker_type (cu, die);
17227
17228 return this_type;
17229 }
17230
17231 /* Return the type in DIE, CU.
17232 Returns NULL for invalid types.
17233
17234 This first does a lookup in die_type_hash,
17235 and only reads the die in if necessary.
17236
17237 NOTE: This can be called when reading in partial or full symbols. */
17238
17239 static struct type *
17240 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
17241 {
17242 struct type *this_type;
17243
17244 this_type = get_die_type (die, cu);
17245 if (this_type)
17246 return this_type;
17247
17248 return read_type_die_1 (die, cu);
17249 }
17250
17251 /* Read the type in DIE, CU.
17252 Returns NULL for invalid types. */
17253
17254 static struct type *
17255 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
17256 {
17257 struct type *this_type = NULL;
17258
17259 switch (die->tag)
17260 {
17261 case DW_TAG_class_type:
17262 case DW_TAG_interface_type:
17263 case DW_TAG_structure_type:
17264 case DW_TAG_union_type:
17265 this_type = read_structure_type (die, cu);
17266 break;
17267 case DW_TAG_enumeration_type:
17268 this_type = read_enumeration_type (die, cu);
17269 break;
17270 case DW_TAG_subprogram:
17271 case DW_TAG_subroutine_type:
17272 case DW_TAG_inlined_subroutine:
17273 this_type = read_subroutine_type (die, cu);
17274 break;
17275 case DW_TAG_array_type:
17276 this_type = read_array_type (die, cu);
17277 break;
17278 case DW_TAG_set_type:
17279 this_type = read_set_type (die, cu);
17280 break;
17281 case DW_TAG_pointer_type:
17282 this_type = read_tag_pointer_type (die, cu);
17283 break;
17284 case DW_TAG_ptr_to_member_type:
17285 this_type = read_tag_ptr_to_member_type (die, cu);
17286 break;
17287 case DW_TAG_reference_type:
17288 this_type = read_tag_reference_type (die, cu);
17289 break;
17290 case DW_TAG_const_type:
17291 this_type = read_tag_const_type (die, cu);
17292 break;
17293 case DW_TAG_volatile_type:
17294 this_type = read_tag_volatile_type (die, cu);
17295 break;
17296 case DW_TAG_restrict_type:
17297 this_type = read_tag_restrict_type (die, cu);
17298 break;
17299 case DW_TAG_string_type:
17300 this_type = read_tag_string_type (die, cu);
17301 break;
17302 case DW_TAG_typedef:
17303 this_type = read_typedef (die, cu);
17304 break;
17305 case DW_TAG_subrange_type:
17306 this_type = read_subrange_type (die, cu);
17307 break;
17308 case DW_TAG_base_type:
17309 this_type = read_base_type (die, cu);
17310 break;
17311 case DW_TAG_unspecified_type:
17312 this_type = read_unspecified_type (die, cu);
17313 break;
17314 case DW_TAG_namespace:
17315 this_type = read_namespace_type (die, cu);
17316 break;
17317 case DW_TAG_module:
17318 this_type = read_module_type (die, cu);
17319 break;
17320 default:
17321 complaint (&symfile_complaints,
17322 _("unexpected tag in read_type_die: '%s'"),
17323 dwarf_tag_name (die->tag));
17324 break;
17325 }
17326
17327 return this_type;
17328 }
17329
17330 /* See if we can figure out if the class lives in a namespace. We do
17331 this by looking for a member function; its demangled name will
17332 contain namespace info, if there is any.
17333 Return the computed name or NULL.
17334 Space for the result is allocated on the objfile's obstack.
17335 This is the full-die version of guess_partial_die_structure_name.
17336 In this case we know DIE has no useful parent. */
17337
17338 static char *
17339 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
17340 {
17341 struct die_info *spec_die;
17342 struct dwarf2_cu *spec_cu;
17343 struct die_info *child;
17344
17345 spec_cu = cu;
17346 spec_die = die_specification (die, &spec_cu);
17347 if (spec_die != NULL)
17348 {
17349 die = spec_die;
17350 cu = spec_cu;
17351 }
17352
17353 for (child = die->child;
17354 child != NULL;
17355 child = child->sibling)
17356 {
17357 if (child->tag == DW_TAG_subprogram)
17358 {
17359 struct attribute *attr;
17360
17361 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
17362 if (attr == NULL)
17363 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
17364 if (attr != NULL)
17365 {
17366 char *actual_name
17367 = language_class_name_from_physname (cu->language_defn,
17368 DW_STRING (attr));
17369 char *name = NULL;
17370
17371 if (actual_name != NULL)
17372 {
17373 const char *die_name = dwarf2_name (die, cu);
17374
17375 if (die_name != NULL
17376 && strcmp (die_name, actual_name) != 0)
17377 {
17378 /* Strip off the class name from the full name.
17379 We want the prefix. */
17380 int die_name_len = strlen (die_name);
17381 int actual_name_len = strlen (actual_name);
17382
17383 /* Test for '::' as a sanity check. */
17384 if (actual_name_len > die_name_len + 2
17385 && actual_name[actual_name_len
17386 - die_name_len - 1] == ':')
17387 name =
17388 obstack_copy0 (&cu->objfile->objfile_obstack,
17389 actual_name,
17390 actual_name_len - die_name_len - 2);
17391 }
17392 }
17393 xfree (actual_name);
17394 return name;
17395 }
17396 }
17397 }
17398
17399 return NULL;
17400 }
17401
17402 /* GCC might emit a nameless typedef that has a linkage name. Determine the
17403 prefix part in such case. See
17404 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17405
17406 static char *
17407 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
17408 {
17409 struct attribute *attr;
17410 char *base;
17411
17412 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
17413 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
17414 return NULL;
17415
17416 attr = dwarf2_attr (die, DW_AT_name, cu);
17417 if (attr != NULL && DW_STRING (attr) != NULL)
17418 return NULL;
17419
17420 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17421 if (attr == NULL)
17422 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17423 if (attr == NULL || DW_STRING (attr) == NULL)
17424 return NULL;
17425
17426 /* dwarf2_name had to be already called. */
17427 gdb_assert (DW_STRING_IS_CANONICAL (attr));
17428
17429 /* Strip the base name, keep any leading namespaces/classes. */
17430 base = strrchr (DW_STRING (attr), ':');
17431 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
17432 return "";
17433
17434 return obstack_copy0 (&cu->objfile->objfile_obstack,
17435 DW_STRING (attr), &base[-1] - DW_STRING (attr));
17436 }
17437
17438 /* Return the name of the namespace/class that DIE is defined within,
17439 or "" if we can't tell. The caller should not xfree the result.
17440
17441 For example, if we're within the method foo() in the following
17442 code:
17443
17444 namespace N {
17445 class C {
17446 void foo () {
17447 }
17448 };
17449 }
17450
17451 then determine_prefix on foo's die will return "N::C". */
17452
17453 static const char *
17454 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
17455 {
17456 struct die_info *parent, *spec_die;
17457 struct dwarf2_cu *spec_cu;
17458 struct type *parent_type;
17459 char *retval;
17460
17461 if (cu->language != language_cplus && cu->language != language_java
17462 && cu->language != language_fortran)
17463 return "";
17464
17465 retval = anonymous_struct_prefix (die, cu);
17466 if (retval)
17467 return retval;
17468
17469 /* We have to be careful in the presence of DW_AT_specification.
17470 For example, with GCC 3.4, given the code
17471
17472 namespace N {
17473 void foo() {
17474 // Definition of N::foo.
17475 }
17476 }
17477
17478 then we'll have a tree of DIEs like this:
17479
17480 1: DW_TAG_compile_unit
17481 2: DW_TAG_namespace // N
17482 3: DW_TAG_subprogram // declaration of N::foo
17483 4: DW_TAG_subprogram // definition of N::foo
17484 DW_AT_specification // refers to die #3
17485
17486 Thus, when processing die #4, we have to pretend that we're in
17487 the context of its DW_AT_specification, namely the contex of die
17488 #3. */
17489 spec_cu = cu;
17490 spec_die = die_specification (die, &spec_cu);
17491 if (spec_die == NULL)
17492 parent = die->parent;
17493 else
17494 {
17495 parent = spec_die->parent;
17496 cu = spec_cu;
17497 }
17498
17499 if (parent == NULL)
17500 return "";
17501 else if (parent->building_fullname)
17502 {
17503 const char *name;
17504 const char *parent_name;
17505
17506 /* It has been seen on RealView 2.2 built binaries,
17507 DW_TAG_template_type_param types actually _defined_ as
17508 children of the parent class:
17509
17510 enum E {};
17511 template class <class Enum> Class{};
17512 Class<enum E> class_e;
17513
17514 1: DW_TAG_class_type (Class)
17515 2: DW_TAG_enumeration_type (E)
17516 3: DW_TAG_enumerator (enum1:0)
17517 3: DW_TAG_enumerator (enum2:1)
17518 ...
17519 2: DW_TAG_template_type_param
17520 DW_AT_type DW_FORM_ref_udata (E)
17521
17522 Besides being broken debug info, it can put GDB into an
17523 infinite loop. Consider:
17524
17525 When we're building the full name for Class<E>, we'll start
17526 at Class, and go look over its template type parameters,
17527 finding E. We'll then try to build the full name of E, and
17528 reach here. We're now trying to build the full name of E,
17529 and look over the parent DIE for containing scope. In the
17530 broken case, if we followed the parent DIE of E, we'd again
17531 find Class, and once again go look at its template type
17532 arguments, etc., etc. Simply don't consider such parent die
17533 as source-level parent of this die (it can't be, the language
17534 doesn't allow it), and break the loop here. */
17535 name = dwarf2_name (die, cu);
17536 parent_name = dwarf2_name (parent, cu);
17537 complaint (&symfile_complaints,
17538 _("template param type '%s' defined within parent '%s'"),
17539 name ? name : "<unknown>",
17540 parent_name ? parent_name : "<unknown>");
17541 return "";
17542 }
17543 else
17544 switch (parent->tag)
17545 {
17546 case DW_TAG_namespace:
17547 parent_type = read_type_die (parent, cu);
17548 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
17549 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
17550 Work around this problem here. */
17551 if (cu->language == language_cplus
17552 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
17553 return "";
17554 /* We give a name to even anonymous namespaces. */
17555 return TYPE_TAG_NAME (parent_type);
17556 case DW_TAG_class_type:
17557 case DW_TAG_interface_type:
17558 case DW_TAG_structure_type:
17559 case DW_TAG_union_type:
17560 case DW_TAG_module:
17561 parent_type = read_type_die (parent, cu);
17562 if (TYPE_TAG_NAME (parent_type) != NULL)
17563 return TYPE_TAG_NAME (parent_type);
17564 else
17565 /* An anonymous structure is only allowed non-static data
17566 members; no typedefs, no member functions, et cetera.
17567 So it does not need a prefix. */
17568 return "";
17569 case DW_TAG_compile_unit:
17570 case DW_TAG_partial_unit:
17571 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
17572 if (cu->language == language_cplus
17573 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
17574 && die->child != NULL
17575 && (die->tag == DW_TAG_class_type
17576 || die->tag == DW_TAG_structure_type
17577 || die->tag == DW_TAG_union_type))
17578 {
17579 char *name = guess_full_die_structure_name (die, cu);
17580 if (name != NULL)
17581 return name;
17582 }
17583 return "";
17584 default:
17585 return determine_prefix (parent, cu);
17586 }
17587 }
17588
17589 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
17590 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
17591 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
17592 an obconcat, otherwise allocate storage for the result. The CU argument is
17593 used to determine the language and hence, the appropriate separator. */
17594
17595 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
17596
17597 static char *
17598 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
17599 int physname, struct dwarf2_cu *cu)
17600 {
17601 const char *lead = "";
17602 const char *sep;
17603
17604 if (suffix == NULL || suffix[0] == '\0'
17605 || prefix == NULL || prefix[0] == '\0')
17606 sep = "";
17607 else if (cu->language == language_java)
17608 sep = ".";
17609 else if (cu->language == language_fortran && physname)
17610 {
17611 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
17612 DW_AT_MIPS_linkage_name is preferred and used instead. */
17613
17614 lead = "__";
17615 sep = "_MOD_";
17616 }
17617 else
17618 sep = "::";
17619
17620 if (prefix == NULL)
17621 prefix = "";
17622 if (suffix == NULL)
17623 suffix = "";
17624
17625 if (obs == NULL)
17626 {
17627 char *retval
17628 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
17629
17630 strcpy (retval, lead);
17631 strcat (retval, prefix);
17632 strcat (retval, sep);
17633 strcat (retval, suffix);
17634 return retval;
17635 }
17636 else
17637 {
17638 /* We have an obstack. */
17639 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
17640 }
17641 }
17642
17643 /* Return sibling of die, NULL if no sibling. */
17644
17645 static struct die_info *
17646 sibling_die (struct die_info *die)
17647 {
17648 return die->sibling;
17649 }
17650
17651 /* Get name of a die, return NULL if not found. */
17652
17653 static const char *
17654 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
17655 struct obstack *obstack)
17656 {
17657 if (name && cu->language == language_cplus)
17658 {
17659 char *canon_name = cp_canonicalize_string (name);
17660
17661 if (canon_name != NULL)
17662 {
17663 if (strcmp (canon_name, name) != 0)
17664 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
17665 xfree (canon_name);
17666 }
17667 }
17668
17669 return name;
17670 }
17671
17672 /* Get name of a die, return NULL if not found. */
17673
17674 static const char *
17675 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
17676 {
17677 struct attribute *attr;
17678
17679 attr = dwarf2_attr (die, DW_AT_name, cu);
17680 if ((!attr || !DW_STRING (attr))
17681 && die->tag != DW_TAG_class_type
17682 && die->tag != DW_TAG_interface_type
17683 && die->tag != DW_TAG_structure_type
17684 && die->tag != DW_TAG_union_type)
17685 return NULL;
17686
17687 switch (die->tag)
17688 {
17689 case DW_TAG_compile_unit:
17690 case DW_TAG_partial_unit:
17691 /* Compilation units have a DW_AT_name that is a filename, not
17692 a source language identifier. */
17693 case DW_TAG_enumeration_type:
17694 case DW_TAG_enumerator:
17695 /* These tags always have simple identifiers already; no need
17696 to canonicalize them. */
17697 return DW_STRING (attr);
17698
17699 case DW_TAG_subprogram:
17700 /* Java constructors will all be named "<init>", so return
17701 the class name when we see this special case. */
17702 if (cu->language == language_java
17703 && DW_STRING (attr) != NULL
17704 && strcmp (DW_STRING (attr), "<init>") == 0)
17705 {
17706 struct dwarf2_cu *spec_cu = cu;
17707 struct die_info *spec_die;
17708
17709 /* GCJ will output '<init>' for Java constructor names.
17710 For this special case, return the name of the parent class. */
17711
17712 /* GCJ may output suprogram DIEs with AT_specification set.
17713 If so, use the name of the specified DIE. */
17714 spec_die = die_specification (die, &spec_cu);
17715 if (spec_die != NULL)
17716 return dwarf2_name (spec_die, spec_cu);
17717
17718 do
17719 {
17720 die = die->parent;
17721 if (die->tag == DW_TAG_class_type)
17722 return dwarf2_name (die, cu);
17723 }
17724 while (die->tag != DW_TAG_compile_unit
17725 && die->tag != DW_TAG_partial_unit);
17726 }
17727 break;
17728
17729 case DW_TAG_class_type:
17730 case DW_TAG_interface_type:
17731 case DW_TAG_structure_type:
17732 case DW_TAG_union_type:
17733 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17734 structures or unions. These were of the form "._%d" in GCC 4.1,
17735 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17736 and GCC 4.4. We work around this problem by ignoring these. */
17737 if (attr && DW_STRING (attr)
17738 && (strncmp (DW_STRING (attr), "._", 2) == 0
17739 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
17740 return NULL;
17741
17742 /* GCC might emit a nameless typedef that has a linkage name. See
17743 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17744 if (!attr || DW_STRING (attr) == NULL)
17745 {
17746 char *demangled = NULL;
17747
17748 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17749 if (attr == NULL)
17750 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17751
17752 if (attr == NULL || DW_STRING (attr) == NULL)
17753 return NULL;
17754
17755 /* Avoid demangling DW_STRING (attr) the second time on a second
17756 call for the same DIE. */
17757 if (!DW_STRING_IS_CANONICAL (attr))
17758 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
17759
17760 if (demangled)
17761 {
17762 char *base;
17763
17764 /* FIXME: we already did this for the partial symbol... */
17765 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17766 demangled, strlen (demangled));
17767 DW_STRING_IS_CANONICAL (attr) = 1;
17768 xfree (demangled);
17769
17770 /* Strip any leading namespaces/classes, keep only the base name.
17771 DW_AT_name for named DIEs does not contain the prefixes. */
17772 base = strrchr (DW_STRING (attr), ':');
17773 if (base && base > DW_STRING (attr) && base[-1] == ':')
17774 return &base[1];
17775 else
17776 return DW_STRING (attr);
17777 }
17778 }
17779 break;
17780
17781 default:
17782 break;
17783 }
17784
17785 if (!DW_STRING_IS_CANONICAL (attr))
17786 {
17787 DW_STRING (attr)
17788 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17789 &cu->objfile->objfile_obstack);
17790 DW_STRING_IS_CANONICAL (attr) = 1;
17791 }
17792 return DW_STRING (attr);
17793 }
17794
17795 /* Return the die that this die in an extension of, or NULL if there
17796 is none. *EXT_CU is the CU containing DIE on input, and the CU
17797 containing the return value on output. */
17798
17799 static struct die_info *
17800 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
17801 {
17802 struct attribute *attr;
17803
17804 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
17805 if (attr == NULL)
17806 return NULL;
17807
17808 return follow_die_ref (die, attr, ext_cu);
17809 }
17810
17811 /* Convert a DIE tag into its string name. */
17812
17813 static const char *
17814 dwarf_tag_name (unsigned tag)
17815 {
17816 const char *name = get_DW_TAG_name (tag);
17817
17818 if (name == NULL)
17819 return "DW_TAG_<unknown>";
17820
17821 return name;
17822 }
17823
17824 /* Convert a DWARF attribute code into its string name. */
17825
17826 static const char *
17827 dwarf_attr_name (unsigned attr)
17828 {
17829 const char *name;
17830
17831 #ifdef MIPS /* collides with DW_AT_HP_block_index */
17832 if (attr == DW_AT_MIPS_fde)
17833 return "DW_AT_MIPS_fde";
17834 #else
17835 if (attr == DW_AT_HP_block_index)
17836 return "DW_AT_HP_block_index";
17837 #endif
17838
17839 name = get_DW_AT_name (attr);
17840
17841 if (name == NULL)
17842 return "DW_AT_<unknown>";
17843
17844 return name;
17845 }
17846
17847 /* Convert a DWARF value form code into its string name. */
17848
17849 static const char *
17850 dwarf_form_name (unsigned form)
17851 {
17852 const char *name = get_DW_FORM_name (form);
17853
17854 if (name == NULL)
17855 return "DW_FORM_<unknown>";
17856
17857 return name;
17858 }
17859
17860 static char *
17861 dwarf_bool_name (unsigned mybool)
17862 {
17863 if (mybool)
17864 return "TRUE";
17865 else
17866 return "FALSE";
17867 }
17868
17869 /* Convert a DWARF type code into its string name. */
17870
17871 static const char *
17872 dwarf_type_encoding_name (unsigned enc)
17873 {
17874 const char *name = get_DW_ATE_name (enc);
17875
17876 if (name == NULL)
17877 return "DW_ATE_<unknown>";
17878
17879 return name;
17880 }
17881
17882 static void
17883 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
17884 {
17885 unsigned int i;
17886
17887 print_spaces (indent, f);
17888 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
17889 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
17890
17891 if (die->parent != NULL)
17892 {
17893 print_spaces (indent, f);
17894 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
17895 die->parent->offset.sect_off);
17896 }
17897
17898 print_spaces (indent, f);
17899 fprintf_unfiltered (f, " has children: %s\n",
17900 dwarf_bool_name (die->child != NULL));
17901
17902 print_spaces (indent, f);
17903 fprintf_unfiltered (f, " attributes:\n");
17904
17905 for (i = 0; i < die->num_attrs; ++i)
17906 {
17907 print_spaces (indent, f);
17908 fprintf_unfiltered (f, " %s (%s) ",
17909 dwarf_attr_name (die->attrs[i].name),
17910 dwarf_form_name (die->attrs[i].form));
17911
17912 switch (die->attrs[i].form)
17913 {
17914 case DW_FORM_addr:
17915 case DW_FORM_GNU_addr_index:
17916 fprintf_unfiltered (f, "address: ");
17917 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
17918 break;
17919 case DW_FORM_block2:
17920 case DW_FORM_block4:
17921 case DW_FORM_block:
17922 case DW_FORM_block1:
17923 fprintf_unfiltered (f, "block: size %s",
17924 pulongest (DW_BLOCK (&die->attrs[i])->size));
17925 break;
17926 case DW_FORM_exprloc:
17927 fprintf_unfiltered (f, "expression: size %s",
17928 pulongest (DW_BLOCK (&die->attrs[i])->size));
17929 break;
17930 case DW_FORM_ref_addr:
17931 fprintf_unfiltered (f, "ref address: ");
17932 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17933 break;
17934 case DW_FORM_GNU_ref_alt:
17935 fprintf_unfiltered (f, "alt ref address: ");
17936 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17937 break;
17938 case DW_FORM_ref1:
17939 case DW_FORM_ref2:
17940 case DW_FORM_ref4:
17941 case DW_FORM_ref8:
17942 case DW_FORM_ref_udata:
17943 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
17944 (long) (DW_UNSND (&die->attrs[i])));
17945 break;
17946 case DW_FORM_data1:
17947 case DW_FORM_data2:
17948 case DW_FORM_data4:
17949 case DW_FORM_data8:
17950 case DW_FORM_udata:
17951 case DW_FORM_sdata:
17952 fprintf_unfiltered (f, "constant: %s",
17953 pulongest (DW_UNSND (&die->attrs[i])));
17954 break;
17955 case DW_FORM_sec_offset:
17956 fprintf_unfiltered (f, "section offset: %s",
17957 pulongest (DW_UNSND (&die->attrs[i])));
17958 break;
17959 case DW_FORM_ref_sig8:
17960 fprintf_unfiltered (f, "signature: %s",
17961 hex_string (DW_SIGNATURE (&die->attrs[i])));
17962 break;
17963 case DW_FORM_string:
17964 case DW_FORM_strp:
17965 case DW_FORM_GNU_str_index:
17966 case DW_FORM_GNU_strp_alt:
17967 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
17968 DW_STRING (&die->attrs[i])
17969 ? DW_STRING (&die->attrs[i]) : "",
17970 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
17971 break;
17972 case DW_FORM_flag:
17973 if (DW_UNSND (&die->attrs[i]))
17974 fprintf_unfiltered (f, "flag: TRUE");
17975 else
17976 fprintf_unfiltered (f, "flag: FALSE");
17977 break;
17978 case DW_FORM_flag_present:
17979 fprintf_unfiltered (f, "flag: TRUE");
17980 break;
17981 case DW_FORM_indirect:
17982 /* The reader will have reduced the indirect form to
17983 the "base form" so this form should not occur. */
17984 fprintf_unfiltered (f,
17985 "unexpected attribute form: DW_FORM_indirect");
17986 break;
17987 default:
17988 fprintf_unfiltered (f, "unsupported attribute form: %d.",
17989 die->attrs[i].form);
17990 break;
17991 }
17992 fprintf_unfiltered (f, "\n");
17993 }
17994 }
17995
17996 static void
17997 dump_die_for_error (struct die_info *die)
17998 {
17999 dump_die_shallow (gdb_stderr, 0, die);
18000 }
18001
18002 static void
18003 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
18004 {
18005 int indent = level * 4;
18006
18007 gdb_assert (die != NULL);
18008
18009 if (level >= max_level)
18010 return;
18011
18012 dump_die_shallow (f, indent, die);
18013
18014 if (die->child != NULL)
18015 {
18016 print_spaces (indent, f);
18017 fprintf_unfiltered (f, " Children:");
18018 if (level + 1 < max_level)
18019 {
18020 fprintf_unfiltered (f, "\n");
18021 dump_die_1 (f, level + 1, max_level, die->child);
18022 }
18023 else
18024 {
18025 fprintf_unfiltered (f,
18026 " [not printed, max nesting level reached]\n");
18027 }
18028 }
18029
18030 if (die->sibling != NULL && level > 0)
18031 {
18032 dump_die_1 (f, level, max_level, die->sibling);
18033 }
18034 }
18035
18036 /* This is called from the pdie macro in gdbinit.in.
18037 It's not static so gcc will keep a copy callable from gdb. */
18038
18039 void
18040 dump_die (struct die_info *die, int max_level)
18041 {
18042 dump_die_1 (gdb_stdlog, 0, max_level, die);
18043 }
18044
18045 static void
18046 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
18047 {
18048 void **slot;
18049
18050 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
18051 INSERT);
18052
18053 *slot = die;
18054 }
18055
18056 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
18057 required kind. */
18058
18059 static sect_offset
18060 dwarf2_get_ref_die_offset (const struct attribute *attr)
18061 {
18062 sect_offset retval = { DW_UNSND (attr) };
18063
18064 if (attr_form_is_ref (attr))
18065 return retval;
18066
18067 retval.sect_off = 0;
18068 complaint (&symfile_complaints,
18069 _("unsupported die ref attribute form: '%s'"),
18070 dwarf_form_name (attr->form));
18071 return retval;
18072 }
18073
18074 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
18075 * the value held by the attribute is not constant. */
18076
18077 static LONGEST
18078 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
18079 {
18080 if (attr->form == DW_FORM_sdata)
18081 return DW_SND (attr);
18082 else if (attr->form == DW_FORM_udata
18083 || attr->form == DW_FORM_data1
18084 || attr->form == DW_FORM_data2
18085 || attr->form == DW_FORM_data4
18086 || attr->form == DW_FORM_data8)
18087 return DW_UNSND (attr);
18088 else
18089 {
18090 complaint (&symfile_complaints,
18091 _("Attribute value is not a constant (%s)"),
18092 dwarf_form_name (attr->form));
18093 return default_value;
18094 }
18095 }
18096
18097 /* Follow reference or signature attribute ATTR of SRC_DIE.
18098 On entry *REF_CU is the CU of SRC_DIE.
18099 On exit *REF_CU is the CU of the result. */
18100
18101 static struct die_info *
18102 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
18103 struct dwarf2_cu **ref_cu)
18104 {
18105 struct die_info *die;
18106
18107 if (attr_form_is_ref (attr))
18108 die = follow_die_ref (src_die, attr, ref_cu);
18109 else if (attr->form == DW_FORM_ref_sig8)
18110 die = follow_die_sig (src_die, attr, ref_cu);
18111 else
18112 {
18113 dump_die_for_error (src_die);
18114 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
18115 objfile_name ((*ref_cu)->objfile));
18116 }
18117
18118 return die;
18119 }
18120
18121 /* Follow reference OFFSET.
18122 On entry *REF_CU is the CU of the source die referencing OFFSET.
18123 On exit *REF_CU is the CU of the result.
18124 Returns NULL if OFFSET is invalid. */
18125
18126 static struct die_info *
18127 follow_die_offset (sect_offset offset, int offset_in_dwz,
18128 struct dwarf2_cu **ref_cu)
18129 {
18130 struct die_info temp_die;
18131 struct dwarf2_cu *target_cu, *cu = *ref_cu;
18132
18133 gdb_assert (cu->per_cu != NULL);
18134
18135 target_cu = cu;
18136
18137 if (cu->per_cu->is_debug_types)
18138 {
18139 /* .debug_types CUs cannot reference anything outside their CU.
18140 If they need to, they have to reference a signatured type via
18141 DW_FORM_ref_sig8. */
18142 if (! offset_in_cu_p (&cu->header, offset))
18143 return NULL;
18144 }
18145 else if (offset_in_dwz != cu->per_cu->is_dwz
18146 || ! offset_in_cu_p (&cu->header, offset))
18147 {
18148 struct dwarf2_per_cu_data *per_cu;
18149
18150 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
18151 cu->objfile);
18152
18153 /* If necessary, add it to the queue and load its DIEs. */
18154 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
18155 load_full_comp_unit (per_cu, cu->language);
18156
18157 target_cu = per_cu->cu;
18158 }
18159 else if (cu->dies == NULL)
18160 {
18161 /* We're loading full DIEs during partial symbol reading. */
18162 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
18163 load_full_comp_unit (cu->per_cu, language_minimal);
18164 }
18165
18166 *ref_cu = target_cu;
18167 temp_die.offset = offset;
18168 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
18169 }
18170
18171 /* Follow reference attribute ATTR of SRC_DIE.
18172 On entry *REF_CU is the CU of SRC_DIE.
18173 On exit *REF_CU is the CU of the result. */
18174
18175 static struct die_info *
18176 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
18177 struct dwarf2_cu **ref_cu)
18178 {
18179 sect_offset offset = dwarf2_get_ref_die_offset (attr);
18180 struct dwarf2_cu *cu = *ref_cu;
18181 struct die_info *die;
18182
18183 die = follow_die_offset (offset,
18184 (attr->form == DW_FORM_GNU_ref_alt
18185 || cu->per_cu->is_dwz),
18186 ref_cu);
18187 if (!die)
18188 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
18189 "at 0x%x [in module %s]"),
18190 offset.sect_off, src_die->offset.sect_off,
18191 objfile_name (cu->objfile));
18192
18193 return die;
18194 }
18195
18196 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
18197 Returned value is intended for DW_OP_call*. Returned
18198 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
18199
18200 struct dwarf2_locexpr_baton
18201 dwarf2_fetch_die_loc_sect_off (sect_offset offset,
18202 struct dwarf2_per_cu_data *per_cu,
18203 CORE_ADDR (*get_frame_pc) (void *baton),
18204 void *baton)
18205 {
18206 struct dwarf2_cu *cu;
18207 struct die_info *die;
18208 struct attribute *attr;
18209 struct dwarf2_locexpr_baton retval;
18210
18211 dw2_setup (per_cu->objfile);
18212
18213 if (per_cu->cu == NULL)
18214 load_cu (per_cu);
18215 cu = per_cu->cu;
18216
18217 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
18218 if (!die)
18219 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
18220 offset.sect_off, objfile_name (per_cu->objfile));
18221
18222 attr = dwarf2_attr (die, DW_AT_location, cu);
18223 if (!attr)
18224 {
18225 /* DWARF: "If there is no such attribute, then there is no effect.".
18226 DATA is ignored if SIZE is 0. */
18227
18228 retval.data = NULL;
18229 retval.size = 0;
18230 }
18231 else if (attr_form_is_section_offset (attr))
18232 {
18233 struct dwarf2_loclist_baton loclist_baton;
18234 CORE_ADDR pc = (*get_frame_pc) (baton);
18235 size_t size;
18236
18237 fill_in_loclist_baton (cu, &loclist_baton, attr);
18238
18239 retval.data = dwarf2_find_location_expression (&loclist_baton,
18240 &size, pc);
18241 retval.size = size;
18242 }
18243 else
18244 {
18245 if (!attr_form_is_block (attr))
18246 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
18247 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
18248 offset.sect_off, objfile_name (per_cu->objfile));
18249
18250 retval.data = DW_BLOCK (attr)->data;
18251 retval.size = DW_BLOCK (attr)->size;
18252 }
18253 retval.per_cu = cu->per_cu;
18254
18255 age_cached_comp_units ();
18256
18257 return retval;
18258 }
18259
18260 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
18261 offset. */
18262
18263 struct dwarf2_locexpr_baton
18264 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
18265 struct dwarf2_per_cu_data *per_cu,
18266 CORE_ADDR (*get_frame_pc) (void *baton),
18267 void *baton)
18268 {
18269 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
18270
18271 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
18272 }
18273
18274 /* Write a constant of a given type as target-ordered bytes into
18275 OBSTACK. */
18276
18277 static const gdb_byte *
18278 write_constant_as_bytes (struct obstack *obstack,
18279 enum bfd_endian byte_order,
18280 struct type *type,
18281 ULONGEST value,
18282 LONGEST *len)
18283 {
18284 gdb_byte *result;
18285
18286 *len = TYPE_LENGTH (type);
18287 result = obstack_alloc (obstack, *len);
18288 store_unsigned_integer (result, *len, byte_order, value);
18289
18290 return result;
18291 }
18292
18293 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
18294 pointer to the constant bytes and set LEN to the length of the
18295 data. If memory is needed, allocate it on OBSTACK. If the DIE
18296 does not have a DW_AT_const_value, return NULL. */
18297
18298 const gdb_byte *
18299 dwarf2_fetch_constant_bytes (sect_offset offset,
18300 struct dwarf2_per_cu_data *per_cu,
18301 struct obstack *obstack,
18302 LONGEST *len)
18303 {
18304 struct dwarf2_cu *cu;
18305 struct die_info *die;
18306 struct attribute *attr;
18307 const gdb_byte *result = NULL;
18308 struct type *type;
18309 LONGEST value;
18310 enum bfd_endian byte_order;
18311
18312 dw2_setup (per_cu->objfile);
18313
18314 if (per_cu->cu == NULL)
18315 load_cu (per_cu);
18316 cu = per_cu->cu;
18317
18318 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
18319 if (!die)
18320 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
18321 offset.sect_off, objfile_name (per_cu->objfile));
18322
18323
18324 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18325 if (attr == NULL)
18326 return NULL;
18327
18328 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
18329 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
18330
18331 switch (attr->form)
18332 {
18333 case DW_FORM_addr:
18334 case DW_FORM_GNU_addr_index:
18335 {
18336 gdb_byte *tem;
18337
18338 *len = cu->header.addr_size;
18339 tem = obstack_alloc (obstack, *len);
18340 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
18341 result = tem;
18342 }
18343 break;
18344 case DW_FORM_string:
18345 case DW_FORM_strp:
18346 case DW_FORM_GNU_str_index:
18347 case DW_FORM_GNU_strp_alt:
18348 /* DW_STRING is already allocated on the objfile obstack, point
18349 directly to it. */
18350 result = (const gdb_byte *) DW_STRING (attr);
18351 *len = strlen (DW_STRING (attr));
18352 break;
18353 case DW_FORM_block1:
18354 case DW_FORM_block2:
18355 case DW_FORM_block4:
18356 case DW_FORM_block:
18357 case DW_FORM_exprloc:
18358 result = DW_BLOCK (attr)->data;
18359 *len = DW_BLOCK (attr)->size;
18360 break;
18361
18362 /* The DW_AT_const_value attributes are supposed to carry the
18363 symbol's value "represented as it would be on the target
18364 architecture." By the time we get here, it's already been
18365 converted to host endianness, so we just need to sign- or
18366 zero-extend it as appropriate. */
18367 case DW_FORM_data1:
18368 type = die_type (die, cu);
18369 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
18370 if (result == NULL)
18371 result = write_constant_as_bytes (obstack, byte_order,
18372 type, value, len);
18373 break;
18374 case DW_FORM_data2:
18375 type = die_type (die, cu);
18376 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
18377 if (result == NULL)
18378 result = write_constant_as_bytes (obstack, byte_order,
18379 type, value, len);
18380 break;
18381 case DW_FORM_data4:
18382 type = die_type (die, cu);
18383 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
18384 if (result == NULL)
18385 result = write_constant_as_bytes (obstack, byte_order,
18386 type, value, len);
18387 break;
18388 case DW_FORM_data8:
18389 type = die_type (die, cu);
18390 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
18391 if (result == NULL)
18392 result = write_constant_as_bytes (obstack, byte_order,
18393 type, value, len);
18394 break;
18395
18396 case DW_FORM_sdata:
18397 type = die_type (die, cu);
18398 result = write_constant_as_bytes (obstack, byte_order,
18399 type, DW_SND (attr), len);
18400 break;
18401
18402 case DW_FORM_udata:
18403 type = die_type (die, cu);
18404 result = write_constant_as_bytes (obstack, byte_order,
18405 type, DW_UNSND (attr), len);
18406 break;
18407
18408 default:
18409 complaint (&symfile_complaints,
18410 _("unsupported const value attribute form: '%s'"),
18411 dwarf_form_name (attr->form));
18412 break;
18413 }
18414
18415 return result;
18416 }
18417
18418 /* Return the type of the DIE at DIE_OFFSET in the CU named by
18419 PER_CU. */
18420
18421 struct type *
18422 dwarf2_get_die_type (cu_offset die_offset,
18423 struct dwarf2_per_cu_data *per_cu)
18424 {
18425 sect_offset die_offset_sect;
18426
18427 dw2_setup (per_cu->objfile);
18428
18429 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
18430 return get_die_type_at_offset (die_offset_sect, per_cu);
18431 }
18432
18433 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
18434 On entry *REF_CU is the CU of SRC_DIE.
18435 On exit *REF_CU is the CU of the result.
18436 Returns NULL if the referenced DIE isn't found. */
18437
18438 static struct die_info *
18439 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
18440 struct dwarf2_cu **ref_cu)
18441 {
18442 struct objfile *objfile = (*ref_cu)->objfile;
18443 struct die_info temp_die;
18444 struct dwarf2_cu *sig_cu;
18445 struct die_info *die;
18446
18447 /* While it might be nice to assert sig_type->type == NULL here,
18448 we can get here for DW_AT_imported_declaration where we need
18449 the DIE not the type. */
18450
18451 /* If necessary, add it to the queue and load its DIEs. */
18452
18453 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
18454 read_signatured_type (sig_type);
18455
18456 sig_cu = sig_type->per_cu.cu;
18457 gdb_assert (sig_cu != NULL);
18458 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
18459 temp_die.offset = sig_type->type_offset_in_section;
18460 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
18461 temp_die.offset.sect_off);
18462 if (die)
18463 {
18464 /* For .gdb_index version 7 keep track of included TUs.
18465 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
18466 if (dwarf2_per_objfile->index_table != NULL
18467 && dwarf2_per_objfile->index_table->version <= 7)
18468 {
18469 VEC_safe_push (dwarf2_per_cu_ptr,
18470 (*ref_cu)->per_cu->imported_symtabs,
18471 sig_cu->per_cu);
18472 }
18473
18474 *ref_cu = sig_cu;
18475 return die;
18476 }
18477
18478 return NULL;
18479 }
18480
18481 /* Follow signatured type referenced by ATTR in SRC_DIE.
18482 On entry *REF_CU is the CU of SRC_DIE.
18483 On exit *REF_CU is the CU of the result.
18484 The result is the DIE of the type.
18485 If the referenced type cannot be found an error is thrown. */
18486
18487 static struct die_info *
18488 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
18489 struct dwarf2_cu **ref_cu)
18490 {
18491 ULONGEST signature = DW_SIGNATURE (attr);
18492 struct signatured_type *sig_type;
18493 struct die_info *die;
18494
18495 gdb_assert (attr->form == DW_FORM_ref_sig8);
18496
18497 sig_type = lookup_signatured_type (*ref_cu, signature);
18498 /* sig_type will be NULL if the signatured type is missing from
18499 the debug info. */
18500 if (sig_type == NULL)
18501 {
18502 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
18503 " from DIE at 0x%x [in module %s]"),
18504 hex_string (signature), src_die->offset.sect_off,
18505 objfile_name ((*ref_cu)->objfile));
18506 }
18507
18508 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
18509 if (die == NULL)
18510 {
18511 dump_die_for_error (src_die);
18512 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
18513 " from DIE at 0x%x [in module %s]"),
18514 hex_string (signature), src_die->offset.sect_off,
18515 objfile_name ((*ref_cu)->objfile));
18516 }
18517
18518 return die;
18519 }
18520
18521 /* Get the type specified by SIGNATURE referenced in DIE/CU,
18522 reading in and processing the type unit if necessary. */
18523
18524 static struct type *
18525 get_signatured_type (struct die_info *die, ULONGEST signature,
18526 struct dwarf2_cu *cu)
18527 {
18528 struct signatured_type *sig_type;
18529 struct dwarf2_cu *type_cu;
18530 struct die_info *type_die;
18531 struct type *type;
18532
18533 sig_type = lookup_signatured_type (cu, signature);
18534 /* sig_type will be NULL if the signatured type is missing from
18535 the debug info. */
18536 if (sig_type == NULL)
18537 {
18538 complaint (&symfile_complaints,
18539 _("Dwarf Error: Cannot find signatured DIE %s referenced"
18540 " from DIE at 0x%x [in module %s]"),
18541 hex_string (signature), die->offset.sect_off,
18542 objfile_name (dwarf2_per_objfile->objfile));
18543 return build_error_marker_type (cu, die);
18544 }
18545
18546 /* If we already know the type we're done. */
18547 if (sig_type->type != NULL)
18548 return sig_type->type;
18549
18550 type_cu = cu;
18551 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
18552 if (type_die != NULL)
18553 {
18554 /* N.B. We need to call get_die_type to ensure only one type for this DIE
18555 is created. This is important, for example, because for c++ classes
18556 we need TYPE_NAME set which is only done by new_symbol. Blech. */
18557 type = read_type_die (type_die, type_cu);
18558 if (type == NULL)
18559 {
18560 complaint (&symfile_complaints,
18561 _("Dwarf Error: Cannot build signatured type %s"
18562 " referenced from DIE at 0x%x [in module %s]"),
18563 hex_string (signature), die->offset.sect_off,
18564 objfile_name (dwarf2_per_objfile->objfile));
18565 type = build_error_marker_type (cu, die);
18566 }
18567 }
18568 else
18569 {
18570 complaint (&symfile_complaints,
18571 _("Dwarf Error: Problem reading signatured DIE %s referenced"
18572 " from DIE at 0x%x [in module %s]"),
18573 hex_string (signature), die->offset.sect_off,
18574 objfile_name (dwarf2_per_objfile->objfile));
18575 type = build_error_marker_type (cu, die);
18576 }
18577 sig_type->type = type;
18578
18579 return type;
18580 }
18581
18582 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
18583 reading in and processing the type unit if necessary. */
18584
18585 static struct type *
18586 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
18587 struct dwarf2_cu *cu) /* ARI: editCase function */
18588 {
18589 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
18590 if (attr_form_is_ref (attr))
18591 {
18592 struct dwarf2_cu *type_cu = cu;
18593 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
18594
18595 return read_type_die (type_die, type_cu);
18596 }
18597 else if (attr->form == DW_FORM_ref_sig8)
18598 {
18599 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
18600 }
18601 else
18602 {
18603 complaint (&symfile_complaints,
18604 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
18605 " at 0x%x [in module %s]"),
18606 dwarf_form_name (attr->form), die->offset.sect_off,
18607 objfile_name (dwarf2_per_objfile->objfile));
18608 return build_error_marker_type (cu, die);
18609 }
18610 }
18611
18612 /* Load the DIEs associated with type unit PER_CU into memory. */
18613
18614 static void
18615 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
18616 {
18617 struct signatured_type *sig_type;
18618
18619 /* Caller is responsible for ensuring type_unit_groups don't get here. */
18620 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
18621
18622 /* We have the per_cu, but we need the signatured_type.
18623 Fortunately this is an easy translation. */
18624 gdb_assert (per_cu->is_debug_types);
18625 sig_type = (struct signatured_type *) per_cu;
18626
18627 gdb_assert (per_cu->cu == NULL);
18628
18629 read_signatured_type (sig_type);
18630
18631 gdb_assert (per_cu->cu != NULL);
18632 }
18633
18634 /* die_reader_func for read_signatured_type.
18635 This is identical to load_full_comp_unit_reader,
18636 but is kept separate for now. */
18637
18638 static void
18639 read_signatured_type_reader (const struct die_reader_specs *reader,
18640 const gdb_byte *info_ptr,
18641 struct die_info *comp_unit_die,
18642 int has_children,
18643 void *data)
18644 {
18645 struct dwarf2_cu *cu = reader->cu;
18646
18647 gdb_assert (cu->die_hash == NULL);
18648 cu->die_hash =
18649 htab_create_alloc_ex (cu->header.length / 12,
18650 die_hash,
18651 die_eq,
18652 NULL,
18653 &cu->comp_unit_obstack,
18654 hashtab_obstack_allocate,
18655 dummy_obstack_deallocate);
18656
18657 if (has_children)
18658 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
18659 &info_ptr, comp_unit_die);
18660 cu->dies = comp_unit_die;
18661 /* comp_unit_die is not stored in die_hash, no need. */
18662
18663 /* We try not to read any attributes in this function, because not
18664 all CUs needed for references have been loaded yet, and symbol
18665 table processing isn't initialized. But we have to set the CU language,
18666 or we won't be able to build types correctly.
18667 Similarly, if we do not read the producer, we can not apply
18668 producer-specific interpretation. */
18669 prepare_one_comp_unit (cu, cu->dies, language_minimal);
18670 }
18671
18672 /* Read in a signatured type and build its CU and DIEs.
18673 If the type is a stub for the real type in a DWO file,
18674 read in the real type from the DWO file as well. */
18675
18676 static void
18677 read_signatured_type (struct signatured_type *sig_type)
18678 {
18679 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
18680
18681 gdb_assert (per_cu->is_debug_types);
18682 gdb_assert (per_cu->cu == NULL);
18683
18684 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
18685 read_signatured_type_reader, NULL);
18686 sig_type->per_cu.tu_read = 1;
18687 }
18688
18689 /* Decode simple location descriptions.
18690 Given a pointer to a dwarf block that defines a location, compute
18691 the location and return the value.
18692
18693 NOTE drow/2003-11-18: This function is called in two situations
18694 now: for the address of static or global variables (partial symbols
18695 only) and for offsets into structures which are expected to be
18696 (more or less) constant. The partial symbol case should go away,
18697 and only the constant case should remain. That will let this
18698 function complain more accurately. A few special modes are allowed
18699 without complaint for global variables (for instance, global
18700 register values and thread-local values).
18701
18702 A location description containing no operations indicates that the
18703 object is optimized out. The return value is 0 for that case.
18704 FIXME drow/2003-11-16: No callers check for this case any more; soon all
18705 callers will only want a very basic result and this can become a
18706 complaint.
18707
18708 Note that stack[0] is unused except as a default error return. */
18709
18710 static CORE_ADDR
18711 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
18712 {
18713 struct objfile *objfile = cu->objfile;
18714 size_t i;
18715 size_t size = blk->size;
18716 const gdb_byte *data = blk->data;
18717 CORE_ADDR stack[64];
18718 int stacki;
18719 unsigned int bytes_read, unsnd;
18720 gdb_byte op;
18721
18722 i = 0;
18723 stacki = 0;
18724 stack[stacki] = 0;
18725 stack[++stacki] = 0;
18726
18727 while (i < size)
18728 {
18729 op = data[i++];
18730 switch (op)
18731 {
18732 case DW_OP_lit0:
18733 case DW_OP_lit1:
18734 case DW_OP_lit2:
18735 case DW_OP_lit3:
18736 case DW_OP_lit4:
18737 case DW_OP_lit5:
18738 case DW_OP_lit6:
18739 case DW_OP_lit7:
18740 case DW_OP_lit8:
18741 case DW_OP_lit9:
18742 case DW_OP_lit10:
18743 case DW_OP_lit11:
18744 case DW_OP_lit12:
18745 case DW_OP_lit13:
18746 case DW_OP_lit14:
18747 case DW_OP_lit15:
18748 case DW_OP_lit16:
18749 case DW_OP_lit17:
18750 case DW_OP_lit18:
18751 case DW_OP_lit19:
18752 case DW_OP_lit20:
18753 case DW_OP_lit21:
18754 case DW_OP_lit22:
18755 case DW_OP_lit23:
18756 case DW_OP_lit24:
18757 case DW_OP_lit25:
18758 case DW_OP_lit26:
18759 case DW_OP_lit27:
18760 case DW_OP_lit28:
18761 case DW_OP_lit29:
18762 case DW_OP_lit30:
18763 case DW_OP_lit31:
18764 stack[++stacki] = op - DW_OP_lit0;
18765 break;
18766
18767 case DW_OP_reg0:
18768 case DW_OP_reg1:
18769 case DW_OP_reg2:
18770 case DW_OP_reg3:
18771 case DW_OP_reg4:
18772 case DW_OP_reg5:
18773 case DW_OP_reg6:
18774 case DW_OP_reg7:
18775 case DW_OP_reg8:
18776 case DW_OP_reg9:
18777 case DW_OP_reg10:
18778 case DW_OP_reg11:
18779 case DW_OP_reg12:
18780 case DW_OP_reg13:
18781 case DW_OP_reg14:
18782 case DW_OP_reg15:
18783 case DW_OP_reg16:
18784 case DW_OP_reg17:
18785 case DW_OP_reg18:
18786 case DW_OP_reg19:
18787 case DW_OP_reg20:
18788 case DW_OP_reg21:
18789 case DW_OP_reg22:
18790 case DW_OP_reg23:
18791 case DW_OP_reg24:
18792 case DW_OP_reg25:
18793 case DW_OP_reg26:
18794 case DW_OP_reg27:
18795 case DW_OP_reg28:
18796 case DW_OP_reg29:
18797 case DW_OP_reg30:
18798 case DW_OP_reg31:
18799 stack[++stacki] = op - DW_OP_reg0;
18800 if (i < size)
18801 dwarf2_complex_location_expr_complaint ();
18802 break;
18803
18804 case DW_OP_regx:
18805 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
18806 i += bytes_read;
18807 stack[++stacki] = unsnd;
18808 if (i < size)
18809 dwarf2_complex_location_expr_complaint ();
18810 break;
18811
18812 case DW_OP_addr:
18813 stack[++stacki] = read_address (objfile->obfd, &data[i],
18814 cu, &bytes_read);
18815 i += bytes_read;
18816 break;
18817
18818 case DW_OP_const1u:
18819 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
18820 i += 1;
18821 break;
18822
18823 case DW_OP_const1s:
18824 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
18825 i += 1;
18826 break;
18827
18828 case DW_OP_const2u:
18829 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
18830 i += 2;
18831 break;
18832
18833 case DW_OP_const2s:
18834 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
18835 i += 2;
18836 break;
18837
18838 case DW_OP_const4u:
18839 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
18840 i += 4;
18841 break;
18842
18843 case DW_OP_const4s:
18844 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
18845 i += 4;
18846 break;
18847
18848 case DW_OP_const8u:
18849 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
18850 i += 8;
18851 break;
18852
18853 case DW_OP_constu:
18854 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
18855 &bytes_read);
18856 i += bytes_read;
18857 break;
18858
18859 case DW_OP_consts:
18860 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
18861 i += bytes_read;
18862 break;
18863
18864 case DW_OP_dup:
18865 stack[stacki + 1] = stack[stacki];
18866 stacki++;
18867 break;
18868
18869 case DW_OP_plus:
18870 stack[stacki - 1] += stack[stacki];
18871 stacki--;
18872 break;
18873
18874 case DW_OP_plus_uconst:
18875 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
18876 &bytes_read);
18877 i += bytes_read;
18878 break;
18879
18880 case DW_OP_minus:
18881 stack[stacki - 1] -= stack[stacki];
18882 stacki--;
18883 break;
18884
18885 case DW_OP_deref:
18886 /* If we're not the last op, then we definitely can't encode
18887 this using GDB's address_class enum. This is valid for partial
18888 global symbols, although the variable's address will be bogus
18889 in the psymtab. */
18890 if (i < size)
18891 dwarf2_complex_location_expr_complaint ();
18892 break;
18893
18894 case DW_OP_GNU_push_tls_address:
18895 /* The top of the stack has the offset from the beginning
18896 of the thread control block at which the variable is located. */
18897 /* Nothing should follow this operator, so the top of stack would
18898 be returned. */
18899 /* This is valid for partial global symbols, but the variable's
18900 address will be bogus in the psymtab. Make it always at least
18901 non-zero to not look as a variable garbage collected by linker
18902 which have DW_OP_addr 0. */
18903 if (i < size)
18904 dwarf2_complex_location_expr_complaint ();
18905 stack[stacki]++;
18906 break;
18907
18908 case DW_OP_GNU_uninit:
18909 break;
18910
18911 case DW_OP_GNU_addr_index:
18912 case DW_OP_GNU_const_index:
18913 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18914 &bytes_read);
18915 i += bytes_read;
18916 break;
18917
18918 default:
18919 {
18920 const char *name = get_DW_OP_name (op);
18921
18922 if (name)
18923 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18924 name);
18925 else
18926 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18927 op);
18928 }
18929
18930 return (stack[stacki]);
18931 }
18932
18933 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18934 outside of the allocated space. Also enforce minimum>0. */
18935 if (stacki >= ARRAY_SIZE (stack) - 1)
18936 {
18937 complaint (&symfile_complaints,
18938 _("location description stack overflow"));
18939 return 0;
18940 }
18941
18942 if (stacki <= 0)
18943 {
18944 complaint (&symfile_complaints,
18945 _("location description stack underflow"));
18946 return 0;
18947 }
18948 }
18949 return (stack[stacki]);
18950 }
18951
18952 /* memory allocation interface */
18953
18954 static struct dwarf_block *
18955 dwarf_alloc_block (struct dwarf2_cu *cu)
18956 {
18957 struct dwarf_block *blk;
18958
18959 blk = (struct dwarf_block *)
18960 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
18961 return (blk);
18962 }
18963
18964 static struct die_info *
18965 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
18966 {
18967 struct die_info *die;
18968 size_t size = sizeof (struct die_info);
18969
18970 if (num_attrs > 1)
18971 size += (num_attrs - 1) * sizeof (struct attribute);
18972
18973 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
18974 memset (die, 0, sizeof (struct die_info));
18975 return (die);
18976 }
18977
18978 \f
18979 /* Macro support. */
18980
18981 /* Return file name relative to the compilation directory of file number I in
18982 *LH's file name table. The result is allocated using xmalloc; the caller is
18983 responsible for freeing it. */
18984
18985 static char *
18986 file_file_name (int file, struct line_header *lh)
18987 {
18988 /* Is the file number a valid index into the line header's file name
18989 table? Remember that file numbers start with one, not zero. */
18990 if (1 <= file && file <= lh->num_file_names)
18991 {
18992 struct file_entry *fe = &lh->file_names[file - 1];
18993
18994 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
18995 return xstrdup (fe->name);
18996 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18997 fe->name, NULL);
18998 }
18999 else
19000 {
19001 /* The compiler produced a bogus file number. We can at least
19002 record the macro definitions made in the file, even if we
19003 won't be able to find the file by name. */
19004 char fake_name[80];
19005
19006 xsnprintf (fake_name, sizeof (fake_name),
19007 "<bad macro file number %d>", file);
19008
19009 complaint (&symfile_complaints,
19010 _("bad file number in macro information (%d)"),
19011 file);
19012
19013 return xstrdup (fake_name);
19014 }
19015 }
19016
19017 /* Return the full name of file number I in *LH's file name table.
19018 Use COMP_DIR as the name of the current directory of the
19019 compilation. The result is allocated using xmalloc; the caller is
19020 responsible for freeing it. */
19021 static char *
19022 file_full_name (int file, struct line_header *lh, const char *comp_dir)
19023 {
19024 /* Is the file number a valid index into the line header's file name
19025 table? Remember that file numbers start with one, not zero. */
19026 if (1 <= file && file <= lh->num_file_names)
19027 {
19028 char *relative = file_file_name (file, lh);
19029
19030 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
19031 return relative;
19032 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
19033 }
19034 else
19035 return file_file_name (file, lh);
19036 }
19037
19038
19039 static struct macro_source_file *
19040 macro_start_file (int file, int line,
19041 struct macro_source_file *current_file,
19042 const char *comp_dir,
19043 struct line_header *lh, struct objfile *objfile)
19044 {
19045 /* File name relative to the compilation directory of this source file. */
19046 char *file_name = file_file_name (file, lh);
19047
19048 if (! current_file)
19049 {
19050 /* Note: We don't create a macro table for this compilation unit
19051 at all until we actually get a filename. */
19052 struct macro_table *macro_table = get_macro_table (objfile, comp_dir);
19053
19054 /* If we have no current file, then this must be the start_file
19055 directive for the compilation unit's main source file. */
19056 current_file = macro_set_main (macro_table, file_name);
19057 macro_define_special (macro_table);
19058 }
19059 else
19060 current_file = macro_include (current_file, line, file_name);
19061
19062 xfree (file_name);
19063
19064 return current_file;
19065 }
19066
19067
19068 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
19069 followed by a null byte. */
19070 static char *
19071 copy_string (const char *buf, int len)
19072 {
19073 char *s = xmalloc (len + 1);
19074
19075 memcpy (s, buf, len);
19076 s[len] = '\0';
19077 return s;
19078 }
19079
19080
19081 static const char *
19082 consume_improper_spaces (const char *p, const char *body)
19083 {
19084 if (*p == ' ')
19085 {
19086 complaint (&symfile_complaints,
19087 _("macro definition contains spaces "
19088 "in formal argument list:\n`%s'"),
19089 body);
19090
19091 while (*p == ' ')
19092 p++;
19093 }
19094
19095 return p;
19096 }
19097
19098
19099 static void
19100 parse_macro_definition (struct macro_source_file *file, int line,
19101 const char *body)
19102 {
19103 const char *p;
19104
19105 /* The body string takes one of two forms. For object-like macro
19106 definitions, it should be:
19107
19108 <macro name> " " <definition>
19109
19110 For function-like macro definitions, it should be:
19111
19112 <macro name> "() " <definition>
19113 or
19114 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
19115
19116 Spaces may appear only where explicitly indicated, and in the
19117 <definition>.
19118
19119 The Dwarf 2 spec says that an object-like macro's name is always
19120 followed by a space, but versions of GCC around March 2002 omit
19121 the space when the macro's definition is the empty string.
19122
19123 The Dwarf 2 spec says that there should be no spaces between the
19124 formal arguments in a function-like macro's formal argument list,
19125 but versions of GCC around March 2002 include spaces after the
19126 commas. */
19127
19128
19129 /* Find the extent of the macro name. The macro name is terminated
19130 by either a space or null character (for an object-like macro) or
19131 an opening paren (for a function-like macro). */
19132 for (p = body; *p; p++)
19133 if (*p == ' ' || *p == '(')
19134 break;
19135
19136 if (*p == ' ' || *p == '\0')
19137 {
19138 /* It's an object-like macro. */
19139 int name_len = p - body;
19140 char *name = copy_string (body, name_len);
19141 const char *replacement;
19142
19143 if (*p == ' ')
19144 replacement = body + name_len + 1;
19145 else
19146 {
19147 dwarf2_macro_malformed_definition_complaint (body);
19148 replacement = body + name_len;
19149 }
19150
19151 macro_define_object (file, line, name, replacement);
19152
19153 xfree (name);
19154 }
19155 else if (*p == '(')
19156 {
19157 /* It's a function-like macro. */
19158 char *name = copy_string (body, p - body);
19159 int argc = 0;
19160 int argv_size = 1;
19161 char **argv = xmalloc (argv_size * sizeof (*argv));
19162
19163 p++;
19164
19165 p = consume_improper_spaces (p, body);
19166
19167 /* Parse the formal argument list. */
19168 while (*p && *p != ')')
19169 {
19170 /* Find the extent of the current argument name. */
19171 const char *arg_start = p;
19172
19173 while (*p && *p != ',' && *p != ')' && *p != ' ')
19174 p++;
19175
19176 if (! *p || p == arg_start)
19177 dwarf2_macro_malformed_definition_complaint (body);
19178 else
19179 {
19180 /* Make sure argv has room for the new argument. */
19181 if (argc >= argv_size)
19182 {
19183 argv_size *= 2;
19184 argv = xrealloc (argv, argv_size * sizeof (*argv));
19185 }
19186
19187 argv[argc++] = copy_string (arg_start, p - arg_start);
19188 }
19189
19190 p = consume_improper_spaces (p, body);
19191
19192 /* Consume the comma, if present. */
19193 if (*p == ',')
19194 {
19195 p++;
19196
19197 p = consume_improper_spaces (p, body);
19198 }
19199 }
19200
19201 if (*p == ')')
19202 {
19203 p++;
19204
19205 if (*p == ' ')
19206 /* Perfectly formed definition, no complaints. */
19207 macro_define_function (file, line, name,
19208 argc, (const char **) argv,
19209 p + 1);
19210 else if (*p == '\0')
19211 {
19212 /* Complain, but do define it. */
19213 dwarf2_macro_malformed_definition_complaint (body);
19214 macro_define_function (file, line, name,
19215 argc, (const char **) argv,
19216 p);
19217 }
19218 else
19219 /* Just complain. */
19220 dwarf2_macro_malformed_definition_complaint (body);
19221 }
19222 else
19223 /* Just complain. */
19224 dwarf2_macro_malformed_definition_complaint (body);
19225
19226 xfree (name);
19227 {
19228 int i;
19229
19230 for (i = 0; i < argc; i++)
19231 xfree (argv[i]);
19232 }
19233 xfree (argv);
19234 }
19235 else
19236 dwarf2_macro_malformed_definition_complaint (body);
19237 }
19238
19239 /* Skip some bytes from BYTES according to the form given in FORM.
19240 Returns the new pointer. */
19241
19242 static const gdb_byte *
19243 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
19244 enum dwarf_form form,
19245 unsigned int offset_size,
19246 struct dwarf2_section_info *section)
19247 {
19248 unsigned int bytes_read;
19249
19250 switch (form)
19251 {
19252 case DW_FORM_data1:
19253 case DW_FORM_flag:
19254 ++bytes;
19255 break;
19256
19257 case DW_FORM_data2:
19258 bytes += 2;
19259 break;
19260
19261 case DW_FORM_data4:
19262 bytes += 4;
19263 break;
19264
19265 case DW_FORM_data8:
19266 bytes += 8;
19267 break;
19268
19269 case DW_FORM_string:
19270 read_direct_string (abfd, bytes, &bytes_read);
19271 bytes += bytes_read;
19272 break;
19273
19274 case DW_FORM_sec_offset:
19275 case DW_FORM_strp:
19276 case DW_FORM_GNU_strp_alt:
19277 bytes += offset_size;
19278 break;
19279
19280 case DW_FORM_block:
19281 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
19282 bytes += bytes_read;
19283 break;
19284
19285 case DW_FORM_block1:
19286 bytes += 1 + read_1_byte (abfd, bytes);
19287 break;
19288 case DW_FORM_block2:
19289 bytes += 2 + read_2_bytes (abfd, bytes);
19290 break;
19291 case DW_FORM_block4:
19292 bytes += 4 + read_4_bytes (abfd, bytes);
19293 break;
19294
19295 case DW_FORM_sdata:
19296 case DW_FORM_udata:
19297 case DW_FORM_GNU_addr_index:
19298 case DW_FORM_GNU_str_index:
19299 bytes = gdb_skip_leb128 (bytes, buffer_end);
19300 if (bytes == NULL)
19301 {
19302 dwarf2_section_buffer_overflow_complaint (section);
19303 return NULL;
19304 }
19305 break;
19306
19307 default:
19308 {
19309 complain:
19310 complaint (&symfile_complaints,
19311 _("invalid form 0x%x in `%s'"),
19312 form,
19313 section->asection->name);
19314 return NULL;
19315 }
19316 }
19317
19318 return bytes;
19319 }
19320
19321 /* A helper for dwarf_decode_macros that handles skipping an unknown
19322 opcode. Returns an updated pointer to the macro data buffer; or,
19323 on error, issues a complaint and returns NULL. */
19324
19325 static const gdb_byte *
19326 skip_unknown_opcode (unsigned int opcode,
19327 const gdb_byte **opcode_definitions,
19328 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
19329 bfd *abfd,
19330 unsigned int offset_size,
19331 struct dwarf2_section_info *section)
19332 {
19333 unsigned int bytes_read, i;
19334 unsigned long arg;
19335 const gdb_byte *defn;
19336
19337 if (opcode_definitions[opcode] == NULL)
19338 {
19339 complaint (&symfile_complaints,
19340 _("unrecognized DW_MACFINO opcode 0x%x"),
19341 opcode);
19342 return NULL;
19343 }
19344
19345 defn = opcode_definitions[opcode];
19346 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
19347 defn += bytes_read;
19348
19349 for (i = 0; i < arg; ++i)
19350 {
19351 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
19352 section);
19353 if (mac_ptr == NULL)
19354 {
19355 /* skip_form_bytes already issued the complaint. */
19356 return NULL;
19357 }
19358 }
19359
19360 return mac_ptr;
19361 }
19362
19363 /* A helper function which parses the header of a macro section.
19364 If the macro section is the extended (for now called "GNU") type,
19365 then this updates *OFFSET_SIZE. Returns a pointer to just after
19366 the header, or issues a complaint and returns NULL on error. */
19367
19368 static const gdb_byte *
19369 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
19370 bfd *abfd,
19371 const gdb_byte *mac_ptr,
19372 unsigned int *offset_size,
19373 int section_is_gnu)
19374 {
19375 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
19376
19377 if (section_is_gnu)
19378 {
19379 unsigned int version, flags;
19380
19381 version = read_2_bytes (abfd, mac_ptr);
19382 if (version != 4)
19383 {
19384 complaint (&symfile_complaints,
19385 _("unrecognized version `%d' in .debug_macro section"),
19386 version);
19387 return NULL;
19388 }
19389 mac_ptr += 2;
19390
19391 flags = read_1_byte (abfd, mac_ptr);
19392 ++mac_ptr;
19393 *offset_size = (flags & 1) ? 8 : 4;
19394
19395 if ((flags & 2) != 0)
19396 /* We don't need the line table offset. */
19397 mac_ptr += *offset_size;
19398
19399 /* Vendor opcode descriptions. */
19400 if ((flags & 4) != 0)
19401 {
19402 unsigned int i, count;
19403
19404 count = read_1_byte (abfd, mac_ptr);
19405 ++mac_ptr;
19406 for (i = 0; i < count; ++i)
19407 {
19408 unsigned int opcode, bytes_read;
19409 unsigned long arg;
19410
19411 opcode = read_1_byte (abfd, mac_ptr);
19412 ++mac_ptr;
19413 opcode_definitions[opcode] = mac_ptr;
19414 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19415 mac_ptr += bytes_read;
19416 mac_ptr += arg;
19417 }
19418 }
19419 }
19420
19421 return mac_ptr;
19422 }
19423
19424 /* A helper for dwarf_decode_macros that handles the GNU extensions,
19425 including DW_MACRO_GNU_transparent_include. */
19426
19427 static void
19428 dwarf_decode_macro_bytes (bfd *abfd,
19429 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
19430 struct macro_source_file *current_file,
19431 struct line_header *lh, const char *comp_dir,
19432 struct dwarf2_section_info *section,
19433 int section_is_gnu, int section_is_dwz,
19434 unsigned int offset_size,
19435 struct objfile *objfile,
19436 htab_t include_hash)
19437 {
19438 enum dwarf_macro_record_type macinfo_type;
19439 int at_commandline;
19440 const gdb_byte *opcode_definitions[256];
19441
19442 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19443 &offset_size, section_is_gnu);
19444 if (mac_ptr == NULL)
19445 {
19446 /* We already issued a complaint. */
19447 return;
19448 }
19449
19450 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
19451 GDB is still reading the definitions from command line. First
19452 DW_MACINFO_start_file will need to be ignored as it was already executed
19453 to create CURRENT_FILE for the main source holding also the command line
19454 definitions. On first met DW_MACINFO_start_file this flag is reset to
19455 normally execute all the remaining DW_MACINFO_start_file macinfos. */
19456
19457 at_commandline = 1;
19458
19459 do
19460 {
19461 /* Do we at least have room for a macinfo type byte? */
19462 if (mac_ptr >= mac_end)
19463 {
19464 dwarf2_section_buffer_overflow_complaint (section);
19465 break;
19466 }
19467
19468 macinfo_type = read_1_byte (abfd, mac_ptr);
19469 mac_ptr++;
19470
19471 /* Note that we rely on the fact that the corresponding GNU and
19472 DWARF constants are the same. */
19473 switch (macinfo_type)
19474 {
19475 /* A zero macinfo type indicates the end of the macro
19476 information. */
19477 case 0:
19478 break;
19479
19480 case DW_MACRO_GNU_define:
19481 case DW_MACRO_GNU_undef:
19482 case DW_MACRO_GNU_define_indirect:
19483 case DW_MACRO_GNU_undef_indirect:
19484 case DW_MACRO_GNU_define_indirect_alt:
19485 case DW_MACRO_GNU_undef_indirect_alt:
19486 {
19487 unsigned int bytes_read;
19488 int line;
19489 const char *body;
19490 int is_define;
19491
19492 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19493 mac_ptr += bytes_read;
19494
19495 if (macinfo_type == DW_MACRO_GNU_define
19496 || macinfo_type == DW_MACRO_GNU_undef)
19497 {
19498 body = read_direct_string (abfd, mac_ptr, &bytes_read);
19499 mac_ptr += bytes_read;
19500 }
19501 else
19502 {
19503 LONGEST str_offset;
19504
19505 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
19506 mac_ptr += offset_size;
19507
19508 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
19509 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
19510 || section_is_dwz)
19511 {
19512 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19513
19514 body = read_indirect_string_from_dwz (dwz, str_offset);
19515 }
19516 else
19517 body = read_indirect_string_at_offset (abfd, str_offset);
19518 }
19519
19520 is_define = (macinfo_type == DW_MACRO_GNU_define
19521 || macinfo_type == DW_MACRO_GNU_define_indirect
19522 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
19523 if (! current_file)
19524 {
19525 /* DWARF violation as no main source is present. */
19526 complaint (&symfile_complaints,
19527 _("debug info with no main source gives macro %s "
19528 "on line %d: %s"),
19529 is_define ? _("definition") : _("undefinition"),
19530 line, body);
19531 break;
19532 }
19533 if ((line == 0 && !at_commandline)
19534 || (line != 0 && at_commandline))
19535 complaint (&symfile_complaints,
19536 _("debug info gives %s macro %s with %s line %d: %s"),
19537 at_commandline ? _("command-line") : _("in-file"),
19538 is_define ? _("definition") : _("undefinition"),
19539 line == 0 ? _("zero") : _("non-zero"), line, body);
19540
19541 if (is_define)
19542 parse_macro_definition (current_file, line, body);
19543 else
19544 {
19545 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
19546 || macinfo_type == DW_MACRO_GNU_undef_indirect
19547 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
19548 macro_undef (current_file, line, body);
19549 }
19550 }
19551 break;
19552
19553 case DW_MACRO_GNU_start_file:
19554 {
19555 unsigned int bytes_read;
19556 int line, file;
19557
19558 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19559 mac_ptr += bytes_read;
19560 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19561 mac_ptr += bytes_read;
19562
19563 if ((line == 0 && !at_commandline)
19564 || (line != 0 && at_commandline))
19565 complaint (&symfile_complaints,
19566 _("debug info gives source %d included "
19567 "from %s at %s line %d"),
19568 file, at_commandline ? _("command-line") : _("file"),
19569 line == 0 ? _("zero") : _("non-zero"), line);
19570
19571 if (at_commandline)
19572 {
19573 /* This DW_MACRO_GNU_start_file was executed in the
19574 pass one. */
19575 at_commandline = 0;
19576 }
19577 else
19578 current_file = macro_start_file (file, line,
19579 current_file, comp_dir,
19580 lh, objfile);
19581 }
19582 break;
19583
19584 case DW_MACRO_GNU_end_file:
19585 if (! current_file)
19586 complaint (&symfile_complaints,
19587 _("macro debug info has an unmatched "
19588 "`close_file' directive"));
19589 else
19590 {
19591 current_file = current_file->included_by;
19592 if (! current_file)
19593 {
19594 enum dwarf_macro_record_type next_type;
19595
19596 /* GCC circa March 2002 doesn't produce the zero
19597 type byte marking the end of the compilation
19598 unit. Complain if it's not there, but exit no
19599 matter what. */
19600
19601 /* Do we at least have room for a macinfo type byte? */
19602 if (mac_ptr >= mac_end)
19603 {
19604 dwarf2_section_buffer_overflow_complaint (section);
19605 return;
19606 }
19607
19608 /* We don't increment mac_ptr here, so this is just
19609 a look-ahead. */
19610 next_type = read_1_byte (abfd, mac_ptr);
19611 if (next_type != 0)
19612 complaint (&symfile_complaints,
19613 _("no terminating 0-type entry for "
19614 "macros in `.debug_macinfo' section"));
19615
19616 return;
19617 }
19618 }
19619 break;
19620
19621 case DW_MACRO_GNU_transparent_include:
19622 case DW_MACRO_GNU_transparent_include_alt:
19623 {
19624 LONGEST offset;
19625 void **slot;
19626 bfd *include_bfd = abfd;
19627 struct dwarf2_section_info *include_section = section;
19628 struct dwarf2_section_info alt_section;
19629 const gdb_byte *include_mac_end = mac_end;
19630 int is_dwz = section_is_dwz;
19631 const gdb_byte *new_mac_ptr;
19632
19633 offset = read_offset_1 (abfd, mac_ptr, offset_size);
19634 mac_ptr += offset_size;
19635
19636 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
19637 {
19638 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19639
19640 dwarf2_read_section (dwarf2_per_objfile->objfile,
19641 &dwz->macro);
19642
19643 include_bfd = dwz->macro.asection->owner;
19644 include_section = &dwz->macro;
19645 include_mac_end = dwz->macro.buffer + dwz->macro.size;
19646 is_dwz = 1;
19647 }
19648
19649 new_mac_ptr = include_section->buffer + offset;
19650 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
19651
19652 if (*slot != NULL)
19653 {
19654 /* This has actually happened; see
19655 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
19656 complaint (&symfile_complaints,
19657 _("recursive DW_MACRO_GNU_transparent_include in "
19658 ".debug_macro section"));
19659 }
19660 else
19661 {
19662 *slot = (void *) new_mac_ptr;
19663
19664 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
19665 include_mac_end, current_file,
19666 lh, comp_dir,
19667 section, section_is_gnu, is_dwz,
19668 offset_size, objfile, include_hash);
19669
19670 htab_remove_elt (include_hash, (void *) new_mac_ptr);
19671 }
19672 }
19673 break;
19674
19675 case DW_MACINFO_vendor_ext:
19676 if (!section_is_gnu)
19677 {
19678 unsigned int bytes_read;
19679 int constant;
19680
19681 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19682 mac_ptr += bytes_read;
19683 read_direct_string (abfd, mac_ptr, &bytes_read);
19684 mac_ptr += bytes_read;
19685
19686 /* We don't recognize any vendor extensions. */
19687 break;
19688 }
19689 /* FALLTHROUGH */
19690
19691 default:
19692 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
19693 mac_ptr, mac_end, abfd, offset_size,
19694 section);
19695 if (mac_ptr == NULL)
19696 return;
19697 break;
19698 }
19699 } while (macinfo_type != 0);
19700 }
19701
19702 static void
19703 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
19704 const char *comp_dir, int section_is_gnu)
19705 {
19706 struct objfile *objfile = dwarf2_per_objfile->objfile;
19707 struct line_header *lh = cu->line_header;
19708 bfd *abfd;
19709 const gdb_byte *mac_ptr, *mac_end;
19710 struct macro_source_file *current_file = 0;
19711 enum dwarf_macro_record_type macinfo_type;
19712 unsigned int offset_size = cu->header.offset_size;
19713 const gdb_byte *opcode_definitions[256];
19714 struct cleanup *cleanup;
19715 htab_t include_hash;
19716 void **slot;
19717 struct dwarf2_section_info *section;
19718 const char *section_name;
19719
19720 if (cu->dwo_unit != NULL)
19721 {
19722 if (section_is_gnu)
19723 {
19724 section = &cu->dwo_unit->dwo_file->sections.macro;
19725 section_name = ".debug_macro.dwo";
19726 }
19727 else
19728 {
19729 section = &cu->dwo_unit->dwo_file->sections.macinfo;
19730 section_name = ".debug_macinfo.dwo";
19731 }
19732 }
19733 else
19734 {
19735 if (section_is_gnu)
19736 {
19737 section = &dwarf2_per_objfile->macro;
19738 section_name = ".debug_macro";
19739 }
19740 else
19741 {
19742 section = &dwarf2_per_objfile->macinfo;
19743 section_name = ".debug_macinfo";
19744 }
19745 }
19746
19747 dwarf2_read_section (objfile, section);
19748 if (section->buffer == NULL)
19749 {
19750 complaint (&symfile_complaints, _("missing %s section"), section_name);
19751 return;
19752 }
19753 abfd = section->asection->owner;
19754
19755 /* First pass: Find the name of the base filename.
19756 This filename is needed in order to process all macros whose definition
19757 (or undefinition) comes from the command line. These macros are defined
19758 before the first DW_MACINFO_start_file entry, and yet still need to be
19759 associated to the base file.
19760
19761 To determine the base file name, we scan the macro definitions until we
19762 reach the first DW_MACINFO_start_file entry. We then initialize
19763 CURRENT_FILE accordingly so that any macro definition found before the
19764 first DW_MACINFO_start_file can still be associated to the base file. */
19765
19766 mac_ptr = section->buffer + offset;
19767 mac_end = section->buffer + section->size;
19768
19769 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19770 &offset_size, section_is_gnu);
19771 if (mac_ptr == NULL)
19772 {
19773 /* We already issued a complaint. */
19774 return;
19775 }
19776
19777 do
19778 {
19779 /* Do we at least have room for a macinfo type byte? */
19780 if (mac_ptr >= mac_end)
19781 {
19782 /* Complaint is printed during the second pass as GDB will probably
19783 stop the first pass earlier upon finding
19784 DW_MACINFO_start_file. */
19785 break;
19786 }
19787
19788 macinfo_type = read_1_byte (abfd, mac_ptr);
19789 mac_ptr++;
19790
19791 /* Note that we rely on the fact that the corresponding GNU and
19792 DWARF constants are the same. */
19793 switch (macinfo_type)
19794 {
19795 /* A zero macinfo type indicates the end of the macro
19796 information. */
19797 case 0:
19798 break;
19799
19800 case DW_MACRO_GNU_define:
19801 case DW_MACRO_GNU_undef:
19802 /* Only skip the data by MAC_PTR. */
19803 {
19804 unsigned int bytes_read;
19805
19806 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19807 mac_ptr += bytes_read;
19808 read_direct_string (abfd, mac_ptr, &bytes_read);
19809 mac_ptr += bytes_read;
19810 }
19811 break;
19812
19813 case DW_MACRO_GNU_start_file:
19814 {
19815 unsigned int bytes_read;
19816 int line, file;
19817
19818 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19819 mac_ptr += bytes_read;
19820 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19821 mac_ptr += bytes_read;
19822
19823 current_file = macro_start_file (file, line, current_file,
19824 comp_dir, lh, objfile);
19825 }
19826 break;
19827
19828 case DW_MACRO_GNU_end_file:
19829 /* No data to skip by MAC_PTR. */
19830 break;
19831
19832 case DW_MACRO_GNU_define_indirect:
19833 case DW_MACRO_GNU_undef_indirect:
19834 case DW_MACRO_GNU_define_indirect_alt:
19835 case DW_MACRO_GNU_undef_indirect_alt:
19836 {
19837 unsigned int bytes_read;
19838
19839 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19840 mac_ptr += bytes_read;
19841 mac_ptr += offset_size;
19842 }
19843 break;
19844
19845 case DW_MACRO_GNU_transparent_include:
19846 case DW_MACRO_GNU_transparent_include_alt:
19847 /* Note that, according to the spec, a transparent include
19848 chain cannot call DW_MACRO_GNU_start_file. So, we can just
19849 skip this opcode. */
19850 mac_ptr += offset_size;
19851 break;
19852
19853 case DW_MACINFO_vendor_ext:
19854 /* Only skip the data by MAC_PTR. */
19855 if (!section_is_gnu)
19856 {
19857 unsigned int bytes_read;
19858
19859 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19860 mac_ptr += bytes_read;
19861 read_direct_string (abfd, mac_ptr, &bytes_read);
19862 mac_ptr += bytes_read;
19863 }
19864 /* FALLTHROUGH */
19865
19866 default:
19867 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
19868 mac_ptr, mac_end, abfd, offset_size,
19869 section);
19870 if (mac_ptr == NULL)
19871 return;
19872 break;
19873 }
19874 } while (macinfo_type != 0 && current_file == NULL);
19875
19876 /* Second pass: Process all entries.
19877
19878 Use the AT_COMMAND_LINE flag to determine whether we are still processing
19879 command-line macro definitions/undefinitions. This flag is unset when we
19880 reach the first DW_MACINFO_start_file entry. */
19881
19882 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
19883 NULL, xcalloc, xfree);
19884 cleanup = make_cleanup_htab_delete (include_hash);
19885 mac_ptr = section->buffer + offset;
19886 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
19887 *slot = (void *) mac_ptr;
19888 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
19889 current_file, lh, comp_dir, section,
19890 section_is_gnu, 0,
19891 offset_size, objfile, include_hash);
19892 do_cleanups (cleanup);
19893 }
19894
19895 /* Check if the attribute's form is a DW_FORM_block*
19896 if so return true else false. */
19897
19898 static int
19899 attr_form_is_block (const struct attribute *attr)
19900 {
19901 return (attr == NULL ? 0 :
19902 attr->form == DW_FORM_block1
19903 || attr->form == DW_FORM_block2
19904 || attr->form == DW_FORM_block4
19905 || attr->form == DW_FORM_block
19906 || attr->form == DW_FORM_exprloc);
19907 }
19908
19909 /* Return non-zero if ATTR's value is a section offset --- classes
19910 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19911 You may use DW_UNSND (attr) to retrieve such offsets.
19912
19913 Section 7.5.4, "Attribute Encodings", explains that no attribute
19914 may have a value that belongs to more than one of these classes; it
19915 would be ambiguous if we did, because we use the same forms for all
19916 of them. */
19917
19918 static int
19919 attr_form_is_section_offset (const struct attribute *attr)
19920 {
19921 return (attr->form == DW_FORM_data4
19922 || attr->form == DW_FORM_data8
19923 || attr->form == DW_FORM_sec_offset);
19924 }
19925
19926 /* Return non-zero if ATTR's value falls in the 'constant' class, or
19927 zero otherwise. When this function returns true, you can apply
19928 dwarf2_get_attr_constant_value to it.
19929
19930 However, note that for some attributes you must check
19931 attr_form_is_section_offset before using this test. DW_FORM_data4
19932 and DW_FORM_data8 are members of both the constant class, and of
19933 the classes that contain offsets into other debug sections
19934 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19935 that, if an attribute's can be either a constant or one of the
19936 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19937 taken as section offsets, not constants. */
19938
19939 static int
19940 attr_form_is_constant (const struct attribute *attr)
19941 {
19942 switch (attr->form)
19943 {
19944 case DW_FORM_sdata:
19945 case DW_FORM_udata:
19946 case DW_FORM_data1:
19947 case DW_FORM_data2:
19948 case DW_FORM_data4:
19949 case DW_FORM_data8:
19950 return 1;
19951 default:
19952 return 0;
19953 }
19954 }
19955
19956
19957 /* DW_ADDR is always stored already as sect_offset; despite for the forms
19958 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
19959
19960 static int
19961 attr_form_is_ref (const struct attribute *attr)
19962 {
19963 switch (attr->form)
19964 {
19965 case DW_FORM_ref_addr:
19966 case DW_FORM_ref1:
19967 case DW_FORM_ref2:
19968 case DW_FORM_ref4:
19969 case DW_FORM_ref8:
19970 case DW_FORM_ref_udata:
19971 case DW_FORM_GNU_ref_alt:
19972 return 1;
19973 default:
19974 return 0;
19975 }
19976 }
19977
19978 /* Return the .debug_loc section to use for CU.
19979 For DWO files use .debug_loc.dwo. */
19980
19981 static struct dwarf2_section_info *
19982 cu_debug_loc_section (struct dwarf2_cu *cu)
19983 {
19984 if (cu->dwo_unit)
19985 return &cu->dwo_unit->dwo_file->sections.loc;
19986 return &dwarf2_per_objfile->loc;
19987 }
19988
19989 /* A helper function that fills in a dwarf2_loclist_baton. */
19990
19991 static void
19992 fill_in_loclist_baton (struct dwarf2_cu *cu,
19993 struct dwarf2_loclist_baton *baton,
19994 const struct attribute *attr)
19995 {
19996 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19997
19998 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
19999
20000 baton->per_cu = cu->per_cu;
20001 gdb_assert (baton->per_cu);
20002 /* We don't know how long the location list is, but make sure we
20003 don't run off the edge of the section. */
20004 baton->size = section->size - DW_UNSND (attr);
20005 baton->data = section->buffer + DW_UNSND (attr);
20006 baton->base_address = cu->base_address;
20007 baton->from_dwo = cu->dwo_unit != NULL;
20008 }
20009
20010 static void
20011 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
20012 struct dwarf2_cu *cu, int is_block)
20013 {
20014 struct objfile *objfile = dwarf2_per_objfile->objfile;
20015 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20016
20017 if (attr_form_is_section_offset (attr)
20018 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
20019 the section. If so, fall through to the complaint in the
20020 other branch. */
20021 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
20022 {
20023 struct dwarf2_loclist_baton *baton;
20024
20025 baton = obstack_alloc (&objfile->objfile_obstack,
20026 sizeof (struct dwarf2_loclist_baton));
20027
20028 fill_in_loclist_baton (cu, baton, attr);
20029
20030 if (cu->base_known == 0)
20031 complaint (&symfile_complaints,
20032 _("Location list used without "
20033 "specifying the CU base address."));
20034
20035 SYMBOL_ACLASS_INDEX (sym) = (is_block
20036 ? dwarf2_loclist_block_index
20037 : dwarf2_loclist_index);
20038 SYMBOL_LOCATION_BATON (sym) = baton;
20039 }
20040 else
20041 {
20042 struct dwarf2_locexpr_baton *baton;
20043
20044 baton = obstack_alloc (&objfile->objfile_obstack,
20045 sizeof (struct dwarf2_locexpr_baton));
20046 baton->per_cu = cu->per_cu;
20047 gdb_assert (baton->per_cu);
20048
20049 if (attr_form_is_block (attr))
20050 {
20051 /* Note that we're just copying the block's data pointer
20052 here, not the actual data. We're still pointing into the
20053 info_buffer for SYM's objfile; right now we never release
20054 that buffer, but when we do clean up properly this may
20055 need to change. */
20056 baton->size = DW_BLOCK (attr)->size;
20057 baton->data = DW_BLOCK (attr)->data;
20058 }
20059 else
20060 {
20061 dwarf2_invalid_attrib_class_complaint ("location description",
20062 SYMBOL_NATURAL_NAME (sym));
20063 baton->size = 0;
20064 }
20065
20066 SYMBOL_ACLASS_INDEX (sym) = (is_block
20067 ? dwarf2_locexpr_block_index
20068 : dwarf2_locexpr_index);
20069 SYMBOL_LOCATION_BATON (sym) = baton;
20070 }
20071 }
20072
20073 /* Return the OBJFILE associated with the compilation unit CU. If CU
20074 came from a separate debuginfo file, then the master objfile is
20075 returned. */
20076
20077 struct objfile *
20078 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
20079 {
20080 struct objfile *objfile = per_cu->objfile;
20081
20082 /* Return the master objfile, so that we can report and look up the
20083 correct file containing this variable. */
20084 if (objfile->separate_debug_objfile_backlink)
20085 objfile = objfile->separate_debug_objfile_backlink;
20086
20087 return objfile;
20088 }
20089
20090 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
20091 (CU_HEADERP is unused in such case) or prepare a temporary copy at
20092 CU_HEADERP first. */
20093
20094 static const struct comp_unit_head *
20095 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
20096 struct dwarf2_per_cu_data *per_cu)
20097 {
20098 const gdb_byte *info_ptr;
20099
20100 if (per_cu->cu)
20101 return &per_cu->cu->header;
20102
20103 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
20104
20105 memset (cu_headerp, 0, sizeof (*cu_headerp));
20106 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
20107
20108 return cu_headerp;
20109 }
20110
20111 /* Return the address size given in the compilation unit header for CU. */
20112
20113 int
20114 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
20115 {
20116 struct comp_unit_head cu_header_local;
20117 const struct comp_unit_head *cu_headerp;
20118
20119 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20120
20121 return cu_headerp->addr_size;
20122 }
20123
20124 /* Return the offset size given in the compilation unit header for CU. */
20125
20126 int
20127 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
20128 {
20129 struct comp_unit_head cu_header_local;
20130 const struct comp_unit_head *cu_headerp;
20131
20132 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20133
20134 return cu_headerp->offset_size;
20135 }
20136
20137 /* See its dwarf2loc.h declaration. */
20138
20139 int
20140 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
20141 {
20142 struct comp_unit_head cu_header_local;
20143 const struct comp_unit_head *cu_headerp;
20144
20145 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20146
20147 if (cu_headerp->version == 2)
20148 return cu_headerp->addr_size;
20149 else
20150 return cu_headerp->offset_size;
20151 }
20152
20153 /* Return the text offset of the CU. The returned offset comes from
20154 this CU's objfile. If this objfile came from a separate debuginfo
20155 file, then the offset may be different from the corresponding
20156 offset in the parent objfile. */
20157
20158 CORE_ADDR
20159 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
20160 {
20161 struct objfile *objfile = per_cu->objfile;
20162
20163 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20164 }
20165
20166 /* Locate the .debug_info compilation unit from CU's objfile which contains
20167 the DIE at OFFSET. Raises an error on failure. */
20168
20169 static struct dwarf2_per_cu_data *
20170 dwarf2_find_containing_comp_unit (sect_offset offset,
20171 unsigned int offset_in_dwz,
20172 struct objfile *objfile)
20173 {
20174 struct dwarf2_per_cu_data *this_cu;
20175 int low, high;
20176 const sect_offset *cu_off;
20177
20178 low = 0;
20179 high = dwarf2_per_objfile->n_comp_units - 1;
20180 while (high > low)
20181 {
20182 struct dwarf2_per_cu_data *mid_cu;
20183 int mid = low + (high - low) / 2;
20184
20185 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
20186 cu_off = &mid_cu->offset;
20187 if (mid_cu->is_dwz > offset_in_dwz
20188 || (mid_cu->is_dwz == offset_in_dwz
20189 && cu_off->sect_off >= offset.sect_off))
20190 high = mid;
20191 else
20192 low = mid + 1;
20193 }
20194 gdb_assert (low == high);
20195 this_cu = dwarf2_per_objfile->all_comp_units[low];
20196 cu_off = &this_cu->offset;
20197 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
20198 {
20199 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
20200 error (_("Dwarf Error: could not find partial DIE containing "
20201 "offset 0x%lx [in module %s]"),
20202 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
20203
20204 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
20205 <= offset.sect_off);
20206 return dwarf2_per_objfile->all_comp_units[low-1];
20207 }
20208 else
20209 {
20210 this_cu = dwarf2_per_objfile->all_comp_units[low];
20211 if (low == dwarf2_per_objfile->n_comp_units - 1
20212 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
20213 error (_("invalid dwarf2 offset %u"), offset.sect_off);
20214 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
20215 return this_cu;
20216 }
20217 }
20218
20219 /* Initialize dwarf2_cu CU, owned by PER_CU. */
20220
20221 static void
20222 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
20223 {
20224 memset (cu, 0, sizeof (*cu));
20225 per_cu->cu = cu;
20226 cu->per_cu = per_cu;
20227 cu->objfile = per_cu->objfile;
20228 obstack_init (&cu->comp_unit_obstack);
20229 }
20230
20231 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
20232
20233 static void
20234 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
20235 enum language pretend_language)
20236 {
20237 struct attribute *attr;
20238
20239 /* Set the language we're debugging. */
20240 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
20241 if (attr)
20242 set_cu_language (DW_UNSND (attr), cu);
20243 else
20244 {
20245 cu->language = pretend_language;
20246 cu->language_defn = language_def (cu->language);
20247 }
20248
20249 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
20250 if (attr)
20251 cu->producer = DW_STRING (attr);
20252 }
20253
20254 /* Release one cached compilation unit, CU. We unlink it from the tree
20255 of compilation units, but we don't remove it from the read_in_chain;
20256 the caller is responsible for that.
20257 NOTE: DATA is a void * because this function is also used as a
20258 cleanup routine. */
20259
20260 static void
20261 free_heap_comp_unit (void *data)
20262 {
20263 struct dwarf2_cu *cu = data;
20264
20265 gdb_assert (cu->per_cu != NULL);
20266 cu->per_cu->cu = NULL;
20267 cu->per_cu = NULL;
20268
20269 obstack_free (&cu->comp_unit_obstack, NULL);
20270
20271 xfree (cu);
20272 }
20273
20274 /* This cleanup function is passed the address of a dwarf2_cu on the stack
20275 when we're finished with it. We can't free the pointer itself, but be
20276 sure to unlink it from the cache. Also release any associated storage. */
20277
20278 static void
20279 free_stack_comp_unit (void *data)
20280 {
20281 struct dwarf2_cu *cu = data;
20282
20283 gdb_assert (cu->per_cu != NULL);
20284 cu->per_cu->cu = NULL;
20285 cu->per_cu = NULL;
20286
20287 obstack_free (&cu->comp_unit_obstack, NULL);
20288 cu->partial_dies = NULL;
20289 }
20290
20291 /* Free all cached compilation units. */
20292
20293 static void
20294 free_cached_comp_units (void *data)
20295 {
20296 struct dwarf2_per_cu_data *per_cu, **last_chain;
20297
20298 per_cu = dwarf2_per_objfile->read_in_chain;
20299 last_chain = &dwarf2_per_objfile->read_in_chain;
20300 while (per_cu != NULL)
20301 {
20302 struct dwarf2_per_cu_data *next_cu;
20303
20304 next_cu = per_cu->cu->read_in_chain;
20305
20306 free_heap_comp_unit (per_cu->cu);
20307 *last_chain = next_cu;
20308
20309 per_cu = next_cu;
20310 }
20311 }
20312
20313 /* Increase the age counter on each cached compilation unit, and free
20314 any that are too old. */
20315
20316 static void
20317 age_cached_comp_units (void)
20318 {
20319 struct dwarf2_per_cu_data *per_cu, **last_chain;
20320
20321 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
20322 per_cu = dwarf2_per_objfile->read_in_chain;
20323 while (per_cu != NULL)
20324 {
20325 per_cu->cu->last_used ++;
20326 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
20327 dwarf2_mark (per_cu->cu);
20328 per_cu = per_cu->cu->read_in_chain;
20329 }
20330
20331 per_cu = dwarf2_per_objfile->read_in_chain;
20332 last_chain = &dwarf2_per_objfile->read_in_chain;
20333 while (per_cu != NULL)
20334 {
20335 struct dwarf2_per_cu_data *next_cu;
20336
20337 next_cu = per_cu->cu->read_in_chain;
20338
20339 if (!per_cu->cu->mark)
20340 {
20341 free_heap_comp_unit (per_cu->cu);
20342 *last_chain = next_cu;
20343 }
20344 else
20345 last_chain = &per_cu->cu->read_in_chain;
20346
20347 per_cu = next_cu;
20348 }
20349 }
20350
20351 /* Remove a single compilation unit from the cache. */
20352
20353 static void
20354 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
20355 {
20356 struct dwarf2_per_cu_data *per_cu, **last_chain;
20357
20358 per_cu = dwarf2_per_objfile->read_in_chain;
20359 last_chain = &dwarf2_per_objfile->read_in_chain;
20360 while (per_cu != NULL)
20361 {
20362 struct dwarf2_per_cu_data *next_cu;
20363
20364 next_cu = per_cu->cu->read_in_chain;
20365
20366 if (per_cu == target_per_cu)
20367 {
20368 free_heap_comp_unit (per_cu->cu);
20369 per_cu->cu = NULL;
20370 *last_chain = next_cu;
20371 break;
20372 }
20373 else
20374 last_chain = &per_cu->cu->read_in_chain;
20375
20376 per_cu = next_cu;
20377 }
20378 }
20379
20380 /* Release all extra memory associated with OBJFILE. */
20381
20382 void
20383 dwarf2_free_objfile (struct objfile *objfile)
20384 {
20385 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20386
20387 if (dwarf2_per_objfile == NULL)
20388 return;
20389
20390 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
20391 free_cached_comp_units (NULL);
20392
20393 if (dwarf2_per_objfile->quick_file_names_table)
20394 htab_delete (dwarf2_per_objfile->quick_file_names_table);
20395
20396 /* Everything else should be on the objfile obstack. */
20397 }
20398
20399 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
20400 We store these in a hash table separate from the DIEs, and preserve them
20401 when the DIEs are flushed out of cache.
20402
20403 The CU "per_cu" pointer is needed because offset alone is not enough to
20404 uniquely identify the type. A file may have multiple .debug_types sections,
20405 or the type may come from a DWO file. Furthermore, while it's more logical
20406 to use per_cu->section+offset, with Fission the section with the data is in
20407 the DWO file but we don't know that section at the point we need it.
20408 We have to use something in dwarf2_per_cu_data (or the pointer to it)
20409 because we can enter the lookup routine, get_die_type_at_offset, from
20410 outside this file, and thus won't necessarily have PER_CU->cu.
20411 Fortunately, PER_CU is stable for the life of the objfile. */
20412
20413 struct dwarf2_per_cu_offset_and_type
20414 {
20415 const struct dwarf2_per_cu_data *per_cu;
20416 sect_offset offset;
20417 struct type *type;
20418 };
20419
20420 /* Hash function for a dwarf2_per_cu_offset_and_type. */
20421
20422 static hashval_t
20423 per_cu_offset_and_type_hash (const void *item)
20424 {
20425 const struct dwarf2_per_cu_offset_and_type *ofs = item;
20426
20427 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
20428 }
20429
20430 /* Equality function for a dwarf2_per_cu_offset_and_type. */
20431
20432 static int
20433 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
20434 {
20435 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
20436 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
20437
20438 return (ofs_lhs->per_cu == ofs_rhs->per_cu
20439 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
20440 }
20441
20442 /* Set the type associated with DIE to TYPE. Save it in CU's hash
20443 table if necessary. For convenience, return TYPE.
20444
20445 The DIEs reading must have careful ordering to:
20446 * Not cause infite loops trying to read in DIEs as a prerequisite for
20447 reading current DIE.
20448 * Not trying to dereference contents of still incompletely read in types
20449 while reading in other DIEs.
20450 * Enable referencing still incompletely read in types just by a pointer to
20451 the type without accessing its fields.
20452
20453 Therefore caller should follow these rules:
20454 * Try to fetch any prerequisite types we may need to build this DIE type
20455 before building the type and calling set_die_type.
20456 * After building type call set_die_type for current DIE as soon as
20457 possible before fetching more types to complete the current type.
20458 * Make the type as complete as possible before fetching more types. */
20459
20460 static struct type *
20461 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
20462 {
20463 struct dwarf2_per_cu_offset_and_type **slot, ofs;
20464 struct objfile *objfile = cu->objfile;
20465
20466 /* For Ada types, make sure that the gnat-specific data is always
20467 initialized (if not already set). There are a few types where
20468 we should not be doing so, because the type-specific area is
20469 already used to hold some other piece of info (eg: TYPE_CODE_FLT
20470 where the type-specific area is used to store the floatformat).
20471 But this is not a problem, because the gnat-specific information
20472 is actually not needed for these types. */
20473 if (need_gnat_info (cu)
20474 && TYPE_CODE (type) != TYPE_CODE_FUNC
20475 && TYPE_CODE (type) != TYPE_CODE_FLT
20476 && !HAVE_GNAT_AUX_INFO (type))
20477 INIT_GNAT_SPECIFIC (type);
20478
20479 if (dwarf2_per_objfile->die_type_hash == NULL)
20480 {
20481 dwarf2_per_objfile->die_type_hash =
20482 htab_create_alloc_ex (127,
20483 per_cu_offset_and_type_hash,
20484 per_cu_offset_and_type_eq,
20485 NULL,
20486 &objfile->objfile_obstack,
20487 hashtab_obstack_allocate,
20488 dummy_obstack_deallocate);
20489 }
20490
20491 ofs.per_cu = cu->per_cu;
20492 ofs.offset = die->offset;
20493 ofs.type = type;
20494 slot = (struct dwarf2_per_cu_offset_and_type **)
20495 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
20496 if (*slot)
20497 complaint (&symfile_complaints,
20498 _("A problem internal to GDB: DIE 0x%x has type already set"),
20499 die->offset.sect_off);
20500 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
20501 **slot = ofs;
20502 return type;
20503 }
20504
20505 /* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
20506 or return NULL if the die does not have a saved type. */
20507
20508 static struct type *
20509 get_die_type_at_offset (sect_offset offset,
20510 struct dwarf2_per_cu_data *per_cu)
20511 {
20512 struct dwarf2_per_cu_offset_and_type *slot, ofs;
20513
20514 if (dwarf2_per_objfile->die_type_hash == NULL)
20515 return NULL;
20516
20517 ofs.per_cu = per_cu;
20518 ofs.offset = offset;
20519 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
20520 if (slot)
20521 return slot->type;
20522 else
20523 return NULL;
20524 }
20525
20526 /* Look up the type for DIE in CU in die_type_hash,
20527 or return NULL if DIE does not have a saved type. */
20528
20529 static struct type *
20530 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
20531 {
20532 return get_die_type_at_offset (die->offset, cu->per_cu);
20533 }
20534
20535 /* Add a dependence relationship from CU to REF_PER_CU. */
20536
20537 static void
20538 dwarf2_add_dependence (struct dwarf2_cu *cu,
20539 struct dwarf2_per_cu_data *ref_per_cu)
20540 {
20541 void **slot;
20542
20543 if (cu->dependencies == NULL)
20544 cu->dependencies
20545 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
20546 NULL, &cu->comp_unit_obstack,
20547 hashtab_obstack_allocate,
20548 dummy_obstack_deallocate);
20549
20550 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
20551 if (*slot == NULL)
20552 *slot = ref_per_cu;
20553 }
20554
20555 /* Subroutine of dwarf2_mark to pass to htab_traverse.
20556 Set the mark field in every compilation unit in the
20557 cache that we must keep because we are keeping CU. */
20558
20559 static int
20560 dwarf2_mark_helper (void **slot, void *data)
20561 {
20562 struct dwarf2_per_cu_data *per_cu;
20563
20564 per_cu = (struct dwarf2_per_cu_data *) *slot;
20565
20566 /* cu->dependencies references may not yet have been ever read if QUIT aborts
20567 reading of the chain. As such dependencies remain valid it is not much
20568 useful to track and undo them during QUIT cleanups. */
20569 if (per_cu->cu == NULL)
20570 return 1;
20571
20572 if (per_cu->cu->mark)
20573 return 1;
20574 per_cu->cu->mark = 1;
20575
20576 if (per_cu->cu->dependencies != NULL)
20577 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
20578
20579 return 1;
20580 }
20581
20582 /* Set the mark field in CU and in every other compilation unit in the
20583 cache that we must keep because we are keeping CU. */
20584
20585 static void
20586 dwarf2_mark (struct dwarf2_cu *cu)
20587 {
20588 if (cu->mark)
20589 return;
20590 cu->mark = 1;
20591 if (cu->dependencies != NULL)
20592 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
20593 }
20594
20595 static void
20596 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
20597 {
20598 while (per_cu)
20599 {
20600 per_cu->cu->mark = 0;
20601 per_cu = per_cu->cu->read_in_chain;
20602 }
20603 }
20604
20605 /* Trivial hash function for partial_die_info: the hash value of a DIE
20606 is its offset in .debug_info for this objfile. */
20607
20608 static hashval_t
20609 partial_die_hash (const void *item)
20610 {
20611 const struct partial_die_info *part_die = item;
20612
20613 return part_die->offset.sect_off;
20614 }
20615
20616 /* Trivial comparison function for partial_die_info structures: two DIEs
20617 are equal if they have the same offset. */
20618
20619 static int
20620 partial_die_eq (const void *item_lhs, const void *item_rhs)
20621 {
20622 const struct partial_die_info *part_die_lhs = item_lhs;
20623 const struct partial_die_info *part_die_rhs = item_rhs;
20624
20625 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
20626 }
20627
20628 static struct cmd_list_element *set_dwarf2_cmdlist;
20629 static struct cmd_list_element *show_dwarf2_cmdlist;
20630
20631 static void
20632 set_dwarf2_cmd (char *args, int from_tty)
20633 {
20634 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
20635 }
20636
20637 static void
20638 show_dwarf2_cmd (char *args, int from_tty)
20639 {
20640 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
20641 }
20642
20643 /* Free data associated with OBJFILE, if necessary. */
20644
20645 static void
20646 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
20647 {
20648 struct dwarf2_per_objfile *data = d;
20649 int ix;
20650
20651 /* Make sure we don't accidentally use dwarf2_per_objfile while
20652 cleaning up. */
20653 dwarf2_per_objfile = NULL;
20654
20655 for (ix = 0; ix < data->n_comp_units; ++ix)
20656 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
20657
20658 for (ix = 0; ix < data->n_type_units; ++ix)
20659 VEC_free (dwarf2_per_cu_ptr,
20660 data->all_type_units[ix]->per_cu.imported_symtabs);
20661 xfree (data->all_type_units);
20662
20663 VEC_free (dwarf2_section_info_def, data->types);
20664
20665 if (data->dwo_files)
20666 free_dwo_files (data->dwo_files, objfile);
20667 if (data->dwp_file)
20668 gdb_bfd_unref (data->dwp_file->dbfd);
20669
20670 if (data->dwz_file && data->dwz_file->dwz_bfd)
20671 gdb_bfd_unref (data->dwz_file->dwz_bfd);
20672 }
20673
20674 \f
20675 /* The "save gdb-index" command. */
20676
20677 /* The contents of the hash table we create when building the string
20678 table. */
20679 struct strtab_entry
20680 {
20681 offset_type offset;
20682 const char *str;
20683 };
20684
20685 /* Hash function for a strtab_entry.
20686
20687 Function is used only during write_hash_table so no index format backward
20688 compatibility is needed. */
20689
20690 static hashval_t
20691 hash_strtab_entry (const void *e)
20692 {
20693 const struct strtab_entry *entry = e;
20694 return mapped_index_string_hash (INT_MAX, entry->str);
20695 }
20696
20697 /* Equality function for a strtab_entry. */
20698
20699 static int
20700 eq_strtab_entry (const void *a, const void *b)
20701 {
20702 const struct strtab_entry *ea = a;
20703 const struct strtab_entry *eb = b;
20704 return !strcmp (ea->str, eb->str);
20705 }
20706
20707 /* Create a strtab_entry hash table. */
20708
20709 static htab_t
20710 create_strtab (void)
20711 {
20712 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
20713 xfree, xcalloc, xfree);
20714 }
20715
20716 /* Add a string to the constant pool. Return the string's offset in
20717 host order. */
20718
20719 static offset_type
20720 add_string (htab_t table, struct obstack *cpool, const char *str)
20721 {
20722 void **slot;
20723 struct strtab_entry entry;
20724 struct strtab_entry *result;
20725
20726 entry.str = str;
20727 slot = htab_find_slot (table, &entry, INSERT);
20728 if (*slot)
20729 result = *slot;
20730 else
20731 {
20732 result = XNEW (struct strtab_entry);
20733 result->offset = obstack_object_size (cpool);
20734 result->str = str;
20735 obstack_grow_str0 (cpool, str);
20736 *slot = result;
20737 }
20738 return result->offset;
20739 }
20740
20741 /* An entry in the symbol table. */
20742 struct symtab_index_entry
20743 {
20744 /* The name of the symbol. */
20745 const char *name;
20746 /* The offset of the name in the constant pool. */
20747 offset_type index_offset;
20748 /* A sorted vector of the indices of all the CUs that hold an object
20749 of this name. */
20750 VEC (offset_type) *cu_indices;
20751 };
20752
20753 /* The symbol table. This is a power-of-2-sized hash table. */
20754 struct mapped_symtab
20755 {
20756 offset_type n_elements;
20757 offset_type size;
20758 struct symtab_index_entry **data;
20759 };
20760
20761 /* Hash function for a symtab_index_entry. */
20762
20763 static hashval_t
20764 hash_symtab_entry (const void *e)
20765 {
20766 const struct symtab_index_entry *entry = e;
20767 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
20768 sizeof (offset_type) * VEC_length (offset_type,
20769 entry->cu_indices),
20770 0);
20771 }
20772
20773 /* Equality function for a symtab_index_entry. */
20774
20775 static int
20776 eq_symtab_entry (const void *a, const void *b)
20777 {
20778 const struct symtab_index_entry *ea = a;
20779 const struct symtab_index_entry *eb = b;
20780 int len = VEC_length (offset_type, ea->cu_indices);
20781 if (len != VEC_length (offset_type, eb->cu_indices))
20782 return 0;
20783 return !memcmp (VEC_address (offset_type, ea->cu_indices),
20784 VEC_address (offset_type, eb->cu_indices),
20785 sizeof (offset_type) * len);
20786 }
20787
20788 /* Destroy a symtab_index_entry. */
20789
20790 static void
20791 delete_symtab_entry (void *p)
20792 {
20793 struct symtab_index_entry *entry = p;
20794 VEC_free (offset_type, entry->cu_indices);
20795 xfree (entry);
20796 }
20797
20798 /* Create a hash table holding symtab_index_entry objects. */
20799
20800 static htab_t
20801 create_symbol_hash_table (void)
20802 {
20803 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
20804 delete_symtab_entry, xcalloc, xfree);
20805 }
20806
20807 /* Create a new mapped symtab object. */
20808
20809 static struct mapped_symtab *
20810 create_mapped_symtab (void)
20811 {
20812 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
20813 symtab->n_elements = 0;
20814 symtab->size = 1024;
20815 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20816 return symtab;
20817 }
20818
20819 /* Destroy a mapped_symtab. */
20820
20821 static void
20822 cleanup_mapped_symtab (void *p)
20823 {
20824 struct mapped_symtab *symtab = p;
20825 /* The contents of the array are freed when the other hash table is
20826 destroyed. */
20827 xfree (symtab->data);
20828 xfree (symtab);
20829 }
20830
20831 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
20832 the slot.
20833
20834 Function is used only during write_hash_table so no index format backward
20835 compatibility is needed. */
20836
20837 static struct symtab_index_entry **
20838 find_slot (struct mapped_symtab *symtab, const char *name)
20839 {
20840 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
20841
20842 index = hash & (symtab->size - 1);
20843 step = ((hash * 17) & (symtab->size - 1)) | 1;
20844
20845 for (;;)
20846 {
20847 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
20848 return &symtab->data[index];
20849 index = (index + step) & (symtab->size - 1);
20850 }
20851 }
20852
20853 /* Expand SYMTAB's hash table. */
20854
20855 static void
20856 hash_expand (struct mapped_symtab *symtab)
20857 {
20858 offset_type old_size = symtab->size;
20859 offset_type i;
20860 struct symtab_index_entry **old_entries = symtab->data;
20861
20862 symtab->size *= 2;
20863 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20864
20865 for (i = 0; i < old_size; ++i)
20866 {
20867 if (old_entries[i])
20868 {
20869 struct symtab_index_entry **slot = find_slot (symtab,
20870 old_entries[i]->name);
20871 *slot = old_entries[i];
20872 }
20873 }
20874
20875 xfree (old_entries);
20876 }
20877
20878 /* Add an entry to SYMTAB. NAME is the name of the symbol.
20879 CU_INDEX is the index of the CU in which the symbol appears.
20880 IS_STATIC is one if the symbol is static, otherwise zero (global). */
20881
20882 static void
20883 add_index_entry (struct mapped_symtab *symtab, const char *name,
20884 int is_static, gdb_index_symbol_kind kind,
20885 offset_type cu_index)
20886 {
20887 struct symtab_index_entry **slot;
20888 offset_type cu_index_and_attrs;
20889
20890 ++symtab->n_elements;
20891 if (4 * symtab->n_elements / 3 >= symtab->size)
20892 hash_expand (symtab);
20893
20894 slot = find_slot (symtab, name);
20895 if (!*slot)
20896 {
20897 *slot = XNEW (struct symtab_index_entry);
20898 (*slot)->name = name;
20899 /* index_offset is set later. */
20900 (*slot)->cu_indices = NULL;
20901 }
20902
20903 cu_index_and_attrs = 0;
20904 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
20905 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
20906 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
20907
20908 /* We don't want to record an index value twice as we want to avoid the
20909 duplication.
20910 We process all global symbols and then all static symbols
20911 (which would allow us to avoid the duplication by only having to check
20912 the last entry pushed), but a symbol could have multiple kinds in one CU.
20913 To keep things simple we don't worry about the duplication here and
20914 sort and uniqufy the list after we've processed all symbols. */
20915 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
20916 }
20917
20918 /* qsort helper routine for uniquify_cu_indices. */
20919
20920 static int
20921 offset_type_compare (const void *ap, const void *bp)
20922 {
20923 offset_type a = *(offset_type *) ap;
20924 offset_type b = *(offset_type *) bp;
20925
20926 return (a > b) - (b > a);
20927 }
20928
20929 /* Sort and remove duplicates of all symbols' cu_indices lists. */
20930
20931 static void
20932 uniquify_cu_indices (struct mapped_symtab *symtab)
20933 {
20934 int i;
20935
20936 for (i = 0; i < symtab->size; ++i)
20937 {
20938 struct symtab_index_entry *entry = symtab->data[i];
20939
20940 if (entry
20941 && entry->cu_indices != NULL)
20942 {
20943 unsigned int next_to_insert, next_to_check;
20944 offset_type last_value;
20945
20946 qsort (VEC_address (offset_type, entry->cu_indices),
20947 VEC_length (offset_type, entry->cu_indices),
20948 sizeof (offset_type), offset_type_compare);
20949
20950 last_value = VEC_index (offset_type, entry->cu_indices, 0);
20951 next_to_insert = 1;
20952 for (next_to_check = 1;
20953 next_to_check < VEC_length (offset_type, entry->cu_indices);
20954 ++next_to_check)
20955 {
20956 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20957 != last_value)
20958 {
20959 last_value = VEC_index (offset_type, entry->cu_indices,
20960 next_to_check);
20961 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20962 last_value);
20963 ++next_to_insert;
20964 }
20965 }
20966 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20967 }
20968 }
20969 }
20970
20971 /* Add a vector of indices to the constant pool. */
20972
20973 static offset_type
20974 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
20975 struct symtab_index_entry *entry)
20976 {
20977 void **slot;
20978
20979 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
20980 if (!*slot)
20981 {
20982 offset_type len = VEC_length (offset_type, entry->cu_indices);
20983 offset_type val = MAYBE_SWAP (len);
20984 offset_type iter;
20985 int i;
20986
20987 *slot = entry;
20988 entry->index_offset = obstack_object_size (cpool);
20989
20990 obstack_grow (cpool, &val, sizeof (val));
20991 for (i = 0;
20992 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20993 ++i)
20994 {
20995 val = MAYBE_SWAP (iter);
20996 obstack_grow (cpool, &val, sizeof (val));
20997 }
20998 }
20999 else
21000 {
21001 struct symtab_index_entry *old_entry = *slot;
21002 entry->index_offset = old_entry->index_offset;
21003 entry = old_entry;
21004 }
21005 return entry->index_offset;
21006 }
21007
21008 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
21009 constant pool entries going into the obstack CPOOL. */
21010
21011 static void
21012 write_hash_table (struct mapped_symtab *symtab,
21013 struct obstack *output, struct obstack *cpool)
21014 {
21015 offset_type i;
21016 htab_t symbol_hash_table;
21017 htab_t str_table;
21018
21019 symbol_hash_table = create_symbol_hash_table ();
21020 str_table = create_strtab ();
21021
21022 /* We add all the index vectors to the constant pool first, to
21023 ensure alignment is ok. */
21024 for (i = 0; i < symtab->size; ++i)
21025 {
21026 if (symtab->data[i])
21027 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
21028 }
21029
21030 /* Now write out the hash table. */
21031 for (i = 0; i < symtab->size; ++i)
21032 {
21033 offset_type str_off, vec_off;
21034
21035 if (symtab->data[i])
21036 {
21037 str_off = add_string (str_table, cpool, symtab->data[i]->name);
21038 vec_off = symtab->data[i]->index_offset;
21039 }
21040 else
21041 {
21042 /* While 0 is a valid constant pool index, it is not valid
21043 to have 0 for both offsets. */
21044 str_off = 0;
21045 vec_off = 0;
21046 }
21047
21048 str_off = MAYBE_SWAP (str_off);
21049 vec_off = MAYBE_SWAP (vec_off);
21050
21051 obstack_grow (output, &str_off, sizeof (str_off));
21052 obstack_grow (output, &vec_off, sizeof (vec_off));
21053 }
21054
21055 htab_delete (str_table);
21056 htab_delete (symbol_hash_table);
21057 }
21058
21059 /* Struct to map psymtab to CU index in the index file. */
21060 struct psymtab_cu_index_map
21061 {
21062 struct partial_symtab *psymtab;
21063 unsigned int cu_index;
21064 };
21065
21066 static hashval_t
21067 hash_psymtab_cu_index (const void *item)
21068 {
21069 const struct psymtab_cu_index_map *map = item;
21070
21071 return htab_hash_pointer (map->psymtab);
21072 }
21073
21074 static int
21075 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
21076 {
21077 const struct psymtab_cu_index_map *lhs = item_lhs;
21078 const struct psymtab_cu_index_map *rhs = item_rhs;
21079
21080 return lhs->psymtab == rhs->psymtab;
21081 }
21082
21083 /* Helper struct for building the address table. */
21084 struct addrmap_index_data
21085 {
21086 struct objfile *objfile;
21087 struct obstack *addr_obstack;
21088 htab_t cu_index_htab;
21089
21090 /* Non-zero if the previous_* fields are valid.
21091 We can't write an entry until we see the next entry (since it is only then
21092 that we know the end of the entry). */
21093 int previous_valid;
21094 /* Index of the CU in the table of all CUs in the index file. */
21095 unsigned int previous_cu_index;
21096 /* Start address of the CU. */
21097 CORE_ADDR previous_cu_start;
21098 };
21099
21100 /* Write an address entry to OBSTACK. */
21101
21102 static void
21103 add_address_entry (struct objfile *objfile, struct obstack *obstack,
21104 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
21105 {
21106 offset_type cu_index_to_write;
21107 gdb_byte addr[8];
21108 CORE_ADDR baseaddr;
21109
21110 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21111
21112 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
21113 obstack_grow (obstack, addr, 8);
21114 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
21115 obstack_grow (obstack, addr, 8);
21116 cu_index_to_write = MAYBE_SWAP (cu_index);
21117 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
21118 }
21119
21120 /* Worker function for traversing an addrmap to build the address table. */
21121
21122 static int
21123 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
21124 {
21125 struct addrmap_index_data *data = datap;
21126 struct partial_symtab *pst = obj;
21127
21128 if (data->previous_valid)
21129 add_address_entry (data->objfile, data->addr_obstack,
21130 data->previous_cu_start, start_addr,
21131 data->previous_cu_index);
21132
21133 data->previous_cu_start = start_addr;
21134 if (pst != NULL)
21135 {
21136 struct psymtab_cu_index_map find_map, *map;
21137 find_map.psymtab = pst;
21138 map = htab_find (data->cu_index_htab, &find_map);
21139 gdb_assert (map != NULL);
21140 data->previous_cu_index = map->cu_index;
21141 data->previous_valid = 1;
21142 }
21143 else
21144 data->previous_valid = 0;
21145
21146 return 0;
21147 }
21148
21149 /* Write OBJFILE's address map to OBSTACK.
21150 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
21151 in the index file. */
21152
21153 static void
21154 write_address_map (struct objfile *objfile, struct obstack *obstack,
21155 htab_t cu_index_htab)
21156 {
21157 struct addrmap_index_data addrmap_index_data;
21158
21159 /* When writing the address table, we have to cope with the fact that
21160 the addrmap iterator only provides the start of a region; we have to
21161 wait until the next invocation to get the start of the next region. */
21162
21163 addrmap_index_data.objfile = objfile;
21164 addrmap_index_data.addr_obstack = obstack;
21165 addrmap_index_data.cu_index_htab = cu_index_htab;
21166 addrmap_index_data.previous_valid = 0;
21167
21168 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
21169 &addrmap_index_data);
21170
21171 /* It's highly unlikely the last entry (end address = 0xff...ff)
21172 is valid, but we should still handle it.
21173 The end address is recorded as the start of the next region, but that
21174 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
21175 anyway. */
21176 if (addrmap_index_data.previous_valid)
21177 add_address_entry (objfile, obstack,
21178 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
21179 addrmap_index_data.previous_cu_index);
21180 }
21181
21182 /* Return the symbol kind of PSYM. */
21183
21184 static gdb_index_symbol_kind
21185 symbol_kind (struct partial_symbol *psym)
21186 {
21187 domain_enum domain = PSYMBOL_DOMAIN (psym);
21188 enum address_class aclass = PSYMBOL_CLASS (psym);
21189
21190 switch (domain)
21191 {
21192 case VAR_DOMAIN:
21193 switch (aclass)
21194 {
21195 case LOC_BLOCK:
21196 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
21197 case LOC_TYPEDEF:
21198 return GDB_INDEX_SYMBOL_KIND_TYPE;
21199 case LOC_COMPUTED:
21200 case LOC_CONST_BYTES:
21201 case LOC_OPTIMIZED_OUT:
21202 case LOC_STATIC:
21203 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21204 case LOC_CONST:
21205 /* Note: It's currently impossible to recognize psyms as enum values
21206 short of reading the type info. For now punt. */
21207 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21208 default:
21209 /* There are other LOC_FOO values that one might want to classify
21210 as variables, but dwarf2read.c doesn't currently use them. */
21211 return GDB_INDEX_SYMBOL_KIND_OTHER;
21212 }
21213 case STRUCT_DOMAIN:
21214 return GDB_INDEX_SYMBOL_KIND_TYPE;
21215 default:
21216 return GDB_INDEX_SYMBOL_KIND_OTHER;
21217 }
21218 }
21219
21220 /* Add a list of partial symbols to SYMTAB. */
21221
21222 static void
21223 write_psymbols (struct mapped_symtab *symtab,
21224 htab_t psyms_seen,
21225 struct partial_symbol **psymp,
21226 int count,
21227 offset_type cu_index,
21228 int is_static)
21229 {
21230 for (; count-- > 0; ++psymp)
21231 {
21232 struct partial_symbol *psym = *psymp;
21233 void **slot;
21234
21235 if (SYMBOL_LANGUAGE (psym) == language_ada)
21236 error (_("Ada is not currently supported by the index"));
21237
21238 /* Only add a given psymbol once. */
21239 slot = htab_find_slot (psyms_seen, psym, INSERT);
21240 if (!*slot)
21241 {
21242 gdb_index_symbol_kind kind = symbol_kind (psym);
21243
21244 *slot = psym;
21245 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
21246 is_static, kind, cu_index);
21247 }
21248 }
21249 }
21250
21251 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
21252 exception if there is an error. */
21253
21254 static void
21255 write_obstack (FILE *file, struct obstack *obstack)
21256 {
21257 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
21258 file)
21259 != obstack_object_size (obstack))
21260 error (_("couldn't data write to file"));
21261 }
21262
21263 /* Unlink a file if the argument is not NULL. */
21264
21265 static void
21266 unlink_if_set (void *p)
21267 {
21268 char **filename = p;
21269 if (*filename)
21270 unlink (*filename);
21271 }
21272
21273 /* A helper struct used when iterating over debug_types. */
21274 struct signatured_type_index_data
21275 {
21276 struct objfile *objfile;
21277 struct mapped_symtab *symtab;
21278 struct obstack *types_list;
21279 htab_t psyms_seen;
21280 int cu_index;
21281 };
21282
21283 /* A helper function that writes a single signatured_type to an
21284 obstack. */
21285
21286 static int
21287 write_one_signatured_type (void **slot, void *d)
21288 {
21289 struct signatured_type_index_data *info = d;
21290 struct signatured_type *entry = (struct signatured_type *) *slot;
21291 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
21292 gdb_byte val[8];
21293
21294 write_psymbols (info->symtab,
21295 info->psyms_seen,
21296 info->objfile->global_psymbols.list
21297 + psymtab->globals_offset,
21298 psymtab->n_global_syms, info->cu_index,
21299 0);
21300 write_psymbols (info->symtab,
21301 info->psyms_seen,
21302 info->objfile->static_psymbols.list
21303 + psymtab->statics_offset,
21304 psymtab->n_static_syms, info->cu_index,
21305 1);
21306
21307 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21308 entry->per_cu.offset.sect_off);
21309 obstack_grow (info->types_list, val, 8);
21310 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21311 entry->type_offset_in_tu.cu_off);
21312 obstack_grow (info->types_list, val, 8);
21313 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
21314 obstack_grow (info->types_list, val, 8);
21315
21316 ++info->cu_index;
21317
21318 return 1;
21319 }
21320
21321 /* Recurse into all "included" dependencies and write their symbols as
21322 if they appeared in this psymtab. */
21323
21324 static void
21325 recursively_write_psymbols (struct objfile *objfile,
21326 struct partial_symtab *psymtab,
21327 struct mapped_symtab *symtab,
21328 htab_t psyms_seen,
21329 offset_type cu_index)
21330 {
21331 int i;
21332
21333 for (i = 0; i < psymtab->number_of_dependencies; ++i)
21334 if (psymtab->dependencies[i]->user != NULL)
21335 recursively_write_psymbols (objfile, psymtab->dependencies[i],
21336 symtab, psyms_seen, cu_index);
21337
21338 write_psymbols (symtab,
21339 psyms_seen,
21340 objfile->global_psymbols.list + psymtab->globals_offset,
21341 psymtab->n_global_syms, cu_index,
21342 0);
21343 write_psymbols (symtab,
21344 psyms_seen,
21345 objfile->static_psymbols.list + psymtab->statics_offset,
21346 psymtab->n_static_syms, cu_index,
21347 1);
21348 }
21349
21350 /* Create an index file for OBJFILE in the directory DIR. */
21351
21352 static void
21353 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
21354 {
21355 struct cleanup *cleanup;
21356 char *filename, *cleanup_filename;
21357 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
21358 struct obstack cu_list, types_cu_list;
21359 int i;
21360 FILE *out_file;
21361 struct mapped_symtab *symtab;
21362 offset_type val, size_of_contents, total_len;
21363 struct stat st;
21364 htab_t psyms_seen;
21365 htab_t cu_index_htab;
21366 struct psymtab_cu_index_map *psymtab_cu_index_map;
21367
21368 if (dwarf2_per_objfile->using_index)
21369 error (_("Cannot use an index to create the index"));
21370
21371 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
21372 error (_("Cannot make an index when the file has multiple .debug_types sections"));
21373
21374 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
21375 return;
21376
21377 if (stat (objfile_name (objfile), &st) < 0)
21378 perror_with_name (objfile_name (objfile));
21379
21380 filename = concat (dir, SLASH_STRING, lbasename (objfile_name (objfile)),
21381 INDEX_SUFFIX, (char *) NULL);
21382 cleanup = make_cleanup (xfree, filename);
21383
21384 out_file = gdb_fopen_cloexec (filename, "wb");
21385 if (!out_file)
21386 error (_("Can't open `%s' for writing"), filename);
21387
21388 cleanup_filename = filename;
21389 make_cleanup (unlink_if_set, &cleanup_filename);
21390
21391 symtab = create_mapped_symtab ();
21392 make_cleanup (cleanup_mapped_symtab, symtab);
21393
21394 obstack_init (&addr_obstack);
21395 make_cleanup_obstack_free (&addr_obstack);
21396
21397 obstack_init (&cu_list);
21398 make_cleanup_obstack_free (&cu_list);
21399
21400 obstack_init (&types_cu_list);
21401 make_cleanup_obstack_free (&types_cu_list);
21402
21403 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
21404 NULL, xcalloc, xfree);
21405 make_cleanup_htab_delete (psyms_seen);
21406
21407 /* While we're scanning CU's create a table that maps a psymtab pointer
21408 (which is what addrmap records) to its index (which is what is recorded
21409 in the index file). This will later be needed to write the address
21410 table. */
21411 cu_index_htab = htab_create_alloc (100,
21412 hash_psymtab_cu_index,
21413 eq_psymtab_cu_index,
21414 NULL, xcalloc, xfree);
21415 make_cleanup_htab_delete (cu_index_htab);
21416 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
21417 xmalloc (sizeof (struct psymtab_cu_index_map)
21418 * dwarf2_per_objfile->n_comp_units);
21419 make_cleanup (xfree, psymtab_cu_index_map);
21420
21421 /* The CU list is already sorted, so we don't need to do additional
21422 work here. Also, the debug_types entries do not appear in
21423 all_comp_units, but only in their own hash table. */
21424 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
21425 {
21426 struct dwarf2_per_cu_data *per_cu
21427 = dwarf2_per_objfile->all_comp_units[i];
21428 struct partial_symtab *psymtab = per_cu->v.psymtab;
21429 gdb_byte val[8];
21430 struct psymtab_cu_index_map *map;
21431 void **slot;
21432
21433 /* CU of a shared file from 'dwz -m' may be unused by this main file.
21434 It may be referenced from a local scope but in such case it does not
21435 need to be present in .gdb_index. */
21436 if (psymtab == NULL)
21437 continue;
21438
21439 if (psymtab->user == NULL)
21440 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
21441
21442 map = &psymtab_cu_index_map[i];
21443 map->psymtab = psymtab;
21444 map->cu_index = i;
21445 slot = htab_find_slot (cu_index_htab, map, INSERT);
21446 gdb_assert (slot != NULL);
21447 gdb_assert (*slot == NULL);
21448 *slot = map;
21449
21450 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21451 per_cu->offset.sect_off);
21452 obstack_grow (&cu_list, val, 8);
21453 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
21454 obstack_grow (&cu_list, val, 8);
21455 }
21456
21457 /* Dump the address map. */
21458 write_address_map (objfile, &addr_obstack, cu_index_htab);
21459
21460 /* Write out the .debug_type entries, if any. */
21461 if (dwarf2_per_objfile->signatured_types)
21462 {
21463 struct signatured_type_index_data sig_data;
21464
21465 sig_data.objfile = objfile;
21466 sig_data.symtab = symtab;
21467 sig_data.types_list = &types_cu_list;
21468 sig_data.psyms_seen = psyms_seen;
21469 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
21470 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
21471 write_one_signatured_type, &sig_data);
21472 }
21473
21474 /* Now that we've processed all symbols we can shrink their cu_indices
21475 lists. */
21476 uniquify_cu_indices (symtab);
21477
21478 obstack_init (&constant_pool);
21479 make_cleanup_obstack_free (&constant_pool);
21480 obstack_init (&symtab_obstack);
21481 make_cleanup_obstack_free (&symtab_obstack);
21482 write_hash_table (symtab, &symtab_obstack, &constant_pool);
21483
21484 obstack_init (&contents);
21485 make_cleanup_obstack_free (&contents);
21486 size_of_contents = 6 * sizeof (offset_type);
21487 total_len = size_of_contents;
21488
21489 /* The version number. */
21490 val = MAYBE_SWAP (8);
21491 obstack_grow (&contents, &val, sizeof (val));
21492
21493 /* The offset of the CU list from the start of the file. */
21494 val = MAYBE_SWAP (total_len);
21495 obstack_grow (&contents, &val, sizeof (val));
21496 total_len += obstack_object_size (&cu_list);
21497
21498 /* The offset of the types CU list from the start of the file. */
21499 val = MAYBE_SWAP (total_len);
21500 obstack_grow (&contents, &val, sizeof (val));
21501 total_len += obstack_object_size (&types_cu_list);
21502
21503 /* The offset of the address table from the start of the file. */
21504 val = MAYBE_SWAP (total_len);
21505 obstack_grow (&contents, &val, sizeof (val));
21506 total_len += obstack_object_size (&addr_obstack);
21507
21508 /* The offset of the symbol table from the start of the file. */
21509 val = MAYBE_SWAP (total_len);
21510 obstack_grow (&contents, &val, sizeof (val));
21511 total_len += obstack_object_size (&symtab_obstack);
21512
21513 /* The offset of the constant pool from the start of the file. */
21514 val = MAYBE_SWAP (total_len);
21515 obstack_grow (&contents, &val, sizeof (val));
21516 total_len += obstack_object_size (&constant_pool);
21517
21518 gdb_assert (obstack_object_size (&contents) == size_of_contents);
21519
21520 write_obstack (out_file, &contents);
21521 write_obstack (out_file, &cu_list);
21522 write_obstack (out_file, &types_cu_list);
21523 write_obstack (out_file, &addr_obstack);
21524 write_obstack (out_file, &symtab_obstack);
21525 write_obstack (out_file, &constant_pool);
21526
21527 fclose (out_file);
21528
21529 /* We want to keep the file, so we set cleanup_filename to NULL
21530 here. See unlink_if_set. */
21531 cleanup_filename = NULL;
21532
21533 do_cleanups (cleanup);
21534 }
21535
21536 /* Implementation of the `save gdb-index' command.
21537
21538 Note that the file format used by this command is documented in the
21539 GDB manual. Any changes here must be documented there. */
21540
21541 static void
21542 save_gdb_index_command (char *arg, int from_tty)
21543 {
21544 struct objfile *objfile;
21545
21546 if (!arg || !*arg)
21547 error (_("usage: save gdb-index DIRECTORY"));
21548
21549 ALL_OBJFILES (objfile)
21550 {
21551 struct stat st;
21552
21553 /* If the objfile does not correspond to an actual file, skip it. */
21554 if (stat (objfile_name (objfile), &st) < 0)
21555 continue;
21556
21557 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
21558 if (dwarf2_per_objfile)
21559 {
21560 volatile struct gdb_exception except;
21561
21562 TRY_CATCH (except, RETURN_MASK_ERROR)
21563 {
21564 write_psymtabs_to_index (objfile, arg);
21565 }
21566 if (except.reason < 0)
21567 exception_fprintf (gdb_stderr, except,
21568 _("Error while writing index for `%s': "),
21569 objfile_name (objfile));
21570 }
21571 }
21572 }
21573
21574 \f
21575
21576 int dwarf2_always_disassemble;
21577
21578 static void
21579 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
21580 struct cmd_list_element *c, const char *value)
21581 {
21582 fprintf_filtered (file,
21583 _("Whether to always disassemble "
21584 "DWARF expressions is %s.\n"),
21585 value);
21586 }
21587
21588 static void
21589 show_check_physname (struct ui_file *file, int from_tty,
21590 struct cmd_list_element *c, const char *value)
21591 {
21592 fprintf_filtered (file,
21593 _("Whether to check \"physname\" is %s.\n"),
21594 value);
21595 }
21596
21597 void _initialize_dwarf2_read (void);
21598
21599 void
21600 _initialize_dwarf2_read (void)
21601 {
21602 struct cmd_list_element *c;
21603
21604 dwarf2_objfile_data_key
21605 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
21606
21607 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
21608 Set DWARF 2 specific variables.\n\
21609 Configure DWARF 2 variables such as the cache size"),
21610 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
21611 0/*allow-unknown*/, &maintenance_set_cmdlist);
21612
21613 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
21614 Show DWARF 2 specific variables\n\
21615 Show DWARF 2 variables such as the cache size"),
21616 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
21617 0/*allow-unknown*/, &maintenance_show_cmdlist);
21618
21619 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
21620 &dwarf2_max_cache_age, _("\
21621 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
21622 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
21623 A higher limit means that cached compilation units will be stored\n\
21624 in memory longer, and more total memory will be used. Zero disables\n\
21625 caching, which can slow down startup."),
21626 NULL,
21627 show_dwarf2_max_cache_age,
21628 &set_dwarf2_cmdlist,
21629 &show_dwarf2_cmdlist);
21630
21631 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
21632 &dwarf2_always_disassemble, _("\
21633 Set whether `info address' always disassembles DWARF expressions."), _("\
21634 Show whether `info address' always disassembles DWARF expressions."), _("\
21635 When enabled, DWARF expressions are always printed in an assembly-like\n\
21636 syntax. When disabled, expressions will be printed in a more\n\
21637 conversational style, when possible."),
21638 NULL,
21639 show_dwarf2_always_disassemble,
21640 &set_dwarf2_cmdlist,
21641 &show_dwarf2_cmdlist);
21642
21643 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
21644 Set debugging of the dwarf2 reader."), _("\
21645 Show debugging of the dwarf2 reader."), _("\
21646 When enabled, debugging messages are printed during dwarf2 reading\n\
21647 and symtab expansion."),
21648 NULL,
21649 NULL,
21650 &setdebuglist, &showdebuglist);
21651
21652 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
21653 Set debugging of the dwarf2 DIE reader."), _("\
21654 Show debugging of the dwarf2 DIE reader."), _("\
21655 When enabled (non-zero), DIEs are dumped after they are read in.\n\
21656 The value is the maximum depth to print."),
21657 NULL,
21658 NULL,
21659 &setdebuglist, &showdebuglist);
21660
21661 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
21662 Set cross-checking of \"physname\" code against demangler."), _("\
21663 Show cross-checking of \"physname\" code against demangler."), _("\
21664 When enabled, GDB's internal \"physname\" code is checked against\n\
21665 the demangler."),
21666 NULL, show_check_physname,
21667 &setdebuglist, &showdebuglist);
21668
21669 add_setshow_boolean_cmd ("use-deprecated-index-sections",
21670 no_class, &use_deprecated_index_sections, _("\
21671 Set whether to use deprecated gdb_index sections."), _("\
21672 Show whether to use deprecated gdb_index sections."), _("\
21673 When enabled, deprecated .gdb_index sections are used anyway.\n\
21674 Normally they are ignored either because of a missing feature or\n\
21675 performance issue.\n\
21676 Warning: This option must be enabled before gdb reads the file."),
21677 NULL,
21678 NULL,
21679 &setlist, &showlist);
21680
21681 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
21682 _("\
21683 Save a gdb-index file.\n\
21684 Usage: save gdb-index DIRECTORY"),
21685 &save_cmdlist);
21686 set_cmd_completer (c, filename_completer);
21687
21688 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
21689 &dwarf2_locexpr_funcs);
21690 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
21691 &dwarf2_loclist_funcs);
21692
21693 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
21694 &dwarf2_block_frame_base_locexpr_funcs);
21695 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
21696 &dwarf2_block_frame_base_loclist_funcs);
21697 }
This page took 0.441645 seconds and 5 git commands to generate.