dwarf2read.c: Make dir_index and file_name_index strong typedefs
[binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2017 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 "psympriv.h"
57 #include <sys/stat.h>
58 #include "completer.h"
59 #include "vec.h"
60 #include "c-lang.h"
61 #include "go-lang.h"
62 #include "valprint.h"
63 #include "gdbcore.h" /* for gnutarget */
64 #include "gdb/gdb-index.h"
65 #include <ctype.h>
66 #include "gdb_bfd.h"
67 #include "f-lang.h"
68 #include "source.h"
69 #include "filestuff.h"
70 #include "build-id.h"
71 #include "namespace.h"
72 #include "common/gdb_unlinker.h"
73 #include "common/function-view.h"
74 #include "common/gdb_optional.h"
75 #include "common/underlying.h"
76
77 #include <fcntl.h>
78 #include <sys/types.h>
79 #include <algorithm>
80
81 typedef struct symbol *symbolp;
82 DEF_VEC_P (symbolp);
83
84 /* When == 1, print basic high level tracing messages.
85 When > 1, be more verbose.
86 This is in contrast to the low level DIE reading of dwarf_die_debug. */
87 static unsigned int dwarf_read_debug = 0;
88
89 /* When non-zero, dump DIEs after they are read in. */
90 static unsigned int dwarf_die_debug = 0;
91
92 /* When non-zero, dump line number entries as they are read in. */
93 static unsigned int dwarf_line_debug = 0;
94
95 /* When non-zero, cross-check physname against demangler. */
96 static int check_physname = 0;
97
98 /* When non-zero, do not reject deprecated .gdb_index sections. */
99 static int use_deprecated_index_sections = 0;
100
101 static const struct objfile_data *dwarf2_objfile_data_key;
102
103 /* The "aclass" indices for various kinds of computed DWARF symbols. */
104
105 static int dwarf2_locexpr_index;
106 static int dwarf2_loclist_index;
107 static int dwarf2_locexpr_block_index;
108 static int dwarf2_loclist_block_index;
109
110 /* A descriptor for dwarf sections.
111
112 S.ASECTION, SIZE are typically initialized when the objfile is first
113 scanned. BUFFER, READIN are filled in later when the section is read.
114 If the section contained compressed data then SIZE is updated to record
115 the uncompressed size of the section.
116
117 DWP file format V2 introduces a wrinkle that is easiest to handle by
118 creating the concept of virtual sections contained within a real section.
119 In DWP V2 the sections of the input DWO files are concatenated together
120 into one section, but section offsets are kept relative to the original
121 input section.
122 If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
123 the real section this "virtual" section is contained in, and BUFFER,SIZE
124 describe the virtual section. */
125
126 struct dwarf2_section_info
127 {
128 union
129 {
130 /* If this is a real section, the bfd section. */
131 asection *section;
132 /* If this is a virtual section, pointer to the containing ("real")
133 section. */
134 struct dwarf2_section_info *containing_section;
135 } s;
136 /* Pointer to section data, only valid if readin. */
137 const gdb_byte *buffer;
138 /* The size of the section, real or virtual. */
139 bfd_size_type size;
140 /* If this is a virtual section, the offset in the real section.
141 Only valid if is_virtual. */
142 bfd_size_type virtual_offset;
143 /* True if we have tried to read this section. */
144 char readin;
145 /* True if this is a virtual section, False otherwise.
146 This specifies which of s.section and s.containing_section to use. */
147 char is_virtual;
148 };
149
150 typedef struct dwarf2_section_info dwarf2_section_info_def;
151 DEF_VEC_O (dwarf2_section_info_def);
152
153 /* All offsets in the index are of this type. It must be
154 architecture-independent. */
155 typedef uint32_t offset_type;
156
157 DEF_VEC_I (offset_type);
158
159 /* Ensure only legit values are used. */
160 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
161 do { \
162 gdb_assert ((unsigned int) (value) <= 1); \
163 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
164 } while (0)
165
166 /* Ensure only legit values are used. */
167 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
168 do { \
169 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
170 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
171 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
172 } while (0)
173
174 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
175 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
176 do { \
177 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
178 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
179 } while (0)
180
181 /* A description of the mapped index. The file format is described in
182 a comment by the code that writes the index. */
183 struct mapped_index
184 {
185 /* Index data format version. */
186 int version;
187
188 /* The total length of the buffer. */
189 off_t total_size;
190
191 /* A pointer to the address table data. */
192 const gdb_byte *address_table;
193
194 /* Size of the address table data in bytes. */
195 offset_type address_table_size;
196
197 /* The symbol table, implemented as a hash table. */
198 const offset_type *symbol_table;
199
200 /* Size in slots, each slot is 2 offset_types. */
201 offset_type symbol_table_slots;
202
203 /* A pointer to the constant pool. */
204 const char *constant_pool;
205 };
206
207 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
208 DEF_VEC_P (dwarf2_per_cu_ptr);
209
210 struct tu_stats
211 {
212 int nr_uniq_abbrev_tables;
213 int nr_symtabs;
214 int nr_symtab_sharers;
215 int nr_stmt_less_type_units;
216 int nr_all_type_units_reallocs;
217 };
218
219 /* Collection of data recorded per objfile.
220 This hangs off of dwarf2_objfile_data_key. */
221
222 struct dwarf2_per_objfile
223 {
224 struct dwarf2_section_info info;
225 struct dwarf2_section_info abbrev;
226 struct dwarf2_section_info line;
227 struct dwarf2_section_info loc;
228 struct dwarf2_section_info loclists;
229 struct dwarf2_section_info macinfo;
230 struct dwarf2_section_info macro;
231 struct dwarf2_section_info str;
232 struct dwarf2_section_info line_str;
233 struct dwarf2_section_info ranges;
234 struct dwarf2_section_info rnglists;
235 struct dwarf2_section_info addr;
236 struct dwarf2_section_info frame;
237 struct dwarf2_section_info eh_frame;
238 struct dwarf2_section_info gdb_index;
239
240 VEC (dwarf2_section_info_def) *types;
241
242 /* Back link. */
243 struct objfile *objfile;
244
245 /* Table of all the compilation units. This is used to locate
246 the target compilation unit of a particular reference. */
247 struct dwarf2_per_cu_data **all_comp_units;
248
249 /* The number of compilation units in ALL_COMP_UNITS. */
250 int n_comp_units;
251
252 /* The number of .debug_types-related CUs. */
253 int n_type_units;
254
255 /* The number of elements allocated in all_type_units.
256 If there are skeleton-less TUs, we add them to all_type_units lazily. */
257 int n_allocated_type_units;
258
259 /* The .debug_types-related CUs (TUs).
260 This is stored in malloc space because we may realloc it. */
261 struct signatured_type **all_type_units;
262
263 /* Table of struct type_unit_group objects.
264 The hash key is the DW_AT_stmt_list value. */
265 htab_t type_unit_groups;
266
267 /* A table mapping .debug_types signatures to its signatured_type entry.
268 This is NULL if the .debug_types section hasn't been read in yet. */
269 htab_t signatured_types;
270
271 /* Type unit statistics, to see how well the scaling improvements
272 are doing. */
273 struct tu_stats tu_stats;
274
275 /* A chain of compilation units that are currently read in, so that
276 they can be freed later. */
277 struct dwarf2_per_cu_data *read_in_chain;
278
279 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
280 This is NULL if the table hasn't been allocated yet. */
281 htab_t dwo_files;
282
283 /* Non-zero if we've check for whether there is a DWP file. */
284 int dwp_checked;
285
286 /* The DWP file if there is one, or NULL. */
287 struct dwp_file *dwp_file;
288
289 /* The shared '.dwz' file, if one exists. This is used when the
290 original data was compressed using 'dwz -m'. */
291 struct dwz_file *dwz_file;
292
293 /* A flag indicating wether this objfile has a section loaded at a
294 VMA of 0. */
295 int has_section_at_zero;
296
297 /* True if we are using the mapped index,
298 or we are faking it for OBJF_READNOW's sake. */
299 unsigned char using_index;
300
301 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
302 struct mapped_index *index_table;
303
304 /* When using index_table, this keeps track of all quick_file_names entries.
305 TUs typically share line table entries with a CU, so we maintain a
306 separate table of all line table entries to support the sharing.
307 Note that while there can be way more TUs than CUs, we've already
308 sorted all the TUs into "type unit groups", grouped by their
309 DW_AT_stmt_list value. Therefore the only sharing done here is with a
310 CU and its associated TU group if there is one. */
311 htab_t quick_file_names_table;
312
313 /* Set during partial symbol reading, to prevent queueing of full
314 symbols. */
315 int reading_partial_symbols;
316
317 /* Table mapping type DIEs to their struct type *.
318 This is NULL if not allocated yet.
319 The mapping is done via (CU/TU + DIE offset) -> type. */
320 htab_t die_type_hash;
321
322 /* The CUs we recently read. */
323 VEC (dwarf2_per_cu_ptr) *just_read_cus;
324
325 /* Table containing line_header indexed by offset and offset_in_dwz. */
326 htab_t line_header_hash;
327 };
328
329 static struct dwarf2_per_objfile *dwarf2_per_objfile;
330
331 /* Default names of the debugging sections. */
332
333 /* Note that if the debugging section has been compressed, it might
334 have a name like .zdebug_info. */
335
336 static const struct dwarf2_debug_sections dwarf2_elf_names =
337 {
338 { ".debug_info", ".zdebug_info" },
339 { ".debug_abbrev", ".zdebug_abbrev" },
340 { ".debug_line", ".zdebug_line" },
341 { ".debug_loc", ".zdebug_loc" },
342 { ".debug_loclists", ".zdebug_loclists" },
343 { ".debug_macinfo", ".zdebug_macinfo" },
344 { ".debug_macro", ".zdebug_macro" },
345 { ".debug_str", ".zdebug_str" },
346 { ".debug_line_str", ".zdebug_line_str" },
347 { ".debug_ranges", ".zdebug_ranges" },
348 { ".debug_rnglists", ".zdebug_rnglists" },
349 { ".debug_types", ".zdebug_types" },
350 { ".debug_addr", ".zdebug_addr" },
351 { ".debug_frame", ".zdebug_frame" },
352 { ".eh_frame", NULL },
353 { ".gdb_index", ".zgdb_index" },
354 23
355 };
356
357 /* List of DWO/DWP sections. */
358
359 static const struct dwop_section_names
360 {
361 struct dwarf2_section_names abbrev_dwo;
362 struct dwarf2_section_names info_dwo;
363 struct dwarf2_section_names line_dwo;
364 struct dwarf2_section_names loc_dwo;
365 struct dwarf2_section_names loclists_dwo;
366 struct dwarf2_section_names macinfo_dwo;
367 struct dwarf2_section_names macro_dwo;
368 struct dwarf2_section_names str_dwo;
369 struct dwarf2_section_names str_offsets_dwo;
370 struct dwarf2_section_names types_dwo;
371 struct dwarf2_section_names cu_index;
372 struct dwarf2_section_names tu_index;
373 }
374 dwop_section_names =
375 {
376 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
377 { ".debug_info.dwo", ".zdebug_info.dwo" },
378 { ".debug_line.dwo", ".zdebug_line.dwo" },
379 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
380 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
381 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
382 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
383 { ".debug_str.dwo", ".zdebug_str.dwo" },
384 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
385 { ".debug_types.dwo", ".zdebug_types.dwo" },
386 { ".debug_cu_index", ".zdebug_cu_index" },
387 { ".debug_tu_index", ".zdebug_tu_index" },
388 };
389
390 /* local data types */
391
392 /* The data in a compilation unit header, after target2host
393 translation, looks like this. */
394 struct comp_unit_head
395 {
396 unsigned int length;
397 short version;
398 unsigned char addr_size;
399 unsigned char signed_addr_p;
400 sect_offset abbrev_offset;
401
402 /* Size of file offsets; either 4 or 8. */
403 unsigned int offset_size;
404
405 /* Size of the length field; either 4 or 12. */
406 unsigned int initial_length_size;
407
408 enum dwarf_unit_type unit_type;
409
410 /* Offset to the first byte of this compilation unit header in the
411 .debug_info section, for resolving relative reference dies. */
412 sect_offset offset;
413
414 /* Offset to first die in this cu from the start of the cu.
415 This will be the first byte following the compilation unit header. */
416 cu_offset first_die_offset;
417
418 /* 64-bit signature of this type unit - it is valid only for
419 UNIT_TYPE DW_UT_type. */
420 ULONGEST signature;
421
422 /* For types, offset in the type's DIE of the type defined by this TU. */
423 cu_offset type_offset_in_tu;
424 };
425
426 /* Type used for delaying computation of method physnames.
427 See comments for compute_delayed_physnames. */
428 struct delayed_method_info
429 {
430 /* The type to which the method is attached, i.e., its parent class. */
431 struct type *type;
432
433 /* The index of the method in the type's function fieldlists. */
434 int fnfield_index;
435
436 /* The index of the method in the fieldlist. */
437 int index;
438
439 /* The name of the DIE. */
440 const char *name;
441
442 /* The DIE associated with this method. */
443 struct die_info *die;
444 };
445
446 typedef struct delayed_method_info delayed_method_info;
447 DEF_VEC_O (delayed_method_info);
448
449 /* Internal state when decoding a particular compilation unit. */
450 struct dwarf2_cu
451 {
452 /* The objfile containing this compilation unit. */
453 struct objfile *objfile;
454
455 /* The header of the compilation unit. */
456 struct comp_unit_head header;
457
458 /* Base address of this compilation unit. */
459 CORE_ADDR base_address;
460
461 /* Non-zero if base_address has been set. */
462 int base_known;
463
464 /* The language we are debugging. */
465 enum language language;
466 const struct language_defn *language_defn;
467
468 const char *producer;
469
470 /* The generic symbol table building routines have separate lists for
471 file scope symbols and all all other scopes (local scopes). So
472 we need to select the right one to pass to add_symbol_to_list().
473 We do it by keeping a pointer to the correct list in list_in_scope.
474
475 FIXME: The original dwarf code just treated the file scope as the
476 first local scope, and all other local scopes as nested local
477 scopes, and worked fine. Check to see if we really need to
478 distinguish these in buildsym.c. */
479 struct pending **list_in_scope;
480
481 /* The abbrev table for this CU.
482 Normally this points to the abbrev table in the objfile.
483 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
484 struct abbrev_table *abbrev_table;
485
486 /* Hash table holding all the loaded partial DIEs
487 with partial_die->offset.SECT_OFF as hash. */
488 htab_t partial_dies;
489
490 /* Storage for things with the same lifetime as this read-in compilation
491 unit, including partial DIEs. */
492 struct obstack comp_unit_obstack;
493
494 /* When multiple dwarf2_cu structures are living in memory, this field
495 chains them all together, so that they can be released efficiently.
496 We will probably also want a generation counter so that most-recently-used
497 compilation units are cached... */
498 struct dwarf2_per_cu_data *read_in_chain;
499
500 /* Backlink to our per_cu entry. */
501 struct dwarf2_per_cu_data *per_cu;
502
503 /* How many compilation units ago was this CU last referenced? */
504 int last_used;
505
506 /* A hash table of DIE cu_offset for following references with
507 die_info->offset.sect_off as hash. */
508 htab_t die_hash;
509
510 /* Full DIEs if read in. */
511 struct die_info *dies;
512
513 /* A set of pointers to dwarf2_per_cu_data objects for compilation
514 units referenced by this one. Only set during full symbol processing;
515 partial symbol tables do not have dependencies. */
516 htab_t dependencies;
517
518 /* Header data from the line table, during full symbol processing. */
519 struct line_header *line_header;
520
521 /* A list of methods which need to have physnames computed
522 after all type information has been read. */
523 VEC (delayed_method_info) *method_list;
524
525 /* To be copied to symtab->call_site_htab. */
526 htab_t call_site_htab;
527
528 /* Non-NULL if this CU came from a DWO file.
529 There is an invariant here that is important to remember:
530 Except for attributes copied from the top level DIE in the "main"
531 (or "stub") file in preparation for reading the DWO file
532 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
533 Either there isn't a DWO file (in which case this is NULL and the point
534 is moot), or there is and either we're not going to read it (in which
535 case this is NULL) or there is and we are reading it (in which case this
536 is non-NULL). */
537 struct dwo_unit *dwo_unit;
538
539 /* The DW_AT_addr_base attribute if present, zero otherwise
540 (zero is a valid value though).
541 Note this value comes from the Fission stub CU/TU's DIE. */
542 ULONGEST addr_base;
543
544 /* The DW_AT_ranges_base attribute if present, zero otherwise
545 (zero is a valid value though).
546 Note this value comes from the Fission stub CU/TU's DIE.
547 Also note that the value is zero in the non-DWO case so this value can
548 be used without needing to know whether DWO files are in use or not.
549 N.B. This does not apply to DW_AT_ranges appearing in
550 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
551 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
552 DW_AT_ranges_base *would* have to be applied, and we'd have to care
553 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
554 ULONGEST ranges_base;
555
556 /* Mark used when releasing cached dies. */
557 unsigned int mark : 1;
558
559 /* This CU references .debug_loc. See the symtab->locations_valid field.
560 This test is imperfect as there may exist optimized debug code not using
561 any location list and still facing inlining issues if handled as
562 unoptimized code. For a future better test see GCC PR other/32998. */
563 unsigned int has_loclist : 1;
564
565 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
566 if all the producer_is_* fields are valid. This information is cached
567 because profiling CU expansion showed excessive time spent in
568 producer_is_gxx_lt_4_6. */
569 unsigned int checked_producer : 1;
570 unsigned int producer_is_gxx_lt_4_6 : 1;
571 unsigned int producer_is_gcc_lt_4_3 : 1;
572 unsigned int producer_is_icc : 1;
573
574 /* When set, the file that we're processing is known to have
575 debugging info for C++ namespaces. GCC 3.3.x did not produce
576 this information, but later versions do. */
577
578 unsigned int processing_has_namespace_info : 1;
579 };
580
581 /* Persistent data held for a compilation unit, even when not
582 processing it. We put a pointer to this structure in the
583 read_symtab_private field of the psymtab. */
584
585 struct dwarf2_per_cu_data
586 {
587 /* The start offset and length of this compilation unit.
588 NOTE: Unlike comp_unit_head.length, this length includes
589 initial_length_size.
590 If the DIE refers to a DWO file, this is always of the original die,
591 not the DWO file. */
592 sect_offset offset;
593 unsigned int length;
594
595 /* DWARF standard version this data has been read from (such as 4 or 5). */
596 short dwarf_version;
597
598 /* Flag indicating this compilation unit will be read in before
599 any of the current compilation units are processed. */
600 unsigned int queued : 1;
601
602 /* This flag will be set when reading partial DIEs if we need to load
603 absolutely all DIEs for this compilation unit, instead of just the ones
604 we think are interesting. It gets set if we look for a DIE in the
605 hash table and don't find it. */
606 unsigned int load_all_dies : 1;
607
608 /* Non-zero if this CU is from .debug_types.
609 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
610 this is non-zero. */
611 unsigned int is_debug_types : 1;
612
613 /* Non-zero if this CU is from the .dwz file. */
614 unsigned int is_dwz : 1;
615
616 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
617 This flag is only valid if is_debug_types is true.
618 We can't read a CU directly from a DWO file: There are required
619 attributes in the stub. */
620 unsigned int reading_dwo_directly : 1;
621
622 /* Non-zero if the TU has been read.
623 This is used to assist the "Stay in DWO Optimization" for Fission:
624 When reading a DWO, it's faster to read TUs from the DWO instead of
625 fetching them from random other DWOs (due to comdat folding).
626 If the TU has already been read, the optimization is unnecessary
627 (and unwise - we don't want to change where gdb thinks the TU lives
628 "midflight").
629 This flag is only valid if is_debug_types is true. */
630 unsigned int tu_read : 1;
631
632 /* The section this CU/TU lives in.
633 If the DIE refers to a DWO file, this is always the original die,
634 not the DWO file. */
635 struct dwarf2_section_info *section;
636
637 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
638 of the CU cache it gets reset to NULL again. This is left as NULL for
639 dummy CUs (a CU header, but nothing else). */
640 struct dwarf2_cu *cu;
641
642 /* The corresponding objfile.
643 Normally we can get the objfile from dwarf2_per_objfile.
644 However we can enter this file with just a "per_cu" handle. */
645 struct objfile *objfile;
646
647 /* When dwarf2_per_objfile->using_index is true, the 'quick' field
648 is active. Otherwise, the 'psymtab' field is active. */
649 union
650 {
651 /* The partial symbol table associated with this compilation unit,
652 or NULL for unread partial units. */
653 struct partial_symtab *psymtab;
654
655 /* Data needed by the "quick" functions. */
656 struct dwarf2_per_cu_quick_data *quick;
657 } v;
658
659 /* The CUs we import using DW_TAG_imported_unit. This is filled in
660 while reading psymtabs, used to compute the psymtab dependencies,
661 and then cleared. Then it is filled in again while reading full
662 symbols, and only deleted when the objfile is destroyed.
663
664 This is also used to work around a difference between the way gold
665 generates .gdb_index version <=7 and the way gdb does. Arguably this
666 is a gold bug. For symbols coming from TUs, gold records in the index
667 the CU that includes the TU instead of the TU itself. This breaks
668 dw2_lookup_symbol: It assumes that if the index says symbol X lives
669 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
670 will find X. Alas TUs live in their own symtab, so after expanding CU Y
671 we need to look in TU Z to find X. Fortunately, this is akin to
672 DW_TAG_imported_unit, so we just use the same mechanism: For
673 .gdb_index version <=7 this also records the TUs that the CU referred
674 to. Concurrently with this change gdb was modified to emit version 8
675 indices so we only pay a price for gold generated indices.
676 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
677 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
678 };
679
680 /* Entry in the signatured_types hash table. */
681
682 struct signatured_type
683 {
684 /* The "per_cu" object of this type.
685 This struct is used iff per_cu.is_debug_types.
686 N.B.: This is the first member so that it's easy to convert pointers
687 between them. */
688 struct dwarf2_per_cu_data per_cu;
689
690 /* The type's signature. */
691 ULONGEST signature;
692
693 /* Offset in the TU of the type's DIE, as read from the TU header.
694 If this TU is a DWO stub and the definition lives in a DWO file
695 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
696 cu_offset type_offset_in_tu;
697
698 /* Offset in the section of the type's DIE.
699 If the definition lives in a DWO file, this is the offset in the
700 .debug_types.dwo section.
701 The value is zero until the actual value is known.
702 Zero is otherwise not a valid section offset. */
703 sect_offset type_offset_in_section;
704
705 /* Type units are grouped by their DW_AT_stmt_list entry so that they
706 can share them. This points to the containing symtab. */
707 struct type_unit_group *type_unit_group;
708
709 /* The type.
710 The first time we encounter this type we fully read it in and install it
711 in the symbol tables. Subsequent times we only need the type. */
712 struct type *type;
713
714 /* Containing DWO unit.
715 This field is valid iff per_cu.reading_dwo_directly. */
716 struct dwo_unit *dwo_unit;
717 };
718
719 typedef struct signatured_type *sig_type_ptr;
720 DEF_VEC_P (sig_type_ptr);
721
722 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
723 This includes type_unit_group and quick_file_names. */
724
725 struct stmt_list_hash
726 {
727 /* The DWO unit this table is from or NULL if there is none. */
728 struct dwo_unit *dwo_unit;
729
730 /* Offset in .debug_line or .debug_line.dwo. */
731 sect_offset line_offset;
732 };
733
734 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
735 an object of this type. */
736
737 struct type_unit_group
738 {
739 /* dwarf2read.c's main "handle" on a TU symtab.
740 To simplify things we create an artificial CU that "includes" all the
741 type units using this stmt_list so that the rest of the code still has
742 a "per_cu" handle on the symtab.
743 This PER_CU is recognized by having no section. */
744 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
745 struct dwarf2_per_cu_data per_cu;
746
747 /* The TUs that share this DW_AT_stmt_list entry.
748 This is added to while parsing type units to build partial symtabs,
749 and is deleted afterwards and not used again. */
750 VEC (sig_type_ptr) *tus;
751
752 /* The compunit symtab.
753 Type units in a group needn't all be defined in the same source file,
754 so we create an essentially anonymous symtab as the compunit symtab. */
755 struct compunit_symtab *compunit_symtab;
756
757 /* The data used to construct the hash key. */
758 struct stmt_list_hash hash;
759
760 /* The number of symtabs from the line header.
761 The value here must match line_header.num_file_names. */
762 unsigned int num_symtabs;
763
764 /* The symbol tables for this TU (obtained from the files listed in
765 DW_AT_stmt_list).
766 WARNING: The order of entries here must match the order of entries
767 in the line header. After the first TU using this type_unit_group, the
768 line header for the subsequent TUs is recreated from this. This is done
769 because we need to use the same symtabs for each TU using the same
770 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
771 there's no guarantee the line header doesn't have duplicate entries. */
772 struct symtab **symtabs;
773 };
774
775 /* These sections are what may appear in a (real or virtual) DWO file. */
776
777 struct dwo_sections
778 {
779 struct dwarf2_section_info abbrev;
780 struct dwarf2_section_info line;
781 struct dwarf2_section_info loc;
782 struct dwarf2_section_info loclists;
783 struct dwarf2_section_info macinfo;
784 struct dwarf2_section_info macro;
785 struct dwarf2_section_info str;
786 struct dwarf2_section_info str_offsets;
787 /* In the case of a virtual DWO file, these two are unused. */
788 struct dwarf2_section_info info;
789 VEC (dwarf2_section_info_def) *types;
790 };
791
792 /* CUs/TUs in DWP/DWO files. */
793
794 struct dwo_unit
795 {
796 /* Backlink to the containing struct dwo_file. */
797 struct dwo_file *dwo_file;
798
799 /* The "id" that distinguishes this CU/TU.
800 .debug_info calls this "dwo_id", .debug_types calls this "signature".
801 Since signatures came first, we stick with it for consistency. */
802 ULONGEST signature;
803
804 /* The section this CU/TU lives in, in the DWO file. */
805 struct dwarf2_section_info *section;
806
807 /* Same as dwarf2_per_cu_data:{offset,length} but in the DWO section. */
808 sect_offset offset;
809 unsigned int length;
810
811 /* For types, offset in the type's DIE of the type defined by this TU. */
812 cu_offset type_offset_in_tu;
813 };
814
815 /* include/dwarf2.h defines the DWP section codes.
816 It defines a max value but it doesn't define a min value, which we
817 use for error checking, so provide one. */
818
819 enum dwp_v2_section_ids
820 {
821 DW_SECT_MIN = 1
822 };
823
824 /* Data for one DWO file.
825
826 This includes virtual DWO files (a virtual DWO file is a DWO file as it
827 appears in a DWP file). DWP files don't really have DWO files per se -
828 comdat folding of types "loses" the DWO file they came from, and from
829 a high level view DWP files appear to contain a mass of random types.
830 However, to maintain consistency with the non-DWP case we pretend DWP
831 files contain virtual DWO files, and we assign each TU with one virtual
832 DWO file (generally based on the line and abbrev section offsets -
833 a heuristic that seems to work in practice). */
834
835 struct dwo_file
836 {
837 /* The DW_AT_GNU_dwo_name attribute.
838 For virtual DWO files the name is constructed from the section offsets
839 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
840 from related CU+TUs. */
841 const char *dwo_name;
842
843 /* The DW_AT_comp_dir attribute. */
844 const char *comp_dir;
845
846 /* The bfd, when the file is open. Otherwise this is NULL.
847 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
848 bfd *dbfd;
849
850 /* The sections that make up this DWO file.
851 Remember that for virtual DWO files in DWP V2, these are virtual
852 sections (for lack of a better name). */
853 struct dwo_sections sections;
854
855 /* The CU in the file.
856 We only support one because having more than one requires hacking the
857 dwo_name of each to match, which is highly unlikely to happen.
858 Doing this means all TUs can share comp_dir: We also assume that
859 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
860 struct dwo_unit *cu;
861
862 /* Table of TUs in the file.
863 Each element is a struct dwo_unit. */
864 htab_t tus;
865 };
866
867 /* These sections are what may appear in a DWP file. */
868
869 struct dwp_sections
870 {
871 /* These are used by both DWP version 1 and 2. */
872 struct dwarf2_section_info str;
873 struct dwarf2_section_info cu_index;
874 struct dwarf2_section_info tu_index;
875
876 /* These are only used by DWP version 2 files.
877 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
878 sections are referenced by section number, and are not recorded here.
879 In DWP version 2 there is at most one copy of all these sections, each
880 section being (effectively) comprised of the concatenation of all of the
881 individual sections that exist in the version 1 format.
882 To keep the code simple we treat each of these concatenated pieces as a
883 section itself (a virtual section?). */
884 struct dwarf2_section_info abbrev;
885 struct dwarf2_section_info info;
886 struct dwarf2_section_info line;
887 struct dwarf2_section_info loc;
888 struct dwarf2_section_info macinfo;
889 struct dwarf2_section_info macro;
890 struct dwarf2_section_info str_offsets;
891 struct dwarf2_section_info types;
892 };
893
894 /* These sections are what may appear in a virtual DWO file in DWP version 1.
895 A virtual DWO file is a DWO file as it appears in a DWP file. */
896
897 struct virtual_v1_dwo_sections
898 {
899 struct dwarf2_section_info abbrev;
900 struct dwarf2_section_info line;
901 struct dwarf2_section_info loc;
902 struct dwarf2_section_info macinfo;
903 struct dwarf2_section_info macro;
904 struct dwarf2_section_info str_offsets;
905 /* Each DWP hash table entry records one CU or one TU.
906 That is recorded here, and copied to dwo_unit.section. */
907 struct dwarf2_section_info info_or_types;
908 };
909
910 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
911 In version 2, the sections of the DWO files are concatenated together
912 and stored in one section of that name. Thus each ELF section contains
913 several "virtual" sections. */
914
915 struct virtual_v2_dwo_sections
916 {
917 bfd_size_type abbrev_offset;
918 bfd_size_type abbrev_size;
919
920 bfd_size_type line_offset;
921 bfd_size_type line_size;
922
923 bfd_size_type loc_offset;
924 bfd_size_type loc_size;
925
926 bfd_size_type macinfo_offset;
927 bfd_size_type macinfo_size;
928
929 bfd_size_type macro_offset;
930 bfd_size_type macro_size;
931
932 bfd_size_type str_offsets_offset;
933 bfd_size_type str_offsets_size;
934
935 /* Each DWP hash table entry records one CU or one TU.
936 That is recorded here, and copied to dwo_unit.section. */
937 bfd_size_type info_or_types_offset;
938 bfd_size_type info_or_types_size;
939 };
940
941 /* Contents of DWP hash tables. */
942
943 struct dwp_hash_table
944 {
945 uint32_t version, nr_columns;
946 uint32_t nr_units, nr_slots;
947 const gdb_byte *hash_table, *unit_table;
948 union
949 {
950 struct
951 {
952 const gdb_byte *indices;
953 } v1;
954 struct
955 {
956 /* This is indexed by column number and gives the id of the section
957 in that column. */
958 #define MAX_NR_V2_DWO_SECTIONS \
959 (1 /* .debug_info or .debug_types */ \
960 + 1 /* .debug_abbrev */ \
961 + 1 /* .debug_line */ \
962 + 1 /* .debug_loc */ \
963 + 1 /* .debug_str_offsets */ \
964 + 1 /* .debug_macro or .debug_macinfo */)
965 int section_ids[MAX_NR_V2_DWO_SECTIONS];
966 const gdb_byte *offsets;
967 const gdb_byte *sizes;
968 } v2;
969 } section_pool;
970 };
971
972 /* Data for one DWP file. */
973
974 struct dwp_file
975 {
976 /* Name of the file. */
977 const char *name;
978
979 /* File format version. */
980 int version;
981
982 /* The bfd. */
983 bfd *dbfd;
984
985 /* Section info for this file. */
986 struct dwp_sections sections;
987
988 /* Table of CUs in the file. */
989 const struct dwp_hash_table *cus;
990
991 /* Table of TUs in the file. */
992 const struct dwp_hash_table *tus;
993
994 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
995 htab_t loaded_cus;
996 htab_t loaded_tus;
997
998 /* Table to map ELF section numbers to their sections.
999 This is only needed for the DWP V1 file format. */
1000 unsigned int num_sections;
1001 asection **elf_sections;
1002 };
1003
1004 /* This represents a '.dwz' file. */
1005
1006 struct dwz_file
1007 {
1008 /* A dwz file can only contain a few sections. */
1009 struct dwarf2_section_info abbrev;
1010 struct dwarf2_section_info info;
1011 struct dwarf2_section_info str;
1012 struct dwarf2_section_info line;
1013 struct dwarf2_section_info macro;
1014 struct dwarf2_section_info gdb_index;
1015
1016 /* The dwz's BFD. */
1017 bfd *dwz_bfd;
1018 };
1019
1020 /* Struct used to pass misc. parameters to read_die_and_children, et
1021 al. which are used for both .debug_info and .debug_types dies.
1022 All parameters here are unchanging for the life of the call. This
1023 struct exists to abstract away the constant parameters of die reading. */
1024
1025 struct die_reader_specs
1026 {
1027 /* The bfd of die_section. */
1028 bfd* abfd;
1029
1030 /* The CU of the DIE we are parsing. */
1031 struct dwarf2_cu *cu;
1032
1033 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1034 struct dwo_file *dwo_file;
1035
1036 /* The section the die comes from.
1037 This is either .debug_info or .debug_types, or the .dwo variants. */
1038 struct dwarf2_section_info *die_section;
1039
1040 /* die_section->buffer. */
1041 const gdb_byte *buffer;
1042
1043 /* The end of the buffer. */
1044 const gdb_byte *buffer_end;
1045
1046 /* The value of the DW_AT_comp_dir attribute. */
1047 const char *comp_dir;
1048 };
1049
1050 /* Type of function passed to init_cutu_and_read_dies, et.al. */
1051 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
1052 const gdb_byte *info_ptr,
1053 struct die_info *comp_unit_die,
1054 int has_children,
1055 void *data);
1056
1057 /* A 1-based directory index. This is a strong typedef to prevent
1058 accidentally using a directory index as a 0-based index into an
1059 array/vector. */
1060 enum class dir_index : unsigned int {};
1061
1062 /* Likewise, a 1-based file name index. */
1063 enum class file_name_index : unsigned int {};
1064
1065 struct file_entry
1066 {
1067 file_entry () = default;
1068
1069 file_entry (const char *name_, dir_index d_index_,
1070 unsigned int mod_time_, unsigned int length_)
1071 : name (name_),
1072 d_index (d_index_),
1073 mod_time (mod_time_),
1074 length (length_)
1075 {}
1076
1077 /* Return the include directory at D_INDEX stored in LH. Returns
1078 NULL if D_INDEX is out of bounds. */
1079 const char *include_dir (const line_header *lh) const;
1080
1081 /* The file name. Note this is an observing pointer. The memory is
1082 owned by debug_line_buffer. */
1083 const char *name {};
1084
1085 /* The directory index (1-based). */
1086 dir_index d_index {};
1087
1088 unsigned int mod_time {};
1089
1090 unsigned int length {};
1091
1092 /* True if referenced by the Line Number Program. */
1093 bool included_p {};
1094
1095 /* The associated symbol table, if any. */
1096 struct symtab *symtab {};
1097 };
1098
1099 /* The line number information for a compilation unit (found in the
1100 .debug_line section) begins with a "statement program header",
1101 which contains the following information. */
1102 struct line_header
1103 {
1104 line_header ()
1105 : offset_in_dwz {}
1106 {}
1107
1108 /* Add an entry to the include directory table. */
1109 void add_include_dir (const char *include_dir);
1110
1111 /* Add an entry to the file name table. */
1112 void add_file_name (const char *name, dir_index d_index,
1113 unsigned int mod_time, unsigned int length);
1114
1115 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
1116 is out of bounds. */
1117 const char *include_dir_at (dir_index index) const
1118 {
1119 /* Convert directory index number (1-based) to vector index
1120 (0-based). */
1121 size_t vec_index = to_underlying (index) - 1;
1122
1123 if (vec_index >= include_dirs.size ())
1124 return NULL;
1125 return include_dirs[vec_index];
1126 }
1127
1128 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
1129 is out of bounds. */
1130 file_entry *file_name_at (file_name_index index)
1131 {
1132 /* Convert file name index number (1-based) to vector index
1133 (0-based). */
1134 size_t vec_index = to_underlying (index) - 1;
1135
1136 if (vec_index >= file_names.size ())
1137 return NULL;
1138 return &file_names[vec_index];
1139 }
1140
1141 /* Const version of the above. */
1142 const file_entry *file_name_at (unsigned int index) const
1143 {
1144 if (index >= file_names.size ())
1145 return NULL;
1146 return &file_names[index];
1147 }
1148
1149 /* Offset of line number information in .debug_line section. */
1150 sect_offset offset {};
1151
1152 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1153 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1154
1155 unsigned int total_length {};
1156 unsigned short version {};
1157 unsigned int header_length {};
1158 unsigned char minimum_instruction_length {};
1159 unsigned char maximum_ops_per_instruction {};
1160 unsigned char default_is_stmt {};
1161 int line_base {};
1162 unsigned char line_range {};
1163 unsigned char opcode_base {};
1164
1165 /* standard_opcode_lengths[i] is the number of operands for the
1166 standard opcode whose value is i. This means that
1167 standard_opcode_lengths[0] is unused, and the last meaningful
1168 element is standard_opcode_lengths[opcode_base - 1]. */
1169 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1170
1171 /* The include_directories table. Note these are observing
1172 pointers. The memory is owned by debug_line_buffer. */
1173 std::vector<const char *> include_dirs;
1174
1175 /* The file_names table. */
1176 std::vector<file_entry> file_names;
1177
1178 /* The start and end of the statement program following this
1179 header. These point into dwarf2_per_objfile->line_buffer. */
1180 const gdb_byte *statement_program_start {}, *statement_program_end {};
1181 };
1182
1183 typedef std::unique_ptr<line_header> line_header_up;
1184
1185 const char *
1186 file_entry::include_dir (const line_header *lh) const
1187 {
1188 return lh->include_dir_at (d_index);
1189 }
1190
1191 /* When we construct a partial symbol table entry we only
1192 need this much information. */
1193 struct partial_die_info
1194 {
1195 /* Offset of this DIE. */
1196 sect_offset offset;
1197
1198 /* DWARF-2 tag for this DIE. */
1199 ENUM_BITFIELD(dwarf_tag) tag : 16;
1200
1201 /* Assorted flags describing the data found in this DIE. */
1202 unsigned int has_children : 1;
1203 unsigned int is_external : 1;
1204 unsigned int is_declaration : 1;
1205 unsigned int has_type : 1;
1206 unsigned int has_specification : 1;
1207 unsigned int has_pc_info : 1;
1208 unsigned int may_be_inlined : 1;
1209
1210 /* This DIE has been marked DW_AT_main_subprogram. */
1211 unsigned int main_subprogram : 1;
1212
1213 /* Flag set if the SCOPE field of this structure has been
1214 computed. */
1215 unsigned int scope_set : 1;
1216
1217 /* Flag set if the DIE has a byte_size attribute. */
1218 unsigned int has_byte_size : 1;
1219
1220 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1221 unsigned int has_const_value : 1;
1222
1223 /* Flag set if any of the DIE's children are template arguments. */
1224 unsigned int has_template_arguments : 1;
1225
1226 /* Flag set if fixup_partial_die has been called on this die. */
1227 unsigned int fixup_called : 1;
1228
1229 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1230 unsigned int is_dwz : 1;
1231
1232 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1233 unsigned int spec_is_dwz : 1;
1234
1235 /* The name of this DIE. Normally the value of DW_AT_name, but
1236 sometimes a default name for unnamed DIEs. */
1237 const char *name;
1238
1239 /* The linkage name, if present. */
1240 const char *linkage_name;
1241
1242 /* The scope to prepend to our children. This is generally
1243 allocated on the comp_unit_obstack, so will disappear
1244 when this compilation unit leaves the cache. */
1245 const char *scope;
1246
1247 /* Some data associated with the partial DIE. The tag determines
1248 which field is live. */
1249 union
1250 {
1251 /* The location description associated with this DIE, if any. */
1252 struct dwarf_block *locdesc;
1253 /* The offset of an import, for DW_TAG_imported_unit. */
1254 sect_offset offset;
1255 } d;
1256
1257 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1258 CORE_ADDR lowpc;
1259 CORE_ADDR highpc;
1260
1261 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1262 DW_AT_sibling, if any. */
1263 /* NOTE: This member isn't strictly necessary, read_partial_die could
1264 return DW_AT_sibling values to its caller load_partial_dies. */
1265 const gdb_byte *sibling;
1266
1267 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1268 DW_AT_specification (or DW_AT_abstract_origin or
1269 DW_AT_extension). */
1270 sect_offset spec_offset;
1271
1272 /* Pointers to this DIE's parent, first child, and next sibling,
1273 if any. */
1274 struct partial_die_info *die_parent, *die_child, *die_sibling;
1275 };
1276
1277 /* This data structure holds the information of an abbrev. */
1278 struct abbrev_info
1279 {
1280 unsigned int number; /* number identifying abbrev */
1281 enum dwarf_tag tag; /* dwarf tag */
1282 unsigned short has_children; /* boolean */
1283 unsigned short num_attrs; /* number of attributes */
1284 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1285 struct abbrev_info *next; /* next in chain */
1286 };
1287
1288 struct attr_abbrev
1289 {
1290 ENUM_BITFIELD(dwarf_attribute) name : 16;
1291 ENUM_BITFIELD(dwarf_form) form : 16;
1292
1293 /* It is valid only if FORM is DW_FORM_implicit_const. */
1294 LONGEST implicit_const;
1295 };
1296
1297 /* Size of abbrev_table.abbrev_hash_table. */
1298 #define ABBREV_HASH_SIZE 121
1299
1300 /* Top level data structure to contain an abbreviation table. */
1301
1302 struct abbrev_table
1303 {
1304 /* Where the abbrev table came from.
1305 This is used as a sanity check when the table is used. */
1306 sect_offset offset;
1307
1308 /* Storage for the abbrev table. */
1309 struct obstack abbrev_obstack;
1310
1311 /* Hash table of abbrevs.
1312 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1313 It could be statically allocated, but the previous code didn't so we
1314 don't either. */
1315 struct abbrev_info **abbrevs;
1316 };
1317
1318 /* Attributes have a name and a value. */
1319 struct attribute
1320 {
1321 ENUM_BITFIELD(dwarf_attribute) name : 16;
1322 ENUM_BITFIELD(dwarf_form) form : 15;
1323
1324 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1325 field should be in u.str (existing only for DW_STRING) but it is kept
1326 here for better struct attribute alignment. */
1327 unsigned int string_is_canonical : 1;
1328
1329 union
1330 {
1331 const char *str;
1332 struct dwarf_block *blk;
1333 ULONGEST unsnd;
1334 LONGEST snd;
1335 CORE_ADDR addr;
1336 ULONGEST signature;
1337 }
1338 u;
1339 };
1340
1341 /* This data structure holds a complete die structure. */
1342 struct die_info
1343 {
1344 /* DWARF-2 tag for this DIE. */
1345 ENUM_BITFIELD(dwarf_tag) tag : 16;
1346
1347 /* Number of attributes */
1348 unsigned char num_attrs;
1349
1350 /* True if we're presently building the full type name for the
1351 type derived from this DIE. */
1352 unsigned char building_fullname : 1;
1353
1354 /* True if this die is in process. PR 16581. */
1355 unsigned char in_process : 1;
1356
1357 /* Abbrev number */
1358 unsigned int abbrev;
1359
1360 /* Offset in .debug_info or .debug_types section. */
1361 sect_offset offset;
1362
1363 /* The dies in a compilation unit form an n-ary tree. PARENT
1364 points to this die's parent; CHILD points to the first child of
1365 this node; and all the children of a given node are chained
1366 together via their SIBLING fields. */
1367 struct die_info *child; /* Its first child, if any. */
1368 struct die_info *sibling; /* Its next sibling, if any. */
1369 struct die_info *parent; /* Its parent, if any. */
1370
1371 /* An array of attributes, with NUM_ATTRS elements. There may be
1372 zero, but it's not common and zero-sized arrays are not
1373 sufficiently portable C. */
1374 struct attribute attrs[1];
1375 };
1376
1377 /* Get at parts of an attribute structure. */
1378
1379 #define DW_STRING(attr) ((attr)->u.str)
1380 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1381 #define DW_UNSND(attr) ((attr)->u.unsnd)
1382 #define DW_BLOCK(attr) ((attr)->u.blk)
1383 #define DW_SND(attr) ((attr)->u.snd)
1384 #define DW_ADDR(attr) ((attr)->u.addr)
1385 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1386
1387 /* Blocks are a bunch of untyped bytes. */
1388 struct dwarf_block
1389 {
1390 size_t size;
1391
1392 /* Valid only if SIZE is not zero. */
1393 const gdb_byte *data;
1394 };
1395
1396 #ifndef ATTR_ALLOC_CHUNK
1397 #define ATTR_ALLOC_CHUNK 4
1398 #endif
1399
1400 /* Allocate fields for structs, unions and enums in this size. */
1401 #ifndef DW_FIELD_ALLOC_CHUNK
1402 #define DW_FIELD_ALLOC_CHUNK 4
1403 #endif
1404
1405 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1406 but this would require a corresponding change in unpack_field_as_long
1407 and friends. */
1408 static int bits_per_byte = 8;
1409
1410 struct nextfield
1411 {
1412 struct nextfield *next;
1413 int accessibility;
1414 int virtuality;
1415 struct field field;
1416 };
1417
1418 struct nextfnfield
1419 {
1420 struct nextfnfield *next;
1421 struct fn_field fnfield;
1422 };
1423
1424 struct fnfieldlist
1425 {
1426 const char *name;
1427 int length;
1428 struct nextfnfield *head;
1429 };
1430
1431 struct typedef_field_list
1432 {
1433 struct typedef_field field;
1434 struct typedef_field_list *next;
1435 };
1436
1437 /* The routines that read and process dies for a C struct or C++ class
1438 pass lists of data member fields and lists of member function fields
1439 in an instance of a field_info structure, as defined below. */
1440 struct field_info
1441 {
1442 /* List of data member and baseclasses fields. */
1443 struct nextfield *fields, *baseclasses;
1444
1445 /* Number of fields (including baseclasses). */
1446 int nfields;
1447
1448 /* Number of baseclasses. */
1449 int nbaseclasses;
1450
1451 /* Set if the accesibility of one of the fields is not public. */
1452 int non_public_fields;
1453
1454 /* Member function fields array, entries are allocated in the order they
1455 are encountered in the object file. */
1456 struct nextfnfield *fnfields;
1457
1458 /* Member function fieldlist array, contains name of possibly overloaded
1459 member function, number of overloaded member functions and a pointer
1460 to the head of the member function field chain. */
1461 struct fnfieldlist *fnfieldlists;
1462
1463 /* Number of entries in the fnfieldlists array. */
1464 int nfnfields;
1465
1466 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1467 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1468 struct typedef_field_list *typedef_field_list;
1469 unsigned typedef_field_list_count;
1470 };
1471
1472 /* One item on the queue of compilation units to read in full symbols
1473 for. */
1474 struct dwarf2_queue_item
1475 {
1476 struct dwarf2_per_cu_data *per_cu;
1477 enum language pretend_language;
1478 struct dwarf2_queue_item *next;
1479 };
1480
1481 /* The current queue. */
1482 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1483
1484 /* Loaded secondary compilation units are kept in memory until they
1485 have not been referenced for the processing of this many
1486 compilation units. Set this to zero to disable caching. Cache
1487 sizes of up to at least twenty will improve startup time for
1488 typical inter-CU-reference binaries, at an obvious memory cost. */
1489 static int dwarf_max_cache_age = 5;
1490 static void
1491 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1492 struct cmd_list_element *c, const char *value)
1493 {
1494 fprintf_filtered (file, _("The upper bound on the age of cached "
1495 "DWARF compilation units is %s.\n"),
1496 value);
1497 }
1498 \f
1499 /* local function prototypes */
1500
1501 static const char *get_section_name (const struct dwarf2_section_info *);
1502
1503 static const char *get_section_file_name (const struct dwarf2_section_info *);
1504
1505 static void dwarf2_locate_sections (bfd *, asection *, void *);
1506
1507 static void dwarf2_find_base_address (struct die_info *die,
1508 struct dwarf2_cu *cu);
1509
1510 static struct partial_symtab *create_partial_symtab
1511 (struct dwarf2_per_cu_data *per_cu, const char *name);
1512
1513 static void dwarf2_build_psymtabs_hard (struct objfile *);
1514
1515 static void scan_partial_symbols (struct partial_die_info *,
1516 CORE_ADDR *, CORE_ADDR *,
1517 int, struct dwarf2_cu *);
1518
1519 static void add_partial_symbol (struct partial_die_info *,
1520 struct dwarf2_cu *);
1521
1522 static void add_partial_namespace (struct partial_die_info *pdi,
1523 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1524 int set_addrmap, struct dwarf2_cu *cu);
1525
1526 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1527 CORE_ADDR *highpc, int set_addrmap,
1528 struct dwarf2_cu *cu);
1529
1530 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1531 struct dwarf2_cu *cu);
1532
1533 static void add_partial_subprogram (struct partial_die_info *pdi,
1534 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1535 int need_pc, struct dwarf2_cu *cu);
1536
1537 static void dwarf2_read_symtab (struct partial_symtab *,
1538 struct objfile *);
1539
1540 static void psymtab_to_symtab_1 (struct partial_symtab *);
1541
1542 static struct abbrev_info *abbrev_table_lookup_abbrev
1543 (const struct abbrev_table *, unsigned int);
1544
1545 static struct abbrev_table *abbrev_table_read_table
1546 (struct dwarf2_section_info *, sect_offset);
1547
1548 static void abbrev_table_free (struct abbrev_table *);
1549
1550 static void abbrev_table_free_cleanup (void *);
1551
1552 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1553 struct dwarf2_section_info *);
1554
1555 static void dwarf2_free_abbrev_table (void *);
1556
1557 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1558
1559 static struct partial_die_info *load_partial_dies
1560 (const struct die_reader_specs *, const gdb_byte *, int);
1561
1562 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1563 struct partial_die_info *,
1564 struct abbrev_info *,
1565 unsigned int,
1566 const gdb_byte *);
1567
1568 static struct partial_die_info *find_partial_die (sect_offset, int,
1569 struct dwarf2_cu *);
1570
1571 static void fixup_partial_die (struct partial_die_info *,
1572 struct dwarf2_cu *);
1573
1574 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1575 struct attribute *, struct attr_abbrev *,
1576 const gdb_byte *);
1577
1578 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1579
1580 static int read_1_signed_byte (bfd *, const gdb_byte *);
1581
1582 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1583
1584 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1585
1586 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1587
1588 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1589 unsigned int *);
1590
1591 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1592
1593 static LONGEST read_checked_initial_length_and_offset
1594 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1595 unsigned int *, unsigned int *);
1596
1597 static LONGEST read_offset (bfd *, const gdb_byte *,
1598 const struct comp_unit_head *,
1599 unsigned int *);
1600
1601 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1602
1603 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1604 sect_offset);
1605
1606 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1607
1608 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1609
1610 static const char *read_indirect_string (bfd *, const gdb_byte *,
1611 const struct comp_unit_head *,
1612 unsigned int *);
1613
1614 static const char *read_indirect_line_string (bfd *, const gdb_byte *,
1615 const struct comp_unit_head *,
1616 unsigned int *);
1617
1618 static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1619
1620 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1621
1622 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1623 const gdb_byte *,
1624 unsigned int *);
1625
1626 static const char *read_str_index (const struct die_reader_specs *reader,
1627 ULONGEST str_index);
1628
1629 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1630
1631 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1632 struct dwarf2_cu *);
1633
1634 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1635 unsigned int);
1636
1637 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1638 struct dwarf2_cu *cu);
1639
1640 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1641 struct dwarf2_cu *cu);
1642
1643 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1644
1645 static struct die_info *die_specification (struct die_info *die,
1646 struct dwarf2_cu **);
1647
1648 static line_header_up dwarf_decode_line_header (unsigned int offset,
1649 struct dwarf2_cu *cu);
1650
1651 static void dwarf_decode_lines (struct line_header *, const char *,
1652 struct dwarf2_cu *, struct partial_symtab *,
1653 CORE_ADDR, int decode_mapping);
1654
1655 static void dwarf2_start_subfile (const char *, const char *);
1656
1657 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1658 const char *, const char *,
1659 CORE_ADDR);
1660
1661 static struct symbol *new_symbol (struct die_info *, struct type *,
1662 struct dwarf2_cu *);
1663
1664 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1665 struct dwarf2_cu *, struct symbol *);
1666
1667 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1668 struct dwarf2_cu *);
1669
1670 static void dwarf2_const_value_attr (const struct attribute *attr,
1671 struct type *type,
1672 const char *name,
1673 struct obstack *obstack,
1674 struct dwarf2_cu *cu, LONGEST *value,
1675 const gdb_byte **bytes,
1676 struct dwarf2_locexpr_baton **baton);
1677
1678 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1679
1680 static int need_gnat_info (struct dwarf2_cu *);
1681
1682 static struct type *die_descriptive_type (struct die_info *,
1683 struct dwarf2_cu *);
1684
1685 static void set_descriptive_type (struct type *, struct die_info *,
1686 struct dwarf2_cu *);
1687
1688 static struct type *die_containing_type (struct die_info *,
1689 struct dwarf2_cu *);
1690
1691 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1692 struct dwarf2_cu *);
1693
1694 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1695
1696 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1697
1698 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1699
1700 static char *typename_concat (struct obstack *obs, const char *prefix,
1701 const char *suffix, int physname,
1702 struct dwarf2_cu *cu);
1703
1704 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1705
1706 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1707
1708 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1709
1710 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1711
1712 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1713
1714 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1715 struct dwarf2_cu *, struct partial_symtab *);
1716
1717 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1718 values. Keep the items ordered with increasing constraints compliance. */
1719 enum pc_bounds_kind
1720 {
1721 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1722 PC_BOUNDS_NOT_PRESENT,
1723
1724 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1725 were present but they do not form a valid range of PC addresses. */
1726 PC_BOUNDS_INVALID,
1727
1728 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1729 PC_BOUNDS_RANGES,
1730
1731 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1732 PC_BOUNDS_HIGH_LOW,
1733 };
1734
1735 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1736 CORE_ADDR *, CORE_ADDR *,
1737 struct dwarf2_cu *,
1738 struct partial_symtab *);
1739
1740 static void get_scope_pc_bounds (struct die_info *,
1741 CORE_ADDR *, CORE_ADDR *,
1742 struct dwarf2_cu *);
1743
1744 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1745 CORE_ADDR, struct dwarf2_cu *);
1746
1747 static void dwarf2_add_field (struct field_info *, struct die_info *,
1748 struct dwarf2_cu *);
1749
1750 static void dwarf2_attach_fields_to_type (struct field_info *,
1751 struct type *, struct dwarf2_cu *);
1752
1753 static void dwarf2_add_member_fn (struct field_info *,
1754 struct die_info *, struct type *,
1755 struct dwarf2_cu *);
1756
1757 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1758 struct type *,
1759 struct dwarf2_cu *);
1760
1761 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1762
1763 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1764
1765 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1766
1767 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1768
1769 static struct using_direct **using_directives (enum language);
1770
1771 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1772
1773 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1774
1775 static struct type *read_module_type (struct die_info *die,
1776 struct dwarf2_cu *cu);
1777
1778 static const char *namespace_name (struct die_info *die,
1779 int *is_anonymous, struct dwarf2_cu *);
1780
1781 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1782
1783 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1784
1785 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1786 struct dwarf2_cu *);
1787
1788 static struct die_info *read_die_and_siblings_1
1789 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1790 struct die_info *);
1791
1792 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1793 const gdb_byte *info_ptr,
1794 const gdb_byte **new_info_ptr,
1795 struct die_info *parent);
1796
1797 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1798 struct die_info **, const gdb_byte *,
1799 int *, int);
1800
1801 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1802 struct die_info **, const gdb_byte *,
1803 int *);
1804
1805 static void process_die (struct die_info *, struct dwarf2_cu *);
1806
1807 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1808 struct obstack *);
1809
1810 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1811
1812 static const char *dwarf2_full_name (const char *name,
1813 struct die_info *die,
1814 struct dwarf2_cu *cu);
1815
1816 static const char *dwarf2_physname (const char *name, struct die_info *die,
1817 struct dwarf2_cu *cu);
1818
1819 static struct die_info *dwarf2_extension (struct die_info *die,
1820 struct dwarf2_cu **);
1821
1822 static const char *dwarf_tag_name (unsigned int);
1823
1824 static const char *dwarf_attr_name (unsigned int);
1825
1826 static const char *dwarf_form_name (unsigned int);
1827
1828 static char *dwarf_bool_name (unsigned int);
1829
1830 static const char *dwarf_type_encoding_name (unsigned int);
1831
1832 static struct die_info *sibling_die (struct die_info *);
1833
1834 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1835
1836 static void dump_die_for_error (struct die_info *);
1837
1838 static void dump_die_1 (struct ui_file *, int level, int max_level,
1839 struct die_info *);
1840
1841 /*static*/ void dump_die (struct die_info *, int max_level);
1842
1843 static void store_in_ref_table (struct die_info *,
1844 struct dwarf2_cu *);
1845
1846 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1847
1848 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1849
1850 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1851 const struct attribute *,
1852 struct dwarf2_cu **);
1853
1854 static struct die_info *follow_die_ref (struct die_info *,
1855 const struct attribute *,
1856 struct dwarf2_cu **);
1857
1858 static struct die_info *follow_die_sig (struct die_info *,
1859 const struct attribute *,
1860 struct dwarf2_cu **);
1861
1862 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1863 struct dwarf2_cu *);
1864
1865 static struct type *get_DW_AT_signature_type (struct die_info *,
1866 const struct attribute *,
1867 struct dwarf2_cu *);
1868
1869 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1870
1871 static void read_signatured_type (struct signatured_type *);
1872
1873 static int attr_to_dynamic_prop (const struct attribute *attr,
1874 struct die_info *die, struct dwarf2_cu *cu,
1875 struct dynamic_prop *prop);
1876
1877 /* memory allocation interface */
1878
1879 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1880
1881 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1882
1883 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1884
1885 static int attr_form_is_block (const struct attribute *);
1886
1887 static int attr_form_is_section_offset (const struct attribute *);
1888
1889 static int attr_form_is_constant (const struct attribute *);
1890
1891 static int attr_form_is_ref (const struct attribute *);
1892
1893 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1894 struct dwarf2_loclist_baton *baton,
1895 const struct attribute *attr);
1896
1897 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1898 struct symbol *sym,
1899 struct dwarf2_cu *cu,
1900 int is_block);
1901
1902 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1903 const gdb_byte *info_ptr,
1904 struct abbrev_info *abbrev);
1905
1906 static void free_stack_comp_unit (void *);
1907
1908 static hashval_t partial_die_hash (const void *item);
1909
1910 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1911
1912 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1913 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1914
1915 static void init_one_comp_unit (struct dwarf2_cu *cu,
1916 struct dwarf2_per_cu_data *per_cu);
1917
1918 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1919 struct die_info *comp_unit_die,
1920 enum language pretend_language);
1921
1922 static void free_heap_comp_unit (void *);
1923
1924 static void free_cached_comp_units (void *);
1925
1926 static void age_cached_comp_units (void);
1927
1928 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1929
1930 static struct type *set_die_type (struct die_info *, struct type *,
1931 struct dwarf2_cu *);
1932
1933 static void create_all_comp_units (struct objfile *);
1934
1935 static int create_all_type_units (struct objfile *);
1936
1937 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1938 enum language);
1939
1940 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1941 enum language);
1942
1943 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1944 enum language);
1945
1946 static void dwarf2_add_dependence (struct dwarf2_cu *,
1947 struct dwarf2_per_cu_data *);
1948
1949 static void dwarf2_mark (struct dwarf2_cu *);
1950
1951 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1952
1953 static struct type *get_die_type_at_offset (sect_offset,
1954 struct dwarf2_per_cu_data *);
1955
1956 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1957
1958 static void dwarf2_release_queue (void *dummy);
1959
1960 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1961 enum language pretend_language);
1962
1963 static void process_queue (void);
1964
1965 /* The return type of find_file_and_directory. Note, the enclosed
1966 string pointers are only valid while this object is valid. */
1967
1968 struct file_and_directory
1969 {
1970 /* The filename. This is never NULL. */
1971 const char *name;
1972
1973 /* The compilation directory. NULL if not known. If we needed to
1974 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1975 points directly to the DW_AT_comp_dir string attribute owned by
1976 the obstack that owns the DIE. */
1977 const char *comp_dir;
1978
1979 /* If we needed to build a new string for comp_dir, this is what
1980 owns the storage. */
1981 std::string comp_dir_storage;
1982 };
1983
1984 static file_and_directory find_file_and_directory (struct die_info *die,
1985 struct dwarf2_cu *cu);
1986
1987 static char *file_full_name (int file, struct line_header *lh,
1988 const char *comp_dir);
1989
1990 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1991 enum class rcuh_kind { COMPILE, TYPE };
1992
1993 static const gdb_byte *read_and_check_comp_unit_head
1994 (struct comp_unit_head *header,
1995 struct dwarf2_section_info *section,
1996 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1997 rcuh_kind section_kind);
1998
1999 static void init_cutu_and_read_dies
2000 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
2001 int use_existing_cu, int keep,
2002 die_reader_func_ftype *die_reader_func, void *data);
2003
2004 static void init_cutu_and_read_dies_simple
2005 (struct dwarf2_per_cu_data *this_cu,
2006 die_reader_func_ftype *die_reader_func, void *data);
2007
2008 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2009
2010 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2011
2012 static struct dwo_unit *lookup_dwo_unit_in_dwp
2013 (struct dwp_file *dwp_file, const char *comp_dir,
2014 ULONGEST signature, int is_debug_types);
2015
2016 static struct dwp_file *get_dwp_file (void);
2017
2018 static struct dwo_unit *lookup_dwo_comp_unit
2019 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2020
2021 static struct dwo_unit *lookup_dwo_type_unit
2022 (struct signatured_type *, const char *, const char *);
2023
2024 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2025
2026 static void free_dwo_file_cleanup (void *);
2027
2028 static void process_cu_includes (void);
2029
2030 static void check_producer (struct dwarf2_cu *cu);
2031
2032 static void free_line_header_voidp (void *arg);
2033 \f
2034 /* Various complaints about symbol reading that don't abort the process. */
2035
2036 static void
2037 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2038 {
2039 complaint (&symfile_complaints,
2040 _("statement list doesn't fit in .debug_line section"));
2041 }
2042
2043 static void
2044 dwarf2_debug_line_missing_file_complaint (void)
2045 {
2046 complaint (&symfile_complaints,
2047 _(".debug_line section has line data without a file"));
2048 }
2049
2050 static void
2051 dwarf2_debug_line_missing_end_sequence_complaint (void)
2052 {
2053 complaint (&symfile_complaints,
2054 _(".debug_line section has line "
2055 "program sequence without an end"));
2056 }
2057
2058 static void
2059 dwarf2_complex_location_expr_complaint (void)
2060 {
2061 complaint (&symfile_complaints, _("location expression too complex"));
2062 }
2063
2064 static void
2065 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2066 int arg3)
2067 {
2068 complaint (&symfile_complaints,
2069 _("const value length mismatch for '%s', got %d, expected %d"),
2070 arg1, arg2, arg3);
2071 }
2072
2073 static void
2074 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2075 {
2076 complaint (&symfile_complaints,
2077 _("debug info runs off end of %s section"
2078 " [in module %s]"),
2079 get_section_name (section),
2080 get_section_file_name (section));
2081 }
2082
2083 static void
2084 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2085 {
2086 complaint (&symfile_complaints,
2087 _("macro debug info contains a "
2088 "malformed macro definition:\n`%s'"),
2089 arg1);
2090 }
2091
2092 static void
2093 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2094 {
2095 complaint (&symfile_complaints,
2096 _("invalid attribute class or form for '%s' in '%s'"),
2097 arg1, arg2);
2098 }
2099
2100 /* Hash function for line_header_hash. */
2101
2102 static hashval_t
2103 line_header_hash (const struct line_header *ofs)
2104 {
2105 return ofs->offset.sect_off ^ ofs->offset_in_dwz;
2106 }
2107
2108 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2109
2110 static hashval_t
2111 line_header_hash_voidp (const void *item)
2112 {
2113 const struct line_header *ofs = (const struct line_header *) item;
2114
2115 return line_header_hash (ofs);
2116 }
2117
2118 /* Equality function for line_header_hash. */
2119
2120 static int
2121 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2122 {
2123 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2124 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2125
2126 return (ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off
2127 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2128 }
2129
2130 \f
2131 #if WORDS_BIGENDIAN
2132
2133 /* Convert VALUE between big- and little-endian. */
2134 static offset_type
2135 byte_swap (offset_type value)
2136 {
2137 offset_type result;
2138
2139 result = (value & 0xff) << 24;
2140 result |= (value & 0xff00) << 8;
2141 result |= (value & 0xff0000) >> 8;
2142 result |= (value & 0xff000000) >> 24;
2143 return result;
2144 }
2145
2146 #define MAYBE_SWAP(V) byte_swap (V)
2147
2148 #else
2149 #define MAYBE_SWAP(V) (V)
2150 #endif /* WORDS_BIGENDIAN */
2151
2152 /* Read the given attribute value as an address, taking the attribute's
2153 form into account. */
2154
2155 static CORE_ADDR
2156 attr_value_as_address (struct attribute *attr)
2157 {
2158 CORE_ADDR addr;
2159
2160 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2161 {
2162 /* Aside from a few clearly defined exceptions, attributes that
2163 contain an address must always be in DW_FORM_addr form.
2164 Unfortunately, some compilers happen to be violating this
2165 requirement by encoding addresses using other forms, such
2166 as DW_FORM_data4 for example. For those broken compilers,
2167 we try to do our best, without any guarantee of success,
2168 to interpret the address correctly. It would also be nice
2169 to generate a complaint, but that would require us to maintain
2170 a list of legitimate cases where a non-address form is allowed,
2171 as well as update callers to pass in at least the CU's DWARF
2172 version. This is more overhead than what we're willing to
2173 expand for a pretty rare case. */
2174 addr = DW_UNSND (attr);
2175 }
2176 else
2177 addr = DW_ADDR (attr);
2178
2179 return addr;
2180 }
2181
2182 /* The suffix for an index file. */
2183 #define INDEX_SUFFIX ".gdb-index"
2184
2185 /* Try to locate the sections we need for DWARF 2 debugging
2186 information and return true if we have enough to do something.
2187 NAMES points to the dwarf2 section names, or is NULL if the standard
2188 ELF names are used. */
2189
2190 int
2191 dwarf2_has_info (struct objfile *objfile,
2192 const struct dwarf2_debug_sections *names)
2193 {
2194 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
2195 objfile_data (objfile, dwarf2_objfile_data_key));
2196 if (!dwarf2_per_objfile)
2197 {
2198 /* Initialize per-objfile state. */
2199 struct dwarf2_per_objfile *data
2200 = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_objfile);
2201
2202 memset (data, 0, sizeof (*data));
2203 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
2204 dwarf2_per_objfile = data;
2205
2206 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
2207 (void *) names);
2208 dwarf2_per_objfile->objfile = objfile;
2209 }
2210 return (!dwarf2_per_objfile->info.is_virtual
2211 && dwarf2_per_objfile->info.s.section != NULL
2212 && !dwarf2_per_objfile->abbrev.is_virtual
2213 && dwarf2_per_objfile->abbrev.s.section != NULL);
2214 }
2215
2216 /* Return the containing section of virtual section SECTION. */
2217
2218 static struct dwarf2_section_info *
2219 get_containing_section (const struct dwarf2_section_info *section)
2220 {
2221 gdb_assert (section->is_virtual);
2222 return section->s.containing_section;
2223 }
2224
2225 /* Return the bfd owner of SECTION. */
2226
2227 static struct bfd *
2228 get_section_bfd_owner (const struct dwarf2_section_info *section)
2229 {
2230 if (section->is_virtual)
2231 {
2232 section = get_containing_section (section);
2233 gdb_assert (!section->is_virtual);
2234 }
2235 return section->s.section->owner;
2236 }
2237
2238 /* Return the bfd section of SECTION.
2239 Returns NULL if the section is not present. */
2240
2241 static asection *
2242 get_section_bfd_section (const struct dwarf2_section_info *section)
2243 {
2244 if (section->is_virtual)
2245 {
2246 section = get_containing_section (section);
2247 gdb_assert (!section->is_virtual);
2248 }
2249 return section->s.section;
2250 }
2251
2252 /* Return the name of SECTION. */
2253
2254 static const char *
2255 get_section_name (const struct dwarf2_section_info *section)
2256 {
2257 asection *sectp = get_section_bfd_section (section);
2258
2259 gdb_assert (sectp != NULL);
2260 return bfd_section_name (get_section_bfd_owner (section), sectp);
2261 }
2262
2263 /* Return the name of the file SECTION is in. */
2264
2265 static const char *
2266 get_section_file_name (const struct dwarf2_section_info *section)
2267 {
2268 bfd *abfd = get_section_bfd_owner (section);
2269
2270 return bfd_get_filename (abfd);
2271 }
2272
2273 /* Return the id of SECTION.
2274 Returns 0 if SECTION doesn't exist. */
2275
2276 static int
2277 get_section_id (const struct dwarf2_section_info *section)
2278 {
2279 asection *sectp = get_section_bfd_section (section);
2280
2281 if (sectp == NULL)
2282 return 0;
2283 return sectp->id;
2284 }
2285
2286 /* Return the flags of SECTION.
2287 SECTION (or containing section if this is a virtual section) must exist. */
2288
2289 static int
2290 get_section_flags (const struct dwarf2_section_info *section)
2291 {
2292 asection *sectp = get_section_bfd_section (section);
2293
2294 gdb_assert (sectp != NULL);
2295 return bfd_get_section_flags (sectp->owner, sectp);
2296 }
2297
2298 /* When loading sections, we look either for uncompressed section or for
2299 compressed section names. */
2300
2301 static int
2302 section_is_p (const char *section_name,
2303 const struct dwarf2_section_names *names)
2304 {
2305 if (names->normal != NULL
2306 && strcmp (section_name, names->normal) == 0)
2307 return 1;
2308 if (names->compressed != NULL
2309 && strcmp (section_name, names->compressed) == 0)
2310 return 1;
2311 return 0;
2312 }
2313
2314 /* This function is mapped across the sections and remembers the
2315 offset and size of each of the debugging sections we are interested
2316 in. */
2317
2318 static void
2319 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
2320 {
2321 const struct dwarf2_debug_sections *names;
2322 flagword aflag = bfd_get_section_flags (abfd, sectp);
2323
2324 if (vnames == NULL)
2325 names = &dwarf2_elf_names;
2326 else
2327 names = (const struct dwarf2_debug_sections *) vnames;
2328
2329 if ((aflag & SEC_HAS_CONTENTS) == 0)
2330 {
2331 }
2332 else if (section_is_p (sectp->name, &names->info))
2333 {
2334 dwarf2_per_objfile->info.s.section = sectp;
2335 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
2336 }
2337 else if (section_is_p (sectp->name, &names->abbrev))
2338 {
2339 dwarf2_per_objfile->abbrev.s.section = sectp;
2340 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
2341 }
2342 else if (section_is_p (sectp->name, &names->line))
2343 {
2344 dwarf2_per_objfile->line.s.section = sectp;
2345 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
2346 }
2347 else if (section_is_p (sectp->name, &names->loc))
2348 {
2349 dwarf2_per_objfile->loc.s.section = sectp;
2350 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
2351 }
2352 else if (section_is_p (sectp->name, &names->loclists))
2353 {
2354 dwarf2_per_objfile->loclists.s.section = sectp;
2355 dwarf2_per_objfile->loclists.size = bfd_get_section_size (sectp);
2356 }
2357 else if (section_is_p (sectp->name, &names->macinfo))
2358 {
2359 dwarf2_per_objfile->macinfo.s.section = sectp;
2360 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
2361 }
2362 else if (section_is_p (sectp->name, &names->macro))
2363 {
2364 dwarf2_per_objfile->macro.s.section = sectp;
2365 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
2366 }
2367 else if (section_is_p (sectp->name, &names->str))
2368 {
2369 dwarf2_per_objfile->str.s.section = sectp;
2370 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
2371 }
2372 else if (section_is_p (sectp->name, &names->line_str))
2373 {
2374 dwarf2_per_objfile->line_str.s.section = sectp;
2375 dwarf2_per_objfile->line_str.size = bfd_get_section_size (sectp);
2376 }
2377 else if (section_is_p (sectp->name, &names->addr))
2378 {
2379 dwarf2_per_objfile->addr.s.section = sectp;
2380 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
2381 }
2382 else if (section_is_p (sectp->name, &names->frame))
2383 {
2384 dwarf2_per_objfile->frame.s.section = sectp;
2385 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
2386 }
2387 else if (section_is_p (sectp->name, &names->eh_frame))
2388 {
2389 dwarf2_per_objfile->eh_frame.s.section = sectp;
2390 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
2391 }
2392 else if (section_is_p (sectp->name, &names->ranges))
2393 {
2394 dwarf2_per_objfile->ranges.s.section = sectp;
2395 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
2396 }
2397 else if (section_is_p (sectp->name, &names->rnglists))
2398 {
2399 dwarf2_per_objfile->rnglists.s.section = sectp;
2400 dwarf2_per_objfile->rnglists.size = bfd_get_section_size (sectp);
2401 }
2402 else if (section_is_p (sectp->name, &names->types))
2403 {
2404 struct dwarf2_section_info type_section;
2405
2406 memset (&type_section, 0, sizeof (type_section));
2407 type_section.s.section = sectp;
2408 type_section.size = bfd_get_section_size (sectp);
2409
2410 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
2411 &type_section);
2412 }
2413 else if (section_is_p (sectp->name, &names->gdb_index))
2414 {
2415 dwarf2_per_objfile->gdb_index.s.section = sectp;
2416 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
2417 }
2418
2419 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2420 && bfd_section_vma (abfd, sectp) == 0)
2421 dwarf2_per_objfile->has_section_at_zero = 1;
2422 }
2423
2424 /* A helper function that decides whether a section is empty,
2425 or not present. */
2426
2427 static int
2428 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2429 {
2430 if (section->is_virtual)
2431 return section->size == 0;
2432 return section->s.section == NULL || section->size == 0;
2433 }
2434
2435 /* Read the contents of the section INFO.
2436 OBJFILE is the main object file, but not necessarily the file where
2437 the section comes from. E.g., for DWO files the bfd of INFO is the bfd
2438 of the DWO file.
2439 If the section is compressed, uncompress it before returning. */
2440
2441 static void
2442 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
2443 {
2444 asection *sectp;
2445 bfd *abfd;
2446 gdb_byte *buf, *retbuf;
2447
2448 if (info->readin)
2449 return;
2450 info->buffer = NULL;
2451 info->readin = 1;
2452
2453 if (dwarf2_section_empty_p (info))
2454 return;
2455
2456 sectp = get_section_bfd_section (info);
2457
2458 /* If this is a virtual section we need to read in the real one first. */
2459 if (info->is_virtual)
2460 {
2461 struct dwarf2_section_info *containing_section =
2462 get_containing_section (info);
2463
2464 gdb_assert (sectp != NULL);
2465 if ((sectp->flags & SEC_RELOC) != 0)
2466 {
2467 error (_("Dwarf Error: DWP format V2 with relocations is not"
2468 " supported in section %s [in module %s]"),
2469 get_section_name (info), get_section_file_name (info));
2470 }
2471 dwarf2_read_section (objfile, containing_section);
2472 /* Other code should have already caught virtual sections that don't
2473 fit. */
2474 gdb_assert (info->virtual_offset + info->size
2475 <= containing_section->size);
2476 /* If the real section is empty or there was a problem reading the
2477 section we shouldn't get here. */
2478 gdb_assert (containing_section->buffer != NULL);
2479 info->buffer = containing_section->buffer + info->virtual_offset;
2480 return;
2481 }
2482
2483 /* If the section has relocations, we must read it ourselves.
2484 Otherwise we attach it to the BFD. */
2485 if ((sectp->flags & SEC_RELOC) == 0)
2486 {
2487 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2488 return;
2489 }
2490
2491 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2492 info->buffer = buf;
2493
2494 /* When debugging .o files, we may need to apply relocations; see
2495 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2496 We never compress sections in .o files, so we only need to
2497 try this when the section is not compressed. */
2498 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2499 if (retbuf != NULL)
2500 {
2501 info->buffer = retbuf;
2502 return;
2503 }
2504
2505 abfd = get_section_bfd_owner (info);
2506 gdb_assert (abfd != NULL);
2507
2508 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2509 || bfd_bread (buf, info->size, abfd) != info->size)
2510 {
2511 error (_("Dwarf Error: Can't read DWARF data"
2512 " in section %s [in module %s]"),
2513 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2514 }
2515 }
2516
2517 /* A helper function that returns the size of a section in a safe way.
2518 If you are positive that the section has been read before using the
2519 size, then it is safe to refer to the dwarf2_section_info object's
2520 "size" field directly. In other cases, you must call this
2521 function, because for compressed sections the size field is not set
2522 correctly until the section has been read. */
2523
2524 static bfd_size_type
2525 dwarf2_section_size (struct objfile *objfile,
2526 struct dwarf2_section_info *info)
2527 {
2528 if (!info->readin)
2529 dwarf2_read_section (objfile, info);
2530 return info->size;
2531 }
2532
2533 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2534 SECTION_NAME. */
2535
2536 void
2537 dwarf2_get_section_info (struct objfile *objfile,
2538 enum dwarf2_section_enum sect,
2539 asection **sectp, const gdb_byte **bufp,
2540 bfd_size_type *sizep)
2541 {
2542 struct dwarf2_per_objfile *data
2543 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2544 dwarf2_objfile_data_key);
2545 struct dwarf2_section_info *info;
2546
2547 /* We may see an objfile without any DWARF, in which case we just
2548 return nothing. */
2549 if (data == NULL)
2550 {
2551 *sectp = NULL;
2552 *bufp = NULL;
2553 *sizep = 0;
2554 return;
2555 }
2556 switch (sect)
2557 {
2558 case DWARF2_DEBUG_FRAME:
2559 info = &data->frame;
2560 break;
2561 case DWARF2_EH_FRAME:
2562 info = &data->eh_frame;
2563 break;
2564 default:
2565 gdb_assert_not_reached ("unexpected section");
2566 }
2567
2568 dwarf2_read_section (objfile, info);
2569
2570 *sectp = get_section_bfd_section (info);
2571 *bufp = info->buffer;
2572 *sizep = info->size;
2573 }
2574
2575 /* A helper function to find the sections for a .dwz file. */
2576
2577 static void
2578 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2579 {
2580 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2581
2582 /* Note that we only support the standard ELF names, because .dwz
2583 is ELF-only (at the time of writing). */
2584 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2585 {
2586 dwz_file->abbrev.s.section = sectp;
2587 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2588 }
2589 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2590 {
2591 dwz_file->info.s.section = sectp;
2592 dwz_file->info.size = bfd_get_section_size (sectp);
2593 }
2594 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2595 {
2596 dwz_file->str.s.section = sectp;
2597 dwz_file->str.size = bfd_get_section_size (sectp);
2598 }
2599 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2600 {
2601 dwz_file->line.s.section = sectp;
2602 dwz_file->line.size = bfd_get_section_size (sectp);
2603 }
2604 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2605 {
2606 dwz_file->macro.s.section = sectp;
2607 dwz_file->macro.size = bfd_get_section_size (sectp);
2608 }
2609 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2610 {
2611 dwz_file->gdb_index.s.section = sectp;
2612 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2613 }
2614 }
2615
2616 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2617 there is no .gnu_debugaltlink section in the file. Error if there
2618 is such a section but the file cannot be found. */
2619
2620 static struct dwz_file *
2621 dwarf2_get_dwz_file (void)
2622 {
2623 char *data;
2624 struct cleanup *cleanup;
2625 const char *filename;
2626 struct dwz_file *result;
2627 bfd_size_type buildid_len_arg;
2628 size_t buildid_len;
2629 bfd_byte *buildid;
2630
2631 if (dwarf2_per_objfile->dwz_file != NULL)
2632 return dwarf2_per_objfile->dwz_file;
2633
2634 bfd_set_error (bfd_error_no_error);
2635 data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2636 &buildid_len_arg, &buildid);
2637 if (data == NULL)
2638 {
2639 if (bfd_get_error () == bfd_error_no_error)
2640 return NULL;
2641 error (_("could not read '.gnu_debugaltlink' section: %s"),
2642 bfd_errmsg (bfd_get_error ()));
2643 }
2644 cleanup = make_cleanup (xfree, data);
2645 make_cleanup (xfree, buildid);
2646
2647 buildid_len = (size_t) buildid_len_arg;
2648
2649 filename = (const char *) data;
2650
2651 std::string abs_storage;
2652 if (!IS_ABSOLUTE_PATH (filename))
2653 {
2654 char *abs = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2655
2656 make_cleanup (xfree, abs);
2657 abs_storage = ldirname (abs) + SLASH_STRING + filename;
2658 filename = abs_storage.c_str ();
2659 }
2660
2661 /* First try the file name given in the section. If that doesn't
2662 work, try to use the build-id instead. */
2663 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2664 if (dwz_bfd != NULL)
2665 {
2666 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2667 dwz_bfd.release ();
2668 }
2669
2670 if (dwz_bfd == NULL)
2671 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2672
2673 if (dwz_bfd == NULL)
2674 error (_("could not find '.gnu_debugaltlink' file for %s"),
2675 objfile_name (dwarf2_per_objfile->objfile));
2676
2677 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2678 struct dwz_file);
2679 result->dwz_bfd = dwz_bfd.release ();
2680
2681 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
2682
2683 do_cleanups (cleanup);
2684
2685 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
2686 dwarf2_per_objfile->dwz_file = result;
2687 return result;
2688 }
2689 \f
2690 /* DWARF quick_symbols_functions support. */
2691
2692 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2693 unique line tables, so we maintain a separate table of all .debug_line
2694 derived entries to support the sharing.
2695 All the quick functions need is the list of file names. We discard the
2696 line_header when we're done and don't need to record it here. */
2697 struct quick_file_names
2698 {
2699 /* The data used to construct the hash key. */
2700 struct stmt_list_hash hash;
2701
2702 /* The number of entries in file_names, real_names. */
2703 unsigned int num_file_names;
2704
2705 /* The file names from the line table, after being run through
2706 file_full_name. */
2707 const char **file_names;
2708
2709 /* The file names from the line table after being run through
2710 gdb_realpath. These are computed lazily. */
2711 const char **real_names;
2712 };
2713
2714 /* When using the index (and thus not using psymtabs), each CU has an
2715 object of this type. This is used to hold information needed by
2716 the various "quick" methods. */
2717 struct dwarf2_per_cu_quick_data
2718 {
2719 /* The file table. This can be NULL if there was no file table
2720 or it's currently not read in.
2721 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2722 struct quick_file_names *file_names;
2723
2724 /* The corresponding symbol table. This is NULL if symbols for this
2725 CU have not yet been read. */
2726 struct compunit_symtab *compunit_symtab;
2727
2728 /* A temporary mark bit used when iterating over all CUs in
2729 expand_symtabs_matching. */
2730 unsigned int mark : 1;
2731
2732 /* True if we've tried to read the file table and found there isn't one.
2733 There will be no point in trying to read it again next time. */
2734 unsigned int no_file_data : 1;
2735 };
2736
2737 /* Utility hash function for a stmt_list_hash. */
2738
2739 static hashval_t
2740 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2741 {
2742 hashval_t v = 0;
2743
2744 if (stmt_list_hash->dwo_unit != NULL)
2745 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2746 v += stmt_list_hash->line_offset.sect_off;
2747 return v;
2748 }
2749
2750 /* Utility equality function for a stmt_list_hash. */
2751
2752 static int
2753 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2754 const struct stmt_list_hash *rhs)
2755 {
2756 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2757 return 0;
2758 if (lhs->dwo_unit != NULL
2759 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2760 return 0;
2761
2762 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2763 }
2764
2765 /* Hash function for a quick_file_names. */
2766
2767 static hashval_t
2768 hash_file_name_entry (const void *e)
2769 {
2770 const struct quick_file_names *file_data
2771 = (const struct quick_file_names *) e;
2772
2773 return hash_stmt_list_entry (&file_data->hash);
2774 }
2775
2776 /* Equality function for a quick_file_names. */
2777
2778 static int
2779 eq_file_name_entry (const void *a, const void *b)
2780 {
2781 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2782 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2783
2784 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2785 }
2786
2787 /* Delete function for a quick_file_names. */
2788
2789 static void
2790 delete_file_name_entry (void *e)
2791 {
2792 struct quick_file_names *file_data = (struct quick_file_names *) e;
2793 int i;
2794
2795 for (i = 0; i < file_data->num_file_names; ++i)
2796 {
2797 xfree ((void*) file_data->file_names[i]);
2798 if (file_data->real_names)
2799 xfree ((void*) file_data->real_names[i]);
2800 }
2801
2802 /* The space for the struct itself lives on objfile_obstack,
2803 so we don't free it here. */
2804 }
2805
2806 /* Create a quick_file_names hash table. */
2807
2808 static htab_t
2809 create_quick_file_names_table (unsigned int nr_initial_entries)
2810 {
2811 return htab_create_alloc (nr_initial_entries,
2812 hash_file_name_entry, eq_file_name_entry,
2813 delete_file_name_entry, xcalloc, xfree);
2814 }
2815
2816 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2817 have to be created afterwards. You should call age_cached_comp_units after
2818 processing PER_CU->CU. dw2_setup must have been already called. */
2819
2820 static void
2821 load_cu (struct dwarf2_per_cu_data *per_cu)
2822 {
2823 if (per_cu->is_debug_types)
2824 load_full_type_unit (per_cu);
2825 else
2826 load_full_comp_unit (per_cu, language_minimal);
2827
2828 if (per_cu->cu == NULL)
2829 return; /* Dummy CU. */
2830
2831 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2832 }
2833
2834 /* Read in the symbols for PER_CU. */
2835
2836 static void
2837 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2838 {
2839 struct cleanup *back_to;
2840
2841 /* Skip type_unit_groups, reading the type units they contain
2842 is handled elsewhere. */
2843 if (IS_TYPE_UNIT_GROUP (per_cu))
2844 return;
2845
2846 back_to = make_cleanup (dwarf2_release_queue, NULL);
2847
2848 if (dwarf2_per_objfile->using_index
2849 ? per_cu->v.quick->compunit_symtab == NULL
2850 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2851 {
2852 queue_comp_unit (per_cu, language_minimal);
2853 load_cu (per_cu);
2854
2855 /* If we just loaded a CU from a DWO, and we're working with an index
2856 that may badly handle TUs, load all the TUs in that DWO as well.
2857 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2858 if (!per_cu->is_debug_types
2859 && per_cu->cu != NULL
2860 && per_cu->cu->dwo_unit != NULL
2861 && dwarf2_per_objfile->index_table != NULL
2862 && dwarf2_per_objfile->index_table->version <= 7
2863 /* DWP files aren't supported yet. */
2864 && get_dwp_file () == NULL)
2865 queue_and_load_all_dwo_tus (per_cu);
2866 }
2867
2868 process_queue ();
2869
2870 /* Age the cache, releasing compilation units that have not
2871 been used recently. */
2872 age_cached_comp_units ();
2873
2874 do_cleanups (back_to);
2875 }
2876
2877 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2878 the objfile from which this CU came. Returns the resulting symbol
2879 table. */
2880
2881 static struct compunit_symtab *
2882 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2883 {
2884 gdb_assert (dwarf2_per_objfile->using_index);
2885 if (!per_cu->v.quick->compunit_symtab)
2886 {
2887 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2888 increment_reading_symtab ();
2889 dw2_do_instantiate_symtab (per_cu);
2890 process_cu_includes ();
2891 do_cleanups (back_to);
2892 }
2893
2894 return per_cu->v.quick->compunit_symtab;
2895 }
2896
2897 /* Return the CU/TU given its index.
2898
2899 This is intended for loops like:
2900
2901 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2902 + dwarf2_per_objfile->n_type_units); ++i)
2903 {
2904 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
2905
2906 ...;
2907 }
2908 */
2909
2910 static struct dwarf2_per_cu_data *
2911 dw2_get_cutu (int index)
2912 {
2913 if (index >= dwarf2_per_objfile->n_comp_units)
2914 {
2915 index -= dwarf2_per_objfile->n_comp_units;
2916 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2917 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2918 }
2919
2920 return dwarf2_per_objfile->all_comp_units[index];
2921 }
2922
2923 /* Return the CU given its index.
2924 This differs from dw2_get_cutu in that it's for when you know INDEX
2925 refers to a CU. */
2926
2927 static struct dwarf2_per_cu_data *
2928 dw2_get_cu (int index)
2929 {
2930 gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);
2931
2932 return dwarf2_per_objfile->all_comp_units[index];
2933 }
2934
2935 /* A helper for create_cus_from_index that handles a given list of
2936 CUs. */
2937
2938 static void
2939 create_cus_from_index_list (struct objfile *objfile,
2940 const gdb_byte *cu_list, offset_type n_elements,
2941 struct dwarf2_section_info *section,
2942 int is_dwz,
2943 int base_offset)
2944 {
2945 offset_type i;
2946
2947 for (i = 0; i < n_elements; i += 2)
2948 {
2949 struct dwarf2_per_cu_data *the_cu;
2950 ULONGEST offset, length;
2951
2952 gdb_static_assert (sizeof (ULONGEST) >= 8);
2953 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2954 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2955 cu_list += 2 * 8;
2956
2957 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2958 struct dwarf2_per_cu_data);
2959 the_cu->offset.sect_off = offset;
2960 the_cu->length = length;
2961 the_cu->objfile = objfile;
2962 the_cu->section = section;
2963 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2964 struct dwarf2_per_cu_quick_data);
2965 the_cu->is_dwz = is_dwz;
2966 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2967 }
2968 }
2969
2970 /* Read the CU list from the mapped index, and use it to create all
2971 the CU objects for this objfile. */
2972
2973 static void
2974 create_cus_from_index (struct objfile *objfile,
2975 const gdb_byte *cu_list, offset_type cu_list_elements,
2976 const gdb_byte *dwz_list, offset_type dwz_elements)
2977 {
2978 struct dwz_file *dwz;
2979
2980 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2981 dwarf2_per_objfile->all_comp_units =
2982 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
2983 dwarf2_per_objfile->n_comp_units);
2984
2985 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2986 &dwarf2_per_objfile->info, 0, 0);
2987
2988 if (dwz_elements == 0)
2989 return;
2990
2991 dwz = dwarf2_get_dwz_file ();
2992 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2993 cu_list_elements / 2);
2994 }
2995
2996 /* Create the signatured type hash table from the index. */
2997
2998 static void
2999 create_signatured_type_table_from_index (struct objfile *objfile,
3000 struct dwarf2_section_info *section,
3001 const gdb_byte *bytes,
3002 offset_type elements)
3003 {
3004 offset_type i;
3005 htab_t sig_types_hash;
3006
3007 dwarf2_per_objfile->n_type_units
3008 = dwarf2_per_objfile->n_allocated_type_units
3009 = elements / 3;
3010 dwarf2_per_objfile->all_type_units =
3011 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3012
3013 sig_types_hash = allocate_signatured_type_table (objfile);
3014
3015 for (i = 0; i < elements; i += 3)
3016 {
3017 struct signatured_type *sig_type;
3018 ULONGEST offset, type_offset_in_tu, signature;
3019 void **slot;
3020
3021 gdb_static_assert (sizeof (ULONGEST) >= 8);
3022 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3023 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
3024 BFD_ENDIAN_LITTLE);
3025 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3026 bytes += 3 * 8;
3027
3028 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3029 struct signatured_type);
3030 sig_type->signature = signature;
3031 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
3032 sig_type->per_cu.is_debug_types = 1;
3033 sig_type->per_cu.section = section;
3034 sig_type->per_cu.offset.sect_off = offset;
3035 sig_type->per_cu.objfile = objfile;
3036 sig_type->per_cu.v.quick
3037 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3038 struct dwarf2_per_cu_quick_data);
3039
3040 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3041 *slot = sig_type;
3042
3043 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3044 }
3045
3046 dwarf2_per_objfile->signatured_types = sig_types_hash;
3047 }
3048
3049 /* Read the address map data from the mapped index, and use it to
3050 populate the objfile's psymtabs_addrmap. */
3051
3052 static void
3053 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
3054 {
3055 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3056 const gdb_byte *iter, *end;
3057 struct obstack temp_obstack;
3058 struct addrmap *mutable_map;
3059 struct cleanup *cleanup;
3060 CORE_ADDR baseaddr;
3061
3062 obstack_init (&temp_obstack);
3063 cleanup = make_cleanup_obstack_free (&temp_obstack);
3064 mutable_map = addrmap_create_mutable (&temp_obstack);
3065
3066 iter = index->address_table;
3067 end = iter + index->address_table_size;
3068
3069 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3070
3071 while (iter < end)
3072 {
3073 ULONGEST hi, lo, cu_index;
3074 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3075 iter += 8;
3076 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3077 iter += 8;
3078 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3079 iter += 4;
3080
3081 if (lo > hi)
3082 {
3083 complaint (&symfile_complaints,
3084 _(".gdb_index address table has invalid range (%s - %s)"),
3085 hex_string (lo), hex_string (hi));
3086 continue;
3087 }
3088
3089 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3090 {
3091 complaint (&symfile_complaints,
3092 _(".gdb_index address table has invalid CU number %u"),
3093 (unsigned) cu_index);
3094 continue;
3095 }
3096
3097 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3098 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3099 addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
3100 }
3101
3102 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3103 &objfile->objfile_obstack);
3104 do_cleanups (cleanup);
3105 }
3106
3107 /* The hash function for strings in the mapped index. This is the same as
3108 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
3109 implementation. This is necessary because the hash function is tied to the
3110 format of the mapped index file. The hash values do not have to match with
3111 SYMBOL_HASH_NEXT.
3112
3113 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
3114
3115 static hashval_t
3116 mapped_index_string_hash (int index_version, const void *p)
3117 {
3118 const unsigned char *str = (const unsigned char *) p;
3119 hashval_t r = 0;
3120 unsigned char c;
3121
3122 while ((c = *str++) != 0)
3123 {
3124 if (index_version >= 5)
3125 c = tolower (c);
3126 r = r * 67 + c - 113;
3127 }
3128
3129 return r;
3130 }
3131
3132 /* Find a slot in the mapped index INDEX for the object named NAME.
3133 If NAME is found, set *VEC_OUT to point to the CU vector in the
3134 constant pool and return 1. If NAME cannot be found, return 0. */
3135
3136 static int
3137 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3138 offset_type **vec_out)
3139 {
3140 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
3141 offset_type hash;
3142 offset_type slot, step;
3143 int (*cmp) (const char *, const char *);
3144
3145 if (current_language->la_language == language_cplus
3146 || current_language->la_language == language_fortran
3147 || current_language->la_language == language_d)
3148 {
3149 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3150 not contain any. */
3151
3152 if (strchr (name, '(') != NULL)
3153 {
3154 char *without_params = cp_remove_params (name);
3155
3156 if (without_params != NULL)
3157 {
3158 make_cleanup (xfree, without_params);
3159 name = without_params;
3160 }
3161 }
3162 }
3163
3164 /* Index version 4 did not support case insensitive searches. But the
3165 indices for case insensitive languages are built in lowercase, therefore
3166 simulate our NAME being searched is also lowercased. */
3167 hash = mapped_index_string_hash ((index->version == 4
3168 && case_sensitivity == case_sensitive_off
3169 ? 5 : index->version),
3170 name);
3171
3172 slot = hash & (index->symbol_table_slots - 1);
3173 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
3174 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3175
3176 for (;;)
3177 {
3178 /* Convert a slot number to an offset into the table. */
3179 offset_type i = 2 * slot;
3180 const char *str;
3181 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
3182 {
3183 do_cleanups (back_to);
3184 return 0;
3185 }
3186
3187 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
3188 if (!cmp (name, str))
3189 {
3190 *vec_out = (offset_type *) (index->constant_pool
3191 + MAYBE_SWAP (index->symbol_table[i + 1]));
3192 do_cleanups (back_to);
3193 return 1;
3194 }
3195
3196 slot = (slot + step) & (index->symbol_table_slots - 1);
3197 }
3198 }
3199
3200 /* A helper function that reads the .gdb_index from SECTION and fills
3201 in MAP. FILENAME is the name of the file containing the section;
3202 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3203 ok to use deprecated sections.
3204
3205 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3206 out parameters that are filled in with information about the CU and
3207 TU lists in the section.
3208
3209 Returns 1 if all went well, 0 otherwise. */
3210
3211 static int
3212 read_index_from_section (struct objfile *objfile,
3213 const char *filename,
3214 int deprecated_ok,
3215 struct dwarf2_section_info *section,
3216 struct mapped_index *map,
3217 const gdb_byte **cu_list,
3218 offset_type *cu_list_elements,
3219 const gdb_byte **types_list,
3220 offset_type *types_list_elements)
3221 {
3222 const gdb_byte *addr;
3223 offset_type version;
3224 offset_type *metadata;
3225 int i;
3226
3227 if (dwarf2_section_empty_p (section))
3228 return 0;
3229
3230 /* Older elfutils strip versions could keep the section in the main
3231 executable while splitting it for the separate debug info file. */
3232 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3233 return 0;
3234
3235 dwarf2_read_section (objfile, section);
3236
3237 addr = section->buffer;
3238 /* Version check. */
3239 version = MAYBE_SWAP (*(offset_type *) addr);
3240 /* Versions earlier than 3 emitted every copy of a psymbol. This
3241 causes the index to behave very poorly for certain requests. Version 3
3242 contained incomplete addrmap. So, it seems better to just ignore such
3243 indices. */
3244 if (version < 4)
3245 {
3246 static int warning_printed = 0;
3247 if (!warning_printed)
3248 {
3249 warning (_("Skipping obsolete .gdb_index section in %s."),
3250 filename);
3251 warning_printed = 1;
3252 }
3253 return 0;
3254 }
3255 /* Index version 4 uses a different hash function than index version
3256 5 and later.
3257
3258 Versions earlier than 6 did not emit psymbols for inlined
3259 functions. Using these files will cause GDB not to be able to
3260 set breakpoints on inlined functions by name, so we ignore these
3261 indices unless the user has done
3262 "set use-deprecated-index-sections on". */
3263 if (version < 6 && !deprecated_ok)
3264 {
3265 static int warning_printed = 0;
3266 if (!warning_printed)
3267 {
3268 warning (_("\
3269 Skipping deprecated .gdb_index section in %s.\n\
3270 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3271 to use the section anyway."),
3272 filename);
3273 warning_printed = 1;
3274 }
3275 return 0;
3276 }
3277 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3278 of the TU (for symbols coming from TUs),
3279 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3280 Plus gold-generated indices can have duplicate entries for global symbols,
3281 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3282 These are just performance bugs, and we can't distinguish gdb-generated
3283 indices from gold-generated ones, so issue no warning here. */
3284
3285 /* Indexes with higher version than the one supported by GDB may be no
3286 longer backward compatible. */
3287 if (version > 8)
3288 return 0;
3289
3290 map->version = version;
3291 map->total_size = section->size;
3292
3293 metadata = (offset_type *) (addr + sizeof (offset_type));
3294
3295 i = 0;
3296 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3297 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3298 / 8);
3299 ++i;
3300
3301 *types_list = addr + MAYBE_SWAP (metadata[i]);
3302 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3303 - MAYBE_SWAP (metadata[i]))
3304 / 8);
3305 ++i;
3306
3307 map->address_table = addr + MAYBE_SWAP (metadata[i]);
3308 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
3309 - MAYBE_SWAP (metadata[i]));
3310 ++i;
3311
3312 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
3313 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
3314 - MAYBE_SWAP (metadata[i]))
3315 / (2 * sizeof (offset_type)));
3316 ++i;
3317
3318 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3319
3320 return 1;
3321 }
3322
3323
3324 /* Read the index file. If everything went ok, initialize the "quick"
3325 elements of all the CUs and return 1. Otherwise, return 0. */
3326
3327 static int
3328 dwarf2_read_index (struct objfile *objfile)
3329 {
3330 struct mapped_index local_map, *map;
3331 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3332 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3333 struct dwz_file *dwz;
3334
3335 if (!read_index_from_section (objfile, objfile_name (objfile),
3336 use_deprecated_index_sections,
3337 &dwarf2_per_objfile->gdb_index, &local_map,
3338 &cu_list, &cu_list_elements,
3339 &types_list, &types_list_elements))
3340 return 0;
3341
3342 /* Don't use the index if it's empty. */
3343 if (local_map.symbol_table_slots == 0)
3344 return 0;
3345
3346 /* If there is a .dwz file, read it so we can get its CU list as
3347 well. */
3348 dwz = dwarf2_get_dwz_file ();
3349 if (dwz != NULL)
3350 {
3351 struct mapped_index dwz_map;
3352 const gdb_byte *dwz_types_ignore;
3353 offset_type dwz_types_elements_ignore;
3354
3355 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3356 1,
3357 &dwz->gdb_index, &dwz_map,
3358 &dwz_list, &dwz_list_elements,
3359 &dwz_types_ignore,
3360 &dwz_types_elements_ignore))
3361 {
3362 warning (_("could not read '.gdb_index' section from %s; skipping"),
3363 bfd_get_filename (dwz->dwz_bfd));
3364 return 0;
3365 }
3366 }
3367
3368 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
3369 dwz_list_elements);
3370
3371 if (types_list_elements)
3372 {
3373 struct dwarf2_section_info *section;
3374
3375 /* We can only handle a single .debug_types when we have an
3376 index. */
3377 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3378 return 0;
3379
3380 section = VEC_index (dwarf2_section_info_def,
3381 dwarf2_per_objfile->types, 0);
3382
3383 create_signatured_type_table_from_index (objfile, section, types_list,
3384 types_list_elements);
3385 }
3386
3387 create_addrmap_from_index (objfile, &local_map);
3388
3389 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3390 *map = local_map;
3391
3392 dwarf2_per_objfile->index_table = map;
3393 dwarf2_per_objfile->using_index = 1;
3394 dwarf2_per_objfile->quick_file_names_table =
3395 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3396
3397 return 1;
3398 }
3399
3400 /* A helper for the "quick" functions which sets the global
3401 dwarf2_per_objfile according to OBJFILE. */
3402
3403 static void
3404 dw2_setup (struct objfile *objfile)
3405 {
3406 dwarf2_per_objfile = ((struct dwarf2_per_objfile *)
3407 objfile_data (objfile, dwarf2_objfile_data_key));
3408 gdb_assert (dwarf2_per_objfile);
3409 }
3410
3411 /* die_reader_func for dw2_get_file_names. */
3412
3413 static void
3414 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3415 const gdb_byte *info_ptr,
3416 struct die_info *comp_unit_die,
3417 int has_children,
3418 void *data)
3419 {
3420 struct dwarf2_cu *cu = reader->cu;
3421 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3422 struct objfile *objfile = dwarf2_per_objfile->objfile;
3423 struct dwarf2_per_cu_data *lh_cu;
3424 struct attribute *attr;
3425 int i;
3426 void **slot;
3427 struct quick_file_names *qfn;
3428 unsigned int line_offset;
3429
3430 gdb_assert (! this_cu->is_debug_types);
3431
3432 /* Our callers never want to match partial units -- instead they
3433 will match the enclosing full CU. */
3434 if (comp_unit_die->tag == DW_TAG_partial_unit)
3435 {
3436 this_cu->v.quick->no_file_data = 1;
3437 return;
3438 }
3439
3440 lh_cu = this_cu;
3441 slot = NULL;
3442 line_offset = 0;
3443
3444 line_header_up lh;
3445
3446 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3447 if (attr)
3448 {
3449 struct quick_file_names find_entry;
3450
3451 line_offset = DW_UNSND (attr);
3452
3453 /* We may have already read in this line header (TU line header sharing).
3454 If we have we're done. */
3455 find_entry.hash.dwo_unit = cu->dwo_unit;
3456 find_entry.hash.line_offset.sect_off = line_offset;
3457 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3458 &find_entry, INSERT);
3459 if (*slot != NULL)
3460 {
3461 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3462 return;
3463 }
3464
3465 lh = dwarf_decode_line_header (line_offset, cu);
3466 }
3467 if (lh == NULL)
3468 {
3469 lh_cu->v.quick->no_file_data = 1;
3470 return;
3471 }
3472
3473 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3474 qfn->hash.dwo_unit = cu->dwo_unit;
3475 qfn->hash.line_offset.sect_off = line_offset;
3476 gdb_assert (slot != NULL);
3477 *slot = qfn;
3478
3479 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3480
3481 qfn->num_file_names = lh->file_names.size ();
3482 qfn->file_names =
3483 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3484 for (i = 0; i < lh->file_names.size (); ++i)
3485 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3486 qfn->real_names = NULL;
3487
3488 lh_cu->v.quick->file_names = qfn;
3489 }
3490
3491 /* A helper for the "quick" functions which attempts to read the line
3492 table for THIS_CU. */
3493
3494 static struct quick_file_names *
3495 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3496 {
3497 /* This should never be called for TUs. */
3498 gdb_assert (! this_cu->is_debug_types);
3499 /* Nor type unit groups. */
3500 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3501
3502 if (this_cu->v.quick->file_names != NULL)
3503 return this_cu->v.quick->file_names;
3504 /* If we know there is no line data, no point in looking again. */
3505 if (this_cu->v.quick->no_file_data)
3506 return NULL;
3507
3508 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3509
3510 if (this_cu->v.quick->no_file_data)
3511 return NULL;
3512 return this_cu->v.quick->file_names;
3513 }
3514
3515 /* A helper for the "quick" functions which computes and caches the
3516 real path for a given file name from the line table. */
3517
3518 static const char *
3519 dw2_get_real_path (struct objfile *objfile,
3520 struct quick_file_names *qfn, int index)
3521 {
3522 if (qfn->real_names == NULL)
3523 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3524 qfn->num_file_names, const char *);
3525
3526 if (qfn->real_names[index] == NULL)
3527 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
3528
3529 return qfn->real_names[index];
3530 }
3531
3532 static struct symtab *
3533 dw2_find_last_source_symtab (struct objfile *objfile)
3534 {
3535 struct compunit_symtab *cust;
3536 int index;
3537
3538 dw2_setup (objfile);
3539 index = dwarf2_per_objfile->n_comp_units - 1;
3540 cust = dw2_instantiate_symtab (dw2_get_cutu (index));
3541 if (cust == NULL)
3542 return NULL;
3543 return compunit_primary_filetab (cust);
3544 }
3545
3546 /* Traversal function for dw2_forget_cached_source_info. */
3547
3548 static int
3549 dw2_free_cached_file_names (void **slot, void *info)
3550 {
3551 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3552
3553 if (file_data->real_names)
3554 {
3555 int i;
3556
3557 for (i = 0; i < file_data->num_file_names; ++i)
3558 {
3559 xfree ((void*) file_data->real_names[i]);
3560 file_data->real_names[i] = NULL;
3561 }
3562 }
3563
3564 return 1;
3565 }
3566
3567 static void
3568 dw2_forget_cached_source_info (struct objfile *objfile)
3569 {
3570 dw2_setup (objfile);
3571
3572 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3573 dw2_free_cached_file_names, NULL);
3574 }
3575
3576 /* Helper function for dw2_map_symtabs_matching_filename that expands
3577 the symtabs and calls the iterator. */
3578
3579 static int
3580 dw2_map_expand_apply (struct objfile *objfile,
3581 struct dwarf2_per_cu_data *per_cu,
3582 const char *name, const char *real_path,
3583 gdb::function_view<bool (symtab *)> callback)
3584 {
3585 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3586
3587 /* Don't visit already-expanded CUs. */
3588 if (per_cu->v.quick->compunit_symtab)
3589 return 0;
3590
3591 /* This may expand more than one symtab, and we want to iterate over
3592 all of them. */
3593 dw2_instantiate_symtab (per_cu);
3594
3595 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3596 last_made, callback);
3597 }
3598
3599 /* Implementation of the map_symtabs_matching_filename method. */
3600
3601 static bool
3602 dw2_map_symtabs_matching_filename
3603 (struct objfile *objfile, const char *name, const char *real_path,
3604 gdb::function_view<bool (symtab *)> callback)
3605 {
3606 int i;
3607 const char *name_basename = lbasename (name);
3608
3609 dw2_setup (objfile);
3610
3611 /* The rule is CUs specify all the files, including those used by
3612 any TU, so there's no need to scan TUs here. */
3613
3614 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3615 {
3616 int j;
3617 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3618 struct quick_file_names *file_data;
3619
3620 /* We only need to look at symtabs not already expanded. */
3621 if (per_cu->v.quick->compunit_symtab)
3622 continue;
3623
3624 file_data = dw2_get_file_names (per_cu);
3625 if (file_data == NULL)
3626 continue;
3627
3628 for (j = 0; j < file_data->num_file_names; ++j)
3629 {
3630 const char *this_name = file_data->file_names[j];
3631 const char *this_real_name;
3632
3633 if (compare_filenames_for_search (this_name, name))
3634 {
3635 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3636 callback))
3637 return true;
3638 continue;
3639 }
3640
3641 /* Before we invoke realpath, which can get expensive when many
3642 files are involved, do a quick comparison of the basenames. */
3643 if (! basenames_may_differ
3644 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3645 continue;
3646
3647 this_real_name = dw2_get_real_path (objfile, file_data, j);
3648 if (compare_filenames_for_search (this_real_name, name))
3649 {
3650 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3651 callback))
3652 return true;
3653 continue;
3654 }
3655
3656 if (real_path != NULL)
3657 {
3658 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3659 gdb_assert (IS_ABSOLUTE_PATH (name));
3660 if (this_real_name != NULL
3661 && FILENAME_CMP (real_path, this_real_name) == 0)
3662 {
3663 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3664 callback))
3665 return true;
3666 continue;
3667 }
3668 }
3669 }
3670 }
3671
3672 return false;
3673 }
3674
3675 /* Struct used to manage iterating over all CUs looking for a symbol. */
3676
3677 struct dw2_symtab_iterator
3678 {
3679 /* The internalized form of .gdb_index. */
3680 struct mapped_index *index;
3681 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3682 int want_specific_block;
3683 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3684 Unused if !WANT_SPECIFIC_BLOCK. */
3685 int block_index;
3686 /* The kind of symbol we're looking for. */
3687 domain_enum domain;
3688 /* The list of CUs from the index entry of the symbol,
3689 or NULL if not found. */
3690 offset_type *vec;
3691 /* The next element in VEC to look at. */
3692 int next;
3693 /* The number of elements in VEC, or zero if there is no match. */
3694 int length;
3695 /* Have we seen a global version of the symbol?
3696 If so we can ignore all further global instances.
3697 This is to work around gold/15646, inefficient gold-generated
3698 indices. */
3699 int global_seen;
3700 };
3701
3702 /* Initialize the index symtab iterator ITER.
3703 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3704 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3705
3706 static void
3707 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3708 struct mapped_index *index,
3709 int want_specific_block,
3710 int block_index,
3711 domain_enum domain,
3712 const char *name)
3713 {
3714 iter->index = index;
3715 iter->want_specific_block = want_specific_block;
3716 iter->block_index = block_index;
3717 iter->domain = domain;
3718 iter->next = 0;
3719 iter->global_seen = 0;
3720
3721 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3722 iter->length = MAYBE_SWAP (*iter->vec);
3723 else
3724 {
3725 iter->vec = NULL;
3726 iter->length = 0;
3727 }
3728 }
3729
3730 /* Return the next matching CU or NULL if there are no more. */
3731
3732 static struct dwarf2_per_cu_data *
3733 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3734 {
3735 for ( ; iter->next < iter->length; ++iter->next)
3736 {
3737 offset_type cu_index_and_attrs =
3738 MAYBE_SWAP (iter->vec[iter->next + 1]);
3739 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3740 struct dwarf2_per_cu_data *per_cu;
3741 int want_static = iter->block_index != GLOBAL_BLOCK;
3742 /* This value is only valid for index versions >= 7. */
3743 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3744 gdb_index_symbol_kind symbol_kind =
3745 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3746 /* Only check the symbol attributes if they're present.
3747 Indices prior to version 7 don't record them,
3748 and indices >= 7 may elide them for certain symbols
3749 (gold does this). */
3750 int attrs_valid =
3751 (iter->index->version >= 7
3752 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3753
3754 /* Don't crash on bad data. */
3755 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3756 + dwarf2_per_objfile->n_type_units))
3757 {
3758 complaint (&symfile_complaints,
3759 _(".gdb_index entry has bad CU index"
3760 " [in module %s]"),
3761 objfile_name (dwarf2_per_objfile->objfile));
3762 continue;
3763 }
3764
3765 per_cu = dw2_get_cutu (cu_index);
3766
3767 /* Skip if already read in. */
3768 if (per_cu->v.quick->compunit_symtab)
3769 continue;
3770
3771 /* Check static vs global. */
3772 if (attrs_valid)
3773 {
3774 if (iter->want_specific_block
3775 && want_static != is_static)
3776 continue;
3777 /* Work around gold/15646. */
3778 if (!is_static && iter->global_seen)
3779 continue;
3780 if (!is_static)
3781 iter->global_seen = 1;
3782 }
3783
3784 /* Only check the symbol's kind if it has one. */
3785 if (attrs_valid)
3786 {
3787 switch (iter->domain)
3788 {
3789 case VAR_DOMAIN:
3790 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3791 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3792 /* Some types are also in VAR_DOMAIN. */
3793 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3794 continue;
3795 break;
3796 case STRUCT_DOMAIN:
3797 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3798 continue;
3799 break;
3800 case LABEL_DOMAIN:
3801 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3802 continue;
3803 break;
3804 default:
3805 break;
3806 }
3807 }
3808
3809 ++iter->next;
3810 return per_cu;
3811 }
3812
3813 return NULL;
3814 }
3815
3816 static struct compunit_symtab *
3817 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3818 const char *name, domain_enum domain)
3819 {
3820 struct compunit_symtab *stab_best = NULL;
3821 struct mapped_index *index;
3822
3823 dw2_setup (objfile);
3824
3825 index = dwarf2_per_objfile->index_table;
3826
3827 /* index is NULL if OBJF_READNOW. */
3828 if (index)
3829 {
3830 struct dw2_symtab_iterator iter;
3831 struct dwarf2_per_cu_data *per_cu;
3832
3833 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3834
3835 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3836 {
3837 struct symbol *sym, *with_opaque = NULL;
3838 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
3839 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3840 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3841
3842 sym = block_find_symbol (block, name, domain,
3843 block_find_non_opaque_type_preferred,
3844 &with_opaque);
3845
3846 /* Some caution must be observed with overloaded functions
3847 and methods, since the index will not contain any overload
3848 information (but NAME might contain it). */
3849
3850 if (sym != NULL
3851 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3852 return stab;
3853 if (with_opaque != NULL
3854 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
3855 stab_best = stab;
3856
3857 /* Keep looking through other CUs. */
3858 }
3859 }
3860
3861 return stab_best;
3862 }
3863
3864 static void
3865 dw2_print_stats (struct objfile *objfile)
3866 {
3867 int i, total, count;
3868
3869 dw2_setup (objfile);
3870 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
3871 count = 0;
3872 for (i = 0; i < total; ++i)
3873 {
3874 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3875
3876 if (!per_cu->v.quick->compunit_symtab)
3877 ++count;
3878 }
3879 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3880 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3881 }
3882
3883 /* This dumps minimal information about the index.
3884 It is called via "mt print objfiles".
3885 One use is to verify .gdb_index has been loaded by the
3886 gdb.dwarf2/gdb-index.exp testcase. */
3887
3888 static void
3889 dw2_dump (struct objfile *objfile)
3890 {
3891 dw2_setup (objfile);
3892 gdb_assert (dwarf2_per_objfile->using_index);
3893 printf_filtered (".gdb_index:");
3894 if (dwarf2_per_objfile->index_table != NULL)
3895 {
3896 printf_filtered (" version %d\n",
3897 dwarf2_per_objfile->index_table->version);
3898 }
3899 else
3900 printf_filtered (" faked for \"readnow\"\n");
3901 printf_filtered ("\n");
3902 }
3903
3904 static void
3905 dw2_relocate (struct objfile *objfile,
3906 const struct section_offsets *new_offsets,
3907 const struct section_offsets *delta)
3908 {
3909 /* There's nothing to relocate here. */
3910 }
3911
3912 static void
3913 dw2_expand_symtabs_for_function (struct objfile *objfile,
3914 const char *func_name)
3915 {
3916 struct mapped_index *index;
3917
3918 dw2_setup (objfile);
3919
3920 index = dwarf2_per_objfile->index_table;
3921
3922 /* index is NULL if OBJF_READNOW. */
3923 if (index)
3924 {
3925 struct dw2_symtab_iterator iter;
3926 struct dwarf2_per_cu_data *per_cu;
3927
3928 /* Note: It doesn't matter what we pass for block_index here. */
3929 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3930 func_name);
3931
3932 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3933 dw2_instantiate_symtab (per_cu);
3934 }
3935 }
3936
3937 static void
3938 dw2_expand_all_symtabs (struct objfile *objfile)
3939 {
3940 int i;
3941
3942 dw2_setup (objfile);
3943
3944 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3945 + dwarf2_per_objfile->n_type_units); ++i)
3946 {
3947 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3948
3949 dw2_instantiate_symtab (per_cu);
3950 }
3951 }
3952
3953 static void
3954 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3955 const char *fullname)
3956 {
3957 int i;
3958
3959 dw2_setup (objfile);
3960
3961 /* We don't need to consider type units here.
3962 This is only called for examining code, e.g. expand_line_sal.
3963 There can be an order of magnitude (or more) more type units
3964 than comp units, and we avoid them if we can. */
3965
3966 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3967 {
3968 int j;
3969 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
3970 struct quick_file_names *file_data;
3971
3972 /* We only need to look at symtabs not already expanded. */
3973 if (per_cu->v.quick->compunit_symtab)
3974 continue;
3975
3976 file_data = dw2_get_file_names (per_cu);
3977 if (file_data == NULL)
3978 continue;
3979
3980 for (j = 0; j < file_data->num_file_names; ++j)
3981 {
3982 const char *this_fullname = file_data->file_names[j];
3983
3984 if (filename_cmp (this_fullname, fullname) == 0)
3985 {
3986 dw2_instantiate_symtab (per_cu);
3987 break;
3988 }
3989 }
3990 }
3991 }
3992
3993 static void
3994 dw2_map_matching_symbols (struct objfile *objfile,
3995 const char * name, domain_enum domain,
3996 int global,
3997 int (*callback) (struct block *,
3998 struct symbol *, void *),
3999 void *data, symbol_compare_ftype *match,
4000 symbol_compare_ftype *ordered_compare)
4001 {
4002 /* Currently unimplemented; used for Ada. The function can be called if the
4003 current language is Ada for a non-Ada objfile using GNU index. As Ada
4004 does not look for non-Ada symbols this function should just return. */
4005 }
4006
4007 static void
4008 dw2_expand_symtabs_matching
4009 (struct objfile *objfile,
4010 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4011 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4012 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4013 enum search_domain kind)
4014 {
4015 int i;
4016 offset_type iter;
4017 struct mapped_index *index;
4018
4019 dw2_setup (objfile);
4020
4021 /* index_table is NULL if OBJF_READNOW. */
4022 if (!dwarf2_per_objfile->index_table)
4023 return;
4024 index = dwarf2_per_objfile->index_table;
4025
4026 if (file_matcher != NULL)
4027 {
4028 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4029 htab_eq_pointer,
4030 NULL, xcalloc, xfree));
4031 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4032 htab_eq_pointer,
4033 NULL, xcalloc, xfree));
4034
4035 /* The rule is CUs specify all the files, including those used by
4036 any TU, so there's no need to scan TUs here. */
4037
4038 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4039 {
4040 int j;
4041 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4042 struct quick_file_names *file_data;
4043 void **slot;
4044
4045 QUIT;
4046
4047 per_cu->v.quick->mark = 0;
4048
4049 /* We only need to look at symtabs not already expanded. */
4050 if (per_cu->v.quick->compunit_symtab)
4051 continue;
4052
4053 file_data = dw2_get_file_names (per_cu);
4054 if (file_data == NULL)
4055 continue;
4056
4057 if (htab_find (visited_not_found.get (), file_data) != NULL)
4058 continue;
4059 else if (htab_find (visited_found.get (), file_data) != NULL)
4060 {
4061 per_cu->v.quick->mark = 1;
4062 continue;
4063 }
4064
4065 for (j = 0; j < file_data->num_file_names; ++j)
4066 {
4067 const char *this_real_name;
4068
4069 if (file_matcher (file_data->file_names[j], false))
4070 {
4071 per_cu->v.quick->mark = 1;
4072 break;
4073 }
4074
4075 /* Before we invoke realpath, which can get expensive when many
4076 files are involved, do a quick comparison of the basenames. */
4077 if (!basenames_may_differ
4078 && !file_matcher (lbasename (file_data->file_names[j]),
4079 true))
4080 continue;
4081
4082 this_real_name = dw2_get_real_path (objfile, file_data, j);
4083 if (file_matcher (this_real_name, false))
4084 {
4085 per_cu->v.quick->mark = 1;
4086 break;
4087 }
4088 }
4089
4090 slot = htab_find_slot (per_cu->v.quick->mark
4091 ? visited_found.get ()
4092 : visited_not_found.get (),
4093 file_data, INSERT);
4094 *slot = file_data;
4095 }
4096 }
4097
4098 for (iter = 0; iter < index->symbol_table_slots; ++iter)
4099 {
4100 offset_type idx = 2 * iter;
4101 const char *name;
4102 offset_type *vec, vec_len, vec_idx;
4103 int global_seen = 0;
4104
4105 QUIT;
4106
4107 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
4108 continue;
4109
4110 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
4111
4112 if (!symbol_matcher (name))
4113 continue;
4114
4115 /* The name was matched, now expand corresponding CUs that were
4116 marked. */
4117 vec = (offset_type *) (index->constant_pool
4118 + MAYBE_SWAP (index->symbol_table[idx + 1]));
4119 vec_len = MAYBE_SWAP (vec[0]);
4120 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4121 {
4122 struct dwarf2_per_cu_data *per_cu;
4123 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4124 /* This value is only valid for index versions >= 7. */
4125 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4126 gdb_index_symbol_kind symbol_kind =
4127 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4128 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4129 /* Only check the symbol attributes if they're present.
4130 Indices prior to version 7 don't record them,
4131 and indices >= 7 may elide them for certain symbols
4132 (gold does this). */
4133 int attrs_valid =
4134 (index->version >= 7
4135 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4136
4137 /* Work around gold/15646. */
4138 if (attrs_valid)
4139 {
4140 if (!is_static && global_seen)
4141 continue;
4142 if (!is_static)
4143 global_seen = 1;
4144 }
4145
4146 /* Only check the symbol's kind if it has one. */
4147 if (attrs_valid)
4148 {
4149 switch (kind)
4150 {
4151 case VARIABLES_DOMAIN:
4152 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4153 continue;
4154 break;
4155 case FUNCTIONS_DOMAIN:
4156 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4157 continue;
4158 break;
4159 case TYPES_DOMAIN:
4160 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4161 continue;
4162 break;
4163 default:
4164 break;
4165 }
4166 }
4167
4168 /* Don't crash on bad data. */
4169 if (cu_index >= (dwarf2_per_objfile->n_comp_units
4170 + dwarf2_per_objfile->n_type_units))
4171 {
4172 complaint (&symfile_complaints,
4173 _(".gdb_index entry has bad CU index"
4174 " [in module %s]"), objfile_name (objfile));
4175 continue;
4176 }
4177
4178 per_cu = dw2_get_cutu (cu_index);
4179 if (file_matcher == NULL || per_cu->v.quick->mark)
4180 {
4181 int symtab_was_null =
4182 (per_cu->v.quick->compunit_symtab == NULL);
4183
4184 dw2_instantiate_symtab (per_cu);
4185
4186 if (expansion_notify != NULL
4187 && symtab_was_null
4188 && per_cu->v.quick->compunit_symtab != NULL)
4189 {
4190 expansion_notify (per_cu->v.quick->compunit_symtab);
4191 }
4192 }
4193 }
4194 }
4195 }
4196
4197 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4198 symtab. */
4199
4200 static struct compunit_symtab *
4201 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4202 CORE_ADDR pc)
4203 {
4204 int i;
4205
4206 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4207 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4208 return cust;
4209
4210 if (cust->includes == NULL)
4211 return NULL;
4212
4213 for (i = 0; cust->includes[i]; ++i)
4214 {
4215 struct compunit_symtab *s = cust->includes[i];
4216
4217 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4218 if (s != NULL)
4219 return s;
4220 }
4221
4222 return NULL;
4223 }
4224
4225 static struct compunit_symtab *
4226 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4227 struct bound_minimal_symbol msymbol,
4228 CORE_ADDR pc,
4229 struct obj_section *section,
4230 int warn_if_readin)
4231 {
4232 struct dwarf2_per_cu_data *data;
4233 struct compunit_symtab *result;
4234
4235 dw2_setup (objfile);
4236
4237 if (!objfile->psymtabs_addrmap)
4238 return NULL;
4239
4240 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
4241 pc);
4242 if (!data)
4243 return NULL;
4244
4245 if (warn_if_readin && data->v.quick->compunit_symtab)
4246 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4247 paddress (get_objfile_arch (objfile), pc));
4248
4249 result
4250 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
4251 pc);
4252 gdb_assert (result != NULL);
4253 return result;
4254 }
4255
4256 static void
4257 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4258 void *data, int need_fullname)
4259 {
4260 int i;
4261 htab_up visited (htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
4262 NULL, xcalloc, xfree));
4263
4264 dw2_setup (objfile);
4265
4266 /* The rule is CUs specify all the files, including those used by
4267 any TU, so there's no need to scan TUs here.
4268 We can ignore file names coming from already-expanded CUs. */
4269
4270 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4271 {
4272 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4273
4274 if (per_cu->v.quick->compunit_symtab)
4275 {
4276 void **slot = htab_find_slot (visited.get (),
4277 per_cu->v.quick->file_names,
4278 INSERT);
4279
4280 *slot = per_cu->v.quick->file_names;
4281 }
4282 }
4283
4284 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4285 {
4286 int j;
4287 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4288 struct quick_file_names *file_data;
4289 void **slot;
4290
4291 /* We only need to look at symtabs not already expanded. */
4292 if (per_cu->v.quick->compunit_symtab)
4293 continue;
4294
4295 file_data = dw2_get_file_names (per_cu);
4296 if (file_data == NULL)
4297 continue;
4298
4299 slot = htab_find_slot (visited.get (), file_data, INSERT);
4300 if (*slot)
4301 {
4302 /* Already visited. */
4303 continue;
4304 }
4305 *slot = file_data;
4306
4307 for (j = 0; j < file_data->num_file_names; ++j)
4308 {
4309 const char *this_real_name;
4310
4311 if (need_fullname)
4312 this_real_name = dw2_get_real_path (objfile, file_data, j);
4313 else
4314 this_real_name = NULL;
4315 (*fun) (file_data->file_names[j], this_real_name, data);
4316 }
4317 }
4318 }
4319
4320 static int
4321 dw2_has_symbols (struct objfile *objfile)
4322 {
4323 return 1;
4324 }
4325
4326 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4327 {
4328 dw2_has_symbols,
4329 dw2_find_last_source_symtab,
4330 dw2_forget_cached_source_info,
4331 dw2_map_symtabs_matching_filename,
4332 dw2_lookup_symbol,
4333 dw2_print_stats,
4334 dw2_dump,
4335 dw2_relocate,
4336 dw2_expand_symtabs_for_function,
4337 dw2_expand_all_symtabs,
4338 dw2_expand_symtabs_with_fullname,
4339 dw2_map_matching_symbols,
4340 dw2_expand_symtabs_matching,
4341 dw2_find_pc_sect_compunit_symtab,
4342 dw2_map_symbol_filenames
4343 };
4344
4345 /* Initialize for reading DWARF for this objfile. Return 0 if this
4346 file will use psymtabs, or 1 if using the GNU index. */
4347
4348 int
4349 dwarf2_initialize_objfile (struct objfile *objfile)
4350 {
4351 /* If we're about to read full symbols, don't bother with the
4352 indices. In this case we also don't care if some other debug
4353 format is making psymtabs, because they are all about to be
4354 expanded anyway. */
4355 if ((objfile->flags & OBJF_READNOW))
4356 {
4357 int i;
4358
4359 dwarf2_per_objfile->using_index = 1;
4360 create_all_comp_units (objfile);
4361 create_all_type_units (objfile);
4362 dwarf2_per_objfile->quick_file_names_table =
4363 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
4364
4365 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
4366 + dwarf2_per_objfile->n_type_units); ++i)
4367 {
4368 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
4369
4370 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4371 struct dwarf2_per_cu_quick_data);
4372 }
4373
4374 /* Return 1 so that gdb sees the "quick" functions. However,
4375 these functions will be no-ops because we will have expanded
4376 all symtabs. */
4377 return 1;
4378 }
4379
4380 if (dwarf2_read_index (objfile))
4381 return 1;
4382
4383 return 0;
4384 }
4385
4386 \f
4387
4388 /* Build a partial symbol table. */
4389
4390 void
4391 dwarf2_build_psymtabs (struct objfile *objfile)
4392 {
4393
4394 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
4395 {
4396 init_psymbol_list (objfile, 1024);
4397 }
4398
4399 TRY
4400 {
4401 /* This isn't really ideal: all the data we allocate on the
4402 objfile's obstack is still uselessly kept around. However,
4403 freeing it seems unsafe. */
4404 psymtab_discarder psymtabs (objfile);
4405 dwarf2_build_psymtabs_hard (objfile);
4406 psymtabs.keep ();
4407 }
4408 CATCH (except, RETURN_MASK_ERROR)
4409 {
4410 exception_print (gdb_stderr, except);
4411 }
4412 END_CATCH
4413 }
4414
4415 /* Return the total length of the CU described by HEADER. */
4416
4417 static unsigned int
4418 get_cu_length (const struct comp_unit_head *header)
4419 {
4420 return header->initial_length_size + header->length;
4421 }
4422
4423 /* Return TRUE if OFFSET is within CU_HEADER. */
4424
4425 static inline int
4426 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
4427 {
4428 sect_offset bottom = { cu_header->offset.sect_off };
4429 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
4430
4431 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
4432 }
4433
4434 /* Find the base address of the compilation unit for range lists and
4435 location lists. It will normally be specified by DW_AT_low_pc.
4436 In DWARF-3 draft 4, the base address could be overridden by
4437 DW_AT_entry_pc. It's been removed, but GCC still uses this for
4438 compilation units with discontinuous ranges. */
4439
4440 static void
4441 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
4442 {
4443 struct attribute *attr;
4444
4445 cu->base_known = 0;
4446 cu->base_address = 0;
4447
4448 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
4449 if (attr)
4450 {
4451 cu->base_address = attr_value_as_address (attr);
4452 cu->base_known = 1;
4453 }
4454 else
4455 {
4456 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4457 if (attr)
4458 {
4459 cu->base_address = attr_value_as_address (attr);
4460 cu->base_known = 1;
4461 }
4462 }
4463 }
4464
4465 /* Read in the comp unit header information from the debug_info at info_ptr.
4466 Use rcuh_kind::COMPILE as the default type if not known by the caller.
4467 NOTE: This leaves members offset, first_die_offset to be filled in
4468 by the caller. */
4469
4470 static const gdb_byte *
4471 read_comp_unit_head (struct comp_unit_head *cu_header,
4472 const gdb_byte *info_ptr,
4473 struct dwarf2_section_info *section,
4474 rcuh_kind section_kind)
4475 {
4476 int signed_addr;
4477 unsigned int bytes_read;
4478 const char *filename = get_section_file_name (section);
4479 bfd *abfd = get_section_bfd_owner (section);
4480
4481 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
4482 cu_header->initial_length_size = bytes_read;
4483 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
4484 info_ptr += bytes_read;
4485 cu_header->version = read_2_bytes (abfd, info_ptr);
4486 info_ptr += 2;
4487 if (cu_header->version < 5)
4488 switch (section_kind)
4489 {
4490 case rcuh_kind::COMPILE:
4491 cu_header->unit_type = DW_UT_compile;
4492 break;
4493 case rcuh_kind::TYPE:
4494 cu_header->unit_type = DW_UT_type;
4495 break;
4496 default:
4497 internal_error (__FILE__, __LINE__,
4498 _("read_comp_unit_head: invalid section_kind"));
4499 }
4500 else
4501 {
4502 cu_header->unit_type = static_cast<enum dwarf_unit_type>
4503 (read_1_byte (abfd, info_ptr));
4504 info_ptr += 1;
4505 switch (cu_header->unit_type)
4506 {
4507 case DW_UT_compile:
4508 if (section_kind != rcuh_kind::COMPILE)
4509 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4510 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
4511 filename);
4512 break;
4513 case DW_UT_type:
4514 section_kind = rcuh_kind::TYPE;
4515 break;
4516 default:
4517 error (_("Dwarf Error: wrong unit_type in compilation unit header "
4518 "(is %d, should be %d or %d) [in module %s]"),
4519 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
4520 }
4521
4522 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4523 info_ptr += 1;
4524 }
4525 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
4526 &bytes_read);
4527 info_ptr += bytes_read;
4528 if (cu_header->version < 5)
4529 {
4530 cu_header->addr_size = read_1_byte (abfd, info_ptr);
4531 info_ptr += 1;
4532 }
4533 signed_addr = bfd_get_sign_extend_vma (abfd);
4534 if (signed_addr < 0)
4535 internal_error (__FILE__, __LINE__,
4536 _("read_comp_unit_head: dwarf from non elf file"));
4537 cu_header->signed_addr_p = signed_addr;
4538
4539 if (section_kind == rcuh_kind::TYPE)
4540 {
4541 LONGEST type_offset;
4542
4543 cu_header->signature = read_8_bytes (abfd, info_ptr);
4544 info_ptr += 8;
4545
4546 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
4547 info_ptr += bytes_read;
4548 cu_header->type_offset_in_tu.cu_off = type_offset;
4549 if (cu_header->type_offset_in_tu.cu_off != type_offset)
4550 error (_("Dwarf Error: Too big type_offset in compilation unit "
4551 "header (is %s) [in module %s]"), plongest (type_offset),
4552 filename);
4553 }
4554
4555 return info_ptr;
4556 }
4557
4558 /* Helper function that returns the proper abbrev section for
4559 THIS_CU. */
4560
4561 static struct dwarf2_section_info *
4562 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
4563 {
4564 struct dwarf2_section_info *abbrev;
4565
4566 if (this_cu->is_dwz)
4567 abbrev = &dwarf2_get_dwz_file ()->abbrev;
4568 else
4569 abbrev = &dwarf2_per_objfile->abbrev;
4570
4571 return abbrev;
4572 }
4573
4574 /* Subroutine of read_and_check_comp_unit_head and
4575 read_and_check_type_unit_head to simplify them.
4576 Perform various error checking on the header. */
4577
4578 static void
4579 error_check_comp_unit_head (struct comp_unit_head *header,
4580 struct dwarf2_section_info *section,
4581 struct dwarf2_section_info *abbrev_section)
4582 {
4583 const char *filename = get_section_file_name (section);
4584
4585 if (header->version < 2 || header->version > 5)
4586 error (_("Dwarf Error: wrong version in compilation unit header "
4587 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
4588 filename);
4589
4590 if (header->abbrev_offset.sect_off
4591 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
4592 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
4593 "(offset 0x%lx + 6) [in module %s]"),
4594 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
4595 filename);
4596
4597 /* Cast to unsigned long to use 64-bit arithmetic when possible to
4598 avoid potential 32-bit overflow. */
4599 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
4600 > section->size)
4601 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
4602 "(offset 0x%lx + 0) [in module %s]"),
4603 (long) header->length, (long) header->offset.sect_off,
4604 filename);
4605 }
4606
4607 /* Read in a CU/TU header and perform some basic error checking.
4608 The contents of the header are stored in HEADER.
4609 The result is a pointer to the start of the first DIE. */
4610
4611 static const gdb_byte *
4612 read_and_check_comp_unit_head (struct comp_unit_head *header,
4613 struct dwarf2_section_info *section,
4614 struct dwarf2_section_info *abbrev_section,
4615 const gdb_byte *info_ptr,
4616 rcuh_kind section_kind)
4617 {
4618 const gdb_byte *beg_of_comp_unit = info_ptr;
4619 bfd *abfd = get_section_bfd_owner (section);
4620
4621 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4622
4623 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
4624
4625 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4626
4627 error_check_comp_unit_head (header, section, abbrev_section);
4628
4629 return info_ptr;
4630 }
4631
4632 /* Fetch the abbreviation table offset from a comp or type unit header. */
4633
4634 static sect_offset
4635 read_abbrev_offset (struct dwarf2_section_info *section,
4636 sect_offset offset)
4637 {
4638 bfd *abfd = get_section_bfd_owner (section);
4639 const gdb_byte *info_ptr;
4640 unsigned int initial_length_size, offset_size;
4641 sect_offset abbrev_offset;
4642 uint16_t version;
4643
4644 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4645 info_ptr = section->buffer + offset.sect_off;
4646 read_initial_length (abfd, info_ptr, &initial_length_size);
4647 offset_size = initial_length_size == 4 ? 4 : 8;
4648 info_ptr += initial_length_size;
4649
4650 version = read_2_bytes (abfd, info_ptr);
4651 info_ptr += 2;
4652 if (version >= 5)
4653 {
4654 /* Skip unit type and address size. */
4655 info_ptr += 2;
4656 }
4657
4658 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4659 return abbrev_offset;
4660 }
4661
4662 /* Allocate a new partial symtab for file named NAME and mark this new
4663 partial symtab as being an include of PST. */
4664
4665 static void
4666 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
4667 struct objfile *objfile)
4668 {
4669 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4670
4671 if (!IS_ABSOLUTE_PATH (subpst->filename))
4672 {
4673 /* It shares objfile->objfile_obstack. */
4674 subpst->dirname = pst->dirname;
4675 }
4676
4677 subpst->textlow = 0;
4678 subpst->texthigh = 0;
4679
4680 subpst->dependencies
4681 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
4682 subpst->dependencies[0] = pst;
4683 subpst->number_of_dependencies = 1;
4684
4685 subpst->globals_offset = 0;
4686 subpst->n_global_syms = 0;
4687 subpst->statics_offset = 0;
4688 subpst->n_static_syms = 0;
4689 subpst->compunit_symtab = NULL;
4690 subpst->read_symtab = pst->read_symtab;
4691 subpst->readin = 0;
4692
4693 /* No private part is necessary for include psymtabs. This property
4694 can be used to differentiate between such include psymtabs and
4695 the regular ones. */
4696 subpst->read_symtab_private = NULL;
4697 }
4698
4699 /* Read the Line Number Program data and extract the list of files
4700 included by the source file represented by PST. Build an include
4701 partial symtab for each of these included files. */
4702
4703 static void
4704 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4705 struct die_info *die,
4706 struct partial_symtab *pst)
4707 {
4708 line_header_up lh;
4709 struct attribute *attr;
4710
4711 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4712 if (attr)
4713 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
4714 if (lh == NULL)
4715 return; /* No linetable, so no includes. */
4716
4717 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4718 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
4719 }
4720
4721 static hashval_t
4722 hash_signatured_type (const void *item)
4723 {
4724 const struct signatured_type *sig_type
4725 = (const struct signatured_type *) item;
4726
4727 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4728 return sig_type->signature;
4729 }
4730
4731 static int
4732 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4733 {
4734 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
4735 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
4736
4737 return lhs->signature == rhs->signature;
4738 }
4739
4740 /* Allocate a hash table for signatured types. */
4741
4742 static htab_t
4743 allocate_signatured_type_table (struct objfile *objfile)
4744 {
4745 return htab_create_alloc_ex (41,
4746 hash_signatured_type,
4747 eq_signatured_type,
4748 NULL,
4749 &objfile->objfile_obstack,
4750 hashtab_obstack_allocate,
4751 dummy_obstack_deallocate);
4752 }
4753
4754 /* A helper function to add a signatured type CU to a table. */
4755
4756 static int
4757 add_signatured_type_cu_to_table (void **slot, void *datum)
4758 {
4759 struct signatured_type *sigt = (struct signatured_type *) *slot;
4760 struct signatured_type ***datap = (struct signatured_type ***) datum;
4761
4762 **datap = sigt;
4763 ++*datap;
4764
4765 return 1;
4766 }
4767
4768 /* A helper for create_debug_types_hash_table. Read types from SECTION
4769 and fill them into TYPES_HTAB. It will process only type units,
4770 therefore DW_UT_type. */
4771
4772 static void
4773 create_debug_type_hash_table (struct dwo_file *dwo_file,
4774 dwarf2_section_info *section, htab_t &types_htab,
4775 rcuh_kind section_kind)
4776 {
4777 struct objfile *objfile = dwarf2_per_objfile->objfile;
4778 struct dwarf2_section_info *abbrev_section;
4779 bfd *abfd;
4780 const gdb_byte *info_ptr, *end_ptr;
4781
4782 abbrev_section = (dwo_file != NULL
4783 ? &dwo_file->sections.abbrev
4784 : &dwarf2_per_objfile->abbrev);
4785
4786 if (dwarf_read_debug)
4787 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
4788 get_section_name (section),
4789 get_section_file_name (abbrev_section));
4790
4791 dwarf2_read_section (objfile, section);
4792 info_ptr = section->buffer;
4793
4794 if (info_ptr == NULL)
4795 return;
4796
4797 /* We can't set abfd until now because the section may be empty or
4798 not present, in which case the bfd is unknown. */
4799 abfd = get_section_bfd_owner (section);
4800
4801 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4802 because we don't need to read any dies: the signature is in the
4803 header. */
4804
4805 end_ptr = info_ptr + section->size;
4806 while (info_ptr < end_ptr)
4807 {
4808 sect_offset offset;
4809 struct signatured_type *sig_type;
4810 struct dwo_unit *dwo_tu;
4811 void **slot;
4812 const gdb_byte *ptr = info_ptr;
4813 struct comp_unit_head header;
4814 unsigned int length;
4815
4816 offset.sect_off = ptr - section->buffer;
4817
4818 /* Initialize it due to a false compiler warning. */
4819 header.signature = -1;
4820 header.type_offset_in_tu.cu_off = -1;
4821
4822 /* We need to read the type's signature in order to build the hash
4823 table, but we don't need anything else just yet. */
4824
4825 ptr = read_and_check_comp_unit_head (&header, section,
4826 abbrev_section, ptr, section_kind);
4827
4828 length = get_cu_length (&header);
4829
4830 /* Skip dummy type units. */
4831 if (ptr >= info_ptr + length
4832 || peek_abbrev_code (abfd, ptr) == 0
4833 || header.unit_type != DW_UT_type)
4834 {
4835 info_ptr += length;
4836 continue;
4837 }
4838
4839 if (types_htab == NULL)
4840 {
4841 if (dwo_file)
4842 types_htab = allocate_dwo_unit_table (objfile);
4843 else
4844 types_htab = allocate_signatured_type_table (objfile);
4845 }
4846
4847 if (dwo_file)
4848 {
4849 sig_type = NULL;
4850 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4851 struct dwo_unit);
4852 dwo_tu->dwo_file = dwo_file;
4853 dwo_tu->signature = header.signature;
4854 dwo_tu->type_offset_in_tu = header.type_offset_in_tu;
4855 dwo_tu->section = section;
4856 dwo_tu->offset = offset;
4857 dwo_tu->length = length;
4858 }
4859 else
4860 {
4861 /* N.B.: type_offset is not usable if this type uses a DWO file.
4862 The real type_offset is in the DWO file. */
4863 dwo_tu = NULL;
4864 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4865 struct signatured_type);
4866 sig_type->signature = header.signature;
4867 sig_type->type_offset_in_tu = header.type_offset_in_tu;
4868 sig_type->per_cu.objfile = objfile;
4869 sig_type->per_cu.is_debug_types = 1;
4870 sig_type->per_cu.section = section;
4871 sig_type->per_cu.offset = offset;
4872 sig_type->per_cu.length = length;
4873 }
4874
4875 slot = htab_find_slot (types_htab,
4876 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4877 INSERT);
4878 gdb_assert (slot != NULL);
4879 if (*slot != NULL)
4880 {
4881 sect_offset dup_offset;
4882
4883 if (dwo_file)
4884 {
4885 const struct dwo_unit *dup_tu
4886 = (const struct dwo_unit *) *slot;
4887
4888 dup_offset = dup_tu->offset;
4889 }
4890 else
4891 {
4892 const struct signatured_type *dup_tu
4893 = (const struct signatured_type *) *slot;
4894
4895 dup_offset = dup_tu->per_cu.offset;
4896 }
4897
4898 complaint (&symfile_complaints,
4899 _("debug type entry at offset 0x%x is duplicate to"
4900 " the entry at offset 0x%x, signature %s"),
4901 offset.sect_off, dup_offset.sect_off,
4902 hex_string (header.signature));
4903 }
4904 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4905
4906 if (dwarf_read_debug > 1)
4907 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
4908 offset.sect_off,
4909 hex_string (header.signature));
4910
4911 info_ptr += length;
4912 }
4913 }
4914
4915 /* Create the hash table of all entries in the .debug_types
4916 (or .debug_types.dwo) section(s).
4917 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4918 otherwise it is NULL.
4919
4920 The result is a pointer to the hash table or NULL if there are no types.
4921
4922 Note: This function processes DWO files only, not DWP files. */
4923
4924 static void
4925 create_debug_types_hash_table (struct dwo_file *dwo_file,
4926 VEC (dwarf2_section_info_def) *types,
4927 htab_t &types_htab)
4928 {
4929 int ix;
4930 struct dwarf2_section_info *section;
4931
4932 if (VEC_empty (dwarf2_section_info_def, types))
4933 return;
4934
4935 for (ix = 0;
4936 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4937 ++ix)
4938 create_debug_type_hash_table (dwo_file, section, types_htab,
4939 rcuh_kind::TYPE);
4940 }
4941
4942 /* Create the hash table of all entries in the .debug_types section,
4943 and initialize all_type_units.
4944 The result is zero if there is an error (e.g. missing .debug_types section),
4945 otherwise non-zero. */
4946
4947 static int
4948 create_all_type_units (struct objfile *objfile)
4949 {
4950 htab_t types_htab = NULL;
4951 struct signatured_type **iter;
4952
4953 create_debug_type_hash_table (NULL, &dwarf2_per_objfile->info, types_htab,
4954 rcuh_kind::COMPILE);
4955 create_debug_types_hash_table (NULL, dwarf2_per_objfile->types, types_htab);
4956 if (types_htab == NULL)
4957 {
4958 dwarf2_per_objfile->signatured_types = NULL;
4959 return 0;
4960 }
4961
4962 dwarf2_per_objfile->signatured_types = types_htab;
4963
4964 dwarf2_per_objfile->n_type_units
4965 = dwarf2_per_objfile->n_allocated_type_units
4966 = htab_elements (types_htab);
4967 dwarf2_per_objfile->all_type_units =
4968 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
4969 iter = &dwarf2_per_objfile->all_type_units[0];
4970 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4971 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4972 == dwarf2_per_objfile->n_type_units);
4973
4974 return 1;
4975 }
4976
4977 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
4978 If SLOT is non-NULL, it is the entry to use in the hash table.
4979 Otherwise we find one. */
4980
4981 static struct signatured_type *
4982 add_type_unit (ULONGEST sig, void **slot)
4983 {
4984 struct objfile *objfile = dwarf2_per_objfile->objfile;
4985 int n_type_units = dwarf2_per_objfile->n_type_units;
4986 struct signatured_type *sig_type;
4987
4988 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
4989 ++n_type_units;
4990 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
4991 {
4992 if (dwarf2_per_objfile->n_allocated_type_units == 0)
4993 dwarf2_per_objfile->n_allocated_type_units = 1;
4994 dwarf2_per_objfile->n_allocated_type_units *= 2;
4995 dwarf2_per_objfile->all_type_units
4996 = XRESIZEVEC (struct signatured_type *,
4997 dwarf2_per_objfile->all_type_units,
4998 dwarf2_per_objfile->n_allocated_type_units);
4999 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
5000 }
5001 dwarf2_per_objfile->n_type_units = n_type_units;
5002
5003 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5004 struct signatured_type);
5005 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
5006 sig_type->signature = sig;
5007 sig_type->per_cu.is_debug_types = 1;
5008 if (dwarf2_per_objfile->using_index)
5009 {
5010 sig_type->per_cu.v.quick =
5011 OBSTACK_ZALLOC (&objfile->objfile_obstack,
5012 struct dwarf2_per_cu_quick_data);
5013 }
5014
5015 if (slot == NULL)
5016 {
5017 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5018 sig_type, INSERT);
5019 }
5020 gdb_assert (*slot == NULL);
5021 *slot = sig_type;
5022 /* The rest of sig_type must be filled in by the caller. */
5023 return sig_type;
5024 }
5025
5026 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
5027 Fill in SIG_ENTRY with DWO_ENTRY. */
5028
5029 static void
5030 fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
5031 struct signatured_type *sig_entry,
5032 struct dwo_unit *dwo_entry)
5033 {
5034 /* Make sure we're not clobbering something we don't expect to. */
5035 gdb_assert (! sig_entry->per_cu.queued);
5036 gdb_assert (sig_entry->per_cu.cu == NULL);
5037 if (dwarf2_per_objfile->using_index)
5038 {
5039 gdb_assert (sig_entry->per_cu.v.quick != NULL);
5040 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
5041 }
5042 else
5043 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
5044 gdb_assert (sig_entry->signature == dwo_entry->signature);
5045 gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
5046 gdb_assert (sig_entry->type_unit_group == NULL);
5047 gdb_assert (sig_entry->dwo_unit == NULL);
5048
5049 sig_entry->per_cu.section = dwo_entry->section;
5050 sig_entry->per_cu.offset = dwo_entry->offset;
5051 sig_entry->per_cu.length = dwo_entry->length;
5052 sig_entry->per_cu.reading_dwo_directly = 1;
5053 sig_entry->per_cu.objfile = objfile;
5054 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
5055 sig_entry->dwo_unit = dwo_entry;
5056 }
5057
5058 /* Subroutine of lookup_signatured_type.
5059 If we haven't read the TU yet, create the signatured_type data structure
5060 for a TU to be read in directly from a DWO file, bypassing the stub.
5061 This is the "Stay in DWO Optimization": When there is no DWP file and we're
5062 using .gdb_index, then when reading a CU we want to stay in the DWO file
5063 containing that CU. Otherwise we could end up reading several other DWO
5064 files (due to comdat folding) to process the transitive closure of all the
5065 mentioned TUs, and that can be slow. The current DWO file will have every
5066 type signature that it needs.
5067 We only do this for .gdb_index because in the psymtab case we already have
5068 to read all the DWOs to build the type unit groups. */
5069
5070 static struct signatured_type *
5071 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5072 {
5073 struct objfile *objfile = dwarf2_per_objfile->objfile;
5074 struct dwo_file *dwo_file;
5075 struct dwo_unit find_dwo_entry, *dwo_entry;
5076 struct signatured_type find_sig_entry, *sig_entry;
5077 void **slot;
5078
5079 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5080
5081 /* If TU skeletons have been removed then we may not have read in any
5082 TUs yet. */
5083 if (dwarf2_per_objfile->signatured_types == NULL)
5084 {
5085 dwarf2_per_objfile->signatured_types
5086 = allocate_signatured_type_table (objfile);
5087 }
5088
5089 /* We only ever need to read in one copy of a signatured type.
5090 Use the global signatured_types array to do our own comdat-folding
5091 of types. If this is the first time we're reading this TU, and
5092 the TU has an entry in .gdb_index, replace the recorded data from
5093 .gdb_index with this TU. */
5094
5095 find_sig_entry.signature = sig;
5096 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5097 &find_sig_entry, INSERT);
5098 sig_entry = (struct signatured_type *) *slot;
5099
5100 /* We can get here with the TU already read, *or* in the process of being
5101 read. Don't reassign the global entry to point to this DWO if that's
5102 the case. Also note that if the TU is already being read, it may not
5103 have come from a DWO, the program may be a mix of Fission-compiled
5104 code and non-Fission-compiled code. */
5105
5106 /* Have we already tried to read this TU?
5107 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5108 needn't exist in the global table yet). */
5109 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
5110 return sig_entry;
5111
5112 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
5113 dwo_unit of the TU itself. */
5114 dwo_file = cu->dwo_unit->dwo_file;
5115
5116 /* Ok, this is the first time we're reading this TU. */
5117 if (dwo_file->tus == NULL)
5118 return NULL;
5119 find_dwo_entry.signature = sig;
5120 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
5121 if (dwo_entry == NULL)
5122 return NULL;
5123
5124 /* If the global table doesn't have an entry for this TU, add one. */
5125 if (sig_entry == NULL)
5126 sig_entry = add_type_unit (sig, slot);
5127
5128 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5129 sig_entry->per_cu.tu_read = 1;
5130 return sig_entry;
5131 }
5132
5133 /* Subroutine of lookup_signatured_type.
5134 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
5135 then try the DWP file. If the TU stub (skeleton) has been removed then
5136 it won't be in .gdb_index. */
5137
5138 static struct signatured_type *
5139 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5140 {
5141 struct objfile *objfile = dwarf2_per_objfile->objfile;
5142 struct dwp_file *dwp_file = get_dwp_file ();
5143 struct dwo_unit *dwo_entry;
5144 struct signatured_type find_sig_entry, *sig_entry;
5145 void **slot;
5146
5147 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
5148 gdb_assert (dwp_file != NULL);
5149
5150 /* If TU skeletons have been removed then we may not have read in any
5151 TUs yet. */
5152 if (dwarf2_per_objfile->signatured_types == NULL)
5153 {
5154 dwarf2_per_objfile->signatured_types
5155 = allocate_signatured_type_table (objfile);
5156 }
5157
5158 find_sig_entry.signature = sig;
5159 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
5160 &find_sig_entry, INSERT);
5161 sig_entry = (struct signatured_type *) *slot;
5162
5163 /* Have we already tried to read this TU?
5164 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
5165 needn't exist in the global table yet). */
5166 if (sig_entry != NULL)
5167 return sig_entry;
5168
5169 if (dwp_file->tus == NULL)
5170 return NULL;
5171 dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
5172 sig, 1 /* is_debug_types */);
5173 if (dwo_entry == NULL)
5174 return NULL;
5175
5176 sig_entry = add_type_unit (sig, slot);
5177 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
5178
5179 return sig_entry;
5180 }
5181
5182 /* Lookup a signature based type for DW_FORM_ref_sig8.
5183 Returns NULL if signature SIG is not present in the table.
5184 It is up to the caller to complain about this. */
5185
5186 static struct signatured_type *
5187 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
5188 {
5189 if (cu->dwo_unit
5190 && dwarf2_per_objfile->using_index)
5191 {
5192 /* We're in a DWO/DWP file, and we're using .gdb_index.
5193 These cases require special processing. */
5194 if (get_dwp_file () == NULL)
5195 return lookup_dwo_signatured_type (cu, sig);
5196 else
5197 return lookup_dwp_signatured_type (cu, sig);
5198 }
5199 else
5200 {
5201 struct signatured_type find_entry, *entry;
5202
5203 if (dwarf2_per_objfile->signatured_types == NULL)
5204 return NULL;
5205 find_entry.signature = sig;
5206 entry = ((struct signatured_type *)
5207 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
5208 return entry;
5209 }
5210 }
5211 \f
5212 /* Low level DIE reading support. */
5213
5214 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
5215
5216 static void
5217 init_cu_die_reader (struct die_reader_specs *reader,
5218 struct dwarf2_cu *cu,
5219 struct dwarf2_section_info *section,
5220 struct dwo_file *dwo_file)
5221 {
5222 gdb_assert (section->readin && section->buffer != NULL);
5223 reader->abfd = get_section_bfd_owner (section);
5224 reader->cu = cu;
5225 reader->dwo_file = dwo_file;
5226 reader->die_section = section;
5227 reader->buffer = section->buffer;
5228 reader->buffer_end = section->buffer + section->size;
5229 reader->comp_dir = NULL;
5230 }
5231
5232 /* Subroutine of init_cutu_and_read_dies to simplify it.
5233 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
5234 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
5235 already.
5236
5237 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
5238 from it to the DIE in the DWO. If NULL we are skipping the stub.
5239 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
5240 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
5241 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
5242 STUB_COMP_DIR may be non-NULL.
5243 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
5244 are filled in with the info of the DIE from the DWO file.
5245 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
5246 provided an abbrev table to use.
5247 The result is non-zero if a valid (non-dummy) DIE was found. */
5248
5249 static int
5250 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
5251 struct dwo_unit *dwo_unit,
5252 int abbrev_table_provided,
5253 struct die_info *stub_comp_unit_die,
5254 const char *stub_comp_dir,
5255 struct die_reader_specs *result_reader,
5256 const gdb_byte **result_info_ptr,
5257 struct die_info **result_comp_unit_die,
5258 int *result_has_children)
5259 {
5260 struct objfile *objfile = dwarf2_per_objfile->objfile;
5261 struct dwarf2_cu *cu = this_cu->cu;
5262 struct dwarf2_section_info *section;
5263 bfd *abfd;
5264 const gdb_byte *begin_info_ptr, *info_ptr;
5265 ULONGEST signature; /* Or dwo_id. */
5266 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
5267 int i,num_extra_attrs;
5268 struct dwarf2_section_info *dwo_abbrev_section;
5269 struct attribute *attr;
5270 struct die_info *comp_unit_die;
5271
5272 /* At most one of these may be provided. */
5273 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
5274
5275 /* These attributes aren't processed until later:
5276 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
5277 DW_AT_comp_dir is used now, to find the DWO file, but it is also
5278 referenced later. However, these attributes are found in the stub
5279 which we won't have later. In order to not impose this complication
5280 on the rest of the code, we read them here and copy them to the
5281 DWO CU/TU die. */
5282
5283 stmt_list = NULL;
5284 low_pc = NULL;
5285 high_pc = NULL;
5286 ranges = NULL;
5287 comp_dir = NULL;
5288
5289 if (stub_comp_unit_die != NULL)
5290 {
5291 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
5292 DWO file. */
5293 if (! this_cu->is_debug_types)
5294 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
5295 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
5296 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
5297 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
5298 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
5299
5300 /* There should be a DW_AT_addr_base attribute here (if needed).
5301 We need the value before we can process DW_FORM_GNU_addr_index. */
5302 cu->addr_base = 0;
5303 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
5304 if (attr)
5305 cu->addr_base = DW_UNSND (attr);
5306
5307 /* There should be a DW_AT_ranges_base attribute here (if needed).
5308 We need the value before we can process DW_AT_ranges. */
5309 cu->ranges_base = 0;
5310 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
5311 if (attr)
5312 cu->ranges_base = DW_UNSND (attr);
5313 }
5314 else if (stub_comp_dir != NULL)
5315 {
5316 /* Reconstruct the comp_dir attribute to simplify the code below. */
5317 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
5318 comp_dir->name = DW_AT_comp_dir;
5319 comp_dir->form = DW_FORM_string;
5320 DW_STRING_IS_CANONICAL (comp_dir) = 0;
5321 DW_STRING (comp_dir) = stub_comp_dir;
5322 }
5323
5324 /* Set up for reading the DWO CU/TU. */
5325 cu->dwo_unit = dwo_unit;
5326 section = dwo_unit->section;
5327 dwarf2_read_section (objfile, section);
5328 abfd = get_section_bfd_owner (section);
5329 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
5330 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
5331 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
5332
5333 if (this_cu->is_debug_types)
5334 {
5335 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
5336
5337 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5338 dwo_abbrev_section,
5339 info_ptr, rcuh_kind::TYPE);
5340 /* This is not an assert because it can be caused by bad debug info. */
5341 if (sig_type->signature != cu->header.signature)
5342 {
5343 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
5344 " TU at offset 0x%x [in module %s]"),
5345 hex_string (sig_type->signature),
5346 hex_string (cu->header.signature),
5347 dwo_unit->offset.sect_off,
5348 bfd_get_filename (abfd));
5349 }
5350 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
5351 /* For DWOs coming from DWP files, we don't know the CU length
5352 nor the type's offset in the TU until now. */
5353 dwo_unit->length = get_cu_length (&cu->header);
5354 dwo_unit->type_offset_in_tu = cu->header.type_offset_in_tu;
5355
5356 /* Establish the type offset that can be used to lookup the type.
5357 For DWO files, we don't know it until now. */
5358 sig_type->type_offset_in_section.sect_off =
5359 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
5360 }
5361 else
5362 {
5363 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5364 dwo_abbrev_section,
5365 info_ptr, rcuh_kind::COMPILE);
5366 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
5367 /* For DWOs coming from DWP files, we don't know the CU length
5368 until now. */
5369 dwo_unit->length = get_cu_length (&cu->header);
5370 }
5371
5372 /* Replace the CU's original abbrev table with the DWO's.
5373 Reminder: We can't read the abbrev table until we've read the header. */
5374 if (abbrev_table_provided)
5375 {
5376 /* Don't free the provided abbrev table, the caller of
5377 init_cutu_and_read_dies owns it. */
5378 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
5379 /* Ensure the DWO abbrev table gets freed. */
5380 make_cleanup (dwarf2_free_abbrev_table, cu);
5381 }
5382 else
5383 {
5384 dwarf2_free_abbrev_table (cu);
5385 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
5386 /* Leave any existing abbrev table cleanup as is. */
5387 }
5388
5389 /* Read in the die, but leave space to copy over the attributes
5390 from the stub. This has the benefit of simplifying the rest of
5391 the code - all the work to maintain the illusion of a single
5392 DW_TAG_{compile,type}_unit DIE is done here. */
5393 num_extra_attrs = ((stmt_list != NULL)
5394 + (low_pc != NULL)
5395 + (high_pc != NULL)
5396 + (ranges != NULL)
5397 + (comp_dir != NULL));
5398 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
5399 result_has_children, num_extra_attrs);
5400
5401 /* Copy over the attributes from the stub to the DIE we just read in. */
5402 comp_unit_die = *result_comp_unit_die;
5403 i = comp_unit_die->num_attrs;
5404 if (stmt_list != NULL)
5405 comp_unit_die->attrs[i++] = *stmt_list;
5406 if (low_pc != NULL)
5407 comp_unit_die->attrs[i++] = *low_pc;
5408 if (high_pc != NULL)
5409 comp_unit_die->attrs[i++] = *high_pc;
5410 if (ranges != NULL)
5411 comp_unit_die->attrs[i++] = *ranges;
5412 if (comp_dir != NULL)
5413 comp_unit_die->attrs[i++] = *comp_dir;
5414 comp_unit_die->num_attrs += num_extra_attrs;
5415
5416 if (dwarf_die_debug)
5417 {
5418 fprintf_unfiltered (gdb_stdlog,
5419 "Read die from %s@0x%x of %s:\n",
5420 get_section_name (section),
5421 (unsigned) (begin_info_ptr - section->buffer),
5422 bfd_get_filename (abfd));
5423 dump_die (comp_unit_die, dwarf_die_debug);
5424 }
5425
5426 /* Save the comp_dir attribute. If there is no DWP file then we'll read
5427 TUs by skipping the stub and going directly to the entry in the DWO file.
5428 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
5429 to get it via circuitous means. Blech. */
5430 if (comp_dir != NULL)
5431 result_reader->comp_dir = DW_STRING (comp_dir);
5432
5433 /* Skip dummy compilation units. */
5434 if (info_ptr >= begin_info_ptr + dwo_unit->length
5435 || peek_abbrev_code (abfd, info_ptr) == 0)
5436 return 0;
5437
5438 *result_info_ptr = info_ptr;
5439 return 1;
5440 }
5441
5442 /* Subroutine of init_cutu_and_read_dies to simplify it.
5443 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
5444 Returns NULL if the specified DWO unit cannot be found. */
5445
5446 static struct dwo_unit *
5447 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
5448 struct die_info *comp_unit_die)
5449 {
5450 struct dwarf2_cu *cu = this_cu->cu;
5451 struct attribute *attr;
5452 ULONGEST signature;
5453 struct dwo_unit *dwo_unit;
5454 const char *comp_dir, *dwo_name;
5455
5456 gdb_assert (cu != NULL);
5457
5458 /* Yeah, we look dwo_name up again, but it simplifies the code. */
5459 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5460 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
5461
5462 if (this_cu->is_debug_types)
5463 {
5464 struct signatured_type *sig_type;
5465
5466 /* Since this_cu is the first member of struct signatured_type,
5467 we can go from a pointer to one to a pointer to the other. */
5468 sig_type = (struct signatured_type *) this_cu;
5469 signature = sig_type->signature;
5470 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
5471 }
5472 else
5473 {
5474 struct attribute *attr;
5475
5476 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
5477 if (! attr)
5478 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
5479 " [in module %s]"),
5480 dwo_name, objfile_name (this_cu->objfile));
5481 signature = DW_UNSND (attr);
5482 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
5483 signature);
5484 }
5485
5486 return dwo_unit;
5487 }
5488
5489 /* Subroutine of init_cutu_and_read_dies to simplify it.
5490 See it for a description of the parameters.
5491 Read a TU directly from a DWO file, bypassing the stub.
5492
5493 Note: This function could be a little bit simpler if we shared cleanups
5494 with our caller, init_cutu_and_read_dies. That's generally a fragile thing
5495 to do, so we keep this function self-contained. Or we could move this
5496 into our caller, but it's complex enough already. */
5497
5498 static void
5499 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
5500 int use_existing_cu, int keep,
5501 die_reader_func_ftype *die_reader_func,
5502 void *data)
5503 {
5504 struct dwarf2_cu *cu;
5505 struct signatured_type *sig_type;
5506 struct cleanup *cleanups, *free_cu_cleanup = NULL;
5507 struct die_reader_specs reader;
5508 const gdb_byte *info_ptr;
5509 struct die_info *comp_unit_die;
5510 int has_children;
5511
5512 /* Verify we can do the following downcast, and that we have the
5513 data we need. */
5514 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
5515 sig_type = (struct signatured_type *) this_cu;
5516 gdb_assert (sig_type->dwo_unit != NULL);
5517
5518 cleanups = make_cleanup (null_cleanup, NULL);
5519
5520 if (use_existing_cu && this_cu->cu != NULL)
5521 {
5522 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
5523 cu = this_cu->cu;
5524 /* There's no need to do the rereading_dwo_cu handling that
5525 init_cutu_and_read_dies does since we don't read the stub. */
5526 }
5527 else
5528 {
5529 /* If !use_existing_cu, this_cu->cu must be NULL. */
5530 gdb_assert (this_cu->cu == NULL);
5531 cu = XNEW (struct dwarf2_cu);
5532 init_one_comp_unit (cu, this_cu);
5533 /* If an error occurs while loading, release our storage. */
5534 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5535 }
5536
5537 /* A future optimization, if needed, would be to use an existing
5538 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
5539 could share abbrev tables. */
5540
5541 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
5542 0 /* abbrev_table_provided */,
5543 NULL /* stub_comp_unit_die */,
5544 sig_type->dwo_unit->dwo_file->comp_dir,
5545 &reader, &info_ptr,
5546 &comp_unit_die, &has_children) == 0)
5547 {
5548 /* Dummy die. */
5549 do_cleanups (cleanups);
5550 return;
5551 }
5552
5553 /* All the "real" work is done here. */
5554 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5555
5556 /* This duplicates the code in init_cutu_and_read_dies,
5557 but the alternative is making the latter more complex.
5558 This function is only for the special case of using DWO files directly:
5559 no point in overly complicating the general case just to handle this. */
5560 if (free_cu_cleanup != NULL)
5561 {
5562 if (keep)
5563 {
5564 /* We've successfully allocated this compilation unit. Let our
5565 caller clean it up when finished with it. */
5566 discard_cleanups (free_cu_cleanup);
5567
5568 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5569 So we have to manually free the abbrev table. */
5570 dwarf2_free_abbrev_table (cu);
5571
5572 /* Link this CU into read_in_chain. */
5573 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5574 dwarf2_per_objfile->read_in_chain = this_cu;
5575 }
5576 else
5577 do_cleanups (free_cu_cleanup);
5578 }
5579
5580 do_cleanups (cleanups);
5581 }
5582
5583 /* Initialize a CU (or TU) and read its DIEs.
5584 If the CU defers to a DWO file, read the DWO file as well.
5585
5586 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
5587 Otherwise the table specified in the comp unit header is read in and used.
5588 This is an optimization for when we already have the abbrev table.
5589
5590 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
5591 Otherwise, a new CU is allocated with xmalloc.
5592
5593 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
5594 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
5595
5596 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5597 linker) then DIE_READER_FUNC will not get called. */
5598
5599 static void
5600 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
5601 struct abbrev_table *abbrev_table,
5602 int use_existing_cu, int keep,
5603 die_reader_func_ftype *die_reader_func,
5604 void *data)
5605 {
5606 struct objfile *objfile = dwarf2_per_objfile->objfile;
5607 struct dwarf2_section_info *section = this_cu->section;
5608 bfd *abfd = get_section_bfd_owner (section);
5609 struct dwarf2_cu *cu;
5610 const gdb_byte *begin_info_ptr, *info_ptr;
5611 struct die_reader_specs reader;
5612 struct die_info *comp_unit_die;
5613 int has_children;
5614 struct attribute *attr;
5615 struct cleanup *cleanups, *free_cu_cleanup = NULL;
5616 struct signatured_type *sig_type = NULL;
5617 struct dwarf2_section_info *abbrev_section;
5618 /* Non-zero if CU currently points to a DWO file and we need to
5619 reread it. When this happens we need to reread the skeleton die
5620 before we can reread the DWO file (this only applies to CUs, not TUs). */
5621 int rereading_dwo_cu = 0;
5622
5623 if (dwarf_die_debug)
5624 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5625 this_cu->is_debug_types ? "type" : "comp",
5626 this_cu->offset.sect_off);
5627
5628 if (use_existing_cu)
5629 gdb_assert (keep);
5630
5631 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5632 file (instead of going through the stub), short-circuit all of this. */
5633 if (this_cu->reading_dwo_directly)
5634 {
5635 /* Narrow down the scope of possibilities to have to understand. */
5636 gdb_assert (this_cu->is_debug_types);
5637 gdb_assert (abbrev_table == NULL);
5638 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
5639 die_reader_func, data);
5640 return;
5641 }
5642
5643 cleanups = make_cleanup (null_cleanup, NULL);
5644
5645 /* This is cheap if the section is already read in. */
5646 dwarf2_read_section (objfile, section);
5647
5648 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
5649
5650 abbrev_section = get_abbrev_section_for_cu (this_cu);
5651
5652 if (use_existing_cu && this_cu->cu != NULL)
5653 {
5654 cu = this_cu->cu;
5655 /* If this CU is from a DWO file we need to start over, we need to
5656 refetch the attributes from the skeleton CU.
5657 This could be optimized by retrieving those attributes from when we
5658 were here the first time: the previous comp_unit_die was stored in
5659 comp_unit_obstack. But there's no data yet that we need this
5660 optimization. */
5661 if (cu->dwo_unit != NULL)
5662 rereading_dwo_cu = 1;
5663 }
5664 else
5665 {
5666 /* If !use_existing_cu, this_cu->cu must be NULL. */
5667 gdb_assert (this_cu->cu == NULL);
5668 cu = XNEW (struct dwarf2_cu);
5669 init_one_comp_unit (cu, this_cu);
5670 /* If an error occurs while loading, release our storage. */
5671 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5672 }
5673
5674 /* Get the header. */
5675 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
5676 {
5677 /* We already have the header, there's no need to read it in again. */
5678 info_ptr += cu->header.first_die_offset.cu_off;
5679 }
5680 else
5681 {
5682 if (this_cu->is_debug_types)
5683 {
5684 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5685 abbrev_section, info_ptr,
5686 rcuh_kind::TYPE);
5687
5688 /* Since per_cu is the first member of struct signatured_type,
5689 we can go from a pointer to one to a pointer to the other. */
5690 sig_type = (struct signatured_type *) this_cu;
5691 gdb_assert (sig_type->signature == cu->header.signature);
5692 gdb_assert (sig_type->type_offset_in_tu.cu_off
5693 == cu->header.type_offset_in_tu.cu_off);
5694 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5695
5696 /* LENGTH has not been set yet for type units if we're
5697 using .gdb_index. */
5698 this_cu->length = get_cu_length (&cu->header);
5699
5700 /* Establish the type offset that can be used to lookup the type. */
5701 sig_type->type_offset_in_section.sect_off =
5702 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
5703
5704 this_cu->dwarf_version = cu->header.version;
5705 }
5706 else
5707 {
5708 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5709 abbrev_section,
5710 info_ptr,
5711 rcuh_kind::COMPILE);
5712
5713 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5714 gdb_assert (this_cu->length == get_cu_length (&cu->header));
5715 this_cu->dwarf_version = cu->header.version;
5716 }
5717 }
5718
5719 /* Skip dummy compilation units. */
5720 if (info_ptr >= begin_info_ptr + this_cu->length
5721 || peek_abbrev_code (abfd, info_ptr) == 0)
5722 {
5723 do_cleanups (cleanups);
5724 return;
5725 }
5726
5727 /* If we don't have them yet, read the abbrevs for this compilation unit.
5728 And if we need to read them now, make sure they're freed when we're
5729 done. Note that it's important that if the CU had an abbrev table
5730 on entry we don't free it when we're done: Somewhere up the call stack
5731 it may be in use. */
5732 if (abbrev_table != NULL)
5733 {
5734 gdb_assert (cu->abbrev_table == NULL);
5735 gdb_assert (cu->header.abbrev_offset.sect_off
5736 == abbrev_table->offset.sect_off);
5737 cu->abbrev_table = abbrev_table;
5738 }
5739 else if (cu->abbrev_table == NULL)
5740 {
5741 dwarf2_read_abbrevs (cu, abbrev_section);
5742 make_cleanup (dwarf2_free_abbrev_table, cu);
5743 }
5744 else if (rereading_dwo_cu)
5745 {
5746 dwarf2_free_abbrev_table (cu);
5747 dwarf2_read_abbrevs (cu, abbrev_section);
5748 }
5749
5750 /* Read the top level CU/TU die. */
5751 init_cu_die_reader (&reader, cu, section, NULL);
5752 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5753
5754 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5755 from the DWO file.
5756 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5757 DWO CU, that this test will fail (the attribute will not be present). */
5758 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5759 if (attr)
5760 {
5761 struct dwo_unit *dwo_unit;
5762 struct die_info *dwo_comp_unit_die;
5763
5764 if (has_children)
5765 {
5766 complaint (&symfile_complaints,
5767 _("compilation unit with DW_AT_GNU_dwo_name"
5768 " has children (offset 0x%x) [in module %s]"),
5769 this_cu->offset.sect_off, bfd_get_filename (abfd));
5770 }
5771 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
5772 if (dwo_unit != NULL)
5773 {
5774 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5775 abbrev_table != NULL,
5776 comp_unit_die, NULL,
5777 &reader, &info_ptr,
5778 &dwo_comp_unit_die, &has_children) == 0)
5779 {
5780 /* Dummy die. */
5781 do_cleanups (cleanups);
5782 return;
5783 }
5784 comp_unit_die = dwo_comp_unit_die;
5785 }
5786 else
5787 {
5788 /* Yikes, we couldn't find the rest of the DIE, we only have
5789 the stub. A complaint has already been logged. There's
5790 not much more we can do except pass on the stub DIE to
5791 die_reader_func. We don't want to throw an error on bad
5792 debug info. */
5793 }
5794 }
5795
5796 /* All of the above is setup for this call. Yikes. */
5797 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5798
5799 /* Done, clean up. */
5800 if (free_cu_cleanup != NULL)
5801 {
5802 if (keep)
5803 {
5804 /* We've successfully allocated this compilation unit. Let our
5805 caller clean it up when finished with it. */
5806 discard_cleanups (free_cu_cleanup);
5807
5808 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5809 So we have to manually free the abbrev table. */
5810 dwarf2_free_abbrev_table (cu);
5811
5812 /* Link this CU into read_in_chain. */
5813 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5814 dwarf2_per_objfile->read_in_chain = this_cu;
5815 }
5816 else
5817 do_cleanups (free_cu_cleanup);
5818 }
5819
5820 do_cleanups (cleanups);
5821 }
5822
5823 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
5824 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
5825 to have already done the lookup to find the DWO file).
5826
5827 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
5828 THIS_CU->is_debug_types, but nothing else.
5829
5830 We fill in THIS_CU->length.
5831
5832 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5833 linker) then DIE_READER_FUNC will not get called.
5834
5835 THIS_CU->cu is always freed when done.
5836 This is done in order to not leave THIS_CU->cu in a state where we have
5837 to care whether it refers to the "main" CU or the DWO CU. */
5838
5839 static void
5840 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
5841 struct dwo_file *dwo_file,
5842 die_reader_func_ftype *die_reader_func,
5843 void *data)
5844 {
5845 struct objfile *objfile = dwarf2_per_objfile->objfile;
5846 struct dwarf2_section_info *section = this_cu->section;
5847 bfd *abfd = get_section_bfd_owner (section);
5848 struct dwarf2_section_info *abbrev_section;
5849 struct dwarf2_cu cu;
5850 const gdb_byte *begin_info_ptr, *info_ptr;
5851 struct die_reader_specs reader;
5852 struct cleanup *cleanups;
5853 struct die_info *comp_unit_die;
5854 int has_children;
5855
5856 if (dwarf_die_debug)
5857 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5858 this_cu->is_debug_types ? "type" : "comp",
5859 this_cu->offset.sect_off);
5860
5861 gdb_assert (this_cu->cu == NULL);
5862
5863 abbrev_section = (dwo_file != NULL
5864 ? &dwo_file->sections.abbrev
5865 : get_abbrev_section_for_cu (this_cu));
5866
5867 /* This is cheap if the section is already read in. */
5868 dwarf2_read_section (objfile, section);
5869
5870 init_one_comp_unit (&cu, this_cu);
5871
5872 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5873
5874 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
5875 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5876 abbrev_section, info_ptr,
5877 (this_cu->is_debug_types
5878 ? rcuh_kind::TYPE
5879 : rcuh_kind::COMPILE));
5880
5881 this_cu->length = get_cu_length (&cu.header);
5882
5883 /* Skip dummy compilation units. */
5884 if (info_ptr >= begin_info_ptr + this_cu->length
5885 || peek_abbrev_code (abfd, info_ptr) == 0)
5886 {
5887 do_cleanups (cleanups);
5888 return;
5889 }
5890
5891 dwarf2_read_abbrevs (&cu, abbrev_section);
5892 make_cleanup (dwarf2_free_abbrev_table, &cu);
5893
5894 init_cu_die_reader (&reader, &cu, section, dwo_file);
5895 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5896
5897 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5898
5899 do_cleanups (cleanups);
5900 }
5901
5902 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5903 does not lookup the specified DWO file.
5904 This cannot be used to read DWO files.
5905
5906 THIS_CU->cu is always freed when done.
5907 This is done in order to not leave THIS_CU->cu in a state where we have
5908 to care whether it refers to the "main" CU or the DWO CU.
5909 We can revisit this if the data shows there's a performance issue. */
5910
5911 static void
5912 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5913 die_reader_func_ftype *die_reader_func,
5914 void *data)
5915 {
5916 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
5917 }
5918 \f
5919 /* Type Unit Groups.
5920
5921 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5922 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5923 so that all types coming from the same compilation (.o file) are grouped
5924 together. A future step could be to put the types in the same symtab as
5925 the CU the types ultimately came from. */
5926
5927 static hashval_t
5928 hash_type_unit_group (const void *item)
5929 {
5930 const struct type_unit_group *tu_group
5931 = (const struct type_unit_group *) item;
5932
5933 return hash_stmt_list_entry (&tu_group->hash);
5934 }
5935
5936 static int
5937 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
5938 {
5939 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
5940 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
5941
5942 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
5943 }
5944
5945 /* Allocate a hash table for type unit groups. */
5946
5947 static htab_t
5948 allocate_type_unit_groups_table (void)
5949 {
5950 return htab_create_alloc_ex (3,
5951 hash_type_unit_group,
5952 eq_type_unit_group,
5953 NULL,
5954 &dwarf2_per_objfile->objfile->objfile_obstack,
5955 hashtab_obstack_allocate,
5956 dummy_obstack_deallocate);
5957 }
5958
5959 /* Type units that don't have DW_AT_stmt_list are grouped into their own
5960 partial symtabs. We combine several TUs per psymtab to not let the size
5961 of any one psymtab grow too big. */
5962 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5963 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
5964
5965 /* Helper routine for get_type_unit_group.
5966 Create the type_unit_group object used to hold one or more TUs. */
5967
5968 static struct type_unit_group *
5969 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
5970 {
5971 struct objfile *objfile = dwarf2_per_objfile->objfile;
5972 struct dwarf2_per_cu_data *per_cu;
5973 struct type_unit_group *tu_group;
5974
5975 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5976 struct type_unit_group);
5977 per_cu = &tu_group->per_cu;
5978 per_cu->objfile = objfile;
5979
5980 if (dwarf2_per_objfile->using_index)
5981 {
5982 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5983 struct dwarf2_per_cu_quick_data);
5984 }
5985 else
5986 {
5987 unsigned int line_offset = line_offset_struct.sect_off;
5988 struct partial_symtab *pst;
5989 char *name;
5990
5991 /* Give the symtab a useful name for debug purposes. */
5992 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5993 name = xstrprintf ("<type_units_%d>",
5994 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5995 else
5996 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5997
5998 pst = create_partial_symtab (per_cu, name);
5999 pst->anonymous = 1;
6000
6001 xfree (name);
6002 }
6003
6004 tu_group->hash.dwo_unit = cu->dwo_unit;
6005 tu_group->hash.line_offset = line_offset_struct;
6006
6007 return tu_group;
6008 }
6009
6010 /* Look up the type_unit_group for type unit CU, and create it if necessary.
6011 STMT_LIST is a DW_AT_stmt_list attribute. */
6012
6013 static struct type_unit_group *
6014 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
6015 {
6016 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6017 struct type_unit_group *tu_group;
6018 void **slot;
6019 unsigned int line_offset;
6020 struct type_unit_group type_unit_group_for_lookup;
6021
6022 if (dwarf2_per_objfile->type_unit_groups == NULL)
6023 {
6024 dwarf2_per_objfile->type_unit_groups =
6025 allocate_type_unit_groups_table ();
6026 }
6027
6028 /* Do we need to create a new group, or can we use an existing one? */
6029
6030 if (stmt_list)
6031 {
6032 line_offset = DW_UNSND (stmt_list);
6033 ++tu_stats->nr_symtab_sharers;
6034 }
6035 else
6036 {
6037 /* Ugh, no stmt_list. Rare, but we have to handle it.
6038 We can do various things here like create one group per TU or
6039 spread them over multiple groups to split up the expansion work.
6040 To avoid worst case scenarios (too many groups or too large groups)
6041 we, umm, group them in bunches. */
6042 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
6043 | (tu_stats->nr_stmt_less_type_units
6044 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
6045 ++tu_stats->nr_stmt_less_type_units;
6046 }
6047
6048 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
6049 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
6050 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
6051 &type_unit_group_for_lookup, INSERT);
6052 if (*slot != NULL)
6053 {
6054 tu_group = (struct type_unit_group *) *slot;
6055 gdb_assert (tu_group != NULL);
6056 }
6057 else
6058 {
6059 sect_offset line_offset_struct;
6060
6061 line_offset_struct.sect_off = line_offset;
6062 tu_group = create_type_unit_group (cu, line_offset_struct);
6063 *slot = tu_group;
6064 ++tu_stats->nr_symtabs;
6065 }
6066
6067 return tu_group;
6068 }
6069 \f
6070 /* Partial symbol tables. */
6071
6072 /* Create a psymtab named NAME and assign it to PER_CU.
6073
6074 The caller must fill in the following details:
6075 dirname, textlow, texthigh. */
6076
6077 static struct partial_symtab *
6078 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
6079 {
6080 struct objfile *objfile = per_cu->objfile;
6081 struct partial_symtab *pst;
6082
6083 pst = start_psymtab_common (objfile, name, 0,
6084 objfile->global_psymbols.next,
6085 objfile->static_psymbols.next);
6086
6087 pst->psymtabs_addrmap_supported = 1;
6088
6089 /* This is the glue that links PST into GDB's symbol API. */
6090 pst->read_symtab_private = per_cu;
6091 pst->read_symtab = dwarf2_read_symtab;
6092 per_cu->v.psymtab = pst;
6093
6094 return pst;
6095 }
6096
6097 /* The DATA object passed to process_psymtab_comp_unit_reader has this
6098 type. */
6099
6100 struct process_psymtab_comp_unit_data
6101 {
6102 /* True if we are reading a DW_TAG_partial_unit. */
6103
6104 int want_partial_unit;
6105
6106 /* The "pretend" language that is used if the CU doesn't declare a
6107 language. */
6108
6109 enum language pretend_language;
6110 };
6111
6112 /* die_reader_func for process_psymtab_comp_unit. */
6113
6114 static void
6115 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
6116 const gdb_byte *info_ptr,
6117 struct die_info *comp_unit_die,
6118 int has_children,
6119 void *data)
6120 {
6121 struct dwarf2_cu *cu = reader->cu;
6122 struct objfile *objfile = cu->objfile;
6123 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6124 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6125 CORE_ADDR baseaddr;
6126 CORE_ADDR best_lowpc = 0, best_highpc = 0;
6127 struct partial_symtab *pst;
6128 enum pc_bounds_kind cu_bounds_kind;
6129 const char *filename;
6130 struct process_psymtab_comp_unit_data *info
6131 = (struct process_psymtab_comp_unit_data *) data;
6132
6133 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
6134 return;
6135
6136 gdb_assert (! per_cu->is_debug_types);
6137
6138 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
6139
6140 cu->list_in_scope = &file_symbols;
6141
6142 /* Allocate a new partial symbol table structure. */
6143 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
6144 if (filename == NULL)
6145 filename = "";
6146
6147 pst = create_partial_symtab (per_cu, filename);
6148
6149 /* This must be done before calling dwarf2_build_include_psymtabs. */
6150 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6151
6152 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6153
6154 dwarf2_find_base_address (comp_unit_die, cu);
6155
6156 /* Possibly set the default values of LOWPC and HIGHPC from
6157 `DW_AT_ranges'. */
6158 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
6159 &best_highpc, cu, pst);
6160 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
6161 /* Store the contiguous range if it is not empty; it can be empty for
6162 CUs with no code. */
6163 addrmap_set_empty (objfile->psymtabs_addrmap,
6164 gdbarch_adjust_dwarf2_addr (gdbarch,
6165 best_lowpc + baseaddr),
6166 gdbarch_adjust_dwarf2_addr (gdbarch,
6167 best_highpc + baseaddr) - 1,
6168 pst);
6169
6170 /* Check if comp unit has_children.
6171 If so, read the rest of the partial symbols from this comp unit.
6172 If not, there's no more debug_info for this comp unit. */
6173 if (has_children)
6174 {
6175 struct partial_die_info *first_die;
6176 CORE_ADDR lowpc, highpc;
6177
6178 lowpc = ((CORE_ADDR) -1);
6179 highpc = ((CORE_ADDR) 0);
6180
6181 first_die = load_partial_dies (reader, info_ptr, 1);
6182
6183 scan_partial_symbols (first_die, &lowpc, &highpc,
6184 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
6185
6186 /* If we didn't find a lowpc, set it to highpc to avoid
6187 complaints from `maint check'. */
6188 if (lowpc == ((CORE_ADDR) -1))
6189 lowpc = highpc;
6190
6191 /* If the compilation unit didn't have an explicit address range,
6192 then use the information extracted from its child dies. */
6193 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
6194 {
6195 best_lowpc = lowpc;
6196 best_highpc = highpc;
6197 }
6198 }
6199 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
6200 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
6201
6202 end_psymtab_common (objfile, pst);
6203
6204 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
6205 {
6206 int i;
6207 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6208 struct dwarf2_per_cu_data *iter;
6209
6210 /* Fill in 'dependencies' here; we fill in 'users' in a
6211 post-pass. */
6212 pst->number_of_dependencies = len;
6213 pst->dependencies =
6214 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
6215 for (i = 0;
6216 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6217 i, iter);
6218 ++i)
6219 pst->dependencies[i] = iter->v.psymtab;
6220
6221 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
6222 }
6223
6224 /* Get the list of files included in the current compilation unit,
6225 and build a psymtab for each of them. */
6226 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
6227
6228 if (dwarf_read_debug)
6229 {
6230 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6231
6232 fprintf_unfiltered (gdb_stdlog,
6233 "Psymtab for %s unit @0x%x: %s - %s"
6234 ", %d global, %d static syms\n",
6235 per_cu->is_debug_types ? "type" : "comp",
6236 per_cu->offset.sect_off,
6237 paddress (gdbarch, pst->textlow),
6238 paddress (gdbarch, pst->texthigh),
6239 pst->n_global_syms, pst->n_static_syms);
6240 }
6241 }
6242
6243 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6244 Process compilation unit THIS_CU for a psymtab. */
6245
6246 static void
6247 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
6248 int want_partial_unit,
6249 enum language pretend_language)
6250 {
6251 struct process_psymtab_comp_unit_data info;
6252
6253 /* If this compilation unit was already read in, free the
6254 cached copy in order to read it in again. This is
6255 necessary because we skipped some symbols when we first
6256 read in the compilation unit (see load_partial_dies).
6257 This problem could be avoided, but the benefit is unclear. */
6258 if (this_cu->cu != NULL)
6259 free_one_cached_comp_unit (this_cu);
6260
6261 gdb_assert (! this_cu->is_debug_types);
6262 info.want_partial_unit = want_partial_unit;
6263 info.pretend_language = pretend_language;
6264 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
6265 process_psymtab_comp_unit_reader,
6266 &info);
6267
6268 /* Age out any secondary CUs. */
6269 age_cached_comp_units ();
6270 }
6271
6272 /* Reader function for build_type_psymtabs. */
6273
6274 static void
6275 build_type_psymtabs_reader (const struct die_reader_specs *reader,
6276 const gdb_byte *info_ptr,
6277 struct die_info *type_unit_die,
6278 int has_children,
6279 void *data)
6280 {
6281 struct objfile *objfile = dwarf2_per_objfile->objfile;
6282 struct dwarf2_cu *cu = reader->cu;
6283 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
6284 struct signatured_type *sig_type;
6285 struct type_unit_group *tu_group;
6286 struct attribute *attr;
6287 struct partial_die_info *first_die;
6288 CORE_ADDR lowpc, highpc;
6289 struct partial_symtab *pst;
6290
6291 gdb_assert (data == NULL);
6292 gdb_assert (per_cu->is_debug_types);
6293 sig_type = (struct signatured_type *) per_cu;
6294
6295 if (! has_children)
6296 return;
6297
6298 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
6299 tu_group = get_type_unit_group (cu, attr);
6300
6301 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
6302
6303 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
6304 cu->list_in_scope = &file_symbols;
6305 pst = create_partial_symtab (per_cu, "");
6306 pst->anonymous = 1;
6307
6308 first_die = load_partial_dies (reader, info_ptr, 1);
6309
6310 lowpc = (CORE_ADDR) -1;
6311 highpc = (CORE_ADDR) 0;
6312 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
6313
6314 end_psymtab_common (objfile, pst);
6315 }
6316
6317 /* Struct used to sort TUs by their abbreviation table offset. */
6318
6319 struct tu_abbrev_offset
6320 {
6321 struct signatured_type *sig_type;
6322 sect_offset abbrev_offset;
6323 };
6324
6325 /* Helper routine for build_type_psymtabs_1, passed to qsort. */
6326
6327 static int
6328 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
6329 {
6330 const struct tu_abbrev_offset * const *a
6331 = (const struct tu_abbrev_offset * const*) ap;
6332 const struct tu_abbrev_offset * const *b
6333 = (const struct tu_abbrev_offset * const*) bp;
6334 unsigned int aoff = (*a)->abbrev_offset.sect_off;
6335 unsigned int boff = (*b)->abbrev_offset.sect_off;
6336
6337 return (aoff > boff) - (aoff < boff);
6338 }
6339
6340 /* Efficiently read all the type units.
6341 This does the bulk of the work for build_type_psymtabs.
6342
6343 The efficiency is because we sort TUs by the abbrev table they use and
6344 only read each abbrev table once. In one program there are 200K TUs
6345 sharing 8K abbrev tables.
6346
6347 The main purpose of this function is to support building the
6348 dwarf2_per_objfile->type_unit_groups table.
6349 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
6350 can collapse the search space by grouping them by stmt_list.
6351 The savings can be significant, in the same program from above the 200K TUs
6352 share 8K stmt_list tables.
6353
6354 FUNC is expected to call get_type_unit_group, which will create the
6355 struct type_unit_group if necessary and add it to
6356 dwarf2_per_objfile->type_unit_groups. */
6357
6358 static void
6359 build_type_psymtabs_1 (void)
6360 {
6361 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6362 struct cleanup *cleanups;
6363 struct abbrev_table *abbrev_table;
6364 sect_offset abbrev_offset;
6365 struct tu_abbrev_offset *sorted_by_abbrev;
6366 int i;
6367
6368 /* It's up to the caller to not call us multiple times. */
6369 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
6370
6371 if (dwarf2_per_objfile->n_type_units == 0)
6372 return;
6373
6374 /* TUs typically share abbrev tables, and there can be way more TUs than
6375 abbrev tables. Sort by abbrev table to reduce the number of times we
6376 read each abbrev table in.
6377 Alternatives are to punt or to maintain a cache of abbrev tables.
6378 This is simpler and efficient enough for now.
6379
6380 Later we group TUs by their DW_AT_stmt_list value (as this defines the
6381 symtab to use). Typically TUs with the same abbrev offset have the same
6382 stmt_list value too so in practice this should work well.
6383
6384 The basic algorithm here is:
6385
6386 sort TUs by abbrev table
6387 for each TU with same abbrev table:
6388 read abbrev table if first user
6389 read TU top level DIE
6390 [IWBN if DWO skeletons had DW_AT_stmt_list]
6391 call FUNC */
6392
6393 if (dwarf_read_debug)
6394 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
6395
6396 /* Sort in a separate table to maintain the order of all_type_units
6397 for .gdb_index: TU indices directly index all_type_units. */
6398 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
6399 dwarf2_per_objfile->n_type_units);
6400 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6401 {
6402 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
6403
6404 sorted_by_abbrev[i].sig_type = sig_type;
6405 sorted_by_abbrev[i].abbrev_offset =
6406 read_abbrev_offset (sig_type->per_cu.section,
6407 sig_type->per_cu.offset);
6408 }
6409 cleanups = make_cleanup (xfree, sorted_by_abbrev);
6410 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
6411 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
6412
6413 abbrev_offset.sect_off = ~(unsigned) 0;
6414 abbrev_table = NULL;
6415 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
6416
6417 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
6418 {
6419 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
6420
6421 /* Switch to the next abbrev table if necessary. */
6422 if (abbrev_table == NULL
6423 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
6424 {
6425 if (abbrev_table != NULL)
6426 {
6427 abbrev_table_free (abbrev_table);
6428 /* Reset to NULL in case abbrev_table_read_table throws
6429 an error: abbrev_table_free_cleanup will get called. */
6430 abbrev_table = NULL;
6431 }
6432 abbrev_offset = tu->abbrev_offset;
6433 abbrev_table =
6434 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
6435 abbrev_offset);
6436 ++tu_stats->nr_uniq_abbrev_tables;
6437 }
6438
6439 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
6440 build_type_psymtabs_reader, NULL);
6441 }
6442
6443 do_cleanups (cleanups);
6444 }
6445
6446 /* Print collected type unit statistics. */
6447
6448 static void
6449 print_tu_stats (void)
6450 {
6451 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
6452
6453 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
6454 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
6455 dwarf2_per_objfile->n_type_units);
6456 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
6457 tu_stats->nr_uniq_abbrev_tables);
6458 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
6459 tu_stats->nr_symtabs);
6460 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
6461 tu_stats->nr_symtab_sharers);
6462 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
6463 tu_stats->nr_stmt_less_type_units);
6464 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
6465 tu_stats->nr_all_type_units_reallocs);
6466 }
6467
6468 /* Traversal function for build_type_psymtabs. */
6469
6470 static int
6471 build_type_psymtab_dependencies (void **slot, void *info)
6472 {
6473 struct objfile *objfile = dwarf2_per_objfile->objfile;
6474 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
6475 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
6476 struct partial_symtab *pst = per_cu->v.psymtab;
6477 int len = VEC_length (sig_type_ptr, tu_group->tus);
6478 struct signatured_type *iter;
6479 int i;
6480
6481 gdb_assert (len > 0);
6482 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
6483
6484 pst->number_of_dependencies = len;
6485 pst->dependencies =
6486 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
6487 for (i = 0;
6488 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
6489 ++i)
6490 {
6491 gdb_assert (iter->per_cu.is_debug_types);
6492 pst->dependencies[i] = iter->per_cu.v.psymtab;
6493 iter->type_unit_group = tu_group;
6494 }
6495
6496 VEC_free (sig_type_ptr, tu_group->tus);
6497
6498 return 1;
6499 }
6500
6501 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
6502 Build partial symbol tables for the .debug_types comp-units. */
6503
6504 static void
6505 build_type_psymtabs (struct objfile *objfile)
6506 {
6507 if (! create_all_type_units (objfile))
6508 return;
6509
6510 build_type_psymtabs_1 ();
6511 }
6512
6513 /* Traversal function for process_skeletonless_type_unit.
6514 Read a TU in a DWO file and build partial symbols for it. */
6515
6516 static int
6517 process_skeletonless_type_unit (void **slot, void *info)
6518 {
6519 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
6520 struct objfile *objfile = (struct objfile *) info;
6521 struct signatured_type find_entry, *entry;
6522
6523 /* If this TU doesn't exist in the global table, add it and read it in. */
6524
6525 if (dwarf2_per_objfile->signatured_types == NULL)
6526 {
6527 dwarf2_per_objfile->signatured_types
6528 = allocate_signatured_type_table (objfile);
6529 }
6530
6531 find_entry.signature = dwo_unit->signature;
6532 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
6533 INSERT);
6534 /* If we've already seen this type there's nothing to do. What's happening
6535 is we're doing our own version of comdat-folding here. */
6536 if (*slot != NULL)
6537 return 1;
6538
6539 /* This does the job that create_all_type_units would have done for
6540 this TU. */
6541 entry = add_type_unit (dwo_unit->signature, slot);
6542 fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
6543 *slot = entry;
6544
6545 /* This does the job that build_type_psymtabs_1 would have done. */
6546 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
6547 build_type_psymtabs_reader, NULL);
6548
6549 return 1;
6550 }
6551
6552 /* Traversal function for process_skeletonless_type_units. */
6553
6554 static int
6555 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
6556 {
6557 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
6558
6559 if (dwo_file->tus != NULL)
6560 {
6561 htab_traverse_noresize (dwo_file->tus,
6562 process_skeletonless_type_unit, info);
6563 }
6564
6565 return 1;
6566 }
6567
6568 /* Scan all TUs of DWO files, verifying we've processed them.
6569 This is needed in case a TU was emitted without its skeleton.
6570 Note: This can't be done until we know what all the DWO files are. */
6571
6572 static void
6573 process_skeletonless_type_units (struct objfile *objfile)
6574 {
6575 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
6576 if (get_dwp_file () == NULL
6577 && dwarf2_per_objfile->dwo_files != NULL)
6578 {
6579 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
6580 process_dwo_file_for_skeletonless_type_units,
6581 objfile);
6582 }
6583 }
6584
6585 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
6586
6587 static void
6588 psymtabs_addrmap_cleanup (void *o)
6589 {
6590 struct objfile *objfile = (struct objfile *) o;
6591
6592 objfile->psymtabs_addrmap = NULL;
6593 }
6594
6595 /* Compute the 'user' field for each psymtab in OBJFILE. */
6596
6597 static void
6598 set_partial_user (struct objfile *objfile)
6599 {
6600 int i;
6601
6602 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6603 {
6604 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
6605 struct partial_symtab *pst = per_cu->v.psymtab;
6606 int j;
6607
6608 if (pst == NULL)
6609 continue;
6610
6611 for (j = 0; j < pst->number_of_dependencies; ++j)
6612 {
6613 /* Set the 'user' field only if it is not already set. */
6614 if (pst->dependencies[j]->user == NULL)
6615 pst->dependencies[j]->user = pst;
6616 }
6617 }
6618 }
6619
6620 /* Build the partial symbol table by doing a quick pass through the
6621 .debug_info and .debug_abbrev sections. */
6622
6623 static void
6624 dwarf2_build_psymtabs_hard (struct objfile *objfile)
6625 {
6626 struct cleanup *back_to, *addrmap_cleanup;
6627 struct obstack temp_obstack;
6628 int i;
6629
6630 if (dwarf_read_debug)
6631 {
6632 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
6633 objfile_name (objfile));
6634 }
6635
6636 dwarf2_per_objfile->reading_partial_symbols = 1;
6637
6638 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
6639
6640 /* Any cached compilation units will be linked by the per-objfile
6641 read_in_chain. Make sure to free them when we're done. */
6642 back_to = make_cleanup (free_cached_comp_units, NULL);
6643
6644 build_type_psymtabs (objfile);
6645
6646 create_all_comp_units (objfile);
6647
6648 /* Create a temporary address map on a temporary obstack. We later
6649 copy this to the final obstack. */
6650 obstack_init (&temp_obstack);
6651 make_cleanup_obstack_free (&temp_obstack);
6652 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
6653 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
6654
6655 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
6656 {
6657 struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
6658
6659 process_psymtab_comp_unit (per_cu, 0, language_minimal);
6660 }
6661
6662 /* This has to wait until we read the CUs, we need the list of DWOs. */
6663 process_skeletonless_type_units (objfile);
6664
6665 /* Now that all TUs have been processed we can fill in the dependencies. */
6666 if (dwarf2_per_objfile->type_unit_groups != NULL)
6667 {
6668 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
6669 build_type_psymtab_dependencies, NULL);
6670 }
6671
6672 if (dwarf_read_debug)
6673 print_tu_stats ();
6674
6675 set_partial_user (objfile);
6676
6677 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
6678 &objfile->objfile_obstack);
6679 discard_cleanups (addrmap_cleanup);
6680
6681 do_cleanups (back_to);
6682
6683 if (dwarf_read_debug)
6684 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
6685 objfile_name (objfile));
6686 }
6687
6688 /* die_reader_func for load_partial_comp_unit. */
6689
6690 static void
6691 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
6692 const gdb_byte *info_ptr,
6693 struct die_info *comp_unit_die,
6694 int has_children,
6695 void *data)
6696 {
6697 struct dwarf2_cu *cu = reader->cu;
6698
6699 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
6700
6701 /* Check if comp unit has_children.
6702 If so, read the rest of the partial symbols from this comp unit.
6703 If not, there's no more debug_info for this comp unit. */
6704 if (has_children)
6705 load_partial_dies (reader, info_ptr, 0);
6706 }
6707
6708 /* Load the partial DIEs for a secondary CU into memory.
6709 This is also used when rereading a primary CU with load_all_dies. */
6710
6711 static void
6712 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6713 {
6714 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6715 load_partial_comp_unit_reader, NULL);
6716 }
6717
6718 static void
6719 read_comp_units_from_section (struct objfile *objfile,
6720 struct dwarf2_section_info *section,
6721 unsigned int is_dwz,
6722 int *n_allocated,
6723 int *n_comp_units,
6724 struct dwarf2_per_cu_data ***all_comp_units)
6725 {
6726 const gdb_byte *info_ptr;
6727 bfd *abfd = get_section_bfd_owner (section);
6728
6729 if (dwarf_read_debug)
6730 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
6731 get_section_name (section),
6732 get_section_file_name (section));
6733
6734 dwarf2_read_section (objfile, section);
6735
6736 info_ptr = section->buffer;
6737
6738 while (info_ptr < section->buffer + section->size)
6739 {
6740 unsigned int length, initial_length_size;
6741 struct dwarf2_per_cu_data *this_cu;
6742 sect_offset offset;
6743
6744 offset.sect_off = info_ptr - section->buffer;
6745
6746 /* Read just enough information to find out where the next
6747 compilation unit is. */
6748 length = read_initial_length (abfd, info_ptr, &initial_length_size);
6749
6750 /* Save the compilation unit for later lookup. */
6751 this_cu = XOBNEW (&objfile->objfile_obstack, struct dwarf2_per_cu_data);
6752 memset (this_cu, 0, sizeof (*this_cu));
6753 this_cu->offset = offset;
6754 this_cu->length = length + initial_length_size;
6755 this_cu->is_dwz = is_dwz;
6756 this_cu->objfile = objfile;
6757 this_cu->section = section;
6758
6759 if (*n_comp_units == *n_allocated)
6760 {
6761 *n_allocated *= 2;
6762 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
6763 *all_comp_units, *n_allocated);
6764 }
6765 (*all_comp_units)[*n_comp_units] = this_cu;
6766 ++*n_comp_units;
6767
6768 info_ptr = info_ptr + this_cu->length;
6769 }
6770 }
6771
6772 /* Create a list of all compilation units in OBJFILE.
6773 This is only done for -readnow and building partial symtabs. */
6774
6775 static void
6776 create_all_comp_units (struct objfile *objfile)
6777 {
6778 int n_allocated;
6779 int n_comp_units;
6780 struct dwarf2_per_cu_data **all_comp_units;
6781 struct dwz_file *dwz;
6782
6783 n_comp_units = 0;
6784 n_allocated = 10;
6785 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
6786
6787 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6788 &n_allocated, &n_comp_units, &all_comp_units);
6789
6790 dwz = dwarf2_get_dwz_file ();
6791 if (dwz != NULL)
6792 read_comp_units_from_section (objfile, &dwz->info, 1,
6793 &n_allocated, &n_comp_units,
6794 &all_comp_units);
6795
6796 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
6797 struct dwarf2_per_cu_data *,
6798 n_comp_units);
6799 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6800 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6801 xfree (all_comp_units);
6802 dwarf2_per_objfile->n_comp_units = n_comp_units;
6803 }
6804
6805 /* Process all loaded DIEs for compilation unit CU, starting at
6806 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
6807 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
6808 DW_AT_ranges). See the comments of add_partial_subprogram on how
6809 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
6810
6811 static void
6812 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
6813 CORE_ADDR *highpc, int set_addrmap,
6814 struct dwarf2_cu *cu)
6815 {
6816 struct partial_die_info *pdi;
6817
6818 /* Now, march along the PDI's, descending into ones which have
6819 interesting children but skipping the children of the other ones,
6820 until we reach the end of the compilation unit. */
6821
6822 pdi = first_die;
6823
6824 while (pdi != NULL)
6825 {
6826 fixup_partial_die (pdi, cu);
6827
6828 /* Anonymous namespaces or modules have no name but have interesting
6829 children, so we need to look at them. Ditto for anonymous
6830 enums. */
6831
6832 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
6833 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6834 || pdi->tag == DW_TAG_imported_unit)
6835 {
6836 switch (pdi->tag)
6837 {
6838 case DW_TAG_subprogram:
6839 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
6840 break;
6841 case DW_TAG_constant:
6842 case DW_TAG_variable:
6843 case DW_TAG_typedef:
6844 case DW_TAG_union_type:
6845 if (!pdi->is_declaration)
6846 {
6847 add_partial_symbol (pdi, cu);
6848 }
6849 break;
6850 case DW_TAG_class_type:
6851 case DW_TAG_interface_type:
6852 case DW_TAG_structure_type:
6853 if (!pdi->is_declaration)
6854 {
6855 add_partial_symbol (pdi, cu);
6856 }
6857 if (cu->language == language_rust && pdi->has_children)
6858 scan_partial_symbols (pdi->die_child, lowpc, highpc,
6859 set_addrmap, cu);
6860 break;
6861 case DW_TAG_enumeration_type:
6862 if (!pdi->is_declaration)
6863 add_partial_enumeration (pdi, cu);
6864 break;
6865 case DW_TAG_base_type:
6866 case DW_TAG_subrange_type:
6867 /* File scope base type definitions are added to the partial
6868 symbol table. */
6869 add_partial_symbol (pdi, cu);
6870 break;
6871 case DW_TAG_namespace:
6872 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
6873 break;
6874 case DW_TAG_module:
6875 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
6876 break;
6877 case DW_TAG_imported_unit:
6878 {
6879 struct dwarf2_per_cu_data *per_cu;
6880
6881 /* For now we don't handle imported units in type units. */
6882 if (cu->per_cu->is_debug_types)
6883 {
6884 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6885 " supported in type units [in module %s]"),
6886 objfile_name (cu->objfile));
6887 }
6888
6889 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
6890 pdi->is_dwz,
6891 cu->objfile);
6892
6893 /* Go read the partial unit, if needed. */
6894 if (per_cu->v.psymtab == NULL)
6895 process_psymtab_comp_unit (per_cu, 1, cu->language);
6896
6897 VEC_safe_push (dwarf2_per_cu_ptr,
6898 cu->per_cu->imported_symtabs, per_cu);
6899 }
6900 break;
6901 case DW_TAG_imported_declaration:
6902 add_partial_symbol (pdi, cu);
6903 break;
6904 default:
6905 break;
6906 }
6907 }
6908
6909 /* If the die has a sibling, skip to the sibling. */
6910
6911 pdi = pdi->die_sibling;
6912 }
6913 }
6914
6915 /* Functions used to compute the fully scoped name of a partial DIE.
6916
6917 Normally, this is simple. For C++, the parent DIE's fully scoped
6918 name is concatenated with "::" and the partial DIE's name.
6919 Enumerators are an exception; they use the scope of their parent
6920 enumeration type, i.e. the name of the enumeration type is not
6921 prepended to the enumerator.
6922
6923 There are two complexities. One is DW_AT_specification; in this
6924 case "parent" means the parent of the target of the specification,
6925 instead of the direct parent of the DIE. The other is compilers
6926 which do not emit DW_TAG_namespace; in this case we try to guess
6927 the fully qualified name of structure types from their members'
6928 linkage names. This must be done using the DIE's children rather
6929 than the children of any DW_AT_specification target. We only need
6930 to do this for structures at the top level, i.e. if the target of
6931 any DW_AT_specification (if any; otherwise the DIE itself) does not
6932 have a parent. */
6933
6934 /* Compute the scope prefix associated with PDI's parent, in
6935 compilation unit CU. The result will be allocated on CU's
6936 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6937 field. NULL is returned if no prefix is necessary. */
6938 static const char *
6939 partial_die_parent_scope (struct partial_die_info *pdi,
6940 struct dwarf2_cu *cu)
6941 {
6942 const char *grandparent_scope;
6943 struct partial_die_info *parent, *real_pdi;
6944
6945 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6946 then this means the parent of the specification DIE. */
6947
6948 real_pdi = pdi;
6949 while (real_pdi->has_specification)
6950 real_pdi = find_partial_die (real_pdi->spec_offset,
6951 real_pdi->spec_is_dwz, cu);
6952
6953 parent = real_pdi->die_parent;
6954 if (parent == NULL)
6955 return NULL;
6956
6957 if (parent->scope_set)
6958 return parent->scope;
6959
6960 fixup_partial_die (parent, cu);
6961
6962 grandparent_scope = partial_die_parent_scope (parent, cu);
6963
6964 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6965 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6966 Work around this problem here. */
6967 if (cu->language == language_cplus
6968 && parent->tag == DW_TAG_namespace
6969 && strcmp (parent->name, "::") == 0
6970 && grandparent_scope == NULL)
6971 {
6972 parent->scope = NULL;
6973 parent->scope_set = 1;
6974 return NULL;
6975 }
6976
6977 if (pdi->tag == DW_TAG_enumerator)
6978 /* Enumerators should not get the name of the enumeration as a prefix. */
6979 parent->scope = grandparent_scope;
6980 else if (parent->tag == DW_TAG_namespace
6981 || parent->tag == DW_TAG_module
6982 || parent->tag == DW_TAG_structure_type
6983 || parent->tag == DW_TAG_class_type
6984 || parent->tag == DW_TAG_interface_type
6985 || parent->tag == DW_TAG_union_type
6986 || parent->tag == DW_TAG_enumeration_type)
6987 {
6988 if (grandparent_scope == NULL)
6989 parent->scope = parent->name;
6990 else
6991 parent->scope = typename_concat (&cu->comp_unit_obstack,
6992 grandparent_scope,
6993 parent->name, 0, cu);
6994 }
6995 else
6996 {
6997 /* FIXME drow/2004-04-01: What should we be doing with
6998 function-local names? For partial symbols, we should probably be
6999 ignoring them. */
7000 complaint (&symfile_complaints,
7001 _("unhandled containing DIE tag %d for DIE at %d"),
7002 parent->tag, pdi->offset.sect_off);
7003 parent->scope = grandparent_scope;
7004 }
7005
7006 parent->scope_set = 1;
7007 return parent->scope;
7008 }
7009
7010 /* Return the fully scoped name associated with PDI, from compilation unit
7011 CU. The result will be allocated with malloc. */
7012
7013 static char *
7014 partial_die_full_name (struct partial_die_info *pdi,
7015 struct dwarf2_cu *cu)
7016 {
7017 const char *parent_scope;
7018
7019 /* If this is a template instantiation, we can not work out the
7020 template arguments from partial DIEs. So, unfortunately, we have
7021 to go through the full DIEs. At least any work we do building
7022 types here will be reused if full symbols are loaded later. */
7023 if (pdi->has_template_arguments)
7024 {
7025 fixup_partial_die (pdi, cu);
7026
7027 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
7028 {
7029 struct die_info *die;
7030 struct attribute attr;
7031 struct dwarf2_cu *ref_cu = cu;
7032
7033 /* DW_FORM_ref_addr is using section offset. */
7034 attr.name = (enum dwarf_attribute) 0;
7035 attr.form = DW_FORM_ref_addr;
7036 attr.u.unsnd = pdi->offset.sect_off;
7037 die = follow_die_ref (NULL, &attr, &ref_cu);
7038
7039 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
7040 }
7041 }
7042
7043 parent_scope = partial_die_parent_scope (pdi, cu);
7044 if (parent_scope == NULL)
7045 return NULL;
7046 else
7047 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
7048 }
7049
7050 static void
7051 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
7052 {
7053 struct objfile *objfile = cu->objfile;
7054 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7055 CORE_ADDR addr = 0;
7056 const char *actual_name = NULL;
7057 CORE_ADDR baseaddr;
7058 char *built_actual_name;
7059
7060 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7061
7062 built_actual_name = partial_die_full_name (pdi, cu);
7063 if (built_actual_name != NULL)
7064 actual_name = built_actual_name;
7065
7066 if (actual_name == NULL)
7067 actual_name = pdi->name;
7068
7069 switch (pdi->tag)
7070 {
7071 case DW_TAG_subprogram:
7072 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
7073 if (pdi->is_external || cu->language == language_ada)
7074 {
7075 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
7076 of the global scope. But in Ada, we want to be able to access
7077 nested procedures globally. So all Ada subprograms are stored
7078 in the global scope. */
7079 add_psymbol_to_list (actual_name, strlen (actual_name),
7080 built_actual_name != NULL,
7081 VAR_DOMAIN, LOC_BLOCK,
7082 &objfile->global_psymbols,
7083 addr, cu->language, objfile);
7084 }
7085 else
7086 {
7087 add_psymbol_to_list (actual_name, strlen (actual_name),
7088 built_actual_name != NULL,
7089 VAR_DOMAIN, LOC_BLOCK,
7090 &objfile->static_psymbols,
7091 addr, cu->language, objfile);
7092 }
7093
7094 if (pdi->main_subprogram && actual_name != NULL)
7095 set_objfile_main_name (objfile, actual_name, cu->language);
7096 break;
7097 case DW_TAG_constant:
7098 {
7099 struct psymbol_allocation_list *list;
7100
7101 if (pdi->is_external)
7102 list = &objfile->global_psymbols;
7103 else
7104 list = &objfile->static_psymbols;
7105 add_psymbol_to_list (actual_name, strlen (actual_name),
7106 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
7107 list, 0, cu->language, objfile);
7108 }
7109 break;
7110 case DW_TAG_variable:
7111 if (pdi->d.locdesc)
7112 addr = decode_locdesc (pdi->d.locdesc, cu);
7113
7114 if (pdi->d.locdesc
7115 && addr == 0
7116 && !dwarf2_per_objfile->has_section_at_zero)
7117 {
7118 /* A global or static variable may also have been stripped
7119 out by the linker if unused, in which case its address
7120 will be nullified; do not add such variables into partial
7121 symbol table then. */
7122 }
7123 else if (pdi->is_external)
7124 {
7125 /* Global Variable.
7126 Don't enter into the minimal symbol tables as there is
7127 a minimal symbol table entry from the ELF symbols already.
7128 Enter into partial symbol table if it has a location
7129 descriptor or a type.
7130 If the location descriptor is missing, new_symbol will create
7131 a LOC_UNRESOLVED symbol, the address of the variable will then
7132 be determined from the minimal symbol table whenever the variable
7133 is referenced.
7134 The address for the partial symbol table entry is not
7135 used by GDB, but it comes in handy for debugging partial symbol
7136 table building. */
7137
7138 if (pdi->d.locdesc || pdi->has_type)
7139 add_psymbol_to_list (actual_name, strlen (actual_name),
7140 built_actual_name != NULL,
7141 VAR_DOMAIN, LOC_STATIC,
7142 &objfile->global_psymbols,
7143 addr + baseaddr,
7144 cu->language, objfile);
7145 }
7146 else
7147 {
7148 int has_loc = pdi->d.locdesc != NULL;
7149
7150 /* Static Variable. Skip symbols whose value we cannot know (those
7151 without location descriptors or constant values). */
7152 if (!has_loc && !pdi->has_const_value)
7153 {
7154 xfree (built_actual_name);
7155 return;
7156 }
7157
7158 add_psymbol_to_list (actual_name, strlen (actual_name),
7159 built_actual_name != NULL,
7160 VAR_DOMAIN, LOC_STATIC,
7161 &objfile->static_psymbols,
7162 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
7163 cu->language, objfile);
7164 }
7165 break;
7166 case DW_TAG_typedef:
7167 case DW_TAG_base_type:
7168 case DW_TAG_subrange_type:
7169 add_psymbol_to_list (actual_name, strlen (actual_name),
7170 built_actual_name != NULL,
7171 VAR_DOMAIN, LOC_TYPEDEF,
7172 &objfile->static_psymbols,
7173 0, cu->language, objfile);
7174 break;
7175 case DW_TAG_imported_declaration:
7176 case DW_TAG_namespace:
7177 add_psymbol_to_list (actual_name, strlen (actual_name),
7178 built_actual_name != NULL,
7179 VAR_DOMAIN, LOC_TYPEDEF,
7180 &objfile->global_psymbols,
7181 0, cu->language, objfile);
7182 break;
7183 case DW_TAG_module:
7184 add_psymbol_to_list (actual_name, strlen (actual_name),
7185 built_actual_name != NULL,
7186 MODULE_DOMAIN, LOC_TYPEDEF,
7187 &objfile->global_psymbols,
7188 0, cu->language, objfile);
7189 break;
7190 case DW_TAG_class_type:
7191 case DW_TAG_interface_type:
7192 case DW_TAG_structure_type:
7193 case DW_TAG_union_type:
7194 case DW_TAG_enumeration_type:
7195 /* Skip external references. The DWARF standard says in the section
7196 about "Structure, Union, and Class Type Entries": "An incomplete
7197 structure, union or class type is represented by a structure,
7198 union or class entry that does not have a byte size attribute
7199 and that has a DW_AT_declaration attribute." */
7200 if (!pdi->has_byte_size && pdi->is_declaration)
7201 {
7202 xfree (built_actual_name);
7203 return;
7204 }
7205
7206 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
7207 static vs. global. */
7208 add_psymbol_to_list (actual_name, strlen (actual_name),
7209 built_actual_name != NULL,
7210 STRUCT_DOMAIN, LOC_TYPEDEF,
7211 cu->language == language_cplus
7212 ? &objfile->global_psymbols
7213 : &objfile->static_psymbols,
7214 0, cu->language, objfile);
7215
7216 break;
7217 case DW_TAG_enumerator:
7218 add_psymbol_to_list (actual_name, strlen (actual_name),
7219 built_actual_name != NULL,
7220 VAR_DOMAIN, LOC_CONST,
7221 cu->language == language_cplus
7222 ? &objfile->global_psymbols
7223 : &objfile->static_psymbols,
7224 0, cu->language, objfile);
7225 break;
7226 default:
7227 break;
7228 }
7229
7230 xfree (built_actual_name);
7231 }
7232
7233 /* Read a partial die corresponding to a namespace; also, add a symbol
7234 corresponding to that namespace to the symbol table. NAMESPACE is
7235 the name of the enclosing namespace. */
7236
7237 static void
7238 add_partial_namespace (struct partial_die_info *pdi,
7239 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7240 int set_addrmap, struct dwarf2_cu *cu)
7241 {
7242 /* Add a symbol for the namespace. */
7243
7244 add_partial_symbol (pdi, cu);
7245
7246 /* Now scan partial symbols in that namespace. */
7247
7248 if (pdi->has_children)
7249 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
7250 }
7251
7252 /* Read a partial die corresponding to a Fortran module. */
7253
7254 static void
7255 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
7256 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
7257 {
7258 /* Add a symbol for the namespace. */
7259
7260 add_partial_symbol (pdi, cu);
7261
7262 /* Now scan partial symbols in that module. */
7263
7264 if (pdi->has_children)
7265 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
7266 }
7267
7268 /* Read a partial die corresponding to a subprogram and create a partial
7269 symbol for that subprogram. When the CU language allows it, this
7270 routine also defines a partial symbol for each nested subprogram
7271 that this subprogram contains. If SET_ADDRMAP is true, record the
7272 covered ranges in the addrmap. Set *LOWPC and *HIGHPC to the lowest
7273 and highest PC values found in PDI.
7274
7275 PDI may also be a lexical block, in which case we simply search
7276 recursively for subprograms defined inside that lexical block.
7277 Again, this is only performed when the CU language allows this
7278 type of definitions. */
7279
7280 static void
7281 add_partial_subprogram (struct partial_die_info *pdi,
7282 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7283 int set_addrmap, struct dwarf2_cu *cu)
7284 {
7285 if (pdi->tag == DW_TAG_subprogram)
7286 {
7287 if (pdi->has_pc_info)
7288 {
7289 if (pdi->lowpc < *lowpc)
7290 *lowpc = pdi->lowpc;
7291 if (pdi->highpc > *highpc)
7292 *highpc = pdi->highpc;
7293 if (set_addrmap)
7294 {
7295 struct objfile *objfile = cu->objfile;
7296 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7297 CORE_ADDR baseaddr;
7298 CORE_ADDR highpc;
7299 CORE_ADDR lowpc;
7300
7301 baseaddr = ANOFFSET (objfile->section_offsets,
7302 SECT_OFF_TEXT (objfile));
7303 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7304 pdi->lowpc + baseaddr);
7305 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
7306 pdi->highpc + baseaddr);
7307 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
7308 cu->per_cu->v.psymtab);
7309 }
7310 }
7311
7312 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
7313 {
7314 if (!pdi->is_declaration)
7315 /* Ignore subprogram DIEs that do not have a name, they are
7316 illegal. Do not emit a complaint at this point, we will
7317 do so when we convert this psymtab into a symtab. */
7318 if (pdi->name)
7319 add_partial_symbol (pdi, cu);
7320 }
7321 }
7322
7323 if (! pdi->has_children)
7324 return;
7325
7326 if (cu->language == language_ada)
7327 {
7328 pdi = pdi->die_child;
7329 while (pdi != NULL)
7330 {
7331 fixup_partial_die (pdi, cu);
7332 if (pdi->tag == DW_TAG_subprogram
7333 || pdi->tag == DW_TAG_lexical_block)
7334 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7335 pdi = pdi->die_sibling;
7336 }
7337 }
7338 }
7339
7340 /* Read a partial die corresponding to an enumeration type. */
7341
7342 static void
7343 add_partial_enumeration (struct partial_die_info *enum_pdi,
7344 struct dwarf2_cu *cu)
7345 {
7346 struct partial_die_info *pdi;
7347
7348 if (enum_pdi->name != NULL)
7349 add_partial_symbol (enum_pdi, cu);
7350
7351 pdi = enum_pdi->die_child;
7352 while (pdi)
7353 {
7354 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
7355 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
7356 else
7357 add_partial_symbol (pdi, cu);
7358 pdi = pdi->die_sibling;
7359 }
7360 }
7361
7362 /* Return the initial uleb128 in the die at INFO_PTR. */
7363
7364 static unsigned int
7365 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
7366 {
7367 unsigned int bytes_read;
7368
7369 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7370 }
7371
7372 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
7373 Return the corresponding abbrev, or NULL if the number is zero (indicating
7374 an empty DIE). In either case *BYTES_READ will be set to the length of
7375 the initial number. */
7376
7377 static struct abbrev_info *
7378 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
7379 struct dwarf2_cu *cu)
7380 {
7381 bfd *abfd = cu->objfile->obfd;
7382 unsigned int abbrev_number;
7383 struct abbrev_info *abbrev;
7384
7385 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
7386
7387 if (abbrev_number == 0)
7388 return NULL;
7389
7390 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
7391 if (!abbrev)
7392 {
7393 error (_("Dwarf Error: Could not find abbrev number %d in %s"
7394 " at offset 0x%x [in module %s]"),
7395 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
7396 cu->header.offset.sect_off, bfd_get_filename (abfd));
7397 }
7398
7399 return abbrev;
7400 }
7401
7402 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7403 Returns a pointer to the end of a series of DIEs, terminated by an empty
7404 DIE. Any children of the skipped DIEs will also be skipped. */
7405
7406 static const gdb_byte *
7407 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
7408 {
7409 struct dwarf2_cu *cu = reader->cu;
7410 struct abbrev_info *abbrev;
7411 unsigned int bytes_read;
7412
7413 while (1)
7414 {
7415 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7416 if (abbrev == NULL)
7417 return info_ptr + bytes_read;
7418 else
7419 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
7420 }
7421 }
7422
7423 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
7424 INFO_PTR should point just after the initial uleb128 of a DIE, and the
7425 abbrev corresponding to that skipped uleb128 should be passed in
7426 ABBREV. Returns a pointer to this DIE's sibling, skipping any
7427 children. */
7428
7429 static const gdb_byte *
7430 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7431 struct abbrev_info *abbrev)
7432 {
7433 unsigned int bytes_read;
7434 struct attribute attr;
7435 bfd *abfd = reader->abfd;
7436 struct dwarf2_cu *cu = reader->cu;
7437 const gdb_byte *buffer = reader->buffer;
7438 const gdb_byte *buffer_end = reader->buffer_end;
7439 unsigned int form, i;
7440
7441 for (i = 0; i < abbrev->num_attrs; i++)
7442 {
7443 /* The only abbrev we care about is DW_AT_sibling. */
7444 if (abbrev->attrs[i].name == DW_AT_sibling)
7445 {
7446 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
7447 if (attr.form == DW_FORM_ref_addr)
7448 complaint (&symfile_complaints,
7449 _("ignoring absolute DW_AT_sibling"));
7450 else
7451 {
7452 unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
7453 const gdb_byte *sibling_ptr = buffer + off;
7454
7455 if (sibling_ptr < info_ptr)
7456 complaint (&symfile_complaints,
7457 _("DW_AT_sibling points backwards"));
7458 else if (sibling_ptr > reader->buffer_end)
7459 dwarf2_section_buffer_overflow_complaint (reader->die_section);
7460 else
7461 return sibling_ptr;
7462 }
7463 }
7464
7465 /* If it isn't DW_AT_sibling, skip this attribute. */
7466 form = abbrev->attrs[i].form;
7467 skip_attribute:
7468 switch (form)
7469 {
7470 case DW_FORM_ref_addr:
7471 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
7472 and later it is offset sized. */
7473 if (cu->header.version == 2)
7474 info_ptr += cu->header.addr_size;
7475 else
7476 info_ptr += cu->header.offset_size;
7477 break;
7478 case DW_FORM_GNU_ref_alt:
7479 info_ptr += cu->header.offset_size;
7480 break;
7481 case DW_FORM_addr:
7482 info_ptr += cu->header.addr_size;
7483 break;
7484 case DW_FORM_data1:
7485 case DW_FORM_ref1:
7486 case DW_FORM_flag:
7487 info_ptr += 1;
7488 break;
7489 case DW_FORM_flag_present:
7490 case DW_FORM_implicit_const:
7491 break;
7492 case DW_FORM_data2:
7493 case DW_FORM_ref2:
7494 info_ptr += 2;
7495 break;
7496 case DW_FORM_data4:
7497 case DW_FORM_ref4:
7498 info_ptr += 4;
7499 break;
7500 case DW_FORM_data8:
7501 case DW_FORM_ref8:
7502 case DW_FORM_ref_sig8:
7503 info_ptr += 8;
7504 break;
7505 case DW_FORM_data16:
7506 info_ptr += 16;
7507 break;
7508 case DW_FORM_string:
7509 read_direct_string (abfd, info_ptr, &bytes_read);
7510 info_ptr += bytes_read;
7511 break;
7512 case DW_FORM_sec_offset:
7513 case DW_FORM_strp:
7514 case DW_FORM_GNU_strp_alt:
7515 info_ptr += cu->header.offset_size;
7516 break;
7517 case DW_FORM_exprloc:
7518 case DW_FORM_block:
7519 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7520 info_ptr += bytes_read;
7521 break;
7522 case DW_FORM_block1:
7523 info_ptr += 1 + read_1_byte (abfd, info_ptr);
7524 break;
7525 case DW_FORM_block2:
7526 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
7527 break;
7528 case DW_FORM_block4:
7529 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
7530 break;
7531 case DW_FORM_sdata:
7532 case DW_FORM_udata:
7533 case DW_FORM_ref_udata:
7534 case DW_FORM_GNU_addr_index:
7535 case DW_FORM_GNU_str_index:
7536 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
7537 break;
7538 case DW_FORM_indirect:
7539 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7540 info_ptr += bytes_read;
7541 /* We need to continue parsing from here, so just go back to
7542 the top. */
7543 goto skip_attribute;
7544
7545 default:
7546 error (_("Dwarf Error: Cannot handle %s "
7547 "in DWARF reader [in module %s]"),
7548 dwarf_form_name (form),
7549 bfd_get_filename (abfd));
7550 }
7551 }
7552
7553 if (abbrev->has_children)
7554 return skip_children (reader, info_ptr);
7555 else
7556 return info_ptr;
7557 }
7558
7559 /* Locate ORIG_PDI's sibling.
7560 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
7561
7562 static const gdb_byte *
7563 locate_pdi_sibling (const struct die_reader_specs *reader,
7564 struct partial_die_info *orig_pdi,
7565 const gdb_byte *info_ptr)
7566 {
7567 /* Do we know the sibling already? */
7568
7569 if (orig_pdi->sibling)
7570 return orig_pdi->sibling;
7571
7572 /* Are there any children to deal with? */
7573
7574 if (!orig_pdi->has_children)
7575 return info_ptr;
7576
7577 /* Skip the children the long way. */
7578
7579 return skip_children (reader, info_ptr);
7580 }
7581
7582 /* Expand this partial symbol table into a full symbol table. SELF is
7583 not NULL. */
7584
7585 static void
7586 dwarf2_read_symtab (struct partial_symtab *self,
7587 struct objfile *objfile)
7588 {
7589 if (self->readin)
7590 {
7591 warning (_("bug: psymtab for %s is already read in."),
7592 self->filename);
7593 }
7594 else
7595 {
7596 if (info_verbose)
7597 {
7598 printf_filtered (_("Reading in symbols for %s..."),
7599 self->filename);
7600 gdb_flush (gdb_stdout);
7601 }
7602
7603 /* Restore our global data. */
7604 dwarf2_per_objfile
7605 = (struct dwarf2_per_objfile *) objfile_data (objfile,
7606 dwarf2_objfile_data_key);
7607
7608 /* If this psymtab is constructed from a debug-only objfile, the
7609 has_section_at_zero flag will not necessarily be correct. We
7610 can get the correct value for this flag by looking at the data
7611 associated with the (presumably stripped) associated objfile. */
7612 if (objfile->separate_debug_objfile_backlink)
7613 {
7614 struct dwarf2_per_objfile *dpo_backlink
7615 = ((struct dwarf2_per_objfile *)
7616 objfile_data (objfile->separate_debug_objfile_backlink,
7617 dwarf2_objfile_data_key));
7618
7619 dwarf2_per_objfile->has_section_at_zero
7620 = dpo_backlink->has_section_at_zero;
7621 }
7622
7623 dwarf2_per_objfile->reading_partial_symbols = 0;
7624
7625 psymtab_to_symtab_1 (self);
7626
7627 /* Finish up the debug error message. */
7628 if (info_verbose)
7629 printf_filtered (_("done.\n"));
7630 }
7631
7632 process_cu_includes ();
7633 }
7634 \f
7635 /* Reading in full CUs. */
7636
7637 /* Add PER_CU to the queue. */
7638
7639 static void
7640 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
7641 enum language pretend_language)
7642 {
7643 struct dwarf2_queue_item *item;
7644
7645 per_cu->queued = 1;
7646 item = XNEW (struct dwarf2_queue_item);
7647 item->per_cu = per_cu;
7648 item->pretend_language = pretend_language;
7649 item->next = NULL;
7650
7651 if (dwarf2_queue == NULL)
7652 dwarf2_queue = item;
7653 else
7654 dwarf2_queue_tail->next = item;
7655
7656 dwarf2_queue_tail = item;
7657 }
7658
7659 /* If PER_CU is not yet queued, add it to the queue.
7660 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
7661 dependency.
7662 The result is non-zero if PER_CU was queued, otherwise the result is zero
7663 meaning either PER_CU is already queued or it is already loaded.
7664
7665 N.B. There is an invariant here that if a CU is queued then it is loaded.
7666 The caller is required to load PER_CU if we return non-zero. */
7667
7668 static int
7669 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
7670 struct dwarf2_per_cu_data *per_cu,
7671 enum language pretend_language)
7672 {
7673 /* We may arrive here during partial symbol reading, if we need full
7674 DIEs to process an unusual case (e.g. template arguments). Do
7675 not queue PER_CU, just tell our caller to load its DIEs. */
7676 if (dwarf2_per_objfile->reading_partial_symbols)
7677 {
7678 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
7679 return 1;
7680 return 0;
7681 }
7682
7683 /* Mark the dependence relation so that we don't flush PER_CU
7684 too early. */
7685 if (dependent_cu != NULL)
7686 dwarf2_add_dependence (dependent_cu, per_cu);
7687
7688 /* If it's already on the queue, we have nothing to do. */
7689 if (per_cu->queued)
7690 return 0;
7691
7692 /* If the compilation unit is already loaded, just mark it as
7693 used. */
7694 if (per_cu->cu != NULL)
7695 {
7696 per_cu->cu->last_used = 0;
7697 return 0;
7698 }
7699
7700 /* Add it to the queue. */
7701 queue_comp_unit (per_cu, pretend_language);
7702
7703 return 1;
7704 }
7705
7706 /* Process the queue. */
7707
7708 static void
7709 process_queue (void)
7710 {
7711 struct dwarf2_queue_item *item, *next_item;
7712
7713 if (dwarf_read_debug)
7714 {
7715 fprintf_unfiltered (gdb_stdlog,
7716 "Expanding one or more symtabs of objfile %s ...\n",
7717 objfile_name (dwarf2_per_objfile->objfile));
7718 }
7719
7720 /* The queue starts out with one item, but following a DIE reference
7721 may load a new CU, adding it to the end of the queue. */
7722 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
7723 {
7724 if ((dwarf2_per_objfile->using_index
7725 ? !item->per_cu->v.quick->compunit_symtab
7726 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7727 /* Skip dummy CUs. */
7728 && item->per_cu->cu != NULL)
7729 {
7730 struct dwarf2_per_cu_data *per_cu = item->per_cu;
7731 unsigned int debug_print_threshold;
7732 char buf[100];
7733
7734 if (per_cu->is_debug_types)
7735 {
7736 struct signatured_type *sig_type =
7737 (struct signatured_type *) per_cu;
7738
7739 sprintf (buf, "TU %s at offset 0x%x",
7740 hex_string (sig_type->signature),
7741 per_cu->offset.sect_off);
7742 /* There can be 100s of TUs.
7743 Only print them in verbose mode. */
7744 debug_print_threshold = 2;
7745 }
7746 else
7747 {
7748 sprintf (buf, "CU at offset 0x%x", per_cu->offset.sect_off);
7749 debug_print_threshold = 1;
7750 }
7751
7752 if (dwarf_read_debug >= debug_print_threshold)
7753 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
7754
7755 if (per_cu->is_debug_types)
7756 process_full_type_unit (per_cu, item->pretend_language);
7757 else
7758 process_full_comp_unit (per_cu, item->pretend_language);
7759
7760 if (dwarf_read_debug >= debug_print_threshold)
7761 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
7762 }
7763
7764 item->per_cu->queued = 0;
7765 next_item = item->next;
7766 xfree (item);
7767 }
7768
7769 dwarf2_queue_tail = NULL;
7770
7771 if (dwarf_read_debug)
7772 {
7773 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
7774 objfile_name (dwarf2_per_objfile->objfile));
7775 }
7776 }
7777
7778 /* Free all allocated queue entries. This function only releases anything if
7779 an error was thrown; if the queue was processed then it would have been
7780 freed as we went along. */
7781
7782 static void
7783 dwarf2_release_queue (void *dummy)
7784 {
7785 struct dwarf2_queue_item *item, *last;
7786
7787 item = dwarf2_queue;
7788 while (item)
7789 {
7790 /* Anything still marked queued is likely to be in an
7791 inconsistent state, so discard it. */
7792 if (item->per_cu->queued)
7793 {
7794 if (item->per_cu->cu != NULL)
7795 free_one_cached_comp_unit (item->per_cu);
7796 item->per_cu->queued = 0;
7797 }
7798
7799 last = item;
7800 item = item->next;
7801 xfree (last);
7802 }
7803
7804 dwarf2_queue = dwarf2_queue_tail = NULL;
7805 }
7806
7807 /* Read in full symbols for PST, and anything it depends on. */
7808
7809 static void
7810 psymtab_to_symtab_1 (struct partial_symtab *pst)
7811 {
7812 struct dwarf2_per_cu_data *per_cu;
7813 int i;
7814
7815 if (pst->readin)
7816 return;
7817
7818 for (i = 0; i < pst->number_of_dependencies; i++)
7819 if (!pst->dependencies[i]->readin
7820 && pst->dependencies[i]->user == NULL)
7821 {
7822 /* Inform about additional files that need to be read in. */
7823 if (info_verbose)
7824 {
7825 /* FIXME: i18n: Need to make this a single string. */
7826 fputs_filtered (" ", gdb_stdout);
7827 wrap_here ("");
7828 fputs_filtered ("and ", gdb_stdout);
7829 wrap_here ("");
7830 printf_filtered ("%s...", pst->dependencies[i]->filename);
7831 wrap_here (""); /* Flush output. */
7832 gdb_flush (gdb_stdout);
7833 }
7834 psymtab_to_symtab_1 (pst->dependencies[i]);
7835 }
7836
7837 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
7838
7839 if (per_cu == NULL)
7840 {
7841 /* It's an include file, no symbols to read for it.
7842 Everything is in the parent symtab. */
7843 pst->readin = 1;
7844 return;
7845 }
7846
7847 dw2_do_instantiate_symtab (per_cu);
7848 }
7849
7850 /* Trivial hash function for die_info: the hash value of a DIE
7851 is its offset in .debug_info for this objfile. */
7852
7853 static hashval_t
7854 die_hash (const void *item)
7855 {
7856 const struct die_info *die = (const struct die_info *) item;
7857
7858 return die->offset.sect_off;
7859 }
7860
7861 /* Trivial comparison function for die_info structures: two DIEs
7862 are equal if they have the same offset. */
7863
7864 static int
7865 die_eq (const void *item_lhs, const void *item_rhs)
7866 {
7867 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
7868 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
7869
7870 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
7871 }
7872
7873 /* die_reader_func for load_full_comp_unit.
7874 This is identical to read_signatured_type_reader,
7875 but is kept separate for now. */
7876
7877 static void
7878 load_full_comp_unit_reader (const struct die_reader_specs *reader,
7879 const gdb_byte *info_ptr,
7880 struct die_info *comp_unit_die,
7881 int has_children,
7882 void *data)
7883 {
7884 struct dwarf2_cu *cu = reader->cu;
7885 enum language *language_ptr = (enum language *) data;
7886
7887 gdb_assert (cu->die_hash == NULL);
7888 cu->die_hash =
7889 htab_create_alloc_ex (cu->header.length / 12,
7890 die_hash,
7891 die_eq,
7892 NULL,
7893 &cu->comp_unit_obstack,
7894 hashtab_obstack_allocate,
7895 dummy_obstack_deallocate);
7896
7897 if (has_children)
7898 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7899 &info_ptr, comp_unit_die);
7900 cu->dies = comp_unit_die;
7901 /* comp_unit_die is not stored in die_hash, no need. */
7902
7903 /* We try not to read any attributes in this function, because not
7904 all CUs needed for references have been loaded yet, and symbol
7905 table processing isn't initialized. But we have to set the CU language,
7906 or we won't be able to build types correctly.
7907 Similarly, if we do not read the producer, we can not apply
7908 producer-specific interpretation. */
7909 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
7910 }
7911
7912 /* Load the DIEs associated with PER_CU into memory. */
7913
7914 static void
7915 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7916 enum language pretend_language)
7917 {
7918 gdb_assert (! this_cu->is_debug_types);
7919
7920 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7921 load_full_comp_unit_reader, &pretend_language);
7922 }
7923
7924 /* Add a DIE to the delayed physname list. */
7925
7926 static void
7927 add_to_method_list (struct type *type, int fnfield_index, int index,
7928 const char *name, struct die_info *die,
7929 struct dwarf2_cu *cu)
7930 {
7931 struct delayed_method_info mi;
7932 mi.type = type;
7933 mi.fnfield_index = fnfield_index;
7934 mi.index = index;
7935 mi.name = name;
7936 mi.die = die;
7937 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7938 }
7939
7940 /* A cleanup for freeing the delayed method list. */
7941
7942 static void
7943 free_delayed_list (void *ptr)
7944 {
7945 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7946 if (cu->method_list != NULL)
7947 {
7948 VEC_free (delayed_method_info, cu->method_list);
7949 cu->method_list = NULL;
7950 }
7951 }
7952
7953 /* Compute the physnames of any methods on the CU's method list.
7954
7955 The computation of method physnames is delayed in order to avoid the
7956 (bad) condition that one of the method's formal parameters is of an as yet
7957 incomplete type. */
7958
7959 static void
7960 compute_delayed_physnames (struct dwarf2_cu *cu)
7961 {
7962 int i;
7963 struct delayed_method_info *mi;
7964 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7965 {
7966 const char *physname;
7967 struct fn_fieldlist *fn_flp
7968 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7969 physname = dwarf2_physname (mi->name, mi->die, cu);
7970 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi->index)
7971 = physname ? physname : "";
7972 }
7973 }
7974
7975 /* Go objects should be embedded in a DW_TAG_module DIE,
7976 and it's not clear if/how imported objects will appear.
7977 To keep Go support simple until that's worked out,
7978 go back through what we've read and create something usable.
7979 We could do this while processing each DIE, and feels kinda cleaner,
7980 but that way is more invasive.
7981 This is to, for example, allow the user to type "p var" or "b main"
7982 without having to specify the package name, and allow lookups
7983 of module.object to work in contexts that use the expression
7984 parser. */
7985
7986 static void
7987 fixup_go_packaging (struct dwarf2_cu *cu)
7988 {
7989 char *package_name = NULL;
7990 struct pending *list;
7991 int i;
7992
7993 for (list = global_symbols; list != NULL; list = list->next)
7994 {
7995 for (i = 0; i < list->nsyms; ++i)
7996 {
7997 struct symbol *sym = list->symbol[i];
7998
7999 if (SYMBOL_LANGUAGE (sym) == language_go
8000 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8001 {
8002 char *this_package_name = go_symbol_package_name (sym);
8003
8004 if (this_package_name == NULL)
8005 continue;
8006 if (package_name == NULL)
8007 package_name = this_package_name;
8008 else
8009 {
8010 if (strcmp (package_name, this_package_name) != 0)
8011 complaint (&symfile_complaints,
8012 _("Symtab %s has objects from two different Go packages: %s and %s"),
8013 (symbol_symtab (sym) != NULL
8014 ? symtab_to_filename_for_display
8015 (symbol_symtab (sym))
8016 : objfile_name (cu->objfile)),
8017 this_package_name, package_name);
8018 xfree (this_package_name);
8019 }
8020 }
8021 }
8022 }
8023
8024 if (package_name != NULL)
8025 {
8026 struct objfile *objfile = cu->objfile;
8027 const char *saved_package_name
8028 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
8029 package_name,
8030 strlen (package_name));
8031 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8032 saved_package_name);
8033 struct symbol *sym;
8034
8035 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8036
8037 sym = allocate_symbol (objfile);
8038 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
8039 SYMBOL_SET_NAMES (sym, saved_package_name,
8040 strlen (saved_package_name), 0, objfile);
8041 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8042 e.g., "main" finds the "main" module and not C's main(). */
8043 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
8044 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
8045 SYMBOL_TYPE (sym) = type;
8046
8047 add_symbol_to_list (sym, &global_symbols);
8048
8049 xfree (package_name);
8050 }
8051 }
8052
8053 /* Return the symtab for PER_CU. This works properly regardless of
8054 whether we're using the index or psymtabs. */
8055
8056 static struct compunit_symtab *
8057 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
8058 {
8059 return (dwarf2_per_objfile->using_index
8060 ? per_cu->v.quick->compunit_symtab
8061 : per_cu->v.psymtab->compunit_symtab);
8062 }
8063
8064 /* A helper function for computing the list of all symbol tables
8065 included by PER_CU. */
8066
8067 static void
8068 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
8069 htab_t all_children, htab_t all_type_symtabs,
8070 struct dwarf2_per_cu_data *per_cu,
8071 struct compunit_symtab *immediate_parent)
8072 {
8073 void **slot;
8074 int ix;
8075 struct compunit_symtab *cust;
8076 struct dwarf2_per_cu_data *iter;
8077
8078 slot = htab_find_slot (all_children, per_cu, INSERT);
8079 if (*slot != NULL)
8080 {
8081 /* This inclusion and its children have been processed. */
8082 return;
8083 }
8084
8085 *slot = per_cu;
8086 /* Only add a CU if it has a symbol table. */
8087 cust = get_compunit_symtab (per_cu);
8088 if (cust != NULL)
8089 {
8090 /* If this is a type unit only add its symbol table if we haven't
8091 seen it yet (type unit per_cu's can share symtabs). */
8092 if (per_cu->is_debug_types)
8093 {
8094 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
8095 if (*slot == NULL)
8096 {
8097 *slot = cust;
8098 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8099 if (cust->user == NULL)
8100 cust->user = immediate_parent;
8101 }
8102 }
8103 else
8104 {
8105 VEC_safe_push (compunit_symtab_ptr, *result, cust);
8106 if (cust->user == NULL)
8107 cust->user = immediate_parent;
8108 }
8109 }
8110
8111 for (ix = 0;
8112 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
8113 ++ix)
8114 {
8115 recursively_compute_inclusions (result, all_children,
8116 all_type_symtabs, iter, cust);
8117 }
8118 }
8119
8120 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
8121 PER_CU. */
8122
8123 static void
8124 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
8125 {
8126 gdb_assert (! per_cu->is_debug_types);
8127
8128 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
8129 {
8130 int ix, len;
8131 struct dwarf2_per_cu_data *per_cu_iter;
8132 struct compunit_symtab *compunit_symtab_iter;
8133 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
8134 htab_t all_children, all_type_symtabs;
8135 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
8136
8137 /* If we don't have a symtab, we can just skip this case. */
8138 if (cust == NULL)
8139 return;
8140
8141 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8142 NULL, xcalloc, xfree);
8143 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
8144 NULL, xcalloc, xfree);
8145
8146 for (ix = 0;
8147 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
8148 ix, per_cu_iter);
8149 ++ix)
8150 {
8151 recursively_compute_inclusions (&result_symtabs, all_children,
8152 all_type_symtabs, per_cu_iter,
8153 cust);
8154 }
8155
8156 /* Now we have a transitive closure of all the included symtabs. */
8157 len = VEC_length (compunit_symtab_ptr, result_symtabs);
8158 cust->includes
8159 = XOBNEWVEC (&dwarf2_per_objfile->objfile->objfile_obstack,
8160 struct compunit_symtab *, len + 1);
8161 for (ix = 0;
8162 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
8163 compunit_symtab_iter);
8164 ++ix)
8165 cust->includes[ix] = compunit_symtab_iter;
8166 cust->includes[len] = NULL;
8167
8168 VEC_free (compunit_symtab_ptr, result_symtabs);
8169 htab_delete (all_children);
8170 htab_delete (all_type_symtabs);
8171 }
8172 }
8173
8174 /* Compute the 'includes' field for the symtabs of all the CUs we just
8175 read. */
8176
8177 static void
8178 process_cu_includes (void)
8179 {
8180 int ix;
8181 struct dwarf2_per_cu_data *iter;
8182
8183 for (ix = 0;
8184 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
8185 ix, iter);
8186 ++ix)
8187 {
8188 if (! iter->is_debug_types)
8189 compute_compunit_symtab_includes (iter);
8190 }
8191
8192 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
8193 }
8194
8195 /* Generate full symbol information for PER_CU, whose DIEs have
8196 already been loaded into memory. */
8197
8198 static void
8199 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
8200 enum language pretend_language)
8201 {
8202 struct dwarf2_cu *cu = per_cu->cu;
8203 struct objfile *objfile = per_cu->objfile;
8204 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8205 CORE_ADDR lowpc, highpc;
8206 struct compunit_symtab *cust;
8207 struct cleanup *back_to, *delayed_list_cleanup;
8208 CORE_ADDR baseaddr;
8209 struct block *static_block;
8210 CORE_ADDR addr;
8211
8212 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8213
8214 buildsym_init ();
8215 back_to = make_cleanup (really_free_pendings, NULL);
8216 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8217
8218 cu->list_in_scope = &file_symbols;
8219
8220 cu->language = pretend_language;
8221 cu->language_defn = language_def (cu->language);
8222
8223 /* Do line number decoding in read_file_scope () */
8224 process_die (cu->dies, cu);
8225
8226 /* For now fudge the Go package. */
8227 if (cu->language == language_go)
8228 fixup_go_packaging (cu);
8229
8230 /* Now that we have processed all the DIEs in the CU, all the types
8231 should be complete, and it should now be safe to compute all of the
8232 physnames. */
8233 compute_delayed_physnames (cu);
8234 do_cleanups (delayed_list_cleanup);
8235
8236 /* Some compilers don't define a DW_AT_high_pc attribute for the
8237 compilation unit. If the DW_AT_high_pc is missing, synthesize
8238 it, by scanning the DIE's below the compilation unit. */
8239 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
8240
8241 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
8242 static_block = end_symtab_get_static_block (addr, 0, 1);
8243
8244 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
8245 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
8246 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
8247 addrmap to help ensure it has an accurate map of pc values belonging to
8248 this comp unit. */
8249 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
8250
8251 cust = end_symtab_from_static_block (static_block,
8252 SECT_OFF_TEXT (objfile), 0);
8253
8254 if (cust != NULL)
8255 {
8256 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
8257
8258 /* Set symtab language to language from DW_AT_language. If the
8259 compilation is from a C file generated by language preprocessors, do
8260 not set the language if it was already deduced by start_subfile. */
8261 if (!(cu->language == language_c
8262 && COMPUNIT_FILETABS (cust)->language != language_unknown))
8263 COMPUNIT_FILETABS (cust)->language = cu->language;
8264
8265 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
8266 produce DW_AT_location with location lists but it can be possibly
8267 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
8268 there were bugs in prologue debug info, fixed later in GCC-4.5
8269 by "unwind info for epilogues" patch (which is not directly related).
8270
8271 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
8272 needed, it would be wrong due to missing DW_AT_producer there.
8273
8274 Still one can confuse GDB by using non-standard GCC compilation
8275 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
8276 */
8277 if (cu->has_loclist && gcc_4_minor >= 5)
8278 cust->locations_valid = 1;
8279
8280 if (gcc_4_minor >= 5)
8281 cust->epilogue_unwind_valid = 1;
8282
8283 cust->call_site_htab = cu->call_site_htab;
8284 }
8285
8286 if (dwarf2_per_objfile->using_index)
8287 per_cu->v.quick->compunit_symtab = cust;
8288 else
8289 {
8290 struct partial_symtab *pst = per_cu->v.psymtab;
8291 pst->compunit_symtab = cust;
8292 pst->readin = 1;
8293 }
8294
8295 /* Push it for inclusion processing later. */
8296 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
8297
8298 do_cleanups (back_to);
8299 }
8300
8301 /* Generate full symbol information for type unit PER_CU, whose DIEs have
8302 already been loaded into memory. */
8303
8304 static void
8305 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
8306 enum language pretend_language)
8307 {
8308 struct dwarf2_cu *cu = per_cu->cu;
8309 struct objfile *objfile = per_cu->objfile;
8310 struct compunit_symtab *cust;
8311 struct cleanup *back_to, *delayed_list_cleanup;
8312 struct signatured_type *sig_type;
8313
8314 gdb_assert (per_cu->is_debug_types);
8315 sig_type = (struct signatured_type *) per_cu;
8316
8317 buildsym_init ();
8318 back_to = make_cleanup (really_free_pendings, NULL);
8319 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
8320
8321 cu->list_in_scope = &file_symbols;
8322
8323 cu->language = pretend_language;
8324 cu->language_defn = language_def (cu->language);
8325
8326 /* The symbol tables are set up in read_type_unit_scope. */
8327 process_die (cu->dies, cu);
8328
8329 /* For now fudge the Go package. */
8330 if (cu->language == language_go)
8331 fixup_go_packaging (cu);
8332
8333 /* Now that we have processed all the DIEs in the CU, all the types
8334 should be complete, and it should now be safe to compute all of the
8335 physnames. */
8336 compute_delayed_physnames (cu);
8337 do_cleanups (delayed_list_cleanup);
8338
8339 /* TUs share symbol tables.
8340 If this is the first TU to use this symtab, complete the construction
8341 of it with end_expandable_symtab. Otherwise, complete the addition of
8342 this TU's symbols to the existing symtab. */
8343 if (sig_type->type_unit_group->compunit_symtab == NULL)
8344 {
8345 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8346 sig_type->type_unit_group->compunit_symtab = cust;
8347
8348 if (cust != NULL)
8349 {
8350 /* Set symtab language to language from DW_AT_language. If the
8351 compilation is from a C file generated by language preprocessors,
8352 do not set the language if it was already deduced by
8353 start_subfile. */
8354 if (!(cu->language == language_c
8355 && COMPUNIT_FILETABS (cust)->language != language_c))
8356 COMPUNIT_FILETABS (cust)->language = cu->language;
8357 }
8358 }
8359 else
8360 {
8361 augment_type_symtab ();
8362 cust = sig_type->type_unit_group->compunit_symtab;
8363 }
8364
8365 if (dwarf2_per_objfile->using_index)
8366 per_cu->v.quick->compunit_symtab = cust;
8367 else
8368 {
8369 struct partial_symtab *pst = per_cu->v.psymtab;
8370 pst->compunit_symtab = cust;
8371 pst->readin = 1;
8372 }
8373
8374 do_cleanups (back_to);
8375 }
8376
8377 /* Process an imported unit DIE. */
8378
8379 static void
8380 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
8381 {
8382 struct attribute *attr;
8383
8384 /* For now we don't handle imported units in type units. */
8385 if (cu->per_cu->is_debug_types)
8386 {
8387 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8388 " supported in type units [in module %s]"),
8389 objfile_name (cu->objfile));
8390 }
8391
8392 attr = dwarf2_attr (die, DW_AT_import, cu);
8393 if (attr != NULL)
8394 {
8395 struct dwarf2_per_cu_data *per_cu;
8396 sect_offset offset;
8397 int is_dwz;
8398
8399 offset = dwarf2_get_ref_die_offset (attr);
8400 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
8401 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
8402
8403 /* If necessary, add it to the queue and load its DIEs. */
8404 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
8405 load_full_comp_unit (per_cu, cu->language);
8406
8407 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8408 per_cu);
8409 }
8410 }
8411
8412 /* Reset the in_process bit of a die. */
8413
8414 static void
8415 reset_die_in_process (void *arg)
8416 {
8417 struct die_info *die = (struct die_info *) arg;
8418
8419 die->in_process = 0;
8420 }
8421
8422 /* Process a die and its children. */
8423
8424 static void
8425 process_die (struct die_info *die, struct dwarf2_cu *cu)
8426 {
8427 struct cleanup *in_process;
8428
8429 /* We should only be processing those not already in process. */
8430 gdb_assert (!die->in_process);
8431
8432 die->in_process = 1;
8433 in_process = make_cleanup (reset_die_in_process,die);
8434
8435 switch (die->tag)
8436 {
8437 case DW_TAG_padding:
8438 break;
8439 case DW_TAG_compile_unit:
8440 case DW_TAG_partial_unit:
8441 read_file_scope (die, cu);
8442 break;
8443 case DW_TAG_type_unit:
8444 read_type_unit_scope (die, cu);
8445 break;
8446 case DW_TAG_subprogram:
8447 case DW_TAG_inlined_subroutine:
8448 read_func_scope (die, cu);
8449 break;
8450 case DW_TAG_lexical_block:
8451 case DW_TAG_try_block:
8452 case DW_TAG_catch_block:
8453 read_lexical_block_scope (die, cu);
8454 break;
8455 case DW_TAG_call_site:
8456 case DW_TAG_GNU_call_site:
8457 read_call_site_scope (die, cu);
8458 break;
8459 case DW_TAG_class_type:
8460 case DW_TAG_interface_type:
8461 case DW_TAG_structure_type:
8462 case DW_TAG_union_type:
8463 process_structure_scope (die, cu);
8464 break;
8465 case DW_TAG_enumeration_type:
8466 process_enumeration_scope (die, cu);
8467 break;
8468
8469 /* These dies have a type, but processing them does not create
8470 a symbol or recurse to process the children. Therefore we can
8471 read them on-demand through read_type_die. */
8472 case DW_TAG_subroutine_type:
8473 case DW_TAG_set_type:
8474 case DW_TAG_array_type:
8475 case DW_TAG_pointer_type:
8476 case DW_TAG_ptr_to_member_type:
8477 case DW_TAG_reference_type:
8478 case DW_TAG_rvalue_reference_type:
8479 case DW_TAG_string_type:
8480 break;
8481
8482 case DW_TAG_base_type:
8483 case DW_TAG_subrange_type:
8484 case DW_TAG_typedef:
8485 /* Add a typedef symbol for the type definition, if it has a
8486 DW_AT_name. */
8487 new_symbol (die, read_type_die (die, cu), cu);
8488 break;
8489 case DW_TAG_common_block:
8490 read_common_block (die, cu);
8491 break;
8492 case DW_TAG_common_inclusion:
8493 break;
8494 case DW_TAG_namespace:
8495 cu->processing_has_namespace_info = 1;
8496 read_namespace (die, cu);
8497 break;
8498 case DW_TAG_module:
8499 cu->processing_has_namespace_info = 1;
8500 read_module (die, cu);
8501 break;
8502 case DW_TAG_imported_declaration:
8503 cu->processing_has_namespace_info = 1;
8504 if (read_namespace_alias (die, cu))
8505 break;
8506 /* The declaration is not a global namespace alias: fall through. */
8507 case DW_TAG_imported_module:
8508 cu->processing_has_namespace_info = 1;
8509 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
8510 || cu->language != language_fortran))
8511 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
8512 dwarf_tag_name (die->tag));
8513 read_import_statement (die, cu);
8514 break;
8515
8516 case DW_TAG_imported_unit:
8517 process_imported_unit_die (die, cu);
8518 break;
8519
8520 default:
8521 new_symbol (die, NULL, cu);
8522 break;
8523 }
8524
8525 do_cleanups (in_process);
8526 }
8527 \f
8528 /* DWARF name computation. */
8529
8530 /* A helper function for dwarf2_compute_name which determines whether DIE
8531 needs to have the name of the scope prepended to the name listed in the
8532 die. */
8533
8534 static int
8535 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
8536 {
8537 struct attribute *attr;
8538
8539 switch (die->tag)
8540 {
8541 case DW_TAG_namespace:
8542 case DW_TAG_typedef:
8543 case DW_TAG_class_type:
8544 case DW_TAG_interface_type:
8545 case DW_TAG_structure_type:
8546 case DW_TAG_union_type:
8547 case DW_TAG_enumeration_type:
8548 case DW_TAG_enumerator:
8549 case DW_TAG_subprogram:
8550 case DW_TAG_inlined_subroutine:
8551 case DW_TAG_member:
8552 case DW_TAG_imported_declaration:
8553 return 1;
8554
8555 case DW_TAG_variable:
8556 case DW_TAG_constant:
8557 /* We only need to prefix "globally" visible variables. These include
8558 any variable marked with DW_AT_external or any variable that
8559 lives in a namespace. [Variables in anonymous namespaces
8560 require prefixing, but they are not DW_AT_external.] */
8561
8562 if (dwarf2_attr (die, DW_AT_specification, cu))
8563 {
8564 struct dwarf2_cu *spec_cu = cu;
8565
8566 return die_needs_namespace (die_specification (die, &spec_cu),
8567 spec_cu);
8568 }
8569
8570 attr = dwarf2_attr (die, DW_AT_external, cu);
8571 if (attr == NULL && die->parent->tag != DW_TAG_namespace
8572 && die->parent->tag != DW_TAG_module)
8573 return 0;
8574 /* A variable in a lexical block of some kind does not need a
8575 namespace, even though in C++ such variables may be external
8576 and have a mangled name. */
8577 if (die->parent->tag == DW_TAG_lexical_block
8578 || die->parent->tag == DW_TAG_try_block
8579 || die->parent->tag == DW_TAG_catch_block
8580 || die->parent->tag == DW_TAG_subprogram)
8581 return 0;
8582 return 1;
8583
8584 default:
8585 return 0;
8586 }
8587 }
8588
8589 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
8590 compute the physname for the object, which include a method's:
8591 - formal parameters (C++),
8592 - receiver type (Go),
8593
8594 The term "physname" is a bit confusing.
8595 For C++, for example, it is the demangled name.
8596 For Go, for example, it's the mangled name.
8597
8598 For Ada, return the DIE's linkage name rather than the fully qualified
8599 name. PHYSNAME is ignored..
8600
8601 The result is allocated on the objfile_obstack and canonicalized. */
8602
8603 static const char *
8604 dwarf2_compute_name (const char *name,
8605 struct die_info *die, struct dwarf2_cu *cu,
8606 int physname)
8607 {
8608 struct objfile *objfile = cu->objfile;
8609
8610 if (name == NULL)
8611 name = dwarf2_name (die, cu);
8612
8613 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
8614 but otherwise compute it by typename_concat inside GDB.
8615 FIXME: Actually this is not really true, or at least not always true.
8616 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
8617 Fortran names because there is no mangling standard. So new_symbol_full
8618 will set the demangled name to the result of dwarf2_full_name, and it is
8619 the demangled name that GDB uses if it exists. */
8620 if (cu->language == language_ada
8621 || (cu->language == language_fortran && physname))
8622 {
8623 /* For Ada unit, we prefer the linkage name over the name, as
8624 the former contains the exported name, which the user expects
8625 to be able to reference. Ideally, we want the user to be able
8626 to reference this entity using either natural or linkage name,
8627 but we haven't started looking at this enhancement yet. */
8628 const char *linkage_name;
8629
8630 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8631 if (linkage_name == NULL)
8632 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8633 if (linkage_name != NULL)
8634 return linkage_name;
8635 }
8636
8637 /* These are the only languages we know how to qualify names in. */
8638 if (name != NULL
8639 && (cu->language == language_cplus
8640 || cu->language == language_fortran || cu->language == language_d
8641 || cu->language == language_rust))
8642 {
8643 if (die_needs_namespace (die, cu))
8644 {
8645 long length;
8646 const char *prefix;
8647 const char *canonical_name = NULL;
8648
8649 string_file buf;
8650
8651 prefix = determine_prefix (die, cu);
8652 if (*prefix != '\0')
8653 {
8654 char *prefixed_name = typename_concat (NULL, prefix, name,
8655 physname, cu);
8656
8657 buf.puts (prefixed_name);
8658 xfree (prefixed_name);
8659 }
8660 else
8661 buf.puts (name);
8662
8663 /* Template parameters may be specified in the DIE's DW_AT_name, or
8664 as children with DW_TAG_template_type_param or
8665 DW_TAG_value_type_param. If the latter, add them to the name
8666 here. If the name already has template parameters, then
8667 skip this step; some versions of GCC emit both, and
8668 it is more efficient to use the pre-computed name.
8669
8670 Something to keep in mind about this process: it is very
8671 unlikely, or in some cases downright impossible, to produce
8672 something that will match the mangled name of a function.
8673 If the definition of the function has the same debug info,
8674 we should be able to match up with it anyway. But fallbacks
8675 using the minimal symbol, for instance to find a method
8676 implemented in a stripped copy of libstdc++, will not work.
8677 If we do not have debug info for the definition, we will have to
8678 match them up some other way.
8679
8680 When we do name matching there is a related problem with function
8681 templates; two instantiated function templates are allowed to
8682 differ only by their return types, which we do not add here. */
8683
8684 if (cu->language == language_cplus && strchr (name, '<') == NULL)
8685 {
8686 struct attribute *attr;
8687 struct die_info *child;
8688 int first = 1;
8689
8690 die->building_fullname = 1;
8691
8692 for (child = die->child; child != NULL; child = child->sibling)
8693 {
8694 struct type *type;
8695 LONGEST value;
8696 const gdb_byte *bytes;
8697 struct dwarf2_locexpr_baton *baton;
8698 struct value *v;
8699
8700 if (child->tag != DW_TAG_template_type_param
8701 && child->tag != DW_TAG_template_value_param)
8702 continue;
8703
8704 if (first)
8705 {
8706 buf.puts ("<");
8707 first = 0;
8708 }
8709 else
8710 buf.puts (", ");
8711
8712 attr = dwarf2_attr (child, DW_AT_type, cu);
8713 if (attr == NULL)
8714 {
8715 complaint (&symfile_complaints,
8716 _("template parameter missing DW_AT_type"));
8717 buf.puts ("UNKNOWN_TYPE");
8718 continue;
8719 }
8720 type = die_type (child, cu);
8721
8722 if (child->tag == DW_TAG_template_type_param)
8723 {
8724 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
8725 continue;
8726 }
8727
8728 attr = dwarf2_attr (child, DW_AT_const_value, cu);
8729 if (attr == NULL)
8730 {
8731 complaint (&symfile_complaints,
8732 _("template parameter missing "
8733 "DW_AT_const_value"));
8734 buf.puts ("UNKNOWN_VALUE");
8735 continue;
8736 }
8737
8738 dwarf2_const_value_attr (attr, type, name,
8739 &cu->comp_unit_obstack, cu,
8740 &value, &bytes, &baton);
8741
8742 if (TYPE_NOSIGN (type))
8743 /* GDB prints characters as NUMBER 'CHAR'. If that's
8744 changed, this can use value_print instead. */
8745 c_printchar (value, type, &buf);
8746 else
8747 {
8748 struct value_print_options opts;
8749
8750 if (baton != NULL)
8751 v = dwarf2_evaluate_loc_desc (type, NULL,
8752 baton->data,
8753 baton->size,
8754 baton->per_cu);
8755 else if (bytes != NULL)
8756 {
8757 v = allocate_value (type);
8758 memcpy (value_contents_writeable (v), bytes,
8759 TYPE_LENGTH (type));
8760 }
8761 else
8762 v = value_from_longest (type, value);
8763
8764 /* Specify decimal so that we do not depend on
8765 the radix. */
8766 get_formatted_print_options (&opts, 'd');
8767 opts.raw = 1;
8768 value_print (v, &buf, &opts);
8769 release_value (v);
8770 value_free (v);
8771 }
8772 }
8773
8774 die->building_fullname = 0;
8775
8776 if (!first)
8777 {
8778 /* Close the argument list, with a space if necessary
8779 (nested templates). */
8780 if (!buf.empty () && buf.string ().back () == '>')
8781 buf.puts (" >");
8782 else
8783 buf.puts (">");
8784 }
8785 }
8786
8787 /* For C++ methods, append formal parameter type
8788 information, if PHYSNAME. */
8789
8790 if (physname && die->tag == DW_TAG_subprogram
8791 && cu->language == language_cplus)
8792 {
8793 struct type *type = read_type_die (die, cu);
8794
8795 c_type_print_args (type, &buf, 1, cu->language,
8796 &type_print_raw_options);
8797
8798 if (cu->language == language_cplus)
8799 {
8800 /* Assume that an artificial first parameter is
8801 "this", but do not crash if it is not. RealView
8802 marks unnamed (and thus unused) parameters as
8803 artificial; there is no way to differentiate
8804 the two cases. */
8805 if (TYPE_NFIELDS (type) > 0
8806 && TYPE_FIELD_ARTIFICIAL (type, 0)
8807 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
8808 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8809 0))))
8810 buf.puts (" const");
8811 }
8812 }
8813
8814 const std::string &intermediate_name = buf.string ();
8815
8816 if (cu->language == language_cplus)
8817 canonical_name
8818 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
8819 &objfile->per_bfd->storage_obstack);
8820
8821 /* If we only computed INTERMEDIATE_NAME, or if
8822 INTERMEDIATE_NAME is already canonical, then we need to
8823 copy it to the appropriate obstack. */
8824 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
8825 name = ((const char *)
8826 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8827 intermediate_name.c_str (),
8828 intermediate_name.length ()));
8829 else
8830 name = canonical_name;
8831 }
8832 }
8833
8834 return name;
8835 }
8836
8837 /* Return the fully qualified name of DIE, based on its DW_AT_name.
8838 If scope qualifiers are appropriate they will be added. The result
8839 will be allocated on the storage_obstack, or NULL if the DIE does
8840 not have a name. NAME may either be from a previous call to
8841 dwarf2_name or NULL.
8842
8843 The output string will be canonicalized (if C++). */
8844
8845 static const char *
8846 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8847 {
8848 return dwarf2_compute_name (name, die, cu, 0);
8849 }
8850
8851 /* Construct a physname for the given DIE in CU. NAME may either be
8852 from a previous call to dwarf2_name or NULL. The result will be
8853 allocated on the objfile_objstack or NULL if the DIE does not have a
8854 name.
8855
8856 The output string will be canonicalized (if C++). */
8857
8858 static const char *
8859 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8860 {
8861 struct objfile *objfile = cu->objfile;
8862 const char *retval, *mangled = NULL, *canon = NULL;
8863 struct cleanup *back_to;
8864 int need_copy = 1;
8865
8866 /* In this case dwarf2_compute_name is just a shortcut not building anything
8867 on its own. */
8868 if (!die_needs_namespace (die, cu))
8869 return dwarf2_compute_name (name, die, cu, 1);
8870
8871 back_to = make_cleanup (null_cleanup, NULL);
8872
8873 mangled = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
8874 if (mangled == NULL)
8875 mangled = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
8876
8877 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
8878 See https://github.com/rust-lang/rust/issues/32925. */
8879 if (cu->language == language_rust && mangled != NULL
8880 && strchr (mangled, '{') != NULL)
8881 mangled = NULL;
8882
8883 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8884 has computed. */
8885 if (mangled != NULL)
8886 {
8887 char *demangled;
8888
8889 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8890 type. It is easier for GDB users to search for such functions as
8891 `name(params)' than `long name(params)'. In such case the minimal
8892 symbol names do not match the full symbol names but for template
8893 functions there is never a need to look up their definition from their
8894 declaration so the only disadvantage remains the minimal symbol
8895 variant `long name(params)' does not have the proper inferior type.
8896 */
8897
8898 if (cu->language == language_go)
8899 {
8900 /* This is a lie, but we already lie to the caller new_symbol_full.
8901 new_symbol_full assumes we return the mangled name.
8902 This just undoes that lie until things are cleaned up. */
8903 demangled = NULL;
8904 }
8905 else
8906 {
8907 demangled = gdb_demangle (mangled,
8908 (DMGL_PARAMS | DMGL_ANSI | DMGL_RET_DROP));
8909 }
8910 if (demangled)
8911 {
8912 make_cleanup (xfree, demangled);
8913 canon = demangled;
8914 }
8915 else
8916 {
8917 canon = mangled;
8918 need_copy = 0;
8919 }
8920 }
8921
8922 if (canon == NULL || check_physname)
8923 {
8924 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8925
8926 if (canon != NULL && strcmp (physname, canon) != 0)
8927 {
8928 /* It may not mean a bug in GDB. The compiler could also
8929 compute DW_AT_linkage_name incorrectly. But in such case
8930 GDB would need to be bug-to-bug compatible. */
8931
8932 complaint (&symfile_complaints,
8933 _("Computed physname <%s> does not match demangled <%s> "
8934 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
8935 physname, canon, mangled, die->offset.sect_off,
8936 objfile_name (objfile));
8937
8938 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8939 is available here - over computed PHYSNAME. It is safer
8940 against both buggy GDB and buggy compilers. */
8941
8942 retval = canon;
8943 }
8944 else
8945 {
8946 retval = physname;
8947 need_copy = 0;
8948 }
8949 }
8950 else
8951 retval = canon;
8952
8953 if (need_copy)
8954 retval = ((const char *)
8955 obstack_copy0 (&objfile->per_bfd->storage_obstack,
8956 retval, strlen (retval)));
8957
8958 do_cleanups (back_to);
8959 return retval;
8960 }
8961
8962 /* Inspect DIE in CU for a namespace alias. If one exists, record
8963 a new symbol for it.
8964
8965 Returns 1 if a namespace alias was recorded, 0 otherwise. */
8966
8967 static int
8968 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
8969 {
8970 struct attribute *attr;
8971
8972 /* If the die does not have a name, this is not a namespace
8973 alias. */
8974 attr = dwarf2_attr (die, DW_AT_name, cu);
8975 if (attr != NULL)
8976 {
8977 int num;
8978 struct die_info *d = die;
8979 struct dwarf2_cu *imported_cu = cu;
8980
8981 /* If the compiler has nested DW_AT_imported_declaration DIEs,
8982 keep inspecting DIEs until we hit the underlying import. */
8983 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
8984 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
8985 {
8986 attr = dwarf2_attr (d, DW_AT_import, cu);
8987 if (attr == NULL)
8988 break;
8989
8990 d = follow_die_ref (d, attr, &imported_cu);
8991 if (d->tag != DW_TAG_imported_declaration)
8992 break;
8993 }
8994
8995 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
8996 {
8997 complaint (&symfile_complaints,
8998 _("DIE at 0x%x has too many recursively imported "
8999 "declarations"), d->offset.sect_off);
9000 return 0;
9001 }
9002
9003 if (attr != NULL)
9004 {
9005 struct type *type;
9006 sect_offset offset = dwarf2_get_ref_die_offset (attr);
9007
9008 type = get_die_type_at_offset (offset, cu->per_cu);
9009 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
9010 {
9011 /* This declaration is a global namespace alias. Add
9012 a symbol for it whose type is the aliased namespace. */
9013 new_symbol (die, type, cu);
9014 return 1;
9015 }
9016 }
9017 }
9018
9019 return 0;
9020 }
9021
9022 /* Return the using directives repository (global or local?) to use in the
9023 current context for LANGUAGE.
9024
9025 For Ada, imported declarations can materialize renamings, which *may* be
9026 global. However it is impossible (for now?) in DWARF to distinguish
9027 "external" imported declarations and "static" ones. As all imported
9028 declarations seem to be static in all other languages, make them all CU-wide
9029 global only in Ada. */
9030
9031 static struct using_direct **
9032 using_directives (enum language language)
9033 {
9034 if (language == language_ada && context_stack_depth == 0)
9035 return &global_using_directives;
9036 else
9037 return &local_using_directives;
9038 }
9039
9040 /* Read the import statement specified by the given die and record it. */
9041
9042 static void
9043 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
9044 {
9045 struct objfile *objfile = cu->objfile;
9046 struct attribute *import_attr;
9047 struct die_info *imported_die, *child_die;
9048 struct dwarf2_cu *imported_cu;
9049 const char *imported_name;
9050 const char *imported_name_prefix;
9051 const char *canonical_name;
9052 const char *import_alias;
9053 const char *imported_declaration = NULL;
9054 const char *import_prefix;
9055 VEC (const_char_ptr) *excludes = NULL;
9056 struct cleanup *cleanups;
9057
9058 import_attr = dwarf2_attr (die, DW_AT_import, cu);
9059 if (import_attr == NULL)
9060 {
9061 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9062 dwarf_tag_name (die->tag));
9063 return;
9064 }
9065
9066 imported_cu = cu;
9067 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
9068 imported_name = dwarf2_name (imported_die, imported_cu);
9069 if (imported_name == NULL)
9070 {
9071 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
9072
9073 The import in the following code:
9074 namespace A
9075 {
9076 typedef int B;
9077 }
9078
9079 int main ()
9080 {
9081 using A::B;
9082 B b;
9083 return b;
9084 }
9085
9086 ...
9087 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
9088 <52> DW_AT_decl_file : 1
9089 <53> DW_AT_decl_line : 6
9090 <54> DW_AT_import : <0x75>
9091 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
9092 <59> DW_AT_name : B
9093 <5b> DW_AT_decl_file : 1
9094 <5c> DW_AT_decl_line : 2
9095 <5d> DW_AT_type : <0x6e>
9096 ...
9097 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
9098 <76> DW_AT_byte_size : 4
9099 <77> DW_AT_encoding : 5 (signed)
9100
9101 imports the wrong die ( 0x75 instead of 0x58 ).
9102 This case will be ignored until the gcc bug is fixed. */
9103 return;
9104 }
9105
9106 /* Figure out the local name after import. */
9107 import_alias = dwarf2_name (die, cu);
9108
9109 /* Figure out where the statement is being imported to. */
9110 import_prefix = determine_prefix (die, cu);
9111
9112 /* Figure out what the scope of the imported die is and prepend it
9113 to the name of the imported die. */
9114 imported_name_prefix = determine_prefix (imported_die, imported_cu);
9115
9116 if (imported_die->tag != DW_TAG_namespace
9117 && imported_die->tag != DW_TAG_module)
9118 {
9119 imported_declaration = imported_name;
9120 canonical_name = imported_name_prefix;
9121 }
9122 else if (strlen (imported_name_prefix) > 0)
9123 canonical_name = obconcat (&objfile->objfile_obstack,
9124 imported_name_prefix,
9125 (cu->language == language_d ? "." : "::"),
9126 imported_name, (char *) NULL);
9127 else
9128 canonical_name = imported_name;
9129
9130 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
9131
9132 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
9133 for (child_die = die->child; child_die && child_die->tag;
9134 child_die = sibling_die (child_die))
9135 {
9136 /* DWARF-4: A Fortran use statement with a “rename list” may be
9137 represented by an imported module entry with an import attribute
9138 referring to the module and owned entries corresponding to those
9139 entities that are renamed as part of being imported. */
9140
9141 if (child_die->tag != DW_TAG_imported_declaration)
9142 {
9143 complaint (&symfile_complaints,
9144 _("child DW_TAG_imported_declaration expected "
9145 "- DIE at 0x%x [in module %s]"),
9146 child_die->offset.sect_off, objfile_name (objfile));
9147 continue;
9148 }
9149
9150 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
9151 if (import_attr == NULL)
9152 {
9153 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
9154 dwarf_tag_name (child_die->tag));
9155 continue;
9156 }
9157
9158 imported_cu = cu;
9159 imported_die = follow_die_ref_or_sig (child_die, import_attr,
9160 &imported_cu);
9161 imported_name = dwarf2_name (imported_die, imported_cu);
9162 if (imported_name == NULL)
9163 {
9164 complaint (&symfile_complaints,
9165 _("child DW_TAG_imported_declaration has unknown "
9166 "imported name - DIE at 0x%x [in module %s]"),
9167 child_die->offset.sect_off, objfile_name (objfile));
9168 continue;
9169 }
9170
9171 VEC_safe_push (const_char_ptr, excludes, imported_name);
9172
9173 process_die (child_die, cu);
9174 }
9175
9176 add_using_directive (using_directives (cu->language),
9177 import_prefix,
9178 canonical_name,
9179 import_alias,
9180 imported_declaration,
9181 excludes,
9182 0,
9183 &objfile->objfile_obstack);
9184
9185 do_cleanups (cleanups);
9186 }
9187
9188 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
9189 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
9190 this, it was first present in GCC release 4.3.0. */
9191
9192 static int
9193 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
9194 {
9195 if (!cu->checked_producer)
9196 check_producer (cu);
9197
9198 return cu->producer_is_gcc_lt_4_3;
9199 }
9200
9201 static file_and_directory
9202 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9203 {
9204 file_and_directory res;
9205
9206 /* Find the filename. Do not use dwarf2_name here, since the filename
9207 is not a source language identifier. */
9208 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
9209 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9210
9211 if (res.comp_dir == NULL
9212 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
9213 && IS_ABSOLUTE_PATH (res.name))
9214 {
9215 res.comp_dir_storage = ldirname (res.name);
9216 if (!res.comp_dir_storage.empty ())
9217 res.comp_dir = res.comp_dir_storage.c_str ();
9218 }
9219 if (res.comp_dir != NULL)
9220 {
9221 /* Irix 6.2 native cc prepends <machine>.: to the compilation
9222 directory, get rid of it. */
9223 const char *cp = strchr (res.comp_dir, ':');
9224
9225 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
9226 res.comp_dir = cp + 1;
9227 }
9228
9229 if (res.name == NULL)
9230 res.name = "<unknown>";
9231
9232 return res;
9233 }
9234
9235 /* Handle DW_AT_stmt_list for a compilation unit.
9236 DIE is the DW_TAG_compile_unit die for CU.
9237 COMP_DIR is the compilation directory. LOWPC is passed to
9238 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
9239
9240 static void
9241 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
9242 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
9243 {
9244 struct objfile *objfile = dwarf2_per_objfile->objfile;
9245 struct attribute *attr;
9246 unsigned int line_offset;
9247 struct line_header line_header_local;
9248 hashval_t line_header_local_hash;
9249 unsigned u;
9250 void **slot;
9251 int decode_mapping;
9252
9253 gdb_assert (! cu->per_cu->is_debug_types);
9254
9255 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9256 if (attr == NULL)
9257 return;
9258
9259 line_offset = DW_UNSND (attr);
9260
9261 /* The line header hash table is only created if needed (it exists to
9262 prevent redundant reading of the line table for partial_units).
9263 If we're given a partial_unit, we'll need it. If we're given a
9264 compile_unit, then use the line header hash table if it's already
9265 created, but don't create one just yet. */
9266
9267 if (dwarf2_per_objfile->line_header_hash == NULL
9268 && die->tag == DW_TAG_partial_unit)
9269 {
9270 dwarf2_per_objfile->line_header_hash
9271 = htab_create_alloc_ex (127, line_header_hash_voidp,
9272 line_header_eq_voidp,
9273 free_line_header_voidp,
9274 &objfile->objfile_obstack,
9275 hashtab_obstack_allocate,
9276 dummy_obstack_deallocate);
9277 }
9278
9279 line_header_local.offset.sect_off = line_offset;
9280 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
9281 line_header_local_hash = line_header_hash (&line_header_local);
9282 if (dwarf2_per_objfile->line_header_hash != NULL)
9283 {
9284 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9285 &line_header_local,
9286 line_header_local_hash, NO_INSERT);
9287
9288 /* For DW_TAG_compile_unit we need info like symtab::linetable which
9289 is not present in *SLOT (since if there is something in *SLOT then
9290 it will be for a partial_unit). */
9291 if (die->tag == DW_TAG_partial_unit && slot != NULL)
9292 {
9293 gdb_assert (*slot != NULL);
9294 cu->line_header = (struct line_header *) *slot;
9295 return;
9296 }
9297 }
9298
9299 /* dwarf_decode_line_header does not yet provide sufficient information.
9300 We always have to call also dwarf_decode_lines for it. */
9301 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
9302 if (lh == NULL)
9303 return;
9304 cu->line_header = lh.get ();
9305
9306 if (dwarf2_per_objfile->line_header_hash == NULL)
9307 slot = NULL;
9308 else
9309 {
9310 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
9311 &line_header_local,
9312 line_header_local_hash, INSERT);
9313 gdb_assert (slot != NULL);
9314 }
9315 if (slot != NULL && *slot == NULL)
9316 {
9317 /* This newly decoded line number information unit will be owned
9318 by line_header_hash hash table. */
9319 *slot = cu->line_header;
9320 }
9321 else
9322 {
9323 /* We cannot free any current entry in (*slot) as that struct line_header
9324 may be already used by multiple CUs. Create only temporary decoded
9325 line_header for this CU - it may happen at most once for each line
9326 number information unit. And if we're not using line_header_hash
9327 then this is what we want as well. */
9328 gdb_assert (die->tag != DW_TAG_partial_unit);
9329 }
9330 decode_mapping = (die->tag != DW_TAG_partial_unit);
9331 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
9332 decode_mapping);
9333
9334 lh.release ();
9335 }
9336
9337 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
9338
9339 static void
9340 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
9341 {
9342 struct objfile *objfile = dwarf2_per_objfile->objfile;
9343 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9344 CORE_ADDR lowpc = ((CORE_ADDR) -1);
9345 CORE_ADDR highpc = ((CORE_ADDR) 0);
9346 struct attribute *attr;
9347 struct die_info *child_die;
9348 CORE_ADDR baseaddr;
9349
9350 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9351
9352 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
9353
9354 /* If we didn't find a lowpc, set it to highpc to avoid complaints
9355 from finish_block. */
9356 if (lowpc == ((CORE_ADDR) -1))
9357 lowpc = highpc;
9358 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
9359
9360 file_and_directory fnd = find_file_and_directory (die, cu);
9361
9362 prepare_one_comp_unit (cu, die, cu->language);
9363
9364 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
9365 standardised yet. As a workaround for the language detection we fall
9366 back to the DW_AT_producer string. */
9367 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
9368 cu->language = language_opencl;
9369
9370 /* Similar hack for Go. */
9371 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
9372 set_cu_language (DW_LANG_Go, cu);
9373
9374 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
9375
9376 /* Decode line number information if present. We do this before
9377 processing child DIEs, so that the line header table is available
9378 for DW_AT_decl_file. */
9379 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
9380
9381 /* Process all dies in compilation unit. */
9382 if (die->child != NULL)
9383 {
9384 child_die = die->child;
9385 while (child_die && child_die->tag)
9386 {
9387 process_die (child_die, cu);
9388 child_die = sibling_die (child_die);
9389 }
9390 }
9391
9392 /* Decode macro information, if present. Dwarf 2 macro information
9393 refers to information in the line number info statement program
9394 header, so we can only read it if we've read the header
9395 successfully. */
9396 attr = dwarf2_attr (die, DW_AT_macros, cu);
9397 if (attr == NULL)
9398 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
9399 if (attr && cu->line_header)
9400 {
9401 if (dwarf2_attr (die, DW_AT_macro_info, cu))
9402 complaint (&symfile_complaints,
9403 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
9404
9405 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
9406 }
9407 else
9408 {
9409 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
9410 if (attr && cu->line_header)
9411 {
9412 unsigned int macro_offset = DW_UNSND (attr);
9413
9414 dwarf_decode_macros (cu, macro_offset, 0);
9415 }
9416 }
9417 }
9418
9419 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
9420 Create the set of symtabs used by this TU, or if this TU is sharing
9421 symtabs with another TU and the symtabs have already been created
9422 then restore those symtabs in the line header.
9423 We don't need the pc/line-number mapping for type units. */
9424
9425 static void
9426 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
9427 {
9428 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
9429 struct type_unit_group *tu_group;
9430 int first_time;
9431 struct attribute *attr;
9432 unsigned int i, line_offset;
9433 struct signatured_type *sig_type;
9434
9435 gdb_assert (per_cu->is_debug_types);
9436 sig_type = (struct signatured_type *) per_cu;
9437
9438 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
9439
9440 /* If we're using .gdb_index (includes -readnow) then
9441 per_cu->type_unit_group may not have been set up yet. */
9442 if (sig_type->type_unit_group == NULL)
9443 sig_type->type_unit_group = get_type_unit_group (cu, attr);
9444 tu_group = sig_type->type_unit_group;
9445
9446 /* If we've already processed this stmt_list there's no real need to
9447 do it again, we could fake it and just recreate the part we need
9448 (file name,index -> symtab mapping). If data shows this optimization
9449 is useful we can do it then. */
9450 first_time = tu_group->compunit_symtab == NULL;
9451
9452 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
9453 debug info. */
9454 line_header_up lh;
9455 if (attr != NULL)
9456 {
9457 line_offset = DW_UNSND (attr);
9458 lh = dwarf_decode_line_header (line_offset, cu);
9459 }
9460 if (lh == NULL)
9461 {
9462 if (first_time)
9463 dwarf2_start_symtab (cu, "", NULL, 0);
9464 else
9465 {
9466 gdb_assert (tu_group->symtabs == NULL);
9467 restart_symtab (tu_group->compunit_symtab, "", 0);
9468 }
9469 return;
9470 }
9471
9472 cu->line_header = lh.get ();
9473
9474 if (first_time)
9475 {
9476 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
9477
9478 /* Note: We don't assign tu_group->compunit_symtab yet because we're
9479 still initializing it, and our caller (a few levels up)
9480 process_full_type_unit still needs to know if this is the first
9481 time. */
9482
9483 tu_group->num_symtabs = lh->file_names.size ();
9484 tu_group->symtabs = XNEWVEC (struct symtab *, lh->file_names.size ());
9485
9486 for (i = 0; i < lh->file_names.size (); ++i)
9487 {
9488 file_entry &fe = lh->file_names[i];
9489
9490 dwarf2_start_subfile (fe.name, fe.include_dir (lh.get ()));
9491
9492 if (current_subfile->symtab == NULL)
9493 {
9494 /* NOTE: start_subfile will recognize when it's been passed
9495 a file it has already seen. So we can't assume there's a
9496 simple mapping from lh->file_names to subfiles, plus
9497 lh->file_names may contain dups. */
9498 current_subfile->symtab
9499 = allocate_symtab (cust, current_subfile->name);
9500 }
9501
9502 fe.symtab = current_subfile->symtab;
9503 tu_group->symtabs[i] = fe.symtab;
9504 }
9505 }
9506 else
9507 {
9508 restart_symtab (tu_group->compunit_symtab, "", 0);
9509
9510 for (i = 0; i < lh->file_names.size (); ++i)
9511 {
9512 struct file_entry *fe = &lh->file_names[i];
9513
9514 fe->symtab = tu_group->symtabs[i];
9515 }
9516 }
9517
9518 lh.release ();
9519
9520 /* The main symtab is allocated last. Type units don't have DW_AT_name
9521 so they don't have a "real" (so to speak) symtab anyway.
9522 There is later code that will assign the main symtab to all symbols
9523 that don't have one. We need to handle the case of a symbol with a
9524 missing symtab (DW_AT_decl_file) anyway. */
9525 }
9526
9527 /* Process DW_TAG_type_unit.
9528 For TUs we want to skip the first top level sibling if it's not the
9529 actual type being defined by this TU. In this case the first top
9530 level sibling is there to provide context only. */
9531
9532 static void
9533 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
9534 {
9535 struct die_info *child_die;
9536
9537 prepare_one_comp_unit (cu, die, language_minimal);
9538
9539 /* Initialize (or reinitialize) the machinery for building symtabs.
9540 We do this before processing child DIEs, so that the line header table
9541 is available for DW_AT_decl_file. */
9542 setup_type_unit_groups (die, cu);
9543
9544 if (die->child != NULL)
9545 {
9546 child_die = die->child;
9547 while (child_die && child_die->tag)
9548 {
9549 process_die (child_die, cu);
9550 child_die = sibling_die (child_die);
9551 }
9552 }
9553 }
9554 \f
9555 /* DWO/DWP files.
9556
9557 http://gcc.gnu.org/wiki/DebugFission
9558 http://gcc.gnu.org/wiki/DebugFissionDWP
9559
9560 To simplify handling of both DWO files ("object" files with the DWARF info)
9561 and DWP files (a file with the DWOs packaged up into one file), we treat
9562 DWP files as having a collection of virtual DWO files. */
9563
9564 static hashval_t
9565 hash_dwo_file (const void *item)
9566 {
9567 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
9568 hashval_t hash;
9569
9570 hash = htab_hash_string (dwo_file->dwo_name);
9571 if (dwo_file->comp_dir != NULL)
9572 hash += htab_hash_string (dwo_file->comp_dir);
9573 return hash;
9574 }
9575
9576 static int
9577 eq_dwo_file (const void *item_lhs, const void *item_rhs)
9578 {
9579 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
9580 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
9581
9582 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
9583 return 0;
9584 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
9585 return lhs->comp_dir == rhs->comp_dir;
9586 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
9587 }
9588
9589 /* Allocate a hash table for DWO files. */
9590
9591 static htab_t
9592 allocate_dwo_file_hash_table (void)
9593 {
9594 struct objfile *objfile = dwarf2_per_objfile->objfile;
9595
9596 return htab_create_alloc_ex (41,
9597 hash_dwo_file,
9598 eq_dwo_file,
9599 NULL,
9600 &objfile->objfile_obstack,
9601 hashtab_obstack_allocate,
9602 dummy_obstack_deallocate);
9603 }
9604
9605 /* Lookup DWO file DWO_NAME. */
9606
9607 static void **
9608 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
9609 {
9610 struct dwo_file find_entry;
9611 void **slot;
9612
9613 if (dwarf2_per_objfile->dwo_files == NULL)
9614 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
9615
9616 memset (&find_entry, 0, sizeof (find_entry));
9617 find_entry.dwo_name = dwo_name;
9618 find_entry.comp_dir = comp_dir;
9619 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
9620
9621 return slot;
9622 }
9623
9624 static hashval_t
9625 hash_dwo_unit (const void *item)
9626 {
9627 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
9628
9629 /* This drops the top 32 bits of the id, but is ok for a hash. */
9630 return dwo_unit->signature;
9631 }
9632
9633 static int
9634 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
9635 {
9636 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
9637 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
9638
9639 /* The signature is assumed to be unique within the DWO file.
9640 So while object file CU dwo_id's always have the value zero,
9641 that's OK, assuming each object file DWO file has only one CU,
9642 and that's the rule for now. */
9643 return lhs->signature == rhs->signature;
9644 }
9645
9646 /* Allocate a hash table for DWO CUs,TUs.
9647 There is one of these tables for each of CUs,TUs for each DWO file. */
9648
9649 static htab_t
9650 allocate_dwo_unit_table (struct objfile *objfile)
9651 {
9652 /* Start out with a pretty small number.
9653 Generally DWO files contain only one CU and maybe some TUs. */
9654 return htab_create_alloc_ex (3,
9655 hash_dwo_unit,
9656 eq_dwo_unit,
9657 NULL,
9658 &objfile->objfile_obstack,
9659 hashtab_obstack_allocate,
9660 dummy_obstack_deallocate);
9661 }
9662
9663 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
9664
9665 struct create_dwo_cu_data
9666 {
9667 struct dwo_file *dwo_file;
9668 struct dwo_unit dwo_unit;
9669 };
9670
9671 /* die_reader_func for create_dwo_cu. */
9672
9673 static void
9674 create_dwo_cu_reader (const struct die_reader_specs *reader,
9675 const gdb_byte *info_ptr,
9676 struct die_info *comp_unit_die,
9677 int has_children,
9678 void *datap)
9679 {
9680 struct dwarf2_cu *cu = reader->cu;
9681 sect_offset offset = cu->per_cu->offset;
9682 struct dwarf2_section_info *section = cu->per_cu->section;
9683 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
9684 struct dwo_file *dwo_file = data->dwo_file;
9685 struct dwo_unit *dwo_unit = &data->dwo_unit;
9686 struct attribute *attr;
9687
9688 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
9689 if (attr == NULL)
9690 {
9691 complaint (&symfile_complaints,
9692 _("Dwarf Error: debug entry at offset 0x%x is missing"
9693 " its dwo_id [in module %s]"),
9694 offset.sect_off, dwo_file->dwo_name);
9695 return;
9696 }
9697
9698 dwo_unit->dwo_file = dwo_file;
9699 dwo_unit->signature = DW_UNSND (attr);
9700 dwo_unit->section = section;
9701 dwo_unit->offset = offset;
9702 dwo_unit->length = cu->per_cu->length;
9703
9704 if (dwarf_read_debug)
9705 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
9706 offset.sect_off, hex_string (dwo_unit->signature));
9707 }
9708
9709 /* Create the dwo_unit for the lone CU in DWO_FILE.
9710 Note: This function processes DWO files only, not DWP files. */
9711
9712 static struct dwo_unit *
9713 create_dwo_cu (struct dwo_file *dwo_file)
9714 {
9715 struct objfile *objfile = dwarf2_per_objfile->objfile;
9716 struct dwarf2_section_info *section = &dwo_file->sections.info;
9717 const gdb_byte *info_ptr, *end_ptr;
9718 struct create_dwo_cu_data create_dwo_cu_data;
9719 struct dwo_unit *dwo_unit;
9720
9721 dwarf2_read_section (objfile, section);
9722 info_ptr = section->buffer;
9723
9724 if (info_ptr == NULL)
9725 return NULL;
9726
9727 if (dwarf_read_debug)
9728 {
9729 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
9730 get_section_name (section),
9731 get_section_file_name (section));
9732 }
9733
9734 create_dwo_cu_data.dwo_file = dwo_file;
9735 dwo_unit = NULL;
9736
9737 end_ptr = info_ptr + section->size;
9738 while (info_ptr < end_ptr)
9739 {
9740 struct dwarf2_per_cu_data per_cu;
9741
9742 memset (&create_dwo_cu_data.dwo_unit, 0,
9743 sizeof (create_dwo_cu_data.dwo_unit));
9744 memset (&per_cu, 0, sizeof (per_cu));
9745 per_cu.objfile = objfile;
9746 per_cu.is_debug_types = 0;
9747 per_cu.offset.sect_off = info_ptr - section->buffer;
9748 per_cu.section = section;
9749
9750 init_cutu_and_read_dies_no_follow (&per_cu, dwo_file,
9751 create_dwo_cu_reader,
9752 &create_dwo_cu_data);
9753
9754 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
9755 {
9756 /* If we've already found one, complain. We only support one
9757 because having more than one requires hacking the dwo_name of
9758 each to match, which is highly unlikely to happen. */
9759 if (dwo_unit != NULL)
9760 {
9761 complaint (&symfile_complaints,
9762 _("Multiple CUs in DWO file %s [in module %s]"),
9763 dwo_file->dwo_name, objfile_name (objfile));
9764 break;
9765 }
9766
9767 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9768 *dwo_unit = create_dwo_cu_data.dwo_unit;
9769 }
9770
9771 info_ptr += per_cu.length;
9772 }
9773
9774 return dwo_unit;
9775 }
9776
9777 /* DWP file .debug_{cu,tu}_index section format:
9778 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
9779
9780 DWP Version 1:
9781
9782 Both index sections have the same format, and serve to map a 64-bit
9783 signature to a set of section numbers. Each section begins with a header,
9784 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
9785 indexes, and a pool of 32-bit section numbers. The index sections will be
9786 aligned at 8-byte boundaries in the file.
9787
9788 The index section header consists of:
9789
9790 V, 32 bit version number
9791 -, 32 bits unused
9792 N, 32 bit number of compilation units or type units in the index
9793 M, 32 bit number of slots in the hash table
9794
9795 Numbers are recorded using the byte order of the application binary.
9796
9797 The hash table begins at offset 16 in the section, and consists of an array
9798 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
9799 order of the application binary). Unused slots in the hash table are 0.
9800 (We rely on the extreme unlikeliness of a signature being exactly 0.)
9801
9802 The parallel table begins immediately after the hash table
9803 (at offset 16 + 8 * M from the beginning of the section), and consists of an
9804 array of 32-bit indexes (using the byte order of the application binary),
9805 corresponding 1-1 with slots in the hash table. Each entry in the parallel
9806 table contains a 32-bit index into the pool of section numbers. For unused
9807 hash table slots, the corresponding entry in the parallel table will be 0.
9808
9809 The pool of section numbers begins immediately following the hash table
9810 (at offset 16 + 12 * M from the beginning of the section). The pool of
9811 section numbers consists of an array of 32-bit words (using the byte order
9812 of the application binary). Each item in the array is indexed starting
9813 from 0. The hash table entry provides the index of the first section
9814 number in the set. Additional section numbers in the set follow, and the
9815 set is terminated by a 0 entry (section number 0 is not used in ELF).
9816
9817 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
9818 section must be the first entry in the set, and the .debug_abbrev.dwo must
9819 be the second entry. Other members of the set may follow in any order.
9820
9821 ---
9822
9823 DWP Version 2:
9824
9825 DWP Version 2 combines all the .debug_info, etc. sections into one,
9826 and the entries in the index tables are now offsets into these sections.
9827 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
9828 section.
9829
9830 Index Section Contents:
9831 Header
9832 Hash Table of Signatures dwp_hash_table.hash_table
9833 Parallel Table of Indices dwp_hash_table.unit_table
9834 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
9835 Table of Section Sizes dwp_hash_table.v2.sizes
9836
9837 The index section header consists of:
9838
9839 V, 32 bit version number
9840 L, 32 bit number of columns in the table of section offsets
9841 N, 32 bit number of compilation units or type units in the index
9842 M, 32 bit number of slots in the hash table
9843
9844 Numbers are recorded using the byte order of the application binary.
9845
9846 The hash table has the same format as version 1.
9847 The parallel table of indices has the same format as version 1,
9848 except that the entries are origin-1 indices into the table of sections
9849 offsets and the table of section sizes.
9850
9851 The table of offsets begins immediately following the parallel table
9852 (at offset 16 + 12 * M from the beginning of the section). The table is
9853 a two-dimensional array of 32-bit words (using the byte order of the
9854 application binary), with L columns and N+1 rows, in row-major order.
9855 Each row in the array is indexed starting from 0. The first row provides
9856 a key to the remaining rows: each column in this row provides an identifier
9857 for a debug section, and the offsets in the same column of subsequent rows
9858 refer to that section. The section identifiers are:
9859
9860 DW_SECT_INFO 1 .debug_info.dwo
9861 DW_SECT_TYPES 2 .debug_types.dwo
9862 DW_SECT_ABBREV 3 .debug_abbrev.dwo
9863 DW_SECT_LINE 4 .debug_line.dwo
9864 DW_SECT_LOC 5 .debug_loc.dwo
9865 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
9866 DW_SECT_MACINFO 7 .debug_macinfo.dwo
9867 DW_SECT_MACRO 8 .debug_macro.dwo
9868
9869 The offsets provided by the CU and TU index sections are the base offsets
9870 for the contributions made by each CU or TU to the corresponding section
9871 in the package file. Each CU and TU header contains an abbrev_offset
9872 field, used to find the abbreviations table for that CU or TU within the
9873 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
9874 be interpreted as relative to the base offset given in the index section.
9875 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
9876 should be interpreted as relative to the base offset for .debug_line.dwo,
9877 and offsets into other debug sections obtained from DWARF attributes should
9878 also be interpreted as relative to the corresponding base offset.
9879
9880 The table of sizes begins immediately following the table of offsets.
9881 Like the table of offsets, it is a two-dimensional array of 32-bit words,
9882 with L columns and N rows, in row-major order. Each row in the array is
9883 indexed starting from 1 (row 0 is shared by the two tables).
9884
9885 ---
9886
9887 Hash table lookup is handled the same in version 1 and 2:
9888
9889 We assume that N and M will not exceed 2^32 - 1.
9890 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
9891
9892 Given a 64-bit compilation unit signature or a type signature S, an entry
9893 in the hash table is located as follows:
9894
9895 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
9896 the low-order k bits all set to 1.
9897
9898 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
9899
9900 3) If the hash table entry at index H matches the signature, use that
9901 entry. If the hash table entry at index H is unused (all zeroes),
9902 terminate the search: the signature is not present in the table.
9903
9904 4) Let H = (H + H') modulo M. Repeat at Step 3.
9905
9906 Because M > N and H' and M are relatively prime, the search is guaranteed
9907 to stop at an unused slot or find the match. */
9908
9909 /* Create a hash table to map DWO IDs to their CU/TU entry in
9910 .debug_{info,types}.dwo in DWP_FILE.
9911 Returns NULL if there isn't one.
9912 Note: This function processes DWP files only, not DWO files. */
9913
9914 static struct dwp_hash_table *
9915 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
9916 {
9917 struct objfile *objfile = dwarf2_per_objfile->objfile;
9918 bfd *dbfd = dwp_file->dbfd;
9919 const gdb_byte *index_ptr, *index_end;
9920 struct dwarf2_section_info *index;
9921 uint32_t version, nr_columns, nr_units, nr_slots;
9922 struct dwp_hash_table *htab;
9923
9924 if (is_debug_types)
9925 index = &dwp_file->sections.tu_index;
9926 else
9927 index = &dwp_file->sections.cu_index;
9928
9929 if (dwarf2_section_empty_p (index))
9930 return NULL;
9931 dwarf2_read_section (objfile, index);
9932
9933 index_ptr = index->buffer;
9934 index_end = index_ptr + index->size;
9935
9936 version = read_4_bytes (dbfd, index_ptr);
9937 index_ptr += 4;
9938 if (version == 2)
9939 nr_columns = read_4_bytes (dbfd, index_ptr);
9940 else
9941 nr_columns = 0;
9942 index_ptr += 4;
9943 nr_units = read_4_bytes (dbfd, index_ptr);
9944 index_ptr += 4;
9945 nr_slots = read_4_bytes (dbfd, index_ptr);
9946 index_ptr += 4;
9947
9948 if (version != 1 && version != 2)
9949 {
9950 error (_("Dwarf Error: unsupported DWP file version (%s)"
9951 " [in module %s]"),
9952 pulongest (version), dwp_file->name);
9953 }
9954 if (nr_slots != (nr_slots & -nr_slots))
9955 {
9956 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
9957 " is not power of 2 [in module %s]"),
9958 pulongest (nr_slots), dwp_file->name);
9959 }
9960
9961 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
9962 htab->version = version;
9963 htab->nr_columns = nr_columns;
9964 htab->nr_units = nr_units;
9965 htab->nr_slots = nr_slots;
9966 htab->hash_table = index_ptr;
9967 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
9968
9969 /* Exit early if the table is empty. */
9970 if (nr_slots == 0 || nr_units == 0
9971 || (version == 2 && nr_columns == 0))
9972 {
9973 /* All must be zero. */
9974 if (nr_slots != 0 || nr_units != 0
9975 || (version == 2 && nr_columns != 0))
9976 {
9977 complaint (&symfile_complaints,
9978 _("Empty DWP but nr_slots,nr_units,nr_columns not"
9979 " all zero [in modules %s]"),
9980 dwp_file->name);
9981 }
9982 return htab;
9983 }
9984
9985 if (version == 1)
9986 {
9987 htab->section_pool.v1.indices =
9988 htab->unit_table + sizeof (uint32_t) * nr_slots;
9989 /* It's harder to decide whether the section is too small in v1.
9990 V1 is deprecated anyway so we punt. */
9991 }
9992 else
9993 {
9994 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
9995 int *ids = htab->section_pool.v2.section_ids;
9996 /* Reverse map for error checking. */
9997 int ids_seen[DW_SECT_MAX + 1];
9998 int i;
9999
10000 if (nr_columns < 2)
10001 {
10002 error (_("Dwarf Error: bad DWP hash table, too few columns"
10003 " in section table [in module %s]"),
10004 dwp_file->name);
10005 }
10006 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
10007 {
10008 error (_("Dwarf Error: bad DWP hash table, too many columns"
10009 " in section table [in module %s]"),
10010 dwp_file->name);
10011 }
10012 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10013 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
10014 for (i = 0; i < nr_columns; ++i)
10015 {
10016 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
10017
10018 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
10019 {
10020 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
10021 " in section table [in module %s]"),
10022 id, dwp_file->name);
10023 }
10024 if (ids_seen[id] != -1)
10025 {
10026 error (_("Dwarf Error: bad DWP hash table, duplicate section"
10027 " id %d in section table [in module %s]"),
10028 id, dwp_file->name);
10029 }
10030 ids_seen[id] = i;
10031 ids[i] = id;
10032 }
10033 /* Must have exactly one info or types section. */
10034 if (((ids_seen[DW_SECT_INFO] != -1)
10035 + (ids_seen[DW_SECT_TYPES] != -1))
10036 != 1)
10037 {
10038 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
10039 " DWO info/types section [in module %s]"),
10040 dwp_file->name);
10041 }
10042 /* Must have an abbrev section. */
10043 if (ids_seen[DW_SECT_ABBREV] == -1)
10044 {
10045 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
10046 " section [in module %s]"),
10047 dwp_file->name);
10048 }
10049 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
10050 htab->section_pool.v2.sizes =
10051 htab->section_pool.v2.offsets + (sizeof (uint32_t)
10052 * nr_units * nr_columns);
10053 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
10054 * nr_units * nr_columns))
10055 > index_end)
10056 {
10057 error (_("Dwarf Error: DWP index section is corrupt (too small)"
10058 " [in module %s]"),
10059 dwp_file->name);
10060 }
10061 }
10062
10063 return htab;
10064 }
10065
10066 /* Update SECTIONS with the data from SECTP.
10067
10068 This function is like the other "locate" section routines that are
10069 passed to bfd_map_over_sections, but in this context the sections to
10070 read comes from the DWP V1 hash table, not the full ELF section table.
10071
10072 The result is non-zero for success, or zero if an error was found. */
10073
10074 static int
10075 locate_v1_virtual_dwo_sections (asection *sectp,
10076 struct virtual_v1_dwo_sections *sections)
10077 {
10078 const struct dwop_section_names *names = &dwop_section_names;
10079
10080 if (section_is_p (sectp->name, &names->abbrev_dwo))
10081 {
10082 /* There can be only one. */
10083 if (sections->abbrev.s.section != NULL)
10084 return 0;
10085 sections->abbrev.s.section = sectp;
10086 sections->abbrev.size = bfd_get_section_size (sectp);
10087 }
10088 else if (section_is_p (sectp->name, &names->info_dwo)
10089 || section_is_p (sectp->name, &names->types_dwo))
10090 {
10091 /* There can be only one. */
10092 if (sections->info_or_types.s.section != NULL)
10093 return 0;
10094 sections->info_or_types.s.section = sectp;
10095 sections->info_or_types.size = bfd_get_section_size (sectp);
10096 }
10097 else if (section_is_p (sectp->name, &names->line_dwo))
10098 {
10099 /* There can be only one. */
10100 if (sections->line.s.section != NULL)
10101 return 0;
10102 sections->line.s.section = sectp;
10103 sections->line.size = bfd_get_section_size (sectp);
10104 }
10105 else if (section_is_p (sectp->name, &names->loc_dwo))
10106 {
10107 /* There can be only one. */
10108 if (sections->loc.s.section != NULL)
10109 return 0;
10110 sections->loc.s.section = sectp;
10111 sections->loc.size = bfd_get_section_size (sectp);
10112 }
10113 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10114 {
10115 /* There can be only one. */
10116 if (sections->macinfo.s.section != NULL)
10117 return 0;
10118 sections->macinfo.s.section = sectp;
10119 sections->macinfo.size = bfd_get_section_size (sectp);
10120 }
10121 else if (section_is_p (sectp->name, &names->macro_dwo))
10122 {
10123 /* There can be only one. */
10124 if (sections->macro.s.section != NULL)
10125 return 0;
10126 sections->macro.s.section = sectp;
10127 sections->macro.size = bfd_get_section_size (sectp);
10128 }
10129 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10130 {
10131 /* There can be only one. */
10132 if (sections->str_offsets.s.section != NULL)
10133 return 0;
10134 sections->str_offsets.s.section = sectp;
10135 sections->str_offsets.size = bfd_get_section_size (sectp);
10136 }
10137 else
10138 {
10139 /* No other kind of section is valid. */
10140 return 0;
10141 }
10142
10143 return 1;
10144 }
10145
10146 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10147 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10148 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10149 This is for DWP version 1 files. */
10150
10151 static struct dwo_unit *
10152 create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
10153 uint32_t unit_index,
10154 const char *comp_dir,
10155 ULONGEST signature, int is_debug_types)
10156 {
10157 struct objfile *objfile = dwarf2_per_objfile->objfile;
10158 const struct dwp_hash_table *dwp_htab =
10159 is_debug_types ? dwp_file->tus : dwp_file->cus;
10160 bfd *dbfd = dwp_file->dbfd;
10161 const char *kind = is_debug_types ? "TU" : "CU";
10162 struct dwo_file *dwo_file;
10163 struct dwo_unit *dwo_unit;
10164 struct virtual_v1_dwo_sections sections;
10165 void **dwo_file_slot;
10166 char *virtual_dwo_name;
10167 struct cleanup *cleanups;
10168 int i;
10169
10170 gdb_assert (dwp_file->version == 1);
10171
10172 if (dwarf_read_debug)
10173 {
10174 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
10175 kind,
10176 pulongest (unit_index), hex_string (signature),
10177 dwp_file->name);
10178 }
10179
10180 /* Fetch the sections of this DWO unit.
10181 Put a limit on the number of sections we look for so that bad data
10182 doesn't cause us to loop forever. */
10183
10184 #define MAX_NR_V1_DWO_SECTIONS \
10185 (1 /* .debug_info or .debug_types */ \
10186 + 1 /* .debug_abbrev */ \
10187 + 1 /* .debug_line */ \
10188 + 1 /* .debug_loc */ \
10189 + 1 /* .debug_str_offsets */ \
10190 + 1 /* .debug_macro or .debug_macinfo */ \
10191 + 1 /* trailing zero */)
10192
10193 memset (&sections, 0, sizeof (sections));
10194 cleanups = make_cleanup (null_cleanup, 0);
10195
10196 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
10197 {
10198 asection *sectp;
10199 uint32_t section_nr =
10200 read_4_bytes (dbfd,
10201 dwp_htab->section_pool.v1.indices
10202 + (unit_index + i) * sizeof (uint32_t));
10203
10204 if (section_nr == 0)
10205 break;
10206 if (section_nr >= dwp_file->num_sections)
10207 {
10208 error (_("Dwarf Error: bad DWP hash table, section number too large"
10209 " [in module %s]"),
10210 dwp_file->name);
10211 }
10212
10213 sectp = dwp_file->elf_sections[section_nr];
10214 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
10215 {
10216 error (_("Dwarf Error: bad DWP hash table, invalid section found"
10217 " [in module %s]"),
10218 dwp_file->name);
10219 }
10220 }
10221
10222 if (i < 2
10223 || dwarf2_section_empty_p (&sections.info_or_types)
10224 || dwarf2_section_empty_p (&sections.abbrev))
10225 {
10226 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
10227 " [in module %s]"),
10228 dwp_file->name);
10229 }
10230 if (i == MAX_NR_V1_DWO_SECTIONS)
10231 {
10232 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
10233 " [in module %s]"),
10234 dwp_file->name);
10235 }
10236
10237 /* It's easier for the rest of the code if we fake a struct dwo_file and
10238 have dwo_unit "live" in that. At least for now.
10239
10240 The DWP file can be made up of a random collection of CUs and TUs.
10241 However, for each CU + set of TUs that came from the same original DWO
10242 file, we can combine them back into a virtual DWO file to save space
10243 (fewer struct dwo_file objects to allocate). Remember that for really
10244 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10245
10246 virtual_dwo_name =
10247 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
10248 get_section_id (&sections.abbrev),
10249 get_section_id (&sections.line),
10250 get_section_id (&sections.loc),
10251 get_section_id (&sections.str_offsets));
10252 make_cleanup (xfree, virtual_dwo_name);
10253 /* Can we use an existing virtual DWO file? */
10254 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10255 /* Create one if necessary. */
10256 if (*dwo_file_slot == NULL)
10257 {
10258 if (dwarf_read_debug)
10259 {
10260 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10261 virtual_dwo_name);
10262 }
10263 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10264 dwo_file->dwo_name
10265 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10266 virtual_dwo_name,
10267 strlen (virtual_dwo_name));
10268 dwo_file->comp_dir = comp_dir;
10269 dwo_file->sections.abbrev = sections.abbrev;
10270 dwo_file->sections.line = sections.line;
10271 dwo_file->sections.loc = sections.loc;
10272 dwo_file->sections.macinfo = sections.macinfo;
10273 dwo_file->sections.macro = sections.macro;
10274 dwo_file->sections.str_offsets = sections.str_offsets;
10275 /* The "str" section is global to the entire DWP file. */
10276 dwo_file->sections.str = dwp_file->sections.str;
10277 /* The info or types section is assigned below to dwo_unit,
10278 there's no need to record it in dwo_file.
10279 Also, we can't simply record type sections in dwo_file because
10280 we record a pointer into the vector in dwo_unit. As we collect more
10281 types we'll grow the vector and eventually have to reallocate space
10282 for it, invalidating all copies of pointers into the previous
10283 contents. */
10284 *dwo_file_slot = dwo_file;
10285 }
10286 else
10287 {
10288 if (dwarf_read_debug)
10289 {
10290 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10291 virtual_dwo_name);
10292 }
10293 dwo_file = (struct dwo_file *) *dwo_file_slot;
10294 }
10295 do_cleanups (cleanups);
10296
10297 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10298 dwo_unit->dwo_file = dwo_file;
10299 dwo_unit->signature = signature;
10300 dwo_unit->section =
10301 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
10302 *dwo_unit->section = sections.info_or_types;
10303 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10304
10305 return dwo_unit;
10306 }
10307
10308 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
10309 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
10310 piece within that section used by a TU/CU, return a virtual section
10311 of just that piece. */
10312
10313 static struct dwarf2_section_info
10314 create_dwp_v2_section (struct dwarf2_section_info *section,
10315 bfd_size_type offset, bfd_size_type size)
10316 {
10317 struct dwarf2_section_info result;
10318 asection *sectp;
10319
10320 gdb_assert (section != NULL);
10321 gdb_assert (!section->is_virtual);
10322
10323 memset (&result, 0, sizeof (result));
10324 result.s.containing_section = section;
10325 result.is_virtual = 1;
10326
10327 if (size == 0)
10328 return result;
10329
10330 sectp = get_section_bfd_section (section);
10331
10332 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
10333 bounds of the real section. This is a pretty-rare event, so just
10334 flag an error (easier) instead of a warning and trying to cope. */
10335 if (sectp == NULL
10336 || offset + size > bfd_get_section_size (sectp))
10337 {
10338 bfd *abfd = sectp->owner;
10339
10340 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
10341 " in section %s [in module %s]"),
10342 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
10343 objfile_name (dwarf2_per_objfile->objfile));
10344 }
10345
10346 result.virtual_offset = offset;
10347 result.size = size;
10348 return result;
10349 }
10350
10351 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
10352 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
10353 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
10354 This is for DWP version 2 files. */
10355
10356 static struct dwo_unit *
10357 create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
10358 uint32_t unit_index,
10359 const char *comp_dir,
10360 ULONGEST signature, int is_debug_types)
10361 {
10362 struct objfile *objfile = dwarf2_per_objfile->objfile;
10363 const struct dwp_hash_table *dwp_htab =
10364 is_debug_types ? dwp_file->tus : dwp_file->cus;
10365 bfd *dbfd = dwp_file->dbfd;
10366 const char *kind = is_debug_types ? "TU" : "CU";
10367 struct dwo_file *dwo_file;
10368 struct dwo_unit *dwo_unit;
10369 struct virtual_v2_dwo_sections sections;
10370 void **dwo_file_slot;
10371 char *virtual_dwo_name;
10372 struct cleanup *cleanups;
10373 int i;
10374
10375 gdb_assert (dwp_file->version == 2);
10376
10377 if (dwarf_read_debug)
10378 {
10379 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
10380 kind,
10381 pulongest (unit_index), hex_string (signature),
10382 dwp_file->name);
10383 }
10384
10385 /* Fetch the section offsets of this DWO unit. */
10386
10387 memset (&sections, 0, sizeof (sections));
10388 cleanups = make_cleanup (null_cleanup, 0);
10389
10390 for (i = 0; i < dwp_htab->nr_columns; ++i)
10391 {
10392 uint32_t offset = read_4_bytes (dbfd,
10393 dwp_htab->section_pool.v2.offsets
10394 + (((unit_index - 1) * dwp_htab->nr_columns
10395 + i)
10396 * sizeof (uint32_t)));
10397 uint32_t size = read_4_bytes (dbfd,
10398 dwp_htab->section_pool.v2.sizes
10399 + (((unit_index - 1) * dwp_htab->nr_columns
10400 + i)
10401 * sizeof (uint32_t)));
10402
10403 switch (dwp_htab->section_pool.v2.section_ids[i])
10404 {
10405 case DW_SECT_INFO:
10406 case DW_SECT_TYPES:
10407 sections.info_or_types_offset = offset;
10408 sections.info_or_types_size = size;
10409 break;
10410 case DW_SECT_ABBREV:
10411 sections.abbrev_offset = offset;
10412 sections.abbrev_size = size;
10413 break;
10414 case DW_SECT_LINE:
10415 sections.line_offset = offset;
10416 sections.line_size = size;
10417 break;
10418 case DW_SECT_LOC:
10419 sections.loc_offset = offset;
10420 sections.loc_size = size;
10421 break;
10422 case DW_SECT_STR_OFFSETS:
10423 sections.str_offsets_offset = offset;
10424 sections.str_offsets_size = size;
10425 break;
10426 case DW_SECT_MACINFO:
10427 sections.macinfo_offset = offset;
10428 sections.macinfo_size = size;
10429 break;
10430 case DW_SECT_MACRO:
10431 sections.macro_offset = offset;
10432 sections.macro_size = size;
10433 break;
10434 }
10435 }
10436
10437 /* It's easier for the rest of the code if we fake a struct dwo_file and
10438 have dwo_unit "live" in that. At least for now.
10439
10440 The DWP file can be made up of a random collection of CUs and TUs.
10441 However, for each CU + set of TUs that came from the same original DWO
10442 file, we can combine them back into a virtual DWO file to save space
10443 (fewer struct dwo_file objects to allocate). Remember that for really
10444 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
10445
10446 virtual_dwo_name =
10447 xstrprintf ("virtual-dwo/%ld-%ld-%ld-%ld",
10448 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
10449 (long) (sections.line_size ? sections.line_offset : 0),
10450 (long) (sections.loc_size ? sections.loc_offset : 0),
10451 (long) (sections.str_offsets_size
10452 ? sections.str_offsets_offset : 0));
10453 make_cleanup (xfree, virtual_dwo_name);
10454 /* Can we use an existing virtual DWO file? */
10455 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
10456 /* Create one if necessary. */
10457 if (*dwo_file_slot == NULL)
10458 {
10459 if (dwarf_read_debug)
10460 {
10461 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
10462 virtual_dwo_name);
10463 }
10464 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10465 dwo_file->dwo_name
10466 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10467 virtual_dwo_name,
10468 strlen (virtual_dwo_name));
10469 dwo_file->comp_dir = comp_dir;
10470 dwo_file->sections.abbrev =
10471 create_dwp_v2_section (&dwp_file->sections.abbrev,
10472 sections.abbrev_offset, sections.abbrev_size);
10473 dwo_file->sections.line =
10474 create_dwp_v2_section (&dwp_file->sections.line,
10475 sections.line_offset, sections.line_size);
10476 dwo_file->sections.loc =
10477 create_dwp_v2_section (&dwp_file->sections.loc,
10478 sections.loc_offset, sections.loc_size);
10479 dwo_file->sections.macinfo =
10480 create_dwp_v2_section (&dwp_file->sections.macinfo,
10481 sections.macinfo_offset, sections.macinfo_size);
10482 dwo_file->sections.macro =
10483 create_dwp_v2_section (&dwp_file->sections.macro,
10484 sections.macro_offset, sections.macro_size);
10485 dwo_file->sections.str_offsets =
10486 create_dwp_v2_section (&dwp_file->sections.str_offsets,
10487 sections.str_offsets_offset,
10488 sections.str_offsets_size);
10489 /* The "str" section is global to the entire DWP file. */
10490 dwo_file->sections.str = dwp_file->sections.str;
10491 /* The info or types section is assigned below to dwo_unit,
10492 there's no need to record it in dwo_file.
10493 Also, we can't simply record type sections in dwo_file because
10494 we record a pointer into the vector in dwo_unit. As we collect more
10495 types we'll grow the vector and eventually have to reallocate space
10496 for it, invalidating all copies of pointers into the previous
10497 contents. */
10498 *dwo_file_slot = dwo_file;
10499 }
10500 else
10501 {
10502 if (dwarf_read_debug)
10503 {
10504 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
10505 virtual_dwo_name);
10506 }
10507 dwo_file = (struct dwo_file *) *dwo_file_slot;
10508 }
10509 do_cleanups (cleanups);
10510
10511 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
10512 dwo_unit->dwo_file = dwo_file;
10513 dwo_unit->signature = signature;
10514 dwo_unit->section =
10515 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
10516 *dwo_unit->section = create_dwp_v2_section (is_debug_types
10517 ? &dwp_file->sections.types
10518 : &dwp_file->sections.info,
10519 sections.info_or_types_offset,
10520 sections.info_or_types_size);
10521 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
10522
10523 return dwo_unit;
10524 }
10525
10526 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
10527 Returns NULL if the signature isn't found. */
10528
10529 static struct dwo_unit *
10530 lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
10531 ULONGEST signature, int is_debug_types)
10532 {
10533 const struct dwp_hash_table *dwp_htab =
10534 is_debug_types ? dwp_file->tus : dwp_file->cus;
10535 bfd *dbfd = dwp_file->dbfd;
10536 uint32_t mask = dwp_htab->nr_slots - 1;
10537 uint32_t hash = signature & mask;
10538 uint32_t hash2 = ((signature >> 32) & mask) | 1;
10539 unsigned int i;
10540 void **slot;
10541 struct dwo_unit find_dwo_cu;
10542
10543 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
10544 find_dwo_cu.signature = signature;
10545 slot = htab_find_slot (is_debug_types
10546 ? dwp_file->loaded_tus
10547 : dwp_file->loaded_cus,
10548 &find_dwo_cu, INSERT);
10549
10550 if (*slot != NULL)
10551 return (struct dwo_unit *) *slot;
10552
10553 /* Use a for loop so that we don't loop forever on bad debug info. */
10554 for (i = 0; i < dwp_htab->nr_slots; ++i)
10555 {
10556 ULONGEST signature_in_table;
10557
10558 signature_in_table =
10559 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
10560 if (signature_in_table == signature)
10561 {
10562 uint32_t unit_index =
10563 read_4_bytes (dbfd,
10564 dwp_htab->unit_table + hash * sizeof (uint32_t));
10565
10566 if (dwp_file->version == 1)
10567 {
10568 *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
10569 comp_dir, signature,
10570 is_debug_types);
10571 }
10572 else
10573 {
10574 *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
10575 comp_dir, signature,
10576 is_debug_types);
10577 }
10578 return (struct dwo_unit *) *slot;
10579 }
10580 if (signature_in_table == 0)
10581 return NULL;
10582 hash = (hash + hash2) & mask;
10583 }
10584
10585 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
10586 " [in module %s]"),
10587 dwp_file->name);
10588 }
10589
10590 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
10591 Open the file specified by FILE_NAME and hand it off to BFD for
10592 preliminary analysis. Return a newly initialized bfd *, which
10593 includes a canonicalized copy of FILE_NAME.
10594 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
10595 SEARCH_CWD is true if the current directory is to be searched.
10596 It will be searched before debug-file-directory.
10597 If successful, the file is added to the bfd include table of the
10598 objfile's bfd (see gdb_bfd_record_inclusion).
10599 If unable to find/open the file, return NULL.
10600 NOTE: This function is derived from symfile_bfd_open. */
10601
10602 static gdb_bfd_ref_ptr
10603 try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
10604 {
10605 int desc, flags;
10606 char *absolute_name;
10607 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
10608 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
10609 to debug_file_directory. */
10610 char *search_path;
10611 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10612
10613 if (search_cwd)
10614 {
10615 if (*debug_file_directory != '\0')
10616 search_path = concat (".", dirname_separator_string,
10617 debug_file_directory, (char *) NULL);
10618 else
10619 search_path = xstrdup (".");
10620 }
10621 else
10622 search_path = xstrdup (debug_file_directory);
10623
10624 flags = OPF_RETURN_REALPATH;
10625 if (is_dwp)
10626 flags |= OPF_SEARCH_IN_PATH;
10627 desc = openp (search_path, flags, file_name,
10628 O_RDONLY | O_BINARY, &absolute_name);
10629 xfree (search_path);
10630 if (desc < 0)
10631 return NULL;
10632
10633 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name, gnutarget, desc));
10634 xfree (absolute_name);
10635 if (sym_bfd == NULL)
10636 return NULL;
10637 bfd_set_cacheable (sym_bfd.get (), 1);
10638
10639 if (!bfd_check_format (sym_bfd.get (), bfd_object))
10640 return NULL;
10641
10642 /* Success. Record the bfd as having been included by the objfile's bfd.
10643 This is important because things like demangled_names_hash lives in the
10644 objfile's per_bfd space and may have references to things like symbol
10645 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
10646 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
10647
10648 return sym_bfd;
10649 }
10650
10651 /* Try to open DWO file FILE_NAME.
10652 COMP_DIR is the DW_AT_comp_dir attribute.
10653 The result is the bfd handle of the file.
10654 If there is a problem finding or opening the file, return NULL.
10655 Upon success, the canonicalized path of the file is stored in the bfd,
10656 same as symfile_bfd_open. */
10657
10658 static gdb_bfd_ref_ptr
10659 open_dwo_file (const char *file_name, const char *comp_dir)
10660 {
10661 if (IS_ABSOLUTE_PATH (file_name))
10662 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
10663
10664 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
10665
10666 if (comp_dir != NULL)
10667 {
10668 char *path_to_try = concat (comp_dir, SLASH_STRING,
10669 file_name, (char *) NULL);
10670
10671 /* NOTE: If comp_dir is a relative path, this will also try the
10672 search path, which seems useful. */
10673 gdb_bfd_ref_ptr abfd (try_open_dwop_file (path_to_try, 0 /*is_dwp*/,
10674 1 /*search_cwd*/));
10675 xfree (path_to_try);
10676 if (abfd != NULL)
10677 return abfd;
10678 }
10679
10680 /* That didn't work, try debug-file-directory, which, despite its name,
10681 is a list of paths. */
10682
10683 if (*debug_file_directory == '\0')
10684 return NULL;
10685
10686 return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
10687 }
10688
10689 /* This function is mapped across the sections and remembers the offset and
10690 size of each of the DWO debugging sections we are interested in. */
10691
10692 static void
10693 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10694 {
10695 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
10696 const struct dwop_section_names *names = &dwop_section_names;
10697
10698 if (section_is_p (sectp->name, &names->abbrev_dwo))
10699 {
10700 dwo_sections->abbrev.s.section = sectp;
10701 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10702 }
10703 else if (section_is_p (sectp->name, &names->info_dwo))
10704 {
10705 dwo_sections->info.s.section = sectp;
10706 dwo_sections->info.size = bfd_get_section_size (sectp);
10707 }
10708 else if (section_is_p (sectp->name, &names->line_dwo))
10709 {
10710 dwo_sections->line.s.section = sectp;
10711 dwo_sections->line.size = bfd_get_section_size (sectp);
10712 }
10713 else if (section_is_p (sectp->name, &names->loc_dwo))
10714 {
10715 dwo_sections->loc.s.section = sectp;
10716 dwo_sections->loc.size = bfd_get_section_size (sectp);
10717 }
10718 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10719 {
10720 dwo_sections->macinfo.s.section = sectp;
10721 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10722 }
10723 else if (section_is_p (sectp->name, &names->macro_dwo))
10724 {
10725 dwo_sections->macro.s.section = sectp;
10726 dwo_sections->macro.size = bfd_get_section_size (sectp);
10727 }
10728 else if (section_is_p (sectp->name, &names->str_dwo))
10729 {
10730 dwo_sections->str.s.section = sectp;
10731 dwo_sections->str.size = bfd_get_section_size (sectp);
10732 }
10733 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10734 {
10735 dwo_sections->str_offsets.s.section = sectp;
10736 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10737 }
10738 else if (section_is_p (sectp->name, &names->types_dwo))
10739 {
10740 struct dwarf2_section_info type_section;
10741
10742 memset (&type_section, 0, sizeof (type_section));
10743 type_section.s.section = sectp;
10744 type_section.size = bfd_get_section_size (sectp);
10745 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10746 &type_section);
10747 }
10748 }
10749
10750 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
10751 by PER_CU. This is for the non-DWP case.
10752 The result is NULL if DWO_NAME can't be found. */
10753
10754 static struct dwo_file *
10755 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10756 const char *dwo_name, const char *comp_dir)
10757 {
10758 struct objfile *objfile = dwarf2_per_objfile->objfile;
10759 struct dwo_file *dwo_file;
10760 struct cleanup *cleanups;
10761
10762 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwo_name, comp_dir));
10763 if (dbfd == NULL)
10764 {
10765 if (dwarf_read_debug)
10766 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
10767 return NULL;
10768 }
10769 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10770 dwo_file->dwo_name = dwo_name;
10771 dwo_file->comp_dir = comp_dir;
10772 dwo_file->dbfd = dbfd.release ();
10773
10774 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10775
10776 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
10777 &dwo_file->sections);
10778
10779 dwo_file->cu = create_dwo_cu (dwo_file);
10780
10781 create_debug_types_hash_table (dwo_file, dwo_file->sections.types,
10782 dwo_file->tus);
10783
10784 discard_cleanups (cleanups);
10785
10786 if (dwarf_read_debug)
10787 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10788
10789 return dwo_file;
10790 }
10791
10792 /* This function is mapped across the sections and remembers the offset and
10793 size of each of the DWP debugging sections common to version 1 and 2 that
10794 we are interested in. */
10795
10796 static void
10797 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10798 void *dwp_file_ptr)
10799 {
10800 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
10801 const struct dwop_section_names *names = &dwop_section_names;
10802 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10803
10804 /* Record the ELF section number for later lookup: this is what the
10805 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10806 gdb_assert (elf_section_nr < dwp_file->num_sections);
10807 dwp_file->elf_sections[elf_section_nr] = sectp;
10808
10809 /* Look for specific sections that we need. */
10810 if (section_is_p (sectp->name, &names->str_dwo))
10811 {
10812 dwp_file->sections.str.s.section = sectp;
10813 dwp_file->sections.str.size = bfd_get_section_size (sectp);
10814 }
10815 else if (section_is_p (sectp->name, &names->cu_index))
10816 {
10817 dwp_file->sections.cu_index.s.section = sectp;
10818 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
10819 }
10820 else if (section_is_p (sectp->name, &names->tu_index))
10821 {
10822 dwp_file->sections.tu_index.s.section = sectp;
10823 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
10824 }
10825 }
10826
10827 /* This function is mapped across the sections and remembers the offset and
10828 size of each of the DWP version 2 debugging sections that we are interested
10829 in. This is split into a separate function because we don't know if we
10830 have version 1 or 2 until we parse the cu_index/tu_index sections. */
10831
10832 static void
10833 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
10834 {
10835 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
10836 const struct dwop_section_names *names = &dwop_section_names;
10837 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10838
10839 /* Record the ELF section number for later lookup: this is what the
10840 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
10841 gdb_assert (elf_section_nr < dwp_file->num_sections);
10842 dwp_file->elf_sections[elf_section_nr] = sectp;
10843
10844 /* Look for specific sections that we need. */
10845 if (section_is_p (sectp->name, &names->abbrev_dwo))
10846 {
10847 dwp_file->sections.abbrev.s.section = sectp;
10848 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
10849 }
10850 else if (section_is_p (sectp->name, &names->info_dwo))
10851 {
10852 dwp_file->sections.info.s.section = sectp;
10853 dwp_file->sections.info.size = bfd_get_section_size (sectp);
10854 }
10855 else if (section_is_p (sectp->name, &names->line_dwo))
10856 {
10857 dwp_file->sections.line.s.section = sectp;
10858 dwp_file->sections.line.size = bfd_get_section_size (sectp);
10859 }
10860 else if (section_is_p (sectp->name, &names->loc_dwo))
10861 {
10862 dwp_file->sections.loc.s.section = sectp;
10863 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
10864 }
10865 else if (section_is_p (sectp->name, &names->macinfo_dwo))
10866 {
10867 dwp_file->sections.macinfo.s.section = sectp;
10868 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
10869 }
10870 else if (section_is_p (sectp->name, &names->macro_dwo))
10871 {
10872 dwp_file->sections.macro.s.section = sectp;
10873 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
10874 }
10875 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10876 {
10877 dwp_file->sections.str_offsets.s.section = sectp;
10878 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
10879 }
10880 else if (section_is_p (sectp->name, &names->types_dwo))
10881 {
10882 dwp_file->sections.types.s.section = sectp;
10883 dwp_file->sections.types.size = bfd_get_section_size (sectp);
10884 }
10885 }
10886
10887 /* Hash function for dwp_file loaded CUs/TUs. */
10888
10889 static hashval_t
10890 hash_dwp_loaded_cutus (const void *item)
10891 {
10892 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10893
10894 /* This drops the top 32 bits of the signature, but is ok for a hash. */
10895 return dwo_unit->signature;
10896 }
10897
10898 /* Equality function for dwp_file loaded CUs/TUs. */
10899
10900 static int
10901 eq_dwp_loaded_cutus (const void *a, const void *b)
10902 {
10903 const struct dwo_unit *dua = (const struct dwo_unit *) a;
10904 const struct dwo_unit *dub = (const struct dwo_unit *) b;
10905
10906 return dua->signature == dub->signature;
10907 }
10908
10909 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
10910
10911 static htab_t
10912 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
10913 {
10914 return htab_create_alloc_ex (3,
10915 hash_dwp_loaded_cutus,
10916 eq_dwp_loaded_cutus,
10917 NULL,
10918 &objfile->objfile_obstack,
10919 hashtab_obstack_allocate,
10920 dummy_obstack_deallocate);
10921 }
10922
10923 /* Try to open DWP file FILE_NAME.
10924 The result is the bfd handle of the file.
10925 If there is a problem finding or opening the file, return NULL.
10926 Upon success, the canonicalized path of the file is stored in the bfd,
10927 same as symfile_bfd_open. */
10928
10929 static gdb_bfd_ref_ptr
10930 open_dwp_file (const char *file_name)
10931 {
10932 gdb_bfd_ref_ptr abfd (try_open_dwop_file (file_name, 1 /*is_dwp*/,
10933 1 /*search_cwd*/));
10934 if (abfd != NULL)
10935 return abfd;
10936
10937 /* Work around upstream bug 15652.
10938 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
10939 [Whether that's a "bug" is debatable, but it is getting in our way.]
10940 We have no real idea where the dwp file is, because gdb's realpath-ing
10941 of the executable's path may have discarded the needed info.
10942 [IWBN if the dwp file name was recorded in the executable, akin to
10943 .gnu_debuglink, but that doesn't exist yet.]
10944 Strip the directory from FILE_NAME and search again. */
10945 if (*debug_file_directory != '\0')
10946 {
10947 /* Don't implicitly search the current directory here.
10948 If the user wants to search "." to handle this case,
10949 it must be added to debug-file-directory. */
10950 return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
10951 0 /*search_cwd*/);
10952 }
10953
10954 return NULL;
10955 }
10956
10957 /* Initialize the use of the DWP file for the current objfile.
10958 By convention the name of the DWP file is ${objfile}.dwp.
10959 The result is NULL if it can't be found. */
10960
10961 static struct dwp_file *
10962 open_and_init_dwp_file (void)
10963 {
10964 struct objfile *objfile = dwarf2_per_objfile->objfile;
10965 struct dwp_file *dwp_file;
10966
10967 /* Try to find first .dwp for the binary file before any symbolic links
10968 resolving. */
10969
10970 /* If the objfile is a debug file, find the name of the real binary
10971 file and get the name of dwp file from there. */
10972 std::string dwp_name;
10973 if (objfile->separate_debug_objfile_backlink != NULL)
10974 {
10975 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
10976 const char *backlink_basename = lbasename (backlink->original_name);
10977
10978 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
10979 }
10980 else
10981 dwp_name = objfile->original_name;
10982
10983 dwp_name += ".dwp";
10984
10985 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwp_name.c_str ()));
10986 if (dbfd == NULL
10987 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
10988 {
10989 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
10990 dwp_name = objfile_name (objfile);
10991 dwp_name += ".dwp";
10992 dbfd = open_dwp_file (dwp_name.c_str ());
10993 }
10994
10995 if (dbfd == NULL)
10996 {
10997 if (dwarf_read_debug)
10998 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
10999 return NULL;
11000 }
11001 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
11002 dwp_file->name = bfd_get_filename (dbfd.get ());
11003 dwp_file->dbfd = dbfd.release ();
11004
11005 /* +1: section 0 is unused */
11006 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
11007 dwp_file->elf_sections =
11008 OBSTACK_CALLOC (&objfile->objfile_obstack,
11009 dwp_file->num_sections, asection *);
11010
11011 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
11012 dwp_file);
11013
11014 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
11015
11016 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
11017
11018 /* The DWP file version is stored in the hash table. Oh well. */
11019 if (dwp_file->cus->version != dwp_file->tus->version)
11020 {
11021 /* Technically speaking, we should try to limp along, but this is
11022 pretty bizarre. We use pulongest here because that's the established
11023 portability solution (e.g, we cannot use %u for uint32_t). */
11024 error (_("Dwarf Error: DWP file CU version %s doesn't match"
11025 " TU version %s [in DWP file %s]"),
11026 pulongest (dwp_file->cus->version),
11027 pulongest (dwp_file->tus->version), dwp_name.c_str ());
11028 }
11029 dwp_file->version = dwp_file->cus->version;
11030
11031 if (dwp_file->version == 2)
11032 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
11033 dwp_file);
11034
11035 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
11036 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
11037
11038 if (dwarf_read_debug)
11039 {
11040 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
11041 fprintf_unfiltered (gdb_stdlog,
11042 " %s CUs, %s TUs\n",
11043 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
11044 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
11045 }
11046
11047 return dwp_file;
11048 }
11049
11050 /* Wrapper around open_and_init_dwp_file, only open it once. */
11051
11052 static struct dwp_file *
11053 get_dwp_file (void)
11054 {
11055 if (! dwarf2_per_objfile->dwp_checked)
11056 {
11057 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
11058 dwarf2_per_objfile->dwp_checked = 1;
11059 }
11060 return dwarf2_per_objfile->dwp_file;
11061 }
11062
11063 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
11064 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
11065 or in the DWP file for the objfile, referenced by THIS_UNIT.
11066 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
11067 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
11068
11069 This is called, for example, when wanting to read a variable with a
11070 complex location. Therefore we don't want to do file i/o for every call.
11071 Therefore we don't want to look for a DWO file on every call.
11072 Therefore we first see if we've already seen SIGNATURE in a DWP file,
11073 then we check if we've already seen DWO_NAME, and only THEN do we check
11074 for a DWO file.
11075
11076 The result is a pointer to the dwo_unit object or NULL if we didn't find it
11077 (dwo_id mismatch or couldn't find the DWO/DWP file). */
11078
11079 static struct dwo_unit *
11080 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
11081 const char *dwo_name, const char *comp_dir,
11082 ULONGEST signature, int is_debug_types)
11083 {
11084 struct objfile *objfile = dwarf2_per_objfile->objfile;
11085 const char *kind = is_debug_types ? "TU" : "CU";
11086 void **dwo_file_slot;
11087 struct dwo_file *dwo_file;
11088 struct dwp_file *dwp_file;
11089
11090 /* First see if there's a DWP file.
11091 If we have a DWP file but didn't find the DWO inside it, don't
11092 look for the original DWO file. It makes gdb behave differently
11093 depending on whether one is debugging in the build tree. */
11094
11095 dwp_file = get_dwp_file ();
11096 if (dwp_file != NULL)
11097 {
11098 const struct dwp_hash_table *dwp_htab =
11099 is_debug_types ? dwp_file->tus : dwp_file->cus;
11100
11101 if (dwp_htab != NULL)
11102 {
11103 struct dwo_unit *dwo_cutu =
11104 lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
11105 signature, is_debug_types);
11106
11107 if (dwo_cutu != NULL)
11108 {
11109 if (dwarf_read_debug)
11110 {
11111 fprintf_unfiltered (gdb_stdlog,
11112 "Virtual DWO %s %s found: @%s\n",
11113 kind, hex_string (signature),
11114 host_address_to_string (dwo_cutu));
11115 }
11116 return dwo_cutu;
11117 }
11118 }
11119 }
11120 else
11121 {
11122 /* No DWP file, look for the DWO file. */
11123
11124 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
11125 if (*dwo_file_slot == NULL)
11126 {
11127 /* Read in the file and build a table of the CUs/TUs it contains. */
11128 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
11129 }
11130 /* NOTE: This will be NULL if unable to open the file. */
11131 dwo_file = (struct dwo_file *) *dwo_file_slot;
11132
11133 if (dwo_file != NULL)
11134 {
11135 struct dwo_unit *dwo_cutu = NULL;
11136
11137 if (is_debug_types && dwo_file->tus)
11138 {
11139 struct dwo_unit find_dwo_cutu;
11140
11141 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
11142 find_dwo_cutu.signature = signature;
11143 dwo_cutu
11144 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
11145 }
11146 else if (!is_debug_types && dwo_file->cu)
11147 {
11148 if (signature == dwo_file->cu->signature)
11149 dwo_cutu = dwo_file->cu;
11150 }
11151
11152 if (dwo_cutu != NULL)
11153 {
11154 if (dwarf_read_debug)
11155 {
11156 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
11157 kind, dwo_name, hex_string (signature),
11158 host_address_to_string (dwo_cutu));
11159 }
11160 return dwo_cutu;
11161 }
11162 }
11163 }
11164
11165 /* We didn't find it. This could mean a dwo_id mismatch, or
11166 someone deleted the DWO/DWP file, or the search path isn't set up
11167 correctly to find the file. */
11168
11169 if (dwarf_read_debug)
11170 {
11171 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
11172 kind, dwo_name, hex_string (signature));
11173 }
11174
11175 /* This is a warning and not a complaint because it can be caused by
11176 pilot error (e.g., user accidentally deleting the DWO). */
11177 {
11178 /* Print the name of the DWP file if we looked there, helps the user
11179 better diagnose the problem. */
11180 char *dwp_text = NULL;
11181 struct cleanup *cleanups;
11182
11183 if (dwp_file != NULL)
11184 dwp_text = xstrprintf (" [in DWP file %s]", lbasename (dwp_file->name));
11185 cleanups = make_cleanup (xfree, dwp_text);
11186
11187 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
11188 " [in module %s]"),
11189 kind, dwo_name, hex_string (signature),
11190 dwp_text != NULL ? dwp_text : "",
11191 this_unit->is_debug_types ? "TU" : "CU",
11192 this_unit->offset.sect_off, objfile_name (objfile));
11193
11194 do_cleanups (cleanups);
11195 }
11196 return NULL;
11197 }
11198
11199 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
11200 See lookup_dwo_cutu_unit for details. */
11201
11202 static struct dwo_unit *
11203 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
11204 const char *dwo_name, const char *comp_dir,
11205 ULONGEST signature)
11206 {
11207 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
11208 }
11209
11210 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
11211 See lookup_dwo_cutu_unit for details. */
11212
11213 static struct dwo_unit *
11214 lookup_dwo_type_unit (struct signatured_type *this_tu,
11215 const char *dwo_name, const char *comp_dir)
11216 {
11217 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
11218 }
11219
11220 /* Traversal function for queue_and_load_all_dwo_tus. */
11221
11222 static int
11223 queue_and_load_dwo_tu (void **slot, void *info)
11224 {
11225 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
11226 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
11227 ULONGEST signature = dwo_unit->signature;
11228 struct signatured_type *sig_type =
11229 lookup_dwo_signatured_type (per_cu->cu, signature);
11230
11231 if (sig_type != NULL)
11232 {
11233 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
11234
11235 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
11236 a real dependency of PER_CU on SIG_TYPE. That is detected later
11237 while processing PER_CU. */
11238 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
11239 load_full_type_unit (sig_cu);
11240 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
11241 }
11242
11243 return 1;
11244 }
11245
11246 /* Queue all TUs contained in the DWO of PER_CU to be read in.
11247 The DWO may have the only definition of the type, though it may not be
11248 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
11249 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
11250
11251 static void
11252 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
11253 {
11254 struct dwo_unit *dwo_unit;
11255 struct dwo_file *dwo_file;
11256
11257 gdb_assert (!per_cu->is_debug_types);
11258 gdb_assert (get_dwp_file () == NULL);
11259 gdb_assert (per_cu->cu != NULL);
11260
11261 dwo_unit = per_cu->cu->dwo_unit;
11262 gdb_assert (dwo_unit != NULL);
11263
11264 dwo_file = dwo_unit->dwo_file;
11265 if (dwo_file->tus != NULL)
11266 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
11267 }
11268
11269 /* Free all resources associated with DWO_FILE.
11270 Close the DWO file and munmap the sections.
11271 All memory should be on the objfile obstack. */
11272
11273 static void
11274 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
11275 {
11276
11277 /* Note: dbfd is NULL for virtual DWO files. */
11278 gdb_bfd_unref (dwo_file->dbfd);
11279
11280 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
11281 }
11282
11283 /* Wrapper for free_dwo_file for use in cleanups. */
11284
11285 static void
11286 free_dwo_file_cleanup (void *arg)
11287 {
11288 struct dwo_file *dwo_file = (struct dwo_file *) arg;
11289 struct objfile *objfile = dwarf2_per_objfile->objfile;
11290
11291 free_dwo_file (dwo_file, objfile);
11292 }
11293
11294 /* Traversal function for free_dwo_files. */
11295
11296 static int
11297 free_dwo_file_from_slot (void **slot, void *info)
11298 {
11299 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
11300 struct objfile *objfile = (struct objfile *) info;
11301
11302 free_dwo_file (dwo_file, objfile);
11303
11304 return 1;
11305 }
11306
11307 /* Free all resources associated with DWO_FILES. */
11308
11309 static void
11310 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
11311 {
11312 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
11313 }
11314 \f
11315 /* Read in various DIEs. */
11316
11317 /* qsort helper for inherit_abstract_dies. */
11318
11319 static int
11320 unsigned_int_compar (const void *ap, const void *bp)
11321 {
11322 unsigned int a = *(unsigned int *) ap;
11323 unsigned int b = *(unsigned int *) bp;
11324
11325 return (a > b) - (b > a);
11326 }
11327
11328 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
11329 Inherit only the children of the DW_AT_abstract_origin DIE not being
11330 already referenced by DW_AT_abstract_origin from the children of the
11331 current DIE. */
11332
11333 static void
11334 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
11335 {
11336 struct die_info *child_die;
11337 unsigned die_children_count;
11338 /* CU offsets which were referenced by children of the current DIE. */
11339 sect_offset *offsets;
11340 sect_offset *offsets_end, *offsetp;
11341 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
11342 struct die_info *origin_die;
11343 /* Iterator of the ORIGIN_DIE children. */
11344 struct die_info *origin_child_die;
11345 struct cleanup *cleanups;
11346 struct attribute *attr;
11347 struct dwarf2_cu *origin_cu;
11348 struct pending **origin_previous_list_in_scope;
11349
11350 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11351 if (!attr)
11352 return;
11353
11354 /* Note that following die references may follow to a die in a
11355 different cu. */
11356
11357 origin_cu = cu;
11358 origin_die = follow_die_ref (die, attr, &origin_cu);
11359
11360 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
11361 symbols in. */
11362 origin_previous_list_in_scope = origin_cu->list_in_scope;
11363 origin_cu->list_in_scope = cu->list_in_scope;
11364
11365 if (die->tag != origin_die->tag
11366 && !(die->tag == DW_TAG_inlined_subroutine
11367 && origin_die->tag == DW_TAG_subprogram))
11368 complaint (&symfile_complaints,
11369 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
11370 die->offset.sect_off, origin_die->offset.sect_off);
11371
11372 child_die = die->child;
11373 die_children_count = 0;
11374 while (child_die && child_die->tag)
11375 {
11376 child_die = sibling_die (child_die);
11377 die_children_count++;
11378 }
11379 offsets = XNEWVEC (sect_offset, die_children_count);
11380 cleanups = make_cleanup (xfree, offsets);
11381
11382 offsets_end = offsets;
11383 for (child_die = die->child;
11384 child_die && child_die->tag;
11385 child_die = sibling_die (child_die))
11386 {
11387 struct die_info *child_origin_die;
11388 struct dwarf2_cu *child_origin_cu;
11389
11390 /* We are trying to process concrete instance entries:
11391 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
11392 it's not relevant to our analysis here. i.e. detecting DIEs that are
11393 present in the abstract instance but not referenced in the concrete
11394 one. */
11395 if (child_die->tag == DW_TAG_call_site
11396 || child_die->tag == DW_TAG_GNU_call_site)
11397 continue;
11398
11399 /* For each CHILD_DIE, find the corresponding child of
11400 ORIGIN_DIE. If there is more than one layer of
11401 DW_AT_abstract_origin, follow them all; there shouldn't be,
11402 but GCC versions at least through 4.4 generate this (GCC PR
11403 40573). */
11404 child_origin_die = child_die;
11405 child_origin_cu = cu;
11406 while (1)
11407 {
11408 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
11409 child_origin_cu);
11410 if (attr == NULL)
11411 break;
11412 child_origin_die = follow_die_ref (child_origin_die, attr,
11413 &child_origin_cu);
11414 }
11415
11416 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
11417 counterpart may exist. */
11418 if (child_origin_die != child_die)
11419 {
11420 if (child_die->tag != child_origin_die->tag
11421 && !(child_die->tag == DW_TAG_inlined_subroutine
11422 && child_origin_die->tag == DW_TAG_subprogram))
11423 complaint (&symfile_complaints,
11424 _("Child DIE 0x%x and its abstract origin 0x%x have "
11425 "different tags"), child_die->offset.sect_off,
11426 child_origin_die->offset.sect_off);
11427 if (child_origin_die->parent != origin_die)
11428 complaint (&symfile_complaints,
11429 _("Child DIE 0x%x and its abstract origin 0x%x have "
11430 "different parents"), child_die->offset.sect_off,
11431 child_origin_die->offset.sect_off);
11432 else
11433 *offsets_end++ = child_origin_die->offset;
11434 }
11435 }
11436 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
11437 unsigned_int_compar);
11438 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
11439 if (offsetp[-1].sect_off == offsetp->sect_off)
11440 complaint (&symfile_complaints,
11441 _("Multiple children of DIE 0x%x refer "
11442 "to DIE 0x%x as their abstract origin"),
11443 die->offset.sect_off, offsetp->sect_off);
11444
11445 offsetp = offsets;
11446 origin_child_die = origin_die->child;
11447 while (origin_child_die && origin_child_die->tag)
11448 {
11449 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
11450 while (offsetp < offsets_end
11451 && offsetp->sect_off < origin_child_die->offset.sect_off)
11452 offsetp++;
11453 if (offsetp >= offsets_end
11454 || offsetp->sect_off > origin_child_die->offset.sect_off)
11455 {
11456 /* Found that ORIGIN_CHILD_DIE is really not referenced.
11457 Check whether we're already processing ORIGIN_CHILD_DIE.
11458 This can happen with mutually referenced abstract_origins.
11459 PR 16581. */
11460 if (!origin_child_die->in_process)
11461 process_die (origin_child_die, origin_cu);
11462 }
11463 origin_child_die = sibling_die (origin_child_die);
11464 }
11465 origin_cu->list_in_scope = origin_previous_list_in_scope;
11466
11467 do_cleanups (cleanups);
11468 }
11469
11470 static void
11471 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
11472 {
11473 struct objfile *objfile = cu->objfile;
11474 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11475 struct context_stack *newobj;
11476 CORE_ADDR lowpc;
11477 CORE_ADDR highpc;
11478 struct die_info *child_die;
11479 struct attribute *attr, *call_line, *call_file;
11480 const char *name;
11481 CORE_ADDR baseaddr;
11482 struct block *block;
11483 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11484 VEC (symbolp) *template_args = NULL;
11485 struct template_symbol *templ_func = NULL;
11486
11487 if (inlined_func)
11488 {
11489 /* If we do not have call site information, we can't show the
11490 caller of this inlined function. That's too confusing, so
11491 only use the scope for local variables. */
11492 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
11493 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
11494 if (call_line == NULL || call_file == NULL)
11495 {
11496 read_lexical_block_scope (die, cu);
11497 return;
11498 }
11499 }
11500
11501 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11502
11503 name = dwarf2_name (die, cu);
11504
11505 /* Ignore functions with missing or empty names. These are actually
11506 illegal according to the DWARF standard. */
11507 if (name == NULL)
11508 {
11509 complaint (&symfile_complaints,
11510 _("missing name for subprogram DIE at %d"),
11511 die->offset.sect_off);
11512 return;
11513 }
11514
11515 /* Ignore functions with missing or invalid low and high pc attributes. */
11516 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
11517 <= PC_BOUNDS_INVALID)
11518 {
11519 attr = dwarf2_attr (die, DW_AT_external, cu);
11520 if (!attr || !DW_UNSND (attr))
11521 complaint (&symfile_complaints,
11522 _("cannot get low and high bounds "
11523 "for subprogram DIE at %d"),
11524 die->offset.sect_off);
11525 return;
11526 }
11527
11528 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11529 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11530
11531 /* If we have any template arguments, then we must allocate a
11532 different sort of symbol. */
11533 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
11534 {
11535 if (child_die->tag == DW_TAG_template_type_param
11536 || child_die->tag == DW_TAG_template_value_param)
11537 {
11538 templ_func = allocate_template_symbol (objfile);
11539 templ_func->base.is_cplus_template_function = 1;
11540 break;
11541 }
11542 }
11543
11544 newobj = push_context (0, lowpc);
11545 newobj->name = new_symbol_full (die, read_type_die (die, cu), cu,
11546 (struct symbol *) templ_func);
11547
11548 /* If there is a location expression for DW_AT_frame_base, record
11549 it. */
11550 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
11551 if (attr)
11552 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
11553
11554 /* If there is a location for the static link, record it. */
11555 newobj->static_link = NULL;
11556 attr = dwarf2_attr (die, DW_AT_static_link, cu);
11557 if (attr)
11558 {
11559 newobj->static_link
11560 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
11561 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
11562 }
11563
11564 cu->list_in_scope = &local_symbols;
11565
11566 if (die->child != NULL)
11567 {
11568 child_die = die->child;
11569 while (child_die && child_die->tag)
11570 {
11571 if (child_die->tag == DW_TAG_template_type_param
11572 || child_die->tag == DW_TAG_template_value_param)
11573 {
11574 struct symbol *arg = new_symbol (child_die, NULL, cu);
11575
11576 if (arg != NULL)
11577 VEC_safe_push (symbolp, template_args, arg);
11578 }
11579 else
11580 process_die (child_die, cu);
11581 child_die = sibling_die (child_die);
11582 }
11583 }
11584
11585 inherit_abstract_dies (die, cu);
11586
11587 /* If we have a DW_AT_specification, we might need to import using
11588 directives from the context of the specification DIE. See the
11589 comment in determine_prefix. */
11590 if (cu->language == language_cplus
11591 && dwarf2_attr (die, DW_AT_specification, cu))
11592 {
11593 struct dwarf2_cu *spec_cu = cu;
11594 struct die_info *spec_die = die_specification (die, &spec_cu);
11595
11596 while (spec_die)
11597 {
11598 child_die = spec_die->child;
11599 while (child_die && child_die->tag)
11600 {
11601 if (child_die->tag == DW_TAG_imported_module)
11602 process_die (child_die, spec_cu);
11603 child_die = sibling_die (child_die);
11604 }
11605
11606 /* In some cases, GCC generates specification DIEs that
11607 themselves contain DW_AT_specification attributes. */
11608 spec_die = die_specification (spec_die, &spec_cu);
11609 }
11610 }
11611
11612 newobj = pop_context ();
11613 /* Make a block for the local symbols within. */
11614 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
11615 newobj->static_link, lowpc, highpc);
11616
11617 /* For C++, set the block's scope. */
11618 if ((cu->language == language_cplus
11619 || cu->language == language_fortran
11620 || cu->language == language_d
11621 || cu->language == language_rust)
11622 && cu->processing_has_namespace_info)
11623 block_set_scope (block, determine_prefix (die, cu),
11624 &objfile->objfile_obstack);
11625
11626 /* If we have address ranges, record them. */
11627 dwarf2_record_block_ranges (die, block, baseaddr, cu);
11628
11629 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
11630
11631 /* Attach template arguments to function. */
11632 if (! VEC_empty (symbolp, template_args))
11633 {
11634 gdb_assert (templ_func != NULL);
11635
11636 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
11637 templ_func->template_arguments
11638 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
11639 templ_func->n_template_arguments);
11640 memcpy (templ_func->template_arguments,
11641 VEC_address (symbolp, template_args),
11642 (templ_func->n_template_arguments * sizeof (struct symbol *)));
11643 VEC_free (symbolp, template_args);
11644 }
11645
11646 /* In C++, we can have functions nested inside functions (e.g., when
11647 a function declares a class that has methods). This means that
11648 when we finish processing a function scope, we may need to go
11649 back to building a containing block's symbol lists. */
11650 local_symbols = newobj->locals;
11651 local_using_directives = newobj->local_using_directives;
11652
11653 /* If we've finished processing a top-level function, subsequent
11654 symbols go in the file symbol list. */
11655 if (outermost_context_p ())
11656 cu->list_in_scope = &file_symbols;
11657 }
11658
11659 /* Process all the DIES contained within a lexical block scope. Start
11660 a new scope, process the dies, and then close the scope. */
11661
11662 static void
11663 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
11664 {
11665 struct objfile *objfile = cu->objfile;
11666 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11667 struct context_stack *newobj;
11668 CORE_ADDR lowpc, highpc;
11669 struct die_info *child_die;
11670 CORE_ADDR baseaddr;
11671
11672 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11673
11674 /* Ignore blocks with missing or invalid low and high pc attributes. */
11675 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11676 as multiple lexical blocks? Handling children in a sane way would
11677 be nasty. Might be easier to properly extend generic blocks to
11678 describe ranges. */
11679 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11680 {
11681 case PC_BOUNDS_NOT_PRESENT:
11682 /* DW_TAG_lexical_block has no attributes, process its children as if
11683 there was no wrapping by that DW_TAG_lexical_block.
11684 GCC does no longer produces such DWARF since GCC r224161. */
11685 for (child_die = die->child;
11686 child_die != NULL && child_die->tag;
11687 child_die = sibling_die (child_die))
11688 process_die (child_die, cu);
11689 return;
11690 case PC_BOUNDS_INVALID:
11691 return;
11692 }
11693 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11694 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
11695
11696 push_context (0, lowpc);
11697 if (die->child != NULL)
11698 {
11699 child_die = die->child;
11700 while (child_die && child_die->tag)
11701 {
11702 process_die (child_die, cu);
11703 child_die = sibling_die (child_die);
11704 }
11705 }
11706 inherit_abstract_dies (die, cu);
11707 newobj = pop_context ();
11708
11709 if (local_symbols != NULL || local_using_directives != NULL)
11710 {
11711 struct block *block
11712 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
11713 newobj->start_addr, highpc);
11714
11715 /* Note that recording ranges after traversing children, as we
11716 do here, means that recording a parent's ranges entails
11717 walking across all its children's ranges as they appear in
11718 the address map, which is quadratic behavior.
11719
11720 It would be nicer to record the parent's ranges before
11721 traversing its children, simply overriding whatever you find
11722 there. But since we don't even decide whether to create a
11723 block until after we've traversed its children, that's hard
11724 to do. */
11725 dwarf2_record_block_ranges (die, block, baseaddr, cu);
11726 }
11727 local_symbols = newobj->locals;
11728 local_using_directives = newobj->local_using_directives;
11729 }
11730
11731 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
11732
11733 static void
11734 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11735 {
11736 struct objfile *objfile = cu->objfile;
11737 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11738 CORE_ADDR pc, baseaddr;
11739 struct attribute *attr;
11740 struct call_site *call_site, call_site_local;
11741 void **slot;
11742 int nparams;
11743 struct die_info *child_die;
11744
11745 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11746
11747 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
11748 if (attr == NULL)
11749 {
11750 /* This was a pre-DWARF-5 GNU extension alias
11751 for DW_AT_call_return_pc. */
11752 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11753 }
11754 if (!attr)
11755 {
11756 complaint (&symfile_complaints,
11757 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
11758 "DIE 0x%x [in module %s]"),
11759 die->offset.sect_off, objfile_name (objfile));
11760 return;
11761 }
11762 pc = attr_value_as_address (attr) + baseaddr;
11763 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
11764
11765 if (cu->call_site_htab == NULL)
11766 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11767 NULL, &objfile->objfile_obstack,
11768 hashtab_obstack_allocate, NULL);
11769 call_site_local.pc = pc;
11770 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11771 if (*slot != NULL)
11772 {
11773 complaint (&symfile_complaints,
11774 _("Duplicate PC %s for DW_TAG_call_site "
11775 "DIE 0x%x [in module %s]"),
11776 paddress (gdbarch, pc), die->offset.sect_off,
11777 objfile_name (objfile));
11778 return;
11779 }
11780
11781 /* Count parameters at the caller. */
11782
11783 nparams = 0;
11784 for (child_die = die->child; child_die && child_die->tag;
11785 child_die = sibling_die (child_die))
11786 {
11787 if (child_die->tag != DW_TAG_call_site_parameter
11788 && child_die->tag != DW_TAG_GNU_call_site_parameter)
11789 {
11790 complaint (&symfile_complaints,
11791 _("Tag %d is not DW_TAG_call_site_parameter in "
11792 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11793 child_die->tag, child_die->offset.sect_off,
11794 objfile_name (objfile));
11795 continue;
11796 }
11797
11798 nparams++;
11799 }
11800
11801 call_site
11802 = ((struct call_site *)
11803 obstack_alloc (&objfile->objfile_obstack,
11804 sizeof (*call_site)
11805 + (sizeof (*call_site->parameter) * (nparams - 1))));
11806 *slot = call_site;
11807 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
11808 call_site->pc = pc;
11809
11810 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
11811 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
11812 {
11813 struct die_info *func_die;
11814
11815 /* Skip also over DW_TAG_inlined_subroutine. */
11816 for (func_die = die->parent;
11817 func_die && func_die->tag != DW_TAG_subprogram
11818 && func_die->tag != DW_TAG_subroutine_type;
11819 func_die = func_die->parent);
11820
11821 /* DW_AT_call_all_calls is a superset
11822 of DW_AT_call_all_tail_calls. */
11823 if (func_die
11824 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
11825 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
11826 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
11827 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
11828 {
11829 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
11830 not complete. But keep CALL_SITE for look ups via call_site_htab,
11831 both the initial caller containing the real return address PC and
11832 the final callee containing the current PC of a chain of tail
11833 calls do not need to have the tail call list complete. But any
11834 function candidate for a virtual tail call frame searched via
11835 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
11836 determined unambiguously. */
11837 }
11838 else
11839 {
11840 struct type *func_type = NULL;
11841
11842 if (func_die)
11843 func_type = get_die_type (func_die, cu);
11844 if (func_type != NULL)
11845 {
11846 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
11847
11848 /* Enlist this call site to the function. */
11849 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
11850 TYPE_TAIL_CALL_LIST (func_type) = call_site;
11851 }
11852 else
11853 complaint (&symfile_complaints,
11854 _("Cannot find function owning DW_TAG_call_site "
11855 "DIE 0x%x [in module %s]"),
11856 die->offset.sect_off, objfile_name (objfile));
11857 }
11858 }
11859
11860 attr = dwarf2_attr (die, DW_AT_call_target, cu);
11861 if (attr == NULL)
11862 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
11863 if (attr == NULL)
11864 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
11865 if (attr == NULL)
11866 {
11867 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
11868 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11869 }
11870 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
11871 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
11872 /* Keep NULL DWARF_BLOCK. */;
11873 else if (attr_form_is_block (attr))
11874 {
11875 struct dwarf2_locexpr_baton *dlbaton;
11876
11877 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
11878 dlbaton->data = DW_BLOCK (attr)->data;
11879 dlbaton->size = DW_BLOCK (attr)->size;
11880 dlbaton->per_cu = cu->per_cu;
11881
11882 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
11883 }
11884 else if (attr_form_is_ref (attr))
11885 {
11886 struct dwarf2_cu *target_cu = cu;
11887 struct die_info *target_die;
11888
11889 target_die = follow_die_ref (die, attr, &target_cu);
11890 gdb_assert (target_cu->objfile == objfile);
11891 if (die_is_declaration (target_die, target_cu))
11892 {
11893 const char *target_physname;
11894
11895 /* Prefer the mangled name; otherwise compute the demangled one. */
11896 target_physname = dwarf2_string_attr (target_die,
11897 DW_AT_linkage_name,
11898 target_cu);
11899 if (target_physname == NULL)
11900 target_physname = dwarf2_string_attr (target_die,
11901 DW_AT_MIPS_linkage_name,
11902 target_cu);
11903 if (target_physname == NULL)
11904 target_physname = dwarf2_physname (NULL, target_die, target_cu);
11905 if (target_physname == NULL)
11906 complaint (&symfile_complaints,
11907 _("DW_AT_call_target target DIE has invalid "
11908 "physname, for referencing DIE 0x%x [in module %s]"),
11909 die->offset.sect_off, objfile_name (objfile));
11910 else
11911 SET_FIELD_PHYSNAME (call_site->target, target_physname);
11912 }
11913 else
11914 {
11915 CORE_ADDR lowpc;
11916
11917 /* DW_AT_entry_pc should be preferred. */
11918 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
11919 <= PC_BOUNDS_INVALID)
11920 complaint (&symfile_complaints,
11921 _("DW_AT_call_target target DIE has invalid "
11922 "low pc, for referencing DIE 0x%x [in module %s]"),
11923 die->offset.sect_off, objfile_name (objfile));
11924 else
11925 {
11926 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11927 SET_FIELD_PHYSADDR (call_site->target, lowpc);
11928 }
11929 }
11930 }
11931 else
11932 complaint (&symfile_complaints,
11933 _("DW_TAG_call_site DW_AT_call_target is neither "
11934 "block nor reference, for DIE 0x%x [in module %s]"),
11935 die->offset.sect_off, objfile_name (objfile));
11936
11937 call_site->per_cu = cu->per_cu;
11938
11939 for (child_die = die->child;
11940 child_die && child_die->tag;
11941 child_die = sibling_die (child_die))
11942 {
11943 struct call_site_parameter *parameter;
11944 struct attribute *loc, *origin;
11945
11946 if (child_die->tag != DW_TAG_call_site_parameter
11947 && child_die->tag != DW_TAG_GNU_call_site_parameter)
11948 {
11949 /* Already printed the complaint above. */
11950 continue;
11951 }
11952
11953 gdb_assert (call_site->parameter_count < nparams);
11954 parameter = &call_site->parameter[call_site->parameter_count];
11955
11956 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
11957 specifies DW_TAG_formal_parameter. Value of the data assumed for the
11958 register is contained in DW_AT_call_value. */
11959
11960 loc = dwarf2_attr (child_die, DW_AT_location, cu);
11961 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
11962 if (origin == NULL)
11963 {
11964 /* This was a pre-DWARF-5 GNU extension alias
11965 for DW_AT_call_parameter. */
11966 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
11967 }
11968 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
11969 {
11970 sect_offset offset;
11971
11972 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
11973 offset = dwarf2_get_ref_die_offset (origin);
11974 if (!offset_in_cu_p (&cu->header, offset))
11975 {
11976 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
11977 binding can be done only inside one CU. Such referenced DIE
11978 therefore cannot be even moved to DW_TAG_partial_unit. */
11979 complaint (&symfile_complaints,
11980 _("DW_AT_call_parameter offset is not in CU for "
11981 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11982 child_die->offset.sect_off, objfile_name (objfile));
11983 continue;
11984 }
11985 parameter->u.param_offset.cu_off = (offset.sect_off
11986 - cu->header.offset.sect_off);
11987 }
11988 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
11989 {
11990 complaint (&symfile_complaints,
11991 _("No DW_FORM_block* DW_AT_location for "
11992 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
11993 child_die->offset.sect_off, objfile_name (objfile));
11994 continue;
11995 }
11996 else
11997 {
11998 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
11999 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
12000 if (parameter->u.dwarf_reg != -1)
12001 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
12002 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
12003 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
12004 &parameter->u.fb_offset))
12005 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
12006 else
12007 {
12008 complaint (&symfile_complaints,
12009 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
12010 "for DW_FORM_block* DW_AT_location is supported for "
12011 "DW_TAG_call_site child DIE 0x%x "
12012 "[in module %s]"),
12013 child_die->offset.sect_off, objfile_name (objfile));
12014 continue;
12015 }
12016 }
12017
12018 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
12019 if (attr == NULL)
12020 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
12021 if (!attr_form_is_block (attr))
12022 {
12023 complaint (&symfile_complaints,
12024 _("No DW_FORM_block* DW_AT_call_value for "
12025 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12026 child_die->offset.sect_off, objfile_name (objfile));
12027 continue;
12028 }
12029 parameter->value = DW_BLOCK (attr)->data;
12030 parameter->value_size = DW_BLOCK (attr)->size;
12031
12032 /* Parameters are not pre-cleared by memset above. */
12033 parameter->data_value = NULL;
12034 parameter->data_value_size = 0;
12035 call_site->parameter_count++;
12036
12037 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
12038 if (attr == NULL)
12039 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
12040 if (attr)
12041 {
12042 if (!attr_form_is_block (attr))
12043 complaint (&symfile_complaints,
12044 _("No DW_FORM_block* DW_AT_call_data_value for "
12045 "DW_TAG_call_site child DIE 0x%x [in module %s]"),
12046 child_die->offset.sect_off, objfile_name (objfile));
12047 else
12048 {
12049 parameter->data_value = DW_BLOCK (attr)->data;
12050 parameter->data_value_size = DW_BLOCK (attr)->size;
12051 }
12052 }
12053 }
12054 }
12055
12056 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
12057 reading .debug_rnglists.
12058 Callback's type should be:
12059 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12060 Return true if the attributes are present and valid, otherwise,
12061 return false. */
12062
12063 template <typename Callback>
12064 static bool
12065 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
12066 Callback &&callback)
12067 {
12068 struct objfile *objfile = cu->objfile;
12069 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12070 struct comp_unit_head *cu_header = &cu->header;
12071 bfd *obfd = objfile->obfd;
12072 unsigned int addr_size = cu_header->addr_size;
12073 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12074 /* Base address selection entry. */
12075 CORE_ADDR base;
12076 int found_base;
12077 unsigned int dummy;
12078 const gdb_byte *buffer;
12079 CORE_ADDR low = 0;
12080 CORE_ADDR high = 0;
12081 CORE_ADDR baseaddr;
12082 bool overflow = false;
12083
12084 found_base = cu->base_known;
12085 base = cu->base_address;
12086
12087 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
12088 if (offset >= dwarf2_per_objfile->rnglists.size)
12089 {
12090 complaint (&symfile_complaints,
12091 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12092 offset);
12093 return false;
12094 }
12095 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
12096
12097 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12098
12099 while (1)
12100 {
12101 /* Initialize it due to a false compiler warning. */
12102 CORE_ADDR range_beginning = 0, range_end = 0;
12103 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
12104 + dwarf2_per_objfile->rnglists.size);
12105 unsigned int bytes_read;
12106
12107 if (buffer == buf_end)
12108 {
12109 overflow = true;
12110 break;
12111 }
12112 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
12113 switch (rlet)
12114 {
12115 case DW_RLE_end_of_list:
12116 break;
12117 case DW_RLE_base_address:
12118 if (buffer + cu->header.addr_size > buf_end)
12119 {
12120 overflow = true;
12121 break;
12122 }
12123 base = read_address (obfd, buffer, cu, &bytes_read);
12124 found_base = 1;
12125 buffer += bytes_read;
12126 break;
12127 case DW_RLE_start_length:
12128 if (buffer + cu->header.addr_size > buf_end)
12129 {
12130 overflow = true;
12131 break;
12132 }
12133 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12134 buffer += bytes_read;
12135 range_end = (range_beginning
12136 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
12137 buffer += bytes_read;
12138 if (buffer > buf_end)
12139 {
12140 overflow = true;
12141 break;
12142 }
12143 break;
12144 case DW_RLE_offset_pair:
12145 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12146 buffer += bytes_read;
12147 if (buffer > buf_end)
12148 {
12149 overflow = true;
12150 break;
12151 }
12152 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
12153 buffer += bytes_read;
12154 if (buffer > buf_end)
12155 {
12156 overflow = true;
12157 break;
12158 }
12159 break;
12160 case DW_RLE_start_end:
12161 if (buffer + 2 * cu->header.addr_size > buf_end)
12162 {
12163 overflow = true;
12164 break;
12165 }
12166 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
12167 buffer += bytes_read;
12168 range_end = read_address (obfd, buffer, cu, &bytes_read);
12169 buffer += bytes_read;
12170 break;
12171 default:
12172 complaint (&symfile_complaints,
12173 _("Invalid .debug_rnglists data (no base address)"));
12174 return false;
12175 }
12176 if (rlet == DW_RLE_end_of_list || overflow)
12177 break;
12178 if (rlet == DW_RLE_base_address)
12179 continue;
12180
12181 if (!found_base)
12182 {
12183 /* We have no valid base address for the ranges
12184 data. */
12185 complaint (&symfile_complaints,
12186 _("Invalid .debug_rnglists data (no base address)"));
12187 return false;
12188 }
12189
12190 if (range_beginning > range_end)
12191 {
12192 /* Inverted range entries are invalid. */
12193 complaint (&symfile_complaints,
12194 _("Invalid .debug_rnglists data (inverted range)"));
12195 return false;
12196 }
12197
12198 /* Empty range entries have no effect. */
12199 if (range_beginning == range_end)
12200 continue;
12201
12202 range_beginning += base;
12203 range_end += base;
12204
12205 /* A not-uncommon case of bad debug info.
12206 Don't pollute the addrmap with bad data. */
12207 if (range_beginning + baseaddr == 0
12208 && !dwarf2_per_objfile->has_section_at_zero)
12209 {
12210 complaint (&symfile_complaints,
12211 _(".debug_rnglists entry has start address of zero"
12212 " [in module %s]"), objfile_name (objfile));
12213 continue;
12214 }
12215
12216 callback (range_beginning, range_end);
12217 }
12218
12219 if (overflow)
12220 {
12221 complaint (&symfile_complaints,
12222 _("Offset %d is not terminated "
12223 "for DW_AT_ranges attribute"),
12224 offset);
12225 return false;
12226 }
12227
12228 return true;
12229 }
12230
12231 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
12232 Callback's type should be:
12233 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
12234 Return 1 if the attributes are present and valid, otherwise, return 0. */
12235
12236 template <typename Callback>
12237 static int
12238 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
12239 Callback &&callback)
12240 {
12241 struct objfile *objfile = cu->objfile;
12242 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12243 struct comp_unit_head *cu_header = &cu->header;
12244 bfd *obfd = objfile->obfd;
12245 unsigned int addr_size = cu_header->addr_size;
12246 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12247 /* Base address selection entry. */
12248 CORE_ADDR base;
12249 int found_base;
12250 unsigned int dummy;
12251 const gdb_byte *buffer;
12252 CORE_ADDR baseaddr;
12253
12254 if (cu_header->version >= 5)
12255 return dwarf2_rnglists_process (offset, cu, callback);
12256
12257 found_base = cu->base_known;
12258 base = cu->base_address;
12259
12260 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
12261 if (offset >= dwarf2_per_objfile->ranges.size)
12262 {
12263 complaint (&symfile_complaints,
12264 _("Offset %d out of bounds for DW_AT_ranges attribute"),
12265 offset);
12266 return 0;
12267 }
12268 buffer = dwarf2_per_objfile->ranges.buffer + offset;
12269
12270 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
12271
12272 while (1)
12273 {
12274 CORE_ADDR range_beginning, range_end;
12275
12276 range_beginning = read_address (obfd, buffer, cu, &dummy);
12277 buffer += addr_size;
12278 range_end = read_address (obfd, buffer, cu, &dummy);
12279 buffer += addr_size;
12280 offset += 2 * addr_size;
12281
12282 /* An end of list marker is a pair of zero addresses. */
12283 if (range_beginning == 0 && range_end == 0)
12284 /* Found the end of list entry. */
12285 break;
12286
12287 /* Each base address selection entry is a pair of 2 values.
12288 The first is the largest possible address, the second is
12289 the base address. Check for a base address here. */
12290 if ((range_beginning & mask) == mask)
12291 {
12292 /* If we found the largest possible address, then we already
12293 have the base address in range_end. */
12294 base = range_end;
12295 found_base = 1;
12296 continue;
12297 }
12298
12299 if (!found_base)
12300 {
12301 /* We have no valid base address for the ranges
12302 data. */
12303 complaint (&symfile_complaints,
12304 _("Invalid .debug_ranges data (no base address)"));
12305 return 0;
12306 }
12307
12308 if (range_beginning > range_end)
12309 {
12310 /* Inverted range entries are invalid. */
12311 complaint (&symfile_complaints,
12312 _("Invalid .debug_ranges data (inverted range)"));
12313 return 0;
12314 }
12315
12316 /* Empty range entries have no effect. */
12317 if (range_beginning == range_end)
12318 continue;
12319
12320 range_beginning += base;
12321 range_end += base;
12322
12323 /* A not-uncommon case of bad debug info.
12324 Don't pollute the addrmap with bad data. */
12325 if (range_beginning + baseaddr == 0
12326 && !dwarf2_per_objfile->has_section_at_zero)
12327 {
12328 complaint (&symfile_complaints,
12329 _(".debug_ranges entry has start address of zero"
12330 " [in module %s]"), objfile_name (objfile));
12331 continue;
12332 }
12333
12334 callback (range_beginning, range_end);
12335 }
12336
12337 return 1;
12338 }
12339
12340 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
12341 Return 1 if the attributes are present and valid, otherwise, return 0.
12342 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
12343
12344 static int
12345 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
12346 CORE_ADDR *high_return, struct dwarf2_cu *cu,
12347 struct partial_symtab *ranges_pst)
12348 {
12349 struct objfile *objfile = cu->objfile;
12350 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12351 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
12352 SECT_OFF_TEXT (objfile));
12353 int low_set = 0;
12354 CORE_ADDR low = 0;
12355 CORE_ADDR high = 0;
12356 int retval;
12357
12358 retval = dwarf2_ranges_process (offset, cu,
12359 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
12360 {
12361 if (ranges_pst != NULL)
12362 {
12363 CORE_ADDR lowpc;
12364 CORE_ADDR highpc;
12365
12366 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12367 range_beginning + baseaddr);
12368 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
12369 range_end + baseaddr);
12370 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
12371 ranges_pst);
12372 }
12373
12374 /* FIXME: This is recording everything as a low-high
12375 segment of consecutive addresses. We should have a
12376 data structure for discontiguous block ranges
12377 instead. */
12378 if (! low_set)
12379 {
12380 low = range_beginning;
12381 high = range_end;
12382 low_set = 1;
12383 }
12384 else
12385 {
12386 if (range_beginning < low)
12387 low = range_beginning;
12388 if (range_end > high)
12389 high = range_end;
12390 }
12391 });
12392 if (!retval)
12393 return 0;
12394
12395 if (! low_set)
12396 /* If the first entry is an end-of-list marker, the range
12397 describes an empty scope, i.e. no instructions. */
12398 return 0;
12399
12400 if (low_return)
12401 *low_return = low;
12402 if (high_return)
12403 *high_return = high;
12404 return 1;
12405 }
12406
12407 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
12408 definition for the return value. *LOWPC and *HIGHPC are set iff
12409 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
12410
12411 static enum pc_bounds_kind
12412 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
12413 CORE_ADDR *highpc, struct dwarf2_cu *cu,
12414 struct partial_symtab *pst)
12415 {
12416 struct attribute *attr;
12417 struct attribute *attr_high;
12418 CORE_ADDR low = 0;
12419 CORE_ADDR high = 0;
12420 enum pc_bounds_kind ret;
12421
12422 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12423 if (attr_high)
12424 {
12425 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12426 if (attr)
12427 {
12428 low = attr_value_as_address (attr);
12429 high = attr_value_as_address (attr_high);
12430 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12431 high += low;
12432 }
12433 else
12434 /* Found high w/o low attribute. */
12435 return PC_BOUNDS_INVALID;
12436
12437 /* Found consecutive range of addresses. */
12438 ret = PC_BOUNDS_HIGH_LOW;
12439 }
12440 else
12441 {
12442 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12443 if (attr != NULL)
12444 {
12445 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12446 We take advantage of the fact that DW_AT_ranges does not appear
12447 in DW_TAG_compile_unit of DWO files. */
12448 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12449 unsigned int ranges_offset = (DW_UNSND (attr)
12450 + (need_ranges_base
12451 ? cu->ranges_base
12452 : 0));
12453
12454 /* Value of the DW_AT_ranges attribute is the offset in the
12455 .debug_ranges section. */
12456 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
12457 return PC_BOUNDS_INVALID;
12458 /* Found discontinuous range of addresses. */
12459 ret = PC_BOUNDS_RANGES;
12460 }
12461 else
12462 return PC_BOUNDS_NOT_PRESENT;
12463 }
12464
12465 /* read_partial_die has also the strict LOW < HIGH requirement. */
12466 if (high <= low)
12467 return PC_BOUNDS_INVALID;
12468
12469 /* When using the GNU linker, .gnu.linkonce. sections are used to
12470 eliminate duplicate copies of functions and vtables and such.
12471 The linker will arbitrarily choose one and discard the others.
12472 The AT_*_pc values for such functions refer to local labels in
12473 these sections. If the section from that file was discarded, the
12474 labels are not in the output, so the relocs get a value of 0.
12475 If this is a discarded function, mark the pc bounds as invalid,
12476 so that GDB will ignore it. */
12477 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
12478 return PC_BOUNDS_INVALID;
12479
12480 *lowpc = low;
12481 if (highpc)
12482 *highpc = high;
12483 return ret;
12484 }
12485
12486 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
12487 its low and high PC addresses. Do nothing if these addresses could not
12488 be determined. Otherwise, set LOWPC to the low address if it is smaller,
12489 and HIGHPC to the high address if greater than HIGHPC. */
12490
12491 static void
12492 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
12493 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12494 struct dwarf2_cu *cu)
12495 {
12496 CORE_ADDR low, high;
12497 struct die_info *child = die->child;
12498
12499 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
12500 {
12501 *lowpc = std::min (*lowpc, low);
12502 *highpc = std::max (*highpc, high);
12503 }
12504
12505 /* If the language does not allow nested subprograms (either inside
12506 subprograms or lexical blocks), we're done. */
12507 if (cu->language != language_ada)
12508 return;
12509
12510 /* Check all the children of the given DIE. If it contains nested
12511 subprograms, then check their pc bounds. Likewise, we need to
12512 check lexical blocks as well, as they may also contain subprogram
12513 definitions. */
12514 while (child && child->tag)
12515 {
12516 if (child->tag == DW_TAG_subprogram
12517 || child->tag == DW_TAG_lexical_block)
12518 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
12519 child = sibling_die (child);
12520 }
12521 }
12522
12523 /* Get the low and high pc's represented by the scope DIE, and store
12524 them in *LOWPC and *HIGHPC. If the correct values can't be
12525 determined, set *LOWPC to -1 and *HIGHPC to 0. */
12526
12527 static void
12528 get_scope_pc_bounds (struct die_info *die,
12529 CORE_ADDR *lowpc, CORE_ADDR *highpc,
12530 struct dwarf2_cu *cu)
12531 {
12532 CORE_ADDR best_low = (CORE_ADDR) -1;
12533 CORE_ADDR best_high = (CORE_ADDR) 0;
12534 CORE_ADDR current_low, current_high;
12535
12536 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
12537 >= PC_BOUNDS_RANGES)
12538 {
12539 best_low = current_low;
12540 best_high = current_high;
12541 }
12542 else
12543 {
12544 struct die_info *child = die->child;
12545
12546 while (child && child->tag)
12547 {
12548 switch (child->tag) {
12549 case DW_TAG_subprogram:
12550 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
12551 break;
12552 case DW_TAG_namespace:
12553 case DW_TAG_module:
12554 /* FIXME: carlton/2004-01-16: Should we do this for
12555 DW_TAG_class_type/DW_TAG_structure_type, too? I think
12556 that current GCC's always emit the DIEs corresponding
12557 to definitions of methods of classes as children of a
12558 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
12559 the DIEs giving the declarations, which could be
12560 anywhere). But I don't see any reason why the
12561 standards says that they have to be there. */
12562 get_scope_pc_bounds (child, &current_low, &current_high, cu);
12563
12564 if (current_low != ((CORE_ADDR) -1))
12565 {
12566 best_low = std::min (best_low, current_low);
12567 best_high = std::max (best_high, current_high);
12568 }
12569 break;
12570 default:
12571 /* Ignore. */
12572 break;
12573 }
12574
12575 child = sibling_die (child);
12576 }
12577 }
12578
12579 *lowpc = best_low;
12580 *highpc = best_high;
12581 }
12582
12583 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
12584 in DIE. */
12585
12586 static void
12587 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
12588 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
12589 {
12590 struct objfile *objfile = cu->objfile;
12591 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12592 struct attribute *attr;
12593 struct attribute *attr_high;
12594
12595 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
12596 if (attr_high)
12597 {
12598 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
12599 if (attr)
12600 {
12601 CORE_ADDR low = attr_value_as_address (attr);
12602 CORE_ADDR high = attr_value_as_address (attr_high);
12603
12604 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
12605 high += low;
12606
12607 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
12608 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
12609 record_block_range (block, low, high - 1);
12610 }
12611 }
12612
12613 attr = dwarf2_attr (die, DW_AT_ranges, cu);
12614 if (attr)
12615 {
12616 bfd *obfd = objfile->obfd;
12617 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
12618 We take advantage of the fact that DW_AT_ranges does not appear
12619 in DW_TAG_compile_unit of DWO files. */
12620 int need_ranges_base = die->tag != DW_TAG_compile_unit;
12621
12622 /* The value of the DW_AT_ranges attribute is the offset of the
12623 address range list in the .debug_ranges section. */
12624 unsigned long offset = (DW_UNSND (attr)
12625 + (need_ranges_base ? cu->ranges_base : 0));
12626 const gdb_byte *buffer;
12627
12628 /* For some target architectures, but not others, the
12629 read_address function sign-extends the addresses it returns.
12630 To recognize base address selection entries, we need a
12631 mask. */
12632 unsigned int addr_size = cu->header.addr_size;
12633 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
12634
12635 /* The base address, to which the next pair is relative. Note
12636 that this 'base' is a DWARF concept: most entries in a range
12637 list are relative, to reduce the number of relocs against the
12638 debugging information. This is separate from this function's
12639 'baseaddr' argument, which GDB uses to relocate debugging
12640 information from a shared library based on the address at
12641 which the library was loaded. */
12642 CORE_ADDR base = cu->base_address;
12643 int base_known = cu->base_known;
12644
12645 dwarf2_ranges_process (offset, cu,
12646 [&] (CORE_ADDR start, CORE_ADDR end)
12647 {
12648 start += baseaddr;
12649 end += baseaddr;
12650 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
12651 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
12652 record_block_range (block, start, end - 1);
12653 });
12654 }
12655 }
12656
12657 /* Check whether the producer field indicates either of GCC < 4.6, or the
12658 Intel C/C++ compiler, and cache the result in CU. */
12659
12660 static void
12661 check_producer (struct dwarf2_cu *cu)
12662 {
12663 int major, minor;
12664
12665 if (cu->producer == NULL)
12666 {
12667 /* For unknown compilers expect their behavior is DWARF version
12668 compliant.
12669
12670 GCC started to support .debug_types sections by -gdwarf-4 since
12671 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
12672 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
12673 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
12674 interpreted incorrectly by GDB now - GCC PR debug/48229. */
12675 }
12676 else if (producer_is_gcc (cu->producer, &major, &minor))
12677 {
12678 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
12679 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
12680 }
12681 else if (startswith (cu->producer, "Intel(R) C"))
12682 cu->producer_is_icc = 1;
12683 else
12684 {
12685 /* For other non-GCC compilers, expect their behavior is DWARF version
12686 compliant. */
12687 }
12688
12689 cu->checked_producer = 1;
12690 }
12691
12692 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
12693 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
12694 during 4.6.0 experimental. */
12695
12696 static int
12697 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
12698 {
12699 if (!cu->checked_producer)
12700 check_producer (cu);
12701
12702 return cu->producer_is_gxx_lt_4_6;
12703 }
12704
12705 /* Return the default accessibility type if it is not overriden by
12706 DW_AT_accessibility. */
12707
12708 static enum dwarf_access_attribute
12709 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
12710 {
12711 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
12712 {
12713 /* The default DWARF 2 accessibility for members is public, the default
12714 accessibility for inheritance is private. */
12715
12716 if (die->tag != DW_TAG_inheritance)
12717 return DW_ACCESS_public;
12718 else
12719 return DW_ACCESS_private;
12720 }
12721 else
12722 {
12723 /* DWARF 3+ defines the default accessibility a different way. The same
12724 rules apply now for DW_TAG_inheritance as for the members and it only
12725 depends on the container kind. */
12726
12727 if (die->parent->tag == DW_TAG_class_type)
12728 return DW_ACCESS_private;
12729 else
12730 return DW_ACCESS_public;
12731 }
12732 }
12733
12734 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
12735 offset. If the attribute was not found return 0, otherwise return
12736 1. If it was found but could not properly be handled, set *OFFSET
12737 to 0. */
12738
12739 static int
12740 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
12741 LONGEST *offset)
12742 {
12743 struct attribute *attr;
12744
12745 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
12746 if (attr != NULL)
12747 {
12748 *offset = 0;
12749
12750 /* Note that we do not check for a section offset first here.
12751 This is because DW_AT_data_member_location is new in DWARF 4,
12752 so if we see it, we can assume that a constant form is really
12753 a constant and not a section offset. */
12754 if (attr_form_is_constant (attr))
12755 *offset = dwarf2_get_attr_constant_value (attr, 0);
12756 else if (attr_form_is_section_offset (attr))
12757 dwarf2_complex_location_expr_complaint ();
12758 else if (attr_form_is_block (attr))
12759 *offset = decode_locdesc (DW_BLOCK (attr), cu);
12760 else
12761 dwarf2_complex_location_expr_complaint ();
12762
12763 return 1;
12764 }
12765
12766 return 0;
12767 }
12768
12769 /* Add an aggregate field to the field list. */
12770
12771 static void
12772 dwarf2_add_field (struct field_info *fip, struct die_info *die,
12773 struct dwarf2_cu *cu)
12774 {
12775 struct objfile *objfile = cu->objfile;
12776 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12777 struct nextfield *new_field;
12778 struct attribute *attr;
12779 struct field *fp;
12780 const char *fieldname = "";
12781
12782 /* Allocate a new field list entry and link it in. */
12783 new_field = XNEW (struct nextfield);
12784 make_cleanup (xfree, new_field);
12785 memset (new_field, 0, sizeof (struct nextfield));
12786
12787 if (die->tag == DW_TAG_inheritance)
12788 {
12789 new_field->next = fip->baseclasses;
12790 fip->baseclasses = new_field;
12791 }
12792 else
12793 {
12794 new_field->next = fip->fields;
12795 fip->fields = new_field;
12796 }
12797 fip->nfields++;
12798
12799 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
12800 if (attr)
12801 new_field->accessibility = DW_UNSND (attr);
12802 else
12803 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
12804 if (new_field->accessibility != DW_ACCESS_public)
12805 fip->non_public_fields = 1;
12806
12807 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
12808 if (attr)
12809 new_field->virtuality = DW_UNSND (attr);
12810 else
12811 new_field->virtuality = DW_VIRTUALITY_none;
12812
12813 fp = &new_field->field;
12814
12815 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
12816 {
12817 LONGEST offset;
12818
12819 /* Data member other than a C++ static data member. */
12820
12821 /* Get type of field. */
12822 fp->type = die_type (die, cu);
12823
12824 SET_FIELD_BITPOS (*fp, 0);
12825
12826 /* Get bit size of field (zero if none). */
12827 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
12828 if (attr)
12829 {
12830 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
12831 }
12832 else
12833 {
12834 FIELD_BITSIZE (*fp) = 0;
12835 }
12836
12837 /* Get bit offset of field. */
12838 if (handle_data_member_location (die, cu, &offset))
12839 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
12840 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
12841 if (attr)
12842 {
12843 if (gdbarch_bits_big_endian (gdbarch))
12844 {
12845 /* For big endian bits, the DW_AT_bit_offset gives the
12846 additional bit offset from the MSB of the containing
12847 anonymous object to the MSB of the field. We don't
12848 have to do anything special since we don't need to
12849 know the size of the anonymous object. */
12850 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
12851 }
12852 else
12853 {
12854 /* For little endian bits, compute the bit offset to the
12855 MSB of the anonymous object, subtract off the number of
12856 bits from the MSB of the field to the MSB of the
12857 object, and then subtract off the number of bits of
12858 the field itself. The result is the bit offset of
12859 the LSB of the field. */
12860 int anonymous_size;
12861 int bit_offset = DW_UNSND (attr);
12862
12863 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12864 if (attr)
12865 {
12866 /* The size of the anonymous object containing
12867 the bit field is explicit, so use the
12868 indicated size (in bytes). */
12869 anonymous_size = DW_UNSND (attr);
12870 }
12871 else
12872 {
12873 /* The size of the anonymous object containing
12874 the bit field must be inferred from the type
12875 attribute of the data member containing the
12876 bit field. */
12877 anonymous_size = TYPE_LENGTH (fp->type);
12878 }
12879 SET_FIELD_BITPOS (*fp,
12880 (FIELD_BITPOS (*fp)
12881 + anonymous_size * bits_per_byte
12882 - bit_offset - FIELD_BITSIZE (*fp)));
12883 }
12884 }
12885 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
12886 if (attr != NULL)
12887 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
12888 + dwarf2_get_attr_constant_value (attr, 0)));
12889
12890 /* Get name of field. */
12891 fieldname = dwarf2_name (die, cu);
12892 if (fieldname == NULL)
12893 fieldname = "";
12894
12895 /* The name is already allocated along with this objfile, so we don't
12896 need to duplicate it for the type. */
12897 fp->name = fieldname;
12898
12899 /* Change accessibility for artificial fields (e.g. virtual table
12900 pointer or virtual base class pointer) to private. */
12901 if (dwarf2_attr (die, DW_AT_artificial, cu))
12902 {
12903 FIELD_ARTIFICIAL (*fp) = 1;
12904 new_field->accessibility = DW_ACCESS_private;
12905 fip->non_public_fields = 1;
12906 }
12907 }
12908 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
12909 {
12910 /* C++ static member. */
12911
12912 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
12913 is a declaration, but all versions of G++ as of this writing
12914 (so through at least 3.2.1) incorrectly generate
12915 DW_TAG_variable tags. */
12916
12917 const char *physname;
12918
12919 /* Get name of field. */
12920 fieldname = dwarf2_name (die, cu);
12921 if (fieldname == NULL)
12922 return;
12923
12924 attr = dwarf2_attr (die, DW_AT_const_value, cu);
12925 if (attr
12926 /* Only create a symbol if this is an external value.
12927 new_symbol checks this and puts the value in the global symbol
12928 table, which we want. If it is not external, new_symbol
12929 will try to put the value in cu->list_in_scope which is wrong. */
12930 && dwarf2_flag_true_p (die, DW_AT_external, cu))
12931 {
12932 /* A static const member, not much different than an enum as far as
12933 we're concerned, except that we can support more types. */
12934 new_symbol (die, NULL, cu);
12935 }
12936
12937 /* Get physical name. */
12938 physname = dwarf2_physname (fieldname, die, cu);
12939
12940 /* The name is already allocated along with this objfile, so we don't
12941 need to duplicate it for the type. */
12942 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
12943 FIELD_TYPE (*fp) = die_type (die, cu);
12944 FIELD_NAME (*fp) = fieldname;
12945 }
12946 else if (die->tag == DW_TAG_inheritance)
12947 {
12948 LONGEST offset;
12949
12950 /* C++ base class field. */
12951 if (handle_data_member_location (die, cu, &offset))
12952 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
12953 FIELD_BITSIZE (*fp) = 0;
12954 FIELD_TYPE (*fp) = die_type (die, cu);
12955 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
12956 fip->nbaseclasses++;
12957 }
12958 }
12959
12960 /* Add a typedef defined in the scope of the FIP's class. */
12961
12962 static void
12963 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
12964 struct dwarf2_cu *cu)
12965 {
12966 struct typedef_field_list *new_field;
12967 struct typedef_field *fp;
12968
12969 /* Allocate a new field list entry and link it in. */
12970 new_field = XCNEW (struct typedef_field_list);
12971 make_cleanup (xfree, new_field);
12972
12973 gdb_assert (die->tag == DW_TAG_typedef);
12974
12975 fp = &new_field->field;
12976
12977 /* Get name of field. */
12978 fp->name = dwarf2_name (die, cu);
12979 if (fp->name == NULL)
12980 return;
12981
12982 fp->type = read_type_die (die, cu);
12983
12984 new_field->next = fip->typedef_field_list;
12985 fip->typedef_field_list = new_field;
12986 fip->typedef_field_list_count++;
12987 }
12988
12989 /* Create the vector of fields, and attach it to the type. */
12990
12991 static void
12992 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
12993 struct dwarf2_cu *cu)
12994 {
12995 int nfields = fip->nfields;
12996
12997 /* Record the field count, allocate space for the array of fields,
12998 and create blank accessibility bitfields if necessary. */
12999 TYPE_NFIELDS (type) = nfields;
13000 TYPE_FIELDS (type) = (struct field *)
13001 TYPE_ALLOC (type, sizeof (struct field) * nfields);
13002 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
13003
13004 if (fip->non_public_fields && cu->language != language_ada)
13005 {
13006 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13007
13008 TYPE_FIELD_PRIVATE_BITS (type) =
13009 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13010 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
13011
13012 TYPE_FIELD_PROTECTED_BITS (type) =
13013 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13014 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
13015
13016 TYPE_FIELD_IGNORE_BITS (type) =
13017 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
13018 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
13019 }
13020
13021 /* If the type has baseclasses, allocate and clear a bit vector for
13022 TYPE_FIELD_VIRTUAL_BITS. */
13023 if (fip->nbaseclasses && cu->language != language_ada)
13024 {
13025 int num_bytes = B_BYTES (fip->nbaseclasses);
13026 unsigned char *pointer;
13027
13028 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13029 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
13030 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
13031 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
13032 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
13033 }
13034
13035 /* Copy the saved-up fields into the field vector. Start from the head of
13036 the list, adding to the tail of the field array, so that they end up in
13037 the same order in the array in which they were added to the list. */
13038 while (nfields-- > 0)
13039 {
13040 struct nextfield *fieldp;
13041
13042 if (fip->fields)
13043 {
13044 fieldp = fip->fields;
13045 fip->fields = fieldp->next;
13046 }
13047 else
13048 {
13049 fieldp = fip->baseclasses;
13050 fip->baseclasses = fieldp->next;
13051 }
13052
13053 TYPE_FIELD (type, nfields) = fieldp->field;
13054 switch (fieldp->accessibility)
13055 {
13056 case DW_ACCESS_private:
13057 if (cu->language != language_ada)
13058 SET_TYPE_FIELD_PRIVATE (type, nfields);
13059 break;
13060
13061 case DW_ACCESS_protected:
13062 if (cu->language != language_ada)
13063 SET_TYPE_FIELD_PROTECTED (type, nfields);
13064 break;
13065
13066 case DW_ACCESS_public:
13067 break;
13068
13069 default:
13070 /* Unknown accessibility. Complain and treat it as public. */
13071 {
13072 complaint (&symfile_complaints, _("unsupported accessibility %d"),
13073 fieldp->accessibility);
13074 }
13075 break;
13076 }
13077 if (nfields < fip->nbaseclasses)
13078 {
13079 switch (fieldp->virtuality)
13080 {
13081 case DW_VIRTUALITY_virtual:
13082 case DW_VIRTUALITY_pure_virtual:
13083 if (cu->language == language_ada)
13084 error (_("unexpected virtuality in component of Ada type"));
13085 SET_TYPE_FIELD_VIRTUAL (type, nfields);
13086 break;
13087 }
13088 }
13089 }
13090 }
13091
13092 /* Return true if this member function is a constructor, false
13093 otherwise. */
13094
13095 static int
13096 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
13097 {
13098 const char *fieldname;
13099 const char *type_name;
13100 int len;
13101
13102 if (die->parent == NULL)
13103 return 0;
13104
13105 if (die->parent->tag != DW_TAG_structure_type
13106 && die->parent->tag != DW_TAG_union_type
13107 && die->parent->tag != DW_TAG_class_type)
13108 return 0;
13109
13110 fieldname = dwarf2_name (die, cu);
13111 type_name = dwarf2_name (die->parent, cu);
13112 if (fieldname == NULL || type_name == NULL)
13113 return 0;
13114
13115 len = strlen (fieldname);
13116 return (strncmp (fieldname, type_name, len) == 0
13117 && (type_name[len] == '\0' || type_name[len] == '<'));
13118 }
13119
13120 /* Add a member function to the proper fieldlist. */
13121
13122 static void
13123 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
13124 struct type *type, struct dwarf2_cu *cu)
13125 {
13126 struct objfile *objfile = cu->objfile;
13127 struct attribute *attr;
13128 struct fnfieldlist *flp;
13129 int i;
13130 struct fn_field *fnp;
13131 const char *fieldname;
13132 struct nextfnfield *new_fnfield;
13133 struct type *this_type;
13134 enum dwarf_access_attribute accessibility;
13135
13136 if (cu->language == language_ada)
13137 error (_("unexpected member function in Ada type"));
13138
13139 /* Get name of member function. */
13140 fieldname = dwarf2_name (die, cu);
13141 if (fieldname == NULL)
13142 return;
13143
13144 /* Look up member function name in fieldlist. */
13145 for (i = 0; i < fip->nfnfields; i++)
13146 {
13147 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
13148 break;
13149 }
13150
13151 /* Create new list element if necessary. */
13152 if (i < fip->nfnfields)
13153 flp = &fip->fnfieldlists[i];
13154 else
13155 {
13156 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
13157 {
13158 fip->fnfieldlists = (struct fnfieldlist *)
13159 xrealloc (fip->fnfieldlists,
13160 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
13161 * sizeof (struct fnfieldlist));
13162 if (fip->nfnfields == 0)
13163 make_cleanup (free_current_contents, &fip->fnfieldlists);
13164 }
13165 flp = &fip->fnfieldlists[fip->nfnfields];
13166 flp->name = fieldname;
13167 flp->length = 0;
13168 flp->head = NULL;
13169 i = fip->nfnfields++;
13170 }
13171
13172 /* Create a new member function field and chain it to the field list
13173 entry. */
13174 new_fnfield = XNEW (struct nextfnfield);
13175 make_cleanup (xfree, new_fnfield);
13176 memset (new_fnfield, 0, sizeof (struct nextfnfield));
13177 new_fnfield->next = flp->head;
13178 flp->head = new_fnfield;
13179 flp->length++;
13180
13181 /* Fill in the member function field info. */
13182 fnp = &new_fnfield->fnfield;
13183
13184 /* Delay processing of the physname until later. */
13185 if (cu->language == language_cplus)
13186 {
13187 add_to_method_list (type, i, flp->length - 1, fieldname,
13188 die, cu);
13189 }
13190 else
13191 {
13192 const char *physname = dwarf2_physname (fieldname, die, cu);
13193 fnp->physname = physname ? physname : "";
13194 }
13195
13196 fnp->type = alloc_type (objfile);
13197 this_type = read_type_die (die, cu);
13198 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
13199 {
13200 int nparams = TYPE_NFIELDS (this_type);
13201
13202 /* TYPE is the domain of this method, and THIS_TYPE is the type
13203 of the method itself (TYPE_CODE_METHOD). */
13204 smash_to_method_type (fnp->type, type,
13205 TYPE_TARGET_TYPE (this_type),
13206 TYPE_FIELDS (this_type),
13207 TYPE_NFIELDS (this_type),
13208 TYPE_VARARGS (this_type));
13209
13210 /* Handle static member functions.
13211 Dwarf2 has no clean way to discern C++ static and non-static
13212 member functions. G++ helps GDB by marking the first
13213 parameter for non-static member functions (which is the this
13214 pointer) as artificial. We obtain this information from
13215 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
13216 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
13217 fnp->voffset = VOFFSET_STATIC;
13218 }
13219 else
13220 complaint (&symfile_complaints, _("member function type missing for '%s'"),
13221 dwarf2_full_name (fieldname, die, cu));
13222
13223 /* Get fcontext from DW_AT_containing_type if present. */
13224 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
13225 fnp->fcontext = die_containing_type (die, cu);
13226
13227 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
13228 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
13229
13230 /* Get accessibility. */
13231 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
13232 if (attr)
13233 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
13234 else
13235 accessibility = dwarf2_default_access_attribute (die, cu);
13236 switch (accessibility)
13237 {
13238 case DW_ACCESS_private:
13239 fnp->is_private = 1;
13240 break;
13241 case DW_ACCESS_protected:
13242 fnp->is_protected = 1;
13243 break;
13244 }
13245
13246 /* Check for artificial methods. */
13247 attr = dwarf2_attr (die, DW_AT_artificial, cu);
13248 if (attr && DW_UNSND (attr) != 0)
13249 fnp->is_artificial = 1;
13250
13251 fnp->is_constructor = dwarf2_is_constructor (die, cu);
13252
13253 /* Get index in virtual function table if it is a virtual member
13254 function. For older versions of GCC, this is an offset in the
13255 appropriate virtual table, as specified by DW_AT_containing_type.
13256 For everyone else, it is an expression to be evaluated relative
13257 to the object address. */
13258
13259 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
13260 if (attr)
13261 {
13262 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
13263 {
13264 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
13265 {
13266 /* Old-style GCC. */
13267 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
13268 }
13269 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
13270 || (DW_BLOCK (attr)->size > 1
13271 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
13272 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
13273 {
13274 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
13275 if ((fnp->voffset % cu->header.addr_size) != 0)
13276 dwarf2_complex_location_expr_complaint ();
13277 else
13278 fnp->voffset /= cu->header.addr_size;
13279 fnp->voffset += 2;
13280 }
13281 else
13282 dwarf2_complex_location_expr_complaint ();
13283
13284 if (!fnp->fcontext)
13285 {
13286 /* If there is no `this' field and no DW_AT_containing_type,
13287 we cannot actually find a base class context for the
13288 vtable! */
13289 if (TYPE_NFIELDS (this_type) == 0
13290 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
13291 {
13292 complaint (&symfile_complaints,
13293 _("cannot determine context for virtual member "
13294 "function \"%s\" (offset %d)"),
13295 fieldname, die->offset.sect_off);
13296 }
13297 else
13298 {
13299 fnp->fcontext
13300 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
13301 }
13302 }
13303 }
13304 else if (attr_form_is_section_offset (attr))
13305 {
13306 dwarf2_complex_location_expr_complaint ();
13307 }
13308 else
13309 {
13310 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
13311 fieldname);
13312 }
13313 }
13314 else
13315 {
13316 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
13317 if (attr && DW_UNSND (attr))
13318 {
13319 /* GCC does this, as of 2008-08-25; PR debug/37237. */
13320 complaint (&symfile_complaints,
13321 _("Member function \"%s\" (offset %d) is virtual "
13322 "but the vtable offset is not specified"),
13323 fieldname, die->offset.sect_off);
13324 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13325 TYPE_CPLUS_DYNAMIC (type) = 1;
13326 }
13327 }
13328 }
13329
13330 /* Create the vector of member function fields, and attach it to the type. */
13331
13332 static void
13333 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
13334 struct dwarf2_cu *cu)
13335 {
13336 struct fnfieldlist *flp;
13337 int i;
13338
13339 if (cu->language == language_ada)
13340 error (_("unexpected member functions in Ada type"));
13341
13342 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13343 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
13344 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
13345
13346 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
13347 {
13348 struct nextfnfield *nfp = flp->head;
13349 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
13350 int k;
13351
13352 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
13353 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
13354 fn_flp->fn_fields = (struct fn_field *)
13355 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
13356 for (k = flp->length; (k--, nfp); nfp = nfp->next)
13357 fn_flp->fn_fields[k] = nfp->fnfield;
13358 }
13359
13360 TYPE_NFN_FIELDS (type) = fip->nfnfields;
13361 }
13362
13363 /* Returns non-zero if NAME is the name of a vtable member in CU's
13364 language, zero otherwise. */
13365 static int
13366 is_vtable_name (const char *name, struct dwarf2_cu *cu)
13367 {
13368 static const char vptr[] = "_vptr";
13369 static const char vtable[] = "vtable";
13370
13371 /* Look for the C++ form of the vtable. */
13372 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
13373 return 1;
13374
13375 return 0;
13376 }
13377
13378 /* GCC outputs unnamed structures that are really pointers to member
13379 functions, with the ABI-specified layout. If TYPE describes
13380 such a structure, smash it into a member function type.
13381
13382 GCC shouldn't do this; it should just output pointer to member DIEs.
13383 This is GCC PR debug/28767. */
13384
13385 static void
13386 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
13387 {
13388 struct type *pfn_type, *self_type, *new_type;
13389
13390 /* Check for a structure with no name and two children. */
13391 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
13392 return;
13393
13394 /* Check for __pfn and __delta members. */
13395 if (TYPE_FIELD_NAME (type, 0) == NULL
13396 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
13397 || TYPE_FIELD_NAME (type, 1) == NULL
13398 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
13399 return;
13400
13401 /* Find the type of the method. */
13402 pfn_type = TYPE_FIELD_TYPE (type, 0);
13403 if (pfn_type == NULL
13404 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
13405 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
13406 return;
13407
13408 /* Look for the "this" argument. */
13409 pfn_type = TYPE_TARGET_TYPE (pfn_type);
13410 if (TYPE_NFIELDS (pfn_type) == 0
13411 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
13412 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
13413 return;
13414
13415 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
13416 new_type = alloc_type (objfile);
13417 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
13418 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
13419 TYPE_VARARGS (pfn_type));
13420 smash_to_methodptr_type (type, new_type);
13421 }
13422
13423 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
13424 (icc). */
13425
13426 static int
13427 producer_is_icc (struct dwarf2_cu *cu)
13428 {
13429 if (!cu->checked_producer)
13430 check_producer (cu);
13431
13432 return cu->producer_is_icc;
13433 }
13434
13435 /* Called when we find the DIE that starts a structure or union scope
13436 (definition) to create a type for the structure or union. Fill in
13437 the type's name and general properties; the members will not be
13438 processed until process_structure_scope. A symbol table entry for
13439 the type will also not be done until process_structure_scope (assuming
13440 the type has a name).
13441
13442 NOTE: we need to call these functions regardless of whether or not the
13443 DIE has a DW_AT_name attribute, since it might be an anonymous
13444 structure or union. This gets the type entered into our set of
13445 user defined types. */
13446
13447 static struct type *
13448 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
13449 {
13450 struct objfile *objfile = cu->objfile;
13451 struct type *type;
13452 struct attribute *attr;
13453 const char *name;
13454
13455 /* If the definition of this type lives in .debug_types, read that type.
13456 Don't follow DW_AT_specification though, that will take us back up
13457 the chain and we want to go down. */
13458 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
13459 if (attr)
13460 {
13461 type = get_DW_AT_signature_type (die, attr, cu);
13462
13463 /* The type's CU may not be the same as CU.
13464 Ensure TYPE is recorded with CU in die_type_hash. */
13465 return set_die_type (die, type, cu);
13466 }
13467
13468 type = alloc_type (objfile);
13469 INIT_CPLUS_SPECIFIC (type);
13470
13471 name = dwarf2_name (die, cu);
13472 if (name != NULL)
13473 {
13474 if (cu->language == language_cplus
13475 || cu->language == language_d
13476 || cu->language == language_rust)
13477 {
13478 const char *full_name = dwarf2_full_name (name, die, cu);
13479
13480 /* dwarf2_full_name might have already finished building the DIE's
13481 type. If so, there is no need to continue. */
13482 if (get_die_type (die, cu) != NULL)
13483 return get_die_type (die, cu);
13484
13485 TYPE_TAG_NAME (type) = full_name;
13486 if (die->tag == DW_TAG_structure_type
13487 || die->tag == DW_TAG_class_type)
13488 TYPE_NAME (type) = TYPE_TAG_NAME (type);
13489 }
13490 else
13491 {
13492 /* The name is already allocated along with this objfile, so
13493 we don't need to duplicate it for the type. */
13494 TYPE_TAG_NAME (type) = name;
13495 if (die->tag == DW_TAG_class_type)
13496 TYPE_NAME (type) = TYPE_TAG_NAME (type);
13497 }
13498 }
13499
13500 if (die->tag == DW_TAG_structure_type)
13501 {
13502 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13503 }
13504 else if (die->tag == DW_TAG_union_type)
13505 {
13506 TYPE_CODE (type) = TYPE_CODE_UNION;
13507 }
13508 else
13509 {
13510 TYPE_CODE (type) = TYPE_CODE_STRUCT;
13511 }
13512
13513 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
13514 TYPE_DECLARED_CLASS (type) = 1;
13515
13516 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13517 if (attr)
13518 {
13519 if (attr_form_is_constant (attr))
13520 TYPE_LENGTH (type) = DW_UNSND (attr);
13521 else
13522 {
13523 /* For the moment, dynamic type sizes are not supported
13524 by GDB's struct type. The actual size is determined
13525 on-demand when resolving the type of a given object,
13526 so set the type's length to zero for now. Otherwise,
13527 we record an expression as the length, and that expression
13528 could lead to a very large value, which could eventually
13529 lead to us trying to allocate that much memory when creating
13530 a value of that type. */
13531 TYPE_LENGTH (type) = 0;
13532 }
13533 }
13534 else
13535 {
13536 TYPE_LENGTH (type) = 0;
13537 }
13538
13539 if (producer_is_icc (cu) && (TYPE_LENGTH (type) == 0))
13540 {
13541 /* ICC does not output the required DW_AT_declaration
13542 on incomplete types, but gives them a size of zero. */
13543 TYPE_STUB (type) = 1;
13544 }
13545 else
13546 TYPE_STUB_SUPPORTED (type) = 1;
13547
13548 if (die_is_declaration (die, cu))
13549 TYPE_STUB (type) = 1;
13550 else if (attr == NULL && die->child == NULL
13551 && producer_is_realview (cu->producer))
13552 /* RealView does not output the required DW_AT_declaration
13553 on incomplete types. */
13554 TYPE_STUB (type) = 1;
13555
13556 /* We need to add the type field to the die immediately so we don't
13557 infinitely recurse when dealing with pointers to the structure
13558 type within the structure itself. */
13559 set_die_type (die, type, cu);
13560
13561 /* set_die_type should be already done. */
13562 set_descriptive_type (type, die, cu);
13563
13564 return type;
13565 }
13566
13567 /* Finish creating a structure or union type, including filling in
13568 its members and creating a symbol for it. */
13569
13570 static void
13571 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
13572 {
13573 struct objfile *objfile = cu->objfile;
13574 struct die_info *child_die;
13575 struct type *type;
13576
13577 type = get_die_type (die, cu);
13578 if (type == NULL)
13579 type = read_structure_type (die, cu);
13580
13581 if (die->child != NULL && ! die_is_declaration (die, cu))
13582 {
13583 struct field_info fi;
13584 VEC (symbolp) *template_args = NULL;
13585 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
13586
13587 memset (&fi, 0, sizeof (struct field_info));
13588
13589 child_die = die->child;
13590
13591 while (child_die && child_die->tag)
13592 {
13593 if (child_die->tag == DW_TAG_member
13594 || child_die->tag == DW_TAG_variable)
13595 {
13596 /* NOTE: carlton/2002-11-05: A C++ static data member
13597 should be a DW_TAG_member that is a declaration, but
13598 all versions of G++ as of this writing (so through at
13599 least 3.2.1) incorrectly generate DW_TAG_variable
13600 tags for them instead. */
13601 dwarf2_add_field (&fi, child_die, cu);
13602 }
13603 else if (child_die->tag == DW_TAG_subprogram)
13604 {
13605 /* Rust doesn't have member functions in the C++ sense.
13606 However, it does emit ordinary functions as children
13607 of a struct DIE. */
13608 if (cu->language == language_rust)
13609 read_func_scope (child_die, cu);
13610 else
13611 {
13612 /* C++ member function. */
13613 dwarf2_add_member_fn (&fi, child_die, type, cu);
13614 }
13615 }
13616 else if (child_die->tag == DW_TAG_inheritance)
13617 {
13618 /* C++ base class field. */
13619 dwarf2_add_field (&fi, child_die, cu);
13620 }
13621 else if (child_die->tag == DW_TAG_typedef)
13622 dwarf2_add_typedef (&fi, child_die, cu);
13623 else if (child_die->tag == DW_TAG_template_type_param
13624 || child_die->tag == DW_TAG_template_value_param)
13625 {
13626 struct symbol *arg = new_symbol (child_die, NULL, cu);
13627
13628 if (arg != NULL)
13629 VEC_safe_push (symbolp, template_args, arg);
13630 }
13631
13632 child_die = sibling_die (child_die);
13633 }
13634
13635 /* Attach template arguments to type. */
13636 if (! VEC_empty (symbolp, template_args))
13637 {
13638 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13639 TYPE_N_TEMPLATE_ARGUMENTS (type)
13640 = VEC_length (symbolp, template_args);
13641 TYPE_TEMPLATE_ARGUMENTS (type)
13642 = XOBNEWVEC (&objfile->objfile_obstack,
13643 struct symbol *,
13644 TYPE_N_TEMPLATE_ARGUMENTS (type));
13645 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
13646 VEC_address (symbolp, template_args),
13647 (TYPE_N_TEMPLATE_ARGUMENTS (type)
13648 * sizeof (struct symbol *)));
13649 VEC_free (symbolp, template_args);
13650 }
13651
13652 /* Attach fields and member functions to the type. */
13653 if (fi.nfields)
13654 dwarf2_attach_fields_to_type (&fi, type, cu);
13655 if (fi.nfnfields)
13656 {
13657 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
13658
13659 /* Get the type which refers to the base class (possibly this
13660 class itself) which contains the vtable pointer for the current
13661 class from the DW_AT_containing_type attribute. This use of
13662 DW_AT_containing_type is a GNU extension. */
13663
13664 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
13665 {
13666 struct type *t = die_containing_type (die, cu);
13667
13668 set_type_vptr_basetype (type, t);
13669 if (type == t)
13670 {
13671 int i;
13672
13673 /* Our own class provides vtbl ptr. */
13674 for (i = TYPE_NFIELDS (t) - 1;
13675 i >= TYPE_N_BASECLASSES (t);
13676 --i)
13677 {
13678 const char *fieldname = TYPE_FIELD_NAME (t, i);
13679
13680 if (is_vtable_name (fieldname, cu))
13681 {
13682 set_type_vptr_fieldno (type, i);
13683 break;
13684 }
13685 }
13686
13687 /* Complain if virtual function table field not found. */
13688 if (i < TYPE_N_BASECLASSES (t))
13689 complaint (&symfile_complaints,
13690 _("virtual function table pointer "
13691 "not found when defining class '%s'"),
13692 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
13693 "");
13694 }
13695 else
13696 {
13697 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
13698 }
13699 }
13700 else if (cu->producer
13701 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
13702 {
13703 /* The IBM XLC compiler does not provide direct indication
13704 of the containing type, but the vtable pointer is
13705 always named __vfp. */
13706
13707 int i;
13708
13709 for (i = TYPE_NFIELDS (type) - 1;
13710 i >= TYPE_N_BASECLASSES (type);
13711 --i)
13712 {
13713 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
13714 {
13715 set_type_vptr_fieldno (type, i);
13716 set_type_vptr_basetype (type, type);
13717 break;
13718 }
13719 }
13720 }
13721 }
13722
13723 /* Copy fi.typedef_field_list linked list elements content into the
13724 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
13725 if (fi.typedef_field_list)
13726 {
13727 int i = fi.typedef_field_list_count;
13728
13729 ALLOCATE_CPLUS_STRUCT_TYPE (type);
13730 TYPE_TYPEDEF_FIELD_ARRAY (type)
13731 = ((struct typedef_field *)
13732 TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i));
13733 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
13734
13735 /* Reverse the list order to keep the debug info elements order. */
13736 while (--i >= 0)
13737 {
13738 struct typedef_field *dest, *src;
13739
13740 dest = &TYPE_TYPEDEF_FIELD (type, i);
13741 src = &fi.typedef_field_list->field;
13742 fi.typedef_field_list = fi.typedef_field_list->next;
13743 *dest = *src;
13744 }
13745 }
13746
13747 do_cleanups (back_to);
13748 }
13749
13750 quirk_gcc_member_function_pointer (type, objfile);
13751
13752 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
13753 snapshots) has been known to create a die giving a declaration
13754 for a class that has, as a child, a die giving a definition for a
13755 nested class. So we have to process our children even if the
13756 current die is a declaration. Normally, of course, a declaration
13757 won't have any children at all. */
13758
13759 child_die = die->child;
13760
13761 while (child_die != NULL && child_die->tag)
13762 {
13763 if (child_die->tag == DW_TAG_member
13764 || child_die->tag == DW_TAG_variable
13765 || child_die->tag == DW_TAG_inheritance
13766 || child_die->tag == DW_TAG_template_value_param
13767 || child_die->tag == DW_TAG_template_type_param)
13768 {
13769 /* Do nothing. */
13770 }
13771 else
13772 process_die (child_die, cu);
13773
13774 child_die = sibling_die (child_die);
13775 }
13776
13777 /* Do not consider external references. According to the DWARF standard,
13778 these DIEs are identified by the fact that they have no byte_size
13779 attribute, and a declaration attribute. */
13780 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
13781 || !die_is_declaration (die, cu))
13782 new_symbol (die, type, cu);
13783 }
13784
13785 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
13786 update TYPE using some information only available in DIE's children. */
13787
13788 static void
13789 update_enumeration_type_from_children (struct die_info *die,
13790 struct type *type,
13791 struct dwarf2_cu *cu)
13792 {
13793 struct obstack obstack;
13794 struct die_info *child_die;
13795 int unsigned_enum = 1;
13796 int flag_enum = 1;
13797 ULONGEST mask = 0;
13798 struct cleanup *old_chain;
13799
13800 obstack_init (&obstack);
13801 old_chain = make_cleanup_obstack_free (&obstack);
13802
13803 for (child_die = die->child;
13804 child_die != NULL && child_die->tag;
13805 child_die = sibling_die (child_die))
13806 {
13807 struct attribute *attr;
13808 LONGEST value;
13809 const gdb_byte *bytes;
13810 struct dwarf2_locexpr_baton *baton;
13811 const char *name;
13812
13813 if (child_die->tag != DW_TAG_enumerator)
13814 continue;
13815
13816 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
13817 if (attr == NULL)
13818 continue;
13819
13820 name = dwarf2_name (child_die, cu);
13821 if (name == NULL)
13822 name = "<anonymous enumerator>";
13823
13824 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
13825 &value, &bytes, &baton);
13826 if (value < 0)
13827 {
13828 unsigned_enum = 0;
13829 flag_enum = 0;
13830 }
13831 else if ((mask & value) != 0)
13832 flag_enum = 0;
13833 else
13834 mask |= value;
13835
13836 /* If we already know that the enum type is neither unsigned, nor
13837 a flag type, no need to look at the rest of the enumerates. */
13838 if (!unsigned_enum && !flag_enum)
13839 break;
13840 }
13841
13842 if (unsigned_enum)
13843 TYPE_UNSIGNED (type) = 1;
13844 if (flag_enum)
13845 TYPE_FLAG_ENUM (type) = 1;
13846
13847 do_cleanups (old_chain);
13848 }
13849
13850 /* Given a DW_AT_enumeration_type die, set its type. We do not
13851 complete the type's fields yet, or create any symbols. */
13852
13853 static struct type *
13854 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
13855 {
13856 struct objfile *objfile = cu->objfile;
13857 struct type *type;
13858 struct attribute *attr;
13859 const char *name;
13860
13861 /* If the definition of this type lives in .debug_types, read that type.
13862 Don't follow DW_AT_specification though, that will take us back up
13863 the chain and we want to go down. */
13864 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
13865 if (attr)
13866 {
13867 type = get_DW_AT_signature_type (die, attr, cu);
13868
13869 /* The type's CU may not be the same as CU.
13870 Ensure TYPE is recorded with CU in die_type_hash. */
13871 return set_die_type (die, type, cu);
13872 }
13873
13874 type = alloc_type (objfile);
13875
13876 TYPE_CODE (type) = TYPE_CODE_ENUM;
13877 name = dwarf2_full_name (NULL, die, cu);
13878 if (name != NULL)
13879 TYPE_TAG_NAME (type) = name;
13880
13881 attr = dwarf2_attr (die, DW_AT_type, cu);
13882 if (attr != NULL)
13883 {
13884 struct type *underlying_type = die_type (die, cu);
13885
13886 TYPE_TARGET_TYPE (type) = underlying_type;
13887 }
13888
13889 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13890 if (attr)
13891 {
13892 TYPE_LENGTH (type) = DW_UNSND (attr);
13893 }
13894 else
13895 {
13896 TYPE_LENGTH (type) = 0;
13897 }
13898
13899 /* The enumeration DIE can be incomplete. In Ada, any type can be
13900 declared as private in the package spec, and then defined only
13901 inside the package body. Such types are known as Taft Amendment
13902 Types. When another package uses such a type, an incomplete DIE
13903 may be generated by the compiler. */
13904 if (die_is_declaration (die, cu))
13905 TYPE_STUB (type) = 1;
13906
13907 /* Finish the creation of this type by using the enum's children.
13908 We must call this even when the underlying type has been provided
13909 so that we can determine if we're looking at a "flag" enum. */
13910 update_enumeration_type_from_children (die, type, cu);
13911
13912 /* If this type has an underlying type that is not a stub, then we
13913 may use its attributes. We always use the "unsigned" attribute
13914 in this situation, because ordinarily we guess whether the type
13915 is unsigned -- but the guess can be wrong and the underlying type
13916 can tell us the reality. However, we defer to a local size
13917 attribute if one exists, because this lets the compiler override
13918 the underlying type if needed. */
13919 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
13920 {
13921 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
13922 if (TYPE_LENGTH (type) == 0)
13923 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
13924 }
13925
13926 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
13927
13928 return set_die_type (die, type, cu);
13929 }
13930
13931 /* Given a pointer to a die which begins an enumeration, process all
13932 the dies that define the members of the enumeration, and create the
13933 symbol for the enumeration type.
13934
13935 NOTE: We reverse the order of the element list. */
13936
13937 static void
13938 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
13939 {
13940 struct type *this_type;
13941
13942 this_type = get_die_type (die, cu);
13943 if (this_type == NULL)
13944 this_type = read_enumeration_type (die, cu);
13945
13946 if (die->child != NULL)
13947 {
13948 struct die_info *child_die;
13949 struct symbol *sym;
13950 struct field *fields = NULL;
13951 int num_fields = 0;
13952 const char *name;
13953
13954 child_die = die->child;
13955 while (child_die && child_die->tag)
13956 {
13957 if (child_die->tag != DW_TAG_enumerator)
13958 {
13959 process_die (child_die, cu);
13960 }
13961 else
13962 {
13963 name = dwarf2_name (child_die, cu);
13964 if (name)
13965 {
13966 sym = new_symbol (child_die, this_type, cu);
13967
13968 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
13969 {
13970 fields = (struct field *)
13971 xrealloc (fields,
13972 (num_fields + DW_FIELD_ALLOC_CHUNK)
13973 * sizeof (struct field));
13974 }
13975
13976 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
13977 FIELD_TYPE (fields[num_fields]) = NULL;
13978 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
13979 FIELD_BITSIZE (fields[num_fields]) = 0;
13980
13981 num_fields++;
13982 }
13983 }
13984
13985 child_die = sibling_die (child_die);
13986 }
13987
13988 if (num_fields)
13989 {
13990 TYPE_NFIELDS (this_type) = num_fields;
13991 TYPE_FIELDS (this_type) = (struct field *)
13992 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
13993 memcpy (TYPE_FIELDS (this_type), fields,
13994 sizeof (struct field) * num_fields);
13995 xfree (fields);
13996 }
13997 }
13998
13999 /* If we are reading an enum from a .debug_types unit, and the enum
14000 is a declaration, and the enum is not the signatured type in the
14001 unit, then we do not want to add a symbol for it. Adding a
14002 symbol would in some cases obscure the true definition of the
14003 enum, giving users an incomplete type when the definition is
14004 actually available. Note that we do not want to do this for all
14005 enums which are just declarations, because C++0x allows forward
14006 enum declarations. */
14007 if (cu->per_cu->is_debug_types
14008 && die_is_declaration (die, cu))
14009 {
14010 struct signatured_type *sig_type;
14011
14012 sig_type = (struct signatured_type *) cu->per_cu;
14013 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
14014 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
14015 return;
14016 }
14017
14018 new_symbol (die, this_type, cu);
14019 }
14020
14021 /* Extract all information from a DW_TAG_array_type DIE and put it in
14022 the DIE's type field. For now, this only handles one dimensional
14023 arrays. */
14024
14025 static struct type *
14026 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
14027 {
14028 struct objfile *objfile = cu->objfile;
14029 struct die_info *child_die;
14030 struct type *type;
14031 struct type *element_type, *range_type, *index_type;
14032 struct type **range_types = NULL;
14033 struct attribute *attr;
14034 int ndim = 0;
14035 struct cleanup *back_to;
14036 const char *name;
14037 unsigned int bit_stride = 0;
14038
14039 element_type = die_type (die, cu);
14040
14041 /* The die_type call above may have already set the type for this DIE. */
14042 type = get_die_type (die, cu);
14043 if (type)
14044 return type;
14045
14046 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
14047 if (attr != NULL)
14048 bit_stride = DW_UNSND (attr) * 8;
14049
14050 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
14051 if (attr != NULL)
14052 bit_stride = DW_UNSND (attr);
14053
14054 /* Irix 6.2 native cc creates array types without children for
14055 arrays with unspecified length. */
14056 if (die->child == NULL)
14057 {
14058 index_type = objfile_type (objfile)->builtin_int;
14059 range_type = create_static_range_type (NULL, index_type, 0, -1);
14060 type = create_array_type_with_stride (NULL, element_type, range_type,
14061 bit_stride);
14062 return set_die_type (die, type, cu);
14063 }
14064
14065 back_to = make_cleanup (null_cleanup, NULL);
14066 child_die = die->child;
14067 while (child_die && child_die->tag)
14068 {
14069 if (child_die->tag == DW_TAG_subrange_type)
14070 {
14071 struct type *child_type = read_type_die (child_die, cu);
14072
14073 if (child_type != NULL)
14074 {
14075 /* The range type was succesfully read. Save it for the
14076 array type creation. */
14077 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
14078 {
14079 range_types = (struct type **)
14080 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
14081 * sizeof (struct type *));
14082 if (ndim == 0)
14083 make_cleanup (free_current_contents, &range_types);
14084 }
14085 range_types[ndim++] = child_type;
14086 }
14087 }
14088 child_die = sibling_die (child_die);
14089 }
14090
14091 /* Dwarf2 dimensions are output from left to right, create the
14092 necessary array types in backwards order. */
14093
14094 type = element_type;
14095
14096 if (read_array_order (die, cu) == DW_ORD_col_major)
14097 {
14098 int i = 0;
14099
14100 while (i < ndim)
14101 type = create_array_type_with_stride (NULL, type, range_types[i++],
14102 bit_stride);
14103 }
14104 else
14105 {
14106 while (ndim-- > 0)
14107 type = create_array_type_with_stride (NULL, type, range_types[ndim],
14108 bit_stride);
14109 }
14110
14111 /* Understand Dwarf2 support for vector types (like they occur on
14112 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
14113 array type. This is not part of the Dwarf2/3 standard yet, but a
14114 custom vendor extension. The main difference between a regular
14115 array and the vector variant is that vectors are passed by value
14116 to functions. */
14117 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
14118 if (attr)
14119 make_vector_type (type);
14120
14121 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
14122 implementation may choose to implement triple vectors using this
14123 attribute. */
14124 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14125 if (attr)
14126 {
14127 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
14128 TYPE_LENGTH (type) = DW_UNSND (attr);
14129 else
14130 complaint (&symfile_complaints,
14131 _("DW_AT_byte_size for array type smaller "
14132 "than the total size of elements"));
14133 }
14134
14135 name = dwarf2_name (die, cu);
14136 if (name)
14137 TYPE_NAME (type) = name;
14138
14139 /* Install the type in the die. */
14140 set_die_type (die, type, cu);
14141
14142 /* set_die_type should be already done. */
14143 set_descriptive_type (type, die, cu);
14144
14145 do_cleanups (back_to);
14146
14147 return type;
14148 }
14149
14150 static enum dwarf_array_dim_ordering
14151 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
14152 {
14153 struct attribute *attr;
14154
14155 attr = dwarf2_attr (die, DW_AT_ordering, cu);
14156
14157 if (attr)
14158 return (enum dwarf_array_dim_ordering) DW_SND (attr);
14159
14160 /* GNU F77 is a special case, as at 08/2004 array type info is the
14161 opposite order to the dwarf2 specification, but data is still
14162 laid out as per normal fortran.
14163
14164 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
14165 version checking. */
14166
14167 if (cu->language == language_fortran
14168 && cu->producer && strstr (cu->producer, "GNU F77"))
14169 {
14170 return DW_ORD_row_major;
14171 }
14172
14173 switch (cu->language_defn->la_array_ordering)
14174 {
14175 case array_column_major:
14176 return DW_ORD_col_major;
14177 case array_row_major:
14178 default:
14179 return DW_ORD_row_major;
14180 };
14181 }
14182
14183 /* Extract all information from a DW_TAG_set_type DIE and put it in
14184 the DIE's type field. */
14185
14186 static struct type *
14187 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
14188 {
14189 struct type *domain_type, *set_type;
14190 struct attribute *attr;
14191
14192 domain_type = die_type (die, cu);
14193
14194 /* The die_type call above may have already set the type for this DIE. */
14195 set_type = get_die_type (die, cu);
14196 if (set_type)
14197 return set_type;
14198
14199 set_type = create_set_type (NULL, domain_type);
14200
14201 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14202 if (attr)
14203 TYPE_LENGTH (set_type) = DW_UNSND (attr);
14204
14205 return set_die_type (die, set_type, cu);
14206 }
14207
14208 /* A helper for read_common_block that creates a locexpr baton.
14209 SYM is the symbol which we are marking as computed.
14210 COMMON_DIE is the DIE for the common block.
14211 COMMON_LOC is the location expression attribute for the common
14212 block itself.
14213 MEMBER_LOC is the location expression attribute for the particular
14214 member of the common block that we are processing.
14215 CU is the CU from which the above come. */
14216
14217 static void
14218 mark_common_block_symbol_computed (struct symbol *sym,
14219 struct die_info *common_die,
14220 struct attribute *common_loc,
14221 struct attribute *member_loc,
14222 struct dwarf2_cu *cu)
14223 {
14224 struct objfile *objfile = dwarf2_per_objfile->objfile;
14225 struct dwarf2_locexpr_baton *baton;
14226 gdb_byte *ptr;
14227 unsigned int cu_off;
14228 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
14229 LONGEST offset = 0;
14230
14231 gdb_assert (common_loc && member_loc);
14232 gdb_assert (attr_form_is_block (common_loc));
14233 gdb_assert (attr_form_is_block (member_loc)
14234 || attr_form_is_constant (member_loc));
14235
14236 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14237 baton->per_cu = cu->per_cu;
14238 gdb_assert (baton->per_cu);
14239
14240 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
14241
14242 if (attr_form_is_constant (member_loc))
14243 {
14244 offset = dwarf2_get_attr_constant_value (member_loc, 0);
14245 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
14246 }
14247 else
14248 baton->size += DW_BLOCK (member_loc)->size;
14249
14250 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
14251 baton->data = ptr;
14252
14253 *ptr++ = DW_OP_call4;
14254 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
14255 store_unsigned_integer (ptr, 4, byte_order, cu_off);
14256 ptr += 4;
14257
14258 if (attr_form_is_constant (member_loc))
14259 {
14260 *ptr++ = DW_OP_addr;
14261 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
14262 ptr += cu->header.addr_size;
14263 }
14264 else
14265 {
14266 /* We have to copy the data here, because DW_OP_call4 will only
14267 use a DW_AT_location attribute. */
14268 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
14269 ptr += DW_BLOCK (member_loc)->size;
14270 }
14271
14272 *ptr++ = DW_OP_plus;
14273 gdb_assert (ptr - baton->data == baton->size);
14274
14275 SYMBOL_LOCATION_BATON (sym) = baton;
14276 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
14277 }
14278
14279 /* Create appropriate locally-scoped variables for all the
14280 DW_TAG_common_block entries. Also create a struct common_block
14281 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
14282 is used to sepate the common blocks name namespace from regular
14283 variable names. */
14284
14285 static void
14286 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
14287 {
14288 struct attribute *attr;
14289
14290 attr = dwarf2_attr (die, DW_AT_location, cu);
14291 if (attr)
14292 {
14293 /* Support the .debug_loc offsets. */
14294 if (attr_form_is_block (attr))
14295 {
14296 /* Ok. */
14297 }
14298 else if (attr_form_is_section_offset (attr))
14299 {
14300 dwarf2_complex_location_expr_complaint ();
14301 attr = NULL;
14302 }
14303 else
14304 {
14305 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14306 "common block member");
14307 attr = NULL;
14308 }
14309 }
14310
14311 if (die->child != NULL)
14312 {
14313 struct objfile *objfile = cu->objfile;
14314 struct die_info *child_die;
14315 size_t n_entries = 0, size;
14316 struct common_block *common_block;
14317 struct symbol *sym;
14318
14319 for (child_die = die->child;
14320 child_die && child_die->tag;
14321 child_die = sibling_die (child_die))
14322 ++n_entries;
14323
14324 size = (sizeof (struct common_block)
14325 + (n_entries - 1) * sizeof (struct symbol *));
14326 common_block
14327 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
14328 size);
14329 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
14330 common_block->n_entries = 0;
14331
14332 for (child_die = die->child;
14333 child_die && child_die->tag;
14334 child_die = sibling_die (child_die))
14335 {
14336 /* Create the symbol in the DW_TAG_common_block block in the current
14337 symbol scope. */
14338 sym = new_symbol (child_die, NULL, cu);
14339 if (sym != NULL)
14340 {
14341 struct attribute *member_loc;
14342
14343 common_block->contents[common_block->n_entries++] = sym;
14344
14345 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
14346 cu);
14347 if (member_loc)
14348 {
14349 /* GDB has handled this for a long time, but it is
14350 not specified by DWARF. It seems to have been
14351 emitted by gfortran at least as recently as:
14352 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
14353 complaint (&symfile_complaints,
14354 _("Variable in common block has "
14355 "DW_AT_data_member_location "
14356 "- DIE at 0x%x [in module %s]"),
14357 child_die->offset.sect_off,
14358 objfile_name (cu->objfile));
14359
14360 if (attr_form_is_section_offset (member_loc))
14361 dwarf2_complex_location_expr_complaint ();
14362 else if (attr_form_is_constant (member_loc)
14363 || attr_form_is_block (member_loc))
14364 {
14365 if (attr)
14366 mark_common_block_symbol_computed (sym, die, attr,
14367 member_loc, cu);
14368 }
14369 else
14370 dwarf2_complex_location_expr_complaint ();
14371 }
14372 }
14373 }
14374
14375 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
14376 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
14377 }
14378 }
14379
14380 /* Create a type for a C++ namespace. */
14381
14382 static struct type *
14383 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
14384 {
14385 struct objfile *objfile = cu->objfile;
14386 const char *previous_prefix, *name;
14387 int is_anonymous;
14388 struct type *type;
14389
14390 /* For extensions, reuse the type of the original namespace. */
14391 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
14392 {
14393 struct die_info *ext_die;
14394 struct dwarf2_cu *ext_cu = cu;
14395
14396 ext_die = dwarf2_extension (die, &ext_cu);
14397 type = read_type_die (ext_die, ext_cu);
14398
14399 /* EXT_CU may not be the same as CU.
14400 Ensure TYPE is recorded with CU in die_type_hash. */
14401 return set_die_type (die, type, cu);
14402 }
14403
14404 name = namespace_name (die, &is_anonymous, cu);
14405
14406 /* Now build the name of the current namespace. */
14407
14408 previous_prefix = determine_prefix (die, cu);
14409 if (previous_prefix[0] != '\0')
14410 name = typename_concat (&objfile->objfile_obstack,
14411 previous_prefix, name, 0, cu);
14412
14413 /* Create the type. */
14414 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
14415 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14416
14417 return set_die_type (die, type, cu);
14418 }
14419
14420 /* Read a namespace scope. */
14421
14422 static void
14423 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
14424 {
14425 struct objfile *objfile = cu->objfile;
14426 int is_anonymous;
14427
14428 /* Add a symbol associated to this if we haven't seen the namespace
14429 before. Also, add a using directive if it's an anonymous
14430 namespace. */
14431
14432 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
14433 {
14434 struct type *type;
14435
14436 type = read_type_die (die, cu);
14437 new_symbol (die, type, cu);
14438
14439 namespace_name (die, &is_anonymous, cu);
14440 if (is_anonymous)
14441 {
14442 const char *previous_prefix = determine_prefix (die, cu);
14443
14444 add_using_directive (using_directives (cu->language),
14445 previous_prefix, TYPE_NAME (type), NULL,
14446 NULL, NULL, 0, &objfile->objfile_obstack);
14447 }
14448 }
14449
14450 if (die->child != NULL)
14451 {
14452 struct die_info *child_die = die->child;
14453
14454 while (child_die && child_die->tag)
14455 {
14456 process_die (child_die, cu);
14457 child_die = sibling_die (child_die);
14458 }
14459 }
14460 }
14461
14462 /* Read a Fortran module as type. This DIE can be only a declaration used for
14463 imported module. Still we need that type as local Fortran "use ... only"
14464 declaration imports depend on the created type in determine_prefix. */
14465
14466 static struct type *
14467 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
14468 {
14469 struct objfile *objfile = cu->objfile;
14470 const char *module_name;
14471 struct type *type;
14472
14473 module_name = dwarf2_name (die, cu);
14474 if (!module_name)
14475 complaint (&symfile_complaints,
14476 _("DW_TAG_module has no name, offset 0x%x"),
14477 die->offset.sect_off);
14478 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
14479
14480 /* determine_prefix uses TYPE_TAG_NAME. */
14481 TYPE_TAG_NAME (type) = TYPE_NAME (type);
14482
14483 return set_die_type (die, type, cu);
14484 }
14485
14486 /* Read a Fortran module. */
14487
14488 static void
14489 read_module (struct die_info *die, struct dwarf2_cu *cu)
14490 {
14491 struct die_info *child_die = die->child;
14492 struct type *type;
14493
14494 type = read_type_die (die, cu);
14495 new_symbol (die, type, cu);
14496
14497 while (child_die && child_die->tag)
14498 {
14499 process_die (child_die, cu);
14500 child_die = sibling_die (child_die);
14501 }
14502 }
14503
14504 /* Return the name of the namespace represented by DIE. Set
14505 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
14506 namespace. */
14507
14508 static const char *
14509 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
14510 {
14511 struct die_info *current_die;
14512 const char *name = NULL;
14513
14514 /* Loop through the extensions until we find a name. */
14515
14516 for (current_die = die;
14517 current_die != NULL;
14518 current_die = dwarf2_extension (die, &cu))
14519 {
14520 /* We don't use dwarf2_name here so that we can detect the absence
14521 of a name -> anonymous namespace. */
14522 name = dwarf2_string_attr (die, DW_AT_name, cu);
14523
14524 if (name != NULL)
14525 break;
14526 }
14527
14528 /* Is it an anonymous namespace? */
14529
14530 *is_anonymous = (name == NULL);
14531 if (*is_anonymous)
14532 name = CP_ANONYMOUS_NAMESPACE_STR;
14533
14534 return name;
14535 }
14536
14537 /* Extract all information from a DW_TAG_pointer_type DIE and add to
14538 the user defined type vector. */
14539
14540 static struct type *
14541 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
14542 {
14543 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
14544 struct comp_unit_head *cu_header = &cu->header;
14545 struct type *type;
14546 struct attribute *attr_byte_size;
14547 struct attribute *attr_address_class;
14548 int byte_size, addr_class;
14549 struct type *target_type;
14550
14551 target_type = die_type (die, cu);
14552
14553 /* The die_type call above may have already set the type for this DIE. */
14554 type = get_die_type (die, cu);
14555 if (type)
14556 return type;
14557
14558 type = lookup_pointer_type (target_type);
14559
14560 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
14561 if (attr_byte_size)
14562 byte_size = DW_UNSND (attr_byte_size);
14563 else
14564 byte_size = cu_header->addr_size;
14565
14566 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
14567 if (attr_address_class)
14568 addr_class = DW_UNSND (attr_address_class);
14569 else
14570 addr_class = DW_ADDR_none;
14571
14572 /* If the pointer size or address class is different than the
14573 default, create a type variant marked as such and set the
14574 length accordingly. */
14575 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
14576 {
14577 if (gdbarch_address_class_type_flags_p (gdbarch))
14578 {
14579 int type_flags;
14580
14581 type_flags = gdbarch_address_class_type_flags
14582 (gdbarch, byte_size, addr_class);
14583 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
14584 == 0);
14585 type = make_type_with_address_space (type, type_flags);
14586 }
14587 else if (TYPE_LENGTH (type) != byte_size)
14588 {
14589 complaint (&symfile_complaints,
14590 _("invalid pointer size %d"), byte_size);
14591 }
14592 else
14593 {
14594 /* Should we also complain about unhandled address classes? */
14595 }
14596 }
14597
14598 TYPE_LENGTH (type) = byte_size;
14599 return set_die_type (die, type, cu);
14600 }
14601
14602 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
14603 the user defined type vector. */
14604
14605 static struct type *
14606 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
14607 {
14608 struct type *type;
14609 struct type *to_type;
14610 struct type *domain;
14611
14612 to_type = die_type (die, cu);
14613 domain = die_containing_type (die, cu);
14614
14615 /* The calls above may have already set the type for this DIE. */
14616 type = get_die_type (die, cu);
14617 if (type)
14618 return type;
14619
14620 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
14621 type = lookup_methodptr_type (to_type);
14622 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
14623 {
14624 struct type *new_type = alloc_type (cu->objfile);
14625
14626 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
14627 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
14628 TYPE_VARARGS (to_type));
14629 type = lookup_methodptr_type (new_type);
14630 }
14631 else
14632 type = lookup_memberptr_type (to_type, domain);
14633
14634 return set_die_type (die, type, cu);
14635 }
14636
14637 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
14638 the user defined type vector. */
14639
14640 static struct type *
14641 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
14642 enum type_code refcode)
14643 {
14644 struct comp_unit_head *cu_header = &cu->header;
14645 struct type *type, *target_type;
14646 struct attribute *attr;
14647
14648 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
14649
14650 target_type = die_type (die, cu);
14651
14652 /* The die_type call above may have already set the type for this DIE. */
14653 type = get_die_type (die, cu);
14654 if (type)
14655 return type;
14656
14657 type = lookup_reference_type (target_type, refcode);
14658 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14659 if (attr)
14660 {
14661 TYPE_LENGTH (type) = DW_UNSND (attr);
14662 }
14663 else
14664 {
14665 TYPE_LENGTH (type) = cu_header->addr_size;
14666 }
14667 return set_die_type (die, type, cu);
14668 }
14669
14670 /* Add the given cv-qualifiers to the element type of the array. GCC
14671 outputs DWARF type qualifiers that apply to an array, not the
14672 element type. But GDB relies on the array element type to carry
14673 the cv-qualifiers. This mimics section 6.7.3 of the C99
14674 specification. */
14675
14676 static struct type *
14677 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
14678 struct type *base_type, int cnst, int voltl)
14679 {
14680 struct type *el_type, *inner_array;
14681
14682 base_type = copy_type (base_type);
14683 inner_array = base_type;
14684
14685 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
14686 {
14687 TYPE_TARGET_TYPE (inner_array) =
14688 copy_type (TYPE_TARGET_TYPE (inner_array));
14689 inner_array = TYPE_TARGET_TYPE (inner_array);
14690 }
14691
14692 el_type = TYPE_TARGET_TYPE (inner_array);
14693 cnst |= TYPE_CONST (el_type);
14694 voltl |= TYPE_VOLATILE (el_type);
14695 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
14696
14697 return set_die_type (die, base_type, cu);
14698 }
14699
14700 static struct type *
14701 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
14702 {
14703 struct type *base_type, *cv_type;
14704
14705 base_type = die_type (die, cu);
14706
14707 /* The die_type call above may have already set the type for this DIE. */
14708 cv_type = get_die_type (die, cu);
14709 if (cv_type)
14710 return cv_type;
14711
14712 /* In case the const qualifier is applied to an array type, the element type
14713 is so qualified, not the array type (section 6.7.3 of C99). */
14714 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14715 return add_array_cv_type (die, cu, base_type, 1, 0);
14716
14717 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
14718 return set_die_type (die, cv_type, cu);
14719 }
14720
14721 static struct type *
14722 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
14723 {
14724 struct type *base_type, *cv_type;
14725
14726 base_type = die_type (die, cu);
14727
14728 /* The die_type call above may have already set the type for this DIE. */
14729 cv_type = get_die_type (die, cu);
14730 if (cv_type)
14731 return cv_type;
14732
14733 /* In case the volatile qualifier is applied to an array type, the
14734 element type is so qualified, not the array type (section 6.7.3
14735 of C99). */
14736 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
14737 return add_array_cv_type (die, cu, base_type, 0, 1);
14738
14739 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
14740 return set_die_type (die, cv_type, cu);
14741 }
14742
14743 /* Handle DW_TAG_restrict_type. */
14744
14745 static struct type *
14746 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
14747 {
14748 struct type *base_type, *cv_type;
14749
14750 base_type = die_type (die, cu);
14751
14752 /* The die_type call above may have already set the type for this DIE. */
14753 cv_type = get_die_type (die, cu);
14754 if (cv_type)
14755 return cv_type;
14756
14757 cv_type = make_restrict_type (base_type);
14758 return set_die_type (die, cv_type, cu);
14759 }
14760
14761 /* Handle DW_TAG_atomic_type. */
14762
14763 static struct type *
14764 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
14765 {
14766 struct type *base_type, *cv_type;
14767
14768 base_type = die_type (die, cu);
14769
14770 /* The die_type call above may have already set the type for this DIE. */
14771 cv_type = get_die_type (die, cu);
14772 if (cv_type)
14773 return cv_type;
14774
14775 cv_type = make_atomic_type (base_type);
14776 return set_die_type (die, cv_type, cu);
14777 }
14778
14779 /* Extract all information from a DW_TAG_string_type DIE and add to
14780 the user defined type vector. It isn't really a user defined type,
14781 but it behaves like one, with other DIE's using an AT_user_def_type
14782 attribute to reference it. */
14783
14784 static struct type *
14785 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
14786 {
14787 struct objfile *objfile = cu->objfile;
14788 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14789 struct type *type, *range_type, *index_type, *char_type;
14790 struct attribute *attr;
14791 unsigned int length;
14792
14793 attr = dwarf2_attr (die, DW_AT_string_length, cu);
14794 if (attr)
14795 {
14796 length = DW_UNSND (attr);
14797 }
14798 else
14799 {
14800 /* Check for the DW_AT_byte_size attribute. */
14801 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14802 if (attr)
14803 {
14804 length = DW_UNSND (attr);
14805 }
14806 else
14807 {
14808 length = 1;
14809 }
14810 }
14811
14812 index_type = objfile_type (objfile)->builtin_int;
14813 range_type = create_static_range_type (NULL, index_type, 1, length);
14814 char_type = language_string_char_type (cu->language_defn, gdbarch);
14815 type = create_string_type (NULL, char_type, range_type);
14816
14817 return set_die_type (die, type, cu);
14818 }
14819
14820 /* Assuming that DIE corresponds to a function, returns nonzero
14821 if the function is prototyped. */
14822
14823 static int
14824 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
14825 {
14826 struct attribute *attr;
14827
14828 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
14829 if (attr && (DW_UNSND (attr) != 0))
14830 return 1;
14831
14832 /* The DWARF standard implies that the DW_AT_prototyped attribute
14833 is only meaninful for C, but the concept also extends to other
14834 languages that allow unprototyped functions (Eg: Objective C).
14835 For all other languages, assume that functions are always
14836 prototyped. */
14837 if (cu->language != language_c
14838 && cu->language != language_objc
14839 && cu->language != language_opencl)
14840 return 1;
14841
14842 /* RealView does not emit DW_AT_prototyped. We can not distinguish
14843 prototyped and unprototyped functions; default to prototyped,
14844 since that is more common in modern code (and RealView warns
14845 about unprototyped functions). */
14846 if (producer_is_realview (cu->producer))
14847 return 1;
14848
14849 return 0;
14850 }
14851
14852 /* Handle DIES due to C code like:
14853
14854 struct foo
14855 {
14856 int (*funcp)(int a, long l);
14857 int b;
14858 };
14859
14860 ('funcp' generates a DW_TAG_subroutine_type DIE). */
14861
14862 static struct type *
14863 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
14864 {
14865 struct objfile *objfile = cu->objfile;
14866 struct type *type; /* Type that this function returns. */
14867 struct type *ftype; /* Function that returns above type. */
14868 struct attribute *attr;
14869
14870 type = die_type (die, cu);
14871
14872 /* The die_type call above may have already set the type for this DIE. */
14873 ftype = get_die_type (die, cu);
14874 if (ftype)
14875 return ftype;
14876
14877 ftype = lookup_function_type (type);
14878
14879 if (prototyped_function_p (die, cu))
14880 TYPE_PROTOTYPED (ftype) = 1;
14881
14882 /* Store the calling convention in the type if it's available in
14883 the subroutine die. Otherwise set the calling convention to
14884 the default value DW_CC_normal. */
14885 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
14886 if (attr)
14887 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
14888 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
14889 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
14890 else
14891 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
14892
14893 /* Record whether the function returns normally to its caller or not
14894 if the DWARF producer set that information. */
14895 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
14896 if (attr && (DW_UNSND (attr) != 0))
14897 TYPE_NO_RETURN (ftype) = 1;
14898
14899 /* We need to add the subroutine type to the die immediately so
14900 we don't infinitely recurse when dealing with parameters
14901 declared as the same subroutine type. */
14902 set_die_type (die, ftype, cu);
14903
14904 if (die->child != NULL)
14905 {
14906 struct type *void_type = objfile_type (objfile)->builtin_void;
14907 struct die_info *child_die;
14908 int nparams, iparams;
14909
14910 /* Count the number of parameters.
14911 FIXME: GDB currently ignores vararg functions, but knows about
14912 vararg member functions. */
14913 nparams = 0;
14914 child_die = die->child;
14915 while (child_die && child_die->tag)
14916 {
14917 if (child_die->tag == DW_TAG_formal_parameter)
14918 nparams++;
14919 else if (child_die->tag == DW_TAG_unspecified_parameters)
14920 TYPE_VARARGS (ftype) = 1;
14921 child_die = sibling_die (child_die);
14922 }
14923
14924 /* Allocate storage for parameters and fill them in. */
14925 TYPE_NFIELDS (ftype) = nparams;
14926 TYPE_FIELDS (ftype) = (struct field *)
14927 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
14928
14929 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
14930 even if we error out during the parameters reading below. */
14931 for (iparams = 0; iparams < nparams; iparams++)
14932 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
14933
14934 iparams = 0;
14935 child_die = die->child;
14936 while (child_die && child_die->tag)
14937 {
14938 if (child_die->tag == DW_TAG_formal_parameter)
14939 {
14940 struct type *arg_type;
14941
14942 /* DWARF version 2 has no clean way to discern C++
14943 static and non-static member functions. G++ helps
14944 GDB by marking the first parameter for non-static
14945 member functions (which is the this pointer) as
14946 artificial. We pass this information to
14947 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
14948
14949 DWARF version 3 added DW_AT_object_pointer, which GCC
14950 4.5 does not yet generate. */
14951 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
14952 if (attr)
14953 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
14954 else
14955 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
14956 arg_type = die_type (child_die, cu);
14957
14958 /* RealView does not mark THIS as const, which the testsuite
14959 expects. GCC marks THIS as const in method definitions,
14960 but not in the class specifications (GCC PR 43053). */
14961 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
14962 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
14963 {
14964 int is_this = 0;
14965 struct dwarf2_cu *arg_cu = cu;
14966 const char *name = dwarf2_name (child_die, cu);
14967
14968 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
14969 if (attr)
14970 {
14971 /* If the compiler emits this, use it. */
14972 if (follow_die_ref (die, attr, &arg_cu) == child_die)
14973 is_this = 1;
14974 }
14975 else if (name && strcmp (name, "this") == 0)
14976 /* Function definitions will have the argument names. */
14977 is_this = 1;
14978 else if (name == NULL && iparams == 0)
14979 /* Declarations may not have the names, so like
14980 elsewhere in GDB, assume an artificial first
14981 argument is "this". */
14982 is_this = 1;
14983
14984 if (is_this)
14985 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
14986 arg_type, 0);
14987 }
14988
14989 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
14990 iparams++;
14991 }
14992 child_die = sibling_die (child_die);
14993 }
14994 }
14995
14996 return ftype;
14997 }
14998
14999 static struct type *
15000 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
15001 {
15002 struct objfile *objfile = cu->objfile;
15003 const char *name = NULL;
15004 struct type *this_type, *target_type;
15005
15006 name = dwarf2_full_name (NULL, die, cu);
15007 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
15008 TYPE_TARGET_STUB (this_type) = 1;
15009 set_die_type (die, this_type, cu);
15010 target_type = die_type (die, cu);
15011 if (target_type != this_type)
15012 TYPE_TARGET_TYPE (this_type) = target_type;
15013 else
15014 {
15015 /* Self-referential typedefs are, it seems, not allowed by the DWARF
15016 spec and cause infinite loops in GDB. */
15017 complaint (&symfile_complaints,
15018 _("Self-referential DW_TAG_typedef "
15019 "- DIE at 0x%x [in module %s]"),
15020 die->offset.sect_off, objfile_name (objfile));
15021 TYPE_TARGET_TYPE (this_type) = NULL;
15022 }
15023 return this_type;
15024 }
15025
15026 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
15027 (which may be different from NAME) to the architecture back-end to allow
15028 it to guess the correct format if necessary. */
15029
15030 static struct type *
15031 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
15032 const char *name_hint)
15033 {
15034 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15035 const struct floatformat **format;
15036 struct type *type;
15037
15038 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
15039 if (format)
15040 type = init_float_type (objfile, bits, name, format);
15041 else
15042 type = init_type (objfile, TYPE_CODE_ERROR, bits / TARGET_CHAR_BIT, name);
15043
15044 return type;
15045 }
15046
15047 /* Find a representation of a given base type and install
15048 it in the TYPE field of the die. */
15049
15050 static struct type *
15051 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
15052 {
15053 struct objfile *objfile = cu->objfile;
15054 struct type *type;
15055 struct attribute *attr;
15056 int encoding = 0, bits = 0;
15057 const char *name;
15058
15059 attr = dwarf2_attr (die, DW_AT_encoding, cu);
15060 if (attr)
15061 {
15062 encoding = DW_UNSND (attr);
15063 }
15064 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15065 if (attr)
15066 {
15067 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
15068 }
15069 name = dwarf2_name (die, cu);
15070 if (!name)
15071 {
15072 complaint (&symfile_complaints,
15073 _("DW_AT_name missing from DW_TAG_base_type"));
15074 }
15075
15076 switch (encoding)
15077 {
15078 case DW_ATE_address:
15079 /* Turn DW_ATE_address into a void * pointer. */
15080 type = init_type (objfile, TYPE_CODE_VOID, 1, NULL);
15081 type = init_pointer_type (objfile, bits, name, type);
15082 break;
15083 case DW_ATE_boolean:
15084 type = init_boolean_type (objfile, bits, 1, name);
15085 break;
15086 case DW_ATE_complex_float:
15087 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
15088 type = init_complex_type (objfile, name, type);
15089 break;
15090 case DW_ATE_decimal_float:
15091 type = init_decfloat_type (objfile, bits, name);
15092 break;
15093 case DW_ATE_float:
15094 type = dwarf2_init_float_type (objfile, bits, name, name);
15095 break;
15096 case DW_ATE_signed:
15097 type = init_integer_type (objfile, bits, 0, name);
15098 break;
15099 case DW_ATE_unsigned:
15100 if (cu->language == language_fortran
15101 && name
15102 && startswith (name, "character("))
15103 type = init_character_type (objfile, bits, 1, name);
15104 else
15105 type = init_integer_type (objfile, bits, 1, name);
15106 break;
15107 case DW_ATE_signed_char:
15108 if (cu->language == language_ada || cu->language == language_m2
15109 || cu->language == language_pascal
15110 || cu->language == language_fortran)
15111 type = init_character_type (objfile, bits, 0, name);
15112 else
15113 type = init_integer_type (objfile, bits, 0, name);
15114 break;
15115 case DW_ATE_unsigned_char:
15116 if (cu->language == language_ada || cu->language == language_m2
15117 || cu->language == language_pascal
15118 || cu->language == language_fortran
15119 || cu->language == language_rust)
15120 type = init_character_type (objfile, bits, 1, name);
15121 else
15122 type = init_integer_type (objfile, bits, 1, name);
15123 break;
15124 case DW_ATE_UTF:
15125 /* We just treat this as an integer and then recognize the
15126 type by name elsewhere. */
15127 type = init_integer_type (objfile, bits, 0, name);
15128 break;
15129
15130 default:
15131 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
15132 dwarf_type_encoding_name (encoding));
15133 type = init_type (objfile, TYPE_CODE_ERROR,
15134 bits / TARGET_CHAR_BIT, name);
15135 break;
15136 }
15137
15138 if (name && strcmp (name, "char") == 0)
15139 TYPE_NOSIGN (type) = 1;
15140
15141 return set_die_type (die, type, cu);
15142 }
15143
15144 /* Parse dwarf attribute if it's a block, reference or constant and put the
15145 resulting value of the attribute into struct bound_prop.
15146 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
15147
15148 static int
15149 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
15150 struct dwarf2_cu *cu, struct dynamic_prop *prop)
15151 {
15152 struct dwarf2_property_baton *baton;
15153 struct obstack *obstack = &cu->objfile->objfile_obstack;
15154
15155 if (attr == NULL || prop == NULL)
15156 return 0;
15157
15158 if (attr_form_is_block (attr))
15159 {
15160 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15161 baton->referenced_type = NULL;
15162 baton->locexpr.per_cu = cu->per_cu;
15163 baton->locexpr.size = DW_BLOCK (attr)->size;
15164 baton->locexpr.data = DW_BLOCK (attr)->data;
15165 prop->data.baton = baton;
15166 prop->kind = PROP_LOCEXPR;
15167 gdb_assert (prop->data.baton != NULL);
15168 }
15169 else if (attr_form_is_ref (attr))
15170 {
15171 struct dwarf2_cu *target_cu = cu;
15172 struct die_info *target_die;
15173 struct attribute *target_attr;
15174
15175 target_die = follow_die_ref (die, attr, &target_cu);
15176 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
15177 if (target_attr == NULL)
15178 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
15179 target_cu);
15180 if (target_attr == NULL)
15181 return 0;
15182
15183 switch (target_attr->name)
15184 {
15185 case DW_AT_location:
15186 if (attr_form_is_section_offset (target_attr))
15187 {
15188 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15189 baton->referenced_type = die_type (target_die, target_cu);
15190 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
15191 prop->data.baton = baton;
15192 prop->kind = PROP_LOCLIST;
15193 gdb_assert (prop->data.baton != NULL);
15194 }
15195 else if (attr_form_is_block (target_attr))
15196 {
15197 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15198 baton->referenced_type = die_type (target_die, target_cu);
15199 baton->locexpr.per_cu = cu->per_cu;
15200 baton->locexpr.size = DW_BLOCK (target_attr)->size;
15201 baton->locexpr.data = DW_BLOCK (target_attr)->data;
15202 prop->data.baton = baton;
15203 prop->kind = PROP_LOCEXPR;
15204 gdb_assert (prop->data.baton != NULL);
15205 }
15206 else
15207 {
15208 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15209 "dynamic property");
15210 return 0;
15211 }
15212 break;
15213 case DW_AT_data_member_location:
15214 {
15215 LONGEST offset;
15216
15217 if (!handle_data_member_location (target_die, target_cu,
15218 &offset))
15219 return 0;
15220
15221 baton = XOBNEW (obstack, struct dwarf2_property_baton);
15222 baton->referenced_type = read_type_die (target_die->parent,
15223 target_cu);
15224 baton->offset_info.offset = offset;
15225 baton->offset_info.type = die_type (target_die, target_cu);
15226 prop->data.baton = baton;
15227 prop->kind = PROP_ADDR_OFFSET;
15228 break;
15229 }
15230 }
15231 }
15232 else if (attr_form_is_constant (attr))
15233 {
15234 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
15235 prop->kind = PROP_CONST;
15236 }
15237 else
15238 {
15239 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
15240 dwarf2_name (die, cu));
15241 return 0;
15242 }
15243
15244 return 1;
15245 }
15246
15247 /* Read the given DW_AT_subrange DIE. */
15248
15249 static struct type *
15250 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
15251 {
15252 struct type *base_type, *orig_base_type;
15253 struct type *range_type;
15254 struct attribute *attr;
15255 struct dynamic_prop low, high;
15256 int low_default_is_valid;
15257 int high_bound_is_count = 0;
15258 const char *name;
15259 LONGEST negative_mask;
15260
15261 orig_base_type = die_type (die, cu);
15262 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
15263 whereas the real type might be. So, we use ORIG_BASE_TYPE when
15264 creating the range type, but we use the result of check_typedef
15265 when examining properties of the type. */
15266 base_type = check_typedef (orig_base_type);
15267
15268 /* The die_type call above may have already set the type for this DIE. */
15269 range_type = get_die_type (die, cu);
15270 if (range_type)
15271 return range_type;
15272
15273 low.kind = PROP_CONST;
15274 high.kind = PROP_CONST;
15275 high.data.const_val = 0;
15276
15277 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
15278 omitting DW_AT_lower_bound. */
15279 switch (cu->language)
15280 {
15281 case language_c:
15282 case language_cplus:
15283 low.data.const_val = 0;
15284 low_default_is_valid = 1;
15285 break;
15286 case language_fortran:
15287 low.data.const_val = 1;
15288 low_default_is_valid = 1;
15289 break;
15290 case language_d:
15291 case language_objc:
15292 case language_rust:
15293 low.data.const_val = 0;
15294 low_default_is_valid = (cu->header.version >= 4);
15295 break;
15296 case language_ada:
15297 case language_m2:
15298 case language_pascal:
15299 low.data.const_val = 1;
15300 low_default_is_valid = (cu->header.version >= 4);
15301 break;
15302 default:
15303 low.data.const_val = 0;
15304 low_default_is_valid = 0;
15305 break;
15306 }
15307
15308 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
15309 if (attr)
15310 attr_to_dynamic_prop (attr, die, cu, &low);
15311 else if (!low_default_is_valid)
15312 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
15313 "- DIE at 0x%x [in module %s]"),
15314 die->offset.sect_off, objfile_name (cu->objfile));
15315
15316 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
15317 if (!attr_to_dynamic_prop (attr, die, cu, &high))
15318 {
15319 attr = dwarf2_attr (die, DW_AT_count, cu);
15320 if (attr_to_dynamic_prop (attr, die, cu, &high))
15321 {
15322 /* If bounds are constant do the final calculation here. */
15323 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
15324 high.data.const_val = low.data.const_val + high.data.const_val - 1;
15325 else
15326 high_bound_is_count = 1;
15327 }
15328 }
15329
15330 /* Dwarf-2 specifications explicitly allows to create subrange types
15331 without specifying a base type.
15332 In that case, the base type must be set to the type of
15333 the lower bound, upper bound or count, in that order, if any of these
15334 three attributes references an object that has a type.
15335 If no base type is found, the Dwarf-2 specifications say that
15336 a signed integer type of size equal to the size of an address should
15337 be used.
15338 For the following C code: `extern char gdb_int [];'
15339 GCC produces an empty range DIE.
15340 FIXME: muller/2010-05-28: Possible references to object for low bound,
15341 high bound or count are not yet handled by this code. */
15342 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
15343 {
15344 struct objfile *objfile = cu->objfile;
15345 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15346 int addr_size = gdbarch_addr_bit (gdbarch) /8;
15347 struct type *int_type = objfile_type (objfile)->builtin_int;
15348
15349 /* Test "int", "long int", and "long long int" objfile types,
15350 and select the first one having a size above or equal to the
15351 architecture address size. */
15352 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15353 base_type = int_type;
15354 else
15355 {
15356 int_type = objfile_type (objfile)->builtin_long;
15357 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15358 base_type = int_type;
15359 else
15360 {
15361 int_type = objfile_type (objfile)->builtin_long_long;
15362 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
15363 base_type = int_type;
15364 }
15365 }
15366 }
15367
15368 /* Normally, the DWARF producers are expected to use a signed
15369 constant form (Eg. DW_FORM_sdata) to express negative bounds.
15370 But this is unfortunately not always the case, as witnessed
15371 with GCC, for instance, where the ambiguous DW_FORM_dataN form
15372 is used instead. To work around that ambiguity, we treat
15373 the bounds as signed, and thus sign-extend their values, when
15374 the base type is signed. */
15375 negative_mask =
15376 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
15377 if (low.kind == PROP_CONST
15378 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
15379 low.data.const_val |= negative_mask;
15380 if (high.kind == PROP_CONST
15381 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
15382 high.data.const_val |= negative_mask;
15383
15384 range_type = create_range_type (NULL, orig_base_type, &low, &high);
15385
15386 if (high_bound_is_count)
15387 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
15388
15389 /* Ada expects an empty array on no boundary attributes. */
15390 if (attr == NULL && cu->language != language_ada)
15391 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
15392
15393 name = dwarf2_name (die, cu);
15394 if (name)
15395 TYPE_NAME (range_type) = name;
15396
15397 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15398 if (attr)
15399 TYPE_LENGTH (range_type) = DW_UNSND (attr);
15400
15401 set_die_type (die, range_type, cu);
15402
15403 /* set_die_type should be already done. */
15404 set_descriptive_type (range_type, die, cu);
15405
15406 return range_type;
15407 }
15408
15409 static struct type *
15410 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
15411 {
15412 struct type *type;
15413
15414 /* For now, we only support the C meaning of an unspecified type: void. */
15415
15416 type = init_type (cu->objfile, TYPE_CODE_VOID, 0, NULL);
15417 TYPE_NAME (type) = dwarf2_name (die, cu);
15418
15419 return set_die_type (die, type, cu);
15420 }
15421
15422 /* Read a single die and all its descendents. Set the die's sibling
15423 field to NULL; set other fields in the die correctly, and set all
15424 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
15425 location of the info_ptr after reading all of those dies. PARENT
15426 is the parent of the die in question. */
15427
15428 static struct die_info *
15429 read_die_and_children (const struct die_reader_specs *reader,
15430 const gdb_byte *info_ptr,
15431 const gdb_byte **new_info_ptr,
15432 struct die_info *parent)
15433 {
15434 struct die_info *die;
15435 const gdb_byte *cur_ptr;
15436 int has_children;
15437
15438 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
15439 if (die == NULL)
15440 {
15441 *new_info_ptr = cur_ptr;
15442 return NULL;
15443 }
15444 store_in_ref_table (die, reader->cu);
15445
15446 if (has_children)
15447 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
15448 else
15449 {
15450 die->child = NULL;
15451 *new_info_ptr = cur_ptr;
15452 }
15453
15454 die->sibling = NULL;
15455 die->parent = parent;
15456 return die;
15457 }
15458
15459 /* Read a die, all of its descendents, and all of its siblings; set
15460 all of the fields of all of the dies correctly. Arguments are as
15461 in read_die_and_children. */
15462
15463 static struct die_info *
15464 read_die_and_siblings_1 (const struct die_reader_specs *reader,
15465 const gdb_byte *info_ptr,
15466 const gdb_byte **new_info_ptr,
15467 struct die_info *parent)
15468 {
15469 struct die_info *first_die, *last_sibling;
15470 const gdb_byte *cur_ptr;
15471
15472 cur_ptr = info_ptr;
15473 first_die = last_sibling = NULL;
15474
15475 while (1)
15476 {
15477 struct die_info *die
15478 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
15479
15480 if (die == NULL)
15481 {
15482 *new_info_ptr = cur_ptr;
15483 return first_die;
15484 }
15485
15486 if (!first_die)
15487 first_die = die;
15488 else
15489 last_sibling->sibling = die;
15490
15491 last_sibling = die;
15492 }
15493 }
15494
15495 /* Read a die, all of its descendents, and all of its siblings; set
15496 all of the fields of all of the dies correctly. Arguments are as
15497 in read_die_and_children.
15498 This the main entry point for reading a DIE and all its children. */
15499
15500 static struct die_info *
15501 read_die_and_siblings (const struct die_reader_specs *reader,
15502 const gdb_byte *info_ptr,
15503 const gdb_byte **new_info_ptr,
15504 struct die_info *parent)
15505 {
15506 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
15507 new_info_ptr, parent);
15508
15509 if (dwarf_die_debug)
15510 {
15511 fprintf_unfiltered (gdb_stdlog,
15512 "Read die from %s@0x%x of %s:\n",
15513 get_section_name (reader->die_section),
15514 (unsigned) (info_ptr - reader->die_section->buffer),
15515 bfd_get_filename (reader->abfd));
15516 dump_die (die, dwarf_die_debug);
15517 }
15518
15519 return die;
15520 }
15521
15522 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
15523 attributes.
15524 The caller is responsible for filling in the extra attributes
15525 and updating (*DIEP)->num_attrs.
15526 Set DIEP to point to a newly allocated die with its information,
15527 except for its child, sibling, and parent fields.
15528 Set HAS_CHILDREN to tell whether the die has children or not. */
15529
15530 static const gdb_byte *
15531 read_full_die_1 (const struct die_reader_specs *reader,
15532 struct die_info **diep, const gdb_byte *info_ptr,
15533 int *has_children, int num_extra_attrs)
15534 {
15535 unsigned int abbrev_number, bytes_read, i;
15536 sect_offset offset;
15537 struct abbrev_info *abbrev;
15538 struct die_info *die;
15539 struct dwarf2_cu *cu = reader->cu;
15540 bfd *abfd = reader->abfd;
15541
15542 offset.sect_off = info_ptr - reader->buffer;
15543 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15544 info_ptr += bytes_read;
15545 if (!abbrev_number)
15546 {
15547 *diep = NULL;
15548 *has_children = 0;
15549 return info_ptr;
15550 }
15551
15552 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
15553 if (!abbrev)
15554 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
15555 abbrev_number,
15556 bfd_get_filename (abfd));
15557
15558 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
15559 die->offset = offset;
15560 die->tag = abbrev->tag;
15561 die->abbrev = abbrev_number;
15562
15563 /* Make the result usable.
15564 The caller needs to update num_attrs after adding the extra
15565 attributes. */
15566 die->num_attrs = abbrev->num_attrs;
15567
15568 for (i = 0; i < abbrev->num_attrs; ++i)
15569 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
15570 info_ptr);
15571
15572 *diep = die;
15573 *has_children = abbrev->has_children;
15574 return info_ptr;
15575 }
15576
15577 /* Read a die and all its attributes.
15578 Set DIEP to point to a newly allocated die with its information,
15579 except for its child, sibling, and parent fields.
15580 Set HAS_CHILDREN to tell whether the die has children or not. */
15581
15582 static const gdb_byte *
15583 read_full_die (const struct die_reader_specs *reader,
15584 struct die_info **diep, const gdb_byte *info_ptr,
15585 int *has_children)
15586 {
15587 const gdb_byte *result;
15588
15589 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
15590
15591 if (dwarf_die_debug)
15592 {
15593 fprintf_unfiltered (gdb_stdlog,
15594 "Read die from %s@0x%x of %s:\n",
15595 get_section_name (reader->die_section),
15596 (unsigned) (info_ptr - reader->die_section->buffer),
15597 bfd_get_filename (reader->abfd));
15598 dump_die (*diep, dwarf_die_debug);
15599 }
15600
15601 return result;
15602 }
15603 \f
15604 /* Abbreviation tables.
15605
15606 In DWARF version 2, the description of the debugging information is
15607 stored in a separate .debug_abbrev section. Before we read any
15608 dies from a section we read in all abbreviations and install them
15609 in a hash table. */
15610
15611 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
15612
15613 static struct abbrev_info *
15614 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
15615 {
15616 struct abbrev_info *abbrev;
15617
15618 abbrev = XOBNEW (&abbrev_table->abbrev_obstack, struct abbrev_info);
15619 memset (abbrev, 0, sizeof (struct abbrev_info));
15620
15621 return abbrev;
15622 }
15623
15624 /* Add an abbreviation to the table. */
15625
15626 static void
15627 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
15628 unsigned int abbrev_number,
15629 struct abbrev_info *abbrev)
15630 {
15631 unsigned int hash_number;
15632
15633 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15634 abbrev->next = abbrev_table->abbrevs[hash_number];
15635 abbrev_table->abbrevs[hash_number] = abbrev;
15636 }
15637
15638 /* Look up an abbrev in the table.
15639 Returns NULL if the abbrev is not found. */
15640
15641 static struct abbrev_info *
15642 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
15643 unsigned int abbrev_number)
15644 {
15645 unsigned int hash_number;
15646 struct abbrev_info *abbrev;
15647
15648 hash_number = abbrev_number % ABBREV_HASH_SIZE;
15649 abbrev = abbrev_table->abbrevs[hash_number];
15650
15651 while (abbrev)
15652 {
15653 if (abbrev->number == abbrev_number)
15654 return abbrev;
15655 abbrev = abbrev->next;
15656 }
15657 return NULL;
15658 }
15659
15660 /* Read in an abbrev table. */
15661
15662 static struct abbrev_table *
15663 abbrev_table_read_table (struct dwarf2_section_info *section,
15664 sect_offset offset)
15665 {
15666 struct objfile *objfile = dwarf2_per_objfile->objfile;
15667 bfd *abfd = get_section_bfd_owner (section);
15668 struct abbrev_table *abbrev_table;
15669 const gdb_byte *abbrev_ptr;
15670 struct abbrev_info *cur_abbrev;
15671 unsigned int abbrev_number, bytes_read, abbrev_name;
15672 unsigned int abbrev_form;
15673 struct attr_abbrev *cur_attrs;
15674 unsigned int allocated_attrs;
15675
15676 abbrev_table = XNEW (struct abbrev_table);
15677 abbrev_table->offset = offset;
15678 obstack_init (&abbrev_table->abbrev_obstack);
15679 abbrev_table->abbrevs =
15680 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct abbrev_info *,
15681 ABBREV_HASH_SIZE);
15682 memset (abbrev_table->abbrevs, 0,
15683 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
15684
15685 dwarf2_read_section (objfile, section);
15686 abbrev_ptr = section->buffer + offset.sect_off;
15687 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15688 abbrev_ptr += bytes_read;
15689
15690 allocated_attrs = ATTR_ALLOC_CHUNK;
15691 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
15692
15693 /* Loop until we reach an abbrev number of 0. */
15694 while (abbrev_number)
15695 {
15696 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
15697
15698 /* read in abbrev header */
15699 cur_abbrev->number = abbrev_number;
15700 cur_abbrev->tag
15701 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15702 abbrev_ptr += bytes_read;
15703 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
15704 abbrev_ptr += 1;
15705
15706 /* now read in declarations */
15707 for (;;)
15708 {
15709 LONGEST implicit_const;
15710
15711 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15712 abbrev_ptr += bytes_read;
15713 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15714 abbrev_ptr += bytes_read;
15715 if (abbrev_form == DW_FORM_implicit_const)
15716 {
15717 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
15718 &bytes_read);
15719 abbrev_ptr += bytes_read;
15720 }
15721 else
15722 {
15723 /* Initialize it due to a false compiler warning. */
15724 implicit_const = -1;
15725 }
15726
15727 if (abbrev_name == 0)
15728 break;
15729
15730 if (cur_abbrev->num_attrs == allocated_attrs)
15731 {
15732 allocated_attrs += ATTR_ALLOC_CHUNK;
15733 cur_attrs
15734 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
15735 }
15736
15737 cur_attrs[cur_abbrev->num_attrs].name
15738 = (enum dwarf_attribute) abbrev_name;
15739 cur_attrs[cur_abbrev->num_attrs].form
15740 = (enum dwarf_form) abbrev_form;
15741 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
15742 ++cur_abbrev->num_attrs;
15743 }
15744
15745 cur_abbrev->attrs =
15746 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
15747 cur_abbrev->num_attrs);
15748 memcpy (cur_abbrev->attrs, cur_attrs,
15749 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
15750
15751 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
15752
15753 /* Get next abbreviation.
15754 Under Irix6 the abbreviations for a compilation unit are not
15755 always properly terminated with an abbrev number of 0.
15756 Exit loop if we encounter an abbreviation which we have
15757 already read (which means we are about to read the abbreviations
15758 for the next compile unit) or if the end of the abbreviation
15759 table is reached. */
15760 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
15761 break;
15762 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
15763 abbrev_ptr += bytes_read;
15764 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
15765 break;
15766 }
15767
15768 xfree (cur_attrs);
15769 return abbrev_table;
15770 }
15771
15772 /* Free the resources held by ABBREV_TABLE. */
15773
15774 static void
15775 abbrev_table_free (struct abbrev_table *abbrev_table)
15776 {
15777 obstack_free (&abbrev_table->abbrev_obstack, NULL);
15778 xfree (abbrev_table);
15779 }
15780
15781 /* Same as abbrev_table_free but as a cleanup.
15782 We pass in a pointer to the pointer to the table so that we can
15783 set the pointer to NULL when we're done. It also simplifies
15784 build_type_psymtabs_1. */
15785
15786 static void
15787 abbrev_table_free_cleanup (void *table_ptr)
15788 {
15789 struct abbrev_table **abbrev_table_ptr = (struct abbrev_table **) table_ptr;
15790
15791 if (*abbrev_table_ptr != NULL)
15792 abbrev_table_free (*abbrev_table_ptr);
15793 *abbrev_table_ptr = NULL;
15794 }
15795
15796 /* Read the abbrev table for CU from ABBREV_SECTION. */
15797
15798 static void
15799 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
15800 struct dwarf2_section_info *abbrev_section)
15801 {
15802 cu->abbrev_table =
15803 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
15804 }
15805
15806 /* Release the memory used by the abbrev table for a compilation unit. */
15807
15808 static void
15809 dwarf2_free_abbrev_table (void *ptr_to_cu)
15810 {
15811 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr_to_cu;
15812
15813 if (cu->abbrev_table != NULL)
15814 abbrev_table_free (cu->abbrev_table);
15815 /* Set this to NULL so that we SEGV if we try to read it later,
15816 and also because free_comp_unit verifies this is NULL. */
15817 cu->abbrev_table = NULL;
15818 }
15819 \f
15820 /* Returns nonzero if TAG represents a type that we might generate a partial
15821 symbol for. */
15822
15823 static int
15824 is_type_tag_for_partial (int tag)
15825 {
15826 switch (tag)
15827 {
15828 #if 0
15829 /* Some types that would be reasonable to generate partial symbols for,
15830 that we don't at present. */
15831 case DW_TAG_array_type:
15832 case DW_TAG_file_type:
15833 case DW_TAG_ptr_to_member_type:
15834 case DW_TAG_set_type:
15835 case DW_TAG_string_type:
15836 case DW_TAG_subroutine_type:
15837 #endif
15838 case DW_TAG_base_type:
15839 case DW_TAG_class_type:
15840 case DW_TAG_interface_type:
15841 case DW_TAG_enumeration_type:
15842 case DW_TAG_structure_type:
15843 case DW_TAG_subrange_type:
15844 case DW_TAG_typedef:
15845 case DW_TAG_union_type:
15846 return 1;
15847 default:
15848 return 0;
15849 }
15850 }
15851
15852 /* Load all DIEs that are interesting for partial symbols into memory. */
15853
15854 static struct partial_die_info *
15855 load_partial_dies (const struct die_reader_specs *reader,
15856 const gdb_byte *info_ptr, int building_psymtab)
15857 {
15858 struct dwarf2_cu *cu = reader->cu;
15859 struct objfile *objfile = cu->objfile;
15860 struct partial_die_info *part_die;
15861 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
15862 struct abbrev_info *abbrev;
15863 unsigned int bytes_read;
15864 unsigned int load_all = 0;
15865 int nesting_level = 1;
15866
15867 parent_die = NULL;
15868 last_die = NULL;
15869
15870 gdb_assert (cu->per_cu != NULL);
15871 if (cu->per_cu->load_all_dies)
15872 load_all = 1;
15873
15874 cu->partial_dies
15875 = htab_create_alloc_ex (cu->header.length / 12,
15876 partial_die_hash,
15877 partial_die_eq,
15878 NULL,
15879 &cu->comp_unit_obstack,
15880 hashtab_obstack_allocate,
15881 dummy_obstack_deallocate);
15882
15883 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
15884
15885 while (1)
15886 {
15887 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
15888
15889 /* A NULL abbrev means the end of a series of children. */
15890 if (abbrev == NULL)
15891 {
15892 if (--nesting_level == 0)
15893 {
15894 /* PART_DIE was probably the last thing allocated on the
15895 comp_unit_obstack, so we could call obstack_free
15896 here. We don't do that because the waste is small,
15897 and will be cleaned up when we're done with this
15898 compilation unit. This way, we're also more robust
15899 against other users of the comp_unit_obstack. */
15900 return first_die;
15901 }
15902 info_ptr += bytes_read;
15903 last_die = parent_die;
15904 parent_die = parent_die->die_parent;
15905 continue;
15906 }
15907
15908 /* Check for template arguments. We never save these; if
15909 they're seen, we just mark the parent, and go on our way. */
15910 if (parent_die != NULL
15911 && cu->language == language_cplus
15912 && (abbrev->tag == DW_TAG_template_type_param
15913 || abbrev->tag == DW_TAG_template_value_param))
15914 {
15915 parent_die->has_template_arguments = 1;
15916
15917 if (!load_all)
15918 {
15919 /* We don't need a partial DIE for the template argument. */
15920 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15921 continue;
15922 }
15923 }
15924
15925 /* We only recurse into c++ subprograms looking for template arguments.
15926 Skip their other children. */
15927 if (!load_all
15928 && cu->language == language_cplus
15929 && parent_die != NULL
15930 && parent_die->tag == DW_TAG_subprogram)
15931 {
15932 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15933 continue;
15934 }
15935
15936 /* Check whether this DIE is interesting enough to save. Normally
15937 we would not be interested in members here, but there may be
15938 later variables referencing them via DW_AT_specification (for
15939 static members). */
15940 if (!load_all
15941 && !is_type_tag_for_partial (abbrev->tag)
15942 && abbrev->tag != DW_TAG_constant
15943 && abbrev->tag != DW_TAG_enumerator
15944 && abbrev->tag != DW_TAG_subprogram
15945 && abbrev->tag != DW_TAG_lexical_block
15946 && abbrev->tag != DW_TAG_variable
15947 && abbrev->tag != DW_TAG_namespace
15948 && abbrev->tag != DW_TAG_module
15949 && abbrev->tag != DW_TAG_member
15950 && abbrev->tag != DW_TAG_imported_unit
15951 && abbrev->tag != DW_TAG_imported_declaration)
15952 {
15953 /* Otherwise we skip to the next sibling, if any. */
15954 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
15955 continue;
15956 }
15957
15958 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
15959 info_ptr);
15960
15961 /* This two-pass algorithm for processing partial symbols has a
15962 high cost in cache pressure. Thus, handle some simple cases
15963 here which cover the majority of C partial symbols. DIEs
15964 which neither have specification tags in them, nor could have
15965 specification tags elsewhere pointing at them, can simply be
15966 processed and discarded.
15967
15968 This segment is also optional; scan_partial_symbols and
15969 add_partial_symbol will handle these DIEs if we chain
15970 them in normally. When compilers which do not emit large
15971 quantities of duplicate debug information are more common,
15972 this code can probably be removed. */
15973
15974 /* Any complete simple types at the top level (pretty much all
15975 of them, for a language without namespaces), can be processed
15976 directly. */
15977 if (parent_die == NULL
15978 && part_die->has_specification == 0
15979 && part_die->is_declaration == 0
15980 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
15981 || part_die->tag == DW_TAG_base_type
15982 || part_die->tag == DW_TAG_subrange_type))
15983 {
15984 if (building_psymtab && part_die->name != NULL)
15985 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
15986 VAR_DOMAIN, LOC_TYPEDEF,
15987 &objfile->static_psymbols,
15988 0, cu->language, objfile);
15989 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
15990 continue;
15991 }
15992
15993 /* The exception for DW_TAG_typedef with has_children above is
15994 a workaround of GCC PR debug/47510. In the case of this complaint
15995 type_name_no_tag_or_error will error on such types later.
15996
15997 GDB skipped children of DW_TAG_typedef by the shortcut above and then
15998 it could not find the child DIEs referenced later, this is checked
15999 above. In correct DWARF DW_TAG_typedef should have no children. */
16000
16001 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
16002 complaint (&symfile_complaints,
16003 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
16004 "- DIE at 0x%x [in module %s]"),
16005 part_die->offset.sect_off, objfile_name (objfile));
16006
16007 /* If we're at the second level, and we're an enumerator, and
16008 our parent has no specification (meaning possibly lives in a
16009 namespace elsewhere), then we can add the partial symbol now
16010 instead of queueing it. */
16011 if (part_die->tag == DW_TAG_enumerator
16012 && parent_die != NULL
16013 && parent_die->die_parent == NULL
16014 && parent_die->tag == DW_TAG_enumeration_type
16015 && parent_die->has_specification == 0)
16016 {
16017 if (part_die->name == NULL)
16018 complaint (&symfile_complaints,
16019 _("malformed enumerator DIE ignored"));
16020 else if (building_psymtab)
16021 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
16022 VAR_DOMAIN, LOC_CONST,
16023 cu->language == language_cplus
16024 ? &objfile->global_psymbols
16025 : &objfile->static_psymbols,
16026 0, cu->language, objfile);
16027
16028 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
16029 continue;
16030 }
16031
16032 /* We'll save this DIE so link it in. */
16033 part_die->die_parent = parent_die;
16034 part_die->die_sibling = NULL;
16035 part_die->die_child = NULL;
16036
16037 if (last_die && last_die == parent_die)
16038 last_die->die_child = part_die;
16039 else if (last_die)
16040 last_die->die_sibling = part_die;
16041
16042 last_die = part_die;
16043
16044 if (first_die == NULL)
16045 first_die = part_die;
16046
16047 /* Maybe add the DIE to the hash table. Not all DIEs that we
16048 find interesting need to be in the hash table, because we
16049 also have the parent/sibling/child chains; only those that we
16050 might refer to by offset later during partial symbol reading.
16051
16052 For now this means things that might have be the target of a
16053 DW_AT_specification, DW_AT_abstract_origin, or
16054 DW_AT_extension. DW_AT_extension will refer only to
16055 namespaces; DW_AT_abstract_origin refers to functions (and
16056 many things under the function DIE, but we do not recurse
16057 into function DIEs during partial symbol reading) and
16058 possibly variables as well; DW_AT_specification refers to
16059 declarations. Declarations ought to have the DW_AT_declaration
16060 flag. It happens that GCC forgets to put it in sometimes, but
16061 only for functions, not for types.
16062
16063 Adding more things than necessary to the hash table is harmless
16064 except for the performance cost. Adding too few will result in
16065 wasted time in find_partial_die, when we reread the compilation
16066 unit with load_all_dies set. */
16067
16068 if (load_all
16069 || abbrev->tag == DW_TAG_constant
16070 || abbrev->tag == DW_TAG_subprogram
16071 || abbrev->tag == DW_TAG_variable
16072 || abbrev->tag == DW_TAG_namespace
16073 || part_die->is_declaration)
16074 {
16075 void **slot;
16076
16077 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
16078 part_die->offset.sect_off, INSERT);
16079 *slot = part_die;
16080 }
16081
16082 part_die = XOBNEW (&cu->comp_unit_obstack, struct partial_die_info);
16083
16084 /* For some DIEs we want to follow their children (if any). For C
16085 we have no reason to follow the children of structures; for other
16086 languages we have to, so that we can get at method physnames
16087 to infer fully qualified class names, for DW_AT_specification,
16088 and for C++ template arguments. For C++, we also look one level
16089 inside functions to find template arguments (if the name of the
16090 function does not already contain the template arguments).
16091
16092 For Ada, we need to scan the children of subprograms and lexical
16093 blocks as well because Ada allows the definition of nested
16094 entities that could be interesting for the debugger, such as
16095 nested subprograms for instance. */
16096 if (last_die->has_children
16097 && (load_all
16098 || last_die->tag == DW_TAG_namespace
16099 || last_die->tag == DW_TAG_module
16100 || last_die->tag == DW_TAG_enumeration_type
16101 || (cu->language == language_cplus
16102 && last_die->tag == DW_TAG_subprogram
16103 && (last_die->name == NULL
16104 || strchr (last_die->name, '<') == NULL))
16105 || (cu->language != language_c
16106 && (last_die->tag == DW_TAG_class_type
16107 || last_die->tag == DW_TAG_interface_type
16108 || last_die->tag == DW_TAG_structure_type
16109 || last_die->tag == DW_TAG_union_type))
16110 || (cu->language == language_ada
16111 && (last_die->tag == DW_TAG_subprogram
16112 || last_die->tag == DW_TAG_lexical_block))))
16113 {
16114 nesting_level++;
16115 parent_die = last_die;
16116 continue;
16117 }
16118
16119 /* Otherwise we skip to the next sibling, if any. */
16120 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
16121
16122 /* Back to the top, do it again. */
16123 }
16124 }
16125
16126 /* Read a minimal amount of information into the minimal die structure. */
16127
16128 static const gdb_byte *
16129 read_partial_die (const struct die_reader_specs *reader,
16130 struct partial_die_info *part_die,
16131 struct abbrev_info *abbrev, unsigned int abbrev_len,
16132 const gdb_byte *info_ptr)
16133 {
16134 struct dwarf2_cu *cu = reader->cu;
16135 struct objfile *objfile = cu->objfile;
16136 const gdb_byte *buffer = reader->buffer;
16137 unsigned int i;
16138 struct attribute attr;
16139 int has_low_pc_attr = 0;
16140 int has_high_pc_attr = 0;
16141 int high_pc_relative = 0;
16142
16143 memset (part_die, 0, sizeof (struct partial_die_info));
16144
16145 part_die->offset.sect_off = info_ptr - buffer;
16146
16147 info_ptr += abbrev_len;
16148
16149 if (abbrev == NULL)
16150 return info_ptr;
16151
16152 part_die->tag = abbrev->tag;
16153 part_die->has_children = abbrev->has_children;
16154
16155 for (i = 0; i < abbrev->num_attrs; ++i)
16156 {
16157 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
16158
16159 /* Store the data if it is of an attribute we want to keep in a
16160 partial symbol table. */
16161 switch (attr.name)
16162 {
16163 case DW_AT_name:
16164 switch (part_die->tag)
16165 {
16166 case DW_TAG_compile_unit:
16167 case DW_TAG_partial_unit:
16168 case DW_TAG_type_unit:
16169 /* Compilation units have a DW_AT_name that is a filename, not
16170 a source language identifier. */
16171 case DW_TAG_enumeration_type:
16172 case DW_TAG_enumerator:
16173 /* These tags always have simple identifiers already; no need
16174 to canonicalize them. */
16175 part_die->name = DW_STRING (&attr);
16176 break;
16177 default:
16178 part_die->name
16179 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
16180 &objfile->per_bfd->storage_obstack);
16181 break;
16182 }
16183 break;
16184 case DW_AT_linkage_name:
16185 case DW_AT_MIPS_linkage_name:
16186 /* Note that both forms of linkage name might appear. We
16187 assume they will be the same, and we only store the last
16188 one we see. */
16189 if (cu->language == language_ada)
16190 part_die->name = DW_STRING (&attr);
16191 part_die->linkage_name = DW_STRING (&attr);
16192 break;
16193 case DW_AT_low_pc:
16194 has_low_pc_attr = 1;
16195 part_die->lowpc = attr_value_as_address (&attr);
16196 break;
16197 case DW_AT_high_pc:
16198 has_high_pc_attr = 1;
16199 part_die->highpc = attr_value_as_address (&attr);
16200 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
16201 high_pc_relative = 1;
16202 break;
16203 case DW_AT_location:
16204 /* Support the .debug_loc offsets. */
16205 if (attr_form_is_block (&attr))
16206 {
16207 part_die->d.locdesc = DW_BLOCK (&attr);
16208 }
16209 else if (attr_form_is_section_offset (&attr))
16210 {
16211 dwarf2_complex_location_expr_complaint ();
16212 }
16213 else
16214 {
16215 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16216 "partial symbol information");
16217 }
16218 break;
16219 case DW_AT_external:
16220 part_die->is_external = DW_UNSND (&attr);
16221 break;
16222 case DW_AT_declaration:
16223 part_die->is_declaration = DW_UNSND (&attr);
16224 break;
16225 case DW_AT_type:
16226 part_die->has_type = 1;
16227 break;
16228 case DW_AT_abstract_origin:
16229 case DW_AT_specification:
16230 case DW_AT_extension:
16231 part_die->has_specification = 1;
16232 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
16233 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16234 || cu->per_cu->is_dwz);
16235 break;
16236 case DW_AT_sibling:
16237 /* Ignore absolute siblings, they might point outside of
16238 the current compile unit. */
16239 if (attr.form == DW_FORM_ref_addr)
16240 complaint (&symfile_complaints,
16241 _("ignoring absolute DW_AT_sibling"));
16242 else
16243 {
16244 unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
16245 const gdb_byte *sibling_ptr = buffer + off;
16246
16247 if (sibling_ptr < info_ptr)
16248 complaint (&symfile_complaints,
16249 _("DW_AT_sibling points backwards"));
16250 else if (sibling_ptr > reader->buffer_end)
16251 dwarf2_section_buffer_overflow_complaint (reader->die_section);
16252 else
16253 part_die->sibling = sibling_ptr;
16254 }
16255 break;
16256 case DW_AT_byte_size:
16257 part_die->has_byte_size = 1;
16258 break;
16259 case DW_AT_const_value:
16260 part_die->has_const_value = 1;
16261 break;
16262 case DW_AT_calling_convention:
16263 /* DWARF doesn't provide a way to identify a program's source-level
16264 entry point. DW_AT_calling_convention attributes are only meant
16265 to describe functions' calling conventions.
16266
16267 However, because it's a necessary piece of information in
16268 Fortran, and before DWARF 4 DW_CC_program was the only
16269 piece of debugging information whose definition refers to
16270 a 'main program' at all, several compilers marked Fortran
16271 main programs with DW_CC_program --- even when those
16272 functions use the standard calling conventions.
16273
16274 Although DWARF now specifies a way to provide this
16275 information, we support this practice for backward
16276 compatibility. */
16277 if (DW_UNSND (&attr) == DW_CC_program
16278 && cu->language == language_fortran)
16279 part_die->main_subprogram = 1;
16280 break;
16281 case DW_AT_inline:
16282 if (DW_UNSND (&attr) == DW_INL_inlined
16283 || DW_UNSND (&attr) == DW_INL_declared_inlined)
16284 part_die->may_be_inlined = 1;
16285 break;
16286
16287 case DW_AT_import:
16288 if (part_die->tag == DW_TAG_imported_unit)
16289 {
16290 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
16291 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
16292 || cu->per_cu->is_dwz);
16293 }
16294 break;
16295
16296 case DW_AT_main_subprogram:
16297 part_die->main_subprogram = DW_UNSND (&attr);
16298 break;
16299
16300 default:
16301 break;
16302 }
16303 }
16304
16305 if (high_pc_relative)
16306 part_die->highpc += part_die->lowpc;
16307
16308 if (has_low_pc_attr && has_high_pc_attr)
16309 {
16310 /* When using the GNU linker, .gnu.linkonce. sections are used to
16311 eliminate duplicate copies of functions and vtables and such.
16312 The linker will arbitrarily choose one and discard the others.
16313 The AT_*_pc values for such functions refer to local labels in
16314 these sections. If the section from that file was discarded, the
16315 labels are not in the output, so the relocs get a value of 0.
16316 If this is a discarded function, mark the pc bounds as invalid,
16317 so that GDB will ignore it. */
16318 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
16319 {
16320 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16321
16322 complaint (&symfile_complaints,
16323 _("DW_AT_low_pc %s is zero "
16324 "for DIE at 0x%x [in module %s]"),
16325 paddress (gdbarch, part_die->lowpc),
16326 part_die->offset.sect_off, objfile_name (objfile));
16327 }
16328 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
16329 else if (part_die->lowpc >= part_die->highpc)
16330 {
16331 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16332
16333 complaint (&symfile_complaints,
16334 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
16335 "for DIE at 0x%x [in module %s]"),
16336 paddress (gdbarch, part_die->lowpc),
16337 paddress (gdbarch, part_die->highpc),
16338 part_die->offset.sect_off, objfile_name (objfile));
16339 }
16340 else
16341 part_die->has_pc_info = 1;
16342 }
16343
16344 return info_ptr;
16345 }
16346
16347 /* Find a cached partial DIE at OFFSET in CU. */
16348
16349 static struct partial_die_info *
16350 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
16351 {
16352 struct partial_die_info *lookup_die = NULL;
16353 struct partial_die_info part_die;
16354
16355 part_die.offset = offset;
16356 lookup_die = ((struct partial_die_info *)
16357 htab_find_with_hash (cu->partial_dies, &part_die,
16358 offset.sect_off));
16359
16360 return lookup_die;
16361 }
16362
16363 /* Find a partial DIE at OFFSET, which may or may not be in CU,
16364 except in the case of .debug_types DIEs which do not reference
16365 outside their CU (they do however referencing other types via
16366 DW_FORM_ref_sig8). */
16367
16368 static struct partial_die_info *
16369 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
16370 {
16371 struct objfile *objfile = cu->objfile;
16372 struct dwarf2_per_cu_data *per_cu = NULL;
16373 struct partial_die_info *pd = NULL;
16374
16375 if (offset_in_dwz == cu->per_cu->is_dwz
16376 && offset_in_cu_p (&cu->header, offset))
16377 {
16378 pd = find_partial_die_in_comp_unit (offset, cu);
16379 if (pd != NULL)
16380 return pd;
16381 /* We missed recording what we needed.
16382 Load all dies and try again. */
16383 per_cu = cu->per_cu;
16384 }
16385 else
16386 {
16387 /* TUs don't reference other CUs/TUs (except via type signatures). */
16388 if (cu->per_cu->is_debug_types)
16389 {
16390 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
16391 " external reference to offset 0x%lx [in module %s].\n"),
16392 (long) cu->header.offset.sect_off, (long) offset.sect_off,
16393 bfd_get_filename (objfile->obfd));
16394 }
16395 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
16396 objfile);
16397
16398 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
16399 load_partial_comp_unit (per_cu);
16400
16401 per_cu->cu->last_used = 0;
16402 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
16403 }
16404
16405 /* If we didn't find it, and not all dies have been loaded,
16406 load them all and try again. */
16407
16408 if (pd == NULL && per_cu->load_all_dies == 0)
16409 {
16410 per_cu->load_all_dies = 1;
16411
16412 /* This is nasty. When we reread the DIEs, somewhere up the call chain
16413 THIS_CU->cu may already be in use. So we can't just free it and
16414 replace its DIEs with the ones we read in. Instead, we leave those
16415 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
16416 and clobber THIS_CU->cu->partial_dies with the hash table for the new
16417 set. */
16418 load_partial_comp_unit (per_cu);
16419
16420 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
16421 }
16422
16423 if (pd == NULL)
16424 internal_error (__FILE__, __LINE__,
16425 _("could not find partial DIE 0x%x "
16426 "in cache [from module %s]\n"),
16427 offset.sect_off, bfd_get_filename (objfile->obfd));
16428 return pd;
16429 }
16430
16431 /* See if we can figure out if the class lives in a namespace. We do
16432 this by looking for a member function; its demangled name will
16433 contain namespace info, if there is any. */
16434
16435 static void
16436 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
16437 struct dwarf2_cu *cu)
16438 {
16439 /* NOTE: carlton/2003-10-07: Getting the info this way changes
16440 what template types look like, because the demangler
16441 frequently doesn't give the same name as the debug info. We
16442 could fix this by only using the demangled name to get the
16443 prefix (but see comment in read_structure_type). */
16444
16445 struct partial_die_info *real_pdi;
16446 struct partial_die_info *child_pdi;
16447
16448 /* If this DIE (this DIE's specification, if any) has a parent, then
16449 we should not do this. We'll prepend the parent's fully qualified
16450 name when we create the partial symbol. */
16451
16452 real_pdi = struct_pdi;
16453 while (real_pdi->has_specification)
16454 real_pdi = find_partial_die (real_pdi->spec_offset,
16455 real_pdi->spec_is_dwz, cu);
16456
16457 if (real_pdi->die_parent != NULL)
16458 return;
16459
16460 for (child_pdi = struct_pdi->die_child;
16461 child_pdi != NULL;
16462 child_pdi = child_pdi->die_sibling)
16463 {
16464 if (child_pdi->tag == DW_TAG_subprogram
16465 && child_pdi->linkage_name != NULL)
16466 {
16467 char *actual_class_name
16468 = language_class_name_from_physname (cu->language_defn,
16469 child_pdi->linkage_name);
16470 if (actual_class_name != NULL)
16471 {
16472 struct_pdi->name
16473 = ((const char *)
16474 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16475 actual_class_name,
16476 strlen (actual_class_name)));
16477 xfree (actual_class_name);
16478 }
16479 break;
16480 }
16481 }
16482 }
16483
16484 /* Adjust PART_DIE before generating a symbol for it. This function
16485 may set the is_external flag or change the DIE's name. */
16486
16487 static void
16488 fixup_partial_die (struct partial_die_info *part_die,
16489 struct dwarf2_cu *cu)
16490 {
16491 /* Once we've fixed up a die, there's no point in doing so again.
16492 This also avoids a memory leak if we were to call
16493 guess_partial_die_structure_name multiple times. */
16494 if (part_die->fixup_called)
16495 return;
16496
16497 /* If we found a reference attribute and the DIE has no name, try
16498 to find a name in the referred to DIE. */
16499
16500 if (part_die->name == NULL && part_die->has_specification)
16501 {
16502 struct partial_die_info *spec_die;
16503
16504 spec_die = find_partial_die (part_die->spec_offset,
16505 part_die->spec_is_dwz, cu);
16506
16507 fixup_partial_die (spec_die, cu);
16508
16509 if (spec_die->name)
16510 {
16511 part_die->name = spec_die->name;
16512
16513 /* Copy DW_AT_external attribute if it is set. */
16514 if (spec_die->is_external)
16515 part_die->is_external = spec_die->is_external;
16516 }
16517 }
16518
16519 /* Set default names for some unnamed DIEs. */
16520
16521 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
16522 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
16523
16524 /* If there is no parent die to provide a namespace, and there are
16525 children, see if we can determine the namespace from their linkage
16526 name. */
16527 if (cu->language == language_cplus
16528 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
16529 && part_die->die_parent == NULL
16530 && part_die->has_children
16531 && (part_die->tag == DW_TAG_class_type
16532 || part_die->tag == DW_TAG_structure_type
16533 || part_die->tag == DW_TAG_union_type))
16534 guess_partial_die_structure_name (part_die, cu);
16535
16536 /* GCC might emit a nameless struct or union that has a linkage
16537 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
16538 if (part_die->name == NULL
16539 && (part_die->tag == DW_TAG_class_type
16540 || part_die->tag == DW_TAG_interface_type
16541 || part_die->tag == DW_TAG_structure_type
16542 || part_die->tag == DW_TAG_union_type)
16543 && part_die->linkage_name != NULL)
16544 {
16545 char *demangled;
16546
16547 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
16548 if (demangled)
16549 {
16550 const char *base;
16551
16552 /* Strip any leading namespaces/classes, keep only the base name.
16553 DW_AT_name for named DIEs does not contain the prefixes. */
16554 base = strrchr (demangled, ':');
16555 if (base && base > demangled && base[-1] == ':')
16556 base++;
16557 else
16558 base = demangled;
16559
16560 part_die->name
16561 = ((const char *)
16562 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
16563 base, strlen (base)));
16564 xfree (demangled);
16565 }
16566 }
16567
16568 part_die->fixup_called = 1;
16569 }
16570
16571 /* Read an attribute value described by an attribute form. */
16572
16573 static const gdb_byte *
16574 read_attribute_value (const struct die_reader_specs *reader,
16575 struct attribute *attr, unsigned form,
16576 LONGEST implicit_const, const gdb_byte *info_ptr)
16577 {
16578 struct dwarf2_cu *cu = reader->cu;
16579 struct objfile *objfile = cu->objfile;
16580 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16581 bfd *abfd = reader->abfd;
16582 struct comp_unit_head *cu_header = &cu->header;
16583 unsigned int bytes_read;
16584 struct dwarf_block *blk;
16585
16586 attr->form = (enum dwarf_form) form;
16587 switch (form)
16588 {
16589 case DW_FORM_ref_addr:
16590 if (cu->header.version == 2)
16591 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
16592 else
16593 DW_UNSND (attr) = read_offset (abfd, info_ptr,
16594 &cu->header, &bytes_read);
16595 info_ptr += bytes_read;
16596 break;
16597 case DW_FORM_GNU_ref_alt:
16598 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16599 info_ptr += bytes_read;
16600 break;
16601 case DW_FORM_addr:
16602 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
16603 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
16604 info_ptr += bytes_read;
16605 break;
16606 case DW_FORM_block2:
16607 blk = dwarf_alloc_block (cu);
16608 blk->size = read_2_bytes (abfd, info_ptr);
16609 info_ptr += 2;
16610 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16611 info_ptr += blk->size;
16612 DW_BLOCK (attr) = blk;
16613 break;
16614 case DW_FORM_block4:
16615 blk = dwarf_alloc_block (cu);
16616 blk->size = read_4_bytes (abfd, info_ptr);
16617 info_ptr += 4;
16618 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16619 info_ptr += blk->size;
16620 DW_BLOCK (attr) = blk;
16621 break;
16622 case DW_FORM_data2:
16623 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
16624 info_ptr += 2;
16625 break;
16626 case DW_FORM_data4:
16627 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
16628 info_ptr += 4;
16629 break;
16630 case DW_FORM_data8:
16631 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
16632 info_ptr += 8;
16633 break;
16634 case DW_FORM_data16:
16635 blk = dwarf_alloc_block (cu);
16636 blk->size = 16;
16637 blk->data = read_n_bytes (abfd, info_ptr, 16);
16638 info_ptr += 16;
16639 DW_BLOCK (attr) = blk;
16640 break;
16641 case DW_FORM_sec_offset:
16642 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
16643 info_ptr += bytes_read;
16644 break;
16645 case DW_FORM_string:
16646 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
16647 DW_STRING_IS_CANONICAL (attr) = 0;
16648 info_ptr += bytes_read;
16649 break;
16650 case DW_FORM_strp:
16651 if (!cu->per_cu->is_dwz)
16652 {
16653 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
16654 &bytes_read);
16655 DW_STRING_IS_CANONICAL (attr) = 0;
16656 info_ptr += bytes_read;
16657 break;
16658 }
16659 /* FALLTHROUGH */
16660 case DW_FORM_line_strp:
16661 if (!cu->per_cu->is_dwz)
16662 {
16663 DW_STRING (attr) = read_indirect_line_string (abfd, info_ptr,
16664 cu_header, &bytes_read);
16665 DW_STRING_IS_CANONICAL (attr) = 0;
16666 info_ptr += bytes_read;
16667 break;
16668 }
16669 /* FALLTHROUGH */
16670 case DW_FORM_GNU_strp_alt:
16671 {
16672 struct dwz_file *dwz = dwarf2_get_dwz_file ();
16673 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
16674 &bytes_read);
16675
16676 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
16677 DW_STRING_IS_CANONICAL (attr) = 0;
16678 info_ptr += bytes_read;
16679 }
16680 break;
16681 case DW_FORM_exprloc:
16682 case DW_FORM_block:
16683 blk = dwarf_alloc_block (cu);
16684 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16685 info_ptr += bytes_read;
16686 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16687 info_ptr += blk->size;
16688 DW_BLOCK (attr) = blk;
16689 break;
16690 case DW_FORM_block1:
16691 blk = dwarf_alloc_block (cu);
16692 blk->size = read_1_byte (abfd, info_ptr);
16693 info_ptr += 1;
16694 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
16695 info_ptr += blk->size;
16696 DW_BLOCK (attr) = blk;
16697 break;
16698 case DW_FORM_data1:
16699 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16700 info_ptr += 1;
16701 break;
16702 case DW_FORM_flag:
16703 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
16704 info_ptr += 1;
16705 break;
16706 case DW_FORM_flag_present:
16707 DW_UNSND (attr) = 1;
16708 break;
16709 case DW_FORM_sdata:
16710 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16711 info_ptr += bytes_read;
16712 break;
16713 case DW_FORM_udata:
16714 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16715 info_ptr += bytes_read;
16716 break;
16717 case DW_FORM_ref1:
16718 DW_UNSND (attr) = (cu->header.offset.sect_off
16719 + read_1_byte (abfd, info_ptr));
16720 info_ptr += 1;
16721 break;
16722 case DW_FORM_ref2:
16723 DW_UNSND (attr) = (cu->header.offset.sect_off
16724 + read_2_bytes (abfd, info_ptr));
16725 info_ptr += 2;
16726 break;
16727 case DW_FORM_ref4:
16728 DW_UNSND (attr) = (cu->header.offset.sect_off
16729 + read_4_bytes (abfd, info_ptr));
16730 info_ptr += 4;
16731 break;
16732 case DW_FORM_ref8:
16733 DW_UNSND (attr) = (cu->header.offset.sect_off
16734 + read_8_bytes (abfd, info_ptr));
16735 info_ptr += 8;
16736 break;
16737 case DW_FORM_ref_sig8:
16738 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
16739 info_ptr += 8;
16740 break;
16741 case DW_FORM_ref_udata:
16742 DW_UNSND (attr) = (cu->header.offset.sect_off
16743 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
16744 info_ptr += bytes_read;
16745 break;
16746 case DW_FORM_indirect:
16747 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16748 info_ptr += bytes_read;
16749 if (form == DW_FORM_implicit_const)
16750 {
16751 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
16752 info_ptr += bytes_read;
16753 }
16754 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
16755 info_ptr);
16756 break;
16757 case DW_FORM_implicit_const:
16758 DW_SND (attr) = implicit_const;
16759 break;
16760 case DW_FORM_GNU_addr_index:
16761 if (reader->dwo_file == NULL)
16762 {
16763 /* For now flag a hard error.
16764 Later we can turn this into a complaint. */
16765 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16766 dwarf_form_name (form),
16767 bfd_get_filename (abfd));
16768 }
16769 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
16770 info_ptr += bytes_read;
16771 break;
16772 case DW_FORM_GNU_str_index:
16773 if (reader->dwo_file == NULL)
16774 {
16775 /* For now flag a hard error.
16776 Later we can turn this into a complaint if warranted. */
16777 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
16778 dwarf_form_name (form),
16779 bfd_get_filename (abfd));
16780 }
16781 {
16782 ULONGEST str_index =
16783 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
16784
16785 DW_STRING (attr) = read_str_index (reader, str_index);
16786 DW_STRING_IS_CANONICAL (attr) = 0;
16787 info_ptr += bytes_read;
16788 }
16789 break;
16790 default:
16791 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
16792 dwarf_form_name (form),
16793 bfd_get_filename (abfd));
16794 }
16795
16796 /* Super hack. */
16797 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
16798 attr->form = DW_FORM_GNU_ref_alt;
16799
16800 /* We have seen instances where the compiler tried to emit a byte
16801 size attribute of -1 which ended up being encoded as an unsigned
16802 0xffffffff. Although 0xffffffff is technically a valid size value,
16803 an object of this size seems pretty unlikely so we can relatively
16804 safely treat these cases as if the size attribute was invalid and
16805 treat them as zero by default. */
16806 if (attr->name == DW_AT_byte_size
16807 && form == DW_FORM_data4
16808 && DW_UNSND (attr) >= 0xffffffff)
16809 {
16810 complaint
16811 (&symfile_complaints,
16812 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
16813 hex_string (DW_UNSND (attr)));
16814 DW_UNSND (attr) = 0;
16815 }
16816
16817 return info_ptr;
16818 }
16819
16820 /* Read an attribute described by an abbreviated attribute. */
16821
16822 static const gdb_byte *
16823 read_attribute (const struct die_reader_specs *reader,
16824 struct attribute *attr, struct attr_abbrev *abbrev,
16825 const gdb_byte *info_ptr)
16826 {
16827 attr->name = abbrev->name;
16828 return read_attribute_value (reader, attr, abbrev->form,
16829 abbrev->implicit_const, info_ptr);
16830 }
16831
16832 /* Read dwarf information from a buffer. */
16833
16834 static unsigned int
16835 read_1_byte (bfd *abfd, const gdb_byte *buf)
16836 {
16837 return bfd_get_8 (abfd, buf);
16838 }
16839
16840 static int
16841 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
16842 {
16843 return bfd_get_signed_8 (abfd, buf);
16844 }
16845
16846 static unsigned int
16847 read_2_bytes (bfd *abfd, const gdb_byte *buf)
16848 {
16849 return bfd_get_16 (abfd, buf);
16850 }
16851
16852 static int
16853 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
16854 {
16855 return bfd_get_signed_16 (abfd, buf);
16856 }
16857
16858 static unsigned int
16859 read_4_bytes (bfd *abfd, const gdb_byte *buf)
16860 {
16861 return bfd_get_32 (abfd, buf);
16862 }
16863
16864 static int
16865 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
16866 {
16867 return bfd_get_signed_32 (abfd, buf);
16868 }
16869
16870 static ULONGEST
16871 read_8_bytes (bfd *abfd, const gdb_byte *buf)
16872 {
16873 return bfd_get_64 (abfd, buf);
16874 }
16875
16876 static CORE_ADDR
16877 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
16878 unsigned int *bytes_read)
16879 {
16880 struct comp_unit_head *cu_header = &cu->header;
16881 CORE_ADDR retval = 0;
16882
16883 if (cu_header->signed_addr_p)
16884 {
16885 switch (cu_header->addr_size)
16886 {
16887 case 2:
16888 retval = bfd_get_signed_16 (abfd, buf);
16889 break;
16890 case 4:
16891 retval = bfd_get_signed_32 (abfd, buf);
16892 break;
16893 case 8:
16894 retval = bfd_get_signed_64 (abfd, buf);
16895 break;
16896 default:
16897 internal_error (__FILE__, __LINE__,
16898 _("read_address: bad switch, signed [in module %s]"),
16899 bfd_get_filename (abfd));
16900 }
16901 }
16902 else
16903 {
16904 switch (cu_header->addr_size)
16905 {
16906 case 2:
16907 retval = bfd_get_16 (abfd, buf);
16908 break;
16909 case 4:
16910 retval = bfd_get_32 (abfd, buf);
16911 break;
16912 case 8:
16913 retval = bfd_get_64 (abfd, buf);
16914 break;
16915 default:
16916 internal_error (__FILE__, __LINE__,
16917 _("read_address: bad switch, "
16918 "unsigned [in module %s]"),
16919 bfd_get_filename (abfd));
16920 }
16921 }
16922
16923 *bytes_read = cu_header->addr_size;
16924 return retval;
16925 }
16926
16927 /* Read the initial length from a section. The (draft) DWARF 3
16928 specification allows the initial length to take up either 4 bytes
16929 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
16930 bytes describe the length and all offsets will be 8 bytes in length
16931 instead of 4.
16932
16933 An older, non-standard 64-bit format is also handled by this
16934 function. The older format in question stores the initial length
16935 as an 8-byte quantity without an escape value. Lengths greater
16936 than 2^32 aren't very common which means that the initial 4 bytes
16937 is almost always zero. Since a length value of zero doesn't make
16938 sense for the 32-bit format, this initial zero can be considered to
16939 be an escape value which indicates the presence of the older 64-bit
16940 format. As written, the code can't detect (old format) lengths
16941 greater than 4GB. If it becomes necessary to handle lengths
16942 somewhat larger than 4GB, we could allow other small values (such
16943 as the non-sensical values of 1, 2, and 3) to also be used as
16944 escape values indicating the presence of the old format.
16945
16946 The value returned via bytes_read should be used to increment the
16947 relevant pointer after calling read_initial_length().
16948
16949 [ Note: read_initial_length() and read_offset() are based on the
16950 document entitled "DWARF Debugging Information Format", revision
16951 3, draft 8, dated November 19, 2001. This document was obtained
16952 from:
16953
16954 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
16955
16956 This document is only a draft and is subject to change. (So beware.)
16957
16958 Details regarding the older, non-standard 64-bit format were
16959 determined empirically by examining 64-bit ELF files produced by
16960 the SGI toolchain on an IRIX 6.5 machine.
16961
16962 - Kevin, July 16, 2002
16963 ] */
16964
16965 static LONGEST
16966 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
16967 {
16968 LONGEST length = bfd_get_32 (abfd, buf);
16969
16970 if (length == 0xffffffff)
16971 {
16972 length = bfd_get_64 (abfd, buf + 4);
16973 *bytes_read = 12;
16974 }
16975 else if (length == 0)
16976 {
16977 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
16978 length = bfd_get_64 (abfd, buf);
16979 *bytes_read = 8;
16980 }
16981 else
16982 {
16983 *bytes_read = 4;
16984 }
16985
16986 return length;
16987 }
16988
16989 /* Cover function for read_initial_length.
16990 Returns the length of the object at BUF, and stores the size of the
16991 initial length in *BYTES_READ and stores the size that offsets will be in
16992 *OFFSET_SIZE.
16993 If the initial length size is not equivalent to that specified in
16994 CU_HEADER then issue a complaint.
16995 This is useful when reading non-comp-unit headers. */
16996
16997 static LONGEST
16998 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
16999 const struct comp_unit_head *cu_header,
17000 unsigned int *bytes_read,
17001 unsigned int *offset_size)
17002 {
17003 LONGEST length = read_initial_length (abfd, buf, bytes_read);
17004
17005 gdb_assert (cu_header->initial_length_size == 4
17006 || cu_header->initial_length_size == 8
17007 || cu_header->initial_length_size == 12);
17008
17009 if (cu_header->initial_length_size != *bytes_read)
17010 complaint (&symfile_complaints,
17011 _("intermixed 32-bit and 64-bit DWARF sections"));
17012
17013 *offset_size = (*bytes_read == 4) ? 4 : 8;
17014 return length;
17015 }
17016
17017 /* Read an offset from the data stream. The size of the offset is
17018 given by cu_header->offset_size. */
17019
17020 static LONGEST
17021 read_offset (bfd *abfd, const gdb_byte *buf,
17022 const struct comp_unit_head *cu_header,
17023 unsigned int *bytes_read)
17024 {
17025 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
17026
17027 *bytes_read = cu_header->offset_size;
17028 return offset;
17029 }
17030
17031 /* Read an offset from the data stream. */
17032
17033 static LONGEST
17034 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
17035 {
17036 LONGEST retval = 0;
17037
17038 switch (offset_size)
17039 {
17040 case 4:
17041 retval = bfd_get_32 (abfd, buf);
17042 break;
17043 case 8:
17044 retval = bfd_get_64 (abfd, buf);
17045 break;
17046 default:
17047 internal_error (__FILE__, __LINE__,
17048 _("read_offset_1: bad switch [in module %s]"),
17049 bfd_get_filename (abfd));
17050 }
17051
17052 return retval;
17053 }
17054
17055 static const gdb_byte *
17056 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
17057 {
17058 /* If the size of a host char is 8 bits, we can return a pointer
17059 to the buffer, otherwise we have to copy the data to a buffer
17060 allocated on the temporary obstack. */
17061 gdb_assert (HOST_CHAR_BIT == 8);
17062 return buf;
17063 }
17064
17065 static const char *
17066 read_direct_string (bfd *abfd, const gdb_byte *buf,
17067 unsigned int *bytes_read_ptr)
17068 {
17069 /* If the size of a host char is 8 bits, we can return a pointer
17070 to the string, otherwise we have to copy the string to a buffer
17071 allocated on the temporary obstack. */
17072 gdb_assert (HOST_CHAR_BIT == 8);
17073 if (*buf == '\0')
17074 {
17075 *bytes_read_ptr = 1;
17076 return NULL;
17077 }
17078 *bytes_read_ptr = strlen ((const char *) buf) + 1;
17079 return (const char *) buf;
17080 }
17081
17082 /* Return pointer to string at section SECT offset STR_OFFSET with error
17083 reporting strings FORM_NAME and SECT_NAME. */
17084
17085 static const char *
17086 read_indirect_string_at_offset_from (bfd *abfd, LONGEST str_offset,
17087 struct dwarf2_section_info *sect,
17088 const char *form_name,
17089 const char *sect_name)
17090 {
17091 dwarf2_read_section (dwarf2_per_objfile->objfile, sect);
17092 if (sect->buffer == NULL)
17093 error (_("%s used without %s section [in module %s]"),
17094 form_name, sect_name, bfd_get_filename (abfd));
17095 if (str_offset >= sect->size)
17096 error (_("%s pointing outside of %s section [in module %s]"),
17097 form_name, sect_name, bfd_get_filename (abfd));
17098 gdb_assert (HOST_CHAR_BIT == 8);
17099 if (sect->buffer[str_offset] == '\0')
17100 return NULL;
17101 return (const char *) (sect->buffer + str_offset);
17102 }
17103
17104 /* Return pointer to string at .debug_str offset STR_OFFSET. */
17105
17106 static const char *
17107 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
17108 {
17109 return read_indirect_string_at_offset_from (abfd, str_offset,
17110 &dwarf2_per_objfile->str,
17111 "DW_FORM_strp", ".debug_str");
17112 }
17113
17114 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
17115
17116 static const char *
17117 read_indirect_line_string_at_offset (bfd *abfd, LONGEST str_offset)
17118 {
17119 return read_indirect_string_at_offset_from (abfd, str_offset,
17120 &dwarf2_per_objfile->line_str,
17121 "DW_FORM_line_strp",
17122 ".debug_line_str");
17123 }
17124
17125 /* Read a string at offset STR_OFFSET in the .debug_str section from
17126 the .dwz file DWZ. Throw an error if the offset is too large. If
17127 the string consists of a single NUL byte, return NULL; otherwise
17128 return a pointer to the string. */
17129
17130 static const char *
17131 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
17132 {
17133 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
17134
17135 if (dwz->str.buffer == NULL)
17136 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
17137 "section [in module %s]"),
17138 bfd_get_filename (dwz->dwz_bfd));
17139 if (str_offset >= dwz->str.size)
17140 error (_("DW_FORM_GNU_strp_alt pointing outside of "
17141 ".debug_str section [in module %s]"),
17142 bfd_get_filename (dwz->dwz_bfd));
17143 gdb_assert (HOST_CHAR_BIT == 8);
17144 if (dwz->str.buffer[str_offset] == '\0')
17145 return NULL;
17146 return (const char *) (dwz->str.buffer + str_offset);
17147 }
17148
17149 /* Return pointer to string at .debug_str offset as read from BUF.
17150 BUF is assumed to be in a compilation unit described by CU_HEADER.
17151 Return *BYTES_READ_PTR count of bytes read from BUF. */
17152
17153 static const char *
17154 read_indirect_string (bfd *abfd, const gdb_byte *buf,
17155 const struct comp_unit_head *cu_header,
17156 unsigned int *bytes_read_ptr)
17157 {
17158 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17159
17160 return read_indirect_string_at_offset (abfd, str_offset);
17161 }
17162
17163 /* Return pointer to string at .debug_line_str offset as read from BUF.
17164 BUF is assumed to be in a compilation unit described by CU_HEADER.
17165 Return *BYTES_READ_PTR count of bytes read from BUF. */
17166
17167 static const char *
17168 read_indirect_line_string (bfd *abfd, const gdb_byte *buf,
17169 const struct comp_unit_head *cu_header,
17170 unsigned int *bytes_read_ptr)
17171 {
17172 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
17173
17174 return read_indirect_line_string_at_offset (abfd, str_offset);
17175 }
17176
17177 ULONGEST
17178 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
17179 unsigned int *bytes_read_ptr)
17180 {
17181 ULONGEST result;
17182 unsigned int num_read;
17183 int shift;
17184 unsigned char byte;
17185
17186 result = 0;
17187 shift = 0;
17188 num_read = 0;
17189 while (1)
17190 {
17191 byte = bfd_get_8 (abfd, buf);
17192 buf++;
17193 num_read++;
17194 result |= ((ULONGEST) (byte & 127) << shift);
17195 if ((byte & 128) == 0)
17196 {
17197 break;
17198 }
17199 shift += 7;
17200 }
17201 *bytes_read_ptr = num_read;
17202 return result;
17203 }
17204
17205 static LONGEST
17206 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
17207 unsigned int *bytes_read_ptr)
17208 {
17209 LONGEST result;
17210 int shift, num_read;
17211 unsigned char byte;
17212
17213 result = 0;
17214 shift = 0;
17215 num_read = 0;
17216 while (1)
17217 {
17218 byte = bfd_get_8 (abfd, buf);
17219 buf++;
17220 num_read++;
17221 result |= ((LONGEST) (byte & 127) << shift);
17222 shift += 7;
17223 if ((byte & 128) == 0)
17224 {
17225 break;
17226 }
17227 }
17228 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
17229 result |= -(((LONGEST) 1) << shift);
17230 *bytes_read_ptr = num_read;
17231 return result;
17232 }
17233
17234 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
17235 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
17236 ADDR_SIZE is the size of addresses from the CU header. */
17237
17238 static CORE_ADDR
17239 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
17240 {
17241 struct objfile *objfile = dwarf2_per_objfile->objfile;
17242 bfd *abfd = objfile->obfd;
17243 const gdb_byte *info_ptr;
17244
17245 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
17246 if (dwarf2_per_objfile->addr.buffer == NULL)
17247 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
17248 objfile_name (objfile));
17249 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
17250 error (_("DW_FORM_addr_index pointing outside of "
17251 ".debug_addr section [in module %s]"),
17252 objfile_name (objfile));
17253 info_ptr = (dwarf2_per_objfile->addr.buffer
17254 + addr_base + addr_index * addr_size);
17255 if (addr_size == 4)
17256 return bfd_get_32 (abfd, info_ptr);
17257 else
17258 return bfd_get_64 (abfd, info_ptr);
17259 }
17260
17261 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
17262
17263 static CORE_ADDR
17264 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
17265 {
17266 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
17267 }
17268
17269 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
17270
17271 static CORE_ADDR
17272 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
17273 unsigned int *bytes_read)
17274 {
17275 bfd *abfd = cu->objfile->obfd;
17276 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
17277
17278 return read_addr_index (cu, addr_index);
17279 }
17280
17281 /* Data structure to pass results from dwarf2_read_addr_index_reader
17282 back to dwarf2_read_addr_index. */
17283
17284 struct dwarf2_read_addr_index_data
17285 {
17286 ULONGEST addr_base;
17287 int addr_size;
17288 };
17289
17290 /* die_reader_func for dwarf2_read_addr_index. */
17291
17292 static void
17293 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
17294 const gdb_byte *info_ptr,
17295 struct die_info *comp_unit_die,
17296 int has_children,
17297 void *data)
17298 {
17299 struct dwarf2_cu *cu = reader->cu;
17300 struct dwarf2_read_addr_index_data *aidata =
17301 (struct dwarf2_read_addr_index_data *) data;
17302
17303 aidata->addr_base = cu->addr_base;
17304 aidata->addr_size = cu->header.addr_size;
17305 }
17306
17307 /* Given an index in .debug_addr, fetch the value.
17308 NOTE: This can be called during dwarf expression evaluation,
17309 long after the debug information has been read, and thus per_cu->cu
17310 may no longer exist. */
17311
17312 CORE_ADDR
17313 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
17314 unsigned int addr_index)
17315 {
17316 struct objfile *objfile = per_cu->objfile;
17317 struct dwarf2_cu *cu = per_cu->cu;
17318 ULONGEST addr_base;
17319 int addr_size;
17320
17321 /* This is intended to be called from outside this file. */
17322 dw2_setup (objfile);
17323
17324 /* We need addr_base and addr_size.
17325 If we don't have PER_CU->cu, we have to get it.
17326 Nasty, but the alternative is storing the needed info in PER_CU,
17327 which at this point doesn't seem justified: it's not clear how frequently
17328 it would get used and it would increase the size of every PER_CU.
17329 Entry points like dwarf2_per_cu_addr_size do a similar thing
17330 so we're not in uncharted territory here.
17331 Alas we need to be a bit more complicated as addr_base is contained
17332 in the DIE.
17333
17334 We don't need to read the entire CU(/TU).
17335 We just need the header and top level die.
17336
17337 IWBN to use the aging mechanism to let us lazily later discard the CU.
17338 For now we skip this optimization. */
17339
17340 if (cu != NULL)
17341 {
17342 addr_base = cu->addr_base;
17343 addr_size = cu->header.addr_size;
17344 }
17345 else
17346 {
17347 struct dwarf2_read_addr_index_data aidata;
17348
17349 /* Note: We can't use init_cutu_and_read_dies_simple here,
17350 we need addr_base. */
17351 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
17352 dwarf2_read_addr_index_reader, &aidata);
17353 addr_base = aidata.addr_base;
17354 addr_size = aidata.addr_size;
17355 }
17356
17357 return read_addr_index_1 (addr_index, addr_base, addr_size);
17358 }
17359
17360 /* Given a DW_FORM_GNU_str_index, fetch the string.
17361 This is only used by the Fission support. */
17362
17363 static const char *
17364 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
17365 {
17366 struct objfile *objfile = dwarf2_per_objfile->objfile;
17367 const char *objf_name = objfile_name (objfile);
17368 bfd *abfd = objfile->obfd;
17369 struct dwarf2_cu *cu = reader->cu;
17370 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
17371 struct dwarf2_section_info *str_offsets_section =
17372 &reader->dwo_file->sections.str_offsets;
17373 const gdb_byte *info_ptr;
17374 ULONGEST str_offset;
17375 static const char form_name[] = "DW_FORM_GNU_str_index";
17376
17377 dwarf2_read_section (objfile, str_section);
17378 dwarf2_read_section (objfile, str_offsets_section);
17379 if (str_section->buffer == NULL)
17380 error (_("%s used without .debug_str.dwo section"
17381 " in CU at offset 0x%lx [in module %s]"),
17382 form_name, (long) cu->header.offset.sect_off, objf_name);
17383 if (str_offsets_section->buffer == NULL)
17384 error (_("%s used without .debug_str_offsets.dwo section"
17385 " in CU at offset 0x%lx [in module %s]"),
17386 form_name, (long) cu->header.offset.sect_off, objf_name);
17387 if (str_index * cu->header.offset_size >= str_offsets_section->size)
17388 error (_("%s pointing outside of .debug_str_offsets.dwo"
17389 " section in CU at offset 0x%lx [in module %s]"),
17390 form_name, (long) cu->header.offset.sect_off, objf_name);
17391 info_ptr = (str_offsets_section->buffer
17392 + str_index * cu->header.offset_size);
17393 if (cu->header.offset_size == 4)
17394 str_offset = bfd_get_32 (abfd, info_ptr);
17395 else
17396 str_offset = bfd_get_64 (abfd, info_ptr);
17397 if (str_offset >= str_section->size)
17398 error (_("Offset from %s pointing outside of"
17399 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
17400 form_name, (long) cu->header.offset.sect_off, objf_name);
17401 return (const char *) (str_section->buffer + str_offset);
17402 }
17403
17404 /* Return the length of an LEB128 number in BUF. */
17405
17406 static int
17407 leb128_size (const gdb_byte *buf)
17408 {
17409 const gdb_byte *begin = buf;
17410 gdb_byte byte;
17411
17412 while (1)
17413 {
17414 byte = *buf++;
17415 if ((byte & 128) == 0)
17416 return buf - begin;
17417 }
17418 }
17419
17420 static void
17421 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
17422 {
17423 switch (lang)
17424 {
17425 case DW_LANG_C89:
17426 case DW_LANG_C99:
17427 case DW_LANG_C11:
17428 case DW_LANG_C:
17429 case DW_LANG_UPC:
17430 cu->language = language_c;
17431 break;
17432 case DW_LANG_Java:
17433 case DW_LANG_C_plus_plus:
17434 case DW_LANG_C_plus_plus_11:
17435 case DW_LANG_C_plus_plus_14:
17436 cu->language = language_cplus;
17437 break;
17438 case DW_LANG_D:
17439 cu->language = language_d;
17440 break;
17441 case DW_LANG_Fortran77:
17442 case DW_LANG_Fortran90:
17443 case DW_LANG_Fortran95:
17444 case DW_LANG_Fortran03:
17445 case DW_LANG_Fortran08:
17446 cu->language = language_fortran;
17447 break;
17448 case DW_LANG_Go:
17449 cu->language = language_go;
17450 break;
17451 case DW_LANG_Mips_Assembler:
17452 cu->language = language_asm;
17453 break;
17454 case DW_LANG_Ada83:
17455 case DW_LANG_Ada95:
17456 cu->language = language_ada;
17457 break;
17458 case DW_LANG_Modula2:
17459 cu->language = language_m2;
17460 break;
17461 case DW_LANG_Pascal83:
17462 cu->language = language_pascal;
17463 break;
17464 case DW_LANG_ObjC:
17465 cu->language = language_objc;
17466 break;
17467 case DW_LANG_Rust:
17468 case DW_LANG_Rust_old:
17469 cu->language = language_rust;
17470 break;
17471 case DW_LANG_Cobol74:
17472 case DW_LANG_Cobol85:
17473 default:
17474 cu->language = language_minimal;
17475 break;
17476 }
17477 cu->language_defn = language_def (cu->language);
17478 }
17479
17480 /* Return the named attribute or NULL if not there. */
17481
17482 static struct attribute *
17483 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17484 {
17485 for (;;)
17486 {
17487 unsigned int i;
17488 struct attribute *spec = NULL;
17489
17490 for (i = 0; i < die->num_attrs; ++i)
17491 {
17492 if (die->attrs[i].name == name)
17493 return &die->attrs[i];
17494 if (die->attrs[i].name == DW_AT_specification
17495 || die->attrs[i].name == DW_AT_abstract_origin)
17496 spec = &die->attrs[i];
17497 }
17498
17499 if (!spec)
17500 break;
17501
17502 die = follow_die_ref (die, spec, &cu);
17503 }
17504
17505 return NULL;
17506 }
17507
17508 /* Return the named attribute or NULL if not there,
17509 but do not follow DW_AT_specification, etc.
17510 This is for use in contexts where we're reading .debug_types dies.
17511 Following DW_AT_specification, DW_AT_abstract_origin will take us
17512 back up the chain, and we want to go down. */
17513
17514 static struct attribute *
17515 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
17516 {
17517 unsigned int i;
17518
17519 for (i = 0; i < die->num_attrs; ++i)
17520 if (die->attrs[i].name == name)
17521 return &die->attrs[i];
17522
17523 return NULL;
17524 }
17525
17526 /* Return the string associated with a string-typed attribute, or NULL if it
17527 is either not found or is of an incorrect type. */
17528
17529 static const char *
17530 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
17531 {
17532 struct attribute *attr;
17533 const char *str = NULL;
17534
17535 attr = dwarf2_attr (die, name, cu);
17536
17537 if (attr != NULL)
17538 {
17539 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
17540 || attr->form == DW_FORM_string || attr->form == DW_FORM_GNU_strp_alt)
17541 str = DW_STRING (attr);
17542 else
17543 complaint (&symfile_complaints,
17544 _("string type expected for attribute %s for "
17545 "DIE at 0x%x in module %s"),
17546 dwarf_attr_name (name), die->offset.sect_off,
17547 objfile_name (cu->objfile));
17548 }
17549
17550 return str;
17551 }
17552
17553 /* Return non-zero iff the attribute NAME is defined for the given DIE,
17554 and holds a non-zero value. This function should only be used for
17555 DW_FORM_flag or DW_FORM_flag_present attributes. */
17556
17557 static int
17558 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
17559 {
17560 struct attribute *attr = dwarf2_attr (die, name, cu);
17561
17562 return (attr && DW_UNSND (attr));
17563 }
17564
17565 static int
17566 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
17567 {
17568 /* A DIE is a declaration if it has a DW_AT_declaration attribute
17569 which value is non-zero. However, we have to be careful with
17570 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
17571 (via dwarf2_flag_true_p) follows this attribute. So we may
17572 end up accidently finding a declaration attribute that belongs
17573 to a different DIE referenced by the specification attribute,
17574 even though the given DIE does not have a declaration attribute. */
17575 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
17576 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
17577 }
17578
17579 /* Return the die giving the specification for DIE, if there is
17580 one. *SPEC_CU is the CU containing DIE on input, and the CU
17581 containing the return value on output. If there is no
17582 specification, but there is an abstract origin, that is
17583 returned. */
17584
17585 static struct die_info *
17586 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
17587 {
17588 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
17589 *spec_cu);
17590
17591 if (spec_attr == NULL)
17592 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
17593
17594 if (spec_attr == NULL)
17595 return NULL;
17596 else
17597 return follow_die_ref (die, spec_attr, spec_cu);
17598 }
17599
17600 /* Stub for free_line_header to match void * callback types. */
17601
17602 static void
17603 free_line_header_voidp (void *arg)
17604 {
17605 struct line_header *lh = (struct line_header *) arg;
17606
17607 delete lh;
17608 }
17609
17610 void
17611 line_header::add_include_dir (const char *include_dir)
17612 {
17613 if (dwarf_line_debug >= 2)
17614 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
17615 include_dirs.size () + 1, include_dir);
17616
17617 include_dirs.push_back (include_dir);
17618 }
17619
17620 void
17621 line_header::add_file_name (const char *name,
17622 dir_index d_index,
17623 unsigned int mod_time,
17624 unsigned int length)
17625 {
17626 if (dwarf_line_debug >= 2)
17627 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
17628 (unsigned) file_names.size () + 1, name);
17629
17630 file_names.emplace_back (name, d_index, mod_time, length);
17631 }
17632
17633 /* A convenience function to find the proper .debug_line section for a CU. */
17634
17635 static struct dwarf2_section_info *
17636 get_debug_line_section (struct dwarf2_cu *cu)
17637 {
17638 struct dwarf2_section_info *section;
17639
17640 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
17641 DWO file. */
17642 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17643 section = &cu->dwo_unit->dwo_file->sections.line;
17644 else if (cu->per_cu->is_dwz)
17645 {
17646 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17647
17648 section = &dwz->line;
17649 }
17650 else
17651 section = &dwarf2_per_objfile->line;
17652
17653 return section;
17654 }
17655
17656 /* Read directory or file name entry format, starting with byte of
17657 format count entries, ULEB128 pairs of entry formats, ULEB128 of
17658 entries count and the entries themselves in the described entry
17659 format. */
17660
17661 static void
17662 read_formatted_entries (bfd *abfd, const gdb_byte **bufp,
17663 struct line_header *lh,
17664 const struct comp_unit_head *cu_header,
17665 void (*callback) (struct line_header *lh,
17666 const char *name,
17667 dir_index d_index,
17668 unsigned int mod_time,
17669 unsigned int length))
17670 {
17671 gdb_byte format_count, formati;
17672 ULONGEST data_count, datai;
17673 const gdb_byte *buf = *bufp;
17674 const gdb_byte *format_header_data;
17675 int i;
17676 unsigned int bytes_read;
17677
17678 format_count = read_1_byte (abfd, buf);
17679 buf += 1;
17680 format_header_data = buf;
17681 for (formati = 0; formati < format_count; formati++)
17682 {
17683 read_unsigned_leb128 (abfd, buf, &bytes_read);
17684 buf += bytes_read;
17685 read_unsigned_leb128 (abfd, buf, &bytes_read);
17686 buf += bytes_read;
17687 }
17688
17689 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
17690 buf += bytes_read;
17691 for (datai = 0; datai < data_count; datai++)
17692 {
17693 const gdb_byte *format = format_header_data;
17694 struct file_entry fe;
17695
17696 for (formati = 0; formati < format_count; formati++)
17697 {
17698 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
17699 format += bytes_read;
17700
17701 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
17702 format += bytes_read;
17703
17704 gdb::optional<const char *> string;
17705 gdb::optional<unsigned int> uint;
17706
17707 switch (form)
17708 {
17709 case DW_FORM_string:
17710 string.emplace (read_direct_string (abfd, buf, &bytes_read));
17711 buf += bytes_read;
17712 break;
17713
17714 case DW_FORM_line_strp:
17715 string.emplace (read_indirect_line_string (abfd, buf,
17716 cu_header,
17717 &bytes_read));
17718 buf += bytes_read;
17719 break;
17720
17721 case DW_FORM_data1:
17722 uint.emplace (read_1_byte (abfd, buf));
17723 buf += 1;
17724 break;
17725
17726 case DW_FORM_data2:
17727 uint.emplace (read_2_bytes (abfd, buf));
17728 buf += 2;
17729 break;
17730
17731 case DW_FORM_data4:
17732 uint.emplace (read_4_bytes (abfd, buf));
17733 buf += 4;
17734 break;
17735
17736 case DW_FORM_data8:
17737 uint.emplace (read_8_bytes (abfd, buf));
17738 buf += 8;
17739 break;
17740
17741 case DW_FORM_udata:
17742 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
17743 buf += bytes_read;
17744 break;
17745
17746 case DW_FORM_block:
17747 /* It is valid only for DW_LNCT_timestamp which is ignored by
17748 current GDB. */
17749 break;
17750 }
17751
17752 switch (content_type)
17753 {
17754 case DW_LNCT_path:
17755 if (string.has_value ())
17756 fe.name = *string;
17757 break;
17758 case DW_LNCT_directory_index:
17759 if (uint.has_value ())
17760 fe.d_index = (dir_index) *uint;
17761 break;
17762 case DW_LNCT_timestamp:
17763 if (uint.has_value ())
17764 fe.mod_time = *uint;
17765 break;
17766 case DW_LNCT_size:
17767 if (uint.has_value ())
17768 fe.length = *uint;
17769 break;
17770 case DW_LNCT_MD5:
17771 break;
17772 default:
17773 complaint (&symfile_complaints,
17774 _("Unknown format content type %s"),
17775 pulongest (content_type));
17776 }
17777 }
17778
17779 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
17780 }
17781
17782 *bufp = buf;
17783 }
17784
17785 /* Read the statement program header starting at OFFSET in
17786 .debug_line, or .debug_line.dwo. Return a pointer
17787 to a struct line_header, allocated using xmalloc.
17788 Returns NULL if there is a problem reading the header, e.g., if it
17789 has a version we don't understand.
17790
17791 NOTE: the strings in the include directory and file name tables of
17792 the returned object point into the dwarf line section buffer,
17793 and must not be freed. */
17794
17795 static line_header_up
17796 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
17797 {
17798 const gdb_byte *line_ptr;
17799 unsigned int bytes_read, offset_size;
17800 int i;
17801 const char *cur_dir, *cur_file;
17802 struct dwarf2_section_info *section;
17803 bfd *abfd;
17804
17805 section = get_debug_line_section (cu);
17806 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
17807 if (section->buffer == NULL)
17808 {
17809 if (cu->dwo_unit && cu->per_cu->is_debug_types)
17810 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
17811 else
17812 complaint (&symfile_complaints, _("missing .debug_line section"));
17813 return 0;
17814 }
17815
17816 /* We can't do this until we know the section is non-empty.
17817 Only then do we know we have such a section. */
17818 abfd = get_section_bfd_owner (section);
17819
17820 /* Make sure that at least there's room for the total_length field.
17821 That could be 12 bytes long, but we're just going to fudge that. */
17822 if (offset + 4 >= section->size)
17823 {
17824 dwarf2_statement_list_fits_in_line_number_section_complaint ();
17825 return 0;
17826 }
17827
17828 line_header_up lh (new line_header ());
17829
17830 lh->offset.sect_off = offset;
17831 lh->offset_in_dwz = cu->per_cu->is_dwz;
17832
17833 line_ptr = section->buffer + offset;
17834
17835 /* Read in the header. */
17836 lh->total_length =
17837 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
17838 &bytes_read, &offset_size);
17839 line_ptr += bytes_read;
17840 if (line_ptr + lh->total_length > (section->buffer + section->size))
17841 {
17842 dwarf2_statement_list_fits_in_line_number_section_complaint ();
17843 return 0;
17844 }
17845 lh->statement_program_end = line_ptr + lh->total_length;
17846 lh->version = read_2_bytes (abfd, line_ptr);
17847 line_ptr += 2;
17848 if (lh->version > 5)
17849 {
17850 /* This is a version we don't understand. The format could have
17851 changed in ways we don't handle properly so just punt. */
17852 complaint (&symfile_complaints,
17853 _("unsupported version in .debug_line section"));
17854 return NULL;
17855 }
17856 if (lh->version >= 5)
17857 {
17858 gdb_byte segment_selector_size;
17859
17860 /* Skip address size. */
17861 read_1_byte (abfd, line_ptr);
17862 line_ptr += 1;
17863
17864 segment_selector_size = read_1_byte (abfd, line_ptr);
17865 line_ptr += 1;
17866 if (segment_selector_size != 0)
17867 {
17868 complaint (&symfile_complaints,
17869 _("unsupported segment selector size %u "
17870 "in .debug_line section"),
17871 segment_selector_size);
17872 return NULL;
17873 }
17874 }
17875 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
17876 line_ptr += offset_size;
17877 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
17878 line_ptr += 1;
17879 if (lh->version >= 4)
17880 {
17881 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
17882 line_ptr += 1;
17883 }
17884 else
17885 lh->maximum_ops_per_instruction = 1;
17886
17887 if (lh->maximum_ops_per_instruction == 0)
17888 {
17889 lh->maximum_ops_per_instruction = 1;
17890 complaint (&symfile_complaints,
17891 _("invalid maximum_ops_per_instruction "
17892 "in `.debug_line' section"));
17893 }
17894
17895 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
17896 line_ptr += 1;
17897 lh->line_base = read_1_signed_byte (abfd, line_ptr);
17898 line_ptr += 1;
17899 lh->line_range = read_1_byte (abfd, line_ptr);
17900 line_ptr += 1;
17901 lh->opcode_base = read_1_byte (abfd, line_ptr);
17902 line_ptr += 1;
17903 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
17904
17905 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
17906 for (i = 1; i < lh->opcode_base; ++i)
17907 {
17908 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
17909 line_ptr += 1;
17910 }
17911
17912 if (lh->version >= 5)
17913 {
17914 /* Read directory table. */
17915 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
17916 [] (struct line_header *lh, const char *name,
17917 dir_index d_index, unsigned int mod_time,
17918 unsigned int length)
17919 {
17920 lh->add_include_dir (name);
17921 });
17922
17923 /* Read file name table. */
17924 read_formatted_entries (abfd, &line_ptr, lh.get (), &cu->header,
17925 [] (struct line_header *lh, const char *name,
17926 dir_index d_index, unsigned int mod_time,
17927 unsigned int length)
17928 {
17929 lh->add_file_name (name, d_index, mod_time, length);
17930 });
17931 }
17932 else
17933 {
17934 /* Read directory table. */
17935 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17936 {
17937 line_ptr += bytes_read;
17938 lh->add_include_dir (cur_dir);
17939 }
17940 line_ptr += bytes_read;
17941
17942 /* Read file name table. */
17943 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
17944 {
17945 unsigned int mod_time, length;
17946 dir_index d_index;
17947
17948 line_ptr += bytes_read;
17949 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17950 line_ptr += bytes_read;
17951 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17952 line_ptr += bytes_read;
17953 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
17954 line_ptr += bytes_read;
17955
17956 lh->add_file_name (cur_file, d_index, mod_time, length);
17957 }
17958 line_ptr += bytes_read;
17959 }
17960 lh->statement_program_start = line_ptr;
17961
17962 if (line_ptr > (section->buffer + section->size))
17963 complaint (&symfile_complaints,
17964 _("line number info header doesn't "
17965 "fit in `.debug_line' section"));
17966
17967 return lh;
17968 }
17969
17970 /* Subroutine of dwarf_decode_lines to simplify it.
17971 Return the file name of the psymtab for included file FILE_INDEX
17972 in line header LH of PST.
17973 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
17974 If space for the result is malloc'd, it will be freed by a cleanup.
17975 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
17976
17977 The function creates dangling cleanup registration. */
17978
17979 static const char *
17980 psymtab_include_file_name (const struct line_header *lh, int file_index,
17981 const struct partial_symtab *pst,
17982 const char *comp_dir)
17983 {
17984 const file_entry &fe = lh->file_names[file_index];
17985 const char *include_name = fe.name;
17986 const char *include_name_to_compare = include_name;
17987 const char *pst_filename;
17988 char *copied_name = NULL;
17989 int file_is_pst;
17990
17991 const char *dir_name = fe.include_dir (lh);
17992
17993 if (!IS_ABSOLUTE_PATH (include_name)
17994 && (dir_name != NULL || comp_dir != NULL))
17995 {
17996 /* Avoid creating a duplicate psymtab for PST.
17997 We do this by comparing INCLUDE_NAME and PST_FILENAME.
17998 Before we do the comparison, however, we need to account
17999 for DIR_NAME and COMP_DIR.
18000 First prepend dir_name (if non-NULL). If we still don't
18001 have an absolute path prepend comp_dir (if non-NULL).
18002 However, the directory we record in the include-file's
18003 psymtab does not contain COMP_DIR (to match the
18004 corresponding symtab(s)).
18005
18006 Example:
18007
18008 bash$ cd /tmp
18009 bash$ gcc -g ./hello.c
18010 include_name = "hello.c"
18011 dir_name = "."
18012 DW_AT_comp_dir = comp_dir = "/tmp"
18013 DW_AT_name = "./hello.c"
18014
18015 */
18016
18017 if (dir_name != NULL)
18018 {
18019 char *tem = concat (dir_name, SLASH_STRING,
18020 include_name, (char *)NULL);
18021
18022 make_cleanup (xfree, tem);
18023 include_name = tem;
18024 include_name_to_compare = include_name;
18025 }
18026 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
18027 {
18028 char *tem = concat (comp_dir, SLASH_STRING,
18029 include_name, (char *)NULL);
18030
18031 make_cleanup (xfree, tem);
18032 include_name_to_compare = tem;
18033 }
18034 }
18035
18036 pst_filename = pst->filename;
18037 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
18038 {
18039 copied_name = concat (pst->dirname, SLASH_STRING,
18040 pst_filename, (char *)NULL);
18041 pst_filename = copied_name;
18042 }
18043
18044 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
18045
18046 if (copied_name != NULL)
18047 xfree (copied_name);
18048
18049 if (file_is_pst)
18050 return NULL;
18051 return include_name;
18052 }
18053
18054 /* State machine to track the state of the line number program. */
18055
18056 struct lnp_state_machine
18057 {
18058 file_entry *current_file ()
18059 {
18060 /* lh->file_names is 0-based, but the file name numbers in the
18061 statement program are 1-based. */
18062 return the_line_header->file_name_at (file);
18063 }
18064
18065 /* The line number header. */
18066 line_header *the_line_header;
18067
18068 /* These are part of the standard DWARF line number state machine. */
18069
18070 unsigned char op_index;
18071 /* The line table index (1-based) of the current file. */
18072 file_name_index file;
18073 unsigned int line;
18074 CORE_ADDR address;
18075 int is_stmt;
18076 unsigned int discriminator;
18077
18078 /* Additional bits of state we need to track. */
18079
18080 /* The last file that we called dwarf2_start_subfile for.
18081 This is only used for TLLs. */
18082 unsigned int last_file;
18083 /* The last file a line number was recorded for. */
18084 struct subfile *last_subfile;
18085
18086 /* The function to call to record a line. */
18087 record_line_ftype *record_line;
18088
18089 /* The last line number that was recorded, used to coalesce
18090 consecutive entries for the same line. This can happen, for
18091 example, when discriminators are present. PR 17276. */
18092 unsigned int last_line;
18093 int line_has_non_zero_discriminator;
18094 };
18095
18096 /* There's a lot of static state to pass to dwarf_record_line.
18097 This keeps it all together. */
18098
18099 typedef struct
18100 {
18101 /* The gdbarch. */
18102 struct gdbarch *gdbarch;
18103
18104 /* The line number header. */
18105 struct line_header *line_header;
18106
18107 /* Non-zero if we're recording lines.
18108 Otherwise we're building partial symtabs and are just interested in
18109 finding include files mentioned by the line number program. */
18110 int record_lines_p;
18111 } lnp_reader_state;
18112
18113 /* Ignore this record_line request. */
18114
18115 static void
18116 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
18117 {
18118 return;
18119 }
18120
18121 /* Return non-zero if we should add LINE to the line number table.
18122 LINE is the line to add, LAST_LINE is the last line that was added,
18123 LAST_SUBFILE is the subfile for LAST_LINE.
18124 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
18125 had a non-zero discriminator.
18126
18127 We have to be careful in the presence of discriminators.
18128 E.g., for this line:
18129
18130 for (i = 0; i < 100000; i++);
18131
18132 clang can emit four line number entries for that one line,
18133 each with a different discriminator.
18134 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
18135
18136 However, we want gdb to coalesce all four entries into one.
18137 Otherwise the user could stepi into the middle of the line and
18138 gdb would get confused about whether the pc really was in the
18139 middle of the line.
18140
18141 Things are further complicated by the fact that two consecutive
18142 line number entries for the same line is a heuristic used by gcc
18143 to denote the end of the prologue. So we can't just discard duplicate
18144 entries, we have to be selective about it. The heuristic we use is
18145 that we only collapse consecutive entries for the same line if at least
18146 one of those entries has a non-zero discriminator. PR 17276.
18147
18148 Note: Addresses in the line number state machine can never go backwards
18149 within one sequence, thus this coalescing is ok. */
18150
18151 static int
18152 dwarf_record_line_p (unsigned int line, unsigned int last_line,
18153 int line_has_non_zero_discriminator,
18154 struct subfile *last_subfile)
18155 {
18156 if (current_subfile != last_subfile)
18157 return 1;
18158 if (line != last_line)
18159 return 1;
18160 /* Same line for the same file that we've seen already.
18161 As a last check, for pr 17276, only record the line if the line
18162 has never had a non-zero discriminator. */
18163 if (!line_has_non_zero_discriminator)
18164 return 1;
18165 return 0;
18166 }
18167
18168 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
18169 in the line table of subfile SUBFILE. */
18170
18171 static void
18172 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
18173 unsigned int line, CORE_ADDR address,
18174 record_line_ftype p_record_line)
18175 {
18176 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
18177
18178 if (dwarf_line_debug)
18179 {
18180 fprintf_unfiltered (gdb_stdlog,
18181 "Recording line %u, file %s, address %s\n",
18182 line, lbasename (subfile->name),
18183 paddress (gdbarch, address));
18184 }
18185
18186 (*p_record_line) (subfile, line, addr);
18187 }
18188
18189 /* Subroutine of dwarf_decode_lines_1 to simplify it.
18190 Mark the end of a set of line number records.
18191 The arguments are the same as for dwarf_record_line_1.
18192 If SUBFILE is NULL the request is ignored. */
18193
18194 static void
18195 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
18196 CORE_ADDR address, record_line_ftype p_record_line)
18197 {
18198 if (subfile == NULL)
18199 return;
18200
18201 if (dwarf_line_debug)
18202 {
18203 fprintf_unfiltered (gdb_stdlog,
18204 "Finishing current line, file %s, address %s\n",
18205 lbasename (subfile->name),
18206 paddress (gdbarch, address));
18207 }
18208
18209 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
18210 }
18211
18212 /* Record the line in STATE.
18213 END_SEQUENCE is non-zero if we're processing the end of a sequence. */
18214
18215 static void
18216 dwarf_record_line (lnp_reader_state *reader, lnp_state_machine *state,
18217 int end_sequence)
18218 {
18219 const struct line_header *lh = reader->line_header;
18220 unsigned int line, discriminator;
18221 int is_stmt;
18222
18223 line = state->line;
18224 is_stmt = state->is_stmt;
18225 discriminator = state->discriminator;
18226
18227 if (dwarf_line_debug)
18228 {
18229 fprintf_unfiltered (gdb_stdlog,
18230 "Processing actual line %u: file %u,"
18231 " address %s, is_stmt %u, discrim %u\n",
18232 line, to_underlying (state->file),
18233 paddress (reader->gdbarch, state->address),
18234 is_stmt, discriminator);
18235 }
18236
18237 file_entry *fe = state->current_file ();
18238
18239 if (fe == NULL)
18240 dwarf2_debug_line_missing_file_complaint ();
18241 /* For now we ignore lines not starting on an instruction boundary.
18242 But not when processing end_sequence for compatibility with the
18243 previous version of the code. */
18244 else if (state->op_index == 0 || end_sequence)
18245 {
18246 fe->included_p = 1;
18247 if (reader->record_lines_p && is_stmt)
18248 {
18249 if (state->last_subfile != current_subfile || end_sequence)
18250 {
18251 dwarf_finish_line (reader->gdbarch, state->last_subfile,
18252 state->address, state->record_line);
18253 }
18254
18255 if (!end_sequence)
18256 {
18257 if (dwarf_record_line_p (line, state->last_line,
18258 state->line_has_non_zero_discriminator,
18259 state->last_subfile))
18260 {
18261 dwarf_record_line_1 (reader->gdbarch, current_subfile,
18262 line, state->address,
18263 state->record_line);
18264 }
18265 state->last_subfile = current_subfile;
18266 state->last_line = line;
18267 }
18268 }
18269 }
18270 }
18271
18272 /* Initialize STATE for the start of a line number program. */
18273
18274 static void
18275 init_lnp_state_machine (lnp_state_machine *state,
18276 const lnp_reader_state *reader)
18277 {
18278 memset (state, 0, sizeof (*state));
18279
18280 /* Just starting, there is no "last file". */
18281 state->last_file = 0;
18282 state->last_subfile = NULL;
18283
18284 state->record_line = record_line;
18285
18286 state->last_line = 0;
18287 state->line_has_non_zero_discriminator = 0;
18288
18289 /* Initialize these according to the DWARF spec. */
18290 state->op_index = 0;
18291 state->file = (file_name_index) 1;
18292 state->line = 1;
18293 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
18294 was a line entry for it so that the backend has a chance to adjust it
18295 and also record it in case it needs it. This is currently used by MIPS
18296 code, cf. `mips_adjust_dwarf2_line'. */
18297 state->address = gdbarch_adjust_dwarf2_line (reader->gdbarch, 0, 0);
18298 state->is_stmt = reader->line_header->default_is_stmt;
18299 state->discriminator = 0;
18300 state->the_line_header = reader->line_header;
18301 }
18302
18303 /* Check address and if invalid nop-out the rest of the lines in this
18304 sequence. */
18305
18306 static void
18307 check_line_address (struct dwarf2_cu *cu, lnp_state_machine *state,
18308 const gdb_byte *line_ptr,
18309 CORE_ADDR lowpc, CORE_ADDR address)
18310 {
18311 /* If address < lowpc then it's not a usable value, it's outside the
18312 pc range of the CU. However, we restrict the test to only address
18313 values of zero to preserve GDB's previous behaviour which is to
18314 handle the specific case of a function being GC'd by the linker. */
18315
18316 if (address == 0 && address < lowpc)
18317 {
18318 /* This line table is for a function which has been
18319 GCd by the linker. Ignore it. PR gdb/12528 */
18320
18321 struct objfile *objfile = cu->objfile;
18322 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
18323
18324 complaint (&symfile_complaints,
18325 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
18326 line_offset, objfile_name (objfile));
18327 state->record_line = noop_record_line;
18328 /* Note: sm.record_line is left as noop_record_line
18329 until we see DW_LNE_end_sequence. */
18330 }
18331 }
18332
18333 /* Subroutine of dwarf_decode_lines to simplify it.
18334 Process the line number information in LH.
18335 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
18336 program in order to set included_p for every referenced header. */
18337
18338 static void
18339 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
18340 const int decode_for_pst_p, CORE_ADDR lowpc)
18341 {
18342 const gdb_byte *line_ptr, *extended_end;
18343 const gdb_byte *line_end;
18344 unsigned int bytes_read, extended_len;
18345 unsigned char op_code, extended_op;
18346 CORE_ADDR baseaddr;
18347 struct objfile *objfile = cu->objfile;
18348 bfd *abfd = objfile->obfd;
18349 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18350 /* Non-zero if we're recording line info (as opposed to building partial
18351 symtabs). */
18352 int record_lines_p = !decode_for_pst_p;
18353 /* A collection of things we need to pass to dwarf_record_line. */
18354 lnp_reader_state reader_state;
18355
18356 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18357
18358 line_ptr = lh->statement_program_start;
18359 line_end = lh->statement_program_end;
18360
18361 reader_state.gdbarch = gdbarch;
18362 reader_state.line_header = lh;
18363 reader_state.record_lines_p = record_lines_p;
18364
18365 /* Read the statement sequences until there's nothing left. */
18366 while (line_ptr < line_end)
18367 {
18368 /* The DWARF line number program state machine. */
18369 lnp_state_machine state_machine;
18370 int end_sequence = 0;
18371
18372 /* Reset the state machine at the start of each sequence. */
18373 init_lnp_state_machine (&state_machine, &reader_state);
18374
18375 if (record_lines_p)
18376 {
18377 /* Start a subfile for the current file of the state
18378 machine. */
18379 const file_entry *fe = state_machine.current_file ();
18380
18381 if (fe != NULL)
18382 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
18383 }
18384
18385 /* Decode the table. */
18386 while (line_ptr < line_end && !end_sequence)
18387 {
18388 op_code = read_1_byte (abfd, line_ptr);
18389 line_ptr += 1;
18390
18391 if (op_code >= lh->opcode_base)
18392 {
18393 /* Special opcode. */
18394 unsigned char adj_opcode;
18395 CORE_ADDR addr_adj;
18396 int line_delta;
18397
18398 adj_opcode = op_code - lh->opcode_base;
18399 addr_adj = (((state_machine.op_index
18400 + (adj_opcode / lh->line_range))
18401 / lh->maximum_ops_per_instruction)
18402 * lh->minimum_instruction_length);
18403 state_machine.address
18404 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18405 state_machine.op_index = ((state_machine.op_index
18406 + (adj_opcode / lh->line_range))
18407 % lh->maximum_ops_per_instruction);
18408 line_delta = lh->line_base + (adj_opcode % lh->line_range);
18409 state_machine.line += line_delta;
18410 if (line_delta != 0)
18411 state_machine.line_has_non_zero_discriminator
18412 = state_machine.discriminator != 0;
18413
18414 dwarf_record_line (&reader_state, &state_machine, 0);
18415 state_machine.discriminator = 0;
18416 }
18417 else switch (op_code)
18418 {
18419 case DW_LNS_extended_op:
18420 extended_len = read_unsigned_leb128 (abfd, line_ptr,
18421 &bytes_read);
18422 line_ptr += bytes_read;
18423 extended_end = line_ptr + extended_len;
18424 extended_op = read_1_byte (abfd, line_ptr);
18425 line_ptr += 1;
18426 switch (extended_op)
18427 {
18428 case DW_LNE_end_sequence:
18429 state_machine.record_line = record_line;
18430 end_sequence = 1;
18431 break;
18432 case DW_LNE_set_address:
18433 {
18434 CORE_ADDR address
18435 = read_address (abfd, line_ptr, cu, &bytes_read);
18436
18437 line_ptr += bytes_read;
18438 check_line_address (cu, &state_machine, line_ptr,
18439 lowpc, address);
18440 state_machine.op_index = 0;
18441 address += baseaddr;
18442 state_machine.address
18443 = gdbarch_adjust_dwarf2_line (gdbarch, address, 0);
18444 }
18445 break;
18446 case DW_LNE_define_file:
18447 {
18448 const char *cur_file;
18449 unsigned int mod_time, length;
18450 dir_index dindex;
18451
18452 cur_file = read_direct_string (abfd, line_ptr,
18453 &bytes_read);
18454 line_ptr += bytes_read;
18455 dindex = (dir_index)
18456 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18457 line_ptr += bytes_read;
18458 mod_time =
18459 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18460 line_ptr += bytes_read;
18461 length =
18462 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18463 line_ptr += bytes_read;
18464 lh->add_file_name (cur_file, dindex, mod_time, length);
18465 }
18466 break;
18467 case DW_LNE_set_discriminator:
18468 /* The discriminator is not interesting to the debugger;
18469 just ignore it. We still need to check its value though:
18470 if there are consecutive entries for the same
18471 (non-prologue) line we want to coalesce them.
18472 PR 17276. */
18473 state_machine.discriminator
18474 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18475 state_machine.line_has_non_zero_discriminator
18476 |= state_machine.discriminator != 0;
18477 line_ptr += bytes_read;
18478 break;
18479 default:
18480 complaint (&symfile_complaints,
18481 _("mangled .debug_line section"));
18482 return;
18483 }
18484 /* Make sure that we parsed the extended op correctly. If e.g.
18485 we expected a different address size than the producer used,
18486 we may have read the wrong number of bytes. */
18487 if (line_ptr != extended_end)
18488 {
18489 complaint (&symfile_complaints,
18490 _("mangled .debug_line section"));
18491 return;
18492 }
18493 break;
18494 case DW_LNS_copy:
18495 dwarf_record_line (&reader_state, &state_machine, 0);
18496 state_machine.discriminator = 0;
18497 break;
18498 case DW_LNS_advance_pc:
18499 {
18500 CORE_ADDR adjust
18501 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18502 CORE_ADDR addr_adj;
18503
18504 addr_adj = (((state_machine.op_index + adjust)
18505 / lh->maximum_ops_per_instruction)
18506 * lh->minimum_instruction_length);
18507 state_machine.address
18508 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18509 state_machine.op_index = ((state_machine.op_index + adjust)
18510 % lh->maximum_ops_per_instruction);
18511 line_ptr += bytes_read;
18512 }
18513 break;
18514 case DW_LNS_advance_line:
18515 {
18516 int line_delta
18517 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
18518
18519 state_machine.line += line_delta;
18520 if (line_delta != 0)
18521 state_machine.line_has_non_zero_discriminator
18522 = state_machine.discriminator != 0;
18523 line_ptr += bytes_read;
18524 }
18525 break;
18526 case DW_LNS_set_file:
18527 {
18528 state_machine.file
18529 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
18530 &bytes_read);
18531 line_ptr += bytes_read;
18532
18533 const file_entry *fe = state_machine.current_file ();
18534 if (fe == NULL)
18535 dwarf2_debug_line_missing_file_complaint ();
18536 else
18537 {
18538 if (record_lines_p)
18539 {
18540 const char *dir = fe->include_dir (lh);
18541
18542 state_machine.last_subfile = current_subfile;
18543 state_machine.line_has_non_zero_discriminator
18544 = state_machine.discriminator != 0;
18545 dwarf2_start_subfile (fe->name, dir);
18546 }
18547 }
18548 }
18549 break;
18550 case DW_LNS_set_column:
18551 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18552 line_ptr += bytes_read;
18553 break;
18554 case DW_LNS_negate_stmt:
18555 state_machine.is_stmt = (!state_machine.is_stmt);
18556 break;
18557 case DW_LNS_set_basic_block:
18558 break;
18559 /* Add to the address register of the state machine the
18560 address increment value corresponding to special opcode
18561 255. I.e., this value is scaled by the minimum
18562 instruction length since special opcode 255 would have
18563 scaled the increment. */
18564 case DW_LNS_const_add_pc:
18565 {
18566 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
18567 CORE_ADDR addr_adj;
18568
18569 addr_adj = (((state_machine.op_index + adjust)
18570 / lh->maximum_ops_per_instruction)
18571 * lh->minimum_instruction_length);
18572 state_machine.address
18573 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18574 state_machine.op_index = ((state_machine.op_index + adjust)
18575 % lh->maximum_ops_per_instruction);
18576 }
18577 break;
18578 case DW_LNS_fixed_advance_pc:
18579 {
18580 CORE_ADDR addr_adj;
18581
18582 addr_adj = read_2_bytes (abfd, line_ptr);
18583 state_machine.address
18584 += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
18585 state_machine.op_index = 0;
18586 line_ptr += 2;
18587 }
18588 break;
18589 default:
18590 {
18591 /* Unknown standard opcode, ignore it. */
18592 int i;
18593
18594 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
18595 {
18596 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
18597 line_ptr += bytes_read;
18598 }
18599 }
18600 }
18601 }
18602
18603 if (!end_sequence)
18604 dwarf2_debug_line_missing_end_sequence_complaint ();
18605
18606 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
18607 in which case we still finish recording the last line). */
18608 dwarf_record_line (&reader_state, &state_machine, 1);
18609 }
18610 }
18611
18612 /* Decode the Line Number Program (LNP) for the given line_header
18613 structure and CU. The actual information extracted and the type
18614 of structures created from the LNP depends on the value of PST.
18615
18616 1. If PST is NULL, then this procedure uses the data from the program
18617 to create all necessary symbol tables, and their linetables.
18618
18619 2. If PST is not NULL, this procedure reads the program to determine
18620 the list of files included by the unit represented by PST, and
18621 builds all the associated partial symbol tables.
18622
18623 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
18624 It is used for relative paths in the line table.
18625 NOTE: When processing partial symtabs (pst != NULL),
18626 comp_dir == pst->dirname.
18627
18628 NOTE: It is important that psymtabs have the same file name (via strcmp)
18629 as the corresponding symtab. Since COMP_DIR is not used in the name of the
18630 symtab we don't use it in the name of the psymtabs we create.
18631 E.g. expand_line_sal requires this when finding psymtabs to expand.
18632 A good testcase for this is mb-inline.exp.
18633
18634 LOWPC is the lowest address in CU (or 0 if not known).
18635
18636 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
18637 for its PC<->lines mapping information. Otherwise only the filename
18638 table is read in. */
18639
18640 static void
18641 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
18642 struct dwarf2_cu *cu, struct partial_symtab *pst,
18643 CORE_ADDR lowpc, int decode_mapping)
18644 {
18645 struct objfile *objfile = cu->objfile;
18646 const int decode_for_pst_p = (pst != NULL);
18647
18648 if (decode_mapping)
18649 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
18650
18651 if (decode_for_pst_p)
18652 {
18653 int file_index;
18654
18655 /* Now that we're done scanning the Line Header Program, we can
18656 create the psymtab of each included file. */
18657 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
18658 if (lh->file_names[file_index].included_p == 1)
18659 {
18660 const char *include_name =
18661 psymtab_include_file_name (lh, file_index, pst, comp_dir);
18662 if (include_name != NULL)
18663 dwarf2_create_include_psymtab (include_name, pst, objfile);
18664 }
18665 }
18666 else
18667 {
18668 /* Make sure a symtab is created for every file, even files
18669 which contain only variables (i.e. no code with associated
18670 line numbers). */
18671 struct compunit_symtab *cust = buildsym_compunit_symtab ();
18672 int i;
18673
18674 for (i = 0; i < lh->file_names.size (); i++)
18675 {
18676 file_entry &fe = lh->file_names[i];
18677
18678 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
18679
18680 if (current_subfile->symtab == NULL)
18681 {
18682 current_subfile->symtab
18683 = allocate_symtab (cust, current_subfile->name);
18684 }
18685 fe.symtab = current_subfile->symtab;
18686 }
18687 }
18688 }
18689
18690 /* Start a subfile for DWARF. FILENAME is the name of the file and
18691 DIRNAME the name of the source directory which contains FILENAME
18692 or NULL if not known.
18693 This routine tries to keep line numbers from identical absolute and
18694 relative file names in a common subfile.
18695
18696 Using the `list' example from the GDB testsuite, which resides in
18697 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
18698 of /srcdir/list0.c yields the following debugging information for list0.c:
18699
18700 DW_AT_name: /srcdir/list0.c
18701 DW_AT_comp_dir: /compdir
18702 files.files[0].name: list0.h
18703 files.files[0].dir: /srcdir
18704 files.files[1].name: list0.c
18705 files.files[1].dir: /srcdir
18706
18707 The line number information for list0.c has to end up in a single
18708 subfile, so that `break /srcdir/list0.c:1' works as expected.
18709 start_subfile will ensure that this happens provided that we pass the
18710 concatenation of files.files[1].dir and files.files[1].name as the
18711 subfile's name. */
18712
18713 static void
18714 dwarf2_start_subfile (const char *filename, const char *dirname)
18715 {
18716 char *copy = NULL;
18717
18718 /* In order not to lose the line information directory,
18719 we concatenate it to the filename when it makes sense.
18720 Note that the Dwarf3 standard says (speaking of filenames in line
18721 information): ``The directory index is ignored for file names
18722 that represent full path names''. Thus ignoring dirname in the
18723 `else' branch below isn't an issue. */
18724
18725 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
18726 {
18727 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
18728 filename = copy;
18729 }
18730
18731 start_subfile (filename);
18732
18733 if (copy != NULL)
18734 xfree (copy);
18735 }
18736
18737 /* Start a symtab for DWARF.
18738 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
18739
18740 static struct compunit_symtab *
18741 dwarf2_start_symtab (struct dwarf2_cu *cu,
18742 const char *name, const char *comp_dir, CORE_ADDR low_pc)
18743 {
18744 struct compunit_symtab *cust
18745 = start_symtab (cu->objfile, name, comp_dir, low_pc);
18746
18747 record_debugformat ("DWARF 2");
18748 record_producer (cu->producer);
18749
18750 /* We assume that we're processing GCC output. */
18751 processing_gcc_compilation = 2;
18752
18753 cu->processing_has_namespace_info = 0;
18754
18755 return cust;
18756 }
18757
18758 static void
18759 var_decode_location (struct attribute *attr, struct symbol *sym,
18760 struct dwarf2_cu *cu)
18761 {
18762 struct objfile *objfile = cu->objfile;
18763 struct comp_unit_head *cu_header = &cu->header;
18764
18765 /* NOTE drow/2003-01-30: There used to be a comment and some special
18766 code here to turn a symbol with DW_AT_external and a
18767 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
18768 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
18769 with some versions of binutils) where shared libraries could have
18770 relocations against symbols in their debug information - the
18771 minimal symbol would have the right address, but the debug info
18772 would not. It's no longer necessary, because we will explicitly
18773 apply relocations when we read in the debug information now. */
18774
18775 /* A DW_AT_location attribute with no contents indicates that a
18776 variable has been optimized away. */
18777 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
18778 {
18779 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
18780 return;
18781 }
18782
18783 /* Handle one degenerate form of location expression specially, to
18784 preserve GDB's previous behavior when section offsets are
18785 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
18786 then mark this symbol as LOC_STATIC. */
18787
18788 if (attr_form_is_block (attr)
18789 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
18790 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
18791 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
18792 && (DW_BLOCK (attr)->size
18793 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
18794 {
18795 unsigned int dummy;
18796
18797 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
18798 SYMBOL_VALUE_ADDRESS (sym) =
18799 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
18800 else
18801 SYMBOL_VALUE_ADDRESS (sym) =
18802 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
18803 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
18804 fixup_symbol_section (sym, objfile);
18805 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
18806 SYMBOL_SECTION (sym));
18807 return;
18808 }
18809
18810 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
18811 expression evaluator, and use LOC_COMPUTED only when necessary
18812 (i.e. when the value of a register or memory location is
18813 referenced, or a thread-local block, etc.). Then again, it might
18814 not be worthwhile. I'm assuming that it isn't unless performance
18815 or memory numbers show me otherwise. */
18816
18817 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
18818
18819 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
18820 cu->has_loclist = 1;
18821 }
18822
18823 /* Given a pointer to a DWARF information entry, figure out if we need
18824 to make a symbol table entry for it, and if so, create a new entry
18825 and return a pointer to it.
18826 If TYPE is NULL, determine symbol type from the die, otherwise
18827 used the passed type.
18828 If SPACE is not NULL, use it to hold the new symbol. If it is
18829 NULL, allocate a new symbol on the objfile's obstack. */
18830
18831 static struct symbol *
18832 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
18833 struct symbol *space)
18834 {
18835 struct objfile *objfile = cu->objfile;
18836 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18837 struct symbol *sym = NULL;
18838 const char *name;
18839 struct attribute *attr = NULL;
18840 struct attribute *attr2 = NULL;
18841 CORE_ADDR baseaddr;
18842 struct pending **list_to_add = NULL;
18843
18844 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
18845
18846 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18847
18848 name = dwarf2_name (die, cu);
18849 if (name)
18850 {
18851 const char *linkagename;
18852 int suppress_add = 0;
18853
18854 if (space)
18855 sym = space;
18856 else
18857 sym = allocate_symbol (objfile);
18858 OBJSTAT (objfile, n_syms++);
18859
18860 /* Cache this symbol's name and the name's demangled form (if any). */
18861 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
18862 linkagename = dwarf2_physname (name, die, cu);
18863 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
18864
18865 /* Fortran does not have mangling standard and the mangling does differ
18866 between gfortran, iFort etc. */
18867 if (cu->language == language_fortran
18868 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
18869 symbol_set_demangled_name (&(sym->ginfo),
18870 dwarf2_full_name (name, die, cu),
18871 NULL);
18872
18873 /* Default assumptions.
18874 Use the passed type or decode it from the die. */
18875 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
18876 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
18877 if (type != NULL)
18878 SYMBOL_TYPE (sym) = type;
18879 else
18880 SYMBOL_TYPE (sym) = die_type (die, cu);
18881 attr = dwarf2_attr (die,
18882 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
18883 cu);
18884 if (attr)
18885 {
18886 SYMBOL_LINE (sym) = DW_UNSND (attr);
18887 }
18888
18889 attr = dwarf2_attr (die,
18890 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
18891 cu);
18892 if (attr)
18893 {
18894 file_name_index file_index = (file_name_index) DW_UNSND (attr);
18895 struct file_entry *fe;
18896
18897 if (cu->line_header != NULL)
18898 fe = cu->line_header->file_name_at (file_index);
18899 else
18900 fe = NULL;
18901
18902 if (fe == NULL)
18903 complaint (&symfile_complaints,
18904 _("file index out of range"));
18905 else
18906 symbol_set_symtab (sym, fe->symtab);
18907 }
18908
18909 switch (die->tag)
18910 {
18911 case DW_TAG_label:
18912 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
18913 if (attr)
18914 {
18915 CORE_ADDR addr;
18916
18917 addr = attr_value_as_address (attr);
18918 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
18919 SYMBOL_VALUE_ADDRESS (sym) = addr;
18920 }
18921 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
18922 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
18923 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
18924 add_symbol_to_list (sym, cu->list_in_scope);
18925 break;
18926 case DW_TAG_subprogram:
18927 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18928 finish_block. */
18929 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
18930 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18931 if ((attr2 && (DW_UNSND (attr2) != 0))
18932 || cu->language == language_ada)
18933 {
18934 /* Subprograms marked external are stored as a global symbol.
18935 Ada subprograms, whether marked external or not, are always
18936 stored as a global symbol, because we want to be able to
18937 access them globally. For instance, we want to be able
18938 to break on a nested subprogram without having to
18939 specify the context. */
18940 list_to_add = &global_symbols;
18941 }
18942 else
18943 {
18944 list_to_add = cu->list_in_scope;
18945 }
18946 break;
18947 case DW_TAG_inlined_subroutine:
18948 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
18949 finish_block. */
18950 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
18951 SYMBOL_INLINED (sym) = 1;
18952 list_to_add = cu->list_in_scope;
18953 break;
18954 case DW_TAG_template_value_param:
18955 suppress_add = 1;
18956 /* Fall through. */
18957 case DW_TAG_constant:
18958 case DW_TAG_variable:
18959 case DW_TAG_member:
18960 /* Compilation with minimal debug info may result in
18961 variables with missing type entries. Change the
18962 misleading `void' type to something sensible. */
18963 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
18964 SYMBOL_TYPE (sym)
18965 = objfile_type (objfile)->nodebug_data_symbol;
18966
18967 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18968 /* In the case of DW_TAG_member, we should only be called for
18969 static const members. */
18970 if (die->tag == DW_TAG_member)
18971 {
18972 /* dwarf2_add_field uses die_is_declaration,
18973 so we do the same. */
18974 gdb_assert (die_is_declaration (die, cu));
18975 gdb_assert (attr);
18976 }
18977 if (attr)
18978 {
18979 dwarf2_const_value (attr, sym, cu);
18980 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18981 if (!suppress_add)
18982 {
18983 if (attr2 && (DW_UNSND (attr2) != 0))
18984 list_to_add = &global_symbols;
18985 else
18986 list_to_add = cu->list_in_scope;
18987 }
18988 break;
18989 }
18990 attr = dwarf2_attr (die, DW_AT_location, cu);
18991 if (attr)
18992 {
18993 var_decode_location (attr, sym, cu);
18994 attr2 = dwarf2_attr (die, DW_AT_external, cu);
18995
18996 /* Fortran explicitly imports any global symbols to the local
18997 scope by DW_TAG_common_block. */
18998 if (cu->language == language_fortran && die->parent
18999 && die->parent->tag == DW_TAG_common_block)
19000 attr2 = NULL;
19001
19002 if (SYMBOL_CLASS (sym) == LOC_STATIC
19003 && SYMBOL_VALUE_ADDRESS (sym) == 0
19004 && !dwarf2_per_objfile->has_section_at_zero)
19005 {
19006 /* When a static variable is eliminated by the linker,
19007 the corresponding debug information is not stripped
19008 out, but the variable address is set to null;
19009 do not add such variables into symbol table. */
19010 }
19011 else if (attr2 && (DW_UNSND (attr2) != 0))
19012 {
19013 /* Workaround gfortran PR debug/40040 - it uses
19014 DW_AT_location for variables in -fPIC libraries which may
19015 get overriden by other libraries/executable and get
19016 a different address. Resolve it by the minimal symbol
19017 which may come from inferior's executable using copy
19018 relocation. Make this workaround only for gfortran as for
19019 other compilers GDB cannot guess the minimal symbol
19020 Fortran mangling kind. */
19021 if (cu->language == language_fortran && die->parent
19022 && die->parent->tag == DW_TAG_module
19023 && cu->producer
19024 && startswith (cu->producer, "GNU Fortran"))
19025 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
19026
19027 /* A variable with DW_AT_external is never static,
19028 but it may be block-scoped. */
19029 list_to_add = (cu->list_in_scope == &file_symbols
19030 ? &global_symbols : cu->list_in_scope);
19031 }
19032 else
19033 list_to_add = cu->list_in_scope;
19034 }
19035 else
19036 {
19037 /* We do not know the address of this symbol.
19038 If it is an external symbol and we have type information
19039 for it, enter the symbol as a LOC_UNRESOLVED symbol.
19040 The address of the variable will then be determined from
19041 the minimal symbol table whenever the variable is
19042 referenced. */
19043 attr2 = dwarf2_attr (die, DW_AT_external, cu);
19044
19045 /* Fortran explicitly imports any global symbols to the local
19046 scope by DW_TAG_common_block. */
19047 if (cu->language == language_fortran && die->parent
19048 && die->parent->tag == DW_TAG_common_block)
19049 {
19050 /* SYMBOL_CLASS doesn't matter here because
19051 read_common_block is going to reset it. */
19052 if (!suppress_add)
19053 list_to_add = cu->list_in_scope;
19054 }
19055 else if (attr2 && (DW_UNSND (attr2) != 0)
19056 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
19057 {
19058 /* A variable with DW_AT_external is never static, but it
19059 may be block-scoped. */
19060 list_to_add = (cu->list_in_scope == &file_symbols
19061 ? &global_symbols : cu->list_in_scope);
19062
19063 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
19064 }
19065 else if (!die_is_declaration (die, cu))
19066 {
19067 /* Use the default LOC_OPTIMIZED_OUT class. */
19068 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
19069 if (!suppress_add)
19070 list_to_add = cu->list_in_scope;
19071 }
19072 }
19073 break;
19074 case DW_TAG_formal_parameter:
19075 /* If we are inside a function, mark this as an argument. If
19076 not, we might be looking at an argument to an inlined function
19077 when we do not have enough information to show inlined frames;
19078 pretend it's a local variable in that case so that the user can
19079 still see it. */
19080 if (context_stack_depth > 0
19081 && context_stack[context_stack_depth - 1].name != NULL)
19082 SYMBOL_IS_ARGUMENT (sym) = 1;
19083 attr = dwarf2_attr (die, DW_AT_location, cu);
19084 if (attr)
19085 {
19086 var_decode_location (attr, sym, cu);
19087 }
19088 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19089 if (attr)
19090 {
19091 dwarf2_const_value (attr, sym, cu);
19092 }
19093
19094 list_to_add = cu->list_in_scope;
19095 break;
19096 case DW_TAG_unspecified_parameters:
19097 /* From varargs functions; gdb doesn't seem to have any
19098 interest in this information, so just ignore it for now.
19099 (FIXME?) */
19100 break;
19101 case DW_TAG_template_type_param:
19102 suppress_add = 1;
19103 /* Fall through. */
19104 case DW_TAG_class_type:
19105 case DW_TAG_interface_type:
19106 case DW_TAG_structure_type:
19107 case DW_TAG_union_type:
19108 case DW_TAG_set_type:
19109 case DW_TAG_enumeration_type:
19110 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19111 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
19112
19113 {
19114 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
19115 really ever be static objects: otherwise, if you try
19116 to, say, break of a class's method and you're in a file
19117 which doesn't mention that class, it won't work unless
19118 the check for all static symbols in lookup_symbol_aux
19119 saves you. See the OtherFileClass tests in
19120 gdb.c++/namespace.exp. */
19121
19122 if (!suppress_add)
19123 {
19124 list_to_add = (cu->list_in_scope == &file_symbols
19125 && cu->language == language_cplus
19126 ? &global_symbols : cu->list_in_scope);
19127
19128 /* The semantics of C++ state that "struct foo {
19129 ... }" also defines a typedef for "foo". */
19130 if (cu->language == language_cplus
19131 || cu->language == language_ada
19132 || cu->language == language_d
19133 || cu->language == language_rust)
19134 {
19135 /* The symbol's name is already allocated along
19136 with this objfile, so we don't need to
19137 duplicate it for the type. */
19138 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
19139 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
19140 }
19141 }
19142 }
19143 break;
19144 case DW_TAG_typedef:
19145 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19146 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
19147 list_to_add = cu->list_in_scope;
19148 break;
19149 case DW_TAG_base_type:
19150 case DW_TAG_subrange_type:
19151 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19152 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
19153 list_to_add = cu->list_in_scope;
19154 break;
19155 case DW_TAG_enumerator:
19156 attr = dwarf2_attr (die, DW_AT_const_value, cu);
19157 if (attr)
19158 {
19159 dwarf2_const_value (attr, sym, cu);
19160 }
19161 {
19162 /* NOTE: carlton/2003-11-10: See comment above in the
19163 DW_TAG_class_type, etc. block. */
19164
19165 list_to_add = (cu->list_in_scope == &file_symbols
19166 && cu->language == language_cplus
19167 ? &global_symbols : cu->list_in_scope);
19168 }
19169 break;
19170 case DW_TAG_imported_declaration:
19171 case DW_TAG_namespace:
19172 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19173 list_to_add = &global_symbols;
19174 break;
19175 case DW_TAG_module:
19176 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
19177 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
19178 list_to_add = &global_symbols;
19179 break;
19180 case DW_TAG_common_block:
19181 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
19182 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
19183 add_symbol_to_list (sym, cu->list_in_scope);
19184 break;
19185 default:
19186 /* Not a tag we recognize. Hopefully we aren't processing
19187 trash data, but since we must specifically ignore things
19188 we don't recognize, there is nothing else we should do at
19189 this point. */
19190 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
19191 dwarf_tag_name (die->tag));
19192 break;
19193 }
19194
19195 if (suppress_add)
19196 {
19197 sym->hash_next = objfile->template_symbols;
19198 objfile->template_symbols = sym;
19199 list_to_add = NULL;
19200 }
19201
19202 if (list_to_add != NULL)
19203 add_symbol_to_list (sym, list_to_add);
19204
19205 /* For the benefit of old versions of GCC, check for anonymous
19206 namespaces based on the demangled name. */
19207 if (!cu->processing_has_namespace_info
19208 && cu->language == language_cplus)
19209 cp_scan_for_anonymous_namespaces (sym, objfile);
19210 }
19211 return (sym);
19212 }
19213
19214 /* A wrapper for new_symbol_full that always allocates a new symbol. */
19215
19216 static struct symbol *
19217 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19218 {
19219 return new_symbol_full (die, type, cu, NULL);
19220 }
19221
19222 /* Given an attr with a DW_FORM_dataN value in host byte order,
19223 zero-extend it as appropriate for the symbol's type. The DWARF
19224 standard (v4) is not entirely clear about the meaning of using
19225 DW_FORM_dataN for a constant with a signed type, where the type is
19226 wider than the data. The conclusion of a discussion on the DWARF
19227 list was that this is unspecified. We choose to always zero-extend
19228 because that is the interpretation long in use by GCC. */
19229
19230 static gdb_byte *
19231 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
19232 struct dwarf2_cu *cu, LONGEST *value, int bits)
19233 {
19234 struct objfile *objfile = cu->objfile;
19235 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
19236 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
19237 LONGEST l = DW_UNSND (attr);
19238
19239 if (bits < sizeof (*value) * 8)
19240 {
19241 l &= ((LONGEST) 1 << bits) - 1;
19242 *value = l;
19243 }
19244 else if (bits == sizeof (*value) * 8)
19245 *value = l;
19246 else
19247 {
19248 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
19249 store_unsigned_integer (bytes, bits / 8, byte_order, l);
19250 return bytes;
19251 }
19252
19253 return NULL;
19254 }
19255
19256 /* Read a constant value from an attribute. Either set *VALUE, or if
19257 the value does not fit in *VALUE, set *BYTES - either already
19258 allocated on the objfile obstack, or newly allocated on OBSTACK,
19259 or, set *BATON, if we translated the constant to a location
19260 expression. */
19261
19262 static void
19263 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
19264 const char *name, struct obstack *obstack,
19265 struct dwarf2_cu *cu,
19266 LONGEST *value, const gdb_byte **bytes,
19267 struct dwarf2_locexpr_baton **baton)
19268 {
19269 struct objfile *objfile = cu->objfile;
19270 struct comp_unit_head *cu_header = &cu->header;
19271 struct dwarf_block *blk;
19272 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
19273 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19274
19275 *value = 0;
19276 *bytes = NULL;
19277 *baton = NULL;
19278
19279 switch (attr->form)
19280 {
19281 case DW_FORM_addr:
19282 case DW_FORM_GNU_addr_index:
19283 {
19284 gdb_byte *data;
19285
19286 if (TYPE_LENGTH (type) != cu_header->addr_size)
19287 dwarf2_const_value_length_mismatch_complaint (name,
19288 cu_header->addr_size,
19289 TYPE_LENGTH (type));
19290 /* Symbols of this form are reasonably rare, so we just
19291 piggyback on the existing location code rather than writing
19292 a new implementation of symbol_computed_ops. */
19293 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
19294 (*baton)->per_cu = cu->per_cu;
19295 gdb_assert ((*baton)->per_cu);
19296
19297 (*baton)->size = 2 + cu_header->addr_size;
19298 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
19299 (*baton)->data = data;
19300
19301 data[0] = DW_OP_addr;
19302 store_unsigned_integer (&data[1], cu_header->addr_size,
19303 byte_order, DW_ADDR (attr));
19304 data[cu_header->addr_size + 1] = DW_OP_stack_value;
19305 }
19306 break;
19307 case DW_FORM_string:
19308 case DW_FORM_strp:
19309 case DW_FORM_GNU_str_index:
19310 case DW_FORM_GNU_strp_alt:
19311 /* DW_STRING is already allocated on the objfile obstack, point
19312 directly to it. */
19313 *bytes = (const gdb_byte *) DW_STRING (attr);
19314 break;
19315 case DW_FORM_block1:
19316 case DW_FORM_block2:
19317 case DW_FORM_block4:
19318 case DW_FORM_block:
19319 case DW_FORM_exprloc:
19320 case DW_FORM_data16:
19321 blk = DW_BLOCK (attr);
19322 if (TYPE_LENGTH (type) != blk->size)
19323 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
19324 TYPE_LENGTH (type));
19325 *bytes = blk->data;
19326 break;
19327
19328 /* The DW_AT_const_value attributes are supposed to carry the
19329 symbol's value "represented as it would be on the target
19330 architecture." By the time we get here, it's already been
19331 converted to host endianness, so we just need to sign- or
19332 zero-extend it as appropriate. */
19333 case DW_FORM_data1:
19334 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
19335 break;
19336 case DW_FORM_data2:
19337 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
19338 break;
19339 case DW_FORM_data4:
19340 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
19341 break;
19342 case DW_FORM_data8:
19343 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
19344 break;
19345
19346 case DW_FORM_sdata:
19347 *value = DW_SND (attr);
19348 break;
19349
19350 case DW_FORM_udata:
19351 *value = DW_UNSND (attr);
19352 break;
19353
19354 default:
19355 complaint (&symfile_complaints,
19356 _("unsupported const value attribute form: '%s'"),
19357 dwarf_form_name (attr->form));
19358 *value = 0;
19359 break;
19360 }
19361 }
19362
19363
19364 /* Copy constant value from an attribute to a symbol. */
19365
19366 static void
19367 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
19368 struct dwarf2_cu *cu)
19369 {
19370 struct objfile *objfile = cu->objfile;
19371 LONGEST value;
19372 const gdb_byte *bytes;
19373 struct dwarf2_locexpr_baton *baton;
19374
19375 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
19376 SYMBOL_PRINT_NAME (sym),
19377 &objfile->objfile_obstack, cu,
19378 &value, &bytes, &baton);
19379
19380 if (baton != NULL)
19381 {
19382 SYMBOL_LOCATION_BATON (sym) = baton;
19383 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
19384 }
19385 else if (bytes != NULL)
19386 {
19387 SYMBOL_VALUE_BYTES (sym) = bytes;
19388 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
19389 }
19390 else
19391 {
19392 SYMBOL_VALUE (sym) = value;
19393 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
19394 }
19395 }
19396
19397 /* Return the type of the die in question using its DW_AT_type attribute. */
19398
19399 static struct type *
19400 die_type (struct die_info *die, struct dwarf2_cu *cu)
19401 {
19402 struct attribute *type_attr;
19403
19404 type_attr = dwarf2_attr (die, DW_AT_type, cu);
19405 if (!type_attr)
19406 {
19407 /* A missing DW_AT_type represents a void type. */
19408 return objfile_type (cu->objfile)->builtin_void;
19409 }
19410
19411 return lookup_die_type (die, type_attr, cu);
19412 }
19413
19414 /* True iff CU's producer generates GNAT Ada auxiliary information
19415 that allows to find parallel types through that information instead
19416 of having to do expensive parallel lookups by type name. */
19417
19418 static int
19419 need_gnat_info (struct dwarf2_cu *cu)
19420 {
19421 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
19422 of GNAT produces this auxiliary information, without any indication
19423 that it is produced. Part of enhancing the FSF version of GNAT
19424 to produce that information will be to put in place an indicator
19425 that we can use in order to determine whether the descriptive type
19426 info is available or not. One suggestion that has been made is
19427 to use a new attribute, attached to the CU die. For now, assume
19428 that the descriptive type info is not available. */
19429 return 0;
19430 }
19431
19432 /* Return the auxiliary type of the die in question using its
19433 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
19434 attribute is not present. */
19435
19436 static struct type *
19437 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
19438 {
19439 struct attribute *type_attr;
19440
19441 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
19442 if (!type_attr)
19443 return NULL;
19444
19445 return lookup_die_type (die, type_attr, cu);
19446 }
19447
19448 /* If DIE has a descriptive_type attribute, then set the TYPE's
19449 descriptive type accordingly. */
19450
19451 static void
19452 set_descriptive_type (struct type *type, struct die_info *die,
19453 struct dwarf2_cu *cu)
19454 {
19455 struct type *descriptive_type = die_descriptive_type (die, cu);
19456
19457 if (descriptive_type)
19458 {
19459 ALLOCATE_GNAT_AUX_TYPE (type);
19460 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
19461 }
19462 }
19463
19464 /* Return the containing type of the die in question using its
19465 DW_AT_containing_type attribute. */
19466
19467 static struct type *
19468 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
19469 {
19470 struct attribute *type_attr;
19471
19472 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
19473 if (!type_attr)
19474 error (_("Dwarf Error: Problem turning containing type into gdb type "
19475 "[in module %s]"), objfile_name (cu->objfile));
19476
19477 return lookup_die_type (die, type_attr, cu);
19478 }
19479
19480 /* Return an error marker type to use for the ill formed type in DIE/CU. */
19481
19482 static struct type *
19483 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
19484 {
19485 struct objfile *objfile = dwarf2_per_objfile->objfile;
19486 char *message, *saved;
19487
19488 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
19489 objfile_name (objfile),
19490 cu->header.offset.sect_off,
19491 die->offset.sect_off);
19492 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
19493 message, strlen (message));
19494 xfree (message);
19495
19496 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
19497 }
19498
19499 /* Look up the type of DIE in CU using its type attribute ATTR.
19500 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
19501 DW_AT_containing_type.
19502 If there is no type substitute an error marker. */
19503
19504 static struct type *
19505 lookup_die_type (struct die_info *die, const struct attribute *attr,
19506 struct dwarf2_cu *cu)
19507 {
19508 struct objfile *objfile = cu->objfile;
19509 struct type *this_type;
19510
19511 gdb_assert (attr->name == DW_AT_type
19512 || attr->name == DW_AT_GNAT_descriptive_type
19513 || attr->name == DW_AT_containing_type);
19514
19515 /* First see if we have it cached. */
19516
19517 if (attr->form == DW_FORM_GNU_ref_alt)
19518 {
19519 struct dwarf2_per_cu_data *per_cu;
19520 sect_offset offset = dwarf2_get_ref_die_offset (attr);
19521
19522 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
19523 this_type = get_die_type_at_offset (offset, per_cu);
19524 }
19525 else if (attr_form_is_ref (attr))
19526 {
19527 sect_offset offset = dwarf2_get_ref_die_offset (attr);
19528
19529 this_type = get_die_type_at_offset (offset, cu->per_cu);
19530 }
19531 else if (attr->form == DW_FORM_ref_sig8)
19532 {
19533 ULONGEST signature = DW_SIGNATURE (attr);
19534
19535 return get_signatured_type (die, signature, cu);
19536 }
19537 else
19538 {
19539 complaint (&symfile_complaints,
19540 _("Dwarf Error: Bad type attribute %s in DIE"
19541 " at 0x%x [in module %s]"),
19542 dwarf_attr_name (attr->name), die->offset.sect_off,
19543 objfile_name (objfile));
19544 return build_error_marker_type (cu, die);
19545 }
19546
19547 /* If not cached we need to read it in. */
19548
19549 if (this_type == NULL)
19550 {
19551 struct die_info *type_die = NULL;
19552 struct dwarf2_cu *type_cu = cu;
19553
19554 if (attr_form_is_ref (attr))
19555 type_die = follow_die_ref (die, attr, &type_cu);
19556 if (type_die == NULL)
19557 return build_error_marker_type (cu, die);
19558 /* If we find the type now, it's probably because the type came
19559 from an inter-CU reference and the type's CU got expanded before
19560 ours. */
19561 this_type = read_type_die (type_die, type_cu);
19562 }
19563
19564 /* If we still don't have a type use an error marker. */
19565
19566 if (this_type == NULL)
19567 return build_error_marker_type (cu, die);
19568
19569 return this_type;
19570 }
19571
19572 /* Return the type in DIE, CU.
19573 Returns NULL for invalid types.
19574
19575 This first does a lookup in die_type_hash,
19576 and only reads the die in if necessary.
19577
19578 NOTE: This can be called when reading in partial or full symbols. */
19579
19580 static struct type *
19581 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
19582 {
19583 struct type *this_type;
19584
19585 this_type = get_die_type (die, cu);
19586 if (this_type)
19587 return this_type;
19588
19589 return read_type_die_1 (die, cu);
19590 }
19591
19592 /* Read the type in DIE, CU.
19593 Returns NULL for invalid types. */
19594
19595 static struct type *
19596 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
19597 {
19598 struct type *this_type = NULL;
19599
19600 switch (die->tag)
19601 {
19602 case DW_TAG_class_type:
19603 case DW_TAG_interface_type:
19604 case DW_TAG_structure_type:
19605 case DW_TAG_union_type:
19606 this_type = read_structure_type (die, cu);
19607 break;
19608 case DW_TAG_enumeration_type:
19609 this_type = read_enumeration_type (die, cu);
19610 break;
19611 case DW_TAG_subprogram:
19612 case DW_TAG_subroutine_type:
19613 case DW_TAG_inlined_subroutine:
19614 this_type = read_subroutine_type (die, cu);
19615 break;
19616 case DW_TAG_array_type:
19617 this_type = read_array_type (die, cu);
19618 break;
19619 case DW_TAG_set_type:
19620 this_type = read_set_type (die, cu);
19621 break;
19622 case DW_TAG_pointer_type:
19623 this_type = read_tag_pointer_type (die, cu);
19624 break;
19625 case DW_TAG_ptr_to_member_type:
19626 this_type = read_tag_ptr_to_member_type (die, cu);
19627 break;
19628 case DW_TAG_reference_type:
19629 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
19630 break;
19631 case DW_TAG_rvalue_reference_type:
19632 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
19633 break;
19634 case DW_TAG_const_type:
19635 this_type = read_tag_const_type (die, cu);
19636 break;
19637 case DW_TAG_volatile_type:
19638 this_type = read_tag_volatile_type (die, cu);
19639 break;
19640 case DW_TAG_restrict_type:
19641 this_type = read_tag_restrict_type (die, cu);
19642 break;
19643 case DW_TAG_string_type:
19644 this_type = read_tag_string_type (die, cu);
19645 break;
19646 case DW_TAG_typedef:
19647 this_type = read_typedef (die, cu);
19648 break;
19649 case DW_TAG_subrange_type:
19650 this_type = read_subrange_type (die, cu);
19651 break;
19652 case DW_TAG_base_type:
19653 this_type = read_base_type (die, cu);
19654 break;
19655 case DW_TAG_unspecified_type:
19656 this_type = read_unspecified_type (die, cu);
19657 break;
19658 case DW_TAG_namespace:
19659 this_type = read_namespace_type (die, cu);
19660 break;
19661 case DW_TAG_module:
19662 this_type = read_module_type (die, cu);
19663 break;
19664 case DW_TAG_atomic_type:
19665 this_type = read_tag_atomic_type (die, cu);
19666 break;
19667 default:
19668 complaint (&symfile_complaints,
19669 _("unexpected tag in read_type_die: '%s'"),
19670 dwarf_tag_name (die->tag));
19671 break;
19672 }
19673
19674 return this_type;
19675 }
19676
19677 /* See if we can figure out if the class lives in a namespace. We do
19678 this by looking for a member function; its demangled name will
19679 contain namespace info, if there is any.
19680 Return the computed name or NULL.
19681 Space for the result is allocated on the objfile's obstack.
19682 This is the full-die version of guess_partial_die_structure_name.
19683 In this case we know DIE has no useful parent. */
19684
19685 static char *
19686 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
19687 {
19688 struct die_info *spec_die;
19689 struct dwarf2_cu *spec_cu;
19690 struct die_info *child;
19691
19692 spec_cu = cu;
19693 spec_die = die_specification (die, &spec_cu);
19694 if (spec_die != NULL)
19695 {
19696 die = spec_die;
19697 cu = spec_cu;
19698 }
19699
19700 for (child = die->child;
19701 child != NULL;
19702 child = child->sibling)
19703 {
19704 if (child->tag == DW_TAG_subprogram)
19705 {
19706 const char *linkage_name;
19707
19708 linkage_name = dwarf2_string_attr (child, DW_AT_linkage_name, cu);
19709 if (linkage_name == NULL)
19710 linkage_name = dwarf2_string_attr (child, DW_AT_MIPS_linkage_name,
19711 cu);
19712 if (linkage_name != NULL)
19713 {
19714 char *actual_name
19715 = language_class_name_from_physname (cu->language_defn,
19716 linkage_name);
19717 char *name = NULL;
19718
19719 if (actual_name != NULL)
19720 {
19721 const char *die_name = dwarf2_name (die, cu);
19722
19723 if (die_name != NULL
19724 && strcmp (die_name, actual_name) != 0)
19725 {
19726 /* Strip off the class name from the full name.
19727 We want the prefix. */
19728 int die_name_len = strlen (die_name);
19729 int actual_name_len = strlen (actual_name);
19730
19731 /* Test for '::' as a sanity check. */
19732 if (actual_name_len > die_name_len + 2
19733 && actual_name[actual_name_len
19734 - die_name_len - 1] == ':')
19735 name = (char *) obstack_copy0 (
19736 &cu->objfile->per_bfd->storage_obstack,
19737 actual_name, actual_name_len - die_name_len - 2);
19738 }
19739 }
19740 xfree (actual_name);
19741 return name;
19742 }
19743 }
19744 }
19745
19746 return NULL;
19747 }
19748
19749 /* GCC might emit a nameless typedef that has a linkage name. Determine the
19750 prefix part in such case. See
19751 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19752
19753 static char *
19754 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
19755 {
19756 struct attribute *attr;
19757 const char *base;
19758
19759 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
19760 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
19761 return NULL;
19762
19763 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
19764 return NULL;
19765
19766 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
19767 if (attr == NULL)
19768 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
19769 if (attr == NULL || DW_STRING (attr) == NULL)
19770 return NULL;
19771
19772 /* dwarf2_name had to be already called. */
19773 gdb_assert (DW_STRING_IS_CANONICAL (attr));
19774
19775 /* Strip the base name, keep any leading namespaces/classes. */
19776 base = strrchr (DW_STRING (attr), ':');
19777 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
19778 return "";
19779
19780 return (char *) obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
19781 DW_STRING (attr),
19782 &base[-1] - DW_STRING (attr));
19783 }
19784
19785 /* Return the name of the namespace/class that DIE is defined within,
19786 or "" if we can't tell. The caller should not xfree the result.
19787
19788 For example, if we're within the method foo() in the following
19789 code:
19790
19791 namespace N {
19792 class C {
19793 void foo () {
19794 }
19795 };
19796 }
19797
19798 then determine_prefix on foo's die will return "N::C". */
19799
19800 static const char *
19801 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
19802 {
19803 struct die_info *parent, *spec_die;
19804 struct dwarf2_cu *spec_cu;
19805 struct type *parent_type;
19806 char *retval;
19807
19808 if (cu->language != language_cplus
19809 && cu->language != language_fortran && cu->language != language_d
19810 && cu->language != language_rust)
19811 return "";
19812
19813 retval = anonymous_struct_prefix (die, cu);
19814 if (retval)
19815 return retval;
19816
19817 /* We have to be careful in the presence of DW_AT_specification.
19818 For example, with GCC 3.4, given the code
19819
19820 namespace N {
19821 void foo() {
19822 // Definition of N::foo.
19823 }
19824 }
19825
19826 then we'll have a tree of DIEs like this:
19827
19828 1: DW_TAG_compile_unit
19829 2: DW_TAG_namespace // N
19830 3: DW_TAG_subprogram // declaration of N::foo
19831 4: DW_TAG_subprogram // definition of N::foo
19832 DW_AT_specification // refers to die #3
19833
19834 Thus, when processing die #4, we have to pretend that we're in
19835 the context of its DW_AT_specification, namely the contex of die
19836 #3. */
19837 spec_cu = cu;
19838 spec_die = die_specification (die, &spec_cu);
19839 if (spec_die == NULL)
19840 parent = die->parent;
19841 else
19842 {
19843 parent = spec_die->parent;
19844 cu = spec_cu;
19845 }
19846
19847 if (parent == NULL)
19848 return "";
19849 else if (parent->building_fullname)
19850 {
19851 const char *name;
19852 const char *parent_name;
19853
19854 /* It has been seen on RealView 2.2 built binaries,
19855 DW_TAG_template_type_param types actually _defined_ as
19856 children of the parent class:
19857
19858 enum E {};
19859 template class <class Enum> Class{};
19860 Class<enum E> class_e;
19861
19862 1: DW_TAG_class_type (Class)
19863 2: DW_TAG_enumeration_type (E)
19864 3: DW_TAG_enumerator (enum1:0)
19865 3: DW_TAG_enumerator (enum2:1)
19866 ...
19867 2: DW_TAG_template_type_param
19868 DW_AT_type DW_FORM_ref_udata (E)
19869
19870 Besides being broken debug info, it can put GDB into an
19871 infinite loop. Consider:
19872
19873 When we're building the full name for Class<E>, we'll start
19874 at Class, and go look over its template type parameters,
19875 finding E. We'll then try to build the full name of E, and
19876 reach here. We're now trying to build the full name of E,
19877 and look over the parent DIE for containing scope. In the
19878 broken case, if we followed the parent DIE of E, we'd again
19879 find Class, and once again go look at its template type
19880 arguments, etc., etc. Simply don't consider such parent die
19881 as source-level parent of this die (it can't be, the language
19882 doesn't allow it), and break the loop here. */
19883 name = dwarf2_name (die, cu);
19884 parent_name = dwarf2_name (parent, cu);
19885 complaint (&symfile_complaints,
19886 _("template param type '%s' defined within parent '%s'"),
19887 name ? name : "<unknown>",
19888 parent_name ? parent_name : "<unknown>");
19889 return "";
19890 }
19891 else
19892 switch (parent->tag)
19893 {
19894 case DW_TAG_namespace:
19895 parent_type = read_type_die (parent, cu);
19896 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
19897 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
19898 Work around this problem here. */
19899 if (cu->language == language_cplus
19900 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
19901 return "";
19902 /* We give a name to even anonymous namespaces. */
19903 return TYPE_TAG_NAME (parent_type);
19904 case DW_TAG_class_type:
19905 case DW_TAG_interface_type:
19906 case DW_TAG_structure_type:
19907 case DW_TAG_union_type:
19908 case DW_TAG_module:
19909 parent_type = read_type_die (parent, cu);
19910 if (TYPE_TAG_NAME (parent_type) != NULL)
19911 return TYPE_TAG_NAME (parent_type);
19912 else
19913 /* An anonymous structure is only allowed non-static data
19914 members; no typedefs, no member functions, et cetera.
19915 So it does not need a prefix. */
19916 return "";
19917 case DW_TAG_compile_unit:
19918 case DW_TAG_partial_unit:
19919 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
19920 if (cu->language == language_cplus
19921 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
19922 && die->child != NULL
19923 && (die->tag == DW_TAG_class_type
19924 || die->tag == DW_TAG_structure_type
19925 || die->tag == DW_TAG_union_type))
19926 {
19927 char *name = guess_full_die_structure_name (die, cu);
19928 if (name != NULL)
19929 return name;
19930 }
19931 return "";
19932 case DW_TAG_enumeration_type:
19933 parent_type = read_type_die (parent, cu);
19934 if (TYPE_DECLARED_CLASS (parent_type))
19935 {
19936 if (TYPE_TAG_NAME (parent_type) != NULL)
19937 return TYPE_TAG_NAME (parent_type);
19938 return "";
19939 }
19940 /* Fall through. */
19941 default:
19942 return determine_prefix (parent, cu);
19943 }
19944 }
19945
19946 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
19947 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
19948 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
19949 an obconcat, otherwise allocate storage for the result. The CU argument is
19950 used to determine the language and hence, the appropriate separator. */
19951
19952 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
19953
19954 static char *
19955 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
19956 int physname, struct dwarf2_cu *cu)
19957 {
19958 const char *lead = "";
19959 const char *sep;
19960
19961 if (suffix == NULL || suffix[0] == '\0'
19962 || prefix == NULL || prefix[0] == '\0')
19963 sep = "";
19964 else if (cu->language == language_d)
19965 {
19966 /* For D, the 'main' function could be defined in any module, but it
19967 should never be prefixed. */
19968 if (strcmp (suffix, "D main") == 0)
19969 {
19970 prefix = "";
19971 sep = "";
19972 }
19973 else
19974 sep = ".";
19975 }
19976 else if (cu->language == language_fortran && physname)
19977 {
19978 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
19979 DW_AT_MIPS_linkage_name is preferred and used instead. */
19980
19981 lead = "__";
19982 sep = "_MOD_";
19983 }
19984 else
19985 sep = "::";
19986
19987 if (prefix == NULL)
19988 prefix = "";
19989 if (suffix == NULL)
19990 suffix = "";
19991
19992 if (obs == NULL)
19993 {
19994 char *retval
19995 = ((char *)
19996 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
19997
19998 strcpy (retval, lead);
19999 strcat (retval, prefix);
20000 strcat (retval, sep);
20001 strcat (retval, suffix);
20002 return retval;
20003 }
20004 else
20005 {
20006 /* We have an obstack. */
20007 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
20008 }
20009 }
20010
20011 /* Return sibling of die, NULL if no sibling. */
20012
20013 static struct die_info *
20014 sibling_die (struct die_info *die)
20015 {
20016 return die->sibling;
20017 }
20018
20019 /* Get name of a die, return NULL if not found. */
20020
20021 static const char *
20022 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
20023 struct obstack *obstack)
20024 {
20025 if (name && cu->language == language_cplus)
20026 {
20027 std::string canon_name = cp_canonicalize_string (name);
20028
20029 if (!canon_name.empty ())
20030 {
20031 if (canon_name != name)
20032 name = (const char *) obstack_copy0 (obstack,
20033 canon_name.c_str (),
20034 canon_name.length ());
20035 }
20036 }
20037
20038 return name;
20039 }
20040
20041 /* Get name of a die, return NULL if not found.
20042 Anonymous namespaces are converted to their magic string. */
20043
20044 static const char *
20045 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
20046 {
20047 struct attribute *attr;
20048
20049 attr = dwarf2_attr (die, DW_AT_name, cu);
20050 if ((!attr || !DW_STRING (attr))
20051 && die->tag != DW_TAG_namespace
20052 && die->tag != DW_TAG_class_type
20053 && die->tag != DW_TAG_interface_type
20054 && die->tag != DW_TAG_structure_type
20055 && die->tag != DW_TAG_union_type)
20056 return NULL;
20057
20058 switch (die->tag)
20059 {
20060 case DW_TAG_compile_unit:
20061 case DW_TAG_partial_unit:
20062 /* Compilation units have a DW_AT_name that is a filename, not
20063 a source language identifier. */
20064 case DW_TAG_enumeration_type:
20065 case DW_TAG_enumerator:
20066 /* These tags always have simple identifiers already; no need
20067 to canonicalize them. */
20068 return DW_STRING (attr);
20069
20070 case DW_TAG_namespace:
20071 if (attr != NULL && DW_STRING (attr) != NULL)
20072 return DW_STRING (attr);
20073 return CP_ANONYMOUS_NAMESPACE_STR;
20074
20075 case DW_TAG_class_type:
20076 case DW_TAG_interface_type:
20077 case DW_TAG_structure_type:
20078 case DW_TAG_union_type:
20079 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
20080 structures or unions. These were of the form "._%d" in GCC 4.1,
20081 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
20082 and GCC 4.4. We work around this problem by ignoring these. */
20083 if (attr && DW_STRING (attr)
20084 && (startswith (DW_STRING (attr), "._")
20085 || startswith (DW_STRING (attr), "<anonymous")))
20086 return NULL;
20087
20088 /* GCC might emit a nameless typedef that has a linkage name. See
20089 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20090 if (!attr || DW_STRING (attr) == NULL)
20091 {
20092 char *demangled = NULL;
20093
20094 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
20095 if (attr == NULL)
20096 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
20097
20098 if (attr == NULL || DW_STRING (attr) == NULL)
20099 return NULL;
20100
20101 /* Avoid demangling DW_STRING (attr) the second time on a second
20102 call for the same DIE. */
20103 if (!DW_STRING_IS_CANONICAL (attr))
20104 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
20105
20106 if (demangled)
20107 {
20108 const char *base;
20109
20110 /* FIXME: we already did this for the partial symbol... */
20111 DW_STRING (attr)
20112 = ((const char *)
20113 obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
20114 demangled, strlen (demangled)));
20115 DW_STRING_IS_CANONICAL (attr) = 1;
20116 xfree (demangled);
20117
20118 /* Strip any leading namespaces/classes, keep only the base name.
20119 DW_AT_name for named DIEs does not contain the prefixes. */
20120 base = strrchr (DW_STRING (attr), ':');
20121 if (base && base > DW_STRING (attr) && base[-1] == ':')
20122 return &base[1];
20123 else
20124 return DW_STRING (attr);
20125 }
20126 }
20127 break;
20128
20129 default:
20130 break;
20131 }
20132
20133 if (!DW_STRING_IS_CANONICAL (attr))
20134 {
20135 DW_STRING (attr)
20136 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
20137 &cu->objfile->per_bfd->storage_obstack);
20138 DW_STRING_IS_CANONICAL (attr) = 1;
20139 }
20140 return DW_STRING (attr);
20141 }
20142
20143 /* Return the die that this die in an extension of, or NULL if there
20144 is none. *EXT_CU is the CU containing DIE on input, and the CU
20145 containing the return value on output. */
20146
20147 static struct die_info *
20148 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
20149 {
20150 struct attribute *attr;
20151
20152 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
20153 if (attr == NULL)
20154 return NULL;
20155
20156 return follow_die_ref (die, attr, ext_cu);
20157 }
20158
20159 /* Convert a DIE tag into its string name. */
20160
20161 static const char *
20162 dwarf_tag_name (unsigned tag)
20163 {
20164 const char *name = get_DW_TAG_name (tag);
20165
20166 if (name == NULL)
20167 return "DW_TAG_<unknown>";
20168
20169 return name;
20170 }
20171
20172 /* Convert a DWARF attribute code into its string name. */
20173
20174 static const char *
20175 dwarf_attr_name (unsigned attr)
20176 {
20177 const char *name;
20178
20179 #ifdef MIPS /* collides with DW_AT_HP_block_index */
20180 if (attr == DW_AT_MIPS_fde)
20181 return "DW_AT_MIPS_fde";
20182 #else
20183 if (attr == DW_AT_HP_block_index)
20184 return "DW_AT_HP_block_index";
20185 #endif
20186
20187 name = get_DW_AT_name (attr);
20188
20189 if (name == NULL)
20190 return "DW_AT_<unknown>";
20191
20192 return name;
20193 }
20194
20195 /* Convert a DWARF value form code into its string name. */
20196
20197 static const char *
20198 dwarf_form_name (unsigned form)
20199 {
20200 const char *name = get_DW_FORM_name (form);
20201
20202 if (name == NULL)
20203 return "DW_FORM_<unknown>";
20204
20205 return name;
20206 }
20207
20208 static char *
20209 dwarf_bool_name (unsigned mybool)
20210 {
20211 if (mybool)
20212 return "TRUE";
20213 else
20214 return "FALSE";
20215 }
20216
20217 /* Convert a DWARF type code into its string name. */
20218
20219 static const char *
20220 dwarf_type_encoding_name (unsigned enc)
20221 {
20222 const char *name = get_DW_ATE_name (enc);
20223
20224 if (name == NULL)
20225 return "DW_ATE_<unknown>";
20226
20227 return name;
20228 }
20229
20230 static void
20231 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
20232 {
20233 unsigned int i;
20234
20235 print_spaces (indent, f);
20236 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
20237 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
20238
20239 if (die->parent != NULL)
20240 {
20241 print_spaces (indent, f);
20242 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
20243 die->parent->offset.sect_off);
20244 }
20245
20246 print_spaces (indent, f);
20247 fprintf_unfiltered (f, " has children: %s\n",
20248 dwarf_bool_name (die->child != NULL));
20249
20250 print_spaces (indent, f);
20251 fprintf_unfiltered (f, " attributes:\n");
20252
20253 for (i = 0; i < die->num_attrs; ++i)
20254 {
20255 print_spaces (indent, f);
20256 fprintf_unfiltered (f, " %s (%s) ",
20257 dwarf_attr_name (die->attrs[i].name),
20258 dwarf_form_name (die->attrs[i].form));
20259
20260 switch (die->attrs[i].form)
20261 {
20262 case DW_FORM_addr:
20263 case DW_FORM_GNU_addr_index:
20264 fprintf_unfiltered (f, "address: ");
20265 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
20266 break;
20267 case DW_FORM_block2:
20268 case DW_FORM_block4:
20269 case DW_FORM_block:
20270 case DW_FORM_block1:
20271 fprintf_unfiltered (f, "block: size %s",
20272 pulongest (DW_BLOCK (&die->attrs[i])->size));
20273 break;
20274 case DW_FORM_exprloc:
20275 fprintf_unfiltered (f, "expression: size %s",
20276 pulongest (DW_BLOCK (&die->attrs[i])->size));
20277 break;
20278 case DW_FORM_data16:
20279 fprintf_unfiltered (f, "constant of 16 bytes");
20280 break;
20281 case DW_FORM_ref_addr:
20282 fprintf_unfiltered (f, "ref address: ");
20283 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20284 break;
20285 case DW_FORM_GNU_ref_alt:
20286 fprintf_unfiltered (f, "alt ref address: ");
20287 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
20288 break;
20289 case DW_FORM_ref1:
20290 case DW_FORM_ref2:
20291 case DW_FORM_ref4:
20292 case DW_FORM_ref8:
20293 case DW_FORM_ref_udata:
20294 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
20295 (long) (DW_UNSND (&die->attrs[i])));
20296 break;
20297 case DW_FORM_data1:
20298 case DW_FORM_data2:
20299 case DW_FORM_data4:
20300 case DW_FORM_data8:
20301 case DW_FORM_udata:
20302 case DW_FORM_sdata:
20303 fprintf_unfiltered (f, "constant: %s",
20304 pulongest (DW_UNSND (&die->attrs[i])));
20305 break;
20306 case DW_FORM_sec_offset:
20307 fprintf_unfiltered (f, "section offset: %s",
20308 pulongest (DW_UNSND (&die->attrs[i])));
20309 break;
20310 case DW_FORM_ref_sig8:
20311 fprintf_unfiltered (f, "signature: %s",
20312 hex_string (DW_SIGNATURE (&die->attrs[i])));
20313 break;
20314 case DW_FORM_string:
20315 case DW_FORM_strp:
20316 case DW_FORM_line_strp:
20317 case DW_FORM_GNU_str_index:
20318 case DW_FORM_GNU_strp_alt:
20319 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
20320 DW_STRING (&die->attrs[i])
20321 ? DW_STRING (&die->attrs[i]) : "",
20322 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
20323 break;
20324 case DW_FORM_flag:
20325 if (DW_UNSND (&die->attrs[i]))
20326 fprintf_unfiltered (f, "flag: TRUE");
20327 else
20328 fprintf_unfiltered (f, "flag: FALSE");
20329 break;
20330 case DW_FORM_flag_present:
20331 fprintf_unfiltered (f, "flag: TRUE");
20332 break;
20333 case DW_FORM_indirect:
20334 /* The reader will have reduced the indirect form to
20335 the "base form" so this form should not occur. */
20336 fprintf_unfiltered (f,
20337 "unexpected attribute form: DW_FORM_indirect");
20338 break;
20339 default:
20340 fprintf_unfiltered (f, "unsupported attribute form: %d.",
20341 die->attrs[i].form);
20342 break;
20343 }
20344 fprintf_unfiltered (f, "\n");
20345 }
20346 }
20347
20348 static void
20349 dump_die_for_error (struct die_info *die)
20350 {
20351 dump_die_shallow (gdb_stderr, 0, die);
20352 }
20353
20354 static void
20355 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
20356 {
20357 int indent = level * 4;
20358
20359 gdb_assert (die != NULL);
20360
20361 if (level >= max_level)
20362 return;
20363
20364 dump_die_shallow (f, indent, die);
20365
20366 if (die->child != NULL)
20367 {
20368 print_spaces (indent, f);
20369 fprintf_unfiltered (f, " Children:");
20370 if (level + 1 < max_level)
20371 {
20372 fprintf_unfiltered (f, "\n");
20373 dump_die_1 (f, level + 1, max_level, die->child);
20374 }
20375 else
20376 {
20377 fprintf_unfiltered (f,
20378 " [not printed, max nesting level reached]\n");
20379 }
20380 }
20381
20382 if (die->sibling != NULL && level > 0)
20383 {
20384 dump_die_1 (f, level, max_level, die->sibling);
20385 }
20386 }
20387
20388 /* This is called from the pdie macro in gdbinit.in.
20389 It's not static so gcc will keep a copy callable from gdb. */
20390
20391 void
20392 dump_die (struct die_info *die, int max_level)
20393 {
20394 dump_die_1 (gdb_stdlog, 0, max_level, die);
20395 }
20396
20397 static void
20398 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
20399 {
20400 void **slot;
20401
20402 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
20403 INSERT);
20404
20405 *slot = die;
20406 }
20407
20408 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
20409 required kind. */
20410
20411 static sect_offset
20412 dwarf2_get_ref_die_offset (const struct attribute *attr)
20413 {
20414 sect_offset retval = { DW_UNSND (attr) };
20415
20416 if (attr_form_is_ref (attr))
20417 return retval;
20418
20419 retval.sect_off = 0;
20420 complaint (&symfile_complaints,
20421 _("unsupported die ref attribute form: '%s'"),
20422 dwarf_form_name (attr->form));
20423 return retval;
20424 }
20425
20426 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
20427 * the value held by the attribute is not constant. */
20428
20429 static LONGEST
20430 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
20431 {
20432 if (attr->form == DW_FORM_sdata)
20433 return DW_SND (attr);
20434 else if (attr->form == DW_FORM_udata
20435 || attr->form == DW_FORM_data1
20436 || attr->form == DW_FORM_data2
20437 || attr->form == DW_FORM_data4
20438 || attr->form == DW_FORM_data8)
20439 return DW_UNSND (attr);
20440 else
20441 {
20442 /* For DW_FORM_data16 see attr_form_is_constant. */
20443 complaint (&symfile_complaints,
20444 _("Attribute value is not a constant (%s)"),
20445 dwarf_form_name (attr->form));
20446 return default_value;
20447 }
20448 }
20449
20450 /* Follow reference or signature attribute ATTR of SRC_DIE.
20451 On entry *REF_CU is the CU of SRC_DIE.
20452 On exit *REF_CU is the CU of the result. */
20453
20454 static struct die_info *
20455 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
20456 struct dwarf2_cu **ref_cu)
20457 {
20458 struct die_info *die;
20459
20460 if (attr_form_is_ref (attr))
20461 die = follow_die_ref (src_die, attr, ref_cu);
20462 else if (attr->form == DW_FORM_ref_sig8)
20463 die = follow_die_sig (src_die, attr, ref_cu);
20464 else
20465 {
20466 dump_die_for_error (src_die);
20467 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
20468 objfile_name ((*ref_cu)->objfile));
20469 }
20470
20471 return die;
20472 }
20473
20474 /* Follow reference OFFSET.
20475 On entry *REF_CU is the CU of the source die referencing OFFSET.
20476 On exit *REF_CU is the CU of the result.
20477 Returns NULL if OFFSET is invalid. */
20478
20479 static struct die_info *
20480 follow_die_offset (sect_offset offset, int offset_in_dwz,
20481 struct dwarf2_cu **ref_cu)
20482 {
20483 struct die_info temp_die;
20484 struct dwarf2_cu *target_cu, *cu = *ref_cu;
20485
20486 gdb_assert (cu->per_cu != NULL);
20487
20488 target_cu = cu;
20489
20490 if (cu->per_cu->is_debug_types)
20491 {
20492 /* .debug_types CUs cannot reference anything outside their CU.
20493 If they need to, they have to reference a signatured type via
20494 DW_FORM_ref_sig8. */
20495 if (! offset_in_cu_p (&cu->header, offset))
20496 return NULL;
20497 }
20498 else if (offset_in_dwz != cu->per_cu->is_dwz
20499 || ! offset_in_cu_p (&cu->header, offset))
20500 {
20501 struct dwarf2_per_cu_data *per_cu;
20502
20503 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
20504 cu->objfile);
20505
20506 /* If necessary, add it to the queue and load its DIEs. */
20507 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
20508 load_full_comp_unit (per_cu, cu->language);
20509
20510 target_cu = per_cu->cu;
20511 }
20512 else if (cu->dies == NULL)
20513 {
20514 /* We're loading full DIEs during partial symbol reading. */
20515 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
20516 load_full_comp_unit (cu->per_cu, language_minimal);
20517 }
20518
20519 *ref_cu = target_cu;
20520 temp_die.offset = offset;
20521 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
20522 &temp_die, offset.sect_off);
20523 }
20524
20525 /* Follow reference attribute ATTR of SRC_DIE.
20526 On entry *REF_CU is the CU of SRC_DIE.
20527 On exit *REF_CU is the CU of the result. */
20528
20529 static struct die_info *
20530 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
20531 struct dwarf2_cu **ref_cu)
20532 {
20533 sect_offset offset = dwarf2_get_ref_die_offset (attr);
20534 struct dwarf2_cu *cu = *ref_cu;
20535 struct die_info *die;
20536
20537 die = follow_die_offset (offset,
20538 (attr->form == DW_FORM_GNU_ref_alt
20539 || cu->per_cu->is_dwz),
20540 ref_cu);
20541 if (!die)
20542 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
20543 "at 0x%x [in module %s]"),
20544 offset.sect_off, src_die->offset.sect_off,
20545 objfile_name (cu->objfile));
20546
20547 return die;
20548 }
20549
20550 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
20551 Returned value is intended for DW_OP_call*. Returned
20552 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
20553
20554 struct dwarf2_locexpr_baton
20555 dwarf2_fetch_die_loc_sect_off (sect_offset offset,
20556 struct dwarf2_per_cu_data *per_cu,
20557 CORE_ADDR (*get_frame_pc) (void *baton),
20558 void *baton)
20559 {
20560 struct dwarf2_cu *cu;
20561 struct die_info *die;
20562 struct attribute *attr;
20563 struct dwarf2_locexpr_baton retval;
20564
20565 dw2_setup (per_cu->objfile);
20566
20567 if (per_cu->cu == NULL)
20568 load_cu (per_cu);
20569 cu = per_cu->cu;
20570 if (cu == NULL)
20571 {
20572 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20573 Instead just throw an error, not much else we can do. */
20574 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20575 offset.sect_off, objfile_name (per_cu->objfile));
20576 }
20577
20578 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
20579 if (!die)
20580 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
20581 offset.sect_off, objfile_name (per_cu->objfile));
20582
20583 attr = dwarf2_attr (die, DW_AT_location, cu);
20584 if (!attr)
20585 {
20586 /* DWARF: "If there is no such attribute, then there is no effect.".
20587 DATA is ignored if SIZE is 0. */
20588
20589 retval.data = NULL;
20590 retval.size = 0;
20591 }
20592 else if (attr_form_is_section_offset (attr))
20593 {
20594 struct dwarf2_loclist_baton loclist_baton;
20595 CORE_ADDR pc = (*get_frame_pc) (baton);
20596 size_t size;
20597
20598 fill_in_loclist_baton (cu, &loclist_baton, attr);
20599
20600 retval.data = dwarf2_find_location_expression (&loclist_baton,
20601 &size, pc);
20602 retval.size = size;
20603 }
20604 else
20605 {
20606 if (!attr_form_is_block (attr))
20607 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
20608 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
20609 offset.sect_off, objfile_name (per_cu->objfile));
20610
20611 retval.data = DW_BLOCK (attr)->data;
20612 retval.size = DW_BLOCK (attr)->size;
20613 }
20614 retval.per_cu = cu->per_cu;
20615
20616 age_cached_comp_units ();
20617
20618 return retval;
20619 }
20620
20621 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
20622 offset. */
20623
20624 struct dwarf2_locexpr_baton
20625 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
20626 struct dwarf2_per_cu_data *per_cu,
20627 CORE_ADDR (*get_frame_pc) (void *baton),
20628 void *baton)
20629 {
20630 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
20631
20632 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
20633 }
20634
20635 /* Write a constant of a given type as target-ordered bytes into
20636 OBSTACK. */
20637
20638 static const gdb_byte *
20639 write_constant_as_bytes (struct obstack *obstack,
20640 enum bfd_endian byte_order,
20641 struct type *type,
20642 ULONGEST value,
20643 LONGEST *len)
20644 {
20645 gdb_byte *result;
20646
20647 *len = TYPE_LENGTH (type);
20648 result = (gdb_byte *) obstack_alloc (obstack, *len);
20649 store_unsigned_integer (result, *len, byte_order, value);
20650
20651 return result;
20652 }
20653
20654 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
20655 pointer to the constant bytes and set LEN to the length of the
20656 data. If memory is needed, allocate it on OBSTACK. If the DIE
20657 does not have a DW_AT_const_value, return NULL. */
20658
20659 const gdb_byte *
20660 dwarf2_fetch_constant_bytes (sect_offset offset,
20661 struct dwarf2_per_cu_data *per_cu,
20662 struct obstack *obstack,
20663 LONGEST *len)
20664 {
20665 struct dwarf2_cu *cu;
20666 struct die_info *die;
20667 struct attribute *attr;
20668 const gdb_byte *result = NULL;
20669 struct type *type;
20670 LONGEST value;
20671 enum bfd_endian byte_order;
20672
20673 dw2_setup (per_cu->objfile);
20674
20675 if (per_cu->cu == NULL)
20676 load_cu (per_cu);
20677 cu = per_cu->cu;
20678 if (cu == NULL)
20679 {
20680 /* We shouldn't get here for a dummy CU, but don't crash on the user.
20681 Instead just throw an error, not much else we can do. */
20682 error (_("Dwarf Error: Dummy CU at 0x%x referenced in module %s"),
20683 offset.sect_off, objfile_name (per_cu->objfile));
20684 }
20685
20686 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
20687 if (!die)
20688 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
20689 offset.sect_off, objfile_name (per_cu->objfile));
20690
20691
20692 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20693 if (attr == NULL)
20694 return NULL;
20695
20696 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
20697 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20698
20699 switch (attr->form)
20700 {
20701 case DW_FORM_addr:
20702 case DW_FORM_GNU_addr_index:
20703 {
20704 gdb_byte *tem;
20705
20706 *len = cu->header.addr_size;
20707 tem = (gdb_byte *) obstack_alloc (obstack, *len);
20708 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
20709 result = tem;
20710 }
20711 break;
20712 case DW_FORM_string:
20713 case DW_FORM_strp:
20714 case DW_FORM_GNU_str_index:
20715 case DW_FORM_GNU_strp_alt:
20716 /* DW_STRING is already allocated on the objfile obstack, point
20717 directly to it. */
20718 result = (const gdb_byte *) DW_STRING (attr);
20719 *len = strlen (DW_STRING (attr));
20720 break;
20721 case DW_FORM_block1:
20722 case DW_FORM_block2:
20723 case DW_FORM_block4:
20724 case DW_FORM_block:
20725 case DW_FORM_exprloc:
20726 case DW_FORM_data16:
20727 result = DW_BLOCK (attr)->data;
20728 *len = DW_BLOCK (attr)->size;
20729 break;
20730
20731 /* The DW_AT_const_value attributes are supposed to carry the
20732 symbol's value "represented as it would be on the target
20733 architecture." By the time we get here, it's already been
20734 converted to host endianness, so we just need to sign- or
20735 zero-extend it as appropriate. */
20736 case DW_FORM_data1:
20737 type = die_type (die, cu);
20738 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
20739 if (result == NULL)
20740 result = write_constant_as_bytes (obstack, byte_order,
20741 type, value, len);
20742 break;
20743 case DW_FORM_data2:
20744 type = die_type (die, cu);
20745 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
20746 if (result == NULL)
20747 result = write_constant_as_bytes (obstack, byte_order,
20748 type, value, len);
20749 break;
20750 case DW_FORM_data4:
20751 type = die_type (die, cu);
20752 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
20753 if (result == NULL)
20754 result = write_constant_as_bytes (obstack, byte_order,
20755 type, value, len);
20756 break;
20757 case DW_FORM_data8:
20758 type = die_type (die, cu);
20759 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
20760 if (result == NULL)
20761 result = write_constant_as_bytes (obstack, byte_order,
20762 type, value, len);
20763 break;
20764
20765 case DW_FORM_sdata:
20766 type = die_type (die, cu);
20767 result = write_constant_as_bytes (obstack, byte_order,
20768 type, DW_SND (attr), len);
20769 break;
20770
20771 case DW_FORM_udata:
20772 type = die_type (die, cu);
20773 result = write_constant_as_bytes (obstack, byte_order,
20774 type, DW_UNSND (attr), len);
20775 break;
20776
20777 default:
20778 complaint (&symfile_complaints,
20779 _("unsupported const value attribute form: '%s'"),
20780 dwarf_form_name (attr->form));
20781 break;
20782 }
20783
20784 return result;
20785 }
20786
20787 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
20788 valid type for this die is found. */
20789
20790 struct type *
20791 dwarf2_fetch_die_type_sect_off (sect_offset offset,
20792 struct dwarf2_per_cu_data *per_cu)
20793 {
20794 struct dwarf2_cu *cu;
20795 struct die_info *die;
20796
20797 dw2_setup (per_cu->objfile);
20798
20799 if (per_cu->cu == NULL)
20800 load_cu (per_cu);
20801 cu = per_cu->cu;
20802 if (!cu)
20803 return NULL;
20804
20805 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
20806 if (!die)
20807 return NULL;
20808
20809 return die_type (die, cu);
20810 }
20811
20812 /* Return the type of the DIE at DIE_OFFSET in the CU named by
20813 PER_CU. */
20814
20815 struct type *
20816 dwarf2_get_die_type (cu_offset die_offset,
20817 struct dwarf2_per_cu_data *per_cu)
20818 {
20819 sect_offset die_offset_sect;
20820
20821 dw2_setup (per_cu->objfile);
20822
20823 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
20824 return get_die_type_at_offset (die_offset_sect, per_cu);
20825 }
20826
20827 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
20828 On entry *REF_CU is the CU of SRC_DIE.
20829 On exit *REF_CU is the CU of the result.
20830 Returns NULL if the referenced DIE isn't found. */
20831
20832 static struct die_info *
20833 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
20834 struct dwarf2_cu **ref_cu)
20835 {
20836 struct die_info temp_die;
20837 struct dwarf2_cu *sig_cu;
20838 struct die_info *die;
20839
20840 /* While it might be nice to assert sig_type->type == NULL here,
20841 we can get here for DW_AT_imported_declaration where we need
20842 the DIE not the type. */
20843
20844 /* If necessary, add it to the queue and load its DIEs. */
20845
20846 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
20847 read_signatured_type (sig_type);
20848
20849 sig_cu = sig_type->per_cu.cu;
20850 gdb_assert (sig_cu != NULL);
20851 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
20852 temp_die.offset = sig_type->type_offset_in_section;
20853 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
20854 temp_die.offset.sect_off);
20855 if (die)
20856 {
20857 /* For .gdb_index version 7 keep track of included TUs.
20858 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
20859 if (dwarf2_per_objfile->index_table != NULL
20860 && dwarf2_per_objfile->index_table->version <= 7)
20861 {
20862 VEC_safe_push (dwarf2_per_cu_ptr,
20863 (*ref_cu)->per_cu->imported_symtabs,
20864 sig_cu->per_cu);
20865 }
20866
20867 *ref_cu = sig_cu;
20868 return die;
20869 }
20870
20871 return NULL;
20872 }
20873
20874 /* Follow signatured type referenced by ATTR in SRC_DIE.
20875 On entry *REF_CU is the CU of SRC_DIE.
20876 On exit *REF_CU is the CU of the result.
20877 The result is the DIE of the type.
20878 If the referenced type cannot be found an error is thrown. */
20879
20880 static struct die_info *
20881 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
20882 struct dwarf2_cu **ref_cu)
20883 {
20884 ULONGEST signature = DW_SIGNATURE (attr);
20885 struct signatured_type *sig_type;
20886 struct die_info *die;
20887
20888 gdb_assert (attr->form == DW_FORM_ref_sig8);
20889
20890 sig_type = lookup_signatured_type (*ref_cu, signature);
20891 /* sig_type will be NULL if the signatured type is missing from
20892 the debug info. */
20893 if (sig_type == NULL)
20894 {
20895 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
20896 " from DIE at 0x%x [in module %s]"),
20897 hex_string (signature), src_die->offset.sect_off,
20898 objfile_name ((*ref_cu)->objfile));
20899 }
20900
20901 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
20902 if (die == NULL)
20903 {
20904 dump_die_for_error (src_die);
20905 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
20906 " from DIE at 0x%x [in module %s]"),
20907 hex_string (signature), src_die->offset.sect_off,
20908 objfile_name ((*ref_cu)->objfile));
20909 }
20910
20911 return die;
20912 }
20913
20914 /* Get the type specified by SIGNATURE referenced in DIE/CU,
20915 reading in and processing the type unit if necessary. */
20916
20917 static struct type *
20918 get_signatured_type (struct die_info *die, ULONGEST signature,
20919 struct dwarf2_cu *cu)
20920 {
20921 struct signatured_type *sig_type;
20922 struct dwarf2_cu *type_cu;
20923 struct die_info *type_die;
20924 struct type *type;
20925
20926 sig_type = lookup_signatured_type (cu, signature);
20927 /* sig_type will be NULL if the signatured type is missing from
20928 the debug info. */
20929 if (sig_type == NULL)
20930 {
20931 complaint (&symfile_complaints,
20932 _("Dwarf Error: Cannot find signatured DIE %s referenced"
20933 " from DIE at 0x%x [in module %s]"),
20934 hex_string (signature), die->offset.sect_off,
20935 objfile_name (dwarf2_per_objfile->objfile));
20936 return build_error_marker_type (cu, die);
20937 }
20938
20939 /* If we already know the type we're done. */
20940 if (sig_type->type != NULL)
20941 return sig_type->type;
20942
20943 type_cu = cu;
20944 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
20945 if (type_die != NULL)
20946 {
20947 /* N.B. We need to call get_die_type to ensure only one type for this DIE
20948 is created. This is important, for example, because for c++ classes
20949 we need TYPE_NAME set which is only done by new_symbol. Blech. */
20950 type = read_type_die (type_die, type_cu);
20951 if (type == NULL)
20952 {
20953 complaint (&symfile_complaints,
20954 _("Dwarf Error: Cannot build signatured type %s"
20955 " referenced from DIE at 0x%x [in module %s]"),
20956 hex_string (signature), die->offset.sect_off,
20957 objfile_name (dwarf2_per_objfile->objfile));
20958 type = build_error_marker_type (cu, die);
20959 }
20960 }
20961 else
20962 {
20963 complaint (&symfile_complaints,
20964 _("Dwarf Error: Problem reading signatured DIE %s referenced"
20965 " from DIE at 0x%x [in module %s]"),
20966 hex_string (signature), die->offset.sect_off,
20967 objfile_name (dwarf2_per_objfile->objfile));
20968 type = build_error_marker_type (cu, die);
20969 }
20970 sig_type->type = type;
20971
20972 return type;
20973 }
20974
20975 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
20976 reading in and processing the type unit if necessary. */
20977
20978 static struct type *
20979 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
20980 struct dwarf2_cu *cu) /* ARI: editCase function */
20981 {
20982 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
20983 if (attr_form_is_ref (attr))
20984 {
20985 struct dwarf2_cu *type_cu = cu;
20986 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
20987
20988 return read_type_die (type_die, type_cu);
20989 }
20990 else if (attr->form == DW_FORM_ref_sig8)
20991 {
20992 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
20993 }
20994 else
20995 {
20996 complaint (&symfile_complaints,
20997 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
20998 " at 0x%x [in module %s]"),
20999 dwarf_form_name (attr->form), die->offset.sect_off,
21000 objfile_name (dwarf2_per_objfile->objfile));
21001 return build_error_marker_type (cu, die);
21002 }
21003 }
21004
21005 /* Load the DIEs associated with type unit PER_CU into memory. */
21006
21007 static void
21008 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
21009 {
21010 struct signatured_type *sig_type;
21011
21012 /* Caller is responsible for ensuring type_unit_groups don't get here. */
21013 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
21014
21015 /* We have the per_cu, but we need the signatured_type.
21016 Fortunately this is an easy translation. */
21017 gdb_assert (per_cu->is_debug_types);
21018 sig_type = (struct signatured_type *) per_cu;
21019
21020 gdb_assert (per_cu->cu == NULL);
21021
21022 read_signatured_type (sig_type);
21023
21024 gdb_assert (per_cu->cu != NULL);
21025 }
21026
21027 /* die_reader_func for read_signatured_type.
21028 This is identical to load_full_comp_unit_reader,
21029 but is kept separate for now. */
21030
21031 static void
21032 read_signatured_type_reader (const struct die_reader_specs *reader,
21033 const gdb_byte *info_ptr,
21034 struct die_info *comp_unit_die,
21035 int has_children,
21036 void *data)
21037 {
21038 struct dwarf2_cu *cu = reader->cu;
21039
21040 gdb_assert (cu->die_hash == NULL);
21041 cu->die_hash =
21042 htab_create_alloc_ex (cu->header.length / 12,
21043 die_hash,
21044 die_eq,
21045 NULL,
21046 &cu->comp_unit_obstack,
21047 hashtab_obstack_allocate,
21048 dummy_obstack_deallocate);
21049
21050 if (has_children)
21051 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
21052 &info_ptr, comp_unit_die);
21053 cu->dies = comp_unit_die;
21054 /* comp_unit_die is not stored in die_hash, no need. */
21055
21056 /* We try not to read any attributes in this function, because not
21057 all CUs needed for references have been loaded yet, and symbol
21058 table processing isn't initialized. But we have to set the CU language,
21059 or we won't be able to build types correctly.
21060 Similarly, if we do not read the producer, we can not apply
21061 producer-specific interpretation. */
21062 prepare_one_comp_unit (cu, cu->dies, language_minimal);
21063 }
21064
21065 /* Read in a signatured type and build its CU and DIEs.
21066 If the type is a stub for the real type in a DWO file,
21067 read in the real type from the DWO file as well. */
21068
21069 static void
21070 read_signatured_type (struct signatured_type *sig_type)
21071 {
21072 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
21073
21074 gdb_assert (per_cu->is_debug_types);
21075 gdb_assert (per_cu->cu == NULL);
21076
21077 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
21078 read_signatured_type_reader, NULL);
21079 sig_type->per_cu.tu_read = 1;
21080 }
21081
21082 /* Decode simple location descriptions.
21083 Given a pointer to a dwarf block that defines a location, compute
21084 the location and return the value.
21085
21086 NOTE drow/2003-11-18: This function is called in two situations
21087 now: for the address of static or global variables (partial symbols
21088 only) and for offsets into structures which are expected to be
21089 (more or less) constant. The partial symbol case should go away,
21090 and only the constant case should remain. That will let this
21091 function complain more accurately. A few special modes are allowed
21092 without complaint for global variables (for instance, global
21093 register values and thread-local values).
21094
21095 A location description containing no operations indicates that the
21096 object is optimized out. The return value is 0 for that case.
21097 FIXME drow/2003-11-16: No callers check for this case any more; soon all
21098 callers will only want a very basic result and this can become a
21099 complaint.
21100
21101 Note that stack[0] is unused except as a default error return. */
21102
21103 static CORE_ADDR
21104 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
21105 {
21106 struct objfile *objfile = cu->objfile;
21107 size_t i;
21108 size_t size = blk->size;
21109 const gdb_byte *data = blk->data;
21110 CORE_ADDR stack[64];
21111 int stacki;
21112 unsigned int bytes_read, unsnd;
21113 gdb_byte op;
21114
21115 i = 0;
21116 stacki = 0;
21117 stack[stacki] = 0;
21118 stack[++stacki] = 0;
21119
21120 while (i < size)
21121 {
21122 op = data[i++];
21123 switch (op)
21124 {
21125 case DW_OP_lit0:
21126 case DW_OP_lit1:
21127 case DW_OP_lit2:
21128 case DW_OP_lit3:
21129 case DW_OP_lit4:
21130 case DW_OP_lit5:
21131 case DW_OP_lit6:
21132 case DW_OP_lit7:
21133 case DW_OP_lit8:
21134 case DW_OP_lit9:
21135 case DW_OP_lit10:
21136 case DW_OP_lit11:
21137 case DW_OP_lit12:
21138 case DW_OP_lit13:
21139 case DW_OP_lit14:
21140 case DW_OP_lit15:
21141 case DW_OP_lit16:
21142 case DW_OP_lit17:
21143 case DW_OP_lit18:
21144 case DW_OP_lit19:
21145 case DW_OP_lit20:
21146 case DW_OP_lit21:
21147 case DW_OP_lit22:
21148 case DW_OP_lit23:
21149 case DW_OP_lit24:
21150 case DW_OP_lit25:
21151 case DW_OP_lit26:
21152 case DW_OP_lit27:
21153 case DW_OP_lit28:
21154 case DW_OP_lit29:
21155 case DW_OP_lit30:
21156 case DW_OP_lit31:
21157 stack[++stacki] = op - DW_OP_lit0;
21158 break;
21159
21160 case DW_OP_reg0:
21161 case DW_OP_reg1:
21162 case DW_OP_reg2:
21163 case DW_OP_reg3:
21164 case DW_OP_reg4:
21165 case DW_OP_reg5:
21166 case DW_OP_reg6:
21167 case DW_OP_reg7:
21168 case DW_OP_reg8:
21169 case DW_OP_reg9:
21170 case DW_OP_reg10:
21171 case DW_OP_reg11:
21172 case DW_OP_reg12:
21173 case DW_OP_reg13:
21174 case DW_OP_reg14:
21175 case DW_OP_reg15:
21176 case DW_OP_reg16:
21177 case DW_OP_reg17:
21178 case DW_OP_reg18:
21179 case DW_OP_reg19:
21180 case DW_OP_reg20:
21181 case DW_OP_reg21:
21182 case DW_OP_reg22:
21183 case DW_OP_reg23:
21184 case DW_OP_reg24:
21185 case DW_OP_reg25:
21186 case DW_OP_reg26:
21187 case DW_OP_reg27:
21188 case DW_OP_reg28:
21189 case DW_OP_reg29:
21190 case DW_OP_reg30:
21191 case DW_OP_reg31:
21192 stack[++stacki] = op - DW_OP_reg0;
21193 if (i < size)
21194 dwarf2_complex_location_expr_complaint ();
21195 break;
21196
21197 case DW_OP_regx:
21198 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
21199 i += bytes_read;
21200 stack[++stacki] = unsnd;
21201 if (i < size)
21202 dwarf2_complex_location_expr_complaint ();
21203 break;
21204
21205 case DW_OP_addr:
21206 stack[++stacki] = read_address (objfile->obfd, &data[i],
21207 cu, &bytes_read);
21208 i += bytes_read;
21209 break;
21210
21211 case DW_OP_const1u:
21212 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
21213 i += 1;
21214 break;
21215
21216 case DW_OP_const1s:
21217 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
21218 i += 1;
21219 break;
21220
21221 case DW_OP_const2u:
21222 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
21223 i += 2;
21224 break;
21225
21226 case DW_OP_const2s:
21227 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
21228 i += 2;
21229 break;
21230
21231 case DW_OP_const4u:
21232 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
21233 i += 4;
21234 break;
21235
21236 case DW_OP_const4s:
21237 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
21238 i += 4;
21239 break;
21240
21241 case DW_OP_const8u:
21242 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
21243 i += 8;
21244 break;
21245
21246 case DW_OP_constu:
21247 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
21248 &bytes_read);
21249 i += bytes_read;
21250 break;
21251
21252 case DW_OP_consts:
21253 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
21254 i += bytes_read;
21255 break;
21256
21257 case DW_OP_dup:
21258 stack[stacki + 1] = stack[stacki];
21259 stacki++;
21260 break;
21261
21262 case DW_OP_plus:
21263 stack[stacki - 1] += stack[stacki];
21264 stacki--;
21265 break;
21266
21267 case DW_OP_plus_uconst:
21268 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
21269 &bytes_read);
21270 i += bytes_read;
21271 break;
21272
21273 case DW_OP_minus:
21274 stack[stacki - 1] -= stack[stacki];
21275 stacki--;
21276 break;
21277
21278 case DW_OP_deref:
21279 /* If we're not the last op, then we definitely can't encode
21280 this using GDB's address_class enum. This is valid for partial
21281 global symbols, although the variable's address will be bogus
21282 in the psymtab. */
21283 if (i < size)
21284 dwarf2_complex_location_expr_complaint ();
21285 break;
21286
21287 case DW_OP_GNU_push_tls_address:
21288 case DW_OP_form_tls_address:
21289 /* The top of the stack has the offset from the beginning
21290 of the thread control block at which the variable is located. */
21291 /* Nothing should follow this operator, so the top of stack would
21292 be returned. */
21293 /* This is valid for partial global symbols, but the variable's
21294 address will be bogus in the psymtab. Make it always at least
21295 non-zero to not look as a variable garbage collected by linker
21296 which have DW_OP_addr 0. */
21297 if (i < size)
21298 dwarf2_complex_location_expr_complaint ();
21299 stack[stacki]++;
21300 break;
21301
21302 case DW_OP_GNU_uninit:
21303 break;
21304
21305 case DW_OP_GNU_addr_index:
21306 case DW_OP_GNU_const_index:
21307 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
21308 &bytes_read);
21309 i += bytes_read;
21310 break;
21311
21312 default:
21313 {
21314 const char *name = get_DW_OP_name (op);
21315
21316 if (name)
21317 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
21318 name);
21319 else
21320 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
21321 op);
21322 }
21323
21324 return (stack[stacki]);
21325 }
21326
21327 /* Enforce maximum stack depth of SIZE-1 to avoid writing
21328 outside of the allocated space. Also enforce minimum>0. */
21329 if (stacki >= ARRAY_SIZE (stack) - 1)
21330 {
21331 complaint (&symfile_complaints,
21332 _("location description stack overflow"));
21333 return 0;
21334 }
21335
21336 if (stacki <= 0)
21337 {
21338 complaint (&symfile_complaints,
21339 _("location description stack underflow"));
21340 return 0;
21341 }
21342 }
21343 return (stack[stacki]);
21344 }
21345
21346 /* memory allocation interface */
21347
21348 static struct dwarf_block *
21349 dwarf_alloc_block (struct dwarf2_cu *cu)
21350 {
21351 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
21352 }
21353
21354 static struct die_info *
21355 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
21356 {
21357 struct die_info *die;
21358 size_t size = sizeof (struct die_info);
21359
21360 if (num_attrs > 1)
21361 size += (num_attrs - 1) * sizeof (struct attribute);
21362
21363 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
21364 memset (die, 0, sizeof (struct die_info));
21365 return (die);
21366 }
21367
21368 \f
21369 /* Macro support. */
21370
21371 /* Return file name relative to the compilation directory of file number I in
21372 *LH's file name table. The result is allocated using xmalloc; the caller is
21373 responsible for freeing it. */
21374
21375 static char *
21376 file_file_name (int file, struct line_header *lh)
21377 {
21378 /* Is the file number a valid index into the line header's file name
21379 table? Remember that file numbers start with one, not zero. */
21380 if (1 <= file && file <= lh->file_names.size ())
21381 {
21382 const file_entry &fe = lh->file_names[file - 1];
21383
21384 if (!IS_ABSOLUTE_PATH (fe.name))
21385 {
21386 const char *dir = fe.include_dir (lh);
21387 if (dir != NULL)
21388 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
21389 }
21390 return xstrdup (fe.name);
21391 }
21392 else
21393 {
21394 /* The compiler produced a bogus file number. We can at least
21395 record the macro definitions made in the file, even if we
21396 won't be able to find the file by name. */
21397 char fake_name[80];
21398
21399 xsnprintf (fake_name, sizeof (fake_name),
21400 "<bad macro file number %d>", file);
21401
21402 complaint (&symfile_complaints,
21403 _("bad file number in macro information (%d)"),
21404 file);
21405
21406 return xstrdup (fake_name);
21407 }
21408 }
21409
21410 /* Return the full name of file number I in *LH's file name table.
21411 Use COMP_DIR as the name of the current directory of the
21412 compilation. The result is allocated using xmalloc; the caller is
21413 responsible for freeing it. */
21414 static char *
21415 file_full_name (int file, struct line_header *lh, const char *comp_dir)
21416 {
21417 /* Is the file number a valid index into the line header's file name
21418 table? Remember that file numbers start with one, not zero. */
21419 if (1 <= file && file <= lh->file_names.size ())
21420 {
21421 char *relative = file_file_name (file, lh);
21422
21423 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
21424 return relative;
21425 return reconcat (relative, comp_dir, SLASH_STRING,
21426 relative, (char *) NULL);
21427 }
21428 else
21429 return file_file_name (file, lh);
21430 }
21431
21432
21433 static struct macro_source_file *
21434 macro_start_file (int file, int line,
21435 struct macro_source_file *current_file,
21436 struct line_header *lh)
21437 {
21438 /* File name relative to the compilation directory of this source file. */
21439 char *file_name = file_file_name (file, lh);
21440
21441 if (! current_file)
21442 {
21443 /* Note: We don't create a macro table for this compilation unit
21444 at all until we actually get a filename. */
21445 struct macro_table *macro_table = get_macro_table ();
21446
21447 /* If we have no current file, then this must be the start_file
21448 directive for the compilation unit's main source file. */
21449 current_file = macro_set_main (macro_table, file_name);
21450 macro_define_special (macro_table);
21451 }
21452 else
21453 current_file = macro_include (current_file, line, file_name);
21454
21455 xfree (file_name);
21456
21457 return current_file;
21458 }
21459
21460
21461 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
21462 followed by a null byte. */
21463 static char *
21464 copy_string (const char *buf, int len)
21465 {
21466 char *s = (char *) xmalloc (len + 1);
21467
21468 memcpy (s, buf, len);
21469 s[len] = '\0';
21470 return s;
21471 }
21472
21473
21474 static const char *
21475 consume_improper_spaces (const char *p, const char *body)
21476 {
21477 if (*p == ' ')
21478 {
21479 complaint (&symfile_complaints,
21480 _("macro definition contains spaces "
21481 "in formal argument list:\n`%s'"),
21482 body);
21483
21484 while (*p == ' ')
21485 p++;
21486 }
21487
21488 return p;
21489 }
21490
21491
21492 static void
21493 parse_macro_definition (struct macro_source_file *file, int line,
21494 const char *body)
21495 {
21496 const char *p;
21497
21498 /* The body string takes one of two forms. For object-like macro
21499 definitions, it should be:
21500
21501 <macro name> " " <definition>
21502
21503 For function-like macro definitions, it should be:
21504
21505 <macro name> "() " <definition>
21506 or
21507 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
21508
21509 Spaces may appear only where explicitly indicated, and in the
21510 <definition>.
21511
21512 The Dwarf 2 spec says that an object-like macro's name is always
21513 followed by a space, but versions of GCC around March 2002 omit
21514 the space when the macro's definition is the empty string.
21515
21516 The Dwarf 2 spec says that there should be no spaces between the
21517 formal arguments in a function-like macro's formal argument list,
21518 but versions of GCC around March 2002 include spaces after the
21519 commas. */
21520
21521
21522 /* Find the extent of the macro name. The macro name is terminated
21523 by either a space or null character (for an object-like macro) or
21524 an opening paren (for a function-like macro). */
21525 for (p = body; *p; p++)
21526 if (*p == ' ' || *p == '(')
21527 break;
21528
21529 if (*p == ' ' || *p == '\0')
21530 {
21531 /* It's an object-like macro. */
21532 int name_len = p - body;
21533 char *name = copy_string (body, name_len);
21534 const char *replacement;
21535
21536 if (*p == ' ')
21537 replacement = body + name_len + 1;
21538 else
21539 {
21540 dwarf2_macro_malformed_definition_complaint (body);
21541 replacement = body + name_len;
21542 }
21543
21544 macro_define_object (file, line, name, replacement);
21545
21546 xfree (name);
21547 }
21548 else if (*p == '(')
21549 {
21550 /* It's a function-like macro. */
21551 char *name = copy_string (body, p - body);
21552 int argc = 0;
21553 int argv_size = 1;
21554 char **argv = XNEWVEC (char *, argv_size);
21555
21556 p++;
21557
21558 p = consume_improper_spaces (p, body);
21559
21560 /* Parse the formal argument list. */
21561 while (*p && *p != ')')
21562 {
21563 /* Find the extent of the current argument name. */
21564 const char *arg_start = p;
21565
21566 while (*p && *p != ',' && *p != ')' && *p != ' ')
21567 p++;
21568
21569 if (! *p || p == arg_start)
21570 dwarf2_macro_malformed_definition_complaint (body);
21571 else
21572 {
21573 /* Make sure argv has room for the new argument. */
21574 if (argc >= argv_size)
21575 {
21576 argv_size *= 2;
21577 argv = XRESIZEVEC (char *, argv, argv_size);
21578 }
21579
21580 argv[argc++] = copy_string (arg_start, p - arg_start);
21581 }
21582
21583 p = consume_improper_spaces (p, body);
21584
21585 /* Consume the comma, if present. */
21586 if (*p == ',')
21587 {
21588 p++;
21589
21590 p = consume_improper_spaces (p, body);
21591 }
21592 }
21593
21594 if (*p == ')')
21595 {
21596 p++;
21597
21598 if (*p == ' ')
21599 /* Perfectly formed definition, no complaints. */
21600 macro_define_function (file, line, name,
21601 argc, (const char **) argv,
21602 p + 1);
21603 else if (*p == '\0')
21604 {
21605 /* Complain, but do define it. */
21606 dwarf2_macro_malformed_definition_complaint (body);
21607 macro_define_function (file, line, name,
21608 argc, (const char **) argv,
21609 p);
21610 }
21611 else
21612 /* Just complain. */
21613 dwarf2_macro_malformed_definition_complaint (body);
21614 }
21615 else
21616 /* Just complain. */
21617 dwarf2_macro_malformed_definition_complaint (body);
21618
21619 xfree (name);
21620 {
21621 int i;
21622
21623 for (i = 0; i < argc; i++)
21624 xfree (argv[i]);
21625 }
21626 xfree (argv);
21627 }
21628 else
21629 dwarf2_macro_malformed_definition_complaint (body);
21630 }
21631
21632 /* Skip some bytes from BYTES according to the form given in FORM.
21633 Returns the new pointer. */
21634
21635 static const gdb_byte *
21636 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
21637 enum dwarf_form form,
21638 unsigned int offset_size,
21639 struct dwarf2_section_info *section)
21640 {
21641 unsigned int bytes_read;
21642
21643 switch (form)
21644 {
21645 case DW_FORM_data1:
21646 case DW_FORM_flag:
21647 ++bytes;
21648 break;
21649
21650 case DW_FORM_data2:
21651 bytes += 2;
21652 break;
21653
21654 case DW_FORM_data4:
21655 bytes += 4;
21656 break;
21657
21658 case DW_FORM_data8:
21659 bytes += 8;
21660 break;
21661
21662 case DW_FORM_data16:
21663 bytes += 16;
21664 break;
21665
21666 case DW_FORM_string:
21667 read_direct_string (abfd, bytes, &bytes_read);
21668 bytes += bytes_read;
21669 break;
21670
21671 case DW_FORM_sec_offset:
21672 case DW_FORM_strp:
21673 case DW_FORM_GNU_strp_alt:
21674 bytes += offset_size;
21675 break;
21676
21677 case DW_FORM_block:
21678 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
21679 bytes += bytes_read;
21680 break;
21681
21682 case DW_FORM_block1:
21683 bytes += 1 + read_1_byte (abfd, bytes);
21684 break;
21685 case DW_FORM_block2:
21686 bytes += 2 + read_2_bytes (abfd, bytes);
21687 break;
21688 case DW_FORM_block4:
21689 bytes += 4 + read_4_bytes (abfd, bytes);
21690 break;
21691
21692 case DW_FORM_sdata:
21693 case DW_FORM_udata:
21694 case DW_FORM_GNU_addr_index:
21695 case DW_FORM_GNU_str_index:
21696 bytes = gdb_skip_leb128 (bytes, buffer_end);
21697 if (bytes == NULL)
21698 {
21699 dwarf2_section_buffer_overflow_complaint (section);
21700 return NULL;
21701 }
21702 break;
21703
21704 default:
21705 {
21706 complain:
21707 complaint (&symfile_complaints,
21708 _("invalid form 0x%x in `%s'"),
21709 form, get_section_name (section));
21710 return NULL;
21711 }
21712 }
21713
21714 return bytes;
21715 }
21716
21717 /* A helper for dwarf_decode_macros that handles skipping an unknown
21718 opcode. Returns an updated pointer to the macro data buffer; or,
21719 on error, issues a complaint and returns NULL. */
21720
21721 static const gdb_byte *
21722 skip_unknown_opcode (unsigned int opcode,
21723 const gdb_byte **opcode_definitions,
21724 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
21725 bfd *abfd,
21726 unsigned int offset_size,
21727 struct dwarf2_section_info *section)
21728 {
21729 unsigned int bytes_read, i;
21730 unsigned long arg;
21731 const gdb_byte *defn;
21732
21733 if (opcode_definitions[opcode] == NULL)
21734 {
21735 complaint (&symfile_complaints,
21736 _("unrecognized DW_MACFINO opcode 0x%x"),
21737 opcode);
21738 return NULL;
21739 }
21740
21741 defn = opcode_definitions[opcode];
21742 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
21743 defn += bytes_read;
21744
21745 for (i = 0; i < arg; ++i)
21746 {
21747 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
21748 (enum dwarf_form) defn[i], offset_size,
21749 section);
21750 if (mac_ptr == NULL)
21751 {
21752 /* skip_form_bytes already issued the complaint. */
21753 return NULL;
21754 }
21755 }
21756
21757 return mac_ptr;
21758 }
21759
21760 /* A helper function which parses the header of a macro section.
21761 If the macro section is the extended (for now called "GNU") type,
21762 then this updates *OFFSET_SIZE. Returns a pointer to just after
21763 the header, or issues a complaint and returns NULL on error. */
21764
21765 static const gdb_byte *
21766 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
21767 bfd *abfd,
21768 const gdb_byte *mac_ptr,
21769 unsigned int *offset_size,
21770 int section_is_gnu)
21771 {
21772 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
21773
21774 if (section_is_gnu)
21775 {
21776 unsigned int version, flags;
21777
21778 version = read_2_bytes (abfd, mac_ptr);
21779 if (version != 4 && version != 5)
21780 {
21781 complaint (&symfile_complaints,
21782 _("unrecognized version `%d' in .debug_macro section"),
21783 version);
21784 return NULL;
21785 }
21786 mac_ptr += 2;
21787
21788 flags = read_1_byte (abfd, mac_ptr);
21789 ++mac_ptr;
21790 *offset_size = (flags & 1) ? 8 : 4;
21791
21792 if ((flags & 2) != 0)
21793 /* We don't need the line table offset. */
21794 mac_ptr += *offset_size;
21795
21796 /* Vendor opcode descriptions. */
21797 if ((flags & 4) != 0)
21798 {
21799 unsigned int i, count;
21800
21801 count = read_1_byte (abfd, mac_ptr);
21802 ++mac_ptr;
21803 for (i = 0; i < count; ++i)
21804 {
21805 unsigned int opcode, bytes_read;
21806 unsigned long arg;
21807
21808 opcode = read_1_byte (abfd, mac_ptr);
21809 ++mac_ptr;
21810 opcode_definitions[opcode] = mac_ptr;
21811 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21812 mac_ptr += bytes_read;
21813 mac_ptr += arg;
21814 }
21815 }
21816 }
21817
21818 return mac_ptr;
21819 }
21820
21821 /* A helper for dwarf_decode_macros that handles the GNU extensions,
21822 including DW_MACRO_import. */
21823
21824 static void
21825 dwarf_decode_macro_bytes (bfd *abfd,
21826 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
21827 struct macro_source_file *current_file,
21828 struct line_header *lh,
21829 struct dwarf2_section_info *section,
21830 int section_is_gnu, int section_is_dwz,
21831 unsigned int offset_size,
21832 htab_t include_hash)
21833 {
21834 struct objfile *objfile = dwarf2_per_objfile->objfile;
21835 enum dwarf_macro_record_type macinfo_type;
21836 int at_commandline;
21837 const gdb_byte *opcode_definitions[256];
21838
21839 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
21840 &offset_size, section_is_gnu);
21841 if (mac_ptr == NULL)
21842 {
21843 /* We already issued a complaint. */
21844 return;
21845 }
21846
21847 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
21848 GDB is still reading the definitions from command line. First
21849 DW_MACINFO_start_file will need to be ignored as it was already executed
21850 to create CURRENT_FILE for the main source holding also the command line
21851 definitions. On first met DW_MACINFO_start_file this flag is reset to
21852 normally execute all the remaining DW_MACINFO_start_file macinfos. */
21853
21854 at_commandline = 1;
21855
21856 do
21857 {
21858 /* Do we at least have room for a macinfo type byte? */
21859 if (mac_ptr >= mac_end)
21860 {
21861 dwarf2_section_buffer_overflow_complaint (section);
21862 break;
21863 }
21864
21865 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
21866 mac_ptr++;
21867
21868 /* Note that we rely on the fact that the corresponding GNU and
21869 DWARF constants are the same. */
21870 switch (macinfo_type)
21871 {
21872 /* A zero macinfo type indicates the end of the macro
21873 information. */
21874 case 0:
21875 break;
21876
21877 case DW_MACRO_define:
21878 case DW_MACRO_undef:
21879 case DW_MACRO_define_strp:
21880 case DW_MACRO_undef_strp:
21881 case DW_MACRO_define_sup:
21882 case DW_MACRO_undef_sup:
21883 {
21884 unsigned int bytes_read;
21885 int line;
21886 const char *body;
21887 int is_define;
21888
21889 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21890 mac_ptr += bytes_read;
21891
21892 if (macinfo_type == DW_MACRO_define
21893 || macinfo_type == DW_MACRO_undef)
21894 {
21895 body = read_direct_string (abfd, mac_ptr, &bytes_read);
21896 mac_ptr += bytes_read;
21897 }
21898 else
21899 {
21900 LONGEST str_offset;
21901
21902 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
21903 mac_ptr += offset_size;
21904
21905 if (macinfo_type == DW_MACRO_define_sup
21906 || macinfo_type == DW_MACRO_undef_sup
21907 || section_is_dwz)
21908 {
21909 struct dwz_file *dwz = dwarf2_get_dwz_file ();
21910
21911 body = read_indirect_string_from_dwz (dwz, str_offset);
21912 }
21913 else
21914 body = read_indirect_string_at_offset (abfd, str_offset);
21915 }
21916
21917 is_define = (macinfo_type == DW_MACRO_define
21918 || macinfo_type == DW_MACRO_define_strp
21919 || macinfo_type == DW_MACRO_define_sup);
21920 if (! current_file)
21921 {
21922 /* DWARF violation as no main source is present. */
21923 complaint (&symfile_complaints,
21924 _("debug info with no main source gives macro %s "
21925 "on line %d: %s"),
21926 is_define ? _("definition") : _("undefinition"),
21927 line, body);
21928 break;
21929 }
21930 if ((line == 0 && !at_commandline)
21931 || (line != 0 && at_commandline))
21932 complaint (&symfile_complaints,
21933 _("debug info gives %s macro %s with %s line %d: %s"),
21934 at_commandline ? _("command-line") : _("in-file"),
21935 is_define ? _("definition") : _("undefinition"),
21936 line == 0 ? _("zero") : _("non-zero"), line, body);
21937
21938 if (is_define)
21939 parse_macro_definition (current_file, line, body);
21940 else
21941 {
21942 gdb_assert (macinfo_type == DW_MACRO_undef
21943 || macinfo_type == DW_MACRO_undef_strp
21944 || macinfo_type == DW_MACRO_undef_sup);
21945 macro_undef (current_file, line, body);
21946 }
21947 }
21948 break;
21949
21950 case DW_MACRO_start_file:
21951 {
21952 unsigned int bytes_read;
21953 int line, file;
21954
21955 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21956 mac_ptr += bytes_read;
21957 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
21958 mac_ptr += bytes_read;
21959
21960 if ((line == 0 && !at_commandline)
21961 || (line != 0 && at_commandline))
21962 complaint (&symfile_complaints,
21963 _("debug info gives source %d included "
21964 "from %s at %s line %d"),
21965 file, at_commandline ? _("command-line") : _("file"),
21966 line == 0 ? _("zero") : _("non-zero"), line);
21967
21968 if (at_commandline)
21969 {
21970 /* This DW_MACRO_start_file was executed in the
21971 pass one. */
21972 at_commandline = 0;
21973 }
21974 else
21975 current_file = macro_start_file (file, line, current_file, lh);
21976 }
21977 break;
21978
21979 case DW_MACRO_end_file:
21980 if (! current_file)
21981 complaint (&symfile_complaints,
21982 _("macro debug info has an unmatched "
21983 "`close_file' directive"));
21984 else
21985 {
21986 current_file = current_file->included_by;
21987 if (! current_file)
21988 {
21989 enum dwarf_macro_record_type next_type;
21990
21991 /* GCC circa March 2002 doesn't produce the zero
21992 type byte marking the end of the compilation
21993 unit. Complain if it's not there, but exit no
21994 matter what. */
21995
21996 /* Do we at least have room for a macinfo type byte? */
21997 if (mac_ptr >= mac_end)
21998 {
21999 dwarf2_section_buffer_overflow_complaint (section);
22000 return;
22001 }
22002
22003 /* We don't increment mac_ptr here, so this is just
22004 a look-ahead. */
22005 next_type
22006 = (enum dwarf_macro_record_type) read_1_byte (abfd,
22007 mac_ptr);
22008 if (next_type != 0)
22009 complaint (&symfile_complaints,
22010 _("no terminating 0-type entry for "
22011 "macros in `.debug_macinfo' section"));
22012
22013 return;
22014 }
22015 }
22016 break;
22017
22018 case DW_MACRO_import:
22019 case DW_MACRO_import_sup:
22020 {
22021 LONGEST offset;
22022 void **slot;
22023 bfd *include_bfd = abfd;
22024 struct dwarf2_section_info *include_section = section;
22025 const gdb_byte *include_mac_end = mac_end;
22026 int is_dwz = section_is_dwz;
22027 const gdb_byte *new_mac_ptr;
22028
22029 offset = read_offset_1 (abfd, mac_ptr, offset_size);
22030 mac_ptr += offset_size;
22031
22032 if (macinfo_type == DW_MACRO_import_sup)
22033 {
22034 struct dwz_file *dwz = dwarf2_get_dwz_file ();
22035
22036 dwarf2_read_section (objfile, &dwz->macro);
22037
22038 include_section = &dwz->macro;
22039 include_bfd = get_section_bfd_owner (include_section);
22040 include_mac_end = dwz->macro.buffer + dwz->macro.size;
22041 is_dwz = 1;
22042 }
22043
22044 new_mac_ptr = include_section->buffer + offset;
22045 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
22046
22047 if (*slot != NULL)
22048 {
22049 /* This has actually happened; see
22050 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
22051 complaint (&symfile_complaints,
22052 _("recursive DW_MACRO_import in "
22053 ".debug_macro section"));
22054 }
22055 else
22056 {
22057 *slot = (void *) new_mac_ptr;
22058
22059 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
22060 include_mac_end, current_file, lh,
22061 section, section_is_gnu, is_dwz,
22062 offset_size, include_hash);
22063
22064 htab_remove_elt (include_hash, (void *) new_mac_ptr);
22065 }
22066 }
22067 break;
22068
22069 case DW_MACINFO_vendor_ext:
22070 if (!section_is_gnu)
22071 {
22072 unsigned int bytes_read;
22073
22074 /* This reads the constant, but since we don't recognize
22075 any vendor extensions, we ignore it. */
22076 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22077 mac_ptr += bytes_read;
22078 read_direct_string (abfd, mac_ptr, &bytes_read);
22079 mac_ptr += bytes_read;
22080
22081 /* We don't recognize any vendor extensions. */
22082 break;
22083 }
22084 /* FALLTHROUGH */
22085
22086 default:
22087 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
22088 mac_ptr, mac_end, abfd, offset_size,
22089 section);
22090 if (mac_ptr == NULL)
22091 return;
22092 break;
22093 }
22094 } while (macinfo_type != 0);
22095 }
22096
22097 static void
22098 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22099 int section_is_gnu)
22100 {
22101 struct objfile *objfile = dwarf2_per_objfile->objfile;
22102 struct line_header *lh = cu->line_header;
22103 bfd *abfd;
22104 const gdb_byte *mac_ptr, *mac_end;
22105 struct macro_source_file *current_file = 0;
22106 enum dwarf_macro_record_type macinfo_type;
22107 unsigned int offset_size = cu->header.offset_size;
22108 const gdb_byte *opcode_definitions[256];
22109 struct cleanup *cleanup;
22110 void **slot;
22111 struct dwarf2_section_info *section;
22112 const char *section_name;
22113
22114 if (cu->dwo_unit != NULL)
22115 {
22116 if (section_is_gnu)
22117 {
22118 section = &cu->dwo_unit->dwo_file->sections.macro;
22119 section_name = ".debug_macro.dwo";
22120 }
22121 else
22122 {
22123 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22124 section_name = ".debug_macinfo.dwo";
22125 }
22126 }
22127 else
22128 {
22129 if (section_is_gnu)
22130 {
22131 section = &dwarf2_per_objfile->macro;
22132 section_name = ".debug_macro";
22133 }
22134 else
22135 {
22136 section = &dwarf2_per_objfile->macinfo;
22137 section_name = ".debug_macinfo";
22138 }
22139 }
22140
22141 dwarf2_read_section (objfile, section);
22142 if (section->buffer == NULL)
22143 {
22144 complaint (&symfile_complaints, _("missing %s section"), section_name);
22145 return;
22146 }
22147 abfd = get_section_bfd_owner (section);
22148
22149 /* First pass: Find the name of the base filename.
22150 This filename is needed in order to process all macros whose definition
22151 (or undefinition) comes from the command line. These macros are defined
22152 before the first DW_MACINFO_start_file entry, and yet still need to be
22153 associated to the base file.
22154
22155 To determine the base file name, we scan the macro definitions until we
22156 reach the first DW_MACINFO_start_file entry. We then initialize
22157 CURRENT_FILE accordingly so that any macro definition found before the
22158 first DW_MACINFO_start_file can still be associated to the base file. */
22159
22160 mac_ptr = section->buffer + offset;
22161 mac_end = section->buffer + section->size;
22162
22163 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
22164 &offset_size, section_is_gnu);
22165 if (mac_ptr == NULL)
22166 {
22167 /* We already issued a complaint. */
22168 return;
22169 }
22170
22171 do
22172 {
22173 /* Do we at least have room for a macinfo type byte? */
22174 if (mac_ptr >= mac_end)
22175 {
22176 /* Complaint is printed during the second pass as GDB will probably
22177 stop the first pass earlier upon finding
22178 DW_MACINFO_start_file. */
22179 break;
22180 }
22181
22182 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
22183 mac_ptr++;
22184
22185 /* Note that we rely on the fact that the corresponding GNU and
22186 DWARF constants are the same. */
22187 switch (macinfo_type)
22188 {
22189 /* A zero macinfo type indicates the end of the macro
22190 information. */
22191 case 0:
22192 break;
22193
22194 case DW_MACRO_define:
22195 case DW_MACRO_undef:
22196 /* Only skip the data by MAC_PTR. */
22197 {
22198 unsigned int bytes_read;
22199
22200 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22201 mac_ptr += bytes_read;
22202 read_direct_string (abfd, mac_ptr, &bytes_read);
22203 mac_ptr += bytes_read;
22204 }
22205 break;
22206
22207 case DW_MACRO_start_file:
22208 {
22209 unsigned int bytes_read;
22210 int line, file;
22211
22212 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22213 mac_ptr += bytes_read;
22214 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22215 mac_ptr += bytes_read;
22216
22217 current_file = macro_start_file (file, line, current_file, lh);
22218 }
22219 break;
22220
22221 case DW_MACRO_end_file:
22222 /* No data to skip by MAC_PTR. */
22223 break;
22224
22225 case DW_MACRO_define_strp:
22226 case DW_MACRO_undef_strp:
22227 case DW_MACRO_define_sup:
22228 case DW_MACRO_undef_sup:
22229 {
22230 unsigned int bytes_read;
22231
22232 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22233 mac_ptr += bytes_read;
22234 mac_ptr += offset_size;
22235 }
22236 break;
22237
22238 case DW_MACRO_import:
22239 case DW_MACRO_import_sup:
22240 /* Note that, according to the spec, a transparent include
22241 chain cannot call DW_MACRO_start_file. So, we can just
22242 skip this opcode. */
22243 mac_ptr += offset_size;
22244 break;
22245
22246 case DW_MACINFO_vendor_ext:
22247 /* Only skip the data by MAC_PTR. */
22248 if (!section_is_gnu)
22249 {
22250 unsigned int bytes_read;
22251
22252 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
22253 mac_ptr += bytes_read;
22254 read_direct_string (abfd, mac_ptr, &bytes_read);
22255 mac_ptr += bytes_read;
22256 }
22257 /* FALLTHROUGH */
22258
22259 default:
22260 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
22261 mac_ptr, mac_end, abfd, offset_size,
22262 section);
22263 if (mac_ptr == NULL)
22264 return;
22265 break;
22266 }
22267 } while (macinfo_type != 0 && current_file == NULL);
22268
22269 /* Second pass: Process all entries.
22270
22271 Use the AT_COMMAND_LINE flag to determine whether we are still processing
22272 command-line macro definitions/undefinitions. This flag is unset when we
22273 reach the first DW_MACINFO_start_file entry. */
22274
22275 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
22276 htab_eq_pointer,
22277 NULL, xcalloc, xfree));
22278 mac_ptr = section->buffer + offset;
22279 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
22280 *slot = (void *) mac_ptr;
22281 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
22282 current_file, lh, section,
22283 section_is_gnu, 0, offset_size,
22284 include_hash.get ());
22285 }
22286
22287 /* Check if the attribute's form is a DW_FORM_block*
22288 if so return true else false. */
22289
22290 static int
22291 attr_form_is_block (const struct attribute *attr)
22292 {
22293 return (attr == NULL ? 0 :
22294 attr->form == DW_FORM_block1
22295 || attr->form == DW_FORM_block2
22296 || attr->form == DW_FORM_block4
22297 || attr->form == DW_FORM_block
22298 || attr->form == DW_FORM_exprloc);
22299 }
22300
22301 /* Return non-zero if ATTR's value is a section offset --- classes
22302 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
22303 You may use DW_UNSND (attr) to retrieve such offsets.
22304
22305 Section 7.5.4, "Attribute Encodings", explains that no attribute
22306 may have a value that belongs to more than one of these classes; it
22307 would be ambiguous if we did, because we use the same forms for all
22308 of them. */
22309
22310 static int
22311 attr_form_is_section_offset (const struct attribute *attr)
22312 {
22313 return (attr->form == DW_FORM_data4
22314 || attr->form == DW_FORM_data8
22315 || attr->form == DW_FORM_sec_offset);
22316 }
22317
22318 /* Return non-zero if ATTR's value falls in the 'constant' class, or
22319 zero otherwise. When this function returns true, you can apply
22320 dwarf2_get_attr_constant_value to it.
22321
22322 However, note that for some attributes you must check
22323 attr_form_is_section_offset before using this test. DW_FORM_data4
22324 and DW_FORM_data8 are members of both the constant class, and of
22325 the classes that contain offsets into other debug sections
22326 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
22327 that, if an attribute's can be either a constant or one of the
22328 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
22329 taken as section offsets, not constants.
22330
22331 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
22332 cannot handle that. */
22333
22334 static int
22335 attr_form_is_constant (const struct attribute *attr)
22336 {
22337 switch (attr->form)
22338 {
22339 case DW_FORM_sdata:
22340 case DW_FORM_udata:
22341 case DW_FORM_data1:
22342 case DW_FORM_data2:
22343 case DW_FORM_data4:
22344 case DW_FORM_data8:
22345 return 1;
22346 default:
22347 return 0;
22348 }
22349 }
22350
22351
22352 /* DW_ADDR is always stored already as sect_offset; despite for the forms
22353 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
22354
22355 static int
22356 attr_form_is_ref (const struct attribute *attr)
22357 {
22358 switch (attr->form)
22359 {
22360 case DW_FORM_ref_addr:
22361 case DW_FORM_ref1:
22362 case DW_FORM_ref2:
22363 case DW_FORM_ref4:
22364 case DW_FORM_ref8:
22365 case DW_FORM_ref_udata:
22366 case DW_FORM_GNU_ref_alt:
22367 return 1;
22368 default:
22369 return 0;
22370 }
22371 }
22372
22373 /* Return the .debug_loc section to use for CU.
22374 For DWO files use .debug_loc.dwo. */
22375
22376 static struct dwarf2_section_info *
22377 cu_debug_loc_section (struct dwarf2_cu *cu)
22378 {
22379 if (cu->dwo_unit)
22380 {
22381 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22382
22383 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22384 }
22385 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22386 : &dwarf2_per_objfile->loc);
22387 }
22388
22389 /* A helper function that fills in a dwarf2_loclist_baton. */
22390
22391 static void
22392 fill_in_loclist_baton (struct dwarf2_cu *cu,
22393 struct dwarf2_loclist_baton *baton,
22394 const struct attribute *attr)
22395 {
22396 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22397
22398 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
22399
22400 baton->per_cu = cu->per_cu;
22401 gdb_assert (baton->per_cu);
22402 /* We don't know how long the location list is, but make sure we
22403 don't run off the edge of the section. */
22404 baton->size = section->size - DW_UNSND (attr);
22405 baton->data = section->buffer + DW_UNSND (attr);
22406 baton->base_address = cu->base_address;
22407 baton->from_dwo = cu->dwo_unit != NULL;
22408 }
22409
22410 static void
22411 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
22412 struct dwarf2_cu *cu, int is_block)
22413 {
22414 struct objfile *objfile = dwarf2_per_objfile->objfile;
22415 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22416
22417 if (attr_form_is_section_offset (attr)
22418 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
22419 the section. If so, fall through to the complaint in the
22420 other branch. */
22421 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
22422 {
22423 struct dwarf2_loclist_baton *baton;
22424
22425 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
22426
22427 fill_in_loclist_baton (cu, baton, attr);
22428
22429 if (cu->base_known == 0)
22430 complaint (&symfile_complaints,
22431 _("Location list used without "
22432 "specifying the CU base address."));
22433
22434 SYMBOL_ACLASS_INDEX (sym) = (is_block
22435 ? dwarf2_loclist_block_index
22436 : dwarf2_loclist_index);
22437 SYMBOL_LOCATION_BATON (sym) = baton;
22438 }
22439 else
22440 {
22441 struct dwarf2_locexpr_baton *baton;
22442
22443 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
22444 baton->per_cu = cu->per_cu;
22445 gdb_assert (baton->per_cu);
22446
22447 if (attr_form_is_block (attr))
22448 {
22449 /* Note that we're just copying the block's data pointer
22450 here, not the actual data. We're still pointing into the
22451 info_buffer for SYM's objfile; right now we never release
22452 that buffer, but when we do clean up properly this may
22453 need to change. */
22454 baton->size = DW_BLOCK (attr)->size;
22455 baton->data = DW_BLOCK (attr)->data;
22456 }
22457 else
22458 {
22459 dwarf2_invalid_attrib_class_complaint ("location description",
22460 SYMBOL_NATURAL_NAME (sym));
22461 baton->size = 0;
22462 }
22463
22464 SYMBOL_ACLASS_INDEX (sym) = (is_block
22465 ? dwarf2_locexpr_block_index
22466 : dwarf2_locexpr_index);
22467 SYMBOL_LOCATION_BATON (sym) = baton;
22468 }
22469 }
22470
22471 /* Return the OBJFILE associated with the compilation unit CU. If CU
22472 came from a separate debuginfo file, then the master objfile is
22473 returned. */
22474
22475 struct objfile *
22476 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
22477 {
22478 struct objfile *objfile = per_cu->objfile;
22479
22480 /* Return the master objfile, so that we can report and look up the
22481 correct file containing this variable. */
22482 if (objfile->separate_debug_objfile_backlink)
22483 objfile = objfile->separate_debug_objfile_backlink;
22484
22485 return objfile;
22486 }
22487
22488 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22489 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22490 CU_HEADERP first. */
22491
22492 static const struct comp_unit_head *
22493 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22494 struct dwarf2_per_cu_data *per_cu)
22495 {
22496 const gdb_byte *info_ptr;
22497
22498 if (per_cu->cu)
22499 return &per_cu->cu->header;
22500
22501 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
22502
22503 memset (cu_headerp, 0, sizeof (*cu_headerp));
22504 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22505 rcuh_kind::COMPILE);
22506
22507 return cu_headerp;
22508 }
22509
22510 /* Return the address size given in the compilation unit header for CU. */
22511
22512 int
22513 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
22514 {
22515 struct comp_unit_head cu_header_local;
22516 const struct comp_unit_head *cu_headerp;
22517
22518 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22519
22520 return cu_headerp->addr_size;
22521 }
22522
22523 /* Return the offset size given in the compilation unit header for CU. */
22524
22525 int
22526 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
22527 {
22528 struct comp_unit_head cu_header_local;
22529 const struct comp_unit_head *cu_headerp;
22530
22531 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22532
22533 return cu_headerp->offset_size;
22534 }
22535
22536 /* See its dwarf2loc.h declaration. */
22537
22538 int
22539 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
22540 {
22541 struct comp_unit_head cu_header_local;
22542 const struct comp_unit_head *cu_headerp;
22543
22544 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
22545
22546 if (cu_headerp->version == 2)
22547 return cu_headerp->addr_size;
22548 else
22549 return cu_headerp->offset_size;
22550 }
22551
22552 /* Return the text offset of the CU. The returned offset comes from
22553 this CU's objfile. If this objfile came from a separate debuginfo
22554 file, then the offset may be different from the corresponding
22555 offset in the parent objfile. */
22556
22557 CORE_ADDR
22558 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
22559 {
22560 struct objfile *objfile = per_cu->objfile;
22561
22562 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
22563 }
22564
22565 /* Return DWARF version number of PER_CU. */
22566
22567 short
22568 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
22569 {
22570 return per_cu->dwarf_version;
22571 }
22572
22573 /* Locate the .debug_info compilation unit from CU's objfile which contains
22574 the DIE at OFFSET. Raises an error on failure. */
22575
22576 static struct dwarf2_per_cu_data *
22577 dwarf2_find_containing_comp_unit (sect_offset offset,
22578 unsigned int offset_in_dwz,
22579 struct objfile *objfile)
22580 {
22581 struct dwarf2_per_cu_data *this_cu;
22582 int low, high;
22583 const sect_offset *cu_off;
22584
22585 low = 0;
22586 high = dwarf2_per_objfile->n_comp_units - 1;
22587 while (high > low)
22588 {
22589 struct dwarf2_per_cu_data *mid_cu;
22590 int mid = low + (high - low) / 2;
22591
22592 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
22593 cu_off = &mid_cu->offset;
22594 if (mid_cu->is_dwz > offset_in_dwz
22595 || (mid_cu->is_dwz == offset_in_dwz
22596 && cu_off->sect_off >= offset.sect_off))
22597 high = mid;
22598 else
22599 low = mid + 1;
22600 }
22601 gdb_assert (low == high);
22602 this_cu = dwarf2_per_objfile->all_comp_units[low];
22603 cu_off = &this_cu->offset;
22604 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
22605 {
22606 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
22607 error (_("Dwarf Error: could not find partial DIE containing "
22608 "offset 0x%lx [in module %s]"),
22609 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
22610
22611 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
22612 <= offset.sect_off);
22613 return dwarf2_per_objfile->all_comp_units[low-1];
22614 }
22615 else
22616 {
22617 this_cu = dwarf2_per_objfile->all_comp_units[low];
22618 if (low == dwarf2_per_objfile->n_comp_units - 1
22619 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
22620 error (_("invalid dwarf2 offset %u"), offset.sect_off);
22621 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
22622 return this_cu;
22623 }
22624 }
22625
22626 /* Initialize dwarf2_cu CU, owned by PER_CU. */
22627
22628 static void
22629 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
22630 {
22631 memset (cu, 0, sizeof (*cu));
22632 per_cu->cu = cu;
22633 cu->per_cu = per_cu;
22634 cu->objfile = per_cu->objfile;
22635 obstack_init (&cu->comp_unit_obstack);
22636 }
22637
22638 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22639
22640 static void
22641 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22642 enum language pretend_language)
22643 {
22644 struct attribute *attr;
22645
22646 /* Set the language we're debugging. */
22647 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22648 if (attr)
22649 set_cu_language (DW_UNSND (attr), cu);
22650 else
22651 {
22652 cu->language = pretend_language;
22653 cu->language_defn = language_def (cu->language);
22654 }
22655
22656 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
22657 }
22658
22659 /* Release one cached compilation unit, CU. We unlink it from the tree
22660 of compilation units, but we don't remove it from the read_in_chain;
22661 the caller is responsible for that.
22662 NOTE: DATA is a void * because this function is also used as a
22663 cleanup routine. */
22664
22665 static void
22666 free_heap_comp_unit (void *data)
22667 {
22668 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
22669
22670 gdb_assert (cu->per_cu != NULL);
22671 cu->per_cu->cu = NULL;
22672 cu->per_cu = NULL;
22673
22674 obstack_free (&cu->comp_unit_obstack, NULL);
22675
22676 xfree (cu);
22677 }
22678
22679 /* This cleanup function is passed the address of a dwarf2_cu on the stack
22680 when we're finished with it. We can't free the pointer itself, but be
22681 sure to unlink it from the cache. Also release any associated storage. */
22682
22683 static void
22684 free_stack_comp_unit (void *data)
22685 {
22686 struct dwarf2_cu *cu = (struct dwarf2_cu *) data;
22687
22688 gdb_assert (cu->per_cu != NULL);
22689 cu->per_cu->cu = NULL;
22690 cu->per_cu = NULL;
22691
22692 obstack_free (&cu->comp_unit_obstack, NULL);
22693 cu->partial_dies = NULL;
22694 }
22695
22696 /* Free all cached compilation units. */
22697
22698 static void
22699 free_cached_comp_units (void *data)
22700 {
22701 struct dwarf2_per_cu_data *per_cu, **last_chain;
22702
22703 per_cu = dwarf2_per_objfile->read_in_chain;
22704 last_chain = &dwarf2_per_objfile->read_in_chain;
22705 while (per_cu != NULL)
22706 {
22707 struct dwarf2_per_cu_data *next_cu;
22708
22709 next_cu = per_cu->cu->read_in_chain;
22710
22711 free_heap_comp_unit (per_cu->cu);
22712 *last_chain = next_cu;
22713
22714 per_cu = next_cu;
22715 }
22716 }
22717
22718 /* Increase the age counter on each cached compilation unit, and free
22719 any that are too old. */
22720
22721 static void
22722 age_cached_comp_units (void)
22723 {
22724 struct dwarf2_per_cu_data *per_cu, **last_chain;
22725
22726 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22727 per_cu = dwarf2_per_objfile->read_in_chain;
22728 while (per_cu != NULL)
22729 {
22730 per_cu->cu->last_used ++;
22731 if (per_cu->cu->last_used <= dwarf_max_cache_age)
22732 dwarf2_mark (per_cu->cu);
22733 per_cu = per_cu->cu->read_in_chain;
22734 }
22735
22736 per_cu = dwarf2_per_objfile->read_in_chain;
22737 last_chain = &dwarf2_per_objfile->read_in_chain;
22738 while (per_cu != NULL)
22739 {
22740 struct dwarf2_per_cu_data *next_cu;
22741
22742 next_cu = per_cu->cu->read_in_chain;
22743
22744 if (!per_cu->cu->mark)
22745 {
22746 free_heap_comp_unit (per_cu->cu);
22747 *last_chain = next_cu;
22748 }
22749 else
22750 last_chain = &per_cu->cu->read_in_chain;
22751
22752 per_cu = next_cu;
22753 }
22754 }
22755
22756 /* Remove a single compilation unit from the cache. */
22757
22758 static void
22759 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
22760 {
22761 struct dwarf2_per_cu_data *per_cu, **last_chain;
22762
22763 per_cu = dwarf2_per_objfile->read_in_chain;
22764 last_chain = &dwarf2_per_objfile->read_in_chain;
22765 while (per_cu != NULL)
22766 {
22767 struct dwarf2_per_cu_data *next_cu;
22768
22769 next_cu = per_cu->cu->read_in_chain;
22770
22771 if (per_cu == target_per_cu)
22772 {
22773 free_heap_comp_unit (per_cu->cu);
22774 per_cu->cu = NULL;
22775 *last_chain = next_cu;
22776 break;
22777 }
22778 else
22779 last_chain = &per_cu->cu->read_in_chain;
22780
22781 per_cu = next_cu;
22782 }
22783 }
22784
22785 /* Release all extra memory associated with OBJFILE. */
22786
22787 void
22788 dwarf2_free_objfile (struct objfile *objfile)
22789 {
22790 dwarf2_per_objfile
22791 = (struct dwarf2_per_objfile *) objfile_data (objfile,
22792 dwarf2_objfile_data_key);
22793
22794 if (dwarf2_per_objfile == NULL)
22795 return;
22796
22797 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
22798 free_cached_comp_units (NULL);
22799
22800 if (dwarf2_per_objfile->quick_file_names_table)
22801 htab_delete (dwarf2_per_objfile->quick_file_names_table);
22802
22803 if (dwarf2_per_objfile->line_header_hash)
22804 htab_delete (dwarf2_per_objfile->line_header_hash);
22805
22806 /* Everything else should be on the objfile obstack. */
22807 }
22808
22809 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
22810 We store these in a hash table separate from the DIEs, and preserve them
22811 when the DIEs are flushed out of cache.
22812
22813 The CU "per_cu" pointer is needed because offset alone is not enough to
22814 uniquely identify the type. A file may have multiple .debug_types sections,
22815 or the type may come from a DWO file. Furthermore, while it's more logical
22816 to use per_cu->section+offset, with Fission the section with the data is in
22817 the DWO file but we don't know that section at the point we need it.
22818 We have to use something in dwarf2_per_cu_data (or the pointer to it)
22819 because we can enter the lookup routine, get_die_type_at_offset, from
22820 outside this file, and thus won't necessarily have PER_CU->cu.
22821 Fortunately, PER_CU is stable for the life of the objfile. */
22822
22823 struct dwarf2_per_cu_offset_and_type
22824 {
22825 const struct dwarf2_per_cu_data *per_cu;
22826 sect_offset offset;
22827 struct type *type;
22828 };
22829
22830 /* Hash function for a dwarf2_per_cu_offset_and_type. */
22831
22832 static hashval_t
22833 per_cu_offset_and_type_hash (const void *item)
22834 {
22835 const struct dwarf2_per_cu_offset_and_type *ofs
22836 = (const struct dwarf2_per_cu_offset_and_type *) item;
22837
22838 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
22839 }
22840
22841 /* Equality function for a dwarf2_per_cu_offset_and_type. */
22842
22843 static int
22844 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
22845 {
22846 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
22847 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
22848 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
22849 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
22850
22851 return (ofs_lhs->per_cu == ofs_rhs->per_cu
22852 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
22853 }
22854
22855 /* Set the type associated with DIE to TYPE. Save it in CU's hash
22856 table if necessary. For convenience, return TYPE.
22857
22858 The DIEs reading must have careful ordering to:
22859 * Not cause infite loops trying to read in DIEs as a prerequisite for
22860 reading current DIE.
22861 * Not trying to dereference contents of still incompletely read in types
22862 while reading in other DIEs.
22863 * Enable referencing still incompletely read in types just by a pointer to
22864 the type without accessing its fields.
22865
22866 Therefore caller should follow these rules:
22867 * Try to fetch any prerequisite types we may need to build this DIE type
22868 before building the type and calling set_die_type.
22869 * After building type call set_die_type for current DIE as soon as
22870 possible before fetching more types to complete the current type.
22871 * Make the type as complete as possible before fetching more types. */
22872
22873 static struct type *
22874 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
22875 {
22876 struct dwarf2_per_cu_offset_and_type **slot, ofs;
22877 struct objfile *objfile = cu->objfile;
22878 struct attribute *attr;
22879 struct dynamic_prop prop;
22880
22881 /* For Ada types, make sure that the gnat-specific data is always
22882 initialized (if not already set). There are a few types where
22883 we should not be doing so, because the type-specific area is
22884 already used to hold some other piece of info (eg: TYPE_CODE_FLT
22885 where the type-specific area is used to store the floatformat).
22886 But this is not a problem, because the gnat-specific information
22887 is actually not needed for these types. */
22888 if (need_gnat_info (cu)
22889 && TYPE_CODE (type) != TYPE_CODE_FUNC
22890 && TYPE_CODE (type) != TYPE_CODE_FLT
22891 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
22892 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
22893 && TYPE_CODE (type) != TYPE_CODE_METHOD
22894 && !HAVE_GNAT_AUX_INFO (type))
22895 INIT_GNAT_SPECIFIC (type);
22896
22897 /* Read DW_AT_allocated and set in type. */
22898 attr = dwarf2_attr (die, DW_AT_allocated, cu);
22899 if (attr_form_is_block (attr))
22900 {
22901 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22902 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type, objfile);
22903 }
22904 else if (attr != NULL)
22905 {
22906 complaint (&symfile_complaints,
22907 _("DW_AT_allocated has the wrong form (%s) at DIE 0x%x"),
22908 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22909 die->offset.sect_off);
22910 }
22911
22912 /* Read DW_AT_associated and set in type. */
22913 attr = dwarf2_attr (die, DW_AT_associated, cu);
22914 if (attr_form_is_block (attr))
22915 {
22916 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22917 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type, objfile);
22918 }
22919 else if (attr != NULL)
22920 {
22921 complaint (&symfile_complaints,
22922 _("DW_AT_associated has the wrong form (%s) at DIE 0x%x"),
22923 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
22924 die->offset.sect_off);
22925 }
22926
22927 /* Read DW_AT_data_location and set in type. */
22928 attr = dwarf2_attr (die, DW_AT_data_location, cu);
22929 if (attr_to_dynamic_prop (attr, die, cu, &prop))
22930 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type, objfile);
22931
22932 if (dwarf2_per_objfile->die_type_hash == NULL)
22933 {
22934 dwarf2_per_objfile->die_type_hash =
22935 htab_create_alloc_ex (127,
22936 per_cu_offset_and_type_hash,
22937 per_cu_offset_and_type_eq,
22938 NULL,
22939 &objfile->objfile_obstack,
22940 hashtab_obstack_allocate,
22941 dummy_obstack_deallocate);
22942 }
22943
22944 ofs.per_cu = cu->per_cu;
22945 ofs.offset = die->offset;
22946 ofs.type = type;
22947 slot = (struct dwarf2_per_cu_offset_and_type **)
22948 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
22949 if (*slot)
22950 complaint (&symfile_complaints,
22951 _("A problem internal to GDB: DIE 0x%x has type already set"),
22952 die->offset.sect_off);
22953 *slot = XOBNEW (&objfile->objfile_obstack,
22954 struct dwarf2_per_cu_offset_and_type);
22955 **slot = ofs;
22956 return type;
22957 }
22958
22959 /* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
22960 or return NULL if the die does not have a saved type. */
22961
22962 static struct type *
22963 get_die_type_at_offset (sect_offset offset,
22964 struct dwarf2_per_cu_data *per_cu)
22965 {
22966 struct dwarf2_per_cu_offset_and_type *slot, ofs;
22967
22968 if (dwarf2_per_objfile->die_type_hash == NULL)
22969 return NULL;
22970
22971 ofs.per_cu = per_cu;
22972 ofs.offset = offset;
22973 slot = ((struct dwarf2_per_cu_offset_and_type *)
22974 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
22975 if (slot)
22976 return slot->type;
22977 else
22978 return NULL;
22979 }
22980
22981 /* Look up the type for DIE in CU in die_type_hash,
22982 or return NULL if DIE does not have a saved type. */
22983
22984 static struct type *
22985 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
22986 {
22987 return get_die_type_at_offset (die->offset, cu->per_cu);
22988 }
22989
22990 /* Add a dependence relationship from CU to REF_PER_CU. */
22991
22992 static void
22993 dwarf2_add_dependence (struct dwarf2_cu *cu,
22994 struct dwarf2_per_cu_data *ref_per_cu)
22995 {
22996 void **slot;
22997
22998 if (cu->dependencies == NULL)
22999 cu->dependencies
23000 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23001 NULL, &cu->comp_unit_obstack,
23002 hashtab_obstack_allocate,
23003 dummy_obstack_deallocate);
23004
23005 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23006 if (*slot == NULL)
23007 *slot = ref_per_cu;
23008 }
23009
23010 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23011 Set the mark field in every compilation unit in the
23012 cache that we must keep because we are keeping CU. */
23013
23014 static int
23015 dwarf2_mark_helper (void **slot, void *data)
23016 {
23017 struct dwarf2_per_cu_data *per_cu;
23018
23019 per_cu = (struct dwarf2_per_cu_data *) *slot;
23020
23021 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23022 reading of the chain. As such dependencies remain valid it is not much
23023 useful to track and undo them during QUIT cleanups. */
23024 if (per_cu->cu == NULL)
23025 return 1;
23026
23027 if (per_cu->cu->mark)
23028 return 1;
23029 per_cu->cu->mark = 1;
23030
23031 if (per_cu->cu->dependencies != NULL)
23032 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23033
23034 return 1;
23035 }
23036
23037 /* Set the mark field in CU and in every other compilation unit in the
23038 cache that we must keep because we are keeping CU. */
23039
23040 static void
23041 dwarf2_mark (struct dwarf2_cu *cu)
23042 {
23043 if (cu->mark)
23044 return;
23045 cu->mark = 1;
23046 if (cu->dependencies != NULL)
23047 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23048 }
23049
23050 static void
23051 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23052 {
23053 while (per_cu)
23054 {
23055 per_cu->cu->mark = 0;
23056 per_cu = per_cu->cu->read_in_chain;
23057 }
23058 }
23059
23060 /* Trivial hash function for partial_die_info: the hash value of a DIE
23061 is its offset in .debug_info for this objfile. */
23062
23063 static hashval_t
23064 partial_die_hash (const void *item)
23065 {
23066 const struct partial_die_info *part_die
23067 = (const struct partial_die_info *) item;
23068
23069 return part_die->offset.sect_off;
23070 }
23071
23072 /* Trivial comparison function for partial_die_info structures: two DIEs
23073 are equal if they have the same offset. */
23074
23075 static int
23076 partial_die_eq (const void *item_lhs, const void *item_rhs)
23077 {
23078 const struct partial_die_info *part_die_lhs
23079 = (const struct partial_die_info *) item_lhs;
23080 const struct partial_die_info *part_die_rhs
23081 = (const struct partial_die_info *) item_rhs;
23082
23083 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
23084 }
23085
23086 static struct cmd_list_element *set_dwarf_cmdlist;
23087 static struct cmd_list_element *show_dwarf_cmdlist;
23088
23089 static void
23090 set_dwarf_cmd (char *args, int from_tty)
23091 {
23092 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
23093 gdb_stdout);
23094 }
23095
23096 static void
23097 show_dwarf_cmd (char *args, int from_tty)
23098 {
23099 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
23100 }
23101
23102 /* Free data associated with OBJFILE, if necessary. */
23103
23104 static void
23105 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
23106 {
23107 struct dwarf2_per_objfile *data = (struct dwarf2_per_objfile *) d;
23108 int ix;
23109
23110 /* Make sure we don't accidentally use dwarf2_per_objfile while
23111 cleaning up. */
23112 dwarf2_per_objfile = NULL;
23113
23114 for (ix = 0; ix < data->n_comp_units; ++ix)
23115 VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
23116
23117 for (ix = 0; ix < data->n_type_units; ++ix)
23118 VEC_free (dwarf2_per_cu_ptr,
23119 data->all_type_units[ix]->per_cu.imported_symtabs);
23120 xfree (data->all_type_units);
23121
23122 VEC_free (dwarf2_section_info_def, data->types);
23123
23124 if (data->dwo_files)
23125 free_dwo_files (data->dwo_files, objfile);
23126 if (data->dwp_file)
23127 gdb_bfd_unref (data->dwp_file->dbfd);
23128
23129 if (data->dwz_file && data->dwz_file->dwz_bfd)
23130 gdb_bfd_unref (data->dwz_file->dwz_bfd);
23131 }
23132
23133 \f
23134 /* The "save gdb-index" command. */
23135
23136 /* The contents of the hash table we create when building the string
23137 table. */
23138 struct strtab_entry
23139 {
23140 offset_type offset;
23141 const char *str;
23142 };
23143
23144 /* Hash function for a strtab_entry.
23145
23146 Function is used only during write_hash_table so no index format backward
23147 compatibility is needed. */
23148
23149 static hashval_t
23150 hash_strtab_entry (const void *e)
23151 {
23152 const struct strtab_entry *entry = (const struct strtab_entry *) e;
23153 return mapped_index_string_hash (INT_MAX, entry->str);
23154 }
23155
23156 /* Equality function for a strtab_entry. */
23157
23158 static int
23159 eq_strtab_entry (const void *a, const void *b)
23160 {
23161 const struct strtab_entry *ea = (const struct strtab_entry *) a;
23162 const struct strtab_entry *eb = (const struct strtab_entry *) b;
23163 return !strcmp (ea->str, eb->str);
23164 }
23165
23166 /* Create a strtab_entry hash table. */
23167
23168 static htab_t
23169 create_strtab (void)
23170 {
23171 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
23172 xfree, xcalloc, xfree);
23173 }
23174
23175 /* Add a string to the constant pool. Return the string's offset in
23176 host order. */
23177
23178 static offset_type
23179 add_string (htab_t table, struct obstack *cpool, const char *str)
23180 {
23181 void **slot;
23182 struct strtab_entry entry;
23183 struct strtab_entry *result;
23184
23185 entry.str = str;
23186 slot = htab_find_slot (table, &entry, INSERT);
23187 if (*slot)
23188 result = (struct strtab_entry *) *slot;
23189 else
23190 {
23191 result = XNEW (struct strtab_entry);
23192 result->offset = obstack_object_size (cpool);
23193 result->str = str;
23194 obstack_grow_str0 (cpool, str);
23195 *slot = result;
23196 }
23197 return result->offset;
23198 }
23199
23200 /* An entry in the symbol table. */
23201 struct symtab_index_entry
23202 {
23203 /* The name of the symbol. */
23204 const char *name;
23205 /* The offset of the name in the constant pool. */
23206 offset_type index_offset;
23207 /* A sorted vector of the indices of all the CUs that hold an object
23208 of this name. */
23209 VEC (offset_type) *cu_indices;
23210 };
23211
23212 /* The symbol table. This is a power-of-2-sized hash table. */
23213 struct mapped_symtab
23214 {
23215 offset_type n_elements;
23216 offset_type size;
23217 struct symtab_index_entry **data;
23218 };
23219
23220 /* Hash function for a symtab_index_entry. */
23221
23222 static hashval_t
23223 hash_symtab_entry (const void *e)
23224 {
23225 const struct symtab_index_entry *entry
23226 = (const struct symtab_index_entry *) e;
23227 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
23228 sizeof (offset_type) * VEC_length (offset_type,
23229 entry->cu_indices),
23230 0);
23231 }
23232
23233 /* Equality function for a symtab_index_entry. */
23234
23235 static int
23236 eq_symtab_entry (const void *a, const void *b)
23237 {
23238 const struct symtab_index_entry *ea = (const struct symtab_index_entry *) a;
23239 const struct symtab_index_entry *eb = (const struct symtab_index_entry *) b;
23240 int len = VEC_length (offset_type, ea->cu_indices);
23241 if (len != VEC_length (offset_type, eb->cu_indices))
23242 return 0;
23243 return !memcmp (VEC_address (offset_type, ea->cu_indices),
23244 VEC_address (offset_type, eb->cu_indices),
23245 sizeof (offset_type) * len);
23246 }
23247
23248 /* Destroy a symtab_index_entry. */
23249
23250 static void
23251 delete_symtab_entry (void *p)
23252 {
23253 struct symtab_index_entry *entry = (struct symtab_index_entry *) p;
23254 VEC_free (offset_type, entry->cu_indices);
23255 xfree (entry);
23256 }
23257
23258 /* Create a hash table holding symtab_index_entry objects. */
23259
23260 static htab_t
23261 create_symbol_hash_table (void)
23262 {
23263 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
23264 delete_symtab_entry, xcalloc, xfree);
23265 }
23266
23267 /* Create a new mapped symtab object. */
23268
23269 static struct mapped_symtab *
23270 create_mapped_symtab (void)
23271 {
23272 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
23273 symtab->n_elements = 0;
23274 symtab->size = 1024;
23275 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
23276 return symtab;
23277 }
23278
23279 /* Destroy a mapped_symtab. */
23280
23281 static void
23282 cleanup_mapped_symtab (void *p)
23283 {
23284 struct mapped_symtab *symtab = (struct mapped_symtab *) p;
23285 /* The contents of the array are freed when the other hash table is
23286 destroyed. */
23287 xfree (symtab->data);
23288 xfree (symtab);
23289 }
23290
23291 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
23292 the slot.
23293
23294 Function is used only during write_hash_table so no index format backward
23295 compatibility is needed. */
23296
23297 static struct symtab_index_entry **
23298 find_slot (struct mapped_symtab *symtab, const char *name)
23299 {
23300 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
23301
23302 index = hash & (symtab->size - 1);
23303 step = ((hash * 17) & (symtab->size - 1)) | 1;
23304
23305 for (;;)
23306 {
23307 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
23308 return &symtab->data[index];
23309 index = (index + step) & (symtab->size - 1);
23310 }
23311 }
23312
23313 /* Expand SYMTAB's hash table. */
23314
23315 static void
23316 hash_expand (struct mapped_symtab *symtab)
23317 {
23318 offset_type old_size = symtab->size;
23319 offset_type i;
23320 struct symtab_index_entry **old_entries = symtab->data;
23321
23322 symtab->size *= 2;
23323 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
23324
23325 for (i = 0; i < old_size; ++i)
23326 {
23327 if (old_entries[i])
23328 {
23329 struct symtab_index_entry **slot = find_slot (symtab,
23330 old_entries[i]->name);
23331 *slot = old_entries[i];
23332 }
23333 }
23334
23335 xfree (old_entries);
23336 }
23337
23338 /* Add an entry to SYMTAB. NAME is the name of the symbol.
23339 CU_INDEX is the index of the CU in which the symbol appears.
23340 IS_STATIC is one if the symbol is static, otherwise zero (global). */
23341
23342 static void
23343 add_index_entry (struct mapped_symtab *symtab, const char *name,
23344 int is_static, gdb_index_symbol_kind kind,
23345 offset_type cu_index)
23346 {
23347 struct symtab_index_entry **slot;
23348 offset_type cu_index_and_attrs;
23349
23350 ++symtab->n_elements;
23351 if (4 * symtab->n_elements / 3 >= symtab->size)
23352 hash_expand (symtab);
23353
23354 slot = find_slot (symtab, name);
23355 if (!*slot)
23356 {
23357 *slot = XNEW (struct symtab_index_entry);
23358 (*slot)->name = name;
23359 /* index_offset is set later. */
23360 (*slot)->cu_indices = NULL;
23361 }
23362
23363 cu_index_and_attrs = 0;
23364 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
23365 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
23366 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
23367
23368 /* We don't want to record an index value twice as we want to avoid the
23369 duplication.
23370 We process all global symbols and then all static symbols
23371 (which would allow us to avoid the duplication by only having to check
23372 the last entry pushed), but a symbol could have multiple kinds in one CU.
23373 To keep things simple we don't worry about the duplication here and
23374 sort and uniqufy the list after we've processed all symbols. */
23375 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
23376 }
23377
23378 /* qsort helper routine for uniquify_cu_indices. */
23379
23380 static int
23381 offset_type_compare (const void *ap, const void *bp)
23382 {
23383 offset_type a = *(offset_type *) ap;
23384 offset_type b = *(offset_type *) bp;
23385
23386 return (a > b) - (b > a);
23387 }
23388
23389 /* Sort and remove duplicates of all symbols' cu_indices lists. */
23390
23391 static void
23392 uniquify_cu_indices (struct mapped_symtab *symtab)
23393 {
23394 int i;
23395
23396 for (i = 0; i < symtab->size; ++i)
23397 {
23398 struct symtab_index_entry *entry = symtab->data[i];
23399
23400 if (entry
23401 && entry->cu_indices != NULL)
23402 {
23403 unsigned int next_to_insert, next_to_check;
23404 offset_type last_value;
23405
23406 qsort (VEC_address (offset_type, entry->cu_indices),
23407 VEC_length (offset_type, entry->cu_indices),
23408 sizeof (offset_type), offset_type_compare);
23409
23410 last_value = VEC_index (offset_type, entry->cu_indices, 0);
23411 next_to_insert = 1;
23412 for (next_to_check = 1;
23413 next_to_check < VEC_length (offset_type, entry->cu_indices);
23414 ++next_to_check)
23415 {
23416 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
23417 != last_value)
23418 {
23419 last_value = VEC_index (offset_type, entry->cu_indices,
23420 next_to_check);
23421 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
23422 last_value);
23423 ++next_to_insert;
23424 }
23425 }
23426 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
23427 }
23428 }
23429 }
23430
23431 /* Add a vector of indices to the constant pool. */
23432
23433 static offset_type
23434 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
23435 struct symtab_index_entry *entry)
23436 {
23437 void **slot;
23438
23439 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
23440 if (!*slot)
23441 {
23442 offset_type len = VEC_length (offset_type, entry->cu_indices);
23443 offset_type val = MAYBE_SWAP (len);
23444 offset_type iter;
23445 int i;
23446
23447 *slot = entry;
23448 entry->index_offset = obstack_object_size (cpool);
23449
23450 obstack_grow (cpool, &val, sizeof (val));
23451 for (i = 0;
23452 VEC_iterate (offset_type, entry->cu_indices, i, iter);
23453 ++i)
23454 {
23455 val = MAYBE_SWAP (iter);
23456 obstack_grow (cpool, &val, sizeof (val));
23457 }
23458 }
23459 else
23460 {
23461 struct symtab_index_entry *old_entry
23462 = (struct symtab_index_entry *) *slot;
23463 entry->index_offset = old_entry->index_offset;
23464 entry = old_entry;
23465 }
23466 return entry->index_offset;
23467 }
23468
23469 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
23470 constant pool entries going into the obstack CPOOL. */
23471
23472 static void
23473 write_hash_table (struct mapped_symtab *symtab,
23474 struct obstack *output, struct obstack *cpool)
23475 {
23476 offset_type i;
23477 htab_t symbol_hash_table;
23478 htab_t str_table;
23479
23480 symbol_hash_table = create_symbol_hash_table ();
23481 str_table = create_strtab ();
23482
23483 /* We add all the index vectors to the constant pool first, to
23484 ensure alignment is ok. */
23485 for (i = 0; i < symtab->size; ++i)
23486 {
23487 if (symtab->data[i])
23488 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
23489 }
23490
23491 /* Now write out the hash table. */
23492 for (i = 0; i < symtab->size; ++i)
23493 {
23494 offset_type str_off, vec_off;
23495
23496 if (symtab->data[i])
23497 {
23498 str_off = add_string (str_table, cpool, symtab->data[i]->name);
23499 vec_off = symtab->data[i]->index_offset;
23500 }
23501 else
23502 {
23503 /* While 0 is a valid constant pool index, it is not valid
23504 to have 0 for both offsets. */
23505 str_off = 0;
23506 vec_off = 0;
23507 }
23508
23509 str_off = MAYBE_SWAP (str_off);
23510 vec_off = MAYBE_SWAP (vec_off);
23511
23512 obstack_grow (output, &str_off, sizeof (str_off));
23513 obstack_grow (output, &vec_off, sizeof (vec_off));
23514 }
23515
23516 htab_delete (str_table);
23517 htab_delete (symbol_hash_table);
23518 }
23519
23520 /* Struct to map psymtab to CU index in the index file. */
23521 struct psymtab_cu_index_map
23522 {
23523 struct partial_symtab *psymtab;
23524 unsigned int cu_index;
23525 };
23526
23527 static hashval_t
23528 hash_psymtab_cu_index (const void *item)
23529 {
23530 const struct psymtab_cu_index_map *map
23531 = (const struct psymtab_cu_index_map *) item;
23532
23533 return htab_hash_pointer (map->psymtab);
23534 }
23535
23536 static int
23537 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
23538 {
23539 const struct psymtab_cu_index_map *lhs
23540 = (const struct psymtab_cu_index_map *) item_lhs;
23541 const struct psymtab_cu_index_map *rhs
23542 = (const struct psymtab_cu_index_map *) item_rhs;
23543
23544 return lhs->psymtab == rhs->psymtab;
23545 }
23546
23547 /* Helper struct for building the address table. */
23548 struct addrmap_index_data
23549 {
23550 struct objfile *objfile;
23551 struct obstack *addr_obstack;
23552 htab_t cu_index_htab;
23553
23554 /* Non-zero if the previous_* fields are valid.
23555 We can't write an entry until we see the next entry (since it is only then
23556 that we know the end of the entry). */
23557 int previous_valid;
23558 /* Index of the CU in the table of all CUs in the index file. */
23559 unsigned int previous_cu_index;
23560 /* Start address of the CU. */
23561 CORE_ADDR previous_cu_start;
23562 };
23563
23564 /* Write an address entry to OBSTACK. */
23565
23566 static void
23567 add_address_entry (struct objfile *objfile, struct obstack *obstack,
23568 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
23569 {
23570 offset_type cu_index_to_write;
23571 gdb_byte addr[8];
23572 CORE_ADDR baseaddr;
23573
23574 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23575
23576 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
23577 obstack_grow (obstack, addr, 8);
23578 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
23579 obstack_grow (obstack, addr, 8);
23580 cu_index_to_write = MAYBE_SWAP (cu_index);
23581 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
23582 }
23583
23584 /* Worker function for traversing an addrmap to build the address table. */
23585
23586 static int
23587 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
23588 {
23589 struct addrmap_index_data *data = (struct addrmap_index_data *) datap;
23590 struct partial_symtab *pst = (struct partial_symtab *) obj;
23591
23592 if (data->previous_valid)
23593 add_address_entry (data->objfile, data->addr_obstack,
23594 data->previous_cu_start, start_addr,
23595 data->previous_cu_index);
23596
23597 data->previous_cu_start = start_addr;
23598 if (pst != NULL)
23599 {
23600 struct psymtab_cu_index_map find_map, *map;
23601 find_map.psymtab = pst;
23602 map = ((struct psymtab_cu_index_map *)
23603 htab_find (data->cu_index_htab, &find_map));
23604 gdb_assert (map != NULL);
23605 data->previous_cu_index = map->cu_index;
23606 data->previous_valid = 1;
23607 }
23608 else
23609 data->previous_valid = 0;
23610
23611 return 0;
23612 }
23613
23614 /* Write OBJFILE's address map to OBSTACK.
23615 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
23616 in the index file. */
23617
23618 static void
23619 write_address_map (struct objfile *objfile, struct obstack *obstack,
23620 htab_t cu_index_htab)
23621 {
23622 struct addrmap_index_data addrmap_index_data;
23623
23624 /* When writing the address table, we have to cope with the fact that
23625 the addrmap iterator only provides the start of a region; we have to
23626 wait until the next invocation to get the start of the next region. */
23627
23628 addrmap_index_data.objfile = objfile;
23629 addrmap_index_data.addr_obstack = obstack;
23630 addrmap_index_data.cu_index_htab = cu_index_htab;
23631 addrmap_index_data.previous_valid = 0;
23632
23633 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
23634 &addrmap_index_data);
23635
23636 /* It's highly unlikely the last entry (end address = 0xff...ff)
23637 is valid, but we should still handle it.
23638 The end address is recorded as the start of the next region, but that
23639 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
23640 anyway. */
23641 if (addrmap_index_data.previous_valid)
23642 add_address_entry (objfile, obstack,
23643 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
23644 addrmap_index_data.previous_cu_index);
23645 }
23646
23647 /* Return the symbol kind of PSYM. */
23648
23649 static gdb_index_symbol_kind
23650 symbol_kind (struct partial_symbol *psym)
23651 {
23652 domain_enum domain = PSYMBOL_DOMAIN (psym);
23653 enum address_class aclass = PSYMBOL_CLASS (psym);
23654
23655 switch (domain)
23656 {
23657 case VAR_DOMAIN:
23658 switch (aclass)
23659 {
23660 case LOC_BLOCK:
23661 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
23662 case LOC_TYPEDEF:
23663 return GDB_INDEX_SYMBOL_KIND_TYPE;
23664 case LOC_COMPUTED:
23665 case LOC_CONST_BYTES:
23666 case LOC_OPTIMIZED_OUT:
23667 case LOC_STATIC:
23668 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23669 case LOC_CONST:
23670 /* Note: It's currently impossible to recognize psyms as enum values
23671 short of reading the type info. For now punt. */
23672 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
23673 default:
23674 /* There are other LOC_FOO values that one might want to classify
23675 as variables, but dwarf2read.c doesn't currently use them. */
23676 return GDB_INDEX_SYMBOL_KIND_OTHER;
23677 }
23678 case STRUCT_DOMAIN:
23679 return GDB_INDEX_SYMBOL_KIND_TYPE;
23680 default:
23681 return GDB_INDEX_SYMBOL_KIND_OTHER;
23682 }
23683 }
23684
23685 /* Add a list of partial symbols to SYMTAB. */
23686
23687 static void
23688 write_psymbols (struct mapped_symtab *symtab,
23689 htab_t psyms_seen,
23690 struct partial_symbol **psymp,
23691 int count,
23692 offset_type cu_index,
23693 int is_static)
23694 {
23695 for (; count-- > 0; ++psymp)
23696 {
23697 struct partial_symbol *psym = *psymp;
23698 void **slot;
23699
23700 if (SYMBOL_LANGUAGE (psym) == language_ada)
23701 error (_("Ada is not currently supported by the index"));
23702
23703 /* Only add a given psymbol once. */
23704 slot = htab_find_slot (psyms_seen, psym, INSERT);
23705 if (!*slot)
23706 {
23707 gdb_index_symbol_kind kind = symbol_kind (psym);
23708
23709 *slot = psym;
23710 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
23711 is_static, kind, cu_index);
23712 }
23713 }
23714 }
23715
23716 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
23717 exception if there is an error. */
23718
23719 static void
23720 write_obstack (FILE *file, struct obstack *obstack)
23721 {
23722 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
23723 file)
23724 != obstack_object_size (obstack))
23725 error (_("couldn't data write to file"));
23726 }
23727
23728 /* A helper struct used when iterating over debug_types. */
23729 struct signatured_type_index_data
23730 {
23731 struct objfile *objfile;
23732 struct mapped_symtab *symtab;
23733 struct obstack *types_list;
23734 htab_t psyms_seen;
23735 int cu_index;
23736 };
23737
23738 /* A helper function that writes a single signatured_type to an
23739 obstack. */
23740
23741 static int
23742 write_one_signatured_type (void **slot, void *d)
23743 {
23744 struct signatured_type_index_data *info
23745 = (struct signatured_type_index_data *) d;
23746 struct signatured_type *entry = (struct signatured_type *) *slot;
23747 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
23748 gdb_byte val[8];
23749
23750 write_psymbols (info->symtab,
23751 info->psyms_seen,
23752 info->objfile->global_psymbols.list
23753 + psymtab->globals_offset,
23754 psymtab->n_global_syms, info->cu_index,
23755 0);
23756 write_psymbols (info->symtab,
23757 info->psyms_seen,
23758 info->objfile->static_psymbols.list
23759 + psymtab->statics_offset,
23760 psymtab->n_static_syms, info->cu_index,
23761 1);
23762
23763 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23764 entry->per_cu.offset.sect_off);
23765 obstack_grow (info->types_list, val, 8);
23766 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23767 entry->type_offset_in_tu.cu_off);
23768 obstack_grow (info->types_list, val, 8);
23769 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
23770 obstack_grow (info->types_list, val, 8);
23771
23772 ++info->cu_index;
23773
23774 return 1;
23775 }
23776
23777 /* Recurse into all "included" dependencies and write their symbols as
23778 if they appeared in this psymtab. */
23779
23780 static void
23781 recursively_write_psymbols (struct objfile *objfile,
23782 struct partial_symtab *psymtab,
23783 struct mapped_symtab *symtab,
23784 htab_t psyms_seen,
23785 offset_type cu_index)
23786 {
23787 int i;
23788
23789 for (i = 0; i < psymtab->number_of_dependencies; ++i)
23790 if (psymtab->dependencies[i]->user != NULL)
23791 recursively_write_psymbols (objfile, psymtab->dependencies[i],
23792 symtab, psyms_seen, cu_index);
23793
23794 write_psymbols (symtab,
23795 psyms_seen,
23796 objfile->global_psymbols.list + psymtab->globals_offset,
23797 psymtab->n_global_syms, cu_index,
23798 0);
23799 write_psymbols (symtab,
23800 psyms_seen,
23801 objfile->static_psymbols.list + psymtab->statics_offset,
23802 psymtab->n_static_syms, cu_index,
23803 1);
23804 }
23805
23806 /* Create an index file for OBJFILE in the directory DIR. */
23807
23808 static void
23809 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
23810 {
23811 struct cleanup *cleanup;
23812 char *filename;
23813 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
23814 struct obstack cu_list, types_cu_list;
23815 int i;
23816 FILE *out_file;
23817 struct mapped_symtab *symtab;
23818 offset_type val, size_of_contents, total_len;
23819 struct stat st;
23820 struct psymtab_cu_index_map *psymtab_cu_index_map;
23821
23822 if (dwarf2_per_objfile->using_index)
23823 error (_("Cannot use an index to create the index"));
23824
23825 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
23826 error (_("Cannot make an index when the file has multiple .debug_types sections"));
23827
23828 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
23829 return;
23830
23831 if (stat (objfile_name (objfile), &st) < 0)
23832 perror_with_name (objfile_name (objfile));
23833
23834 filename = concat (dir, SLASH_STRING, lbasename (objfile_name (objfile)),
23835 INDEX_SUFFIX, (char *) NULL);
23836 cleanup = make_cleanup (xfree, filename);
23837
23838 out_file = gdb_fopen_cloexec (filename, "wb");
23839 if (!out_file)
23840 error (_("Can't open `%s' for writing"), filename);
23841
23842 gdb::unlinker unlink_file (filename);
23843
23844 symtab = create_mapped_symtab ();
23845 make_cleanup (cleanup_mapped_symtab, symtab);
23846
23847 obstack_init (&addr_obstack);
23848 make_cleanup_obstack_free (&addr_obstack);
23849
23850 obstack_init (&cu_list);
23851 make_cleanup_obstack_free (&cu_list);
23852
23853 obstack_init (&types_cu_list);
23854 make_cleanup_obstack_free (&types_cu_list);
23855
23856 htab_up psyms_seen (htab_create_alloc (100, htab_hash_pointer,
23857 htab_eq_pointer,
23858 NULL, xcalloc, xfree));
23859
23860 /* While we're scanning CU's create a table that maps a psymtab pointer
23861 (which is what addrmap records) to its index (which is what is recorded
23862 in the index file). This will later be needed to write the address
23863 table. */
23864 htab_up cu_index_htab (htab_create_alloc (100,
23865 hash_psymtab_cu_index,
23866 eq_psymtab_cu_index,
23867 NULL, xcalloc, xfree));
23868 psymtab_cu_index_map = XNEWVEC (struct psymtab_cu_index_map,
23869 dwarf2_per_objfile->n_comp_units);
23870 make_cleanup (xfree, psymtab_cu_index_map);
23871
23872 /* The CU list is already sorted, so we don't need to do additional
23873 work here. Also, the debug_types entries do not appear in
23874 all_comp_units, but only in their own hash table. */
23875 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
23876 {
23877 struct dwarf2_per_cu_data *per_cu
23878 = dwarf2_per_objfile->all_comp_units[i];
23879 struct partial_symtab *psymtab = per_cu->v.psymtab;
23880 gdb_byte val[8];
23881 struct psymtab_cu_index_map *map;
23882 void **slot;
23883
23884 /* CU of a shared file from 'dwz -m' may be unused by this main file.
23885 It may be referenced from a local scope but in such case it does not
23886 need to be present in .gdb_index. */
23887 if (psymtab == NULL)
23888 continue;
23889
23890 if (psymtab->user == NULL)
23891 recursively_write_psymbols (objfile, psymtab, symtab,
23892 psyms_seen.get (), i);
23893
23894 map = &psymtab_cu_index_map[i];
23895 map->psymtab = psymtab;
23896 map->cu_index = i;
23897 slot = htab_find_slot (cu_index_htab.get (), map, INSERT);
23898 gdb_assert (slot != NULL);
23899 gdb_assert (*slot == NULL);
23900 *slot = map;
23901
23902 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
23903 per_cu->offset.sect_off);
23904 obstack_grow (&cu_list, val, 8);
23905 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
23906 obstack_grow (&cu_list, val, 8);
23907 }
23908
23909 /* Dump the address map. */
23910 write_address_map (objfile, &addr_obstack, cu_index_htab.get ());
23911
23912 /* Write out the .debug_type entries, if any. */
23913 if (dwarf2_per_objfile->signatured_types)
23914 {
23915 struct signatured_type_index_data sig_data;
23916
23917 sig_data.objfile = objfile;
23918 sig_data.symtab = symtab;
23919 sig_data.types_list = &types_cu_list;
23920 sig_data.psyms_seen = psyms_seen.get ();
23921 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
23922 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
23923 write_one_signatured_type, &sig_data);
23924 }
23925
23926 /* Now that we've processed all symbols we can shrink their cu_indices
23927 lists. */
23928 uniquify_cu_indices (symtab);
23929
23930 obstack_init (&constant_pool);
23931 make_cleanup_obstack_free (&constant_pool);
23932 obstack_init (&symtab_obstack);
23933 make_cleanup_obstack_free (&symtab_obstack);
23934 write_hash_table (symtab, &symtab_obstack, &constant_pool);
23935
23936 obstack_init (&contents);
23937 make_cleanup_obstack_free (&contents);
23938 size_of_contents = 6 * sizeof (offset_type);
23939 total_len = size_of_contents;
23940
23941 /* The version number. */
23942 val = MAYBE_SWAP (8);
23943 obstack_grow (&contents, &val, sizeof (val));
23944
23945 /* The offset of the CU list from the start of the file. */
23946 val = MAYBE_SWAP (total_len);
23947 obstack_grow (&contents, &val, sizeof (val));
23948 total_len += obstack_object_size (&cu_list);
23949
23950 /* The offset of the types CU list from the start of the file. */
23951 val = MAYBE_SWAP (total_len);
23952 obstack_grow (&contents, &val, sizeof (val));
23953 total_len += obstack_object_size (&types_cu_list);
23954
23955 /* The offset of the address table from the start of the file. */
23956 val = MAYBE_SWAP (total_len);
23957 obstack_grow (&contents, &val, sizeof (val));
23958 total_len += obstack_object_size (&addr_obstack);
23959
23960 /* The offset of the symbol table from the start of the file. */
23961 val = MAYBE_SWAP (total_len);
23962 obstack_grow (&contents, &val, sizeof (val));
23963 total_len += obstack_object_size (&symtab_obstack);
23964
23965 /* The offset of the constant pool from the start of the file. */
23966 val = MAYBE_SWAP (total_len);
23967 obstack_grow (&contents, &val, sizeof (val));
23968 total_len += obstack_object_size (&constant_pool);
23969
23970 gdb_assert (obstack_object_size (&contents) == size_of_contents);
23971
23972 write_obstack (out_file, &contents);
23973 write_obstack (out_file, &cu_list);
23974 write_obstack (out_file, &types_cu_list);
23975 write_obstack (out_file, &addr_obstack);
23976 write_obstack (out_file, &symtab_obstack);
23977 write_obstack (out_file, &constant_pool);
23978
23979 fclose (out_file);
23980
23981 /* We want to keep the file. */
23982 unlink_file.keep ();
23983
23984 do_cleanups (cleanup);
23985 }
23986
23987 /* Implementation of the `save gdb-index' command.
23988
23989 Note that the file format used by this command is documented in the
23990 GDB manual. Any changes here must be documented there. */
23991
23992 static void
23993 save_gdb_index_command (char *arg, int from_tty)
23994 {
23995 struct objfile *objfile;
23996
23997 if (!arg || !*arg)
23998 error (_("usage: save gdb-index DIRECTORY"));
23999
24000 ALL_OBJFILES (objfile)
24001 {
24002 struct stat st;
24003
24004 /* If the objfile does not correspond to an actual file, skip it. */
24005 if (stat (objfile_name (objfile), &st) < 0)
24006 continue;
24007
24008 dwarf2_per_objfile
24009 = (struct dwarf2_per_objfile *) objfile_data (objfile,
24010 dwarf2_objfile_data_key);
24011 if (dwarf2_per_objfile)
24012 {
24013
24014 TRY
24015 {
24016 write_psymtabs_to_index (objfile, arg);
24017 }
24018 CATCH (except, RETURN_MASK_ERROR)
24019 {
24020 exception_fprintf (gdb_stderr, except,
24021 _("Error while writing index for `%s': "),
24022 objfile_name (objfile));
24023 }
24024 END_CATCH
24025 }
24026 }
24027 }
24028
24029 \f
24030
24031 int dwarf_always_disassemble;
24032
24033 static void
24034 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
24035 struct cmd_list_element *c, const char *value)
24036 {
24037 fprintf_filtered (file,
24038 _("Whether to always disassemble "
24039 "DWARF expressions is %s.\n"),
24040 value);
24041 }
24042
24043 static void
24044 show_check_physname (struct ui_file *file, int from_tty,
24045 struct cmd_list_element *c, const char *value)
24046 {
24047 fprintf_filtered (file,
24048 _("Whether to check \"physname\" is %s.\n"),
24049 value);
24050 }
24051
24052 void _initialize_dwarf2_read (void);
24053
24054 void
24055 _initialize_dwarf2_read (void)
24056 {
24057 struct cmd_list_element *c;
24058
24059 dwarf2_objfile_data_key
24060 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
24061
24062 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24063 Set DWARF specific variables.\n\
24064 Configure DWARF variables such as the cache size"),
24065 &set_dwarf_cmdlist, "maintenance set dwarf ",
24066 0/*allow-unknown*/, &maintenance_set_cmdlist);
24067
24068 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24069 Show DWARF specific variables\n\
24070 Show DWARF variables such as the cache size"),
24071 &show_dwarf_cmdlist, "maintenance show dwarf ",
24072 0/*allow-unknown*/, &maintenance_show_cmdlist);
24073
24074 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24075 &dwarf_max_cache_age, _("\
24076 Set the upper bound on the age of cached DWARF compilation units."), _("\
24077 Show the upper bound on the age of cached DWARF compilation units."), _("\
24078 A higher limit means that cached compilation units will be stored\n\
24079 in memory longer, and more total memory will be used. Zero disables\n\
24080 caching, which can slow down startup."),
24081 NULL,
24082 show_dwarf_max_cache_age,
24083 &set_dwarf_cmdlist,
24084 &show_dwarf_cmdlist);
24085
24086 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
24087 &dwarf_always_disassemble, _("\
24088 Set whether `info address' always disassembles DWARF expressions."), _("\
24089 Show whether `info address' always disassembles DWARF expressions."), _("\
24090 When enabled, DWARF expressions are always printed in an assembly-like\n\
24091 syntax. When disabled, expressions will be printed in a more\n\
24092 conversational style, when possible."),
24093 NULL,
24094 show_dwarf_always_disassemble,
24095 &set_dwarf_cmdlist,
24096 &show_dwarf_cmdlist);
24097
24098 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24099 Set debugging of the DWARF reader."), _("\
24100 Show debugging of the DWARF reader."), _("\
24101 When enabled (non-zero), debugging messages are printed during DWARF\n\
24102 reading and symtab expansion. A value of 1 (one) provides basic\n\
24103 information. A value greater than 1 provides more verbose information."),
24104 NULL,
24105 NULL,
24106 &setdebuglist, &showdebuglist);
24107
24108 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24109 Set debugging of the DWARF DIE reader."), _("\
24110 Show debugging of the DWARF DIE reader."), _("\
24111 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24112 The value is the maximum depth to print."),
24113 NULL,
24114 NULL,
24115 &setdebuglist, &showdebuglist);
24116
24117 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24118 Set debugging of the dwarf line reader."), _("\
24119 Show debugging of the dwarf line reader."), _("\
24120 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24121 A value of 1 (one) provides basic information.\n\
24122 A value greater than 1 provides more verbose information."),
24123 NULL,
24124 NULL,
24125 &setdebuglist, &showdebuglist);
24126
24127 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24128 Set cross-checking of \"physname\" code against demangler."), _("\
24129 Show cross-checking of \"physname\" code against demangler."), _("\
24130 When enabled, GDB's internal \"physname\" code is checked against\n\
24131 the demangler."),
24132 NULL, show_check_physname,
24133 &setdebuglist, &showdebuglist);
24134
24135 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24136 no_class, &use_deprecated_index_sections, _("\
24137 Set whether to use deprecated gdb_index sections."), _("\
24138 Show whether to use deprecated gdb_index sections."), _("\
24139 When enabled, deprecated .gdb_index sections are used anyway.\n\
24140 Normally they are ignored either because of a missing feature or\n\
24141 performance issue.\n\
24142 Warning: This option must be enabled before gdb reads the file."),
24143 NULL,
24144 NULL,
24145 &setlist, &showlist);
24146
24147 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
24148 _("\
24149 Save a gdb-index file.\n\
24150 Usage: save gdb-index DIRECTORY"),
24151 &save_cmdlist);
24152 set_cmd_completer (c, filename_completer);
24153
24154 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24155 &dwarf2_locexpr_funcs);
24156 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24157 &dwarf2_loclist_funcs);
24158
24159 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24160 &dwarf2_block_frame_base_locexpr_funcs);
24161 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24162 &dwarf2_block_frame_base_loclist_funcs);
24163 }