This patch implements the new gdbarch method gdbarch_gdb_signal_to_target.
[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 /* Backchain to our per_cu entry if the tree has been built. */
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 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
615 };
616
617 /* Entry in the signatured_types hash table. */
618
619 struct signatured_type
620 {
621 /* The "per_cu" object of this type.
622 This struct is used iff per_cu.is_debug_types.
623 N.B.: This is the first member so that it's easy to convert pointers
624 between them. */
625 struct dwarf2_per_cu_data per_cu;
626
627 /* The type's signature. */
628 ULONGEST signature;
629
630 /* Offset in the TU of the type's DIE, as read from the TU header.
631 If this TU is a DWO stub and the definition lives in a DWO file
632 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
633 cu_offset type_offset_in_tu;
634
635 /* Offset in the section of the type's DIE.
636 If the definition lives in a DWO file, this is the offset in the
637 .debug_types.dwo section.
638 The value is zero until the actual value is known.
639 Zero is otherwise not a valid section offset. */
640 sect_offset type_offset_in_section;
641
642 /* Type units are grouped by their DW_AT_stmt_list entry so that they
643 can share them. This points to the containing symtab. */
644 struct type_unit_group *type_unit_group;
645
646 /* The type.
647 The first time we encounter this type we fully read it in and install it
648 in the symbol tables. Subsequent times we only need the type. */
649 struct type *type;
650
651 /* Containing DWO unit.
652 This field is valid iff per_cu.reading_dwo_directly. */
653 struct dwo_unit *dwo_unit;
654 };
655
656 typedef struct signatured_type *sig_type_ptr;
657 DEF_VEC_P (sig_type_ptr);
658
659 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
660 This includes type_unit_group and quick_file_names. */
661
662 struct stmt_list_hash
663 {
664 /* The DWO unit this table is from or NULL if there is none. */
665 struct dwo_unit *dwo_unit;
666
667 /* Offset in .debug_line or .debug_line.dwo. */
668 sect_offset line_offset;
669 };
670
671 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
672 an object of this type. */
673
674 struct type_unit_group
675 {
676 /* dwarf2read.c's main "handle" on a TU symtab.
677 To simplify things we create an artificial CU that "includes" all the
678 type units using this stmt_list so that the rest of the code still has
679 a "per_cu" handle on the symtab.
680 This PER_CU is recognized by having no section. */
681 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
682 struct dwarf2_per_cu_data per_cu;
683
684 /* The TUs that share this DW_AT_stmt_list entry.
685 This is added to while parsing type units to build partial symtabs,
686 and is deleted afterwards and not used again. */
687 VEC (sig_type_ptr) *tus;
688
689 /* The primary symtab.
690 Type units in a group needn't all be defined in the same source file,
691 so we create an essentially anonymous symtab as the primary symtab. */
692 struct symtab *primary_symtab;
693
694 /* The data used to construct the hash key. */
695 struct stmt_list_hash hash;
696
697 /* The number of symtabs from the line header.
698 The value here must match line_header.num_file_names. */
699 unsigned int num_symtabs;
700
701 /* The symbol tables for this TU (obtained from the files listed in
702 DW_AT_stmt_list).
703 WARNING: The order of entries here must match the order of entries
704 in the line header. After the first TU using this type_unit_group, the
705 line header for the subsequent TUs is recreated from this. This is done
706 because we need to use the same symtabs for each TU using the same
707 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
708 there's no guarantee the line header doesn't have duplicate entries. */
709 struct symtab **symtabs;
710 };
711
712 /* These sections are what may appear in a DWO file. */
713
714 struct dwo_sections
715 {
716 struct dwarf2_section_info abbrev;
717 struct dwarf2_section_info line;
718 struct dwarf2_section_info loc;
719 struct dwarf2_section_info macinfo;
720 struct dwarf2_section_info macro;
721 struct dwarf2_section_info str;
722 struct dwarf2_section_info str_offsets;
723 /* In the case of a virtual DWO file, these two are unused. */
724 struct dwarf2_section_info info;
725 VEC (dwarf2_section_info_def) *types;
726 };
727
728 /* CUs/TUs in DWP/DWO files. */
729
730 struct dwo_unit
731 {
732 /* Backlink to the containing struct dwo_file. */
733 struct dwo_file *dwo_file;
734
735 /* The "id" that distinguishes this CU/TU.
736 .debug_info calls this "dwo_id", .debug_types calls this "signature".
737 Since signatures came first, we stick with it for consistency. */
738 ULONGEST signature;
739
740 /* The section this CU/TU lives in, in the DWO file. */
741 struct dwarf2_section_info *section;
742
743 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
744 sect_offset offset;
745 unsigned int length;
746
747 /* For types, offset in the type's DIE of the type defined by this TU. */
748 cu_offset type_offset_in_tu;
749 };
750
751 /* Data for one DWO file.
752 This includes virtual DWO files that have been packaged into a
753 DWP file. */
754
755 struct dwo_file
756 {
757 /* The DW_AT_GNU_dwo_name attribute.
758 For virtual DWO files the name is constructed from the section offsets
759 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
760 from related CU+TUs. */
761 const char *dwo_name;
762
763 /* The DW_AT_comp_dir attribute. */
764 const char *comp_dir;
765
766 /* The bfd, when the file is open. Otherwise this is NULL.
767 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
768 bfd *dbfd;
769
770 /* Section info for this file. */
771 struct dwo_sections sections;
772
773 /* The CU in the file.
774 We only support one because having more than one requires hacking the
775 dwo_name of each to match, which is highly unlikely to happen.
776 Doing this means all TUs can share comp_dir: We also assume that
777 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
778 struct dwo_unit *cu;
779
780 /* Table of TUs in the file.
781 Each element is a struct dwo_unit. */
782 htab_t tus;
783 };
784
785 /* These sections are what may appear in a DWP file. */
786
787 struct dwp_sections
788 {
789 struct dwarf2_section_info str;
790 struct dwarf2_section_info cu_index;
791 struct dwarf2_section_info tu_index;
792 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
793 by section number. We don't need to record them here. */
794 };
795
796 /* These sections are what may appear in a virtual DWO file. */
797
798 struct virtual_dwo_sections
799 {
800 struct dwarf2_section_info abbrev;
801 struct dwarf2_section_info line;
802 struct dwarf2_section_info loc;
803 struct dwarf2_section_info macinfo;
804 struct dwarf2_section_info macro;
805 struct dwarf2_section_info str_offsets;
806 /* Each DWP hash table entry records one CU or one TU.
807 That is recorded here, and copied to dwo_unit.section. */
808 struct dwarf2_section_info info_or_types;
809 };
810
811 /* Contents of DWP hash tables. */
812
813 struct dwp_hash_table
814 {
815 uint32_t nr_units, nr_slots;
816 const gdb_byte *hash_table, *unit_table, *section_pool;
817 };
818
819 /* Data for one DWP file. */
820
821 struct dwp_file
822 {
823 /* Name of the file. */
824 const char *name;
825
826 /* The bfd. */
827 bfd *dbfd;
828
829 /* Section info for this file. */
830 struct dwp_sections sections;
831
832 /* Table of CUs in the file. */
833 const struct dwp_hash_table *cus;
834
835 /* Table of TUs in the file. */
836 const struct dwp_hash_table *tus;
837
838 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
839 htab_t loaded_cutus;
840
841 /* Table to map ELF section numbers to their sections. */
842 unsigned int num_sections;
843 asection **elf_sections;
844 };
845
846 /* This represents a '.dwz' file. */
847
848 struct dwz_file
849 {
850 /* A dwz file can only contain a few sections. */
851 struct dwarf2_section_info abbrev;
852 struct dwarf2_section_info info;
853 struct dwarf2_section_info str;
854 struct dwarf2_section_info line;
855 struct dwarf2_section_info macro;
856 struct dwarf2_section_info gdb_index;
857
858 /* The dwz's BFD. */
859 bfd *dwz_bfd;
860 };
861
862 /* Struct used to pass misc. parameters to read_die_and_children, et
863 al. which are used for both .debug_info and .debug_types dies.
864 All parameters here are unchanging for the life of the call. This
865 struct exists to abstract away the constant parameters of die reading. */
866
867 struct die_reader_specs
868 {
869 /* die_section->asection->owner. */
870 bfd* abfd;
871
872 /* The CU of the DIE we are parsing. */
873 struct dwarf2_cu *cu;
874
875 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
876 struct dwo_file *dwo_file;
877
878 /* The section the die comes from.
879 This is either .debug_info or .debug_types, or the .dwo variants. */
880 struct dwarf2_section_info *die_section;
881
882 /* die_section->buffer. */
883 const gdb_byte *buffer;
884
885 /* The end of the buffer. */
886 const gdb_byte *buffer_end;
887
888 /* The value of the DW_AT_comp_dir attribute. */
889 const char *comp_dir;
890 };
891
892 /* Type of function passed to init_cutu_and_read_dies, et.al. */
893 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
894 const gdb_byte *info_ptr,
895 struct die_info *comp_unit_die,
896 int has_children,
897 void *data);
898
899 /* The line number information for a compilation unit (found in the
900 .debug_line section) begins with a "statement program header",
901 which contains the following information. */
902 struct line_header
903 {
904 unsigned int total_length;
905 unsigned short version;
906 unsigned int header_length;
907 unsigned char minimum_instruction_length;
908 unsigned char maximum_ops_per_instruction;
909 unsigned char default_is_stmt;
910 int line_base;
911 unsigned char line_range;
912 unsigned char opcode_base;
913
914 /* standard_opcode_lengths[i] is the number of operands for the
915 standard opcode whose value is i. This means that
916 standard_opcode_lengths[0] is unused, and the last meaningful
917 element is standard_opcode_lengths[opcode_base - 1]. */
918 unsigned char *standard_opcode_lengths;
919
920 /* The include_directories table. NOTE! These strings are not
921 allocated with xmalloc; instead, they are pointers into
922 debug_line_buffer. If you try to free them, `free' will get
923 indigestion. */
924 unsigned int num_include_dirs, include_dirs_size;
925 const char **include_dirs;
926
927 /* The file_names table. NOTE! These strings are not allocated
928 with xmalloc; instead, they are pointers into debug_line_buffer.
929 Don't try to free them directly. */
930 unsigned int num_file_names, file_names_size;
931 struct file_entry
932 {
933 const char *name;
934 unsigned int dir_index;
935 unsigned int mod_time;
936 unsigned int length;
937 int included_p; /* Non-zero if referenced by the Line Number Program. */
938 struct symtab *symtab; /* The associated symbol table, if any. */
939 } *file_names;
940
941 /* The start and end of the statement program following this
942 header. These point into dwarf2_per_objfile->line_buffer. */
943 const gdb_byte *statement_program_start, *statement_program_end;
944 };
945
946 /* When we construct a partial symbol table entry we only
947 need this much information. */
948 struct partial_die_info
949 {
950 /* Offset of this DIE. */
951 sect_offset offset;
952
953 /* DWARF-2 tag for this DIE. */
954 ENUM_BITFIELD(dwarf_tag) tag : 16;
955
956 /* Assorted flags describing the data found in this DIE. */
957 unsigned int has_children : 1;
958 unsigned int is_external : 1;
959 unsigned int is_declaration : 1;
960 unsigned int has_type : 1;
961 unsigned int has_specification : 1;
962 unsigned int has_pc_info : 1;
963 unsigned int may_be_inlined : 1;
964
965 /* Flag set if the SCOPE field of this structure has been
966 computed. */
967 unsigned int scope_set : 1;
968
969 /* Flag set if the DIE has a byte_size attribute. */
970 unsigned int has_byte_size : 1;
971
972 /* Flag set if any of the DIE's children are template arguments. */
973 unsigned int has_template_arguments : 1;
974
975 /* Flag set if fixup_partial_die has been called on this die. */
976 unsigned int fixup_called : 1;
977
978 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
979 unsigned int is_dwz : 1;
980
981 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
982 unsigned int spec_is_dwz : 1;
983
984 /* The name of this DIE. Normally the value of DW_AT_name, but
985 sometimes a default name for unnamed DIEs. */
986 const char *name;
987
988 /* The linkage name, if present. */
989 const char *linkage_name;
990
991 /* The scope to prepend to our children. This is generally
992 allocated on the comp_unit_obstack, so will disappear
993 when this compilation unit leaves the cache. */
994 const char *scope;
995
996 /* Some data associated with the partial DIE. The tag determines
997 which field is live. */
998 union
999 {
1000 /* The location description associated with this DIE, if any. */
1001 struct dwarf_block *locdesc;
1002 /* The offset of an import, for DW_TAG_imported_unit. */
1003 sect_offset offset;
1004 } d;
1005
1006 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1007 CORE_ADDR lowpc;
1008 CORE_ADDR highpc;
1009
1010 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1011 DW_AT_sibling, if any. */
1012 /* NOTE: This member isn't strictly necessary, read_partial_die could
1013 return DW_AT_sibling values to its caller load_partial_dies. */
1014 const gdb_byte *sibling;
1015
1016 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1017 DW_AT_specification (or DW_AT_abstract_origin or
1018 DW_AT_extension). */
1019 sect_offset spec_offset;
1020
1021 /* Pointers to this DIE's parent, first child, and next sibling,
1022 if any. */
1023 struct partial_die_info *die_parent, *die_child, *die_sibling;
1024 };
1025
1026 /* This data structure holds the information of an abbrev. */
1027 struct abbrev_info
1028 {
1029 unsigned int number; /* number identifying abbrev */
1030 enum dwarf_tag tag; /* dwarf tag */
1031 unsigned short has_children; /* boolean */
1032 unsigned short num_attrs; /* number of attributes */
1033 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1034 struct abbrev_info *next; /* next in chain */
1035 };
1036
1037 struct attr_abbrev
1038 {
1039 ENUM_BITFIELD(dwarf_attribute) name : 16;
1040 ENUM_BITFIELD(dwarf_form) form : 16;
1041 };
1042
1043 /* Size of abbrev_table.abbrev_hash_table. */
1044 #define ABBREV_HASH_SIZE 121
1045
1046 /* Top level data structure to contain an abbreviation table. */
1047
1048 struct abbrev_table
1049 {
1050 /* Where the abbrev table came from.
1051 This is used as a sanity check when the table is used. */
1052 sect_offset offset;
1053
1054 /* Storage for the abbrev table. */
1055 struct obstack abbrev_obstack;
1056
1057 /* Hash table of abbrevs.
1058 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1059 It could be statically allocated, but the previous code didn't so we
1060 don't either. */
1061 struct abbrev_info **abbrevs;
1062 };
1063
1064 /* Attributes have a name and a value. */
1065 struct attribute
1066 {
1067 ENUM_BITFIELD(dwarf_attribute) name : 16;
1068 ENUM_BITFIELD(dwarf_form) form : 15;
1069
1070 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1071 field should be in u.str (existing only for DW_STRING) but it is kept
1072 here for better struct attribute alignment. */
1073 unsigned int string_is_canonical : 1;
1074
1075 union
1076 {
1077 const char *str;
1078 struct dwarf_block *blk;
1079 ULONGEST unsnd;
1080 LONGEST snd;
1081 CORE_ADDR addr;
1082 ULONGEST signature;
1083 }
1084 u;
1085 };
1086
1087 /* This data structure holds a complete die structure. */
1088 struct die_info
1089 {
1090 /* DWARF-2 tag for this DIE. */
1091 ENUM_BITFIELD(dwarf_tag) tag : 16;
1092
1093 /* Number of attributes */
1094 unsigned char num_attrs;
1095
1096 /* True if we're presently building the full type name for the
1097 type derived from this DIE. */
1098 unsigned char building_fullname : 1;
1099
1100 /* Abbrev number */
1101 unsigned int abbrev;
1102
1103 /* Offset in .debug_info or .debug_types section. */
1104 sect_offset offset;
1105
1106 /* The dies in a compilation unit form an n-ary tree. PARENT
1107 points to this die's parent; CHILD points to the first child of
1108 this node; and all the children of a given node are chained
1109 together via their SIBLING fields. */
1110 struct die_info *child; /* Its first child, if any. */
1111 struct die_info *sibling; /* Its next sibling, if any. */
1112 struct die_info *parent; /* Its parent, if any. */
1113
1114 /* An array of attributes, with NUM_ATTRS elements. There may be
1115 zero, but it's not common and zero-sized arrays are not
1116 sufficiently portable C. */
1117 struct attribute attrs[1];
1118 };
1119
1120 /* Get at parts of an attribute structure. */
1121
1122 #define DW_STRING(attr) ((attr)->u.str)
1123 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1124 #define DW_UNSND(attr) ((attr)->u.unsnd)
1125 #define DW_BLOCK(attr) ((attr)->u.blk)
1126 #define DW_SND(attr) ((attr)->u.snd)
1127 #define DW_ADDR(attr) ((attr)->u.addr)
1128 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1129
1130 /* Blocks are a bunch of untyped bytes. */
1131 struct dwarf_block
1132 {
1133 size_t size;
1134
1135 /* Valid only if SIZE is not zero. */
1136 const gdb_byte *data;
1137 };
1138
1139 #ifndef ATTR_ALLOC_CHUNK
1140 #define ATTR_ALLOC_CHUNK 4
1141 #endif
1142
1143 /* Allocate fields for structs, unions and enums in this size. */
1144 #ifndef DW_FIELD_ALLOC_CHUNK
1145 #define DW_FIELD_ALLOC_CHUNK 4
1146 #endif
1147
1148 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1149 but this would require a corresponding change in unpack_field_as_long
1150 and friends. */
1151 static int bits_per_byte = 8;
1152
1153 /* The routines that read and process dies for a C struct or C++ class
1154 pass lists of data member fields and lists of member function fields
1155 in an instance of a field_info structure, as defined below. */
1156 struct field_info
1157 {
1158 /* List of data member and baseclasses fields. */
1159 struct nextfield
1160 {
1161 struct nextfield *next;
1162 int accessibility;
1163 int virtuality;
1164 struct field field;
1165 }
1166 *fields, *baseclasses;
1167
1168 /* Number of fields (including baseclasses). */
1169 int nfields;
1170
1171 /* Number of baseclasses. */
1172 int nbaseclasses;
1173
1174 /* Set if the accesibility of one of the fields is not public. */
1175 int non_public_fields;
1176
1177 /* Member function fields array, entries are allocated in the order they
1178 are encountered in the object file. */
1179 struct nextfnfield
1180 {
1181 struct nextfnfield *next;
1182 struct fn_field fnfield;
1183 }
1184 *fnfields;
1185
1186 /* Member function fieldlist array, contains name of possibly overloaded
1187 member function, number of overloaded member functions and a pointer
1188 to the head of the member function field chain. */
1189 struct fnfieldlist
1190 {
1191 const char *name;
1192 int length;
1193 struct nextfnfield *head;
1194 }
1195 *fnfieldlists;
1196
1197 /* Number of entries in the fnfieldlists array. */
1198 int nfnfields;
1199
1200 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1201 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1202 struct typedef_field_list
1203 {
1204 struct typedef_field field;
1205 struct typedef_field_list *next;
1206 }
1207 *typedef_field_list;
1208 unsigned typedef_field_list_count;
1209 };
1210
1211 /* One item on the queue of compilation units to read in full symbols
1212 for. */
1213 struct dwarf2_queue_item
1214 {
1215 struct dwarf2_per_cu_data *per_cu;
1216 enum language pretend_language;
1217 struct dwarf2_queue_item *next;
1218 };
1219
1220 /* The current queue. */
1221 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1222
1223 /* Loaded secondary compilation units are kept in memory until they
1224 have not been referenced for the processing of this many
1225 compilation units. Set this to zero to disable caching. Cache
1226 sizes of up to at least twenty will improve startup time for
1227 typical inter-CU-reference binaries, at an obvious memory cost. */
1228 static int dwarf2_max_cache_age = 5;
1229 static void
1230 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1231 struct cmd_list_element *c, const char *value)
1232 {
1233 fprintf_filtered (file, _("The upper bound on the age of cached "
1234 "dwarf2 compilation units is %s.\n"),
1235 value);
1236 }
1237
1238
1239 /* Various complaints about symbol reading that don't abort the process. */
1240
1241 static void
1242 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1243 {
1244 complaint (&symfile_complaints,
1245 _("statement list doesn't fit in .debug_line section"));
1246 }
1247
1248 static void
1249 dwarf2_debug_line_missing_file_complaint (void)
1250 {
1251 complaint (&symfile_complaints,
1252 _(".debug_line section has line data without a file"));
1253 }
1254
1255 static void
1256 dwarf2_debug_line_missing_end_sequence_complaint (void)
1257 {
1258 complaint (&symfile_complaints,
1259 _(".debug_line section has line "
1260 "program sequence without an end"));
1261 }
1262
1263 static void
1264 dwarf2_complex_location_expr_complaint (void)
1265 {
1266 complaint (&symfile_complaints, _("location expression too complex"));
1267 }
1268
1269 static void
1270 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1271 int arg3)
1272 {
1273 complaint (&symfile_complaints,
1274 _("const value length mismatch for '%s', got %d, expected %d"),
1275 arg1, arg2, arg3);
1276 }
1277
1278 static void
1279 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1280 {
1281 complaint (&symfile_complaints,
1282 _("debug info runs off end of %s section"
1283 " [in module %s]"),
1284 section->asection->name,
1285 bfd_get_filename (section->asection->owner));
1286 }
1287
1288 static void
1289 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1290 {
1291 complaint (&symfile_complaints,
1292 _("macro debug info contains a "
1293 "malformed macro definition:\n`%s'"),
1294 arg1);
1295 }
1296
1297 static void
1298 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1299 {
1300 complaint (&symfile_complaints,
1301 _("invalid attribute class or form for '%s' in '%s'"),
1302 arg1, arg2);
1303 }
1304
1305 /* local function prototypes */
1306
1307 static void dwarf2_locate_sections (bfd *, asection *, void *);
1308
1309 static void dwarf2_find_base_address (struct die_info *die,
1310 struct dwarf2_cu *cu);
1311
1312 static struct partial_symtab *create_partial_symtab
1313 (struct dwarf2_per_cu_data *per_cu, const char *name);
1314
1315 static void dwarf2_build_psymtabs_hard (struct objfile *);
1316
1317 static void scan_partial_symbols (struct partial_die_info *,
1318 CORE_ADDR *, CORE_ADDR *,
1319 int, struct dwarf2_cu *);
1320
1321 static void add_partial_symbol (struct partial_die_info *,
1322 struct dwarf2_cu *);
1323
1324 static void add_partial_namespace (struct partial_die_info *pdi,
1325 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1326 int need_pc, struct dwarf2_cu *cu);
1327
1328 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1329 CORE_ADDR *highpc, int need_pc,
1330 struct dwarf2_cu *cu);
1331
1332 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1333 struct dwarf2_cu *cu);
1334
1335 static void add_partial_subprogram (struct partial_die_info *pdi,
1336 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1337 int need_pc, struct dwarf2_cu *cu);
1338
1339 static void dwarf2_read_symtab (struct partial_symtab *,
1340 struct objfile *);
1341
1342 static void psymtab_to_symtab_1 (struct partial_symtab *);
1343
1344 static struct abbrev_info *abbrev_table_lookup_abbrev
1345 (const struct abbrev_table *, unsigned int);
1346
1347 static struct abbrev_table *abbrev_table_read_table
1348 (struct dwarf2_section_info *, sect_offset);
1349
1350 static void abbrev_table_free (struct abbrev_table *);
1351
1352 static void abbrev_table_free_cleanup (void *);
1353
1354 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1355 struct dwarf2_section_info *);
1356
1357 static void dwarf2_free_abbrev_table (void *);
1358
1359 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1360
1361 static struct partial_die_info *load_partial_dies
1362 (const struct die_reader_specs *, const gdb_byte *, int);
1363
1364 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1365 struct partial_die_info *,
1366 struct abbrev_info *,
1367 unsigned int,
1368 const gdb_byte *);
1369
1370 static struct partial_die_info *find_partial_die (sect_offset, int,
1371 struct dwarf2_cu *);
1372
1373 static void fixup_partial_die (struct partial_die_info *,
1374 struct dwarf2_cu *);
1375
1376 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1377 struct attribute *, struct attr_abbrev *,
1378 const gdb_byte *);
1379
1380 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1381
1382 static int read_1_signed_byte (bfd *, const gdb_byte *);
1383
1384 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1385
1386 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1387
1388 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1389
1390 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1391 unsigned int *);
1392
1393 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1394
1395 static LONGEST read_checked_initial_length_and_offset
1396 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1397 unsigned int *, unsigned int *);
1398
1399 static LONGEST read_offset (bfd *, const gdb_byte *,
1400 const struct comp_unit_head *,
1401 unsigned int *);
1402
1403 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1404
1405 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1406 sect_offset);
1407
1408 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1409
1410 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1411
1412 static const char *read_indirect_string (bfd *, const gdb_byte *,
1413 const struct comp_unit_head *,
1414 unsigned int *);
1415
1416 static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1417
1418 static ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);
1419
1420 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1421
1422 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1423 const gdb_byte *,
1424 unsigned int *);
1425
1426 static const char *read_str_index (const struct die_reader_specs *reader,
1427 struct dwarf2_cu *cu, ULONGEST str_index);
1428
1429 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1430
1431 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1432 struct dwarf2_cu *);
1433
1434 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1435 unsigned int);
1436
1437 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1438 struct dwarf2_cu *cu);
1439
1440 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1441
1442 static struct die_info *die_specification (struct die_info *die,
1443 struct dwarf2_cu **);
1444
1445 static void free_line_header (struct line_header *lh);
1446
1447 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1448 struct dwarf2_cu *cu);
1449
1450 static void dwarf_decode_lines (struct line_header *, const char *,
1451 struct dwarf2_cu *, struct partial_symtab *,
1452 int);
1453
1454 static void dwarf2_start_subfile (const char *, const char *, const char *);
1455
1456 static void dwarf2_start_symtab (struct dwarf2_cu *,
1457 const char *, const char *, CORE_ADDR);
1458
1459 static struct symbol *new_symbol (struct die_info *, struct type *,
1460 struct dwarf2_cu *);
1461
1462 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1463 struct dwarf2_cu *, struct symbol *);
1464
1465 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1466 struct dwarf2_cu *);
1467
1468 static void dwarf2_const_value_attr (const struct attribute *attr,
1469 struct type *type,
1470 const char *name,
1471 struct obstack *obstack,
1472 struct dwarf2_cu *cu, LONGEST *value,
1473 const gdb_byte **bytes,
1474 struct dwarf2_locexpr_baton **baton);
1475
1476 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1477
1478 static int need_gnat_info (struct dwarf2_cu *);
1479
1480 static struct type *die_descriptive_type (struct die_info *,
1481 struct dwarf2_cu *);
1482
1483 static void set_descriptive_type (struct type *, struct die_info *,
1484 struct dwarf2_cu *);
1485
1486 static struct type *die_containing_type (struct die_info *,
1487 struct dwarf2_cu *);
1488
1489 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1490 struct dwarf2_cu *);
1491
1492 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1493
1494 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1495
1496 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1497
1498 static char *typename_concat (struct obstack *obs, const char *prefix,
1499 const char *suffix, int physname,
1500 struct dwarf2_cu *cu);
1501
1502 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1503
1504 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1505
1506 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1507
1508 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1509
1510 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1511
1512 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1513 struct dwarf2_cu *, struct partial_symtab *);
1514
1515 static int dwarf2_get_pc_bounds (struct die_info *,
1516 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1517 struct partial_symtab *);
1518
1519 static void get_scope_pc_bounds (struct die_info *,
1520 CORE_ADDR *, CORE_ADDR *,
1521 struct dwarf2_cu *);
1522
1523 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1524 CORE_ADDR, struct dwarf2_cu *);
1525
1526 static void dwarf2_add_field (struct field_info *, struct die_info *,
1527 struct dwarf2_cu *);
1528
1529 static void dwarf2_attach_fields_to_type (struct field_info *,
1530 struct type *, struct dwarf2_cu *);
1531
1532 static void dwarf2_add_member_fn (struct field_info *,
1533 struct die_info *, struct type *,
1534 struct dwarf2_cu *);
1535
1536 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1537 struct type *,
1538 struct dwarf2_cu *);
1539
1540 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1541
1542 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1543
1544 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1545
1546 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1547
1548 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1549
1550 static struct type *read_module_type (struct die_info *die,
1551 struct dwarf2_cu *cu);
1552
1553 static const char *namespace_name (struct die_info *die,
1554 int *is_anonymous, struct dwarf2_cu *);
1555
1556 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1557
1558 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1559
1560 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1561 struct dwarf2_cu *);
1562
1563 static struct die_info *read_die_and_siblings_1
1564 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1565 struct die_info *);
1566
1567 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1568 const gdb_byte *info_ptr,
1569 const gdb_byte **new_info_ptr,
1570 struct die_info *parent);
1571
1572 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1573 struct die_info **, const gdb_byte *,
1574 int *, int);
1575
1576 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1577 struct die_info **, const gdb_byte *,
1578 int *);
1579
1580 static void process_die (struct die_info *, struct dwarf2_cu *);
1581
1582 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1583 struct obstack *);
1584
1585 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1586
1587 static const char *dwarf2_full_name (const char *name,
1588 struct die_info *die,
1589 struct dwarf2_cu *cu);
1590
1591 static const char *dwarf2_physname (const char *name, struct die_info *die,
1592 struct dwarf2_cu *cu);
1593
1594 static struct die_info *dwarf2_extension (struct die_info *die,
1595 struct dwarf2_cu **);
1596
1597 static const char *dwarf_tag_name (unsigned int);
1598
1599 static const char *dwarf_attr_name (unsigned int);
1600
1601 static const char *dwarf_form_name (unsigned int);
1602
1603 static char *dwarf_bool_name (unsigned int);
1604
1605 static const char *dwarf_type_encoding_name (unsigned int);
1606
1607 static struct die_info *sibling_die (struct die_info *);
1608
1609 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1610
1611 static void dump_die_for_error (struct die_info *);
1612
1613 static void dump_die_1 (struct ui_file *, int level, int max_level,
1614 struct die_info *);
1615
1616 /*static*/ void dump_die (struct die_info *, int max_level);
1617
1618 static void store_in_ref_table (struct die_info *,
1619 struct dwarf2_cu *);
1620
1621 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1622
1623 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1624
1625 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1626 const struct attribute *,
1627 struct dwarf2_cu **);
1628
1629 static struct die_info *follow_die_ref (struct die_info *,
1630 const struct attribute *,
1631 struct dwarf2_cu **);
1632
1633 static struct die_info *follow_die_sig (struct die_info *,
1634 const struct attribute *,
1635 struct dwarf2_cu **);
1636
1637 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1638 struct dwarf2_cu *);
1639
1640 static struct type *get_DW_AT_signature_type (struct die_info *,
1641 const struct attribute *,
1642 struct dwarf2_cu *);
1643
1644 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1645
1646 static void read_signatured_type (struct signatured_type *);
1647
1648 static struct type_unit_group *get_type_unit_group
1649 (struct dwarf2_cu *, const struct attribute *);
1650
1651 static void build_type_unit_groups (die_reader_func_ftype *, void *);
1652
1653 /* memory allocation interface */
1654
1655 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1656
1657 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1658
1659 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1660 const char *, int);
1661
1662 static int attr_form_is_block (const struct attribute *);
1663
1664 static int attr_form_is_section_offset (const struct attribute *);
1665
1666 static int attr_form_is_constant (const struct attribute *);
1667
1668 static int attr_form_is_ref (const struct attribute *);
1669
1670 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1671 struct dwarf2_loclist_baton *baton,
1672 const struct attribute *attr);
1673
1674 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1675 struct symbol *sym,
1676 struct dwarf2_cu *cu,
1677 int is_block);
1678
1679 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1680 const gdb_byte *info_ptr,
1681 struct abbrev_info *abbrev);
1682
1683 static void free_stack_comp_unit (void *);
1684
1685 static hashval_t partial_die_hash (const void *item);
1686
1687 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1688
1689 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1690 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1691
1692 static void init_one_comp_unit (struct dwarf2_cu *cu,
1693 struct dwarf2_per_cu_data *per_cu);
1694
1695 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1696 struct die_info *comp_unit_die,
1697 enum language pretend_language);
1698
1699 static void free_heap_comp_unit (void *);
1700
1701 static void free_cached_comp_units (void *);
1702
1703 static void age_cached_comp_units (void);
1704
1705 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1706
1707 static struct type *set_die_type (struct die_info *, struct type *,
1708 struct dwarf2_cu *);
1709
1710 static void create_all_comp_units (struct objfile *);
1711
1712 static int create_all_type_units (struct objfile *);
1713
1714 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1715 enum language);
1716
1717 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1718 enum language);
1719
1720 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1721 enum language);
1722
1723 static void dwarf2_add_dependence (struct dwarf2_cu *,
1724 struct dwarf2_per_cu_data *);
1725
1726 static void dwarf2_mark (struct dwarf2_cu *);
1727
1728 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1729
1730 static struct type *get_die_type_at_offset (sect_offset,
1731 struct dwarf2_per_cu_data *);
1732
1733 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1734
1735 static void dwarf2_release_queue (void *dummy);
1736
1737 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1738 enum language pretend_language);
1739
1740 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1741 struct dwarf2_per_cu_data *per_cu,
1742 enum language pretend_language);
1743
1744 static void process_queue (void);
1745
1746 static void find_file_and_directory (struct die_info *die,
1747 struct dwarf2_cu *cu,
1748 const char **name, const char **comp_dir);
1749
1750 static char *file_full_name (int file, struct line_header *lh,
1751 const char *comp_dir);
1752
1753 static const gdb_byte *read_and_check_comp_unit_head
1754 (struct comp_unit_head *header,
1755 struct dwarf2_section_info *section,
1756 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1757 int is_debug_types_section);
1758
1759 static void init_cutu_and_read_dies
1760 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1761 int use_existing_cu, int keep,
1762 die_reader_func_ftype *die_reader_func, void *data);
1763
1764 static void init_cutu_and_read_dies_simple
1765 (struct dwarf2_per_cu_data *this_cu,
1766 die_reader_func_ftype *die_reader_func, void *data);
1767
1768 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1769
1770 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1771
1772 static struct dwo_unit *lookup_dwo_in_dwp
1773 (struct dwp_file *dwp_file, const struct dwp_hash_table *htab,
1774 const char *comp_dir, ULONGEST signature, int is_debug_types);
1775
1776 static struct dwp_file *get_dwp_file (void);
1777
1778 static struct dwo_unit *lookup_dwo_comp_unit
1779 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1780
1781 static struct dwo_unit *lookup_dwo_type_unit
1782 (struct signatured_type *, const char *, const char *);
1783
1784 static void free_dwo_file_cleanup (void *);
1785
1786 static void process_cu_includes (void);
1787
1788 static void check_producer (struct dwarf2_cu *cu);
1789
1790 #if WORDS_BIGENDIAN
1791
1792 /* Convert VALUE between big- and little-endian. */
1793 static offset_type
1794 byte_swap (offset_type value)
1795 {
1796 offset_type result;
1797
1798 result = (value & 0xff) << 24;
1799 result |= (value & 0xff00) << 8;
1800 result |= (value & 0xff0000) >> 8;
1801 result |= (value & 0xff000000) >> 24;
1802 return result;
1803 }
1804
1805 #define MAYBE_SWAP(V) byte_swap (V)
1806
1807 #else
1808 #define MAYBE_SWAP(V) (V)
1809 #endif /* WORDS_BIGENDIAN */
1810
1811 /* The suffix for an index file. */
1812 #define INDEX_SUFFIX ".gdb-index"
1813
1814 /* Try to locate the sections we need for DWARF 2 debugging
1815 information and return true if we have enough to do something.
1816 NAMES points to the dwarf2 section names, or is NULL if the standard
1817 ELF names are used. */
1818
1819 int
1820 dwarf2_has_info (struct objfile *objfile,
1821 const struct dwarf2_debug_sections *names)
1822 {
1823 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1824 if (!dwarf2_per_objfile)
1825 {
1826 /* Initialize per-objfile state. */
1827 struct dwarf2_per_objfile *data
1828 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1829
1830 memset (data, 0, sizeof (*data));
1831 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1832 dwarf2_per_objfile = data;
1833
1834 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1835 (void *) names);
1836 dwarf2_per_objfile->objfile = objfile;
1837 }
1838 return (dwarf2_per_objfile->info.asection != NULL
1839 && dwarf2_per_objfile->abbrev.asection != NULL);
1840 }
1841
1842 /* When loading sections, we look either for uncompressed section or for
1843 compressed section names. */
1844
1845 static int
1846 section_is_p (const char *section_name,
1847 const struct dwarf2_section_names *names)
1848 {
1849 if (names->normal != NULL
1850 && strcmp (section_name, names->normal) == 0)
1851 return 1;
1852 if (names->compressed != NULL
1853 && strcmp (section_name, names->compressed) == 0)
1854 return 1;
1855 return 0;
1856 }
1857
1858 /* This function is mapped across the sections and remembers the
1859 offset and size of each of the debugging sections we are interested
1860 in. */
1861
1862 static void
1863 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1864 {
1865 const struct dwarf2_debug_sections *names;
1866 flagword aflag = bfd_get_section_flags (abfd, sectp);
1867
1868 if (vnames == NULL)
1869 names = &dwarf2_elf_names;
1870 else
1871 names = (const struct dwarf2_debug_sections *) vnames;
1872
1873 if ((aflag & SEC_HAS_CONTENTS) == 0)
1874 {
1875 }
1876 else if (section_is_p (sectp->name, &names->info))
1877 {
1878 dwarf2_per_objfile->info.asection = sectp;
1879 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1880 }
1881 else if (section_is_p (sectp->name, &names->abbrev))
1882 {
1883 dwarf2_per_objfile->abbrev.asection = sectp;
1884 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1885 }
1886 else if (section_is_p (sectp->name, &names->line))
1887 {
1888 dwarf2_per_objfile->line.asection = sectp;
1889 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1890 }
1891 else if (section_is_p (sectp->name, &names->loc))
1892 {
1893 dwarf2_per_objfile->loc.asection = sectp;
1894 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1895 }
1896 else if (section_is_p (sectp->name, &names->macinfo))
1897 {
1898 dwarf2_per_objfile->macinfo.asection = sectp;
1899 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1900 }
1901 else if (section_is_p (sectp->name, &names->macro))
1902 {
1903 dwarf2_per_objfile->macro.asection = sectp;
1904 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1905 }
1906 else if (section_is_p (sectp->name, &names->str))
1907 {
1908 dwarf2_per_objfile->str.asection = sectp;
1909 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1910 }
1911 else if (section_is_p (sectp->name, &names->addr))
1912 {
1913 dwarf2_per_objfile->addr.asection = sectp;
1914 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1915 }
1916 else if (section_is_p (sectp->name, &names->frame))
1917 {
1918 dwarf2_per_objfile->frame.asection = sectp;
1919 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1920 }
1921 else if (section_is_p (sectp->name, &names->eh_frame))
1922 {
1923 dwarf2_per_objfile->eh_frame.asection = sectp;
1924 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1925 }
1926 else if (section_is_p (sectp->name, &names->ranges))
1927 {
1928 dwarf2_per_objfile->ranges.asection = sectp;
1929 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1930 }
1931 else if (section_is_p (sectp->name, &names->types))
1932 {
1933 struct dwarf2_section_info type_section;
1934
1935 memset (&type_section, 0, sizeof (type_section));
1936 type_section.asection = sectp;
1937 type_section.size = bfd_get_section_size (sectp);
1938
1939 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1940 &type_section);
1941 }
1942 else if (section_is_p (sectp->name, &names->gdb_index))
1943 {
1944 dwarf2_per_objfile->gdb_index.asection = sectp;
1945 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1946 }
1947
1948 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1949 && bfd_section_vma (abfd, sectp) == 0)
1950 dwarf2_per_objfile->has_section_at_zero = 1;
1951 }
1952
1953 /* A helper function that decides whether a section is empty,
1954 or not present. */
1955
1956 static int
1957 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1958 {
1959 return info->asection == NULL || info->size == 0;
1960 }
1961
1962 /* Read the contents of the section INFO.
1963 OBJFILE is the main object file, but not necessarily the file where
1964 the section comes from. E.g., for DWO files INFO->asection->owner
1965 is the bfd of the DWO file.
1966 If the section is compressed, uncompress it before returning. */
1967
1968 static void
1969 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1970 {
1971 asection *sectp = info->asection;
1972 bfd *abfd;
1973 gdb_byte *buf, *retbuf;
1974 unsigned char header[4];
1975
1976 if (info->readin)
1977 return;
1978 info->buffer = NULL;
1979 info->readin = 1;
1980
1981 if (dwarf2_section_empty_p (info))
1982 return;
1983
1984 abfd = sectp->owner;
1985
1986 /* If the section has relocations, we must read it ourselves.
1987 Otherwise we attach it to the BFD. */
1988 if ((sectp->flags & SEC_RELOC) == 0)
1989 {
1990 info->buffer = gdb_bfd_map_section (sectp, &info->size);
1991 return;
1992 }
1993
1994 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1995 info->buffer = buf;
1996
1997 /* When debugging .o files, we may need to apply relocations; see
1998 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1999 We never compress sections in .o files, so we only need to
2000 try this when the section is not compressed. */
2001 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2002 if (retbuf != NULL)
2003 {
2004 info->buffer = retbuf;
2005 return;
2006 }
2007
2008 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2009 || bfd_bread (buf, info->size, abfd) != info->size)
2010 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
2011 bfd_get_filename (abfd));
2012 }
2013
2014 /* A helper function that returns the size of a section in a safe way.
2015 If you are positive that the section has been read before using the
2016 size, then it is safe to refer to the dwarf2_section_info object's
2017 "size" field directly. In other cases, you must call this
2018 function, because for compressed sections the size field is not set
2019 correctly until the section has been read. */
2020
2021 static bfd_size_type
2022 dwarf2_section_size (struct objfile *objfile,
2023 struct dwarf2_section_info *info)
2024 {
2025 if (!info->readin)
2026 dwarf2_read_section (objfile, info);
2027 return info->size;
2028 }
2029
2030 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2031 SECTION_NAME. */
2032
2033 void
2034 dwarf2_get_section_info (struct objfile *objfile,
2035 enum dwarf2_section_enum sect,
2036 asection **sectp, const gdb_byte **bufp,
2037 bfd_size_type *sizep)
2038 {
2039 struct dwarf2_per_objfile *data
2040 = objfile_data (objfile, dwarf2_objfile_data_key);
2041 struct dwarf2_section_info *info;
2042
2043 /* We may see an objfile without any DWARF, in which case we just
2044 return nothing. */
2045 if (data == NULL)
2046 {
2047 *sectp = NULL;
2048 *bufp = NULL;
2049 *sizep = 0;
2050 return;
2051 }
2052 switch (sect)
2053 {
2054 case DWARF2_DEBUG_FRAME:
2055 info = &data->frame;
2056 break;
2057 case DWARF2_EH_FRAME:
2058 info = &data->eh_frame;
2059 break;
2060 default:
2061 gdb_assert_not_reached ("unexpected section");
2062 }
2063
2064 dwarf2_read_section (objfile, info);
2065
2066 *sectp = info->asection;
2067 *bufp = info->buffer;
2068 *sizep = info->size;
2069 }
2070
2071 /* A helper function to find the sections for a .dwz file. */
2072
2073 static void
2074 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2075 {
2076 struct dwz_file *dwz_file = arg;
2077
2078 /* Note that we only support the standard ELF names, because .dwz
2079 is ELF-only (at the time of writing). */
2080 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2081 {
2082 dwz_file->abbrev.asection = sectp;
2083 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2084 }
2085 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2086 {
2087 dwz_file->info.asection = sectp;
2088 dwz_file->info.size = bfd_get_section_size (sectp);
2089 }
2090 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2091 {
2092 dwz_file->str.asection = sectp;
2093 dwz_file->str.size = bfd_get_section_size (sectp);
2094 }
2095 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2096 {
2097 dwz_file->line.asection = sectp;
2098 dwz_file->line.size = bfd_get_section_size (sectp);
2099 }
2100 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2101 {
2102 dwz_file->macro.asection = sectp;
2103 dwz_file->macro.size = bfd_get_section_size (sectp);
2104 }
2105 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2106 {
2107 dwz_file->gdb_index.asection = sectp;
2108 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2109 }
2110 }
2111
2112 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2113 there is no .gnu_debugaltlink section in the file. Error if there
2114 is such a section but the file cannot be found. */
2115
2116 static struct dwz_file *
2117 dwarf2_get_dwz_file (void)
2118 {
2119 bfd *dwz_bfd;
2120 char *data;
2121 struct cleanup *cleanup;
2122 const char *filename;
2123 struct dwz_file *result;
2124 unsigned long buildid;
2125
2126 if (dwarf2_per_objfile->dwz_file != NULL)
2127 return dwarf2_per_objfile->dwz_file;
2128
2129 bfd_set_error (bfd_error_no_error);
2130 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2131 &buildid);
2132 if (data == NULL)
2133 {
2134 if (bfd_get_error () == bfd_error_no_error)
2135 return NULL;
2136 error (_("could not read '.gnu_debugaltlink' section: %s"),
2137 bfd_errmsg (bfd_get_error ()));
2138 }
2139 cleanup = make_cleanup (xfree, data);
2140
2141 filename = (const char *) data;
2142 if (!IS_ABSOLUTE_PATH (filename))
2143 {
2144 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2145 char *rel;
2146
2147 make_cleanup (xfree, abs);
2148 abs = ldirname (abs);
2149 make_cleanup (xfree, abs);
2150
2151 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2152 make_cleanup (xfree, rel);
2153 filename = rel;
2154 }
2155
2156 /* The format is just a NUL-terminated file name, followed by the
2157 build-id. For now, though, we ignore the build-id. */
2158 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2159 if (dwz_bfd == NULL)
2160 error (_("could not read '%s': %s"), filename,
2161 bfd_errmsg (bfd_get_error ()));
2162
2163 if (!bfd_check_format (dwz_bfd, bfd_object))
2164 {
2165 gdb_bfd_unref (dwz_bfd);
2166 error (_("file '%s' was not usable: %s"), filename,
2167 bfd_errmsg (bfd_get_error ()));
2168 }
2169
2170 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2171 struct dwz_file);
2172 result->dwz_bfd = dwz_bfd;
2173
2174 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2175
2176 do_cleanups (cleanup);
2177
2178 dwarf2_per_objfile->dwz_file = result;
2179 return result;
2180 }
2181 \f
2182 /* DWARF quick_symbols_functions support. */
2183
2184 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2185 unique line tables, so we maintain a separate table of all .debug_line
2186 derived entries to support the sharing.
2187 All the quick functions need is the list of file names. We discard the
2188 line_header when we're done and don't need to record it here. */
2189 struct quick_file_names
2190 {
2191 /* The data used to construct the hash key. */
2192 struct stmt_list_hash hash;
2193
2194 /* The number of entries in file_names, real_names. */
2195 unsigned int num_file_names;
2196
2197 /* The file names from the line table, after being run through
2198 file_full_name. */
2199 const char **file_names;
2200
2201 /* The file names from the line table after being run through
2202 gdb_realpath. These are computed lazily. */
2203 const char **real_names;
2204 };
2205
2206 /* When using the index (and thus not using psymtabs), each CU has an
2207 object of this type. This is used to hold information needed by
2208 the various "quick" methods. */
2209 struct dwarf2_per_cu_quick_data
2210 {
2211 /* The file table. This can be NULL if there was no file table
2212 or it's currently not read in.
2213 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2214 struct quick_file_names *file_names;
2215
2216 /* The corresponding symbol table. This is NULL if symbols for this
2217 CU have not yet been read. */
2218 struct symtab *symtab;
2219
2220 /* A temporary mark bit used when iterating over all CUs in
2221 expand_symtabs_matching. */
2222 unsigned int mark : 1;
2223
2224 /* True if we've tried to read the file table and found there isn't one.
2225 There will be no point in trying to read it again next time. */
2226 unsigned int no_file_data : 1;
2227 };
2228
2229 /* Utility hash function for a stmt_list_hash. */
2230
2231 static hashval_t
2232 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2233 {
2234 hashval_t v = 0;
2235
2236 if (stmt_list_hash->dwo_unit != NULL)
2237 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2238 v += stmt_list_hash->line_offset.sect_off;
2239 return v;
2240 }
2241
2242 /* Utility equality function for a stmt_list_hash. */
2243
2244 static int
2245 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2246 const struct stmt_list_hash *rhs)
2247 {
2248 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2249 return 0;
2250 if (lhs->dwo_unit != NULL
2251 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2252 return 0;
2253
2254 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2255 }
2256
2257 /* Hash function for a quick_file_names. */
2258
2259 static hashval_t
2260 hash_file_name_entry (const void *e)
2261 {
2262 const struct quick_file_names *file_data = e;
2263
2264 return hash_stmt_list_entry (&file_data->hash);
2265 }
2266
2267 /* Equality function for a quick_file_names. */
2268
2269 static int
2270 eq_file_name_entry (const void *a, const void *b)
2271 {
2272 const struct quick_file_names *ea = a;
2273 const struct quick_file_names *eb = b;
2274
2275 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2276 }
2277
2278 /* Delete function for a quick_file_names. */
2279
2280 static void
2281 delete_file_name_entry (void *e)
2282 {
2283 struct quick_file_names *file_data = e;
2284 int i;
2285
2286 for (i = 0; i < file_data->num_file_names; ++i)
2287 {
2288 xfree ((void*) file_data->file_names[i]);
2289 if (file_data->real_names)
2290 xfree ((void*) file_data->real_names[i]);
2291 }
2292
2293 /* The space for the struct itself lives on objfile_obstack,
2294 so we don't free it here. */
2295 }
2296
2297 /* Create a quick_file_names hash table. */
2298
2299 static htab_t
2300 create_quick_file_names_table (unsigned int nr_initial_entries)
2301 {
2302 return htab_create_alloc (nr_initial_entries,
2303 hash_file_name_entry, eq_file_name_entry,
2304 delete_file_name_entry, xcalloc, xfree);
2305 }
2306
2307 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2308 have to be created afterwards. You should call age_cached_comp_units after
2309 processing PER_CU->CU. dw2_setup must have been already called. */
2310
2311 static void
2312 load_cu (struct dwarf2_per_cu_data *per_cu)
2313 {
2314 if (per_cu->is_debug_types)
2315 load_full_type_unit (per_cu);
2316 else
2317 load_full_comp_unit (per_cu, language_minimal);
2318
2319 gdb_assert (per_cu->cu != NULL);
2320
2321 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2322 }
2323
2324 /* Read in the symbols for PER_CU. */
2325
2326 static void
2327 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2328 {
2329 struct cleanup *back_to;
2330
2331 /* Skip type_unit_groups, reading the type units they contain
2332 is handled elsewhere. */
2333 if (IS_TYPE_UNIT_GROUP (per_cu))
2334 return;
2335
2336 back_to = make_cleanup (dwarf2_release_queue, NULL);
2337
2338 if (dwarf2_per_objfile->using_index
2339 ? per_cu->v.quick->symtab == NULL
2340 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2341 {
2342 queue_comp_unit (per_cu, language_minimal);
2343 load_cu (per_cu);
2344 }
2345
2346 process_queue ();
2347
2348 /* Age the cache, releasing compilation units that have not
2349 been used recently. */
2350 age_cached_comp_units ();
2351
2352 do_cleanups (back_to);
2353 }
2354
2355 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2356 the objfile from which this CU came. Returns the resulting symbol
2357 table. */
2358
2359 static struct symtab *
2360 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2361 {
2362 gdb_assert (dwarf2_per_objfile->using_index);
2363 if (!per_cu->v.quick->symtab)
2364 {
2365 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2366 increment_reading_symtab ();
2367 dw2_do_instantiate_symtab (per_cu);
2368 process_cu_includes ();
2369 do_cleanups (back_to);
2370 }
2371 return per_cu->v.quick->symtab;
2372 }
2373
2374 /* Return the CU given its index.
2375
2376 This is intended for loops like:
2377
2378 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2379 + dwarf2_per_objfile->n_type_units); ++i)
2380 {
2381 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2382
2383 ...;
2384 }
2385 */
2386
2387 static struct dwarf2_per_cu_data *
2388 dw2_get_cu (int index)
2389 {
2390 if (index >= dwarf2_per_objfile->n_comp_units)
2391 {
2392 index -= dwarf2_per_objfile->n_comp_units;
2393 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2394 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2395 }
2396
2397 return dwarf2_per_objfile->all_comp_units[index];
2398 }
2399
2400 /* Return the primary CU given its index.
2401 The difference between this function and dw2_get_cu is in the handling
2402 of type units (TUs). Here we return the type_unit_group object.
2403
2404 This is intended for loops like:
2405
2406 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2407 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2408 {
2409 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2410
2411 ...;
2412 }
2413 */
2414
2415 static struct dwarf2_per_cu_data *
2416 dw2_get_primary_cu (int index)
2417 {
2418 if (index >= dwarf2_per_objfile->n_comp_units)
2419 {
2420 index -= dwarf2_per_objfile->n_comp_units;
2421 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2422 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
2423 }
2424
2425 return dwarf2_per_objfile->all_comp_units[index];
2426 }
2427
2428 /* A helper for create_cus_from_index that handles a given list of
2429 CUs. */
2430
2431 static void
2432 create_cus_from_index_list (struct objfile *objfile,
2433 const gdb_byte *cu_list, offset_type n_elements,
2434 struct dwarf2_section_info *section,
2435 int is_dwz,
2436 int base_offset)
2437 {
2438 offset_type i;
2439
2440 for (i = 0; i < n_elements; i += 2)
2441 {
2442 struct dwarf2_per_cu_data *the_cu;
2443 ULONGEST offset, length;
2444
2445 gdb_static_assert (sizeof (ULONGEST) >= 8);
2446 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2447 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2448 cu_list += 2 * 8;
2449
2450 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2451 struct dwarf2_per_cu_data);
2452 the_cu->offset.sect_off = offset;
2453 the_cu->length = length;
2454 the_cu->objfile = objfile;
2455 the_cu->section = section;
2456 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2457 struct dwarf2_per_cu_quick_data);
2458 the_cu->is_dwz = is_dwz;
2459 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2460 }
2461 }
2462
2463 /* Read the CU list from the mapped index, and use it to create all
2464 the CU objects for this objfile. */
2465
2466 static void
2467 create_cus_from_index (struct objfile *objfile,
2468 const gdb_byte *cu_list, offset_type cu_list_elements,
2469 const gdb_byte *dwz_list, offset_type dwz_elements)
2470 {
2471 struct dwz_file *dwz;
2472
2473 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2474 dwarf2_per_objfile->all_comp_units
2475 = obstack_alloc (&objfile->objfile_obstack,
2476 dwarf2_per_objfile->n_comp_units
2477 * sizeof (struct dwarf2_per_cu_data *));
2478
2479 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2480 &dwarf2_per_objfile->info, 0, 0);
2481
2482 if (dwz_elements == 0)
2483 return;
2484
2485 dwz = dwarf2_get_dwz_file ();
2486 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2487 cu_list_elements / 2);
2488 }
2489
2490 /* Create the signatured type hash table from the index. */
2491
2492 static void
2493 create_signatured_type_table_from_index (struct objfile *objfile,
2494 struct dwarf2_section_info *section,
2495 const gdb_byte *bytes,
2496 offset_type elements)
2497 {
2498 offset_type i;
2499 htab_t sig_types_hash;
2500
2501 dwarf2_per_objfile->n_type_units = elements / 3;
2502 dwarf2_per_objfile->all_type_units
2503 = xmalloc (dwarf2_per_objfile->n_type_units
2504 * sizeof (struct signatured_type *));
2505
2506 sig_types_hash = allocate_signatured_type_table (objfile);
2507
2508 for (i = 0; i < elements; i += 3)
2509 {
2510 struct signatured_type *sig_type;
2511 ULONGEST offset, type_offset_in_tu, signature;
2512 void **slot;
2513
2514 gdb_static_assert (sizeof (ULONGEST) >= 8);
2515 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2516 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2517 BFD_ENDIAN_LITTLE);
2518 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2519 bytes += 3 * 8;
2520
2521 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2522 struct signatured_type);
2523 sig_type->signature = signature;
2524 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2525 sig_type->per_cu.is_debug_types = 1;
2526 sig_type->per_cu.section = section;
2527 sig_type->per_cu.offset.sect_off = offset;
2528 sig_type->per_cu.objfile = objfile;
2529 sig_type->per_cu.v.quick
2530 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2531 struct dwarf2_per_cu_quick_data);
2532
2533 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2534 *slot = sig_type;
2535
2536 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2537 }
2538
2539 dwarf2_per_objfile->signatured_types = sig_types_hash;
2540 }
2541
2542 /* Read the address map data from the mapped index, and use it to
2543 populate the objfile's psymtabs_addrmap. */
2544
2545 static void
2546 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2547 {
2548 const gdb_byte *iter, *end;
2549 struct obstack temp_obstack;
2550 struct addrmap *mutable_map;
2551 struct cleanup *cleanup;
2552 CORE_ADDR baseaddr;
2553
2554 obstack_init (&temp_obstack);
2555 cleanup = make_cleanup_obstack_free (&temp_obstack);
2556 mutable_map = addrmap_create_mutable (&temp_obstack);
2557
2558 iter = index->address_table;
2559 end = iter + index->address_table_size;
2560
2561 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2562
2563 while (iter < end)
2564 {
2565 ULONGEST hi, lo, cu_index;
2566 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2567 iter += 8;
2568 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2569 iter += 8;
2570 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2571 iter += 4;
2572
2573 if (cu_index < dwarf2_per_objfile->n_comp_units)
2574 {
2575 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2576 dw2_get_cu (cu_index));
2577 }
2578 else
2579 {
2580 complaint (&symfile_complaints,
2581 _(".gdb_index address table has invalid CU number %u"),
2582 (unsigned) cu_index);
2583 }
2584 }
2585
2586 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2587 &objfile->objfile_obstack);
2588 do_cleanups (cleanup);
2589 }
2590
2591 /* The hash function for strings in the mapped index. This is the same as
2592 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2593 implementation. This is necessary because the hash function is tied to the
2594 format of the mapped index file. The hash values do not have to match with
2595 SYMBOL_HASH_NEXT.
2596
2597 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2598
2599 static hashval_t
2600 mapped_index_string_hash (int index_version, const void *p)
2601 {
2602 const unsigned char *str = (const unsigned char *) p;
2603 hashval_t r = 0;
2604 unsigned char c;
2605
2606 while ((c = *str++) != 0)
2607 {
2608 if (index_version >= 5)
2609 c = tolower (c);
2610 r = r * 67 + c - 113;
2611 }
2612
2613 return r;
2614 }
2615
2616 /* Find a slot in the mapped index INDEX for the object named NAME.
2617 If NAME is found, set *VEC_OUT to point to the CU vector in the
2618 constant pool and return 1. If NAME cannot be found, return 0. */
2619
2620 static int
2621 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2622 offset_type **vec_out)
2623 {
2624 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2625 offset_type hash;
2626 offset_type slot, step;
2627 int (*cmp) (const char *, const char *);
2628
2629 if (current_language->la_language == language_cplus
2630 || current_language->la_language == language_java
2631 || current_language->la_language == language_fortran)
2632 {
2633 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2634 not contain any. */
2635 const char *paren = strchr (name, '(');
2636
2637 if (paren)
2638 {
2639 char *dup;
2640
2641 dup = xmalloc (paren - name + 1);
2642 memcpy (dup, name, paren - name);
2643 dup[paren - name] = 0;
2644
2645 make_cleanup (xfree, dup);
2646 name = dup;
2647 }
2648 }
2649
2650 /* Index version 4 did not support case insensitive searches. But the
2651 indices for case insensitive languages are built in lowercase, therefore
2652 simulate our NAME being searched is also lowercased. */
2653 hash = mapped_index_string_hash ((index->version == 4
2654 && case_sensitivity == case_sensitive_off
2655 ? 5 : index->version),
2656 name);
2657
2658 slot = hash & (index->symbol_table_slots - 1);
2659 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2660 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2661
2662 for (;;)
2663 {
2664 /* Convert a slot number to an offset into the table. */
2665 offset_type i = 2 * slot;
2666 const char *str;
2667 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2668 {
2669 do_cleanups (back_to);
2670 return 0;
2671 }
2672
2673 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2674 if (!cmp (name, str))
2675 {
2676 *vec_out = (offset_type *) (index->constant_pool
2677 + MAYBE_SWAP (index->symbol_table[i + 1]));
2678 do_cleanups (back_to);
2679 return 1;
2680 }
2681
2682 slot = (slot + step) & (index->symbol_table_slots - 1);
2683 }
2684 }
2685
2686 /* A helper function that reads the .gdb_index from SECTION and fills
2687 in MAP. FILENAME is the name of the file containing the section;
2688 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2689 ok to use deprecated sections.
2690
2691 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2692 out parameters that are filled in with information about the CU and
2693 TU lists in the section.
2694
2695 Returns 1 if all went well, 0 otherwise. */
2696
2697 static int
2698 read_index_from_section (struct objfile *objfile,
2699 const char *filename,
2700 int deprecated_ok,
2701 struct dwarf2_section_info *section,
2702 struct mapped_index *map,
2703 const gdb_byte **cu_list,
2704 offset_type *cu_list_elements,
2705 const gdb_byte **types_list,
2706 offset_type *types_list_elements)
2707 {
2708 const gdb_byte *addr;
2709 offset_type version;
2710 offset_type *metadata;
2711 int i;
2712
2713 if (dwarf2_section_empty_p (section))
2714 return 0;
2715
2716 /* Older elfutils strip versions could keep the section in the main
2717 executable while splitting it for the separate debug info file. */
2718 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
2719 return 0;
2720
2721 dwarf2_read_section (objfile, section);
2722
2723 addr = section->buffer;
2724 /* Version check. */
2725 version = MAYBE_SWAP (*(offset_type *) addr);
2726 /* Versions earlier than 3 emitted every copy of a psymbol. This
2727 causes the index to behave very poorly for certain requests. Version 3
2728 contained incomplete addrmap. So, it seems better to just ignore such
2729 indices. */
2730 if (version < 4)
2731 {
2732 static int warning_printed = 0;
2733 if (!warning_printed)
2734 {
2735 warning (_("Skipping obsolete .gdb_index section in %s."),
2736 filename);
2737 warning_printed = 1;
2738 }
2739 return 0;
2740 }
2741 /* Index version 4 uses a different hash function than index version
2742 5 and later.
2743
2744 Versions earlier than 6 did not emit psymbols for inlined
2745 functions. Using these files will cause GDB not to be able to
2746 set breakpoints on inlined functions by name, so we ignore these
2747 indices unless the user has done
2748 "set use-deprecated-index-sections on". */
2749 if (version < 6 && !deprecated_ok)
2750 {
2751 static int warning_printed = 0;
2752 if (!warning_printed)
2753 {
2754 warning (_("\
2755 Skipping deprecated .gdb_index section in %s.\n\
2756 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2757 to use the section anyway."),
2758 filename);
2759 warning_printed = 1;
2760 }
2761 return 0;
2762 }
2763 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2764 of the TU (for symbols coming from TUs). It's just a performance bug, and
2765 we can't distinguish gdb-generated indices from gold-generated ones, so
2766 nothing to do here. */
2767
2768 /* Indexes with higher version than the one supported by GDB may be no
2769 longer backward compatible. */
2770 if (version > 8)
2771 return 0;
2772
2773 map->version = version;
2774 map->total_size = section->size;
2775
2776 metadata = (offset_type *) (addr + sizeof (offset_type));
2777
2778 i = 0;
2779 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2780 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2781 / 8);
2782 ++i;
2783
2784 *types_list = addr + MAYBE_SWAP (metadata[i]);
2785 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2786 - MAYBE_SWAP (metadata[i]))
2787 / 8);
2788 ++i;
2789
2790 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2791 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2792 - MAYBE_SWAP (metadata[i]));
2793 ++i;
2794
2795 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2796 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2797 - MAYBE_SWAP (metadata[i]))
2798 / (2 * sizeof (offset_type)));
2799 ++i;
2800
2801 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2802
2803 return 1;
2804 }
2805
2806
2807 /* Read the index file. If everything went ok, initialize the "quick"
2808 elements of all the CUs and return 1. Otherwise, return 0. */
2809
2810 static int
2811 dwarf2_read_index (struct objfile *objfile)
2812 {
2813 struct mapped_index local_map, *map;
2814 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2815 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2816 struct dwz_file *dwz;
2817
2818 if (!read_index_from_section (objfile, objfile->name,
2819 use_deprecated_index_sections,
2820 &dwarf2_per_objfile->gdb_index, &local_map,
2821 &cu_list, &cu_list_elements,
2822 &types_list, &types_list_elements))
2823 return 0;
2824
2825 /* Don't use the index if it's empty. */
2826 if (local_map.symbol_table_slots == 0)
2827 return 0;
2828
2829 /* If there is a .dwz file, read it so we can get its CU list as
2830 well. */
2831 dwz = dwarf2_get_dwz_file ();
2832 if (dwz != NULL)
2833 {
2834 struct mapped_index dwz_map;
2835 const gdb_byte *dwz_types_ignore;
2836 offset_type dwz_types_elements_ignore;
2837
2838 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2839 1,
2840 &dwz->gdb_index, &dwz_map,
2841 &dwz_list, &dwz_list_elements,
2842 &dwz_types_ignore,
2843 &dwz_types_elements_ignore))
2844 {
2845 warning (_("could not read '.gdb_index' section from %s; skipping"),
2846 bfd_get_filename (dwz->dwz_bfd));
2847 return 0;
2848 }
2849 }
2850
2851 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2852 dwz_list_elements);
2853
2854 if (types_list_elements)
2855 {
2856 struct dwarf2_section_info *section;
2857
2858 /* We can only handle a single .debug_types when we have an
2859 index. */
2860 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2861 return 0;
2862
2863 section = VEC_index (dwarf2_section_info_def,
2864 dwarf2_per_objfile->types, 0);
2865
2866 create_signatured_type_table_from_index (objfile, section, types_list,
2867 types_list_elements);
2868 }
2869
2870 create_addrmap_from_index (objfile, &local_map);
2871
2872 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2873 *map = local_map;
2874
2875 dwarf2_per_objfile->index_table = map;
2876 dwarf2_per_objfile->using_index = 1;
2877 dwarf2_per_objfile->quick_file_names_table =
2878 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2879
2880 return 1;
2881 }
2882
2883 /* A helper for the "quick" functions which sets the global
2884 dwarf2_per_objfile according to OBJFILE. */
2885
2886 static void
2887 dw2_setup (struct objfile *objfile)
2888 {
2889 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2890 gdb_assert (dwarf2_per_objfile);
2891 }
2892
2893 /* die_reader_func for dw2_get_file_names. */
2894
2895 static void
2896 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2897 const gdb_byte *info_ptr,
2898 struct die_info *comp_unit_die,
2899 int has_children,
2900 void *data)
2901 {
2902 struct dwarf2_cu *cu = reader->cu;
2903 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2904 struct objfile *objfile = dwarf2_per_objfile->objfile;
2905 struct dwarf2_per_cu_data *lh_cu;
2906 struct line_header *lh;
2907 struct attribute *attr;
2908 int i;
2909 const char *name, *comp_dir;
2910 void **slot;
2911 struct quick_file_names *qfn;
2912 unsigned int line_offset;
2913
2914 gdb_assert (! this_cu->is_debug_types);
2915
2916 /* Our callers never want to match partial units -- instead they
2917 will match the enclosing full CU. */
2918 if (comp_unit_die->tag == DW_TAG_partial_unit)
2919 {
2920 this_cu->v.quick->no_file_data = 1;
2921 return;
2922 }
2923
2924 lh_cu = this_cu;
2925 lh = NULL;
2926 slot = NULL;
2927 line_offset = 0;
2928
2929 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2930 if (attr)
2931 {
2932 struct quick_file_names find_entry;
2933
2934 line_offset = DW_UNSND (attr);
2935
2936 /* We may have already read in this line header (TU line header sharing).
2937 If we have we're done. */
2938 find_entry.hash.dwo_unit = cu->dwo_unit;
2939 find_entry.hash.line_offset.sect_off = line_offset;
2940 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2941 &find_entry, INSERT);
2942 if (*slot != NULL)
2943 {
2944 lh_cu->v.quick->file_names = *slot;
2945 return;
2946 }
2947
2948 lh = dwarf_decode_line_header (line_offset, cu);
2949 }
2950 if (lh == NULL)
2951 {
2952 lh_cu->v.quick->no_file_data = 1;
2953 return;
2954 }
2955
2956 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2957 qfn->hash.dwo_unit = cu->dwo_unit;
2958 qfn->hash.line_offset.sect_off = line_offset;
2959 gdb_assert (slot != NULL);
2960 *slot = qfn;
2961
2962 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2963
2964 qfn->num_file_names = lh->num_file_names;
2965 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2966 lh->num_file_names * sizeof (char *));
2967 for (i = 0; i < lh->num_file_names; ++i)
2968 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2969 qfn->real_names = NULL;
2970
2971 free_line_header (lh);
2972
2973 lh_cu->v.quick->file_names = qfn;
2974 }
2975
2976 /* A helper for the "quick" functions which attempts to read the line
2977 table for THIS_CU. */
2978
2979 static struct quick_file_names *
2980 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
2981 {
2982 /* This should never be called for TUs. */
2983 gdb_assert (! this_cu->is_debug_types);
2984 /* Nor type unit groups. */
2985 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
2986
2987 if (this_cu->v.quick->file_names != NULL)
2988 return this_cu->v.quick->file_names;
2989 /* If we know there is no line data, no point in looking again. */
2990 if (this_cu->v.quick->no_file_data)
2991 return NULL;
2992
2993 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2994
2995 if (this_cu->v.quick->no_file_data)
2996 return NULL;
2997 return this_cu->v.quick->file_names;
2998 }
2999
3000 /* A helper for the "quick" functions which computes and caches the
3001 real path for a given file name from the line table. */
3002
3003 static const char *
3004 dw2_get_real_path (struct objfile *objfile,
3005 struct quick_file_names *qfn, int index)
3006 {
3007 if (qfn->real_names == NULL)
3008 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3009 qfn->num_file_names, sizeof (char *));
3010
3011 if (qfn->real_names[index] == NULL)
3012 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
3013
3014 return qfn->real_names[index];
3015 }
3016
3017 static struct symtab *
3018 dw2_find_last_source_symtab (struct objfile *objfile)
3019 {
3020 int index;
3021
3022 dw2_setup (objfile);
3023 index = dwarf2_per_objfile->n_comp_units - 1;
3024 return dw2_instantiate_symtab (dw2_get_cu (index));
3025 }
3026
3027 /* Traversal function for dw2_forget_cached_source_info. */
3028
3029 static int
3030 dw2_free_cached_file_names (void **slot, void *info)
3031 {
3032 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3033
3034 if (file_data->real_names)
3035 {
3036 int i;
3037
3038 for (i = 0; i < file_data->num_file_names; ++i)
3039 {
3040 xfree ((void*) file_data->real_names[i]);
3041 file_data->real_names[i] = NULL;
3042 }
3043 }
3044
3045 return 1;
3046 }
3047
3048 static void
3049 dw2_forget_cached_source_info (struct objfile *objfile)
3050 {
3051 dw2_setup (objfile);
3052
3053 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3054 dw2_free_cached_file_names, NULL);
3055 }
3056
3057 /* Helper function for dw2_map_symtabs_matching_filename that expands
3058 the symtabs and calls the iterator. */
3059
3060 static int
3061 dw2_map_expand_apply (struct objfile *objfile,
3062 struct dwarf2_per_cu_data *per_cu,
3063 const char *name, const char *real_path,
3064 int (*callback) (struct symtab *, void *),
3065 void *data)
3066 {
3067 struct symtab *last_made = objfile->symtabs;
3068
3069 /* Don't visit already-expanded CUs. */
3070 if (per_cu->v.quick->symtab)
3071 return 0;
3072
3073 /* This may expand more than one symtab, and we want to iterate over
3074 all of them. */
3075 dw2_instantiate_symtab (per_cu);
3076
3077 return iterate_over_some_symtabs (name, real_path, callback, data,
3078 objfile->symtabs, last_made);
3079 }
3080
3081 /* Implementation of the map_symtabs_matching_filename method. */
3082
3083 static int
3084 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3085 const char *real_path,
3086 int (*callback) (struct symtab *, void *),
3087 void *data)
3088 {
3089 int i;
3090 const char *name_basename = lbasename (name);
3091
3092 dw2_setup (objfile);
3093
3094 /* The rule is CUs specify all the files, including those used by
3095 any TU, so there's no need to scan TUs here. */
3096
3097 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3098 {
3099 int j;
3100 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3101 struct quick_file_names *file_data;
3102
3103 /* We only need to look at symtabs not already expanded. */
3104 if (per_cu->v.quick->symtab)
3105 continue;
3106
3107 file_data = dw2_get_file_names (per_cu);
3108 if (file_data == NULL)
3109 continue;
3110
3111 for (j = 0; j < file_data->num_file_names; ++j)
3112 {
3113 const char *this_name = file_data->file_names[j];
3114 const char *this_real_name;
3115
3116 if (compare_filenames_for_search (this_name, name))
3117 {
3118 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3119 callback, data))
3120 return 1;
3121 continue;
3122 }
3123
3124 /* Before we invoke realpath, which can get expensive when many
3125 files are involved, do a quick comparison of the basenames. */
3126 if (! basenames_may_differ
3127 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3128 continue;
3129
3130 this_real_name = dw2_get_real_path (objfile, file_data, j);
3131 if (compare_filenames_for_search (this_real_name, name))
3132 {
3133 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3134 callback, data))
3135 return 1;
3136 continue;
3137 }
3138
3139 if (real_path != NULL)
3140 {
3141 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3142 gdb_assert (IS_ABSOLUTE_PATH (name));
3143 if (this_real_name != NULL
3144 && FILENAME_CMP (real_path, this_real_name) == 0)
3145 {
3146 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3147 callback, data))
3148 return 1;
3149 continue;
3150 }
3151 }
3152 }
3153 }
3154
3155 return 0;
3156 }
3157
3158 /* Struct used to manage iterating over all CUs looking for a symbol. */
3159
3160 struct dw2_symtab_iterator
3161 {
3162 /* The internalized form of .gdb_index. */
3163 struct mapped_index *index;
3164 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3165 int want_specific_block;
3166 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3167 Unused if !WANT_SPECIFIC_BLOCK. */
3168 int block_index;
3169 /* The kind of symbol we're looking for. */
3170 domain_enum domain;
3171 /* The list of CUs from the index entry of the symbol,
3172 or NULL if not found. */
3173 offset_type *vec;
3174 /* The next element in VEC to look at. */
3175 int next;
3176 /* The number of elements in VEC, or zero if there is no match. */
3177 int length;
3178 };
3179
3180 /* Initialize the index symtab iterator ITER.
3181 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3182 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3183
3184 static void
3185 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3186 struct mapped_index *index,
3187 int want_specific_block,
3188 int block_index,
3189 domain_enum domain,
3190 const char *name)
3191 {
3192 iter->index = index;
3193 iter->want_specific_block = want_specific_block;
3194 iter->block_index = block_index;
3195 iter->domain = domain;
3196 iter->next = 0;
3197
3198 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3199 iter->length = MAYBE_SWAP (*iter->vec);
3200 else
3201 {
3202 iter->vec = NULL;
3203 iter->length = 0;
3204 }
3205 }
3206
3207 /* Return the next matching CU or NULL if there are no more. */
3208
3209 static struct dwarf2_per_cu_data *
3210 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3211 {
3212 for ( ; iter->next < iter->length; ++iter->next)
3213 {
3214 offset_type cu_index_and_attrs =
3215 MAYBE_SWAP (iter->vec[iter->next + 1]);
3216 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3217 struct dwarf2_per_cu_data *per_cu;
3218 int want_static = iter->block_index != GLOBAL_BLOCK;
3219 /* This value is only valid for index versions >= 7. */
3220 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3221 gdb_index_symbol_kind symbol_kind =
3222 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3223 /* Only check the symbol attributes if they're present.
3224 Indices prior to version 7 don't record them,
3225 and indices >= 7 may elide them for certain symbols
3226 (gold does this). */
3227 int attrs_valid =
3228 (iter->index->version >= 7
3229 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3230
3231 /* Don't crash on bad data. */
3232 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3233 + dwarf2_per_objfile->n_type_units))
3234 {
3235 complaint (&symfile_complaints,
3236 _(".gdb_index entry has bad CU index"
3237 " [in module %s]"), dwarf2_per_objfile->objfile->name);
3238 continue;
3239 }
3240
3241 per_cu = dw2_get_cu (cu_index);
3242
3243 /* Skip if already read in. */
3244 if (per_cu->v.quick->symtab)
3245 continue;
3246
3247 if (attrs_valid
3248 && iter->want_specific_block
3249 && want_static != is_static)
3250 continue;
3251
3252 /* Only check the symbol's kind if it has one. */
3253 if (attrs_valid)
3254 {
3255 switch (iter->domain)
3256 {
3257 case VAR_DOMAIN:
3258 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3259 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3260 /* Some types are also in VAR_DOMAIN. */
3261 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3262 continue;
3263 break;
3264 case STRUCT_DOMAIN:
3265 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3266 continue;
3267 break;
3268 case LABEL_DOMAIN:
3269 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3270 continue;
3271 break;
3272 default:
3273 break;
3274 }
3275 }
3276
3277 ++iter->next;
3278 return per_cu;
3279 }
3280
3281 return NULL;
3282 }
3283
3284 static struct symtab *
3285 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3286 const char *name, domain_enum domain)
3287 {
3288 struct symtab *stab_best = NULL;
3289 struct mapped_index *index;
3290
3291 dw2_setup (objfile);
3292
3293 index = dwarf2_per_objfile->index_table;
3294
3295 /* index is NULL if OBJF_READNOW. */
3296 if (index)
3297 {
3298 struct dw2_symtab_iterator iter;
3299 struct dwarf2_per_cu_data *per_cu;
3300
3301 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3302
3303 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3304 {
3305 struct symbol *sym = NULL;
3306 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3307
3308 /* Some caution must be observed with overloaded functions
3309 and methods, since the index will not contain any overload
3310 information (but NAME might contain it). */
3311 if (stab->primary)
3312 {
3313 struct blockvector *bv = BLOCKVECTOR (stab);
3314 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3315
3316 sym = lookup_block_symbol (block, name, domain);
3317 }
3318
3319 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3320 {
3321 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3322 return stab;
3323
3324 stab_best = stab;
3325 }
3326
3327 /* Keep looking through other CUs. */
3328 }
3329 }
3330
3331 return stab_best;
3332 }
3333
3334 static void
3335 dw2_print_stats (struct objfile *objfile)
3336 {
3337 int i, total, count;
3338
3339 dw2_setup (objfile);
3340 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
3341 count = 0;
3342 for (i = 0; i < total; ++i)
3343 {
3344 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3345
3346 if (!per_cu->v.quick->symtab)
3347 ++count;
3348 }
3349 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3350 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3351 }
3352
3353 static void
3354 dw2_dump (struct objfile *objfile)
3355 {
3356 /* Nothing worth printing. */
3357 }
3358
3359 static void
3360 dw2_relocate (struct objfile *objfile,
3361 const struct section_offsets *new_offsets,
3362 const struct section_offsets *delta)
3363 {
3364 /* There's nothing to relocate here. */
3365 }
3366
3367 static void
3368 dw2_expand_symtabs_for_function (struct objfile *objfile,
3369 const char *func_name)
3370 {
3371 struct mapped_index *index;
3372
3373 dw2_setup (objfile);
3374
3375 index = dwarf2_per_objfile->index_table;
3376
3377 /* index is NULL if OBJF_READNOW. */
3378 if (index)
3379 {
3380 struct dw2_symtab_iterator iter;
3381 struct dwarf2_per_cu_data *per_cu;
3382
3383 /* Note: It doesn't matter what we pass for block_index here. */
3384 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3385 func_name);
3386
3387 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3388 dw2_instantiate_symtab (per_cu);
3389 }
3390 }
3391
3392 static void
3393 dw2_expand_all_symtabs (struct objfile *objfile)
3394 {
3395 int i;
3396
3397 dw2_setup (objfile);
3398
3399 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3400 + dwarf2_per_objfile->n_type_units); ++i)
3401 {
3402 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3403
3404 dw2_instantiate_symtab (per_cu);
3405 }
3406 }
3407
3408 static void
3409 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3410 const char *fullname)
3411 {
3412 int i;
3413
3414 dw2_setup (objfile);
3415
3416 /* We don't need to consider type units here.
3417 This is only called for examining code, e.g. expand_line_sal.
3418 There can be an order of magnitude (or more) more type units
3419 than comp units, and we avoid them if we can. */
3420
3421 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3422 {
3423 int j;
3424 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3425 struct quick_file_names *file_data;
3426
3427 /* We only need to look at symtabs not already expanded. */
3428 if (per_cu->v.quick->symtab)
3429 continue;
3430
3431 file_data = dw2_get_file_names (per_cu);
3432 if (file_data == NULL)
3433 continue;
3434
3435 for (j = 0; j < file_data->num_file_names; ++j)
3436 {
3437 const char *this_fullname = file_data->file_names[j];
3438
3439 if (filename_cmp (this_fullname, fullname) == 0)
3440 {
3441 dw2_instantiate_symtab (per_cu);
3442 break;
3443 }
3444 }
3445 }
3446 }
3447
3448 static void
3449 dw2_map_matching_symbols (const char * name, domain_enum namespace,
3450 struct objfile *objfile, int global,
3451 int (*callback) (struct block *,
3452 struct symbol *, void *),
3453 void *data, symbol_compare_ftype *match,
3454 symbol_compare_ftype *ordered_compare)
3455 {
3456 /* Currently unimplemented; used for Ada. The function can be called if the
3457 current language is Ada for a non-Ada objfile using GNU index. As Ada
3458 does not look for non-Ada symbols this function should just return. */
3459 }
3460
3461 static void
3462 dw2_expand_symtabs_matching
3463 (struct objfile *objfile,
3464 int (*file_matcher) (const char *, void *, int basenames),
3465 int (*name_matcher) (const char *, void *),
3466 enum search_domain kind,
3467 void *data)
3468 {
3469 int i;
3470 offset_type iter;
3471 struct mapped_index *index;
3472
3473 dw2_setup (objfile);
3474
3475 /* index_table is NULL if OBJF_READNOW. */
3476 if (!dwarf2_per_objfile->index_table)
3477 return;
3478 index = dwarf2_per_objfile->index_table;
3479
3480 if (file_matcher != NULL)
3481 {
3482 struct cleanup *cleanup;
3483 htab_t visited_found, visited_not_found;
3484
3485 visited_found = htab_create_alloc (10,
3486 htab_hash_pointer, htab_eq_pointer,
3487 NULL, xcalloc, xfree);
3488 cleanup = make_cleanup_htab_delete (visited_found);
3489 visited_not_found = htab_create_alloc (10,
3490 htab_hash_pointer, htab_eq_pointer,
3491 NULL, xcalloc, xfree);
3492 make_cleanup_htab_delete (visited_not_found);
3493
3494 /* The rule is CUs specify all the files, including those used by
3495 any TU, so there's no need to scan TUs here. */
3496
3497 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3498 {
3499 int j;
3500 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3501 struct quick_file_names *file_data;
3502 void **slot;
3503
3504 per_cu->v.quick->mark = 0;
3505
3506 /* We only need to look at symtabs not already expanded. */
3507 if (per_cu->v.quick->symtab)
3508 continue;
3509
3510 file_data = dw2_get_file_names (per_cu);
3511 if (file_data == NULL)
3512 continue;
3513
3514 if (htab_find (visited_not_found, file_data) != NULL)
3515 continue;
3516 else if (htab_find (visited_found, file_data) != NULL)
3517 {
3518 per_cu->v.quick->mark = 1;
3519 continue;
3520 }
3521
3522 for (j = 0; j < file_data->num_file_names; ++j)
3523 {
3524 const char *this_real_name;
3525
3526 if (file_matcher (file_data->file_names[j], data, 0))
3527 {
3528 per_cu->v.quick->mark = 1;
3529 break;
3530 }
3531
3532 /* Before we invoke realpath, which can get expensive when many
3533 files are involved, do a quick comparison of the basenames. */
3534 if (!basenames_may_differ
3535 && !file_matcher (lbasename (file_data->file_names[j]),
3536 data, 1))
3537 continue;
3538
3539 this_real_name = dw2_get_real_path (objfile, file_data, j);
3540 if (file_matcher (this_real_name, data, 0))
3541 {
3542 per_cu->v.quick->mark = 1;
3543 break;
3544 }
3545 }
3546
3547 slot = htab_find_slot (per_cu->v.quick->mark
3548 ? visited_found
3549 : visited_not_found,
3550 file_data, INSERT);
3551 *slot = file_data;
3552 }
3553
3554 do_cleanups (cleanup);
3555 }
3556
3557 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3558 {
3559 offset_type idx = 2 * iter;
3560 const char *name;
3561 offset_type *vec, vec_len, vec_idx;
3562
3563 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3564 continue;
3565
3566 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3567
3568 if (! (*name_matcher) (name, data))
3569 continue;
3570
3571 /* The name was matched, now expand corresponding CUs that were
3572 marked. */
3573 vec = (offset_type *) (index->constant_pool
3574 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3575 vec_len = MAYBE_SWAP (vec[0]);
3576 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3577 {
3578 struct dwarf2_per_cu_data *per_cu;
3579 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3580 gdb_index_symbol_kind symbol_kind =
3581 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3582 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3583 /* Only check the symbol attributes if they're present.
3584 Indices prior to version 7 don't record them,
3585 and indices >= 7 may elide them for certain symbols
3586 (gold does this). */
3587 int attrs_valid =
3588 (index->version >= 7
3589 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3590
3591 /* Only check the symbol's kind if it has one. */
3592 if (attrs_valid)
3593 {
3594 switch (kind)
3595 {
3596 case VARIABLES_DOMAIN:
3597 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3598 continue;
3599 break;
3600 case FUNCTIONS_DOMAIN:
3601 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3602 continue;
3603 break;
3604 case TYPES_DOMAIN:
3605 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3606 continue;
3607 break;
3608 default:
3609 break;
3610 }
3611 }
3612
3613 /* Don't crash on bad data. */
3614 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3615 + dwarf2_per_objfile->n_type_units))
3616 {
3617 complaint (&symfile_complaints,
3618 _(".gdb_index entry has bad CU index"
3619 " [in module %s]"), objfile->name);
3620 continue;
3621 }
3622
3623 per_cu = dw2_get_cu (cu_index);
3624 if (file_matcher == NULL || per_cu->v.quick->mark)
3625 dw2_instantiate_symtab (per_cu);
3626 }
3627 }
3628 }
3629
3630 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3631 symtab. */
3632
3633 static struct symtab *
3634 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3635 {
3636 int i;
3637
3638 if (BLOCKVECTOR (symtab) != NULL
3639 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3640 return symtab;
3641
3642 if (symtab->includes == NULL)
3643 return NULL;
3644
3645 for (i = 0; symtab->includes[i]; ++i)
3646 {
3647 struct symtab *s = symtab->includes[i];
3648
3649 s = recursively_find_pc_sect_symtab (s, pc);
3650 if (s != NULL)
3651 return s;
3652 }
3653
3654 return NULL;
3655 }
3656
3657 static struct symtab *
3658 dw2_find_pc_sect_symtab (struct objfile *objfile,
3659 struct minimal_symbol *msymbol,
3660 CORE_ADDR pc,
3661 struct obj_section *section,
3662 int warn_if_readin)
3663 {
3664 struct dwarf2_per_cu_data *data;
3665 struct symtab *result;
3666
3667 dw2_setup (objfile);
3668
3669 if (!objfile->psymtabs_addrmap)
3670 return NULL;
3671
3672 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3673 if (!data)
3674 return NULL;
3675
3676 if (warn_if_readin && data->v.quick->symtab)
3677 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3678 paddress (get_objfile_arch (objfile), pc));
3679
3680 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3681 gdb_assert (result != NULL);
3682 return result;
3683 }
3684
3685 static void
3686 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3687 void *data, int need_fullname)
3688 {
3689 int i;
3690 struct cleanup *cleanup;
3691 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3692 NULL, xcalloc, xfree);
3693
3694 cleanup = make_cleanup_htab_delete (visited);
3695 dw2_setup (objfile);
3696
3697 /* The rule is CUs specify all the files, including those used by
3698 any TU, so there's no need to scan TUs here.
3699 We can ignore file names coming from already-expanded CUs. */
3700
3701 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3702 {
3703 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3704
3705 if (per_cu->v.quick->symtab)
3706 {
3707 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3708 INSERT);
3709
3710 *slot = per_cu->v.quick->file_names;
3711 }
3712 }
3713
3714 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3715 {
3716 int j;
3717 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3718 struct quick_file_names *file_data;
3719 void **slot;
3720
3721 /* We only need to look at symtabs not already expanded. */
3722 if (per_cu->v.quick->symtab)
3723 continue;
3724
3725 file_data = dw2_get_file_names (per_cu);
3726 if (file_data == NULL)
3727 continue;
3728
3729 slot = htab_find_slot (visited, file_data, INSERT);
3730 if (*slot)
3731 {
3732 /* Already visited. */
3733 continue;
3734 }
3735 *slot = file_data;
3736
3737 for (j = 0; j < file_data->num_file_names; ++j)
3738 {
3739 const char *this_real_name;
3740
3741 if (need_fullname)
3742 this_real_name = dw2_get_real_path (objfile, file_data, j);
3743 else
3744 this_real_name = NULL;
3745 (*fun) (file_data->file_names[j], this_real_name, data);
3746 }
3747 }
3748
3749 do_cleanups (cleanup);
3750 }
3751
3752 static int
3753 dw2_has_symbols (struct objfile *objfile)
3754 {
3755 return 1;
3756 }
3757
3758 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3759 {
3760 dw2_has_symbols,
3761 dw2_find_last_source_symtab,
3762 dw2_forget_cached_source_info,
3763 dw2_map_symtabs_matching_filename,
3764 dw2_lookup_symbol,
3765 dw2_print_stats,
3766 dw2_dump,
3767 dw2_relocate,
3768 dw2_expand_symtabs_for_function,
3769 dw2_expand_all_symtabs,
3770 dw2_expand_symtabs_with_fullname,
3771 dw2_map_matching_symbols,
3772 dw2_expand_symtabs_matching,
3773 dw2_find_pc_sect_symtab,
3774 dw2_map_symbol_filenames
3775 };
3776
3777 /* Initialize for reading DWARF for this objfile. Return 0 if this
3778 file will use psymtabs, or 1 if using the GNU index. */
3779
3780 int
3781 dwarf2_initialize_objfile (struct objfile *objfile)
3782 {
3783 /* If we're about to read full symbols, don't bother with the
3784 indices. In this case we also don't care if some other debug
3785 format is making psymtabs, because they are all about to be
3786 expanded anyway. */
3787 if ((objfile->flags & OBJF_READNOW))
3788 {
3789 int i;
3790
3791 dwarf2_per_objfile->using_index = 1;
3792 create_all_comp_units (objfile);
3793 create_all_type_units (objfile);
3794 dwarf2_per_objfile->quick_file_names_table =
3795 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3796
3797 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3798 + dwarf2_per_objfile->n_type_units); ++i)
3799 {
3800 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3801
3802 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3803 struct dwarf2_per_cu_quick_data);
3804 }
3805
3806 /* Return 1 so that gdb sees the "quick" functions. However,
3807 these functions will be no-ops because we will have expanded
3808 all symtabs. */
3809 return 1;
3810 }
3811
3812 if (dwarf2_read_index (objfile))
3813 return 1;
3814
3815 return 0;
3816 }
3817
3818 \f
3819
3820 /* Build a partial symbol table. */
3821
3822 void
3823 dwarf2_build_psymtabs (struct objfile *objfile)
3824 {
3825 volatile struct gdb_exception except;
3826
3827 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3828 {
3829 init_psymbol_list (objfile, 1024);
3830 }
3831
3832 TRY_CATCH (except, RETURN_MASK_ERROR)
3833 {
3834 /* This isn't really ideal: all the data we allocate on the
3835 objfile's obstack is still uselessly kept around. However,
3836 freeing it seems unsafe. */
3837 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3838
3839 dwarf2_build_psymtabs_hard (objfile);
3840 discard_cleanups (cleanups);
3841 }
3842 if (except.reason < 0)
3843 exception_print (gdb_stderr, except);
3844 }
3845
3846 /* Return the total length of the CU described by HEADER. */
3847
3848 static unsigned int
3849 get_cu_length (const struct comp_unit_head *header)
3850 {
3851 return header->initial_length_size + header->length;
3852 }
3853
3854 /* Return TRUE if OFFSET is within CU_HEADER. */
3855
3856 static inline int
3857 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3858 {
3859 sect_offset bottom = { cu_header->offset.sect_off };
3860 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
3861
3862 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3863 }
3864
3865 /* Find the base address of the compilation unit for range lists and
3866 location lists. It will normally be specified by DW_AT_low_pc.
3867 In DWARF-3 draft 4, the base address could be overridden by
3868 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3869 compilation units with discontinuous ranges. */
3870
3871 static void
3872 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3873 {
3874 struct attribute *attr;
3875
3876 cu->base_known = 0;
3877 cu->base_address = 0;
3878
3879 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3880 if (attr)
3881 {
3882 cu->base_address = DW_ADDR (attr);
3883 cu->base_known = 1;
3884 }
3885 else
3886 {
3887 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3888 if (attr)
3889 {
3890 cu->base_address = DW_ADDR (attr);
3891 cu->base_known = 1;
3892 }
3893 }
3894 }
3895
3896 /* Read in the comp unit header information from the debug_info at info_ptr.
3897 NOTE: This leaves members offset, first_die_offset to be filled in
3898 by the caller. */
3899
3900 static const gdb_byte *
3901 read_comp_unit_head (struct comp_unit_head *cu_header,
3902 const gdb_byte *info_ptr, bfd *abfd)
3903 {
3904 int signed_addr;
3905 unsigned int bytes_read;
3906
3907 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3908 cu_header->initial_length_size = bytes_read;
3909 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3910 info_ptr += bytes_read;
3911 cu_header->version = read_2_bytes (abfd, info_ptr);
3912 info_ptr += 2;
3913 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3914 &bytes_read);
3915 info_ptr += bytes_read;
3916 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3917 info_ptr += 1;
3918 signed_addr = bfd_get_sign_extend_vma (abfd);
3919 if (signed_addr < 0)
3920 internal_error (__FILE__, __LINE__,
3921 _("read_comp_unit_head: dwarf from non elf file"));
3922 cu_header->signed_addr_p = signed_addr;
3923
3924 return info_ptr;
3925 }
3926
3927 /* Helper function that returns the proper abbrev section for
3928 THIS_CU. */
3929
3930 static struct dwarf2_section_info *
3931 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3932 {
3933 struct dwarf2_section_info *abbrev;
3934
3935 if (this_cu->is_dwz)
3936 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3937 else
3938 abbrev = &dwarf2_per_objfile->abbrev;
3939
3940 return abbrev;
3941 }
3942
3943 /* Subroutine of read_and_check_comp_unit_head and
3944 read_and_check_type_unit_head to simplify them.
3945 Perform various error checking on the header. */
3946
3947 static void
3948 error_check_comp_unit_head (struct comp_unit_head *header,
3949 struct dwarf2_section_info *section,
3950 struct dwarf2_section_info *abbrev_section)
3951 {
3952 bfd *abfd = section->asection->owner;
3953 const char *filename = bfd_get_filename (abfd);
3954
3955 if (header->version != 2 && header->version != 3 && header->version != 4)
3956 error (_("Dwarf Error: wrong version in compilation unit header "
3957 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3958 filename);
3959
3960 if (header->abbrev_offset.sect_off
3961 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
3962 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3963 "(offset 0x%lx + 6) [in module %s]"),
3964 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3965 filename);
3966
3967 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3968 avoid potential 32-bit overflow. */
3969 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
3970 > section->size)
3971 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3972 "(offset 0x%lx + 0) [in module %s]"),
3973 (long) header->length, (long) header->offset.sect_off,
3974 filename);
3975 }
3976
3977 /* Read in a CU/TU header and perform some basic error checking.
3978 The contents of the header are stored in HEADER.
3979 The result is a pointer to the start of the first DIE. */
3980
3981 static const gdb_byte *
3982 read_and_check_comp_unit_head (struct comp_unit_head *header,
3983 struct dwarf2_section_info *section,
3984 struct dwarf2_section_info *abbrev_section,
3985 const gdb_byte *info_ptr,
3986 int is_debug_types_section)
3987 {
3988 const gdb_byte *beg_of_comp_unit = info_ptr;
3989 bfd *abfd = section->asection->owner;
3990
3991 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3992
3993 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3994
3995 /* If we're reading a type unit, skip over the signature and
3996 type_offset fields. */
3997 if (is_debug_types_section)
3998 info_ptr += 8 /*signature*/ + header->offset_size;
3999
4000 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4001
4002 error_check_comp_unit_head (header, section, abbrev_section);
4003
4004 return info_ptr;
4005 }
4006
4007 /* Read in the types comp unit header information from .debug_types entry at
4008 types_ptr. The result is a pointer to one past the end of the header. */
4009
4010 static const gdb_byte *
4011 read_and_check_type_unit_head (struct comp_unit_head *header,
4012 struct dwarf2_section_info *section,
4013 struct dwarf2_section_info *abbrev_section,
4014 const gdb_byte *info_ptr,
4015 ULONGEST *signature,
4016 cu_offset *type_offset_in_tu)
4017 {
4018 const gdb_byte *beg_of_comp_unit = info_ptr;
4019 bfd *abfd = section->asection->owner;
4020
4021 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4022
4023 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4024
4025 /* If we're reading a type unit, skip over the signature and
4026 type_offset fields. */
4027 if (signature != NULL)
4028 *signature = read_8_bytes (abfd, info_ptr);
4029 info_ptr += 8;
4030 if (type_offset_in_tu != NULL)
4031 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4032 header->offset_size);
4033 info_ptr += header->offset_size;
4034
4035 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4036
4037 error_check_comp_unit_head (header, section, abbrev_section);
4038
4039 return info_ptr;
4040 }
4041
4042 /* Fetch the abbreviation table offset from a comp or type unit header. */
4043
4044 static sect_offset
4045 read_abbrev_offset (struct dwarf2_section_info *section,
4046 sect_offset offset)
4047 {
4048 bfd *abfd = section->asection->owner;
4049 const gdb_byte *info_ptr;
4050 unsigned int length, initial_length_size, offset_size;
4051 sect_offset abbrev_offset;
4052
4053 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4054 info_ptr = section->buffer + offset.sect_off;
4055 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4056 offset_size = initial_length_size == 4 ? 4 : 8;
4057 info_ptr += initial_length_size + 2 /*version*/;
4058 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4059 return abbrev_offset;
4060 }
4061
4062 /* Allocate a new partial symtab for file named NAME and mark this new
4063 partial symtab as being an include of PST. */
4064
4065 static void
4066 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
4067 struct objfile *objfile)
4068 {
4069 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4070
4071 if (!IS_ABSOLUTE_PATH (subpst->filename))
4072 {
4073 /* It shares objfile->objfile_obstack. */
4074 subpst->dirname = pst->dirname;
4075 }
4076
4077 subpst->section_offsets = pst->section_offsets;
4078 subpst->textlow = 0;
4079 subpst->texthigh = 0;
4080
4081 subpst->dependencies = (struct partial_symtab **)
4082 obstack_alloc (&objfile->objfile_obstack,
4083 sizeof (struct partial_symtab *));
4084 subpst->dependencies[0] = pst;
4085 subpst->number_of_dependencies = 1;
4086
4087 subpst->globals_offset = 0;
4088 subpst->n_global_syms = 0;
4089 subpst->statics_offset = 0;
4090 subpst->n_static_syms = 0;
4091 subpst->symtab = NULL;
4092 subpst->read_symtab = pst->read_symtab;
4093 subpst->readin = 0;
4094
4095 /* No private part is necessary for include psymtabs. This property
4096 can be used to differentiate between such include psymtabs and
4097 the regular ones. */
4098 subpst->read_symtab_private = NULL;
4099 }
4100
4101 /* Read the Line Number Program data and extract the list of files
4102 included by the source file represented by PST. Build an include
4103 partial symtab for each of these included files. */
4104
4105 static void
4106 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4107 struct die_info *die,
4108 struct partial_symtab *pst)
4109 {
4110 struct line_header *lh = NULL;
4111 struct attribute *attr;
4112
4113 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4114 if (attr)
4115 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
4116 if (lh == NULL)
4117 return; /* No linetable, so no includes. */
4118
4119 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4120 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
4121
4122 free_line_header (lh);
4123 }
4124
4125 static hashval_t
4126 hash_signatured_type (const void *item)
4127 {
4128 const struct signatured_type *sig_type = item;
4129
4130 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4131 return sig_type->signature;
4132 }
4133
4134 static int
4135 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4136 {
4137 const struct signatured_type *lhs = item_lhs;
4138 const struct signatured_type *rhs = item_rhs;
4139
4140 return lhs->signature == rhs->signature;
4141 }
4142
4143 /* Allocate a hash table for signatured types. */
4144
4145 static htab_t
4146 allocate_signatured_type_table (struct objfile *objfile)
4147 {
4148 return htab_create_alloc_ex (41,
4149 hash_signatured_type,
4150 eq_signatured_type,
4151 NULL,
4152 &objfile->objfile_obstack,
4153 hashtab_obstack_allocate,
4154 dummy_obstack_deallocate);
4155 }
4156
4157 /* A helper function to add a signatured type CU to a table. */
4158
4159 static int
4160 add_signatured_type_cu_to_table (void **slot, void *datum)
4161 {
4162 struct signatured_type *sigt = *slot;
4163 struct signatured_type ***datap = datum;
4164
4165 **datap = sigt;
4166 ++*datap;
4167
4168 return 1;
4169 }
4170
4171 /* Create the hash table of all entries in the .debug_types
4172 (or .debug_types.dwo) section(s).
4173 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4174 otherwise it is NULL.
4175
4176 The result is a pointer to the hash table or NULL if there are no types.
4177
4178 Note: This function processes DWO files only, not DWP files. */
4179
4180 static htab_t
4181 create_debug_types_hash_table (struct dwo_file *dwo_file,
4182 VEC (dwarf2_section_info_def) *types)
4183 {
4184 struct objfile *objfile = dwarf2_per_objfile->objfile;
4185 htab_t types_htab = NULL;
4186 int ix;
4187 struct dwarf2_section_info *section;
4188 struct dwarf2_section_info *abbrev_section;
4189
4190 if (VEC_empty (dwarf2_section_info_def, types))
4191 return NULL;
4192
4193 abbrev_section = (dwo_file != NULL
4194 ? &dwo_file->sections.abbrev
4195 : &dwarf2_per_objfile->abbrev);
4196
4197 if (dwarf2_read_debug)
4198 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4199 dwo_file ? ".dwo" : "",
4200 bfd_get_filename (abbrev_section->asection->owner));
4201
4202 for (ix = 0;
4203 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4204 ++ix)
4205 {
4206 bfd *abfd;
4207 const gdb_byte *info_ptr, *end_ptr;
4208 struct dwarf2_section_info *abbrev_section;
4209
4210 dwarf2_read_section (objfile, section);
4211 info_ptr = section->buffer;
4212
4213 if (info_ptr == NULL)
4214 continue;
4215
4216 /* We can't set abfd until now because the section may be empty or
4217 not present, in which case section->asection will be NULL. */
4218 abfd = section->asection->owner;
4219
4220 if (dwo_file)
4221 abbrev_section = &dwo_file->sections.abbrev;
4222 else
4223 abbrev_section = &dwarf2_per_objfile->abbrev;
4224
4225 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4226 because we don't need to read any dies: the signature is in the
4227 header. */
4228
4229 end_ptr = info_ptr + section->size;
4230 while (info_ptr < end_ptr)
4231 {
4232 sect_offset offset;
4233 cu_offset type_offset_in_tu;
4234 ULONGEST signature;
4235 struct signatured_type *sig_type;
4236 struct dwo_unit *dwo_tu;
4237 void **slot;
4238 const gdb_byte *ptr = info_ptr;
4239 struct comp_unit_head header;
4240 unsigned int length;
4241
4242 offset.sect_off = ptr - section->buffer;
4243
4244 /* We need to read the type's signature in order to build the hash
4245 table, but we don't need anything else just yet. */
4246
4247 ptr = read_and_check_type_unit_head (&header, section,
4248 abbrev_section, ptr,
4249 &signature, &type_offset_in_tu);
4250
4251 length = get_cu_length (&header);
4252
4253 /* Skip dummy type units. */
4254 if (ptr >= info_ptr + length
4255 || peek_abbrev_code (abfd, ptr) == 0)
4256 {
4257 info_ptr += length;
4258 continue;
4259 }
4260
4261 if (types_htab == NULL)
4262 {
4263 if (dwo_file)
4264 types_htab = allocate_dwo_unit_table (objfile);
4265 else
4266 types_htab = allocate_signatured_type_table (objfile);
4267 }
4268
4269 if (dwo_file)
4270 {
4271 sig_type = NULL;
4272 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4273 struct dwo_unit);
4274 dwo_tu->dwo_file = dwo_file;
4275 dwo_tu->signature = signature;
4276 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4277 dwo_tu->section = section;
4278 dwo_tu->offset = offset;
4279 dwo_tu->length = length;
4280 }
4281 else
4282 {
4283 /* N.B.: type_offset is not usable if this type uses a DWO file.
4284 The real type_offset is in the DWO file. */
4285 dwo_tu = NULL;
4286 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4287 struct signatured_type);
4288 sig_type->signature = signature;
4289 sig_type->type_offset_in_tu = type_offset_in_tu;
4290 sig_type->per_cu.objfile = objfile;
4291 sig_type->per_cu.is_debug_types = 1;
4292 sig_type->per_cu.section = section;
4293 sig_type->per_cu.offset = offset;
4294 sig_type->per_cu.length = length;
4295 }
4296
4297 slot = htab_find_slot (types_htab,
4298 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4299 INSERT);
4300 gdb_assert (slot != NULL);
4301 if (*slot != NULL)
4302 {
4303 sect_offset dup_offset;
4304
4305 if (dwo_file)
4306 {
4307 const struct dwo_unit *dup_tu = *slot;
4308
4309 dup_offset = dup_tu->offset;
4310 }
4311 else
4312 {
4313 const struct signatured_type *dup_tu = *slot;
4314
4315 dup_offset = dup_tu->per_cu.offset;
4316 }
4317
4318 complaint (&symfile_complaints,
4319 _("debug type entry at offset 0x%x is duplicate to"
4320 " the entry at offset 0x%x, signature %s"),
4321 offset.sect_off, dup_offset.sect_off,
4322 hex_string (signature));
4323 }
4324 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4325
4326 if (dwarf2_read_debug)
4327 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
4328 offset.sect_off,
4329 hex_string (signature));
4330
4331 info_ptr += length;
4332 }
4333 }
4334
4335 return types_htab;
4336 }
4337
4338 /* Create the hash table of all entries in the .debug_types section,
4339 and initialize all_type_units.
4340 The result is zero if there is an error (e.g. missing .debug_types section),
4341 otherwise non-zero. */
4342
4343 static int
4344 create_all_type_units (struct objfile *objfile)
4345 {
4346 htab_t types_htab;
4347 struct signatured_type **iter;
4348
4349 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4350 if (types_htab == NULL)
4351 {
4352 dwarf2_per_objfile->signatured_types = NULL;
4353 return 0;
4354 }
4355
4356 dwarf2_per_objfile->signatured_types = types_htab;
4357
4358 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4359 dwarf2_per_objfile->all_type_units
4360 = xmalloc (dwarf2_per_objfile->n_type_units
4361 * sizeof (struct signatured_type *));
4362 iter = &dwarf2_per_objfile->all_type_units[0];
4363 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4364 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4365 == dwarf2_per_objfile->n_type_units);
4366
4367 return 1;
4368 }
4369
4370 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
4371 Fill in SIG_ENTRY with DWO_ENTRY. */
4372
4373 static void
4374 fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
4375 struct signatured_type *sig_entry,
4376 struct dwo_unit *dwo_entry)
4377 {
4378 /* Make sure we're not clobbering something we don't expect to. */
4379 gdb_assert (! sig_entry->per_cu.queued);
4380 gdb_assert (sig_entry->per_cu.cu == NULL);
4381 gdb_assert (sig_entry->per_cu.v.quick != NULL);
4382 gdb_assert (sig_entry->per_cu.v.quick->symtab == NULL);
4383 gdb_assert (sig_entry->signature == dwo_entry->signature);
4384 gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
4385 gdb_assert (sig_entry->type_unit_group == NULL);
4386 gdb_assert (sig_entry->dwo_unit == NULL);
4387
4388 sig_entry->per_cu.section = dwo_entry->section;
4389 sig_entry->per_cu.offset = dwo_entry->offset;
4390 sig_entry->per_cu.length = dwo_entry->length;
4391 sig_entry->per_cu.reading_dwo_directly = 1;
4392 sig_entry->per_cu.objfile = objfile;
4393 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
4394 sig_entry->dwo_unit = dwo_entry;
4395 }
4396
4397 /* Subroutine of lookup_signatured_type.
4398 If we haven't read the TU yet, create the signatured_type data structure
4399 for a TU to be read in directly from a DWO file, bypassing the stub.
4400 This is the "Stay in DWO Optimization": When there is no DWP file and we're
4401 using .gdb_index, then when reading a CU we want to stay in the DWO file
4402 containing that CU. Otherwise we could end up reading several other DWO
4403 files (due to comdat folding) to process the transitive closure of all the
4404 mentioned TUs, and that can be slow. The current DWO file will have every
4405 type signature that it needs.
4406 We only do this for .gdb_index because in the psymtab case we already have
4407 to read all the DWOs to build the type unit groups. */
4408
4409 static struct signatured_type *
4410 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4411 {
4412 struct objfile *objfile = dwarf2_per_objfile->objfile;
4413 struct dwo_file *dwo_file;
4414 struct dwo_unit find_dwo_entry, *dwo_entry;
4415 struct signatured_type find_sig_entry, *sig_entry;
4416
4417 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4418
4419 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
4420 dwo_unit of the TU itself. */
4421 dwo_file = cu->dwo_unit->dwo_file;
4422
4423 /* We only ever need to read in one copy of a signatured type.
4424 Just use the global signatured_types array. If this is the first time
4425 we're reading this type, replace the recorded data from .gdb_index with
4426 this TU. */
4427
4428 if (dwarf2_per_objfile->signatured_types == NULL)
4429 return NULL;
4430 find_sig_entry.signature = sig;
4431 sig_entry = htab_find (dwarf2_per_objfile->signatured_types, &find_sig_entry);
4432 if (sig_entry == NULL)
4433 return NULL;
4434
4435 /* We can get here with the TU already read, *or* in the process of being
4436 read. Don't reassign it if that's the case. Also note that if the TU is
4437 already being read, it may not have come from a DWO, the program may be
4438 a mix of Fission-compiled code and non-Fission-compiled code. */
4439 /* Have we already tried to read this TU? */
4440 if (sig_entry->per_cu.tu_read)
4441 return sig_entry;
4442
4443 /* Ok, this is the first time we're reading this TU. */
4444 if (dwo_file->tus == NULL)
4445 return NULL;
4446 find_dwo_entry.signature = sig;
4447 dwo_entry = htab_find (dwo_file->tus, &find_dwo_entry);
4448 if (dwo_entry == NULL)
4449 return NULL;
4450
4451 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4452 return sig_entry;
4453 }
4454
4455 /* Subroutine of lookup_dwp_signatured_type.
4456 Add an entry for signature SIG to dwarf2_per_objfile->signatured_types. */
4457
4458 static struct signatured_type *
4459 add_type_unit (ULONGEST sig)
4460 {
4461 struct objfile *objfile = dwarf2_per_objfile->objfile;
4462 int n_type_units = dwarf2_per_objfile->n_type_units;
4463 struct signatured_type *sig_type;
4464 void **slot;
4465
4466 ++n_type_units;
4467 dwarf2_per_objfile->all_type_units =
4468 xrealloc (dwarf2_per_objfile->all_type_units,
4469 n_type_units * sizeof (struct signatured_type *));
4470 dwarf2_per_objfile->n_type_units = n_type_units;
4471 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4472 struct signatured_type);
4473 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
4474 sig_type->signature = sig;
4475 sig_type->per_cu.is_debug_types = 1;
4476 sig_type->per_cu.v.quick =
4477 OBSTACK_ZALLOC (&objfile->objfile_obstack,
4478 struct dwarf2_per_cu_quick_data);
4479 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4480 sig_type, INSERT);
4481 gdb_assert (*slot == NULL);
4482 *slot = sig_type;
4483 /* The rest of sig_type must be filled in by the caller. */
4484 return sig_type;
4485 }
4486
4487 /* Subroutine of lookup_signatured_type.
4488 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
4489 then try the DWP file.
4490 Normally this "can't happen", but if there's a bug in signature
4491 generation and/or the DWP file is built incorrectly, it can happen.
4492 Using the type directly from the DWP file means we don't have the stub
4493 which has some useful attributes (e.g., DW_AT_comp_dir), but they're
4494 not critical. [Eventually the stub may go away for type units anyway.] */
4495
4496 static struct signatured_type *
4497 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4498 {
4499 struct objfile *objfile = dwarf2_per_objfile->objfile;
4500 struct dwp_file *dwp_file = get_dwp_file ();
4501 struct dwo_unit *dwo_entry;
4502 struct signatured_type find_sig_entry, *sig_entry;
4503
4504 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4505 gdb_assert (dwp_file != NULL);
4506
4507 if (dwarf2_per_objfile->signatured_types != NULL)
4508 {
4509 find_sig_entry.signature = sig;
4510 sig_entry = htab_find (dwarf2_per_objfile->signatured_types,
4511 &find_sig_entry);
4512 if (sig_entry != NULL)
4513 return sig_entry;
4514 }
4515
4516 /* This is the "shouldn't happen" case.
4517 Try the DWP file and hope for the best. */
4518 if (dwp_file->tus == NULL)
4519 return NULL;
4520 dwo_entry = lookup_dwo_in_dwp (dwp_file, dwp_file->tus, NULL,
4521 sig, 1 /* is_debug_types */);
4522 if (dwo_entry == NULL)
4523 return NULL;
4524
4525 sig_entry = add_type_unit (sig);
4526 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4527
4528 /* The caller will signal a complaint if we return NULL.
4529 Here we don't return NULL but we still want to complain. */
4530 complaint (&symfile_complaints,
4531 _("Bad type signature %s referenced by %s at 0x%x,"
4532 " coping by using copy in DWP [in module %s]"),
4533 hex_string (sig),
4534 cu->per_cu->is_debug_types ? "TU" : "CU",
4535 cu->per_cu->offset.sect_off,
4536 objfile->name);
4537
4538 return sig_entry;
4539 }
4540
4541 /* Lookup a signature based type for DW_FORM_ref_sig8.
4542 Returns NULL if signature SIG is not present in the table.
4543 It is up to the caller to complain about this. */
4544
4545 static struct signatured_type *
4546 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4547 {
4548 if (cu->dwo_unit
4549 && dwarf2_per_objfile->using_index)
4550 {
4551 /* We're in a DWO/DWP file, and we're using .gdb_index.
4552 These cases require special processing. */
4553 if (get_dwp_file () == NULL)
4554 return lookup_dwo_signatured_type (cu, sig);
4555 else
4556 return lookup_dwp_signatured_type (cu, sig);
4557 }
4558 else
4559 {
4560 struct signatured_type find_entry, *entry;
4561
4562 if (dwarf2_per_objfile->signatured_types == NULL)
4563 return NULL;
4564 find_entry.signature = sig;
4565 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4566 return entry;
4567 }
4568 }
4569 \f
4570 /* Low level DIE reading support. */
4571
4572 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4573
4574 static void
4575 init_cu_die_reader (struct die_reader_specs *reader,
4576 struct dwarf2_cu *cu,
4577 struct dwarf2_section_info *section,
4578 struct dwo_file *dwo_file)
4579 {
4580 gdb_assert (section->readin && section->buffer != NULL);
4581 reader->abfd = section->asection->owner;
4582 reader->cu = cu;
4583 reader->dwo_file = dwo_file;
4584 reader->die_section = section;
4585 reader->buffer = section->buffer;
4586 reader->buffer_end = section->buffer + section->size;
4587 reader->comp_dir = NULL;
4588 }
4589
4590 /* Subroutine of init_cutu_and_read_dies to simplify it.
4591 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
4592 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
4593 already.
4594
4595 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
4596 from it to the DIE in the DWO. If NULL we are skipping the stub.
4597 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
4598 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
4599 attribute of the referencing CU. Exactly one of STUB_COMP_UNIT_DIE and
4600 COMP_DIR must be non-NULL.
4601 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
4602 are filled in with the info of the DIE from the DWO file.
4603 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
4604 provided an abbrev table to use.
4605 The result is non-zero if a valid (non-dummy) DIE was found. */
4606
4607 static int
4608 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
4609 struct dwo_unit *dwo_unit,
4610 int abbrev_table_provided,
4611 struct die_info *stub_comp_unit_die,
4612 const char *stub_comp_dir,
4613 struct die_reader_specs *result_reader,
4614 const gdb_byte **result_info_ptr,
4615 struct die_info **result_comp_unit_die,
4616 int *result_has_children)
4617 {
4618 struct objfile *objfile = dwarf2_per_objfile->objfile;
4619 struct dwarf2_cu *cu = this_cu->cu;
4620 struct dwarf2_section_info *section;
4621 bfd *abfd;
4622 const gdb_byte *begin_info_ptr, *info_ptr;
4623 const char *comp_dir_string;
4624 ULONGEST signature; /* Or dwo_id. */
4625 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4626 int i,num_extra_attrs;
4627 struct dwarf2_section_info *dwo_abbrev_section;
4628 struct attribute *attr;
4629 struct attribute comp_dir_attr;
4630 struct die_info *comp_unit_die;
4631
4632 /* Both can't be provided. */
4633 gdb_assert (! (stub_comp_unit_die && stub_comp_dir));
4634
4635 /* These attributes aren't processed until later:
4636 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4637 However, the attribute is found in the stub which we won't have later.
4638 In order to not impose this complication on the rest of the code,
4639 we read them here and copy them to the DWO CU/TU die. */
4640
4641 stmt_list = NULL;
4642 low_pc = NULL;
4643 high_pc = NULL;
4644 ranges = NULL;
4645 comp_dir = NULL;
4646
4647 if (stub_comp_unit_die != NULL)
4648 {
4649 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4650 DWO file. */
4651 if (! this_cu->is_debug_types)
4652 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
4653 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
4654 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
4655 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
4656 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
4657
4658 /* There should be a DW_AT_addr_base attribute here (if needed).
4659 We need the value before we can process DW_FORM_GNU_addr_index. */
4660 cu->addr_base = 0;
4661 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
4662 if (attr)
4663 cu->addr_base = DW_UNSND (attr);
4664
4665 /* There should be a DW_AT_ranges_base attribute here (if needed).
4666 We need the value before we can process DW_AT_ranges. */
4667 cu->ranges_base = 0;
4668 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
4669 if (attr)
4670 cu->ranges_base = DW_UNSND (attr);
4671 }
4672 else if (stub_comp_dir != NULL)
4673 {
4674 /* Reconstruct the comp_dir attribute to simplify the code below. */
4675 comp_dir = (struct attribute *)
4676 obstack_alloc (&cu->comp_unit_obstack, sizeof (*comp_dir));
4677 comp_dir->name = DW_AT_comp_dir;
4678 comp_dir->form = DW_FORM_string;
4679 DW_STRING_IS_CANONICAL (comp_dir) = 0;
4680 DW_STRING (comp_dir) = stub_comp_dir;
4681 }
4682
4683 /* Set up for reading the DWO CU/TU. */
4684 cu->dwo_unit = dwo_unit;
4685 section = dwo_unit->section;
4686 dwarf2_read_section (objfile, section);
4687 abfd = section->asection->owner;
4688 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4689 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4690 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
4691
4692 if (this_cu->is_debug_types)
4693 {
4694 ULONGEST header_signature;
4695 cu_offset type_offset_in_tu;
4696 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
4697
4698 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4699 dwo_abbrev_section,
4700 info_ptr,
4701 &header_signature,
4702 &type_offset_in_tu);
4703 /* This is not an assert because it can be caused by bad debug info. */
4704 if (sig_type->signature != header_signature)
4705 {
4706 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
4707 " TU at offset 0x%x [in module %s]"),
4708 hex_string (sig_type->signature),
4709 hex_string (header_signature),
4710 dwo_unit->offset.sect_off,
4711 bfd_get_filename (abfd));
4712 }
4713 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4714 /* For DWOs coming from DWP files, we don't know the CU length
4715 nor the type's offset in the TU until now. */
4716 dwo_unit->length = get_cu_length (&cu->header);
4717 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4718
4719 /* Establish the type offset that can be used to lookup the type.
4720 For DWO files, we don't know it until now. */
4721 sig_type->type_offset_in_section.sect_off =
4722 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4723 }
4724 else
4725 {
4726 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4727 dwo_abbrev_section,
4728 info_ptr, 0);
4729 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4730 /* For DWOs coming from DWP files, we don't know the CU length
4731 until now. */
4732 dwo_unit->length = get_cu_length (&cu->header);
4733 }
4734
4735 /* Replace the CU's original abbrev table with the DWO's.
4736 Reminder: We can't read the abbrev table until we've read the header. */
4737 if (abbrev_table_provided)
4738 {
4739 /* Don't free the provided abbrev table, the caller of
4740 init_cutu_and_read_dies owns it. */
4741 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4742 /* Ensure the DWO abbrev table gets freed. */
4743 make_cleanup (dwarf2_free_abbrev_table, cu);
4744 }
4745 else
4746 {
4747 dwarf2_free_abbrev_table (cu);
4748 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4749 /* Leave any existing abbrev table cleanup as is. */
4750 }
4751
4752 /* Read in the die, but leave space to copy over the attributes
4753 from the stub. This has the benefit of simplifying the rest of
4754 the code - all the work to maintain the illusion of a single
4755 DW_TAG_{compile,type}_unit DIE is done here. */
4756 num_extra_attrs = ((stmt_list != NULL)
4757 + (low_pc != NULL)
4758 + (high_pc != NULL)
4759 + (ranges != NULL)
4760 + (comp_dir != NULL));
4761 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
4762 result_has_children, num_extra_attrs);
4763
4764 /* Copy over the attributes from the stub to the DIE we just read in. */
4765 comp_unit_die = *result_comp_unit_die;
4766 i = comp_unit_die->num_attrs;
4767 if (stmt_list != NULL)
4768 comp_unit_die->attrs[i++] = *stmt_list;
4769 if (low_pc != NULL)
4770 comp_unit_die->attrs[i++] = *low_pc;
4771 if (high_pc != NULL)
4772 comp_unit_die->attrs[i++] = *high_pc;
4773 if (ranges != NULL)
4774 comp_unit_die->attrs[i++] = *ranges;
4775 if (comp_dir != NULL)
4776 comp_unit_die->attrs[i++] = *comp_dir;
4777 comp_unit_die->num_attrs += num_extra_attrs;
4778
4779 if (dwarf2_die_debug)
4780 {
4781 fprintf_unfiltered (gdb_stdlog,
4782 "Read die from %s@0x%x of %s:\n",
4783 bfd_section_name (abfd, section->asection),
4784 (unsigned) (begin_info_ptr - section->buffer),
4785 bfd_get_filename (abfd));
4786 dump_die (comp_unit_die, dwarf2_die_debug);
4787 }
4788
4789 /* Save the comp_dir attribute. If there is no DWP file then we'll read
4790 TUs by skipping the stub and going directly to the entry in the DWO file.
4791 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
4792 to get it via circuitous means. Blech. */
4793 if (comp_dir != NULL)
4794 result_reader->comp_dir = DW_STRING (comp_dir);
4795
4796 /* Skip dummy compilation units. */
4797 if (info_ptr >= begin_info_ptr + dwo_unit->length
4798 || peek_abbrev_code (abfd, info_ptr) == 0)
4799 return 0;
4800
4801 *result_info_ptr = info_ptr;
4802 return 1;
4803 }
4804
4805 /* Subroutine of init_cutu_and_read_dies to simplify it.
4806 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
4807 Returns NULL if the specified DWO unit cannot be found. */
4808
4809 static struct dwo_unit *
4810 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
4811 struct die_info *comp_unit_die)
4812 {
4813 struct dwarf2_cu *cu = this_cu->cu;
4814 struct attribute *attr;
4815 ULONGEST signature;
4816 struct dwo_unit *dwo_unit;
4817 const char *comp_dir, *dwo_name;
4818
4819 gdb_assert (cu != NULL);
4820
4821 /* Yeah, we look dwo_name up again, but it simplifies the code. */
4822 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4823 gdb_assert (attr != NULL);
4824 dwo_name = DW_STRING (attr);
4825 comp_dir = NULL;
4826 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4827 if (attr)
4828 comp_dir = DW_STRING (attr);
4829
4830 if (this_cu->is_debug_types)
4831 {
4832 struct signatured_type *sig_type;
4833
4834 /* Since this_cu is the first member of struct signatured_type,
4835 we can go from a pointer to one to a pointer to the other. */
4836 sig_type = (struct signatured_type *) this_cu;
4837 signature = sig_type->signature;
4838 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4839 }
4840 else
4841 {
4842 struct attribute *attr;
4843
4844 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4845 if (! attr)
4846 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
4847 " [in module %s]"),
4848 dwo_name, this_cu->objfile->name);
4849 signature = DW_UNSND (attr);
4850 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4851 signature);
4852 }
4853
4854 return dwo_unit;
4855 }
4856
4857 /* Subroutine of init_cutu_and_read_dies to simplify it.
4858 Read a TU directly from a DWO file, bypassing the stub. */
4859
4860 static void
4861 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu, int keep,
4862 die_reader_func_ftype *die_reader_func,
4863 void *data)
4864 {
4865 struct dwarf2_cu *cu;
4866 struct signatured_type *sig_type;
4867 struct cleanup *cleanups, *free_cu_cleanup;
4868 struct die_reader_specs reader;
4869 const gdb_byte *info_ptr;
4870 struct die_info *comp_unit_die;
4871 int has_children;
4872
4873 /* Verify we can do the following downcast, and that we have the
4874 data we need. */
4875 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
4876 sig_type = (struct signatured_type *) this_cu;
4877 gdb_assert (sig_type->dwo_unit != NULL);
4878
4879 cleanups = make_cleanup (null_cleanup, NULL);
4880
4881 gdb_assert (this_cu->cu == NULL);
4882 cu = xmalloc (sizeof (*cu));
4883 init_one_comp_unit (cu, this_cu);
4884 /* If an error occurs while loading, release our storage. */
4885 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4886
4887 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
4888 0 /* abbrev_table_provided */,
4889 NULL /* stub_comp_unit_die */,
4890 sig_type->dwo_unit->dwo_file->comp_dir,
4891 &reader, &info_ptr,
4892 &comp_unit_die, &has_children) == 0)
4893 {
4894 /* Dummy die. */
4895 do_cleanups (cleanups);
4896 return;
4897 }
4898
4899 /* All the "real" work is done here. */
4900 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4901
4902 /* This duplicates some code in init_cutu_and_read_dies,
4903 but the alternative is making the latter more complex.
4904 This function is only for the special case of using DWO files directly:
4905 no point in overly complicating the general case just to handle this. */
4906 if (keep)
4907 {
4908 /* We've successfully allocated this compilation unit. Let our
4909 caller clean it up when finished with it. */
4910 discard_cleanups (free_cu_cleanup);
4911
4912 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4913 So we have to manually free the abbrev table. */
4914 dwarf2_free_abbrev_table (cu);
4915
4916 /* Link this CU into read_in_chain. */
4917 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4918 dwarf2_per_objfile->read_in_chain = this_cu;
4919 }
4920 else
4921 do_cleanups (free_cu_cleanup);
4922
4923 do_cleanups (cleanups);
4924 }
4925
4926 /* Initialize a CU (or TU) and read its DIEs.
4927 If the CU defers to a DWO file, read the DWO file as well.
4928
4929 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4930 Otherwise the table specified in the comp unit header is read in and used.
4931 This is an optimization for when we already have the abbrev table.
4932
4933 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4934 Otherwise, a new CU is allocated with xmalloc.
4935
4936 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4937 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4938
4939 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4940 linker) then DIE_READER_FUNC will not get called. */
4941
4942 static void
4943 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
4944 struct abbrev_table *abbrev_table,
4945 int use_existing_cu, int keep,
4946 die_reader_func_ftype *die_reader_func,
4947 void *data)
4948 {
4949 struct objfile *objfile = dwarf2_per_objfile->objfile;
4950 struct dwarf2_section_info *section = this_cu->section;
4951 bfd *abfd = section->asection->owner;
4952 struct dwarf2_cu *cu;
4953 const gdb_byte *begin_info_ptr, *info_ptr;
4954 struct die_reader_specs reader;
4955 struct die_info *comp_unit_die;
4956 int has_children;
4957 struct attribute *attr;
4958 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4959 struct signatured_type *sig_type = NULL;
4960 struct dwarf2_section_info *abbrev_section;
4961 /* Non-zero if CU currently points to a DWO file and we need to
4962 reread it. When this happens we need to reread the skeleton die
4963 before we can reread the DWO file (this only applies to CUs, not TUs). */
4964 int rereading_dwo_cu = 0;
4965
4966 if (dwarf2_die_debug)
4967 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4968 this_cu->is_debug_types ? "type" : "comp",
4969 this_cu->offset.sect_off);
4970
4971 if (use_existing_cu)
4972 gdb_assert (keep);
4973
4974 /* If we're reading a TU directly from a DWO file, including a virtual DWO
4975 file (instead of going through the stub), short-circuit all of this. */
4976 if (this_cu->reading_dwo_directly)
4977 {
4978 /* Narrow down the scope of possibilities to have to understand. */
4979 gdb_assert (this_cu->is_debug_types);
4980 gdb_assert (abbrev_table == NULL);
4981 gdb_assert (!use_existing_cu);
4982 init_tu_and_read_dwo_dies (this_cu, keep, die_reader_func, data);
4983 return;
4984 }
4985
4986 cleanups = make_cleanup (null_cleanup, NULL);
4987
4988 /* This is cheap if the section is already read in. */
4989 dwarf2_read_section (objfile, section);
4990
4991 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4992
4993 abbrev_section = get_abbrev_section_for_cu (this_cu);
4994
4995 if (use_existing_cu && this_cu->cu != NULL)
4996 {
4997 cu = this_cu->cu;
4998
4999 /* If this CU is from a DWO file we need to start over, we need to
5000 refetch the attributes from the skeleton CU.
5001 This could be optimized by retrieving those attributes from when we
5002 were here the first time: the previous comp_unit_die was stored in
5003 comp_unit_obstack. But there's no data yet that we need this
5004 optimization. */
5005 if (cu->dwo_unit != NULL)
5006 rereading_dwo_cu = 1;
5007 }
5008 else
5009 {
5010 /* If !use_existing_cu, this_cu->cu must be NULL. */
5011 gdb_assert (this_cu->cu == NULL);
5012
5013 cu = xmalloc (sizeof (*cu));
5014 init_one_comp_unit (cu, this_cu);
5015
5016 /* If an error occurs while loading, release our storage. */
5017 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5018 }
5019
5020 /* Get the header. */
5021 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
5022 {
5023 /* We already have the header, there's no need to read it in again. */
5024 info_ptr += cu->header.first_die_offset.cu_off;
5025 }
5026 else
5027 {
5028 if (this_cu->is_debug_types)
5029 {
5030 ULONGEST signature;
5031 cu_offset type_offset_in_tu;
5032
5033 info_ptr = read_and_check_type_unit_head (&cu->header, section,
5034 abbrev_section, info_ptr,
5035 &signature,
5036 &type_offset_in_tu);
5037
5038 /* Since per_cu is the first member of struct signatured_type,
5039 we can go from a pointer to one to a pointer to the other. */
5040 sig_type = (struct signatured_type *) this_cu;
5041 gdb_assert (sig_type->signature == signature);
5042 gdb_assert (sig_type->type_offset_in_tu.cu_off
5043 == type_offset_in_tu.cu_off);
5044 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5045
5046 /* LENGTH has not been set yet for type units if we're
5047 using .gdb_index. */
5048 this_cu->length = get_cu_length (&cu->header);
5049
5050 /* Establish the type offset that can be used to lookup the type. */
5051 sig_type->type_offset_in_section.sect_off =
5052 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
5053 }
5054 else
5055 {
5056 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5057 abbrev_section,
5058 info_ptr, 0);
5059
5060 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5061 gdb_assert (this_cu->length == get_cu_length (&cu->header));
5062 }
5063 }
5064
5065 /* Skip dummy compilation units. */
5066 if (info_ptr >= begin_info_ptr + this_cu->length
5067 || peek_abbrev_code (abfd, info_ptr) == 0)
5068 {
5069 do_cleanups (cleanups);
5070 return;
5071 }
5072
5073 /* If we don't have them yet, read the abbrevs for this compilation unit.
5074 And if we need to read them now, make sure they're freed when we're
5075 done. Note that it's important that if the CU had an abbrev table
5076 on entry we don't free it when we're done: Somewhere up the call stack
5077 it may be in use. */
5078 if (abbrev_table != NULL)
5079 {
5080 gdb_assert (cu->abbrev_table == NULL);
5081 gdb_assert (cu->header.abbrev_offset.sect_off
5082 == abbrev_table->offset.sect_off);
5083 cu->abbrev_table = abbrev_table;
5084 }
5085 else if (cu->abbrev_table == NULL)
5086 {
5087 dwarf2_read_abbrevs (cu, abbrev_section);
5088 make_cleanup (dwarf2_free_abbrev_table, cu);
5089 }
5090 else if (rereading_dwo_cu)
5091 {
5092 dwarf2_free_abbrev_table (cu);
5093 dwarf2_read_abbrevs (cu, abbrev_section);
5094 }
5095
5096 /* Read the top level CU/TU die. */
5097 init_cu_die_reader (&reader, cu, section, NULL);
5098 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5099
5100 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5101 from the DWO file.
5102 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5103 DWO CU, that this test will fail (the attribute will not be present). */
5104 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5105 if (attr)
5106 {
5107 struct dwo_unit *dwo_unit;
5108 struct die_info *dwo_comp_unit_die;
5109
5110 if (has_children)
5111 {
5112 complaint (&symfile_complaints,
5113 _("compilation unit with DW_AT_GNU_dwo_name"
5114 " has children (offset 0x%x) [in module %s]"),
5115 this_cu->offset.sect_off, bfd_get_filename (abfd));
5116 }
5117 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
5118 if (dwo_unit != NULL)
5119 {
5120 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5121 abbrev_table != NULL,
5122 comp_unit_die, NULL,
5123 &reader, &info_ptr,
5124 &dwo_comp_unit_die, &has_children) == 0)
5125 {
5126 /* Dummy die. */
5127 do_cleanups (cleanups);
5128 return;
5129 }
5130 comp_unit_die = dwo_comp_unit_die;
5131 }
5132 else
5133 {
5134 /* Yikes, we couldn't find the rest of the DIE, we only have
5135 the stub. A complaint has already been logged. There's
5136 not much more we can do except pass on the stub DIE to
5137 die_reader_func. We don't want to throw an error on bad
5138 debug info. */
5139 }
5140 }
5141
5142 /* All of the above is setup for this call. Yikes. */
5143 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5144
5145 /* Done, clean up. */
5146 if (free_cu_cleanup != NULL)
5147 {
5148 if (keep)
5149 {
5150 /* We've successfully allocated this compilation unit. Let our
5151 caller clean it up when finished with it. */
5152 discard_cleanups (free_cu_cleanup);
5153
5154 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5155 So we have to manually free the abbrev table. */
5156 dwarf2_free_abbrev_table (cu);
5157
5158 /* Link this CU into read_in_chain. */
5159 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5160 dwarf2_per_objfile->read_in_chain = this_cu;
5161 }
5162 else
5163 do_cleanups (free_cu_cleanup);
5164 }
5165
5166 do_cleanups (cleanups);
5167 }
5168
5169 /* Read CU/TU THIS_CU in section SECTION,
5170 but do not follow DW_AT_GNU_dwo_name if present.
5171 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
5172 to have already done the lookup to find the DWO/DWP file).
5173
5174 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
5175 THIS_CU->is_debug_types, but nothing else.
5176
5177 We fill in THIS_CU->length.
5178
5179 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5180 linker) then DIE_READER_FUNC will not get called.
5181
5182 THIS_CU->cu is always freed when done.
5183 This is done in order to not leave THIS_CU->cu in a state where we have
5184 to care whether it refers to the "main" CU or the DWO CU. */
5185
5186 static void
5187 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
5188 struct dwarf2_section_info *abbrev_section,
5189 struct dwo_file *dwo_file,
5190 die_reader_func_ftype *die_reader_func,
5191 void *data)
5192 {
5193 struct objfile *objfile = dwarf2_per_objfile->objfile;
5194 struct dwarf2_section_info *section = this_cu->section;
5195 bfd *abfd = section->asection->owner;
5196 struct dwarf2_cu cu;
5197 const gdb_byte *begin_info_ptr, *info_ptr;
5198 struct die_reader_specs reader;
5199 struct cleanup *cleanups;
5200 struct die_info *comp_unit_die;
5201 int has_children;
5202
5203 if (dwarf2_die_debug)
5204 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5205 this_cu->is_debug_types ? "type" : "comp",
5206 this_cu->offset.sect_off);
5207
5208 gdb_assert (this_cu->cu == NULL);
5209
5210 /* This is cheap if the section is already read in. */
5211 dwarf2_read_section (objfile, section);
5212
5213 init_one_comp_unit (&cu, this_cu);
5214
5215 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5216
5217 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
5218 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5219 abbrev_section, info_ptr,
5220 this_cu->is_debug_types);
5221
5222 this_cu->length = get_cu_length (&cu.header);
5223
5224 /* Skip dummy compilation units. */
5225 if (info_ptr >= begin_info_ptr + this_cu->length
5226 || peek_abbrev_code (abfd, info_ptr) == 0)
5227 {
5228 do_cleanups (cleanups);
5229 return;
5230 }
5231
5232 dwarf2_read_abbrevs (&cu, abbrev_section);
5233 make_cleanup (dwarf2_free_abbrev_table, &cu);
5234
5235 init_cu_die_reader (&reader, &cu, section, dwo_file);
5236 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5237
5238 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5239
5240 do_cleanups (cleanups);
5241 }
5242
5243 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5244 does not lookup the specified DWO file.
5245 This cannot be used to read DWO files.
5246
5247 THIS_CU->cu is always freed when done.
5248 This is done in order to not leave THIS_CU->cu in a state where we have
5249 to care whether it refers to the "main" CU or the DWO CU.
5250 We can revisit this if the data shows there's a performance issue. */
5251
5252 static void
5253 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5254 die_reader_func_ftype *die_reader_func,
5255 void *data)
5256 {
5257 init_cutu_and_read_dies_no_follow (this_cu,
5258 get_abbrev_section_for_cu (this_cu),
5259 NULL,
5260 die_reader_func, data);
5261 }
5262 \f
5263 /* Type Unit Groups.
5264
5265 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5266 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5267 so that all types coming from the same compilation (.o file) are grouped
5268 together. A future step could be to put the types in the same symtab as
5269 the CU the types ultimately came from. */
5270
5271 static hashval_t
5272 hash_type_unit_group (const void *item)
5273 {
5274 const struct type_unit_group *tu_group = item;
5275
5276 return hash_stmt_list_entry (&tu_group->hash);
5277 }
5278
5279 static int
5280 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
5281 {
5282 const struct type_unit_group *lhs = item_lhs;
5283 const struct type_unit_group *rhs = item_rhs;
5284
5285 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
5286 }
5287
5288 /* Allocate a hash table for type unit groups. */
5289
5290 static htab_t
5291 allocate_type_unit_groups_table (void)
5292 {
5293 return htab_create_alloc_ex (3,
5294 hash_type_unit_group,
5295 eq_type_unit_group,
5296 NULL,
5297 &dwarf2_per_objfile->objfile->objfile_obstack,
5298 hashtab_obstack_allocate,
5299 dummy_obstack_deallocate);
5300 }
5301
5302 /* Type units that don't have DW_AT_stmt_list are grouped into their own
5303 partial symtabs. We combine several TUs per psymtab to not let the size
5304 of any one psymtab grow too big. */
5305 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5306 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
5307
5308 /* Helper routine for get_type_unit_group.
5309 Create the type_unit_group object used to hold one or more TUs. */
5310
5311 static struct type_unit_group *
5312 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
5313 {
5314 struct objfile *objfile = dwarf2_per_objfile->objfile;
5315 struct dwarf2_per_cu_data *per_cu;
5316 struct type_unit_group *tu_group;
5317
5318 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5319 struct type_unit_group);
5320 per_cu = &tu_group->per_cu;
5321 per_cu->objfile = objfile;
5322
5323 if (dwarf2_per_objfile->using_index)
5324 {
5325 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5326 struct dwarf2_per_cu_quick_data);
5327 }
5328 else
5329 {
5330 unsigned int line_offset = line_offset_struct.sect_off;
5331 struct partial_symtab *pst;
5332 char *name;
5333
5334 /* Give the symtab a useful name for debug purposes. */
5335 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5336 name = xstrprintf ("<type_units_%d>",
5337 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5338 else
5339 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5340
5341 pst = create_partial_symtab (per_cu, name);
5342 pst->anonymous = 1;
5343
5344 xfree (name);
5345 }
5346
5347 tu_group->hash.dwo_unit = cu->dwo_unit;
5348 tu_group->hash.line_offset = line_offset_struct;
5349
5350 return tu_group;
5351 }
5352
5353 /* Look up the type_unit_group for type unit CU, and create it if necessary.
5354 STMT_LIST is a DW_AT_stmt_list attribute. */
5355
5356 static struct type_unit_group *
5357 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
5358 {
5359 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5360 struct type_unit_group *tu_group;
5361 void **slot;
5362 unsigned int line_offset;
5363 struct type_unit_group type_unit_group_for_lookup;
5364
5365 if (dwarf2_per_objfile->type_unit_groups == NULL)
5366 {
5367 dwarf2_per_objfile->type_unit_groups =
5368 allocate_type_unit_groups_table ();
5369 }
5370
5371 /* Do we need to create a new group, or can we use an existing one? */
5372
5373 if (stmt_list)
5374 {
5375 line_offset = DW_UNSND (stmt_list);
5376 ++tu_stats->nr_symtab_sharers;
5377 }
5378 else
5379 {
5380 /* Ugh, no stmt_list. Rare, but we have to handle it.
5381 We can do various things here like create one group per TU or
5382 spread them over multiple groups to split up the expansion work.
5383 To avoid worst case scenarios (too many groups or too large groups)
5384 we, umm, group them in bunches. */
5385 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5386 | (tu_stats->nr_stmt_less_type_units
5387 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5388 ++tu_stats->nr_stmt_less_type_units;
5389 }
5390
5391 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5392 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5393 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5394 &type_unit_group_for_lookup, INSERT);
5395 if (*slot != NULL)
5396 {
5397 tu_group = *slot;
5398 gdb_assert (tu_group != NULL);
5399 }
5400 else
5401 {
5402 sect_offset line_offset_struct;
5403
5404 line_offset_struct.sect_off = line_offset;
5405 tu_group = create_type_unit_group (cu, line_offset_struct);
5406 *slot = tu_group;
5407 ++tu_stats->nr_symtabs;
5408 }
5409
5410 return tu_group;
5411 }
5412
5413 /* Struct used to sort TUs by their abbreviation table offset. */
5414
5415 struct tu_abbrev_offset
5416 {
5417 struct signatured_type *sig_type;
5418 sect_offset abbrev_offset;
5419 };
5420
5421 /* Helper routine for build_type_unit_groups, passed to qsort. */
5422
5423 static int
5424 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5425 {
5426 const struct tu_abbrev_offset * const *a = ap;
5427 const struct tu_abbrev_offset * const *b = bp;
5428 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5429 unsigned int boff = (*b)->abbrev_offset.sect_off;
5430
5431 return (aoff > boff) - (aoff < boff);
5432 }
5433
5434 /* A helper function to add a type_unit_group to a table. */
5435
5436 static int
5437 add_type_unit_group_to_table (void **slot, void *datum)
5438 {
5439 struct type_unit_group *tu_group = *slot;
5440 struct type_unit_group ***datap = datum;
5441
5442 **datap = tu_group;
5443 ++*datap;
5444
5445 return 1;
5446 }
5447
5448 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
5449 each one passing FUNC,DATA.
5450
5451 The efficiency is because we sort TUs by the abbrev table they use and
5452 only read each abbrev table once. In one program there are 200K TUs
5453 sharing 8K abbrev tables.
5454
5455 The main purpose of this function is to support building the
5456 dwarf2_per_objfile->type_unit_groups table.
5457 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5458 can collapse the search space by grouping them by stmt_list.
5459 The savings can be significant, in the same program from above the 200K TUs
5460 share 8K stmt_list tables.
5461
5462 FUNC is expected to call get_type_unit_group, which will create the
5463 struct type_unit_group if necessary and add it to
5464 dwarf2_per_objfile->type_unit_groups. */
5465
5466 static void
5467 build_type_unit_groups (die_reader_func_ftype *func, void *data)
5468 {
5469 struct objfile *objfile = dwarf2_per_objfile->objfile;
5470 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5471 struct cleanup *cleanups;
5472 struct abbrev_table *abbrev_table;
5473 sect_offset abbrev_offset;
5474 struct tu_abbrev_offset *sorted_by_abbrev;
5475 struct type_unit_group **iter;
5476 int i;
5477
5478 /* It's up to the caller to not call us multiple times. */
5479 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5480
5481 if (dwarf2_per_objfile->n_type_units == 0)
5482 return;
5483
5484 /* TUs typically share abbrev tables, and there can be way more TUs than
5485 abbrev tables. Sort by abbrev table to reduce the number of times we
5486 read each abbrev table in.
5487 Alternatives are to punt or to maintain a cache of abbrev tables.
5488 This is simpler and efficient enough for now.
5489
5490 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5491 symtab to use). Typically TUs with the same abbrev offset have the same
5492 stmt_list value too so in practice this should work well.
5493
5494 The basic algorithm here is:
5495
5496 sort TUs by abbrev table
5497 for each TU with same abbrev table:
5498 read abbrev table if first user
5499 read TU top level DIE
5500 [IWBN if DWO skeletons had DW_AT_stmt_list]
5501 call FUNC */
5502
5503 if (dwarf2_read_debug)
5504 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5505
5506 /* Sort in a separate table to maintain the order of all_type_units
5507 for .gdb_index: TU indices directly index all_type_units. */
5508 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5509 dwarf2_per_objfile->n_type_units);
5510 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5511 {
5512 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5513
5514 sorted_by_abbrev[i].sig_type = sig_type;
5515 sorted_by_abbrev[i].abbrev_offset =
5516 read_abbrev_offset (sig_type->per_cu.section,
5517 sig_type->per_cu.offset);
5518 }
5519 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5520 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5521 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5522
5523 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5524 called any number of times, so we don't reset tu_stats here. */
5525
5526 abbrev_offset.sect_off = ~(unsigned) 0;
5527 abbrev_table = NULL;
5528 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5529
5530 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5531 {
5532 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5533
5534 /* Switch to the next abbrev table if necessary. */
5535 if (abbrev_table == NULL
5536 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5537 {
5538 if (abbrev_table != NULL)
5539 {
5540 abbrev_table_free (abbrev_table);
5541 /* Reset to NULL in case abbrev_table_read_table throws
5542 an error: abbrev_table_free_cleanup will get called. */
5543 abbrev_table = NULL;
5544 }
5545 abbrev_offset = tu->abbrev_offset;
5546 abbrev_table =
5547 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5548 abbrev_offset);
5549 ++tu_stats->nr_uniq_abbrev_tables;
5550 }
5551
5552 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5553 func, data);
5554 }
5555
5556 /* type_unit_groups can be NULL if there is an error in the debug info.
5557 Just create an empty table so the rest of gdb doesn't have to watch
5558 for this error case. */
5559 if (dwarf2_per_objfile->type_unit_groups == NULL)
5560 {
5561 dwarf2_per_objfile->type_unit_groups =
5562 allocate_type_unit_groups_table ();
5563 dwarf2_per_objfile->n_type_unit_groups = 0;
5564 }
5565
5566 /* Create a vector of pointers to primary type units to make it easy to
5567 iterate over them and CUs. See dw2_get_primary_cu. */
5568 dwarf2_per_objfile->n_type_unit_groups =
5569 htab_elements (dwarf2_per_objfile->type_unit_groups);
5570 dwarf2_per_objfile->all_type_unit_groups =
5571 obstack_alloc (&objfile->objfile_obstack,
5572 dwarf2_per_objfile->n_type_unit_groups
5573 * sizeof (struct type_unit_group *));
5574 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5575 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5576 add_type_unit_group_to_table, &iter);
5577 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5578 == dwarf2_per_objfile->n_type_unit_groups);
5579
5580 do_cleanups (cleanups);
5581
5582 if (dwarf2_read_debug)
5583 {
5584 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5585 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5586 dwarf2_per_objfile->n_type_units);
5587 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5588 tu_stats->nr_uniq_abbrev_tables);
5589 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5590 tu_stats->nr_symtabs);
5591 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5592 tu_stats->nr_symtab_sharers);
5593 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5594 tu_stats->nr_stmt_less_type_units);
5595 }
5596 }
5597 \f
5598 /* Partial symbol tables. */
5599
5600 /* Create a psymtab named NAME and assign it to PER_CU.
5601
5602 The caller must fill in the following details:
5603 dirname, textlow, texthigh. */
5604
5605 static struct partial_symtab *
5606 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5607 {
5608 struct objfile *objfile = per_cu->objfile;
5609 struct partial_symtab *pst;
5610
5611 pst = start_psymtab_common (objfile, objfile->section_offsets,
5612 name, 0,
5613 objfile->global_psymbols.next,
5614 objfile->static_psymbols.next);
5615
5616 pst->psymtabs_addrmap_supported = 1;
5617
5618 /* This is the glue that links PST into GDB's symbol API. */
5619 pst->read_symtab_private = per_cu;
5620 pst->read_symtab = dwarf2_read_symtab;
5621 per_cu->v.psymtab = pst;
5622
5623 return pst;
5624 }
5625
5626 /* The DATA object passed to process_psymtab_comp_unit_reader has this
5627 type. */
5628
5629 struct process_psymtab_comp_unit_data
5630 {
5631 /* True if we are reading a DW_TAG_partial_unit. */
5632
5633 int want_partial_unit;
5634
5635 /* The "pretend" language that is used if the CU doesn't declare a
5636 language. */
5637
5638 enum language pretend_language;
5639 };
5640
5641 /* die_reader_func for process_psymtab_comp_unit. */
5642
5643 static void
5644 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
5645 const gdb_byte *info_ptr,
5646 struct die_info *comp_unit_die,
5647 int has_children,
5648 void *data)
5649 {
5650 struct dwarf2_cu *cu = reader->cu;
5651 struct objfile *objfile = cu->objfile;
5652 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5653 struct attribute *attr;
5654 CORE_ADDR baseaddr;
5655 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5656 struct partial_symtab *pst;
5657 int has_pc_info;
5658 const char *filename;
5659 struct process_psymtab_comp_unit_data *info = data;
5660
5661 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
5662 return;
5663
5664 gdb_assert (! per_cu->is_debug_types);
5665
5666 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
5667
5668 cu->list_in_scope = &file_symbols;
5669
5670 /* Allocate a new partial symbol table structure. */
5671 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5672 if (attr == NULL || !DW_STRING (attr))
5673 filename = "";
5674 else
5675 filename = DW_STRING (attr);
5676
5677 pst = create_partial_symtab (per_cu, filename);
5678
5679 /* This must be done before calling dwarf2_build_include_psymtabs. */
5680 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5681 if (attr != NULL)
5682 pst->dirname = DW_STRING (attr);
5683
5684 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5685
5686 dwarf2_find_base_address (comp_unit_die, cu);
5687
5688 /* Possibly set the default values of LOWPC and HIGHPC from
5689 `DW_AT_ranges'. */
5690 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5691 &best_highpc, cu, pst);
5692 if (has_pc_info == 1 && best_lowpc < best_highpc)
5693 /* Store the contiguous range if it is not empty; it can be empty for
5694 CUs with no code. */
5695 addrmap_set_empty (objfile->psymtabs_addrmap,
5696 best_lowpc + baseaddr,
5697 best_highpc + baseaddr - 1, pst);
5698
5699 /* Check if comp unit has_children.
5700 If so, read the rest of the partial symbols from this comp unit.
5701 If not, there's no more debug_info for this comp unit. */
5702 if (has_children)
5703 {
5704 struct partial_die_info *first_die;
5705 CORE_ADDR lowpc, highpc;
5706
5707 lowpc = ((CORE_ADDR) -1);
5708 highpc = ((CORE_ADDR) 0);
5709
5710 first_die = load_partial_dies (reader, info_ptr, 1);
5711
5712 scan_partial_symbols (first_die, &lowpc, &highpc,
5713 ! has_pc_info, cu);
5714
5715 /* If we didn't find a lowpc, set it to highpc to avoid
5716 complaints from `maint check'. */
5717 if (lowpc == ((CORE_ADDR) -1))
5718 lowpc = highpc;
5719
5720 /* If the compilation unit didn't have an explicit address range,
5721 then use the information extracted from its child dies. */
5722 if (! has_pc_info)
5723 {
5724 best_lowpc = lowpc;
5725 best_highpc = highpc;
5726 }
5727 }
5728 pst->textlow = best_lowpc + baseaddr;
5729 pst->texthigh = best_highpc + baseaddr;
5730
5731 pst->n_global_syms = objfile->global_psymbols.next -
5732 (objfile->global_psymbols.list + pst->globals_offset);
5733 pst->n_static_syms = objfile->static_psymbols.next -
5734 (objfile->static_psymbols.list + pst->statics_offset);
5735 sort_pst_symbols (objfile, pst);
5736
5737 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5738 {
5739 int i;
5740 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5741 struct dwarf2_per_cu_data *iter;
5742
5743 /* Fill in 'dependencies' here; we fill in 'users' in a
5744 post-pass. */
5745 pst->number_of_dependencies = len;
5746 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5747 len * sizeof (struct symtab *));
5748 for (i = 0;
5749 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5750 i, iter);
5751 ++i)
5752 pst->dependencies[i] = iter->v.psymtab;
5753
5754 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5755 }
5756
5757 /* Get the list of files included in the current compilation unit,
5758 and build a psymtab for each of them. */
5759 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5760
5761 if (dwarf2_read_debug)
5762 {
5763 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5764
5765 fprintf_unfiltered (gdb_stdlog,
5766 "Psymtab for %s unit @0x%x: %s - %s"
5767 ", %d global, %d static syms\n",
5768 per_cu->is_debug_types ? "type" : "comp",
5769 per_cu->offset.sect_off,
5770 paddress (gdbarch, pst->textlow),
5771 paddress (gdbarch, pst->texthigh),
5772 pst->n_global_syms, pst->n_static_syms);
5773 }
5774 }
5775
5776 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5777 Process compilation unit THIS_CU for a psymtab. */
5778
5779 static void
5780 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5781 int want_partial_unit,
5782 enum language pretend_language)
5783 {
5784 struct process_psymtab_comp_unit_data info;
5785
5786 /* If this compilation unit was already read in, free the
5787 cached copy in order to read it in again. This is
5788 necessary because we skipped some symbols when we first
5789 read in the compilation unit (see load_partial_dies).
5790 This problem could be avoided, but the benefit is unclear. */
5791 if (this_cu->cu != NULL)
5792 free_one_cached_comp_unit (this_cu);
5793
5794 gdb_assert (! this_cu->is_debug_types);
5795 info.want_partial_unit = want_partial_unit;
5796 info.pretend_language = pretend_language;
5797 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5798 process_psymtab_comp_unit_reader,
5799 &info);
5800
5801 /* Age out any secondary CUs. */
5802 age_cached_comp_units ();
5803 }
5804
5805 /* Reader function for build_type_psymtabs. */
5806
5807 static void
5808 build_type_psymtabs_reader (const struct die_reader_specs *reader,
5809 const gdb_byte *info_ptr,
5810 struct die_info *type_unit_die,
5811 int has_children,
5812 void *data)
5813 {
5814 struct objfile *objfile = dwarf2_per_objfile->objfile;
5815 struct dwarf2_cu *cu = reader->cu;
5816 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5817 struct signatured_type *sig_type;
5818 struct type_unit_group *tu_group;
5819 struct attribute *attr;
5820 struct partial_die_info *first_die;
5821 CORE_ADDR lowpc, highpc;
5822 struct partial_symtab *pst;
5823
5824 gdb_assert (data == NULL);
5825 gdb_assert (per_cu->is_debug_types);
5826 sig_type = (struct signatured_type *) per_cu;
5827
5828 if (! has_children)
5829 return;
5830
5831 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
5832 tu_group = get_type_unit_group (cu, attr);
5833
5834 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
5835
5836 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5837 cu->list_in_scope = &file_symbols;
5838 pst = create_partial_symtab (per_cu, "");
5839 pst->anonymous = 1;
5840
5841 first_die = load_partial_dies (reader, info_ptr, 1);
5842
5843 lowpc = (CORE_ADDR) -1;
5844 highpc = (CORE_ADDR) 0;
5845 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5846
5847 pst->n_global_syms = objfile->global_psymbols.next -
5848 (objfile->global_psymbols.list + pst->globals_offset);
5849 pst->n_static_syms = objfile->static_psymbols.next -
5850 (objfile->static_psymbols.list + pst->statics_offset);
5851 sort_pst_symbols (objfile, pst);
5852 }
5853
5854 /* Traversal function for build_type_psymtabs. */
5855
5856 static int
5857 build_type_psymtab_dependencies (void **slot, void *info)
5858 {
5859 struct objfile *objfile = dwarf2_per_objfile->objfile;
5860 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5861 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5862 struct partial_symtab *pst = per_cu->v.psymtab;
5863 int len = VEC_length (sig_type_ptr, tu_group->tus);
5864 struct signatured_type *iter;
5865 int i;
5866
5867 gdb_assert (len > 0);
5868 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
5869
5870 pst->number_of_dependencies = len;
5871 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5872 len * sizeof (struct psymtab *));
5873 for (i = 0;
5874 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
5875 ++i)
5876 {
5877 gdb_assert (iter->per_cu.is_debug_types);
5878 pst->dependencies[i] = iter->per_cu.v.psymtab;
5879 iter->type_unit_group = tu_group;
5880 }
5881
5882 VEC_free (sig_type_ptr, tu_group->tus);
5883
5884 return 1;
5885 }
5886
5887 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5888 Build partial symbol tables for the .debug_types comp-units. */
5889
5890 static void
5891 build_type_psymtabs (struct objfile *objfile)
5892 {
5893 if (! create_all_type_units (objfile))
5894 return;
5895
5896 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5897
5898 /* Now that all TUs have been processed we can fill in the dependencies. */
5899 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5900 build_type_psymtab_dependencies, NULL);
5901 }
5902
5903 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
5904
5905 static void
5906 psymtabs_addrmap_cleanup (void *o)
5907 {
5908 struct objfile *objfile = o;
5909
5910 objfile->psymtabs_addrmap = NULL;
5911 }
5912
5913 /* Compute the 'user' field for each psymtab in OBJFILE. */
5914
5915 static void
5916 set_partial_user (struct objfile *objfile)
5917 {
5918 int i;
5919
5920 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5921 {
5922 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5923 struct partial_symtab *pst = per_cu->v.psymtab;
5924 int j;
5925
5926 if (pst == NULL)
5927 continue;
5928
5929 for (j = 0; j < pst->number_of_dependencies; ++j)
5930 {
5931 /* Set the 'user' field only if it is not already set. */
5932 if (pst->dependencies[j]->user == NULL)
5933 pst->dependencies[j]->user = pst;
5934 }
5935 }
5936 }
5937
5938 /* Build the partial symbol table by doing a quick pass through the
5939 .debug_info and .debug_abbrev sections. */
5940
5941 static void
5942 dwarf2_build_psymtabs_hard (struct objfile *objfile)
5943 {
5944 struct cleanup *back_to, *addrmap_cleanup;
5945 struct obstack temp_obstack;
5946 int i;
5947
5948 if (dwarf2_read_debug)
5949 {
5950 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5951 objfile->name);
5952 }
5953
5954 dwarf2_per_objfile->reading_partial_symbols = 1;
5955
5956 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5957
5958 /* Any cached compilation units will be linked by the per-objfile
5959 read_in_chain. Make sure to free them when we're done. */
5960 back_to = make_cleanup (free_cached_comp_units, NULL);
5961
5962 build_type_psymtabs (objfile);
5963
5964 create_all_comp_units (objfile);
5965
5966 /* Create a temporary address map on a temporary obstack. We later
5967 copy this to the final obstack. */
5968 obstack_init (&temp_obstack);
5969 make_cleanup_obstack_free (&temp_obstack);
5970 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5971 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
5972
5973 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5974 {
5975 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5976
5977 process_psymtab_comp_unit (per_cu, 0, language_minimal);
5978 }
5979
5980 set_partial_user (objfile);
5981
5982 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5983 &objfile->objfile_obstack);
5984 discard_cleanups (addrmap_cleanup);
5985
5986 do_cleanups (back_to);
5987
5988 if (dwarf2_read_debug)
5989 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5990 objfile->name);
5991 }
5992
5993 /* die_reader_func for load_partial_comp_unit. */
5994
5995 static void
5996 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5997 const gdb_byte *info_ptr,
5998 struct die_info *comp_unit_die,
5999 int has_children,
6000 void *data)
6001 {
6002 struct dwarf2_cu *cu = reader->cu;
6003
6004 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
6005
6006 /* Check if comp unit has_children.
6007 If so, read the rest of the partial symbols from this comp unit.
6008 If not, there's no more debug_info for this comp unit. */
6009 if (has_children)
6010 load_partial_dies (reader, info_ptr, 0);
6011 }
6012
6013 /* Load the partial DIEs for a secondary CU into memory.
6014 This is also used when rereading a primary CU with load_all_dies. */
6015
6016 static void
6017 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6018 {
6019 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6020 load_partial_comp_unit_reader, NULL);
6021 }
6022
6023 static void
6024 read_comp_units_from_section (struct objfile *objfile,
6025 struct dwarf2_section_info *section,
6026 unsigned int is_dwz,
6027 int *n_allocated,
6028 int *n_comp_units,
6029 struct dwarf2_per_cu_data ***all_comp_units)
6030 {
6031 const gdb_byte *info_ptr;
6032 bfd *abfd = section->asection->owner;
6033
6034 if (dwarf2_read_debug)
6035 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
6036 section->asection->name, bfd_get_filename (abfd));
6037
6038 dwarf2_read_section (objfile, section);
6039
6040 info_ptr = section->buffer;
6041
6042 while (info_ptr < section->buffer + section->size)
6043 {
6044 unsigned int length, initial_length_size;
6045 struct dwarf2_per_cu_data *this_cu;
6046 sect_offset offset;
6047
6048 offset.sect_off = info_ptr - section->buffer;
6049
6050 /* Read just enough information to find out where the next
6051 compilation unit is. */
6052 length = read_initial_length (abfd, info_ptr, &initial_length_size);
6053
6054 /* Save the compilation unit for later lookup. */
6055 this_cu = obstack_alloc (&objfile->objfile_obstack,
6056 sizeof (struct dwarf2_per_cu_data));
6057 memset (this_cu, 0, sizeof (*this_cu));
6058 this_cu->offset = offset;
6059 this_cu->length = length + initial_length_size;
6060 this_cu->is_dwz = is_dwz;
6061 this_cu->objfile = objfile;
6062 this_cu->section = section;
6063
6064 if (*n_comp_units == *n_allocated)
6065 {
6066 *n_allocated *= 2;
6067 *all_comp_units = xrealloc (*all_comp_units,
6068 *n_allocated
6069 * sizeof (struct dwarf2_per_cu_data *));
6070 }
6071 (*all_comp_units)[*n_comp_units] = this_cu;
6072 ++*n_comp_units;
6073
6074 info_ptr = info_ptr + this_cu->length;
6075 }
6076 }
6077
6078 /* Create a list of all compilation units in OBJFILE.
6079 This is only done for -readnow and building partial symtabs. */
6080
6081 static void
6082 create_all_comp_units (struct objfile *objfile)
6083 {
6084 int n_allocated;
6085 int n_comp_units;
6086 struct dwarf2_per_cu_data **all_comp_units;
6087 struct dwz_file *dwz;
6088
6089 n_comp_units = 0;
6090 n_allocated = 10;
6091 all_comp_units = xmalloc (n_allocated
6092 * sizeof (struct dwarf2_per_cu_data *));
6093
6094 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6095 &n_allocated, &n_comp_units, &all_comp_units);
6096
6097 dwz = dwarf2_get_dwz_file ();
6098 if (dwz != NULL)
6099 read_comp_units_from_section (objfile, &dwz->info, 1,
6100 &n_allocated, &n_comp_units,
6101 &all_comp_units);
6102
6103 dwarf2_per_objfile->all_comp_units
6104 = obstack_alloc (&objfile->objfile_obstack,
6105 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6106 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6107 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6108 xfree (all_comp_units);
6109 dwarf2_per_objfile->n_comp_units = n_comp_units;
6110 }
6111
6112 /* Process all loaded DIEs for compilation unit CU, starting at
6113 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
6114 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
6115 DW_AT_ranges). If NEED_PC is set, then this function will set
6116 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
6117 and record the covered ranges in the addrmap. */
6118
6119 static void
6120 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
6121 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6122 {
6123 struct partial_die_info *pdi;
6124
6125 /* Now, march along the PDI's, descending into ones which have
6126 interesting children but skipping the children of the other ones,
6127 until we reach the end of the compilation unit. */
6128
6129 pdi = first_die;
6130
6131 while (pdi != NULL)
6132 {
6133 fixup_partial_die (pdi, cu);
6134
6135 /* Anonymous namespaces or modules have no name but have interesting
6136 children, so we need to look at them. Ditto for anonymous
6137 enums. */
6138
6139 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
6140 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6141 || pdi->tag == DW_TAG_imported_unit)
6142 {
6143 switch (pdi->tag)
6144 {
6145 case DW_TAG_subprogram:
6146 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6147 break;
6148 case DW_TAG_constant:
6149 case DW_TAG_variable:
6150 case DW_TAG_typedef:
6151 case DW_TAG_union_type:
6152 if (!pdi->is_declaration)
6153 {
6154 add_partial_symbol (pdi, cu);
6155 }
6156 break;
6157 case DW_TAG_class_type:
6158 case DW_TAG_interface_type:
6159 case DW_TAG_structure_type:
6160 if (!pdi->is_declaration)
6161 {
6162 add_partial_symbol (pdi, cu);
6163 }
6164 break;
6165 case DW_TAG_enumeration_type:
6166 if (!pdi->is_declaration)
6167 add_partial_enumeration (pdi, cu);
6168 break;
6169 case DW_TAG_base_type:
6170 case DW_TAG_subrange_type:
6171 /* File scope base type definitions are added to the partial
6172 symbol table. */
6173 add_partial_symbol (pdi, cu);
6174 break;
6175 case DW_TAG_namespace:
6176 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
6177 break;
6178 case DW_TAG_module:
6179 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
6180 break;
6181 case DW_TAG_imported_unit:
6182 {
6183 struct dwarf2_per_cu_data *per_cu;
6184
6185 /* For now we don't handle imported units in type units. */
6186 if (cu->per_cu->is_debug_types)
6187 {
6188 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6189 " supported in type units [in module %s]"),
6190 cu->objfile->name);
6191 }
6192
6193 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
6194 pdi->is_dwz,
6195 cu->objfile);
6196
6197 /* Go read the partial unit, if needed. */
6198 if (per_cu->v.psymtab == NULL)
6199 process_psymtab_comp_unit (per_cu, 1, cu->language);
6200
6201 VEC_safe_push (dwarf2_per_cu_ptr,
6202 cu->per_cu->imported_symtabs, per_cu);
6203 }
6204 break;
6205 default:
6206 break;
6207 }
6208 }
6209
6210 /* If the die has a sibling, skip to the sibling. */
6211
6212 pdi = pdi->die_sibling;
6213 }
6214 }
6215
6216 /* Functions used to compute the fully scoped name of a partial DIE.
6217
6218 Normally, this is simple. For C++, the parent DIE's fully scoped
6219 name is concatenated with "::" and the partial DIE's name. For
6220 Java, the same thing occurs except that "." is used instead of "::".
6221 Enumerators are an exception; they use the scope of their parent
6222 enumeration type, i.e. the name of the enumeration type is not
6223 prepended to the enumerator.
6224
6225 There are two complexities. One is DW_AT_specification; in this
6226 case "parent" means the parent of the target of the specification,
6227 instead of the direct parent of the DIE. The other is compilers
6228 which do not emit DW_TAG_namespace; in this case we try to guess
6229 the fully qualified name of structure types from their members'
6230 linkage names. This must be done using the DIE's children rather
6231 than the children of any DW_AT_specification target. We only need
6232 to do this for structures at the top level, i.e. if the target of
6233 any DW_AT_specification (if any; otherwise the DIE itself) does not
6234 have a parent. */
6235
6236 /* Compute the scope prefix associated with PDI's parent, in
6237 compilation unit CU. The result will be allocated on CU's
6238 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6239 field. NULL is returned if no prefix is necessary. */
6240 static const char *
6241 partial_die_parent_scope (struct partial_die_info *pdi,
6242 struct dwarf2_cu *cu)
6243 {
6244 const char *grandparent_scope;
6245 struct partial_die_info *parent, *real_pdi;
6246
6247 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6248 then this means the parent of the specification DIE. */
6249
6250 real_pdi = pdi;
6251 while (real_pdi->has_specification)
6252 real_pdi = find_partial_die (real_pdi->spec_offset,
6253 real_pdi->spec_is_dwz, cu);
6254
6255 parent = real_pdi->die_parent;
6256 if (parent == NULL)
6257 return NULL;
6258
6259 if (parent->scope_set)
6260 return parent->scope;
6261
6262 fixup_partial_die (parent, cu);
6263
6264 grandparent_scope = partial_die_parent_scope (parent, cu);
6265
6266 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6267 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6268 Work around this problem here. */
6269 if (cu->language == language_cplus
6270 && parent->tag == DW_TAG_namespace
6271 && strcmp (parent->name, "::") == 0
6272 && grandparent_scope == NULL)
6273 {
6274 parent->scope = NULL;
6275 parent->scope_set = 1;
6276 return NULL;
6277 }
6278
6279 if (pdi->tag == DW_TAG_enumerator)
6280 /* Enumerators should not get the name of the enumeration as a prefix. */
6281 parent->scope = grandparent_scope;
6282 else if (parent->tag == DW_TAG_namespace
6283 || parent->tag == DW_TAG_module
6284 || parent->tag == DW_TAG_structure_type
6285 || parent->tag == DW_TAG_class_type
6286 || parent->tag == DW_TAG_interface_type
6287 || parent->tag == DW_TAG_union_type
6288 || parent->tag == DW_TAG_enumeration_type)
6289 {
6290 if (grandparent_scope == NULL)
6291 parent->scope = parent->name;
6292 else
6293 parent->scope = typename_concat (&cu->comp_unit_obstack,
6294 grandparent_scope,
6295 parent->name, 0, cu);
6296 }
6297 else
6298 {
6299 /* FIXME drow/2004-04-01: What should we be doing with
6300 function-local names? For partial symbols, we should probably be
6301 ignoring them. */
6302 complaint (&symfile_complaints,
6303 _("unhandled containing DIE tag %d for DIE at %d"),
6304 parent->tag, pdi->offset.sect_off);
6305 parent->scope = grandparent_scope;
6306 }
6307
6308 parent->scope_set = 1;
6309 return parent->scope;
6310 }
6311
6312 /* Return the fully scoped name associated with PDI, from compilation unit
6313 CU. The result will be allocated with malloc. */
6314
6315 static char *
6316 partial_die_full_name (struct partial_die_info *pdi,
6317 struct dwarf2_cu *cu)
6318 {
6319 const char *parent_scope;
6320
6321 /* If this is a template instantiation, we can not work out the
6322 template arguments from partial DIEs. So, unfortunately, we have
6323 to go through the full DIEs. At least any work we do building
6324 types here will be reused if full symbols are loaded later. */
6325 if (pdi->has_template_arguments)
6326 {
6327 fixup_partial_die (pdi, cu);
6328
6329 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6330 {
6331 struct die_info *die;
6332 struct attribute attr;
6333 struct dwarf2_cu *ref_cu = cu;
6334
6335 /* DW_FORM_ref_addr is using section offset. */
6336 attr.name = 0;
6337 attr.form = DW_FORM_ref_addr;
6338 attr.u.unsnd = pdi->offset.sect_off;
6339 die = follow_die_ref (NULL, &attr, &ref_cu);
6340
6341 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6342 }
6343 }
6344
6345 parent_scope = partial_die_parent_scope (pdi, cu);
6346 if (parent_scope == NULL)
6347 return NULL;
6348 else
6349 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
6350 }
6351
6352 static void
6353 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
6354 {
6355 struct objfile *objfile = cu->objfile;
6356 CORE_ADDR addr = 0;
6357 const char *actual_name = NULL;
6358 CORE_ADDR baseaddr;
6359 char *built_actual_name;
6360
6361 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6362
6363 built_actual_name = partial_die_full_name (pdi, cu);
6364 if (built_actual_name != NULL)
6365 actual_name = built_actual_name;
6366
6367 if (actual_name == NULL)
6368 actual_name = pdi->name;
6369
6370 switch (pdi->tag)
6371 {
6372 case DW_TAG_subprogram:
6373 if (pdi->is_external || cu->language == language_ada)
6374 {
6375 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6376 of the global scope. But in Ada, we want to be able to access
6377 nested procedures globally. So all Ada subprograms are stored
6378 in the global scope. */
6379 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
6380 mst_text, objfile); */
6381 add_psymbol_to_list (actual_name, strlen (actual_name),
6382 built_actual_name != NULL,
6383 VAR_DOMAIN, LOC_BLOCK,
6384 &objfile->global_psymbols,
6385 0, pdi->lowpc + baseaddr,
6386 cu->language, objfile);
6387 }
6388 else
6389 {
6390 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
6391 mst_file_text, objfile); */
6392 add_psymbol_to_list (actual_name, strlen (actual_name),
6393 built_actual_name != NULL,
6394 VAR_DOMAIN, LOC_BLOCK,
6395 &objfile->static_psymbols,
6396 0, pdi->lowpc + baseaddr,
6397 cu->language, objfile);
6398 }
6399 break;
6400 case DW_TAG_constant:
6401 {
6402 struct psymbol_allocation_list *list;
6403
6404 if (pdi->is_external)
6405 list = &objfile->global_psymbols;
6406 else
6407 list = &objfile->static_psymbols;
6408 add_psymbol_to_list (actual_name, strlen (actual_name),
6409 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
6410 list, 0, 0, cu->language, objfile);
6411 }
6412 break;
6413 case DW_TAG_variable:
6414 if (pdi->d.locdesc)
6415 addr = decode_locdesc (pdi->d.locdesc, cu);
6416
6417 if (pdi->d.locdesc
6418 && addr == 0
6419 && !dwarf2_per_objfile->has_section_at_zero)
6420 {
6421 /* A global or static variable may also have been stripped
6422 out by the linker if unused, in which case its address
6423 will be nullified; do not add such variables into partial
6424 symbol table then. */
6425 }
6426 else if (pdi->is_external)
6427 {
6428 /* Global Variable.
6429 Don't enter into the minimal symbol tables as there is
6430 a minimal symbol table entry from the ELF symbols already.
6431 Enter into partial symbol table if it has a location
6432 descriptor or a type.
6433 If the location descriptor is missing, new_symbol will create
6434 a LOC_UNRESOLVED symbol, the address of the variable will then
6435 be determined from the minimal symbol table whenever the variable
6436 is referenced.
6437 The address for the partial symbol table entry is not
6438 used by GDB, but it comes in handy for debugging partial symbol
6439 table building. */
6440
6441 if (pdi->d.locdesc || pdi->has_type)
6442 add_psymbol_to_list (actual_name, strlen (actual_name),
6443 built_actual_name != NULL,
6444 VAR_DOMAIN, LOC_STATIC,
6445 &objfile->global_psymbols,
6446 0, addr + baseaddr,
6447 cu->language, objfile);
6448 }
6449 else
6450 {
6451 /* Static Variable. Skip symbols without location descriptors. */
6452 if (pdi->d.locdesc == NULL)
6453 {
6454 xfree (built_actual_name);
6455 return;
6456 }
6457 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
6458 mst_file_data, objfile); */
6459 add_psymbol_to_list (actual_name, strlen (actual_name),
6460 built_actual_name != NULL,
6461 VAR_DOMAIN, LOC_STATIC,
6462 &objfile->static_psymbols,
6463 0, addr + baseaddr,
6464 cu->language, objfile);
6465 }
6466 break;
6467 case DW_TAG_typedef:
6468 case DW_TAG_base_type:
6469 case DW_TAG_subrange_type:
6470 add_psymbol_to_list (actual_name, strlen (actual_name),
6471 built_actual_name != NULL,
6472 VAR_DOMAIN, LOC_TYPEDEF,
6473 &objfile->static_psymbols,
6474 0, (CORE_ADDR) 0, cu->language, objfile);
6475 break;
6476 case DW_TAG_namespace:
6477 add_psymbol_to_list (actual_name, strlen (actual_name),
6478 built_actual_name != NULL,
6479 VAR_DOMAIN, LOC_TYPEDEF,
6480 &objfile->global_psymbols,
6481 0, (CORE_ADDR) 0, cu->language, objfile);
6482 break;
6483 case DW_TAG_class_type:
6484 case DW_TAG_interface_type:
6485 case DW_TAG_structure_type:
6486 case DW_TAG_union_type:
6487 case DW_TAG_enumeration_type:
6488 /* Skip external references. The DWARF standard says in the section
6489 about "Structure, Union, and Class Type Entries": "An incomplete
6490 structure, union or class type is represented by a structure,
6491 union or class entry that does not have a byte size attribute
6492 and that has a DW_AT_declaration attribute." */
6493 if (!pdi->has_byte_size && pdi->is_declaration)
6494 {
6495 xfree (built_actual_name);
6496 return;
6497 }
6498
6499 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6500 static vs. global. */
6501 add_psymbol_to_list (actual_name, strlen (actual_name),
6502 built_actual_name != NULL,
6503 STRUCT_DOMAIN, LOC_TYPEDEF,
6504 (cu->language == language_cplus
6505 || cu->language == language_java)
6506 ? &objfile->global_psymbols
6507 : &objfile->static_psymbols,
6508 0, (CORE_ADDR) 0, cu->language, objfile);
6509
6510 break;
6511 case DW_TAG_enumerator:
6512 add_psymbol_to_list (actual_name, strlen (actual_name),
6513 built_actual_name != NULL,
6514 VAR_DOMAIN, LOC_CONST,
6515 (cu->language == language_cplus
6516 || cu->language == language_java)
6517 ? &objfile->global_psymbols
6518 : &objfile->static_psymbols,
6519 0, (CORE_ADDR) 0, cu->language, objfile);
6520 break;
6521 default:
6522 break;
6523 }
6524
6525 xfree (built_actual_name);
6526 }
6527
6528 /* Read a partial die corresponding to a namespace; also, add a symbol
6529 corresponding to that namespace to the symbol table. NAMESPACE is
6530 the name of the enclosing namespace. */
6531
6532 static void
6533 add_partial_namespace (struct partial_die_info *pdi,
6534 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6535 int need_pc, struct dwarf2_cu *cu)
6536 {
6537 /* Add a symbol for the namespace. */
6538
6539 add_partial_symbol (pdi, cu);
6540
6541 /* Now scan partial symbols in that namespace. */
6542
6543 if (pdi->has_children)
6544 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6545 }
6546
6547 /* Read a partial die corresponding to a Fortran module. */
6548
6549 static void
6550 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6551 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6552 {
6553 /* Now scan partial symbols in that module. */
6554
6555 if (pdi->has_children)
6556 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6557 }
6558
6559 /* Read a partial die corresponding to a subprogram and create a partial
6560 symbol for that subprogram. When the CU language allows it, this
6561 routine also defines a partial symbol for each nested subprogram
6562 that this subprogram contains.
6563
6564 DIE my also be a lexical block, in which case we simply search
6565 recursively for suprograms defined inside that lexical block.
6566 Again, this is only performed when the CU language allows this
6567 type of definitions. */
6568
6569 static void
6570 add_partial_subprogram (struct partial_die_info *pdi,
6571 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6572 int need_pc, struct dwarf2_cu *cu)
6573 {
6574 if (pdi->tag == DW_TAG_subprogram)
6575 {
6576 if (pdi->has_pc_info)
6577 {
6578 if (pdi->lowpc < *lowpc)
6579 *lowpc = pdi->lowpc;
6580 if (pdi->highpc > *highpc)
6581 *highpc = pdi->highpc;
6582 if (need_pc)
6583 {
6584 CORE_ADDR baseaddr;
6585 struct objfile *objfile = cu->objfile;
6586
6587 baseaddr = ANOFFSET (objfile->section_offsets,
6588 SECT_OFF_TEXT (objfile));
6589 addrmap_set_empty (objfile->psymtabs_addrmap,
6590 pdi->lowpc + baseaddr,
6591 pdi->highpc - 1 + baseaddr,
6592 cu->per_cu->v.psymtab);
6593 }
6594 }
6595
6596 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6597 {
6598 if (!pdi->is_declaration)
6599 /* Ignore subprogram DIEs that do not have a name, they are
6600 illegal. Do not emit a complaint at this point, we will
6601 do so when we convert this psymtab into a symtab. */
6602 if (pdi->name)
6603 add_partial_symbol (pdi, cu);
6604 }
6605 }
6606
6607 if (! pdi->has_children)
6608 return;
6609
6610 if (cu->language == language_ada)
6611 {
6612 pdi = pdi->die_child;
6613 while (pdi != NULL)
6614 {
6615 fixup_partial_die (pdi, cu);
6616 if (pdi->tag == DW_TAG_subprogram
6617 || pdi->tag == DW_TAG_lexical_block)
6618 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6619 pdi = pdi->die_sibling;
6620 }
6621 }
6622 }
6623
6624 /* Read a partial die corresponding to an enumeration type. */
6625
6626 static void
6627 add_partial_enumeration (struct partial_die_info *enum_pdi,
6628 struct dwarf2_cu *cu)
6629 {
6630 struct partial_die_info *pdi;
6631
6632 if (enum_pdi->name != NULL)
6633 add_partial_symbol (enum_pdi, cu);
6634
6635 pdi = enum_pdi->die_child;
6636 while (pdi)
6637 {
6638 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
6639 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6640 else
6641 add_partial_symbol (pdi, cu);
6642 pdi = pdi->die_sibling;
6643 }
6644 }
6645
6646 /* Return the initial uleb128 in the die at INFO_PTR. */
6647
6648 static unsigned int
6649 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6650 {
6651 unsigned int bytes_read;
6652
6653 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6654 }
6655
6656 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6657 Return the corresponding abbrev, or NULL if the number is zero (indicating
6658 an empty DIE). In either case *BYTES_READ will be set to the length of
6659 the initial number. */
6660
6661 static struct abbrev_info *
6662 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
6663 struct dwarf2_cu *cu)
6664 {
6665 bfd *abfd = cu->objfile->obfd;
6666 unsigned int abbrev_number;
6667 struct abbrev_info *abbrev;
6668
6669 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6670
6671 if (abbrev_number == 0)
6672 return NULL;
6673
6674 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
6675 if (!abbrev)
6676 {
6677 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6678 abbrev_number, bfd_get_filename (abfd));
6679 }
6680
6681 return abbrev;
6682 }
6683
6684 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6685 Returns a pointer to the end of a series of DIEs, terminated by an empty
6686 DIE. Any children of the skipped DIEs will also be skipped. */
6687
6688 static const gdb_byte *
6689 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
6690 {
6691 struct dwarf2_cu *cu = reader->cu;
6692 struct abbrev_info *abbrev;
6693 unsigned int bytes_read;
6694
6695 while (1)
6696 {
6697 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6698 if (abbrev == NULL)
6699 return info_ptr + bytes_read;
6700 else
6701 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
6702 }
6703 }
6704
6705 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6706 INFO_PTR should point just after the initial uleb128 of a DIE, and the
6707 abbrev corresponding to that skipped uleb128 should be passed in
6708 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6709 children. */
6710
6711 static const gdb_byte *
6712 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
6713 struct abbrev_info *abbrev)
6714 {
6715 unsigned int bytes_read;
6716 struct attribute attr;
6717 bfd *abfd = reader->abfd;
6718 struct dwarf2_cu *cu = reader->cu;
6719 const gdb_byte *buffer = reader->buffer;
6720 const gdb_byte *buffer_end = reader->buffer_end;
6721 const gdb_byte *start_info_ptr = info_ptr;
6722 unsigned int form, i;
6723
6724 for (i = 0; i < abbrev->num_attrs; i++)
6725 {
6726 /* The only abbrev we care about is DW_AT_sibling. */
6727 if (abbrev->attrs[i].name == DW_AT_sibling)
6728 {
6729 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
6730 if (attr.form == DW_FORM_ref_addr)
6731 complaint (&symfile_complaints,
6732 _("ignoring absolute DW_AT_sibling"));
6733 else
6734 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
6735 }
6736
6737 /* If it isn't DW_AT_sibling, skip this attribute. */
6738 form = abbrev->attrs[i].form;
6739 skip_attribute:
6740 switch (form)
6741 {
6742 case DW_FORM_ref_addr:
6743 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6744 and later it is offset sized. */
6745 if (cu->header.version == 2)
6746 info_ptr += cu->header.addr_size;
6747 else
6748 info_ptr += cu->header.offset_size;
6749 break;
6750 case DW_FORM_GNU_ref_alt:
6751 info_ptr += cu->header.offset_size;
6752 break;
6753 case DW_FORM_addr:
6754 info_ptr += cu->header.addr_size;
6755 break;
6756 case DW_FORM_data1:
6757 case DW_FORM_ref1:
6758 case DW_FORM_flag:
6759 info_ptr += 1;
6760 break;
6761 case DW_FORM_flag_present:
6762 break;
6763 case DW_FORM_data2:
6764 case DW_FORM_ref2:
6765 info_ptr += 2;
6766 break;
6767 case DW_FORM_data4:
6768 case DW_FORM_ref4:
6769 info_ptr += 4;
6770 break;
6771 case DW_FORM_data8:
6772 case DW_FORM_ref8:
6773 case DW_FORM_ref_sig8:
6774 info_ptr += 8;
6775 break;
6776 case DW_FORM_string:
6777 read_direct_string (abfd, info_ptr, &bytes_read);
6778 info_ptr += bytes_read;
6779 break;
6780 case DW_FORM_sec_offset:
6781 case DW_FORM_strp:
6782 case DW_FORM_GNU_strp_alt:
6783 info_ptr += cu->header.offset_size;
6784 break;
6785 case DW_FORM_exprloc:
6786 case DW_FORM_block:
6787 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6788 info_ptr += bytes_read;
6789 break;
6790 case DW_FORM_block1:
6791 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6792 break;
6793 case DW_FORM_block2:
6794 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6795 break;
6796 case DW_FORM_block4:
6797 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6798 break;
6799 case DW_FORM_sdata:
6800 case DW_FORM_udata:
6801 case DW_FORM_ref_udata:
6802 case DW_FORM_GNU_addr_index:
6803 case DW_FORM_GNU_str_index:
6804 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
6805 break;
6806 case DW_FORM_indirect:
6807 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6808 info_ptr += bytes_read;
6809 /* We need to continue parsing from here, so just go back to
6810 the top. */
6811 goto skip_attribute;
6812
6813 default:
6814 error (_("Dwarf Error: Cannot handle %s "
6815 "in DWARF reader [in module %s]"),
6816 dwarf_form_name (form),
6817 bfd_get_filename (abfd));
6818 }
6819 }
6820
6821 if (abbrev->has_children)
6822 return skip_children (reader, info_ptr);
6823 else
6824 return info_ptr;
6825 }
6826
6827 /* Locate ORIG_PDI's sibling.
6828 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
6829
6830 static const gdb_byte *
6831 locate_pdi_sibling (const struct die_reader_specs *reader,
6832 struct partial_die_info *orig_pdi,
6833 const gdb_byte *info_ptr)
6834 {
6835 /* Do we know the sibling already? */
6836
6837 if (orig_pdi->sibling)
6838 return orig_pdi->sibling;
6839
6840 /* Are there any children to deal with? */
6841
6842 if (!orig_pdi->has_children)
6843 return info_ptr;
6844
6845 /* Skip the children the long way. */
6846
6847 return skip_children (reader, info_ptr);
6848 }
6849
6850 /* Expand this partial symbol table into a full symbol table. SELF is
6851 not NULL. */
6852
6853 static void
6854 dwarf2_read_symtab (struct partial_symtab *self,
6855 struct objfile *objfile)
6856 {
6857 if (self->readin)
6858 {
6859 warning (_("bug: psymtab for %s is already read in."),
6860 self->filename);
6861 }
6862 else
6863 {
6864 if (info_verbose)
6865 {
6866 printf_filtered (_("Reading in symbols for %s..."),
6867 self->filename);
6868 gdb_flush (gdb_stdout);
6869 }
6870
6871 /* Restore our global data. */
6872 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
6873
6874 /* If this psymtab is constructed from a debug-only objfile, the
6875 has_section_at_zero flag will not necessarily be correct. We
6876 can get the correct value for this flag by looking at the data
6877 associated with the (presumably stripped) associated objfile. */
6878 if (objfile->separate_debug_objfile_backlink)
6879 {
6880 struct dwarf2_per_objfile *dpo_backlink
6881 = objfile_data (objfile->separate_debug_objfile_backlink,
6882 dwarf2_objfile_data_key);
6883
6884 dwarf2_per_objfile->has_section_at_zero
6885 = dpo_backlink->has_section_at_zero;
6886 }
6887
6888 dwarf2_per_objfile->reading_partial_symbols = 0;
6889
6890 psymtab_to_symtab_1 (self);
6891
6892 /* Finish up the debug error message. */
6893 if (info_verbose)
6894 printf_filtered (_("done.\n"));
6895 }
6896
6897 process_cu_includes ();
6898 }
6899 \f
6900 /* Reading in full CUs. */
6901
6902 /* Add PER_CU to the queue. */
6903
6904 static void
6905 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6906 enum language pretend_language)
6907 {
6908 struct dwarf2_queue_item *item;
6909
6910 per_cu->queued = 1;
6911 item = xmalloc (sizeof (*item));
6912 item->per_cu = per_cu;
6913 item->pretend_language = pretend_language;
6914 item->next = NULL;
6915
6916 if (dwarf2_queue == NULL)
6917 dwarf2_queue = item;
6918 else
6919 dwarf2_queue_tail->next = item;
6920
6921 dwarf2_queue_tail = item;
6922 }
6923
6924 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6925 unit and add it to our queue.
6926 The result is non-zero if PER_CU was queued, otherwise the result is zero
6927 meaning either PER_CU is already queued or it is already loaded. */
6928
6929 static int
6930 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6931 struct dwarf2_per_cu_data *per_cu,
6932 enum language pretend_language)
6933 {
6934 /* We may arrive here during partial symbol reading, if we need full
6935 DIEs to process an unusual case (e.g. template arguments). Do
6936 not queue PER_CU, just tell our caller to load its DIEs. */
6937 if (dwarf2_per_objfile->reading_partial_symbols)
6938 {
6939 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6940 return 1;
6941 return 0;
6942 }
6943
6944 /* Mark the dependence relation so that we don't flush PER_CU
6945 too early. */
6946 dwarf2_add_dependence (this_cu, per_cu);
6947
6948 /* If it's already on the queue, we have nothing to do. */
6949 if (per_cu->queued)
6950 return 0;
6951
6952 /* If the compilation unit is already loaded, just mark it as
6953 used. */
6954 if (per_cu->cu != NULL)
6955 {
6956 per_cu->cu->last_used = 0;
6957 return 0;
6958 }
6959
6960 /* Add it to the queue. */
6961 queue_comp_unit (per_cu, pretend_language);
6962
6963 return 1;
6964 }
6965
6966 /* Process the queue. */
6967
6968 static void
6969 process_queue (void)
6970 {
6971 struct dwarf2_queue_item *item, *next_item;
6972
6973 if (dwarf2_read_debug)
6974 {
6975 fprintf_unfiltered (gdb_stdlog,
6976 "Expanding one or more symtabs of objfile %s ...\n",
6977 dwarf2_per_objfile->objfile->name);
6978 }
6979
6980 /* The queue starts out with one item, but following a DIE reference
6981 may load a new CU, adding it to the end of the queue. */
6982 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6983 {
6984 if (dwarf2_per_objfile->using_index
6985 ? !item->per_cu->v.quick->symtab
6986 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
6987 {
6988 struct dwarf2_per_cu_data *per_cu = item->per_cu;
6989 char buf[100];
6990
6991 if (per_cu->is_debug_types)
6992 {
6993 struct signatured_type *sig_type =
6994 (struct signatured_type *) per_cu;
6995
6996 sprintf (buf, "TU %s at offset 0x%x",
6997 hex_string (sig_type->signature), per_cu->offset.sect_off);
6998 }
6999 else
7000 sprintf (buf, "CU at offset 0x%x", per_cu->offset.sect_off);
7001
7002 if (dwarf2_read_debug)
7003 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
7004
7005 if (per_cu->is_debug_types)
7006 process_full_type_unit (per_cu, item->pretend_language);
7007 else
7008 process_full_comp_unit (per_cu, item->pretend_language);
7009
7010 if (dwarf2_read_debug)
7011 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
7012 }
7013
7014 item->per_cu->queued = 0;
7015 next_item = item->next;
7016 xfree (item);
7017 }
7018
7019 dwarf2_queue_tail = NULL;
7020
7021 if (dwarf2_read_debug)
7022 {
7023 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
7024 dwarf2_per_objfile->objfile->name);
7025 }
7026 }
7027
7028 /* Free all allocated queue entries. This function only releases anything if
7029 an error was thrown; if the queue was processed then it would have been
7030 freed as we went along. */
7031
7032 static void
7033 dwarf2_release_queue (void *dummy)
7034 {
7035 struct dwarf2_queue_item *item, *last;
7036
7037 item = dwarf2_queue;
7038 while (item)
7039 {
7040 /* Anything still marked queued is likely to be in an
7041 inconsistent state, so discard it. */
7042 if (item->per_cu->queued)
7043 {
7044 if (item->per_cu->cu != NULL)
7045 free_one_cached_comp_unit (item->per_cu);
7046 item->per_cu->queued = 0;
7047 }
7048
7049 last = item;
7050 item = item->next;
7051 xfree (last);
7052 }
7053
7054 dwarf2_queue = dwarf2_queue_tail = NULL;
7055 }
7056
7057 /* Read in full symbols for PST, and anything it depends on. */
7058
7059 static void
7060 psymtab_to_symtab_1 (struct partial_symtab *pst)
7061 {
7062 struct dwarf2_per_cu_data *per_cu;
7063 int i;
7064
7065 if (pst->readin)
7066 return;
7067
7068 for (i = 0; i < pst->number_of_dependencies; i++)
7069 if (!pst->dependencies[i]->readin
7070 && pst->dependencies[i]->user == NULL)
7071 {
7072 /* Inform about additional files that need to be read in. */
7073 if (info_verbose)
7074 {
7075 /* FIXME: i18n: Need to make this a single string. */
7076 fputs_filtered (" ", gdb_stdout);
7077 wrap_here ("");
7078 fputs_filtered ("and ", gdb_stdout);
7079 wrap_here ("");
7080 printf_filtered ("%s...", pst->dependencies[i]->filename);
7081 wrap_here (""); /* Flush output. */
7082 gdb_flush (gdb_stdout);
7083 }
7084 psymtab_to_symtab_1 (pst->dependencies[i]);
7085 }
7086
7087 per_cu = pst->read_symtab_private;
7088
7089 if (per_cu == NULL)
7090 {
7091 /* It's an include file, no symbols to read for it.
7092 Everything is in the parent symtab. */
7093 pst->readin = 1;
7094 return;
7095 }
7096
7097 dw2_do_instantiate_symtab (per_cu);
7098 }
7099
7100 /* Trivial hash function for die_info: the hash value of a DIE
7101 is its offset in .debug_info for this objfile. */
7102
7103 static hashval_t
7104 die_hash (const void *item)
7105 {
7106 const struct die_info *die = item;
7107
7108 return die->offset.sect_off;
7109 }
7110
7111 /* Trivial comparison function for die_info structures: two DIEs
7112 are equal if they have the same offset. */
7113
7114 static int
7115 die_eq (const void *item_lhs, const void *item_rhs)
7116 {
7117 const struct die_info *die_lhs = item_lhs;
7118 const struct die_info *die_rhs = item_rhs;
7119
7120 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
7121 }
7122
7123 /* die_reader_func for load_full_comp_unit.
7124 This is identical to read_signatured_type_reader,
7125 but is kept separate for now. */
7126
7127 static void
7128 load_full_comp_unit_reader (const struct die_reader_specs *reader,
7129 const gdb_byte *info_ptr,
7130 struct die_info *comp_unit_die,
7131 int has_children,
7132 void *data)
7133 {
7134 struct dwarf2_cu *cu = reader->cu;
7135 enum language *language_ptr = data;
7136
7137 gdb_assert (cu->die_hash == NULL);
7138 cu->die_hash =
7139 htab_create_alloc_ex (cu->header.length / 12,
7140 die_hash,
7141 die_eq,
7142 NULL,
7143 &cu->comp_unit_obstack,
7144 hashtab_obstack_allocate,
7145 dummy_obstack_deallocate);
7146
7147 if (has_children)
7148 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7149 &info_ptr, comp_unit_die);
7150 cu->dies = comp_unit_die;
7151 /* comp_unit_die is not stored in die_hash, no need. */
7152
7153 /* We try not to read any attributes in this function, because not
7154 all CUs needed for references have been loaded yet, and symbol
7155 table processing isn't initialized. But we have to set the CU language,
7156 or we won't be able to build types correctly.
7157 Similarly, if we do not read the producer, we can not apply
7158 producer-specific interpretation. */
7159 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
7160 }
7161
7162 /* Load the DIEs associated with PER_CU into memory. */
7163
7164 static void
7165 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7166 enum language pretend_language)
7167 {
7168 gdb_assert (! this_cu->is_debug_types);
7169
7170 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7171 load_full_comp_unit_reader, &pretend_language);
7172 }
7173
7174 /* Add a DIE to the delayed physname list. */
7175
7176 static void
7177 add_to_method_list (struct type *type, int fnfield_index, int index,
7178 const char *name, struct die_info *die,
7179 struct dwarf2_cu *cu)
7180 {
7181 struct delayed_method_info mi;
7182 mi.type = type;
7183 mi.fnfield_index = fnfield_index;
7184 mi.index = index;
7185 mi.name = name;
7186 mi.die = die;
7187 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7188 }
7189
7190 /* A cleanup for freeing the delayed method list. */
7191
7192 static void
7193 free_delayed_list (void *ptr)
7194 {
7195 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7196 if (cu->method_list != NULL)
7197 {
7198 VEC_free (delayed_method_info, cu->method_list);
7199 cu->method_list = NULL;
7200 }
7201 }
7202
7203 /* Compute the physnames of any methods on the CU's method list.
7204
7205 The computation of method physnames is delayed in order to avoid the
7206 (bad) condition that one of the method's formal parameters is of an as yet
7207 incomplete type. */
7208
7209 static void
7210 compute_delayed_physnames (struct dwarf2_cu *cu)
7211 {
7212 int i;
7213 struct delayed_method_info *mi;
7214 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7215 {
7216 const char *physname;
7217 struct fn_fieldlist *fn_flp
7218 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7219 physname = dwarf2_physname (mi->name, mi->die, cu);
7220 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
7221 }
7222 }
7223
7224 /* Go objects should be embedded in a DW_TAG_module DIE,
7225 and it's not clear if/how imported objects will appear.
7226 To keep Go support simple until that's worked out,
7227 go back through what we've read and create something usable.
7228 We could do this while processing each DIE, and feels kinda cleaner,
7229 but that way is more invasive.
7230 This is to, for example, allow the user to type "p var" or "b main"
7231 without having to specify the package name, and allow lookups
7232 of module.object to work in contexts that use the expression
7233 parser. */
7234
7235 static void
7236 fixup_go_packaging (struct dwarf2_cu *cu)
7237 {
7238 char *package_name = NULL;
7239 struct pending *list;
7240 int i;
7241
7242 for (list = global_symbols; list != NULL; list = list->next)
7243 {
7244 for (i = 0; i < list->nsyms; ++i)
7245 {
7246 struct symbol *sym = list->symbol[i];
7247
7248 if (SYMBOL_LANGUAGE (sym) == language_go
7249 && SYMBOL_CLASS (sym) == LOC_BLOCK)
7250 {
7251 char *this_package_name = go_symbol_package_name (sym);
7252
7253 if (this_package_name == NULL)
7254 continue;
7255 if (package_name == NULL)
7256 package_name = this_package_name;
7257 else
7258 {
7259 if (strcmp (package_name, this_package_name) != 0)
7260 complaint (&symfile_complaints,
7261 _("Symtab %s has objects from two different Go packages: %s and %s"),
7262 (SYMBOL_SYMTAB (sym)
7263 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
7264 : cu->objfile->name),
7265 this_package_name, package_name);
7266 xfree (this_package_name);
7267 }
7268 }
7269 }
7270 }
7271
7272 if (package_name != NULL)
7273 {
7274 struct objfile *objfile = cu->objfile;
7275 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
7276 package_name,
7277 strlen (package_name));
7278 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
7279 saved_package_name, objfile);
7280 struct symbol *sym;
7281
7282 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7283
7284 sym = allocate_symbol (objfile);
7285 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
7286 SYMBOL_SET_NAMES (sym, saved_package_name,
7287 strlen (saved_package_name), 0, objfile);
7288 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7289 e.g., "main" finds the "main" module and not C's main(). */
7290 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
7291 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
7292 SYMBOL_TYPE (sym) = type;
7293
7294 add_symbol_to_list (sym, &global_symbols);
7295
7296 xfree (package_name);
7297 }
7298 }
7299
7300 /* Return the symtab for PER_CU. This works properly regardless of
7301 whether we're using the index or psymtabs. */
7302
7303 static struct symtab *
7304 get_symtab (struct dwarf2_per_cu_data *per_cu)
7305 {
7306 return (dwarf2_per_objfile->using_index
7307 ? per_cu->v.quick->symtab
7308 : per_cu->v.psymtab->symtab);
7309 }
7310
7311 /* A helper function for computing the list of all symbol tables
7312 included by PER_CU. */
7313
7314 static void
7315 recursively_compute_inclusions (VEC (symtab_ptr) **result,
7316 htab_t all_children, htab_t all_type_symtabs,
7317 struct dwarf2_per_cu_data *per_cu,
7318 struct symtab *immediate_parent)
7319 {
7320 void **slot;
7321 int ix;
7322 struct symtab *symtab;
7323 struct dwarf2_per_cu_data *iter;
7324
7325 slot = htab_find_slot (all_children, per_cu, INSERT);
7326 if (*slot != NULL)
7327 {
7328 /* This inclusion and its children have been processed. */
7329 return;
7330 }
7331
7332 *slot = per_cu;
7333 /* Only add a CU if it has a symbol table. */
7334 symtab = get_symtab (per_cu);
7335 if (symtab != NULL)
7336 {
7337 /* If this is a type unit only add its symbol table if we haven't
7338 seen it yet (type unit per_cu's can share symtabs). */
7339 if (per_cu->is_debug_types)
7340 {
7341 slot = htab_find_slot (all_type_symtabs, symtab, INSERT);
7342 if (*slot == NULL)
7343 {
7344 *slot = symtab;
7345 VEC_safe_push (symtab_ptr, *result, symtab);
7346 if (symtab->user == NULL)
7347 symtab->user = immediate_parent;
7348 }
7349 }
7350 else
7351 {
7352 VEC_safe_push (symtab_ptr, *result, symtab);
7353 if (symtab->user == NULL)
7354 symtab->user = immediate_parent;
7355 }
7356 }
7357
7358 for (ix = 0;
7359 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
7360 ++ix)
7361 {
7362 recursively_compute_inclusions (result, all_children,
7363 all_type_symtabs, iter, symtab);
7364 }
7365 }
7366
7367 /* Compute the symtab 'includes' fields for the symtab related to
7368 PER_CU. */
7369
7370 static void
7371 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7372 {
7373 gdb_assert (! per_cu->is_debug_types);
7374
7375 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
7376 {
7377 int ix, len;
7378 struct dwarf2_per_cu_data *per_cu_iter;
7379 struct symtab *symtab_iter;
7380 VEC (symtab_ptr) *result_symtabs = NULL;
7381 htab_t all_children, all_type_symtabs;
7382 struct symtab *symtab = get_symtab (per_cu);
7383
7384 /* If we don't have a symtab, we can just skip this case. */
7385 if (symtab == NULL)
7386 return;
7387
7388 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7389 NULL, xcalloc, xfree);
7390 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7391 NULL, xcalloc, xfree);
7392
7393 for (ix = 0;
7394 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
7395 ix, per_cu_iter);
7396 ++ix)
7397 {
7398 recursively_compute_inclusions (&result_symtabs, all_children,
7399 all_type_symtabs, per_cu_iter,
7400 symtab);
7401 }
7402
7403 /* Now we have a transitive closure of all the included symtabs. */
7404 len = VEC_length (symtab_ptr, result_symtabs);
7405 symtab->includes
7406 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7407 (len + 1) * sizeof (struct symtab *));
7408 for (ix = 0;
7409 VEC_iterate (symtab_ptr, result_symtabs, ix, symtab_iter);
7410 ++ix)
7411 symtab->includes[ix] = symtab_iter;
7412 symtab->includes[len] = NULL;
7413
7414 VEC_free (symtab_ptr, result_symtabs);
7415 htab_delete (all_children);
7416 htab_delete (all_type_symtabs);
7417 }
7418 }
7419
7420 /* Compute the 'includes' field for the symtabs of all the CUs we just
7421 read. */
7422
7423 static void
7424 process_cu_includes (void)
7425 {
7426 int ix;
7427 struct dwarf2_per_cu_data *iter;
7428
7429 for (ix = 0;
7430 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7431 ix, iter);
7432 ++ix)
7433 {
7434 if (! iter->is_debug_types)
7435 compute_symtab_includes (iter);
7436 }
7437
7438 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
7439 }
7440
7441 /* Generate full symbol information for PER_CU, whose DIEs have
7442 already been loaded into memory. */
7443
7444 static void
7445 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7446 enum language pretend_language)
7447 {
7448 struct dwarf2_cu *cu = per_cu->cu;
7449 struct objfile *objfile = per_cu->objfile;
7450 CORE_ADDR lowpc, highpc;
7451 struct symtab *symtab;
7452 struct cleanup *back_to, *delayed_list_cleanup;
7453 CORE_ADDR baseaddr;
7454 struct block *static_block;
7455
7456 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7457
7458 buildsym_init ();
7459 back_to = make_cleanup (really_free_pendings, NULL);
7460 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7461
7462 cu->list_in_scope = &file_symbols;
7463
7464 cu->language = pretend_language;
7465 cu->language_defn = language_def (cu->language);
7466
7467 /* Do line number decoding in read_file_scope () */
7468 process_die (cu->dies, cu);
7469
7470 /* For now fudge the Go package. */
7471 if (cu->language == language_go)
7472 fixup_go_packaging (cu);
7473
7474 /* Now that we have processed all the DIEs in the CU, all the types
7475 should be complete, and it should now be safe to compute all of the
7476 physnames. */
7477 compute_delayed_physnames (cu);
7478 do_cleanups (delayed_list_cleanup);
7479
7480 /* Some compilers don't define a DW_AT_high_pc attribute for the
7481 compilation unit. If the DW_AT_high_pc is missing, synthesize
7482 it, by scanning the DIE's below the compilation unit. */
7483 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
7484
7485 static_block
7486 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0, 1);
7487
7488 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7489 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7490 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7491 addrmap to help ensure it has an accurate map of pc values belonging to
7492 this comp unit. */
7493 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7494
7495 symtab = end_symtab_from_static_block (static_block, objfile,
7496 SECT_OFF_TEXT (objfile), 0);
7497
7498 if (symtab != NULL)
7499 {
7500 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
7501
7502 /* Set symtab language to language from DW_AT_language. If the
7503 compilation is from a C file generated by language preprocessors, do
7504 not set the language if it was already deduced by start_subfile. */
7505 if (!(cu->language == language_c && symtab->language != language_c))
7506 symtab->language = cu->language;
7507
7508 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7509 produce DW_AT_location with location lists but it can be possibly
7510 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7511 there were bugs in prologue debug info, fixed later in GCC-4.5
7512 by "unwind info for epilogues" patch (which is not directly related).
7513
7514 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7515 needed, it would be wrong due to missing DW_AT_producer there.
7516
7517 Still one can confuse GDB by using non-standard GCC compilation
7518 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7519 */
7520 if (cu->has_loclist && gcc_4_minor >= 5)
7521 symtab->locations_valid = 1;
7522
7523 if (gcc_4_minor >= 5)
7524 symtab->epilogue_unwind_valid = 1;
7525
7526 symtab->call_site_htab = cu->call_site_htab;
7527 }
7528
7529 if (dwarf2_per_objfile->using_index)
7530 per_cu->v.quick->symtab = symtab;
7531 else
7532 {
7533 struct partial_symtab *pst = per_cu->v.psymtab;
7534 pst->symtab = symtab;
7535 pst->readin = 1;
7536 }
7537
7538 /* Push it for inclusion processing later. */
7539 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7540
7541 do_cleanups (back_to);
7542 }
7543
7544 /* Generate full symbol information for type unit PER_CU, whose DIEs have
7545 already been loaded into memory. */
7546
7547 static void
7548 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7549 enum language pretend_language)
7550 {
7551 struct dwarf2_cu *cu = per_cu->cu;
7552 struct objfile *objfile = per_cu->objfile;
7553 struct symtab *symtab;
7554 struct cleanup *back_to, *delayed_list_cleanup;
7555 struct signatured_type *sig_type;
7556
7557 gdb_assert (per_cu->is_debug_types);
7558 sig_type = (struct signatured_type *) per_cu;
7559
7560 buildsym_init ();
7561 back_to = make_cleanup (really_free_pendings, NULL);
7562 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7563
7564 cu->list_in_scope = &file_symbols;
7565
7566 cu->language = pretend_language;
7567 cu->language_defn = language_def (cu->language);
7568
7569 /* The symbol tables are set up in read_type_unit_scope. */
7570 process_die (cu->dies, cu);
7571
7572 /* For now fudge the Go package. */
7573 if (cu->language == language_go)
7574 fixup_go_packaging (cu);
7575
7576 /* Now that we have processed all the DIEs in the CU, all the types
7577 should be complete, and it should now be safe to compute all of the
7578 physnames. */
7579 compute_delayed_physnames (cu);
7580 do_cleanups (delayed_list_cleanup);
7581
7582 /* TUs share symbol tables.
7583 If this is the first TU to use this symtab, complete the construction
7584 of it with end_expandable_symtab. Otherwise, complete the addition of
7585 this TU's symbols to the existing symtab. */
7586 if (sig_type->type_unit_group->primary_symtab == NULL)
7587 {
7588 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7589 sig_type->type_unit_group->primary_symtab = symtab;
7590
7591 if (symtab != NULL)
7592 {
7593 /* Set symtab language to language from DW_AT_language. If the
7594 compilation is from a C file generated by language preprocessors,
7595 do not set the language if it was already deduced by
7596 start_subfile. */
7597 if (!(cu->language == language_c && symtab->language != language_c))
7598 symtab->language = cu->language;
7599 }
7600 }
7601 else
7602 {
7603 augment_type_symtab (objfile,
7604 sig_type->type_unit_group->primary_symtab);
7605 symtab = sig_type->type_unit_group->primary_symtab;
7606 }
7607
7608 if (dwarf2_per_objfile->using_index)
7609 per_cu->v.quick->symtab = symtab;
7610 else
7611 {
7612 struct partial_symtab *pst = per_cu->v.psymtab;
7613 pst->symtab = symtab;
7614 pst->readin = 1;
7615 }
7616
7617 do_cleanups (back_to);
7618 }
7619
7620 /* Process an imported unit DIE. */
7621
7622 static void
7623 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7624 {
7625 struct attribute *attr;
7626
7627 /* For now we don't handle imported units in type units. */
7628 if (cu->per_cu->is_debug_types)
7629 {
7630 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7631 " supported in type units [in module %s]"),
7632 cu->objfile->name);
7633 }
7634
7635 attr = dwarf2_attr (die, DW_AT_import, cu);
7636 if (attr != NULL)
7637 {
7638 struct dwarf2_per_cu_data *per_cu;
7639 struct symtab *imported_symtab;
7640 sect_offset offset;
7641 int is_dwz;
7642
7643 offset = dwarf2_get_ref_die_offset (attr);
7644 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7645 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
7646
7647 /* Queue the unit, if needed. */
7648 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7649 load_full_comp_unit (per_cu, cu->language);
7650
7651 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7652 per_cu);
7653 }
7654 }
7655
7656 /* Process a die and its children. */
7657
7658 static void
7659 process_die (struct die_info *die, struct dwarf2_cu *cu)
7660 {
7661 switch (die->tag)
7662 {
7663 case DW_TAG_padding:
7664 break;
7665 case DW_TAG_compile_unit:
7666 case DW_TAG_partial_unit:
7667 read_file_scope (die, cu);
7668 break;
7669 case DW_TAG_type_unit:
7670 read_type_unit_scope (die, cu);
7671 break;
7672 case DW_TAG_subprogram:
7673 case DW_TAG_inlined_subroutine:
7674 read_func_scope (die, cu);
7675 break;
7676 case DW_TAG_lexical_block:
7677 case DW_TAG_try_block:
7678 case DW_TAG_catch_block:
7679 read_lexical_block_scope (die, cu);
7680 break;
7681 case DW_TAG_GNU_call_site:
7682 read_call_site_scope (die, cu);
7683 break;
7684 case DW_TAG_class_type:
7685 case DW_TAG_interface_type:
7686 case DW_TAG_structure_type:
7687 case DW_TAG_union_type:
7688 process_structure_scope (die, cu);
7689 break;
7690 case DW_TAG_enumeration_type:
7691 process_enumeration_scope (die, cu);
7692 break;
7693
7694 /* These dies have a type, but processing them does not create
7695 a symbol or recurse to process the children. Therefore we can
7696 read them on-demand through read_type_die. */
7697 case DW_TAG_subroutine_type:
7698 case DW_TAG_set_type:
7699 case DW_TAG_array_type:
7700 case DW_TAG_pointer_type:
7701 case DW_TAG_ptr_to_member_type:
7702 case DW_TAG_reference_type:
7703 case DW_TAG_string_type:
7704 break;
7705
7706 case DW_TAG_base_type:
7707 case DW_TAG_subrange_type:
7708 case DW_TAG_typedef:
7709 /* Add a typedef symbol for the type definition, if it has a
7710 DW_AT_name. */
7711 new_symbol (die, read_type_die (die, cu), cu);
7712 break;
7713 case DW_TAG_common_block:
7714 read_common_block (die, cu);
7715 break;
7716 case DW_TAG_common_inclusion:
7717 break;
7718 case DW_TAG_namespace:
7719 cu->processing_has_namespace_info = 1;
7720 read_namespace (die, cu);
7721 break;
7722 case DW_TAG_module:
7723 cu->processing_has_namespace_info = 1;
7724 read_module (die, cu);
7725 break;
7726 case DW_TAG_imported_declaration:
7727 case DW_TAG_imported_module:
7728 cu->processing_has_namespace_info = 1;
7729 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7730 || cu->language != language_fortran))
7731 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7732 dwarf_tag_name (die->tag));
7733 read_import_statement (die, cu);
7734 break;
7735
7736 case DW_TAG_imported_unit:
7737 process_imported_unit_die (die, cu);
7738 break;
7739
7740 default:
7741 new_symbol (die, NULL, cu);
7742 break;
7743 }
7744 }
7745 \f
7746 /* DWARF name computation. */
7747
7748 /* A helper function for dwarf2_compute_name which determines whether DIE
7749 needs to have the name of the scope prepended to the name listed in the
7750 die. */
7751
7752 static int
7753 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7754 {
7755 struct attribute *attr;
7756
7757 switch (die->tag)
7758 {
7759 case DW_TAG_namespace:
7760 case DW_TAG_typedef:
7761 case DW_TAG_class_type:
7762 case DW_TAG_interface_type:
7763 case DW_TAG_structure_type:
7764 case DW_TAG_union_type:
7765 case DW_TAG_enumeration_type:
7766 case DW_TAG_enumerator:
7767 case DW_TAG_subprogram:
7768 case DW_TAG_member:
7769 return 1;
7770
7771 case DW_TAG_variable:
7772 case DW_TAG_constant:
7773 /* We only need to prefix "globally" visible variables. These include
7774 any variable marked with DW_AT_external or any variable that
7775 lives in a namespace. [Variables in anonymous namespaces
7776 require prefixing, but they are not DW_AT_external.] */
7777
7778 if (dwarf2_attr (die, DW_AT_specification, cu))
7779 {
7780 struct dwarf2_cu *spec_cu = cu;
7781
7782 return die_needs_namespace (die_specification (die, &spec_cu),
7783 spec_cu);
7784 }
7785
7786 attr = dwarf2_attr (die, DW_AT_external, cu);
7787 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7788 && die->parent->tag != DW_TAG_module)
7789 return 0;
7790 /* A variable in a lexical block of some kind does not need a
7791 namespace, even though in C++ such variables may be external
7792 and have a mangled name. */
7793 if (die->parent->tag == DW_TAG_lexical_block
7794 || die->parent->tag == DW_TAG_try_block
7795 || die->parent->tag == DW_TAG_catch_block
7796 || die->parent->tag == DW_TAG_subprogram)
7797 return 0;
7798 return 1;
7799
7800 default:
7801 return 0;
7802 }
7803 }
7804
7805 /* Retrieve the last character from a mem_file. */
7806
7807 static void
7808 do_ui_file_peek_last (void *object, const char *buffer, long length)
7809 {
7810 char *last_char_p = (char *) object;
7811
7812 if (length > 0)
7813 *last_char_p = buffer[length - 1];
7814 }
7815
7816 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
7817 compute the physname for the object, which include a method's:
7818 - formal parameters (C++/Java),
7819 - receiver type (Go),
7820 - return type (Java).
7821
7822 The term "physname" is a bit confusing.
7823 For C++, for example, it is the demangled name.
7824 For Go, for example, it's the mangled name.
7825
7826 For Ada, return the DIE's linkage name rather than the fully qualified
7827 name. PHYSNAME is ignored..
7828
7829 The result is allocated on the objfile_obstack and canonicalized. */
7830
7831 static const char *
7832 dwarf2_compute_name (const char *name,
7833 struct die_info *die, struct dwarf2_cu *cu,
7834 int physname)
7835 {
7836 struct objfile *objfile = cu->objfile;
7837
7838 if (name == NULL)
7839 name = dwarf2_name (die, cu);
7840
7841 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7842 compute it by typename_concat inside GDB. */
7843 if (cu->language == language_ada
7844 || (cu->language == language_fortran && physname))
7845 {
7846 /* For Ada unit, we prefer the linkage name over the name, as
7847 the former contains the exported name, which the user expects
7848 to be able to reference. Ideally, we want the user to be able
7849 to reference this entity using either natural or linkage name,
7850 but we haven't started looking at this enhancement yet. */
7851 struct attribute *attr;
7852
7853 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7854 if (attr == NULL)
7855 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7856 if (attr && DW_STRING (attr))
7857 return DW_STRING (attr);
7858 }
7859
7860 /* These are the only languages we know how to qualify names in. */
7861 if (name != NULL
7862 && (cu->language == language_cplus || cu->language == language_java
7863 || cu->language == language_fortran))
7864 {
7865 if (die_needs_namespace (die, cu))
7866 {
7867 long length;
7868 const char *prefix;
7869 struct ui_file *buf;
7870
7871 prefix = determine_prefix (die, cu);
7872 buf = mem_fileopen ();
7873 if (*prefix != '\0')
7874 {
7875 char *prefixed_name = typename_concat (NULL, prefix, name,
7876 physname, cu);
7877
7878 fputs_unfiltered (prefixed_name, buf);
7879 xfree (prefixed_name);
7880 }
7881 else
7882 fputs_unfiltered (name, buf);
7883
7884 /* Template parameters may be specified in the DIE's DW_AT_name, or
7885 as children with DW_TAG_template_type_param or
7886 DW_TAG_value_type_param. If the latter, add them to the name
7887 here. If the name already has template parameters, then
7888 skip this step; some versions of GCC emit both, and
7889 it is more efficient to use the pre-computed name.
7890
7891 Something to keep in mind about this process: it is very
7892 unlikely, or in some cases downright impossible, to produce
7893 something that will match the mangled name of a function.
7894 If the definition of the function has the same debug info,
7895 we should be able to match up with it anyway. But fallbacks
7896 using the minimal symbol, for instance to find a method
7897 implemented in a stripped copy of libstdc++, will not work.
7898 If we do not have debug info for the definition, we will have to
7899 match them up some other way.
7900
7901 When we do name matching there is a related problem with function
7902 templates; two instantiated function templates are allowed to
7903 differ only by their return types, which we do not add here. */
7904
7905 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7906 {
7907 struct attribute *attr;
7908 struct die_info *child;
7909 int first = 1;
7910
7911 die->building_fullname = 1;
7912
7913 for (child = die->child; child != NULL; child = child->sibling)
7914 {
7915 struct type *type;
7916 LONGEST value;
7917 const gdb_byte *bytes;
7918 struct dwarf2_locexpr_baton *baton;
7919 struct value *v;
7920
7921 if (child->tag != DW_TAG_template_type_param
7922 && child->tag != DW_TAG_template_value_param)
7923 continue;
7924
7925 if (first)
7926 {
7927 fputs_unfiltered ("<", buf);
7928 first = 0;
7929 }
7930 else
7931 fputs_unfiltered (", ", buf);
7932
7933 attr = dwarf2_attr (child, DW_AT_type, cu);
7934 if (attr == NULL)
7935 {
7936 complaint (&symfile_complaints,
7937 _("template parameter missing DW_AT_type"));
7938 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7939 continue;
7940 }
7941 type = die_type (child, cu);
7942
7943 if (child->tag == DW_TAG_template_type_param)
7944 {
7945 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
7946 continue;
7947 }
7948
7949 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7950 if (attr == NULL)
7951 {
7952 complaint (&symfile_complaints,
7953 _("template parameter missing "
7954 "DW_AT_const_value"));
7955 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7956 continue;
7957 }
7958
7959 dwarf2_const_value_attr (attr, type, name,
7960 &cu->comp_unit_obstack, cu,
7961 &value, &bytes, &baton);
7962
7963 if (TYPE_NOSIGN (type))
7964 /* GDB prints characters as NUMBER 'CHAR'. If that's
7965 changed, this can use value_print instead. */
7966 c_printchar (value, type, buf);
7967 else
7968 {
7969 struct value_print_options opts;
7970
7971 if (baton != NULL)
7972 v = dwarf2_evaluate_loc_desc (type, NULL,
7973 baton->data,
7974 baton->size,
7975 baton->per_cu);
7976 else if (bytes != NULL)
7977 {
7978 v = allocate_value (type);
7979 memcpy (value_contents_writeable (v), bytes,
7980 TYPE_LENGTH (type));
7981 }
7982 else
7983 v = value_from_longest (type, value);
7984
7985 /* Specify decimal so that we do not depend on
7986 the radix. */
7987 get_formatted_print_options (&opts, 'd');
7988 opts.raw = 1;
7989 value_print (v, buf, &opts);
7990 release_value (v);
7991 value_free (v);
7992 }
7993 }
7994
7995 die->building_fullname = 0;
7996
7997 if (!first)
7998 {
7999 /* Close the argument list, with a space if necessary
8000 (nested templates). */
8001 char last_char = '\0';
8002 ui_file_put (buf, do_ui_file_peek_last, &last_char);
8003 if (last_char == '>')
8004 fputs_unfiltered (" >", buf);
8005 else
8006 fputs_unfiltered (">", buf);
8007 }
8008 }
8009
8010 /* For Java and C++ methods, append formal parameter type
8011 information, if PHYSNAME. */
8012
8013 if (physname && die->tag == DW_TAG_subprogram
8014 && (cu->language == language_cplus
8015 || cu->language == language_java))
8016 {
8017 struct type *type = read_type_die (die, cu);
8018
8019 c_type_print_args (type, buf, 1, cu->language,
8020 &type_print_raw_options);
8021
8022 if (cu->language == language_java)
8023 {
8024 /* For java, we must append the return type to method
8025 names. */
8026 if (die->tag == DW_TAG_subprogram)
8027 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
8028 0, 0, &type_print_raw_options);
8029 }
8030 else if (cu->language == language_cplus)
8031 {
8032 /* Assume that an artificial first parameter is
8033 "this", but do not crash if it is not. RealView
8034 marks unnamed (and thus unused) parameters as
8035 artificial; there is no way to differentiate
8036 the two cases. */
8037 if (TYPE_NFIELDS (type) > 0
8038 && TYPE_FIELD_ARTIFICIAL (type, 0)
8039 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
8040 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8041 0))))
8042 fputs_unfiltered (" const", buf);
8043 }
8044 }
8045
8046 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
8047 &length);
8048 ui_file_delete (buf);
8049
8050 if (cu->language == language_cplus)
8051 {
8052 const char *cname
8053 = dwarf2_canonicalize_name (name, cu,
8054 &objfile->objfile_obstack);
8055
8056 if (cname != NULL)
8057 name = cname;
8058 }
8059 }
8060 }
8061
8062 return name;
8063 }
8064
8065 /* Return the fully qualified name of DIE, based on its DW_AT_name.
8066 If scope qualifiers are appropriate they will be added. The result
8067 will be allocated on the objfile_obstack, or NULL if the DIE does
8068 not have a name. NAME may either be from a previous call to
8069 dwarf2_name or NULL.
8070
8071 The output string will be canonicalized (if C++/Java). */
8072
8073 static const char *
8074 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8075 {
8076 return dwarf2_compute_name (name, die, cu, 0);
8077 }
8078
8079 /* Construct a physname for the given DIE in CU. NAME may either be
8080 from a previous call to dwarf2_name or NULL. The result will be
8081 allocated on the objfile_objstack or NULL if the DIE does not have a
8082 name.
8083
8084 The output string will be canonicalized (if C++/Java). */
8085
8086 static const char *
8087 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8088 {
8089 struct objfile *objfile = cu->objfile;
8090 struct attribute *attr;
8091 const char *retval, *mangled = NULL, *canon = NULL;
8092 struct cleanup *back_to;
8093 int need_copy = 1;
8094
8095 /* In this case dwarf2_compute_name is just a shortcut not building anything
8096 on its own. */
8097 if (!die_needs_namespace (die, cu))
8098 return dwarf2_compute_name (name, die, cu, 1);
8099
8100 back_to = make_cleanup (null_cleanup, NULL);
8101
8102 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8103 if (!attr)
8104 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8105
8106 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8107 has computed. */
8108 if (attr && DW_STRING (attr))
8109 {
8110 char *demangled;
8111
8112 mangled = DW_STRING (attr);
8113
8114 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8115 type. It is easier for GDB users to search for such functions as
8116 `name(params)' than `long name(params)'. In such case the minimal
8117 symbol names do not match the full symbol names but for template
8118 functions there is never a need to look up their definition from their
8119 declaration so the only disadvantage remains the minimal symbol
8120 variant `long name(params)' does not have the proper inferior type.
8121 */
8122
8123 if (cu->language == language_go)
8124 {
8125 /* This is a lie, but we already lie to the caller new_symbol_full.
8126 new_symbol_full assumes we return the mangled name.
8127 This just undoes that lie until things are cleaned up. */
8128 demangled = NULL;
8129 }
8130 else
8131 {
8132 demangled = gdb_demangle (mangled,
8133 (DMGL_PARAMS | DMGL_ANSI
8134 | (cu->language == language_java
8135 ? DMGL_JAVA | DMGL_RET_POSTFIX
8136 : DMGL_RET_DROP)));
8137 }
8138 if (demangled)
8139 {
8140 make_cleanup (xfree, demangled);
8141 canon = demangled;
8142 }
8143 else
8144 {
8145 canon = mangled;
8146 need_copy = 0;
8147 }
8148 }
8149
8150 if (canon == NULL || check_physname)
8151 {
8152 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8153
8154 if (canon != NULL && strcmp (physname, canon) != 0)
8155 {
8156 /* It may not mean a bug in GDB. The compiler could also
8157 compute DW_AT_linkage_name incorrectly. But in such case
8158 GDB would need to be bug-to-bug compatible. */
8159
8160 complaint (&symfile_complaints,
8161 _("Computed physname <%s> does not match demangled <%s> "
8162 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
8163 physname, canon, mangled, die->offset.sect_off, objfile->name);
8164
8165 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8166 is available here - over computed PHYSNAME. It is safer
8167 against both buggy GDB and buggy compilers. */
8168
8169 retval = canon;
8170 }
8171 else
8172 {
8173 retval = physname;
8174 need_copy = 0;
8175 }
8176 }
8177 else
8178 retval = canon;
8179
8180 if (need_copy)
8181 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
8182
8183 do_cleanups (back_to);
8184 return retval;
8185 }
8186
8187 /* Read the import statement specified by the given die and record it. */
8188
8189 static void
8190 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
8191 {
8192 struct objfile *objfile = cu->objfile;
8193 struct attribute *import_attr;
8194 struct die_info *imported_die, *child_die;
8195 struct dwarf2_cu *imported_cu;
8196 const char *imported_name;
8197 const char *imported_name_prefix;
8198 const char *canonical_name;
8199 const char *import_alias;
8200 const char *imported_declaration = NULL;
8201 const char *import_prefix;
8202 VEC (const_char_ptr) *excludes = NULL;
8203 struct cleanup *cleanups;
8204
8205 import_attr = dwarf2_attr (die, DW_AT_import, cu);
8206 if (import_attr == NULL)
8207 {
8208 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8209 dwarf_tag_name (die->tag));
8210 return;
8211 }
8212
8213 imported_cu = cu;
8214 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
8215 imported_name = dwarf2_name (imported_die, imported_cu);
8216 if (imported_name == NULL)
8217 {
8218 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
8219
8220 The import in the following code:
8221 namespace A
8222 {
8223 typedef int B;
8224 }
8225
8226 int main ()
8227 {
8228 using A::B;
8229 B b;
8230 return b;
8231 }
8232
8233 ...
8234 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
8235 <52> DW_AT_decl_file : 1
8236 <53> DW_AT_decl_line : 6
8237 <54> DW_AT_import : <0x75>
8238 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
8239 <59> DW_AT_name : B
8240 <5b> DW_AT_decl_file : 1
8241 <5c> DW_AT_decl_line : 2
8242 <5d> DW_AT_type : <0x6e>
8243 ...
8244 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
8245 <76> DW_AT_byte_size : 4
8246 <77> DW_AT_encoding : 5 (signed)
8247
8248 imports the wrong die ( 0x75 instead of 0x58 ).
8249 This case will be ignored until the gcc bug is fixed. */
8250 return;
8251 }
8252
8253 /* Figure out the local name after import. */
8254 import_alias = dwarf2_name (die, cu);
8255
8256 /* Figure out where the statement is being imported to. */
8257 import_prefix = determine_prefix (die, cu);
8258
8259 /* Figure out what the scope of the imported die is and prepend it
8260 to the name of the imported die. */
8261 imported_name_prefix = determine_prefix (imported_die, imported_cu);
8262
8263 if (imported_die->tag != DW_TAG_namespace
8264 && imported_die->tag != DW_TAG_module)
8265 {
8266 imported_declaration = imported_name;
8267 canonical_name = imported_name_prefix;
8268 }
8269 else if (strlen (imported_name_prefix) > 0)
8270 canonical_name = obconcat (&objfile->objfile_obstack,
8271 imported_name_prefix, "::", imported_name,
8272 (char *) NULL);
8273 else
8274 canonical_name = imported_name;
8275
8276 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
8277
8278 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
8279 for (child_die = die->child; child_die && child_die->tag;
8280 child_die = sibling_die (child_die))
8281 {
8282 /* DWARF-4: A Fortran use statement with a “rename list” may be
8283 represented by an imported module entry with an import attribute
8284 referring to the module and owned entries corresponding to those
8285 entities that are renamed as part of being imported. */
8286
8287 if (child_die->tag != DW_TAG_imported_declaration)
8288 {
8289 complaint (&symfile_complaints,
8290 _("child DW_TAG_imported_declaration expected "
8291 "- DIE at 0x%x [in module %s]"),
8292 child_die->offset.sect_off, objfile->name);
8293 continue;
8294 }
8295
8296 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
8297 if (import_attr == NULL)
8298 {
8299 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8300 dwarf_tag_name (child_die->tag));
8301 continue;
8302 }
8303
8304 imported_cu = cu;
8305 imported_die = follow_die_ref_or_sig (child_die, import_attr,
8306 &imported_cu);
8307 imported_name = dwarf2_name (imported_die, imported_cu);
8308 if (imported_name == NULL)
8309 {
8310 complaint (&symfile_complaints,
8311 _("child DW_TAG_imported_declaration has unknown "
8312 "imported name - DIE at 0x%x [in module %s]"),
8313 child_die->offset.sect_off, objfile->name);
8314 continue;
8315 }
8316
8317 VEC_safe_push (const_char_ptr, excludes, imported_name);
8318
8319 process_die (child_die, cu);
8320 }
8321
8322 cp_add_using_directive (import_prefix,
8323 canonical_name,
8324 import_alias,
8325 imported_declaration,
8326 excludes,
8327 0,
8328 &objfile->objfile_obstack);
8329
8330 do_cleanups (cleanups);
8331 }
8332
8333 /* Cleanup function for handle_DW_AT_stmt_list. */
8334
8335 static void
8336 free_cu_line_header (void *arg)
8337 {
8338 struct dwarf2_cu *cu = arg;
8339
8340 free_line_header (cu->line_header);
8341 cu->line_header = NULL;
8342 }
8343
8344 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
8345 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
8346 this, it was first present in GCC release 4.3.0. */
8347
8348 static int
8349 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
8350 {
8351 if (!cu->checked_producer)
8352 check_producer (cu);
8353
8354 return cu->producer_is_gcc_lt_4_3;
8355 }
8356
8357 static void
8358 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
8359 const char **name, const char **comp_dir)
8360 {
8361 struct attribute *attr;
8362
8363 *name = NULL;
8364 *comp_dir = NULL;
8365
8366 /* Find the filename. Do not use dwarf2_name here, since the filename
8367 is not a source language identifier. */
8368 attr = dwarf2_attr (die, DW_AT_name, cu);
8369 if (attr)
8370 {
8371 *name = DW_STRING (attr);
8372 }
8373
8374 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
8375 if (attr)
8376 *comp_dir = DW_STRING (attr);
8377 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
8378 && IS_ABSOLUTE_PATH (*name))
8379 {
8380 char *d = ldirname (*name);
8381
8382 *comp_dir = d;
8383 if (d != NULL)
8384 make_cleanup (xfree, d);
8385 }
8386 if (*comp_dir != NULL)
8387 {
8388 /* Irix 6.2 native cc prepends <machine>.: to the compilation
8389 directory, get rid of it. */
8390 char *cp = strchr (*comp_dir, ':');
8391
8392 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
8393 *comp_dir = cp + 1;
8394 }
8395
8396 if (*name == NULL)
8397 *name = "<unknown>";
8398 }
8399
8400 /* Handle DW_AT_stmt_list for a compilation unit.
8401 DIE is the DW_TAG_compile_unit die for CU.
8402 COMP_DIR is the compilation directory.
8403 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
8404
8405 static void
8406 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
8407 const char *comp_dir) /* ARI: editCase function */
8408 {
8409 struct attribute *attr;
8410
8411 gdb_assert (! cu->per_cu->is_debug_types);
8412
8413 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8414 if (attr)
8415 {
8416 unsigned int line_offset = DW_UNSND (attr);
8417 struct line_header *line_header
8418 = dwarf_decode_line_header (line_offset, cu);
8419
8420 if (line_header)
8421 {
8422 cu->line_header = line_header;
8423 make_cleanup (free_cu_line_header, cu);
8424 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
8425 }
8426 }
8427 }
8428
8429 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
8430
8431 static void
8432 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
8433 {
8434 struct objfile *objfile = dwarf2_per_objfile->objfile;
8435 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
8436 CORE_ADDR lowpc = ((CORE_ADDR) -1);
8437 CORE_ADDR highpc = ((CORE_ADDR) 0);
8438 struct attribute *attr;
8439 const char *name = NULL;
8440 const char *comp_dir = NULL;
8441 struct die_info *child_die;
8442 bfd *abfd = objfile->obfd;
8443 CORE_ADDR baseaddr;
8444
8445 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8446
8447 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
8448
8449 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8450 from finish_block. */
8451 if (lowpc == ((CORE_ADDR) -1))
8452 lowpc = highpc;
8453 lowpc += baseaddr;
8454 highpc += baseaddr;
8455
8456 find_file_and_directory (die, cu, &name, &comp_dir);
8457
8458 prepare_one_comp_unit (cu, die, cu->language);
8459
8460 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8461 standardised yet. As a workaround for the language detection we fall
8462 back to the DW_AT_producer string. */
8463 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8464 cu->language = language_opencl;
8465
8466 /* Similar hack for Go. */
8467 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8468 set_cu_language (DW_LANG_Go, cu);
8469
8470 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
8471
8472 /* Decode line number information if present. We do this before
8473 processing child DIEs, so that the line header table is available
8474 for DW_AT_decl_file. */
8475 handle_DW_AT_stmt_list (die, cu, comp_dir);
8476
8477 /* Process all dies in compilation unit. */
8478 if (die->child != NULL)
8479 {
8480 child_die = die->child;
8481 while (child_die && child_die->tag)
8482 {
8483 process_die (child_die, cu);
8484 child_die = sibling_die (child_die);
8485 }
8486 }
8487
8488 /* Decode macro information, if present. Dwarf 2 macro information
8489 refers to information in the line number info statement program
8490 header, so we can only read it if we've read the header
8491 successfully. */
8492 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8493 if (attr && cu->line_header)
8494 {
8495 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8496 complaint (&symfile_complaints,
8497 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8498
8499 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
8500 }
8501 else
8502 {
8503 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8504 if (attr && cu->line_header)
8505 {
8506 unsigned int macro_offset = DW_UNSND (attr);
8507
8508 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
8509 }
8510 }
8511
8512 do_cleanups (back_to);
8513 }
8514
8515 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8516 Create the set of symtabs used by this TU, or if this TU is sharing
8517 symtabs with another TU and the symtabs have already been created
8518 then restore those symtabs in the line header.
8519 We don't need the pc/line-number mapping for type units. */
8520
8521 static void
8522 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
8523 {
8524 struct objfile *objfile = dwarf2_per_objfile->objfile;
8525 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8526 struct type_unit_group *tu_group;
8527 int first_time;
8528 struct line_header *lh;
8529 struct attribute *attr;
8530 unsigned int i, line_offset;
8531 struct signatured_type *sig_type;
8532
8533 gdb_assert (per_cu->is_debug_types);
8534 sig_type = (struct signatured_type *) per_cu;
8535
8536 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8537
8538 /* If we're using .gdb_index (includes -readnow) then
8539 per_cu->type_unit_group may not have been set up yet. */
8540 if (sig_type->type_unit_group == NULL)
8541 sig_type->type_unit_group = get_type_unit_group (cu, attr);
8542 tu_group = sig_type->type_unit_group;
8543
8544 /* If we've already processed this stmt_list there's no real need to
8545 do it again, we could fake it and just recreate the part we need
8546 (file name,index -> symtab mapping). If data shows this optimization
8547 is useful we can do it then. */
8548 first_time = tu_group->primary_symtab == NULL;
8549
8550 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8551 debug info. */
8552 lh = NULL;
8553 if (attr != NULL)
8554 {
8555 line_offset = DW_UNSND (attr);
8556 lh = dwarf_decode_line_header (line_offset, cu);
8557 }
8558 if (lh == NULL)
8559 {
8560 if (first_time)
8561 dwarf2_start_symtab (cu, "", NULL, 0);
8562 else
8563 {
8564 gdb_assert (tu_group->symtabs == NULL);
8565 restart_symtab (0);
8566 }
8567 /* Note: The primary symtab will get allocated at the end. */
8568 return;
8569 }
8570
8571 cu->line_header = lh;
8572 make_cleanup (free_cu_line_header, cu);
8573
8574 if (first_time)
8575 {
8576 dwarf2_start_symtab (cu, "", NULL, 0);
8577
8578 tu_group->num_symtabs = lh->num_file_names;
8579 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
8580
8581 for (i = 0; i < lh->num_file_names; ++i)
8582 {
8583 const char *dir = NULL;
8584 struct file_entry *fe = &lh->file_names[i];
8585
8586 if (fe->dir_index)
8587 dir = lh->include_dirs[fe->dir_index - 1];
8588 dwarf2_start_subfile (fe->name, dir, NULL);
8589
8590 /* Note: We don't have to watch for the main subfile here, type units
8591 don't have DW_AT_name. */
8592
8593 if (current_subfile->symtab == NULL)
8594 {
8595 /* NOTE: start_subfile will recognize when it's been passed
8596 a file it has already seen. So we can't assume there's a
8597 simple mapping from lh->file_names to subfiles,
8598 lh->file_names may contain dups. */
8599 current_subfile->symtab = allocate_symtab (current_subfile->name,
8600 objfile);
8601 }
8602
8603 fe->symtab = current_subfile->symtab;
8604 tu_group->symtabs[i] = fe->symtab;
8605 }
8606 }
8607 else
8608 {
8609 restart_symtab (0);
8610
8611 for (i = 0; i < lh->num_file_names; ++i)
8612 {
8613 struct file_entry *fe = &lh->file_names[i];
8614
8615 fe->symtab = tu_group->symtabs[i];
8616 }
8617 }
8618
8619 /* The main symtab is allocated last. Type units don't have DW_AT_name
8620 so they don't have a "real" (so to speak) symtab anyway.
8621 There is later code that will assign the main symtab to all symbols
8622 that don't have one. We need to handle the case of a symbol with a
8623 missing symtab (DW_AT_decl_file) anyway. */
8624 }
8625
8626 /* Process DW_TAG_type_unit.
8627 For TUs we want to skip the first top level sibling if it's not the
8628 actual type being defined by this TU. In this case the first top
8629 level sibling is there to provide context only. */
8630
8631 static void
8632 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8633 {
8634 struct die_info *child_die;
8635
8636 prepare_one_comp_unit (cu, die, language_minimal);
8637
8638 /* Initialize (or reinitialize) the machinery for building symtabs.
8639 We do this before processing child DIEs, so that the line header table
8640 is available for DW_AT_decl_file. */
8641 setup_type_unit_groups (die, cu);
8642
8643 if (die->child != NULL)
8644 {
8645 child_die = die->child;
8646 while (child_die && child_die->tag)
8647 {
8648 process_die (child_die, cu);
8649 child_die = sibling_die (child_die);
8650 }
8651 }
8652 }
8653 \f
8654 /* DWO/DWP files.
8655
8656 http://gcc.gnu.org/wiki/DebugFission
8657 http://gcc.gnu.org/wiki/DebugFissionDWP
8658
8659 To simplify handling of both DWO files ("object" files with the DWARF info)
8660 and DWP files (a file with the DWOs packaged up into one file), we treat
8661 DWP files as having a collection of virtual DWO files. */
8662
8663 static hashval_t
8664 hash_dwo_file (const void *item)
8665 {
8666 const struct dwo_file *dwo_file = item;
8667 hashval_t hash;
8668
8669 hash = htab_hash_string (dwo_file->dwo_name);
8670 if (dwo_file->comp_dir != NULL)
8671 hash += htab_hash_string (dwo_file->comp_dir);
8672 return hash;
8673 }
8674
8675 static int
8676 eq_dwo_file (const void *item_lhs, const void *item_rhs)
8677 {
8678 const struct dwo_file *lhs = item_lhs;
8679 const struct dwo_file *rhs = item_rhs;
8680
8681 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
8682 return 0;
8683 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
8684 return lhs->comp_dir == rhs->comp_dir;
8685 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
8686 }
8687
8688 /* Allocate a hash table for DWO files. */
8689
8690 static htab_t
8691 allocate_dwo_file_hash_table (void)
8692 {
8693 struct objfile *objfile = dwarf2_per_objfile->objfile;
8694
8695 return htab_create_alloc_ex (41,
8696 hash_dwo_file,
8697 eq_dwo_file,
8698 NULL,
8699 &objfile->objfile_obstack,
8700 hashtab_obstack_allocate,
8701 dummy_obstack_deallocate);
8702 }
8703
8704 /* Lookup DWO file DWO_NAME. */
8705
8706 static void **
8707 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
8708 {
8709 struct dwo_file find_entry;
8710 void **slot;
8711
8712 if (dwarf2_per_objfile->dwo_files == NULL)
8713 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8714
8715 memset (&find_entry, 0, sizeof (find_entry));
8716 find_entry.dwo_name = dwo_name;
8717 find_entry.comp_dir = comp_dir;
8718 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8719
8720 return slot;
8721 }
8722
8723 static hashval_t
8724 hash_dwo_unit (const void *item)
8725 {
8726 const struct dwo_unit *dwo_unit = item;
8727
8728 /* This drops the top 32 bits of the id, but is ok for a hash. */
8729 return dwo_unit->signature;
8730 }
8731
8732 static int
8733 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8734 {
8735 const struct dwo_unit *lhs = item_lhs;
8736 const struct dwo_unit *rhs = item_rhs;
8737
8738 /* The signature is assumed to be unique within the DWO file.
8739 So while object file CU dwo_id's always have the value zero,
8740 that's OK, assuming each object file DWO file has only one CU,
8741 and that's the rule for now. */
8742 return lhs->signature == rhs->signature;
8743 }
8744
8745 /* Allocate a hash table for DWO CUs,TUs.
8746 There is one of these tables for each of CUs,TUs for each DWO file. */
8747
8748 static htab_t
8749 allocate_dwo_unit_table (struct objfile *objfile)
8750 {
8751 /* Start out with a pretty small number.
8752 Generally DWO files contain only one CU and maybe some TUs. */
8753 return htab_create_alloc_ex (3,
8754 hash_dwo_unit,
8755 eq_dwo_unit,
8756 NULL,
8757 &objfile->objfile_obstack,
8758 hashtab_obstack_allocate,
8759 dummy_obstack_deallocate);
8760 }
8761
8762 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
8763
8764 struct create_dwo_cu_data
8765 {
8766 struct dwo_file *dwo_file;
8767 struct dwo_unit dwo_unit;
8768 };
8769
8770 /* die_reader_func for create_dwo_cu. */
8771
8772 static void
8773 create_dwo_cu_reader (const struct die_reader_specs *reader,
8774 const gdb_byte *info_ptr,
8775 struct die_info *comp_unit_die,
8776 int has_children,
8777 void *datap)
8778 {
8779 struct dwarf2_cu *cu = reader->cu;
8780 struct objfile *objfile = dwarf2_per_objfile->objfile;
8781 sect_offset offset = cu->per_cu->offset;
8782 struct dwarf2_section_info *section = cu->per_cu->section;
8783 struct create_dwo_cu_data *data = datap;
8784 struct dwo_file *dwo_file = data->dwo_file;
8785 struct dwo_unit *dwo_unit = &data->dwo_unit;
8786 struct attribute *attr;
8787
8788 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8789 if (attr == NULL)
8790 {
8791 complaint (&symfile_complaints,
8792 _("Dwarf Error: debug entry at offset 0x%x is missing"
8793 " its dwo_id [in module %s]"),
8794 offset.sect_off, dwo_file->dwo_name);
8795 return;
8796 }
8797
8798 dwo_unit->dwo_file = dwo_file;
8799 dwo_unit->signature = DW_UNSND (attr);
8800 dwo_unit->section = section;
8801 dwo_unit->offset = offset;
8802 dwo_unit->length = cu->per_cu->length;
8803
8804 if (dwarf2_read_debug)
8805 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
8806 offset.sect_off, hex_string (dwo_unit->signature));
8807 }
8808
8809 /* Create the dwo_unit for the lone CU in DWO_FILE.
8810 Note: This function processes DWO files only, not DWP files. */
8811
8812 static struct dwo_unit *
8813 create_dwo_cu (struct dwo_file *dwo_file)
8814 {
8815 struct objfile *objfile = dwarf2_per_objfile->objfile;
8816 struct dwarf2_section_info *section = &dwo_file->sections.info;
8817 bfd *abfd;
8818 htab_t cu_htab;
8819 const gdb_byte *info_ptr, *end_ptr;
8820 struct create_dwo_cu_data create_dwo_cu_data;
8821 struct dwo_unit *dwo_unit;
8822
8823 dwarf2_read_section (objfile, section);
8824 info_ptr = section->buffer;
8825
8826 if (info_ptr == NULL)
8827 return NULL;
8828
8829 /* We can't set abfd until now because the section may be empty or
8830 not present, in which case section->asection will be NULL. */
8831 abfd = section->asection->owner;
8832
8833 if (dwarf2_read_debug)
8834 {
8835 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
8836 bfd_section_name (abfd, section->asection),
8837 bfd_get_filename (abfd));
8838 }
8839
8840 create_dwo_cu_data.dwo_file = dwo_file;
8841 dwo_unit = NULL;
8842
8843 end_ptr = info_ptr + section->size;
8844 while (info_ptr < end_ptr)
8845 {
8846 struct dwarf2_per_cu_data per_cu;
8847
8848 memset (&create_dwo_cu_data.dwo_unit, 0,
8849 sizeof (create_dwo_cu_data.dwo_unit));
8850 memset (&per_cu, 0, sizeof (per_cu));
8851 per_cu.objfile = objfile;
8852 per_cu.is_debug_types = 0;
8853 per_cu.offset.sect_off = info_ptr - section->buffer;
8854 per_cu.section = section;
8855
8856 init_cutu_and_read_dies_no_follow (&per_cu,
8857 &dwo_file->sections.abbrev,
8858 dwo_file,
8859 create_dwo_cu_reader,
8860 &create_dwo_cu_data);
8861
8862 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
8863 {
8864 /* If we've already found one, complain. We only support one
8865 because having more than one requires hacking the dwo_name of
8866 each to match, which is highly unlikely to happen. */
8867 if (dwo_unit != NULL)
8868 {
8869 complaint (&symfile_complaints,
8870 _("Multiple CUs in DWO file %s [in module %s]"),
8871 dwo_file->dwo_name, objfile->name);
8872 break;
8873 }
8874
8875 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8876 *dwo_unit = create_dwo_cu_data.dwo_unit;
8877 }
8878
8879 info_ptr += per_cu.length;
8880 }
8881
8882 return dwo_unit;
8883 }
8884
8885 /* DWP file .debug_{cu,tu}_index section format:
8886 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8887
8888 DWP Version 1:
8889
8890 Both index sections have the same format, and serve to map a 64-bit
8891 signature to a set of section numbers. Each section begins with a header,
8892 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8893 indexes, and a pool of 32-bit section numbers. The index sections will be
8894 aligned at 8-byte boundaries in the file.
8895
8896 The index section header consists of:
8897
8898 V, 32 bit version number
8899 -, 32 bits unused
8900 N, 32 bit number of compilation units or type units in the index
8901 M, 32 bit number of slots in the hash table
8902
8903 Numbers are recorded using the byte order of the application binary.
8904
8905 We assume that N and M will not exceed 2^32 - 1.
8906
8907 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8908
8909 The hash table begins at offset 16 in the section, and consists of an array
8910 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8911 order of the application binary). Unused slots in the hash table are 0.
8912 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8913
8914 The parallel table begins immediately after the hash table
8915 (at offset 16 + 8 * M from the beginning of the section), and consists of an
8916 array of 32-bit indexes (using the byte order of the application binary),
8917 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8918 table contains a 32-bit index into the pool of section numbers. For unused
8919 hash table slots, the corresponding entry in the parallel table will be 0.
8920
8921 Given a 64-bit compilation unit signature or a type signature S, an entry
8922 in the hash table is located as follows:
8923
8924 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8925 the low-order k bits all set to 1.
8926
8927 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8928
8929 3) If the hash table entry at index H matches the signature, use that
8930 entry. If the hash table entry at index H is unused (all zeroes),
8931 terminate the search: the signature is not present in the table.
8932
8933 4) Let H = (H + H') modulo M. Repeat at Step 3.
8934
8935 Because M > N and H' and M are relatively prime, the search is guaranteed
8936 to stop at an unused slot or find the match.
8937
8938 The pool of section numbers begins immediately following the hash table
8939 (at offset 16 + 12 * M from the beginning of the section). The pool of
8940 section numbers consists of an array of 32-bit words (using the byte order
8941 of the application binary). Each item in the array is indexed starting
8942 from 0. The hash table entry provides the index of the first section
8943 number in the set. Additional section numbers in the set follow, and the
8944 set is terminated by a 0 entry (section number 0 is not used in ELF).
8945
8946 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8947 section must be the first entry in the set, and the .debug_abbrev.dwo must
8948 be the second entry. Other members of the set may follow in any order. */
8949
8950 /* Create a hash table to map DWO IDs to their CU/TU entry in
8951 .debug_{info,types}.dwo in DWP_FILE.
8952 Returns NULL if there isn't one.
8953 Note: This function processes DWP files only, not DWO files. */
8954
8955 static struct dwp_hash_table *
8956 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8957 {
8958 struct objfile *objfile = dwarf2_per_objfile->objfile;
8959 bfd *dbfd = dwp_file->dbfd;
8960 const gdb_byte *index_ptr, *index_end;
8961 struct dwarf2_section_info *index;
8962 uint32_t version, nr_units, nr_slots;
8963 struct dwp_hash_table *htab;
8964
8965 if (is_debug_types)
8966 index = &dwp_file->sections.tu_index;
8967 else
8968 index = &dwp_file->sections.cu_index;
8969
8970 if (dwarf2_section_empty_p (index))
8971 return NULL;
8972 dwarf2_read_section (objfile, index);
8973
8974 index_ptr = index->buffer;
8975 index_end = index_ptr + index->size;
8976
8977 version = read_4_bytes (dbfd, index_ptr);
8978 index_ptr += 8; /* Skip the unused word. */
8979 nr_units = read_4_bytes (dbfd, index_ptr);
8980 index_ptr += 4;
8981 nr_slots = read_4_bytes (dbfd, index_ptr);
8982 index_ptr += 4;
8983
8984 if (version != 1)
8985 {
8986 error (_("Dwarf Error: unsupported DWP file version (%s)"
8987 " [in module %s]"),
8988 pulongest (version), dwp_file->name);
8989 }
8990 if (nr_slots != (nr_slots & -nr_slots))
8991 {
8992 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
8993 " is not power of 2 [in module %s]"),
8994 pulongest (nr_slots), dwp_file->name);
8995 }
8996
8997 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8998 htab->nr_units = nr_units;
8999 htab->nr_slots = nr_slots;
9000 htab->hash_table = index_ptr;
9001 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
9002 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
9003
9004 return htab;
9005 }
9006
9007 /* Update SECTIONS with the data from SECTP.
9008
9009 This function is like the other "locate" section routines that are
9010 passed to bfd_map_over_sections, but in this context the sections to
9011 read comes from the DWP hash table, not the full ELF section table.
9012
9013 The result is non-zero for success, or zero if an error was found. */
9014
9015 static int
9016 locate_virtual_dwo_sections (asection *sectp,
9017 struct virtual_dwo_sections *sections)
9018 {
9019 const struct dwop_section_names *names = &dwop_section_names;
9020
9021 if (section_is_p (sectp->name, &names->abbrev_dwo))
9022 {
9023 /* There can be only one. */
9024 if (sections->abbrev.asection != NULL)
9025 return 0;
9026 sections->abbrev.asection = sectp;
9027 sections->abbrev.size = bfd_get_section_size (sectp);
9028 }
9029 else if (section_is_p (sectp->name, &names->info_dwo)
9030 || section_is_p (sectp->name, &names->types_dwo))
9031 {
9032 /* There can be only one. */
9033 if (sections->info_or_types.asection != NULL)
9034 return 0;
9035 sections->info_or_types.asection = sectp;
9036 sections->info_or_types.size = bfd_get_section_size (sectp);
9037 }
9038 else if (section_is_p (sectp->name, &names->line_dwo))
9039 {
9040 /* There can be only one. */
9041 if (sections->line.asection != NULL)
9042 return 0;
9043 sections->line.asection = sectp;
9044 sections->line.size = bfd_get_section_size (sectp);
9045 }
9046 else if (section_is_p (sectp->name, &names->loc_dwo))
9047 {
9048 /* There can be only one. */
9049 if (sections->loc.asection != NULL)
9050 return 0;
9051 sections->loc.asection = sectp;
9052 sections->loc.size = bfd_get_section_size (sectp);
9053 }
9054 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9055 {
9056 /* There can be only one. */
9057 if (sections->macinfo.asection != NULL)
9058 return 0;
9059 sections->macinfo.asection = sectp;
9060 sections->macinfo.size = bfd_get_section_size (sectp);
9061 }
9062 else if (section_is_p (sectp->name, &names->macro_dwo))
9063 {
9064 /* There can be only one. */
9065 if (sections->macro.asection != NULL)
9066 return 0;
9067 sections->macro.asection = sectp;
9068 sections->macro.size = bfd_get_section_size (sectp);
9069 }
9070 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9071 {
9072 /* There can be only one. */
9073 if (sections->str_offsets.asection != NULL)
9074 return 0;
9075 sections->str_offsets.asection = sectp;
9076 sections->str_offsets.size = bfd_get_section_size (sectp);
9077 }
9078 else
9079 {
9080 /* No other kind of section is valid. */
9081 return 0;
9082 }
9083
9084 return 1;
9085 }
9086
9087 /* Create a dwo_unit object for the DWO with signature SIGNATURE.
9088 HTAB is the hash table from the DWP file.
9089 SECTION_INDEX is the index of the DWO in HTAB.
9090 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU. */
9091
9092 static struct dwo_unit *
9093 create_dwo_in_dwp (struct dwp_file *dwp_file,
9094 const struct dwp_hash_table *htab,
9095 uint32_t section_index,
9096 const char *comp_dir,
9097 ULONGEST signature, int is_debug_types)
9098 {
9099 struct objfile *objfile = dwarf2_per_objfile->objfile;
9100 bfd *dbfd = dwp_file->dbfd;
9101 const char *kind = is_debug_types ? "TU" : "CU";
9102 struct dwo_file *dwo_file;
9103 struct dwo_unit *dwo_unit;
9104 struct virtual_dwo_sections sections;
9105 void **dwo_file_slot;
9106 char *virtual_dwo_name;
9107 struct dwarf2_section_info *cutu;
9108 struct cleanup *cleanups;
9109 int i;
9110
9111 if (dwarf2_read_debug)
9112 {
9113 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP file: %s\n",
9114 kind,
9115 pulongest (section_index), hex_string (signature),
9116 dwp_file->name);
9117 }
9118
9119 /* Fetch the sections of this DWO.
9120 Put a limit on the number of sections we look for so that bad data
9121 doesn't cause us to loop forever. */
9122
9123 #define MAX_NR_DWO_SECTIONS \
9124 (1 /* .debug_info or .debug_types */ \
9125 + 1 /* .debug_abbrev */ \
9126 + 1 /* .debug_line */ \
9127 + 1 /* .debug_loc */ \
9128 + 1 /* .debug_str_offsets */ \
9129 + 1 /* .debug_macro */ \
9130 + 1 /* .debug_macinfo */ \
9131 + 1 /* trailing zero */)
9132
9133 memset (&sections, 0, sizeof (sections));
9134 cleanups = make_cleanup (null_cleanup, 0);
9135
9136 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
9137 {
9138 asection *sectp;
9139 uint32_t section_nr =
9140 read_4_bytes (dbfd,
9141 htab->section_pool
9142 + (section_index + i) * sizeof (uint32_t));
9143
9144 if (section_nr == 0)
9145 break;
9146 if (section_nr >= dwp_file->num_sections)
9147 {
9148 error (_("Dwarf Error: bad DWP hash table, section number too large"
9149 " [in module %s]"),
9150 dwp_file->name);
9151 }
9152
9153 sectp = dwp_file->elf_sections[section_nr];
9154 if (! locate_virtual_dwo_sections (sectp, &sections))
9155 {
9156 error (_("Dwarf Error: bad DWP hash table, invalid section found"
9157 " [in module %s]"),
9158 dwp_file->name);
9159 }
9160 }
9161
9162 if (i < 2
9163 || sections.info_or_types.asection == NULL
9164 || sections.abbrev.asection == NULL)
9165 {
9166 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
9167 " [in module %s]"),
9168 dwp_file->name);
9169 }
9170 if (i == MAX_NR_DWO_SECTIONS)
9171 {
9172 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
9173 " [in module %s]"),
9174 dwp_file->name);
9175 }
9176
9177 /* It's easier for the rest of the code if we fake a struct dwo_file and
9178 have dwo_unit "live" in that. At least for now.
9179
9180 The DWP file can be made up of a random collection of CUs and TUs.
9181 However, for each CU + set of TUs that came from the same original DWO
9182 file, we want to combine them back into a virtual DWO file to save space
9183 (fewer struct dwo_file objects to allocated). Remember that for really
9184 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
9185
9186 virtual_dwo_name =
9187 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
9188 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
9189 sections.line.asection ? sections.line.asection->id : 0,
9190 sections.loc.asection ? sections.loc.asection->id : 0,
9191 (sections.str_offsets.asection
9192 ? sections.str_offsets.asection->id
9193 : 0));
9194 make_cleanup (xfree, virtual_dwo_name);
9195 /* Can we use an existing virtual DWO file? */
9196 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
9197 /* Create one if necessary. */
9198 if (*dwo_file_slot == NULL)
9199 {
9200 if (dwarf2_read_debug)
9201 {
9202 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
9203 virtual_dwo_name);
9204 }
9205 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
9206 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
9207 virtual_dwo_name,
9208 strlen (virtual_dwo_name));
9209 dwo_file->comp_dir = comp_dir;
9210 dwo_file->sections.abbrev = sections.abbrev;
9211 dwo_file->sections.line = sections.line;
9212 dwo_file->sections.loc = sections.loc;
9213 dwo_file->sections.macinfo = sections.macinfo;
9214 dwo_file->sections.macro = sections.macro;
9215 dwo_file->sections.str_offsets = sections.str_offsets;
9216 /* The "str" section is global to the entire DWP file. */
9217 dwo_file->sections.str = dwp_file->sections.str;
9218 /* The info or types section is assigned later to dwo_unit,
9219 there's no need to record it in dwo_file.
9220 Also, we can't simply record type sections in dwo_file because
9221 we record a pointer into the vector in dwo_unit. As we collect more
9222 types we'll grow the vector and eventually have to reallocate space
9223 for it, invalidating all the pointers into the current copy. */
9224 *dwo_file_slot = dwo_file;
9225 }
9226 else
9227 {
9228 if (dwarf2_read_debug)
9229 {
9230 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
9231 virtual_dwo_name);
9232 }
9233 dwo_file = *dwo_file_slot;
9234 }
9235 do_cleanups (cleanups);
9236
9237 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9238 dwo_unit->dwo_file = dwo_file;
9239 dwo_unit->signature = signature;
9240 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
9241 sizeof (struct dwarf2_section_info));
9242 *dwo_unit->section = sections.info_or_types;
9243 /* offset, length, type_offset_in_tu are set later. */
9244
9245 return dwo_unit;
9246 }
9247
9248 /* Lookup the DWO with SIGNATURE in DWP_FILE. */
9249
9250 static struct dwo_unit *
9251 lookup_dwo_in_dwp (struct dwp_file *dwp_file,
9252 const struct dwp_hash_table *htab,
9253 const char *comp_dir,
9254 ULONGEST signature, int is_debug_types)
9255 {
9256 bfd *dbfd = dwp_file->dbfd;
9257 uint32_t mask = htab->nr_slots - 1;
9258 uint32_t hash = signature & mask;
9259 uint32_t hash2 = ((signature >> 32) & mask) | 1;
9260 unsigned int i;
9261 void **slot;
9262 struct dwo_unit find_dwo_cu, *dwo_cu;
9263
9264 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
9265 find_dwo_cu.signature = signature;
9266 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
9267
9268 if (*slot != NULL)
9269 return *slot;
9270
9271 /* Use a for loop so that we don't loop forever on bad debug info. */
9272 for (i = 0; i < htab->nr_slots; ++i)
9273 {
9274 ULONGEST signature_in_table;
9275
9276 signature_in_table =
9277 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
9278 if (signature_in_table == signature)
9279 {
9280 uint32_t section_index =
9281 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
9282
9283 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
9284 comp_dir, signature, is_debug_types);
9285 return *slot;
9286 }
9287 if (signature_in_table == 0)
9288 return NULL;
9289 hash = (hash + hash2) & mask;
9290 }
9291
9292 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
9293 " [in module %s]"),
9294 dwp_file->name);
9295 }
9296
9297 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
9298 Open the file specified by FILE_NAME and hand it off to BFD for
9299 preliminary analysis. Return a newly initialized bfd *, which
9300 includes a canonicalized copy of FILE_NAME.
9301 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
9302 SEARCH_CWD is true if the current directory is to be searched.
9303 It will be searched before debug-file-directory.
9304 If unable to find/open the file, return NULL.
9305 NOTE: This function is derived from symfile_bfd_open. */
9306
9307 static bfd *
9308 try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
9309 {
9310 bfd *sym_bfd;
9311 int desc, flags;
9312 char *absolute_name;
9313 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
9314 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
9315 to debug_file_directory. */
9316 char *search_path;
9317 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
9318
9319 if (search_cwd)
9320 {
9321 if (*debug_file_directory != '\0')
9322 search_path = concat (".", dirname_separator_string,
9323 debug_file_directory, NULL);
9324 else
9325 search_path = xstrdup (".");
9326 }
9327 else
9328 search_path = xstrdup (debug_file_directory);
9329
9330 flags = 0;
9331 if (is_dwp)
9332 flags |= OPF_SEARCH_IN_PATH;
9333 desc = openp (search_path, flags, file_name,
9334 O_RDONLY | O_BINARY, &absolute_name);
9335 xfree (search_path);
9336 if (desc < 0)
9337 return NULL;
9338
9339 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
9340 xfree (absolute_name);
9341 if (sym_bfd == NULL)
9342 return NULL;
9343 bfd_set_cacheable (sym_bfd, 1);
9344
9345 if (!bfd_check_format (sym_bfd, bfd_object))
9346 {
9347 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
9348 return NULL;
9349 }
9350
9351 return sym_bfd;
9352 }
9353
9354 /* Try to open DWO file FILE_NAME.
9355 COMP_DIR is the DW_AT_comp_dir attribute.
9356 The result is the bfd handle of the file.
9357 If there is a problem finding or opening the file, return NULL.
9358 Upon success, the canonicalized path of the file is stored in the bfd,
9359 same as symfile_bfd_open. */
9360
9361 static bfd *
9362 open_dwo_file (const char *file_name, const char *comp_dir)
9363 {
9364 bfd *abfd;
9365
9366 if (IS_ABSOLUTE_PATH (file_name))
9367 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
9368
9369 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
9370
9371 if (comp_dir != NULL)
9372 {
9373 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
9374
9375 /* NOTE: If comp_dir is a relative path, this will also try the
9376 search path, which seems useful. */
9377 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/, 1 /*search_cwd*/);
9378 xfree (path_to_try);
9379 if (abfd != NULL)
9380 return abfd;
9381 }
9382
9383 /* That didn't work, try debug-file-directory, which, despite its name,
9384 is a list of paths. */
9385
9386 if (*debug_file_directory == '\0')
9387 return NULL;
9388
9389 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
9390 }
9391
9392 /* This function is mapped across the sections and remembers the offset and
9393 size of each of the DWO debugging sections we are interested in. */
9394
9395 static void
9396 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
9397 {
9398 struct dwo_sections *dwo_sections = dwo_sections_ptr;
9399 const struct dwop_section_names *names = &dwop_section_names;
9400
9401 if (section_is_p (sectp->name, &names->abbrev_dwo))
9402 {
9403 dwo_sections->abbrev.asection = sectp;
9404 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
9405 }
9406 else if (section_is_p (sectp->name, &names->info_dwo))
9407 {
9408 dwo_sections->info.asection = sectp;
9409 dwo_sections->info.size = bfd_get_section_size (sectp);
9410 }
9411 else if (section_is_p (sectp->name, &names->line_dwo))
9412 {
9413 dwo_sections->line.asection = sectp;
9414 dwo_sections->line.size = bfd_get_section_size (sectp);
9415 }
9416 else if (section_is_p (sectp->name, &names->loc_dwo))
9417 {
9418 dwo_sections->loc.asection = sectp;
9419 dwo_sections->loc.size = bfd_get_section_size (sectp);
9420 }
9421 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9422 {
9423 dwo_sections->macinfo.asection = sectp;
9424 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
9425 }
9426 else if (section_is_p (sectp->name, &names->macro_dwo))
9427 {
9428 dwo_sections->macro.asection = sectp;
9429 dwo_sections->macro.size = bfd_get_section_size (sectp);
9430 }
9431 else if (section_is_p (sectp->name, &names->str_dwo))
9432 {
9433 dwo_sections->str.asection = sectp;
9434 dwo_sections->str.size = bfd_get_section_size (sectp);
9435 }
9436 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9437 {
9438 dwo_sections->str_offsets.asection = sectp;
9439 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
9440 }
9441 else if (section_is_p (sectp->name, &names->types_dwo))
9442 {
9443 struct dwarf2_section_info type_section;
9444
9445 memset (&type_section, 0, sizeof (type_section));
9446 type_section.asection = sectp;
9447 type_section.size = bfd_get_section_size (sectp);
9448 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
9449 &type_section);
9450 }
9451 }
9452
9453 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
9454 by PER_CU. This is for the non-DWP case.
9455 The result is NULL if DWO_NAME can't be found. */
9456
9457 static struct dwo_file *
9458 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
9459 const char *dwo_name, const char *comp_dir)
9460 {
9461 struct objfile *objfile = dwarf2_per_objfile->objfile;
9462 struct dwo_file *dwo_file;
9463 bfd *dbfd;
9464 struct cleanup *cleanups;
9465
9466 dbfd = open_dwo_file (dwo_name, comp_dir);
9467 if (dbfd == NULL)
9468 {
9469 if (dwarf2_read_debug)
9470 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
9471 return NULL;
9472 }
9473 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
9474 dwo_file->dwo_name = dwo_name;
9475 dwo_file->comp_dir = comp_dir;
9476 dwo_file->dbfd = dbfd;
9477
9478 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
9479
9480 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
9481
9482 dwo_file->cu = create_dwo_cu (dwo_file);
9483
9484 dwo_file->tus = create_debug_types_hash_table (dwo_file,
9485 dwo_file->sections.types);
9486
9487 discard_cleanups (cleanups);
9488
9489 if (dwarf2_read_debug)
9490 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9491
9492 return dwo_file;
9493 }
9494
9495 /* This function is mapped across the sections and remembers the offset and
9496 size of each of the DWP debugging sections we are interested in. */
9497
9498 static void
9499 dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
9500 {
9501 struct dwp_file *dwp_file = dwp_file_ptr;
9502 const struct dwop_section_names *names = &dwop_section_names;
9503 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9504
9505 /* Record the ELF section number for later lookup: this is what the
9506 .debug_cu_index,.debug_tu_index tables use. */
9507 gdb_assert (elf_section_nr < dwp_file->num_sections);
9508 dwp_file->elf_sections[elf_section_nr] = sectp;
9509
9510 /* Look for specific sections that we need. */
9511 if (section_is_p (sectp->name, &names->str_dwo))
9512 {
9513 dwp_file->sections.str.asection = sectp;
9514 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9515 }
9516 else if (section_is_p (sectp->name, &names->cu_index))
9517 {
9518 dwp_file->sections.cu_index.asection = sectp;
9519 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9520 }
9521 else if (section_is_p (sectp->name, &names->tu_index))
9522 {
9523 dwp_file->sections.tu_index.asection = sectp;
9524 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9525 }
9526 }
9527
9528 /* Hash function for dwp_file loaded CUs/TUs. */
9529
9530 static hashval_t
9531 hash_dwp_loaded_cutus (const void *item)
9532 {
9533 const struct dwo_unit *dwo_unit = item;
9534
9535 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9536 return dwo_unit->signature;
9537 }
9538
9539 /* Equality function for dwp_file loaded CUs/TUs. */
9540
9541 static int
9542 eq_dwp_loaded_cutus (const void *a, const void *b)
9543 {
9544 const struct dwo_unit *dua = a;
9545 const struct dwo_unit *dub = b;
9546
9547 return dua->signature == dub->signature;
9548 }
9549
9550 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
9551
9552 static htab_t
9553 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9554 {
9555 return htab_create_alloc_ex (3,
9556 hash_dwp_loaded_cutus,
9557 eq_dwp_loaded_cutus,
9558 NULL,
9559 &objfile->objfile_obstack,
9560 hashtab_obstack_allocate,
9561 dummy_obstack_deallocate);
9562 }
9563
9564 /* Try to open DWP file FILE_NAME.
9565 The result is the bfd handle of the file.
9566 If there is a problem finding or opening the file, return NULL.
9567 Upon success, the canonicalized path of the file is stored in the bfd,
9568 same as symfile_bfd_open. */
9569
9570 static bfd *
9571 open_dwp_file (const char *file_name)
9572 {
9573 bfd *abfd;
9574
9575 abfd = try_open_dwop_file (file_name, 1 /*is_dwp*/, 1 /*search_cwd*/);
9576 if (abfd != NULL)
9577 return abfd;
9578
9579 /* Work around upstream bug 15652.
9580 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
9581 [Whether that's a "bug" is debatable, but it is getting in our way.]
9582 We have no real idea where the dwp file is, because gdb's realpath-ing
9583 of the executable's path may have discarded the needed info.
9584 [IWBN if the dwp file name was recorded in the executable, akin to
9585 .gnu_debuglink, but that doesn't exist yet.]
9586 Strip the directory from FILE_NAME and search again. */
9587 if (*debug_file_directory != '\0')
9588 {
9589 /* Don't implicitly search the current directory here.
9590 If the user wants to search "." to handle this case,
9591 it must be added to debug-file-directory. */
9592 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
9593 0 /*search_cwd*/);
9594 }
9595
9596 return NULL;
9597 }
9598
9599 /* Initialize the use of the DWP file for the current objfile.
9600 By convention the name of the DWP file is ${objfile}.dwp.
9601 The result is NULL if it can't be found. */
9602
9603 static struct dwp_file *
9604 open_and_init_dwp_file (void)
9605 {
9606 struct objfile *objfile = dwarf2_per_objfile->objfile;
9607 struct dwp_file *dwp_file;
9608 char *dwp_name;
9609 bfd *dbfd;
9610 struct cleanup *cleanups;
9611
9612 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
9613 cleanups = make_cleanup (xfree, dwp_name);
9614
9615 dbfd = open_dwp_file (dwp_name);
9616 if (dbfd == NULL)
9617 {
9618 if (dwarf2_read_debug)
9619 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9620 do_cleanups (cleanups);
9621 return NULL;
9622 }
9623 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9624 dwp_file->name = bfd_get_filename (dbfd);
9625 dwp_file->dbfd = dbfd;
9626 do_cleanups (cleanups);
9627
9628 /* +1: section 0 is unused */
9629 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9630 dwp_file->elf_sections =
9631 OBSTACK_CALLOC (&objfile->objfile_obstack,
9632 dwp_file->num_sections, asection *);
9633
9634 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9635
9636 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9637
9638 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9639
9640 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9641
9642 if (dwarf2_read_debug)
9643 {
9644 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9645 fprintf_unfiltered (gdb_stdlog,
9646 " %s CUs, %s TUs\n",
9647 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
9648 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
9649 }
9650
9651 return dwp_file;
9652 }
9653
9654 /* Wrapper around open_and_init_dwp_file, only open it once. */
9655
9656 static struct dwp_file *
9657 get_dwp_file (void)
9658 {
9659 if (! dwarf2_per_objfile->dwp_checked)
9660 {
9661 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
9662 dwarf2_per_objfile->dwp_checked = 1;
9663 }
9664 return dwarf2_per_objfile->dwp_file;
9665 }
9666
9667 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9668 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9669 or in the DWP file for the objfile, referenced by THIS_UNIT.
9670 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9671 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9672
9673 This is called, for example, when wanting to read a variable with a
9674 complex location. Therefore we don't want to do file i/o for every call.
9675 Therefore we don't want to look for a DWO file on every call.
9676 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9677 then we check if we've already seen DWO_NAME, and only THEN do we check
9678 for a DWO file.
9679
9680 The result is a pointer to the dwo_unit object or NULL if we didn't find it
9681 (dwo_id mismatch or couldn't find the DWO/DWP file). */
9682
9683 static struct dwo_unit *
9684 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9685 const char *dwo_name, const char *comp_dir,
9686 ULONGEST signature, int is_debug_types)
9687 {
9688 struct objfile *objfile = dwarf2_per_objfile->objfile;
9689 const char *kind = is_debug_types ? "TU" : "CU";
9690 void **dwo_file_slot;
9691 struct dwo_file *dwo_file;
9692 struct dwp_file *dwp_file;
9693
9694 /* First see if there's a DWP file.
9695 If we have a DWP file but didn't find the DWO inside it, don't
9696 look for the original DWO file. It makes gdb behave differently
9697 depending on whether one is debugging in the build tree. */
9698
9699 dwp_file = get_dwp_file ();
9700 if (dwp_file != NULL)
9701 {
9702 const struct dwp_hash_table *dwp_htab =
9703 is_debug_types ? dwp_file->tus : dwp_file->cus;
9704
9705 if (dwp_htab != NULL)
9706 {
9707 struct dwo_unit *dwo_cutu =
9708 lookup_dwo_in_dwp (dwp_file, dwp_htab, comp_dir,
9709 signature, is_debug_types);
9710
9711 if (dwo_cutu != NULL)
9712 {
9713 if (dwarf2_read_debug)
9714 {
9715 fprintf_unfiltered (gdb_stdlog,
9716 "Virtual DWO %s %s found: @%s\n",
9717 kind, hex_string (signature),
9718 host_address_to_string (dwo_cutu));
9719 }
9720 return dwo_cutu;
9721 }
9722 }
9723 }
9724 else
9725 {
9726 /* No DWP file, look for the DWO file. */
9727
9728 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
9729 if (*dwo_file_slot == NULL)
9730 {
9731 /* Read in the file and build a table of the CUs/TUs it contains. */
9732 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
9733 }
9734 /* NOTE: This will be NULL if unable to open the file. */
9735 dwo_file = *dwo_file_slot;
9736
9737 if (dwo_file != NULL)
9738 {
9739 struct dwo_unit *dwo_cutu = NULL;
9740
9741 if (is_debug_types && dwo_file->tus)
9742 {
9743 struct dwo_unit find_dwo_cutu;
9744
9745 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9746 find_dwo_cutu.signature = signature;
9747 dwo_cutu = htab_find (dwo_file->tus, &find_dwo_cutu);
9748 }
9749 else if (!is_debug_types && dwo_file->cu)
9750 {
9751 if (signature == dwo_file->cu->signature)
9752 dwo_cutu = dwo_file->cu;
9753 }
9754
9755 if (dwo_cutu != NULL)
9756 {
9757 if (dwarf2_read_debug)
9758 {
9759 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9760 kind, dwo_name, hex_string (signature),
9761 host_address_to_string (dwo_cutu));
9762 }
9763 return dwo_cutu;
9764 }
9765 }
9766 }
9767
9768 /* We didn't find it. This could mean a dwo_id mismatch, or
9769 someone deleted the DWO/DWP file, or the search path isn't set up
9770 correctly to find the file. */
9771
9772 if (dwarf2_read_debug)
9773 {
9774 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9775 kind, dwo_name, hex_string (signature));
9776 }
9777
9778 /* This is a warning and not a complaint because it can be caused by
9779 pilot error (e.g., user accidentally deleting the DWO). */
9780 warning (_("Could not find DWO %s %s(%s) referenced by %s at offset 0x%x"
9781 " [in module %s]"),
9782 kind, dwo_name, hex_string (signature),
9783 this_unit->is_debug_types ? "TU" : "CU",
9784 this_unit->offset.sect_off, objfile->name);
9785 return NULL;
9786 }
9787
9788 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9789 See lookup_dwo_cutu_unit for details. */
9790
9791 static struct dwo_unit *
9792 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9793 const char *dwo_name, const char *comp_dir,
9794 ULONGEST signature)
9795 {
9796 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9797 }
9798
9799 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9800 See lookup_dwo_cutu_unit for details. */
9801
9802 static struct dwo_unit *
9803 lookup_dwo_type_unit (struct signatured_type *this_tu,
9804 const char *dwo_name, const char *comp_dir)
9805 {
9806 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9807 }
9808
9809 /* Free all resources associated with DWO_FILE.
9810 Close the DWO file and munmap the sections.
9811 All memory should be on the objfile obstack. */
9812
9813 static void
9814 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
9815 {
9816 int ix;
9817 struct dwarf2_section_info *section;
9818
9819 /* Note: dbfd is NULL for virtual DWO files. */
9820 gdb_bfd_unref (dwo_file->dbfd);
9821
9822 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9823 }
9824
9825 /* Wrapper for free_dwo_file for use in cleanups. */
9826
9827 static void
9828 free_dwo_file_cleanup (void *arg)
9829 {
9830 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9831 struct objfile *objfile = dwarf2_per_objfile->objfile;
9832
9833 free_dwo_file (dwo_file, objfile);
9834 }
9835
9836 /* Traversal function for free_dwo_files. */
9837
9838 static int
9839 free_dwo_file_from_slot (void **slot, void *info)
9840 {
9841 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9842 struct objfile *objfile = (struct objfile *) info;
9843
9844 free_dwo_file (dwo_file, objfile);
9845
9846 return 1;
9847 }
9848
9849 /* Free all resources associated with DWO_FILES. */
9850
9851 static void
9852 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9853 {
9854 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
9855 }
9856 \f
9857 /* Read in various DIEs. */
9858
9859 /* qsort helper for inherit_abstract_dies. */
9860
9861 static int
9862 unsigned_int_compar (const void *ap, const void *bp)
9863 {
9864 unsigned int a = *(unsigned int *) ap;
9865 unsigned int b = *(unsigned int *) bp;
9866
9867 return (a > b) - (b > a);
9868 }
9869
9870 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9871 Inherit only the children of the DW_AT_abstract_origin DIE not being
9872 already referenced by DW_AT_abstract_origin from the children of the
9873 current DIE. */
9874
9875 static void
9876 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9877 {
9878 struct die_info *child_die;
9879 unsigned die_children_count;
9880 /* CU offsets which were referenced by children of the current DIE. */
9881 sect_offset *offsets;
9882 sect_offset *offsets_end, *offsetp;
9883 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9884 struct die_info *origin_die;
9885 /* Iterator of the ORIGIN_DIE children. */
9886 struct die_info *origin_child_die;
9887 struct cleanup *cleanups;
9888 struct attribute *attr;
9889 struct dwarf2_cu *origin_cu;
9890 struct pending **origin_previous_list_in_scope;
9891
9892 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9893 if (!attr)
9894 return;
9895
9896 /* Note that following die references may follow to a die in a
9897 different cu. */
9898
9899 origin_cu = cu;
9900 origin_die = follow_die_ref (die, attr, &origin_cu);
9901
9902 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9903 symbols in. */
9904 origin_previous_list_in_scope = origin_cu->list_in_scope;
9905 origin_cu->list_in_scope = cu->list_in_scope;
9906
9907 if (die->tag != origin_die->tag
9908 && !(die->tag == DW_TAG_inlined_subroutine
9909 && origin_die->tag == DW_TAG_subprogram))
9910 complaint (&symfile_complaints,
9911 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9912 die->offset.sect_off, origin_die->offset.sect_off);
9913
9914 child_die = die->child;
9915 die_children_count = 0;
9916 while (child_die && child_die->tag)
9917 {
9918 child_die = sibling_die (child_die);
9919 die_children_count++;
9920 }
9921 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9922 cleanups = make_cleanup (xfree, offsets);
9923
9924 offsets_end = offsets;
9925 child_die = die->child;
9926 while (child_die && child_die->tag)
9927 {
9928 /* For each CHILD_DIE, find the corresponding child of
9929 ORIGIN_DIE. If there is more than one layer of
9930 DW_AT_abstract_origin, follow them all; there shouldn't be,
9931 but GCC versions at least through 4.4 generate this (GCC PR
9932 40573). */
9933 struct die_info *child_origin_die = child_die;
9934 struct dwarf2_cu *child_origin_cu = cu;
9935
9936 while (1)
9937 {
9938 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9939 child_origin_cu);
9940 if (attr == NULL)
9941 break;
9942 child_origin_die = follow_die_ref (child_origin_die, attr,
9943 &child_origin_cu);
9944 }
9945
9946 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9947 counterpart may exist. */
9948 if (child_origin_die != child_die)
9949 {
9950 if (child_die->tag != child_origin_die->tag
9951 && !(child_die->tag == DW_TAG_inlined_subroutine
9952 && child_origin_die->tag == DW_TAG_subprogram))
9953 complaint (&symfile_complaints,
9954 _("Child DIE 0x%x and its abstract origin 0x%x have "
9955 "different tags"), child_die->offset.sect_off,
9956 child_origin_die->offset.sect_off);
9957 if (child_origin_die->parent != origin_die)
9958 complaint (&symfile_complaints,
9959 _("Child DIE 0x%x and its abstract origin 0x%x have "
9960 "different parents"), child_die->offset.sect_off,
9961 child_origin_die->offset.sect_off);
9962 else
9963 *offsets_end++ = child_origin_die->offset;
9964 }
9965 child_die = sibling_die (child_die);
9966 }
9967 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9968 unsigned_int_compar);
9969 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
9970 if (offsetp[-1].sect_off == offsetp->sect_off)
9971 complaint (&symfile_complaints,
9972 _("Multiple children of DIE 0x%x refer "
9973 "to DIE 0x%x as their abstract origin"),
9974 die->offset.sect_off, offsetp->sect_off);
9975
9976 offsetp = offsets;
9977 origin_child_die = origin_die->child;
9978 while (origin_child_die && origin_child_die->tag)
9979 {
9980 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
9981 while (offsetp < offsets_end
9982 && offsetp->sect_off < origin_child_die->offset.sect_off)
9983 offsetp++;
9984 if (offsetp >= offsets_end
9985 || offsetp->sect_off > origin_child_die->offset.sect_off)
9986 {
9987 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
9988 process_die (origin_child_die, origin_cu);
9989 }
9990 origin_child_die = sibling_die (origin_child_die);
9991 }
9992 origin_cu->list_in_scope = origin_previous_list_in_scope;
9993
9994 do_cleanups (cleanups);
9995 }
9996
9997 static void
9998 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
9999 {
10000 struct objfile *objfile = cu->objfile;
10001 struct context_stack *new;
10002 CORE_ADDR lowpc;
10003 CORE_ADDR highpc;
10004 struct die_info *child_die;
10005 struct attribute *attr, *call_line, *call_file;
10006 const char *name;
10007 CORE_ADDR baseaddr;
10008 struct block *block;
10009 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
10010 VEC (symbolp) *template_args = NULL;
10011 struct template_symbol *templ_func = NULL;
10012
10013 if (inlined_func)
10014 {
10015 /* If we do not have call site information, we can't show the
10016 caller of this inlined function. That's too confusing, so
10017 only use the scope for local variables. */
10018 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
10019 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
10020 if (call_line == NULL || call_file == NULL)
10021 {
10022 read_lexical_block_scope (die, cu);
10023 return;
10024 }
10025 }
10026
10027 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10028
10029 name = dwarf2_name (die, cu);
10030
10031 /* Ignore functions with missing or empty names. These are actually
10032 illegal according to the DWARF standard. */
10033 if (name == NULL)
10034 {
10035 complaint (&symfile_complaints,
10036 _("missing name for subprogram DIE at %d"),
10037 die->offset.sect_off);
10038 return;
10039 }
10040
10041 /* Ignore functions with missing or invalid low and high pc attributes. */
10042 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
10043 {
10044 attr = dwarf2_attr (die, DW_AT_external, cu);
10045 if (!attr || !DW_UNSND (attr))
10046 complaint (&symfile_complaints,
10047 _("cannot get low and high bounds "
10048 "for subprogram DIE at %d"),
10049 die->offset.sect_off);
10050 return;
10051 }
10052
10053 lowpc += baseaddr;
10054 highpc += baseaddr;
10055
10056 /* If we have any template arguments, then we must allocate a
10057 different sort of symbol. */
10058 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
10059 {
10060 if (child_die->tag == DW_TAG_template_type_param
10061 || child_die->tag == DW_TAG_template_value_param)
10062 {
10063 templ_func = allocate_template_symbol (objfile);
10064 templ_func->base.is_cplus_template_function = 1;
10065 break;
10066 }
10067 }
10068
10069 new = push_context (0, lowpc);
10070 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
10071 (struct symbol *) templ_func);
10072
10073 /* If there is a location expression for DW_AT_frame_base, record
10074 it. */
10075 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
10076 if (attr)
10077 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
10078
10079 cu->list_in_scope = &local_symbols;
10080
10081 if (die->child != NULL)
10082 {
10083 child_die = die->child;
10084 while (child_die && child_die->tag)
10085 {
10086 if (child_die->tag == DW_TAG_template_type_param
10087 || child_die->tag == DW_TAG_template_value_param)
10088 {
10089 struct symbol *arg = new_symbol (child_die, NULL, cu);
10090
10091 if (arg != NULL)
10092 VEC_safe_push (symbolp, template_args, arg);
10093 }
10094 else
10095 process_die (child_die, cu);
10096 child_die = sibling_die (child_die);
10097 }
10098 }
10099
10100 inherit_abstract_dies (die, cu);
10101
10102 /* If we have a DW_AT_specification, we might need to import using
10103 directives from the context of the specification DIE. See the
10104 comment in determine_prefix. */
10105 if (cu->language == language_cplus
10106 && dwarf2_attr (die, DW_AT_specification, cu))
10107 {
10108 struct dwarf2_cu *spec_cu = cu;
10109 struct die_info *spec_die = die_specification (die, &spec_cu);
10110
10111 while (spec_die)
10112 {
10113 child_die = spec_die->child;
10114 while (child_die && child_die->tag)
10115 {
10116 if (child_die->tag == DW_TAG_imported_module)
10117 process_die (child_die, spec_cu);
10118 child_die = sibling_die (child_die);
10119 }
10120
10121 /* In some cases, GCC generates specification DIEs that
10122 themselves contain DW_AT_specification attributes. */
10123 spec_die = die_specification (spec_die, &spec_cu);
10124 }
10125 }
10126
10127 new = pop_context ();
10128 /* Make a block for the local symbols within. */
10129 block = finish_block (new->name, &local_symbols, new->old_blocks,
10130 lowpc, highpc, objfile);
10131
10132 /* For C++, set the block's scope. */
10133 if ((cu->language == language_cplus || cu->language == language_fortran)
10134 && cu->processing_has_namespace_info)
10135 block_set_scope (block, determine_prefix (die, cu),
10136 &objfile->objfile_obstack);
10137
10138 /* If we have address ranges, record them. */
10139 dwarf2_record_block_ranges (die, block, baseaddr, cu);
10140
10141 /* Attach template arguments to function. */
10142 if (! VEC_empty (symbolp, template_args))
10143 {
10144 gdb_assert (templ_func != NULL);
10145
10146 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
10147 templ_func->template_arguments
10148 = obstack_alloc (&objfile->objfile_obstack,
10149 (templ_func->n_template_arguments
10150 * sizeof (struct symbol *)));
10151 memcpy (templ_func->template_arguments,
10152 VEC_address (symbolp, template_args),
10153 (templ_func->n_template_arguments * sizeof (struct symbol *)));
10154 VEC_free (symbolp, template_args);
10155 }
10156
10157 /* In C++, we can have functions nested inside functions (e.g., when
10158 a function declares a class that has methods). This means that
10159 when we finish processing a function scope, we may need to go
10160 back to building a containing block's symbol lists. */
10161 local_symbols = new->locals;
10162 using_directives = new->using_directives;
10163
10164 /* If we've finished processing a top-level function, subsequent
10165 symbols go in the file symbol list. */
10166 if (outermost_context_p ())
10167 cu->list_in_scope = &file_symbols;
10168 }
10169
10170 /* Process all the DIES contained within a lexical block scope. Start
10171 a new scope, process the dies, and then close the scope. */
10172
10173 static void
10174 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
10175 {
10176 struct objfile *objfile = cu->objfile;
10177 struct context_stack *new;
10178 CORE_ADDR lowpc, highpc;
10179 struct die_info *child_die;
10180 CORE_ADDR baseaddr;
10181
10182 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10183
10184 /* Ignore blocks with missing or invalid low and high pc attributes. */
10185 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
10186 as multiple lexical blocks? Handling children in a sane way would
10187 be nasty. Might be easier to properly extend generic blocks to
10188 describe ranges. */
10189 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
10190 return;
10191 lowpc += baseaddr;
10192 highpc += baseaddr;
10193
10194 push_context (0, lowpc);
10195 if (die->child != NULL)
10196 {
10197 child_die = die->child;
10198 while (child_die && child_die->tag)
10199 {
10200 process_die (child_die, cu);
10201 child_die = sibling_die (child_die);
10202 }
10203 }
10204 new = pop_context ();
10205
10206 if (local_symbols != NULL || using_directives != NULL)
10207 {
10208 struct block *block
10209 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
10210 highpc, objfile);
10211
10212 /* Note that recording ranges after traversing children, as we
10213 do here, means that recording a parent's ranges entails
10214 walking across all its children's ranges as they appear in
10215 the address map, which is quadratic behavior.
10216
10217 It would be nicer to record the parent's ranges before
10218 traversing its children, simply overriding whatever you find
10219 there. But since we don't even decide whether to create a
10220 block until after we've traversed its children, that's hard
10221 to do. */
10222 dwarf2_record_block_ranges (die, block, baseaddr, cu);
10223 }
10224 local_symbols = new->locals;
10225 using_directives = new->using_directives;
10226 }
10227
10228 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
10229
10230 static void
10231 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
10232 {
10233 struct objfile *objfile = cu->objfile;
10234 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10235 CORE_ADDR pc, baseaddr;
10236 struct attribute *attr;
10237 struct call_site *call_site, call_site_local;
10238 void **slot;
10239 int nparams;
10240 struct die_info *child_die;
10241
10242 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10243
10244 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10245 if (!attr)
10246 {
10247 complaint (&symfile_complaints,
10248 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
10249 "DIE 0x%x [in module %s]"),
10250 die->offset.sect_off, objfile->name);
10251 return;
10252 }
10253 pc = DW_ADDR (attr) + baseaddr;
10254
10255 if (cu->call_site_htab == NULL)
10256 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
10257 NULL, &objfile->objfile_obstack,
10258 hashtab_obstack_allocate, NULL);
10259 call_site_local.pc = pc;
10260 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
10261 if (*slot != NULL)
10262 {
10263 complaint (&symfile_complaints,
10264 _("Duplicate PC %s for DW_TAG_GNU_call_site "
10265 "DIE 0x%x [in module %s]"),
10266 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
10267 return;
10268 }
10269
10270 /* Count parameters at the caller. */
10271
10272 nparams = 0;
10273 for (child_die = die->child; child_die && child_die->tag;
10274 child_die = sibling_die (child_die))
10275 {
10276 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10277 {
10278 complaint (&symfile_complaints,
10279 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
10280 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10281 child_die->tag, child_die->offset.sect_off, objfile->name);
10282 continue;
10283 }
10284
10285 nparams++;
10286 }
10287
10288 call_site = obstack_alloc (&objfile->objfile_obstack,
10289 (sizeof (*call_site)
10290 + (sizeof (*call_site->parameter)
10291 * (nparams - 1))));
10292 *slot = call_site;
10293 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
10294 call_site->pc = pc;
10295
10296 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
10297 {
10298 struct die_info *func_die;
10299
10300 /* Skip also over DW_TAG_inlined_subroutine. */
10301 for (func_die = die->parent;
10302 func_die && func_die->tag != DW_TAG_subprogram
10303 && func_die->tag != DW_TAG_subroutine_type;
10304 func_die = func_die->parent);
10305
10306 /* DW_AT_GNU_all_call_sites is a superset
10307 of DW_AT_GNU_all_tail_call_sites. */
10308 if (func_die
10309 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
10310 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
10311 {
10312 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
10313 not complete. But keep CALL_SITE for look ups via call_site_htab,
10314 both the initial caller containing the real return address PC and
10315 the final callee containing the current PC of a chain of tail
10316 calls do not need to have the tail call list complete. But any
10317 function candidate for a virtual tail call frame searched via
10318 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
10319 determined unambiguously. */
10320 }
10321 else
10322 {
10323 struct type *func_type = NULL;
10324
10325 if (func_die)
10326 func_type = get_die_type (func_die, cu);
10327 if (func_type != NULL)
10328 {
10329 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
10330
10331 /* Enlist this call site to the function. */
10332 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
10333 TYPE_TAIL_CALL_LIST (func_type) = call_site;
10334 }
10335 else
10336 complaint (&symfile_complaints,
10337 _("Cannot find function owning DW_TAG_GNU_call_site "
10338 "DIE 0x%x [in module %s]"),
10339 die->offset.sect_off, objfile->name);
10340 }
10341 }
10342
10343 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
10344 if (attr == NULL)
10345 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
10346 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
10347 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
10348 /* Keep NULL DWARF_BLOCK. */;
10349 else if (attr_form_is_block (attr))
10350 {
10351 struct dwarf2_locexpr_baton *dlbaton;
10352
10353 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
10354 dlbaton->data = DW_BLOCK (attr)->data;
10355 dlbaton->size = DW_BLOCK (attr)->size;
10356 dlbaton->per_cu = cu->per_cu;
10357
10358 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
10359 }
10360 else if (attr_form_is_ref (attr))
10361 {
10362 struct dwarf2_cu *target_cu = cu;
10363 struct die_info *target_die;
10364
10365 target_die = follow_die_ref (die, attr, &target_cu);
10366 gdb_assert (target_cu->objfile == objfile);
10367 if (die_is_declaration (target_die, target_cu))
10368 {
10369 const char *target_physname = NULL;
10370 struct attribute *target_attr;
10371
10372 /* Prefer the mangled name; otherwise compute the demangled one. */
10373 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
10374 if (target_attr == NULL)
10375 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
10376 target_cu);
10377 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
10378 target_physname = DW_STRING (target_attr);
10379 else
10380 target_physname = dwarf2_physname (NULL, target_die, target_cu);
10381 if (target_physname == NULL)
10382 complaint (&symfile_complaints,
10383 _("DW_AT_GNU_call_site_target target DIE has invalid "
10384 "physname, for referencing DIE 0x%x [in module %s]"),
10385 die->offset.sect_off, objfile->name);
10386 else
10387 SET_FIELD_PHYSNAME (call_site->target, target_physname);
10388 }
10389 else
10390 {
10391 CORE_ADDR lowpc;
10392
10393 /* DW_AT_entry_pc should be preferred. */
10394 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
10395 complaint (&symfile_complaints,
10396 _("DW_AT_GNU_call_site_target target DIE has invalid "
10397 "low pc, for referencing DIE 0x%x [in module %s]"),
10398 die->offset.sect_off, objfile->name);
10399 else
10400 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
10401 }
10402 }
10403 else
10404 complaint (&symfile_complaints,
10405 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
10406 "block nor reference, for DIE 0x%x [in module %s]"),
10407 die->offset.sect_off, objfile->name);
10408
10409 call_site->per_cu = cu->per_cu;
10410
10411 for (child_die = die->child;
10412 child_die && child_die->tag;
10413 child_die = sibling_die (child_die))
10414 {
10415 struct call_site_parameter *parameter;
10416 struct attribute *loc, *origin;
10417
10418 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10419 {
10420 /* Already printed the complaint above. */
10421 continue;
10422 }
10423
10424 gdb_assert (call_site->parameter_count < nparams);
10425 parameter = &call_site->parameter[call_site->parameter_count];
10426
10427 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10428 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10429 register is contained in DW_AT_GNU_call_site_value. */
10430
10431 loc = dwarf2_attr (child_die, DW_AT_location, cu);
10432 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
10433 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
10434 {
10435 sect_offset offset;
10436
10437 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
10438 offset = dwarf2_get_ref_die_offset (origin);
10439 if (!offset_in_cu_p (&cu->header, offset))
10440 {
10441 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10442 binding can be done only inside one CU. Such referenced DIE
10443 therefore cannot be even moved to DW_TAG_partial_unit. */
10444 complaint (&symfile_complaints,
10445 _("DW_AT_abstract_origin offset is not in CU for "
10446 "DW_TAG_GNU_call_site child DIE 0x%x "
10447 "[in module %s]"),
10448 child_die->offset.sect_off, objfile->name);
10449 continue;
10450 }
10451 parameter->u.param_offset.cu_off = (offset.sect_off
10452 - cu->header.offset.sect_off);
10453 }
10454 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
10455 {
10456 complaint (&symfile_complaints,
10457 _("No DW_FORM_block* DW_AT_location for "
10458 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10459 child_die->offset.sect_off, objfile->name);
10460 continue;
10461 }
10462 else
10463 {
10464 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10465 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
10466 if (parameter->u.dwarf_reg != -1)
10467 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10468 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
10469 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
10470 &parameter->u.fb_offset))
10471 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10472 else
10473 {
10474 complaint (&symfile_complaints,
10475 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
10476 "for DW_FORM_block* DW_AT_location is supported for "
10477 "DW_TAG_GNU_call_site child DIE 0x%x "
10478 "[in module %s]"),
10479 child_die->offset.sect_off, objfile->name);
10480 continue;
10481 }
10482 }
10483
10484 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10485 if (!attr_form_is_block (attr))
10486 {
10487 complaint (&symfile_complaints,
10488 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
10489 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10490 child_die->offset.sect_off, objfile->name);
10491 continue;
10492 }
10493 parameter->value = DW_BLOCK (attr)->data;
10494 parameter->value_size = DW_BLOCK (attr)->size;
10495
10496 /* Parameters are not pre-cleared by memset above. */
10497 parameter->data_value = NULL;
10498 parameter->data_value_size = 0;
10499 call_site->parameter_count++;
10500
10501 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10502 if (attr)
10503 {
10504 if (!attr_form_is_block (attr))
10505 complaint (&symfile_complaints,
10506 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
10507 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10508 child_die->offset.sect_off, objfile->name);
10509 else
10510 {
10511 parameter->data_value = DW_BLOCK (attr)->data;
10512 parameter->data_value_size = DW_BLOCK (attr)->size;
10513 }
10514 }
10515 }
10516 }
10517
10518 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
10519 Return 1 if the attributes are present and valid, otherwise, return 0.
10520 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
10521
10522 static int
10523 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
10524 CORE_ADDR *high_return, struct dwarf2_cu *cu,
10525 struct partial_symtab *ranges_pst)
10526 {
10527 struct objfile *objfile = cu->objfile;
10528 struct comp_unit_head *cu_header = &cu->header;
10529 bfd *obfd = objfile->obfd;
10530 unsigned int addr_size = cu_header->addr_size;
10531 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10532 /* Base address selection entry. */
10533 CORE_ADDR base;
10534 int found_base;
10535 unsigned int dummy;
10536 const gdb_byte *buffer;
10537 CORE_ADDR marker;
10538 int low_set;
10539 CORE_ADDR low = 0;
10540 CORE_ADDR high = 0;
10541 CORE_ADDR baseaddr;
10542
10543 found_base = cu->base_known;
10544 base = cu->base_address;
10545
10546 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
10547 if (offset >= dwarf2_per_objfile->ranges.size)
10548 {
10549 complaint (&symfile_complaints,
10550 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10551 offset);
10552 return 0;
10553 }
10554 buffer = dwarf2_per_objfile->ranges.buffer + offset;
10555
10556 /* Read in the largest possible address. */
10557 marker = read_address (obfd, buffer, cu, &dummy);
10558 if ((marker & mask) == mask)
10559 {
10560 /* If we found the largest possible address, then
10561 read the base address. */
10562 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10563 buffer += 2 * addr_size;
10564 offset += 2 * addr_size;
10565 found_base = 1;
10566 }
10567
10568 low_set = 0;
10569
10570 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10571
10572 while (1)
10573 {
10574 CORE_ADDR range_beginning, range_end;
10575
10576 range_beginning = read_address (obfd, buffer, cu, &dummy);
10577 buffer += addr_size;
10578 range_end = read_address (obfd, buffer, cu, &dummy);
10579 buffer += addr_size;
10580 offset += 2 * addr_size;
10581
10582 /* An end of list marker is a pair of zero addresses. */
10583 if (range_beginning == 0 && range_end == 0)
10584 /* Found the end of list entry. */
10585 break;
10586
10587 /* Each base address selection entry is a pair of 2 values.
10588 The first is the largest possible address, the second is
10589 the base address. Check for a base address here. */
10590 if ((range_beginning & mask) == mask)
10591 {
10592 /* If we found the largest possible address, then
10593 read the base address. */
10594 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10595 found_base = 1;
10596 continue;
10597 }
10598
10599 if (!found_base)
10600 {
10601 /* We have no valid base address for the ranges
10602 data. */
10603 complaint (&symfile_complaints,
10604 _("Invalid .debug_ranges data (no base address)"));
10605 return 0;
10606 }
10607
10608 if (range_beginning > range_end)
10609 {
10610 /* Inverted range entries are invalid. */
10611 complaint (&symfile_complaints,
10612 _("Invalid .debug_ranges data (inverted range)"));
10613 return 0;
10614 }
10615
10616 /* Empty range entries have no effect. */
10617 if (range_beginning == range_end)
10618 continue;
10619
10620 range_beginning += base;
10621 range_end += base;
10622
10623 /* A not-uncommon case of bad debug info.
10624 Don't pollute the addrmap with bad data. */
10625 if (range_beginning + baseaddr == 0
10626 && !dwarf2_per_objfile->has_section_at_zero)
10627 {
10628 complaint (&symfile_complaints,
10629 _(".debug_ranges entry has start address of zero"
10630 " [in module %s]"), objfile->name);
10631 continue;
10632 }
10633
10634 if (ranges_pst != NULL)
10635 addrmap_set_empty (objfile->psymtabs_addrmap,
10636 range_beginning + baseaddr,
10637 range_end - 1 + baseaddr,
10638 ranges_pst);
10639
10640 /* FIXME: This is recording everything as a low-high
10641 segment of consecutive addresses. We should have a
10642 data structure for discontiguous block ranges
10643 instead. */
10644 if (! low_set)
10645 {
10646 low = range_beginning;
10647 high = range_end;
10648 low_set = 1;
10649 }
10650 else
10651 {
10652 if (range_beginning < low)
10653 low = range_beginning;
10654 if (range_end > high)
10655 high = range_end;
10656 }
10657 }
10658
10659 if (! low_set)
10660 /* If the first entry is an end-of-list marker, the range
10661 describes an empty scope, i.e. no instructions. */
10662 return 0;
10663
10664 if (low_return)
10665 *low_return = low;
10666 if (high_return)
10667 *high_return = high;
10668 return 1;
10669 }
10670
10671 /* Get low and high pc attributes from a die. Return 1 if the attributes
10672 are present and valid, otherwise, return 0. Return -1 if the range is
10673 discontinuous, i.e. derived from DW_AT_ranges information. */
10674
10675 static int
10676 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
10677 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10678 struct partial_symtab *pst)
10679 {
10680 struct attribute *attr;
10681 struct attribute *attr_high;
10682 CORE_ADDR low = 0;
10683 CORE_ADDR high = 0;
10684 int ret = 0;
10685
10686 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10687 if (attr_high)
10688 {
10689 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10690 if (attr)
10691 {
10692 low = DW_ADDR (attr);
10693 if (attr_high->form == DW_FORM_addr
10694 || attr_high->form == DW_FORM_GNU_addr_index)
10695 high = DW_ADDR (attr_high);
10696 else
10697 high = low + DW_UNSND (attr_high);
10698 }
10699 else
10700 /* Found high w/o low attribute. */
10701 return 0;
10702
10703 /* Found consecutive range of addresses. */
10704 ret = 1;
10705 }
10706 else
10707 {
10708 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10709 if (attr != NULL)
10710 {
10711 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10712 We take advantage of the fact that DW_AT_ranges does not appear
10713 in DW_TAG_compile_unit of DWO files. */
10714 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10715 unsigned int ranges_offset = (DW_UNSND (attr)
10716 + (need_ranges_base
10717 ? cu->ranges_base
10718 : 0));
10719
10720 /* Value of the DW_AT_ranges attribute is the offset in the
10721 .debug_ranges section. */
10722 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
10723 return 0;
10724 /* Found discontinuous range of addresses. */
10725 ret = -1;
10726 }
10727 }
10728
10729 /* read_partial_die has also the strict LOW < HIGH requirement. */
10730 if (high <= low)
10731 return 0;
10732
10733 /* When using the GNU linker, .gnu.linkonce. sections are used to
10734 eliminate duplicate copies of functions and vtables and such.
10735 The linker will arbitrarily choose one and discard the others.
10736 The AT_*_pc values for such functions refer to local labels in
10737 these sections. If the section from that file was discarded, the
10738 labels are not in the output, so the relocs get a value of 0.
10739 If this is a discarded function, mark the pc bounds as invalid,
10740 so that GDB will ignore it. */
10741 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
10742 return 0;
10743
10744 *lowpc = low;
10745 if (highpc)
10746 *highpc = high;
10747 return ret;
10748 }
10749
10750 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
10751 its low and high PC addresses. Do nothing if these addresses could not
10752 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10753 and HIGHPC to the high address if greater than HIGHPC. */
10754
10755 static void
10756 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10757 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10758 struct dwarf2_cu *cu)
10759 {
10760 CORE_ADDR low, high;
10761 struct die_info *child = die->child;
10762
10763 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
10764 {
10765 *lowpc = min (*lowpc, low);
10766 *highpc = max (*highpc, high);
10767 }
10768
10769 /* If the language does not allow nested subprograms (either inside
10770 subprograms or lexical blocks), we're done. */
10771 if (cu->language != language_ada)
10772 return;
10773
10774 /* Check all the children of the given DIE. If it contains nested
10775 subprograms, then check their pc bounds. Likewise, we need to
10776 check lexical blocks as well, as they may also contain subprogram
10777 definitions. */
10778 while (child && child->tag)
10779 {
10780 if (child->tag == DW_TAG_subprogram
10781 || child->tag == DW_TAG_lexical_block)
10782 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10783 child = sibling_die (child);
10784 }
10785 }
10786
10787 /* Get the low and high pc's represented by the scope DIE, and store
10788 them in *LOWPC and *HIGHPC. If the correct values can't be
10789 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10790
10791 static void
10792 get_scope_pc_bounds (struct die_info *die,
10793 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10794 struct dwarf2_cu *cu)
10795 {
10796 CORE_ADDR best_low = (CORE_ADDR) -1;
10797 CORE_ADDR best_high = (CORE_ADDR) 0;
10798 CORE_ADDR current_low, current_high;
10799
10800 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
10801 {
10802 best_low = current_low;
10803 best_high = current_high;
10804 }
10805 else
10806 {
10807 struct die_info *child = die->child;
10808
10809 while (child && child->tag)
10810 {
10811 switch (child->tag) {
10812 case DW_TAG_subprogram:
10813 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
10814 break;
10815 case DW_TAG_namespace:
10816 case DW_TAG_module:
10817 /* FIXME: carlton/2004-01-16: Should we do this for
10818 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10819 that current GCC's always emit the DIEs corresponding
10820 to definitions of methods of classes as children of a
10821 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10822 the DIEs giving the declarations, which could be
10823 anywhere). But I don't see any reason why the
10824 standards says that they have to be there. */
10825 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10826
10827 if (current_low != ((CORE_ADDR) -1))
10828 {
10829 best_low = min (best_low, current_low);
10830 best_high = max (best_high, current_high);
10831 }
10832 break;
10833 default:
10834 /* Ignore. */
10835 break;
10836 }
10837
10838 child = sibling_die (child);
10839 }
10840 }
10841
10842 *lowpc = best_low;
10843 *highpc = best_high;
10844 }
10845
10846 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
10847 in DIE. */
10848
10849 static void
10850 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10851 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10852 {
10853 struct objfile *objfile = cu->objfile;
10854 struct attribute *attr;
10855 struct attribute *attr_high;
10856
10857 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10858 if (attr_high)
10859 {
10860 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10861 if (attr)
10862 {
10863 CORE_ADDR low = DW_ADDR (attr);
10864 CORE_ADDR high;
10865 if (attr_high->form == DW_FORM_addr
10866 || attr_high->form == DW_FORM_GNU_addr_index)
10867 high = DW_ADDR (attr_high);
10868 else
10869 high = low + DW_UNSND (attr_high);
10870
10871 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10872 }
10873 }
10874
10875 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10876 if (attr)
10877 {
10878 bfd *obfd = objfile->obfd;
10879 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10880 We take advantage of the fact that DW_AT_ranges does not appear
10881 in DW_TAG_compile_unit of DWO files. */
10882 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10883
10884 /* The value of the DW_AT_ranges attribute is the offset of the
10885 address range list in the .debug_ranges section. */
10886 unsigned long offset = (DW_UNSND (attr)
10887 + (need_ranges_base ? cu->ranges_base : 0));
10888 const gdb_byte *buffer;
10889
10890 /* For some target architectures, but not others, the
10891 read_address function sign-extends the addresses it returns.
10892 To recognize base address selection entries, we need a
10893 mask. */
10894 unsigned int addr_size = cu->header.addr_size;
10895 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10896
10897 /* The base address, to which the next pair is relative. Note
10898 that this 'base' is a DWARF concept: most entries in a range
10899 list are relative, to reduce the number of relocs against the
10900 debugging information. This is separate from this function's
10901 'baseaddr' argument, which GDB uses to relocate debugging
10902 information from a shared library based on the address at
10903 which the library was loaded. */
10904 CORE_ADDR base = cu->base_address;
10905 int base_known = cu->base_known;
10906
10907 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
10908 if (offset >= dwarf2_per_objfile->ranges.size)
10909 {
10910 complaint (&symfile_complaints,
10911 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10912 offset);
10913 return;
10914 }
10915 buffer = dwarf2_per_objfile->ranges.buffer + offset;
10916
10917 for (;;)
10918 {
10919 unsigned int bytes_read;
10920 CORE_ADDR start, end;
10921
10922 start = read_address (obfd, buffer, cu, &bytes_read);
10923 buffer += bytes_read;
10924 end = read_address (obfd, buffer, cu, &bytes_read);
10925 buffer += bytes_read;
10926
10927 /* Did we find the end of the range list? */
10928 if (start == 0 && end == 0)
10929 break;
10930
10931 /* Did we find a base address selection entry? */
10932 else if ((start & base_select_mask) == base_select_mask)
10933 {
10934 base = end;
10935 base_known = 1;
10936 }
10937
10938 /* We found an ordinary address range. */
10939 else
10940 {
10941 if (!base_known)
10942 {
10943 complaint (&symfile_complaints,
10944 _("Invalid .debug_ranges data "
10945 "(no base address)"));
10946 return;
10947 }
10948
10949 if (start > end)
10950 {
10951 /* Inverted range entries are invalid. */
10952 complaint (&symfile_complaints,
10953 _("Invalid .debug_ranges data "
10954 "(inverted range)"));
10955 return;
10956 }
10957
10958 /* Empty range entries have no effect. */
10959 if (start == end)
10960 continue;
10961
10962 start += base + baseaddr;
10963 end += base + baseaddr;
10964
10965 /* A not-uncommon case of bad debug info.
10966 Don't pollute the addrmap with bad data. */
10967 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10968 {
10969 complaint (&symfile_complaints,
10970 _(".debug_ranges entry has start address of zero"
10971 " [in module %s]"), objfile->name);
10972 continue;
10973 }
10974
10975 record_block_range (block, start, end - 1);
10976 }
10977 }
10978 }
10979 }
10980
10981 /* Check whether the producer field indicates either of GCC < 4.6, or the
10982 Intel C/C++ compiler, and cache the result in CU. */
10983
10984 static void
10985 check_producer (struct dwarf2_cu *cu)
10986 {
10987 const char *cs;
10988 int major, minor, release;
10989
10990 if (cu->producer == NULL)
10991 {
10992 /* For unknown compilers expect their behavior is DWARF version
10993 compliant.
10994
10995 GCC started to support .debug_types sections by -gdwarf-4 since
10996 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10997 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10998 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10999 interpreted incorrectly by GDB now - GCC PR debug/48229. */
11000 }
11001 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
11002 {
11003 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
11004
11005 cs = &cu->producer[strlen ("GNU ")];
11006 while (*cs && !isdigit (*cs))
11007 cs++;
11008 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
11009 {
11010 /* Not recognized as GCC. */
11011 }
11012 else
11013 {
11014 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
11015 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
11016 }
11017 }
11018 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
11019 cu->producer_is_icc = 1;
11020 else
11021 {
11022 /* For other non-GCC compilers, expect their behavior is DWARF version
11023 compliant. */
11024 }
11025
11026 cu->checked_producer = 1;
11027 }
11028
11029 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
11030 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
11031 during 4.6.0 experimental. */
11032
11033 static int
11034 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
11035 {
11036 if (!cu->checked_producer)
11037 check_producer (cu);
11038
11039 return cu->producer_is_gxx_lt_4_6;
11040 }
11041
11042 /* Return the default accessibility type if it is not overriden by
11043 DW_AT_accessibility. */
11044
11045 static enum dwarf_access_attribute
11046 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
11047 {
11048 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
11049 {
11050 /* The default DWARF 2 accessibility for members is public, the default
11051 accessibility for inheritance is private. */
11052
11053 if (die->tag != DW_TAG_inheritance)
11054 return DW_ACCESS_public;
11055 else
11056 return DW_ACCESS_private;
11057 }
11058 else
11059 {
11060 /* DWARF 3+ defines the default accessibility a different way. The same
11061 rules apply now for DW_TAG_inheritance as for the members and it only
11062 depends on the container kind. */
11063
11064 if (die->parent->tag == DW_TAG_class_type)
11065 return DW_ACCESS_private;
11066 else
11067 return DW_ACCESS_public;
11068 }
11069 }
11070
11071 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
11072 offset. If the attribute was not found return 0, otherwise return
11073 1. If it was found but could not properly be handled, set *OFFSET
11074 to 0. */
11075
11076 static int
11077 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
11078 LONGEST *offset)
11079 {
11080 struct attribute *attr;
11081
11082 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
11083 if (attr != NULL)
11084 {
11085 *offset = 0;
11086
11087 /* Note that we do not check for a section offset first here.
11088 This is because DW_AT_data_member_location is new in DWARF 4,
11089 so if we see it, we can assume that a constant form is really
11090 a constant and not a section offset. */
11091 if (attr_form_is_constant (attr))
11092 *offset = dwarf2_get_attr_constant_value (attr, 0);
11093 else if (attr_form_is_section_offset (attr))
11094 dwarf2_complex_location_expr_complaint ();
11095 else if (attr_form_is_block (attr))
11096 *offset = decode_locdesc (DW_BLOCK (attr), cu);
11097 else
11098 dwarf2_complex_location_expr_complaint ();
11099
11100 return 1;
11101 }
11102
11103 return 0;
11104 }
11105
11106 /* Add an aggregate field to the field list. */
11107
11108 static void
11109 dwarf2_add_field (struct field_info *fip, struct die_info *die,
11110 struct dwarf2_cu *cu)
11111 {
11112 struct objfile *objfile = cu->objfile;
11113 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11114 struct nextfield *new_field;
11115 struct attribute *attr;
11116 struct field *fp;
11117 const char *fieldname = "";
11118
11119 /* Allocate a new field list entry and link it in. */
11120 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
11121 make_cleanup (xfree, new_field);
11122 memset (new_field, 0, sizeof (struct nextfield));
11123
11124 if (die->tag == DW_TAG_inheritance)
11125 {
11126 new_field->next = fip->baseclasses;
11127 fip->baseclasses = new_field;
11128 }
11129 else
11130 {
11131 new_field->next = fip->fields;
11132 fip->fields = new_field;
11133 }
11134 fip->nfields++;
11135
11136 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11137 if (attr)
11138 new_field->accessibility = DW_UNSND (attr);
11139 else
11140 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
11141 if (new_field->accessibility != DW_ACCESS_public)
11142 fip->non_public_fields = 1;
11143
11144 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11145 if (attr)
11146 new_field->virtuality = DW_UNSND (attr);
11147 else
11148 new_field->virtuality = DW_VIRTUALITY_none;
11149
11150 fp = &new_field->field;
11151
11152 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
11153 {
11154 LONGEST offset;
11155
11156 /* Data member other than a C++ static data member. */
11157
11158 /* Get type of field. */
11159 fp->type = die_type (die, cu);
11160
11161 SET_FIELD_BITPOS (*fp, 0);
11162
11163 /* Get bit size of field (zero if none). */
11164 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
11165 if (attr)
11166 {
11167 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
11168 }
11169 else
11170 {
11171 FIELD_BITSIZE (*fp) = 0;
11172 }
11173
11174 /* Get bit offset of field. */
11175 if (handle_data_member_location (die, cu, &offset))
11176 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
11177 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
11178 if (attr)
11179 {
11180 if (gdbarch_bits_big_endian (gdbarch))
11181 {
11182 /* For big endian bits, the DW_AT_bit_offset gives the
11183 additional bit offset from the MSB of the containing
11184 anonymous object to the MSB of the field. We don't
11185 have to do anything special since we don't need to
11186 know the size of the anonymous object. */
11187 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
11188 }
11189 else
11190 {
11191 /* For little endian bits, compute the bit offset to the
11192 MSB of the anonymous object, subtract off the number of
11193 bits from the MSB of the field to the MSB of the
11194 object, and then subtract off the number of bits of
11195 the field itself. The result is the bit offset of
11196 the LSB of the field. */
11197 int anonymous_size;
11198 int bit_offset = DW_UNSND (attr);
11199
11200 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11201 if (attr)
11202 {
11203 /* The size of the anonymous object containing
11204 the bit field is explicit, so use the
11205 indicated size (in bytes). */
11206 anonymous_size = DW_UNSND (attr);
11207 }
11208 else
11209 {
11210 /* The size of the anonymous object containing
11211 the bit field must be inferred from the type
11212 attribute of the data member containing the
11213 bit field. */
11214 anonymous_size = TYPE_LENGTH (fp->type);
11215 }
11216 SET_FIELD_BITPOS (*fp,
11217 (FIELD_BITPOS (*fp)
11218 + anonymous_size * bits_per_byte
11219 - bit_offset - FIELD_BITSIZE (*fp)));
11220 }
11221 }
11222
11223 /* Get name of field. */
11224 fieldname = dwarf2_name (die, cu);
11225 if (fieldname == NULL)
11226 fieldname = "";
11227
11228 /* The name is already allocated along with this objfile, so we don't
11229 need to duplicate it for the type. */
11230 fp->name = fieldname;
11231
11232 /* Change accessibility for artificial fields (e.g. virtual table
11233 pointer or virtual base class pointer) to private. */
11234 if (dwarf2_attr (die, DW_AT_artificial, cu))
11235 {
11236 FIELD_ARTIFICIAL (*fp) = 1;
11237 new_field->accessibility = DW_ACCESS_private;
11238 fip->non_public_fields = 1;
11239 }
11240 }
11241 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
11242 {
11243 /* C++ static member. */
11244
11245 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
11246 is a declaration, but all versions of G++ as of this writing
11247 (so through at least 3.2.1) incorrectly generate
11248 DW_TAG_variable tags. */
11249
11250 const char *physname;
11251
11252 /* Get name of field. */
11253 fieldname = dwarf2_name (die, cu);
11254 if (fieldname == NULL)
11255 return;
11256
11257 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11258 if (attr
11259 /* Only create a symbol if this is an external value.
11260 new_symbol checks this and puts the value in the global symbol
11261 table, which we want. If it is not external, new_symbol
11262 will try to put the value in cu->list_in_scope which is wrong. */
11263 && dwarf2_flag_true_p (die, DW_AT_external, cu))
11264 {
11265 /* A static const member, not much different than an enum as far as
11266 we're concerned, except that we can support more types. */
11267 new_symbol (die, NULL, cu);
11268 }
11269
11270 /* Get physical name. */
11271 physname = dwarf2_physname (fieldname, die, cu);
11272
11273 /* The name is already allocated along with this objfile, so we don't
11274 need to duplicate it for the type. */
11275 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
11276 FIELD_TYPE (*fp) = die_type (die, cu);
11277 FIELD_NAME (*fp) = fieldname;
11278 }
11279 else if (die->tag == DW_TAG_inheritance)
11280 {
11281 LONGEST offset;
11282
11283 /* C++ base class field. */
11284 if (handle_data_member_location (die, cu, &offset))
11285 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
11286 FIELD_BITSIZE (*fp) = 0;
11287 FIELD_TYPE (*fp) = die_type (die, cu);
11288 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
11289 fip->nbaseclasses++;
11290 }
11291 }
11292
11293 /* Add a typedef defined in the scope of the FIP's class. */
11294
11295 static void
11296 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
11297 struct dwarf2_cu *cu)
11298 {
11299 struct objfile *objfile = cu->objfile;
11300 struct typedef_field_list *new_field;
11301 struct attribute *attr;
11302 struct typedef_field *fp;
11303 char *fieldname = "";
11304
11305 /* Allocate a new field list entry and link it in. */
11306 new_field = xzalloc (sizeof (*new_field));
11307 make_cleanup (xfree, new_field);
11308
11309 gdb_assert (die->tag == DW_TAG_typedef);
11310
11311 fp = &new_field->field;
11312
11313 /* Get name of field. */
11314 fp->name = dwarf2_name (die, cu);
11315 if (fp->name == NULL)
11316 return;
11317
11318 fp->type = read_type_die (die, cu);
11319
11320 new_field->next = fip->typedef_field_list;
11321 fip->typedef_field_list = new_field;
11322 fip->typedef_field_list_count++;
11323 }
11324
11325 /* Create the vector of fields, and attach it to the type. */
11326
11327 static void
11328 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
11329 struct dwarf2_cu *cu)
11330 {
11331 int nfields = fip->nfields;
11332
11333 /* Record the field count, allocate space for the array of fields,
11334 and create blank accessibility bitfields if necessary. */
11335 TYPE_NFIELDS (type) = nfields;
11336 TYPE_FIELDS (type) = (struct field *)
11337 TYPE_ALLOC (type, sizeof (struct field) * nfields);
11338 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
11339
11340 if (fip->non_public_fields && cu->language != language_ada)
11341 {
11342 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11343
11344 TYPE_FIELD_PRIVATE_BITS (type) =
11345 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11346 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
11347
11348 TYPE_FIELD_PROTECTED_BITS (type) =
11349 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11350 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
11351
11352 TYPE_FIELD_IGNORE_BITS (type) =
11353 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11354 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
11355 }
11356
11357 /* If the type has baseclasses, allocate and clear a bit vector for
11358 TYPE_FIELD_VIRTUAL_BITS. */
11359 if (fip->nbaseclasses && cu->language != language_ada)
11360 {
11361 int num_bytes = B_BYTES (fip->nbaseclasses);
11362 unsigned char *pointer;
11363
11364 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11365 pointer = TYPE_ALLOC (type, num_bytes);
11366 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
11367 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
11368 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
11369 }
11370
11371 /* Copy the saved-up fields into the field vector. Start from the head of
11372 the list, adding to the tail of the field array, so that they end up in
11373 the same order in the array in which they were added to the list. */
11374 while (nfields-- > 0)
11375 {
11376 struct nextfield *fieldp;
11377
11378 if (fip->fields)
11379 {
11380 fieldp = fip->fields;
11381 fip->fields = fieldp->next;
11382 }
11383 else
11384 {
11385 fieldp = fip->baseclasses;
11386 fip->baseclasses = fieldp->next;
11387 }
11388
11389 TYPE_FIELD (type, nfields) = fieldp->field;
11390 switch (fieldp->accessibility)
11391 {
11392 case DW_ACCESS_private:
11393 if (cu->language != language_ada)
11394 SET_TYPE_FIELD_PRIVATE (type, nfields);
11395 break;
11396
11397 case DW_ACCESS_protected:
11398 if (cu->language != language_ada)
11399 SET_TYPE_FIELD_PROTECTED (type, nfields);
11400 break;
11401
11402 case DW_ACCESS_public:
11403 break;
11404
11405 default:
11406 /* Unknown accessibility. Complain and treat it as public. */
11407 {
11408 complaint (&symfile_complaints, _("unsupported accessibility %d"),
11409 fieldp->accessibility);
11410 }
11411 break;
11412 }
11413 if (nfields < fip->nbaseclasses)
11414 {
11415 switch (fieldp->virtuality)
11416 {
11417 case DW_VIRTUALITY_virtual:
11418 case DW_VIRTUALITY_pure_virtual:
11419 if (cu->language == language_ada)
11420 error (_("unexpected virtuality in component of Ada type"));
11421 SET_TYPE_FIELD_VIRTUAL (type, nfields);
11422 break;
11423 }
11424 }
11425 }
11426 }
11427
11428 /* Return true if this member function is a constructor, false
11429 otherwise. */
11430
11431 static int
11432 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
11433 {
11434 const char *fieldname;
11435 const char *typename;
11436 int len;
11437
11438 if (die->parent == NULL)
11439 return 0;
11440
11441 if (die->parent->tag != DW_TAG_structure_type
11442 && die->parent->tag != DW_TAG_union_type
11443 && die->parent->tag != DW_TAG_class_type)
11444 return 0;
11445
11446 fieldname = dwarf2_name (die, cu);
11447 typename = dwarf2_name (die->parent, cu);
11448 if (fieldname == NULL || typename == NULL)
11449 return 0;
11450
11451 len = strlen (fieldname);
11452 return (strncmp (fieldname, typename, len) == 0
11453 && (typename[len] == '\0' || typename[len] == '<'));
11454 }
11455
11456 /* Add a member function to the proper fieldlist. */
11457
11458 static void
11459 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
11460 struct type *type, struct dwarf2_cu *cu)
11461 {
11462 struct objfile *objfile = cu->objfile;
11463 struct attribute *attr;
11464 struct fnfieldlist *flp;
11465 int i;
11466 struct fn_field *fnp;
11467 const char *fieldname;
11468 struct nextfnfield *new_fnfield;
11469 struct type *this_type;
11470 enum dwarf_access_attribute accessibility;
11471
11472 if (cu->language == language_ada)
11473 error (_("unexpected member function in Ada type"));
11474
11475 /* Get name of member function. */
11476 fieldname = dwarf2_name (die, cu);
11477 if (fieldname == NULL)
11478 return;
11479
11480 /* Look up member function name in fieldlist. */
11481 for (i = 0; i < fip->nfnfields; i++)
11482 {
11483 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
11484 break;
11485 }
11486
11487 /* Create new list element if necessary. */
11488 if (i < fip->nfnfields)
11489 flp = &fip->fnfieldlists[i];
11490 else
11491 {
11492 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
11493 {
11494 fip->fnfieldlists = (struct fnfieldlist *)
11495 xrealloc (fip->fnfieldlists,
11496 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
11497 * sizeof (struct fnfieldlist));
11498 if (fip->nfnfields == 0)
11499 make_cleanup (free_current_contents, &fip->fnfieldlists);
11500 }
11501 flp = &fip->fnfieldlists[fip->nfnfields];
11502 flp->name = fieldname;
11503 flp->length = 0;
11504 flp->head = NULL;
11505 i = fip->nfnfields++;
11506 }
11507
11508 /* Create a new member function field and chain it to the field list
11509 entry. */
11510 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
11511 make_cleanup (xfree, new_fnfield);
11512 memset (new_fnfield, 0, sizeof (struct nextfnfield));
11513 new_fnfield->next = flp->head;
11514 flp->head = new_fnfield;
11515 flp->length++;
11516
11517 /* Fill in the member function field info. */
11518 fnp = &new_fnfield->fnfield;
11519
11520 /* Delay processing of the physname until later. */
11521 if (cu->language == language_cplus || cu->language == language_java)
11522 {
11523 add_to_method_list (type, i, flp->length - 1, fieldname,
11524 die, cu);
11525 }
11526 else
11527 {
11528 const char *physname = dwarf2_physname (fieldname, die, cu);
11529 fnp->physname = physname ? physname : "";
11530 }
11531
11532 fnp->type = alloc_type (objfile);
11533 this_type = read_type_die (die, cu);
11534 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
11535 {
11536 int nparams = TYPE_NFIELDS (this_type);
11537
11538 /* TYPE is the domain of this method, and THIS_TYPE is the type
11539 of the method itself (TYPE_CODE_METHOD). */
11540 smash_to_method_type (fnp->type, type,
11541 TYPE_TARGET_TYPE (this_type),
11542 TYPE_FIELDS (this_type),
11543 TYPE_NFIELDS (this_type),
11544 TYPE_VARARGS (this_type));
11545
11546 /* Handle static member functions.
11547 Dwarf2 has no clean way to discern C++ static and non-static
11548 member functions. G++ helps GDB by marking the first
11549 parameter for non-static member functions (which is the this
11550 pointer) as artificial. We obtain this information from
11551 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
11552 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
11553 fnp->voffset = VOFFSET_STATIC;
11554 }
11555 else
11556 complaint (&symfile_complaints, _("member function type missing for '%s'"),
11557 dwarf2_full_name (fieldname, die, cu));
11558
11559 /* Get fcontext from DW_AT_containing_type if present. */
11560 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11561 fnp->fcontext = die_containing_type (die, cu);
11562
11563 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11564 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
11565
11566 /* Get accessibility. */
11567 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11568 if (attr)
11569 accessibility = DW_UNSND (attr);
11570 else
11571 accessibility = dwarf2_default_access_attribute (die, cu);
11572 switch (accessibility)
11573 {
11574 case DW_ACCESS_private:
11575 fnp->is_private = 1;
11576 break;
11577 case DW_ACCESS_protected:
11578 fnp->is_protected = 1;
11579 break;
11580 }
11581
11582 /* Check for artificial methods. */
11583 attr = dwarf2_attr (die, DW_AT_artificial, cu);
11584 if (attr && DW_UNSND (attr) != 0)
11585 fnp->is_artificial = 1;
11586
11587 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11588
11589 /* Get index in virtual function table if it is a virtual member
11590 function. For older versions of GCC, this is an offset in the
11591 appropriate virtual table, as specified by DW_AT_containing_type.
11592 For everyone else, it is an expression to be evaluated relative
11593 to the object address. */
11594
11595 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
11596 if (attr)
11597 {
11598 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
11599 {
11600 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11601 {
11602 /* Old-style GCC. */
11603 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11604 }
11605 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11606 || (DW_BLOCK (attr)->size > 1
11607 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11608 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11609 {
11610 struct dwarf_block blk;
11611 int offset;
11612
11613 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11614 ? 1 : 2);
11615 blk.size = DW_BLOCK (attr)->size - offset;
11616 blk.data = DW_BLOCK (attr)->data + offset;
11617 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11618 if ((fnp->voffset % cu->header.addr_size) != 0)
11619 dwarf2_complex_location_expr_complaint ();
11620 else
11621 fnp->voffset /= cu->header.addr_size;
11622 fnp->voffset += 2;
11623 }
11624 else
11625 dwarf2_complex_location_expr_complaint ();
11626
11627 if (!fnp->fcontext)
11628 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11629 }
11630 else if (attr_form_is_section_offset (attr))
11631 {
11632 dwarf2_complex_location_expr_complaint ();
11633 }
11634 else
11635 {
11636 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11637 fieldname);
11638 }
11639 }
11640 else
11641 {
11642 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11643 if (attr && DW_UNSND (attr))
11644 {
11645 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11646 complaint (&symfile_complaints,
11647 _("Member function \"%s\" (offset %d) is virtual "
11648 "but the vtable offset is not specified"),
11649 fieldname, die->offset.sect_off);
11650 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11651 TYPE_CPLUS_DYNAMIC (type) = 1;
11652 }
11653 }
11654 }
11655
11656 /* Create the vector of member function fields, and attach it to the type. */
11657
11658 static void
11659 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
11660 struct dwarf2_cu *cu)
11661 {
11662 struct fnfieldlist *flp;
11663 int i;
11664
11665 if (cu->language == language_ada)
11666 error (_("unexpected member functions in Ada type"));
11667
11668 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11669 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11670 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11671
11672 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11673 {
11674 struct nextfnfield *nfp = flp->head;
11675 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11676 int k;
11677
11678 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11679 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11680 fn_flp->fn_fields = (struct fn_field *)
11681 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11682 for (k = flp->length; (k--, nfp); nfp = nfp->next)
11683 fn_flp->fn_fields[k] = nfp->fnfield;
11684 }
11685
11686 TYPE_NFN_FIELDS (type) = fip->nfnfields;
11687 }
11688
11689 /* Returns non-zero if NAME is the name of a vtable member in CU's
11690 language, zero otherwise. */
11691 static int
11692 is_vtable_name (const char *name, struct dwarf2_cu *cu)
11693 {
11694 static const char vptr[] = "_vptr";
11695 static const char vtable[] = "vtable";
11696
11697 /* Look for the C++ and Java forms of the vtable. */
11698 if ((cu->language == language_java
11699 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11700 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11701 && is_cplus_marker (name[sizeof (vptr) - 1])))
11702 return 1;
11703
11704 return 0;
11705 }
11706
11707 /* GCC outputs unnamed structures that are really pointers to member
11708 functions, with the ABI-specified layout. If TYPE describes
11709 such a structure, smash it into a member function type.
11710
11711 GCC shouldn't do this; it should just output pointer to member DIEs.
11712 This is GCC PR debug/28767. */
11713
11714 static void
11715 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
11716 {
11717 struct type *pfn_type, *domain_type, *new_type;
11718
11719 /* Check for a structure with no name and two children. */
11720 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11721 return;
11722
11723 /* Check for __pfn and __delta members. */
11724 if (TYPE_FIELD_NAME (type, 0) == NULL
11725 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11726 || TYPE_FIELD_NAME (type, 1) == NULL
11727 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11728 return;
11729
11730 /* Find the type of the method. */
11731 pfn_type = TYPE_FIELD_TYPE (type, 0);
11732 if (pfn_type == NULL
11733 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11734 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
11735 return;
11736
11737 /* Look for the "this" argument. */
11738 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11739 if (TYPE_NFIELDS (pfn_type) == 0
11740 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
11741 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
11742 return;
11743
11744 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
11745 new_type = alloc_type (objfile);
11746 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
11747 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11748 TYPE_VARARGS (pfn_type));
11749 smash_to_methodptr_type (type, new_type);
11750 }
11751
11752 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11753 (icc). */
11754
11755 static int
11756 producer_is_icc (struct dwarf2_cu *cu)
11757 {
11758 if (!cu->checked_producer)
11759 check_producer (cu);
11760
11761 return cu->producer_is_icc;
11762 }
11763
11764 /* Called when we find the DIE that starts a structure or union scope
11765 (definition) to create a type for the structure or union. Fill in
11766 the type's name and general properties; the members will not be
11767 processed until process_structure_scope.
11768
11769 NOTE: we need to call these functions regardless of whether or not the
11770 DIE has a DW_AT_name attribute, since it might be an anonymous
11771 structure or union. This gets the type entered into our set of
11772 user defined types.
11773
11774 However, if the structure is incomplete (an opaque struct/union)
11775 then suppress creating a symbol table entry for it since gdb only
11776 wants to find the one with the complete definition. Note that if
11777 it is complete, we just call new_symbol, which does it's own
11778 checking about whether the struct/union is anonymous or not (and
11779 suppresses creating a symbol table entry itself). */
11780
11781 static struct type *
11782 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
11783 {
11784 struct objfile *objfile = cu->objfile;
11785 struct type *type;
11786 struct attribute *attr;
11787 const char *name;
11788
11789 /* If the definition of this type lives in .debug_types, read that type.
11790 Don't follow DW_AT_specification though, that will take us back up
11791 the chain and we want to go down. */
11792 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11793 if (attr)
11794 {
11795 type = get_DW_AT_signature_type (die, attr, cu);
11796
11797 /* The type's CU may not be the same as CU.
11798 Ensure TYPE is recorded with CU in die_type_hash. */
11799 return set_die_type (die, type, cu);
11800 }
11801
11802 type = alloc_type (objfile);
11803 INIT_CPLUS_SPECIFIC (type);
11804
11805 name = dwarf2_name (die, cu);
11806 if (name != NULL)
11807 {
11808 if (cu->language == language_cplus
11809 || cu->language == language_java)
11810 {
11811 const char *full_name = dwarf2_full_name (name, die, cu);
11812
11813 /* dwarf2_full_name might have already finished building the DIE's
11814 type. If so, there is no need to continue. */
11815 if (get_die_type (die, cu) != NULL)
11816 return get_die_type (die, cu);
11817
11818 TYPE_TAG_NAME (type) = full_name;
11819 if (die->tag == DW_TAG_structure_type
11820 || die->tag == DW_TAG_class_type)
11821 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11822 }
11823 else
11824 {
11825 /* The name is already allocated along with this objfile, so
11826 we don't need to duplicate it for the type. */
11827 TYPE_TAG_NAME (type) = name;
11828 if (die->tag == DW_TAG_class_type)
11829 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11830 }
11831 }
11832
11833 if (die->tag == DW_TAG_structure_type)
11834 {
11835 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11836 }
11837 else if (die->tag == DW_TAG_union_type)
11838 {
11839 TYPE_CODE (type) = TYPE_CODE_UNION;
11840 }
11841 else
11842 {
11843 TYPE_CODE (type) = TYPE_CODE_CLASS;
11844 }
11845
11846 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11847 TYPE_DECLARED_CLASS (type) = 1;
11848
11849 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11850 if (attr)
11851 {
11852 TYPE_LENGTH (type) = DW_UNSND (attr);
11853 }
11854 else
11855 {
11856 TYPE_LENGTH (type) = 0;
11857 }
11858
11859 if (producer_is_icc (cu))
11860 {
11861 /* ICC does not output the required DW_AT_declaration
11862 on incomplete types, but gives them a size of zero. */
11863 }
11864 else
11865 TYPE_STUB_SUPPORTED (type) = 1;
11866
11867 if (die_is_declaration (die, cu))
11868 TYPE_STUB (type) = 1;
11869 else if (attr == NULL && die->child == NULL
11870 && producer_is_realview (cu->producer))
11871 /* RealView does not output the required DW_AT_declaration
11872 on incomplete types. */
11873 TYPE_STUB (type) = 1;
11874
11875 /* We need to add the type field to the die immediately so we don't
11876 infinitely recurse when dealing with pointers to the structure
11877 type within the structure itself. */
11878 set_die_type (die, type, cu);
11879
11880 /* set_die_type should be already done. */
11881 set_descriptive_type (type, die, cu);
11882
11883 return type;
11884 }
11885
11886 /* Finish creating a structure or union type, including filling in
11887 its members and creating a symbol for it. */
11888
11889 static void
11890 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11891 {
11892 struct objfile *objfile = cu->objfile;
11893 struct die_info *child_die = die->child;
11894 struct type *type;
11895
11896 type = get_die_type (die, cu);
11897 if (type == NULL)
11898 type = read_structure_type (die, cu);
11899
11900 if (die->child != NULL && ! die_is_declaration (die, cu))
11901 {
11902 struct field_info fi;
11903 struct die_info *child_die;
11904 VEC (symbolp) *template_args = NULL;
11905 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
11906
11907 memset (&fi, 0, sizeof (struct field_info));
11908
11909 child_die = die->child;
11910
11911 while (child_die && child_die->tag)
11912 {
11913 if (child_die->tag == DW_TAG_member
11914 || child_die->tag == DW_TAG_variable)
11915 {
11916 /* NOTE: carlton/2002-11-05: A C++ static data member
11917 should be a DW_TAG_member that is a declaration, but
11918 all versions of G++ as of this writing (so through at
11919 least 3.2.1) incorrectly generate DW_TAG_variable
11920 tags for them instead. */
11921 dwarf2_add_field (&fi, child_die, cu);
11922 }
11923 else if (child_die->tag == DW_TAG_subprogram)
11924 {
11925 /* C++ member function. */
11926 dwarf2_add_member_fn (&fi, child_die, type, cu);
11927 }
11928 else if (child_die->tag == DW_TAG_inheritance)
11929 {
11930 /* C++ base class field. */
11931 dwarf2_add_field (&fi, child_die, cu);
11932 }
11933 else if (child_die->tag == DW_TAG_typedef)
11934 dwarf2_add_typedef (&fi, child_die, cu);
11935 else if (child_die->tag == DW_TAG_template_type_param
11936 || child_die->tag == DW_TAG_template_value_param)
11937 {
11938 struct symbol *arg = new_symbol (child_die, NULL, cu);
11939
11940 if (arg != NULL)
11941 VEC_safe_push (symbolp, template_args, arg);
11942 }
11943
11944 child_die = sibling_die (child_die);
11945 }
11946
11947 /* Attach template arguments to type. */
11948 if (! VEC_empty (symbolp, template_args))
11949 {
11950 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11951 TYPE_N_TEMPLATE_ARGUMENTS (type)
11952 = VEC_length (symbolp, template_args);
11953 TYPE_TEMPLATE_ARGUMENTS (type)
11954 = obstack_alloc (&objfile->objfile_obstack,
11955 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11956 * sizeof (struct symbol *)));
11957 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11958 VEC_address (symbolp, template_args),
11959 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11960 * sizeof (struct symbol *)));
11961 VEC_free (symbolp, template_args);
11962 }
11963
11964 /* Attach fields and member functions to the type. */
11965 if (fi.nfields)
11966 dwarf2_attach_fields_to_type (&fi, type, cu);
11967 if (fi.nfnfields)
11968 {
11969 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
11970
11971 /* Get the type which refers to the base class (possibly this
11972 class itself) which contains the vtable pointer for the current
11973 class from the DW_AT_containing_type attribute. This use of
11974 DW_AT_containing_type is a GNU extension. */
11975
11976 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11977 {
11978 struct type *t = die_containing_type (die, cu);
11979
11980 TYPE_VPTR_BASETYPE (type) = t;
11981 if (type == t)
11982 {
11983 int i;
11984
11985 /* Our own class provides vtbl ptr. */
11986 for (i = TYPE_NFIELDS (t) - 1;
11987 i >= TYPE_N_BASECLASSES (t);
11988 --i)
11989 {
11990 const char *fieldname = TYPE_FIELD_NAME (t, i);
11991
11992 if (is_vtable_name (fieldname, cu))
11993 {
11994 TYPE_VPTR_FIELDNO (type) = i;
11995 break;
11996 }
11997 }
11998
11999 /* Complain if virtual function table field not found. */
12000 if (i < TYPE_N_BASECLASSES (t))
12001 complaint (&symfile_complaints,
12002 _("virtual function table pointer "
12003 "not found when defining class '%s'"),
12004 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
12005 "");
12006 }
12007 else
12008 {
12009 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
12010 }
12011 }
12012 else if (cu->producer
12013 && strncmp (cu->producer,
12014 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
12015 {
12016 /* The IBM XLC compiler does not provide direct indication
12017 of the containing type, but the vtable pointer is
12018 always named __vfp. */
12019
12020 int i;
12021
12022 for (i = TYPE_NFIELDS (type) - 1;
12023 i >= TYPE_N_BASECLASSES (type);
12024 --i)
12025 {
12026 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
12027 {
12028 TYPE_VPTR_FIELDNO (type) = i;
12029 TYPE_VPTR_BASETYPE (type) = type;
12030 break;
12031 }
12032 }
12033 }
12034 }
12035
12036 /* Copy fi.typedef_field_list linked list elements content into the
12037 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
12038 if (fi.typedef_field_list)
12039 {
12040 int i = fi.typedef_field_list_count;
12041
12042 ALLOCATE_CPLUS_STRUCT_TYPE (type);
12043 TYPE_TYPEDEF_FIELD_ARRAY (type)
12044 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
12045 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
12046
12047 /* Reverse the list order to keep the debug info elements order. */
12048 while (--i >= 0)
12049 {
12050 struct typedef_field *dest, *src;
12051
12052 dest = &TYPE_TYPEDEF_FIELD (type, i);
12053 src = &fi.typedef_field_list->field;
12054 fi.typedef_field_list = fi.typedef_field_list->next;
12055 *dest = *src;
12056 }
12057 }
12058
12059 do_cleanups (back_to);
12060
12061 if (HAVE_CPLUS_STRUCT (type))
12062 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
12063 }
12064
12065 quirk_gcc_member_function_pointer (type, objfile);
12066
12067 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
12068 snapshots) has been known to create a die giving a declaration
12069 for a class that has, as a child, a die giving a definition for a
12070 nested class. So we have to process our children even if the
12071 current die is a declaration. Normally, of course, a declaration
12072 won't have any children at all. */
12073
12074 while (child_die != NULL && child_die->tag)
12075 {
12076 if (child_die->tag == DW_TAG_member
12077 || child_die->tag == DW_TAG_variable
12078 || child_die->tag == DW_TAG_inheritance
12079 || child_die->tag == DW_TAG_template_value_param
12080 || child_die->tag == DW_TAG_template_type_param)
12081 {
12082 /* Do nothing. */
12083 }
12084 else
12085 process_die (child_die, cu);
12086
12087 child_die = sibling_die (child_die);
12088 }
12089
12090 /* Do not consider external references. According to the DWARF standard,
12091 these DIEs are identified by the fact that they have no byte_size
12092 attribute, and a declaration attribute. */
12093 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
12094 || !die_is_declaration (die, cu))
12095 new_symbol (die, type, cu);
12096 }
12097
12098 /* Given a DW_AT_enumeration_type die, set its type. We do not
12099 complete the type's fields yet, or create any symbols. */
12100
12101 static struct type *
12102 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
12103 {
12104 struct objfile *objfile = cu->objfile;
12105 struct type *type;
12106 struct attribute *attr;
12107 const char *name;
12108
12109 /* If the definition of this type lives in .debug_types, read that type.
12110 Don't follow DW_AT_specification though, that will take us back up
12111 the chain and we want to go down. */
12112 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
12113 if (attr)
12114 {
12115 type = get_DW_AT_signature_type (die, attr, cu);
12116
12117 /* The type's CU may not be the same as CU.
12118 Ensure TYPE is recorded with CU in die_type_hash. */
12119 return set_die_type (die, type, cu);
12120 }
12121
12122 type = alloc_type (objfile);
12123
12124 TYPE_CODE (type) = TYPE_CODE_ENUM;
12125 name = dwarf2_full_name (NULL, die, cu);
12126 if (name != NULL)
12127 TYPE_TAG_NAME (type) = name;
12128
12129 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12130 if (attr)
12131 {
12132 TYPE_LENGTH (type) = DW_UNSND (attr);
12133 }
12134 else
12135 {
12136 TYPE_LENGTH (type) = 0;
12137 }
12138
12139 /* The enumeration DIE can be incomplete. In Ada, any type can be
12140 declared as private in the package spec, and then defined only
12141 inside the package body. Such types are known as Taft Amendment
12142 Types. When another package uses such a type, an incomplete DIE
12143 may be generated by the compiler. */
12144 if (die_is_declaration (die, cu))
12145 TYPE_STUB (type) = 1;
12146
12147 return set_die_type (die, type, cu);
12148 }
12149
12150 /* Given a pointer to a die which begins an enumeration, process all
12151 the dies that define the members of the enumeration, and create the
12152 symbol for the enumeration type.
12153
12154 NOTE: We reverse the order of the element list. */
12155
12156 static void
12157 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
12158 {
12159 struct type *this_type;
12160
12161 this_type = get_die_type (die, cu);
12162 if (this_type == NULL)
12163 this_type = read_enumeration_type (die, cu);
12164
12165 if (die->child != NULL)
12166 {
12167 struct die_info *child_die;
12168 struct symbol *sym;
12169 struct field *fields = NULL;
12170 int num_fields = 0;
12171 int unsigned_enum = 1;
12172 const char *name;
12173 int flag_enum = 1;
12174 ULONGEST mask = 0;
12175
12176 child_die = die->child;
12177 while (child_die && child_die->tag)
12178 {
12179 if (child_die->tag != DW_TAG_enumerator)
12180 {
12181 process_die (child_die, cu);
12182 }
12183 else
12184 {
12185 name = dwarf2_name (child_die, cu);
12186 if (name)
12187 {
12188 sym = new_symbol (child_die, this_type, cu);
12189 if (SYMBOL_VALUE (sym) < 0)
12190 {
12191 unsigned_enum = 0;
12192 flag_enum = 0;
12193 }
12194 else if ((mask & SYMBOL_VALUE (sym)) != 0)
12195 flag_enum = 0;
12196 else
12197 mask |= SYMBOL_VALUE (sym);
12198
12199 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
12200 {
12201 fields = (struct field *)
12202 xrealloc (fields,
12203 (num_fields + DW_FIELD_ALLOC_CHUNK)
12204 * sizeof (struct field));
12205 }
12206
12207 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
12208 FIELD_TYPE (fields[num_fields]) = NULL;
12209 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
12210 FIELD_BITSIZE (fields[num_fields]) = 0;
12211
12212 num_fields++;
12213 }
12214 }
12215
12216 child_die = sibling_die (child_die);
12217 }
12218
12219 if (num_fields)
12220 {
12221 TYPE_NFIELDS (this_type) = num_fields;
12222 TYPE_FIELDS (this_type) = (struct field *)
12223 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
12224 memcpy (TYPE_FIELDS (this_type), fields,
12225 sizeof (struct field) * num_fields);
12226 xfree (fields);
12227 }
12228 if (unsigned_enum)
12229 TYPE_UNSIGNED (this_type) = 1;
12230 if (flag_enum)
12231 TYPE_FLAG_ENUM (this_type) = 1;
12232 }
12233
12234 /* If we are reading an enum from a .debug_types unit, and the enum
12235 is a declaration, and the enum is not the signatured type in the
12236 unit, then we do not want to add a symbol for it. Adding a
12237 symbol would in some cases obscure the true definition of the
12238 enum, giving users an incomplete type when the definition is
12239 actually available. Note that we do not want to do this for all
12240 enums which are just declarations, because C++0x allows forward
12241 enum declarations. */
12242 if (cu->per_cu->is_debug_types
12243 && die_is_declaration (die, cu))
12244 {
12245 struct signatured_type *sig_type;
12246
12247 sig_type = (struct signatured_type *) cu->per_cu;
12248 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
12249 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
12250 return;
12251 }
12252
12253 new_symbol (die, this_type, cu);
12254 }
12255
12256 /* Extract all information from a DW_TAG_array_type DIE and put it in
12257 the DIE's type field. For now, this only handles one dimensional
12258 arrays. */
12259
12260 static struct type *
12261 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
12262 {
12263 struct objfile *objfile = cu->objfile;
12264 struct die_info *child_die;
12265 struct type *type;
12266 struct type *element_type, *range_type, *index_type;
12267 struct type **range_types = NULL;
12268 struct attribute *attr;
12269 int ndim = 0;
12270 struct cleanup *back_to;
12271 const char *name;
12272
12273 element_type = die_type (die, cu);
12274
12275 /* The die_type call above may have already set the type for this DIE. */
12276 type = get_die_type (die, cu);
12277 if (type)
12278 return type;
12279
12280 /* Irix 6.2 native cc creates array types without children for
12281 arrays with unspecified length. */
12282 if (die->child == NULL)
12283 {
12284 index_type = objfile_type (objfile)->builtin_int;
12285 range_type = create_range_type (NULL, index_type, 0, -1);
12286 type = create_array_type (NULL, element_type, range_type);
12287 return set_die_type (die, type, cu);
12288 }
12289
12290 back_to = make_cleanup (null_cleanup, NULL);
12291 child_die = die->child;
12292 while (child_die && child_die->tag)
12293 {
12294 if (child_die->tag == DW_TAG_subrange_type)
12295 {
12296 struct type *child_type = read_type_die (child_die, cu);
12297
12298 if (child_type != NULL)
12299 {
12300 /* The range type was succesfully read. Save it for the
12301 array type creation. */
12302 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
12303 {
12304 range_types = (struct type **)
12305 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
12306 * sizeof (struct type *));
12307 if (ndim == 0)
12308 make_cleanup (free_current_contents, &range_types);
12309 }
12310 range_types[ndim++] = child_type;
12311 }
12312 }
12313 child_die = sibling_die (child_die);
12314 }
12315
12316 /* Dwarf2 dimensions are output from left to right, create the
12317 necessary array types in backwards order. */
12318
12319 type = element_type;
12320
12321 if (read_array_order (die, cu) == DW_ORD_col_major)
12322 {
12323 int i = 0;
12324
12325 while (i < ndim)
12326 type = create_array_type (NULL, type, range_types[i++]);
12327 }
12328 else
12329 {
12330 while (ndim-- > 0)
12331 type = create_array_type (NULL, type, range_types[ndim]);
12332 }
12333
12334 /* Understand Dwarf2 support for vector types (like they occur on
12335 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
12336 array type. This is not part of the Dwarf2/3 standard yet, but a
12337 custom vendor extension. The main difference between a regular
12338 array and the vector variant is that vectors are passed by value
12339 to functions. */
12340 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
12341 if (attr)
12342 make_vector_type (type);
12343
12344 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
12345 implementation may choose to implement triple vectors using this
12346 attribute. */
12347 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12348 if (attr)
12349 {
12350 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
12351 TYPE_LENGTH (type) = DW_UNSND (attr);
12352 else
12353 complaint (&symfile_complaints,
12354 _("DW_AT_byte_size for array type smaller "
12355 "than the total size of elements"));
12356 }
12357
12358 name = dwarf2_name (die, cu);
12359 if (name)
12360 TYPE_NAME (type) = name;
12361
12362 /* Install the type in the die. */
12363 set_die_type (die, type, cu);
12364
12365 /* set_die_type should be already done. */
12366 set_descriptive_type (type, die, cu);
12367
12368 do_cleanups (back_to);
12369
12370 return type;
12371 }
12372
12373 static enum dwarf_array_dim_ordering
12374 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
12375 {
12376 struct attribute *attr;
12377
12378 attr = dwarf2_attr (die, DW_AT_ordering, cu);
12379
12380 if (attr) return DW_SND (attr);
12381
12382 /* GNU F77 is a special case, as at 08/2004 array type info is the
12383 opposite order to the dwarf2 specification, but data is still
12384 laid out as per normal fortran.
12385
12386 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
12387 version checking. */
12388
12389 if (cu->language == language_fortran
12390 && cu->producer && strstr (cu->producer, "GNU F77"))
12391 {
12392 return DW_ORD_row_major;
12393 }
12394
12395 switch (cu->language_defn->la_array_ordering)
12396 {
12397 case array_column_major:
12398 return DW_ORD_col_major;
12399 case array_row_major:
12400 default:
12401 return DW_ORD_row_major;
12402 };
12403 }
12404
12405 /* Extract all information from a DW_TAG_set_type DIE and put it in
12406 the DIE's type field. */
12407
12408 static struct type *
12409 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
12410 {
12411 struct type *domain_type, *set_type;
12412 struct attribute *attr;
12413
12414 domain_type = die_type (die, cu);
12415
12416 /* The die_type call above may have already set the type for this DIE. */
12417 set_type = get_die_type (die, cu);
12418 if (set_type)
12419 return set_type;
12420
12421 set_type = create_set_type (NULL, domain_type);
12422
12423 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12424 if (attr)
12425 TYPE_LENGTH (set_type) = DW_UNSND (attr);
12426
12427 return set_die_type (die, set_type, cu);
12428 }
12429
12430 /* A helper for read_common_block that creates a locexpr baton.
12431 SYM is the symbol which we are marking as computed.
12432 COMMON_DIE is the DIE for the common block.
12433 COMMON_LOC is the location expression attribute for the common
12434 block itself.
12435 MEMBER_LOC is the location expression attribute for the particular
12436 member of the common block that we are processing.
12437 CU is the CU from which the above come. */
12438
12439 static void
12440 mark_common_block_symbol_computed (struct symbol *sym,
12441 struct die_info *common_die,
12442 struct attribute *common_loc,
12443 struct attribute *member_loc,
12444 struct dwarf2_cu *cu)
12445 {
12446 struct objfile *objfile = dwarf2_per_objfile->objfile;
12447 struct dwarf2_locexpr_baton *baton;
12448 gdb_byte *ptr;
12449 unsigned int cu_off;
12450 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
12451 LONGEST offset = 0;
12452
12453 gdb_assert (common_loc && member_loc);
12454 gdb_assert (attr_form_is_block (common_loc));
12455 gdb_assert (attr_form_is_block (member_loc)
12456 || attr_form_is_constant (member_loc));
12457
12458 baton = obstack_alloc (&objfile->objfile_obstack,
12459 sizeof (struct dwarf2_locexpr_baton));
12460 baton->per_cu = cu->per_cu;
12461 gdb_assert (baton->per_cu);
12462
12463 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
12464
12465 if (attr_form_is_constant (member_loc))
12466 {
12467 offset = dwarf2_get_attr_constant_value (member_loc, 0);
12468 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
12469 }
12470 else
12471 baton->size += DW_BLOCK (member_loc)->size;
12472
12473 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
12474 baton->data = ptr;
12475
12476 *ptr++ = DW_OP_call4;
12477 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
12478 store_unsigned_integer (ptr, 4, byte_order, cu_off);
12479 ptr += 4;
12480
12481 if (attr_form_is_constant (member_loc))
12482 {
12483 *ptr++ = DW_OP_addr;
12484 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
12485 ptr += cu->header.addr_size;
12486 }
12487 else
12488 {
12489 /* We have to copy the data here, because DW_OP_call4 will only
12490 use a DW_AT_location attribute. */
12491 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
12492 ptr += DW_BLOCK (member_loc)->size;
12493 }
12494
12495 *ptr++ = DW_OP_plus;
12496 gdb_assert (ptr - baton->data == baton->size);
12497
12498 SYMBOL_LOCATION_BATON (sym) = baton;
12499 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
12500 }
12501
12502 /* Create appropriate locally-scoped variables for all the
12503 DW_TAG_common_block entries. Also create a struct common_block
12504 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
12505 is used to sepate the common blocks name namespace from regular
12506 variable names. */
12507
12508 static void
12509 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
12510 {
12511 struct attribute *attr;
12512
12513 attr = dwarf2_attr (die, DW_AT_location, cu);
12514 if (attr)
12515 {
12516 /* Support the .debug_loc offsets. */
12517 if (attr_form_is_block (attr))
12518 {
12519 /* Ok. */
12520 }
12521 else if (attr_form_is_section_offset (attr))
12522 {
12523 dwarf2_complex_location_expr_complaint ();
12524 attr = NULL;
12525 }
12526 else
12527 {
12528 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12529 "common block member");
12530 attr = NULL;
12531 }
12532 }
12533
12534 if (die->child != NULL)
12535 {
12536 struct objfile *objfile = cu->objfile;
12537 struct die_info *child_die;
12538 size_t n_entries = 0, size;
12539 struct common_block *common_block;
12540 struct symbol *sym;
12541
12542 for (child_die = die->child;
12543 child_die && child_die->tag;
12544 child_die = sibling_die (child_die))
12545 ++n_entries;
12546
12547 size = (sizeof (struct common_block)
12548 + (n_entries - 1) * sizeof (struct symbol *));
12549 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12550 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12551 common_block->n_entries = 0;
12552
12553 for (child_die = die->child;
12554 child_die && child_die->tag;
12555 child_die = sibling_die (child_die))
12556 {
12557 /* Create the symbol in the DW_TAG_common_block block in the current
12558 symbol scope. */
12559 sym = new_symbol (child_die, NULL, cu);
12560 if (sym != NULL)
12561 {
12562 struct attribute *member_loc;
12563
12564 common_block->contents[common_block->n_entries++] = sym;
12565
12566 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12567 cu);
12568 if (member_loc)
12569 {
12570 /* GDB has handled this for a long time, but it is
12571 not specified by DWARF. It seems to have been
12572 emitted by gfortran at least as recently as:
12573 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12574 complaint (&symfile_complaints,
12575 _("Variable in common block has "
12576 "DW_AT_data_member_location "
12577 "- DIE at 0x%x [in module %s]"),
12578 child_die->offset.sect_off, cu->objfile->name);
12579
12580 if (attr_form_is_section_offset (member_loc))
12581 dwarf2_complex_location_expr_complaint ();
12582 else if (attr_form_is_constant (member_loc)
12583 || attr_form_is_block (member_loc))
12584 {
12585 if (attr)
12586 mark_common_block_symbol_computed (sym, die, attr,
12587 member_loc, cu);
12588 }
12589 else
12590 dwarf2_complex_location_expr_complaint ();
12591 }
12592 }
12593 }
12594
12595 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12596 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
12597 }
12598 }
12599
12600 /* Create a type for a C++ namespace. */
12601
12602 static struct type *
12603 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
12604 {
12605 struct objfile *objfile = cu->objfile;
12606 const char *previous_prefix, *name;
12607 int is_anonymous;
12608 struct type *type;
12609
12610 /* For extensions, reuse the type of the original namespace. */
12611 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12612 {
12613 struct die_info *ext_die;
12614 struct dwarf2_cu *ext_cu = cu;
12615
12616 ext_die = dwarf2_extension (die, &ext_cu);
12617 type = read_type_die (ext_die, ext_cu);
12618
12619 /* EXT_CU may not be the same as CU.
12620 Ensure TYPE is recorded with CU in die_type_hash. */
12621 return set_die_type (die, type, cu);
12622 }
12623
12624 name = namespace_name (die, &is_anonymous, cu);
12625
12626 /* Now build the name of the current namespace. */
12627
12628 previous_prefix = determine_prefix (die, cu);
12629 if (previous_prefix[0] != '\0')
12630 name = typename_concat (&objfile->objfile_obstack,
12631 previous_prefix, name, 0, cu);
12632
12633 /* Create the type. */
12634 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12635 objfile);
12636 TYPE_NAME (type) = name;
12637 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12638
12639 return set_die_type (die, type, cu);
12640 }
12641
12642 /* Read a C++ namespace. */
12643
12644 static void
12645 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12646 {
12647 struct objfile *objfile = cu->objfile;
12648 int is_anonymous;
12649
12650 /* Add a symbol associated to this if we haven't seen the namespace
12651 before. Also, add a using directive if it's an anonymous
12652 namespace. */
12653
12654 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
12655 {
12656 struct type *type;
12657
12658 type = read_type_die (die, cu);
12659 new_symbol (die, type, cu);
12660
12661 namespace_name (die, &is_anonymous, cu);
12662 if (is_anonymous)
12663 {
12664 const char *previous_prefix = determine_prefix (die, cu);
12665
12666 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12667 NULL, NULL, 0, &objfile->objfile_obstack);
12668 }
12669 }
12670
12671 if (die->child != NULL)
12672 {
12673 struct die_info *child_die = die->child;
12674
12675 while (child_die && child_die->tag)
12676 {
12677 process_die (child_die, cu);
12678 child_die = sibling_die (child_die);
12679 }
12680 }
12681 }
12682
12683 /* Read a Fortran module as type. This DIE can be only a declaration used for
12684 imported module. Still we need that type as local Fortran "use ... only"
12685 declaration imports depend on the created type in determine_prefix. */
12686
12687 static struct type *
12688 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12689 {
12690 struct objfile *objfile = cu->objfile;
12691 const char *module_name;
12692 struct type *type;
12693
12694 module_name = dwarf2_name (die, cu);
12695 if (!module_name)
12696 complaint (&symfile_complaints,
12697 _("DW_TAG_module has no name, offset 0x%x"),
12698 die->offset.sect_off);
12699 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12700
12701 /* determine_prefix uses TYPE_TAG_NAME. */
12702 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12703
12704 return set_die_type (die, type, cu);
12705 }
12706
12707 /* Read a Fortran module. */
12708
12709 static void
12710 read_module (struct die_info *die, struct dwarf2_cu *cu)
12711 {
12712 struct die_info *child_die = die->child;
12713
12714 while (child_die && child_die->tag)
12715 {
12716 process_die (child_die, cu);
12717 child_die = sibling_die (child_die);
12718 }
12719 }
12720
12721 /* Return the name of the namespace represented by DIE. Set
12722 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12723 namespace. */
12724
12725 static const char *
12726 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
12727 {
12728 struct die_info *current_die;
12729 const char *name = NULL;
12730
12731 /* Loop through the extensions until we find a name. */
12732
12733 for (current_die = die;
12734 current_die != NULL;
12735 current_die = dwarf2_extension (die, &cu))
12736 {
12737 name = dwarf2_name (current_die, cu);
12738 if (name != NULL)
12739 break;
12740 }
12741
12742 /* Is it an anonymous namespace? */
12743
12744 *is_anonymous = (name == NULL);
12745 if (*is_anonymous)
12746 name = CP_ANONYMOUS_NAMESPACE_STR;
12747
12748 return name;
12749 }
12750
12751 /* Extract all information from a DW_TAG_pointer_type DIE and add to
12752 the user defined type vector. */
12753
12754 static struct type *
12755 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
12756 {
12757 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
12758 struct comp_unit_head *cu_header = &cu->header;
12759 struct type *type;
12760 struct attribute *attr_byte_size;
12761 struct attribute *attr_address_class;
12762 int byte_size, addr_class;
12763 struct type *target_type;
12764
12765 target_type = die_type (die, cu);
12766
12767 /* The die_type call above may have already set the type for this DIE. */
12768 type = get_die_type (die, cu);
12769 if (type)
12770 return type;
12771
12772 type = lookup_pointer_type (target_type);
12773
12774 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
12775 if (attr_byte_size)
12776 byte_size = DW_UNSND (attr_byte_size);
12777 else
12778 byte_size = cu_header->addr_size;
12779
12780 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
12781 if (attr_address_class)
12782 addr_class = DW_UNSND (attr_address_class);
12783 else
12784 addr_class = DW_ADDR_none;
12785
12786 /* If the pointer size or address class is different than the
12787 default, create a type variant marked as such and set the
12788 length accordingly. */
12789 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
12790 {
12791 if (gdbarch_address_class_type_flags_p (gdbarch))
12792 {
12793 int type_flags;
12794
12795 type_flags = gdbarch_address_class_type_flags
12796 (gdbarch, byte_size, addr_class);
12797 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12798 == 0);
12799 type = make_type_with_address_space (type, type_flags);
12800 }
12801 else if (TYPE_LENGTH (type) != byte_size)
12802 {
12803 complaint (&symfile_complaints,
12804 _("invalid pointer size %d"), byte_size);
12805 }
12806 else
12807 {
12808 /* Should we also complain about unhandled address classes? */
12809 }
12810 }
12811
12812 TYPE_LENGTH (type) = byte_size;
12813 return set_die_type (die, type, cu);
12814 }
12815
12816 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12817 the user defined type vector. */
12818
12819 static struct type *
12820 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
12821 {
12822 struct type *type;
12823 struct type *to_type;
12824 struct type *domain;
12825
12826 to_type = die_type (die, cu);
12827 domain = die_containing_type (die, cu);
12828
12829 /* The calls above may have already set the type for this DIE. */
12830 type = get_die_type (die, cu);
12831 if (type)
12832 return type;
12833
12834 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12835 type = lookup_methodptr_type (to_type);
12836 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12837 {
12838 struct type *new_type = alloc_type (cu->objfile);
12839
12840 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12841 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12842 TYPE_VARARGS (to_type));
12843 type = lookup_methodptr_type (new_type);
12844 }
12845 else
12846 type = lookup_memberptr_type (to_type, domain);
12847
12848 return set_die_type (die, type, cu);
12849 }
12850
12851 /* Extract all information from a DW_TAG_reference_type DIE and add to
12852 the user defined type vector. */
12853
12854 static struct type *
12855 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
12856 {
12857 struct comp_unit_head *cu_header = &cu->header;
12858 struct type *type, *target_type;
12859 struct attribute *attr;
12860
12861 target_type = die_type (die, cu);
12862
12863 /* The die_type call above may have already set the type for this DIE. */
12864 type = get_die_type (die, cu);
12865 if (type)
12866 return type;
12867
12868 type = lookup_reference_type (target_type);
12869 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12870 if (attr)
12871 {
12872 TYPE_LENGTH (type) = DW_UNSND (attr);
12873 }
12874 else
12875 {
12876 TYPE_LENGTH (type) = cu_header->addr_size;
12877 }
12878 return set_die_type (die, type, cu);
12879 }
12880
12881 static struct type *
12882 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
12883 {
12884 struct type *base_type, *cv_type;
12885
12886 base_type = die_type (die, cu);
12887
12888 /* The die_type call above may have already set the type for this DIE. */
12889 cv_type = get_die_type (die, cu);
12890 if (cv_type)
12891 return cv_type;
12892
12893 /* In case the const qualifier is applied to an array type, the element type
12894 is so qualified, not the array type (section 6.7.3 of C99). */
12895 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12896 {
12897 struct type *el_type, *inner_array;
12898
12899 base_type = copy_type (base_type);
12900 inner_array = base_type;
12901
12902 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12903 {
12904 TYPE_TARGET_TYPE (inner_array) =
12905 copy_type (TYPE_TARGET_TYPE (inner_array));
12906 inner_array = TYPE_TARGET_TYPE (inner_array);
12907 }
12908
12909 el_type = TYPE_TARGET_TYPE (inner_array);
12910 TYPE_TARGET_TYPE (inner_array) =
12911 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12912
12913 return set_die_type (die, base_type, cu);
12914 }
12915
12916 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12917 return set_die_type (die, cv_type, cu);
12918 }
12919
12920 static struct type *
12921 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
12922 {
12923 struct type *base_type, *cv_type;
12924
12925 base_type = die_type (die, cu);
12926
12927 /* The die_type call above may have already set the type for this DIE. */
12928 cv_type = get_die_type (die, cu);
12929 if (cv_type)
12930 return cv_type;
12931
12932 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12933 return set_die_type (die, cv_type, cu);
12934 }
12935
12936 /* Handle DW_TAG_restrict_type. */
12937
12938 static struct type *
12939 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12940 {
12941 struct type *base_type, *cv_type;
12942
12943 base_type = die_type (die, cu);
12944
12945 /* The die_type call above may have already set the type for this DIE. */
12946 cv_type = get_die_type (die, cu);
12947 if (cv_type)
12948 return cv_type;
12949
12950 cv_type = make_restrict_type (base_type);
12951 return set_die_type (die, cv_type, cu);
12952 }
12953
12954 /* Extract all information from a DW_TAG_string_type DIE and add to
12955 the user defined type vector. It isn't really a user defined type,
12956 but it behaves like one, with other DIE's using an AT_user_def_type
12957 attribute to reference it. */
12958
12959 static struct type *
12960 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
12961 {
12962 struct objfile *objfile = cu->objfile;
12963 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12964 struct type *type, *range_type, *index_type, *char_type;
12965 struct attribute *attr;
12966 unsigned int length;
12967
12968 attr = dwarf2_attr (die, DW_AT_string_length, cu);
12969 if (attr)
12970 {
12971 length = DW_UNSND (attr);
12972 }
12973 else
12974 {
12975 /* Check for the DW_AT_byte_size attribute. */
12976 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12977 if (attr)
12978 {
12979 length = DW_UNSND (attr);
12980 }
12981 else
12982 {
12983 length = 1;
12984 }
12985 }
12986
12987 index_type = objfile_type (objfile)->builtin_int;
12988 range_type = create_range_type (NULL, index_type, 1, length);
12989 char_type = language_string_char_type (cu->language_defn, gdbarch);
12990 type = create_string_type (NULL, char_type, range_type);
12991
12992 return set_die_type (die, type, cu);
12993 }
12994
12995 /* Assuming that DIE corresponds to a function, returns nonzero
12996 if the function is prototyped. */
12997
12998 static int
12999 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
13000 {
13001 struct attribute *attr;
13002
13003 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
13004 if (attr && (DW_UNSND (attr) != 0))
13005 return 1;
13006
13007 /* The DWARF standard implies that the DW_AT_prototyped attribute
13008 is only meaninful for C, but the concept also extends to other
13009 languages that allow unprototyped functions (Eg: Objective C).
13010 For all other languages, assume that functions are always
13011 prototyped. */
13012 if (cu->language != language_c
13013 && cu->language != language_objc
13014 && cu->language != language_opencl)
13015 return 1;
13016
13017 /* RealView does not emit DW_AT_prototyped. We can not distinguish
13018 prototyped and unprototyped functions; default to prototyped,
13019 since that is more common in modern code (and RealView warns
13020 about unprototyped functions). */
13021 if (producer_is_realview (cu->producer))
13022 return 1;
13023
13024 return 0;
13025 }
13026
13027 /* Handle DIES due to C code like:
13028
13029 struct foo
13030 {
13031 int (*funcp)(int a, long l);
13032 int b;
13033 };
13034
13035 ('funcp' generates a DW_TAG_subroutine_type DIE). */
13036
13037 static struct type *
13038 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
13039 {
13040 struct objfile *objfile = cu->objfile;
13041 struct type *type; /* Type that this function returns. */
13042 struct type *ftype; /* Function that returns above type. */
13043 struct attribute *attr;
13044
13045 type = die_type (die, cu);
13046
13047 /* The die_type call above may have already set the type for this DIE. */
13048 ftype = get_die_type (die, cu);
13049 if (ftype)
13050 return ftype;
13051
13052 ftype = lookup_function_type (type);
13053
13054 if (prototyped_function_p (die, cu))
13055 TYPE_PROTOTYPED (ftype) = 1;
13056
13057 /* Store the calling convention in the type if it's available in
13058 the subroutine die. Otherwise set the calling convention to
13059 the default value DW_CC_normal. */
13060 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
13061 if (attr)
13062 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
13063 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
13064 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
13065 else
13066 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
13067
13068 /* We need to add the subroutine type to the die immediately so
13069 we don't infinitely recurse when dealing with parameters
13070 declared as the same subroutine type. */
13071 set_die_type (die, ftype, cu);
13072
13073 if (die->child != NULL)
13074 {
13075 struct type *void_type = objfile_type (objfile)->builtin_void;
13076 struct die_info *child_die;
13077 int nparams, iparams;
13078
13079 /* Count the number of parameters.
13080 FIXME: GDB currently ignores vararg functions, but knows about
13081 vararg member functions. */
13082 nparams = 0;
13083 child_die = die->child;
13084 while (child_die && child_die->tag)
13085 {
13086 if (child_die->tag == DW_TAG_formal_parameter)
13087 nparams++;
13088 else if (child_die->tag == DW_TAG_unspecified_parameters)
13089 TYPE_VARARGS (ftype) = 1;
13090 child_die = sibling_die (child_die);
13091 }
13092
13093 /* Allocate storage for parameters and fill them in. */
13094 TYPE_NFIELDS (ftype) = nparams;
13095 TYPE_FIELDS (ftype) = (struct field *)
13096 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
13097
13098 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
13099 even if we error out during the parameters reading below. */
13100 for (iparams = 0; iparams < nparams; iparams++)
13101 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
13102
13103 iparams = 0;
13104 child_die = die->child;
13105 while (child_die && child_die->tag)
13106 {
13107 if (child_die->tag == DW_TAG_formal_parameter)
13108 {
13109 struct type *arg_type;
13110
13111 /* DWARF version 2 has no clean way to discern C++
13112 static and non-static member functions. G++ helps
13113 GDB by marking the first parameter for non-static
13114 member functions (which is the this pointer) as
13115 artificial. We pass this information to
13116 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
13117
13118 DWARF version 3 added DW_AT_object_pointer, which GCC
13119 4.5 does not yet generate. */
13120 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
13121 if (attr)
13122 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
13123 else
13124 {
13125 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
13126
13127 /* GCC/43521: In java, the formal parameter
13128 "this" is sometimes not marked with DW_AT_artificial. */
13129 if (cu->language == language_java)
13130 {
13131 const char *name = dwarf2_name (child_die, cu);
13132
13133 if (name && !strcmp (name, "this"))
13134 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
13135 }
13136 }
13137 arg_type = die_type (child_die, cu);
13138
13139 /* RealView does not mark THIS as const, which the testsuite
13140 expects. GCC marks THIS as const in method definitions,
13141 but not in the class specifications (GCC PR 43053). */
13142 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
13143 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
13144 {
13145 int is_this = 0;
13146 struct dwarf2_cu *arg_cu = cu;
13147 const char *name = dwarf2_name (child_die, cu);
13148
13149 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
13150 if (attr)
13151 {
13152 /* If the compiler emits this, use it. */
13153 if (follow_die_ref (die, attr, &arg_cu) == child_die)
13154 is_this = 1;
13155 }
13156 else if (name && strcmp (name, "this") == 0)
13157 /* Function definitions will have the argument names. */
13158 is_this = 1;
13159 else if (name == NULL && iparams == 0)
13160 /* Declarations may not have the names, so like
13161 elsewhere in GDB, assume an artificial first
13162 argument is "this". */
13163 is_this = 1;
13164
13165 if (is_this)
13166 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
13167 arg_type, 0);
13168 }
13169
13170 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
13171 iparams++;
13172 }
13173 child_die = sibling_die (child_die);
13174 }
13175 }
13176
13177 return ftype;
13178 }
13179
13180 static struct type *
13181 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
13182 {
13183 struct objfile *objfile = cu->objfile;
13184 const char *name = NULL;
13185 struct type *this_type, *target_type;
13186
13187 name = dwarf2_full_name (NULL, die, cu);
13188 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
13189 TYPE_FLAG_TARGET_STUB, NULL, objfile);
13190 TYPE_NAME (this_type) = name;
13191 set_die_type (die, this_type, cu);
13192 target_type = die_type (die, cu);
13193 if (target_type != this_type)
13194 TYPE_TARGET_TYPE (this_type) = target_type;
13195 else
13196 {
13197 /* Self-referential typedefs are, it seems, not allowed by the DWARF
13198 spec and cause infinite loops in GDB. */
13199 complaint (&symfile_complaints,
13200 _("Self-referential DW_TAG_typedef "
13201 "- DIE at 0x%x [in module %s]"),
13202 die->offset.sect_off, objfile->name);
13203 TYPE_TARGET_TYPE (this_type) = NULL;
13204 }
13205 return this_type;
13206 }
13207
13208 /* Find a representation of a given base type and install
13209 it in the TYPE field of the die. */
13210
13211 static struct type *
13212 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
13213 {
13214 struct objfile *objfile = cu->objfile;
13215 struct type *type;
13216 struct attribute *attr;
13217 int encoding = 0, size = 0;
13218 const char *name;
13219 enum type_code code = TYPE_CODE_INT;
13220 int type_flags = 0;
13221 struct type *target_type = NULL;
13222
13223 attr = dwarf2_attr (die, DW_AT_encoding, cu);
13224 if (attr)
13225 {
13226 encoding = DW_UNSND (attr);
13227 }
13228 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13229 if (attr)
13230 {
13231 size = DW_UNSND (attr);
13232 }
13233 name = dwarf2_name (die, cu);
13234 if (!name)
13235 {
13236 complaint (&symfile_complaints,
13237 _("DW_AT_name missing from DW_TAG_base_type"));
13238 }
13239
13240 switch (encoding)
13241 {
13242 case DW_ATE_address:
13243 /* Turn DW_ATE_address into a void * pointer. */
13244 code = TYPE_CODE_PTR;
13245 type_flags |= TYPE_FLAG_UNSIGNED;
13246 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
13247 break;
13248 case DW_ATE_boolean:
13249 code = TYPE_CODE_BOOL;
13250 type_flags |= TYPE_FLAG_UNSIGNED;
13251 break;
13252 case DW_ATE_complex_float:
13253 code = TYPE_CODE_COMPLEX;
13254 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
13255 break;
13256 case DW_ATE_decimal_float:
13257 code = TYPE_CODE_DECFLOAT;
13258 break;
13259 case DW_ATE_float:
13260 code = TYPE_CODE_FLT;
13261 break;
13262 case DW_ATE_signed:
13263 break;
13264 case DW_ATE_unsigned:
13265 type_flags |= TYPE_FLAG_UNSIGNED;
13266 if (cu->language == language_fortran
13267 && name
13268 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
13269 code = TYPE_CODE_CHAR;
13270 break;
13271 case DW_ATE_signed_char:
13272 if (cu->language == language_ada || cu->language == language_m2
13273 || cu->language == language_pascal
13274 || cu->language == language_fortran)
13275 code = TYPE_CODE_CHAR;
13276 break;
13277 case DW_ATE_unsigned_char:
13278 if (cu->language == language_ada || cu->language == language_m2
13279 || cu->language == language_pascal
13280 || cu->language == language_fortran)
13281 code = TYPE_CODE_CHAR;
13282 type_flags |= TYPE_FLAG_UNSIGNED;
13283 break;
13284 case DW_ATE_UTF:
13285 /* We just treat this as an integer and then recognize the
13286 type by name elsewhere. */
13287 break;
13288
13289 default:
13290 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
13291 dwarf_type_encoding_name (encoding));
13292 break;
13293 }
13294
13295 type = init_type (code, size, type_flags, NULL, objfile);
13296 TYPE_NAME (type) = name;
13297 TYPE_TARGET_TYPE (type) = target_type;
13298
13299 if (name && strcmp (name, "char") == 0)
13300 TYPE_NOSIGN (type) = 1;
13301
13302 return set_die_type (die, type, cu);
13303 }
13304
13305 /* Read the given DW_AT_subrange DIE. */
13306
13307 static struct type *
13308 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
13309 {
13310 struct type *base_type, *orig_base_type;
13311 struct type *range_type;
13312 struct attribute *attr;
13313 LONGEST low, high;
13314 int low_default_is_valid;
13315 const char *name;
13316 LONGEST negative_mask;
13317
13318 orig_base_type = die_type (die, cu);
13319 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
13320 whereas the real type might be. So, we use ORIG_BASE_TYPE when
13321 creating the range type, but we use the result of check_typedef
13322 when examining properties of the type. */
13323 base_type = check_typedef (orig_base_type);
13324
13325 /* The die_type call above may have already set the type for this DIE. */
13326 range_type = get_die_type (die, cu);
13327 if (range_type)
13328 return range_type;
13329
13330 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
13331 omitting DW_AT_lower_bound. */
13332 switch (cu->language)
13333 {
13334 case language_c:
13335 case language_cplus:
13336 low = 0;
13337 low_default_is_valid = 1;
13338 break;
13339 case language_fortran:
13340 low = 1;
13341 low_default_is_valid = 1;
13342 break;
13343 case language_d:
13344 case language_java:
13345 case language_objc:
13346 low = 0;
13347 low_default_is_valid = (cu->header.version >= 4);
13348 break;
13349 case language_ada:
13350 case language_m2:
13351 case language_pascal:
13352 low = 1;
13353 low_default_is_valid = (cu->header.version >= 4);
13354 break;
13355 default:
13356 low = 0;
13357 low_default_is_valid = 0;
13358 break;
13359 }
13360
13361 /* FIXME: For variable sized arrays either of these could be
13362 a variable rather than a constant value. We'll allow it,
13363 but we don't know how to handle it. */
13364 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
13365 if (attr)
13366 low = dwarf2_get_attr_constant_value (attr, low);
13367 else if (!low_default_is_valid)
13368 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
13369 "- DIE at 0x%x [in module %s]"),
13370 die->offset.sect_off, cu->objfile->name);
13371
13372 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
13373 if (attr)
13374 {
13375 if (attr_form_is_block (attr) || attr_form_is_ref (attr))
13376 {
13377 /* GCC encodes arrays with unspecified or dynamic length
13378 with a DW_FORM_block1 attribute or a reference attribute.
13379 FIXME: GDB does not yet know how to handle dynamic
13380 arrays properly, treat them as arrays with unspecified
13381 length for now.
13382
13383 FIXME: jimb/2003-09-22: GDB does not really know
13384 how to handle arrays of unspecified length
13385 either; we just represent them as zero-length
13386 arrays. Choose an appropriate upper bound given
13387 the lower bound we've computed above. */
13388 high = low - 1;
13389 }
13390 else
13391 high = dwarf2_get_attr_constant_value (attr, 1);
13392 }
13393 else
13394 {
13395 attr = dwarf2_attr (die, DW_AT_count, cu);
13396 if (attr)
13397 {
13398 int count = dwarf2_get_attr_constant_value (attr, 1);
13399 high = low + count - 1;
13400 }
13401 else
13402 {
13403 /* Unspecified array length. */
13404 high = low - 1;
13405 }
13406 }
13407
13408 /* Dwarf-2 specifications explicitly allows to create subrange types
13409 without specifying a base type.
13410 In that case, the base type must be set to the type of
13411 the lower bound, upper bound or count, in that order, if any of these
13412 three attributes references an object that has a type.
13413 If no base type is found, the Dwarf-2 specifications say that
13414 a signed integer type of size equal to the size of an address should
13415 be used.
13416 For the following C code: `extern char gdb_int [];'
13417 GCC produces an empty range DIE.
13418 FIXME: muller/2010-05-28: Possible references to object for low bound,
13419 high bound or count are not yet handled by this code. */
13420 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
13421 {
13422 struct objfile *objfile = cu->objfile;
13423 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13424 int addr_size = gdbarch_addr_bit (gdbarch) /8;
13425 struct type *int_type = objfile_type (objfile)->builtin_int;
13426
13427 /* Test "int", "long int", and "long long int" objfile types,
13428 and select the first one having a size above or equal to the
13429 architecture address size. */
13430 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13431 base_type = int_type;
13432 else
13433 {
13434 int_type = objfile_type (objfile)->builtin_long;
13435 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13436 base_type = int_type;
13437 else
13438 {
13439 int_type = objfile_type (objfile)->builtin_long_long;
13440 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13441 base_type = int_type;
13442 }
13443 }
13444 }
13445
13446 negative_mask =
13447 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
13448 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
13449 low |= negative_mask;
13450 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
13451 high |= negative_mask;
13452
13453 range_type = create_range_type (NULL, orig_base_type, low, high);
13454
13455 /* Mark arrays with dynamic length at least as an array of unspecified
13456 length. GDB could check the boundary but before it gets implemented at
13457 least allow accessing the array elements. */
13458 if (attr && attr_form_is_block (attr))
13459 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13460
13461 /* Ada expects an empty array on no boundary attributes. */
13462 if (attr == NULL && cu->language != language_ada)
13463 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13464
13465 name = dwarf2_name (die, cu);
13466 if (name)
13467 TYPE_NAME (range_type) = name;
13468
13469 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13470 if (attr)
13471 TYPE_LENGTH (range_type) = DW_UNSND (attr);
13472
13473 set_die_type (die, range_type, cu);
13474
13475 /* set_die_type should be already done. */
13476 set_descriptive_type (range_type, die, cu);
13477
13478 return range_type;
13479 }
13480
13481 static struct type *
13482 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
13483 {
13484 struct type *type;
13485
13486 /* For now, we only support the C meaning of an unspecified type: void. */
13487
13488 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
13489 TYPE_NAME (type) = dwarf2_name (die, cu);
13490
13491 return set_die_type (die, type, cu);
13492 }
13493
13494 /* Read a single die and all its descendents. Set the die's sibling
13495 field to NULL; set other fields in the die correctly, and set all
13496 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
13497 location of the info_ptr after reading all of those dies. PARENT
13498 is the parent of the die in question. */
13499
13500 static struct die_info *
13501 read_die_and_children (const struct die_reader_specs *reader,
13502 const gdb_byte *info_ptr,
13503 const gdb_byte **new_info_ptr,
13504 struct die_info *parent)
13505 {
13506 struct die_info *die;
13507 const gdb_byte *cur_ptr;
13508 int has_children;
13509
13510 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
13511 if (die == NULL)
13512 {
13513 *new_info_ptr = cur_ptr;
13514 return NULL;
13515 }
13516 store_in_ref_table (die, reader->cu);
13517
13518 if (has_children)
13519 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
13520 else
13521 {
13522 die->child = NULL;
13523 *new_info_ptr = cur_ptr;
13524 }
13525
13526 die->sibling = NULL;
13527 die->parent = parent;
13528 return die;
13529 }
13530
13531 /* Read a die, all of its descendents, and all of its siblings; set
13532 all of the fields of all of the dies correctly. Arguments are as
13533 in read_die_and_children. */
13534
13535 static struct die_info *
13536 read_die_and_siblings_1 (const struct die_reader_specs *reader,
13537 const gdb_byte *info_ptr,
13538 const gdb_byte **new_info_ptr,
13539 struct die_info *parent)
13540 {
13541 struct die_info *first_die, *last_sibling;
13542 const gdb_byte *cur_ptr;
13543
13544 cur_ptr = info_ptr;
13545 first_die = last_sibling = NULL;
13546
13547 while (1)
13548 {
13549 struct die_info *die
13550 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
13551
13552 if (die == NULL)
13553 {
13554 *new_info_ptr = cur_ptr;
13555 return first_die;
13556 }
13557
13558 if (!first_die)
13559 first_die = die;
13560 else
13561 last_sibling->sibling = die;
13562
13563 last_sibling = die;
13564 }
13565 }
13566
13567 /* Read a die, all of its descendents, and all of its siblings; set
13568 all of the fields of all of the dies correctly. Arguments are as
13569 in read_die_and_children.
13570 This the main entry point for reading a DIE and all its children. */
13571
13572 static struct die_info *
13573 read_die_and_siblings (const struct die_reader_specs *reader,
13574 const gdb_byte *info_ptr,
13575 const gdb_byte **new_info_ptr,
13576 struct die_info *parent)
13577 {
13578 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
13579 new_info_ptr, parent);
13580
13581 if (dwarf2_die_debug)
13582 {
13583 fprintf_unfiltered (gdb_stdlog,
13584 "Read die from %s@0x%x of %s:\n",
13585 bfd_section_name (reader->abfd,
13586 reader->die_section->asection),
13587 (unsigned) (info_ptr - reader->die_section->buffer),
13588 bfd_get_filename (reader->abfd));
13589 dump_die (die, dwarf2_die_debug);
13590 }
13591
13592 return die;
13593 }
13594
13595 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13596 attributes.
13597 The caller is responsible for filling in the extra attributes
13598 and updating (*DIEP)->num_attrs.
13599 Set DIEP to point to a newly allocated die with its information,
13600 except for its child, sibling, and parent fields.
13601 Set HAS_CHILDREN to tell whether the die has children or not. */
13602
13603 static const gdb_byte *
13604 read_full_die_1 (const struct die_reader_specs *reader,
13605 struct die_info **diep, const gdb_byte *info_ptr,
13606 int *has_children, int num_extra_attrs)
13607 {
13608 unsigned int abbrev_number, bytes_read, i;
13609 sect_offset offset;
13610 struct abbrev_info *abbrev;
13611 struct die_info *die;
13612 struct dwarf2_cu *cu = reader->cu;
13613 bfd *abfd = reader->abfd;
13614
13615 offset.sect_off = info_ptr - reader->buffer;
13616 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13617 info_ptr += bytes_read;
13618 if (!abbrev_number)
13619 {
13620 *diep = NULL;
13621 *has_children = 0;
13622 return info_ptr;
13623 }
13624
13625 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
13626 if (!abbrev)
13627 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13628 abbrev_number,
13629 bfd_get_filename (abfd));
13630
13631 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
13632 die->offset = offset;
13633 die->tag = abbrev->tag;
13634 die->abbrev = abbrev_number;
13635
13636 /* Make the result usable.
13637 The caller needs to update num_attrs after adding the extra
13638 attributes. */
13639 die->num_attrs = abbrev->num_attrs;
13640
13641 for (i = 0; i < abbrev->num_attrs; ++i)
13642 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13643 info_ptr);
13644
13645 *diep = die;
13646 *has_children = abbrev->has_children;
13647 return info_ptr;
13648 }
13649
13650 /* Read a die and all its attributes.
13651 Set DIEP to point to a newly allocated die with its information,
13652 except for its child, sibling, and parent fields.
13653 Set HAS_CHILDREN to tell whether the die has children or not. */
13654
13655 static const gdb_byte *
13656 read_full_die (const struct die_reader_specs *reader,
13657 struct die_info **diep, const gdb_byte *info_ptr,
13658 int *has_children)
13659 {
13660 const gdb_byte *result;
13661
13662 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13663
13664 if (dwarf2_die_debug)
13665 {
13666 fprintf_unfiltered (gdb_stdlog,
13667 "Read die from %s@0x%x of %s:\n",
13668 bfd_section_name (reader->abfd,
13669 reader->die_section->asection),
13670 (unsigned) (info_ptr - reader->die_section->buffer),
13671 bfd_get_filename (reader->abfd));
13672 dump_die (*diep, dwarf2_die_debug);
13673 }
13674
13675 return result;
13676 }
13677 \f
13678 /* Abbreviation tables.
13679
13680 In DWARF version 2, the description of the debugging information is
13681 stored in a separate .debug_abbrev section. Before we read any
13682 dies from a section we read in all abbreviations and install them
13683 in a hash table. */
13684
13685 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13686
13687 static struct abbrev_info *
13688 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13689 {
13690 struct abbrev_info *abbrev;
13691
13692 abbrev = (struct abbrev_info *)
13693 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13694 memset (abbrev, 0, sizeof (struct abbrev_info));
13695 return abbrev;
13696 }
13697
13698 /* Add an abbreviation to the table. */
13699
13700 static void
13701 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13702 unsigned int abbrev_number,
13703 struct abbrev_info *abbrev)
13704 {
13705 unsigned int hash_number;
13706
13707 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13708 abbrev->next = abbrev_table->abbrevs[hash_number];
13709 abbrev_table->abbrevs[hash_number] = abbrev;
13710 }
13711
13712 /* Look up an abbrev in the table.
13713 Returns NULL if the abbrev is not found. */
13714
13715 static struct abbrev_info *
13716 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13717 unsigned int abbrev_number)
13718 {
13719 unsigned int hash_number;
13720 struct abbrev_info *abbrev;
13721
13722 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13723 abbrev = abbrev_table->abbrevs[hash_number];
13724
13725 while (abbrev)
13726 {
13727 if (abbrev->number == abbrev_number)
13728 return abbrev;
13729 abbrev = abbrev->next;
13730 }
13731 return NULL;
13732 }
13733
13734 /* Read in an abbrev table. */
13735
13736 static struct abbrev_table *
13737 abbrev_table_read_table (struct dwarf2_section_info *section,
13738 sect_offset offset)
13739 {
13740 struct objfile *objfile = dwarf2_per_objfile->objfile;
13741 bfd *abfd = section->asection->owner;
13742 struct abbrev_table *abbrev_table;
13743 const gdb_byte *abbrev_ptr;
13744 struct abbrev_info *cur_abbrev;
13745 unsigned int abbrev_number, bytes_read, abbrev_name;
13746 unsigned int abbrev_form;
13747 struct attr_abbrev *cur_attrs;
13748 unsigned int allocated_attrs;
13749
13750 abbrev_table = XMALLOC (struct abbrev_table);
13751 abbrev_table->offset = offset;
13752 obstack_init (&abbrev_table->abbrev_obstack);
13753 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13754 (ABBREV_HASH_SIZE
13755 * sizeof (struct abbrev_info *)));
13756 memset (abbrev_table->abbrevs, 0,
13757 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
13758
13759 dwarf2_read_section (objfile, section);
13760 abbrev_ptr = section->buffer + offset.sect_off;
13761 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13762 abbrev_ptr += bytes_read;
13763
13764 allocated_attrs = ATTR_ALLOC_CHUNK;
13765 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
13766
13767 /* Loop until we reach an abbrev number of 0. */
13768 while (abbrev_number)
13769 {
13770 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
13771
13772 /* read in abbrev header */
13773 cur_abbrev->number = abbrev_number;
13774 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13775 abbrev_ptr += bytes_read;
13776 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13777 abbrev_ptr += 1;
13778
13779 /* now read in declarations */
13780 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13781 abbrev_ptr += bytes_read;
13782 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13783 abbrev_ptr += bytes_read;
13784 while (abbrev_name)
13785 {
13786 if (cur_abbrev->num_attrs == allocated_attrs)
13787 {
13788 allocated_attrs += ATTR_ALLOC_CHUNK;
13789 cur_attrs
13790 = xrealloc (cur_attrs, (allocated_attrs
13791 * sizeof (struct attr_abbrev)));
13792 }
13793
13794 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13795 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
13796 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13797 abbrev_ptr += bytes_read;
13798 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13799 abbrev_ptr += bytes_read;
13800 }
13801
13802 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
13803 (cur_abbrev->num_attrs
13804 * sizeof (struct attr_abbrev)));
13805 memcpy (cur_abbrev->attrs, cur_attrs,
13806 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13807
13808 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
13809
13810 /* Get next abbreviation.
13811 Under Irix6 the abbreviations for a compilation unit are not
13812 always properly terminated with an abbrev number of 0.
13813 Exit loop if we encounter an abbreviation which we have
13814 already read (which means we are about to read the abbreviations
13815 for the next compile unit) or if the end of the abbreviation
13816 table is reached. */
13817 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
13818 break;
13819 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13820 abbrev_ptr += bytes_read;
13821 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
13822 break;
13823 }
13824
13825 xfree (cur_attrs);
13826 return abbrev_table;
13827 }
13828
13829 /* Free the resources held by ABBREV_TABLE. */
13830
13831 static void
13832 abbrev_table_free (struct abbrev_table *abbrev_table)
13833 {
13834 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13835 xfree (abbrev_table);
13836 }
13837
13838 /* Same as abbrev_table_free but as a cleanup.
13839 We pass in a pointer to the pointer to the table so that we can
13840 set the pointer to NULL when we're done. It also simplifies
13841 build_type_unit_groups. */
13842
13843 static void
13844 abbrev_table_free_cleanup (void *table_ptr)
13845 {
13846 struct abbrev_table **abbrev_table_ptr = table_ptr;
13847
13848 if (*abbrev_table_ptr != NULL)
13849 abbrev_table_free (*abbrev_table_ptr);
13850 *abbrev_table_ptr = NULL;
13851 }
13852
13853 /* Read the abbrev table for CU from ABBREV_SECTION. */
13854
13855 static void
13856 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13857 struct dwarf2_section_info *abbrev_section)
13858 {
13859 cu->abbrev_table =
13860 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13861 }
13862
13863 /* Release the memory used by the abbrev table for a compilation unit. */
13864
13865 static void
13866 dwarf2_free_abbrev_table (void *ptr_to_cu)
13867 {
13868 struct dwarf2_cu *cu = ptr_to_cu;
13869
13870 if (cu->abbrev_table != NULL)
13871 abbrev_table_free (cu->abbrev_table);
13872 /* Set this to NULL so that we SEGV if we try to read it later,
13873 and also because free_comp_unit verifies this is NULL. */
13874 cu->abbrev_table = NULL;
13875 }
13876 \f
13877 /* Returns nonzero if TAG represents a type that we might generate a partial
13878 symbol for. */
13879
13880 static int
13881 is_type_tag_for_partial (int tag)
13882 {
13883 switch (tag)
13884 {
13885 #if 0
13886 /* Some types that would be reasonable to generate partial symbols for,
13887 that we don't at present. */
13888 case DW_TAG_array_type:
13889 case DW_TAG_file_type:
13890 case DW_TAG_ptr_to_member_type:
13891 case DW_TAG_set_type:
13892 case DW_TAG_string_type:
13893 case DW_TAG_subroutine_type:
13894 #endif
13895 case DW_TAG_base_type:
13896 case DW_TAG_class_type:
13897 case DW_TAG_interface_type:
13898 case DW_TAG_enumeration_type:
13899 case DW_TAG_structure_type:
13900 case DW_TAG_subrange_type:
13901 case DW_TAG_typedef:
13902 case DW_TAG_union_type:
13903 return 1;
13904 default:
13905 return 0;
13906 }
13907 }
13908
13909 /* Load all DIEs that are interesting for partial symbols into memory. */
13910
13911 static struct partial_die_info *
13912 load_partial_dies (const struct die_reader_specs *reader,
13913 const gdb_byte *info_ptr, int building_psymtab)
13914 {
13915 struct dwarf2_cu *cu = reader->cu;
13916 struct objfile *objfile = cu->objfile;
13917 struct partial_die_info *part_die;
13918 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13919 struct abbrev_info *abbrev;
13920 unsigned int bytes_read;
13921 unsigned int load_all = 0;
13922 int nesting_level = 1;
13923
13924 parent_die = NULL;
13925 last_die = NULL;
13926
13927 gdb_assert (cu->per_cu != NULL);
13928 if (cu->per_cu->load_all_dies)
13929 load_all = 1;
13930
13931 cu->partial_dies
13932 = htab_create_alloc_ex (cu->header.length / 12,
13933 partial_die_hash,
13934 partial_die_eq,
13935 NULL,
13936 &cu->comp_unit_obstack,
13937 hashtab_obstack_allocate,
13938 dummy_obstack_deallocate);
13939
13940 part_die = obstack_alloc (&cu->comp_unit_obstack,
13941 sizeof (struct partial_die_info));
13942
13943 while (1)
13944 {
13945 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13946
13947 /* A NULL abbrev means the end of a series of children. */
13948 if (abbrev == NULL)
13949 {
13950 if (--nesting_level == 0)
13951 {
13952 /* PART_DIE was probably the last thing allocated on the
13953 comp_unit_obstack, so we could call obstack_free
13954 here. We don't do that because the waste is small,
13955 and will be cleaned up when we're done with this
13956 compilation unit. This way, we're also more robust
13957 against other users of the comp_unit_obstack. */
13958 return first_die;
13959 }
13960 info_ptr += bytes_read;
13961 last_die = parent_die;
13962 parent_die = parent_die->die_parent;
13963 continue;
13964 }
13965
13966 /* Check for template arguments. We never save these; if
13967 they're seen, we just mark the parent, and go on our way. */
13968 if (parent_die != NULL
13969 && cu->language == language_cplus
13970 && (abbrev->tag == DW_TAG_template_type_param
13971 || abbrev->tag == DW_TAG_template_value_param))
13972 {
13973 parent_die->has_template_arguments = 1;
13974
13975 if (!load_all)
13976 {
13977 /* We don't need a partial DIE for the template argument. */
13978 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13979 continue;
13980 }
13981 }
13982
13983 /* We only recurse into c++ subprograms looking for template arguments.
13984 Skip their other children. */
13985 if (!load_all
13986 && cu->language == language_cplus
13987 && parent_die != NULL
13988 && parent_die->tag == DW_TAG_subprogram)
13989 {
13990 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13991 continue;
13992 }
13993
13994 /* Check whether this DIE is interesting enough to save. Normally
13995 we would not be interested in members here, but there may be
13996 later variables referencing them via DW_AT_specification (for
13997 static members). */
13998 if (!load_all
13999 && !is_type_tag_for_partial (abbrev->tag)
14000 && abbrev->tag != DW_TAG_constant
14001 && abbrev->tag != DW_TAG_enumerator
14002 && abbrev->tag != DW_TAG_subprogram
14003 && abbrev->tag != DW_TAG_lexical_block
14004 && abbrev->tag != DW_TAG_variable
14005 && abbrev->tag != DW_TAG_namespace
14006 && abbrev->tag != DW_TAG_module
14007 && abbrev->tag != DW_TAG_member
14008 && abbrev->tag != DW_TAG_imported_unit)
14009 {
14010 /* Otherwise we skip to the next sibling, if any. */
14011 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
14012 continue;
14013 }
14014
14015 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
14016 info_ptr);
14017
14018 /* This two-pass algorithm for processing partial symbols has a
14019 high cost in cache pressure. Thus, handle some simple cases
14020 here which cover the majority of C partial symbols. DIEs
14021 which neither have specification tags in them, nor could have
14022 specification tags elsewhere pointing at them, can simply be
14023 processed and discarded.
14024
14025 This segment is also optional; scan_partial_symbols and
14026 add_partial_symbol will handle these DIEs if we chain
14027 them in normally. When compilers which do not emit large
14028 quantities of duplicate debug information are more common,
14029 this code can probably be removed. */
14030
14031 /* Any complete simple types at the top level (pretty much all
14032 of them, for a language without namespaces), can be processed
14033 directly. */
14034 if (parent_die == NULL
14035 && part_die->has_specification == 0
14036 && part_die->is_declaration == 0
14037 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
14038 || part_die->tag == DW_TAG_base_type
14039 || part_die->tag == DW_TAG_subrange_type))
14040 {
14041 if (building_psymtab && part_die->name != NULL)
14042 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
14043 VAR_DOMAIN, LOC_TYPEDEF,
14044 &objfile->static_psymbols,
14045 0, (CORE_ADDR) 0, cu->language, objfile);
14046 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
14047 continue;
14048 }
14049
14050 /* The exception for DW_TAG_typedef with has_children above is
14051 a workaround of GCC PR debug/47510. In the case of this complaint
14052 type_name_no_tag_or_error will error on such types later.
14053
14054 GDB skipped children of DW_TAG_typedef by the shortcut above and then
14055 it could not find the child DIEs referenced later, this is checked
14056 above. In correct DWARF DW_TAG_typedef should have no children. */
14057
14058 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
14059 complaint (&symfile_complaints,
14060 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
14061 "- DIE at 0x%x [in module %s]"),
14062 part_die->offset.sect_off, objfile->name);
14063
14064 /* If we're at the second level, and we're an enumerator, and
14065 our parent has no specification (meaning possibly lives in a
14066 namespace elsewhere), then we can add the partial symbol now
14067 instead of queueing it. */
14068 if (part_die->tag == DW_TAG_enumerator
14069 && parent_die != NULL
14070 && parent_die->die_parent == NULL
14071 && parent_die->tag == DW_TAG_enumeration_type
14072 && parent_die->has_specification == 0)
14073 {
14074 if (part_die->name == NULL)
14075 complaint (&symfile_complaints,
14076 _("malformed enumerator DIE ignored"));
14077 else if (building_psymtab)
14078 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
14079 VAR_DOMAIN, LOC_CONST,
14080 (cu->language == language_cplus
14081 || cu->language == language_java)
14082 ? &objfile->global_psymbols
14083 : &objfile->static_psymbols,
14084 0, (CORE_ADDR) 0, cu->language, objfile);
14085
14086 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
14087 continue;
14088 }
14089
14090 /* We'll save this DIE so link it in. */
14091 part_die->die_parent = parent_die;
14092 part_die->die_sibling = NULL;
14093 part_die->die_child = NULL;
14094
14095 if (last_die && last_die == parent_die)
14096 last_die->die_child = part_die;
14097 else if (last_die)
14098 last_die->die_sibling = part_die;
14099
14100 last_die = part_die;
14101
14102 if (first_die == NULL)
14103 first_die = part_die;
14104
14105 /* Maybe add the DIE to the hash table. Not all DIEs that we
14106 find interesting need to be in the hash table, because we
14107 also have the parent/sibling/child chains; only those that we
14108 might refer to by offset later during partial symbol reading.
14109
14110 For now this means things that might have be the target of a
14111 DW_AT_specification, DW_AT_abstract_origin, or
14112 DW_AT_extension. DW_AT_extension will refer only to
14113 namespaces; DW_AT_abstract_origin refers to functions (and
14114 many things under the function DIE, but we do not recurse
14115 into function DIEs during partial symbol reading) and
14116 possibly variables as well; DW_AT_specification refers to
14117 declarations. Declarations ought to have the DW_AT_declaration
14118 flag. It happens that GCC forgets to put it in sometimes, but
14119 only for functions, not for types.
14120
14121 Adding more things than necessary to the hash table is harmless
14122 except for the performance cost. Adding too few will result in
14123 wasted time in find_partial_die, when we reread the compilation
14124 unit with load_all_dies set. */
14125
14126 if (load_all
14127 || abbrev->tag == DW_TAG_constant
14128 || abbrev->tag == DW_TAG_subprogram
14129 || abbrev->tag == DW_TAG_variable
14130 || abbrev->tag == DW_TAG_namespace
14131 || part_die->is_declaration)
14132 {
14133 void **slot;
14134
14135 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
14136 part_die->offset.sect_off, INSERT);
14137 *slot = part_die;
14138 }
14139
14140 part_die = obstack_alloc (&cu->comp_unit_obstack,
14141 sizeof (struct partial_die_info));
14142
14143 /* For some DIEs we want to follow their children (if any). For C
14144 we have no reason to follow the children of structures; for other
14145 languages we have to, so that we can get at method physnames
14146 to infer fully qualified class names, for DW_AT_specification,
14147 and for C++ template arguments. For C++, we also look one level
14148 inside functions to find template arguments (if the name of the
14149 function does not already contain the template arguments).
14150
14151 For Ada, we need to scan the children of subprograms and lexical
14152 blocks as well because Ada allows the definition of nested
14153 entities that could be interesting for the debugger, such as
14154 nested subprograms for instance. */
14155 if (last_die->has_children
14156 && (load_all
14157 || last_die->tag == DW_TAG_namespace
14158 || last_die->tag == DW_TAG_module
14159 || last_die->tag == DW_TAG_enumeration_type
14160 || (cu->language == language_cplus
14161 && last_die->tag == DW_TAG_subprogram
14162 && (last_die->name == NULL
14163 || strchr (last_die->name, '<') == NULL))
14164 || (cu->language != language_c
14165 && (last_die->tag == DW_TAG_class_type
14166 || last_die->tag == DW_TAG_interface_type
14167 || last_die->tag == DW_TAG_structure_type
14168 || last_die->tag == DW_TAG_union_type))
14169 || (cu->language == language_ada
14170 && (last_die->tag == DW_TAG_subprogram
14171 || last_die->tag == DW_TAG_lexical_block))))
14172 {
14173 nesting_level++;
14174 parent_die = last_die;
14175 continue;
14176 }
14177
14178 /* Otherwise we skip to the next sibling, if any. */
14179 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
14180
14181 /* Back to the top, do it again. */
14182 }
14183 }
14184
14185 /* Read a minimal amount of information into the minimal die structure. */
14186
14187 static const gdb_byte *
14188 read_partial_die (const struct die_reader_specs *reader,
14189 struct partial_die_info *part_die,
14190 struct abbrev_info *abbrev, unsigned int abbrev_len,
14191 const gdb_byte *info_ptr)
14192 {
14193 struct dwarf2_cu *cu = reader->cu;
14194 struct objfile *objfile = cu->objfile;
14195 const gdb_byte *buffer = reader->buffer;
14196 unsigned int i;
14197 struct attribute attr;
14198 int has_low_pc_attr = 0;
14199 int has_high_pc_attr = 0;
14200 int high_pc_relative = 0;
14201
14202 memset (part_die, 0, sizeof (struct partial_die_info));
14203
14204 part_die->offset.sect_off = info_ptr - buffer;
14205
14206 info_ptr += abbrev_len;
14207
14208 if (abbrev == NULL)
14209 return info_ptr;
14210
14211 part_die->tag = abbrev->tag;
14212 part_die->has_children = abbrev->has_children;
14213
14214 for (i = 0; i < abbrev->num_attrs; ++i)
14215 {
14216 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
14217
14218 /* Store the data if it is of an attribute we want to keep in a
14219 partial symbol table. */
14220 switch (attr.name)
14221 {
14222 case DW_AT_name:
14223 switch (part_die->tag)
14224 {
14225 case DW_TAG_compile_unit:
14226 case DW_TAG_partial_unit:
14227 case DW_TAG_type_unit:
14228 /* Compilation units have a DW_AT_name that is a filename, not
14229 a source language identifier. */
14230 case DW_TAG_enumeration_type:
14231 case DW_TAG_enumerator:
14232 /* These tags always have simple identifiers already; no need
14233 to canonicalize them. */
14234 part_die->name = DW_STRING (&attr);
14235 break;
14236 default:
14237 part_die->name
14238 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
14239 &objfile->objfile_obstack);
14240 break;
14241 }
14242 break;
14243 case DW_AT_linkage_name:
14244 case DW_AT_MIPS_linkage_name:
14245 /* Note that both forms of linkage name might appear. We
14246 assume they will be the same, and we only store the last
14247 one we see. */
14248 if (cu->language == language_ada)
14249 part_die->name = DW_STRING (&attr);
14250 part_die->linkage_name = DW_STRING (&attr);
14251 break;
14252 case DW_AT_low_pc:
14253 has_low_pc_attr = 1;
14254 part_die->lowpc = DW_ADDR (&attr);
14255 break;
14256 case DW_AT_high_pc:
14257 has_high_pc_attr = 1;
14258 if (attr.form == DW_FORM_addr
14259 || attr.form == DW_FORM_GNU_addr_index)
14260 part_die->highpc = DW_ADDR (&attr);
14261 else
14262 {
14263 high_pc_relative = 1;
14264 part_die->highpc = DW_UNSND (&attr);
14265 }
14266 break;
14267 case DW_AT_location:
14268 /* Support the .debug_loc offsets. */
14269 if (attr_form_is_block (&attr))
14270 {
14271 part_die->d.locdesc = DW_BLOCK (&attr);
14272 }
14273 else if (attr_form_is_section_offset (&attr))
14274 {
14275 dwarf2_complex_location_expr_complaint ();
14276 }
14277 else
14278 {
14279 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14280 "partial symbol information");
14281 }
14282 break;
14283 case DW_AT_external:
14284 part_die->is_external = DW_UNSND (&attr);
14285 break;
14286 case DW_AT_declaration:
14287 part_die->is_declaration = DW_UNSND (&attr);
14288 break;
14289 case DW_AT_type:
14290 part_die->has_type = 1;
14291 break;
14292 case DW_AT_abstract_origin:
14293 case DW_AT_specification:
14294 case DW_AT_extension:
14295 part_die->has_specification = 1;
14296 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
14297 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
14298 || cu->per_cu->is_dwz);
14299 break;
14300 case DW_AT_sibling:
14301 /* Ignore absolute siblings, they might point outside of
14302 the current compile unit. */
14303 if (attr.form == DW_FORM_ref_addr)
14304 complaint (&symfile_complaints,
14305 _("ignoring absolute DW_AT_sibling"));
14306 else
14307 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
14308 break;
14309 case DW_AT_byte_size:
14310 part_die->has_byte_size = 1;
14311 break;
14312 case DW_AT_calling_convention:
14313 /* DWARF doesn't provide a way to identify a program's source-level
14314 entry point. DW_AT_calling_convention attributes are only meant
14315 to describe functions' calling conventions.
14316
14317 However, because it's a necessary piece of information in
14318 Fortran, and because DW_CC_program is the only piece of debugging
14319 information whose definition refers to a 'main program' at all,
14320 several compilers have begun marking Fortran main programs with
14321 DW_CC_program --- even when those functions use the standard
14322 calling conventions.
14323
14324 So until DWARF specifies a way to provide this information and
14325 compilers pick up the new representation, we'll support this
14326 practice. */
14327 if (DW_UNSND (&attr) == DW_CC_program
14328 && cu->language == language_fortran)
14329 {
14330 set_main_name (part_die->name);
14331
14332 /* As this DIE has a static linkage the name would be difficult
14333 to look up later. */
14334 language_of_main = language_fortran;
14335 }
14336 break;
14337 case DW_AT_inline:
14338 if (DW_UNSND (&attr) == DW_INL_inlined
14339 || DW_UNSND (&attr) == DW_INL_declared_inlined)
14340 part_die->may_be_inlined = 1;
14341 break;
14342
14343 case DW_AT_import:
14344 if (part_die->tag == DW_TAG_imported_unit)
14345 {
14346 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
14347 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
14348 || cu->per_cu->is_dwz);
14349 }
14350 break;
14351
14352 default:
14353 break;
14354 }
14355 }
14356
14357 if (high_pc_relative)
14358 part_die->highpc += part_die->lowpc;
14359
14360 if (has_low_pc_attr && has_high_pc_attr)
14361 {
14362 /* When using the GNU linker, .gnu.linkonce. sections are used to
14363 eliminate duplicate copies of functions and vtables and such.
14364 The linker will arbitrarily choose one and discard the others.
14365 The AT_*_pc values for such functions refer to local labels in
14366 these sections. If the section from that file was discarded, the
14367 labels are not in the output, so the relocs get a value of 0.
14368 If this is a discarded function, mark the pc bounds as invalid,
14369 so that GDB will ignore it. */
14370 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
14371 {
14372 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14373
14374 complaint (&symfile_complaints,
14375 _("DW_AT_low_pc %s is zero "
14376 "for DIE at 0x%x [in module %s]"),
14377 paddress (gdbarch, part_die->lowpc),
14378 part_die->offset.sect_off, objfile->name);
14379 }
14380 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
14381 else if (part_die->lowpc >= part_die->highpc)
14382 {
14383 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14384
14385 complaint (&symfile_complaints,
14386 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
14387 "for DIE at 0x%x [in module %s]"),
14388 paddress (gdbarch, part_die->lowpc),
14389 paddress (gdbarch, part_die->highpc),
14390 part_die->offset.sect_off, objfile->name);
14391 }
14392 else
14393 part_die->has_pc_info = 1;
14394 }
14395
14396 return info_ptr;
14397 }
14398
14399 /* Find a cached partial DIE at OFFSET in CU. */
14400
14401 static struct partial_die_info *
14402 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
14403 {
14404 struct partial_die_info *lookup_die = NULL;
14405 struct partial_die_info part_die;
14406
14407 part_die.offset = offset;
14408 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
14409 offset.sect_off);
14410
14411 return lookup_die;
14412 }
14413
14414 /* Find a partial DIE at OFFSET, which may or may not be in CU,
14415 except in the case of .debug_types DIEs which do not reference
14416 outside their CU (they do however referencing other types via
14417 DW_FORM_ref_sig8). */
14418
14419 static struct partial_die_info *
14420 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
14421 {
14422 struct objfile *objfile = cu->objfile;
14423 struct dwarf2_per_cu_data *per_cu = NULL;
14424 struct partial_die_info *pd = NULL;
14425
14426 if (offset_in_dwz == cu->per_cu->is_dwz
14427 && offset_in_cu_p (&cu->header, offset))
14428 {
14429 pd = find_partial_die_in_comp_unit (offset, cu);
14430 if (pd != NULL)
14431 return pd;
14432 /* We missed recording what we needed.
14433 Load all dies and try again. */
14434 per_cu = cu->per_cu;
14435 }
14436 else
14437 {
14438 /* TUs don't reference other CUs/TUs (except via type signatures). */
14439 if (cu->per_cu->is_debug_types)
14440 {
14441 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
14442 " external reference to offset 0x%lx [in module %s].\n"),
14443 (long) cu->header.offset.sect_off, (long) offset.sect_off,
14444 bfd_get_filename (objfile->obfd));
14445 }
14446 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
14447 objfile);
14448
14449 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
14450 load_partial_comp_unit (per_cu);
14451
14452 per_cu->cu->last_used = 0;
14453 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14454 }
14455
14456 /* If we didn't find it, and not all dies have been loaded,
14457 load them all and try again. */
14458
14459 if (pd == NULL && per_cu->load_all_dies == 0)
14460 {
14461 per_cu->load_all_dies = 1;
14462
14463 /* This is nasty. When we reread the DIEs, somewhere up the call chain
14464 THIS_CU->cu may already be in use. So we can't just free it and
14465 replace its DIEs with the ones we read in. Instead, we leave those
14466 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
14467 and clobber THIS_CU->cu->partial_dies with the hash table for the new
14468 set. */
14469 load_partial_comp_unit (per_cu);
14470
14471 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14472 }
14473
14474 if (pd == NULL)
14475 internal_error (__FILE__, __LINE__,
14476 _("could not find partial DIE 0x%x "
14477 "in cache [from module %s]\n"),
14478 offset.sect_off, bfd_get_filename (objfile->obfd));
14479 return pd;
14480 }
14481
14482 /* See if we can figure out if the class lives in a namespace. We do
14483 this by looking for a member function; its demangled name will
14484 contain namespace info, if there is any. */
14485
14486 static void
14487 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
14488 struct dwarf2_cu *cu)
14489 {
14490 /* NOTE: carlton/2003-10-07: Getting the info this way changes
14491 what template types look like, because the demangler
14492 frequently doesn't give the same name as the debug info. We
14493 could fix this by only using the demangled name to get the
14494 prefix (but see comment in read_structure_type). */
14495
14496 struct partial_die_info *real_pdi;
14497 struct partial_die_info *child_pdi;
14498
14499 /* If this DIE (this DIE's specification, if any) has a parent, then
14500 we should not do this. We'll prepend the parent's fully qualified
14501 name when we create the partial symbol. */
14502
14503 real_pdi = struct_pdi;
14504 while (real_pdi->has_specification)
14505 real_pdi = find_partial_die (real_pdi->spec_offset,
14506 real_pdi->spec_is_dwz, cu);
14507
14508 if (real_pdi->die_parent != NULL)
14509 return;
14510
14511 for (child_pdi = struct_pdi->die_child;
14512 child_pdi != NULL;
14513 child_pdi = child_pdi->die_sibling)
14514 {
14515 if (child_pdi->tag == DW_TAG_subprogram
14516 && child_pdi->linkage_name != NULL)
14517 {
14518 char *actual_class_name
14519 = language_class_name_from_physname (cu->language_defn,
14520 child_pdi->linkage_name);
14521 if (actual_class_name != NULL)
14522 {
14523 struct_pdi->name
14524 = obstack_copy0 (&cu->objfile->objfile_obstack,
14525 actual_class_name,
14526 strlen (actual_class_name));
14527 xfree (actual_class_name);
14528 }
14529 break;
14530 }
14531 }
14532 }
14533
14534 /* Adjust PART_DIE before generating a symbol for it. This function
14535 may set the is_external flag or change the DIE's name. */
14536
14537 static void
14538 fixup_partial_die (struct partial_die_info *part_die,
14539 struct dwarf2_cu *cu)
14540 {
14541 /* Once we've fixed up a die, there's no point in doing so again.
14542 This also avoids a memory leak if we were to call
14543 guess_partial_die_structure_name multiple times. */
14544 if (part_die->fixup_called)
14545 return;
14546
14547 /* If we found a reference attribute and the DIE has no name, try
14548 to find a name in the referred to DIE. */
14549
14550 if (part_die->name == NULL && part_die->has_specification)
14551 {
14552 struct partial_die_info *spec_die;
14553
14554 spec_die = find_partial_die (part_die->spec_offset,
14555 part_die->spec_is_dwz, cu);
14556
14557 fixup_partial_die (spec_die, cu);
14558
14559 if (spec_die->name)
14560 {
14561 part_die->name = spec_die->name;
14562
14563 /* Copy DW_AT_external attribute if it is set. */
14564 if (spec_die->is_external)
14565 part_die->is_external = spec_die->is_external;
14566 }
14567 }
14568
14569 /* Set default names for some unnamed DIEs. */
14570
14571 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
14572 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
14573
14574 /* If there is no parent die to provide a namespace, and there are
14575 children, see if we can determine the namespace from their linkage
14576 name. */
14577 if (cu->language == language_cplus
14578 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
14579 && part_die->die_parent == NULL
14580 && part_die->has_children
14581 && (part_die->tag == DW_TAG_class_type
14582 || part_die->tag == DW_TAG_structure_type
14583 || part_die->tag == DW_TAG_union_type))
14584 guess_partial_die_structure_name (part_die, cu);
14585
14586 /* GCC might emit a nameless struct or union that has a linkage
14587 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14588 if (part_die->name == NULL
14589 && (part_die->tag == DW_TAG_class_type
14590 || part_die->tag == DW_TAG_interface_type
14591 || part_die->tag == DW_TAG_structure_type
14592 || part_die->tag == DW_TAG_union_type)
14593 && part_die->linkage_name != NULL)
14594 {
14595 char *demangled;
14596
14597 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
14598 if (demangled)
14599 {
14600 const char *base;
14601
14602 /* Strip any leading namespaces/classes, keep only the base name.
14603 DW_AT_name for named DIEs does not contain the prefixes. */
14604 base = strrchr (demangled, ':');
14605 if (base && base > demangled && base[-1] == ':')
14606 base++;
14607 else
14608 base = demangled;
14609
14610 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14611 base, strlen (base));
14612 xfree (demangled);
14613 }
14614 }
14615
14616 part_die->fixup_called = 1;
14617 }
14618
14619 /* Read an attribute value described by an attribute form. */
14620
14621 static const gdb_byte *
14622 read_attribute_value (const struct die_reader_specs *reader,
14623 struct attribute *attr, unsigned form,
14624 const gdb_byte *info_ptr)
14625 {
14626 struct dwarf2_cu *cu = reader->cu;
14627 bfd *abfd = reader->abfd;
14628 struct comp_unit_head *cu_header = &cu->header;
14629 unsigned int bytes_read;
14630 struct dwarf_block *blk;
14631
14632 attr->form = form;
14633 switch (form)
14634 {
14635 case DW_FORM_ref_addr:
14636 if (cu->header.version == 2)
14637 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14638 else
14639 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14640 &cu->header, &bytes_read);
14641 info_ptr += bytes_read;
14642 break;
14643 case DW_FORM_GNU_ref_alt:
14644 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14645 info_ptr += bytes_read;
14646 break;
14647 case DW_FORM_addr:
14648 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14649 info_ptr += bytes_read;
14650 break;
14651 case DW_FORM_block2:
14652 blk = dwarf_alloc_block (cu);
14653 blk->size = read_2_bytes (abfd, info_ptr);
14654 info_ptr += 2;
14655 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14656 info_ptr += blk->size;
14657 DW_BLOCK (attr) = blk;
14658 break;
14659 case DW_FORM_block4:
14660 blk = dwarf_alloc_block (cu);
14661 blk->size = read_4_bytes (abfd, info_ptr);
14662 info_ptr += 4;
14663 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14664 info_ptr += blk->size;
14665 DW_BLOCK (attr) = blk;
14666 break;
14667 case DW_FORM_data2:
14668 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14669 info_ptr += 2;
14670 break;
14671 case DW_FORM_data4:
14672 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14673 info_ptr += 4;
14674 break;
14675 case DW_FORM_data8:
14676 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14677 info_ptr += 8;
14678 break;
14679 case DW_FORM_sec_offset:
14680 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14681 info_ptr += bytes_read;
14682 break;
14683 case DW_FORM_string:
14684 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
14685 DW_STRING_IS_CANONICAL (attr) = 0;
14686 info_ptr += bytes_read;
14687 break;
14688 case DW_FORM_strp:
14689 if (!cu->per_cu->is_dwz)
14690 {
14691 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14692 &bytes_read);
14693 DW_STRING_IS_CANONICAL (attr) = 0;
14694 info_ptr += bytes_read;
14695 break;
14696 }
14697 /* FALLTHROUGH */
14698 case DW_FORM_GNU_strp_alt:
14699 {
14700 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14701 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14702 &bytes_read);
14703
14704 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14705 DW_STRING_IS_CANONICAL (attr) = 0;
14706 info_ptr += bytes_read;
14707 }
14708 break;
14709 case DW_FORM_exprloc:
14710 case DW_FORM_block:
14711 blk = dwarf_alloc_block (cu);
14712 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14713 info_ptr += bytes_read;
14714 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14715 info_ptr += blk->size;
14716 DW_BLOCK (attr) = blk;
14717 break;
14718 case DW_FORM_block1:
14719 blk = dwarf_alloc_block (cu);
14720 blk->size = read_1_byte (abfd, info_ptr);
14721 info_ptr += 1;
14722 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14723 info_ptr += blk->size;
14724 DW_BLOCK (attr) = blk;
14725 break;
14726 case DW_FORM_data1:
14727 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14728 info_ptr += 1;
14729 break;
14730 case DW_FORM_flag:
14731 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14732 info_ptr += 1;
14733 break;
14734 case DW_FORM_flag_present:
14735 DW_UNSND (attr) = 1;
14736 break;
14737 case DW_FORM_sdata:
14738 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14739 info_ptr += bytes_read;
14740 break;
14741 case DW_FORM_udata:
14742 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14743 info_ptr += bytes_read;
14744 break;
14745 case DW_FORM_ref1:
14746 DW_UNSND (attr) = (cu->header.offset.sect_off
14747 + read_1_byte (abfd, info_ptr));
14748 info_ptr += 1;
14749 break;
14750 case DW_FORM_ref2:
14751 DW_UNSND (attr) = (cu->header.offset.sect_off
14752 + read_2_bytes (abfd, info_ptr));
14753 info_ptr += 2;
14754 break;
14755 case DW_FORM_ref4:
14756 DW_UNSND (attr) = (cu->header.offset.sect_off
14757 + read_4_bytes (abfd, info_ptr));
14758 info_ptr += 4;
14759 break;
14760 case DW_FORM_ref8:
14761 DW_UNSND (attr) = (cu->header.offset.sect_off
14762 + read_8_bytes (abfd, info_ptr));
14763 info_ptr += 8;
14764 break;
14765 case DW_FORM_ref_sig8:
14766 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
14767 info_ptr += 8;
14768 break;
14769 case DW_FORM_ref_udata:
14770 DW_UNSND (attr) = (cu->header.offset.sect_off
14771 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
14772 info_ptr += bytes_read;
14773 break;
14774 case DW_FORM_indirect:
14775 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14776 info_ptr += bytes_read;
14777 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
14778 break;
14779 case DW_FORM_GNU_addr_index:
14780 if (reader->dwo_file == NULL)
14781 {
14782 /* For now flag a hard error.
14783 Later we can turn this into a complaint. */
14784 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14785 dwarf_form_name (form),
14786 bfd_get_filename (abfd));
14787 }
14788 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14789 info_ptr += bytes_read;
14790 break;
14791 case DW_FORM_GNU_str_index:
14792 if (reader->dwo_file == NULL)
14793 {
14794 /* For now flag a hard error.
14795 Later we can turn this into a complaint if warranted. */
14796 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14797 dwarf_form_name (form),
14798 bfd_get_filename (abfd));
14799 }
14800 {
14801 ULONGEST str_index =
14802 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14803
14804 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14805 DW_STRING_IS_CANONICAL (attr) = 0;
14806 info_ptr += bytes_read;
14807 }
14808 break;
14809 default:
14810 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
14811 dwarf_form_name (form),
14812 bfd_get_filename (abfd));
14813 }
14814
14815 /* Super hack. */
14816 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
14817 attr->form = DW_FORM_GNU_ref_alt;
14818
14819 /* We have seen instances where the compiler tried to emit a byte
14820 size attribute of -1 which ended up being encoded as an unsigned
14821 0xffffffff. Although 0xffffffff is technically a valid size value,
14822 an object of this size seems pretty unlikely so we can relatively
14823 safely treat these cases as if the size attribute was invalid and
14824 treat them as zero by default. */
14825 if (attr->name == DW_AT_byte_size
14826 && form == DW_FORM_data4
14827 && DW_UNSND (attr) >= 0xffffffff)
14828 {
14829 complaint
14830 (&symfile_complaints,
14831 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14832 hex_string (DW_UNSND (attr)));
14833 DW_UNSND (attr) = 0;
14834 }
14835
14836 return info_ptr;
14837 }
14838
14839 /* Read an attribute described by an abbreviated attribute. */
14840
14841 static const gdb_byte *
14842 read_attribute (const struct die_reader_specs *reader,
14843 struct attribute *attr, struct attr_abbrev *abbrev,
14844 const gdb_byte *info_ptr)
14845 {
14846 attr->name = abbrev->name;
14847 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
14848 }
14849
14850 /* Read dwarf information from a buffer. */
14851
14852 static unsigned int
14853 read_1_byte (bfd *abfd, const gdb_byte *buf)
14854 {
14855 return bfd_get_8 (abfd, buf);
14856 }
14857
14858 static int
14859 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
14860 {
14861 return bfd_get_signed_8 (abfd, buf);
14862 }
14863
14864 static unsigned int
14865 read_2_bytes (bfd *abfd, const gdb_byte *buf)
14866 {
14867 return bfd_get_16 (abfd, buf);
14868 }
14869
14870 static int
14871 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
14872 {
14873 return bfd_get_signed_16 (abfd, buf);
14874 }
14875
14876 static unsigned int
14877 read_4_bytes (bfd *abfd, const gdb_byte *buf)
14878 {
14879 return bfd_get_32 (abfd, buf);
14880 }
14881
14882 static int
14883 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
14884 {
14885 return bfd_get_signed_32 (abfd, buf);
14886 }
14887
14888 static ULONGEST
14889 read_8_bytes (bfd *abfd, const gdb_byte *buf)
14890 {
14891 return bfd_get_64 (abfd, buf);
14892 }
14893
14894 static CORE_ADDR
14895 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
14896 unsigned int *bytes_read)
14897 {
14898 struct comp_unit_head *cu_header = &cu->header;
14899 CORE_ADDR retval = 0;
14900
14901 if (cu_header->signed_addr_p)
14902 {
14903 switch (cu_header->addr_size)
14904 {
14905 case 2:
14906 retval = bfd_get_signed_16 (abfd, buf);
14907 break;
14908 case 4:
14909 retval = bfd_get_signed_32 (abfd, buf);
14910 break;
14911 case 8:
14912 retval = bfd_get_signed_64 (abfd, buf);
14913 break;
14914 default:
14915 internal_error (__FILE__, __LINE__,
14916 _("read_address: bad switch, signed [in module %s]"),
14917 bfd_get_filename (abfd));
14918 }
14919 }
14920 else
14921 {
14922 switch (cu_header->addr_size)
14923 {
14924 case 2:
14925 retval = bfd_get_16 (abfd, buf);
14926 break;
14927 case 4:
14928 retval = bfd_get_32 (abfd, buf);
14929 break;
14930 case 8:
14931 retval = bfd_get_64 (abfd, buf);
14932 break;
14933 default:
14934 internal_error (__FILE__, __LINE__,
14935 _("read_address: bad switch, "
14936 "unsigned [in module %s]"),
14937 bfd_get_filename (abfd));
14938 }
14939 }
14940
14941 *bytes_read = cu_header->addr_size;
14942 return retval;
14943 }
14944
14945 /* Read the initial length from a section. The (draft) DWARF 3
14946 specification allows the initial length to take up either 4 bytes
14947 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14948 bytes describe the length and all offsets will be 8 bytes in length
14949 instead of 4.
14950
14951 An older, non-standard 64-bit format is also handled by this
14952 function. The older format in question stores the initial length
14953 as an 8-byte quantity without an escape value. Lengths greater
14954 than 2^32 aren't very common which means that the initial 4 bytes
14955 is almost always zero. Since a length value of zero doesn't make
14956 sense for the 32-bit format, this initial zero can be considered to
14957 be an escape value which indicates the presence of the older 64-bit
14958 format. As written, the code can't detect (old format) lengths
14959 greater than 4GB. If it becomes necessary to handle lengths
14960 somewhat larger than 4GB, we could allow other small values (such
14961 as the non-sensical values of 1, 2, and 3) to also be used as
14962 escape values indicating the presence of the old format.
14963
14964 The value returned via bytes_read should be used to increment the
14965 relevant pointer after calling read_initial_length().
14966
14967 [ Note: read_initial_length() and read_offset() are based on the
14968 document entitled "DWARF Debugging Information Format", revision
14969 3, draft 8, dated November 19, 2001. This document was obtained
14970 from:
14971
14972 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
14973
14974 This document is only a draft and is subject to change. (So beware.)
14975
14976 Details regarding the older, non-standard 64-bit format were
14977 determined empirically by examining 64-bit ELF files produced by
14978 the SGI toolchain on an IRIX 6.5 machine.
14979
14980 - Kevin, July 16, 2002
14981 ] */
14982
14983 static LONGEST
14984 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
14985 {
14986 LONGEST length = bfd_get_32 (abfd, buf);
14987
14988 if (length == 0xffffffff)
14989 {
14990 length = bfd_get_64 (abfd, buf + 4);
14991 *bytes_read = 12;
14992 }
14993 else if (length == 0)
14994 {
14995 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
14996 length = bfd_get_64 (abfd, buf);
14997 *bytes_read = 8;
14998 }
14999 else
15000 {
15001 *bytes_read = 4;
15002 }
15003
15004 return length;
15005 }
15006
15007 /* Cover function for read_initial_length.
15008 Returns the length of the object at BUF, and stores the size of the
15009 initial length in *BYTES_READ and stores the size that offsets will be in
15010 *OFFSET_SIZE.
15011 If the initial length size is not equivalent to that specified in
15012 CU_HEADER then issue a complaint.
15013 This is useful when reading non-comp-unit headers. */
15014
15015 static LONGEST
15016 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
15017 const struct comp_unit_head *cu_header,
15018 unsigned int *bytes_read,
15019 unsigned int *offset_size)
15020 {
15021 LONGEST length = read_initial_length (abfd, buf, bytes_read);
15022
15023 gdb_assert (cu_header->initial_length_size == 4
15024 || cu_header->initial_length_size == 8
15025 || cu_header->initial_length_size == 12);
15026
15027 if (cu_header->initial_length_size != *bytes_read)
15028 complaint (&symfile_complaints,
15029 _("intermixed 32-bit and 64-bit DWARF sections"));
15030
15031 *offset_size = (*bytes_read == 4) ? 4 : 8;
15032 return length;
15033 }
15034
15035 /* Read an offset from the data stream. The size of the offset is
15036 given by cu_header->offset_size. */
15037
15038 static LONGEST
15039 read_offset (bfd *abfd, const gdb_byte *buf,
15040 const struct comp_unit_head *cu_header,
15041 unsigned int *bytes_read)
15042 {
15043 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
15044
15045 *bytes_read = cu_header->offset_size;
15046 return offset;
15047 }
15048
15049 /* Read an offset from the data stream. */
15050
15051 static LONGEST
15052 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
15053 {
15054 LONGEST retval = 0;
15055
15056 switch (offset_size)
15057 {
15058 case 4:
15059 retval = bfd_get_32 (abfd, buf);
15060 break;
15061 case 8:
15062 retval = bfd_get_64 (abfd, buf);
15063 break;
15064 default:
15065 internal_error (__FILE__, __LINE__,
15066 _("read_offset_1: bad switch [in module %s]"),
15067 bfd_get_filename (abfd));
15068 }
15069
15070 return retval;
15071 }
15072
15073 static const gdb_byte *
15074 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
15075 {
15076 /* If the size of a host char is 8 bits, we can return a pointer
15077 to the buffer, otherwise we have to copy the data to a buffer
15078 allocated on the temporary obstack. */
15079 gdb_assert (HOST_CHAR_BIT == 8);
15080 return buf;
15081 }
15082
15083 static const char *
15084 read_direct_string (bfd *abfd, const gdb_byte *buf,
15085 unsigned int *bytes_read_ptr)
15086 {
15087 /* If the size of a host char is 8 bits, we can return a pointer
15088 to the string, otherwise we have to copy the string to a buffer
15089 allocated on the temporary obstack. */
15090 gdb_assert (HOST_CHAR_BIT == 8);
15091 if (*buf == '\0')
15092 {
15093 *bytes_read_ptr = 1;
15094 return NULL;
15095 }
15096 *bytes_read_ptr = strlen ((const char *) buf) + 1;
15097 return (const char *) buf;
15098 }
15099
15100 static const char *
15101 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
15102 {
15103 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
15104 if (dwarf2_per_objfile->str.buffer == NULL)
15105 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
15106 bfd_get_filename (abfd));
15107 if (str_offset >= dwarf2_per_objfile->str.size)
15108 error (_("DW_FORM_strp pointing outside of "
15109 ".debug_str section [in module %s]"),
15110 bfd_get_filename (abfd));
15111 gdb_assert (HOST_CHAR_BIT == 8);
15112 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
15113 return NULL;
15114 return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
15115 }
15116
15117 /* Read a string at offset STR_OFFSET in the .debug_str section from
15118 the .dwz file DWZ. Throw an error if the offset is too large. If
15119 the string consists of a single NUL byte, return NULL; otherwise
15120 return a pointer to the string. */
15121
15122 static const char *
15123 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
15124 {
15125 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
15126
15127 if (dwz->str.buffer == NULL)
15128 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
15129 "section [in module %s]"),
15130 bfd_get_filename (dwz->dwz_bfd));
15131 if (str_offset >= dwz->str.size)
15132 error (_("DW_FORM_GNU_strp_alt pointing outside of "
15133 ".debug_str section [in module %s]"),
15134 bfd_get_filename (dwz->dwz_bfd));
15135 gdb_assert (HOST_CHAR_BIT == 8);
15136 if (dwz->str.buffer[str_offset] == '\0')
15137 return NULL;
15138 return (const char *) (dwz->str.buffer + str_offset);
15139 }
15140
15141 static const char *
15142 read_indirect_string (bfd *abfd, const gdb_byte *buf,
15143 const struct comp_unit_head *cu_header,
15144 unsigned int *bytes_read_ptr)
15145 {
15146 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
15147
15148 return read_indirect_string_at_offset (abfd, str_offset);
15149 }
15150
15151 static ULONGEST
15152 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
15153 unsigned int *bytes_read_ptr)
15154 {
15155 ULONGEST result;
15156 unsigned int num_read;
15157 int i, shift;
15158 unsigned char byte;
15159
15160 result = 0;
15161 shift = 0;
15162 num_read = 0;
15163 i = 0;
15164 while (1)
15165 {
15166 byte = bfd_get_8 (abfd, buf);
15167 buf++;
15168 num_read++;
15169 result |= ((ULONGEST) (byte & 127) << shift);
15170 if ((byte & 128) == 0)
15171 {
15172 break;
15173 }
15174 shift += 7;
15175 }
15176 *bytes_read_ptr = num_read;
15177 return result;
15178 }
15179
15180 static LONGEST
15181 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
15182 unsigned int *bytes_read_ptr)
15183 {
15184 LONGEST result;
15185 int i, shift, num_read;
15186 unsigned char byte;
15187
15188 result = 0;
15189 shift = 0;
15190 num_read = 0;
15191 i = 0;
15192 while (1)
15193 {
15194 byte = bfd_get_8 (abfd, buf);
15195 buf++;
15196 num_read++;
15197 result |= ((LONGEST) (byte & 127) << shift);
15198 shift += 7;
15199 if ((byte & 128) == 0)
15200 {
15201 break;
15202 }
15203 }
15204 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
15205 result |= -(((LONGEST) 1) << shift);
15206 *bytes_read_ptr = num_read;
15207 return result;
15208 }
15209
15210 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
15211 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
15212 ADDR_SIZE is the size of addresses from the CU header. */
15213
15214 static CORE_ADDR
15215 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
15216 {
15217 struct objfile *objfile = dwarf2_per_objfile->objfile;
15218 bfd *abfd = objfile->obfd;
15219 const gdb_byte *info_ptr;
15220
15221 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
15222 if (dwarf2_per_objfile->addr.buffer == NULL)
15223 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
15224 objfile->name);
15225 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
15226 error (_("DW_FORM_addr_index pointing outside of "
15227 ".debug_addr section [in module %s]"),
15228 objfile->name);
15229 info_ptr = (dwarf2_per_objfile->addr.buffer
15230 + addr_base + addr_index * addr_size);
15231 if (addr_size == 4)
15232 return bfd_get_32 (abfd, info_ptr);
15233 else
15234 return bfd_get_64 (abfd, info_ptr);
15235 }
15236
15237 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
15238
15239 static CORE_ADDR
15240 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
15241 {
15242 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
15243 }
15244
15245 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
15246
15247 static CORE_ADDR
15248 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
15249 unsigned int *bytes_read)
15250 {
15251 bfd *abfd = cu->objfile->obfd;
15252 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
15253
15254 return read_addr_index (cu, addr_index);
15255 }
15256
15257 /* Data structure to pass results from dwarf2_read_addr_index_reader
15258 back to dwarf2_read_addr_index. */
15259
15260 struct dwarf2_read_addr_index_data
15261 {
15262 ULONGEST addr_base;
15263 int addr_size;
15264 };
15265
15266 /* die_reader_func for dwarf2_read_addr_index. */
15267
15268 static void
15269 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
15270 const gdb_byte *info_ptr,
15271 struct die_info *comp_unit_die,
15272 int has_children,
15273 void *data)
15274 {
15275 struct dwarf2_cu *cu = reader->cu;
15276 struct dwarf2_read_addr_index_data *aidata =
15277 (struct dwarf2_read_addr_index_data *) data;
15278
15279 aidata->addr_base = cu->addr_base;
15280 aidata->addr_size = cu->header.addr_size;
15281 }
15282
15283 /* Given an index in .debug_addr, fetch the value.
15284 NOTE: This can be called during dwarf expression evaluation,
15285 long after the debug information has been read, and thus per_cu->cu
15286 may no longer exist. */
15287
15288 CORE_ADDR
15289 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
15290 unsigned int addr_index)
15291 {
15292 struct objfile *objfile = per_cu->objfile;
15293 struct dwarf2_cu *cu = per_cu->cu;
15294 ULONGEST addr_base;
15295 int addr_size;
15296
15297 /* This is intended to be called from outside this file. */
15298 dw2_setup (objfile);
15299
15300 /* We need addr_base and addr_size.
15301 If we don't have PER_CU->cu, we have to get it.
15302 Nasty, but the alternative is storing the needed info in PER_CU,
15303 which at this point doesn't seem justified: it's not clear how frequently
15304 it would get used and it would increase the size of every PER_CU.
15305 Entry points like dwarf2_per_cu_addr_size do a similar thing
15306 so we're not in uncharted territory here.
15307 Alas we need to be a bit more complicated as addr_base is contained
15308 in the DIE.
15309
15310 We don't need to read the entire CU(/TU).
15311 We just need the header and top level die.
15312
15313 IWBN to use the aging mechanism to let us lazily later discard the CU.
15314 For now we skip this optimization. */
15315
15316 if (cu != NULL)
15317 {
15318 addr_base = cu->addr_base;
15319 addr_size = cu->header.addr_size;
15320 }
15321 else
15322 {
15323 struct dwarf2_read_addr_index_data aidata;
15324
15325 /* Note: We can't use init_cutu_and_read_dies_simple here,
15326 we need addr_base. */
15327 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
15328 dwarf2_read_addr_index_reader, &aidata);
15329 addr_base = aidata.addr_base;
15330 addr_size = aidata.addr_size;
15331 }
15332
15333 return read_addr_index_1 (addr_index, addr_base, addr_size);
15334 }
15335
15336 /* Given a DW_AT_str_index, fetch the string. */
15337
15338 static const char *
15339 read_str_index (const struct die_reader_specs *reader,
15340 struct dwarf2_cu *cu, ULONGEST str_index)
15341 {
15342 struct objfile *objfile = dwarf2_per_objfile->objfile;
15343 const char *dwo_name = objfile->name;
15344 bfd *abfd = objfile->obfd;
15345 struct dwo_sections *sections = &reader->dwo_file->sections;
15346 const gdb_byte *info_ptr;
15347 ULONGEST str_offset;
15348
15349 dwarf2_read_section (objfile, &sections->str);
15350 dwarf2_read_section (objfile, &sections->str_offsets);
15351 if (sections->str.buffer == NULL)
15352 error (_("DW_FORM_str_index used without .debug_str.dwo section"
15353 " in CU at offset 0x%lx [in module %s]"),
15354 (long) cu->header.offset.sect_off, dwo_name);
15355 if (sections->str_offsets.buffer == NULL)
15356 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
15357 " in CU at offset 0x%lx [in module %s]"),
15358 (long) cu->header.offset.sect_off, dwo_name);
15359 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
15360 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
15361 " section in CU at offset 0x%lx [in module %s]"),
15362 (long) cu->header.offset.sect_off, dwo_name);
15363 info_ptr = (sections->str_offsets.buffer
15364 + str_index * cu->header.offset_size);
15365 if (cu->header.offset_size == 4)
15366 str_offset = bfd_get_32 (abfd, info_ptr);
15367 else
15368 str_offset = bfd_get_64 (abfd, info_ptr);
15369 if (str_offset >= sections->str.size)
15370 error (_("Offset from DW_FORM_str_index pointing outside of"
15371 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
15372 (long) cu->header.offset.sect_off, dwo_name);
15373 return (const char *) (sections->str.buffer + str_offset);
15374 }
15375
15376 /* Return the length of an LEB128 number in BUF. */
15377
15378 static int
15379 leb128_size (const gdb_byte *buf)
15380 {
15381 const gdb_byte *begin = buf;
15382 gdb_byte byte;
15383
15384 while (1)
15385 {
15386 byte = *buf++;
15387 if ((byte & 128) == 0)
15388 return buf - begin;
15389 }
15390 }
15391
15392 static void
15393 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
15394 {
15395 switch (lang)
15396 {
15397 case DW_LANG_C89:
15398 case DW_LANG_C99:
15399 case DW_LANG_C:
15400 case DW_LANG_UPC:
15401 cu->language = language_c;
15402 break;
15403 case DW_LANG_C_plus_plus:
15404 cu->language = language_cplus;
15405 break;
15406 case DW_LANG_D:
15407 cu->language = language_d;
15408 break;
15409 case DW_LANG_Fortran77:
15410 case DW_LANG_Fortran90:
15411 case DW_LANG_Fortran95:
15412 cu->language = language_fortran;
15413 break;
15414 case DW_LANG_Go:
15415 cu->language = language_go;
15416 break;
15417 case DW_LANG_Mips_Assembler:
15418 cu->language = language_asm;
15419 break;
15420 case DW_LANG_Java:
15421 cu->language = language_java;
15422 break;
15423 case DW_LANG_Ada83:
15424 case DW_LANG_Ada95:
15425 cu->language = language_ada;
15426 break;
15427 case DW_LANG_Modula2:
15428 cu->language = language_m2;
15429 break;
15430 case DW_LANG_Pascal83:
15431 cu->language = language_pascal;
15432 break;
15433 case DW_LANG_ObjC:
15434 cu->language = language_objc;
15435 break;
15436 case DW_LANG_Cobol74:
15437 case DW_LANG_Cobol85:
15438 default:
15439 cu->language = language_minimal;
15440 break;
15441 }
15442 cu->language_defn = language_def (cu->language);
15443 }
15444
15445 /* Return the named attribute or NULL if not there. */
15446
15447 static struct attribute *
15448 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
15449 {
15450 for (;;)
15451 {
15452 unsigned int i;
15453 struct attribute *spec = NULL;
15454
15455 for (i = 0; i < die->num_attrs; ++i)
15456 {
15457 if (die->attrs[i].name == name)
15458 return &die->attrs[i];
15459 if (die->attrs[i].name == DW_AT_specification
15460 || die->attrs[i].name == DW_AT_abstract_origin)
15461 spec = &die->attrs[i];
15462 }
15463
15464 if (!spec)
15465 break;
15466
15467 die = follow_die_ref (die, spec, &cu);
15468 }
15469
15470 return NULL;
15471 }
15472
15473 /* Return the named attribute or NULL if not there,
15474 but do not follow DW_AT_specification, etc.
15475 This is for use in contexts where we're reading .debug_types dies.
15476 Following DW_AT_specification, DW_AT_abstract_origin will take us
15477 back up the chain, and we want to go down. */
15478
15479 static struct attribute *
15480 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
15481 {
15482 unsigned int i;
15483
15484 for (i = 0; i < die->num_attrs; ++i)
15485 if (die->attrs[i].name == name)
15486 return &die->attrs[i];
15487
15488 return NULL;
15489 }
15490
15491 /* Return non-zero iff the attribute NAME is defined for the given DIE,
15492 and holds a non-zero value. This function should only be used for
15493 DW_FORM_flag or DW_FORM_flag_present attributes. */
15494
15495 static int
15496 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
15497 {
15498 struct attribute *attr = dwarf2_attr (die, name, cu);
15499
15500 return (attr && DW_UNSND (attr));
15501 }
15502
15503 static int
15504 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
15505 {
15506 /* A DIE is a declaration if it has a DW_AT_declaration attribute
15507 which value is non-zero. However, we have to be careful with
15508 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
15509 (via dwarf2_flag_true_p) follows this attribute. So we may
15510 end up accidently finding a declaration attribute that belongs
15511 to a different DIE referenced by the specification attribute,
15512 even though the given DIE does not have a declaration attribute. */
15513 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
15514 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
15515 }
15516
15517 /* Return the die giving the specification for DIE, if there is
15518 one. *SPEC_CU is the CU containing DIE on input, and the CU
15519 containing the return value on output. If there is no
15520 specification, but there is an abstract origin, that is
15521 returned. */
15522
15523 static struct die_info *
15524 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
15525 {
15526 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
15527 *spec_cu);
15528
15529 if (spec_attr == NULL)
15530 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
15531
15532 if (spec_attr == NULL)
15533 return NULL;
15534 else
15535 return follow_die_ref (die, spec_attr, spec_cu);
15536 }
15537
15538 /* Free the line_header structure *LH, and any arrays and strings it
15539 refers to.
15540 NOTE: This is also used as a "cleanup" function. */
15541
15542 static void
15543 free_line_header (struct line_header *lh)
15544 {
15545 if (lh->standard_opcode_lengths)
15546 xfree (lh->standard_opcode_lengths);
15547
15548 /* Remember that all the lh->file_names[i].name pointers are
15549 pointers into debug_line_buffer, and don't need to be freed. */
15550 if (lh->file_names)
15551 xfree (lh->file_names);
15552
15553 /* Similarly for the include directory names. */
15554 if (lh->include_dirs)
15555 xfree (lh->include_dirs);
15556
15557 xfree (lh);
15558 }
15559
15560 /* Add an entry to LH's include directory table. */
15561
15562 static void
15563 add_include_dir (struct line_header *lh, const char *include_dir)
15564 {
15565 /* Grow the array if necessary. */
15566 if (lh->include_dirs_size == 0)
15567 {
15568 lh->include_dirs_size = 1; /* for testing */
15569 lh->include_dirs = xmalloc (lh->include_dirs_size
15570 * sizeof (*lh->include_dirs));
15571 }
15572 else if (lh->num_include_dirs >= lh->include_dirs_size)
15573 {
15574 lh->include_dirs_size *= 2;
15575 lh->include_dirs = xrealloc (lh->include_dirs,
15576 (lh->include_dirs_size
15577 * sizeof (*lh->include_dirs)));
15578 }
15579
15580 lh->include_dirs[lh->num_include_dirs++] = include_dir;
15581 }
15582
15583 /* Add an entry to LH's file name table. */
15584
15585 static void
15586 add_file_name (struct line_header *lh,
15587 const char *name,
15588 unsigned int dir_index,
15589 unsigned int mod_time,
15590 unsigned int length)
15591 {
15592 struct file_entry *fe;
15593
15594 /* Grow the array if necessary. */
15595 if (lh->file_names_size == 0)
15596 {
15597 lh->file_names_size = 1; /* for testing */
15598 lh->file_names = xmalloc (lh->file_names_size
15599 * sizeof (*lh->file_names));
15600 }
15601 else if (lh->num_file_names >= lh->file_names_size)
15602 {
15603 lh->file_names_size *= 2;
15604 lh->file_names = xrealloc (lh->file_names,
15605 (lh->file_names_size
15606 * sizeof (*lh->file_names)));
15607 }
15608
15609 fe = &lh->file_names[lh->num_file_names++];
15610 fe->name = name;
15611 fe->dir_index = dir_index;
15612 fe->mod_time = mod_time;
15613 fe->length = length;
15614 fe->included_p = 0;
15615 fe->symtab = NULL;
15616 }
15617
15618 /* A convenience function to find the proper .debug_line section for a
15619 CU. */
15620
15621 static struct dwarf2_section_info *
15622 get_debug_line_section (struct dwarf2_cu *cu)
15623 {
15624 struct dwarf2_section_info *section;
15625
15626 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15627 DWO file. */
15628 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15629 section = &cu->dwo_unit->dwo_file->sections.line;
15630 else if (cu->per_cu->is_dwz)
15631 {
15632 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15633
15634 section = &dwz->line;
15635 }
15636 else
15637 section = &dwarf2_per_objfile->line;
15638
15639 return section;
15640 }
15641
15642 /* Read the statement program header starting at OFFSET in
15643 .debug_line, or .debug_line.dwo. Return a pointer
15644 to a struct line_header, allocated using xmalloc.
15645
15646 NOTE: the strings in the include directory and file name tables of
15647 the returned object point into the dwarf line section buffer,
15648 and must not be freed. */
15649
15650 static struct line_header *
15651 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
15652 {
15653 struct cleanup *back_to;
15654 struct line_header *lh;
15655 const gdb_byte *line_ptr;
15656 unsigned int bytes_read, offset_size;
15657 int i;
15658 const char *cur_dir, *cur_file;
15659 struct dwarf2_section_info *section;
15660 bfd *abfd;
15661
15662 section = get_debug_line_section (cu);
15663 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15664 if (section->buffer == NULL)
15665 {
15666 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15667 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15668 else
15669 complaint (&symfile_complaints, _("missing .debug_line section"));
15670 return 0;
15671 }
15672
15673 /* We can't do this until we know the section is non-empty.
15674 Only then do we know we have such a section. */
15675 abfd = section->asection->owner;
15676
15677 /* Make sure that at least there's room for the total_length field.
15678 That could be 12 bytes long, but we're just going to fudge that. */
15679 if (offset + 4 >= section->size)
15680 {
15681 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15682 return 0;
15683 }
15684
15685 lh = xmalloc (sizeof (*lh));
15686 memset (lh, 0, sizeof (*lh));
15687 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15688 (void *) lh);
15689
15690 line_ptr = section->buffer + offset;
15691
15692 /* Read in the header. */
15693 lh->total_length =
15694 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15695 &bytes_read, &offset_size);
15696 line_ptr += bytes_read;
15697 if (line_ptr + lh->total_length > (section->buffer + section->size))
15698 {
15699 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15700 do_cleanups (back_to);
15701 return 0;
15702 }
15703 lh->statement_program_end = line_ptr + lh->total_length;
15704 lh->version = read_2_bytes (abfd, line_ptr);
15705 line_ptr += 2;
15706 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15707 line_ptr += offset_size;
15708 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15709 line_ptr += 1;
15710 if (lh->version >= 4)
15711 {
15712 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15713 line_ptr += 1;
15714 }
15715 else
15716 lh->maximum_ops_per_instruction = 1;
15717
15718 if (lh->maximum_ops_per_instruction == 0)
15719 {
15720 lh->maximum_ops_per_instruction = 1;
15721 complaint (&symfile_complaints,
15722 _("invalid maximum_ops_per_instruction "
15723 "in `.debug_line' section"));
15724 }
15725
15726 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15727 line_ptr += 1;
15728 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15729 line_ptr += 1;
15730 lh->line_range = read_1_byte (abfd, line_ptr);
15731 line_ptr += 1;
15732 lh->opcode_base = read_1_byte (abfd, line_ptr);
15733 line_ptr += 1;
15734 lh->standard_opcode_lengths
15735 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
15736
15737 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15738 for (i = 1; i < lh->opcode_base; ++i)
15739 {
15740 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15741 line_ptr += 1;
15742 }
15743
15744 /* Read directory table. */
15745 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15746 {
15747 line_ptr += bytes_read;
15748 add_include_dir (lh, cur_dir);
15749 }
15750 line_ptr += bytes_read;
15751
15752 /* Read file name table. */
15753 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15754 {
15755 unsigned int dir_index, mod_time, length;
15756
15757 line_ptr += bytes_read;
15758 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15759 line_ptr += bytes_read;
15760 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15761 line_ptr += bytes_read;
15762 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15763 line_ptr += bytes_read;
15764
15765 add_file_name (lh, cur_file, dir_index, mod_time, length);
15766 }
15767 line_ptr += bytes_read;
15768 lh->statement_program_start = line_ptr;
15769
15770 if (line_ptr > (section->buffer + section->size))
15771 complaint (&symfile_complaints,
15772 _("line number info header doesn't "
15773 "fit in `.debug_line' section"));
15774
15775 discard_cleanups (back_to);
15776 return lh;
15777 }
15778
15779 /* Subroutine of dwarf_decode_lines to simplify it.
15780 Return the file name of the psymtab for included file FILE_INDEX
15781 in line header LH of PST.
15782 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15783 If space for the result is malloc'd, it will be freed by a cleanup.
15784 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15785
15786 The function creates dangling cleanup registration. */
15787
15788 static const char *
15789 psymtab_include_file_name (const struct line_header *lh, int file_index,
15790 const struct partial_symtab *pst,
15791 const char *comp_dir)
15792 {
15793 const struct file_entry fe = lh->file_names [file_index];
15794 const char *include_name = fe.name;
15795 const char *include_name_to_compare = include_name;
15796 const char *dir_name = NULL;
15797 const char *pst_filename;
15798 char *copied_name = NULL;
15799 int file_is_pst;
15800
15801 if (fe.dir_index)
15802 dir_name = lh->include_dirs[fe.dir_index - 1];
15803
15804 if (!IS_ABSOLUTE_PATH (include_name)
15805 && (dir_name != NULL || comp_dir != NULL))
15806 {
15807 /* Avoid creating a duplicate psymtab for PST.
15808 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15809 Before we do the comparison, however, we need to account
15810 for DIR_NAME and COMP_DIR.
15811 First prepend dir_name (if non-NULL). If we still don't
15812 have an absolute path prepend comp_dir (if non-NULL).
15813 However, the directory we record in the include-file's
15814 psymtab does not contain COMP_DIR (to match the
15815 corresponding symtab(s)).
15816
15817 Example:
15818
15819 bash$ cd /tmp
15820 bash$ gcc -g ./hello.c
15821 include_name = "hello.c"
15822 dir_name = "."
15823 DW_AT_comp_dir = comp_dir = "/tmp"
15824 DW_AT_name = "./hello.c" */
15825
15826 if (dir_name != NULL)
15827 {
15828 char *tem = concat (dir_name, SLASH_STRING,
15829 include_name, (char *)NULL);
15830
15831 make_cleanup (xfree, tem);
15832 include_name = tem;
15833 include_name_to_compare = include_name;
15834 }
15835 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15836 {
15837 char *tem = concat (comp_dir, SLASH_STRING,
15838 include_name, (char *)NULL);
15839
15840 make_cleanup (xfree, tem);
15841 include_name_to_compare = tem;
15842 }
15843 }
15844
15845 pst_filename = pst->filename;
15846 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15847 {
15848 copied_name = concat (pst->dirname, SLASH_STRING,
15849 pst_filename, (char *)NULL);
15850 pst_filename = copied_name;
15851 }
15852
15853 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
15854
15855 if (copied_name != NULL)
15856 xfree (copied_name);
15857
15858 if (file_is_pst)
15859 return NULL;
15860 return include_name;
15861 }
15862
15863 /* Ignore this record_line request. */
15864
15865 static void
15866 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15867 {
15868 return;
15869 }
15870
15871 /* Subroutine of dwarf_decode_lines to simplify it.
15872 Process the line number information in LH. */
15873
15874 static void
15875 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15876 struct dwarf2_cu *cu, struct partial_symtab *pst)
15877 {
15878 const gdb_byte *line_ptr, *extended_end;
15879 const gdb_byte *line_end;
15880 unsigned int bytes_read, extended_len;
15881 unsigned char op_code, extended_op, adj_opcode;
15882 CORE_ADDR baseaddr;
15883 struct objfile *objfile = cu->objfile;
15884 bfd *abfd = objfile->obfd;
15885 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15886 const int decode_for_pst_p = (pst != NULL);
15887 struct subfile *last_subfile = NULL;
15888 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15889 = record_line;
15890
15891 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15892
15893 line_ptr = lh->statement_program_start;
15894 line_end = lh->statement_program_end;
15895
15896 /* Read the statement sequences until there's nothing left. */
15897 while (line_ptr < line_end)
15898 {
15899 /* state machine registers */
15900 CORE_ADDR address = 0;
15901 unsigned int file = 1;
15902 unsigned int line = 1;
15903 unsigned int column = 0;
15904 int is_stmt = lh->default_is_stmt;
15905 int basic_block = 0;
15906 int end_sequence = 0;
15907 CORE_ADDR addr;
15908 unsigned char op_index = 0;
15909
15910 if (!decode_for_pst_p && lh->num_file_names >= file)
15911 {
15912 /* Start a subfile for the current file of the state machine. */
15913 /* lh->include_dirs and lh->file_names are 0-based, but the
15914 directory and file name numbers in the statement program
15915 are 1-based. */
15916 struct file_entry *fe = &lh->file_names[file - 1];
15917 const char *dir = NULL;
15918
15919 if (fe->dir_index)
15920 dir = lh->include_dirs[fe->dir_index - 1];
15921
15922 dwarf2_start_subfile (fe->name, dir, comp_dir);
15923 }
15924
15925 /* Decode the table. */
15926 while (!end_sequence)
15927 {
15928 op_code = read_1_byte (abfd, line_ptr);
15929 line_ptr += 1;
15930 if (line_ptr > line_end)
15931 {
15932 dwarf2_debug_line_missing_end_sequence_complaint ();
15933 break;
15934 }
15935
15936 if (op_code >= lh->opcode_base)
15937 {
15938 /* Special operand. */
15939 adj_opcode = op_code - lh->opcode_base;
15940 address += (((op_index + (adj_opcode / lh->line_range))
15941 / lh->maximum_ops_per_instruction)
15942 * lh->minimum_instruction_length);
15943 op_index = ((op_index + (adj_opcode / lh->line_range))
15944 % lh->maximum_ops_per_instruction);
15945 line += lh->line_base + (adj_opcode % lh->line_range);
15946 if (lh->num_file_names < file || file == 0)
15947 dwarf2_debug_line_missing_file_complaint ();
15948 /* For now we ignore lines not starting on an
15949 instruction boundary. */
15950 else if (op_index == 0)
15951 {
15952 lh->file_names[file - 1].included_p = 1;
15953 if (!decode_for_pst_p && is_stmt)
15954 {
15955 if (last_subfile != current_subfile)
15956 {
15957 addr = gdbarch_addr_bits_remove (gdbarch, address);
15958 if (last_subfile)
15959 (*p_record_line) (last_subfile, 0, addr);
15960 last_subfile = current_subfile;
15961 }
15962 /* Append row to matrix using current values. */
15963 addr = gdbarch_addr_bits_remove (gdbarch, address);
15964 (*p_record_line) (current_subfile, line, addr);
15965 }
15966 }
15967 basic_block = 0;
15968 }
15969 else switch (op_code)
15970 {
15971 case DW_LNS_extended_op:
15972 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15973 &bytes_read);
15974 line_ptr += bytes_read;
15975 extended_end = line_ptr + extended_len;
15976 extended_op = read_1_byte (abfd, line_ptr);
15977 line_ptr += 1;
15978 switch (extended_op)
15979 {
15980 case DW_LNE_end_sequence:
15981 p_record_line = record_line;
15982 end_sequence = 1;
15983 break;
15984 case DW_LNE_set_address:
15985 address = read_address (abfd, line_ptr, cu, &bytes_read);
15986
15987 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15988 {
15989 /* This line table is for a function which has been
15990 GCd by the linker. Ignore it. PR gdb/12528 */
15991
15992 long line_offset
15993 = line_ptr - get_debug_line_section (cu)->buffer;
15994
15995 complaint (&symfile_complaints,
15996 _(".debug_line address at offset 0x%lx is 0 "
15997 "[in module %s]"),
15998 line_offset, objfile->name);
15999 p_record_line = noop_record_line;
16000 }
16001
16002 op_index = 0;
16003 line_ptr += bytes_read;
16004 address += baseaddr;
16005 break;
16006 case DW_LNE_define_file:
16007 {
16008 const char *cur_file;
16009 unsigned int dir_index, mod_time, length;
16010
16011 cur_file = read_direct_string (abfd, line_ptr,
16012 &bytes_read);
16013 line_ptr += bytes_read;
16014 dir_index =
16015 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16016 line_ptr += bytes_read;
16017 mod_time =
16018 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16019 line_ptr += bytes_read;
16020 length =
16021 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16022 line_ptr += bytes_read;
16023 add_file_name (lh, cur_file, dir_index, mod_time, length);
16024 }
16025 break;
16026 case DW_LNE_set_discriminator:
16027 /* The discriminator is not interesting to the debugger;
16028 just ignore it. */
16029 line_ptr = extended_end;
16030 break;
16031 default:
16032 complaint (&symfile_complaints,
16033 _("mangled .debug_line section"));
16034 return;
16035 }
16036 /* Make sure that we parsed the extended op correctly. If e.g.
16037 we expected a different address size than the producer used,
16038 we may have read the wrong number of bytes. */
16039 if (line_ptr != extended_end)
16040 {
16041 complaint (&symfile_complaints,
16042 _("mangled .debug_line section"));
16043 return;
16044 }
16045 break;
16046 case DW_LNS_copy:
16047 if (lh->num_file_names < file || file == 0)
16048 dwarf2_debug_line_missing_file_complaint ();
16049 else
16050 {
16051 lh->file_names[file - 1].included_p = 1;
16052 if (!decode_for_pst_p && is_stmt)
16053 {
16054 if (last_subfile != current_subfile)
16055 {
16056 addr = gdbarch_addr_bits_remove (gdbarch, address);
16057 if (last_subfile)
16058 (*p_record_line) (last_subfile, 0, addr);
16059 last_subfile = current_subfile;
16060 }
16061 addr = gdbarch_addr_bits_remove (gdbarch, address);
16062 (*p_record_line) (current_subfile, line, addr);
16063 }
16064 }
16065 basic_block = 0;
16066 break;
16067 case DW_LNS_advance_pc:
16068 {
16069 CORE_ADDR adjust
16070 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16071
16072 address += (((op_index + adjust)
16073 / lh->maximum_ops_per_instruction)
16074 * lh->minimum_instruction_length);
16075 op_index = ((op_index + adjust)
16076 % lh->maximum_ops_per_instruction);
16077 line_ptr += bytes_read;
16078 }
16079 break;
16080 case DW_LNS_advance_line:
16081 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
16082 line_ptr += bytes_read;
16083 break;
16084 case DW_LNS_set_file:
16085 {
16086 /* The arrays lh->include_dirs and lh->file_names are
16087 0-based, but the directory and file name numbers in
16088 the statement program are 1-based. */
16089 struct file_entry *fe;
16090 const char *dir = NULL;
16091
16092 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16093 line_ptr += bytes_read;
16094 if (lh->num_file_names < file || file == 0)
16095 dwarf2_debug_line_missing_file_complaint ();
16096 else
16097 {
16098 fe = &lh->file_names[file - 1];
16099 if (fe->dir_index)
16100 dir = lh->include_dirs[fe->dir_index - 1];
16101 if (!decode_for_pst_p)
16102 {
16103 last_subfile = current_subfile;
16104 dwarf2_start_subfile (fe->name, dir, comp_dir);
16105 }
16106 }
16107 }
16108 break;
16109 case DW_LNS_set_column:
16110 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16111 line_ptr += bytes_read;
16112 break;
16113 case DW_LNS_negate_stmt:
16114 is_stmt = (!is_stmt);
16115 break;
16116 case DW_LNS_set_basic_block:
16117 basic_block = 1;
16118 break;
16119 /* Add to the address register of the state machine the
16120 address increment value corresponding to special opcode
16121 255. I.e., this value is scaled by the minimum
16122 instruction length since special opcode 255 would have
16123 scaled the increment. */
16124 case DW_LNS_const_add_pc:
16125 {
16126 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
16127
16128 address += (((op_index + adjust)
16129 / lh->maximum_ops_per_instruction)
16130 * lh->minimum_instruction_length);
16131 op_index = ((op_index + adjust)
16132 % lh->maximum_ops_per_instruction);
16133 }
16134 break;
16135 case DW_LNS_fixed_advance_pc:
16136 address += read_2_bytes (abfd, line_ptr);
16137 op_index = 0;
16138 line_ptr += 2;
16139 break;
16140 default:
16141 {
16142 /* Unknown standard opcode, ignore it. */
16143 int i;
16144
16145 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
16146 {
16147 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16148 line_ptr += bytes_read;
16149 }
16150 }
16151 }
16152 }
16153 if (lh->num_file_names < file || file == 0)
16154 dwarf2_debug_line_missing_file_complaint ();
16155 else
16156 {
16157 lh->file_names[file - 1].included_p = 1;
16158 if (!decode_for_pst_p)
16159 {
16160 addr = gdbarch_addr_bits_remove (gdbarch, address);
16161 (*p_record_line) (current_subfile, 0, addr);
16162 }
16163 }
16164 }
16165 }
16166
16167 /* Decode the Line Number Program (LNP) for the given line_header
16168 structure and CU. The actual information extracted and the type
16169 of structures created from the LNP depends on the value of PST.
16170
16171 1. If PST is NULL, then this procedure uses the data from the program
16172 to create all necessary symbol tables, and their linetables.
16173
16174 2. If PST is not NULL, this procedure reads the program to determine
16175 the list of files included by the unit represented by PST, and
16176 builds all the associated partial symbol tables.
16177
16178 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
16179 It is used for relative paths in the line table.
16180 NOTE: When processing partial symtabs (pst != NULL),
16181 comp_dir == pst->dirname.
16182
16183 NOTE: It is important that psymtabs have the same file name (via strcmp)
16184 as the corresponding symtab. Since COMP_DIR is not used in the name of the
16185 symtab we don't use it in the name of the psymtabs we create.
16186 E.g. expand_line_sal requires this when finding psymtabs to expand.
16187 A good testcase for this is mb-inline.exp. */
16188
16189 static void
16190 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
16191 struct dwarf2_cu *cu, struct partial_symtab *pst,
16192 int want_line_info)
16193 {
16194 struct objfile *objfile = cu->objfile;
16195 const int decode_for_pst_p = (pst != NULL);
16196 struct subfile *first_subfile = current_subfile;
16197
16198 if (want_line_info)
16199 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
16200
16201 if (decode_for_pst_p)
16202 {
16203 int file_index;
16204
16205 /* Now that we're done scanning the Line Header Program, we can
16206 create the psymtab of each included file. */
16207 for (file_index = 0; file_index < lh->num_file_names; file_index++)
16208 if (lh->file_names[file_index].included_p == 1)
16209 {
16210 const char *include_name =
16211 psymtab_include_file_name (lh, file_index, pst, comp_dir);
16212 if (include_name != NULL)
16213 dwarf2_create_include_psymtab (include_name, pst, objfile);
16214 }
16215 }
16216 else
16217 {
16218 /* Make sure a symtab is created for every file, even files
16219 which contain only variables (i.e. no code with associated
16220 line numbers). */
16221 int i;
16222
16223 for (i = 0; i < lh->num_file_names; i++)
16224 {
16225 const char *dir = NULL;
16226 struct file_entry *fe;
16227
16228 fe = &lh->file_names[i];
16229 if (fe->dir_index)
16230 dir = lh->include_dirs[fe->dir_index - 1];
16231 dwarf2_start_subfile (fe->name, dir, comp_dir);
16232
16233 /* Skip the main file; we don't need it, and it must be
16234 allocated last, so that it will show up before the
16235 non-primary symtabs in the objfile's symtab list. */
16236 if (current_subfile == first_subfile)
16237 continue;
16238
16239 if (current_subfile->symtab == NULL)
16240 current_subfile->symtab = allocate_symtab (current_subfile->name,
16241 objfile);
16242 fe->symtab = current_subfile->symtab;
16243 }
16244 }
16245 }
16246
16247 /* Start a subfile for DWARF. FILENAME is the name of the file and
16248 DIRNAME the name of the source directory which contains FILENAME
16249 or NULL if not known. COMP_DIR is the compilation directory for the
16250 linetable's compilation unit or NULL if not known.
16251 This routine tries to keep line numbers from identical absolute and
16252 relative file names in a common subfile.
16253
16254 Using the `list' example from the GDB testsuite, which resides in
16255 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
16256 of /srcdir/list0.c yields the following debugging information for list0.c:
16257
16258 DW_AT_name: /srcdir/list0.c
16259 DW_AT_comp_dir: /compdir
16260 files.files[0].name: list0.h
16261 files.files[0].dir: /srcdir
16262 files.files[1].name: list0.c
16263 files.files[1].dir: /srcdir
16264
16265 The line number information for list0.c has to end up in a single
16266 subfile, so that `break /srcdir/list0.c:1' works as expected.
16267 start_subfile will ensure that this happens provided that we pass the
16268 concatenation of files.files[1].dir and files.files[1].name as the
16269 subfile's name. */
16270
16271 static void
16272 dwarf2_start_subfile (const char *filename, const char *dirname,
16273 const char *comp_dir)
16274 {
16275 char *copy = NULL;
16276
16277 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
16278 `start_symtab' will always pass the contents of DW_AT_comp_dir as
16279 second argument to start_subfile. To be consistent, we do the
16280 same here. In order not to lose the line information directory,
16281 we concatenate it to the filename when it makes sense.
16282 Note that the Dwarf3 standard says (speaking of filenames in line
16283 information): ``The directory index is ignored for file names
16284 that represent full path names''. Thus ignoring dirname in the
16285 `else' branch below isn't an issue. */
16286
16287 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
16288 {
16289 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
16290 filename = copy;
16291 }
16292
16293 start_subfile (filename, comp_dir);
16294
16295 if (copy != NULL)
16296 xfree (copy);
16297 }
16298
16299 /* Start a symtab for DWARF.
16300 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
16301
16302 static void
16303 dwarf2_start_symtab (struct dwarf2_cu *cu,
16304 const char *name, const char *comp_dir, CORE_ADDR low_pc)
16305 {
16306 start_symtab (name, comp_dir, low_pc);
16307 record_debugformat ("DWARF 2");
16308 record_producer (cu->producer);
16309
16310 /* We assume that we're processing GCC output. */
16311 processing_gcc_compilation = 2;
16312
16313 cu->processing_has_namespace_info = 0;
16314 }
16315
16316 static void
16317 var_decode_location (struct attribute *attr, struct symbol *sym,
16318 struct dwarf2_cu *cu)
16319 {
16320 struct objfile *objfile = cu->objfile;
16321 struct comp_unit_head *cu_header = &cu->header;
16322
16323 /* NOTE drow/2003-01-30: There used to be a comment and some special
16324 code here to turn a symbol with DW_AT_external and a
16325 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
16326 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
16327 with some versions of binutils) where shared libraries could have
16328 relocations against symbols in their debug information - the
16329 minimal symbol would have the right address, but the debug info
16330 would not. It's no longer necessary, because we will explicitly
16331 apply relocations when we read in the debug information now. */
16332
16333 /* A DW_AT_location attribute with no contents indicates that a
16334 variable has been optimized away. */
16335 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
16336 {
16337 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
16338 return;
16339 }
16340
16341 /* Handle one degenerate form of location expression specially, to
16342 preserve GDB's previous behavior when section offsets are
16343 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
16344 then mark this symbol as LOC_STATIC. */
16345
16346 if (attr_form_is_block (attr)
16347 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
16348 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
16349 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
16350 && (DW_BLOCK (attr)->size
16351 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
16352 {
16353 unsigned int dummy;
16354
16355 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
16356 SYMBOL_VALUE_ADDRESS (sym) =
16357 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
16358 else
16359 SYMBOL_VALUE_ADDRESS (sym) =
16360 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
16361 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
16362 fixup_symbol_section (sym, objfile);
16363 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
16364 SYMBOL_SECTION (sym));
16365 return;
16366 }
16367
16368 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
16369 expression evaluator, and use LOC_COMPUTED only when necessary
16370 (i.e. when the value of a register or memory location is
16371 referenced, or a thread-local block, etc.). Then again, it might
16372 not be worthwhile. I'm assuming that it isn't unless performance
16373 or memory numbers show me otherwise. */
16374
16375 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
16376
16377 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
16378 cu->has_loclist = 1;
16379 }
16380
16381 /* Given a pointer to a DWARF information entry, figure out if we need
16382 to make a symbol table entry for it, and if so, create a new entry
16383 and return a pointer to it.
16384 If TYPE is NULL, determine symbol type from the die, otherwise
16385 used the passed type.
16386 If SPACE is not NULL, use it to hold the new symbol. If it is
16387 NULL, allocate a new symbol on the objfile's obstack. */
16388
16389 static struct symbol *
16390 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
16391 struct symbol *space)
16392 {
16393 struct objfile *objfile = cu->objfile;
16394 struct symbol *sym = NULL;
16395 const char *name;
16396 struct attribute *attr = NULL;
16397 struct attribute *attr2 = NULL;
16398 CORE_ADDR baseaddr;
16399 struct pending **list_to_add = NULL;
16400
16401 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
16402
16403 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16404
16405 name = dwarf2_name (die, cu);
16406 if (name)
16407 {
16408 const char *linkagename;
16409 int suppress_add = 0;
16410
16411 if (space)
16412 sym = space;
16413 else
16414 sym = allocate_symbol (objfile);
16415 OBJSTAT (objfile, n_syms++);
16416
16417 /* Cache this symbol's name and the name's demangled form (if any). */
16418 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
16419 linkagename = dwarf2_physname (name, die, cu);
16420 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
16421
16422 /* Fortran does not have mangling standard and the mangling does differ
16423 between gfortran, iFort etc. */
16424 if (cu->language == language_fortran
16425 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
16426 symbol_set_demangled_name (&(sym->ginfo),
16427 dwarf2_full_name (name, die, cu),
16428 NULL);
16429
16430 /* Default assumptions.
16431 Use the passed type or decode it from the die. */
16432 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16433 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
16434 if (type != NULL)
16435 SYMBOL_TYPE (sym) = type;
16436 else
16437 SYMBOL_TYPE (sym) = die_type (die, cu);
16438 attr = dwarf2_attr (die,
16439 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
16440 cu);
16441 if (attr)
16442 {
16443 SYMBOL_LINE (sym) = DW_UNSND (attr);
16444 }
16445
16446 attr = dwarf2_attr (die,
16447 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
16448 cu);
16449 if (attr)
16450 {
16451 int file_index = DW_UNSND (attr);
16452
16453 if (cu->line_header == NULL
16454 || file_index > cu->line_header->num_file_names)
16455 complaint (&symfile_complaints,
16456 _("file index out of range"));
16457 else if (file_index > 0)
16458 {
16459 struct file_entry *fe;
16460
16461 fe = &cu->line_header->file_names[file_index - 1];
16462 SYMBOL_SYMTAB (sym) = fe->symtab;
16463 }
16464 }
16465
16466 switch (die->tag)
16467 {
16468 case DW_TAG_label:
16469 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
16470 if (attr)
16471 {
16472 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
16473 }
16474 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
16475 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
16476 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
16477 add_symbol_to_list (sym, cu->list_in_scope);
16478 break;
16479 case DW_TAG_subprogram:
16480 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16481 finish_block. */
16482 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
16483 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16484 if ((attr2 && (DW_UNSND (attr2) != 0))
16485 || cu->language == language_ada)
16486 {
16487 /* Subprograms marked external are stored as a global symbol.
16488 Ada subprograms, whether marked external or not, are always
16489 stored as a global symbol, because we want to be able to
16490 access them globally. For instance, we want to be able
16491 to break on a nested subprogram without having to
16492 specify the context. */
16493 list_to_add = &global_symbols;
16494 }
16495 else
16496 {
16497 list_to_add = cu->list_in_scope;
16498 }
16499 break;
16500 case DW_TAG_inlined_subroutine:
16501 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16502 finish_block. */
16503 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
16504 SYMBOL_INLINED (sym) = 1;
16505 list_to_add = cu->list_in_scope;
16506 break;
16507 case DW_TAG_template_value_param:
16508 suppress_add = 1;
16509 /* Fall through. */
16510 case DW_TAG_constant:
16511 case DW_TAG_variable:
16512 case DW_TAG_member:
16513 /* Compilation with minimal debug info may result in
16514 variables with missing type entries. Change the
16515 misleading `void' type to something sensible. */
16516 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
16517 SYMBOL_TYPE (sym)
16518 = objfile_type (objfile)->nodebug_data_symbol;
16519
16520 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16521 /* In the case of DW_TAG_member, we should only be called for
16522 static const members. */
16523 if (die->tag == DW_TAG_member)
16524 {
16525 /* dwarf2_add_field uses die_is_declaration,
16526 so we do the same. */
16527 gdb_assert (die_is_declaration (die, cu));
16528 gdb_assert (attr);
16529 }
16530 if (attr)
16531 {
16532 dwarf2_const_value (attr, sym, cu);
16533 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16534 if (!suppress_add)
16535 {
16536 if (attr2 && (DW_UNSND (attr2) != 0))
16537 list_to_add = &global_symbols;
16538 else
16539 list_to_add = cu->list_in_scope;
16540 }
16541 break;
16542 }
16543 attr = dwarf2_attr (die, DW_AT_location, cu);
16544 if (attr)
16545 {
16546 var_decode_location (attr, sym, cu);
16547 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16548
16549 /* Fortran explicitly imports any global symbols to the local
16550 scope by DW_TAG_common_block. */
16551 if (cu->language == language_fortran && die->parent
16552 && die->parent->tag == DW_TAG_common_block)
16553 attr2 = NULL;
16554
16555 if (SYMBOL_CLASS (sym) == LOC_STATIC
16556 && SYMBOL_VALUE_ADDRESS (sym) == 0
16557 && !dwarf2_per_objfile->has_section_at_zero)
16558 {
16559 /* When a static variable is eliminated by the linker,
16560 the corresponding debug information is not stripped
16561 out, but the variable address is set to null;
16562 do not add such variables into symbol table. */
16563 }
16564 else if (attr2 && (DW_UNSND (attr2) != 0))
16565 {
16566 /* Workaround gfortran PR debug/40040 - it uses
16567 DW_AT_location for variables in -fPIC libraries which may
16568 get overriden by other libraries/executable and get
16569 a different address. Resolve it by the minimal symbol
16570 which may come from inferior's executable using copy
16571 relocation. Make this workaround only for gfortran as for
16572 other compilers GDB cannot guess the minimal symbol
16573 Fortran mangling kind. */
16574 if (cu->language == language_fortran && die->parent
16575 && die->parent->tag == DW_TAG_module
16576 && cu->producer
16577 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
16578 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
16579
16580 /* A variable with DW_AT_external is never static,
16581 but it may be block-scoped. */
16582 list_to_add = (cu->list_in_scope == &file_symbols
16583 ? &global_symbols : cu->list_in_scope);
16584 }
16585 else
16586 list_to_add = cu->list_in_scope;
16587 }
16588 else
16589 {
16590 /* We do not know the address of this symbol.
16591 If it is an external symbol and we have type information
16592 for it, enter the symbol as a LOC_UNRESOLVED symbol.
16593 The address of the variable will then be determined from
16594 the minimal symbol table whenever the variable is
16595 referenced. */
16596 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16597
16598 /* Fortran explicitly imports any global symbols to the local
16599 scope by DW_TAG_common_block. */
16600 if (cu->language == language_fortran && die->parent
16601 && die->parent->tag == DW_TAG_common_block)
16602 {
16603 /* SYMBOL_CLASS doesn't matter here because
16604 read_common_block is going to reset it. */
16605 if (!suppress_add)
16606 list_to_add = cu->list_in_scope;
16607 }
16608 else if (attr2 && (DW_UNSND (attr2) != 0)
16609 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
16610 {
16611 /* A variable with DW_AT_external is never static, but it
16612 may be block-scoped. */
16613 list_to_add = (cu->list_in_scope == &file_symbols
16614 ? &global_symbols : cu->list_in_scope);
16615
16616 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
16617 }
16618 else if (!die_is_declaration (die, cu))
16619 {
16620 /* Use the default LOC_OPTIMIZED_OUT class. */
16621 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
16622 if (!suppress_add)
16623 list_to_add = cu->list_in_scope;
16624 }
16625 }
16626 break;
16627 case DW_TAG_formal_parameter:
16628 /* If we are inside a function, mark this as an argument. If
16629 not, we might be looking at an argument to an inlined function
16630 when we do not have enough information to show inlined frames;
16631 pretend it's a local variable in that case so that the user can
16632 still see it. */
16633 if (context_stack_depth > 0
16634 && context_stack[context_stack_depth - 1].name != NULL)
16635 SYMBOL_IS_ARGUMENT (sym) = 1;
16636 attr = dwarf2_attr (die, DW_AT_location, cu);
16637 if (attr)
16638 {
16639 var_decode_location (attr, sym, cu);
16640 }
16641 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16642 if (attr)
16643 {
16644 dwarf2_const_value (attr, sym, cu);
16645 }
16646
16647 list_to_add = cu->list_in_scope;
16648 break;
16649 case DW_TAG_unspecified_parameters:
16650 /* From varargs functions; gdb doesn't seem to have any
16651 interest in this information, so just ignore it for now.
16652 (FIXME?) */
16653 break;
16654 case DW_TAG_template_type_param:
16655 suppress_add = 1;
16656 /* Fall through. */
16657 case DW_TAG_class_type:
16658 case DW_TAG_interface_type:
16659 case DW_TAG_structure_type:
16660 case DW_TAG_union_type:
16661 case DW_TAG_set_type:
16662 case DW_TAG_enumeration_type:
16663 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16664 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
16665
16666 {
16667 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
16668 really ever be static objects: otherwise, if you try
16669 to, say, break of a class's method and you're in a file
16670 which doesn't mention that class, it won't work unless
16671 the check for all static symbols in lookup_symbol_aux
16672 saves you. See the OtherFileClass tests in
16673 gdb.c++/namespace.exp. */
16674
16675 if (!suppress_add)
16676 {
16677 list_to_add = (cu->list_in_scope == &file_symbols
16678 && (cu->language == language_cplus
16679 || cu->language == language_java)
16680 ? &global_symbols : cu->list_in_scope);
16681
16682 /* The semantics of C++ state that "struct foo {
16683 ... }" also defines a typedef for "foo". A Java
16684 class declaration also defines a typedef for the
16685 class. */
16686 if (cu->language == language_cplus
16687 || cu->language == language_java
16688 || cu->language == language_ada)
16689 {
16690 /* The symbol's name is already allocated along
16691 with this objfile, so we don't need to
16692 duplicate it for the type. */
16693 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16694 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16695 }
16696 }
16697 }
16698 break;
16699 case DW_TAG_typedef:
16700 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16701 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16702 list_to_add = cu->list_in_scope;
16703 break;
16704 case DW_TAG_base_type:
16705 case DW_TAG_subrange_type:
16706 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16707 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16708 list_to_add = cu->list_in_scope;
16709 break;
16710 case DW_TAG_enumerator:
16711 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16712 if (attr)
16713 {
16714 dwarf2_const_value (attr, sym, cu);
16715 }
16716 {
16717 /* NOTE: carlton/2003-11-10: See comment above in the
16718 DW_TAG_class_type, etc. block. */
16719
16720 list_to_add = (cu->list_in_scope == &file_symbols
16721 && (cu->language == language_cplus
16722 || cu->language == language_java)
16723 ? &global_symbols : cu->list_in_scope);
16724 }
16725 break;
16726 case DW_TAG_namespace:
16727 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16728 list_to_add = &global_symbols;
16729 break;
16730 case DW_TAG_common_block:
16731 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
16732 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16733 add_symbol_to_list (sym, cu->list_in_scope);
16734 break;
16735 default:
16736 /* Not a tag we recognize. Hopefully we aren't processing
16737 trash data, but since we must specifically ignore things
16738 we don't recognize, there is nothing else we should do at
16739 this point. */
16740 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
16741 dwarf_tag_name (die->tag));
16742 break;
16743 }
16744
16745 if (suppress_add)
16746 {
16747 sym->hash_next = objfile->template_symbols;
16748 objfile->template_symbols = sym;
16749 list_to_add = NULL;
16750 }
16751
16752 if (list_to_add != NULL)
16753 add_symbol_to_list (sym, list_to_add);
16754
16755 /* For the benefit of old versions of GCC, check for anonymous
16756 namespaces based on the demangled name. */
16757 if (!cu->processing_has_namespace_info
16758 && cu->language == language_cplus)
16759 cp_scan_for_anonymous_namespaces (sym, objfile);
16760 }
16761 return (sym);
16762 }
16763
16764 /* A wrapper for new_symbol_full that always allocates a new symbol. */
16765
16766 static struct symbol *
16767 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16768 {
16769 return new_symbol_full (die, type, cu, NULL);
16770 }
16771
16772 /* Given an attr with a DW_FORM_dataN value in host byte order,
16773 zero-extend it as appropriate for the symbol's type. The DWARF
16774 standard (v4) is not entirely clear about the meaning of using
16775 DW_FORM_dataN for a constant with a signed type, where the type is
16776 wider than the data. The conclusion of a discussion on the DWARF
16777 list was that this is unspecified. We choose to always zero-extend
16778 because that is the interpretation long in use by GCC. */
16779
16780 static gdb_byte *
16781 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
16782 struct dwarf2_cu *cu, LONGEST *value, int bits)
16783 {
16784 struct objfile *objfile = cu->objfile;
16785 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16786 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
16787 LONGEST l = DW_UNSND (attr);
16788
16789 if (bits < sizeof (*value) * 8)
16790 {
16791 l &= ((LONGEST) 1 << bits) - 1;
16792 *value = l;
16793 }
16794 else if (bits == sizeof (*value) * 8)
16795 *value = l;
16796 else
16797 {
16798 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16799 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16800 return bytes;
16801 }
16802
16803 return NULL;
16804 }
16805
16806 /* Read a constant value from an attribute. Either set *VALUE, or if
16807 the value does not fit in *VALUE, set *BYTES - either already
16808 allocated on the objfile obstack, or newly allocated on OBSTACK,
16809 or, set *BATON, if we translated the constant to a location
16810 expression. */
16811
16812 static void
16813 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
16814 const char *name, struct obstack *obstack,
16815 struct dwarf2_cu *cu,
16816 LONGEST *value, const gdb_byte **bytes,
16817 struct dwarf2_locexpr_baton **baton)
16818 {
16819 struct objfile *objfile = cu->objfile;
16820 struct comp_unit_head *cu_header = &cu->header;
16821 struct dwarf_block *blk;
16822 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16823 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16824
16825 *value = 0;
16826 *bytes = NULL;
16827 *baton = NULL;
16828
16829 switch (attr->form)
16830 {
16831 case DW_FORM_addr:
16832 case DW_FORM_GNU_addr_index:
16833 {
16834 gdb_byte *data;
16835
16836 if (TYPE_LENGTH (type) != cu_header->addr_size)
16837 dwarf2_const_value_length_mismatch_complaint (name,
16838 cu_header->addr_size,
16839 TYPE_LENGTH (type));
16840 /* Symbols of this form are reasonably rare, so we just
16841 piggyback on the existing location code rather than writing
16842 a new implementation of symbol_computed_ops. */
16843 *baton = obstack_alloc (obstack, sizeof (struct dwarf2_locexpr_baton));
16844 (*baton)->per_cu = cu->per_cu;
16845 gdb_assert ((*baton)->per_cu);
16846
16847 (*baton)->size = 2 + cu_header->addr_size;
16848 data = obstack_alloc (obstack, (*baton)->size);
16849 (*baton)->data = data;
16850
16851 data[0] = DW_OP_addr;
16852 store_unsigned_integer (&data[1], cu_header->addr_size,
16853 byte_order, DW_ADDR (attr));
16854 data[cu_header->addr_size + 1] = DW_OP_stack_value;
16855 }
16856 break;
16857 case DW_FORM_string:
16858 case DW_FORM_strp:
16859 case DW_FORM_GNU_str_index:
16860 case DW_FORM_GNU_strp_alt:
16861 /* DW_STRING is already allocated on the objfile obstack, point
16862 directly to it. */
16863 *bytes = (const gdb_byte *) DW_STRING (attr);
16864 break;
16865 case DW_FORM_block1:
16866 case DW_FORM_block2:
16867 case DW_FORM_block4:
16868 case DW_FORM_block:
16869 case DW_FORM_exprloc:
16870 blk = DW_BLOCK (attr);
16871 if (TYPE_LENGTH (type) != blk->size)
16872 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16873 TYPE_LENGTH (type));
16874 *bytes = blk->data;
16875 break;
16876
16877 /* The DW_AT_const_value attributes are supposed to carry the
16878 symbol's value "represented as it would be on the target
16879 architecture." By the time we get here, it's already been
16880 converted to host endianness, so we just need to sign- or
16881 zero-extend it as appropriate. */
16882 case DW_FORM_data1:
16883 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
16884 break;
16885 case DW_FORM_data2:
16886 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
16887 break;
16888 case DW_FORM_data4:
16889 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
16890 break;
16891 case DW_FORM_data8:
16892 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
16893 break;
16894
16895 case DW_FORM_sdata:
16896 *value = DW_SND (attr);
16897 break;
16898
16899 case DW_FORM_udata:
16900 *value = DW_UNSND (attr);
16901 break;
16902
16903 default:
16904 complaint (&symfile_complaints,
16905 _("unsupported const value attribute form: '%s'"),
16906 dwarf_form_name (attr->form));
16907 *value = 0;
16908 break;
16909 }
16910 }
16911
16912
16913 /* Copy constant value from an attribute to a symbol. */
16914
16915 static void
16916 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
16917 struct dwarf2_cu *cu)
16918 {
16919 struct objfile *objfile = cu->objfile;
16920 struct comp_unit_head *cu_header = &cu->header;
16921 LONGEST value;
16922 const gdb_byte *bytes;
16923 struct dwarf2_locexpr_baton *baton;
16924
16925 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16926 SYMBOL_PRINT_NAME (sym),
16927 &objfile->objfile_obstack, cu,
16928 &value, &bytes, &baton);
16929
16930 if (baton != NULL)
16931 {
16932 SYMBOL_LOCATION_BATON (sym) = baton;
16933 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16934 }
16935 else if (bytes != NULL)
16936 {
16937 SYMBOL_VALUE_BYTES (sym) = bytes;
16938 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
16939 }
16940 else
16941 {
16942 SYMBOL_VALUE (sym) = value;
16943 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
16944 }
16945 }
16946
16947 /* Return the type of the die in question using its DW_AT_type attribute. */
16948
16949 static struct type *
16950 die_type (struct die_info *die, struct dwarf2_cu *cu)
16951 {
16952 struct attribute *type_attr;
16953
16954 type_attr = dwarf2_attr (die, DW_AT_type, cu);
16955 if (!type_attr)
16956 {
16957 /* A missing DW_AT_type represents a void type. */
16958 return objfile_type (cu->objfile)->builtin_void;
16959 }
16960
16961 return lookup_die_type (die, type_attr, cu);
16962 }
16963
16964 /* True iff CU's producer generates GNAT Ada auxiliary information
16965 that allows to find parallel types through that information instead
16966 of having to do expensive parallel lookups by type name. */
16967
16968 static int
16969 need_gnat_info (struct dwarf2_cu *cu)
16970 {
16971 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16972 of GNAT produces this auxiliary information, without any indication
16973 that it is produced. Part of enhancing the FSF version of GNAT
16974 to produce that information will be to put in place an indicator
16975 that we can use in order to determine whether the descriptive type
16976 info is available or not. One suggestion that has been made is
16977 to use a new attribute, attached to the CU die. For now, assume
16978 that the descriptive type info is not available. */
16979 return 0;
16980 }
16981
16982 /* Return the auxiliary type of the die in question using its
16983 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16984 attribute is not present. */
16985
16986 static struct type *
16987 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16988 {
16989 struct attribute *type_attr;
16990
16991 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16992 if (!type_attr)
16993 return NULL;
16994
16995 return lookup_die_type (die, type_attr, cu);
16996 }
16997
16998 /* If DIE has a descriptive_type attribute, then set the TYPE's
16999 descriptive type accordingly. */
17000
17001 static void
17002 set_descriptive_type (struct type *type, struct die_info *die,
17003 struct dwarf2_cu *cu)
17004 {
17005 struct type *descriptive_type = die_descriptive_type (die, cu);
17006
17007 if (descriptive_type)
17008 {
17009 ALLOCATE_GNAT_AUX_TYPE (type);
17010 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
17011 }
17012 }
17013
17014 /* Return the containing type of the die in question using its
17015 DW_AT_containing_type attribute. */
17016
17017 static struct type *
17018 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
17019 {
17020 struct attribute *type_attr;
17021
17022 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
17023 if (!type_attr)
17024 error (_("Dwarf Error: Problem turning containing type into gdb type "
17025 "[in module %s]"), cu->objfile->name);
17026
17027 return lookup_die_type (die, type_attr, cu);
17028 }
17029
17030 /* Return an error marker type to use for the ill formed type in DIE/CU. */
17031
17032 static struct type *
17033 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
17034 {
17035 struct objfile *objfile = dwarf2_per_objfile->objfile;
17036 char *message, *saved;
17037
17038 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
17039 objfile->name,
17040 cu->header.offset.sect_off,
17041 die->offset.sect_off);
17042 saved = obstack_copy0 (&objfile->objfile_obstack,
17043 message, strlen (message));
17044 xfree (message);
17045
17046 return init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
17047 }
17048
17049 /* Look up the type of DIE in CU using its type attribute ATTR.
17050 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
17051 DW_AT_containing_type.
17052 If there is no type substitute an error marker. */
17053
17054 static struct type *
17055 lookup_die_type (struct die_info *die, const struct attribute *attr,
17056 struct dwarf2_cu *cu)
17057 {
17058 struct objfile *objfile = cu->objfile;
17059 struct type *this_type;
17060
17061 gdb_assert (attr->name == DW_AT_type
17062 || attr->name == DW_AT_GNAT_descriptive_type
17063 || attr->name == DW_AT_containing_type);
17064
17065 /* First see if we have it cached. */
17066
17067 if (attr->form == DW_FORM_GNU_ref_alt)
17068 {
17069 struct dwarf2_per_cu_data *per_cu;
17070 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17071
17072 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
17073 this_type = get_die_type_at_offset (offset, per_cu);
17074 }
17075 else if (attr_form_is_ref (attr))
17076 {
17077 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17078
17079 this_type = get_die_type_at_offset (offset, cu->per_cu);
17080 }
17081 else if (attr->form == DW_FORM_ref_sig8)
17082 {
17083 ULONGEST signature = DW_SIGNATURE (attr);
17084
17085 return get_signatured_type (die, signature, cu);
17086 }
17087 else
17088 {
17089 complaint (&symfile_complaints,
17090 _("Dwarf Error: Bad type attribute %s in DIE"
17091 " at 0x%x [in module %s]"),
17092 dwarf_attr_name (attr->name), die->offset.sect_off,
17093 objfile->name);
17094 return build_error_marker_type (cu, die);
17095 }
17096
17097 /* If not cached we need to read it in. */
17098
17099 if (this_type == NULL)
17100 {
17101 struct die_info *type_die = NULL;
17102 struct dwarf2_cu *type_cu = cu;
17103
17104 if (attr_form_is_ref (attr))
17105 type_die = follow_die_ref (die, attr, &type_cu);
17106 if (type_die == NULL)
17107 return build_error_marker_type (cu, die);
17108 /* If we find the type now, it's probably because the type came
17109 from an inter-CU reference and the type's CU got expanded before
17110 ours. */
17111 this_type = read_type_die (type_die, type_cu);
17112 }
17113
17114 /* If we still don't have a type use an error marker. */
17115
17116 if (this_type == NULL)
17117 return build_error_marker_type (cu, die);
17118
17119 return this_type;
17120 }
17121
17122 /* Return the type in DIE, CU.
17123 Returns NULL for invalid types.
17124
17125 This first does a lookup in die_type_hash,
17126 and only reads the die in if necessary.
17127
17128 NOTE: This can be called when reading in partial or full symbols. */
17129
17130 static struct type *
17131 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
17132 {
17133 struct type *this_type;
17134
17135 this_type = get_die_type (die, cu);
17136 if (this_type)
17137 return this_type;
17138
17139 return read_type_die_1 (die, cu);
17140 }
17141
17142 /* Read the type in DIE, CU.
17143 Returns NULL for invalid types. */
17144
17145 static struct type *
17146 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
17147 {
17148 struct type *this_type = NULL;
17149
17150 switch (die->tag)
17151 {
17152 case DW_TAG_class_type:
17153 case DW_TAG_interface_type:
17154 case DW_TAG_structure_type:
17155 case DW_TAG_union_type:
17156 this_type = read_structure_type (die, cu);
17157 break;
17158 case DW_TAG_enumeration_type:
17159 this_type = read_enumeration_type (die, cu);
17160 break;
17161 case DW_TAG_subprogram:
17162 case DW_TAG_subroutine_type:
17163 case DW_TAG_inlined_subroutine:
17164 this_type = read_subroutine_type (die, cu);
17165 break;
17166 case DW_TAG_array_type:
17167 this_type = read_array_type (die, cu);
17168 break;
17169 case DW_TAG_set_type:
17170 this_type = read_set_type (die, cu);
17171 break;
17172 case DW_TAG_pointer_type:
17173 this_type = read_tag_pointer_type (die, cu);
17174 break;
17175 case DW_TAG_ptr_to_member_type:
17176 this_type = read_tag_ptr_to_member_type (die, cu);
17177 break;
17178 case DW_TAG_reference_type:
17179 this_type = read_tag_reference_type (die, cu);
17180 break;
17181 case DW_TAG_const_type:
17182 this_type = read_tag_const_type (die, cu);
17183 break;
17184 case DW_TAG_volatile_type:
17185 this_type = read_tag_volatile_type (die, cu);
17186 break;
17187 case DW_TAG_restrict_type:
17188 this_type = read_tag_restrict_type (die, cu);
17189 break;
17190 case DW_TAG_string_type:
17191 this_type = read_tag_string_type (die, cu);
17192 break;
17193 case DW_TAG_typedef:
17194 this_type = read_typedef (die, cu);
17195 break;
17196 case DW_TAG_subrange_type:
17197 this_type = read_subrange_type (die, cu);
17198 break;
17199 case DW_TAG_base_type:
17200 this_type = read_base_type (die, cu);
17201 break;
17202 case DW_TAG_unspecified_type:
17203 this_type = read_unspecified_type (die, cu);
17204 break;
17205 case DW_TAG_namespace:
17206 this_type = read_namespace_type (die, cu);
17207 break;
17208 case DW_TAG_module:
17209 this_type = read_module_type (die, cu);
17210 break;
17211 default:
17212 complaint (&symfile_complaints,
17213 _("unexpected tag in read_type_die: '%s'"),
17214 dwarf_tag_name (die->tag));
17215 break;
17216 }
17217
17218 return this_type;
17219 }
17220
17221 /* See if we can figure out if the class lives in a namespace. We do
17222 this by looking for a member function; its demangled name will
17223 contain namespace info, if there is any.
17224 Return the computed name or NULL.
17225 Space for the result is allocated on the objfile's obstack.
17226 This is the full-die version of guess_partial_die_structure_name.
17227 In this case we know DIE has no useful parent. */
17228
17229 static char *
17230 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
17231 {
17232 struct die_info *spec_die;
17233 struct dwarf2_cu *spec_cu;
17234 struct die_info *child;
17235
17236 spec_cu = cu;
17237 spec_die = die_specification (die, &spec_cu);
17238 if (spec_die != NULL)
17239 {
17240 die = spec_die;
17241 cu = spec_cu;
17242 }
17243
17244 for (child = die->child;
17245 child != NULL;
17246 child = child->sibling)
17247 {
17248 if (child->tag == DW_TAG_subprogram)
17249 {
17250 struct attribute *attr;
17251
17252 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
17253 if (attr == NULL)
17254 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
17255 if (attr != NULL)
17256 {
17257 char *actual_name
17258 = language_class_name_from_physname (cu->language_defn,
17259 DW_STRING (attr));
17260 char *name = NULL;
17261
17262 if (actual_name != NULL)
17263 {
17264 const char *die_name = dwarf2_name (die, cu);
17265
17266 if (die_name != NULL
17267 && strcmp (die_name, actual_name) != 0)
17268 {
17269 /* Strip off the class name from the full name.
17270 We want the prefix. */
17271 int die_name_len = strlen (die_name);
17272 int actual_name_len = strlen (actual_name);
17273
17274 /* Test for '::' as a sanity check. */
17275 if (actual_name_len > die_name_len + 2
17276 && actual_name[actual_name_len
17277 - die_name_len - 1] == ':')
17278 name =
17279 obstack_copy0 (&cu->objfile->objfile_obstack,
17280 actual_name,
17281 actual_name_len - die_name_len - 2);
17282 }
17283 }
17284 xfree (actual_name);
17285 return name;
17286 }
17287 }
17288 }
17289
17290 return NULL;
17291 }
17292
17293 /* GCC might emit a nameless typedef that has a linkage name. Determine the
17294 prefix part in such case. See
17295 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17296
17297 static char *
17298 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
17299 {
17300 struct attribute *attr;
17301 char *base;
17302
17303 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
17304 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
17305 return NULL;
17306
17307 attr = dwarf2_attr (die, DW_AT_name, cu);
17308 if (attr != NULL && DW_STRING (attr) != NULL)
17309 return NULL;
17310
17311 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17312 if (attr == NULL)
17313 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17314 if (attr == NULL || DW_STRING (attr) == NULL)
17315 return NULL;
17316
17317 /* dwarf2_name had to be already called. */
17318 gdb_assert (DW_STRING_IS_CANONICAL (attr));
17319
17320 /* Strip the base name, keep any leading namespaces/classes. */
17321 base = strrchr (DW_STRING (attr), ':');
17322 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
17323 return "";
17324
17325 return obstack_copy0 (&cu->objfile->objfile_obstack,
17326 DW_STRING (attr), &base[-1] - DW_STRING (attr));
17327 }
17328
17329 /* Return the name of the namespace/class that DIE is defined within,
17330 or "" if we can't tell. The caller should not xfree the result.
17331
17332 For example, if we're within the method foo() in the following
17333 code:
17334
17335 namespace N {
17336 class C {
17337 void foo () {
17338 }
17339 };
17340 }
17341
17342 then determine_prefix on foo's die will return "N::C". */
17343
17344 static const char *
17345 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
17346 {
17347 struct die_info *parent, *spec_die;
17348 struct dwarf2_cu *spec_cu;
17349 struct type *parent_type;
17350 char *retval;
17351
17352 if (cu->language != language_cplus && cu->language != language_java
17353 && cu->language != language_fortran)
17354 return "";
17355
17356 retval = anonymous_struct_prefix (die, cu);
17357 if (retval)
17358 return retval;
17359
17360 /* We have to be careful in the presence of DW_AT_specification.
17361 For example, with GCC 3.4, given the code
17362
17363 namespace N {
17364 void foo() {
17365 // Definition of N::foo.
17366 }
17367 }
17368
17369 then we'll have a tree of DIEs like this:
17370
17371 1: DW_TAG_compile_unit
17372 2: DW_TAG_namespace // N
17373 3: DW_TAG_subprogram // declaration of N::foo
17374 4: DW_TAG_subprogram // definition of N::foo
17375 DW_AT_specification // refers to die #3
17376
17377 Thus, when processing die #4, we have to pretend that we're in
17378 the context of its DW_AT_specification, namely the contex of die
17379 #3. */
17380 spec_cu = cu;
17381 spec_die = die_specification (die, &spec_cu);
17382 if (spec_die == NULL)
17383 parent = die->parent;
17384 else
17385 {
17386 parent = spec_die->parent;
17387 cu = spec_cu;
17388 }
17389
17390 if (parent == NULL)
17391 return "";
17392 else if (parent->building_fullname)
17393 {
17394 const char *name;
17395 const char *parent_name;
17396
17397 /* It has been seen on RealView 2.2 built binaries,
17398 DW_TAG_template_type_param types actually _defined_ as
17399 children of the parent class:
17400
17401 enum E {};
17402 template class <class Enum> Class{};
17403 Class<enum E> class_e;
17404
17405 1: DW_TAG_class_type (Class)
17406 2: DW_TAG_enumeration_type (E)
17407 3: DW_TAG_enumerator (enum1:0)
17408 3: DW_TAG_enumerator (enum2:1)
17409 ...
17410 2: DW_TAG_template_type_param
17411 DW_AT_type DW_FORM_ref_udata (E)
17412
17413 Besides being broken debug info, it can put GDB into an
17414 infinite loop. Consider:
17415
17416 When we're building the full name for Class<E>, we'll start
17417 at Class, and go look over its template type parameters,
17418 finding E. We'll then try to build the full name of E, and
17419 reach here. We're now trying to build the full name of E,
17420 and look over the parent DIE for containing scope. In the
17421 broken case, if we followed the parent DIE of E, we'd again
17422 find Class, and once again go look at its template type
17423 arguments, etc., etc. Simply don't consider such parent die
17424 as source-level parent of this die (it can't be, the language
17425 doesn't allow it), and break the loop here. */
17426 name = dwarf2_name (die, cu);
17427 parent_name = dwarf2_name (parent, cu);
17428 complaint (&symfile_complaints,
17429 _("template param type '%s' defined within parent '%s'"),
17430 name ? name : "<unknown>",
17431 parent_name ? parent_name : "<unknown>");
17432 return "";
17433 }
17434 else
17435 switch (parent->tag)
17436 {
17437 case DW_TAG_namespace:
17438 parent_type = read_type_die (parent, cu);
17439 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
17440 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
17441 Work around this problem here. */
17442 if (cu->language == language_cplus
17443 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
17444 return "";
17445 /* We give a name to even anonymous namespaces. */
17446 return TYPE_TAG_NAME (parent_type);
17447 case DW_TAG_class_type:
17448 case DW_TAG_interface_type:
17449 case DW_TAG_structure_type:
17450 case DW_TAG_union_type:
17451 case DW_TAG_module:
17452 parent_type = read_type_die (parent, cu);
17453 if (TYPE_TAG_NAME (parent_type) != NULL)
17454 return TYPE_TAG_NAME (parent_type);
17455 else
17456 /* An anonymous structure is only allowed non-static data
17457 members; no typedefs, no member functions, et cetera.
17458 So it does not need a prefix. */
17459 return "";
17460 case DW_TAG_compile_unit:
17461 case DW_TAG_partial_unit:
17462 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
17463 if (cu->language == language_cplus
17464 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
17465 && die->child != NULL
17466 && (die->tag == DW_TAG_class_type
17467 || die->tag == DW_TAG_structure_type
17468 || die->tag == DW_TAG_union_type))
17469 {
17470 char *name = guess_full_die_structure_name (die, cu);
17471 if (name != NULL)
17472 return name;
17473 }
17474 return "";
17475 default:
17476 return determine_prefix (parent, cu);
17477 }
17478 }
17479
17480 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
17481 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
17482 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
17483 an obconcat, otherwise allocate storage for the result. The CU argument is
17484 used to determine the language and hence, the appropriate separator. */
17485
17486 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
17487
17488 static char *
17489 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
17490 int physname, struct dwarf2_cu *cu)
17491 {
17492 const char *lead = "";
17493 const char *sep;
17494
17495 if (suffix == NULL || suffix[0] == '\0'
17496 || prefix == NULL || prefix[0] == '\0')
17497 sep = "";
17498 else if (cu->language == language_java)
17499 sep = ".";
17500 else if (cu->language == language_fortran && physname)
17501 {
17502 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
17503 DW_AT_MIPS_linkage_name is preferred and used instead. */
17504
17505 lead = "__";
17506 sep = "_MOD_";
17507 }
17508 else
17509 sep = "::";
17510
17511 if (prefix == NULL)
17512 prefix = "";
17513 if (suffix == NULL)
17514 suffix = "";
17515
17516 if (obs == NULL)
17517 {
17518 char *retval
17519 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
17520
17521 strcpy (retval, lead);
17522 strcat (retval, prefix);
17523 strcat (retval, sep);
17524 strcat (retval, suffix);
17525 return retval;
17526 }
17527 else
17528 {
17529 /* We have an obstack. */
17530 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
17531 }
17532 }
17533
17534 /* Return sibling of die, NULL if no sibling. */
17535
17536 static struct die_info *
17537 sibling_die (struct die_info *die)
17538 {
17539 return die->sibling;
17540 }
17541
17542 /* Get name of a die, return NULL if not found. */
17543
17544 static const char *
17545 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
17546 struct obstack *obstack)
17547 {
17548 if (name && cu->language == language_cplus)
17549 {
17550 char *canon_name = cp_canonicalize_string (name);
17551
17552 if (canon_name != NULL)
17553 {
17554 if (strcmp (canon_name, name) != 0)
17555 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
17556 xfree (canon_name);
17557 }
17558 }
17559
17560 return name;
17561 }
17562
17563 /* Get name of a die, return NULL if not found. */
17564
17565 static const char *
17566 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
17567 {
17568 struct attribute *attr;
17569
17570 attr = dwarf2_attr (die, DW_AT_name, cu);
17571 if ((!attr || !DW_STRING (attr))
17572 && die->tag != DW_TAG_class_type
17573 && die->tag != DW_TAG_interface_type
17574 && die->tag != DW_TAG_structure_type
17575 && die->tag != DW_TAG_union_type)
17576 return NULL;
17577
17578 switch (die->tag)
17579 {
17580 case DW_TAG_compile_unit:
17581 case DW_TAG_partial_unit:
17582 /* Compilation units have a DW_AT_name that is a filename, not
17583 a source language identifier. */
17584 case DW_TAG_enumeration_type:
17585 case DW_TAG_enumerator:
17586 /* These tags always have simple identifiers already; no need
17587 to canonicalize them. */
17588 return DW_STRING (attr);
17589
17590 case DW_TAG_subprogram:
17591 /* Java constructors will all be named "<init>", so return
17592 the class name when we see this special case. */
17593 if (cu->language == language_java
17594 && DW_STRING (attr) != NULL
17595 && strcmp (DW_STRING (attr), "<init>") == 0)
17596 {
17597 struct dwarf2_cu *spec_cu = cu;
17598 struct die_info *spec_die;
17599
17600 /* GCJ will output '<init>' for Java constructor names.
17601 For this special case, return the name of the parent class. */
17602
17603 /* GCJ may output suprogram DIEs with AT_specification set.
17604 If so, use the name of the specified DIE. */
17605 spec_die = die_specification (die, &spec_cu);
17606 if (spec_die != NULL)
17607 return dwarf2_name (spec_die, spec_cu);
17608
17609 do
17610 {
17611 die = die->parent;
17612 if (die->tag == DW_TAG_class_type)
17613 return dwarf2_name (die, cu);
17614 }
17615 while (die->tag != DW_TAG_compile_unit
17616 && die->tag != DW_TAG_partial_unit);
17617 }
17618 break;
17619
17620 case DW_TAG_class_type:
17621 case DW_TAG_interface_type:
17622 case DW_TAG_structure_type:
17623 case DW_TAG_union_type:
17624 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17625 structures or unions. These were of the form "._%d" in GCC 4.1,
17626 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17627 and GCC 4.4. We work around this problem by ignoring these. */
17628 if (attr && DW_STRING (attr)
17629 && (strncmp (DW_STRING (attr), "._", 2) == 0
17630 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
17631 return NULL;
17632
17633 /* GCC might emit a nameless typedef that has a linkage name. See
17634 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17635 if (!attr || DW_STRING (attr) == NULL)
17636 {
17637 char *demangled = NULL;
17638
17639 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17640 if (attr == NULL)
17641 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17642
17643 if (attr == NULL || DW_STRING (attr) == NULL)
17644 return NULL;
17645
17646 /* Avoid demangling DW_STRING (attr) the second time on a second
17647 call for the same DIE. */
17648 if (!DW_STRING_IS_CANONICAL (attr))
17649 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
17650
17651 if (demangled)
17652 {
17653 char *base;
17654
17655 /* FIXME: we already did this for the partial symbol... */
17656 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17657 demangled, strlen (demangled));
17658 DW_STRING_IS_CANONICAL (attr) = 1;
17659 xfree (demangled);
17660
17661 /* Strip any leading namespaces/classes, keep only the base name.
17662 DW_AT_name for named DIEs does not contain the prefixes. */
17663 base = strrchr (DW_STRING (attr), ':');
17664 if (base && base > DW_STRING (attr) && base[-1] == ':')
17665 return &base[1];
17666 else
17667 return DW_STRING (attr);
17668 }
17669 }
17670 break;
17671
17672 default:
17673 break;
17674 }
17675
17676 if (!DW_STRING_IS_CANONICAL (attr))
17677 {
17678 DW_STRING (attr)
17679 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17680 &cu->objfile->objfile_obstack);
17681 DW_STRING_IS_CANONICAL (attr) = 1;
17682 }
17683 return DW_STRING (attr);
17684 }
17685
17686 /* Return the die that this die in an extension of, or NULL if there
17687 is none. *EXT_CU is the CU containing DIE on input, and the CU
17688 containing the return value on output. */
17689
17690 static struct die_info *
17691 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
17692 {
17693 struct attribute *attr;
17694
17695 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
17696 if (attr == NULL)
17697 return NULL;
17698
17699 return follow_die_ref (die, attr, ext_cu);
17700 }
17701
17702 /* Convert a DIE tag into its string name. */
17703
17704 static const char *
17705 dwarf_tag_name (unsigned tag)
17706 {
17707 const char *name = get_DW_TAG_name (tag);
17708
17709 if (name == NULL)
17710 return "DW_TAG_<unknown>";
17711
17712 return name;
17713 }
17714
17715 /* Convert a DWARF attribute code into its string name. */
17716
17717 static const char *
17718 dwarf_attr_name (unsigned attr)
17719 {
17720 const char *name;
17721
17722 #ifdef MIPS /* collides with DW_AT_HP_block_index */
17723 if (attr == DW_AT_MIPS_fde)
17724 return "DW_AT_MIPS_fde";
17725 #else
17726 if (attr == DW_AT_HP_block_index)
17727 return "DW_AT_HP_block_index";
17728 #endif
17729
17730 name = get_DW_AT_name (attr);
17731
17732 if (name == NULL)
17733 return "DW_AT_<unknown>";
17734
17735 return name;
17736 }
17737
17738 /* Convert a DWARF value form code into its string name. */
17739
17740 static const char *
17741 dwarf_form_name (unsigned form)
17742 {
17743 const char *name = get_DW_FORM_name (form);
17744
17745 if (name == NULL)
17746 return "DW_FORM_<unknown>";
17747
17748 return name;
17749 }
17750
17751 static char *
17752 dwarf_bool_name (unsigned mybool)
17753 {
17754 if (mybool)
17755 return "TRUE";
17756 else
17757 return "FALSE";
17758 }
17759
17760 /* Convert a DWARF type code into its string name. */
17761
17762 static const char *
17763 dwarf_type_encoding_name (unsigned enc)
17764 {
17765 const char *name = get_DW_ATE_name (enc);
17766
17767 if (name == NULL)
17768 return "DW_ATE_<unknown>";
17769
17770 return name;
17771 }
17772
17773 static void
17774 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
17775 {
17776 unsigned int i;
17777
17778 print_spaces (indent, f);
17779 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
17780 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
17781
17782 if (die->parent != NULL)
17783 {
17784 print_spaces (indent, f);
17785 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
17786 die->parent->offset.sect_off);
17787 }
17788
17789 print_spaces (indent, f);
17790 fprintf_unfiltered (f, " has children: %s\n",
17791 dwarf_bool_name (die->child != NULL));
17792
17793 print_spaces (indent, f);
17794 fprintf_unfiltered (f, " attributes:\n");
17795
17796 for (i = 0; i < die->num_attrs; ++i)
17797 {
17798 print_spaces (indent, f);
17799 fprintf_unfiltered (f, " %s (%s) ",
17800 dwarf_attr_name (die->attrs[i].name),
17801 dwarf_form_name (die->attrs[i].form));
17802
17803 switch (die->attrs[i].form)
17804 {
17805 case DW_FORM_addr:
17806 case DW_FORM_GNU_addr_index:
17807 fprintf_unfiltered (f, "address: ");
17808 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
17809 break;
17810 case DW_FORM_block2:
17811 case DW_FORM_block4:
17812 case DW_FORM_block:
17813 case DW_FORM_block1:
17814 fprintf_unfiltered (f, "block: size %s",
17815 pulongest (DW_BLOCK (&die->attrs[i])->size));
17816 break;
17817 case DW_FORM_exprloc:
17818 fprintf_unfiltered (f, "expression: size %s",
17819 pulongest (DW_BLOCK (&die->attrs[i])->size));
17820 break;
17821 case DW_FORM_ref_addr:
17822 fprintf_unfiltered (f, "ref address: ");
17823 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17824 break;
17825 case DW_FORM_GNU_ref_alt:
17826 fprintf_unfiltered (f, "alt ref address: ");
17827 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17828 break;
17829 case DW_FORM_ref1:
17830 case DW_FORM_ref2:
17831 case DW_FORM_ref4:
17832 case DW_FORM_ref8:
17833 case DW_FORM_ref_udata:
17834 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
17835 (long) (DW_UNSND (&die->attrs[i])));
17836 break;
17837 case DW_FORM_data1:
17838 case DW_FORM_data2:
17839 case DW_FORM_data4:
17840 case DW_FORM_data8:
17841 case DW_FORM_udata:
17842 case DW_FORM_sdata:
17843 fprintf_unfiltered (f, "constant: %s",
17844 pulongest (DW_UNSND (&die->attrs[i])));
17845 break;
17846 case DW_FORM_sec_offset:
17847 fprintf_unfiltered (f, "section offset: %s",
17848 pulongest (DW_UNSND (&die->attrs[i])));
17849 break;
17850 case DW_FORM_ref_sig8:
17851 fprintf_unfiltered (f, "signature: %s",
17852 hex_string (DW_SIGNATURE (&die->attrs[i])));
17853 break;
17854 case DW_FORM_string:
17855 case DW_FORM_strp:
17856 case DW_FORM_GNU_str_index:
17857 case DW_FORM_GNU_strp_alt:
17858 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
17859 DW_STRING (&die->attrs[i])
17860 ? DW_STRING (&die->attrs[i]) : "",
17861 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
17862 break;
17863 case DW_FORM_flag:
17864 if (DW_UNSND (&die->attrs[i]))
17865 fprintf_unfiltered (f, "flag: TRUE");
17866 else
17867 fprintf_unfiltered (f, "flag: FALSE");
17868 break;
17869 case DW_FORM_flag_present:
17870 fprintf_unfiltered (f, "flag: TRUE");
17871 break;
17872 case DW_FORM_indirect:
17873 /* The reader will have reduced the indirect form to
17874 the "base form" so this form should not occur. */
17875 fprintf_unfiltered (f,
17876 "unexpected attribute form: DW_FORM_indirect");
17877 break;
17878 default:
17879 fprintf_unfiltered (f, "unsupported attribute form: %d.",
17880 die->attrs[i].form);
17881 break;
17882 }
17883 fprintf_unfiltered (f, "\n");
17884 }
17885 }
17886
17887 static void
17888 dump_die_for_error (struct die_info *die)
17889 {
17890 dump_die_shallow (gdb_stderr, 0, die);
17891 }
17892
17893 static void
17894 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17895 {
17896 int indent = level * 4;
17897
17898 gdb_assert (die != NULL);
17899
17900 if (level >= max_level)
17901 return;
17902
17903 dump_die_shallow (f, indent, die);
17904
17905 if (die->child != NULL)
17906 {
17907 print_spaces (indent, f);
17908 fprintf_unfiltered (f, " Children:");
17909 if (level + 1 < max_level)
17910 {
17911 fprintf_unfiltered (f, "\n");
17912 dump_die_1 (f, level + 1, max_level, die->child);
17913 }
17914 else
17915 {
17916 fprintf_unfiltered (f,
17917 " [not printed, max nesting level reached]\n");
17918 }
17919 }
17920
17921 if (die->sibling != NULL && level > 0)
17922 {
17923 dump_die_1 (f, level, max_level, die->sibling);
17924 }
17925 }
17926
17927 /* This is called from the pdie macro in gdbinit.in.
17928 It's not static so gcc will keep a copy callable from gdb. */
17929
17930 void
17931 dump_die (struct die_info *die, int max_level)
17932 {
17933 dump_die_1 (gdb_stdlog, 0, max_level, die);
17934 }
17935
17936 static void
17937 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
17938 {
17939 void **slot;
17940
17941 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17942 INSERT);
17943
17944 *slot = die;
17945 }
17946
17947 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17948 required kind. */
17949
17950 static sect_offset
17951 dwarf2_get_ref_die_offset (const struct attribute *attr)
17952 {
17953 sect_offset retval = { DW_UNSND (attr) };
17954
17955 if (attr_form_is_ref (attr))
17956 return retval;
17957
17958 retval.sect_off = 0;
17959 complaint (&symfile_complaints,
17960 _("unsupported die ref attribute form: '%s'"),
17961 dwarf_form_name (attr->form));
17962 return retval;
17963 }
17964
17965 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17966 * the value held by the attribute is not constant. */
17967
17968 static LONGEST
17969 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
17970 {
17971 if (attr->form == DW_FORM_sdata)
17972 return DW_SND (attr);
17973 else if (attr->form == DW_FORM_udata
17974 || attr->form == DW_FORM_data1
17975 || attr->form == DW_FORM_data2
17976 || attr->form == DW_FORM_data4
17977 || attr->form == DW_FORM_data8)
17978 return DW_UNSND (attr);
17979 else
17980 {
17981 complaint (&symfile_complaints,
17982 _("Attribute value is not a constant (%s)"),
17983 dwarf_form_name (attr->form));
17984 return default_value;
17985 }
17986 }
17987
17988 /* Follow reference or signature attribute ATTR of SRC_DIE.
17989 On entry *REF_CU is the CU of SRC_DIE.
17990 On exit *REF_CU is the CU of the result. */
17991
17992 static struct die_info *
17993 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
17994 struct dwarf2_cu **ref_cu)
17995 {
17996 struct die_info *die;
17997
17998 if (attr_form_is_ref (attr))
17999 die = follow_die_ref (src_die, attr, ref_cu);
18000 else if (attr->form == DW_FORM_ref_sig8)
18001 die = follow_die_sig (src_die, attr, ref_cu);
18002 else
18003 {
18004 dump_die_for_error (src_die);
18005 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
18006 (*ref_cu)->objfile->name);
18007 }
18008
18009 return die;
18010 }
18011
18012 /* Follow reference OFFSET.
18013 On entry *REF_CU is the CU of the source die referencing OFFSET.
18014 On exit *REF_CU is the CU of the result.
18015 Returns NULL if OFFSET is invalid. */
18016
18017 static struct die_info *
18018 follow_die_offset (sect_offset offset, int offset_in_dwz,
18019 struct dwarf2_cu **ref_cu)
18020 {
18021 struct die_info temp_die;
18022 struct dwarf2_cu *target_cu, *cu = *ref_cu;
18023
18024 gdb_assert (cu->per_cu != NULL);
18025
18026 target_cu = cu;
18027
18028 if (cu->per_cu->is_debug_types)
18029 {
18030 /* .debug_types CUs cannot reference anything outside their CU.
18031 If they need to, they have to reference a signatured type via
18032 DW_FORM_ref_sig8. */
18033 if (! offset_in_cu_p (&cu->header, offset))
18034 return NULL;
18035 }
18036 else if (offset_in_dwz != cu->per_cu->is_dwz
18037 || ! offset_in_cu_p (&cu->header, offset))
18038 {
18039 struct dwarf2_per_cu_data *per_cu;
18040
18041 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
18042 cu->objfile);
18043
18044 /* If necessary, add it to the queue and load its DIEs. */
18045 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
18046 load_full_comp_unit (per_cu, cu->language);
18047
18048 target_cu = per_cu->cu;
18049 }
18050 else if (cu->dies == NULL)
18051 {
18052 /* We're loading full DIEs during partial symbol reading. */
18053 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
18054 load_full_comp_unit (cu->per_cu, language_minimal);
18055 }
18056
18057 *ref_cu = target_cu;
18058 temp_die.offset = offset;
18059 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
18060 }
18061
18062 /* Follow reference attribute ATTR of SRC_DIE.
18063 On entry *REF_CU is the CU of SRC_DIE.
18064 On exit *REF_CU is the CU of the result. */
18065
18066 static struct die_info *
18067 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
18068 struct dwarf2_cu **ref_cu)
18069 {
18070 sect_offset offset = dwarf2_get_ref_die_offset (attr);
18071 struct dwarf2_cu *cu = *ref_cu;
18072 struct die_info *die;
18073
18074 die = follow_die_offset (offset,
18075 (attr->form == DW_FORM_GNU_ref_alt
18076 || cu->per_cu->is_dwz),
18077 ref_cu);
18078 if (!die)
18079 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
18080 "at 0x%x [in module %s]"),
18081 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
18082
18083 return die;
18084 }
18085
18086 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
18087 Returned value is intended for DW_OP_call*. Returned
18088 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
18089
18090 struct dwarf2_locexpr_baton
18091 dwarf2_fetch_die_loc_sect_off (sect_offset offset,
18092 struct dwarf2_per_cu_data *per_cu,
18093 CORE_ADDR (*get_frame_pc) (void *baton),
18094 void *baton)
18095 {
18096 struct dwarf2_cu *cu;
18097 struct die_info *die;
18098 struct attribute *attr;
18099 struct dwarf2_locexpr_baton retval;
18100
18101 dw2_setup (per_cu->objfile);
18102
18103 if (per_cu->cu == NULL)
18104 load_cu (per_cu);
18105 cu = per_cu->cu;
18106
18107 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
18108 if (!die)
18109 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
18110 offset.sect_off, per_cu->objfile->name);
18111
18112 attr = dwarf2_attr (die, DW_AT_location, cu);
18113 if (!attr)
18114 {
18115 /* DWARF: "If there is no such attribute, then there is no effect.".
18116 DATA is ignored if SIZE is 0. */
18117
18118 retval.data = NULL;
18119 retval.size = 0;
18120 }
18121 else if (attr_form_is_section_offset (attr))
18122 {
18123 struct dwarf2_loclist_baton loclist_baton;
18124 CORE_ADDR pc = (*get_frame_pc) (baton);
18125 size_t size;
18126
18127 fill_in_loclist_baton (cu, &loclist_baton, attr);
18128
18129 retval.data = dwarf2_find_location_expression (&loclist_baton,
18130 &size, pc);
18131 retval.size = size;
18132 }
18133 else
18134 {
18135 if (!attr_form_is_block (attr))
18136 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
18137 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
18138 offset.sect_off, per_cu->objfile->name);
18139
18140 retval.data = DW_BLOCK (attr)->data;
18141 retval.size = DW_BLOCK (attr)->size;
18142 }
18143 retval.per_cu = cu->per_cu;
18144
18145 age_cached_comp_units ();
18146
18147 return retval;
18148 }
18149
18150 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
18151 offset. */
18152
18153 struct dwarf2_locexpr_baton
18154 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
18155 struct dwarf2_per_cu_data *per_cu,
18156 CORE_ADDR (*get_frame_pc) (void *baton),
18157 void *baton)
18158 {
18159 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
18160
18161 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
18162 }
18163
18164 /* Write a constant of a given type as target-ordered bytes into
18165 OBSTACK. */
18166
18167 static const gdb_byte *
18168 write_constant_as_bytes (struct obstack *obstack,
18169 enum bfd_endian byte_order,
18170 struct type *type,
18171 ULONGEST value,
18172 LONGEST *len)
18173 {
18174 gdb_byte *result;
18175
18176 *len = TYPE_LENGTH (type);
18177 result = obstack_alloc (obstack, *len);
18178 store_unsigned_integer (result, *len, byte_order, value);
18179
18180 return result;
18181 }
18182
18183 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
18184 pointer to the constant bytes and set LEN to the length of the
18185 data. If memory is needed, allocate it on OBSTACK. If the DIE
18186 does not have a DW_AT_const_value, return NULL. */
18187
18188 const gdb_byte *
18189 dwarf2_fetch_constant_bytes (sect_offset offset,
18190 struct dwarf2_per_cu_data *per_cu,
18191 struct obstack *obstack,
18192 LONGEST *len)
18193 {
18194 struct dwarf2_cu *cu;
18195 struct die_info *die;
18196 struct attribute *attr;
18197 const gdb_byte *result = NULL;
18198 struct type *type;
18199 LONGEST value;
18200 enum bfd_endian byte_order;
18201
18202 dw2_setup (per_cu->objfile);
18203
18204 if (per_cu->cu == NULL)
18205 load_cu (per_cu);
18206 cu = per_cu->cu;
18207
18208 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
18209 if (!die)
18210 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
18211 offset.sect_off, per_cu->objfile->name);
18212
18213
18214 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18215 if (attr == NULL)
18216 return NULL;
18217
18218 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
18219 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
18220
18221 switch (attr->form)
18222 {
18223 case DW_FORM_addr:
18224 case DW_FORM_GNU_addr_index:
18225 {
18226 gdb_byte *tem;
18227
18228 *len = cu->header.addr_size;
18229 tem = obstack_alloc (obstack, *len);
18230 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
18231 result = tem;
18232 }
18233 break;
18234 case DW_FORM_string:
18235 case DW_FORM_strp:
18236 case DW_FORM_GNU_str_index:
18237 case DW_FORM_GNU_strp_alt:
18238 /* DW_STRING is already allocated on the objfile obstack, point
18239 directly to it. */
18240 result = (const gdb_byte *) DW_STRING (attr);
18241 *len = strlen (DW_STRING (attr));
18242 break;
18243 case DW_FORM_block1:
18244 case DW_FORM_block2:
18245 case DW_FORM_block4:
18246 case DW_FORM_block:
18247 case DW_FORM_exprloc:
18248 result = DW_BLOCK (attr)->data;
18249 *len = DW_BLOCK (attr)->size;
18250 break;
18251
18252 /* The DW_AT_const_value attributes are supposed to carry the
18253 symbol's value "represented as it would be on the target
18254 architecture." By the time we get here, it's already been
18255 converted to host endianness, so we just need to sign- or
18256 zero-extend it as appropriate. */
18257 case DW_FORM_data1:
18258 type = die_type (die, cu);
18259 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
18260 if (result == NULL)
18261 result = write_constant_as_bytes (obstack, byte_order,
18262 type, value, len);
18263 break;
18264 case DW_FORM_data2:
18265 type = die_type (die, cu);
18266 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
18267 if (result == NULL)
18268 result = write_constant_as_bytes (obstack, byte_order,
18269 type, value, len);
18270 break;
18271 case DW_FORM_data4:
18272 type = die_type (die, cu);
18273 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
18274 if (result == NULL)
18275 result = write_constant_as_bytes (obstack, byte_order,
18276 type, value, len);
18277 break;
18278 case DW_FORM_data8:
18279 type = die_type (die, cu);
18280 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
18281 if (result == NULL)
18282 result = write_constant_as_bytes (obstack, byte_order,
18283 type, value, len);
18284 break;
18285
18286 case DW_FORM_sdata:
18287 type = die_type (die, cu);
18288 result = write_constant_as_bytes (obstack, byte_order,
18289 type, DW_SND (attr), len);
18290 break;
18291
18292 case DW_FORM_udata:
18293 type = die_type (die, cu);
18294 result = write_constant_as_bytes (obstack, byte_order,
18295 type, DW_UNSND (attr), len);
18296 break;
18297
18298 default:
18299 complaint (&symfile_complaints,
18300 _("unsupported const value attribute form: '%s'"),
18301 dwarf_form_name (attr->form));
18302 break;
18303 }
18304
18305 return result;
18306 }
18307
18308 /* Return the type of the DIE at DIE_OFFSET in the CU named by
18309 PER_CU. */
18310
18311 struct type *
18312 dwarf2_get_die_type (cu_offset die_offset,
18313 struct dwarf2_per_cu_data *per_cu)
18314 {
18315 sect_offset die_offset_sect;
18316
18317 dw2_setup (per_cu->objfile);
18318
18319 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
18320 return get_die_type_at_offset (die_offset_sect, per_cu);
18321 }
18322
18323 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
18324 On entry *REF_CU is the CU of SRC_DIE.
18325 On exit *REF_CU is the CU of the result.
18326 Returns NULL if the referenced DIE isn't found. */
18327
18328 static struct die_info *
18329 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
18330 struct dwarf2_cu **ref_cu)
18331 {
18332 struct objfile *objfile = (*ref_cu)->objfile;
18333 struct die_info temp_die;
18334 struct dwarf2_cu *sig_cu;
18335 struct die_info *die;
18336
18337 /* While it might be nice to assert sig_type->type == NULL here,
18338 we can get here for DW_AT_imported_declaration where we need
18339 the DIE not the type. */
18340
18341 /* If necessary, add it to the queue and load its DIEs. */
18342
18343 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
18344 read_signatured_type (sig_type);
18345
18346 gdb_assert (sig_type->per_cu.cu != NULL);
18347
18348 sig_cu = sig_type->per_cu.cu;
18349 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
18350 temp_die.offset = sig_type->type_offset_in_section;
18351 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
18352 temp_die.offset.sect_off);
18353 if (die)
18354 {
18355 /* For .gdb_index version 7 keep track of included TUs.
18356 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
18357 if (dwarf2_per_objfile->index_table != NULL
18358 && dwarf2_per_objfile->index_table->version <= 7)
18359 {
18360 VEC_safe_push (dwarf2_per_cu_ptr,
18361 (*ref_cu)->per_cu->imported_symtabs,
18362 sig_cu->per_cu);
18363 }
18364
18365 *ref_cu = sig_cu;
18366 return die;
18367 }
18368
18369 return NULL;
18370 }
18371
18372 /* Follow signatured type referenced by ATTR in SRC_DIE.
18373 On entry *REF_CU is the CU of SRC_DIE.
18374 On exit *REF_CU is the CU of the result.
18375 The result is the DIE of the type.
18376 If the referenced type cannot be found an error is thrown. */
18377
18378 static struct die_info *
18379 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
18380 struct dwarf2_cu **ref_cu)
18381 {
18382 ULONGEST signature = DW_SIGNATURE (attr);
18383 struct signatured_type *sig_type;
18384 struct die_info *die;
18385
18386 gdb_assert (attr->form == DW_FORM_ref_sig8);
18387
18388 sig_type = lookup_signatured_type (*ref_cu, signature);
18389 /* sig_type will be NULL if the signatured type is missing from
18390 the debug info. */
18391 if (sig_type == NULL)
18392 {
18393 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
18394 " from DIE at 0x%x [in module %s]"),
18395 hex_string (signature), src_die->offset.sect_off,
18396 (*ref_cu)->objfile->name);
18397 }
18398
18399 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
18400 if (die == NULL)
18401 {
18402 dump_die_for_error (src_die);
18403 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
18404 " from DIE at 0x%x [in module %s]"),
18405 hex_string (signature), src_die->offset.sect_off,
18406 (*ref_cu)->objfile->name);
18407 }
18408
18409 return die;
18410 }
18411
18412 /* Get the type specified by SIGNATURE referenced in DIE/CU,
18413 reading in and processing the type unit if necessary. */
18414
18415 static struct type *
18416 get_signatured_type (struct die_info *die, ULONGEST signature,
18417 struct dwarf2_cu *cu)
18418 {
18419 struct signatured_type *sig_type;
18420 struct dwarf2_cu *type_cu;
18421 struct die_info *type_die;
18422 struct type *type;
18423
18424 sig_type = lookup_signatured_type (cu, signature);
18425 /* sig_type will be NULL if the signatured type is missing from
18426 the debug info. */
18427 if (sig_type == NULL)
18428 {
18429 complaint (&symfile_complaints,
18430 _("Dwarf Error: Cannot find signatured DIE %s referenced"
18431 " from DIE at 0x%x [in module %s]"),
18432 hex_string (signature), die->offset.sect_off,
18433 dwarf2_per_objfile->objfile->name);
18434 return build_error_marker_type (cu, die);
18435 }
18436
18437 /* If we already know the type we're done. */
18438 if (sig_type->type != NULL)
18439 return sig_type->type;
18440
18441 type_cu = cu;
18442 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
18443 if (type_die != NULL)
18444 {
18445 /* N.B. We need to call get_die_type to ensure only one type for this DIE
18446 is created. This is important, for example, because for c++ classes
18447 we need TYPE_NAME set which is only done by new_symbol. Blech. */
18448 type = read_type_die (type_die, type_cu);
18449 if (type == NULL)
18450 {
18451 complaint (&symfile_complaints,
18452 _("Dwarf Error: Cannot build signatured type %s"
18453 " referenced from DIE at 0x%x [in module %s]"),
18454 hex_string (signature), die->offset.sect_off,
18455 dwarf2_per_objfile->objfile->name);
18456 type = build_error_marker_type (cu, die);
18457 }
18458 }
18459 else
18460 {
18461 complaint (&symfile_complaints,
18462 _("Dwarf Error: Problem reading signatured DIE %s referenced"
18463 " from DIE at 0x%x [in module %s]"),
18464 hex_string (signature), die->offset.sect_off,
18465 dwarf2_per_objfile->objfile->name);
18466 type = build_error_marker_type (cu, die);
18467 }
18468 sig_type->type = type;
18469
18470 return type;
18471 }
18472
18473 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
18474 reading in and processing the type unit if necessary. */
18475
18476 static struct type *
18477 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
18478 struct dwarf2_cu *cu) /* ARI: editCase function */
18479 {
18480 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
18481 if (attr_form_is_ref (attr))
18482 {
18483 struct dwarf2_cu *type_cu = cu;
18484 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
18485
18486 return read_type_die (type_die, type_cu);
18487 }
18488 else if (attr->form == DW_FORM_ref_sig8)
18489 {
18490 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
18491 }
18492 else
18493 {
18494 complaint (&symfile_complaints,
18495 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
18496 " at 0x%x [in module %s]"),
18497 dwarf_form_name (attr->form), die->offset.sect_off,
18498 dwarf2_per_objfile->objfile->name);
18499 return build_error_marker_type (cu, die);
18500 }
18501 }
18502
18503 /* Load the DIEs associated with type unit PER_CU into memory. */
18504
18505 static void
18506 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
18507 {
18508 struct signatured_type *sig_type;
18509
18510 /* Caller is responsible for ensuring type_unit_groups don't get here. */
18511 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
18512
18513 /* We have the per_cu, but we need the signatured_type.
18514 Fortunately this is an easy translation. */
18515 gdb_assert (per_cu->is_debug_types);
18516 sig_type = (struct signatured_type *) per_cu;
18517
18518 gdb_assert (per_cu->cu == NULL);
18519
18520 read_signatured_type (sig_type);
18521
18522 gdb_assert (per_cu->cu != NULL);
18523 }
18524
18525 /* die_reader_func for read_signatured_type.
18526 This is identical to load_full_comp_unit_reader,
18527 but is kept separate for now. */
18528
18529 static void
18530 read_signatured_type_reader (const struct die_reader_specs *reader,
18531 const gdb_byte *info_ptr,
18532 struct die_info *comp_unit_die,
18533 int has_children,
18534 void *data)
18535 {
18536 struct dwarf2_cu *cu = reader->cu;
18537
18538 gdb_assert (cu->die_hash == NULL);
18539 cu->die_hash =
18540 htab_create_alloc_ex (cu->header.length / 12,
18541 die_hash,
18542 die_eq,
18543 NULL,
18544 &cu->comp_unit_obstack,
18545 hashtab_obstack_allocate,
18546 dummy_obstack_deallocate);
18547
18548 if (has_children)
18549 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
18550 &info_ptr, comp_unit_die);
18551 cu->dies = comp_unit_die;
18552 /* comp_unit_die is not stored in die_hash, no need. */
18553
18554 /* We try not to read any attributes in this function, because not
18555 all CUs needed for references have been loaded yet, and symbol
18556 table processing isn't initialized. But we have to set the CU language,
18557 or we won't be able to build types correctly.
18558 Similarly, if we do not read the producer, we can not apply
18559 producer-specific interpretation. */
18560 prepare_one_comp_unit (cu, cu->dies, language_minimal);
18561 }
18562
18563 /* Read in a signatured type and build its CU and DIEs.
18564 If the type is a stub for the real type in a DWO file,
18565 read in the real type from the DWO file as well. */
18566
18567 static void
18568 read_signatured_type (struct signatured_type *sig_type)
18569 {
18570 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
18571
18572 gdb_assert (per_cu->is_debug_types);
18573 gdb_assert (per_cu->cu == NULL);
18574
18575 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
18576 read_signatured_type_reader, NULL);
18577 sig_type->per_cu.tu_read = 1;
18578 }
18579
18580 /* Decode simple location descriptions.
18581 Given a pointer to a dwarf block that defines a location, compute
18582 the location and return the value.
18583
18584 NOTE drow/2003-11-18: This function is called in two situations
18585 now: for the address of static or global variables (partial symbols
18586 only) and for offsets into structures which are expected to be
18587 (more or less) constant. The partial symbol case should go away,
18588 and only the constant case should remain. That will let this
18589 function complain more accurately. A few special modes are allowed
18590 without complaint for global variables (for instance, global
18591 register values and thread-local values).
18592
18593 A location description containing no operations indicates that the
18594 object is optimized out. The return value is 0 for that case.
18595 FIXME drow/2003-11-16: No callers check for this case any more; soon all
18596 callers will only want a very basic result and this can become a
18597 complaint.
18598
18599 Note that stack[0] is unused except as a default error return. */
18600
18601 static CORE_ADDR
18602 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
18603 {
18604 struct objfile *objfile = cu->objfile;
18605 size_t i;
18606 size_t size = blk->size;
18607 const gdb_byte *data = blk->data;
18608 CORE_ADDR stack[64];
18609 int stacki;
18610 unsigned int bytes_read, unsnd;
18611 gdb_byte op;
18612
18613 i = 0;
18614 stacki = 0;
18615 stack[stacki] = 0;
18616 stack[++stacki] = 0;
18617
18618 while (i < size)
18619 {
18620 op = data[i++];
18621 switch (op)
18622 {
18623 case DW_OP_lit0:
18624 case DW_OP_lit1:
18625 case DW_OP_lit2:
18626 case DW_OP_lit3:
18627 case DW_OP_lit4:
18628 case DW_OP_lit5:
18629 case DW_OP_lit6:
18630 case DW_OP_lit7:
18631 case DW_OP_lit8:
18632 case DW_OP_lit9:
18633 case DW_OP_lit10:
18634 case DW_OP_lit11:
18635 case DW_OP_lit12:
18636 case DW_OP_lit13:
18637 case DW_OP_lit14:
18638 case DW_OP_lit15:
18639 case DW_OP_lit16:
18640 case DW_OP_lit17:
18641 case DW_OP_lit18:
18642 case DW_OP_lit19:
18643 case DW_OP_lit20:
18644 case DW_OP_lit21:
18645 case DW_OP_lit22:
18646 case DW_OP_lit23:
18647 case DW_OP_lit24:
18648 case DW_OP_lit25:
18649 case DW_OP_lit26:
18650 case DW_OP_lit27:
18651 case DW_OP_lit28:
18652 case DW_OP_lit29:
18653 case DW_OP_lit30:
18654 case DW_OP_lit31:
18655 stack[++stacki] = op - DW_OP_lit0;
18656 break;
18657
18658 case DW_OP_reg0:
18659 case DW_OP_reg1:
18660 case DW_OP_reg2:
18661 case DW_OP_reg3:
18662 case DW_OP_reg4:
18663 case DW_OP_reg5:
18664 case DW_OP_reg6:
18665 case DW_OP_reg7:
18666 case DW_OP_reg8:
18667 case DW_OP_reg9:
18668 case DW_OP_reg10:
18669 case DW_OP_reg11:
18670 case DW_OP_reg12:
18671 case DW_OP_reg13:
18672 case DW_OP_reg14:
18673 case DW_OP_reg15:
18674 case DW_OP_reg16:
18675 case DW_OP_reg17:
18676 case DW_OP_reg18:
18677 case DW_OP_reg19:
18678 case DW_OP_reg20:
18679 case DW_OP_reg21:
18680 case DW_OP_reg22:
18681 case DW_OP_reg23:
18682 case DW_OP_reg24:
18683 case DW_OP_reg25:
18684 case DW_OP_reg26:
18685 case DW_OP_reg27:
18686 case DW_OP_reg28:
18687 case DW_OP_reg29:
18688 case DW_OP_reg30:
18689 case DW_OP_reg31:
18690 stack[++stacki] = op - DW_OP_reg0;
18691 if (i < size)
18692 dwarf2_complex_location_expr_complaint ();
18693 break;
18694
18695 case DW_OP_regx:
18696 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
18697 i += bytes_read;
18698 stack[++stacki] = unsnd;
18699 if (i < size)
18700 dwarf2_complex_location_expr_complaint ();
18701 break;
18702
18703 case DW_OP_addr:
18704 stack[++stacki] = read_address (objfile->obfd, &data[i],
18705 cu, &bytes_read);
18706 i += bytes_read;
18707 break;
18708
18709 case DW_OP_const1u:
18710 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
18711 i += 1;
18712 break;
18713
18714 case DW_OP_const1s:
18715 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
18716 i += 1;
18717 break;
18718
18719 case DW_OP_const2u:
18720 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
18721 i += 2;
18722 break;
18723
18724 case DW_OP_const2s:
18725 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
18726 i += 2;
18727 break;
18728
18729 case DW_OP_const4u:
18730 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
18731 i += 4;
18732 break;
18733
18734 case DW_OP_const4s:
18735 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
18736 i += 4;
18737 break;
18738
18739 case DW_OP_const8u:
18740 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
18741 i += 8;
18742 break;
18743
18744 case DW_OP_constu:
18745 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
18746 &bytes_read);
18747 i += bytes_read;
18748 break;
18749
18750 case DW_OP_consts:
18751 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
18752 i += bytes_read;
18753 break;
18754
18755 case DW_OP_dup:
18756 stack[stacki + 1] = stack[stacki];
18757 stacki++;
18758 break;
18759
18760 case DW_OP_plus:
18761 stack[stacki - 1] += stack[stacki];
18762 stacki--;
18763 break;
18764
18765 case DW_OP_plus_uconst:
18766 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
18767 &bytes_read);
18768 i += bytes_read;
18769 break;
18770
18771 case DW_OP_minus:
18772 stack[stacki - 1] -= stack[stacki];
18773 stacki--;
18774 break;
18775
18776 case DW_OP_deref:
18777 /* If we're not the last op, then we definitely can't encode
18778 this using GDB's address_class enum. This is valid for partial
18779 global symbols, although the variable's address will be bogus
18780 in the psymtab. */
18781 if (i < size)
18782 dwarf2_complex_location_expr_complaint ();
18783 break;
18784
18785 case DW_OP_GNU_push_tls_address:
18786 /* The top of the stack has the offset from the beginning
18787 of the thread control block at which the variable is located. */
18788 /* Nothing should follow this operator, so the top of stack would
18789 be returned. */
18790 /* This is valid for partial global symbols, but the variable's
18791 address will be bogus in the psymtab. Make it always at least
18792 non-zero to not look as a variable garbage collected by linker
18793 which have DW_OP_addr 0. */
18794 if (i < size)
18795 dwarf2_complex_location_expr_complaint ();
18796 stack[stacki]++;
18797 break;
18798
18799 case DW_OP_GNU_uninit:
18800 break;
18801
18802 case DW_OP_GNU_addr_index:
18803 case DW_OP_GNU_const_index:
18804 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18805 &bytes_read);
18806 i += bytes_read;
18807 break;
18808
18809 default:
18810 {
18811 const char *name = get_DW_OP_name (op);
18812
18813 if (name)
18814 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18815 name);
18816 else
18817 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18818 op);
18819 }
18820
18821 return (stack[stacki]);
18822 }
18823
18824 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18825 outside of the allocated space. Also enforce minimum>0. */
18826 if (stacki >= ARRAY_SIZE (stack) - 1)
18827 {
18828 complaint (&symfile_complaints,
18829 _("location description stack overflow"));
18830 return 0;
18831 }
18832
18833 if (stacki <= 0)
18834 {
18835 complaint (&symfile_complaints,
18836 _("location description stack underflow"));
18837 return 0;
18838 }
18839 }
18840 return (stack[stacki]);
18841 }
18842
18843 /* memory allocation interface */
18844
18845 static struct dwarf_block *
18846 dwarf_alloc_block (struct dwarf2_cu *cu)
18847 {
18848 struct dwarf_block *blk;
18849
18850 blk = (struct dwarf_block *)
18851 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
18852 return (blk);
18853 }
18854
18855 static struct die_info *
18856 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
18857 {
18858 struct die_info *die;
18859 size_t size = sizeof (struct die_info);
18860
18861 if (num_attrs > 1)
18862 size += (num_attrs - 1) * sizeof (struct attribute);
18863
18864 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
18865 memset (die, 0, sizeof (struct die_info));
18866 return (die);
18867 }
18868
18869 \f
18870 /* Macro support. */
18871
18872 /* Return file name relative to the compilation directory of file number I in
18873 *LH's file name table. The result is allocated using xmalloc; the caller is
18874 responsible for freeing it. */
18875
18876 static char *
18877 file_file_name (int file, struct line_header *lh)
18878 {
18879 /* Is the file number a valid index into the line header's file name
18880 table? Remember that file numbers start with one, not zero. */
18881 if (1 <= file && file <= lh->num_file_names)
18882 {
18883 struct file_entry *fe = &lh->file_names[file - 1];
18884
18885 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
18886 return xstrdup (fe->name);
18887 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18888 fe->name, NULL);
18889 }
18890 else
18891 {
18892 /* The compiler produced a bogus file number. We can at least
18893 record the macro definitions made in the file, even if we
18894 won't be able to find the file by name. */
18895 char fake_name[80];
18896
18897 xsnprintf (fake_name, sizeof (fake_name),
18898 "<bad macro file number %d>", file);
18899
18900 complaint (&symfile_complaints,
18901 _("bad file number in macro information (%d)"),
18902 file);
18903
18904 return xstrdup (fake_name);
18905 }
18906 }
18907
18908 /* Return the full name of file number I in *LH's file name table.
18909 Use COMP_DIR as the name of the current directory of the
18910 compilation. The result is allocated using xmalloc; the caller is
18911 responsible for freeing it. */
18912 static char *
18913 file_full_name (int file, struct line_header *lh, const char *comp_dir)
18914 {
18915 /* Is the file number a valid index into the line header's file name
18916 table? Remember that file numbers start with one, not zero. */
18917 if (1 <= file && file <= lh->num_file_names)
18918 {
18919 char *relative = file_file_name (file, lh);
18920
18921 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18922 return relative;
18923 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18924 }
18925 else
18926 return file_file_name (file, lh);
18927 }
18928
18929
18930 static struct macro_source_file *
18931 macro_start_file (int file, int line,
18932 struct macro_source_file *current_file,
18933 const char *comp_dir,
18934 struct line_header *lh, struct objfile *objfile)
18935 {
18936 /* File name relative to the compilation directory of this source file. */
18937 char *file_name = file_file_name (file, lh);
18938
18939 /* We don't create a macro table for this compilation unit
18940 at all until we actually get a filename. */
18941 if (! pending_macros)
18942 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
18943 objfile->per_bfd->macro_cache,
18944 comp_dir);
18945
18946 if (! current_file)
18947 {
18948 /* If we have no current file, then this must be the start_file
18949 directive for the compilation unit's main source file. */
18950 current_file = macro_set_main (pending_macros, file_name);
18951 macro_define_special (pending_macros);
18952 }
18953 else
18954 current_file = macro_include (current_file, line, file_name);
18955
18956 xfree (file_name);
18957
18958 return current_file;
18959 }
18960
18961
18962 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18963 followed by a null byte. */
18964 static char *
18965 copy_string (const char *buf, int len)
18966 {
18967 char *s = xmalloc (len + 1);
18968
18969 memcpy (s, buf, len);
18970 s[len] = '\0';
18971 return s;
18972 }
18973
18974
18975 static const char *
18976 consume_improper_spaces (const char *p, const char *body)
18977 {
18978 if (*p == ' ')
18979 {
18980 complaint (&symfile_complaints,
18981 _("macro definition contains spaces "
18982 "in formal argument list:\n`%s'"),
18983 body);
18984
18985 while (*p == ' ')
18986 p++;
18987 }
18988
18989 return p;
18990 }
18991
18992
18993 static void
18994 parse_macro_definition (struct macro_source_file *file, int line,
18995 const char *body)
18996 {
18997 const char *p;
18998
18999 /* The body string takes one of two forms. For object-like macro
19000 definitions, it should be:
19001
19002 <macro name> " " <definition>
19003
19004 For function-like macro definitions, it should be:
19005
19006 <macro name> "() " <definition>
19007 or
19008 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
19009
19010 Spaces may appear only where explicitly indicated, and in the
19011 <definition>.
19012
19013 The Dwarf 2 spec says that an object-like macro's name is always
19014 followed by a space, but versions of GCC around March 2002 omit
19015 the space when the macro's definition is the empty string.
19016
19017 The Dwarf 2 spec says that there should be no spaces between the
19018 formal arguments in a function-like macro's formal argument list,
19019 but versions of GCC around March 2002 include spaces after the
19020 commas. */
19021
19022
19023 /* Find the extent of the macro name. The macro name is terminated
19024 by either a space or null character (for an object-like macro) or
19025 an opening paren (for a function-like macro). */
19026 for (p = body; *p; p++)
19027 if (*p == ' ' || *p == '(')
19028 break;
19029
19030 if (*p == ' ' || *p == '\0')
19031 {
19032 /* It's an object-like macro. */
19033 int name_len = p - body;
19034 char *name = copy_string (body, name_len);
19035 const char *replacement;
19036
19037 if (*p == ' ')
19038 replacement = body + name_len + 1;
19039 else
19040 {
19041 dwarf2_macro_malformed_definition_complaint (body);
19042 replacement = body + name_len;
19043 }
19044
19045 macro_define_object (file, line, name, replacement);
19046
19047 xfree (name);
19048 }
19049 else if (*p == '(')
19050 {
19051 /* It's a function-like macro. */
19052 char *name = copy_string (body, p - body);
19053 int argc = 0;
19054 int argv_size = 1;
19055 char **argv = xmalloc (argv_size * sizeof (*argv));
19056
19057 p++;
19058
19059 p = consume_improper_spaces (p, body);
19060
19061 /* Parse the formal argument list. */
19062 while (*p && *p != ')')
19063 {
19064 /* Find the extent of the current argument name. */
19065 const char *arg_start = p;
19066
19067 while (*p && *p != ',' && *p != ')' && *p != ' ')
19068 p++;
19069
19070 if (! *p || p == arg_start)
19071 dwarf2_macro_malformed_definition_complaint (body);
19072 else
19073 {
19074 /* Make sure argv has room for the new argument. */
19075 if (argc >= argv_size)
19076 {
19077 argv_size *= 2;
19078 argv = xrealloc (argv, argv_size * sizeof (*argv));
19079 }
19080
19081 argv[argc++] = copy_string (arg_start, p - arg_start);
19082 }
19083
19084 p = consume_improper_spaces (p, body);
19085
19086 /* Consume the comma, if present. */
19087 if (*p == ',')
19088 {
19089 p++;
19090
19091 p = consume_improper_spaces (p, body);
19092 }
19093 }
19094
19095 if (*p == ')')
19096 {
19097 p++;
19098
19099 if (*p == ' ')
19100 /* Perfectly formed definition, no complaints. */
19101 macro_define_function (file, line, name,
19102 argc, (const char **) argv,
19103 p + 1);
19104 else if (*p == '\0')
19105 {
19106 /* Complain, but do define it. */
19107 dwarf2_macro_malformed_definition_complaint (body);
19108 macro_define_function (file, line, name,
19109 argc, (const char **) argv,
19110 p);
19111 }
19112 else
19113 /* Just complain. */
19114 dwarf2_macro_malformed_definition_complaint (body);
19115 }
19116 else
19117 /* Just complain. */
19118 dwarf2_macro_malformed_definition_complaint (body);
19119
19120 xfree (name);
19121 {
19122 int i;
19123
19124 for (i = 0; i < argc; i++)
19125 xfree (argv[i]);
19126 }
19127 xfree (argv);
19128 }
19129 else
19130 dwarf2_macro_malformed_definition_complaint (body);
19131 }
19132
19133 /* Skip some bytes from BYTES according to the form given in FORM.
19134 Returns the new pointer. */
19135
19136 static const gdb_byte *
19137 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
19138 enum dwarf_form form,
19139 unsigned int offset_size,
19140 struct dwarf2_section_info *section)
19141 {
19142 unsigned int bytes_read;
19143
19144 switch (form)
19145 {
19146 case DW_FORM_data1:
19147 case DW_FORM_flag:
19148 ++bytes;
19149 break;
19150
19151 case DW_FORM_data2:
19152 bytes += 2;
19153 break;
19154
19155 case DW_FORM_data4:
19156 bytes += 4;
19157 break;
19158
19159 case DW_FORM_data8:
19160 bytes += 8;
19161 break;
19162
19163 case DW_FORM_string:
19164 read_direct_string (abfd, bytes, &bytes_read);
19165 bytes += bytes_read;
19166 break;
19167
19168 case DW_FORM_sec_offset:
19169 case DW_FORM_strp:
19170 case DW_FORM_GNU_strp_alt:
19171 bytes += offset_size;
19172 break;
19173
19174 case DW_FORM_block:
19175 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
19176 bytes += bytes_read;
19177 break;
19178
19179 case DW_FORM_block1:
19180 bytes += 1 + read_1_byte (abfd, bytes);
19181 break;
19182 case DW_FORM_block2:
19183 bytes += 2 + read_2_bytes (abfd, bytes);
19184 break;
19185 case DW_FORM_block4:
19186 bytes += 4 + read_4_bytes (abfd, bytes);
19187 break;
19188
19189 case DW_FORM_sdata:
19190 case DW_FORM_udata:
19191 case DW_FORM_GNU_addr_index:
19192 case DW_FORM_GNU_str_index:
19193 bytes = gdb_skip_leb128 (bytes, buffer_end);
19194 if (bytes == NULL)
19195 {
19196 dwarf2_section_buffer_overflow_complaint (section);
19197 return NULL;
19198 }
19199 break;
19200
19201 default:
19202 {
19203 complain:
19204 complaint (&symfile_complaints,
19205 _("invalid form 0x%x in `%s'"),
19206 form,
19207 section->asection->name);
19208 return NULL;
19209 }
19210 }
19211
19212 return bytes;
19213 }
19214
19215 /* A helper for dwarf_decode_macros that handles skipping an unknown
19216 opcode. Returns an updated pointer to the macro data buffer; or,
19217 on error, issues a complaint and returns NULL. */
19218
19219 static const gdb_byte *
19220 skip_unknown_opcode (unsigned int opcode,
19221 const gdb_byte **opcode_definitions,
19222 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
19223 bfd *abfd,
19224 unsigned int offset_size,
19225 struct dwarf2_section_info *section)
19226 {
19227 unsigned int bytes_read, i;
19228 unsigned long arg;
19229 const gdb_byte *defn;
19230
19231 if (opcode_definitions[opcode] == NULL)
19232 {
19233 complaint (&symfile_complaints,
19234 _("unrecognized DW_MACFINO opcode 0x%x"),
19235 opcode);
19236 return NULL;
19237 }
19238
19239 defn = opcode_definitions[opcode];
19240 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
19241 defn += bytes_read;
19242
19243 for (i = 0; i < arg; ++i)
19244 {
19245 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
19246 section);
19247 if (mac_ptr == NULL)
19248 {
19249 /* skip_form_bytes already issued the complaint. */
19250 return NULL;
19251 }
19252 }
19253
19254 return mac_ptr;
19255 }
19256
19257 /* A helper function which parses the header of a macro section.
19258 If the macro section is the extended (for now called "GNU") type,
19259 then this updates *OFFSET_SIZE. Returns a pointer to just after
19260 the header, or issues a complaint and returns NULL on error. */
19261
19262 static const gdb_byte *
19263 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
19264 bfd *abfd,
19265 const gdb_byte *mac_ptr,
19266 unsigned int *offset_size,
19267 int section_is_gnu)
19268 {
19269 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
19270
19271 if (section_is_gnu)
19272 {
19273 unsigned int version, flags;
19274
19275 version = read_2_bytes (abfd, mac_ptr);
19276 if (version != 4)
19277 {
19278 complaint (&symfile_complaints,
19279 _("unrecognized version `%d' in .debug_macro section"),
19280 version);
19281 return NULL;
19282 }
19283 mac_ptr += 2;
19284
19285 flags = read_1_byte (abfd, mac_ptr);
19286 ++mac_ptr;
19287 *offset_size = (flags & 1) ? 8 : 4;
19288
19289 if ((flags & 2) != 0)
19290 /* We don't need the line table offset. */
19291 mac_ptr += *offset_size;
19292
19293 /* Vendor opcode descriptions. */
19294 if ((flags & 4) != 0)
19295 {
19296 unsigned int i, count;
19297
19298 count = read_1_byte (abfd, mac_ptr);
19299 ++mac_ptr;
19300 for (i = 0; i < count; ++i)
19301 {
19302 unsigned int opcode, bytes_read;
19303 unsigned long arg;
19304
19305 opcode = read_1_byte (abfd, mac_ptr);
19306 ++mac_ptr;
19307 opcode_definitions[opcode] = mac_ptr;
19308 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19309 mac_ptr += bytes_read;
19310 mac_ptr += arg;
19311 }
19312 }
19313 }
19314
19315 return mac_ptr;
19316 }
19317
19318 /* A helper for dwarf_decode_macros that handles the GNU extensions,
19319 including DW_MACRO_GNU_transparent_include. */
19320
19321 static void
19322 dwarf_decode_macro_bytes (bfd *abfd,
19323 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
19324 struct macro_source_file *current_file,
19325 struct line_header *lh, const char *comp_dir,
19326 struct dwarf2_section_info *section,
19327 int section_is_gnu, int section_is_dwz,
19328 unsigned int offset_size,
19329 struct objfile *objfile,
19330 htab_t include_hash)
19331 {
19332 enum dwarf_macro_record_type macinfo_type;
19333 int at_commandline;
19334 const gdb_byte *opcode_definitions[256];
19335
19336 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19337 &offset_size, section_is_gnu);
19338 if (mac_ptr == NULL)
19339 {
19340 /* We already issued a complaint. */
19341 return;
19342 }
19343
19344 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
19345 GDB is still reading the definitions from command line. First
19346 DW_MACINFO_start_file will need to be ignored as it was already executed
19347 to create CURRENT_FILE for the main source holding also the command line
19348 definitions. On first met DW_MACINFO_start_file this flag is reset to
19349 normally execute all the remaining DW_MACINFO_start_file macinfos. */
19350
19351 at_commandline = 1;
19352
19353 do
19354 {
19355 /* Do we at least have room for a macinfo type byte? */
19356 if (mac_ptr >= mac_end)
19357 {
19358 dwarf2_section_buffer_overflow_complaint (section);
19359 break;
19360 }
19361
19362 macinfo_type = read_1_byte (abfd, mac_ptr);
19363 mac_ptr++;
19364
19365 /* Note that we rely on the fact that the corresponding GNU and
19366 DWARF constants are the same. */
19367 switch (macinfo_type)
19368 {
19369 /* A zero macinfo type indicates the end of the macro
19370 information. */
19371 case 0:
19372 break;
19373
19374 case DW_MACRO_GNU_define:
19375 case DW_MACRO_GNU_undef:
19376 case DW_MACRO_GNU_define_indirect:
19377 case DW_MACRO_GNU_undef_indirect:
19378 case DW_MACRO_GNU_define_indirect_alt:
19379 case DW_MACRO_GNU_undef_indirect_alt:
19380 {
19381 unsigned int bytes_read;
19382 int line;
19383 const char *body;
19384 int is_define;
19385
19386 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19387 mac_ptr += bytes_read;
19388
19389 if (macinfo_type == DW_MACRO_GNU_define
19390 || macinfo_type == DW_MACRO_GNU_undef)
19391 {
19392 body = read_direct_string (abfd, mac_ptr, &bytes_read);
19393 mac_ptr += bytes_read;
19394 }
19395 else
19396 {
19397 LONGEST str_offset;
19398
19399 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
19400 mac_ptr += offset_size;
19401
19402 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
19403 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
19404 || section_is_dwz)
19405 {
19406 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19407
19408 body = read_indirect_string_from_dwz (dwz, str_offset);
19409 }
19410 else
19411 body = read_indirect_string_at_offset (abfd, str_offset);
19412 }
19413
19414 is_define = (macinfo_type == DW_MACRO_GNU_define
19415 || macinfo_type == DW_MACRO_GNU_define_indirect
19416 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
19417 if (! current_file)
19418 {
19419 /* DWARF violation as no main source is present. */
19420 complaint (&symfile_complaints,
19421 _("debug info with no main source gives macro %s "
19422 "on line %d: %s"),
19423 is_define ? _("definition") : _("undefinition"),
19424 line, body);
19425 break;
19426 }
19427 if ((line == 0 && !at_commandline)
19428 || (line != 0 && at_commandline))
19429 complaint (&symfile_complaints,
19430 _("debug info gives %s macro %s with %s line %d: %s"),
19431 at_commandline ? _("command-line") : _("in-file"),
19432 is_define ? _("definition") : _("undefinition"),
19433 line == 0 ? _("zero") : _("non-zero"), line, body);
19434
19435 if (is_define)
19436 parse_macro_definition (current_file, line, body);
19437 else
19438 {
19439 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
19440 || macinfo_type == DW_MACRO_GNU_undef_indirect
19441 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
19442 macro_undef (current_file, line, body);
19443 }
19444 }
19445 break;
19446
19447 case DW_MACRO_GNU_start_file:
19448 {
19449 unsigned int bytes_read;
19450 int line, file;
19451
19452 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19453 mac_ptr += bytes_read;
19454 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19455 mac_ptr += bytes_read;
19456
19457 if ((line == 0 && !at_commandline)
19458 || (line != 0 && at_commandline))
19459 complaint (&symfile_complaints,
19460 _("debug info gives source %d included "
19461 "from %s at %s line %d"),
19462 file, at_commandline ? _("command-line") : _("file"),
19463 line == 0 ? _("zero") : _("non-zero"), line);
19464
19465 if (at_commandline)
19466 {
19467 /* This DW_MACRO_GNU_start_file was executed in the
19468 pass one. */
19469 at_commandline = 0;
19470 }
19471 else
19472 current_file = macro_start_file (file, line,
19473 current_file, comp_dir,
19474 lh, objfile);
19475 }
19476 break;
19477
19478 case DW_MACRO_GNU_end_file:
19479 if (! current_file)
19480 complaint (&symfile_complaints,
19481 _("macro debug info has an unmatched "
19482 "`close_file' directive"));
19483 else
19484 {
19485 current_file = current_file->included_by;
19486 if (! current_file)
19487 {
19488 enum dwarf_macro_record_type next_type;
19489
19490 /* GCC circa March 2002 doesn't produce the zero
19491 type byte marking the end of the compilation
19492 unit. Complain if it's not there, but exit no
19493 matter what. */
19494
19495 /* Do we at least have room for a macinfo type byte? */
19496 if (mac_ptr >= mac_end)
19497 {
19498 dwarf2_section_buffer_overflow_complaint (section);
19499 return;
19500 }
19501
19502 /* We don't increment mac_ptr here, so this is just
19503 a look-ahead. */
19504 next_type = read_1_byte (abfd, mac_ptr);
19505 if (next_type != 0)
19506 complaint (&symfile_complaints,
19507 _("no terminating 0-type entry for "
19508 "macros in `.debug_macinfo' section"));
19509
19510 return;
19511 }
19512 }
19513 break;
19514
19515 case DW_MACRO_GNU_transparent_include:
19516 case DW_MACRO_GNU_transparent_include_alt:
19517 {
19518 LONGEST offset;
19519 void **slot;
19520 bfd *include_bfd = abfd;
19521 struct dwarf2_section_info *include_section = section;
19522 struct dwarf2_section_info alt_section;
19523 const gdb_byte *include_mac_end = mac_end;
19524 int is_dwz = section_is_dwz;
19525 const gdb_byte *new_mac_ptr;
19526
19527 offset = read_offset_1 (abfd, mac_ptr, offset_size);
19528 mac_ptr += offset_size;
19529
19530 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
19531 {
19532 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19533
19534 dwarf2_read_section (dwarf2_per_objfile->objfile,
19535 &dwz->macro);
19536
19537 include_bfd = dwz->macro.asection->owner;
19538 include_section = &dwz->macro;
19539 include_mac_end = dwz->macro.buffer + dwz->macro.size;
19540 is_dwz = 1;
19541 }
19542
19543 new_mac_ptr = include_section->buffer + offset;
19544 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
19545
19546 if (*slot != NULL)
19547 {
19548 /* This has actually happened; see
19549 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
19550 complaint (&symfile_complaints,
19551 _("recursive DW_MACRO_GNU_transparent_include in "
19552 ".debug_macro section"));
19553 }
19554 else
19555 {
19556 *slot = (void *) new_mac_ptr;
19557
19558 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
19559 include_mac_end, current_file,
19560 lh, comp_dir,
19561 section, section_is_gnu, is_dwz,
19562 offset_size, objfile, include_hash);
19563
19564 htab_remove_elt (include_hash, (void *) new_mac_ptr);
19565 }
19566 }
19567 break;
19568
19569 case DW_MACINFO_vendor_ext:
19570 if (!section_is_gnu)
19571 {
19572 unsigned int bytes_read;
19573 int constant;
19574
19575 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19576 mac_ptr += bytes_read;
19577 read_direct_string (abfd, mac_ptr, &bytes_read);
19578 mac_ptr += bytes_read;
19579
19580 /* We don't recognize any vendor extensions. */
19581 break;
19582 }
19583 /* FALLTHROUGH */
19584
19585 default:
19586 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
19587 mac_ptr, mac_end, abfd, offset_size,
19588 section);
19589 if (mac_ptr == NULL)
19590 return;
19591 break;
19592 }
19593 } while (macinfo_type != 0);
19594 }
19595
19596 static void
19597 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
19598 const char *comp_dir, int section_is_gnu)
19599 {
19600 struct objfile *objfile = dwarf2_per_objfile->objfile;
19601 struct line_header *lh = cu->line_header;
19602 bfd *abfd;
19603 const gdb_byte *mac_ptr, *mac_end;
19604 struct macro_source_file *current_file = 0;
19605 enum dwarf_macro_record_type macinfo_type;
19606 unsigned int offset_size = cu->header.offset_size;
19607 const gdb_byte *opcode_definitions[256];
19608 struct cleanup *cleanup;
19609 htab_t include_hash;
19610 void **slot;
19611 struct dwarf2_section_info *section;
19612 const char *section_name;
19613
19614 if (cu->dwo_unit != NULL)
19615 {
19616 if (section_is_gnu)
19617 {
19618 section = &cu->dwo_unit->dwo_file->sections.macro;
19619 section_name = ".debug_macro.dwo";
19620 }
19621 else
19622 {
19623 section = &cu->dwo_unit->dwo_file->sections.macinfo;
19624 section_name = ".debug_macinfo.dwo";
19625 }
19626 }
19627 else
19628 {
19629 if (section_is_gnu)
19630 {
19631 section = &dwarf2_per_objfile->macro;
19632 section_name = ".debug_macro";
19633 }
19634 else
19635 {
19636 section = &dwarf2_per_objfile->macinfo;
19637 section_name = ".debug_macinfo";
19638 }
19639 }
19640
19641 dwarf2_read_section (objfile, section);
19642 if (section->buffer == NULL)
19643 {
19644 complaint (&symfile_complaints, _("missing %s section"), section_name);
19645 return;
19646 }
19647 abfd = section->asection->owner;
19648
19649 /* First pass: Find the name of the base filename.
19650 This filename is needed in order to process all macros whose definition
19651 (or undefinition) comes from the command line. These macros are defined
19652 before the first DW_MACINFO_start_file entry, and yet still need to be
19653 associated to the base file.
19654
19655 To determine the base file name, we scan the macro definitions until we
19656 reach the first DW_MACINFO_start_file entry. We then initialize
19657 CURRENT_FILE accordingly so that any macro definition found before the
19658 first DW_MACINFO_start_file can still be associated to the base file. */
19659
19660 mac_ptr = section->buffer + offset;
19661 mac_end = section->buffer + section->size;
19662
19663 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19664 &offset_size, section_is_gnu);
19665 if (mac_ptr == NULL)
19666 {
19667 /* We already issued a complaint. */
19668 return;
19669 }
19670
19671 do
19672 {
19673 /* Do we at least have room for a macinfo type byte? */
19674 if (mac_ptr >= mac_end)
19675 {
19676 /* Complaint is printed during the second pass as GDB will probably
19677 stop the first pass earlier upon finding
19678 DW_MACINFO_start_file. */
19679 break;
19680 }
19681
19682 macinfo_type = read_1_byte (abfd, mac_ptr);
19683 mac_ptr++;
19684
19685 /* Note that we rely on the fact that the corresponding GNU and
19686 DWARF constants are the same. */
19687 switch (macinfo_type)
19688 {
19689 /* A zero macinfo type indicates the end of the macro
19690 information. */
19691 case 0:
19692 break;
19693
19694 case DW_MACRO_GNU_define:
19695 case DW_MACRO_GNU_undef:
19696 /* Only skip the data by MAC_PTR. */
19697 {
19698 unsigned int bytes_read;
19699
19700 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19701 mac_ptr += bytes_read;
19702 read_direct_string (abfd, mac_ptr, &bytes_read);
19703 mac_ptr += bytes_read;
19704 }
19705 break;
19706
19707 case DW_MACRO_GNU_start_file:
19708 {
19709 unsigned int bytes_read;
19710 int line, file;
19711
19712 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19713 mac_ptr += bytes_read;
19714 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19715 mac_ptr += bytes_read;
19716
19717 current_file = macro_start_file (file, line, current_file,
19718 comp_dir, lh, objfile);
19719 }
19720 break;
19721
19722 case DW_MACRO_GNU_end_file:
19723 /* No data to skip by MAC_PTR. */
19724 break;
19725
19726 case DW_MACRO_GNU_define_indirect:
19727 case DW_MACRO_GNU_undef_indirect:
19728 case DW_MACRO_GNU_define_indirect_alt:
19729 case DW_MACRO_GNU_undef_indirect_alt:
19730 {
19731 unsigned int bytes_read;
19732
19733 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19734 mac_ptr += bytes_read;
19735 mac_ptr += offset_size;
19736 }
19737 break;
19738
19739 case DW_MACRO_GNU_transparent_include:
19740 case DW_MACRO_GNU_transparent_include_alt:
19741 /* Note that, according to the spec, a transparent include
19742 chain cannot call DW_MACRO_GNU_start_file. So, we can just
19743 skip this opcode. */
19744 mac_ptr += offset_size;
19745 break;
19746
19747 case DW_MACINFO_vendor_ext:
19748 /* Only skip the data by MAC_PTR. */
19749 if (!section_is_gnu)
19750 {
19751 unsigned int bytes_read;
19752
19753 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19754 mac_ptr += bytes_read;
19755 read_direct_string (abfd, mac_ptr, &bytes_read);
19756 mac_ptr += bytes_read;
19757 }
19758 /* FALLTHROUGH */
19759
19760 default:
19761 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
19762 mac_ptr, mac_end, abfd, offset_size,
19763 section);
19764 if (mac_ptr == NULL)
19765 return;
19766 break;
19767 }
19768 } while (macinfo_type != 0 && current_file == NULL);
19769
19770 /* Second pass: Process all entries.
19771
19772 Use the AT_COMMAND_LINE flag to determine whether we are still processing
19773 command-line macro definitions/undefinitions. This flag is unset when we
19774 reach the first DW_MACINFO_start_file entry. */
19775
19776 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
19777 NULL, xcalloc, xfree);
19778 cleanup = make_cleanup_htab_delete (include_hash);
19779 mac_ptr = section->buffer + offset;
19780 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
19781 *slot = (void *) mac_ptr;
19782 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
19783 current_file, lh, comp_dir, section,
19784 section_is_gnu, 0,
19785 offset_size, objfile, include_hash);
19786 do_cleanups (cleanup);
19787 }
19788
19789 /* Check if the attribute's form is a DW_FORM_block*
19790 if so return true else false. */
19791
19792 static int
19793 attr_form_is_block (const struct attribute *attr)
19794 {
19795 return (attr == NULL ? 0 :
19796 attr->form == DW_FORM_block1
19797 || attr->form == DW_FORM_block2
19798 || attr->form == DW_FORM_block4
19799 || attr->form == DW_FORM_block
19800 || attr->form == DW_FORM_exprloc);
19801 }
19802
19803 /* Return non-zero if ATTR's value is a section offset --- classes
19804 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19805 You may use DW_UNSND (attr) to retrieve such offsets.
19806
19807 Section 7.5.4, "Attribute Encodings", explains that no attribute
19808 may have a value that belongs to more than one of these classes; it
19809 would be ambiguous if we did, because we use the same forms for all
19810 of them. */
19811
19812 static int
19813 attr_form_is_section_offset (const struct attribute *attr)
19814 {
19815 return (attr->form == DW_FORM_data4
19816 || attr->form == DW_FORM_data8
19817 || attr->form == DW_FORM_sec_offset);
19818 }
19819
19820 /* Return non-zero if ATTR's value falls in the 'constant' class, or
19821 zero otherwise. When this function returns true, you can apply
19822 dwarf2_get_attr_constant_value to it.
19823
19824 However, note that for some attributes you must check
19825 attr_form_is_section_offset before using this test. DW_FORM_data4
19826 and DW_FORM_data8 are members of both the constant class, and of
19827 the classes that contain offsets into other debug sections
19828 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19829 that, if an attribute's can be either a constant or one of the
19830 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19831 taken as section offsets, not constants. */
19832
19833 static int
19834 attr_form_is_constant (const struct attribute *attr)
19835 {
19836 switch (attr->form)
19837 {
19838 case DW_FORM_sdata:
19839 case DW_FORM_udata:
19840 case DW_FORM_data1:
19841 case DW_FORM_data2:
19842 case DW_FORM_data4:
19843 case DW_FORM_data8:
19844 return 1;
19845 default:
19846 return 0;
19847 }
19848 }
19849
19850
19851 /* DW_ADDR is always stored already as sect_offset; despite for the forms
19852 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
19853
19854 static int
19855 attr_form_is_ref (const struct attribute *attr)
19856 {
19857 switch (attr->form)
19858 {
19859 case DW_FORM_ref_addr:
19860 case DW_FORM_ref1:
19861 case DW_FORM_ref2:
19862 case DW_FORM_ref4:
19863 case DW_FORM_ref8:
19864 case DW_FORM_ref_udata:
19865 case DW_FORM_GNU_ref_alt:
19866 return 1;
19867 default:
19868 return 0;
19869 }
19870 }
19871
19872 /* Return the .debug_loc section to use for CU.
19873 For DWO files use .debug_loc.dwo. */
19874
19875 static struct dwarf2_section_info *
19876 cu_debug_loc_section (struct dwarf2_cu *cu)
19877 {
19878 if (cu->dwo_unit)
19879 return &cu->dwo_unit->dwo_file->sections.loc;
19880 return &dwarf2_per_objfile->loc;
19881 }
19882
19883 /* A helper function that fills in a dwarf2_loclist_baton. */
19884
19885 static void
19886 fill_in_loclist_baton (struct dwarf2_cu *cu,
19887 struct dwarf2_loclist_baton *baton,
19888 const struct attribute *attr)
19889 {
19890 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19891
19892 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
19893
19894 baton->per_cu = cu->per_cu;
19895 gdb_assert (baton->per_cu);
19896 /* We don't know how long the location list is, but make sure we
19897 don't run off the edge of the section. */
19898 baton->size = section->size - DW_UNSND (attr);
19899 baton->data = section->buffer + DW_UNSND (attr);
19900 baton->base_address = cu->base_address;
19901 baton->from_dwo = cu->dwo_unit != NULL;
19902 }
19903
19904 static void
19905 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
19906 struct dwarf2_cu *cu, int is_block)
19907 {
19908 struct objfile *objfile = dwarf2_per_objfile->objfile;
19909 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19910
19911 if (attr_form_is_section_offset (attr)
19912 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
19913 the section. If so, fall through to the complaint in the
19914 other branch. */
19915 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
19916 {
19917 struct dwarf2_loclist_baton *baton;
19918
19919 baton = obstack_alloc (&objfile->objfile_obstack,
19920 sizeof (struct dwarf2_loclist_baton));
19921
19922 fill_in_loclist_baton (cu, baton, attr);
19923
19924 if (cu->base_known == 0)
19925 complaint (&symfile_complaints,
19926 _("Location list used without "
19927 "specifying the CU base address."));
19928
19929 SYMBOL_ACLASS_INDEX (sym) = (is_block
19930 ? dwarf2_loclist_block_index
19931 : dwarf2_loclist_index);
19932 SYMBOL_LOCATION_BATON (sym) = baton;
19933 }
19934 else
19935 {
19936 struct dwarf2_locexpr_baton *baton;
19937
19938 baton = obstack_alloc (&objfile->objfile_obstack,
19939 sizeof (struct dwarf2_locexpr_baton));
19940 baton->per_cu = cu->per_cu;
19941 gdb_assert (baton->per_cu);
19942
19943 if (attr_form_is_block (attr))
19944 {
19945 /* Note that we're just copying the block's data pointer
19946 here, not the actual data. We're still pointing into the
19947 info_buffer for SYM's objfile; right now we never release
19948 that buffer, but when we do clean up properly this may
19949 need to change. */
19950 baton->size = DW_BLOCK (attr)->size;
19951 baton->data = DW_BLOCK (attr)->data;
19952 }
19953 else
19954 {
19955 dwarf2_invalid_attrib_class_complaint ("location description",
19956 SYMBOL_NATURAL_NAME (sym));
19957 baton->size = 0;
19958 }
19959
19960 SYMBOL_ACLASS_INDEX (sym) = (is_block
19961 ? dwarf2_locexpr_block_index
19962 : dwarf2_locexpr_index);
19963 SYMBOL_LOCATION_BATON (sym) = baton;
19964 }
19965 }
19966
19967 /* Return the OBJFILE associated with the compilation unit CU. If CU
19968 came from a separate debuginfo file, then the master objfile is
19969 returned. */
19970
19971 struct objfile *
19972 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19973 {
19974 struct objfile *objfile = per_cu->objfile;
19975
19976 /* Return the master objfile, so that we can report and look up the
19977 correct file containing this variable. */
19978 if (objfile->separate_debug_objfile_backlink)
19979 objfile = objfile->separate_debug_objfile_backlink;
19980
19981 return objfile;
19982 }
19983
19984 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19985 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19986 CU_HEADERP first. */
19987
19988 static const struct comp_unit_head *
19989 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19990 struct dwarf2_per_cu_data *per_cu)
19991 {
19992 const gdb_byte *info_ptr;
19993
19994 if (per_cu->cu)
19995 return &per_cu->cu->header;
19996
19997 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
19998
19999 memset (cu_headerp, 0, sizeof (*cu_headerp));
20000 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
20001
20002 return cu_headerp;
20003 }
20004
20005 /* Return the address size given in the compilation unit header for CU. */
20006
20007 int
20008 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
20009 {
20010 struct comp_unit_head cu_header_local;
20011 const struct comp_unit_head *cu_headerp;
20012
20013 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20014
20015 return cu_headerp->addr_size;
20016 }
20017
20018 /* Return the offset size given in the compilation unit header for CU. */
20019
20020 int
20021 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
20022 {
20023 struct comp_unit_head cu_header_local;
20024 const struct comp_unit_head *cu_headerp;
20025
20026 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20027
20028 return cu_headerp->offset_size;
20029 }
20030
20031 /* See its dwarf2loc.h declaration. */
20032
20033 int
20034 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
20035 {
20036 struct comp_unit_head cu_header_local;
20037 const struct comp_unit_head *cu_headerp;
20038
20039 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20040
20041 if (cu_headerp->version == 2)
20042 return cu_headerp->addr_size;
20043 else
20044 return cu_headerp->offset_size;
20045 }
20046
20047 /* Return the text offset of the CU. The returned offset comes from
20048 this CU's objfile. If this objfile came from a separate debuginfo
20049 file, then the offset may be different from the corresponding
20050 offset in the parent objfile. */
20051
20052 CORE_ADDR
20053 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
20054 {
20055 struct objfile *objfile = per_cu->objfile;
20056
20057 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20058 }
20059
20060 /* Locate the .debug_info compilation unit from CU's objfile which contains
20061 the DIE at OFFSET. Raises an error on failure. */
20062
20063 static struct dwarf2_per_cu_data *
20064 dwarf2_find_containing_comp_unit (sect_offset offset,
20065 unsigned int offset_in_dwz,
20066 struct objfile *objfile)
20067 {
20068 struct dwarf2_per_cu_data *this_cu;
20069 int low, high;
20070 const sect_offset *cu_off;
20071
20072 low = 0;
20073 high = dwarf2_per_objfile->n_comp_units - 1;
20074 while (high > low)
20075 {
20076 struct dwarf2_per_cu_data *mid_cu;
20077 int mid = low + (high - low) / 2;
20078
20079 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
20080 cu_off = &mid_cu->offset;
20081 if (mid_cu->is_dwz > offset_in_dwz
20082 || (mid_cu->is_dwz == offset_in_dwz
20083 && cu_off->sect_off >= offset.sect_off))
20084 high = mid;
20085 else
20086 low = mid + 1;
20087 }
20088 gdb_assert (low == high);
20089 this_cu = dwarf2_per_objfile->all_comp_units[low];
20090 cu_off = &this_cu->offset;
20091 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
20092 {
20093 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
20094 error (_("Dwarf Error: could not find partial DIE containing "
20095 "offset 0x%lx [in module %s]"),
20096 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
20097
20098 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
20099 <= offset.sect_off);
20100 return dwarf2_per_objfile->all_comp_units[low-1];
20101 }
20102 else
20103 {
20104 this_cu = dwarf2_per_objfile->all_comp_units[low];
20105 if (low == dwarf2_per_objfile->n_comp_units - 1
20106 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
20107 error (_("invalid dwarf2 offset %u"), offset.sect_off);
20108 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
20109 return this_cu;
20110 }
20111 }
20112
20113 /* Initialize dwarf2_cu CU, owned by PER_CU. */
20114
20115 static void
20116 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
20117 {
20118 memset (cu, 0, sizeof (*cu));
20119 per_cu->cu = cu;
20120 cu->per_cu = per_cu;
20121 cu->objfile = per_cu->objfile;
20122 obstack_init (&cu->comp_unit_obstack);
20123 }
20124
20125 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
20126
20127 static void
20128 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
20129 enum language pretend_language)
20130 {
20131 struct attribute *attr;
20132
20133 /* Set the language we're debugging. */
20134 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
20135 if (attr)
20136 set_cu_language (DW_UNSND (attr), cu);
20137 else
20138 {
20139 cu->language = pretend_language;
20140 cu->language_defn = language_def (cu->language);
20141 }
20142
20143 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
20144 if (attr)
20145 cu->producer = DW_STRING (attr);
20146 }
20147
20148 /* Release one cached compilation unit, CU. We unlink it from the tree
20149 of compilation units, but we don't remove it from the read_in_chain;
20150 the caller is responsible for that.
20151 NOTE: DATA is a void * because this function is also used as a
20152 cleanup routine. */
20153
20154 static void
20155 free_heap_comp_unit (void *data)
20156 {
20157 struct dwarf2_cu *cu = data;
20158
20159 gdb_assert (cu->per_cu != NULL);
20160 cu->per_cu->cu = NULL;
20161 cu->per_cu = NULL;
20162
20163 obstack_free (&cu->comp_unit_obstack, NULL);
20164
20165 xfree (cu);
20166 }
20167
20168 /* This cleanup function is passed the address of a dwarf2_cu on the stack
20169 when we're finished with it. We can't free the pointer itself, but be
20170 sure to unlink it from the cache. Also release any associated storage. */
20171
20172 static void
20173 free_stack_comp_unit (void *data)
20174 {
20175 struct dwarf2_cu *cu = data;
20176
20177 gdb_assert (cu->per_cu != NULL);
20178 cu->per_cu->cu = NULL;
20179 cu->per_cu = NULL;
20180
20181 obstack_free (&cu->comp_unit_obstack, NULL);
20182 cu->partial_dies = NULL;
20183 }
20184
20185 /* Free all cached compilation units. */
20186
20187 static void
20188 free_cached_comp_units (void *data)
20189 {
20190 struct dwarf2_per_cu_data *per_cu, **last_chain;
20191
20192 per_cu = dwarf2_per_objfile->read_in_chain;
20193 last_chain = &dwarf2_per_objfile->read_in_chain;
20194 while (per_cu != NULL)
20195 {
20196 struct dwarf2_per_cu_data *next_cu;
20197
20198 next_cu = per_cu->cu->read_in_chain;
20199
20200 free_heap_comp_unit (per_cu->cu);
20201 *last_chain = next_cu;
20202
20203 per_cu = next_cu;
20204 }
20205 }
20206
20207 /* Increase the age counter on each cached compilation unit, and free
20208 any that are too old. */
20209
20210 static void
20211 age_cached_comp_units (void)
20212 {
20213 struct dwarf2_per_cu_data *per_cu, **last_chain;
20214
20215 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
20216 per_cu = dwarf2_per_objfile->read_in_chain;
20217 while (per_cu != NULL)
20218 {
20219 per_cu->cu->last_used ++;
20220 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
20221 dwarf2_mark (per_cu->cu);
20222 per_cu = per_cu->cu->read_in_chain;
20223 }
20224
20225 per_cu = dwarf2_per_objfile->read_in_chain;
20226 last_chain = &dwarf2_per_objfile->read_in_chain;
20227 while (per_cu != NULL)
20228 {
20229 struct dwarf2_per_cu_data *next_cu;
20230
20231 next_cu = per_cu->cu->read_in_chain;
20232
20233 if (!per_cu->cu->mark)
20234 {
20235 free_heap_comp_unit (per_cu->cu);
20236 *last_chain = next_cu;
20237 }
20238 else
20239 last_chain = &per_cu->cu->read_in_chain;
20240
20241 per_cu = next_cu;
20242 }
20243 }
20244
20245 /* Remove a single compilation unit from the cache. */
20246
20247 static void
20248 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
20249 {
20250 struct dwarf2_per_cu_data *per_cu, **last_chain;
20251
20252 per_cu = dwarf2_per_objfile->read_in_chain;
20253 last_chain = &dwarf2_per_objfile->read_in_chain;
20254 while (per_cu != NULL)
20255 {
20256 struct dwarf2_per_cu_data *next_cu;
20257
20258 next_cu = per_cu->cu->read_in_chain;
20259
20260 if (per_cu == target_per_cu)
20261 {
20262 free_heap_comp_unit (per_cu->cu);
20263 per_cu->cu = NULL;
20264 *last_chain = next_cu;
20265 break;
20266 }
20267 else
20268 last_chain = &per_cu->cu->read_in_chain;
20269
20270 per_cu = next_cu;
20271 }
20272 }
20273
20274 /* Release all extra memory associated with OBJFILE. */
20275
20276 void
20277 dwarf2_free_objfile (struct objfile *objfile)
20278 {
20279 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20280
20281 if (dwarf2_per_objfile == NULL)
20282 return;
20283
20284 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
20285 free_cached_comp_units (NULL);
20286
20287 if (dwarf2_per_objfile->quick_file_names_table)
20288 htab_delete (dwarf2_per_objfile->quick_file_names_table);
20289
20290 /* Everything else should be on the objfile obstack. */
20291 }
20292
20293 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
20294 We store these in a hash table separate from the DIEs, and preserve them
20295 when the DIEs are flushed out of cache.
20296
20297 The CU "per_cu" pointer is needed because offset alone is not enough to
20298 uniquely identify the type. A file may have multiple .debug_types sections,
20299 or the type may come from a DWO file. Furthermore, while it's more logical
20300 to use per_cu->section+offset, with Fission the section with the data is in
20301 the DWO file but we don't know that section at the point we need it.
20302 We have to use something in dwarf2_per_cu_data (or the pointer to it)
20303 because we can enter the lookup routine, get_die_type_at_offset, from
20304 outside this file, and thus won't necessarily have PER_CU->cu.
20305 Fortunately, PER_CU is stable for the life of the objfile. */
20306
20307 struct dwarf2_per_cu_offset_and_type
20308 {
20309 const struct dwarf2_per_cu_data *per_cu;
20310 sect_offset offset;
20311 struct type *type;
20312 };
20313
20314 /* Hash function for a dwarf2_per_cu_offset_and_type. */
20315
20316 static hashval_t
20317 per_cu_offset_and_type_hash (const void *item)
20318 {
20319 const struct dwarf2_per_cu_offset_and_type *ofs = item;
20320
20321 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
20322 }
20323
20324 /* Equality function for a dwarf2_per_cu_offset_and_type. */
20325
20326 static int
20327 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
20328 {
20329 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
20330 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
20331
20332 return (ofs_lhs->per_cu == ofs_rhs->per_cu
20333 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
20334 }
20335
20336 /* Set the type associated with DIE to TYPE. Save it in CU's hash
20337 table if necessary. For convenience, return TYPE.
20338
20339 The DIEs reading must have careful ordering to:
20340 * Not cause infite loops trying to read in DIEs as a prerequisite for
20341 reading current DIE.
20342 * Not trying to dereference contents of still incompletely read in types
20343 while reading in other DIEs.
20344 * Enable referencing still incompletely read in types just by a pointer to
20345 the type without accessing its fields.
20346
20347 Therefore caller should follow these rules:
20348 * Try to fetch any prerequisite types we may need to build this DIE type
20349 before building the type and calling set_die_type.
20350 * After building type call set_die_type for current DIE as soon as
20351 possible before fetching more types to complete the current type.
20352 * Make the type as complete as possible before fetching more types. */
20353
20354 static struct type *
20355 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
20356 {
20357 struct dwarf2_per_cu_offset_and_type **slot, ofs;
20358 struct objfile *objfile = cu->objfile;
20359
20360 /* For Ada types, make sure that the gnat-specific data is always
20361 initialized (if not already set). There are a few types where
20362 we should not be doing so, because the type-specific area is
20363 already used to hold some other piece of info (eg: TYPE_CODE_FLT
20364 where the type-specific area is used to store the floatformat).
20365 But this is not a problem, because the gnat-specific information
20366 is actually not needed for these types. */
20367 if (need_gnat_info (cu)
20368 && TYPE_CODE (type) != TYPE_CODE_FUNC
20369 && TYPE_CODE (type) != TYPE_CODE_FLT
20370 && !HAVE_GNAT_AUX_INFO (type))
20371 INIT_GNAT_SPECIFIC (type);
20372
20373 if (dwarf2_per_objfile->die_type_hash == NULL)
20374 {
20375 dwarf2_per_objfile->die_type_hash =
20376 htab_create_alloc_ex (127,
20377 per_cu_offset_and_type_hash,
20378 per_cu_offset_and_type_eq,
20379 NULL,
20380 &objfile->objfile_obstack,
20381 hashtab_obstack_allocate,
20382 dummy_obstack_deallocate);
20383 }
20384
20385 ofs.per_cu = cu->per_cu;
20386 ofs.offset = die->offset;
20387 ofs.type = type;
20388 slot = (struct dwarf2_per_cu_offset_and_type **)
20389 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
20390 if (*slot)
20391 complaint (&symfile_complaints,
20392 _("A problem internal to GDB: DIE 0x%x has type already set"),
20393 die->offset.sect_off);
20394 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
20395 **slot = ofs;
20396 return type;
20397 }
20398
20399 /* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
20400 or return NULL if the die does not have a saved type. */
20401
20402 static struct type *
20403 get_die_type_at_offset (sect_offset offset,
20404 struct dwarf2_per_cu_data *per_cu)
20405 {
20406 struct dwarf2_per_cu_offset_and_type *slot, ofs;
20407
20408 if (dwarf2_per_objfile->die_type_hash == NULL)
20409 return NULL;
20410
20411 ofs.per_cu = per_cu;
20412 ofs.offset = offset;
20413 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
20414 if (slot)
20415 return slot->type;
20416 else
20417 return NULL;
20418 }
20419
20420 /* Look up the type for DIE in CU in die_type_hash,
20421 or return NULL if DIE does not have a saved type. */
20422
20423 static struct type *
20424 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
20425 {
20426 return get_die_type_at_offset (die->offset, cu->per_cu);
20427 }
20428
20429 /* Add a dependence relationship from CU to REF_PER_CU. */
20430
20431 static void
20432 dwarf2_add_dependence (struct dwarf2_cu *cu,
20433 struct dwarf2_per_cu_data *ref_per_cu)
20434 {
20435 void **slot;
20436
20437 if (cu->dependencies == NULL)
20438 cu->dependencies
20439 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
20440 NULL, &cu->comp_unit_obstack,
20441 hashtab_obstack_allocate,
20442 dummy_obstack_deallocate);
20443
20444 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
20445 if (*slot == NULL)
20446 *slot = ref_per_cu;
20447 }
20448
20449 /* Subroutine of dwarf2_mark to pass to htab_traverse.
20450 Set the mark field in every compilation unit in the
20451 cache that we must keep because we are keeping CU. */
20452
20453 static int
20454 dwarf2_mark_helper (void **slot, void *data)
20455 {
20456 struct dwarf2_per_cu_data *per_cu;
20457
20458 per_cu = (struct dwarf2_per_cu_data *) *slot;
20459
20460 /* cu->dependencies references may not yet have been ever read if QUIT aborts
20461 reading of the chain. As such dependencies remain valid it is not much
20462 useful to track and undo them during QUIT cleanups. */
20463 if (per_cu->cu == NULL)
20464 return 1;
20465
20466 if (per_cu->cu->mark)
20467 return 1;
20468 per_cu->cu->mark = 1;
20469
20470 if (per_cu->cu->dependencies != NULL)
20471 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
20472
20473 return 1;
20474 }
20475
20476 /* Set the mark field in CU and in every other compilation unit in the
20477 cache that we must keep because we are keeping CU. */
20478
20479 static void
20480 dwarf2_mark (struct dwarf2_cu *cu)
20481 {
20482 if (cu->mark)
20483 return;
20484 cu->mark = 1;
20485 if (cu->dependencies != NULL)
20486 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
20487 }
20488
20489 static void
20490 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
20491 {
20492 while (per_cu)
20493 {
20494 per_cu->cu->mark = 0;
20495 per_cu = per_cu->cu->read_in_chain;
20496 }
20497 }
20498
20499 /* Trivial hash function for partial_die_info: the hash value of a DIE
20500 is its offset in .debug_info for this objfile. */
20501
20502 static hashval_t
20503 partial_die_hash (const void *item)
20504 {
20505 const struct partial_die_info *part_die = item;
20506
20507 return part_die->offset.sect_off;
20508 }
20509
20510 /* Trivial comparison function for partial_die_info structures: two DIEs
20511 are equal if they have the same offset. */
20512
20513 static int
20514 partial_die_eq (const void *item_lhs, const void *item_rhs)
20515 {
20516 const struct partial_die_info *part_die_lhs = item_lhs;
20517 const struct partial_die_info *part_die_rhs = item_rhs;
20518
20519 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
20520 }
20521
20522 static struct cmd_list_element *set_dwarf2_cmdlist;
20523 static struct cmd_list_element *show_dwarf2_cmdlist;
20524
20525 static void
20526 set_dwarf2_cmd (char *args, int from_tty)
20527 {
20528 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
20529 }
20530
20531 static void
20532 show_dwarf2_cmd (char *args, int from_tty)
20533 {
20534 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
20535 }
20536
20537 /* Free data associated with OBJFILE, if necessary. */
20538
20539 static void
20540 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
20541 {
20542 struct dwarf2_per_objfile *data = d;
20543 int ix;
20544
20545 /* Make sure we don't accidentally use dwarf2_per_objfile while
20546 cleaning up. */
20547 dwarf2_per_objfile = NULL;
20548
20549 for (ix = 0; ix < data->n_comp_units; ++ix)
20550 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
20551
20552 for (ix = 0; ix < data->n_type_units; ++ix)
20553 VEC_free (dwarf2_per_cu_ptr,
20554 data->all_type_units[ix]->per_cu.imported_symtabs);
20555 xfree (data->all_type_units);
20556
20557 VEC_free (dwarf2_section_info_def, data->types);
20558
20559 if (data->dwo_files)
20560 free_dwo_files (data->dwo_files, objfile);
20561 if (data->dwp_file)
20562 gdb_bfd_unref (data->dwp_file->dbfd);
20563
20564 if (data->dwz_file && data->dwz_file->dwz_bfd)
20565 gdb_bfd_unref (data->dwz_file->dwz_bfd);
20566 }
20567
20568 \f
20569 /* The "save gdb-index" command. */
20570
20571 /* The contents of the hash table we create when building the string
20572 table. */
20573 struct strtab_entry
20574 {
20575 offset_type offset;
20576 const char *str;
20577 };
20578
20579 /* Hash function for a strtab_entry.
20580
20581 Function is used only during write_hash_table so no index format backward
20582 compatibility is needed. */
20583
20584 static hashval_t
20585 hash_strtab_entry (const void *e)
20586 {
20587 const struct strtab_entry *entry = e;
20588 return mapped_index_string_hash (INT_MAX, entry->str);
20589 }
20590
20591 /* Equality function for a strtab_entry. */
20592
20593 static int
20594 eq_strtab_entry (const void *a, const void *b)
20595 {
20596 const struct strtab_entry *ea = a;
20597 const struct strtab_entry *eb = b;
20598 return !strcmp (ea->str, eb->str);
20599 }
20600
20601 /* Create a strtab_entry hash table. */
20602
20603 static htab_t
20604 create_strtab (void)
20605 {
20606 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
20607 xfree, xcalloc, xfree);
20608 }
20609
20610 /* Add a string to the constant pool. Return the string's offset in
20611 host order. */
20612
20613 static offset_type
20614 add_string (htab_t table, struct obstack *cpool, const char *str)
20615 {
20616 void **slot;
20617 struct strtab_entry entry;
20618 struct strtab_entry *result;
20619
20620 entry.str = str;
20621 slot = htab_find_slot (table, &entry, INSERT);
20622 if (*slot)
20623 result = *slot;
20624 else
20625 {
20626 result = XNEW (struct strtab_entry);
20627 result->offset = obstack_object_size (cpool);
20628 result->str = str;
20629 obstack_grow_str0 (cpool, str);
20630 *slot = result;
20631 }
20632 return result->offset;
20633 }
20634
20635 /* An entry in the symbol table. */
20636 struct symtab_index_entry
20637 {
20638 /* The name of the symbol. */
20639 const char *name;
20640 /* The offset of the name in the constant pool. */
20641 offset_type index_offset;
20642 /* A sorted vector of the indices of all the CUs that hold an object
20643 of this name. */
20644 VEC (offset_type) *cu_indices;
20645 };
20646
20647 /* The symbol table. This is a power-of-2-sized hash table. */
20648 struct mapped_symtab
20649 {
20650 offset_type n_elements;
20651 offset_type size;
20652 struct symtab_index_entry **data;
20653 };
20654
20655 /* Hash function for a symtab_index_entry. */
20656
20657 static hashval_t
20658 hash_symtab_entry (const void *e)
20659 {
20660 const struct symtab_index_entry *entry = e;
20661 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
20662 sizeof (offset_type) * VEC_length (offset_type,
20663 entry->cu_indices),
20664 0);
20665 }
20666
20667 /* Equality function for a symtab_index_entry. */
20668
20669 static int
20670 eq_symtab_entry (const void *a, const void *b)
20671 {
20672 const struct symtab_index_entry *ea = a;
20673 const struct symtab_index_entry *eb = b;
20674 int len = VEC_length (offset_type, ea->cu_indices);
20675 if (len != VEC_length (offset_type, eb->cu_indices))
20676 return 0;
20677 return !memcmp (VEC_address (offset_type, ea->cu_indices),
20678 VEC_address (offset_type, eb->cu_indices),
20679 sizeof (offset_type) * len);
20680 }
20681
20682 /* Destroy a symtab_index_entry. */
20683
20684 static void
20685 delete_symtab_entry (void *p)
20686 {
20687 struct symtab_index_entry *entry = p;
20688 VEC_free (offset_type, entry->cu_indices);
20689 xfree (entry);
20690 }
20691
20692 /* Create a hash table holding symtab_index_entry objects. */
20693
20694 static htab_t
20695 create_symbol_hash_table (void)
20696 {
20697 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
20698 delete_symtab_entry, xcalloc, xfree);
20699 }
20700
20701 /* Create a new mapped symtab object. */
20702
20703 static struct mapped_symtab *
20704 create_mapped_symtab (void)
20705 {
20706 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
20707 symtab->n_elements = 0;
20708 symtab->size = 1024;
20709 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20710 return symtab;
20711 }
20712
20713 /* Destroy a mapped_symtab. */
20714
20715 static void
20716 cleanup_mapped_symtab (void *p)
20717 {
20718 struct mapped_symtab *symtab = p;
20719 /* The contents of the array are freed when the other hash table is
20720 destroyed. */
20721 xfree (symtab->data);
20722 xfree (symtab);
20723 }
20724
20725 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
20726 the slot.
20727
20728 Function is used only during write_hash_table so no index format backward
20729 compatibility is needed. */
20730
20731 static struct symtab_index_entry **
20732 find_slot (struct mapped_symtab *symtab, const char *name)
20733 {
20734 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
20735
20736 index = hash & (symtab->size - 1);
20737 step = ((hash * 17) & (symtab->size - 1)) | 1;
20738
20739 for (;;)
20740 {
20741 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
20742 return &symtab->data[index];
20743 index = (index + step) & (symtab->size - 1);
20744 }
20745 }
20746
20747 /* Expand SYMTAB's hash table. */
20748
20749 static void
20750 hash_expand (struct mapped_symtab *symtab)
20751 {
20752 offset_type old_size = symtab->size;
20753 offset_type i;
20754 struct symtab_index_entry **old_entries = symtab->data;
20755
20756 symtab->size *= 2;
20757 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20758
20759 for (i = 0; i < old_size; ++i)
20760 {
20761 if (old_entries[i])
20762 {
20763 struct symtab_index_entry **slot = find_slot (symtab,
20764 old_entries[i]->name);
20765 *slot = old_entries[i];
20766 }
20767 }
20768
20769 xfree (old_entries);
20770 }
20771
20772 /* Add an entry to SYMTAB. NAME is the name of the symbol.
20773 CU_INDEX is the index of the CU in which the symbol appears.
20774 IS_STATIC is one if the symbol is static, otherwise zero (global). */
20775
20776 static void
20777 add_index_entry (struct mapped_symtab *symtab, const char *name,
20778 int is_static, gdb_index_symbol_kind kind,
20779 offset_type cu_index)
20780 {
20781 struct symtab_index_entry **slot;
20782 offset_type cu_index_and_attrs;
20783
20784 ++symtab->n_elements;
20785 if (4 * symtab->n_elements / 3 >= symtab->size)
20786 hash_expand (symtab);
20787
20788 slot = find_slot (symtab, name);
20789 if (!*slot)
20790 {
20791 *slot = XNEW (struct symtab_index_entry);
20792 (*slot)->name = name;
20793 /* index_offset is set later. */
20794 (*slot)->cu_indices = NULL;
20795 }
20796
20797 cu_index_and_attrs = 0;
20798 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
20799 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
20800 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
20801
20802 /* We don't want to record an index value twice as we want to avoid the
20803 duplication.
20804 We process all global symbols and then all static symbols
20805 (which would allow us to avoid the duplication by only having to check
20806 the last entry pushed), but a symbol could have multiple kinds in one CU.
20807 To keep things simple we don't worry about the duplication here and
20808 sort and uniqufy the list after we've processed all symbols. */
20809 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
20810 }
20811
20812 /* qsort helper routine for uniquify_cu_indices. */
20813
20814 static int
20815 offset_type_compare (const void *ap, const void *bp)
20816 {
20817 offset_type a = *(offset_type *) ap;
20818 offset_type b = *(offset_type *) bp;
20819
20820 return (a > b) - (b > a);
20821 }
20822
20823 /* Sort and remove duplicates of all symbols' cu_indices lists. */
20824
20825 static void
20826 uniquify_cu_indices (struct mapped_symtab *symtab)
20827 {
20828 int i;
20829
20830 for (i = 0; i < symtab->size; ++i)
20831 {
20832 struct symtab_index_entry *entry = symtab->data[i];
20833
20834 if (entry
20835 && entry->cu_indices != NULL)
20836 {
20837 unsigned int next_to_insert, next_to_check;
20838 offset_type last_value;
20839
20840 qsort (VEC_address (offset_type, entry->cu_indices),
20841 VEC_length (offset_type, entry->cu_indices),
20842 sizeof (offset_type), offset_type_compare);
20843
20844 last_value = VEC_index (offset_type, entry->cu_indices, 0);
20845 next_to_insert = 1;
20846 for (next_to_check = 1;
20847 next_to_check < VEC_length (offset_type, entry->cu_indices);
20848 ++next_to_check)
20849 {
20850 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20851 != last_value)
20852 {
20853 last_value = VEC_index (offset_type, entry->cu_indices,
20854 next_to_check);
20855 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20856 last_value);
20857 ++next_to_insert;
20858 }
20859 }
20860 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20861 }
20862 }
20863 }
20864
20865 /* Add a vector of indices to the constant pool. */
20866
20867 static offset_type
20868 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
20869 struct symtab_index_entry *entry)
20870 {
20871 void **slot;
20872
20873 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
20874 if (!*slot)
20875 {
20876 offset_type len = VEC_length (offset_type, entry->cu_indices);
20877 offset_type val = MAYBE_SWAP (len);
20878 offset_type iter;
20879 int i;
20880
20881 *slot = entry;
20882 entry->index_offset = obstack_object_size (cpool);
20883
20884 obstack_grow (cpool, &val, sizeof (val));
20885 for (i = 0;
20886 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20887 ++i)
20888 {
20889 val = MAYBE_SWAP (iter);
20890 obstack_grow (cpool, &val, sizeof (val));
20891 }
20892 }
20893 else
20894 {
20895 struct symtab_index_entry *old_entry = *slot;
20896 entry->index_offset = old_entry->index_offset;
20897 entry = old_entry;
20898 }
20899 return entry->index_offset;
20900 }
20901
20902 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20903 constant pool entries going into the obstack CPOOL. */
20904
20905 static void
20906 write_hash_table (struct mapped_symtab *symtab,
20907 struct obstack *output, struct obstack *cpool)
20908 {
20909 offset_type i;
20910 htab_t symbol_hash_table;
20911 htab_t str_table;
20912
20913 symbol_hash_table = create_symbol_hash_table ();
20914 str_table = create_strtab ();
20915
20916 /* We add all the index vectors to the constant pool first, to
20917 ensure alignment is ok. */
20918 for (i = 0; i < symtab->size; ++i)
20919 {
20920 if (symtab->data[i])
20921 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
20922 }
20923
20924 /* Now write out the hash table. */
20925 for (i = 0; i < symtab->size; ++i)
20926 {
20927 offset_type str_off, vec_off;
20928
20929 if (symtab->data[i])
20930 {
20931 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20932 vec_off = symtab->data[i]->index_offset;
20933 }
20934 else
20935 {
20936 /* While 0 is a valid constant pool index, it is not valid
20937 to have 0 for both offsets. */
20938 str_off = 0;
20939 vec_off = 0;
20940 }
20941
20942 str_off = MAYBE_SWAP (str_off);
20943 vec_off = MAYBE_SWAP (vec_off);
20944
20945 obstack_grow (output, &str_off, sizeof (str_off));
20946 obstack_grow (output, &vec_off, sizeof (vec_off));
20947 }
20948
20949 htab_delete (str_table);
20950 htab_delete (symbol_hash_table);
20951 }
20952
20953 /* Struct to map psymtab to CU index in the index file. */
20954 struct psymtab_cu_index_map
20955 {
20956 struct partial_symtab *psymtab;
20957 unsigned int cu_index;
20958 };
20959
20960 static hashval_t
20961 hash_psymtab_cu_index (const void *item)
20962 {
20963 const struct psymtab_cu_index_map *map = item;
20964
20965 return htab_hash_pointer (map->psymtab);
20966 }
20967
20968 static int
20969 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20970 {
20971 const struct psymtab_cu_index_map *lhs = item_lhs;
20972 const struct psymtab_cu_index_map *rhs = item_rhs;
20973
20974 return lhs->psymtab == rhs->psymtab;
20975 }
20976
20977 /* Helper struct for building the address table. */
20978 struct addrmap_index_data
20979 {
20980 struct objfile *objfile;
20981 struct obstack *addr_obstack;
20982 htab_t cu_index_htab;
20983
20984 /* Non-zero if the previous_* fields are valid.
20985 We can't write an entry until we see the next entry (since it is only then
20986 that we know the end of the entry). */
20987 int previous_valid;
20988 /* Index of the CU in the table of all CUs in the index file. */
20989 unsigned int previous_cu_index;
20990 /* Start address of the CU. */
20991 CORE_ADDR previous_cu_start;
20992 };
20993
20994 /* Write an address entry to OBSTACK. */
20995
20996 static void
20997 add_address_entry (struct objfile *objfile, struct obstack *obstack,
20998 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
20999 {
21000 offset_type cu_index_to_write;
21001 gdb_byte addr[8];
21002 CORE_ADDR baseaddr;
21003
21004 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21005
21006 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
21007 obstack_grow (obstack, addr, 8);
21008 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
21009 obstack_grow (obstack, addr, 8);
21010 cu_index_to_write = MAYBE_SWAP (cu_index);
21011 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
21012 }
21013
21014 /* Worker function for traversing an addrmap to build the address table. */
21015
21016 static int
21017 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
21018 {
21019 struct addrmap_index_data *data = datap;
21020 struct partial_symtab *pst = obj;
21021
21022 if (data->previous_valid)
21023 add_address_entry (data->objfile, data->addr_obstack,
21024 data->previous_cu_start, start_addr,
21025 data->previous_cu_index);
21026
21027 data->previous_cu_start = start_addr;
21028 if (pst != NULL)
21029 {
21030 struct psymtab_cu_index_map find_map, *map;
21031 find_map.psymtab = pst;
21032 map = htab_find (data->cu_index_htab, &find_map);
21033 gdb_assert (map != NULL);
21034 data->previous_cu_index = map->cu_index;
21035 data->previous_valid = 1;
21036 }
21037 else
21038 data->previous_valid = 0;
21039
21040 return 0;
21041 }
21042
21043 /* Write OBJFILE's address map to OBSTACK.
21044 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
21045 in the index file. */
21046
21047 static void
21048 write_address_map (struct objfile *objfile, struct obstack *obstack,
21049 htab_t cu_index_htab)
21050 {
21051 struct addrmap_index_data addrmap_index_data;
21052
21053 /* When writing the address table, we have to cope with the fact that
21054 the addrmap iterator only provides the start of a region; we have to
21055 wait until the next invocation to get the start of the next region. */
21056
21057 addrmap_index_data.objfile = objfile;
21058 addrmap_index_data.addr_obstack = obstack;
21059 addrmap_index_data.cu_index_htab = cu_index_htab;
21060 addrmap_index_data.previous_valid = 0;
21061
21062 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
21063 &addrmap_index_data);
21064
21065 /* It's highly unlikely the last entry (end address = 0xff...ff)
21066 is valid, but we should still handle it.
21067 The end address is recorded as the start of the next region, but that
21068 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
21069 anyway. */
21070 if (addrmap_index_data.previous_valid)
21071 add_address_entry (objfile, obstack,
21072 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
21073 addrmap_index_data.previous_cu_index);
21074 }
21075
21076 /* Return the symbol kind of PSYM. */
21077
21078 static gdb_index_symbol_kind
21079 symbol_kind (struct partial_symbol *psym)
21080 {
21081 domain_enum domain = PSYMBOL_DOMAIN (psym);
21082 enum address_class aclass = PSYMBOL_CLASS (psym);
21083
21084 switch (domain)
21085 {
21086 case VAR_DOMAIN:
21087 switch (aclass)
21088 {
21089 case LOC_BLOCK:
21090 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
21091 case LOC_TYPEDEF:
21092 return GDB_INDEX_SYMBOL_KIND_TYPE;
21093 case LOC_COMPUTED:
21094 case LOC_CONST_BYTES:
21095 case LOC_OPTIMIZED_OUT:
21096 case LOC_STATIC:
21097 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21098 case LOC_CONST:
21099 /* Note: It's currently impossible to recognize psyms as enum values
21100 short of reading the type info. For now punt. */
21101 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21102 default:
21103 /* There are other LOC_FOO values that one might want to classify
21104 as variables, but dwarf2read.c doesn't currently use them. */
21105 return GDB_INDEX_SYMBOL_KIND_OTHER;
21106 }
21107 case STRUCT_DOMAIN:
21108 return GDB_INDEX_SYMBOL_KIND_TYPE;
21109 default:
21110 return GDB_INDEX_SYMBOL_KIND_OTHER;
21111 }
21112 }
21113
21114 /* Add a list of partial symbols to SYMTAB. */
21115
21116 static void
21117 write_psymbols (struct mapped_symtab *symtab,
21118 htab_t psyms_seen,
21119 struct partial_symbol **psymp,
21120 int count,
21121 offset_type cu_index,
21122 int is_static)
21123 {
21124 for (; count-- > 0; ++psymp)
21125 {
21126 struct partial_symbol *psym = *psymp;
21127 void **slot;
21128
21129 if (SYMBOL_LANGUAGE (psym) == language_ada)
21130 error (_("Ada is not currently supported by the index"));
21131
21132 /* Only add a given psymbol once. */
21133 slot = htab_find_slot (psyms_seen, psym, INSERT);
21134 if (!*slot)
21135 {
21136 gdb_index_symbol_kind kind = symbol_kind (psym);
21137
21138 *slot = psym;
21139 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
21140 is_static, kind, cu_index);
21141 }
21142 }
21143 }
21144
21145 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
21146 exception if there is an error. */
21147
21148 static void
21149 write_obstack (FILE *file, struct obstack *obstack)
21150 {
21151 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
21152 file)
21153 != obstack_object_size (obstack))
21154 error (_("couldn't data write to file"));
21155 }
21156
21157 /* Unlink a file if the argument is not NULL. */
21158
21159 static void
21160 unlink_if_set (void *p)
21161 {
21162 char **filename = p;
21163 if (*filename)
21164 unlink (*filename);
21165 }
21166
21167 /* A helper struct used when iterating over debug_types. */
21168 struct signatured_type_index_data
21169 {
21170 struct objfile *objfile;
21171 struct mapped_symtab *symtab;
21172 struct obstack *types_list;
21173 htab_t psyms_seen;
21174 int cu_index;
21175 };
21176
21177 /* A helper function that writes a single signatured_type to an
21178 obstack. */
21179
21180 static int
21181 write_one_signatured_type (void **slot, void *d)
21182 {
21183 struct signatured_type_index_data *info = d;
21184 struct signatured_type *entry = (struct signatured_type *) *slot;
21185 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
21186 gdb_byte val[8];
21187
21188 write_psymbols (info->symtab,
21189 info->psyms_seen,
21190 info->objfile->global_psymbols.list
21191 + psymtab->globals_offset,
21192 psymtab->n_global_syms, info->cu_index,
21193 0);
21194 write_psymbols (info->symtab,
21195 info->psyms_seen,
21196 info->objfile->static_psymbols.list
21197 + psymtab->statics_offset,
21198 psymtab->n_static_syms, info->cu_index,
21199 1);
21200
21201 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21202 entry->per_cu.offset.sect_off);
21203 obstack_grow (info->types_list, val, 8);
21204 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21205 entry->type_offset_in_tu.cu_off);
21206 obstack_grow (info->types_list, val, 8);
21207 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
21208 obstack_grow (info->types_list, val, 8);
21209
21210 ++info->cu_index;
21211
21212 return 1;
21213 }
21214
21215 /* Recurse into all "included" dependencies and write their symbols as
21216 if they appeared in this psymtab. */
21217
21218 static void
21219 recursively_write_psymbols (struct objfile *objfile,
21220 struct partial_symtab *psymtab,
21221 struct mapped_symtab *symtab,
21222 htab_t psyms_seen,
21223 offset_type cu_index)
21224 {
21225 int i;
21226
21227 for (i = 0; i < psymtab->number_of_dependencies; ++i)
21228 if (psymtab->dependencies[i]->user != NULL)
21229 recursively_write_psymbols (objfile, psymtab->dependencies[i],
21230 symtab, psyms_seen, cu_index);
21231
21232 write_psymbols (symtab,
21233 psyms_seen,
21234 objfile->global_psymbols.list + psymtab->globals_offset,
21235 psymtab->n_global_syms, cu_index,
21236 0);
21237 write_psymbols (symtab,
21238 psyms_seen,
21239 objfile->static_psymbols.list + psymtab->statics_offset,
21240 psymtab->n_static_syms, cu_index,
21241 1);
21242 }
21243
21244 /* Create an index file for OBJFILE in the directory DIR. */
21245
21246 static void
21247 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
21248 {
21249 struct cleanup *cleanup;
21250 char *filename, *cleanup_filename;
21251 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
21252 struct obstack cu_list, types_cu_list;
21253 int i;
21254 FILE *out_file;
21255 struct mapped_symtab *symtab;
21256 offset_type val, size_of_contents, total_len;
21257 struct stat st;
21258 htab_t psyms_seen;
21259 htab_t cu_index_htab;
21260 struct psymtab_cu_index_map *psymtab_cu_index_map;
21261
21262 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
21263 return;
21264
21265 if (dwarf2_per_objfile->using_index)
21266 error (_("Cannot use an index to create the index"));
21267
21268 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
21269 error (_("Cannot make an index when the file has multiple .debug_types sections"));
21270
21271 if (stat (objfile->name, &st) < 0)
21272 perror_with_name (objfile->name);
21273
21274 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
21275 INDEX_SUFFIX, (char *) NULL);
21276 cleanup = make_cleanup (xfree, filename);
21277
21278 out_file = gdb_fopen_cloexec (filename, "wb");
21279 if (!out_file)
21280 error (_("Can't open `%s' for writing"), filename);
21281
21282 cleanup_filename = filename;
21283 make_cleanup (unlink_if_set, &cleanup_filename);
21284
21285 symtab = create_mapped_symtab ();
21286 make_cleanup (cleanup_mapped_symtab, symtab);
21287
21288 obstack_init (&addr_obstack);
21289 make_cleanup_obstack_free (&addr_obstack);
21290
21291 obstack_init (&cu_list);
21292 make_cleanup_obstack_free (&cu_list);
21293
21294 obstack_init (&types_cu_list);
21295 make_cleanup_obstack_free (&types_cu_list);
21296
21297 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
21298 NULL, xcalloc, xfree);
21299 make_cleanup_htab_delete (psyms_seen);
21300
21301 /* While we're scanning CU's create a table that maps a psymtab pointer
21302 (which is what addrmap records) to its index (which is what is recorded
21303 in the index file). This will later be needed to write the address
21304 table. */
21305 cu_index_htab = htab_create_alloc (100,
21306 hash_psymtab_cu_index,
21307 eq_psymtab_cu_index,
21308 NULL, xcalloc, xfree);
21309 make_cleanup_htab_delete (cu_index_htab);
21310 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
21311 xmalloc (sizeof (struct psymtab_cu_index_map)
21312 * dwarf2_per_objfile->n_comp_units);
21313 make_cleanup (xfree, psymtab_cu_index_map);
21314
21315 /* The CU list is already sorted, so we don't need to do additional
21316 work here. Also, the debug_types entries do not appear in
21317 all_comp_units, but only in their own hash table. */
21318 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
21319 {
21320 struct dwarf2_per_cu_data *per_cu
21321 = dwarf2_per_objfile->all_comp_units[i];
21322 struct partial_symtab *psymtab = per_cu->v.psymtab;
21323 gdb_byte val[8];
21324 struct psymtab_cu_index_map *map;
21325 void **slot;
21326
21327 /* CU of a shared file from 'dwz -m' may be unused by this main file.
21328 It may be referenced from a local scope but in such case it does not
21329 need to be present in .gdb_index. */
21330 if (psymtab == NULL)
21331 continue;
21332
21333 if (psymtab->user == NULL)
21334 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
21335
21336 map = &psymtab_cu_index_map[i];
21337 map->psymtab = psymtab;
21338 map->cu_index = i;
21339 slot = htab_find_slot (cu_index_htab, map, INSERT);
21340 gdb_assert (slot != NULL);
21341 gdb_assert (*slot == NULL);
21342 *slot = map;
21343
21344 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21345 per_cu->offset.sect_off);
21346 obstack_grow (&cu_list, val, 8);
21347 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
21348 obstack_grow (&cu_list, val, 8);
21349 }
21350
21351 /* Dump the address map. */
21352 write_address_map (objfile, &addr_obstack, cu_index_htab);
21353
21354 /* Write out the .debug_type entries, if any. */
21355 if (dwarf2_per_objfile->signatured_types)
21356 {
21357 struct signatured_type_index_data sig_data;
21358
21359 sig_data.objfile = objfile;
21360 sig_data.symtab = symtab;
21361 sig_data.types_list = &types_cu_list;
21362 sig_data.psyms_seen = psyms_seen;
21363 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
21364 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
21365 write_one_signatured_type, &sig_data);
21366 }
21367
21368 /* Now that we've processed all symbols we can shrink their cu_indices
21369 lists. */
21370 uniquify_cu_indices (symtab);
21371
21372 obstack_init (&constant_pool);
21373 make_cleanup_obstack_free (&constant_pool);
21374 obstack_init (&symtab_obstack);
21375 make_cleanup_obstack_free (&symtab_obstack);
21376 write_hash_table (symtab, &symtab_obstack, &constant_pool);
21377
21378 obstack_init (&contents);
21379 make_cleanup_obstack_free (&contents);
21380 size_of_contents = 6 * sizeof (offset_type);
21381 total_len = size_of_contents;
21382
21383 /* The version number. */
21384 val = MAYBE_SWAP (8);
21385 obstack_grow (&contents, &val, sizeof (val));
21386
21387 /* The offset of the CU list from the start of the file. */
21388 val = MAYBE_SWAP (total_len);
21389 obstack_grow (&contents, &val, sizeof (val));
21390 total_len += obstack_object_size (&cu_list);
21391
21392 /* The offset of the types CU list from the start of the file. */
21393 val = MAYBE_SWAP (total_len);
21394 obstack_grow (&contents, &val, sizeof (val));
21395 total_len += obstack_object_size (&types_cu_list);
21396
21397 /* The offset of the address table from the start of the file. */
21398 val = MAYBE_SWAP (total_len);
21399 obstack_grow (&contents, &val, sizeof (val));
21400 total_len += obstack_object_size (&addr_obstack);
21401
21402 /* The offset of the symbol table from the start of the file. */
21403 val = MAYBE_SWAP (total_len);
21404 obstack_grow (&contents, &val, sizeof (val));
21405 total_len += obstack_object_size (&symtab_obstack);
21406
21407 /* The offset of the constant pool from the start of the file. */
21408 val = MAYBE_SWAP (total_len);
21409 obstack_grow (&contents, &val, sizeof (val));
21410 total_len += obstack_object_size (&constant_pool);
21411
21412 gdb_assert (obstack_object_size (&contents) == size_of_contents);
21413
21414 write_obstack (out_file, &contents);
21415 write_obstack (out_file, &cu_list);
21416 write_obstack (out_file, &types_cu_list);
21417 write_obstack (out_file, &addr_obstack);
21418 write_obstack (out_file, &symtab_obstack);
21419 write_obstack (out_file, &constant_pool);
21420
21421 fclose (out_file);
21422
21423 /* We want to keep the file, so we set cleanup_filename to NULL
21424 here. See unlink_if_set. */
21425 cleanup_filename = NULL;
21426
21427 do_cleanups (cleanup);
21428 }
21429
21430 /* Implementation of the `save gdb-index' command.
21431
21432 Note that the file format used by this command is documented in the
21433 GDB manual. Any changes here must be documented there. */
21434
21435 static void
21436 save_gdb_index_command (char *arg, int from_tty)
21437 {
21438 struct objfile *objfile;
21439
21440 if (!arg || !*arg)
21441 error (_("usage: save gdb-index DIRECTORY"));
21442
21443 ALL_OBJFILES (objfile)
21444 {
21445 struct stat st;
21446
21447 /* If the objfile does not correspond to an actual file, skip it. */
21448 if (stat (objfile->name, &st) < 0)
21449 continue;
21450
21451 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
21452 if (dwarf2_per_objfile)
21453 {
21454 volatile struct gdb_exception except;
21455
21456 TRY_CATCH (except, RETURN_MASK_ERROR)
21457 {
21458 write_psymtabs_to_index (objfile, arg);
21459 }
21460 if (except.reason < 0)
21461 exception_fprintf (gdb_stderr, except,
21462 _("Error while writing index for `%s': "),
21463 objfile->name);
21464 }
21465 }
21466 }
21467
21468 \f
21469
21470 int dwarf2_always_disassemble;
21471
21472 static void
21473 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
21474 struct cmd_list_element *c, const char *value)
21475 {
21476 fprintf_filtered (file,
21477 _("Whether to always disassemble "
21478 "DWARF expressions is %s.\n"),
21479 value);
21480 }
21481
21482 static void
21483 show_check_physname (struct ui_file *file, int from_tty,
21484 struct cmd_list_element *c, const char *value)
21485 {
21486 fprintf_filtered (file,
21487 _("Whether to check \"physname\" is %s.\n"),
21488 value);
21489 }
21490
21491 void _initialize_dwarf2_read (void);
21492
21493 void
21494 _initialize_dwarf2_read (void)
21495 {
21496 struct cmd_list_element *c;
21497
21498 dwarf2_objfile_data_key
21499 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
21500
21501 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
21502 Set DWARF 2 specific variables.\n\
21503 Configure DWARF 2 variables such as the cache size"),
21504 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
21505 0/*allow-unknown*/, &maintenance_set_cmdlist);
21506
21507 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
21508 Show DWARF 2 specific variables\n\
21509 Show DWARF 2 variables such as the cache size"),
21510 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
21511 0/*allow-unknown*/, &maintenance_show_cmdlist);
21512
21513 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
21514 &dwarf2_max_cache_age, _("\
21515 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
21516 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
21517 A higher limit means that cached compilation units will be stored\n\
21518 in memory longer, and more total memory will be used. Zero disables\n\
21519 caching, which can slow down startup."),
21520 NULL,
21521 show_dwarf2_max_cache_age,
21522 &set_dwarf2_cmdlist,
21523 &show_dwarf2_cmdlist);
21524
21525 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
21526 &dwarf2_always_disassemble, _("\
21527 Set whether `info address' always disassembles DWARF expressions."), _("\
21528 Show whether `info address' always disassembles DWARF expressions."), _("\
21529 When enabled, DWARF expressions are always printed in an assembly-like\n\
21530 syntax. When disabled, expressions will be printed in a more\n\
21531 conversational style, when possible."),
21532 NULL,
21533 show_dwarf2_always_disassemble,
21534 &set_dwarf2_cmdlist,
21535 &show_dwarf2_cmdlist);
21536
21537 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
21538 Set debugging of the dwarf2 reader."), _("\
21539 Show debugging of the dwarf2 reader."), _("\
21540 When enabled, debugging messages are printed during dwarf2 reading\n\
21541 and symtab expansion."),
21542 NULL,
21543 NULL,
21544 &setdebuglist, &showdebuglist);
21545
21546 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
21547 Set debugging of the dwarf2 DIE reader."), _("\
21548 Show debugging of the dwarf2 DIE reader."), _("\
21549 When enabled (non-zero), DIEs are dumped after they are read in.\n\
21550 The value is the maximum depth to print."),
21551 NULL,
21552 NULL,
21553 &setdebuglist, &showdebuglist);
21554
21555 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
21556 Set cross-checking of \"physname\" code against demangler."), _("\
21557 Show cross-checking of \"physname\" code against demangler."), _("\
21558 When enabled, GDB's internal \"physname\" code is checked against\n\
21559 the demangler."),
21560 NULL, show_check_physname,
21561 &setdebuglist, &showdebuglist);
21562
21563 add_setshow_boolean_cmd ("use-deprecated-index-sections",
21564 no_class, &use_deprecated_index_sections, _("\
21565 Set whether to use deprecated gdb_index sections."), _("\
21566 Show whether to use deprecated gdb_index sections."), _("\
21567 When enabled, deprecated .gdb_index sections are used anyway.\n\
21568 Normally they are ignored either because of a missing feature or\n\
21569 performance issue.\n\
21570 Warning: This option must be enabled before gdb reads the file."),
21571 NULL,
21572 NULL,
21573 &setlist, &showlist);
21574
21575 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
21576 _("\
21577 Save a gdb-index file.\n\
21578 Usage: save gdb-index DIRECTORY"),
21579 &save_cmdlist);
21580 set_cmd_completer (c, filename_completer);
21581
21582 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
21583 &dwarf2_locexpr_funcs);
21584 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
21585 &dwarf2_loclist_funcs);
21586
21587 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
21588 &dwarf2_block_frame_base_locexpr_funcs);
21589 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
21590 &dwarf2_block_frame_base_loclist_funcs);
21591 }
This page took 0.848887 seconds and 4 git commands to generate.