Remove some usages of get_dwarf2_per_objfile
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2018 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 "dwarf2read.h"
33 #include "dwarf-index-common.h"
34 #include "bfd.h"
35 #include "elf-bfd.h"
36 #include "symtab.h"
37 #include "gdbtypes.h"
38 #include "objfiles.h"
39 #include "dwarf2.h"
40 #include "buildsym.h"
41 #include "demangle.h"
42 #include "gdb-demangle.h"
43 #include "expression.h"
44 #include "filenames.h" /* for DOSish file names */
45 #include "macrotab.h"
46 #include "language.h"
47 #include "complaints.h"
48 #include "bcache.h"
49 #include "dwarf2expr.h"
50 #include "dwarf2loc.h"
51 #include "cp-support.h"
52 #include "hashtab.h"
53 #include "command.h"
54 #include "gdbcmd.h"
55 #include "block.h"
56 #include "addrmap.h"
57 #include "typeprint.h"
58 #include "psympriv.h"
59 #include <sys/stat.h>
60 #include "completer.h"
61 #include "vec.h"
62 #include "c-lang.h"
63 #include "go-lang.h"
64 #include "valprint.h"
65 #include "gdbcore.h" /* for gnutarget */
66 #include "gdb/gdb-index.h"
67 #include <ctype.h>
68 #include "gdb_bfd.h"
69 #include "f-lang.h"
70 #include "source.h"
71 #include "filestuff.h"
72 #include "build-id.h"
73 #include "namespace.h"
74 #include "common/gdb_unlinker.h"
75 #include "common/function-view.h"
76 #include "common/gdb_optional.h"
77 #include "common/underlying.h"
78 #include "common/byte-vector.h"
79 #include "common/hash_enum.h"
80 #include "filename-seen-cache.h"
81 #include "producer.h"
82 #include <fcntl.h>
83 #include <sys/types.h>
84 #include <algorithm>
85 #include <unordered_set>
86 #include <unordered_map>
87 #include "selftest.h"
88 #include <cmath>
89 #include <set>
90 #include <forward_list>
91 #include "rust-lang.h"
92 #include "common/pathstuff.h"
93
94 /* When == 1, print basic high level tracing messages.
95 When > 1, be more verbose.
96 This is in contrast to the low level DIE reading of dwarf_die_debug. */
97 static unsigned int dwarf_read_debug = 0;
98
99 /* When non-zero, dump DIEs after they are read in. */
100 static unsigned int dwarf_die_debug = 0;
101
102 /* When non-zero, dump line number entries as they are read in. */
103 static unsigned int dwarf_line_debug = 0;
104
105 /* When non-zero, cross-check physname against demangler. */
106 static int check_physname = 0;
107
108 /* When non-zero, do not reject deprecated .gdb_index sections. */
109 static int use_deprecated_index_sections = 0;
110
111 static const struct objfile_data *dwarf2_objfile_data_key;
112
113 /* The "aclass" indices for various kinds of computed DWARF symbols. */
114
115 static int dwarf2_locexpr_index;
116 static int dwarf2_loclist_index;
117 static int dwarf2_locexpr_block_index;
118 static int dwarf2_loclist_block_index;
119
120 /* An index into a (C++) symbol name component in a symbol name as
121 recorded in the mapped_index's symbol table. For each C++ symbol
122 in the symbol table, we record one entry for the start of each
123 component in the symbol in a table of name components, and then
124 sort the table, in order to be able to binary search symbol names,
125 ignoring leading namespaces, both completion and regular look up.
126 For example, for symbol "A::B::C", we'll have an entry that points
127 to "A::B::C", another that points to "B::C", and another for "C".
128 Note that function symbols in GDB index have no parameter
129 information, just the function/method names. You can convert a
130 name_component to a "const char *" using the
131 'mapped_index::symbol_name_at(offset_type)' method. */
132
133 struct name_component
134 {
135 /* Offset in the symbol name where the component starts. Stored as
136 a (32-bit) offset instead of a pointer to save memory and improve
137 locality on 64-bit architectures. */
138 offset_type name_offset;
139
140 /* The symbol's index in the symbol and constant pool tables of a
141 mapped_index. */
142 offset_type idx;
143 };
144
145 /* Base class containing bits shared by both .gdb_index and
146 .debug_name indexes. */
147
148 struct mapped_index_base
149 {
150 /* The name_component table (a sorted vector). See name_component's
151 description above. */
152 std::vector<name_component> name_components;
153
154 /* How NAME_COMPONENTS is sorted. */
155 enum case_sensitivity name_components_casing;
156
157 /* Return the number of names in the symbol table. */
158 virtual size_t symbol_name_count () const = 0;
159
160 /* Get the name of the symbol at IDX in the symbol table. */
161 virtual const char *symbol_name_at (offset_type idx) const = 0;
162
163 /* Return whether the name at IDX in the symbol table should be
164 ignored. */
165 virtual bool symbol_name_slot_invalid (offset_type idx) const
166 {
167 return false;
168 }
169
170 /* Build the symbol name component sorted vector, if we haven't
171 yet. */
172 void build_name_components ();
173
174 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
175 possible matches for LN_NO_PARAMS in the name component
176 vector. */
177 std::pair<std::vector<name_component>::const_iterator,
178 std::vector<name_component>::const_iterator>
179 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
180
181 /* Prevent deleting/destroying via a base class pointer. */
182 protected:
183 ~mapped_index_base() = default;
184 };
185
186 /* A description of the mapped index. The file format is described in
187 a comment by the code that writes the index. */
188 struct mapped_index final : public mapped_index_base
189 {
190 /* A slot/bucket in the symbol table hash. */
191 struct symbol_table_slot
192 {
193 const offset_type name;
194 const offset_type vec;
195 };
196
197 /* Index data format version. */
198 int version;
199
200 /* The total length of the buffer. */
201 off_t total_size;
202
203 /* The address table data. */
204 gdb::array_view<const gdb_byte> address_table;
205
206 /* The symbol table, implemented as a hash table. */
207 gdb::array_view<symbol_table_slot> symbol_table;
208
209 /* A pointer to the constant pool. */
210 const char *constant_pool;
211
212 bool symbol_name_slot_invalid (offset_type idx) const override
213 {
214 const auto &bucket = this->symbol_table[idx];
215 return bucket.name == 0 && bucket.vec;
216 }
217
218 /* Convenience method to get at the name of the symbol at IDX in the
219 symbol table. */
220 const char *symbol_name_at (offset_type idx) const override
221 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
222
223 size_t symbol_name_count () const override
224 { return this->symbol_table.size (); }
225 };
226
227 /* A description of the mapped .debug_names.
228 Uninitialized map has CU_COUNT 0. */
229 struct mapped_debug_names final : public mapped_index_base
230 {
231 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
232 : dwarf2_per_objfile (dwarf2_per_objfile_)
233 {}
234
235 struct dwarf2_per_objfile *dwarf2_per_objfile;
236 bfd_endian dwarf5_byte_order;
237 bool dwarf5_is_dwarf64;
238 bool augmentation_is_gdb;
239 uint8_t offset_size;
240 uint32_t cu_count = 0;
241 uint32_t tu_count, bucket_count, name_count;
242 const gdb_byte *cu_table_reordered, *tu_table_reordered;
243 const uint32_t *bucket_table_reordered, *hash_table_reordered;
244 const gdb_byte *name_table_string_offs_reordered;
245 const gdb_byte *name_table_entry_offs_reordered;
246 const gdb_byte *entry_pool;
247
248 struct index_val
249 {
250 ULONGEST dwarf_tag;
251 struct attr
252 {
253 /* Attribute name DW_IDX_*. */
254 ULONGEST dw_idx;
255
256 /* Attribute form DW_FORM_*. */
257 ULONGEST form;
258
259 /* Value if FORM is DW_FORM_implicit_const. */
260 LONGEST implicit_const;
261 };
262 std::vector<attr> attr_vec;
263 };
264
265 std::unordered_map<ULONGEST, index_val> abbrev_map;
266
267 const char *namei_to_name (uint32_t namei) const;
268
269 /* Implementation of the mapped_index_base virtual interface, for
270 the name_components cache. */
271
272 const char *symbol_name_at (offset_type idx) const override
273 { return namei_to_name (idx); }
274
275 size_t symbol_name_count () const override
276 { return this->name_count; }
277 };
278
279 /* See dwarf2read.h. */
280
281 dwarf2_per_objfile *
282 get_dwarf2_per_objfile (struct objfile *objfile)
283 {
284 return ((struct dwarf2_per_objfile *)
285 objfile_data (objfile, dwarf2_objfile_data_key));
286 }
287
288 /* Set the dwarf2_per_objfile associated to OBJFILE. */
289
290 void
291 set_dwarf2_per_objfile (struct objfile *objfile,
292 struct dwarf2_per_objfile *dwarf2_per_objfile)
293 {
294 gdb_assert (get_dwarf2_per_objfile (objfile) == NULL);
295 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
296 }
297
298 /* Default names of the debugging sections. */
299
300 /* Note that if the debugging section has been compressed, it might
301 have a name like .zdebug_info. */
302
303 static const struct dwarf2_debug_sections dwarf2_elf_names =
304 {
305 { ".debug_info", ".zdebug_info" },
306 { ".debug_abbrev", ".zdebug_abbrev" },
307 { ".debug_line", ".zdebug_line" },
308 { ".debug_loc", ".zdebug_loc" },
309 { ".debug_loclists", ".zdebug_loclists" },
310 { ".debug_macinfo", ".zdebug_macinfo" },
311 { ".debug_macro", ".zdebug_macro" },
312 { ".debug_str", ".zdebug_str" },
313 { ".debug_line_str", ".zdebug_line_str" },
314 { ".debug_ranges", ".zdebug_ranges" },
315 { ".debug_rnglists", ".zdebug_rnglists" },
316 { ".debug_types", ".zdebug_types" },
317 { ".debug_addr", ".zdebug_addr" },
318 { ".debug_frame", ".zdebug_frame" },
319 { ".eh_frame", NULL },
320 { ".gdb_index", ".zgdb_index" },
321 { ".debug_names", ".zdebug_names" },
322 { ".debug_aranges", ".zdebug_aranges" },
323 23
324 };
325
326 /* List of DWO/DWP sections. */
327
328 static const struct dwop_section_names
329 {
330 struct dwarf2_section_names abbrev_dwo;
331 struct dwarf2_section_names info_dwo;
332 struct dwarf2_section_names line_dwo;
333 struct dwarf2_section_names loc_dwo;
334 struct dwarf2_section_names loclists_dwo;
335 struct dwarf2_section_names macinfo_dwo;
336 struct dwarf2_section_names macro_dwo;
337 struct dwarf2_section_names str_dwo;
338 struct dwarf2_section_names str_offsets_dwo;
339 struct dwarf2_section_names types_dwo;
340 struct dwarf2_section_names cu_index;
341 struct dwarf2_section_names tu_index;
342 }
343 dwop_section_names =
344 {
345 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
346 { ".debug_info.dwo", ".zdebug_info.dwo" },
347 { ".debug_line.dwo", ".zdebug_line.dwo" },
348 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
349 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
350 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
351 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
352 { ".debug_str.dwo", ".zdebug_str.dwo" },
353 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
354 { ".debug_types.dwo", ".zdebug_types.dwo" },
355 { ".debug_cu_index", ".zdebug_cu_index" },
356 { ".debug_tu_index", ".zdebug_tu_index" },
357 };
358
359 /* local data types */
360
361 /* The data in a compilation unit header, after target2host
362 translation, looks like this. */
363 struct comp_unit_head
364 {
365 unsigned int length;
366 short version;
367 unsigned char addr_size;
368 unsigned char signed_addr_p;
369 sect_offset abbrev_sect_off;
370
371 /* Size of file offsets; either 4 or 8. */
372 unsigned int offset_size;
373
374 /* Size of the length field; either 4 or 12. */
375 unsigned int initial_length_size;
376
377 enum dwarf_unit_type unit_type;
378
379 /* Offset to the first byte of this compilation unit header in the
380 .debug_info section, for resolving relative reference dies. */
381 sect_offset sect_off;
382
383 /* Offset to first die in this cu from the start of the cu.
384 This will be the first byte following the compilation unit header. */
385 cu_offset first_die_cu_offset;
386
387 /* 64-bit signature of this type unit - it is valid only for
388 UNIT_TYPE DW_UT_type. */
389 ULONGEST signature;
390
391 /* For types, offset in the type's DIE of the type defined by this TU. */
392 cu_offset type_cu_offset_in_tu;
393 };
394
395 /* Type used for delaying computation of method physnames.
396 See comments for compute_delayed_physnames. */
397 struct delayed_method_info
398 {
399 /* The type to which the method is attached, i.e., its parent class. */
400 struct type *type;
401
402 /* The index of the method in the type's function fieldlists. */
403 int fnfield_index;
404
405 /* The index of the method in the fieldlist. */
406 int index;
407
408 /* The name of the DIE. */
409 const char *name;
410
411 /* The DIE associated with this method. */
412 struct die_info *die;
413 };
414
415 /* Internal state when decoding a particular compilation unit. */
416 struct dwarf2_cu
417 {
418 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
419 ~dwarf2_cu ();
420
421 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
422
423 /* The header of the compilation unit. */
424 struct comp_unit_head header {};
425
426 /* Base address of this compilation unit. */
427 CORE_ADDR base_address = 0;
428
429 /* Non-zero if base_address has been set. */
430 int base_known = 0;
431
432 /* The language we are debugging. */
433 enum language language = language_unknown;
434 const struct language_defn *language_defn = nullptr;
435
436 const char *producer = nullptr;
437
438 /* The generic symbol table building routines have separate lists for
439 file scope symbols and all all other scopes (local scopes). So
440 we need to select the right one to pass to add_symbol_to_list().
441 We do it by keeping a pointer to the correct list in list_in_scope.
442
443 FIXME: The original dwarf code just treated the file scope as the
444 first local scope, and all other local scopes as nested local
445 scopes, and worked fine. Check to see if we really need to
446 distinguish these in buildsym.c. */
447 struct pending **list_in_scope = nullptr;
448
449 /* Hash table holding all the loaded partial DIEs
450 with partial_die->offset.SECT_OFF as hash. */
451 htab_t partial_dies = nullptr;
452
453 /* Storage for things with the same lifetime as this read-in compilation
454 unit, including partial DIEs. */
455 auto_obstack comp_unit_obstack;
456
457 /* When multiple dwarf2_cu structures are living in memory, this field
458 chains them all together, so that they can be released efficiently.
459 We will probably also want a generation counter so that most-recently-used
460 compilation units are cached... */
461 struct dwarf2_per_cu_data *read_in_chain = nullptr;
462
463 /* Backlink to our per_cu entry. */
464 struct dwarf2_per_cu_data *per_cu;
465
466 /* How many compilation units ago was this CU last referenced? */
467 int last_used = 0;
468
469 /* A hash table of DIE cu_offset for following references with
470 die_info->offset.sect_off as hash. */
471 htab_t die_hash = nullptr;
472
473 /* Full DIEs if read in. */
474 struct die_info *dies = nullptr;
475
476 /* A set of pointers to dwarf2_per_cu_data objects for compilation
477 units referenced by this one. Only set during full symbol processing;
478 partial symbol tables do not have dependencies. */
479 htab_t dependencies = nullptr;
480
481 /* Header data from the line table, during full symbol processing. */
482 struct line_header *line_header = nullptr;
483 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
484 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
485 this is the DW_TAG_compile_unit die for this CU. We'll hold on
486 to the line header as long as this DIE is being processed. See
487 process_die_scope. */
488 die_info *line_header_die_owner = nullptr;
489
490 /* A list of methods which need to have physnames computed
491 after all type information has been read. */
492 std::vector<delayed_method_info> method_list;
493
494 /* To be copied to symtab->call_site_htab. */
495 htab_t call_site_htab = nullptr;
496
497 /* Non-NULL if this CU came from a DWO file.
498 There is an invariant here that is important to remember:
499 Except for attributes copied from the top level DIE in the "main"
500 (or "stub") file in preparation for reading the DWO file
501 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
502 Either there isn't a DWO file (in which case this is NULL and the point
503 is moot), or there is and either we're not going to read it (in which
504 case this is NULL) or there is and we are reading it (in which case this
505 is non-NULL). */
506 struct dwo_unit *dwo_unit = nullptr;
507
508 /* The DW_AT_addr_base attribute if present, zero otherwise
509 (zero is a valid value though).
510 Note this value comes from the Fission stub CU/TU's DIE. */
511 ULONGEST addr_base = 0;
512
513 /* The DW_AT_ranges_base attribute if present, zero otherwise
514 (zero is a valid value though).
515 Note this value comes from the Fission stub CU/TU's DIE.
516 Also note that the value is zero in the non-DWO case so this value can
517 be used without needing to know whether DWO files are in use or not.
518 N.B. This does not apply to DW_AT_ranges appearing in
519 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
520 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
521 DW_AT_ranges_base *would* have to be applied, and we'd have to care
522 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
523 ULONGEST ranges_base = 0;
524
525 /* When reading debug info generated by older versions of rustc, we
526 have to rewrite some union types to be struct types with a
527 variant part. This rewriting must be done after the CU is fully
528 read in, because otherwise at the point of rewriting some struct
529 type might not have been fully processed. So, we keep a list of
530 all such types here and process them after expansion. */
531 std::vector<struct type *> rust_unions;
532
533 /* Mark used when releasing cached dies. */
534 unsigned int mark : 1;
535
536 /* This CU references .debug_loc. See the symtab->locations_valid field.
537 This test is imperfect as there may exist optimized debug code not using
538 any location list and still facing inlining issues if handled as
539 unoptimized code. For a future better test see GCC PR other/32998. */
540 unsigned int has_loclist : 1;
541
542 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
543 if all the producer_is_* fields are valid. This information is cached
544 because profiling CU expansion showed excessive time spent in
545 producer_is_gxx_lt_4_6. */
546 unsigned int checked_producer : 1;
547 unsigned int producer_is_gxx_lt_4_6 : 1;
548 unsigned int producer_is_gcc_lt_4_3 : 1;
549 unsigned int producer_is_icc_lt_14 : 1;
550
551 /* When set, the file that we're processing is known to have
552 debugging info for C++ namespaces. GCC 3.3.x did not produce
553 this information, but later versions do. */
554
555 unsigned int processing_has_namespace_info : 1;
556
557 struct partial_die_info *find_partial_die (sect_offset sect_off);
558 };
559
560 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
561 This includes type_unit_group and quick_file_names. */
562
563 struct stmt_list_hash
564 {
565 /* The DWO unit this table is from or NULL if there is none. */
566 struct dwo_unit *dwo_unit;
567
568 /* Offset in .debug_line or .debug_line.dwo. */
569 sect_offset line_sect_off;
570 };
571
572 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
573 an object of this type. */
574
575 struct type_unit_group
576 {
577 /* dwarf2read.c's main "handle" on a TU symtab.
578 To simplify things we create an artificial CU that "includes" all the
579 type units using this stmt_list so that the rest of the code still has
580 a "per_cu" handle on the symtab.
581 This PER_CU is recognized by having no section. */
582 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
583 struct dwarf2_per_cu_data per_cu;
584
585 /* The TUs that share this DW_AT_stmt_list entry.
586 This is added to while parsing type units to build partial symtabs,
587 and is deleted afterwards and not used again. */
588 VEC (sig_type_ptr) *tus;
589
590 /* The compunit symtab.
591 Type units in a group needn't all be defined in the same source file,
592 so we create an essentially anonymous symtab as the compunit symtab. */
593 struct compunit_symtab *compunit_symtab;
594
595 /* The data used to construct the hash key. */
596 struct stmt_list_hash hash;
597
598 /* The number of symtabs from the line header.
599 The value here must match line_header.num_file_names. */
600 unsigned int num_symtabs;
601
602 /* The symbol tables for this TU (obtained from the files listed in
603 DW_AT_stmt_list).
604 WARNING: The order of entries here must match the order of entries
605 in the line header. After the first TU using this type_unit_group, the
606 line header for the subsequent TUs is recreated from this. This is done
607 because we need to use the same symtabs for each TU using the same
608 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
609 there's no guarantee the line header doesn't have duplicate entries. */
610 struct symtab **symtabs;
611 };
612
613 /* These sections are what may appear in a (real or virtual) DWO file. */
614
615 struct dwo_sections
616 {
617 struct dwarf2_section_info abbrev;
618 struct dwarf2_section_info line;
619 struct dwarf2_section_info loc;
620 struct dwarf2_section_info loclists;
621 struct dwarf2_section_info macinfo;
622 struct dwarf2_section_info macro;
623 struct dwarf2_section_info str;
624 struct dwarf2_section_info str_offsets;
625 /* In the case of a virtual DWO file, these two are unused. */
626 struct dwarf2_section_info info;
627 VEC (dwarf2_section_info_def) *types;
628 };
629
630 /* CUs/TUs in DWP/DWO files. */
631
632 struct dwo_unit
633 {
634 /* Backlink to the containing struct dwo_file. */
635 struct dwo_file *dwo_file;
636
637 /* The "id" that distinguishes this CU/TU.
638 .debug_info calls this "dwo_id", .debug_types calls this "signature".
639 Since signatures came first, we stick with it for consistency. */
640 ULONGEST signature;
641
642 /* The section this CU/TU lives in, in the DWO file. */
643 struct dwarf2_section_info *section;
644
645 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
646 sect_offset sect_off;
647 unsigned int length;
648
649 /* For types, offset in the type's DIE of the type defined by this TU. */
650 cu_offset type_offset_in_tu;
651 };
652
653 /* include/dwarf2.h defines the DWP section codes.
654 It defines a max value but it doesn't define a min value, which we
655 use for error checking, so provide one. */
656
657 enum dwp_v2_section_ids
658 {
659 DW_SECT_MIN = 1
660 };
661
662 /* Data for one DWO file.
663
664 This includes virtual DWO files (a virtual DWO file is a DWO file as it
665 appears in a DWP file). DWP files don't really have DWO files per se -
666 comdat folding of types "loses" the DWO file they came from, and from
667 a high level view DWP files appear to contain a mass of random types.
668 However, to maintain consistency with the non-DWP case we pretend DWP
669 files contain virtual DWO files, and we assign each TU with one virtual
670 DWO file (generally based on the line and abbrev section offsets -
671 a heuristic that seems to work in practice). */
672
673 struct dwo_file
674 {
675 /* The DW_AT_GNU_dwo_name attribute.
676 For virtual DWO files the name is constructed from the section offsets
677 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
678 from related CU+TUs. */
679 const char *dwo_name;
680
681 /* The DW_AT_comp_dir attribute. */
682 const char *comp_dir;
683
684 /* The bfd, when the file is open. Otherwise this is NULL.
685 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
686 bfd *dbfd;
687
688 /* The sections that make up this DWO file.
689 Remember that for virtual DWO files in DWP V2, these are virtual
690 sections (for lack of a better name). */
691 struct dwo_sections sections;
692
693 /* The CUs in the file.
694 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
695 an extension to handle LLVM's Link Time Optimization output (where
696 multiple source files may be compiled into a single object/dwo pair). */
697 htab_t cus;
698
699 /* Table of TUs in the file.
700 Each element is a struct dwo_unit. */
701 htab_t tus;
702 };
703
704 /* These sections are what may appear in a DWP file. */
705
706 struct dwp_sections
707 {
708 /* These are used by both DWP version 1 and 2. */
709 struct dwarf2_section_info str;
710 struct dwarf2_section_info cu_index;
711 struct dwarf2_section_info tu_index;
712
713 /* These are only used by DWP version 2 files.
714 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
715 sections are referenced by section number, and are not recorded here.
716 In DWP version 2 there is at most one copy of all these sections, each
717 section being (effectively) comprised of the concatenation of all of the
718 individual sections that exist in the version 1 format.
719 To keep the code simple we treat each of these concatenated pieces as a
720 section itself (a virtual section?). */
721 struct dwarf2_section_info abbrev;
722 struct dwarf2_section_info info;
723 struct dwarf2_section_info line;
724 struct dwarf2_section_info loc;
725 struct dwarf2_section_info macinfo;
726 struct dwarf2_section_info macro;
727 struct dwarf2_section_info str_offsets;
728 struct dwarf2_section_info types;
729 };
730
731 /* These sections are what may appear in a virtual DWO file in DWP version 1.
732 A virtual DWO file is a DWO file as it appears in a DWP file. */
733
734 struct virtual_v1_dwo_sections
735 {
736 struct dwarf2_section_info abbrev;
737 struct dwarf2_section_info line;
738 struct dwarf2_section_info loc;
739 struct dwarf2_section_info macinfo;
740 struct dwarf2_section_info macro;
741 struct dwarf2_section_info str_offsets;
742 /* Each DWP hash table entry records one CU or one TU.
743 That is recorded here, and copied to dwo_unit.section. */
744 struct dwarf2_section_info info_or_types;
745 };
746
747 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
748 In version 2, the sections of the DWO files are concatenated together
749 and stored in one section of that name. Thus each ELF section contains
750 several "virtual" sections. */
751
752 struct virtual_v2_dwo_sections
753 {
754 bfd_size_type abbrev_offset;
755 bfd_size_type abbrev_size;
756
757 bfd_size_type line_offset;
758 bfd_size_type line_size;
759
760 bfd_size_type loc_offset;
761 bfd_size_type loc_size;
762
763 bfd_size_type macinfo_offset;
764 bfd_size_type macinfo_size;
765
766 bfd_size_type macro_offset;
767 bfd_size_type macro_size;
768
769 bfd_size_type str_offsets_offset;
770 bfd_size_type str_offsets_size;
771
772 /* Each DWP hash table entry records one CU or one TU.
773 That is recorded here, and copied to dwo_unit.section. */
774 bfd_size_type info_or_types_offset;
775 bfd_size_type info_or_types_size;
776 };
777
778 /* Contents of DWP hash tables. */
779
780 struct dwp_hash_table
781 {
782 uint32_t version, nr_columns;
783 uint32_t nr_units, nr_slots;
784 const gdb_byte *hash_table, *unit_table;
785 union
786 {
787 struct
788 {
789 const gdb_byte *indices;
790 } v1;
791 struct
792 {
793 /* This is indexed by column number and gives the id of the section
794 in that column. */
795 #define MAX_NR_V2_DWO_SECTIONS \
796 (1 /* .debug_info or .debug_types */ \
797 + 1 /* .debug_abbrev */ \
798 + 1 /* .debug_line */ \
799 + 1 /* .debug_loc */ \
800 + 1 /* .debug_str_offsets */ \
801 + 1 /* .debug_macro or .debug_macinfo */)
802 int section_ids[MAX_NR_V2_DWO_SECTIONS];
803 const gdb_byte *offsets;
804 const gdb_byte *sizes;
805 } v2;
806 } section_pool;
807 };
808
809 /* Data for one DWP file. */
810
811 struct dwp_file
812 {
813 /* Name of the file. */
814 const char *name;
815
816 /* File format version. */
817 int version;
818
819 /* The bfd. */
820 bfd *dbfd;
821
822 /* Section info for this file. */
823 struct dwp_sections sections;
824
825 /* Table of CUs in the file. */
826 const struct dwp_hash_table *cus;
827
828 /* Table of TUs in the file. */
829 const struct dwp_hash_table *tus;
830
831 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
832 htab_t loaded_cus;
833 htab_t loaded_tus;
834
835 /* Table to map ELF section numbers to their sections.
836 This is only needed for the DWP V1 file format. */
837 unsigned int num_sections;
838 asection **elf_sections;
839 };
840
841 /* This represents a '.dwz' file. */
842
843 struct dwz_file
844 {
845 /* A dwz file can only contain a few sections. */
846 struct dwarf2_section_info abbrev;
847 struct dwarf2_section_info info;
848 struct dwarf2_section_info str;
849 struct dwarf2_section_info line;
850 struct dwarf2_section_info macro;
851 struct dwarf2_section_info gdb_index;
852 struct dwarf2_section_info debug_names;
853
854 /* The dwz's BFD. */
855 bfd *dwz_bfd;
856 };
857
858 /* Struct used to pass misc. parameters to read_die_and_children, et
859 al. which are used for both .debug_info and .debug_types dies.
860 All parameters here are unchanging for the life of the call. This
861 struct exists to abstract away the constant parameters of die reading. */
862
863 struct die_reader_specs
864 {
865 /* The bfd of die_section. */
866 bfd* abfd;
867
868 /* The CU of the DIE we are parsing. */
869 struct dwarf2_cu *cu;
870
871 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
872 struct dwo_file *dwo_file;
873
874 /* The section the die comes from.
875 This is either .debug_info or .debug_types, or the .dwo variants. */
876 struct dwarf2_section_info *die_section;
877
878 /* die_section->buffer. */
879 const gdb_byte *buffer;
880
881 /* The end of the buffer. */
882 const gdb_byte *buffer_end;
883
884 /* The value of the DW_AT_comp_dir attribute. */
885 const char *comp_dir;
886
887 /* The abbreviation table to use when reading the DIEs. */
888 struct abbrev_table *abbrev_table;
889 };
890
891 /* Type of function passed to init_cutu_and_read_dies, et.al. */
892 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
893 const gdb_byte *info_ptr,
894 struct die_info *comp_unit_die,
895 int has_children,
896 void *data);
897
898 /* A 1-based directory index. This is a strong typedef to prevent
899 accidentally using a directory index as a 0-based index into an
900 array/vector. */
901 enum class dir_index : unsigned int {};
902
903 /* Likewise, a 1-based file name index. */
904 enum class file_name_index : unsigned int {};
905
906 struct file_entry
907 {
908 file_entry () = default;
909
910 file_entry (const char *name_, dir_index d_index_,
911 unsigned int mod_time_, unsigned int length_)
912 : name (name_),
913 d_index (d_index_),
914 mod_time (mod_time_),
915 length (length_)
916 {}
917
918 /* Return the include directory at D_INDEX stored in LH. Returns
919 NULL if D_INDEX is out of bounds. */
920 const char *include_dir (const line_header *lh) const;
921
922 /* The file name. Note this is an observing pointer. The memory is
923 owned by debug_line_buffer. */
924 const char *name {};
925
926 /* The directory index (1-based). */
927 dir_index d_index {};
928
929 unsigned int mod_time {};
930
931 unsigned int length {};
932
933 /* True if referenced by the Line Number Program. */
934 bool included_p {};
935
936 /* The associated symbol table, if any. */
937 struct symtab *symtab {};
938 };
939
940 /* The line number information for a compilation unit (found in the
941 .debug_line section) begins with a "statement program header",
942 which contains the following information. */
943 struct line_header
944 {
945 line_header ()
946 : offset_in_dwz {}
947 {}
948
949 /* Add an entry to the include directory table. */
950 void add_include_dir (const char *include_dir);
951
952 /* Add an entry to the file name table. */
953 void add_file_name (const char *name, dir_index d_index,
954 unsigned int mod_time, unsigned int length);
955
956 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
957 is out of bounds. */
958 const char *include_dir_at (dir_index index) const
959 {
960 /* Convert directory index number (1-based) to vector index
961 (0-based). */
962 size_t vec_index = to_underlying (index) - 1;
963
964 if (vec_index >= include_dirs.size ())
965 return NULL;
966 return include_dirs[vec_index];
967 }
968
969 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
970 is out of bounds. */
971 file_entry *file_name_at (file_name_index index)
972 {
973 /* Convert file name index number (1-based) to vector index
974 (0-based). */
975 size_t vec_index = to_underlying (index) - 1;
976
977 if (vec_index >= file_names.size ())
978 return NULL;
979 return &file_names[vec_index];
980 }
981
982 /* Const version of the above. */
983 const file_entry *file_name_at (unsigned int index) const
984 {
985 if (index >= file_names.size ())
986 return NULL;
987 return &file_names[index];
988 }
989
990 /* Offset of line number information in .debug_line section. */
991 sect_offset sect_off {};
992
993 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
994 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
995
996 unsigned int total_length {};
997 unsigned short version {};
998 unsigned int header_length {};
999 unsigned char minimum_instruction_length {};
1000 unsigned char maximum_ops_per_instruction {};
1001 unsigned char default_is_stmt {};
1002 int line_base {};
1003 unsigned char line_range {};
1004 unsigned char opcode_base {};
1005
1006 /* standard_opcode_lengths[i] is the number of operands for the
1007 standard opcode whose value is i. This means that
1008 standard_opcode_lengths[0] is unused, and the last meaningful
1009 element is standard_opcode_lengths[opcode_base - 1]. */
1010 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1011
1012 /* The include_directories table. Note these are observing
1013 pointers. The memory is owned by debug_line_buffer. */
1014 std::vector<const char *> include_dirs;
1015
1016 /* The file_names table. */
1017 std::vector<file_entry> file_names;
1018
1019 /* The start and end of the statement program following this
1020 header. These point into dwarf2_per_objfile->line_buffer. */
1021 const gdb_byte *statement_program_start {}, *statement_program_end {};
1022 };
1023
1024 typedef std::unique_ptr<line_header> line_header_up;
1025
1026 const char *
1027 file_entry::include_dir (const line_header *lh) const
1028 {
1029 return lh->include_dir_at (d_index);
1030 }
1031
1032 /* When we construct a partial symbol table entry we only
1033 need this much information. */
1034 struct partial_die_info : public allocate_on_obstack
1035 {
1036 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1037
1038 /* Disable assign but still keep copy ctor, which is needed
1039 load_partial_dies. */
1040 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1041
1042 /* Adjust the partial die before generating a symbol for it. This
1043 function may set the is_external flag or change the DIE's
1044 name. */
1045 void fixup (struct dwarf2_cu *cu);
1046
1047 /* Read a minimal amount of information into the minimal die
1048 structure. */
1049 const gdb_byte *read (const struct die_reader_specs *reader,
1050 const struct abbrev_info &abbrev,
1051 const gdb_byte *info_ptr);
1052
1053 /* Offset of this DIE. */
1054 const sect_offset sect_off;
1055
1056 /* DWARF-2 tag for this DIE. */
1057 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1058
1059 /* Assorted flags describing the data found in this DIE. */
1060 const unsigned int has_children : 1;
1061
1062 unsigned int is_external : 1;
1063 unsigned int is_declaration : 1;
1064 unsigned int has_type : 1;
1065 unsigned int has_specification : 1;
1066 unsigned int has_pc_info : 1;
1067 unsigned int may_be_inlined : 1;
1068
1069 /* This DIE has been marked DW_AT_main_subprogram. */
1070 unsigned int main_subprogram : 1;
1071
1072 /* Flag set if the SCOPE field of this structure has been
1073 computed. */
1074 unsigned int scope_set : 1;
1075
1076 /* Flag set if the DIE has a byte_size attribute. */
1077 unsigned int has_byte_size : 1;
1078
1079 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1080 unsigned int has_const_value : 1;
1081
1082 /* Flag set if any of the DIE's children are template arguments. */
1083 unsigned int has_template_arguments : 1;
1084
1085 /* Flag set if fixup has been called on this die. */
1086 unsigned int fixup_called : 1;
1087
1088 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1089 unsigned int is_dwz : 1;
1090
1091 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1092 unsigned int spec_is_dwz : 1;
1093
1094 /* The name of this DIE. Normally the value of DW_AT_name, but
1095 sometimes a default name for unnamed DIEs. */
1096 const char *name = nullptr;
1097
1098 /* The linkage name, if present. */
1099 const char *linkage_name = nullptr;
1100
1101 /* The scope to prepend to our children. This is generally
1102 allocated on the comp_unit_obstack, so will disappear
1103 when this compilation unit leaves the cache. */
1104 const char *scope = nullptr;
1105
1106 /* Some data associated with the partial DIE. The tag determines
1107 which field is live. */
1108 union
1109 {
1110 /* The location description associated with this DIE, if any. */
1111 struct dwarf_block *locdesc;
1112 /* The offset of an import, for DW_TAG_imported_unit. */
1113 sect_offset sect_off;
1114 } d {};
1115
1116 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1117 CORE_ADDR lowpc = 0;
1118 CORE_ADDR highpc = 0;
1119
1120 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1121 DW_AT_sibling, if any. */
1122 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1123 could return DW_AT_sibling values to its caller load_partial_dies. */
1124 const gdb_byte *sibling = nullptr;
1125
1126 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1127 DW_AT_specification (or DW_AT_abstract_origin or
1128 DW_AT_extension). */
1129 sect_offset spec_offset {};
1130
1131 /* Pointers to this DIE's parent, first child, and next sibling,
1132 if any. */
1133 struct partial_die_info *die_parent = nullptr;
1134 struct partial_die_info *die_child = nullptr;
1135 struct partial_die_info *die_sibling = nullptr;
1136
1137 friend struct partial_die_info *
1138 dwarf2_cu::find_partial_die (sect_offset sect_off);
1139
1140 private:
1141 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1142 partial_die_info (sect_offset sect_off)
1143 : partial_die_info (sect_off, DW_TAG_padding, 0)
1144 {
1145 }
1146
1147 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1148 int has_children_)
1149 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1150 {
1151 is_external = 0;
1152 is_declaration = 0;
1153 has_type = 0;
1154 has_specification = 0;
1155 has_pc_info = 0;
1156 may_be_inlined = 0;
1157 main_subprogram = 0;
1158 scope_set = 0;
1159 has_byte_size = 0;
1160 has_const_value = 0;
1161 has_template_arguments = 0;
1162 fixup_called = 0;
1163 is_dwz = 0;
1164 spec_is_dwz = 0;
1165 }
1166 };
1167
1168 /* This data structure holds the information of an abbrev. */
1169 struct abbrev_info
1170 {
1171 unsigned int number; /* number identifying abbrev */
1172 enum dwarf_tag tag; /* dwarf tag */
1173 unsigned short has_children; /* boolean */
1174 unsigned short num_attrs; /* number of attributes */
1175 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1176 struct abbrev_info *next; /* next in chain */
1177 };
1178
1179 struct attr_abbrev
1180 {
1181 ENUM_BITFIELD(dwarf_attribute) name : 16;
1182 ENUM_BITFIELD(dwarf_form) form : 16;
1183
1184 /* It is valid only if FORM is DW_FORM_implicit_const. */
1185 LONGEST implicit_const;
1186 };
1187
1188 /* Size of abbrev_table.abbrev_hash_table. */
1189 #define ABBREV_HASH_SIZE 121
1190
1191 /* Top level data structure to contain an abbreviation table. */
1192
1193 struct abbrev_table
1194 {
1195 explicit abbrev_table (sect_offset off)
1196 : sect_off (off)
1197 {
1198 m_abbrevs =
1199 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1200 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1201 }
1202
1203 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1204
1205 /* Allocate space for a struct abbrev_info object in
1206 ABBREV_TABLE. */
1207 struct abbrev_info *alloc_abbrev ();
1208
1209 /* Add an abbreviation to the table. */
1210 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1211
1212 /* Look up an abbrev in the table.
1213 Returns NULL if the abbrev is not found. */
1214
1215 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1216
1217
1218 /* Where the abbrev table came from.
1219 This is used as a sanity check when the table is used. */
1220 const sect_offset sect_off;
1221
1222 /* Storage for the abbrev table. */
1223 auto_obstack abbrev_obstack;
1224
1225 private:
1226
1227 /* Hash table of abbrevs.
1228 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1229 It could be statically allocated, but the previous code didn't so we
1230 don't either. */
1231 struct abbrev_info **m_abbrevs;
1232 };
1233
1234 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1235
1236 /* Attributes have a name and a value. */
1237 struct attribute
1238 {
1239 ENUM_BITFIELD(dwarf_attribute) name : 16;
1240 ENUM_BITFIELD(dwarf_form) form : 15;
1241
1242 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1243 field should be in u.str (existing only for DW_STRING) but it is kept
1244 here for better struct attribute alignment. */
1245 unsigned int string_is_canonical : 1;
1246
1247 union
1248 {
1249 const char *str;
1250 struct dwarf_block *blk;
1251 ULONGEST unsnd;
1252 LONGEST snd;
1253 CORE_ADDR addr;
1254 ULONGEST signature;
1255 }
1256 u;
1257 };
1258
1259 /* This data structure holds a complete die structure. */
1260 struct die_info
1261 {
1262 /* DWARF-2 tag for this DIE. */
1263 ENUM_BITFIELD(dwarf_tag) tag : 16;
1264
1265 /* Number of attributes */
1266 unsigned char num_attrs;
1267
1268 /* True if we're presently building the full type name for the
1269 type derived from this DIE. */
1270 unsigned char building_fullname : 1;
1271
1272 /* True if this die is in process. PR 16581. */
1273 unsigned char in_process : 1;
1274
1275 /* Abbrev number */
1276 unsigned int abbrev;
1277
1278 /* Offset in .debug_info or .debug_types section. */
1279 sect_offset sect_off;
1280
1281 /* The dies in a compilation unit form an n-ary tree. PARENT
1282 points to this die's parent; CHILD points to the first child of
1283 this node; and all the children of a given node are chained
1284 together via their SIBLING fields. */
1285 struct die_info *child; /* Its first child, if any. */
1286 struct die_info *sibling; /* Its next sibling, if any. */
1287 struct die_info *parent; /* Its parent, if any. */
1288
1289 /* An array of attributes, with NUM_ATTRS elements. There may be
1290 zero, but it's not common and zero-sized arrays are not
1291 sufficiently portable C. */
1292 struct attribute attrs[1];
1293 };
1294
1295 /* Get at parts of an attribute structure. */
1296
1297 #define DW_STRING(attr) ((attr)->u.str)
1298 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1299 #define DW_UNSND(attr) ((attr)->u.unsnd)
1300 #define DW_BLOCK(attr) ((attr)->u.blk)
1301 #define DW_SND(attr) ((attr)->u.snd)
1302 #define DW_ADDR(attr) ((attr)->u.addr)
1303 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1304
1305 /* Blocks are a bunch of untyped bytes. */
1306 struct dwarf_block
1307 {
1308 size_t size;
1309
1310 /* Valid only if SIZE is not zero. */
1311 const gdb_byte *data;
1312 };
1313
1314 #ifndef ATTR_ALLOC_CHUNK
1315 #define ATTR_ALLOC_CHUNK 4
1316 #endif
1317
1318 /* Allocate fields for structs, unions and enums in this size. */
1319 #ifndef DW_FIELD_ALLOC_CHUNK
1320 #define DW_FIELD_ALLOC_CHUNK 4
1321 #endif
1322
1323 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1324 but this would require a corresponding change in unpack_field_as_long
1325 and friends. */
1326 static int bits_per_byte = 8;
1327
1328 /* When reading a variant or variant part, we track a bit more
1329 information about the field, and store it in an object of this
1330 type. */
1331
1332 struct variant_field
1333 {
1334 /* If we see a DW_TAG_variant, then this will be the discriminant
1335 value. */
1336 ULONGEST discriminant_value;
1337 /* If we see a DW_TAG_variant, then this will be set if this is the
1338 default branch. */
1339 bool default_branch;
1340 /* While reading a DW_TAG_variant_part, this will be set if this
1341 field is the discriminant. */
1342 bool is_discriminant;
1343 };
1344
1345 struct nextfield
1346 {
1347 int accessibility = 0;
1348 int virtuality = 0;
1349 /* Extra information to describe a variant or variant part. */
1350 struct variant_field variant {};
1351 struct field field {};
1352 };
1353
1354 struct fnfieldlist
1355 {
1356 const char *name = nullptr;
1357 std::vector<struct fn_field> fnfields;
1358 };
1359
1360 /* The routines that read and process dies for a C struct or C++ class
1361 pass lists of data member fields and lists of member function fields
1362 in an instance of a field_info structure, as defined below. */
1363 struct field_info
1364 {
1365 /* List of data member and baseclasses fields. */
1366 std::vector<struct nextfield> fields;
1367 std::vector<struct nextfield> baseclasses;
1368
1369 /* Number of fields (including baseclasses). */
1370 int nfields = 0;
1371
1372 /* Set if the accesibility of one of the fields is not public. */
1373 int non_public_fields = 0;
1374
1375 /* Member function fieldlist array, contains name of possibly overloaded
1376 member function, number of overloaded member functions and a pointer
1377 to the head of the member function field chain. */
1378 std::vector<struct fnfieldlist> fnfieldlists;
1379
1380 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1381 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1382 std::vector<struct decl_field> typedef_field_list;
1383
1384 /* Nested types defined by this class and the number of elements in this
1385 list. */
1386 std::vector<struct decl_field> nested_types_list;
1387 };
1388
1389 /* One item on the queue of compilation units to read in full symbols
1390 for. */
1391 struct dwarf2_queue_item
1392 {
1393 struct dwarf2_per_cu_data *per_cu;
1394 enum language pretend_language;
1395 struct dwarf2_queue_item *next;
1396 };
1397
1398 /* The current queue. */
1399 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1400
1401 /* Loaded secondary compilation units are kept in memory until they
1402 have not been referenced for the processing of this many
1403 compilation units. Set this to zero to disable caching. Cache
1404 sizes of up to at least twenty will improve startup time for
1405 typical inter-CU-reference binaries, at an obvious memory cost. */
1406 static int dwarf_max_cache_age = 5;
1407 static void
1408 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1409 struct cmd_list_element *c, const char *value)
1410 {
1411 fprintf_filtered (file, _("The upper bound on the age of cached "
1412 "DWARF compilation units is %s.\n"),
1413 value);
1414 }
1415 \f
1416 /* local function prototypes */
1417
1418 static const char *get_section_name (const struct dwarf2_section_info *);
1419
1420 static const char *get_section_file_name (const struct dwarf2_section_info *);
1421
1422 static void dwarf2_find_base_address (struct die_info *die,
1423 struct dwarf2_cu *cu);
1424
1425 static struct partial_symtab *create_partial_symtab
1426 (struct dwarf2_per_cu_data *per_cu, const char *name);
1427
1428 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1429 const gdb_byte *info_ptr,
1430 struct die_info *type_unit_die,
1431 int has_children, void *data);
1432
1433 static void dwarf2_build_psymtabs_hard
1434 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1435
1436 static void scan_partial_symbols (struct partial_die_info *,
1437 CORE_ADDR *, CORE_ADDR *,
1438 int, struct dwarf2_cu *);
1439
1440 static void add_partial_symbol (struct partial_die_info *,
1441 struct dwarf2_cu *);
1442
1443 static void add_partial_namespace (struct partial_die_info *pdi,
1444 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1445 int set_addrmap, struct dwarf2_cu *cu);
1446
1447 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1448 CORE_ADDR *highpc, int set_addrmap,
1449 struct dwarf2_cu *cu);
1450
1451 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1452 struct dwarf2_cu *cu);
1453
1454 static void add_partial_subprogram (struct partial_die_info *pdi,
1455 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1456 int need_pc, struct dwarf2_cu *cu);
1457
1458 static void dwarf2_read_symtab (struct partial_symtab *,
1459 struct objfile *);
1460
1461 static void psymtab_to_symtab_1 (struct partial_symtab *);
1462
1463 static abbrev_table_up abbrev_table_read_table
1464 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1465 sect_offset);
1466
1467 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1468
1469 static struct partial_die_info *load_partial_dies
1470 (const struct die_reader_specs *, const gdb_byte *, int);
1471
1472 static struct partial_die_info *find_partial_die (sect_offset, int,
1473 struct dwarf2_cu *);
1474
1475 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1476 struct attribute *, struct attr_abbrev *,
1477 const gdb_byte *);
1478
1479 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1480
1481 static int read_1_signed_byte (bfd *, const gdb_byte *);
1482
1483 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1484
1485 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1486
1487 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1488
1489 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1490 unsigned int *);
1491
1492 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1493
1494 static LONGEST read_checked_initial_length_and_offset
1495 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1496 unsigned int *, unsigned int *);
1497
1498 static LONGEST read_offset (bfd *, const gdb_byte *,
1499 const struct comp_unit_head *,
1500 unsigned int *);
1501
1502 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1503
1504 static sect_offset read_abbrev_offset
1505 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1506 struct dwarf2_section_info *, sect_offset);
1507
1508 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1509
1510 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1511
1512 static const char *read_indirect_string
1513 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1514 const struct comp_unit_head *, unsigned int *);
1515
1516 static const char *read_indirect_line_string
1517 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1518 const struct comp_unit_head *, unsigned int *);
1519
1520 static const char *read_indirect_string_at_offset
1521 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1522 LONGEST str_offset);
1523
1524 static const char *read_indirect_string_from_dwz
1525 (struct objfile *objfile, struct dwz_file *, LONGEST);
1526
1527 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1528
1529 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1530 const gdb_byte *,
1531 unsigned int *);
1532
1533 static const char *read_str_index (const struct die_reader_specs *reader,
1534 ULONGEST str_index);
1535
1536 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1537
1538 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1539 struct dwarf2_cu *);
1540
1541 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1542 unsigned int);
1543
1544 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1545 struct dwarf2_cu *cu);
1546
1547 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1548 struct dwarf2_cu *cu);
1549
1550 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1551
1552 static struct die_info *die_specification (struct die_info *die,
1553 struct dwarf2_cu **);
1554
1555 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1556 struct dwarf2_cu *cu);
1557
1558 static void dwarf_decode_lines (struct line_header *, const char *,
1559 struct dwarf2_cu *, struct partial_symtab *,
1560 CORE_ADDR, int decode_mapping);
1561
1562 static void dwarf2_start_subfile (const char *, const char *);
1563
1564 static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1565 const char *, const char *,
1566 CORE_ADDR);
1567
1568 static struct symbol *new_symbol (struct die_info *, struct type *,
1569 struct dwarf2_cu *, struct symbol * = NULL);
1570
1571 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1572 struct dwarf2_cu *);
1573
1574 static void dwarf2_const_value_attr (const struct attribute *attr,
1575 struct type *type,
1576 const char *name,
1577 struct obstack *obstack,
1578 struct dwarf2_cu *cu, LONGEST *value,
1579 const gdb_byte **bytes,
1580 struct dwarf2_locexpr_baton **baton);
1581
1582 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1583
1584 static int need_gnat_info (struct dwarf2_cu *);
1585
1586 static struct type *die_descriptive_type (struct die_info *,
1587 struct dwarf2_cu *);
1588
1589 static void set_descriptive_type (struct type *, struct die_info *,
1590 struct dwarf2_cu *);
1591
1592 static struct type *die_containing_type (struct die_info *,
1593 struct dwarf2_cu *);
1594
1595 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1596 struct dwarf2_cu *);
1597
1598 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1599
1600 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1601
1602 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1603
1604 static char *typename_concat (struct obstack *obs, const char *prefix,
1605 const char *suffix, int physname,
1606 struct dwarf2_cu *cu);
1607
1608 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1609
1610 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1611
1612 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1613
1614 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1615
1616 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1617
1618 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1619
1620 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1621 struct dwarf2_cu *, struct partial_symtab *);
1622
1623 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1624 values. Keep the items ordered with increasing constraints compliance. */
1625 enum pc_bounds_kind
1626 {
1627 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1628 PC_BOUNDS_NOT_PRESENT,
1629
1630 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1631 were present but they do not form a valid range of PC addresses. */
1632 PC_BOUNDS_INVALID,
1633
1634 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1635 PC_BOUNDS_RANGES,
1636
1637 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1638 PC_BOUNDS_HIGH_LOW,
1639 };
1640
1641 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1642 CORE_ADDR *, CORE_ADDR *,
1643 struct dwarf2_cu *,
1644 struct partial_symtab *);
1645
1646 static void get_scope_pc_bounds (struct die_info *,
1647 CORE_ADDR *, CORE_ADDR *,
1648 struct dwarf2_cu *);
1649
1650 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1651 CORE_ADDR, struct dwarf2_cu *);
1652
1653 static void dwarf2_add_field (struct field_info *, struct die_info *,
1654 struct dwarf2_cu *);
1655
1656 static void dwarf2_attach_fields_to_type (struct field_info *,
1657 struct type *, struct dwarf2_cu *);
1658
1659 static void dwarf2_add_member_fn (struct field_info *,
1660 struct die_info *, struct type *,
1661 struct dwarf2_cu *);
1662
1663 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1664 struct type *,
1665 struct dwarf2_cu *);
1666
1667 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1668
1669 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1670
1671 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1672
1673 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1674
1675 static struct using_direct **using_directives (enum language);
1676
1677 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1678
1679 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1680
1681 static struct type *read_module_type (struct die_info *die,
1682 struct dwarf2_cu *cu);
1683
1684 static const char *namespace_name (struct die_info *die,
1685 int *is_anonymous, struct dwarf2_cu *);
1686
1687 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1688
1689 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1690
1691 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1692 struct dwarf2_cu *);
1693
1694 static struct die_info *read_die_and_siblings_1
1695 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1696 struct die_info *);
1697
1698 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1699 const gdb_byte *info_ptr,
1700 const gdb_byte **new_info_ptr,
1701 struct die_info *parent);
1702
1703 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1704 struct die_info **, const gdb_byte *,
1705 int *, int);
1706
1707 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1708 struct die_info **, const gdb_byte *,
1709 int *);
1710
1711 static void process_die (struct die_info *, struct dwarf2_cu *);
1712
1713 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1714 struct obstack *);
1715
1716 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1717
1718 static const char *dwarf2_full_name (const char *name,
1719 struct die_info *die,
1720 struct dwarf2_cu *cu);
1721
1722 static const char *dwarf2_physname (const char *name, struct die_info *die,
1723 struct dwarf2_cu *cu);
1724
1725 static struct die_info *dwarf2_extension (struct die_info *die,
1726 struct dwarf2_cu **);
1727
1728 static const char *dwarf_tag_name (unsigned int);
1729
1730 static const char *dwarf_attr_name (unsigned int);
1731
1732 static const char *dwarf_form_name (unsigned int);
1733
1734 static const char *dwarf_bool_name (unsigned int);
1735
1736 static const char *dwarf_type_encoding_name (unsigned int);
1737
1738 static struct die_info *sibling_die (struct die_info *);
1739
1740 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1741
1742 static void dump_die_for_error (struct die_info *);
1743
1744 static void dump_die_1 (struct ui_file *, int level, int max_level,
1745 struct die_info *);
1746
1747 /*static*/ void dump_die (struct die_info *, int max_level);
1748
1749 static void store_in_ref_table (struct die_info *,
1750 struct dwarf2_cu *);
1751
1752 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1753
1754 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1755
1756 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1757 const struct attribute *,
1758 struct dwarf2_cu **);
1759
1760 static struct die_info *follow_die_ref (struct die_info *,
1761 const struct attribute *,
1762 struct dwarf2_cu **);
1763
1764 static struct die_info *follow_die_sig (struct die_info *,
1765 const struct attribute *,
1766 struct dwarf2_cu **);
1767
1768 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1769 struct dwarf2_cu *);
1770
1771 static struct type *get_DW_AT_signature_type (struct die_info *,
1772 const struct attribute *,
1773 struct dwarf2_cu *);
1774
1775 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1776
1777 static void read_signatured_type (struct signatured_type *);
1778
1779 static int attr_to_dynamic_prop (const struct attribute *attr,
1780 struct die_info *die, struct dwarf2_cu *cu,
1781 struct dynamic_prop *prop);
1782
1783 /* memory allocation interface */
1784
1785 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1786
1787 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1788
1789 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1790
1791 static int attr_form_is_block (const struct attribute *);
1792
1793 static int attr_form_is_section_offset (const struct attribute *);
1794
1795 static int attr_form_is_constant (const struct attribute *);
1796
1797 static int attr_form_is_ref (const struct attribute *);
1798
1799 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1800 struct dwarf2_loclist_baton *baton,
1801 const struct attribute *attr);
1802
1803 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1804 struct symbol *sym,
1805 struct dwarf2_cu *cu,
1806 int is_block);
1807
1808 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1809 const gdb_byte *info_ptr,
1810 struct abbrev_info *abbrev);
1811
1812 static hashval_t partial_die_hash (const void *item);
1813
1814 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1815
1816 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1817 (sect_offset sect_off, unsigned int offset_in_dwz,
1818 struct dwarf2_per_objfile *dwarf2_per_objfile);
1819
1820 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1821 struct die_info *comp_unit_die,
1822 enum language pretend_language);
1823
1824 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1825
1826 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1827
1828 static struct type *set_die_type (struct die_info *, struct type *,
1829 struct dwarf2_cu *);
1830
1831 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1832
1833 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1834
1835 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1836 enum language);
1837
1838 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1839 enum language);
1840
1841 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1842 enum language);
1843
1844 static void dwarf2_add_dependence (struct dwarf2_cu *,
1845 struct dwarf2_per_cu_data *);
1846
1847 static void dwarf2_mark (struct dwarf2_cu *);
1848
1849 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1850
1851 static struct type *get_die_type_at_offset (sect_offset,
1852 struct dwarf2_per_cu_data *);
1853
1854 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1855
1856 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1857 enum language pretend_language);
1858
1859 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1860
1861 /* Class, the destructor of which frees all allocated queue entries. This
1862 will only have work to do if an error was thrown while processing the
1863 dwarf. If no error was thrown then the queue entries should have all
1864 been processed, and freed, as we went along. */
1865
1866 class dwarf2_queue_guard
1867 {
1868 public:
1869 dwarf2_queue_guard () = default;
1870
1871 /* Free any entries remaining on the queue. There should only be
1872 entries left if we hit an error while processing the dwarf. */
1873 ~dwarf2_queue_guard ()
1874 {
1875 struct dwarf2_queue_item *item, *last;
1876
1877 item = dwarf2_queue;
1878 while (item)
1879 {
1880 /* Anything still marked queued is likely to be in an
1881 inconsistent state, so discard it. */
1882 if (item->per_cu->queued)
1883 {
1884 if (item->per_cu->cu != NULL)
1885 free_one_cached_comp_unit (item->per_cu);
1886 item->per_cu->queued = 0;
1887 }
1888
1889 last = item;
1890 item = item->next;
1891 xfree (last);
1892 }
1893
1894 dwarf2_queue = dwarf2_queue_tail = NULL;
1895 }
1896 };
1897
1898 /* The return type of find_file_and_directory. Note, the enclosed
1899 string pointers are only valid while this object is valid. */
1900
1901 struct file_and_directory
1902 {
1903 /* The filename. This is never NULL. */
1904 const char *name;
1905
1906 /* The compilation directory. NULL if not known. If we needed to
1907 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1908 points directly to the DW_AT_comp_dir string attribute owned by
1909 the obstack that owns the DIE. */
1910 const char *comp_dir;
1911
1912 /* If we needed to build a new string for comp_dir, this is what
1913 owns the storage. */
1914 std::string comp_dir_storage;
1915 };
1916
1917 static file_and_directory find_file_and_directory (struct die_info *die,
1918 struct dwarf2_cu *cu);
1919
1920 static char *file_full_name (int file, struct line_header *lh,
1921 const char *comp_dir);
1922
1923 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1924 enum class rcuh_kind { COMPILE, TYPE };
1925
1926 static const gdb_byte *read_and_check_comp_unit_head
1927 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1928 struct comp_unit_head *header,
1929 struct dwarf2_section_info *section,
1930 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1931 rcuh_kind section_kind);
1932
1933 static void init_cutu_and_read_dies
1934 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1935 int use_existing_cu, int keep,
1936 die_reader_func_ftype *die_reader_func, void *data);
1937
1938 static void init_cutu_and_read_dies_simple
1939 (struct dwarf2_per_cu_data *this_cu,
1940 die_reader_func_ftype *die_reader_func, void *data);
1941
1942 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1943
1944 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1945
1946 static struct dwo_unit *lookup_dwo_unit_in_dwp
1947 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1948 struct dwp_file *dwp_file, const char *comp_dir,
1949 ULONGEST signature, int is_debug_types);
1950
1951 static struct dwp_file *get_dwp_file
1952 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1953
1954 static struct dwo_unit *lookup_dwo_comp_unit
1955 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1956
1957 static struct dwo_unit *lookup_dwo_type_unit
1958 (struct signatured_type *, const char *, const char *);
1959
1960 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1961
1962 static void free_dwo_file (struct dwo_file *);
1963
1964 /* A unique_ptr helper to free a dwo_file. */
1965
1966 struct dwo_file_deleter
1967 {
1968 void operator() (struct dwo_file *df) const
1969 {
1970 free_dwo_file (df);
1971 }
1972 };
1973
1974 /* A unique pointer to a dwo_file. */
1975
1976 typedef std::unique_ptr<struct dwo_file, dwo_file_deleter> dwo_file_up;
1977
1978 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1979
1980 static void check_producer (struct dwarf2_cu *cu);
1981
1982 static void free_line_header_voidp (void *arg);
1983 \f
1984 /* Various complaints about symbol reading that don't abort the process. */
1985
1986 static void
1987 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1988 {
1989 complaint (&symfile_complaints,
1990 _("statement list doesn't fit in .debug_line section"));
1991 }
1992
1993 static void
1994 dwarf2_debug_line_missing_file_complaint (void)
1995 {
1996 complaint (&symfile_complaints,
1997 _(".debug_line section has line data without a file"));
1998 }
1999
2000 static void
2001 dwarf2_debug_line_missing_end_sequence_complaint (void)
2002 {
2003 complaint (&symfile_complaints,
2004 _(".debug_line section has line "
2005 "program sequence without an end"));
2006 }
2007
2008 static void
2009 dwarf2_complex_location_expr_complaint (void)
2010 {
2011 complaint (&symfile_complaints, _("location expression too complex"));
2012 }
2013
2014 static void
2015 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2016 int arg3)
2017 {
2018 complaint (&symfile_complaints,
2019 _("const value length mismatch for '%s', got %d, expected %d"),
2020 arg1, arg2, arg3);
2021 }
2022
2023 static void
2024 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2025 {
2026 complaint (&symfile_complaints,
2027 _("debug info runs off end of %s section"
2028 " [in module %s]"),
2029 get_section_name (section),
2030 get_section_file_name (section));
2031 }
2032
2033 static void
2034 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2035 {
2036 complaint (&symfile_complaints,
2037 _("macro debug info contains a "
2038 "malformed macro definition:\n`%s'"),
2039 arg1);
2040 }
2041
2042 static void
2043 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2044 {
2045 complaint (&symfile_complaints,
2046 _("invalid attribute class or form for '%s' in '%s'"),
2047 arg1, arg2);
2048 }
2049
2050 /* Hash function for line_header_hash. */
2051
2052 static hashval_t
2053 line_header_hash (const struct line_header *ofs)
2054 {
2055 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2056 }
2057
2058 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2059
2060 static hashval_t
2061 line_header_hash_voidp (const void *item)
2062 {
2063 const struct line_header *ofs = (const struct line_header *) item;
2064
2065 return line_header_hash (ofs);
2066 }
2067
2068 /* Equality function for line_header_hash. */
2069
2070 static int
2071 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2072 {
2073 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2074 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2075
2076 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2077 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2078 }
2079
2080 \f
2081
2082 /* Read the given attribute value as an address, taking the attribute's
2083 form into account. */
2084
2085 static CORE_ADDR
2086 attr_value_as_address (struct attribute *attr)
2087 {
2088 CORE_ADDR addr;
2089
2090 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2091 {
2092 /* Aside from a few clearly defined exceptions, attributes that
2093 contain an address must always be in DW_FORM_addr form.
2094 Unfortunately, some compilers happen to be violating this
2095 requirement by encoding addresses using other forms, such
2096 as DW_FORM_data4 for example. For those broken compilers,
2097 we try to do our best, without any guarantee of success,
2098 to interpret the address correctly. It would also be nice
2099 to generate a complaint, but that would require us to maintain
2100 a list of legitimate cases where a non-address form is allowed,
2101 as well as update callers to pass in at least the CU's DWARF
2102 version. This is more overhead than what we're willing to
2103 expand for a pretty rare case. */
2104 addr = DW_UNSND (attr);
2105 }
2106 else
2107 addr = DW_ADDR (attr);
2108
2109 return addr;
2110 }
2111
2112 /* See declaration. */
2113
2114 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2115 const dwarf2_debug_sections *names)
2116 : objfile (objfile_)
2117 {
2118 if (names == NULL)
2119 names = &dwarf2_elf_names;
2120
2121 bfd *obfd = objfile->obfd;
2122
2123 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2124 locate_sections (obfd, sec, *names);
2125 }
2126
2127 static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2128
2129 dwarf2_per_objfile::~dwarf2_per_objfile ()
2130 {
2131 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2132 free_cached_comp_units ();
2133
2134 if (quick_file_names_table)
2135 htab_delete (quick_file_names_table);
2136
2137 if (line_header_hash)
2138 htab_delete (line_header_hash);
2139
2140 for (int ix = 0; ix < n_comp_units; ++ix)
2141 VEC_free (dwarf2_per_cu_ptr, all_comp_units[ix]->imported_symtabs);
2142
2143 for (int ix = 0; ix < n_type_units; ++ix)
2144 VEC_free (dwarf2_per_cu_ptr,
2145 all_type_units[ix]->per_cu.imported_symtabs);
2146 xfree (all_type_units);
2147
2148 VEC_free (dwarf2_section_info_def, types);
2149
2150 if (dwo_files != NULL)
2151 free_dwo_files (dwo_files, objfile);
2152 if (dwp_file != NULL)
2153 gdb_bfd_unref (dwp_file->dbfd);
2154
2155 if (dwz_file != NULL && dwz_file->dwz_bfd)
2156 gdb_bfd_unref (dwz_file->dwz_bfd);
2157
2158 if (index_table != NULL)
2159 index_table->~mapped_index ();
2160
2161 /* Everything else should be on the objfile obstack. */
2162 }
2163
2164 /* See declaration. */
2165
2166 void
2167 dwarf2_per_objfile::free_cached_comp_units ()
2168 {
2169 dwarf2_per_cu_data *per_cu = read_in_chain;
2170 dwarf2_per_cu_data **last_chain = &read_in_chain;
2171 while (per_cu != NULL)
2172 {
2173 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2174
2175 delete per_cu->cu;
2176 *last_chain = next_cu;
2177 per_cu = next_cu;
2178 }
2179 }
2180
2181 /* A helper class that calls free_cached_comp_units on
2182 destruction. */
2183
2184 class free_cached_comp_units
2185 {
2186 public:
2187
2188 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2189 : m_per_objfile (per_objfile)
2190 {
2191 }
2192
2193 ~free_cached_comp_units ()
2194 {
2195 m_per_objfile->free_cached_comp_units ();
2196 }
2197
2198 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2199
2200 private:
2201
2202 dwarf2_per_objfile *m_per_objfile;
2203 };
2204
2205 /* Try to locate the sections we need for DWARF 2 debugging
2206 information and return true if we have enough to do something.
2207 NAMES points to the dwarf2 section names, or is NULL if the standard
2208 ELF names are used. */
2209
2210 int
2211 dwarf2_has_info (struct objfile *objfile,
2212 const struct dwarf2_debug_sections *names)
2213 {
2214 if (objfile->flags & OBJF_READNEVER)
2215 return 0;
2216
2217 struct dwarf2_per_objfile *dwarf2_per_objfile
2218 = get_dwarf2_per_objfile (objfile);
2219
2220 if (dwarf2_per_objfile == NULL)
2221 {
2222 /* Initialize per-objfile state. */
2223 dwarf2_per_objfile
2224 = new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
2225 names);
2226 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
2227 }
2228 return (!dwarf2_per_objfile->info.is_virtual
2229 && dwarf2_per_objfile->info.s.section != NULL
2230 && !dwarf2_per_objfile->abbrev.is_virtual
2231 && dwarf2_per_objfile->abbrev.s.section != NULL);
2232 }
2233
2234 /* Return the containing section of virtual section SECTION. */
2235
2236 static struct dwarf2_section_info *
2237 get_containing_section (const struct dwarf2_section_info *section)
2238 {
2239 gdb_assert (section->is_virtual);
2240 return section->s.containing_section;
2241 }
2242
2243 /* Return the bfd owner of SECTION. */
2244
2245 static struct bfd *
2246 get_section_bfd_owner (const struct dwarf2_section_info *section)
2247 {
2248 if (section->is_virtual)
2249 {
2250 section = get_containing_section (section);
2251 gdb_assert (!section->is_virtual);
2252 }
2253 return section->s.section->owner;
2254 }
2255
2256 /* Return the bfd section of SECTION.
2257 Returns NULL if the section is not present. */
2258
2259 static asection *
2260 get_section_bfd_section (const struct dwarf2_section_info *section)
2261 {
2262 if (section->is_virtual)
2263 {
2264 section = get_containing_section (section);
2265 gdb_assert (!section->is_virtual);
2266 }
2267 return section->s.section;
2268 }
2269
2270 /* Return the name of SECTION. */
2271
2272 static const char *
2273 get_section_name (const struct dwarf2_section_info *section)
2274 {
2275 asection *sectp = get_section_bfd_section (section);
2276
2277 gdb_assert (sectp != NULL);
2278 return bfd_section_name (get_section_bfd_owner (section), sectp);
2279 }
2280
2281 /* Return the name of the file SECTION is in. */
2282
2283 static const char *
2284 get_section_file_name (const struct dwarf2_section_info *section)
2285 {
2286 bfd *abfd = get_section_bfd_owner (section);
2287
2288 return bfd_get_filename (abfd);
2289 }
2290
2291 /* Return the id of SECTION.
2292 Returns 0 if SECTION doesn't exist. */
2293
2294 static int
2295 get_section_id (const struct dwarf2_section_info *section)
2296 {
2297 asection *sectp = get_section_bfd_section (section);
2298
2299 if (sectp == NULL)
2300 return 0;
2301 return sectp->id;
2302 }
2303
2304 /* Return the flags of SECTION.
2305 SECTION (or containing section if this is a virtual section) must exist. */
2306
2307 static int
2308 get_section_flags (const struct dwarf2_section_info *section)
2309 {
2310 asection *sectp = get_section_bfd_section (section);
2311
2312 gdb_assert (sectp != NULL);
2313 return bfd_get_section_flags (sectp->owner, sectp);
2314 }
2315
2316 /* When loading sections, we look either for uncompressed section or for
2317 compressed section names. */
2318
2319 static int
2320 section_is_p (const char *section_name,
2321 const struct dwarf2_section_names *names)
2322 {
2323 if (names->normal != NULL
2324 && strcmp (section_name, names->normal) == 0)
2325 return 1;
2326 if (names->compressed != NULL
2327 && strcmp (section_name, names->compressed) == 0)
2328 return 1;
2329 return 0;
2330 }
2331
2332 /* See declaration. */
2333
2334 void
2335 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2336 const dwarf2_debug_sections &names)
2337 {
2338 flagword aflag = bfd_get_section_flags (abfd, sectp);
2339
2340 if ((aflag & SEC_HAS_CONTENTS) == 0)
2341 {
2342 }
2343 else if (section_is_p (sectp->name, &names.info))
2344 {
2345 this->info.s.section = sectp;
2346 this->info.size = bfd_get_section_size (sectp);
2347 }
2348 else if (section_is_p (sectp->name, &names.abbrev))
2349 {
2350 this->abbrev.s.section = sectp;
2351 this->abbrev.size = bfd_get_section_size (sectp);
2352 }
2353 else if (section_is_p (sectp->name, &names.line))
2354 {
2355 this->line.s.section = sectp;
2356 this->line.size = bfd_get_section_size (sectp);
2357 }
2358 else if (section_is_p (sectp->name, &names.loc))
2359 {
2360 this->loc.s.section = sectp;
2361 this->loc.size = bfd_get_section_size (sectp);
2362 }
2363 else if (section_is_p (sectp->name, &names.loclists))
2364 {
2365 this->loclists.s.section = sectp;
2366 this->loclists.size = bfd_get_section_size (sectp);
2367 }
2368 else if (section_is_p (sectp->name, &names.macinfo))
2369 {
2370 this->macinfo.s.section = sectp;
2371 this->macinfo.size = bfd_get_section_size (sectp);
2372 }
2373 else if (section_is_p (sectp->name, &names.macro))
2374 {
2375 this->macro.s.section = sectp;
2376 this->macro.size = bfd_get_section_size (sectp);
2377 }
2378 else if (section_is_p (sectp->name, &names.str))
2379 {
2380 this->str.s.section = sectp;
2381 this->str.size = bfd_get_section_size (sectp);
2382 }
2383 else if (section_is_p (sectp->name, &names.line_str))
2384 {
2385 this->line_str.s.section = sectp;
2386 this->line_str.size = bfd_get_section_size (sectp);
2387 }
2388 else if (section_is_p (sectp->name, &names.addr))
2389 {
2390 this->addr.s.section = sectp;
2391 this->addr.size = bfd_get_section_size (sectp);
2392 }
2393 else if (section_is_p (sectp->name, &names.frame))
2394 {
2395 this->frame.s.section = sectp;
2396 this->frame.size = bfd_get_section_size (sectp);
2397 }
2398 else if (section_is_p (sectp->name, &names.eh_frame))
2399 {
2400 this->eh_frame.s.section = sectp;
2401 this->eh_frame.size = bfd_get_section_size (sectp);
2402 }
2403 else if (section_is_p (sectp->name, &names.ranges))
2404 {
2405 this->ranges.s.section = sectp;
2406 this->ranges.size = bfd_get_section_size (sectp);
2407 }
2408 else if (section_is_p (sectp->name, &names.rnglists))
2409 {
2410 this->rnglists.s.section = sectp;
2411 this->rnglists.size = bfd_get_section_size (sectp);
2412 }
2413 else if (section_is_p (sectp->name, &names.types))
2414 {
2415 struct dwarf2_section_info type_section;
2416
2417 memset (&type_section, 0, sizeof (type_section));
2418 type_section.s.section = sectp;
2419 type_section.size = bfd_get_section_size (sectp);
2420
2421 VEC_safe_push (dwarf2_section_info_def, this->types,
2422 &type_section);
2423 }
2424 else if (section_is_p (sectp->name, &names.gdb_index))
2425 {
2426 this->gdb_index.s.section = sectp;
2427 this->gdb_index.size = bfd_get_section_size (sectp);
2428 }
2429 else if (section_is_p (sectp->name, &names.debug_names))
2430 {
2431 this->debug_names.s.section = sectp;
2432 this->debug_names.size = bfd_get_section_size (sectp);
2433 }
2434 else if (section_is_p (sectp->name, &names.debug_aranges))
2435 {
2436 this->debug_aranges.s.section = sectp;
2437 this->debug_aranges.size = bfd_get_section_size (sectp);
2438 }
2439
2440 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
2441 && bfd_section_vma (abfd, sectp) == 0)
2442 this->has_section_at_zero = true;
2443 }
2444
2445 /* A helper function that decides whether a section is empty,
2446 or not present. */
2447
2448 static int
2449 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2450 {
2451 if (section->is_virtual)
2452 return section->size == 0;
2453 return section->s.section == NULL || section->size == 0;
2454 }
2455
2456 /* See dwarf2read.h. */
2457
2458 void
2459 dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
2460 {
2461 asection *sectp;
2462 bfd *abfd;
2463 gdb_byte *buf, *retbuf;
2464
2465 if (info->readin)
2466 return;
2467 info->buffer = NULL;
2468 info->readin = 1;
2469
2470 if (dwarf2_section_empty_p (info))
2471 return;
2472
2473 sectp = get_section_bfd_section (info);
2474
2475 /* If this is a virtual section we need to read in the real one first. */
2476 if (info->is_virtual)
2477 {
2478 struct dwarf2_section_info *containing_section =
2479 get_containing_section (info);
2480
2481 gdb_assert (sectp != NULL);
2482 if ((sectp->flags & SEC_RELOC) != 0)
2483 {
2484 error (_("Dwarf Error: DWP format V2 with relocations is not"
2485 " supported in section %s [in module %s]"),
2486 get_section_name (info), get_section_file_name (info));
2487 }
2488 dwarf2_read_section (objfile, containing_section);
2489 /* Other code should have already caught virtual sections that don't
2490 fit. */
2491 gdb_assert (info->virtual_offset + info->size
2492 <= containing_section->size);
2493 /* If the real section is empty or there was a problem reading the
2494 section we shouldn't get here. */
2495 gdb_assert (containing_section->buffer != NULL);
2496 info->buffer = containing_section->buffer + info->virtual_offset;
2497 return;
2498 }
2499
2500 /* If the section has relocations, we must read it ourselves.
2501 Otherwise we attach it to the BFD. */
2502 if ((sectp->flags & SEC_RELOC) == 0)
2503 {
2504 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2505 return;
2506 }
2507
2508 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2509 info->buffer = buf;
2510
2511 /* When debugging .o files, we may need to apply relocations; see
2512 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2513 We never compress sections in .o files, so we only need to
2514 try this when the section is not compressed. */
2515 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2516 if (retbuf != NULL)
2517 {
2518 info->buffer = retbuf;
2519 return;
2520 }
2521
2522 abfd = get_section_bfd_owner (info);
2523 gdb_assert (abfd != NULL);
2524
2525 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2526 || bfd_bread (buf, info->size, abfd) != info->size)
2527 {
2528 error (_("Dwarf Error: Can't read DWARF data"
2529 " in section %s [in module %s]"),
2530 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2531 }
2532 }
2533
2534 /* A helper function that returns the size of a section in a safe way.
2535 If you are positive that the section has been read before using the
2536 size, then it is safe to refer to the dwarf2_section_info object's
2537 "size" field directly. In other cases, you must call this
2538 function, because for compressed sections the size field is not set
2539 correctly until the section has been read. */
2540
2541 static bfd_size_type
2542 dwarf2_section_size (struct objfile *objfile,
2543 struct dwarf2_section_info *info)
2544 {
2545 if (!info->readin)
2546 dwarf2_read_section (objfile, info);
2547 return info->size;
2548 }
2549
2550 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2551 SECTION_NAME. */
2552
2553 void
2554 dwarf2_get_section_info (struct objfile *objfile,
2555 enum dwarf2_section_enum sect,
2556 asection **sectp, const gdb_byte **bufp,
2557 bfd_size_type *sizep)
2558 {
2559 struct dwarf2_per_objfile *data
2560 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2561 dwarf2_objfile_data_key);
2562 struct dwarf2_section_info *info;
2563
2564 /* We may see an objfile without any DWARF, in which case we just
2565 return nothing. */
2566 if (data == NULL)
2567 {
2568 *sectp = NULL;
2569 *bufp = NULL;
2570 *sizep = 0;
2571 return;
2572 }
2573 switch (sect)
2574 {
2575 case DWARF2_DEBUG_FRAME:
2576 info = &data->frame;
2577 break;
2578 case DWARF2_EH_FRAME:
2579 info = &data->eh_frame;
2580 break;
2581 default:
2582 gdb_assert_not_reached ("unexpected section");
2583 }
2584
2585 dwarf2_read_section (objfile, info);
2586
2587 *sectp = get_section_bfd_section (info);
2588 *bufp = info->buffer;
2589 *sizep = info->size;
2590 }
2591
2592 /* A helper function to find the sections for a .dwz file. */
2593
2594 static void
2595 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2596 {
2597 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2598
2599 /* Note that we only support the standard ELF names, because .dwz
2600 is ELF-only (at the time of writing). */
2601 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2602 {
2603 dwz_file->abbrev.s.section = sectp;
2604 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2605 }
2606 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2607 {
2608 dwz_file->info.s.section = sectp;
2609 dwz_file->info.size = bfd_get_section_size (sectp);
2610 }
2611 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2612 {
2613 dwz_file->str.s.section = sectp;
2614 dwz_file->str.size = bfd_get_section_size (sectp);
2615 }
2616 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2617 {
2618 dwz_file->line.s.section = sectp;
2619 dwz_file->line.size = bfd_get_section_size (sectp);
2620 }
2621 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2622 {
2623 dwz_file->macro.s.section = sectp;
2624 dwz_file->macro.size = bfd_get_section_size (sectp);
2625 }
2626 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2627 {
2628 dwz_file->gdb_index.s.section = sectp;
2629 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2630 }
2631 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2632 {
2633 dwz_file->debug_names.s.section = sectp;
2634 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2635 }
2636 }
2637
2638 /* Open the separate '.dwz' debug file, if needed. Return NULL if
2639 there is no .gnu_debugaltlink section in the file. Error if there
2640 is such a section but the file cannot be found. */
2641
2642 static struct dwz_file *
2643 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2644 {
2645 const char *filename;
2646 struct dwz_file *result;
2647 bfd_size_type buildid_len_arg;
2648 size_t buildid_len;
2649 bfd_byte *buildid;
2650
2651 if (dwarf2_per_objfile->dwz_file != NULL)
2652 return dwarf2_per_objfile->dwz_file;
2653
2654 bfd_set_error (bfd_error_no_error);
2655 gdb::unique_xmalloc_ptr<char> data
2656 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2657 &buildid_len_arg, &buildid));
2658 if (data == NULL)
2659 {
2660 if (bfd_get_error () == bfd_error_no_error)
2661 return NULL;
2662 error (_("could not read '.gnu_debugaltlink' section: %s"),
2663 bfd_errmsg (bfd_get_error ()));
2664 }
2665
2666 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2667
2668 buildid_len = (size_t) buildid_len_arg;
2669
2670 filename = data.get ();
2671
2672 std::string abs_storage;
2673 if (!IS_ABSOLUTE_PATH (filename))
2674 {
2675 gdb::unique_xmalloc_ptr<char> abs
2676 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2677
2678 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2679 filename = abs_storage.c_str ();
2680 }
2681
2682 /* First try the file name given in the section. If that doesn't
2683 work, try to use the build-id instead. */
2684 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2685 if (dwz_bfd != NULL)
2686 {
2687 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2688 dwz_bfd.release ();
2689 }
2690
2691 if (dwz_bfd == NULL)
2692 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2693
2694 if (dwz_bfd == NULL)
2695 error (_("could not find '.gnu_debugaltlink' file for %s"),
2696 objfile_name (dwarf2_per_objfile->objfile));
2697
2698 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2699 struct dwz_file);
2700 result->dwz_bfd = dwz_bfd.release ();
2701
2702 bfd_map_over_sections (result->dwz_bfd, locate_dwz_sections, result);
2703
2704 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, result->dwz_bfd);
2705 dwarf2_per_objfile->dwz_file = result;
2706 return result;
2707 }
2708 \f
2709 /* DWARF quick_symbols_functions support. */
2710
2711 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2712 unique line tables, so we maintain a separate table of all .debug_line
2713 derived entries to support the sharing.
2714 All the quick functions need is the list of file names. We discard the
2715 line_header when we're done and don't need to record it here. */
2716 struct quick_file_names
2717 {
2718 /* The data used to construct the hash key. */
2719 struct stmt_list_hash hash;
2720
2721 /* The number of entries in file_names, real_names. */
2722 unsigned int num_file_names;
2723
2724 /* The file names from the line table, after being run through
2725 file_full_name. */
2726 const char **file_names;
2727
2728 /* The file names from the line table after being run through
2729 gdb_realpath. These are computed lazily. */
2730 const char **real_names;
2731 };
2732
2733 /* When using the index (and thus not using psymtabs), each CU has an
2734 object of this type. This is used to hold information needed by
2735 the various "quick" methods. */
2736 struct dwarf2_per_cu_quick_data
2737 {
2738 /* The file table. This can be NULL if there was no file table
2739 or it's currently not read in.
2740 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2741 struct quick_file_names *file_names;
2742
2743 /* The corresponding symbol table. This is NULL if symbols for this
2744 CU have not yet been read. */
2745 struct compunit_symtab *compunit_symtab;
2746
2747 /* A temporary mark bit used when iterating over all CUs in
2748 expand_symtabs_matching. */
2749 unsigned int mark : 1;
2750
2751 /* True if we've tried to read the file table and found there isn't one.
2752 There will be no point in trying to read it again next time. */
2753 unsigned int no_file_data : 1;
2754 };
2755
2756 /* Utility hash function for a stmt_list_hash. */
2757
2758 static hashval_t
2759 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2760 {
2761 hashval_t v = 0;
2762
2763 if (stmt_list_hash->dwo_unit != NULL)
2764 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2765 v += to_underlying (stmt_list_hash->line_sect_off);
2766 return v;
2767 }
2768
2769 /* Utility equality function for a stmt_list_hash. */
2770
2771 static int
2772 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2773 const struct stmt_list_hash *rhs)
2774 {
2775 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2776 return 0;
2777 if (lhs->dwo_unit != NULL
2778 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2779 return 0;
2780
2781 return lhs->line_sect_off == rhs->line_sect_off;
2782 }
2783
2784 /* Hash function for a quick_file_names. */
2785
2786 static hashval_t
2787 hash_file_name_entry (const void *e)
2788 {
2789 const struct quick_file_names *file_data
2790 = (const struct quick_file_names *) e;
2791
2792 return hash_stmt_list_entry (&file_data->hash);
2793 }
2794
2795 /* Equality function for a quick_file_names. */
2796
2797 static int
2798 eq_file_name_entry (const void *a, const void *b)
2799 {
2800 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2801 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2802
2803 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2804 }
2805
2806 /* Delete function for a quick_file_names. */
2807
2808 static void
2809 delete_file_name_entry (void *e)
2810 {
2811 struct quick_file_names *file_data = (struct quick_file_names *) e;
2812 int i;
2813
2814 for (i = 0; i < file_data->num_file_names; ++i)
2815 {
2816 xfree ((void*) file_data->file_names[i]);
2817 if (file_data->real_names)
2818 xfree ((void*) file_data->real_names[i]);
2819 }
2820
2821 /* The space for the struct itself lives on objfile_obstack,
2822 so we don't free it here. */
2823 }
2824
2825 /* Create a quick_file_names hash table. */
2826
2827 static htab_t
2828 create_quick_file_names_table (unsigned int nr_initial_entries)
2829 {
2830 return htab_create_alloc (nr_initial_entries,
2831 hash_file_name_entry, eq_file_name_entry,
2832 delete_file_name_entry, xcalloc, xfree);
2833 }
2834
2835 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2836 have to be created afterwards. You should call age_cached_comp_units after
2837 processing PER_CU->CU. dw2_setup must have been already called. */
2838
2839 static void
2840 load_cu (struct dwarf2_per_cu_data *per_cu)
2841 {
2842 if (per_cu->is_debug_types)
2843 load_full_type_unit (per_cu);
2844 else
2845 load_full_comp_unit (per_cu, language_minimal);
2846
2847 if (per_cu->cu == NULL)
2848 return; /* Dummy CU. */
2849
2850 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2851 }
2852
2853 /* Read in the symbols for PER_CU. */
2854
2855 static void
2856 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2857 {
2858 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2859
2860 /* Skip type_unit_groups, reading the type units they contain
2861 is handled elsewhere. */
2862 if (IS_TYPE_UNIT_GROUP (per_cu))
2863 return;
2864
2865 /* The destructor of dwarf2_queue_guard frees any entries left on
2866 the queue. After this point we're guaranteed to leave this function
2867 with the dwarf queue empty. */
2868 dwarf2_queue_guard q_guard;
2869
2870 if (dwarf2_per_objfile->using_index
2871 ? per_cu->v.quick->compunit_symtab == NULL
2872 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2873 {
2874 queue_comp_unit (per_cu, language_minimal);
2875 load_cu (per_cu);
2876
2877 /* If we just loaded a CU from a DWO, and we're working with an index
2878 that may badly handle TUs, load all the TUs in that DWO as well.
2879 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2880 if (!per_cu->is_debug_types
2881 && per_cu->cu != NULL
2882 && per_cu->cu->dwo_unit != NULL
2883 && dwarf2_per_objfile->index_table != NULL
2884 && dwarf2_per_objfile->index_table->version <= 7
2885 /* DWP files aren't supported yet. */
2886 && get_dwp_file (dwarf2_per_objfile) == NULL)
2887 queue_and_load_all_dwo_tus (per_cu);
2888 }
2889
2890 process_queue (dwarf2_per_objfile);
2891
2892 /* Age the cache, releasing compilation units that have not
2893 been used recently. */
2894 age_cached_comp_units (dwarf2_per_objfile);
2895 }
2896
2897 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2898 the objfile from which this CU came. Returns the resulting symbol
2899 table. */
2900
2901 static struct compunit_symtab *
2902 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2903 {
2904 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2905
2906 gdb_assert (dwarf2_per_objfile->using_index);
2907 if (!per_cu->v.quick->compunit_symtab)
2908 {
2909 free_cached_comp_units freer (dwarf2_per_objfile);
2910 scoped_restore decrementer = increment_reading_symtab ();
2911 dw2_do_instantiate_symtab (per_cu);
2912 process_cu_includes (dwarf2_per_objfile);
2913 }
2914
2915 return per_cu->v.quick->compunit_symtab;
2916 }
2917
2918 /* See declaration. */
2919
2920 dwarf2_per_cu_data *
2921 dwarf2_per_objfile::get_cutu (int index)
2922 {
2923 if (index >= this->n_comp_units)
2924 {
2925 index -= this->n_comp_units;
2926 gdb_assert (index < this->n_type_units);
2927 return &this->all_type_units[index]->per_cu;
2928 }
2929
2930 return this->all_comp_units[index];
2931 }
2932
2933 /* See declaration. */
2934
2935 dwarf2_per_cu_data *
2936 dwarf2_per_objfile::get_cu (int index)
2937 {
2938 gdb_assert (index >= 0 && index < this->n_comp_units);
2939
2940 return this->all_comp_units[index];
2941 }
2942
2943 /* See declaration. */
2944
2945 signatured_type *
2946 dwarf2_per_objfile::get_tu (int index)
2947 {
2948 gdb_assert (index >= 0 && index < this->n_type_units);
2949
2950 return this->all_type_units[index];
2951 }
2952
2953 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2954 objfile_obstack, and constructed with the specified field
2955 values. */
2956
2957 static dwarf2_per_cu_data *
2958 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2959 struct dwarf2_section_info *section,
2960 int is_dwz,
2961 sect_offset sect_off, ULONGEST length)
2962 {
2963 struct objfile *objfile = dwarf2_per_objfile->objfile;
2964 dwarf2_per_cu_data *the_cu
2965 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2966 struct dwarf2_per_cu_data);
2967 the_cu->sect_off = sect_off;
2968 the_cu->length = length;
2969 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2970 the_cu->section = section;
2971 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2972 struct dwarf2_per_cu_quick_data);
2973 the_cu->is_dwz = is_dwz;
2974 return the_cu;
2975 }
2976
2977 /* A helper for create_cus_from_index that handles a given list of
2978 CUs. */
2979
2980 static void
2981 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2982 const gdb_byte *cu_list, offset_type n_elements,
2983 struct dwarf2_section_info *section,
2984 int is_dwz,
2985 int base_offset)
2986 {
2987 for (offset_type i = 0; i < n_elements; i += 2)
2988 {
2989 gdb_static_assert (sizeof (ULONGEST) >= 8);
2990
2991 sect_offset sect_off
2992 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2993 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2994 cu_list += 2 * 8;
2995
2996 dwarf2_per_objfile->all_comp_units[base_offset + i / 2]
2997 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2998 sect_off, length);
2999 }
3000 }
3001
3002 /* Read the CU list from the mapped index, and use it to create all
3003 the CU objects for this objfile. */
3004
3005 static void
3006 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3007 const gdb_byte *cu_list, offset_type cu_list_elements,
3008 const gdb_byte *dwz_list, offset_type dwz_elements)
3009 {
3010 struct objfile *objfile = dwarf2_per_objfile->objfile;
3011
3012 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
3013 dwarf2_per_objfile->all_comp_units =
3014 XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
3015 dwarf2_per_objfile->n_comp_units);
3016
3017 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
3018 &dwarf2_per_objfile->info, 0, 0);
3019
3020 if (dwz_elements == 0)
3021 return;
3022
3023 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3024 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
3025 &dwz->info, 1, cu_list_elements / 2);
3026 }
3027
3028 /* Create the signatured type hash table from the index. */
3029
3030 static void
3031 create_signatured_type_table_from_index
3032 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3033 struct dwarf2_section_info *section,
3034 const gdb_byte *bytes,
3035 offset_type elements)
3036 {
3037 struct objfile *objfile = dwarf2_per_objfile->objfile;
3038
3039 dwarf2_per_objfile->n_type_units
3040 = dwarf2_per_objfile->n_allocated_type_units
3041 = elements / 3;
3042 dwarf2_per_objfile->all_type_units =
3043 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3044
3045 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3046
3047 for (offset_type i = 0; i < elements; i += 3)
3048 {
3049 struct signatured_type *sig_type;
3050 ULONGEST signature;
3051 void **slot;
3052 cu_offset type_offset_in_tu;
3053
3054 gdb_static_assert (sizeof (ULONGEST) >= 8);
3055 sect_offset sect_off
3056 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3057 type_offset_in_tu
3058 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3059 BFD_ENDIAN_LITTLE);
3060 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3061 bytes += 3 * 8;
3062
3063 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3064 struct signatured_type);
3065 sig_type->signature = signature;
3066 sig_type->type_offset_in_tu = type_offset_in_tu;
3067 sig_type->per_cu.is_debug_types = 1;
3068 sig_type->per_cu.section = section;
3069 sig_type->per_cu.sect_off = sect_off;
3070 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3071 sig_type->per_cu.v.quick
3072 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3073 struct dwarf2_per_cu_quick_data);
3074
3075 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3076 *slot = sig_type;
3077
3078 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3079 }
3080
3081 dwarf2_per_objfile->signatured_types = sig_types_hash;
3082 }
3083
3084 /* Create the signatured type hash table from .debug_names. */
3085
3086 static void
3087 create_signatured_type_table_from_debug_names
3088 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3089 const mapped_debug_names &map,
3090 struct dwarf2_section_info *section,
3091 struct dwarf2_section_info *abbrev_section)
3092 {
3093 struct objfile *objfile = dwarf2_per_objfile->objfile;
3094
3095 dwarf2_read_section (objfile, section);
3096 dwarf2_read_section (objfile, abbrev_section);
3097
3098 dwarf2_per_objfile->n_type_units
3099 = dwarf2_per_objfile->n_allocated_type_units
3100 = map.tu_count;
3101 dwarf2_per_objfile->all_type_units
3102 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3103
3104 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3105
3106 for (uint32_t i = 0; i < map.tu_count; ++i)
3107 {
3108 struct signatured_type *sig_type;
3109 void **slot;
3110
3111 sect_offset sect_off
3112 = (sect_offset) (extract_unsigned_integer
3113 (map.tu_table_reordered + i * map.offset_size,
3114 map.offset_size,
3115 map.dwarf5_byte_order));
3116
3117 comp_unit_head cu_header;
3118 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3119 abbrev_section,
3120 section->buffer + to_underlying (sect_off),
3121 rcuh_kind::TYPE);
3122
3123 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3124 struct signatured_type);
3125 sig_type->signature = cu_header.signature;
3126 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3127 sig_type->per_cu.is_debug_types = 1;
3128 sig_type->per_cu.section = section;
3129 sig_type->per_cu.sect_off = sect_off;
3130 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3131 sig_type->per_cu.v.quick
3132 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3133 struct dwarf2_per_cu_quick_data);
3134
3135 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3136 *slot = sig_type;
3137
3138 dwarf2_per_objfile->all_type_units[i] = sig_type;
3139 }
3140
3141 dwarf2_per_objfile->signatured_types = sig_types_hash;
3142 }
3143
3144 /* Read the address map data from the mapped index, and use it to
3145 populate the objfile's psymtabs_addrmap. */
3146
3147 static void
3148 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3149 struct mapped_index *index)
3150 {
3151 struct objfile *objfile = dwarf2_per_objfile->objfile;
3152 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3153 const gdb_byte *iter, *end;
3154 struct addrmap *mutable_map;
3155 CORE_ADDR baseaddr;
3156
3157 auto_obstack temp_obstack;
3158
3159 mutable_map = addrmap_create_mutable (&temp_obstack);
3160
3161 iter = index->address_table.data ();
3162 end = iter + index->address_table.size ();
3163
3164 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3165
3166 while (iter < end)
3167 {
3168 ULONGEST hi, lo, cu_index;
3169 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3170 iter += 8;
3171 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3172 iter += 8;
3173 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3174 iter += 4;
3175
3176 if (lo > hi)
3177 {
3178 complaint (&symfile_complaints,
3179 _(".gdb_index address table has invalid range (%s - %s)"),
3180 hex_string (lo), hex_string (hi));
3181 continue;
3182 }
3183
3184 if (cu_index >= dwarf2_per_objfile->n_comp_units)
3185 {
3186 complaint (&symfile_complaints,
3187 _(".gdb_index address table has invalid CU number %u"),
3188 (unsigned) cu_index);
3189 continue;
3190 }
3191
3192 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3193 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3194 addrmap_set_empty (mutable_map, lo, hi - 1,
3195 dwarf2_per_objfile->get_cu (cu_index));
3196 }
3197
3198 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3199 &objfile->objfile_obstack);
3200 }
3201
3202 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3203 populate the objfile's psymtabs_addrmap. */
3204
3205 static void
3206 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3207 struct dwarf2_section_info *section)
3208 {
3209 struct objfile *objfile = dwarf2_per_objfile->objfile;
3210 bfd *abfd = objfile->obfd;
3211 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3212 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3213 SECT_OFF_TEXT (objfile));
3214
3215 auto_obstack temp_obstack;
3216 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3217
3218 std::unordered_map<sect_offset,
3219 dwarf2_per_cu_data *,
3220 gdb::hash_enum<sect_offset>>
3221 debug_info_offset_to_per_cu;
3222 for (int cui = 0; cui < dwarf2_per_objfile->n_comp_units; ++cui)
3223 {
3224 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cu (cui);
3225 const auto insertpair
3226 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3227 if (!insertpair.second)
3228 {
3229 warning (_("Section .debug_aranges in %s has duplicate "
3230 "debug_info_offset %s, ignoring .debug_aranges."),
3231 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3232 return;
3233 }
3234 }
3235
3236 dwarf2_read_section (objfile, section);
3237
3238 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3239
3240 const gdb_byte *addr = section->buffer;
3241
3242 while (addr < section->buffer + section->size)
3243 {
3244 const gdb_byte *const entry_addr = addr;
3245 unsigned int bytes_read;
3246
3247 const LONGEST entry_length = read_initial_length (abfd, addr,
3248 &bytes_read);
3249 addr += bytes_read;
3250
3251 const gdb_byte *const entry_end = addr + entry_length;
3252 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3253 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3254 if (addr + entry_length > section->buffer + section->size)
3255 {
3256 warning (_("Section .debug_aranges in %s entry at offset %zu "
3257 "length %s exceeds section length %s, "
3258 "ignoring .debug_aranges."),
3259 objfile_name (objfile), entry_addr - section->buffer,
3260 plongest (bytes_read + entry_length),
3261 pulongest (section->size));
3262 return;
3263 }
3264
3265 /* The version number. */
3266 const uint16_t version = read_2_bytes (abfd, addr);
3267 addr += 2;
3268 if (version != 2)
3269 {
3270 warning (_("Section .debug_aranges in %s entry at offset %zu "
3271 "has unsupported version %d, ignoring .debug_aranges."),
3272 objfile_name (objfile), entry_addr - section->buffer,
3273 version);
3274 return;
3275 }
3276
3277 const uint64_t debug_info_offset
3278 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3279 addr += offset_size;
3280 const auto per_cu_it
3281 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3282 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3283 {
3284 warning (_("Section .debug_aranges in %s entry at offset %zu "
3285 "debug_info_offset %s does not exists, "
3286 "ignoring .debug_aranges."),
3287 objfile_name (objfile), entry_addr - section->buffer,
3288 pulongest (debug_info_offset));
3289 return;
3290 }
3291 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3292
3293 const uint8_t address_size = *addr++;
3294 if (address_size < 1 || address_size > 8)
3295 {
3296 warning (_("Section .debug_aranges in %s entry at offset %zu "
3297 "address_size %u is invalid, ignoring .debug_aranges."),
3298 objfile_name (objfile), entry_addr - section->buffer,
3299 address_size);
3300 return;
3301 }
3302
3303 const uint8_t segment_selector_size = *addr++;
3304 if (segment_selector_size != 0)
3305 {
3306 warning (_("Section .debug_aranges in %s entry at offset %zu "
3307 "segment_selector_size %u is not supported, "
3308 "ignoring .debug_aranges."),
3309 objfile_name (objfile), entry_addr - section->buffer,
3310 segment_selector_size);
3311 return;
3312 }
3313
3314 /* Must pad to an alignment boundary that is twice the address
3315 size. It is undocumented by the DWARF standard but GCC does
3316 use it. */
3317 for (size_t padding = ((-(addr - section->buffer))
3318 & (2 * address_size - 1));
3319 padding > 0; padding--)
3320 if (*addr++ != 0)
3321 {
3322 warning (_("Section .debug_aranges in %s entry at offset %zu "
3323 "padding is not zero, ignoring .debug_aranges."),
3324 objfile_name (objfile), entry_addr - section->buffer);
3325 return;
3326 }
3327
3328 for (;;)
3329 {
3330 if (addr + 2 * address_size > entry_end)
3331 {
3332 warning (_("Section .debug_aranges in %s entry at offset %zu "
3333 "address list is not properly terminated, "
3334 "ignoring .debug_aranges."),
3335 objfile_name (objfile), entry_addr - section->buffer);
3336 return;
3337 }
3338 ULONGEST start = extract_unsigned_integer (addr, address_size,
3339 dwarf5_byte_order);
3340 addr += address_size;
3341 ULONGEST length = extract_unsigned_integer (addr, address_size,
3342 dwarf5_byte_order);
3343 addr += address_size;
3344 if (start == 0 && length == 0)
3345 break;
3346 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3347 {
3348 /* Symbol was eliminated due to a COMDAT group. */
3349 continue;
3350 }
3351 ULONGEST end = start + length;
3352 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3353 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3354 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3355 }
3356 }
3357
3358 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3359 &objfile->objfile_obstack);
3360 }
3361
3362 /* Find a slot in the mapped index INDEX for the object named NAME.
3363 If NAME is found, set *VEC_OUT to point to the CU vector in the
3364 constant pool and return true. If NAME cannot be found, return
3365 false. */
3366
3367 static bool
3368 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3369 offset_type **vec_out)
3370 {
3371 offset_type hash;
3372 offset_type slot, step;
3373 int (*cmp) (const char *, const char *);
3374
3375 gdb::unique_xmalloc_ptr<char> without_params;
3376 if (current_language->la_language == language_cplus
3377 || current_language->la_language == language_fortran
3378 || current_language->la_language == language_d)
3379 {
3380 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3381 not contain any. */
3382
3383 if (strchr (name, '(') != NULL)
3384 {
3385 without_params = cp_remove_params (name);
3386
3387 if (without_params != NULL)
3388 name = without_params.get ();
3389 }
3390 }
3391
3392 /* Index version 4 did not support case insensitive searches. But the
3393 indices for case insensitive languages are built in lowercase, therefore
3394 simulate our NAME being searched is also lowercased. */
3395 hash = mapped_index_string_hash ((index->version == 4
3396 && case_sensitivity == case_sensitive_off
3397 ? 5 : index->version),
3398 name);
3399
3400 slot = hash & (index->symbol_table.size () - 1);
3401 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3402 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3403
3404 for (;;)
3405 {
3406 const char *str;
3407
3408 const auto &bucket = index->symbol_table[slot];
3409 if (bucket.name == 0 && bucket.vec == 0)
3410 return false;
3411
3412 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3413 if (!cmp (name, str))
3414 {
3415 *vec_out = (offset_type *) (index->constant_pool
3416 + MAYBE_SWAP (bucket.vec));
3417 return true;
3418 }
3419
3420 slot = (slot + step) & (index->symbol_table.size () - 1);
3421 }
3422 }
3423
3424 /* A helper function that reads the .gdb_index from SECTION and fills
3425 in MAP. FILENAME is the name of the file containing the section;
3426 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3427 ok to use deprecated sections.
3428
3429 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3430 out parameters that are filled in with information about the CU and
3431 TU lists in the section.
3432
3433 Returns 1 if all went well, 0 otherwise. */
3434
3435 static int
3436 read_index_from_section (struct objfile *objfile,
3437 const char *filename,
3438 int deprecated_ok,
3439 struct dwarf2_section_info *section,
3440 struct mapped_index *map,
3441 const gdb_byte **cu_list,
3442 offset_type *cu_list_elements,
3443 const gdb_byte **types_list,
3444 offset_type *types_list_elements)
3445 {
3446 const gdb_byte *addr;
3447 offset_type version;
3448 offset_type *metadata;
3449 int i;
3450
3451 if (dwarf2_section_empty_p (section))
3452 return 0;
3453
3454 /* Older elfutils strip versions could keep the section in the main
3455 executable while splitting it for the separate debug info file. */
3456 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3457 return 0;
3458
3459 dwarf2_read_section (objfile, section);
3460
3461 addr = section->buffer;
3462 /* Version check. */
3463 version = MAYBE_SWAP (*(offset_type *) addr);
3464 /* Versions earlier than 3 emitted every copy of a psymbol. This
3465 causes the index to behave very poorly for certain requests. Version 3
3466 contained incomplete addrmap. So, it seems better to just ignore such
3467 indices. */
3468 if (version < 4)
3469 {
3470 static int warning_printed = 0;
3471 if (!warning_printed)
3472 {
3473 warning (_("Skipping obsolete .gdb_index section in %s."),
3474 filename);
3475 warning_printed = 1;
3476 }
3477 return 0;
3478 }
3479 /* Index version 4 uses a different hash function than index version
3480 5 and later.
3481
3482 Versions earlier than 6 did not emit psymbols for inlined
3483 functions. Using these files will cause GDB not to be able to
3484 set breakpoints on inlined functions by name, so we ignore these
3485 indices unless the user has done
3486 "set use-deprecated-index-sections on". */
3487 if (version < 6 && !deprecated_ok)
3488 {
3489 static int warning_printed = 0;
3490 if (!warning_printed)
3491 {
3492 warning (_("\
3493 Skipping deprecated .gdb_index section in %s.\n\
3494 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3495 to use the section anyway."),
3496 filename);
3497 warning_printed = 1;
3498 }
3499 return 0;
3500 }
3501 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3502 of the TU (for symbols coming from TUs),
3503 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3504 Plus gold-generated indices can have duplicate entries for global symbols,
3505 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3506 These are just performance bugs, and we can't distinguish gdb-generated
3507 indices from gold-generated ones, so issue no warning here. */
3508
3509 /* Indexes with higher version than the one supported by GDB may be no
3510 longer backward compatible. */
3511 if (version > 8)
3512 return 0;
3513
3514 map->version = version;
3515 map->total_size = section->size;
3516
3517 metadata = (offset_type *) (addr + sizeof (offset_type));
3518
3519 i = 0;
3520 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3521 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3522 / 8);
3523 ++i;
3524
3525 *types_list = addr + MAYBE_SWAP (metadata[i]);
3526 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3527 - MAYBE_SWAP (metadata[i]))
3528 / 8);
3529 ++i;
3530
3531 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3532 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3533 map->address_table
3534 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3535 ++i;
3536
3537 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3538 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3539 map->symbol_table
3540 = gdb::array_view<mapped_index::symbol_table_slot>
3541 ((mapped_index::symbol_table_slot *) symbol_table,
3542 (mapped_index::symbol_table_slot *) symbol_table_end);
3543
3544 ++i;
3545 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3546
3547 return 1;
3548 }
3549
3550 /* Read .gdb_index. If everything went ok, initialize the "quick"
3551 elements of all the CUs and return 1. Otherwise, return 0. */
3552
3553 static int
3554 dwarf2_read_index (struct dwarf2_per_objfile *dwarf2_per_objfile)
3555 {
3556 struct mapped_index local_map, *map;
3557 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3558 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3559 struct dwz_file *dwz;
3560 struct objfile *objfile = dwarf2_per_objfile->objfile;
3561
3562 if (!read_index_from_section (objfile, objfile_name (objfile),
3563 use_deprecated_index_sections,
3564 &dwarf2_per_objfile->gdb_index, &local_map,
3565 &cu_list, &cu_list_elements,
3566 &types_list, &types_list_elements))
3567 return 0;
3568
3569 /* Don't use the index if it's empty. */
3570 if (local_map.symbol_table.empty ())
3571 return 0;
3572
3573 /* If there is a .dwz file, read it so we can get its CU list as
3574 well. */
3575 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3576 if (dwz != NULL)
3577 {
3578 struct mapped_index dwz_map;
3579 const gdb_byte *dwz_types_ignore;
3580 offset_type dwz_types_elements_ignore;
3581
3582 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3583 1,
3584 &dwz->gdb_index, &dwz_map,
3585 &dwz_list, &dwz_list_elements,
3586 &dwz_types_ignore,
3587 &dwz_types_elements_ignore))
3588 {
3589 warning (_("could not read '.gdb_index' section from %s; skipping"),
3590 bfd_get_filename (dwz->dwz_bfd));
3591 return 0;
3592 }
3593 }
3594
3595 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3596 dwz_list, dwz_list_elements);
3597
3598 if (types_list_elements)
3599 {
3600 struct dwarf2_section_info *section;
3601
3602 /* We can only handle a single .debug_types when we have an
3603 index. */
3604 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3605 return 0;
3606
3607 section = VEC_index (dwarf2_section_info_def,
3608 dwarf2_per_objfile->types, 0);
3609
3610 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3611 types_list, types_list_elements);
3612 }
3613
3614 create_addrmap_from_index (dwarf2_per_objfile, &local_map);
3615
3616 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3617 map = new (map) mapped_index ();
3618 *map = local_map;
3619
3620 dwarf2_per_objfile->index_table = map;
3621 dwarf2_per_objfile->using_index = 1;
3622 dwarf2_per_objfile->quick_file_names_table =
3623 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3624
3625 return 1;
3626 }
3627
3628 /* die_reader_func for dw2_get_file_names. */
3629
3630 static void
3631 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3632 const gdb_byte *info_ptr,
3633 struct die_info *comp_unit_die,
3634 int has_children,
3635 void *data)
3636 {
3637 struct dwarf2_cu *cu = reader->cu;
3638 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3639 struct dwarf2_per_objfile *dwarf2_per_objfile
3640 = cu->per_cu->dwarf2_per_objfile;
3641 struct objfile *objfile = dwarf2_per_objfile->objfile;
3642 struct dwarf2_per_cu_data *lh_cu;
3643 struct attribute *attr;
3644 int i;
3645 void **slot;
3646 struct quick_file_names *qfn;
3647
3648 gdb_assert (! this_cu->is_debug_types);
3649
3650 /* Our callers never want to match partial units -- instead they
3651 will match the enclosing full CU. */
3652 if (comp_unit_die->tag == DW_TAG_partial_unit)
3653 {
3654 this_cu->v.quick->no_file_data = 1;
3655 return;
3656 }
3657
3658 lh_cu = this_cu;
3659 slot = NULL;
3660
3661 line_header_up lh;
3662 sect_offset line_offset {};
3663
3664 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3665 if (attr)
3666 {
3667 struct quick_file_names find_entry;
3668
3669 line_offset = (sect_offset) DW_UNSND (attr);
3670
3671 /* We may have already read in this line header (TU line header sharing).
3672 If we have we're done. */
3673 find_entry.hash.dwo_unit = cu->dwo_unit;
3674 find_entry.hash.line_sect_off = line_offset;
3675 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3676 &find_entry, INSERT);
3677 if (*slot != NULL)
3678 {
3679 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3680 return;
3681 }
3682
3683 lh = dwarf_decode_line_header (line_offset, cu);
3684 }
3685 if (lh == NULL)
3686 {
3687 lh_cu->v.quick->no_file_data = 1;
3688 return;
3689 }
3690
3691 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3692 qfn->hash.dwo_unit = cu->dwo_unit;
3693 qfn->hash.line_sect_off = line_offset;
3694 gdb_assert (slot != NULL);
3695 *slot = qfn;
3696
3697 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3698
3699 qfn->num_file_names = lh->file_names.size ();
3700 qfn->file_names =
3701 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3702 for (i = 0; i < lh->file_names.size (); ++i)
3703 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3704 qfn->real_names = NULL;
3705
3706 lh_cu->v.quick->file_names = qfn;
3707 }
3708
3709 /* A helper for the "quick" functions which attempts to read the line
3710 table for THIS_CU. */
3711
3712 static struct quick_file_names *
3713 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3714 {
3715 /* This should never be called for TUs. */
3716 gdb_assert (! this_cu->is_debug_types);
3717 /* Nor type unit groups. */
3718 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3719
3720 if (this_cu->v.quick->file_names != NULL)
3721 return this_cu->v.quick->file_names;
3722 /* If we know there is no line data, no point in looking again. */
3723 if (this_cu->v.quick->no_file_data)
3724 return NULL;
3725
3726 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3727
3728 if (this_cu->v.quick->no_file_data)
3729 return NULL;
3730 return this_cu->v.quick->file_names;
3731 }
3732
3733 /* A helper for the "quick" functions which computes and caches the
3734 real path for a given file name from the line table. */
3735
3736 static const char *
3737 dw2_get_real_path (struct objfile *objfile,
3738 struct quick_file_names *qfn, int index)
3739 {
3740 if (qfn->real_names == NULL)
3741 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3742 qfn->num_file_names, const char *);
3743
3744 if (qfn->real_names[index] == NULL)
3745 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3746
3747 return qfn->real_names[index];
3748 }
3749
3750 static struct symtab *
3751 dw2_find_last_source_symtab (struct objfile *objfile)
3752 {
3753 struct dwarf2_per_objfile *dwarf2_per_objfile
3754 = get_dwarf2_per_objfile (objfile);
3755 int index = dwarf2_per_objfile->n_comp_units - 1;
3756 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->get_cu (index);
3757 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu);
3758
3759 if (cust == NULL)
3760 return NULL;
3761
3762 return compunit_primary_filetab (cust);
3763 }
3764
3765 /* Traversal function for dw2_forget_cached_source_info. */
3766
3767 static int
3768 dw2_free_cached_file_names (void **slot, void *info)
3769 {
3770 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3771
3772 if (file_data->real_names)
3773 {
3774 int i;
3775
3776 for (i = 0; i < file_data->num_file_names; ++i)
3777 {
3778 xfree ((void*) file_data->real_names[i]);
3779 file_data->real_names[i] = NULL;
3780 }
3781 }
3782
3783 return 1;
3784 }
3785
3786 static void
3787 dw2_forget_cached_source_info (struct objfile *objfile)
3788 {
3789 struct dwarf2_per_objfile *dwarf2_per_objfile
3790 = get_dwarf2_per_objfile (objfile);
3791
3792 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3793 dw2_free_cached_file_names, NULL);
3794 }
3795
3796 /* Helper function for dw2_map_symtabs_matching_filename that expands
3797 the symtabs and calls the iterator. */
3798
3799 static int
3800 dw2_map_expand_apply (struct objfile *objfile,
3801 struct dwarf2_per_cu_data *per_cu,
3802 const char *name, const char *real_path,
3803 gdb::function_view<bool (symtab *)> callback)
3804 {
3805 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3806
3807 /* Don't visit already-expanded CUs. */
3808 if (per_cu->v.quick->compunit_symtab)
3809 return 0;
3810
3811 /* This may expand more than one symtab, and we want to iterate over
3812 all of them. */
3813 dw2_instantiate_symtab (per_cu);
3814
3815 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3816 last_made, callback);
3817 }
3818
3819 /* Implementation of the map_symtabs_matching_filename method. */
3820
3821 static bool
3822 dw2_map_symtabs_matching_filename
3823 (struct objfile *objfile, const char *name, const char *real_path,
3824 gdb::function_view<bool (symtab *)> callback)
3825 {
3826 const char *name_basename = lbasename (name);
3827 struct dwarf2_per_objfile *dwarf2_per_objfile
3828 = get_dwarf2_per_objfile (objfile);
3829
3830 /* The rule is CUs specify all the files, including those used by
3831 any TU, so there's no need to scan TUs here. */
3832
3833 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3834 {
3835 int j;
3836 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cu (i);
3837 struct quick_file_names *file_data;
3838
3839 /* We only need to look at symtabs not already expanded. */
3840 if (per_cu->v.quick->compunit_symtab)
3841 continue;
3842
3843 file_data = dw2_get_file_names (per_cu);
3844 if (file_data == NULL)
3845 continue;
3846
3847 for (j = 0; j < file_data->num_file_names; ++j)
3848 {
3849 const char *this_name = file_data->file_names[j];
3850 const char *this_real_name;
3851
3852 if (compare_filenames_for_search (this_name, name))
3853 {
3854 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3855 callback))
3856 return true;
3857 continue;
3858 }
3859
3860 /* Before we invoke realpath, which can get expensive when many
3861 files are involved, do a quick comparison of the basenames. */
3862 if (! basenames_may_differ
3863 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3864 continue;
3865
3866 this_real_name = dw2_get_real_path (objfile, file_data, j);
3867 if (compare_filenames_for_search (this_real_name, name))
3868 {
3869 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3870 callback))
3871 return true;
3872 continue;
3873 }
3874
3875 if (real_path != NULL)
3876 {
3877 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3878 gdb_assert (IS_ABSOLUTE_PATH (name));
3879 if (this_real_name != NULL
3880 && FILENAME_CMP (real_path, this_real_name) == 0)
3881 {
3882 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3883 callback))
3884 return true;
3885 continue;
3886 }
3887 }
3888 }
3889 }
3890
3891 return false;
3892 }
3893
3894 /* Struct used to manage iterating over all CUs looking for a symbol. */
3895
3896 struct dw2_symtab_iterator
3897 {
3898 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3899 struct dwarf2_per_objfile *dwarf2_per_objfile;
3900 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3901 int want_specific_block;
3902 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3903 Unused if !WANT_SPECIFIC_BLOCK. */
3904 int block_index;
3905 /* The kind of symbol we're looking for. */
3906 domain_enum domain;
3907 /* The list of CUs from the index entry of the symbol,
3908 or NULL if not found. */
3909 offset_type *vec;
3910 /* The next element in VEC to look at. */
3911 int next;
3912 /* The number of elements in VEC, or zero if there is no match. */
3913 int length;
3914 /* Have we seen a global version of the symbol?
3915 If so we can ignore all further global instances.
3916 This is to work around gold/15646, inefficient gold-generated
3917 indices. */
3918 int global_seen;
3919 };
3920
3921 /* Initialize the index symtab iterator ITER.
3922 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3923 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3924
3925 static void
3926 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3927 struct dwarf2_per_objfile *dwarf2_per_objfile,
3928 int want_specific_block,
3929 int block_index,
3930 domain_enum domain,
3931 const char *name)
3932 {
3933 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3934 iter->want_specific_block = want_specific_block;
3935 iter->block_index = block_index;
3936 iter->domain = domain;
3937 iter->next = 0;
3938 iter->global_seen = 0;
3939
3940 mapped_index *index = dwarf2_per_objfile->index_table;
3941
3942 /* index is NULL if OBJF_READNOW. */
3943 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3944 iter->length = MAYBE_SWAP (*iter->vec);
3945 else
3946 {
3947 iter->vec = NULL;
3948 iter->length = 0;
3949 }
3950 }
3951
3952 /* Return the next matching CU or NULL if there are no more. */
3953
3954 static struct dwarf2_per_cu_data *
3955 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3956 {
3957 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3958
3959 for ( ; iter->next < iter->length; ++iter->next)
3960 {
3961 offset_type cu_index_and_attrs =
3962 MAYBE_SWAP (iter->vec[iter->next + 1]);
3963 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3964 int want_static = iter->block_index != GLOBAL_BLOCK;
3965 /* This value is only valid for index versions >= 7. */
3966 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3967 gdb_index_symbol_kind symbol_kind =
3968 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3969 /* Only check the symbol attributes if they're present.
3970 Indices prior to version 7 don't record them,
3971 and indices >= 7 may elide them for certain symbols
3972 (gold does this). */
3973 int attrs_valid =
3974 (dwarf2_per_objfile->index_table->version >= 7
3975 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3976
3977 /* Don't crash on bad data. */
3978 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3979 + dwarf2_per_objfile->n_type_units))
3980 {
3981 complaint (&symfile_complaints,
3982 _(".gdb_index entry has bad CU index"
3983 " [in module %s]"),
3984 objfile_name (dwarf2_per_objfile->objfile));
3985 continue;
3986 }
3987
3988 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3989
3990 /* Skip if already read in. */
3991 if (per_cu->v.quick->compunit_symtab)
3992 continue;
3993
3994 /* Check static vs global. */
3995 if (attrs_valid)
3996 {
3997 if (iter->want_specific_block
3998 && want_static != is_static)
3999 continue;
4000 /* Work around gold/15646. */
4001 if (!is_static && iter->global_seen)
4002 continue;
4003 if (!is_static)
4004 iter->global_seen = 1;
4005 }
4006
4007 /* Only check the symbol's kind if it has one. */
4008 if (attrs_valid)
4009 {
4010 switch (iter->domain)
4011 {
4012 case VAR_DOMAIN:
4013 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4014 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4015 /* Some types are also in VAR_DOMAIN. */
4016 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4017 continue;
4018 break;
4019 case STRUCT_DOMAIN:
4020 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4021 continue;
4022 break;
4023 case LABEL_DOMAIN:
4024 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4025 continue;
4026 break;
4027 default:
4028 break;
4029 }
4030 }
4031
4032 ++iter->next;
4033 return per_cu;
4034 }
4035
4036 return NULL;
4037 }
4038
4039 static struct compunit_symtab *
4040 dw2_lookup_symbol (struct objfile *objfile, int block_index,
4041 const char *name, domain_enum domain)
4042 {
4043 struct compunit_symtab *stab_best = NULL;
4044 struct dwarf2_per_objfile *dwarf2_per_objfile
4045 = get_dwarf2_per_objfile (objfile);
4046
4047 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4048
4049 struct dw2_symtab_iterator iter;
4050 struct dwarf2_per_cu_data *per_cu;
4051
4052 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
4053
4054 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4055 {
4056 struct symbol *sym, *with_opaque = NULL;
4057 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4058 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4059 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4060
4061 sym = block_find_symbol (block, name, domain,
4062 block_find_non_opaque_type_preferred,
4063 &with_opaque);
4064
4065 /* Some caution must be observed with overloaded functions
4066 and methods, since the index will not contain any overload
4067 information (but NAME might contain it). */
4068
4069 if (sym != NULL
4070 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4071 return stab;
4072 if (with_opaque != NULL
4073 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4074 stab_best = stab;
4075
4076 /* Keep looking through other CUs. */
4077 }
4078
4079 return stab_best;
4080 }
4081
4082 static void
4083 dw2_print_stats (struct objfile *objfile)
4084 {
4085 struct dwarf2_per_objfile *dwarf2_per_objfile
4086 = get_dwarf2_per_objfile (objfile);
4087 int total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
4088 int count = 0;
4089
4090 for (int i = 0; i < total; ++i)
4091 {
4092 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4093
4094 if (!per_cu->v.quick->compunit_symtab)
4095 ++count;
4096 }
4097 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4098 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4099 }
4100
4101 /* This dumps minimal information about the index.
4102 It is called via "mt print objfiles".
4103 One use is to verify .gdb_index has been loaded by the
4104 gdb.dwarf2/gdb-index.exp testcase. */
4105
4106 static void
4107 dw2_dump (struct objfile *objfile)
4108 {
4109 struct dwarf2_per_objfile *dwarf2_per_objfile
4110 = get_dwarf2_per_objfile (objfile);
4111
4112 gdb_assert (dwarf2_per_objfile->using_index);
4113 printf_filtered (".gdb_index:");
4114 if (dwarf2_per_objfile->index_table != NULL)
4115 {
4116 printf_filtered (" version %d\n",
4117 dwarf2_per_objfile->index_table->version);
4118 }
4119 else
4120 printf_filtered (" faked for \"readnow\"\n");
4121 printf_filtered ("\n");
4122 }
4123
4124 static void
4125 dw2_relocate (struct objfile *objfile,
4126 const struct section_offsets *new_offsets,
4127 const struct section_offsets *delta)
4128 {
4129 /* There's nothing to relocate here. */
4130 }
4131
4132 static void
4133 dw2_expand_symtabs_for_function (struct objfile *objfile,
4134 const char *func_name)
4135 {
4136 struct dwarf2_per_objfile *dwarf2_per_objfile
4137 = get_dwarf2_per_objfile (objfile);
4138
4139 struct dw2_symtab_iterator iter;
4140 struct dwarf2_per_cu_data *per_cu;
4141
4142 /* Note: It doesn't matter what we pass for block_index here. */
4143 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4144 func_name);
4145
4146 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4147 dw2_instantiate_symtab (per_cu);
4148
4149 }
4150
4151 static void
4152 dw2_expand_all_symtabs (struct objfile *objfile)
4153 {
4154 struct dwarf2_per_objfile *dwarf2_per_objfile
4155 = get_dwarf2_per_objfile (objfile);
4156 int total_units = (dwarf2_per_objfile->n_comp_units
4157 + dwarf2_per_objfile->n_type_units);
4158
4159 for (int i = 0; i < total_units; ++i)
4160 {
4161 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4162
4163 dw2_instantiate_symtab (per_cu);
4164 }
4165 }
4166
4167 static void
4168 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4169 const char *fullname)
4170 {
4171 struct dwarf2_per_objfile *dwarf2_per_objfile
4172 = get_dwarf2_per_objfile (objfile);
4173
4174 /* We don't need to consider type units here.
4175 This is only called for examining code, e.g. expand_line_sal.
4176 There can be an order of magnitude (or more) more type units
4177 than comp units, and we avoid them if we can. */
4178
4179 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4180 {
4181 int j;
4182 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cu (i);
4183 struct quick_file_names *file_data;
4184
4185 /* We only need to look at symtabs not already expanded. */
4186 if (per_cu->v.quick->compunit_symtab)
4187 continue;
4188
4189 file_data = dw2_get_file_names (per_cu);
4190 if (file_data == NULL)
4191 continue;
4192
4193 for (j = 0; j < file_data->num_file_names; ++j)
4194 {
4195 const char *this_fullname = file_data->file_names[j];
4196
4197 if (filename_cmp (this_fullname, fullname) == 0)
4198 {
4199 dw2_instantiate_symtab (per_cu);
4200 break;
4201 }
4202 }
4203 }
4204 }
4205
4206 static void
4207 dw2_map_matching_symbols (struct objfile *objfile,
4208 const char * name, domain_enum domain,
4209 int global,
4210 int (*callback) (struct block *,
4211 struct symbol *, void *),
4212 void *data, symbol_name_match_type match,
4213 symbol_compare_ftype *ordered_compare)
4214 {
4215 /* Currently unimplemented; used for Ada. The function can be called if the
4216 current language is Ada for a non-Ada objfile using GNU index. As Ada
4217 does not look for non-Ada symbols this function should just return. */
4218 }
4219
4220 /* Symbol name matcher for .gdb_index names.
4221
4222 Symbol names in .gdb_index have a few particularities:
4223
4224 - There's no indication of which is the language of each symbol.
4225
4226 Since each language has its own symbol name matching algorithm,
4227 and we don't know which language is the right one, we must match
4228 each symbol against all languages. This would be a potential
4229 performance problem if it were not mitigated by the
4230 mapped_index::name_components lookup table, which significantly
4231 reduces the number of times we need to call into this matcher,
4232 making it a non-issue.
4233
4234 - Symbol names in the index have no overload (parameter)
4235 information. I.e., in C++, "foo(int)" and "foo(long)" both
4236 appear as "foo" in the index, for example.
4237
4238 This means that the lookup names passed to the symbol name
4239 matcher functions must have no parameter information either
4240 because (e.g.) symbol search name "foo" does not match
4241 lookup-name "foo(int)" [while swapping search name for lookup
4242 name would match].
4243 */
4244 class gdb_index_symbol_name_matcher
4245 {
4246 public:
4247 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4248 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4249
4250 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4251 Returns true if any matcher matches. */
4252 bool matches (const char *symbol_name);
4253
4254 private:
4255 /* A reference to the lookup name we're matching against. */
4256 const lookup_name_info &m_lookup_name;
4257
4258 /* A vector holding all the different symbol name matchers, for all
4259 languages. */
4260 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4261 };
4262
4263 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4264 (const lookup_name_info &lookup_name)
4265 : m_lookup_name (lookup_name)
4266 {
4267 /* Prepare the vector of comparison functions upfront, to avoid
4268 doing the same work for each symbol. Care is taken to avoid
4269 matching with the same matcher more than once if/when multiple
4270 languages use the same matcher function. */
4271 auto &matchers = m_symbol_name_matcher_funcs;
4272 matchers.reserve (nr_languages);
4273
4274 matchers.push_back (default_symbol_name_matcher);
4275
4276 for (int i = 0; i < nr_languages; i++)
4277 {
4278 const language_defn *lang = language_def ((enum language) i);
4279 symbol_name_matcher_ftype *name_matcher
4280 = get_symbol_name_matcher (lang, m_lookup_name);
4281
4282 /* Don't insert the same comparison routine more than once.
4283 Note that we do this linear walk instead of a seemingly
4284 cheaper sorted insert, or use a std::set or something like
4285 that, because relative order of function addresses is not
4286 stable. This is not a problem in practice because the number
4287 of supported languages is low, and the cost here is tiny
4288 compared to the number of searches we'll do afterwards using
4289 this object. */
4290 if (name_matcher != default_symbol_name_matcher
4291 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4292 == matchers.end ()))
4293 matchers.push_back (name_matcher);
4294 }
4295 }
4296
4297 bool
4298 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4299 {
4300 for (auto matches_name : m_symbol_name_matcher_funcs)
4301 if (matches_name (symbol_name, m_lookup_name, NULL))
4302 return true;
4303
4304 return false;
4305 }
4306
4307 /* Starting from a search name, return the string that finds the upper
4308 bound of all strings that start with SEARCH_NAME in a sorted name
4309 list. Returns the empty string to indicate that the upper bound is
4310 the end of the list. */
4311
4312 static std::string
4313 make_sort_after_prefix_name (const char *search_name)
4314 {
4315 /* When looking to complete "func", we find the upper bound of all
4316 symbols that start with "func" by looking for where we'd insert
4317 the closest string that would follow "func" in lexicographical
4318 order. Usually, that's "func"-with-last-character-incremented,
4319 i.e. "fund". Mind non-ASCII characters, though. Usually those
4320 will be UTF-8 multi-byte sequences, but we can't be certain.
4321 Especially mind the 0xff character, which is a valid character in
4322 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4323 rule out compilers allowing it in identifiers. Note that
4324 conveniently, strcmp/strcasecmp are specified to compare
4325 characters interpreted as unsigned char. So what we do is treat
4326 the whole string as a base 256 number composed of a sequence of
4327 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4328 to 0, and carries 1 to the following more-significant position.
4329 If the very first character in SEARCH_NAME ends up incremented
4330 and carries/overflows, then the upper bound is the end of the
4331 list. The string after the empty string is also the empty
4332 string.
4333
4334 Some examples of this operation:
4335
4336 SEARCH_NAME => "+1" RESULT
4337
4338 "abc" => "abd"
4339 "ab\xff" => "ac"
4340 "\xff" "a" "\xff" => "\xff" "b"
4341 "\xff" => ""
4342 "\xff\xff" => ""
4343 "" => ""
4344
4345 Then, with these symbols for example:
4346
4347 func
4348 func1
4349 fund
4350
4351 completing "func" looks for symbols between "func" and
4352 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4353 which finds "func" and "func1", but not "fund".
4354
4355 And with:
4356
4357 funcÿ (Latin1 'ÿ' [0xff])
4358 funcÿ1
4359 fund
4360
4361 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4362 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4363
4364 And with:
4365
4366 ÿÿ (Latin1 'ÿ' [0xff])
4367 ÿÿ1
4368
4369 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4370 the end of the list.
4371 */
4372 std::string after = search_name;
4373 while (!after.empty () && (unsigned char) after.back () == 0xff)
4374 after.pop_back ();
4375 if (!after.empty ())
4376 after.back () = (unsigned char) after.back () + 1;
4377 return after;
4378 }
4379
4380 /* See declaration. */
4381
4382 std::pair<std::vector<name_component>::const_iterator,
4383 std::vector<name_component>::const_iterator>
4384 mapped_index_base::find_name_components_bounds
4385 (const lookup_name_info &lookup_name_without_params) const
4386 {
4387 auto *name_cmp
4388 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4389
4390 const char *cplus
4391 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4392
4393 /* Comparison function object for lower_bound that matches against a
4394 given symbol name. */
4395 auto lookup_compare_lower = [&] (const name_component &elem,
4396 const char *name)
4397 {
4398 const char *elem_qualified = this->symbol_name_at (elem.idx);
4399 const char *elem_name = elem_qualified + elem.name_offset;
4400 return name_cmp (elem_name, name) < 0;
4401 };
4402
4403 /* Comparison function object for upper_bound that matches against a
4404 given symbol name. */
4405 auto lookup_compare_upper = [&] (const char *name,
4406 const name_component &elem)
4407 {
4408 const char *elem_qualified = this->symbol_name_at (elem.idx);
4409 const char *elem_name = elem_qualified + elem.name_offset;
4410 return name_cmp (name, elem_name) < 0;
4411 };
4412
4413 auto begin = this->name_components.begin ();
4414 auto end = this->name_components.end ();
4415
4416 /* Find the lower bound. */
4417 auto lower = [&] ()
4418 {
4419 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4420 return begin;
4421 else
4422 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4423 } ();
4424
4425 /* Find the upper bound. */
4426 auto upper = [&] ()
4427 {
4428 if (lookup_name_without_params.completion_mode ())
4429 {
4430 /* In completion mode, we want UPPER to point past all
4431 symbols names that have the same prefix. I.e., with
4432 these symbols, and completing "func":
4433
4434 function << lower bound
4435 function1
4436 other_function << upper bound
4437
4438 We find the upper bound by looking for the insertion
4439 point of "func"-with-last-character-incremented,
4440 i.e. "fund". */
4441 std::string after = make_sort_after_prefix_name (cplus);
4442 if (after.empty ())
4443 return end;
4444 return std::lower_bound (lower, end, after.c_str (),
4445 lookup_compare_lower);
4446 }
4447 else
4448 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4449 } ();
4450
4451 return {lower, upper};
4452 }
4453
4454 /* See declaration. */
4455
4456 void
4457 mapped_index_base::build_name_components ()
4458 {
4459 if (!this->name_components.empty ())
4460 return;
4461
4462 this->name_components_casing = case_sensitivity;
4463 auto *name_cmp
4464 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4465
4466 /* The code below only knows how to break apart components of C++
4467 symbol names (and other languages that use '::' as
4468 namespace/module separator). If we add support for wild matching
4469 to some language that uses some other operator (E.g., Ada, Go and
4470 D use '.'), then we'll need to try splitting the symbol name
4471 according to that language too. Note that Ada does support wild
4472 matching, but doesn't currently support .gdb_index. */
4473 auto count = this->symbol_name_count ();
4474 for (offset_type idx = 0; idx < count; idx++)
4475 {
4476 if (this->symbol_name_slot_invalid (idx))
4477 continue;
4478
4479 const char *name = this->symbol_name_at (idx);
4480
4481 /* Add each name component to the name component table. */
4482 unsigned int previous_len = 0;
4483 for (unsigned int current_len = cp_find_first_component (name);
4484 name[current_len] != '\0';
4485 current_len += cp_find_first_component (name + current_len))
4486 {
4487 gdb_assert (name[current_len] == ':');
4488 this->name_components.push_back ({previous_len, idx});
4489 /* Skip the '::'. */
4490 current_len += 2;
4491 previous_len = current_len;
4492 }
4493 this->name_components.push_back ({previous_len, idx});
4494 }
4495
4496 /* Sort name_components elements by name. */
4497 auto name_comp_compare = [&] (const name_component &left,
4498 const name_component &right)
4499 {
4500 const char *left_qualified = this->symbol_name_at (left.idx);
4501 const char *right_qualified = this->symbol_name_at (right.idx);
4502
4503 const char *left_name = left_qualified + left.name_offset;
4504 const char *right_name = right_qualified + right.name_offset;
4505
4506 return name_cmp (left_name, right_name) < 0;
4507 };
4508
4509 std::sort (this->name_components.begin (),
4510 this->name_components.end (),
4511 name_comp_compare);
4512 }
4513
4514 /* Helper for dw2_expand_symtabs_matching that works with a
4515 mapped_index_base instead of the containing objfile. This is split
4516 to a separate function in order to be able to unit test the
4517 name_components matching using a mock mapped_index_base. For each
4518 symbol name that matches, calls MATCH_CALLBACK, passing it the
4519 symbol's index in the mapped_index_base symbol table. */
4520
4521 static void
4522 dw2_expand_symtabs_matching_symbol
4523 (mapped_index_base &index,
4524 const lookup_name_info &lookup_name_in,
4525 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4526 enum search_domain kind,
4527 gdb::function_view<void (offset_type)> match_callback)
4528 {
4529 lookup_name_info lookup_name_without_params
4530 = lookup_name_in.make_ignore_params ();
4531 gdb_index_symbol_name_matcher lookup_name_matcher
4532 (lookup_name_without_params);
4533
4534 /* Build the symbol name component sorted vector, if we haven't
4535 yet. */
4536 index.build_name_components ();
4537
4538 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4539
4540 /* Now for each symbol name in range, check to see if we have a name
4541 match, and if so, call the MATCH_CALLBACK callback. */
4542
4543 /* The same symbol may appear more than once in the range though.
4544 E.g., if we're looking for symbols that complete "w", and we have
4545 a symbol named "w1::w2", we'll find the two name components for
4546 that same symbol in the range. To be sure we only call the
4547 callback once per symbol, we first collect the symbol name
4548 indexes that matched in a temporary vector and ignore
4549 duplicates. */
4550 std::vector<offset_type> matches;
4551 matches.reserve (std::distance (bounds.first, bounds.second));
4552
4553 for (; bounds.first != bounds.second; ++bounds.first)
4554 {
4555 const char *qualified = index.symbol_name_at (bounds.first->idx);
4556
4557 if (!lookup_name_matcher.matches (qualified)
4558 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4559 continue;
4560
4561 matches.push_back (bounds.first->idx);
4562 }
4563
4564 std::sort (matches.begin (), matches.end ());
4565
4566 /* Finally call the callback, once per match. */
4567 ULONGEST prev = -1;
4568 for (offset_type idx : matches)
4569 {
4570 if (prev != idx)
4571 {
4572 match_callback (idx);
4573 prev = idx;
4574 }
4575 }
4576
4577 /* Above we use a type wider than idx's for 'prev', since 0 and
4578 (offset_type)-1 are both possible values. */
4579 static_assert (sizeof (prev) > sizeof (offset_type), "");
4580 }
4581
4582 #if GDB_SELF_TEST
4583
4584 namespace selftests { namespace dw2_expand_symtabs_matching {
4585
4586 /* A mock .gdb_index/.debug_names-like name index table, enough to
4587 exercise dw2_expand_symtabs_matching_symbol, which works with the
4588 mapped_index_base interface. Builds an index from the symbol list
4589 passed as parameter to the constructor. */
4590 class mock_mapped_index : public mapped_index_base
4591 {
4592 public:
4593 mock_mapped_index (gdb::array_view<const char *> symbols)
4594 : m_symbol_table (symbols)
4595 {}
4596
4597 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4598
4599 /* Return the number of names in the symbol table. */
4600 virtual size_t symbol_name_count () const
4601 {
4602 return m_symbol_table.size ();
4603 }
4604
4605 /* Get the name of the symbol at IDX in the symbol table. */
4606 virtual const char *symbol_name_at (offset_type idx) const
4607 {
4608 return m_symbol_table[idx];
4609 }
4610
4611 private:
4612 gdb::array_view<const char *> m_symbol_table;
4613 };
4614
4615 /* Convenience function that converts a NULL pointer to a "<null>"
4616 string, to pass to print routines. */
4617
4618 static const char *
4619 string_or_null (const char *str)
4620 {
4621 return str != NULL ? str : "<null>";
4622 }
4623
4624 /* Check if a lookup_name_info built from
4625 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4626 index. EXPECTED_LIST is the list of expected matches, in expected
4627 matching order. If no match expected, then an empty list is
4628 specified. Returns true on success. On failure prints a warning
4629 indicating the file:line that failed, and returns false. */
4630
4631 static bool
4632 check_match (const char *file, int line,
4633 mock_mapped_index &mock_index,
4634 const char *name, symbol_name_match_type match_type,
4635 bool completion_mode,
4636 std::initializer_list<const char *> expected_list)
4637 {
4638 lookup_name_info lookup_name (name, match_type, completion_mode);
4639
4640 bool matched = true;
4641
4642 auto mismatch = [&] (const char *expected_str,
4643 const char *got)
4644 {
4645 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4646 "expected=\"%s\", got=\"%s\"\n"),
4647 file, line,
4648 (match_type == symbol_name_match_type::FULL
4649 ? "FULL" : "WILD"),
4650 name, string_or_null (expected_str), string_or_null (got));
4651 matched = false;
4652 };
4653
4654 auto expected_it = expected_list.begin ();
4655 auto expected_end = expected_list.end ();
4656
4657 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4658 NULL, ALL_DOMAIN,
4659 [&] (offset_type idx)
4660 {
4661 const char *matched_name = mock_index.symbol_name_at (idx);
4662 const char *expected_str
4663 = expected_it == expected_end ? NULL : *expected_it++;
4664
4665 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4666 mismatch (expected_str, matched_name);
4667 });
4668
4669 const char *expected_str
4670 = expected_it == expected_end ? NULL : *expected_it++;
4671 if (expected_str != NULL)
4672 mismatch (expected_str, NULL);
4673
4674 return matched;
4675 }
4676
4677 /* The symbols added to the mock mapped_index for testing (in
4678 canonical form). */
4679 static const char *test_symbols[] = {
4680 "function",
4681 "std::bar",
4682 "std::zfunction",
4683 "std::zfunction2",
4684 "w1::w2",
4685 "ns::foo<char*>",
4686 "ns::foo<int>",
4687 "ns::foo<long>",
4688 "ns2::tmpl<int>::foo2",
4689 "(anonymous namespace)::A::B::C",
4690
4691 /* These are used to check that the increment-last-char in the
4692 matching algorithm for completion doesn't match "t1_fund" when
4693 completing "t1_func". */
4694 "t1_func",
4695 "t1_func1",
4696 "t1_fund",
4697 "t1_fund1",
4698
4699 /* A UTF-8 name with multi-byte sequences to make sure that
4700 cp-name-parser understands this as a single identifier ("função"
4701 is "function" in PT). */
4702 u8"u8função",
4703
4704 /* \377 (0xff) is Latin1 'ÿ'. */
4705 "yfunc\377",
4706
4707 /* \377 (0xff) is Latin1 'ÿ'. */
4708 "\377",
4709 "\377\377123",
4710
4711 /* A name with all sorts of complications. Starts with "z" to make
4712 it easier for the completion tests below. */
4713 #define Z_SYM_NAME \
4714 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4715 "::tuple<(anonymous namespace)::ui*, " \
4716 "std::default_delete<(anonymous namespace)::ui>, void>"
4717
4718 Z_SYM_NAME
4719 };
4720
4721 /* Returns true if the mapped_index_base::find_name_component_bounds
4722 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4723 in completion mode. */
4724
4725 static bool
4726 check_find_bounds_finds (mapped_index_base &index,
4727 const char *search_name,
4728 gdb::array_view<const char *> expected_syms)
4729 {
4730 lookup_name_info lookup_name (search_name,
4731 symbol_name_match_type::FULL, true);
4732
4733 auto bounds = index.find_name_components_bounds (lookup_name);
4734
4735 size_t distance = std::distance (bounds.first, bounds.second);
4736 if (distance != expected_syms.size ())
4737 return false;
4738
4739 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4740 {
4741 auto nc_elem = bounds.first + exp_elem;
4742 const char *qualified = index.symbol_name_at (nc_elem->idx);
4743 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4744 return false;
4745 }
4746
4747 return true;
4748 }
4749
4750 /* Test the lower-level mapped_index::find_name_component_bounds
4751 method. */
4752
4753 static void
4754 test_mapped_index_find_name_component_bounds ()
4755 {
4756 mock_mapped_index mock_index (test_symbols);
4757
4758 mock_index.build_name_components ();
4759
4760 /* Test the lower-level mapped_index::find_name_component_bounds
4761 method in completion mode. */
4762 {
4763 static const char *expected_syms[] = {
4764 "t1_func",
4765 "t1_func1",
4766 };
4767
4768 SELF_CHECK (check_find_bounds_finds (mock_index,
4769 "t1_func", expected_syms));
4770 }
4771
4772 /* Check that the increment-last-char in the name matching algorithm
4773 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4774 {
4775 static const char *expected_syms1[] = {
4776 "\377",
4777 "\377\377123",
4778 };
4779 SELF_CHECK (check_find_bounds_finds (mock_index,
4780 "\377", expected_syms1));
4781
4782 static const char *expected_syms2[] = {
4783 "\377\377123",
4784 };
4785 SELF_CHECK (check_find_bounds_finds (mock_index,
4786 "\377\377", expected_syms2));
4787 }
4788 }
4789
4790 /* Test dw2_expand_symtabs_matching_symbol. */
4791
4792 static void
4793 test_dw2_expand_symtabs_matching_symbol ()
4794 {
4795 mock_mapped_index mock_index (test_symbols);
4796
4797 /* We let all tests run until the end even if some fails, for debug
4798 convenience. */
4799 bool any_mismatch = false;
4800
4801 /* Create the expected symbols list (an initializer_list). Needed
4802 because lists have commas, and we need to pass them to CHECK,
4803 which is a macro. */
4804 #define EXPECT(...) { __VA_ARGS__ }
4805
4806 /* Wrapper for check_match that passes down the current
4807 __FILE__/__LINE__. */
4808 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4809 any_mismatch |= !check_match (__FILE__, __LINE__, \
4810 mock_index, \
4811 NAME, MATCH_TYPE, COMPLETION_MODE, \
4812 EXPECTED_LIST)
4813
4814 /* Identity checks. */
4815 for (const char *sym : test_symbols)
4816 {
4817 /* Should be able to match all existing symbols. */
4818 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4819 EXPECT (sym));
4820
4821 /* Should be able to match all existing symbols with
4822 parameters. */
4823 std::string with_params = std::string (sym) + "(int)";
4824 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4825 EXPECT (sym));
4826
4827 /* Should be able to match all existing symbols with
4828 parameters and qualifiers. */
4829 with_params = std::string (sym) + " ( int ) const";
4830 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4831 EXPECT (sym));
4832
4833 /* This should really find sym, but cp-name-parser.y doesn't
4834 know about lvalue/rvalue qualifiers yet. */
4835 with_params = std::string (sym) + " ( int ) &&";
4836 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4837 {});
4838 }
4839
4840 /* Check that the name matching algorithm for completion doesn't get
4841 confused with Latin1 'ÿ' / 0xff. */
4842 {
4843 static const char str[] = "\377";
4844 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4845 EXPECT ("\377", "\377\377123"));
4846 }
4847
4848 /* Check that the increment-last-char in the matching algorithm for
4849 completion doesn't match "t1_fund" when completing "t1_func". */
4850 {
4851 static const char str[] = "t1_func";
4852 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4853 EXPECT ("t1_func", "t1_func1"));
4854 }
4855
4856 /* Check that completion mode works at each prefix of the expected
4857 symbol name. */
4858 {
4859 static const char str[] = "function(int)";
4860 size_t len = strlen (str);
4861 std::string lookup;
4862
4863 for (size_t i = 1; i < len; i++)
4864 {
4865 lookup.assign (str, i);
4866 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4867 EXPECT ("function"));
4868 }
4869 }
4870
4871 /* While "w" is a prefix of both components, the match function
4872 should still only be called once. */
4873 {
4874 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4875 EXPECT ("w1::w2"));
4876 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4877 EXPECT ("w1::w2"));
4878 }
4879
4880 /* Same, with a "complicated" symbol. */
4881 {
4882 static const char str[] = Z_SYM_NAME;
4883 size_t len = strlen (str);
4884 std::string lookup;
4885
4886 for (size_t i = 1; i < len; i++)
4887 {
4888 lookup.assign (str, i);
4889 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4890 EXPECT (Z_SYM_NAME));
4891 }
4892 }
4893
4894 /* In FULL mode, an incomplete symbol doesn't match. */
4895 {
4896 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4897 {});
4898 }
4899
4900 /* A complete symbol with parameters matches any overload, since the
4901 index has no overload info. */
4902 {
4903 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4904 EXPECT ("std::zfunction", "std::zfunction2"));
4905 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4906 EXPECT ("std::zfunction", "std::zfunction2"));
4907 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4908 EXPECT ("std::zfunction", "std::zfunction2"));
4909 }
4910
4911 /* Check that whitespace is ignored appropriately. A symbol with a
4912 template argument list. */
4913 {
4914 static const char expected[] = "ns::foo<int>";
4915 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4916 EXPECT (expected));
4917 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4918 EXPECT (expected));
4919 }
4920
4921 /* Check that whitespace is ignored appropriately. A symbol with a
4922 template argument list that includes a pointer. */
4923 {
4924 static const char expected[] = "ns::foo<char*>";
4925 /* Try both completion and non-completion modes. */
4926 static const bool completion_mode[2] = {false, true};
4927 for (size_t i = 0; i < 2; i++)
4928 {
4929 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4930 completion_mode[i], EXPECT (expected));
4931 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4932 completion_mode[i], EXPECT (expected));
4933
4934 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4935 completion_mode[i], EXPECT (expected));
4936 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4937 completion_mode[i], EXPECT (expected));
4938 }
4939 }
4940
4941 {
4942 /* Check method qualifiers are ignored. */
4943 static const char expected[] = "ns::foo<char*>";
4944 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4945 symbol_name_match_type::FULL, true, EXPECT (expected));
4946 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4947 symbol_name_match_type::FULL, true, EXPECT (expected));
4948 CHECK_MATCH ("foo < char * > ( int ) const",
4949 symbol_name_match_type::WILD, true, EXPECT (expected));
4950 CHECK_MATCH ("foo < char * > ( int ) &&",
4951 symbol_name_match_type::WILD, true, EXPECT (expected));
4952 }
4953
4954 /* Test lookup names that don't match anything. */
4955 {
4956 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4957 {});
4958
4959 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4960 {});
4961 }
4962
4963 /* Some wild matching tests, exercising "(anonymous namespace)",
4964 which should not be confused with a parameter list. */
4965 {
4966 static const char *syms[] = {
4967 "A::B::C",
4968 "B::C",
4969 "C",
4970 "A :: B :: C ( int )",
4971 "B :: C ( int )",
4972 "C ( int )",
4973 };
4974
4975 for (const char *s : syms)
4976 {
4977 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4978 EXPECT ("(anonymous namespace)::A::B::C"));
4979 }
4980 }
4981
4982 {
4983 static const char expected[] = "ns2::tmpl<int>::foo2";
4984 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4985 EXPECT (expected));
4986 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4987 EXPECT (expected));
4988 }
4989
4990 SELF_CHECK (!any_mismatch);
4991
4992 #undef EXPECT
4993 #undef CHECK_MATCH
4994 }
4995
4996 static void
4997 run_test ()
4998 {
4999 test_mapped_index_find_name_component_bounds ();
5000 test_dw2_expand_symtabs_matching_symbol ();
5001 }
5002
5003 }} // namespace selftests::dw2_expand_symtabs_matching
5004
5005 #endif /* GDB_SELF_TEST */
5006
5007 /* If FILE_MATCHER is NULL or if PER_CU has
5008 dwarf2_per_cu_quick_data::MARK set (see
5009 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5010 EXPANSION_NOTIFY on it. */
5011
5012 static void
5013 dw2_expand_symtabs_matching_one
5014 (struct dwarf2_per_cu_data *per_cu,
5015 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5016 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5017 {
5018 if (file_matcher == NULL || per_cu->v.quick->mark)
5019 {
5020 bool symtab_was_null
5021 = (per_cu->v.quick->compunit_symtab == NULL);
5022
5023 dw2_instantiate_symtab (per_cu);
5024
5025 if (expansion_notify != NULL
5026 && symtab_was_null
5027 && per_cu->v.quick->compunit_symtab != NULL)
5028 expansion_notify (per_cu->v.quick->compunit_symtab);
5029 }
5030 }
5031
5032 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5033 matched, to expand corresponding CUs that were marked. IDX is the
5034 index of the symbol name that matched. */
5035
5036 static void
5037 dw2_expand_marked_cus
5038 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5039 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5040 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5041 search_domain kind)
5042 {
5043 offset_type *vec, vec_len, vec_idx;
5044 bool global_seen = false;
5045 mapped_index &index = *dwarf2_per_objfile->index_table;
5046
5047 vec = (offset_type *) (index.constant_pool
5048 + MAYBE_SWAP (index.symbol_table[idx].vec));
5049 vec_len = MAYBE_SWAP (vec[0]);
5050 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5051 {
5052 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5053 /* This value is only valid for index versions >= 7. */
5054 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5055 gdb_index_symbol_kind symbol_kind =
5056 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5057 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5058 /* Only check the symbol attributes if they're present.
5059 Indices prior to version 7 don't record them,
5060 and indices >= 7 may elide them for certain symbols
5061 (gold does this). */
5062 int attrs_valid =
5063 (index.version >= 7
5064 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5065
5066 /* Work around gold/15646. */
5067 if (attrs_valid)
5068 {
5069 if (!is_static && global_seen)
5070 continue;
5071 if (!is_static)
5072 global_seen = true;
5073 }
5074
5075 /* Only check the symbol's kind if it has one. */
5076 if (attrs_valid)
5077 {
5078 switch (kind)
5079 {
5080 case VARIABLES_DOMAIN:
5081 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5082 continue;
5083 break;
5084 case FUNCTIONS_DOMAIN:
5085 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5086 continue;
5087 break;
5088 case TYPES_DOMAIN:
5089 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5090 continue;
5091 break;
5092 default:
5093 break;
5094 }
5095 }
5096
5097 /* Don't crash on bad data. */
5098 if (cu_index >= (dwarf2_per_objfile->n_comp_units
5099 + dwarf2_per_objfile->n_type_units))
5100 {
5101 complaint (&symfile_complaints,
5102 _(".gdb_index entry has bad CU index"
5103 " [in module %s]"),
5104 objfile_name (dwarf2_per_objfile->objfile));
5105 continue;
5106 }
5107
5108 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
5109 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5110 expansion_notify);
5111 }
5112 }
5113
5114 /* If FILE_MATCHER is non-NULL, set all the
5115 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5116 that match FILE_MATCHER. */
5117
5118 static void
5119 dw_expand_symtabs_matching_file_matcher
5120 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5121 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5122 {
5123 if (file_matcher == NULL)
5124 return;
5125
5126 objfile *const objfile = dwarf2_per_objfile->objfile;
5127
5128 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5129 htab_eq_pointer,
5130 NULL, xcalloc, xfree));
5131 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5132 htab_eq_pointer,
5133 NULL, xcalloc, xfree));
5134
5135 /* The rule is CUs specify all the files, including those used by
5136 any TU, so there's no need to scan TUs here. */
5137
5138 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5139 {
5140 int j;
5141 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cu (i);
5142 struct quick_file_names *file_data;
5143 void **slot;
5144
5145 QUIT;
5146
5147 per_cu->v.quick->mark = 0;
5148
5149 /* We only need to look at symtabs not already expanded. */
5150 if (per_cu->v.quick->compunit_symtab)
5151 continue;
5152
5153 file_data = dw2_get_file_names (per_cu);
5154 if (file_data == NULL)
5155 continue;
5156
5157 if (htab_find (visited_not_found.get (), file_data) != NULL)
5158 continue;
5159 else if (htab_find (visited_found.get (), file_data) != NULL)
5160 {
5161 per_cu->v.quick->mark = 1;
5162 continue;
5163 }
5164
5165 for (j = 0; j < file_data->num_file_names; ++j)
5166 {
5167 const char *this_real_name;
5168
5169 if (file_matcher (file_data->file_names[j], false))
5170 {
5171 per_cu->v.quick->mark = 1;
5172 break;
5173 }
5174
5175 /* Before we invoke realpath, which can get expensive when many
5176 files are involved, do a quick comparison of the basenames. */
5177 if (!basenames_may_differ
5178 && !file_matcher (lbasename (file_data->file_names[j]),
5179 true))
5180 continue;
5181
5182 this_real_name = dw2_get_real_path (objfile, file_data, j);
5183 if (file_matcher (this_real_name, false))
5184 {
5185 per_cu->v.quick->mark = 1;
5186 break;
5187 }
5188 }
5189
5190 slot = htab_find_slot (per_cu->v.quick->mark
5191 ? visited_found.get ()
5192 : visited_not_found.get (),
5193 file_data, INSERT);
5194 *slot = file_data;
5195 }
5196 }
5197
5198 static void
5199 dw2_expand_symtabs_matching
5200 (struct objfile *objfile,
5201 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5202 const lookup_name_info &lookup_name,
5203 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5204 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5205 enum search_domain kind)
5206 {
5207 struct dwarf2_per_objfile *dwarf2_per_objfile
5208 = get_dwarf2_per_objfile (objfile);
5209
5210 /* index_table is NULL if OBJF_READNOW. */
5211 if (!dwarf2_per_objfile->index_table)
5212 return;
5213
5214 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5215
5216 mapped_index &index = *dwarf2_per_objfile->index_table;
5217
5218 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5219 symbol_matcher,
5220 kind, [&] (offset_type idx)
5221 {
5222 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5223 expansion_notify, kind);
5224 });
5225 }
5226
5227 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5228 symtab. */
5229
5230 static struct compunit_symtab *
5231 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5232 CORE_ADDR pc)
5233 {
5234 int i;
5235
5236 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5237 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5238 return cust;
5239
5240 if (cust->includes == NULL)
5241 return NULL;
5242
5243 for (i = 0; cust->includes[i]; ++i)
5244 {
5245 struct compunit_symtab *s = cust->includes[i];
5246
5247 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5248 if (s != NULL)
5249 return s;
5250 }
5251
5252 return NULL;
5253 }
5254
5255 static struct compunit_symtab *
5256 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5257 struct bound_minimal_symbol msymbol,
5258 CORE_ADDR pc,
5259 struct obj_section *section,
5260 int warn_if_readin)
5261 {
5262 struct dwarf2_per_cu_data *data;
5263 struct compunit_symtab *result;
5264
5265 if (!objfile->psymtabs_addrmap)
5266 return NULL;
5267
5268 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5269 pc);
5270 if (!data)
5271 return NULL;
5272
5273 if (warn_if_readin && data->v.quick->compunit_symtab)
5274 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5275 paddress (get_objfile_arch (objfile), pc));
5276
5277 result
5278 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5279 pc);
5280 gdb_assert (result != NULL);
5281 return result;
5282 }
5283
5284 static void
5285 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5286 void *data, int need_fullname)
5287 {
5288 struct dwarf2_per_objfile *dwarf2_per_objfile
5289 = get_dwarf2_per_objfile (objfile);
5290
5291 if (!dwarf2_per_objfile->filenames_cache)
5292 {
5293 dwarf2_per_objfile->filenames_cache.emplace ();
5294
5295 htab_up visited (htab_create_alloc (10,
5296 htab_hash_pointer, htab_eq_pointer,
5297 NULL, xcalloc, xfree));
5298
5299 /* The rule is CUs specify all the files, including those used
5300 by any TU, so there's no need to scan TUs here. We can
5301 ignore file names coming from already-expanded CUs. */
5302
5303 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5304 {
5305 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cu (i);
5306
5307 if (per_cu->v.quick->compunit_symtab)
5308 {
5309 void **slot = htab_find_slot (visited.get (),
5310 per_cu->v.quick->file_names,
5311 INSERT);
5312
5313 *slot = per_cu->v.quick->file_names;
5314 }
5315 }
5316
5317 for (int i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5318 {
5319 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cu (i);
5320 struct quick_file_names *file_data;
5321 void **slot;
5322
5323 /* We only need to look at symtabs not already expanded. */
5324 if (per_cu->v.quick->compunit_symtab)
5325 continue;
5326
5327 file_data = dw2_get_file_names (per_cu);
5328 if (file_data == NULL)
5329 continue;
5330
5331 slot = htab_find_slot (visited.get (), file_data, INSERT);
5332 if (*slot)
5333 {
5334 /* Already visited. */
5335 continue;
5336 }
5337 *slot = file_data;
5338
5339 for (int j = 0; j < file_data->num_file_names; ++j)
5340 {
5341 const char *filename = file_data->file_names[j];
5342 dwarf2_per_objfile->filenames_cache->seen (filename);
5343 }
5344 }
5345 }
5346
5347 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5348 {
5349 gdb::unique_xmalloc_ptr<char> this_real_name;
5350
5351 if (need_fullname)
5352 this_real_name = gdb_realpath (filename);
5353 (*fun) (filename, this_real_name.get (), data);
5354 });
5355 }
5356
5357 static int
5358 dw2_has_symbols (struct objfile *objfile)
5359 {
5360 return 1;
5361 }
5362
5363 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5364 {
5365 dw2_has_symbols,
5366 dw2_find_last_source_symtab,
5367 dw2_forget_cached_source_info,
5368 dw2_map_symtabs_matching_filename,
5369 dw2_lookup_symbol,
5370 dw2_print_stats,
5371 dw2_dump,
5372 dw2_relocate,
5373 dw2_expand_symtabs_for_function,
5374 dw2_expand_all_symtabs,
5375 dw2_expand_symtabs_with_fullname,
5376 dw2_map_matching_symbols,
5377 dw2_expand_symtabs_matching,
5378 dw2_find_pc_sect_compunit_symtab,
5379 NULL,
5380 dw2_map_symbol_filenames
5381 };
5382
5383 /* DWARF-5 debug_names reader. */
5384
5385 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5386 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5387
5388 /* A helper function that reads the .debug_names section in SECTION
5389 and fills in MAP. FILENAME is the name of the file containing the
5390 section; it is used for error reporting.
5391
5392 Returns true if all went well, false otherwise. */
5393
5394 static bool
5395 read_debug_names_from_section (struct objfile *objfile,
5396 const char *filename,
5397 struct dwarf2_section_info *section,
5398 mapped_debug_names &map)
5399 {
5400 if (dwarf2_section_empty_p (section))
5401 return false;
5402
5403 /* Older elfutils strip versions could keep the section in the main
5404 executable while splitting it for the separate debug info file. */
5405 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5406 return false;
5407
5408 dwarf2_read_section (objfile, section);
5409
5410 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5411
5412 const gdb_byte *addr = section->buffer;
5413
5414 bfd *const abfd = get_section_bfd_owner (section);
5415
5416 unsigned int bytes_read;
5417 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5418 addr += bytes_read;
5419
5420 map.dwarf5_is_dwarf64 = bytes_read != 4;
5421 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5422 if (bytes_read + length != section->size)
5423 {
5424 /* There may be multiple per-CU indices. */
5425 warning (_("Section .debug_names in %s length %s does not match "
5426 "section length %s, ignoring .debug_names."),
5427 filename, plongest (bytes_read + length),
5428 pulongest (section->size));
5429 return false;
5430 }
5431
5432 /* The version number. */
5433 uint16_t version = read_2_bytes (abfd, addr);
5434 addr += 2;
5435 if (version != 5)
5436 {
5437 warning (_("Section .debug_names in %s has unsupported version %d, "
5438 "ignoring .debug_names."),
5439 filename, version);
5440 return false;
5441 }
5442
5443 /* Padding. */
5444 uint16_t padding = read_2_bytes (abfd, addr);
5445 addr += 2;
5446 if (padding != 0)
5447 {
5448 warning (_("Section .debug_names in %s has unsupported padding %d, "
5449 "ignoring .debug_names."),
5450 filename, padding);
5451 return false;
5452 }
5453
5454 /* comp_unit_count - The number of CUs in the CU list. */
5455 map.cu_count = read_4_bytes (abfd, addr);
5456 addr += 4;
5457
5458 /* local_type_unit_count - The number of TUs in the local TU
5459 list. */
5460 map.tu_count = read_4_bytes (abfd, addr);
5461 addr += 4;
5462
5463 /* foreign_type_unit_count - The number of TUs in the foreign TU
5464 list. */
5465 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5466 addr += 4;
5467 if (foreign_tu_count != 0)
5468 {
5469 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5470 "ignoring .debug_names."),
5471 filename, static_cast<unsigned long> (foreign_tu_count));
5472 return false;
5473 }
5474
5475 /* bucket_count - The number of hash buckets in the hash lookup
5476 table. */
5477 map.bucket_count = read_4_bytes (abfd, addr);
5478 addr += 4;
5479
5480 /* name_count - The number of unique names in the index. */
5481 map.name_count = read_4_bytes (abfd, addr);
5482 addr += 4;
5483
5484 /* abbrev_table_size - The size in bytes of the abbreviations
5485 table. */
5486 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5487 addr += 4;
5488
5489 /* augmentation_string_size - The size in bytes of the augmentation
5490 string. This value is rounded up to a multiple of 4. */
5491 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5492 addr += 4;
5493 map.augmentation_is_gdb = ((augmentation_string_size
5494 == sizeof (dwarf5_augmentation))
5495 && memcmp (addr, dwarf5_augmentation,
5496 sizeof (dwarf5_augmentation)) == 0);
5497 augmentation_string_size += (-augmentation_string_size) & 3;
5498 addr += augmentation_string_size;
5499
5500 /* List of CUs */
5501 map.cu_table_reordered = addr;
5502 addr += map.cu_count * map.offset_size;
5503
5504 /* List of Local TUs */
5505 map.tu_table_reordered = addr;
5506 addr += map.tu_count * map.offset_size;
5507
5508 /* Hash Lookup Table */
5509 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5510 addr += map.bucket_count * 4;
5511 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5512 addr += map.name_count * 4;
5513
5514 /* Name Table */
5515 map.name_table_string_offs_reordered = addr;
5516 addr += map.name_count * map.offset_size;
5517 map.name_table_entry_offs_reordered = addr;
5518 addr += map.name_count * map.offset_size;
5519
5520 const gdb_byte *abbrev_table_start = addr;
5521 for (;;)
5522 {
5523 unsigned int bytes_read;
5524 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5525 addr += bytes_read;
5526 if (index_num == 0)
5527 break;
5528
5529 const auto insertpair
5530 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5531 if (!insertpair.second)
5532 {
5533 warning (_("Section .debug_names in %s has duplicate index %s, "
5534 "ignoring .debug_names."),
5535 filename, pulongest (index_num));
5536 return false;
5537 }
5538 mapped_debug_names::index_val &indexval = insertpair.first->second;
5539 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5540 addr += bytes_read;
5541
5542 for (;;)
5543 {
5544 mapped_debug_names::index_val::attr attr;
5545 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5546 addr += bytes_read;
5547 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5548 addr += bytes_read;
5549 if (attr.form == DW_FORM_implicit_const)
5550 {
5551 attr.implicit_const = read_signed_leb128 (abfd, addr,
5552 &bytes_read);
5553 addr += bytes_read;
5554 }
5555 if (attr.dw_idx == 0 && attr.form == 0)
5556 break;
5557 indexval.attr_vec.push_back (std::move (attr));
5558 }
5559 }
5560 if (addr != abbrev_table_start + abbrev_table_size)
5561 {
5562 warning (_("Section .debug_names in %s has abbreviation_table "
5563 "of size %zu vs. written as %u, ignoring .debug_names."),
5564 filename, addr - abbrev_table_start, abbrev_table_size);
5565 return false;
5566 }
5567 map.entry_pool = addr;
5568
5569 return true;
5570 }
5571
5572 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5573 list. */
5574
5575 static void
5576 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5577 const mapped_debug_names &map,
5578 dwarf2_section_info &section,
5579 bool is_dwz, int base_offset)
5580 {
5581 sect_offset sect_off_prev;
5582 for (uint32_t i = 0; i <= map.cu_count; ++i)
5583 {
5584 sect_offset sect_off_next;
5585 if (i < map.cu_count)
5586 {
5587 sect_off_next
5588 = (sect_offset) (extract_unsigned_integer
5589 (map.cu_table_reordered + i * map.offset_size,
5590 map.offset_size,
5591 map.dwarf5_byte_order));
5592 }
5593 else
5594 sect_off_next = (sect_offset) section.size;
5595 if (i >= 1)
5596 {
5597 const ULONGEST length = sect_off_next - sect_off_prev;
5598 dwarf2_per_objfile->all_comp_units[base_offset + (i - 1)]
5599 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5600 sect_off_prev, length);
5601 }
5602 sect_off_prev = sect_off_next;
5603 }
5604 }
5605
5606 /* Read the CU list from the mapped index, and use it to create all
5607 the CU objects for this dwarf2_per_objfile. */
5608
5609 static void
5610 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5611 const mapped_debug_names &map,
5612 const mapped_debug_names &dwz_map)
5613 {
5614 struct objfile *objfile = dwarf2_per_objfile->objfile;
5615
5616 dwarf2_per_objfile->n_comp_units = map.cu_count + dwz_map.cu_count;
5617 dwarf2_per_objfile->all_comp_units
5618 = XOBNEWVEC (&objfile->objfile_obstack, struct dwarf2_per_cu_data *,
5619 dwarf2_per_objfile->n_comp_units);
5620
5621 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5622 dwarf2_per_objfile->info,
5623 false /* is_dwz */,
5624 0 /* base_offset */);
5625
5626 if (dwz_map.cu_count == 0)
5627 return;
5628
5629 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5630 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5631 true /* is_dwz */,
5632 map.cu_count /* base_offset */);
5633 }
5634
5635 /* Read .debug_names. If everything went ok, initialize the "quick"
5636 elements of all the CUs and return true. Otherwise, return false. */
5637
5638 static bool
5639 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5640 {
5641 mapped_debug_names local_map (dwarf2_per_objfile);
5642 mapped_debug_names dwz_map (dwarf2_per_objfile);
5643 struct objfile *objfile = dwarf2_per_objfile->objfile;
5644
5645 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5646 &dwarf2_per_objfile->debug_names,
5647 local_map))
5648 return false;
5649
5650 /* Don't use the index if it's empty. */
5651 if (local_map.name_count == 0)
5652 return false;
5653
5654 /* If there is a .dwz file, read it so we can get its CU list as
5655 well. */
5656 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5657 if (dwz != NULL)
5658 {
5659 if (!read_debug_names_from_section (objfile,
5660 bfd_get_filename (dwz->dwz_bfd),
5661 &dwz->debug_names, dwz_map))
5662 {
5663 warning (_("could not read '.debug_names' section from %s; skipping"),
5664 bfd_get_filename (dwz->dwz_bfd));
5665 return false;
5666 }
5667 }
5668
5669 create_cus_from_debug_names (dwarf2_per_objfile, local_map, dwz_map);
5670
5671 if (local_map.tu_count != 0)
5672 {
5673 /* We can only handle a single .debug_types when we have an
5674 index. */
5675 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5676 return false;
5677
5678 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5679 dwarf2_per_objfile->types, 0);
5680
5681 create_signatured_type_table_from_debug_names
5682 (dwarf2_per_objfile, local_map, section, &dwarf2_per_objfile->abbrev);
5683 }
5684
5685 create_addrmap_from_aranges (dwarf2_per_objfile,
5686 &dwarf2_per_objfile->debug_aranges);
5687
5688 dwarf2_per_objfile->debug_names_table.reset
5689 (new mapped_debug_names (dwarf2_per_objfile));
5690 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
5691 dwarf2_per_objfile->using_index = 1;
5692 dwarf2_per_objfile->quick_file_names_table =
5693 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
5694
5695 return true;
5696 }
5697
5698 /* Type used to manage iterating over all CUs looking for a symbol for
5699 .debug_names. */
5700
5701 class dw2_debug_names_iterator
5702 {
5703 public:
5704 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
5705 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
5706 dw2_debug_names_iterator (const mapped_debug_names &map,
5707 bool want_specific_block,
5708 block_enum block_index, domain_enum domain,
5709 const char *name)
5710 : m_map (map), m_want_specific_block (want_specific_block),
5711 m_block_index (block_index), m_domain (domain),
5712 m_addr (find_vec_in_debug_names (map, name))
5713 {}
5714
5715 dw2_debug_names_iterator (const mapped_debug_names &map,
5716 search_domain search, uint32_t namei)
5717 : m_map (map),
5718 m_search (search),
5719 m_addr (find_vec_in_debug_names (map, namei))
5720 {}
5721
5722 /* Return the next matching CU or NULL if there are no more. */
5723 dwarf2_per_cu_data *next ();
5724
5725 private:
5726 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5727 const char *name);
5728 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5729 uint32_t namei);
5730
5731 /* The internalized form of .debug_names. */
5732 const mapped_debug_names &m_map;
5733
5734 /* If true, only look for symbols that match BLOCK_INDEX. */
5735 const bool m_want_specific_block = false;
5736
5737 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
5738 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
5739 value. */
5740 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
5741
5742 /* The kind of symbol we're looking for. */
5743 const domain_enum m_domain = UNDEF_DOMAIN;
5744 const search_domain m_search = ALL_DOMAIN;
5745
5746 /* The list of CUs from the index entry of the symbol, or NULL if
5747 not found. */
5748 const gdb_byte *m_addr;
5749 };
5750
5751 const char *
5752 mapped_debug_names::namei_to_name (uint32_t namei) const
5753 {
5754 const ULONGEST namei_string_offs
5755 = extract_unsigned_integer ((name_table_string_offs_reordered
5756 + namei * offset_size),
5757 offset_size,
5758 dwarf5_byte_order);
5759 return read_indirect_string_at_offset
5760 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5761 }
5762
5763 /* Find a slot in .debug_names for the object named NAME. If NAME is
5764 found, return pointer to its pool data. If NAME cannot be found,
5765 return NULL. */
5766
5767 const gdb_byte *
5768 dw2_debug_names_iterator::find_vec_in_debug_names
5769 (const mapped_debug_names &map, const char *name)
5770 {
5771 int (*cmp) (const char *, const char *);
5772
5773 if (current_language->la_language == language_cplus
5774 || current_language->la_language == language_fortran
5775 || current_language->la_language == language_d)
5776 {
5777 /* NAME is already canonical. Drop any qualifiers as
5778 .debug_names does not contain any. */
5779
5780 if (strchr (name, '(') != NULL)
5781 {
5782 gdb::unique_xmalloc_ptr<char> without_params
5783 = cp_remove_params (name);
5784
5785 if (without_params != NULL)
5786 {
5787 name = without_params.get();
5788 }
5789 }
5790 }
5791
5792 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5793
5794 const uint32_t full_hash = dwarf5_djb_hash (name);
5795 uint32_t namei
5796 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5797 (map.bucket_table_reordered
5798 + (full_hash % map.bucket_count)), 4,
5799 map.dwarf5_byte_order);
5800 if (namei == 0)
5801 return NULL;
5802 --namei;
5803 if (namei >= map.name_count)
5804 {
5805 complaint (&symfile_complaints,
5806 _("Wrong .debug_names with name index %u but name_count=%u "
5807 "[in module %s]"),
5808 namei, map.name_count,
5809 objfile_name (map.dwarf2_per_objfile->objfile));
5810 return NULL;
5811 }
5812
5813 for (;;)
5814 {
5815 const uint32_t namei_full_hash
5816 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5817 (map.hash_table_reordered + namei), 4,
5818 map.dwarf5_byte_order);
5819 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5820 return NULL;
5821
5822 if (full_hash == namei_full_hash)
5823 {
5824 const char *const namei_string = map.namei_to_name (namei);
5825
5826 #if 0 /* An expensive sanity check. */
5827 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5828 {
5829 complaint (&symfile_complaints,
5830 _("Wrong .debug_names hash for string at index %u "
5831 "[in module %s]"),
5832 namei, objfile_name (dwarf2_per_objfile->objfile));
5833 return NULL;
5834 }
5835 #endif
5836
5837 if (cmp (namei_string, name) == 0)
5838 {
5839 const ULONGEST namei_entry_offs
5840 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5841 + namei * map.offset_size),
5842 map.offset_size, map.dwarf5_byte_order);
5843 return map.entry_pool + namei_entry_offs;
5844 }
5845 }
5846
5847 ++namei;
5848 if (namei >= map.name_count)
5849 return NULL;
5850 }
5851 }
5852
5853 const gdb_byte *
5854 dw2_debug_names_iterator::find_vec_in_debug_names
5855 (const mapped_debug_names &map, uint32_t namei)
5856 {
5857 if (namei >= map.name_count)
5858 {
5859 complaint (&symfile_complaints,
5860 _("Wrong .debug_names with name index %u but name_count=%u "
5861 "[in module %s]"),
5862 namei, map.name_count,
5863 objfile_name (map.dwarf2_per_objfile->objfile));
5864 return NULL;
5865 }
5866
5867 const ULONGEST namei_entry_offs
5868 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5869 + namei * map.offset_size),
5870 map.offset_size, map.dwarf5_byte_order);
5871 return map.entry_pool + namei_entry_offs;
5872 }
5873
5874 /* See dw2_debug_names_iterator. */
5875
5876 dwarf2_per_cu_data *
5877 dw2_debug_names_iterator::next ()
5878 {
5879 if (m_addr == NULL)
5880 return NULL;
5881
5882 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5883 struct objfile *objfile = dwarf2_per_objfile->objfile;
5884 bfd *const abfd = objfile->obfd;
5885
5886 again:
5887
5888 unsigned int bytes_read;
5889 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5890 m_addr += bytes_read;
5891 if (abbrev == 0)
5892 return NULL;
5893
5894 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5895 if (indexval_it == m_map.abbrev_map.cend ())
5896 {
5897 complaint (&symfile_complaints,
5898 _("Wrong .debug_names undefined abbrev code %s "
5899 "[in module %s]"),
5900 pulongest (abbrev), objfile_name (objfile));
5901 return NULL;
5902 }
5903 const mapped_debug_names::index_val &indexval = indexval_it->second;
5904 bool have_is_static = false;
5905 bool is_static;
5906 dwarf2_per_cu_data *per_cu = NULL;
5907 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5908 {
5909 ULONGEST ull;
5910 switch (attr.form)
5911 {
5912 case DW_FORM_implicit_const:
5913 ull = attr.implicit_const;
5914 break;
5915 case DW_FORM_flag_present:
5916 ull = 1;
5917 break;
5918 case DW_FORM_udata:
5919 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5920 m_addr += bytes_read;
5921 break;
5922 default:
5923 complaint (&symfile_complaints,
5924 _("Unsupported .debug_names form %s [in module %s]"),
5925 dwarf_form_name (attr.form),
5926 objfile_name (objfile));
5927 return NULL;
5928 }
5929 switch (attr.dw_idx)
5930 {
5931 case DW_IDX_compile_unit:
5932 /* Don't crash on bad data. */
5933 if (ull >= dwarf2_per_objfile->n_comp_units)
5934 {
5935 complaint (&symfile_complaints,
5936 _(".debug_names entry has bad CU index %s"
5937 " [in module %s]"),
5938 pulongest (ull),
5939 objfile_name (dwarf2_per_objfile->objfile));
5940 continue;
5941 }
5942 per_cu = dwarf2_per_objfile->get_cutu (ull);
5943 break;
5944 case DW_IDX_type_unit:
5945 /* Don't crash on bad data. */
5946 if (ull >= dwarf2_per_objfile->n_type_units)
5947 {
5948 complaint (&symfile_complaints,
5949 _(".debug_names entry has bad TU index %s"
5950 " [in module %s]"),
5951 pulongest (ull),
5952 objfile_name (dwarf2_per_objfile->objfile));
5953 continue;
5954 }
5955 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5956 break;
5957 case DW_IDX_GNU_internal:
5958 if (!m_map.augmentation_is_gdb)
5959 break;
5960 have_is_static = true;
5961 is_static = true;
5962 break;
5963 case DW_IDX_GNU_external:
5964 if (!m_map.augmentation_is_gdb)
5965 break;
5966 have_is_static = true;
5967 is_static = false;
5968 break;
5969 }
5970 }
5971
5972 /* Skip if already read in. */
5973 if (per_cu->v.quick->compunit_symtab)
5974 goto again;
5975
5976 /* Check static vs global. */
5977 if (have_is_static)
5978 {
5979 const bool want_static = m_block_index != GLOBAL_BLOCK;
5980 if (m_want_specific_block && want_static != is_static)
5981 goto again;
5982 }
5983
5984 /* Match dw2_symtab_iter_next, symbol_kind
5985 and debug_names::psymbol_tag. */
5986 switch (m_domain)
5987 {
5988 case VAR_DOMAIN:
5989 switch (indexval.dwarf_tag)
5990 {
5991 case DW_TAG_variable:
5992 case DW_TAG_subprogram:
5993 /* Some types are also in VAR_DOMAIN. */
5994 case DW_TAG_typedef:
5995 case DW_TAG_structure_type:
5996 break;
5997 default:
5998 goto again;
5999 }
6000 break;
6001 case STRUCT_DOMAIN:
6002 switch (indexval.dwarf_tag)
6003 {
6004 case DW_TAG_typedef:
6005 case DW_TAG_structure_type:
6006 break;
6007 default:
6008 goto again;
6009 }
6010 break;
6011 case LABEL_DOMAIN:
6012 switch (indexval.dwarf_tag)
6013 {
6014 case 0:
6015 case DW_TAG_variable:
6016 break;
6017 default:
6018 goto again;
6019 }
6020 break;
6021 default:
6022 break;
6023 }
6024
6025 /* Match dw2_expand_symtabs_matching, symbol_kind and
6026 debug_names::psymbol_tag. */
6027 switch (m_search)
6028 {
6029 case VARIABLES_DOMAIN:
6030 switch (indexval.dwarf_tag)
6031 {
6032 case DW_TAG_variable:
6033 break;
6034 default:
6035 goto again;
6036 }
6037 break;
6038 case FUNCTIONS_DOMAIN:
6039 switch (indexval.dwarf_tag)
6040 {
6041 case DW_TAG_subprogram:
6042 break;
6043 default:
6044 goto again;
6045 }
6046 break;
6047 case TYPES_DOMAIN:
6048 switch (indexval.dwarf_tag)
6049 {
6050 case DW_TAG_typedef:
6051 case DW_TAG_structure_type:
6052 break;
6053 default:
6054 goto again;
6055 }
6056 break;
6057 default:
6058 break;
6059 }
6060
6061 return per_cu;
6062 }
6063
6064 static struct compunit_symtab *
6065 dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6066 const char *name, domain_enum domain)
6067 {
6068 const block_enum block_index = static_cast<block_enum> (block_index_int);
6069 struct dwarf2_per_objfile *dwarf2_per_objfile
6070 = get_dwarf2_per_objfile (objfile);
6071
6072 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6073 if (!mapp)
6074 {
6075 /* index is NULL if OBJF_READNOW. */
6076 return NULL;
6077 }
6078 const auto &map = *mapp;
6079
6080 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6081 block_index, domain, name);
6082
6083 struct compunit_symtab *stab_best = NULL;
6084 struct dwarf2_per_cu_data *per_cu;
6085 while ((per_cu = iter.next ()) != NULL)
6086 {
6087 struct symbol *sym, *with_opaque = NULL;
6088 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6089 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6090 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6091
6092 sym = block_find_symbol (block, name, domain,
6093 block_find_non_opaque_type_preferred,
6094 &with_opaque);
6095
6096 /* Some caution must be observed with overloaded functions and
6097 methods, since the index will not contain any overload
6098 information (but NAME might contain it). */
6099
6100 if (sym != NULL
6101 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6102 return stab;
6103 if (with_opaque != NULL
6104 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6105 stab_best = stab;
6106
6107 /* Keep looking through other CUs. */
6108 }
6109
6110 return stab_best;
6111 }
6112
6113 /* This dumps minimal information about .debug_names. It is called
6114 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6115 uses this to verify that .debug_names has been loaded. */
6116
6117 static void
6118 dw2_debug_names_dump (struct objfile *objfile)
6119 {
6120 struct dwarf2_per_objfile *dwarf2_per_objfile
6121 = get_dwarf2_per_objfile (objfile);
6122
6123 gdb_assert (dwarf2_per_objfile->using_index);
6124 printf_filtered (".debug_names:");
6125 if (dwarf2_per_objfile->debug_names_table)
6126 printf_filtered (" exists\n");
6127 else
6128 printf_filtered (" faked for \"readnow\"\n");
6129 printf_filtered ("\n");
6130 }
6131
6132 static void
6133 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6134 const char *func_name)
6135 {
6136 struct dwarf2_per_objfile *dwarf2_per_objfile
6137 = get_dwarf2_per_objfile (objfile);
6138
6139 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6140 if (dwarf2_per_objfile->debug_names_table)
6141 {
6142 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6143
6144 /* Note: It doesn't matter what we pass for block_index here. */
6145 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6146 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
6147
6148 struct dwarf2_per_cu_data *per_cu;
6149 while ((per_cu = iter.next ()) != NULL)
6150 dw2_instantiate_symtab (per_cu);
6151 }
6152 }
6153
6154 static void
6155 dw2_debug_names_expand_symtabs_matching
6156 (struct objfile *objfile,
6157 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6158 const lookup_name_info &lookup_name,
6159 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6160 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6161 enum search_domain kind)
6162 {
6163 struct dwarf2_per_objfile *dwarf2_per_objfile
6164 = get_dwarf2_per_objfile (objfile);
6165
6166 /* debug_names_table is NULL if OBJF_READNOW. */
6167 if (!dwarf2_per_objfile->debug_names_table)
6168 return;
6169
6170 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6171
6172 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6173
6174 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6175 symbol_matcher,
6176 kind, [&] (offset_type namei)
6177 {
6178 /* The name was matched, now expand corresponding CUs that were
6179 marked. */
6180 dw2_debug_names_iterator iter (map, kind, namei);
6181
6182 struct dwarf2_per_cu_data *per_cu;
6183 while ((per_cu = iter.next ()) != NULL)
6184 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6185 expansion_notify);
6186 });
6187 }
6188
6189 const struct quick_symbol_functions dwarf2_debug_names_functions =
6190 {
6191 dw2_has_symbols,
6192 dw2_find_last_source_symtab,
6193 dw2_forget_cached_source_info,
6194 dw2_map_symtabs_matching_filename,
6195 dw2_debug_names_lookup_symbol,
6196 dw2_print_stats,
6197 dw2_debug_names_dump,
6198 dw2_relocate,
6199 dw2_debug_names_expand_symtabs_for_function,
6200 dw2_expand_all_symtabs,
6201 dw2_expand_symtabs_with_fullname,
6202 dw2_map_matching_symbols,
6203 dw2_debug_names_expand_symtabs_matching,
6204 dw2_find_pc_sect_compunit_symtab,
6205 NULL,
6206 dw2_map_symbol_filenames
6207 };
6208
6209 /* See symfile.h. */
6210
6211 bool
6212 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6213 {
6214 struct dwarf2_per_objfile *dwarf2_per_objfile
6215 = get_dwarf2_per_objfile (objfile);
6216
6217 /* If we're about to read full symbols, don't bother with the
6218 indices. In this case we also don't care if some other debug
6219 format is making psymtabs, because they are all about to be
6220 expanded anyway. */
6221 if ((objfile->flags & OBJF_READNOW))
6222 {
6223 int i;
6224
6225 dwarf2_per_objfile->using_index = 1;
6226 create_all_comp_units (dwarf2_per_objfile);
6227 create_all_type_units (dwarf2_per_objfile);
6228 dwarf2_per_objfile->quick_file_names_table =
6229 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
6230
6231 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
6232 + dwarf2_per_objfile->n_type_units); ++i)
6233 {
6234 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6235
6236 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6237 struct dwarf2_per_cu_quick_data);
6238 }
6239
6240 /* Return 1 so that gdb sees the "quick" functions. However,
6241 these functions will be no-ops because we will have expanded
6242 all symtabs. */
6243 *index_kind = dw_index_kind::GDB_INDEX;
6244 return true;
6245 }
6246
6247 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6248 {
6249 *index_kind = dw_index_kind::DEBUG_NAMES;
6250 return true;
6251 }
6252
6253 if (dwarf2_read_index (dwarf2_per_objfile))
6254 {
6255 *index_kind = dw_index_kind::GDB_INDEX;
6256 return true;
6257 }
6258
6259 return false;
6260 }
6261
6262 \f
6263
6264 /* Build a partial symbol table. */
6265
6266 void
6267 dwarf2_build_psymtabs (struct objfile *objfile)
6268 {
6269 struct dwarf2_per_objfile *dwarf2_per_objfile
6270 = get_dwarf2_per_objfile (objfile);
6271
6272 if (objfile->global_psymbols.capacity () == 0
6273 && objfile->static_psymbols.capacity () == 0)
6274 init_psymbol_list (objfile, 1024);
6275
6276 TRY
6277 {
6278 /* This isn't really ideal: all the data we allocate on the
6279 objfile's obstack is still uselessly kept around. However,
6280 freeing it seems unsafe. */
6281 psymtab_discarder psymtabs (objfile);
6282 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6283 psymtabs.keep ();
6284 }
6285 CATCH (except, RETURN_MASK_ERROR)
6286 {
6287 exception_print (gdb_stderr, except);
6288 }
6289 END_CATCH
6290 }
6291
6292 /* Return the total length of the CU described by HEADER. */
6293
6294 static unsigned int
6295 get_cu_length (const struct comp_unit_head *header)
6296 {
6297 return header->initial_length_size + header->length;
6298 }
6299
6300 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6301
6302 static inline bool
6303 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6304 {
6305 sect_offset bottom = cu_header->sect_off;
6306 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6307
6308 return sect_off >= bottom && sect_off < top;
6309 }
6310
6311 /* Find the base address of the compilation unit for range lists and
6312 location lists. It will normally be specified by DW_AT_low_pc.
6313 In DWARF-3 draft 4, the base address could be overridden by
6314 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6315 compilation units with discontinuous ranges. */
6316
6317 static void
6318 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6319 {
6320 struct attribute *attr;
6321
6322 cu->base_known = 0;
6323 cu->base_address = 0;
6324
6325 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6326 if (attr)
6327 {
6328 cu->base_address = attr_value_as_address (attr);
6329 cu->base_known = 1;
6330 }
6331 else
6332 {
6333 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6334 if (attr)
6335 {
6336 cu->base_address = attr_value_as_address (attr);
6337 cu->base_known = 1;
6338 }
6339 }
6340 }
6341
6342 /* Read in the comp unit header information from the debug_info at info_ptr.
6343 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6344 NOTE: This leaves members offset, first_die_offset to be filled in
6345 by the caller. */
6346
6347 static const gdb_byte *
6348 read_comp_unit_head (struct comp_unit_head *cu_header,
6349 const gdb_byte *info_ptr,
6350 struct dwarf2_section_info *section,
6351 rcuh_kind section_kind)
6352 {
6353 int signed_addr;
6354 unsigned int bytes_read;
6355 const char *filename = get_section_file_name (section);
6356 bfd *abfd = get_section_bfd_owner (section);
6357
6358 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6359 cu_header->initial_length_size = bytes_read;
6360 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6361 info_ptr += bytes_read;
6362 cu_header->version = read_2_bytes (abfd, info_ptr);
6363 info_ptr += 2;
6364 if (cu_header->version < 5)
6365 switch (section_kind)
6366 {
6367 case rcuh_kind::COMPILE:
6368 cu_header->unit_type = DW_UT_compile;
6369 break;
6370 case rcuh_kind::TYPE:
6371 cu_header->unit_type = DW_UT_type;
6372 break;
6373 default:
6374 internal_error (__FILE__, __LINE__,
6375 _("read_comp_unit_head: invalid section_kind"));
6376 }
6377 else
6378 {
6379 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6380 (read_1_byte (abfd, info_ptr));
6381 info_ptr += 1;
6382 switch (cu_header->unit_type)
6383 {
6384 case DW_UT_compile:
6385 if (section_kind != rcuh_kind::COMPILE)
6386 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6387 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6388 filename);
6389 break;
6390 case DW_UT_type:
6391 section_kind = rcuh_kind::TYPE;
6392 break;
6393 default:
6394 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6395 "(is %d, should be %d or %d) [in module %s]"),
6396 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6397 }
6398
6399 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6400 info_ptr += 1;
6401 }
6402 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6403 cu_header,
6404 &bytes_read);
6405 info_ptr += bytes_read;
6406 if (cu_header->version < 5)
6407 {
6408 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6409 info_ptr += 1;
6410 }
6411 signed_addr = bfd_get_sign_extend_vma (abfd);
6412 if (signed_addr < 0)
6413 internal_error (__FILE__, __LINE__,
6414 _("read_comp_unit_head: dwarf from non elf file"));
6415 cu_header->signed_addr_p = signed_addr;
6416
6417 if (section_kind == rcuh_kind::TYPE)
6418 {
6419 LONGEST type_offset;
6420
6421 cu_header->signature = read_8_bytes (abfd, info_ptr);
6422 info_ptr += 8;
6423
6424 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6425 info_ptr += bytes_read;
6426 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6427 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6428 error (_("Dwarf Error: Too big type_offset in compilation unit "
6429 "header (is %s) [in module %s]"), plongest (type_offset),
6430 filename);
6431 }
6432
6433 return info_ptr;
6434 }
6435
6436 /* Helper function that returns the proper abbrev section for
6437 THIS_CU. */
6438
6439 static struct dwarf2_section_info *
6440 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6441 {
6442 struct dwarf2_section_info *abbrev;
6443 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6444
6445 if (this_cu->is_dwz)
6446 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6447 else
6448 abbrev = &dwarf2_per_objfile->abbrev;
6449
6450 return abbrev;
6451 }
6452
6453 /* Subroutine of read_and_check_comp_unit_head and
6454 read_and_check_type_unit_head to simplify them.
6455 Perform various error checking on the header. */
6456
6457 static void
6458 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6459 struct comp_unit_head *header,
6460 struct dwarf2_section_info *section,
6461 struct dwarf2_section_info *abbrev_section)
6462 {
6463 const char *filename = get_section_file_name (section);
6464
6465 if (header->version < 2 || header->version > 5)
6466 error (_("Dwarf Error: wrong version in compilation unit header "
6467 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
6468 filename);
6469
6470 if (to_underlying (header->abbrev_sect_off)
6471 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6472 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6473 "(offset %s + 6) [in module %s]"),
6474 sect_offset_str (header->abbrev_sect_off),
6475 sect_offset_str (header->sect_off),
6476 filename);
6477
6478 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6479 avoid potential 32-bit overflow. */
6480 if (((ULONGEST) header->sect_off + get_cu_length (header))
6481 > section->size)
6482 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6483 "(offset %s + 0) [in module %s]"),
6484 header->length, sect_offset_str (header->sect_off),
6485 filename);
6486 }
6487
6488 /* Read in a CU/TU header and perform some basic error checking.
6489 The contents of the header are stored in HEADER.
6490 The result is a pointer to the start of the first DIE. */
6491
6492 static const gdb_byte *
6493 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6494 struct comp_unit_head *header,
6495 struct dwarf2_section_info *section,
6496 struct dwarf2_section_info *abbrev_section,
6497 const gdb_byte *info_ptr,
6498 rcuh_kind section_kind)
6499 {
6500 const gdb_byte *beg_of_comp_unit = info_ptr;
6501
6502 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6503
6504 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6505
6506 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6507
6508 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6509 abbrev_section);
6510
6511 return info_ptr;
6512 }
6513
6514 /* Fetch the abbreviation table offset from a comp or type unit header. */
6515
6516 static sect_offset
6517 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6518 struct dwarf2_section_info *section,
6519 sect_offset sect_off)
6520 {
6521 bfd *abfd = get_section_bfd_owner (section);
6522 const gdb_byte *info_ptr;
6523 unsigned int initial_length_size, offset_size;
6524 uint16_t version;
6525
6526 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6527 info_ptr = section->buffer + to_underlying (sect_off);
6528 read_initial_length (abfd, info_ptr, &initial_length_size);
6529 offset_size = initial_length_size == 4 ? 4 : 8;
6530 info_ptr += initial_length_size;
6531
6532 version = read_2_bytes (abfd, info_ptr);
6533 info_ptr += 2;
6534 if (version >= 5)
6535 {
6536 /* Skip unit type and address size. */
6537 info_ptr += 2;
6538 }
6539
6540 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6541 }
6542
6543 /* Allocate a new partial symtab for file named NAME and mark this new
6544 partial symtab as being an include of PST. */
6545
6546 static void
6547 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6548 struct objfile *objfile)
6549 {
6550 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6551
6552 if (!IS_ABSOLUTE_PATH (subpst->filename))
6553 {
6554 /* It shares objfile->objfile_obstack. */
6555 subpst->dirname = pst->dirname;
6556 }
6557
6558 subpst->textlow = 0;
6559 subpst->texthigh = 0;
6560
6561 subpst->dependencies
6562 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
6563 subpst->dependencies[0] = pst;
6564 subpst->number_of_dependencies = 1;
6565
6566 subpst->globals_offset = 0;
6567 subpst->n_global_syms = 0;
6568 subpst->statics_offset = 0;
6569 subpst->n_static_syms = 0;
6570 subpst->compunit_symtab = NULL;
6571 subpst->read_symtab = pst->read_symtab;
6572 subpst->readin = 0;
6573
6574 /* No private part is necessary for include psymtabs. This property
6575 can be used to differentiate between such include psymtabs and
6576 the regular ones. */
6577 subpst->read_symtab_private = NULL;
6578 }
6579
6580 /* Read the Line Number Program data and extract the list of files
6581 included by the source file represented by PST. Build an include
6582 partial symtab for each of these included files. */
6583
6584 static void
6585 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6586 struct die_info *die,
6587 struct partial_symtab *pst)
6588 {
6589 line_header_up lh;
6590 struct attribute *attr;
6591
6592 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6593 if (attr)
6594 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6595 if (lh == NULL)
6596 return; /* No linetable, so no includes. */
6597
6598 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
6599 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
6600 }
6601
6602 static hashval_t
6603 hash_signatured_type (const void *item)
6604 {
6605 const struct signatured_type *sig_type
6606 = (const struct signatured_type *) item;
6607
6608 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6609 return sig_type->signature;
6610 }
6611
6612 static int
6613 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6614 {
6615 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6616 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6617
6618 return lhs->signature == rhs->signature;
6619 }
6620
6621 /* Allocate a hash table for signatured types. */
6622
6623 static htab_t
6624 allocate_signatured_type_table (struct objfile *objfile)
6625 {
6626 return htab_create_alloc_ex (41,
6627 hash_signatured_type,
6628 eq_signatured_type,
6629 NULL,
6630 &objfile->objfile_obstack,
6631 hashtab_obstack_allocate,
6632 dummy_obstack_deallocate);
6633 }
6634
6635 /* A helper function to add a signatured type CU to a table. */
6636
6637 static int
6638 add_signatured_type_cu_to_table (void **slot, void *datum)
6639 {
6640 struct signatured_type *sigt = (struct signatured_type *) *slot;
6641 struct signatured_type ***datap = (struct signatured_type ***) datum;
6642
6643 **datap = sigt;
6644 ++*datap;
6645
6646 return 1;
6647 }
6648
6649 /* A helper for create_debug_types_hash_table. Read types from SECTION
6650 and fill them into TYPES_HTAB. It will process only type units,
6651 therefore DW_UT_type. */
6652
6653 static void
6654 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6655 struct dwo_file *dwo_file,
6656 dwarf2_section_info *section, htab_t &types_htab,
6657 rcuh_kind section_kind)
6658 {
6659 struct objfile *objfile = dwarf2_per_objfile->objfile;
6660 struct dwarf2_section_info *abbrev_section;
6661 bfd *abfd;
6662 const gdb_byte *info_ptr, *end_ptr;
6663
6664 abbrev_section = (dwo_file != NULL
6665 ? &dwo_file->sections.abbrev
6666 : &dwarf2_per_objfile->abbrev);
6667
6668 if (dwarf_read_debug)
6669 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6670 get_section_name (section),
6671 get_section_file_name (abbrev_section));
6672
6673 dwarf2_read_section (objfile, section);
6674 info_ptr = section->buffer;
6675
6676 if (info_ptr == NULL)
6677 return;
6678
6679 /* We can't set abfd until now because the section may be empty or
6680 not present, in which case the bfd is unknown. */
6681 abfd = get_section_bfd_owner (section);
6682
6683 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6684 because we don't need to read any dies: the signature is in the
6685 header. */
6686
6687 end_ptr = info_ptr + section->size;
6688 while (info_ptr < end_ptr)
6689 {
6690 struct signatured_type *sig_type;
6691 struct dwo_unit *dwo_tu;
6692 void **slot;
6693 const gdb_byte *ptr = info_ptr;
6694 struct comp_unit_head header;
6695 unsigned int length;
6696
6697 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6698
6699 /* Initialize it due to a false compiler warning. */
6700 header.signature = -1;
6701 header.type_cu_offset_in_tu = (cu_offset) -1;
6702
6703 /* We need to read the type's signature in order to build the hash
6704 table, but we don't need anything else just yet. */
6705
6706 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6707 abbrev_section, ptr, section_kind);
6708
6709 length = get_cu_length (&header);
6710
6711 /* Skip dummy type units. */
6712 if (ptr >= info_ptr + length
6713 || peek_abbrev_code (abfd, ptr) == 0
6714 || header.unit_type != DW_UT_type)
6715 {
6716 info_ptr += length;
6717 continue;
6718 }
6719
6720 if (types_htab == NULL)
6721 {
6722 if (dwo_file)
6723 types_htab = allocate_dwo_unit_table (objfile);
6724 else
6725 types_htab = allocate_signatured_type_table (objfile);
6726 }
6727
6728 if (dwo_file)
6729 {
6730 sig_type = NULL;
6731 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6732 struct dwo_unit);
6733 dwo_tu->dwo_file = dwo_file;
6734 dwo_tu->signature = header.signature;
6735 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6736 dwo_tu->section = section;
6737 dwo_tu->sect_off = sect_off;
6738 dwo_tu->length = length;
6739 }
6740 else
6741 {
6742 /* N.B.: type_offset is not usable if this type uses a DWO file.
6743 The real type_offset is in the DWO file. */
6744 dwo_tu = NULL;
6745 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6746 struct signatured_type);
6747 sig_type->signature = header.signature;
6748 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6749 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6750 sig_type->per_cu.is_debug_types = 1;
6751 sig_type->per_cu.section = section;
6752 sig_type->per_cu.sect_off = sect_off;
6753 sig_type->per_cu.length = length;
6754 }
6755
6756 slot = htab_find_slot (types_htab,
6757 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6758 INSERT);
6759 gdb_assert (slot != NULL);
6760 if (*slot != NULL)
6761 {
6762 sect_offset dup_sect_off;
6763
6764 if (dwo_file)
6765 {
6766 const struct dwo_unit *dup_tu
6767 = (const struct dwo_unit *) *slot;
6768
6769 dup_sect_off = dup_tu->sect_off;
6770 }
6771 else
6772 {
6773 const struct signatured_type *dup_tu
6774 = (const struct signatured_type *) *slot;
6775
6776 dup_sect_off = dup_tu->per_cu.sect_off;
6777 }
6778
6779 complaint (&symfile_complaints,
6780 _("debug type entry at offset %s is duplicate to"
6781 " the entry at offset %s, signature %s"),
6782 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6783 hex_string (header.signature));
6784 }
6785 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6786
6787 if (dwarf_read_debug > 1)
6788 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6789 sect_offset_str (sect_off),
6790 hex_string (header.signature));
6791
6792 info_ptr += length;
6793 }
6794 }
6795
6796 /* Create the hash table of all entries in the .debug_types
6797 (or .debug_types.dwo) section(s).
6798 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6799 otherwise it is NULL.
6800
6801 The result is a pointer to the hash table or NULL if there are no types.
6802
6803 Note: This function processes DWO files only, not DWP files. */
6804
6805 static void
6806 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6807 struct dwo_file *dwo_file,
6808 VEC (dwarf2_section_info_def) *types,
6809 htab_t &types_htab)
6810 {
6811 int ix;
6812 struct dwarf2_section_info *section;
6813
6814 if (VEC_empty (dwarf2_section_info_def, types))
6815 return;
6816
6817 for (ix = 0;
6818 VEC_iterate (dwarf2_section_info_def, types, ix, section);
6819 ++ix)
6820 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
6821 types_htab, rcuh_kind::TYPE);
6822 }
6823
6824 /* Create the hash table of all entries in the .debug_types section,
6825 and initialize all_type_units.
6826 The result is zero if there is an error (e.g. missing .debug_types section),
6827 otherwise non-zero. */
6828
6829 static int
6830 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6831 {
6832 htab_t types_htab = NULL;
6833 struct signatured_type **iter;
6834
6835 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6836 &dwarf2_per_objfile->info, types_htab,
6837 rcuh_kind::COMPILE);
6838 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6839 dwarf2_per_objfile->types, types_htab);
6840 if (types_htab == NULL)
6841 {
6842 dwarf2_per_objfile->signatured_types = NULL;
6843 return 0;
6844 }
6845
6846 dwarf2_per_objfile->signatured_types = types_htab;
6847
6848 dwarf2_per_objfile->n_type_units
6849 = dwarf2_per_objfile->n_allocated_type_units
6850 = htab_elements (types_htab);
6851 dwarf2_per_objfile->all_type_units =
6852 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
6853 iter = &dwarf2_per_objfile->all_type_units[0];
6854 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
6855 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
6856 == dwarf2_per_objfile->n_type_units);
6857
6858 return 1;
6859 }
6860
6861 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6862 If SLOT is non-NULL, it is the entry to use in the hash table.
6863 Otherwise we find one. */
6864
6865 static struct signatured_type *
6866 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6867 void **slot)
6868 {
6869 struct objfile *objfile = dwarf2_per_objfile->objfile;
6870 int n_type_units = dwarf2_per_objfile->n_type_units;
6871 struct signatured_type *sig_type;
6872
6873 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
6874 ++n_type_units;
6875 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
6876 {
6877 if (dwarf2_per_objfile->n_allocated_type_units == 0)
6878 dwarf2_per_objfile->n_allocated_type_units = 1;
6879 dwarf2_per_objfile->n_allocated_type_units *= 2;
6880 dwarf2_per_objfile->all_type_units
6881 = XRESIZEVEC (struct signatured_type *,
6882 dwarf2_per_objfile->all_type_units,
6883 dwarf2_per_objfile->n_allocated_type_units);
6884 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6885 }
6886 dwarf2_per_objfile->n_type_units = n_type_units;
6887
6888 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6889 struct signatured_type);
6890 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
6891 sig_type->signature = sig;
6892 sig_type->per_cu.is_debug_types = 1;
6893 if (dwarf2_per_objfile->using_index)
6894 {
6895 sig_type->per_cu.v.quick =
6896 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6897 struct dwarf2_per_cu_quick_data);
6898 }
6899
6900 if (slot == NULL)
6901 {
6902 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6903 sig_type, INSERT);
6904 }
6905 gdb_assert (*slot == NULL);
6906 *slot = sig_type;
6907 /* The rest of sig_type must be filled in by the caller. */
6908 return sig_type;
6909 }
6910
6911 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6912 Fill in SIG_ENTRY with DWO_ENTRY. */
6913
6914 static void
6915 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6916 struct signatured_type *sig_entry,
6917 struct dwo_unit *dwo_entry)
6918 {
6919 /* Make sure we're not clobbering something we don't expect to. */
6920 gdb_assert (! sig_entry->per_cu.queued);
6921 gdb_assert (sig_entry->per_cu.cu == NULL);
6922 if (dwarf2_per_objfile->using_index)
6923 {
6924 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6925 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6926 }
6927 else
6928 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6929 gdb_assert (sig_entry->signature == dwo_entry->signature);
6930 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6931 gdb_assert (sig_entry->type_unit_group == NULL);
6932 gdb_assert (sig_entry->dwo_unit == NULL);
6933
6934 sig_entry->per_cu.section = dwo_entry->section;
6935 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6936 sig_entry->per_cu.length = dwo_entry->length;
6937 sig_entry->per_cu.reading_dwo_directly = 1;
6938 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6939 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6940 sig_entry->dwo_unit = dwo_entry;
6941 }
6942
6943 /* Subroutine of lookup_signatured_type.
6944 If we haven't read the TU yet, create the signatured_type data structure
6945 for a TU to be read in directly from a DWO file, bypassing the stub.
6946 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6947 using .gdb_index, then when reading a CU we want to stay in the DWO file
6948 containing that CU. Otherwise we could end up reading several other DWO
6949 files (due to comdat folding) to process the transitive closure of all the
6950 mentioned TUs, and that can be slow. The current DWO file will have every
6951 type signature that it needs.
6952 We only do this for .gdb_index because in the psymtab case we already have
6953 to read all the DWOs to build the type unit groups. */
6954
6955 static struct signatured_type *
6956 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6957 {
6958 struct dwarf2_per_objfile *dwarf2_per_objfile
6959 = cu->per_cu->dwarf2_per_objfile;
6960 struct objfile *objfile = dwarf2_per_objfile->objfile;
6961 struct dwo_file *dwo_file;
6962 struct dwo_unit find_dwo_entry, *dwo_entry;
6963 struct signatured_type find_sig_entry, *sig_entry;
6964 void **slot;
6965
6966 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6967
6968 /* If TU skeletons have been removed then we may not have read in any
6969 TUs yet. */
6970 if (dwarf2_per_objfile->signatured_types == NULL)
6971 {
6972 dwarf2_per_objfile->signatured_types
6973 = allocate_signatured_type_table (objfile);
6974 }
6975
6976 /* We only ever need to read in one copy of a signatured type.
6977 Use the global signatured_types array to do our own comdat-folding
6978 of types. If this is the first time we're reading this TU, and
6979 the TU has an entry in .gdb_index, replace the recorded data from
6980 .gdb_index with this TU. */
6981
6982 find_sig_entry.signature = sig;
6983 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6984 &find_sig_entry, INSERT);
6985 sig_entry = (struct signatured_type *) *slot;
6986
6987 /* We can get here with the TU already read, *or* in the process of being
6988 read. Don't reassign the global entry to point to this DWO if that's
6989 the case. Also note that if the TU is already being read, it may not
6990 have come from a DWO, the program may be a mix of Fission-compiled
6991 code and non-Fission-compiled code. */
6992
6993 /* Have we already tried to read this TU?
6994 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6995 needn't exist in the global table yet). */
6996 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6997 return sig_entry;
6998
6999 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7000 dwo_unit of the TU itself. */
7001 dwo_file = cu->dwo_unit->dwo_file;
7002
7003 /* Ok, this is the first time we're reading this TU. */
7004 if (dwo_file->tus == NULL)
7005 return NULL;
7006 find_dwo_entry.signature = sig;
7007 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7008 if (dwo_entry == NULL)
7009 return NULL;
7010
7011 /* If the global table doesn't have an entry for this TU, add one. */
7012 if (sig_entry == NULL)
7013 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7014
7015 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7016 sig_entry->per_cu.tu_read = 1;
7017 return sig_entry;
7018 }
7019
7020 /* Subroutine of lookup_signatured_type.
7021 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7022 then try the DWP file. If the TU stub (skeleton) has been removed then
7023 it won't be in .gdb_index. */
7024
7025 static struct signatured_type *
7026 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7027 {
7028 struct dwarf2_per_objfile *dwarf2_per_objfile
7029 = cu->per_cu->dwarf2_per_objfile;
7030 struct objfile *objfile = dwarf2_per_objfile->objfile;
7031 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7032 struct dwo_unit *dwo_entry;
7033 struct signatured_type find_sig_entry, *sig_entry;
7034 void **slot;
7035
7036 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7037 gdb_assert (dwp_file != NULL);
7038
7039 /* If TU skeletons have been removed then we may not have read in any
7040 TUs yet. */
7041 if (dwarf2_per_objfile->signatured_types == NULL)
7042 {
7043 dwarf2_per_objfile->signatured_types
7044 = allocate_signatured_type_table (objfile);
7045 }
7046
7047 find_sig_entry.signature = sig;
7048 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7049 &find_sig_entry, INSERT);
7050 sig_entry = (struct signatured_type *) *slot;
7051
7052 /* Have we already tried to read this TU?
7053 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7054 needn't exist in the global table yet). */
7055 if (sig_entry != NULL)
7056 return sig_entry;
7057
7058 if (dwp_file->tus == NULL)
7059 return NULL;
7060 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7061 sig, 1 /* is_debug_types */);
7062 if (dwo_entry == NULL)
7063 return NULL;
7064
7065 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7066 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7067
7068 return sig_entry;
7069 }
7070
7071 /* Lookup a signature based type for DW_FORM_ref_sig8.
7072 Returns NULL if signature SIG is not present in the table.
7073 It is up to the caller to complain about this. */
7074
7075 static struct signatured_type *
7076 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7077 {
7078 struct dwarf2_per_objfile *dwarf2_per_objfile
7079 = cu->per_cu->dwarf2_per_objfile;
7080
7081 if (cu->dwo_unit
7082 && dwarf2_per_objfile->using_index)
7083 {
7084 /* We're in a DWO/DWP file, and we're using .gdb_index.
7085 These cases require special processing. */
7086 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7087 return lookup_dwo_signatured_type (cu, sig);
7088 else
7089 return lookup_dwp_signatured_type (cu, sig);
7090 }
7091 else
7092 {
7093 struct signatured_type find_entry, *entry;
7094
7095 if (dwarf2_per_objfile->signatured_types == NULL)
7096 return NULL;
7097 find_entry.signature = sig;
7098 entry = ((struct signatured_type *)
7099 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7100 return entry;
7101 }
7102 }
7103 \f
7104 /* Low level DIE reading support. */
7105
7106 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7107
7108 static void
7109 init_cu_die_reader (struct die_reader_specs *reader,
7110 struct dwarf2_cu *cu,
7111 struct dwarf2_section_info *section,
7112 struct dwo_file *dwo_file,
7113 struct abbrev_table *abbrev_table)
7114 {
7115 gdb_assert (section->readin && section->buffer != NULL);
7116 reader->abfd = get_section_bfd_owner (section);
7117 reader->cu = cu;
7118 reader->dwo_file = dwo_file;
7119 reader->die_section = section;
7120 reader->buffer = section->buffer;
7121 reader->buffer_end = section->buffer + section->size;
7122 reader->comp_dir = NULL;
7123 reader->abbrev_table = abbrev_table;
7124 }
7125
7126 /* Subroutine of init_cutu_and_read_dies to simplify it.
7127 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7128 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7129 already.
7130
7131 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7132 from it to the DIE in the DWO. If NULL we are skipping the stub.
7133 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7134 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7135 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7136 STUB_COMP_DIR may be non-NULL.
7137 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7138 are filled in with the info of the DIE from the DWO file.
7139 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7140 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7141 kept around for at least as long as *RESULT_READER.
7142
7143 The result is non-zero if a valid (non-dummy) DIE was found. */
7144
7145 static int
7146 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7147 struct dwo_unit *dwo_unit,
7148 struct die_info *stub_comp_unit_die,
7149 const char *stub_comp_dir,
7150 struct die_reader_specs *result_reader,
7151 const gdb_byte **result_info_ptr,
7152 struct die_info **result_comp_unit_die,
7153 int *result_has_children,
7154 abbrev_table_up *result_dwo_abbrev_table)
7155 {
7156 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7157 struct objfile *objfile = dwarf2_per_objfile->objfile;
7158 struct dwarf2_cu *cu = this_cu->cu;
7159 bfd *abfd;
7160 const gdb_byte *begin_info_ptr, *info_ptr;
7161 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7162 int i,num_extra_attrs;
7163 struct dwarf2_section_info *dwo_abbrev_section;
7164 struct attribute *attr;
7165 struct die_info *comp_unit_die;
7166
7167 /* At most one of these may be provided. */
7168 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7169
7170 /* These attributes aren't processed until later:
7171 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7172 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7173 referenced later. However, these attributes are found in the stub
7174 which we won't have later. In order to not impose this complication
7175 on the rest of the code, we read them here and copy them to the
7176 DWO CU/TU die. */
7177
7178 stmt_list = NULL;
7179 low_pc = NULL;
7180 high_pc = NULL;
7181 ranges = NULL;
7182 comp_dir = NULL;
7183
7184 if (stub_comp_unit_die != NULL)
7185 {
7186 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7187 DWO file. */
7188 if (! this_cu->is_debug_types)
7189 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7190 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7191 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7192 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7193 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7194
7195 /* There should be a DW_AT_addr_base attribute here (if needed).
7196 We need the value before we can process DW_FORM_GNU_addr_index. */
7197 cu->addr_base = 0;
7198 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7199 if (attr)
7200 cu->addr_base = DW_UNSND (attr);
7201
7202 /* There should be a DW_AT_ranges_base attribute here (if needed).
7203 We need the value before we can process DW_AT_ranges. */
7204 cu->ranges_base = 0;
7205 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7206 if (attr)
7207 cu->ranges_base = DW_UNSND (attr);
7208 }
7209 else if (stub_comp_dir != NULL)
7210 {
7211 /* Reconstruct the comp_dir attribute to simplify the code below. */
7212 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7213 comp_dir->name = DW_AT_comp_dir;
7214 comp_dir->form = DW_FORM_string;
7215 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7216 DW_STRING (comp_dir) = stub_comp_dir;
7217 }
7218
7219 /* Set up for reading the DWO CU/TU. */
7220 cu->dwo_unit = dwo_unit;
7221 dwarf2_section_info *section = dwo_unit->section;
7222 dwarf2_read_section (objfile, section);
7223 abfd = get_section_bfd_owner (section);
7224 begin_info_ptr = info_ptr = (section->buffer
7225 + to_underlying (dwo_unit->sect_off));
7226 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7227
7228 if (this_cu->is_debug_types)
7229 {
7230 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7231
7232 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7233 &cu->header, section,
7234 dwo_abbrev_section,
7235 info_ptr, rcuh_kind::TYPE);
7236 /* This is not an assert because it can be caused by bad debug info. */
7237 if (sig_type->signature != cu->header.signature)
7238 {
7239 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7240 " TU at offset %s [in module %s]"),
7241 hex_string (sig_type->signature),
7242 hex_string (cu->header.signature),
7243 sect_offset_str (dwo_unit->sect_off),
7244 bfd_get_filename (abfd));
7245 }
7246 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7247 /* For DWOs coming from DWP files, we don't know the CU length
7248 nor the type's offset in the TU until now. */
7249 dwo_unit->length = get_cu_length (&cu->header);
7250 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7251
7252 /* Establish the type offset that can be used to lookup the type.
7253 For DWO files, we don't know it until now. */
7254 sig_type->type_offset_in_section
7255 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7256 }
7257 else
7258 {
7259 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7260 &cu->header, section,
7261 dwo_abbrev_section,
7262 info_ptr, rcuh_kind::COMPILE);
7263 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7264 /* For DWOs coming from DWP files, we don't know the CU length
7265 until now. */
7266 dwo_unit->length = get_cu_length (&cu->header);
7267 }
7268
7269 *result_dwo_abbrev_table
7270 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7271 cu->header.abbrev_sect_off);
7272 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7273 result_dwo_abbrev_table->get ());
7274
7275 /* Read in the die, but leave space to copy over the attributes
7276 from the stub. This has the benefit of simplifying the rest of
7277 the code - all the work to maintain the illusion of a single
7278 DW_TAG_{compile,type}_unit DIE is done here. */
7279 num_extra_attrs = ((stmt_list != NULL)
7280 + (low_pc != NULL)
7281 + (high_pc != NULL)
7282 + (ranges != NULL)
7283 + (comp_dir != NULL));
7284 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7285 result_has_children, num_extra_attrs);
7286
7287 /* Copy over the attributes from the stub to the DIE we just read in. */
7288 comp_unit_die = *result_comp_unit_die;
7289 i = comp_unit_die->num_attrs;
7290 if (stmt_list != NULL)
7291 comp_unit_die->attrs[i++] = *stmt_list;
7292 if (low_pc != NULL)
7293 comp_unit_die->attrs[i++] = *low_pc;
7294 if (high_pc != NULL)
7295 comp_unit_die->attrs[i++] = *high_pc;
7296 if (ranges != NULL)
7297 comp_unit_die->attrs[i++] = *ranges;
7298 if (comp_dir != NULL)
7299 comp_unit_die->attrs[i++] = *comp_dir;
7300 comp_unit_die->num_attrs += num_extra_attrs;
7301
7302 if (dwarf_die_debug)
7303 {
7304 fprintf_unfiltered (gdb_stdlog,
7305 "Read die from %s@0x%x of %s:\n",
7306 get_section_name (section),
7307 (unsigned) (begin_info_ptr - section->buffer),
7308 bfd_get_filename (abfd));
7309 dump_die (comp_unit_die, dwarf_die_debug);
7310 }
7311
7312 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7313 TUs by skipping the stub and going directly to the entry in the DWO file.
7314 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7315 to get it via circuitous means. Blech. */
7316 if (comp_dir != NULL)
7317 result_reader->comp_dir = DW_STRING (comp_dir);
7318
7319 /* Skip dummy compilation units. */
7320 if (info_ptr >= begin_info_ptr + dwo_unit->length
7321 || peek_abbrev_code (abfd, info_ptr) == 0)
7322 return 0;
7323
7324 *result_info_ptr = info_ptr;
7325 return 1;
7326 }
7327
7328 /* Subroutine of init_cutu_and_read_dies to simplify it.
7329 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7330 Returns NULL if the specified DWO unit cannot be found. */
7331
7332 static struct dwo_unit *
7333 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7334 struct die_info *comp_unit_die)
7335 {
7336 struct dwarf2_cu *cu = this_cu->cu;
7337 ULONGEST signature;
7338 struct dwo_unit *dwo_unit;
7339 const char *comp_dir, *dwo_name;
7340
7341 gdb_assert (cu != NULL);
7342
7343 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7344 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7345 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7346
7347 if (this_cu->is_debug_types)
7348 {
7349 struct signatured_type *sig_type;
7350
7351 /* Since this_cu is the first member of struct signatured_type,
7352 we can go from a pointer to one to a pointer to the other. */
7353 sig_type = (struct signatured_type *) this_cu;
7354 signature = sig_type->signature;
7355 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7356 }
7357 else
7358 {
7359 struct attribute *attr;
7360
7361 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7362 if (! attr)
7363 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7364 " [in module %s]"),
7365 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7366 signature = DW_UNSND (attr);
7367 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7368 signature);
7369 }
7370
7371 return dwo_unit;
7372 }
7373
7374 /* Subroutine of init_cutu_and_read_dies to simplify it.
7375 See it for a description of the parameters.
7376 Read a TU directly from a DWO file, bypassing the stub. */
7377
7378 static void
7379 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7380 int use_existing_cu, int keep,
7381 die_reader_func_ftype *die_reader_func,
7382 void *data)
7383 {
7384 std::unique_ptr<dwarf2_cu> new_cu;
7385 struct signatured_type *sig_type;
7386 struct die_reader_specs reader;
7387 const gdb_byte *info_ptr;
7388 struct die_info *comp_unit_die;
7389 int has_children;
7390 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7391
7392 /* Verify we can do the following downcast, and that we have the
7393 data we need. */
7394 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7395 sig_type = (struct signatured_type *) this_cu;
7396 gdb_assert (sig_type->dwo_unit != NULL);
7397
7398 if (use_existing_cu && this_cu->cu != NULL)
7399 {
7400 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7401 /* There's no need to do the rereading_dwo_cu handling that
7402 init_cutu_and_read_dies does since we don't read the stub. */
7403 }
7404 else
7405 {
7406 /* If !use_existing_cu, this_cu->cu must be NULL. */
7407 gdb_assert (this_cu->cu == NULL);
7408 new_cu.reset (new dwarf2_cu (this_cu));
7409 }
7410
7411 /* A future optimization, if needed, would be to use an existing
7412 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7413 could share abbrev tables. */
7414
7415 /* The abbreviation table used by READER, this must live at least as long as
7416 READER. */
7417 abbrev_table_up dwo_abbrev_table;
7418
7419 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7420 NULL /* stub_comp_unit_die */,
7421 sig_type->dwo_unit->dwo_file->comp_dir,
7422 &reader, &info_ptr,
7423 &comp_unit_die, &has_children,
7424 &dwo_abbrev_table) == 0)
7425 {
7426 /* Dummy die. */
7427 return;
7428 }
7429
7430 /* All the "real" work is done here. */
7431 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7432
7433 /* This duplicates the code in init_cutu_and_read_dies,
7434 but the alternative is making the latter more complex.
7435 This function is only for the special case of using DWO files directly:
7436 no point in overly complicating the general case just to handle this. */
7437 if (new_cu != NULL && keep)
7438 {
7439 /* Link this CU into read_in_chain. */
7440 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7441 dwarf2_per_objfile->read_in_chain = this_cu;
7442 /* The chain owns it now. */
7443 new_cu.release ();
7444 }
7445 }
7446
7447 /* Initialize a CU (or TU) and read its DIEs.
7448 If the CU defers to a DWO file, read the DWO file as well.
7449
7450 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7451 Otherwise the table specified in the comp unit header is read in and used.
7452 This is an optimization for when we already have the abbrev table.
7453
7454 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7455 Otherwise, a new CU is allocated with xmalloc.
7456
7457 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7458 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7459
7460 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7461 linker) then DIE_READER_FUNC will not get called. */
7462
7463 static void
7464 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7465 struct abbrev_table *abbrev_table,
7466 int use_existing_cu, int keep,
7467 die_reader_func_ftype *die_reader_func,
7468 void *data)
7469 {
7470 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7471 struct objfile *objfile = dwarf2_per_objfile->objfile;
7472 struct dwarf2_section_info *section = this_cu->section;
7473 bfd *abfd = get_section_bfd_owner (section);
7474 struct dwarf2_cu *cu;
7475 const gdb_byte *begin_info_ptr, *info_ptr;
7476 struct die_reader_specs reader;
7477 struct die_info *comp_unit_die;
7478 int has_children;
7479 struct attribute *attr;
7480 struct signatured_type *sig_type = NULL;
7481 struct dwarf2_section_info *abbrev_section;
7482 /* Non-zero if CU currently points to a DWO file and we need to
7483 reread it. When this happens we need to reread the skeleton die
7484 before we can reread the DWO file (this only applies to CUs, not TUs). */
7485 int rereading_dwo_cu = 0;
7486
7487 if (dwarf_die_debug)
7488 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7489 this_cu->is_debug_types ? "type" : "comp",
7490 sect_offset_str (this_cu->sect_off));
7491
7492 if (use_existing_cu)
7493 gdb_assert (keep);
7494
7495 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7496 file (instead of going through the stub), short-circuit all of this. */
7497 if (this_cu->reading_dwo_directly)
7498 {
7499 /* Narrow down the scope of possibilities to have to understand. */
7500 gdb_assert (this_cu->is_debug_types);
7501 gdb_assert (abbrev_table == NULL);
7502 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7503 die_reader_func, data);
7504 return;
7505 }
7506
7507 /* This is cheap if the section is already read in. */
7508 dwarf2_read_section (objfile, section);
7509
7510 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7511
7512 abbrev_section = get_abbrev_section_for_cu (this_cu);
7513
7514 std::unique_ptr<dwarf2_cu> new_cu;
7515 if (use_existing_cu && this_cu->cu != NULL)
7516 {
7517 cu = this_cu->cu;
7518 /* If this CU is from a DWO file we need to start over, we need to
7519 refetch the attributes from the skeleton CU.
7520 This could be optimized by retrieving those attributes from when we
7521 were here the first time: the previous comp_unit_die was stored in
7522 comp_unit_obstack. But there's no data yet that we need this
7523 optimization. */
7524 if (cu->dwo_unit != NULL)
7525 rereading_dwo_cu = 1;
7526 }
7527 else
7528 {
7529 /* If !use_existing_cu, this_cu->cu must be NULL. */
7530 gdb_assert (this_cu->cu == NULL);
7531 new_cu.reset (new dwarf2_cu (this_cu));
7532 cu = new_cu.get ();
7533 }
7534
7535 /* Get the header. */
7536 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7537 {
7538 /* We already have the header, there's no need to read it in again. */
7539 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7540 }
7541 else
7542 {
7543 if (this_cu->is_debug_types)
7544 {
7545 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7546 &cu->header, section,
7547 abbrev_section, info_ptr,
7548 rcuh_kind::TYPE);
7549
7550 /* Since per_cu is the first member of struct signatured_type,
7551 we can go from a pointer to one to a pointer to the other. */
7552 sig_type = (struct signatured_type *) this_cu;
7553 gdb_assert (sig_type->signature == cu->header.signature);
7554 gdb_assert (sig_type->type_offset_in_tu
7555 == cu->header.type_cu_offset_in_tu);
7556 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7557
7558 /* LENGTH has not been set yet for type units if we're
7559 using .gdb_index. */
7560 this_cu->length = get_cu_length (&cu->header);
7561
7562 /* Establish the type offset that can be used to lookup the type. */
7563 sig_type->type_offset_in_section =
7564 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7565
7566 this_cu->dwarf_version = cu->header.version;
7567 }
7568 else
7569 {
7570 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7571 &cu->header, section,
7572 abbrev_section,
7573 info_ptr,
7574 rcuh_kind::COMPILE);
7575
7576 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7577 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7578 this_cu->dwarf_version = cu->header.version;
7579 }
7580 }
7581
7582 /* Skip dummy compilation units. */
7583 if (info_ptr >= begin_info_ptr + this_cu->length
7584 || peek_abbrev_code (abfd, info_ptr) == 0)
7585 return;
7586
7587 /* If we don't have them yet, read the abbrevs for this compilation unit.
7588 And if we need to read them now, make sure they're freed when we're
7589 done (own the table through ABBREV_TABLE_HOLDER). */
7590 abbrev_table_up abbrev_table_holder;
7591 if (abbrev_table != NULL)
7592 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7593 else
7594 {
7595 abbrev_table_holder
7596 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7597 cu->header.abbrev_sect_off);
7598 abbrev_table = abbrev_table_holder.get ();
7599 }
7600
7601 /* Read the top level CU/TU die. */
7602 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7603 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7604
7605 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7606 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7607 table from the DWO file and pass the ownership over to us. It will be
7608 referenced from READER, so we must make sure to free it after we're done
7609 with READER.
7610
7611 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7612 DWO CU, that this test will fail (the attribute will not be present). */
7613 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7614 abbrev_table_up dwo_abbrev_table;
7615 if (attr)
7616 {
7617 struct dwo_unit *dwo_unit;
7618 struct die_info *dwo_comp_unit_die;
7619
7620 if (has_children)
7621 {
7622 complaint (&symfile_complaints,
7623 _("compilation unit with DW_AT_GNU_dwo_name"
7624 " has children (offset %s) [in module %s]"),
7625 sect_offset_str (this_cu->sect_off),
7626 bfd_get_filename (abfd));
7627 }
7628 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7629 if (dwo_unit != NULL)
7630 {
7631 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7632 comp_unit_die, NULL,
7633 &reader, &info_ptr,
7634 &dwo_comp_unit_die, &has_children,
7635 &dwo_abbrev_table) == 0)
7636 {
7637 /* Dummy die. */
7638 return;
7639 }
7640 comp_unit_die = dwo_comp_unit_die;
7641 }
7642 else
7643 {
7644 /* Yikes, we couldn't find the rest of the DIE, we only have
7645 the stub. A complaint has already been logged. There's
7646 not much more we can do except pass on the stub DIE to
7647 die_reader_func. We don't want to throw an error on bad
7648 debug info. */
7649 }
7650 }
7651
7652 /* All of the above is setup for this call. Yikes. */
7653 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7654
7655 /* Done, clean up. */
7656 if (new_cu != NULL && keep)
7657 {
7658 /* Link this CU into read_in_chain. */
7659 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7660 dwarf2_per_objfile->read_in_chain = this_cu;
7661 /* The chain owns it now. */
7662 new_cu.release ();
7663 }
7664 }
7665
7666 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7667 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7668 to have already done the lookup to find the DWO file).
7669
7670 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7671 THIS_CU->is_debug_types, but nothing else.
7672
7673 We fill in THIS_CU->length.
7674
7675 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7676 linker) then DIE_READER_FUNC will not get called.
7677
7678 THIS_CU->cu is always freed when done.
7679 This is done in order to not leave THIS_CU->cu in a state where we have
7680 to care whether it refers to the "main" CU or the DWO CU. */
7681
7682 static void
7683 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7684 struct dwo_file *dwo_file,
7685 die_reader_func_ftype *die_reader_func,
7686 void *data)
7687 {
7688 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7689 struct objfile *objfile = dwarf2_per_objfile->objfile;
7690 struct dwarf2_section_info *section = this_cu->section;
7691 bfd *abfd = get_section_bfd_owner (section);
7692 struct dwarf2_section_info *abbrev_section;
7693 const gdb_byte *begin_info_ptr, *info_ptr;
7694 struct die_reader_specs reader;
7695 struct die_info *comp_unit_die;
7696 int has_children;
7697
7698 if (dwarf_die_debug)
7699 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7700 this_cu->is_debug_types ? "type" : "comp",
7701 sect_offset_str (this_cu->sect_off));
7702
7703 gdb_assert (this_cu->cu == NULL);
7704
7705 abbrev_section = (dwo_file != NULL
7706 ? &dwo_file->sections.abbrev
7707 : get_abbrev_section_for_cu (this_cu));
7708
7709 /* This is cheap if the section is already read in. */
7710 dwarf2_read_section (objfile, section);
7711
7712 struct dwarf2_cu cu (this_cu);
7713
7714 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7715 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7716 &cu.header, section,
7717 abbrev_section, info_ptr,
7718 (this_cu->is_debug_types
7719 ? rcuh_kind::TYPE
7720 : rcuh_kind::COMPILE));
7721
7722 this_cu->length = get_cu_length (&cu.header);
7723
7724 /* Skip dummy compilation units. */
7725 if (info_ptr >= begin_info_ptr + this_cu->length
7726 || peek_abbrev_code (abfd, info_ptr) == 0)
7727 return;
7728
7729 abbrev_table_up abbrev_table
7730 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7731 cu.header.abbrev_sect_off);
7732
7733 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
7734 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7735
7736 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7737 }
7738
7739 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7740 does not lookup the specified DWO file.
7741 This cannot be used to read DWO files.
7742
7743 THIS_CU->cu is always freed when done.
7744 This is done in order to not leave THIS_CU->cu in a state where we have
7745 to care whether it refers to the "main" CU or the DWO CU.
7746 We can revisit this if the data shows there's a performance issue. */
7747
7748 static void
7749 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7750 die_reader_func_ftype *die_reader_func,
7751 void *data)
7752 {
7753 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
7754 }
7755 \f
7756 /* Type Unit Groups.
7757
7758 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7759 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7760 so that all types coming from the same compilation (.o file) are grouped
7761 together. A future step could be to put the types in the same symtab as
7762 the CU the types ultimately came from. */
7763
7764 static hashval_t
7765 hash_type_unit_group (const void *item)
7766 {
7767 const struct type_unit_group *tu_group
7768 = (const struct type_unit_group *) item;
7769
7770 return hash_stmt_list_entry (&tu_group->hash);
7771 }
7772
7773 static int
7774 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7775 {
7776 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7777 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7778
7779 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7780 }
7781
7782 /* Allocate a hash table for type unit groups. */
7783
7784 static htab_t
7785 allocate_type_unit_groups_table (struct objfile *objfile)
7786 {
7787 return htab_create_alloc_ex (3,
7788 hash_type_unit_group,
7789 eq_type_unit_group,
7790 NULL,
7791 &objfile->objfile_obstack,
7792 hashtab_obstack_allocate,
7793 dummy_obstack_deallocate);
7794 }
7795
7796 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7797 partial symtabs. We combine several TUs per psymtab to not let the size
7798 of any one psymtab grow too big. */
7799 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7800 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7801
7802 /* Helper routine for get_type_unit_group.
7803 Create the type_unit_group object used to hold one or more TUs. */
7804
7805 static struct type_unit_group *
7806 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7807 {
7808 struct dwarf2_per_objfile *dwarf2_per_objfile
7809 = cu->per_cu->dwarf2_per_objfile;
7810 struct objfile *objfile = dwarf2_per_objfile->objfile;
7811 struct dwarf2_per_cu_data *per_cu;
7812 struct type_unit_group *tu_group;
7813
7814 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7815 struct type_unit_group);
7816 per_cu = &tu_group->per_cu;
7817 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7818
7819 if (dwarf2_per_objfile->using_index)
7820 {
7821 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7822 struct dwarf2_per_cu_quick_data);
7823 }
7824 else
7825 {
7826 unsigned int line_offset = to_underlying (line_offset_struct);
7827 struct partial_symtab *pst;
7828 char *name;
7829
7830 /* Give the symtab a useful name for debug purposes. */
7831 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7832 name = xstrprintf ("<type_units_%d>",
7833 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7834 else
7835 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
7836
7837 pst = create_partial_symtab (per_cu, name);
7838 pst->anonymous = 1;
7839
7840 xfree (name);
7841 }
7842
7843 tu_group->hash.dwo_unit = cu->dwo_unit;
7844 tu_group->hash.line_sect_off = line_offset_struct;
7845
7846 return tu_group;
7847 }
7848
7849 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7850 STMT_LIST is a DW_AT_stmt_list attribute. */
7851
7852 static struct type_unit_group *
7853 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7854 {
7855 struct dwarf2_per_objfile *dwarf2_per_objfile
7856 = cu->per_cu->dwarf2_per_objfile;
7857 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7858 struct type_unit_group *tu_group;
7859 void **slot;
7860 unsigned int line_offset;
7861 struct type_unit_group type_unit_group_for_lookup;
7862
7863 if (dwarf2_per_objfile->type_unit_groups == NULL)
7864 {
7865 dwarf2_per_objfile->type_unit_groups =
7866 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7867 }
7868
7869 /* Do we need to create a new group, or can we use an existing one? */
7870
7871 if (stmt_list)
7872 {
7873 line_offset = DW_UNSND (stmt_list);
7874 ++tu_stats->nr_symtab_sharers;
7875 }
7876 else
7877 {
7878 /* Ugh, no stmt_list. Rare, but we have to handle it.
7879 We can do various things here like create one group per TU or
7880 spread them over multiple groups to split up the expansion work.
7881 To avoid worst case scenarios (too many groups or too large groups)
7882 we, umm, group them in bunches. */
7883 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7884 | (tu_stats->nr_stmt_less_type_units
7885 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7886 ++tu_stats->nr_stmt_less_type_units;
7887 }
7888
7889 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7890 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7891 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7892 &type_unit_group_for_lookup, INSERT);
7893 if (*slot != NULL)
7894 {
7895 tu_group = (struct type_unit_group *) *slot;
7896 gdb_assert (tu_group != NULL);
7897 }
7898 else
7899 {
7900 sect_offset line_offset_struct = (sect_offset) line_offset;
7901 tu_group = create_type_unit_group (cu, line_offset_struct);
7902 *slot = tu_group;
7903 ++tu_stats->nr_symtabs;
7904 }
7905
7906 return tu_group;
7907 }
7908 \f
7909 /* Partial symbol tables. */
7910
7911 /* Create a psymtab named NAME and assign it to PER_CU.
7912
7913 The caller must fill in the following details:
7914 dirname, textlow, texthigh. */
7915
7916 static struct partial_symtab *
7917 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7918 {
7919 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7920 struct partial_symtab *pst;
7921
7922 pst = start_psymtab_common (objfile, name, 0,
7923 objfile->global_psymbols,
7924 objfile->static_psymbols);
7925
7926 pst->psymtabs_addrmap_supported = 1;
7927
7928 /* This is the glue that links PST into GDB's symbol API. */
7929 pst->read_symtab_private = per_cu;
7930 pst->read_symtab = dwarf2_read_symtab;
7931 per_cu->v.psymtab = pst;
7932
7933 return pst;
7934 }
7935
7936 /* The DATA object passed to process_psymtab_comp_unit_reader has this
7937 type. */
7938
7939 struct process_psymtab_comp_unit_data
7940 {
7941 /* True if we are reading a DW_TAG_partial_unit. */
7942
7943 int want_partial_unit;
7944
7945 /* The "pretend" language that is used if the CU doesn't declare a
7946 language. */
7947
7948 enum language pretend_language;
7949 };
7950
7951 /* die_reader_func for process_psymtab_comp_unit. */
7952
7953 static void
7954 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7955 const gdb_byte *info_ptr,
7956 struct die_info *comp_unit_die,
7957 int has_children,
7958 void *data)
7959 {
7960 struct dwarf2_cu *cu = reader->cu;
7961 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7962 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7963 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7964 CORE_ADDR baseaddr;
7965 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7966 struct partial_symtab *pst;
7967 enum pc_bounds_kind cu_bounds_kind;
7968 const char *filename;
7969 struct process_psymtab_comp_unit_data *info
7970 = (struct process_psymtab_comp_unit_data *) data;
7971
7972 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
7973 return;
7974
7975 gdb_assert (! per_cu->is_debug_types);
7976
7977 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
7978
7979 cu->list_in_scope = &file_symbols;
7980
7981 /* Allocate a new partial symbol table structure. */
7982 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7983 if (filename == NULL)
7984 filename = "";
7985
7986 pst = create_partial_symtab (per_cu, filename);
7987
7988 /* This must be done before calling dwarf2_build_include_psymtabs. */
7989 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7990
7991 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7992
7993 dwarf2_find_base_address (comp_unit_die, cu);
7994
7995 /* Possibly set the default values of LOWPC and HIGHPC from
7996 `DW_AT_ranges'. */
7997 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7998 &best_highpc, cu, pst);
7999 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8000 /* Store the contiguous range if it is not empty; it can be empty for
8001 CUs with no code. */
8002 addrmap_set_empty (objfile->psymtabs_addrmap,
8003 gdbarch_adjust_dwarf2_addr (gdbarch,
8004 best_lowpc + baseaddr),
8005 gdbarch_adjust_dwarf2_addr (gdbarch,
8006 best_highpc + baseaddr) - 1,
8007 pst);
8008
8009 /* Check if comp unit has_children.
8010 If so, read the rest of the partial symbols from this comp unit.
8011 If not, there's no more debug_info for this comp unit. */
8012 if (has_children)
8013 {
8014 struct partial_die_info *first_die;
8015 CORE_ADDR lowpc, highpc;
8016
8017 lowpc = ((CORE_ADDR) -1);
8018 highpc = ((CORE_ADDR) 0);
8019
8020 first_die = load_partial_dies (reader, info_ptr, 1);
8021
8022 scan_partial_symbols (first_die, &lowpc, &highpc,
8023 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8024
8025 /* If we didn't find a lowpc, set it to highpc to avoid
8026 complaints from `maint check'. */
8027 if (lowpc == ((CORE_ADDR) -1))
8028 lowpc = highpc;
8029
8030 /* If the compilation unit didn't have an explicit address range,
8031 then use the information extracted from its child dies. */
8032 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8033 {
8034 best_lowpc = lowpc;
8035 best_highpc = highpc;
8036 }
8037 }
8038 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8039 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
8040
8041 end_psymtab_common (objfile, pst);
8042
8043 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8044 {
8045 int i;
8046 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8047 struct dwarf2_per_cu_data *iter;
8048
8049 /* Fill in 'dependencies' here; we fill in 'users' in a
8050 post-pass. */
8051 pst->number_of_dependencies = len;
8052 pst->dependencies =
8053 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8054 for (i = 0;
8055 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8056 i, iter);
8057 ++i)
8058 pst->dependencies[i] = iter->v.psymtab;
8059
8060 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8061 }
8062
8063 /* Get the list of files included in the current compilation unit,
8064 and build a psymtab for each of them. */
8065 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8066
8067 if (dwarf_read_debug)
8068 {
8069 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8070
8071 fprintf_unfiltered (gdb_stdlog,
8072 "Psymtab for %s unit @%s: %s - %s"
8073 ", %d global, %d static syms\n",
8074 per_cu->is_debug_types ? "type" : "comp",
8075 sect_offset_str (per_cu->sect_off),
8076 paddress (gdbarch, pst->textlow),
8077 paddress (gdbarch, pst->texthigh),
8078 pst->n_global_syms, pst->n_static_syms);
8079 }
8080 }
8081
8082 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8083 Process compilation unit THIS_CU for a psymtab. */
8084
8085 static void
8086 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8087 int want_partial_unit,
8088 enum language pretend_language)
8089 {
8090 /* If this compilation unit was already read in, free the
8091 cached copy in order to read it in again. This is
8092 necessary because we skipped some symbols when we first
8093 read in the compilation unit (see load_partial_dies).
8094 This problem could be avoided, but the benefit is unclear. */
8095 if (this_cu->cu != NULL)
8096 free_one_cached_comp_unit (this_cu);
8097
8098 if (this_cu->is_debug_types)
8099 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8100 NULL);
8101 else
8102 {
8103 process_psymtab_comp_unit_data info;
8104 info.want_partial_unit = want_partial_unit;
8105 info.pretend_language = pretend_language;
8106 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8107 process_psymtab_comp_unit_reader, &info);
8108 }
8109
8110 /* Age out any secondary CUs. */
8111 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8112 }
8113
8114 /* Reader function for build_type_psymtabs. */
8115
8116 static void
8117 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8118 const gdb_byte *info_ptr,
8119 struct die_info *type_unit_die,
8120 int has_children,
8121 void *data)
8122 {
8123 struct dwarf2_per_objfile *dwarf2_per_objfile
8124 = reader->cu->per_cu->dwarf2_per_objfile;
8125 struct objfile *objfile = dwarf2_per_objfile->objfile;
8126 struct dwarf2_cu *cu = reader->cu;
8127 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8128 struct signatured_type *sig_type;
8129 struct type_unit_group *tu_group;
8130 struct attribute *attr;
8131 struct partial_die_info *first_die;
8132 CORE_ADDR lowpc, highpc;
8133 struct partial_symtab *pst;
8134
8135 gdb_assert (data == NULL);
8136 gdb_assert (per_cu->is_debug_types);
8137 sig_type = (struct signatured_type *) per_cu;
8138
8139 if (! has_children)
8140 return;
8141
8142 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8143 tu_group = get_type_unit_group (cu, attr);
8144
8145 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8146
8147 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8148 cu->list_in_scope = &file_symbols;
8149 pst = create_partial_symtab (per_cu, "");
8150 pst->anonymous = 1;
8151
8152 first_die = load_partial_dies (reader, info_ptr, 1);
8153
8154 lowpc = (CORE_ADDR) -1;
8155 highpc = (CORE_ADDR) 0;
8156 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8157
8158 end_psymtab_common (objfile, pst);
8159 }
8160
8161 /* Struct used to sort TUs by their abbreviation table offset. */
8162
8163 struct tu_abbrev_offset
8164 {
8165 struct signatured_type *sig_type;
8166 sect_offset abbrev_offset;
8167 };
8168
8169 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8170
8171 static bool
8172 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8173 const struct tu_abbrev_offset &b)
8174 {
8175 return a.abbrev_offset < b.abbrev_offset;
8176 }
8177
8178 /* Efficiently read all the type units.
8179 This does the bulk of the work for build_type_psymtabs.
8180
8181 The efficiency is because we sort TUs by the abbrev table they use and
8182 only read each abbrev table once. In one program there are 200K TUs
8183 sharing 8K abbrev tables.
8184
8185 The main purpose of this function is to support building the
8186 dwarf2_per_objfile->type_unit_groups table.
8187 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8188 can collapse the search space by grouping them by stmt_list.
8189 The savings can be significant, in the same program from above the 200K TUs
8190 share 8K stmt_list tables.
8191
8192 FUNC is expected to call get_type_unit_group, which will create the
8193 struct type_unit_group if necessary and add it to
8194 dwarf2_per_objfile->type_unit_groups. */
8195
8196 static void
8197 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8198 {
8199 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8200 abbrev_table_up abbrev_table;
8201 sect_offset abbrev_offset;
8202 int i;
8203
8204 /* It's up to the caller to not call us multiple times. */
8205 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8206
8207 if (dwarf2_per_objfile->n_type_units == 0)
8208 return;
8209
8210 /* TUs typically share abbrev tables, and there can be way more TUs than
8211 abbrev tables. Sort by abbrev table to reduce the number of times we
8212 read each abbrev table in.
8213 Alternatives are to punt or to maintain a cache of abbrev tables.
8214 This is simpler and efficient enough for now.
8215
8216 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8217 symtab to use). Typically TUs with the same abbrev offset have the same
8218 stmt_list value too so in practice this should work well.
8219
8220 The basic algorithm here is:
8221
8222 sort TUs by abbrev table
8223 for each TU with same abbrev table:
8224 read abbrev table if first user
8225 read TU top level DIE
8226 [IWBN if DWO skeletons had DW_AT_stmt_list]
8227 call FUNC */
8228
8229 if (dwarf_read_debug)
8230 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8231
8232 /* Sort in a separate table to maintain the order of all_type_units
8233 for .gdb_index: TU indices directly index all_type_units. */
8234 std::vector<struct tu_abbrev_offset> sorted_by_abbrev
8235 (dwarf2_per_objfile->n_type_units);
8236 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8237 {
8238 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8239
8240 sorted_by_abbrev[i].sig_type = sig_type;
8241 sorted_by_abbrev[i].abbrev_offset =
8242 read_abbrev_offset (dwarf2_per_objfile,
8243 sig_type->per_cu.section,
8244 sig_type->per_cu.sect_off);
8245 }
8246 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8247 sort_tu_by_abbrev_offset);
8248
8249 abbrev_offset = (sect_offset) ~(unsigned) 0;
8250
8251 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8252 {
8253 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8254
8255 /* Switch to the next abbrev table if necessary. */
8256 if (abbrev_table == NULL
8257 || tu->abbrev_offset != abbrev_offset)
8258 {
8259 abbrev_offset = tu->abbrev_offset;
8260 abbrev_table =
8261 abbrev_table_read_table (dwarf2_per_objfile,
8262 &dwarf2_per_objfile->abbrev,
8263 abbrev_offset);
8264 ++tu_stats->nr_uniq_abbrev_tables;
8265 }
8266
8267 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table.get (),
8268 0, 0, build_type_psymtabs_reader, NULL);
8269 }
8270 }
8271
8272 /* Print collected type unit statistics. */
8273
8274 static void
8275 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8276 {
8277 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8278
8279 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8280 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8281 dwarf2_per_objfile->n_type_units);
8282 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8283 tu_stats->nr_uniq_abbrev_tables);
8284 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8285 tu_stats->nr_symtabs);
8286 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8287 tu_stats->nr_symtab_sharers);
8288 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8289 tu_stats->nr_stmt_less_type_units);
8290 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8291 tu_stats->nr_all_type_units_reallocs);
8292 }
8293
8294 /* Traversal function for build_type_psymtabs. */
8295
8296 static int
8297 build_type_psymtab_dependencies (void **slot, void *info)
8298 {
8299 struct dwarf2_per_objfile *dwarf2_per_objfile
8300 = (struct dwarf2_per_objfile *) info;
8301 struct objfile *objfile = dwarf2_per_objfile->objfile;
8302 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8303 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8304 struct partial_symtab *pst = per_cu->v.psymtab;
8305 int len = VEC_length (sig_type_ptr, tu_group->tus);
8306 struct signatured_type *iter;
8307 int i;
8308
8309 gdb_assert (len > 0);
8310 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8311
8312 pst->number_of_dependencies = len;
8313 pst->dependencies =
8314 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8315 for (i = 0;
8316 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8317 ++i)
8318 {
8319 gdb_assert (iter->per_cu.is_debug_types);
8320 pst->dependencies[i] = iter->per_cu.v.psymtab;
8321 iter->type_unit_group = tu_group;
8322 }
8323
8324 VEC_free (sig_type_ptr, tu_group->tus);
8325
8326 return 1;
8327 }
8328
8329 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8330 Build partial symbol tables for the .debug_types comp-units. */
8331
8332 static void
8333 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8334 {
8335 if (! create_all_type_units (dwarf2_per_objfile))
8336 return;
8337
8338 build_type_psymtabs_1 (dwarf2_per_objfile);
8339 }
8340
8341 /* Traversal function for process_skeletonless_type_unit.
8342 Read a TU in a DWO file and build partial symbols for it. */
8343
8344 static int
8345 process_skeletonless_type_unit (void **slot, void *info)
8346 {
8347 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8348 struct dwarf2_per_objfile *dwarf2_per_objfile
8349 = (struct dwarf2_per_objfile *) info;
8350 struct signatured_type find_entry, *entry;
8351
8352 /* If this TU doesn't exist in the global table, add it and read it in. */
8353
8354 if (dwarf2_per_objfile->signatured_types == NULL)
8355 {
8356 dwarf2_per_objfile->signatured_types
8357 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8358 }
8359
8360 find_entry.signature = dwo_unit->signature;
8361 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8362 INSERT);
8363 /* If we've already seen this type there's nothing to do. What's happening
8364 is we're doing our own version of comdat-folding here. */
8365 if (*slot != NULL)
8366 return 1;
8367
8368 /* This does the job that create_all_type_units would have done for
8369 this TU. */
8370 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8371 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8372 *slot = entry;
8373
8374 /* This does the job that build_type_psymtabs_1 would have done. */
8375 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8376 build_type_psymtabs_reader, NULL);
8377
8378 return 1;
8379 }
8380
8381 /* Traversal function for process_skeletonless_type_units. */
8382
8383 static int
8384 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8385 {
8386 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8387
8388 if (dwo_file->tus != NULL)
8389 {
8390 htab_traverse_noresize (dwo_file->tus,
8391 process_skeletonless_type_unit, info);
8392 }
8393
8394 return 1;
8395 }
8396
8397 /* Scan all TUs of DWO files, verifying we've processed them.
8398 This is needed in case a TU was emitted without its skeleton.
8399 Note: This can't be done until we know what all the DWO files are. */
8400
8401 static void
8402 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8403 {
8404 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8405 if (get_dwp_file (dwarf2_per_objfile) == NULL
8406 && dwarf2_per_objfile->dwo_files != NULL)
8407 {
8408 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8409 process_dwo_file_for_skeletonless_type_units,
8410 dwarf2_per_objfile);
8411 }
8412 }
8413
8414 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8415
8416 static void
8417 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8418 {
8419 int i;
8420
8421 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8422 {
8423 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cu (i);
8424 struct partial_symtab *pst = per_cu->v.psymtab;
8425 int j;
8426
8427 if (pst == NULL)
8428 continue;
8429
8430 for (j = 0; j < pst->number_of_dependencies; ++j)
8431 {
8432 /* Set the 'user' field only if it is not already set. */
8433 if (pst->dependencies[j]->user == NULL)
8434 pst->dependencies[j]->user = pst;
8435 }
8436 }
8437 }
8438
8439 /* Build the partial symbol table by doing a quick pass through the
8440 .debug_info and .debug_abbrev sections. */
8441
8442 static void
8443 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8444 {
8445 int i;
8446 struct objfile *objfile = dwarf2_per_objfile->objfile;
8447
8448 if (dwarf_read_debug)
8449 {
8450 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8451 objfile_name (objfile));
8452 }
8453
8454 dwarf2_per_objfile->reading_partial_symbols = 1;
8455
8456 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8457
8458 /* Any cached compilation units will be linked by the per-objfile
8459 read_in_chain. Make sure to free them when we're done. */
8460 free_cached_comp_units freer (dwarf2_per_objfile);
8461
8462 build_type_psymtabs (dwarf2_per_objfile);
8463
8464 create_all_comp_units (dwarf2_per_objfile);
8465
8466 /* Create a temporary address map on a temporary obstack. We later
8467 copy this to the final obstack. */
8468 auto_obstack temp_obstack;
8469
8470 scoped_restore save_psymtabs_addrmap
8471 = make_scoped_restore (&objfile->psymtabs_addrmap,
8472 addrmap_create_mutable (&temp_obstack));
8473
8474 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
8475 {
8476 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cu (i);
8477
8478 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8479 }
8480
8481 /* This has to wait until we read the CUs, we need the list of DWOs. */
8482 process_skeletonless_type_units (dwarf2_per_objfile);
8483
8484 /* Now that all TUs have been processed we can fill in the dependencies. */
8485 if (dwarf2_per_objfile->type_unit_groups != NULL)
8486 {
8487 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8488 build_type_psymtab_dependencies, dwarf2_per_objfile);
8489 }
8490
8491 if (dwarf_read_debug)
8492 print_tu_stats (dwarf2_per_objfile);
8493
8494 set_partial_user (dwarf2_per_objfile);
8495
8496 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8497 &objfile->objfile_obstack);
8498 /* At this point we want to keep the address map. */
8499 save_psymtabs_addrmap.release ();
8500
8501 if (dwarf_read_debug)
8502 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8503 objfile_name (objfile));
8504 }
8505
8506 /* die_reader_func for load_partial_comp_unit. */
8507
8508 static void
8509 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8510 const gdb_byte *info_ptr,
8511 struct die_info *comp_unit_die,
8512 int has_children,
8513 void *data)
8514 {
8515 struct dwarf2_cu *cu = reader->cu;
8516
8517 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8518
8519 /* Check if comp unit has_children.
8520 If so, read the rest of the partial symbols from this comp unit.
8521 If not, there's no more debug_info for this comp unit. */
8522 if (has_children)
8523 load_partial_dies (reader, info_ptr, 0);
8524 }
8525
8526 /* Load the partial DIEs for a secondary CU into memory.
8527 This is also used when rereading a primary CU with load_all_dies. */
8528
8529 static void
8530 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8531 {
8532 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8533 load_partial_comp_unit_reader, NULL);
8534 }
8535
8536 static void
8537 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8538 struct dwarf2_section_info *section,
8539 struct dwarf2_section_info *abbrev_section,
8540 unsigned int is_dwz,
8541 int *n_allocated,
8542 int *n_comp_units,
8543 struct dwarf2_per_cu_data ***all_comp_units)
8544 {
8545 const gdb_byte *info_ptr;
8546 struct objfile *objfile = dwarf2_per_objfile->objfile;
8547
8548 if (dwarf_read_debug)
8549 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8550 get_section_name (section),
8551 get_section_file_name (section));
8552
8553 dwarf2_read_section (objfile, section);
8554
8555 info_ptr = section->buffer;
8556
8557 while (info_ptr < section->buffer + section->size)
8558 {
8559 struct dwarf2_per_cu_data *this_cu;
8560
8561 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8562
8563 comp_unit_head cu_header;
8564 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8565 abbrev_section, info_ptr,
8566 rcuh_kind::COMPILE);
8567
8568 /* Save the compilation unit for later lookup. */
8569 if (cu_header.unit_type != DW_UT_type)
8570 {
8571 this_cu = XOBNEW (&objfile->objfile_obstack,
8572 struct dwarf2_per_cu_data);
8573 memset (this_cu, 0, sizeof (*this_cu));
8574 }
8575 else
8576 {
8577 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8578 struct signatured_type);
8579 memset (sig_type, 0, sizeof (*sig_type));
8580 sig_type->signature = cu_header.signature;
8581 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8582 this_cu = &sig_type->per_cu;
8583 }
8584 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8585 this_cu->sect_off = sect_off;
8586 this_cu->length = cu_header.length + cu_header.initial_length_size;
8587 this_cu->is_dwz = is_dwz;
8588 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8589 this_cu->section = section;
8590
8591 if (*n_comp_units == *n_allocated)
8592 {
8593 *n_allocated *= 2;
8594 *all_comp_units = XRESIZEVEC (struct dwarf2_per_cu_data *,
8595 *all_comp_units, *n_allocated);
8596 }
8597 (*all_comp_units)[*n_comp_units] = this_cu;
8598 ++*n_comp_units;
8599
8600 info_ptr = info_ptr + this_cu->length;
8601 }
8602 }
8603
8604 /* Create a list of all compilation units in OBJFILE.
8605 This is only done for -readnow and building partial symtabs. */
8606
8607 static void
8608 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8609 {
8610 int n_allocated;
8611 int n_comp_units;
8612 struct dwarf2_per_cu_data **all_comp_units;
8613 struct dwz_file *dwz;
8614 struct objfile *objfile = dwarf2_per_objfile->objfile;
8615
8616 n_comp_units = 0;
8617 n_allocated = 10;
8618 all_comp_units = XNEWVEC (struct dwarf2_per_cu_data *, n_allocated);
8619
8620 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8621 &dwarf2_per_objfile->abbrev, 0,
8622 &n_allocated, &n_comp_units, &all_comp_units);
8623
8624 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8625 if (dwz != NULL)
8626 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8627 1, &n_allocated, &n_comp_units,
8628 &all_comp_units);
8629
8630 dwarf2_per_objfile->all_comp_units = XOBNEWVEC (&objfile->objfile_obstack,
8631 struct dwarf2_per_cu_data *,
8632 n_comp_units);
8633 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
8634 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
8635 xfree (all_comp_units);
8636 dwarf2_per_objfile->n_comp_units = n_comp_units;
8637 }
8638
8639 /* Process all loaded DIEs for compilation unit CU, starting at
8640 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8641 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8642 DW_AT_ranges). See the comments of add_partial_subprogram on how
8643 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8644
8645 static void
8646 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8647 CORE_ADDR *highpc, int set_addrmap,
8648 struct dwarf2_cu *cu)
8649 {
8650 struct partial_die_info *pdi;
8651
8652 /* Now, march along the PDI's, descending into ones which have
8653 interesting children but skipping the children of the other ones,
8654 until we reach the end of the compilation unit. */
8655
8656 pdi = first_die;
8657
8658 while (pdi != NULL)
8659 {
8660 pdi->fixup (cu);
8661
8662 /* Anonymous namespaces or modules have no name but have interesting
8663 children, so we need to look at them. Ditto for anonymous
8664 enums. */
8665
8666 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8667 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8668 || pdi->tag == DW_TAG_imported_unit
8669 || pdi->tag == DW_TAG_inlined_subroutine)
8670 {
8671 switch (pdi->tag)
8672 {
8673 case DW_TAG_subprogram:
8674 case DW_TAG_inlined_subroutine:
8675 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8676 break;
8677 case DW_TAG_constant:
8678 case DW_TAG_variable:
8679 case DW_TAG_typedef:
8680 case DW_TAG_union_type:
8681 if (!pdi->is_declaration)
8682 {
8683 add_partial_symbol (pdi, cu);
8684 }
8685 break;
8686 case DW_TAG_class_type:
8687 case DW_TAG_interface_type:
8688 case DW_TAG_structure_type:
8689 if (!pdi->is_declaration)
8690 {
8691 add_partial_symbol (pdi, cu);
8692 }
8693 if ((cu->language == language_rust
8694 || cu->language == language_cplus) && pdi->has_children)
8695 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8696 set_addrmap, cu);
8697 break;
8698 case DW_TAG_enumeration_type:
8699 if (!pdi->is_declaration)
8700 add_partial_enumeration (pdi, cu);
8701 break;
8702 case DW_TAG_base_type:
8703 case DW_TAG_subrange_type:
8704 /* File scope base type definitions are added to the partial
8705 symbol table. */
8706 add_partial_symbol (pdi, cu);
8707 break;
8708 case DW_TAG_namespace:
8709 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8710 break;
8711 case DW_TAG_module:
8712 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8713 break;
8714 case DW_TAG_imported_unit:
8715 {
8716 struct dwarf2_per_cu_data *per_cu;
8717
8718 /* For now we don't handle imported units in type units. */
8719 if (cu->per_cu->is_debug_types)
8720 {
8721 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8722 " supported in type units [in module %s]"),
8723 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8724 }
8725
8726 per_cu = dwarf2_find_containing_comp_unit
8727 (pdi->d.sect_off, pdi->is_dwz,
8728 cu->per_cu->dwarf2_per_objfile);
8729
8730 /* Go read the partial unit, if needed. */
8731 if (per_cu->v.psymtab == NULL)
8732 process_psymtab_comp_unit (per_cu, 1, cu->language);
8733
8734 VEC_safe_push (dwarf2_per_cu_ptr,
8735 cu->per_cu->imported_symtabs, per_cu);
8736 }
8737 break;
8738 case DW_TAG_imported_declaration:
8739 add_partial_symbol (pdi, cu);
8740 break;
8741 default:
8742 break;
8743 }
8744 }
8745
8746 /* If the die has a sibling, skip to the sibling. */
8747
8748 pdi = pdi->die_sibling;
8749 }
8750 }
8751
8752 /* Functions used to compute the fully scoped name of a partial DIE.
8753
8754 Normally, this is simple. For C++, the parent DIE's fully scoped
8755 name is concatenated with "::" and the partial DIE's name.
8756 Enumerators are an exception; they use the scope of their parent
8757 enumeration type, i.e. the name of the enumeration type is not
8758 prepended to the enumerator.
8759
8760 There are two complexities. One is DW_AT_specification; in this
8761 case "parent" means the parent of the target of the specification,
8762 instead of the direct parent of the DIE. The other is compilers
8763 which do not emit DW_TAG_namespace; in this case we try to guess
8764 the fully qualified name of structure types from their members'
8765 linkage names. This must be done using the DIE's children rather
8766 than the children of any DW_AT_specification target. We only need
8767 to do this for structures at the top level, i.e. if the target of
8768 any DW_AT_specification (if any; otherwise the DIE itself) does not
8769 have a parent. */
8770
8771 /* Compute the scope prefix associated with PDI's parent, in
8772 compilation unit CU. The result will be allocated on CU's
8773 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8774 field. NULL is returned if no prefix is necessary. */
8775 static const char *
8776 partial_die_parent_scope (struct partial_die_info *pdi,
8777 struct dwarf2_cu *cu)
8778 {
8779 const char *grandparent_scope;
8780 struct partial_die_info *parent, *real_pdi;
8781
8782 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8783 then this means the parent of the specification DIE. */
8784
8785 real_pdi = pdi;
8786 while (real_pdi->has_specification)
8787 real_pdi = find_partial_die (real_pdi->spec_offset,
8788 real_pdi->spec_is_dwz, cu);
8789
8790 parent = real_pdi->die_parent;
8791 if (parent == NULL)
8792 return NULL;
8793
8794 if (parent->scope_set)
8795 return parent->scope;
8796
8797 parent->fixup (cu);
8798
8799 grandparent_scope = partial_die_parent_scope (parent, cu);
8800
8801 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8802 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8803 Work around this problem here. */
8804 if (cu->language == language_cplus
8805 && parent->tag == DW_TAG_namespace
8806 && strcmp (parent->name, "::") == 0
8807 && grandparent_scope == NULL)
8808 {
8809 parent->scope = NULL;
8810 parent->scope_set = 1;
8811 return NULL;
8812 }
8813
8814 if (pdi->tag == DW_TAG_enumerator)
8815 /* Enumerators should not get the name of the enumeration as a prefix. */
8816 parent->scope = grandparent_scope;
8817 else if (parent->tag == DW_TAG_namespace
8818 || parent->tag == DW_TAG_module
8819 || parent->tag == DW_TAG_structure_type
8820 || parent->tag == DW_TAG_class_type
8821 || parent->tag == DW_TAG_interface_type
8822 || parent->tag == DW_TAG_union_type
8823 || parent->tag == DW_TAG_enumeration_type)
8824 {
8825 if (grandparent_scope == NULL)
8826 parent->scope = parent->name;
8827 else
8828 parent->scope = typename_concat (&cu->comp_unit_obstack,
8829 grandparent_scope,
8830 parent->name, 0, cu);
8831 }
8832 else
8833 {
8834 /* FIXME drow/2004-04-01: What should we be doing with
8835 function-local names? For partial symbols, we should probably be
8836 ignoring them. */
8837 complaint (&symfile_complaints,
8838 _("unhandled containing DIE tag %d for DIE at %s"),
8839 parent->tag, sect_offset_str (pdi->sect_off));
8840 parent->scope = grandparent_scope;
8841 }
8842
8843 parent->scope_set = 1;
8844 return parent->scope;
8845 }
8846
8847 /* Return the fully scoped name associated with PDI, from compilation unit
8848 CU. The result will be allocated with malloc. */
8849
8850 static char *
8851 partial_die_full_name (struct partial_die_info *pdi,
8852 struct dwarf2_cu *cu)
8853 {
8854 const char *parent_scope;
8855
8856 /* If this is a template instantiation, we can not work out the
8857 template arguments from partial DIEs. So, unfortunately, we have
8858 to go through the full DIEs. At least any work we do building
8859 types here will be reused if full symbols are loaded later. */
8860 if (pdi->has_template_arguments)
8861 {
8862 pdi->fixup (cu);
8863
8864 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8865 {
8866 struct die_info *die;
8867 struct attribute attr;
8868 struct dwarf2_cu *ref_cu = cu;
8869
8870 /* DW_FORM_ref_addr is using section offset. */
8871 attr.name = (enum dwarf_attribute) 0;
8872 attr.form = DW_FORM_ref_addr;
8873 attr.u.unsnd = to_underlying (pdi->sect_off);
8874 die = follow_die_ref (NULL, &attr, &ref_cu);
8875
8876 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8877 }
8878 }
8879
8880 parent_scope = partial_die_parent_scope (pdi, cu);
8881 if (parent_scope == NULL)
8882 return NULL;
8883 else
8884 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
8885 }
8886
8887 static void
8888 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8889 {
8890 struct dwarf2_per_objfile *dwarf2_per_objfile
8891 = cu->per_cu->dwarf2_per_objfile;
8892 struct objfile *objfile = dwarf2_per_objfile->objfile;
8893 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8894 CORE_ADDR addr = 0;
8895 const char *actual_name = NULL;
8896 CORE_ADDR baseaddr;
8897 char *built_actual_name;
8898
8899 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8900
8901 built_actual_name = partial_die_full_name (pdi, cu);
8902 if (built_actual_name != NULL)
8903 actual_name = built_actual_name;
8904
8905 if (actual_name == NULL)
8906 actual_name = pdi->name;
8907
8908 switch (pdi->tag)
8909 {
8910 case DW_TAG_inlined_subroutine:
8911 case DW_TAG_subprogram:
8912 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
8913 if (pdi->is_external || cu->language == language_ada)
8914 {
8915 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8916 of the global scope. But in Ada, we want to be able to access
8917 nested procedures globally. So all Ada subprograms are stored
8918 in the global scope. */
8919 add_psymbol_to_list (actual_name, strlen (actual_name),
8920 built_actual_name != NULL,
8921 VAR_DOMAIN, LOC_BLOCK,
8922 &objfile->global_psymbols,
8923 addr, cu->language, objfile);
8924 }
8925 else
8926 {
8927 add_psymbol_to_list (actual_name, strlen (actual_name),
8928 built_actual_name != NULL,
8929 VAR_DOMAIN, LOC_BLOCK,
8930 &objfile->static_psymbols,
8931 addr, cu->language, objfile);
8932 }
8933
8934 if (pdi->main_subprogram && actual_name != NULL)
8935 set_objfile_main_name (objfile, actual_name, cu->language);
8936 break;
8937 case DW_TAG_constant:
8938 {
8939 std::vector<partial_symbol *> *list;
8940
8941 if (pdi->is_external)
8942 list = &objfile->global_psymbols;
8943 else
8944 list = &objfile->static_psymbols;
8945 add_psymbol_to_list (actual_name, strlen (actual_name),
8946 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8947 list, 0, cu->language, objfile);
8948 }
8949 break;
8950 case DW_TAG_variable:
8951 if (pdi->d.locdesc)
8952 addr = decode_locdesc (pdi->d.locdesc, cu);
8953
8954 if (pdi->d.locdesc
8955 && addr == 0
8956 && !dwarf2_per_objfile->has_section_at_zero)
8957 {
8958 /* A global or static variable may also have been stripped
8959 out by the linker if unused, in which case its address
8960 will be nullified; do not add such variables into partial
8961 symbol table then. */
8962 }
8963 else if (pdi->is_external)
8964 {
8965 /* Global Variable.
8966 Don't enter into the minimal symbol tables as there is
8967 a minimal symbol table entry from the ELF symbols already.
8968 Enter into partial symbol table if it has a location
8969 descriptor or a type.
8970 If the location descriptor is missing, new_symbol will create
8971 a LOC_UNRESOLVED symbol, the address of the variable will then
8972 be determined from the minimal symbol table whenever the variable
8973 is referenced.
8974 The address for the partial symbol table entry is not
8975 used by GDB, but it comes in handy for debugging partial symbol
8976 table building. */
8977
8978 if (pdi->d.locdesc || pdi->has_type)
8979 add_psymbol_to_list (actual_name, strlen (actual_name),
8980 built_actual_name != NULL,
8981 VAR_DOMAIN, LOC_STATIC,
8982 &objfile->global_psymbols,
8983 addr + baseaddr,
8984 cu->language, objfile);
8985 }
8986 else
8987 {
8988 int has_loc = pdi->d.locdesc != NULL;
8989
8990 /* Static Variable. Skip symbols whose value we cannot know (those
8991 without location descriptors or constant values). */
8992 if (!has_loc && !pdi->has_const_value)
8993 {
8994 xfree (built_actual_name);
8995 return;
8996 }
8997
8998 add_psymbol_to_list (actual_name, strlen (actual_name),
8999 built_actual_name != NULL,
9000 VAR_DOMAIN, LOC_STATIC,
9001 &objfile->static_psymbols,
9002 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
9003 cu->language, objfile);
9004 }
9005 break;
9006 case DW_TAG_typedef:
9007 case DW_TAG_base_type:
9008 case DW_TAG_subrange_type:
9009 add_psymbol_to_list (actual_name, strlen (actual_name),
9010 built_actual_name != NULL,
9011 VAR_DOMAIN, LOC_TYPEDEF,
9012 &objfile->static_psymbols,
9013 0, cu->language, objfile);
9014 break;
9015 case DW_TAG_imported_declaration:
9016 case DW_TAG_namespace:
9017 add_psymbol_to_list (actual_name, strlen (actual_name),
9018 built_actual_name != NULL,
9019 VAR_DOMAIN, LOC_TYPEDEF,
9020 &objfile->global_psymbols,
9021 0, cu->language, objfile);
9022 break;
9023 case DW_TAG_module:
9024 add_psymbol_to_list (actual_name, strlen (actual_name),
9025 built_actual_name != NULL,
9026 MODULE_DOMAIN, LOC_TYPEDEF,
9027 &objfile->global_psymbols,
9028 0, cu->language, objfile);
9029 break;
9030 case DW_TAG_class_type:
9031 case DW_TAG_interface_type:
9032 case DW_TAG_structure_type:
9033 case DW_TAG_union_type:
9034 case DW_TAG_enumeration_type:
9035 /* Skip external references. The DWARF standard says in the section
9036 about "Structure, Union, and Class Type Entries": "An incomplete
9037 structure, union or class type is represented by a structure,
9038 union or class entry that does not have a byte size attribute
9039 and that has a DW_AT_declaration attribute." */
9040 if (!pdi->has_byte_size && pdi->is_declaration)
9041 {
9042 xfree (built_actual_name);
9043 return;
9044 }
9045
9046 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9047 static vs. global. */
9048 add_psymbol_to_list (actual_name, strlen (actual_name),
9049 built_actual_name != NULL,
9050 STRUCT_DOMAIN, LOC_TYPEDEF,
9051 cu->language == language_cplus
9052 ? &objfile->global_psymbols
9053 : &objfile->static_psymbols,
9054 0, cu->language, objfile);
9055
9056 break;
9057 case DW_TAG_enumerator:
9058 add_psymbol_to_list (actual_name, strlen (actual_name),
9059 built_actual_name != NULL,
9060 VAR_DOMAIN, LOC_CONST,
9061 cu->language == language_cplus
9062 ? &objfile->global_psymbols
9063 : &objfile->static_psymbols,
9064 0, cu->language, objfile);
9065 break;
9066 default:
9067 break;
9068 }
9069
9070 xfree (built_actual_name);
9071 }
9072
9073 /* Read a partial die corresponding to a namespace; also, add a symbol
9074 corresponding to that namespace to the symbol table. NAMESPACE is
9075 the name of the enclosing namespace. */
9076
9077 static void
9078 add_partial_namespace (struct partial_die_info *pdi,
9079 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9080 int set_addrmap, struct dwarf2_cu *cu)
9081 {
9082 /* Add a symbol for the namespace. */
9083
9084 add_partial_symbol (pdi, cu);
9085
9086 /* Now scan partial symbols in that namespace. */
9087
9088 if (pdi->has_children)
9089 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9090 }
9091
9092 /* Read a partial die corresponding to a Fortran module. */
9093
9094 static void
9095 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9096 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9097 {
9098 /* Add a symbol for the namespace. */
9099
9100 add_partial_symbol (pdi, cu);
9101
9102 /* Now scan partial symbols in that module. */
9103
9104 if (pdi->has_children)
9105 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9106 }
9107
9108 /* Read a partial die corresponding to a subprogram or an inlined
9109 subprogram and create a partial symbol for that subprogram.
9110 When the CU language allows it, this routine also defines a partial
9111 symbol for each nested subprogram that this subprogram contains.
9112 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9113 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9114
9115 PDI may also be a lexical block, in which case we simply search
9116 recursively for subprograms defined inside that lexical block.
9117 Again, this is only performed when the CU language allows this
9118 type of definitions. */
9119
9120 static void
9121 add_partial_subprogram (struct partial_die_info *pdi,
9122 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9123 int set_addrmap, struct dwarf2_cu *cu)
9124 {
9125 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9126 {
9127 if (pdi->has_pc_info)
9128 {
9129 if (pdi->lowpc < *lowpc)
9130 *lowpc = pdi->lowpc;
9131 if (pdi->highpc > *highpc)
9132 *highpc = pdi->highpc;
9133 if (set_addrmap)
9134 {
9135 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9136 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9137 CORE_ADDR baseaddr;
9138 CORE_ADDR highpc;
9139 CORE_ADDR lowpc;
9140
9141 baseaddr = ANOFFSET (objfile->section_offsets,
9142 SECT_OFF_TEXT (objfile));
9143 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9144 pdi->lowpc + baseaddr);
9145 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9146 pdi->highpc + baseaddr);
9147 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9148 cu->per_cu->v.psymtab);
9149 }
9150 }
9151
9152 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9153 {
9154 if (!pdi->is_declaration)
9155 /* Ignore subprogram DIEs that do not have a name, they are
9156 illegal. Do not emit a complaint at this point, we will
9157 do so when we convert this psymtab into a symtab. */
9158 if (pdi->name)
9159 add_partial_symbol (pdi, cu);
9160 }
9161 }
9162
9163 if (! pdi->has_children)
9164 return;
9165
9166 if (cu->language == language_ada)
9167 {
9168 pdi = pdi->die_child;
9169 while (pdi != NULL)
9170 {
9171 pdi->fixup (cu);
9172 if (pdi->tag == DW_TAG_subprogram
9173 || pdi->tag == DW_TAG_inlined_subroutine
9174 || pdi->tag == DW_TAG_lexical_block)
9175 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9176 pdi = pdi->die_sibling;
9177 }
9178 }
9179 }
9180
9181 /* Read a partial die corresponding to an enumeration type. */
9182
9183 static void
9184 add_partial_enumeration (struct partial_die_info *enum_pdi,
9185 struct dwarf2_cu *cu)
9186 {
9187 struct partial_die_info *pdi;
9188
9189 if (enum_pdi->name != NULL)
9190 add_partial_symbol (enum_pdi, cu);
9191
9192 pdi = enum_pdi->die_child;
9193 while (pdi)
9194 {
9195 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9196 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
9197 else
9198 add_partial_symbol (pdi, cu);
9199 pdi = pdi->die_sibling;
9200 }
9201 }
9202
9203 /* Return the initial uleb128 in the die at INFO_PTR. */
9204
9205 static unsigned int
9206 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9207 {
9208 unsigned int bytes_read;
9209
9210 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9211 }
9212
9213 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9214 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9215
9216 Return the corresponding abbrev, or NULL if the number is zero (indicating
9217 an empty DIE). In either case *BYTES_READ will be set to the length of
9218 the initial number. */
9219
9220 static struct abbrev_info *
9221 peek_die_abbrev (const die_reader_specs &reader,
9222 const gdb_byte *info_ptr, unsigned int *bytes_read)
9223 {
9224 dwarf2_cu *cu = reader.cu;
9225 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9226 unsigned int abbrev_number
9227 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9228
9229 if (abbrev_number == 0)
9230 return NULL;
9231
9232 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9233 if (!abbrev)
9234 {
9235 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9236 " at offset %s [in module %s]"),
9237 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9238 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9239 }
9240
9241 return abbrev;
9242 }
9243
9244 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9245 Returns a pointer to the end of a series of DIEs, terminated by an empty
9246 DIE. Any children of the skipped DIEs will also be skipped. */
9247
9248 static const gdb_byte *
9249 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9250 {
9251 while (1)
9252 {
9253 unsigned int bytes_read;
9254 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9255
9256 if (abbrev == NULL)
9257 return info_ptr + bytes_read;
9258 else
9259 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9260 }
9261 }
9262
9263 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9264 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9265 abbrev corresponding to that skipped uleb128 should be passed in
9266 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9267 children. */
9268
9269 static const gdb_byte *
9270 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9271 struct abbrev_info *abbrev)
9272 {
9273 unsigned int bytes_read;
9274 struct attribute attr;
9275 bfd *abfd = reader->abfd;
9276 struct dwarf2_cu *cu = reader->cu;
9277 const gdb_byte *buffer = reader->buffer;
9278 const gdb_byte *buffer_end = reader->buffer_end;
9279 unsigned int form, i;
9280
9281 for (i = 0; i < abbrev->num_attrs; i++)
9282 {
9283 /* The only abbrev we care about is DW_AT_sibling. */
9284 if (abbrev->attrs[i].name == DW_AT_sibling)
9285 {
9286 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9287 if (attr.form == DW_FORM_ref_addr)
9288 complaint (&symfile_complaints,
9289 _("ignoring absolute DW_AT_sibling"));
9290 else
9291 {
9292 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9293 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9294
9295 if (sibling_ptr < info_ptr)
9296 complaint (&symfile_complaints,
9297 _("DW_AT_sibling points backwards"));
9298 else if (sibling_ptr > reader->buffer_end)
9299 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9300 else
9301 return sibling_ptr;
9302 }
9303 }
9304
9305 /* If it isn't DW_AT_sibling, skip this attribute. */
9306 form = abbrev->attrs[i].form;
9307 skip_attribute:
9308 switch (form)
9309 {
9310 case DW_FORM_ref_addr:
9311 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9312 and later it is offset sized. */
9313 if (cu->header.version == 2)
9314 info_ptr += cu->header.addr_size;
9315 else
9316 info_ptr += cu->header.offset_size;
9317 break;
9318 case DW_FORM_GNU_ref_alt:
9319 info_ptr += cu->header.offset_size;
9320 break;
9321 case DW_FORM_addr:
9322 info_ptr += cu->header.addr_size;
9323 break;
9324 case DW_FORM_data1:
9325 case DW_FORM_ref1:
9326 case DW_FORM_flag:
9327 info_ptr += 1;
9328 break;
9329 case DW_FORM_flag_present:
9330 case DW_FORM_implicit_const:
9331 break;
9332 case DW_FORM_data2:
9333 case DW_FORM_ref2:
9334 info_ptr += 2;
9335 break;
9336 case DW_FORM_data4:
9337 case DW_FORM_ref4:
9338 info_ptr += 4;
9339 break;
9340 case DW_FORM_data8:
9341 case DW_FORM_ref8:
9342 case DW_FORM_ref_sig8:
9343 info_ptr += 8;
9344 break;
9345 case DW_FORM_data16:
9346 info_ptr += 16;
9347 break;
9348 case DW_FORM_string:
9349 read_direct_string (abfd, info_ptr, &bytes_read);
9350 info_ptr += bytes_read;
9351 break;
9352 case DW_FORM_sec_offset:
9353 case DW_FORM_strp:
9354 case DW_FORM_GNU_strp_alt:
9355 info_ptr += cu->header.offset_size;
9356 break;
9357 case DW_FORM_exprloc:
9358 case DW_FORM_block:
9359 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9360 info_ptr += bytes_read;
9361 break;
9362 case DW_FORM_block1:
9363 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9364 break;
9365 case DW_FORM_block2:
9366 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9367 break;
9368 case DW_FORM_block4:
9369 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9370 break;
9371 case DW_FORM_sdata:
9372 case DW_FORM_udata:
9373 case DW_FORM_ref_udata:
9374 case DW_FORM_GNU_addr_index:
9375 case DW_FORM_GNU_str_index:
9376 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9377 break;
9378 case DW_FORM_indirect:
9379 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9380 info_ptr += bytes_read;
9381 /* We need to continue parsing from here, so just go back to
9382 the top. */
9383 goto skip_attribute;
9384
9385 default:
9386 error (_("Dwarf Error: Cannot handle %s "
9387 "in DWARF reader [in module %s]"),
9388 dwarf_form_name (form),
9389 bfd_get_filename (abfd));
9390 }
9391 }
9392
9393 if (abbrev->has_children)
9394 return skip_children (reader, info_ptr);
9395 else
9396 return info_ptr;
9397 }
9398
9399 /* Locate ORIG_PDI's sibling.
9400 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9401
9402 static const gdb_byte *
9403 locate_pdi_sibling (const struct die_reader_specs *reader,
9404 struct partial_die_info *orig_pdi,
9405 const gdb_byte *info_ptr)
9406 {
9407 /* Do we know the sibling already? */
9408
9409 if (orig_pdi->sibling)
9410 return orig_pdi->sibling;
9411
9412 /* Are there any children to deal with? */
9413
9414 if (!orig_pdi->has_children)
9415 return info_ptr;
9416
9417 /* Skip the children the long way. */
9418
9419 return skip_children (reader, info_ptr);
9420 }
9421
9422 /* Expand this partial symbol table into a full symbol table. SELF is
9423 not NULL. */
9424
9425 static void
9426 dwarf2_read_symtab (struct partial_symtab *self,
9427 struct objfile *objfile)
9428 {
9429 struct dwarf2_per_objfile *dwarf2_per_objfile
9430 = get_dwarf2_per_objfile (objfile);
9431
9432 if (self->readin)
9433 {
9434 warning (_("bug: psymtab for %s is already read in."),
9435 self->filename);
9436 }
9437 else
9438 {
9439 if (info_verbose)
9440 {
9441 printf_filtered (_("Reading in symbols for %s..."),
9442 self->filename);
9443 gdb_flush (gdb_stdout);
9444 }
9445
9446 /* If this psymtab is constructed from a debug-only objfile, the
9447 has_section_at_zero flag will not necessarily be correct. We
9448 can get the correct value for this flag by looking at the data
9449 associated with the (presumably stripped) associated objfile. */
9450 if (objfile->separate_debug_objfile_backlink)
9451 {
9452 struct dwarf2_per_objfile *dpo_backlink
9453 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9454
9455 dwarf2_per_objfile->has_section_at_zero
9456 = dpo_backlink->has_section_at_zero;
9457 }
9458
9459 dwarf2_per_objfile->reading_partial_symbols = 0;
9460
9461 psymtab_to_symtab_1 (self);
9462
9463 /* Finish up the debug error message. */
9464 if (info_verbose)
9465 printf_filtered (_("done.\n"));
9466 }
9467
9468 process_cu_includes (dwarf2_per_objfile);
9469 }
9470 \f
9471 /* Reading in full CUs. */
9472
9473 /* Add PER_CU to the queue. */
9474
9475 static void
9476 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9477 enum language pretend_language)
9478 {
9479 struct dwarf2_queue_item *item;
9480
9481 per_cu->queued = 1;
9482 item = XNEW (struct dwarf2_queue_item);
9483 item->per_cu = per_cu;
9484 item->pretend_language = pretend_language;
9485 item->next = NULL;
9486
9487 if (dwarf2_queue == NULL)
9488 dwarf2_queue = item;
9489 else
9490 dwarf2_queue_tail->next = item;
9491
9492 dwarf2_queue_tail = item;
9493 }
9494
9495 /* If PER_CU is not yet queued, add it to the queue.
9496 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9497 dependency.
9498 The result is non-zero if PER_CU was queued, otherwise the result is zero
9499 meaning either PER_CU is already queued or it is already loaded.
9500
9501 N.B. There is an invariant here that if a CU is queued then it is loaded.
9502 The caller is required to load PER_CU if we return non-zero. */
9503
9504 static int
9505 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9506 struct dwarf2_per_cu_data *per_cu,
9507 enum language pretend_language)
9508 {
9509 /* We may arrive here during partial symbol reading, if we need full
9510 DIEs to process an unusual case (e.g. template arguments). Do
9511 not queue PER_CU, just tell our caller to load its DIEs. */
9512 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9513 {
9514 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9515 return 1;
9516 return 0;
9517 }
9518
9519 /* Mark the dependence relation so that we don't flush PER_CU
9520 too early. */
9521 if (dependent_cu != NULL)
9522 dwarf2_add_dependence (dependent_cu, per_cu);
9523
9524 /* If it's already on the queue, we have nothing to do. */
9525 if (per_cu->queued)
9526 return 0;
9527
9528 /* If the compilation unit is already loaded, just mark it as
9529 used. */
9530 if (per_cu->cu != NULL)
9531 {
9532 per_cu->cu->last_used = 0;
9533 return 0;
9534 }
9535
9536 /* Add it to the queue. */
9537 queue_comp_unit (per_cu, pretend_language);
9538
9539 return 1;
9540 }
9541
9542 /* Process the queue. */
9543
9544 static void
9545 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9546 {
9547 struct dwarf2_queue_item *item, *next_item;
9548
9549 if (dwarf_read_debug)
9550 {
9551 fprintf_unfiltered (gdb_stdlog,
9552 "Expanding one or more symtabs of objfile %s ...\n",
9553 objfile_name (dwarf2_per_objfile->objfile));
9554 }
9555
9556 /* The queue starts out with one item, but following a DIE reference
9557 may load a new CU, adding it to the end of the queue. */
9558 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9559 {
9560 if ((dwarf2_per_objfile->using_index
9561 ? !item->per_cu->v.quick->compunit_symtab
9562 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9563 /* Skip dummy CUs. */
9564 && item->per_cu->cu != NULL)
9565 {
9566 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9567 unsigned int debug_print_threshold;
9568 char buf[100];
9569
9570 if (per_cu->is_debug_types)
9571 {
9572 struct signatured_type *sig_type =
9573 (struct signatured_type *) per_cu;
9574
9575 sprintf (buf, "TU %s at offset %s",
9576 hex_string (sig_type->signature),
9577 sect_offset_str (per_cu->sect_off));
9578 /* There can be 100s of TUs.
9579 Only print them in verbose mode. */
9580 debug_print_threshold = 2;
9581 }
9582 else
9583 {
9584 sprintf (buf, "CU at offset %s",
9585 sect_offset_str (per_cu->sect_off));
9586 debug_print_threshold = 1;
9587 }
9588
9589 if (dwarf_read_debug >= debug_print_threshold)
9590 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9591
9592 if (per_cu->is_debug_types)
9593 process_full_type_unit (per_cu, item->pretend_language);
9594 else
9595 process_full_comp_unit (per_cu, item->pretend_language);
9596
9597 if (dwarf_read_debug >= debug_print_threshold)
9598 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9599 }
9600
9601 item->per_cu->queued = 0;
9602 next_item = item->next;
9603 xfree (item);
9604 }
9605
9606 dwarf2_queue_tail = NULL;
9607
9608 if (dwarf_read_debug)
9609 {
9610 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9611 objfile_name (dwarf2_per_objfile->objfile));
9612 }
9613 }
9614
9615 /* Read in full symbols for PST, and anything it depends on. */
9616
9617 static void
9618 psymtab_to_symtab_1 (struct partial_symtab *pst)
9619 {
9620 struct dwarf2_per_cu_data *per_cu;
9621 int i;
9622
9623 if (pst->readin)
9624 return;
9625
9626 for (i = 0; i < pst->number_of_dependencies; i++)
9627 if (!pst->dependencies[i]->readin
9628 && pst->dependencies[i]->user == NULL)
9629 {
9630 /* Inform about additional files that need to be read in. */
9631 if (info_verbose)
9632 {
9633 /* FIXME: i18n: Need to make this a single string. */
9634 fputs_filtered (" ", gdb_stdout);
9635 wrap_here ("");
9636 fputs_filtered ("and ", gdb_stdout);
9637 wrap_here ("");
9638 printf_filtered ("%s...", pst->dependencies[i]->filename);
9639 wrap_here (""); /* Flush output. */
9640 gdb_flush (gdb_stdout);
9641 }
9642 psymtab_to_symtab_1 (pst->dependencies[i]);
9643 }
9644
9645 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9646
9647 if (per_cu == NULL)
9648 {
9649 /* It's an include file, no symbols to read for it.
9650 Everything is in the parent symtab. */
9651 pst->readin = 1;
9652 return;
9653 }
9654
9655 dw2_do_instantiate_symtab (per_cu);
9656 }
9657
9658 /* Trivial hash function for die_info: the hash value of a DIE
9659 is its offset in .debug_info for this objfile. */
9660
9661 static hashval_t
9662 die_hash (const void *item)
9663 {
9664 const struct die_info *die = (const struct die_info *) item;
9665
9666 return to_underlying (die->sect_off);
9667 }
9668
9669 /* Trivial comparison function for die_info structures: two DIEs
9670 are equal if they have the same offset. */
9671
9672 static int
9673 die_eq (const void *item_lhs, const void *item_rhs)
9674 {
9675 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9676 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9677
9678 return die_lhs->sect_off == die_rhs->sect_off;
9679 }
9680
9681 /* die_reader_func for load_full_comp_unit.
9682 This is identical to read_signatured_type_reader,
9683 but is kept separate for now. */
9684
9685 static void
9686 load_full_comp_unit_reader (const struct die_reader_specs *reader,
9687 const gdb_byte *info_ptr,
9688 struct die_info *comp_unit_die,
9689 int has_children,
9690 void *data)
9691 {
9692 struct dwarf2_cu *cu = reader->cu;
9693 enum language *language_ptr = (enum language *) data;
9694
9695 gdb_assert (cu->die_hash == NULL);
9696 cu->die_hash =
9697 htab_create_alloc_ex (cu->header.length / 12,
9698 die_hash,
9699 die_eq,
9700 NULL,
9701 &cu->comp_unit_obstack,
9702 hashtab_obstack_allocate,
9703 dummy_obstack_deallocate);
9704
9705 if (has_children)
9706 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9707 &info_ptr, comp_unit_die);
9708 cu->dies = comp_unit_die;
9709 /* comp_unit_die is not stored in die_hash, no need. */
9710
9711 /* We try not to read any attributes in this function, because not
9712 all CUs needed for references have been loaded yet, and symbol
9713 table processing isn't initialized. But we have to set the CU language,
9714 or we won't be able to build types correctly.
9715 Similarly, if we do not read the producer, we can not apply
9716 producer-specific interpretation. */
9717 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
9718 }
9719
9720 /* Load the DIEs associated with PER_CU into memory. */
9721
9722 static void
9723 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9724 enum language pretend_language)
9725 {
9726 gdb_assert (! this_cu->is_debug_types);
9727
9728 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
9729 load_full_comp_unit_reader, &pretend_language);
9730 }
9731
9732 /* Add a DIE to the delayed physname list. */
9733
9734 static void
9735 add_to_method_list (struct type *type, int fnfield_index, int index,
9736 const char *name, struct die_info *die,
9737 struct dwarf2_cu *cu)
9738 {
9739 struct delayed_method_info mi;
9740 mi.type = type;
9741 mi.fnfield_index = fnfield_index;
9742 mi.index = index;
9743 mi.name = name;
9744 mi.die = die;
9745 cu->method_list.push_back (mi);
9746 }
9747
9748 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9749 "const" / "volatile". If so, decrements LEN by the length of the
9750 modifier and return true. Otherwise return false. */
9751
9752 template<size_t N>
9753 static bool
9754 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9755 {
9756 size_t mod_len = sizeof (mod) - 1;
9757 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9758 {
9759 len -= mod_len;
9760 return true;
9761 }
9762 return false;
9763 }
9764
9765 /* Compute the physnames of any methods on the CU's method list.
9766
9767 The computation of method physnames is delayed in order to avoid the
9768 (bad) condition that one of the method's formal parameters is of an as yet
9769 incomplete type. */
9770
9771 static void
9772 compute_delayed_physnames (struct dwarf2_cu *cu)
9773 {
9774 /* Only C++ delays computing physnames. */
9775 if (cu->method_list.empty ())
9776 return;
9777 gdb_assert (cu->language == language_cplus);
9778
9779 for (struct delayed_method_info &mi : cu->method_list)
9780 {
9781 const char *physname;
9782 struct fn_fieldlist *fn_flp
9783 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9784 physname = dwarf2_physname (mi.name, mi.die, cu);
9785 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9786 = physname ? physname : "";
9787
9788 /* Since there's no tag to indicate whether a method is a
9789 const/volatile overload, extract that information out of the
9790 demangled name. */
9791 if (physname != NULL)
9792 {
9793 size_t len = strlen (physname);
9794
9795 while (1)
9796 {
9797 if (physname[len] == ')') /* shortcut */
9798 break;
9799 else if (check_modifier (physname, len, " const"))
9800 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9801 else if (check_modifier (physname, len, " volatile"))
9802 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9803 else
9804 break;
9805 }
9806 }
9807 }
9808
9809 /* The list is no longer needed. */
9810 cu->method_list.clear ();
9811 }
9812
9813 /* Go objects should be embedded in a DW_TAG_module DIE,
9814 and it's not clear if/how imported objects will appear.
9815 To keep Go support simple until that's worked out,
9816 go back through what we've read and create something usable.
9817 We could do this while processing each DIE, and feels kinda cleaner,
9818 but that way is more invasive.
9819 This is to, for example, allow the user to type "p var" or "b main"
9820 without having to specify the package name, and allow lookups
9821 of module.object to work in contexts that use the expression
9822 parser. */
9823
9824 static void
9825 fixup_go_packaging (struct dwarf2_cu *cu)
9826 {
9827 char *package_name = NULL;
9828 struct pending *list;
9829 int i;
9830
9831 for (list = global_symbols; list != NULL; list = list->next)
9832 {
9833 for (i = 0; i < list->nsyms; ++i)
9834 {
9835 struct symbol *sym = list->symbol[i];
9836
9837 if (SYMBOL_LANGUAGE (sym) == language_go
9838 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9839 {
9840 char *this_package_name = go_symbol_package_name (sym);
9841
9842 if (this_package_name == NULL)
9843 continue;
9844 if (package_name == NULL)
9845 package_name = this_package_name;
9846 else
9847 {
9848 struct objfile *objfile
9849 = cu->per_cu->dwarf2_per_objfile->objfile;
9850 if (strcmp (package_name, this_package_name) != 0)
9851 complaint (&symfile_complaints,
9852 _("Symtab %s has objects from two different Go packages: %s and %s"),
9853 (symbol_symtab (sym) != NULL
9854 ? symtab_to_filename_for_display
9855 (symbol_symtab (sym))
9856 : objfile_name (objfile)),
9857 this_package_name, package_name);
9858 xfree (this_package_name);
9859 }
9860 }
9861 }
9862 }
9863
9864 if (package_name != NULL)
9865 {
9866 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9867 const char *saved_package_name
9868 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
9869 package_name,
9870 strlen (package_name));
9871 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9872 saved_package_name);
9873 struct symbol *sym;
9874
9875 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9876
9877 sym = allocate_symbol (objfile);
9878 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
9879 SYMBOL_SET_NAMES (sym, saved_package_name,
9880 strlen (saved_package_name), 0, objfile);
9881 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9882 e.g., "main" finds the "main" module and not C's main(). */
9883 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9884 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9885 SYMBOL_TYPE (sym) = type;
9886
9887 add_symbol_to_list (sym, &global_symbols);
9888
9889 xfree (package_name);
9890 }
9891 }
9892
9893 /* Allocate a fully-qualified name consisting of the two parts on the
9894 obstack. */
9895
9896 static const char *
9897 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9898 {
9899 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9900 }
9901
9902 /* A helper that allocates a struct discriminant_info to attach to a
9903 union type. */
9904
9905 static struct discriminant_info *
9906 alloc_discriminant_info (struct type *type, int discriminant_index,
9907 int default_index)
9908 {
9909 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9910 gdb_assert (discriminant_index == -1
9911 || (discriminant_index >= 0
9912 && discriminant_index < TYPE_NFIELDS (type)));
9913 gdb_assert (default_index == -1
9914 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9915
9916 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9917
9918 struct discriminant_info *disc
9919 = ((struct discriminant_info *)
9920 TYPE_ZALLOC (type,
9921 offsetof (struct discriminant_info, discriminants)
9922 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9923 disc->default_index = default_index;
9924 disc->discriminant_index = discriminant_index;
9925
9926 struct dynamic_prop prop;
9927 prop.kind = PROP_UNDEFINED;
9928 prop.data.baton = disc;
9929
9930 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9931
9932 return disc;
9933 }
9934
9935 /* Some versions of rustc emitted enums in an unusual way.
9936
9937 Ordinary enums were emitted as unions. The first element of each
9938 structure in the union was named "RUST$ENUM$DISR". This element
9939 held the discriminant.
9940
9941 These versions of Rust also implemented the "non-zero"
9942 optimization. When the enum had two values, and one is empty and
9943 the other holds a pointer that cannot be zero, the pointer is used
9944 as the discriminant, with a zero value meaning the empty variant.
9945 Here, the union's first member is of the form
9946 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9947 where the fieldnos are the indices of the fields that should be
9948 traversed in order to find the field (which may be several fields deep)
9949 and the variantname is the name of the variant of the case when the
9950 field is zero.
9951
9952 This function recognizes whether TYPE is of one of these forms,
9953 and, if so, smashes it to be a variant type. */
9954
9955 static void
9956 quirk_rust_enum (struct type *type, struct objfile *objfile)
9957 {
9958 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9959
9960 /* We don't need to deal with empty enums. */
9961 if (TYPE_NFIELDS (type) == 0)
9962 return;
9963
9964 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9965 if (TYPE_NFIELDS (type) == 1
9966 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9967 {
9968 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9969
9970 /* Decode the field name to find the offset of the
9971 discriminant. */
9972 ULONGEST bit_offset = 0;
9973 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9974 while (name[0] >= '0' && name[0] <= '9')
9975 {
9976 char *tail;
9977 unsigned long index = strtoul (name, &tail, 10);
9978 name = tail;
9979 if (*name != '$'
9980 || index >= TYPE_NFIELDS (field_type)
9981 || (TYPE_FIELD_LOC_KIND (field_type, index)
9982 != FIELD_LOC_KIND_BITPOS))
9983 {
9984 complaint (&symfile_complaints,
9985 _("Could not parse Rust enum encoding string \"%s\""
9986 "[in module %s]"),
9987 TYPE_FIELD_NAME (type, 0),
9988 objfile_name (objfile));
9989 return;
9990 }
9991 ++name;
9992
9993 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9994 field_type = TYPE_FIELD_TYPE (field_type, index);
9995 }
9996
9997 /* Make a union to hold the variants. */
9998 struct type *union_type = alloc_type (objfile);
9999 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10000 TYPE_NFIELDS (union_type) = 3;
10001 TYPE_FIELDS (union_type)
10002 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10003 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10004
10005 /* Put the discriminant must at index 0. */
10006 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10007 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10008 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10009 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10010
10011 /* The order of fields doesn't really matter, so put the real
10012 field at index 1 and the data-less field at index 2. */
10013 struct discriminant_info *disc
10014 = alloc_discriminant_info (union_type, 0, 1);
10015 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10016 TYPE_FIELD_NAME (union_type, 1)
10017 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10018 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10019 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10020 TYPE_FIELD_NAME (union_type, 1));
10021
10022 const char *dataless_name
10023 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10024 name);
10025 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10026 dataless_name);
10027 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10028 /* NAME points into the original discriminant name, which
10029 already has the correct lifetime. */
10030 TYPE_FIELD_NAME (union_type, 2) = name;
10031 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10032 disc->discriminants[2] = 0;
10033
10034 /* Smash this type to be a structure type. We have to do this
10035 because the type has already been recorded. */
10036 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10037 TYPE_NFIELDS (type) = 1;
10038 TYPE_FIELDS (type)
10039 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10040
10041 /* Install the variant part. */
10042 TYPE_FIELD_TYPE (type, 0) = union_type;
10043 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10044 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10045 }
10046 else if (TYPE_NFIELDS (type) == 1)
10047 {
10048 /* We assume that a union with a single field is a univariant
10049 enum. */
10050 /* Smash this type to be a structure type. We have to do this
10051 because the type has already been recorded. */
10052 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10053
10054 /* Make a union to hold the variants. */
10055 struct type *union_type = alloc_type (objfile);
10056 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10057 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10058 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10059 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10060
10061 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10062 const char *variant_name
10063 = rust_last_path_segment (TYPE_NAME (field_type));
10064 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10065 TYPE_NAME (field_type)
10066 = rust_fully_qualify (&objfile->objfile_obstack,
10067 TYPE_NAME (type), variant_name);
10068
10069 /* Install the union in the outer struct type. */
10070 TYPE_NFIELDS (type) = 1;
10071 TYPE_FIELDS (type)
10072 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10073 TYPE_FIELD_TYPE (type, 0) = union_type;
10074 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10075 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10076
10077 alloc_discriminant_info (union_type, -1, 0);
10078 }
10079 else
10080 {
10081 struct type *disr_type = nullptr;
10082 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10083 {
10084 disr_type = TYPE_FIELD_TYPE (type, i);
10085
10086 if (TYPE_NFIELDS (disr_type) == 0)
10087 {
10088 /* Could be data-less variant, so keep going. */
10089 }
10090 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10091 "RUST$ENUM$DISR") != 0)
10092 {
10093 /* Not a Rust enum. */
10094 return;
10095 }
10096 else
10097 {
10098 /* Found one. */
10099 break;
10100 }
10101 }
10102
10103 /* If we got here without a discriminant, then it's probably
10104 just a union. */
10105 if (disr_type == nullptr)
10106 return;
10107
10108 /* Smash this type to be a structure type. We have to do this
10109 because the type has already been recorded. */
10110 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10111
10112 /* Make a union to hold the variants. */
10113 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10114 struct type *union_type = alloc_type (objfile);
10115 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10116 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10117 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10118 TYPE_FIELDS (union_type)
10119 = (struct field *) TYPE_ZALLOC (union_type,
10120 (TYPE_NFIELDS (union_type)
10121 * sizeof (struct field)));
10122
10123 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10124 TYPE_NFIELDS (type) * sizeof (struct field));
10125
10126 /* Install the discriminant at index 0 in the union. */
10127 TYPE_FIELD (union_type, 0) = *disr_field;
10128 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10129 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10130
10131 /* Install the union in the outer struct type. */
10132 TYPE_FIELD_TYPE (type, 0) = union_type;
10133 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10134 TYPE_NFIELDS (type) = 1;
10135
10136 /* Set the size and offset of the union type. */
10137 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10138
10139 /* We need a way to find the correct discriminant given a
10140 variant name. For convenience we build a map here. */
10141 struct type *enum_type = FIELD_TYPE (*disr_field);
10142 std::unordered_map<std::string, ULONGEST> discriminant_map;
10143 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10144 {
10145 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10146 {
10147 const char *name
10148 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10149 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10150 }
10151 }
10152
10153 int n_fields = TYPE_NFIELDS (union_type);
10154 struct discriminant_info *disc
10155 = alloc_discriminant_info (union_type, 0, -1);
10156 /* Skip the discriminant here. */
10157 for (int i = 1; i < n_fields; ++i)
10158 {
10159 /* Find the final word in the name of this variant's type.
10160 That name can be used to look up the correct
10161 discriminant. */
10162 const char *variant_name
10163 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10164 i)));
10165
10166 auto iter = discriminant_map.find (variant_name);
10167 if (iter != discriminant_map.end ())
10168 disc->discriminants[i] = iter->second;
10169
10170 /* Remove the discriminant field. */
10171 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10172 --TYPE_NFIELDS (sub_type);
10173 ++TYPE_FIELDS (sub_type);
10174 TYPE_FIELD_NAME (union_type, i) = variant_name;
10175 TYPE_NAME (sub_type)
10176 = rust_fully_qualify (&objfile->objfile_obstack,
10177 TYPE_NAME (type), variant_name);
10178 }
10179 }
10180 }
10181
10182 /* Rewrite some Rust unions to be structures with variants parts. */
10183
10184 static void
10185 rust_union_quirks (struct dwarf2_cu *cu)
10186 {
10187 gdb_assert (cu->language == language_rust);
10188 for (struct type *type : cu->rust_unions)
10189 quirk_rust_enum (type, cu->per_cu->dwarf2_per_objfile->objfile);
10190 }
10191
10192 /* Return the symtab for PER_CU. This works properly regardless of
10193 whether we're using the index or psymtabs. */
10194
10195 static struct compunit_symtab *
10196 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10197 {
10198 return (per_cu->dwarf2_per_objfile->using_index
10199 ? per_cu->v.quick->compunit_symtab
10200 : per_cu->v.psymtab->compunit_symtab);
10201 }
10202
10203 /* A helper function for computing the list of all symbol tables
10204 included by PER_CU. */
10205
10206 static void
10207 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
10208 htab_t all_children, htab_t all_type_symtabs,
10209 struct dwarf2_per_cu_data *per_cu,
10210 struct compunit_symtab *immediate_parent)
10211 {
10212 void **slot;
10213 int ix;
10214 struct compunit_symtab *cust;
10215 struct dwarf2_per_cu_data *iter;
10216
10217 slot = htab_find_slot (all_children, per_cu, INSERT);
10218 if (*slot != NULL)
10219 {
10220 /* This inclusion and its children have been processed. */
10221 return;
10222 }
10223
10224 *slot = per_cu;
10225 /* Only add a CU if it has a symbol table. */
10226 cust = get_compunit_symtab (per_cu);
10227 if (cust != NULL)
10228 {
10229 /* If this is a type unit only add its symbol table if we haven't
10230 seen it yet (type unit per_cu's can share symtabs). */
10231 if (per_cu->is_debug_types)
10232 {
10233 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10234 if (*slot == NULL)
10235 {
10236 *slot = cust;
10237 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10238 if (cust->user == NULL)
10239 cust->user = immediate_parent;
10240 }
10241 }
10242 else
10243 {
10244 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10245 if (cust->user == NULL)
10246 cust->user = immediate_parent;
10247 }
10248 }
10249
10250 for (ix = 0;
10251 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10252 ++ix)
10253 {
10254 recursively_compute_inclusions (result, all_children,
10255 all_type_symtabs, iter, cust);
10256 }
10257 }
10258
10259 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10260 PER_CU. */
10261
10262 static void
10263 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10264 {
10265 gdb_assert (! per_cu->is_debug_types);
10266
10267 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10268 {
10269 int ix, len;
10270 struct dwarf2_per_cu_data *per_cu_iter;
10271 struct compunit_symtab *compunit_symtab_iter;
10272 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
10273 htab_t all_children, all_type_symtabs;
10274 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10275
10276 /* If we don't have a symtab, we can just skip this case. */
10277 if (cust == NULL)
10278 return;
10279
10280 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10281 NULL, xcalloc, xfree);
10282 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10283 NULL, xcalloc, xfree);
10284
10285 for (ix = 0;
10286 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10287 ix, per_cu_iter);
10288 ++ix)
10289 {
10290 recursively_compute_inclusions (&result_symtabs, all_children,
10291 all_type_symtabs, per_cu_iter,
10292 cust);
10293 }
10294
10295 /* Now we have a transitive closure of all the included symtabs. */
10296 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10297 cust->includes
10298 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10299 struct compunit_symtab *, len + 1);
10300 for (ix = 0;
10301 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10302 compunit_symtab_iter);
10303 ++ix)
10304 cust->includes[ix] = compunit_symtab_iter;
10305 cust->includes[len] = NULL;
10306
10307 VEC_free (compunit_symtab_ptr, result_symtabs);
10308 htab_delete (all_children);
10309 htab_delete (all_type_symtabs);
10310 }
10311 }
10312
10313 /* Compute the 'includes' field for the symtabs of all the CUs we just
10314 read. */
10315
10316 static void
10317 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10318 {
10319 int ix;
10320 struct dwarf2_per_cu_data *iter;
10321
10322 for (ix = 0;
10323 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10324 ix, iter);
10325 ++ix)
10326 {
10327 if (! iter->is_debug_types)
10328 compute_compunit_symtab_includes (iter);
10329 }
10330
10331 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10332 }
10333
10334 /* Generate full symbol information for PER_CU, whose DIEs have
10335 already been loaded into memory. */
10336
10337 static void
10338 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10339 enum language pretend_language)
10340 {
10341 struct dwarf2_cu *cu = per_cu->cu;
10342 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10343 struct objfile *objfile = dwarf2_per_objfile->objfile;
10344 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10345 CORE_ADDR lowpc, highpc;
10346 struct compunit_symtab *cust;
10347 CORE_ADDR baseaddr;
10348 struct block *static_block;
10349 CORE_ADDR addr;
10350
10351 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10352
10353 buildsym_init ();
10354 scoped_free_pendings free_pending;
10355
10356 /* Clear the list here in case something was left over. */
10357 cu->method_list.clear ();
10358
10359 cu->list_in_scope = &file_symbols;
10360
10361 cu->language = pretend_language;
10362 cu->language_defn = language_def (cu->language);
10363
10364 /* Do line number decoding in read_file_scope () */
10365 process_die (cu->dies, cu);
10366
10367 /* For now fudge the Go package. */
10368 if (cu->language == language_go)
10369 fixup_go_packaging (cu);
10370
10371 /* Now that we have processed all the DIEs in the CU, all the types
10372 should be complete, and it should now be safe to compute all of the
10373 physnames. */
10374 compute_delayed_physnames (cu);
10375
10376 if (cu->language == language_rust)
10377 rust_union_quirks (cu);
10378
10379 /* Some compilers don't define a DW_AT_high_pc attribute for the
10380 compilation unit. If the DW_AT_high_pc is missing, synthesize
10381 it, by scanning the DIE's below the compilation unit. */
10382 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10383
10384 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10385 static_block = end_symtab_get_static_block (addr, 0, 1);
10386
10387 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10388 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10389 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10390 addrmap to help ensure it has an accurate map of pc values belonging to
10391 this comp unit. */
10392 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10393
10394 cust = end_symtab_from_static_block (static_block,
10395 SECT_OFF_TEXT (objfile), 0);
10396
10397 if (cust != NULL)
10398 {
10399 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10400
10401 /* Set symtab language to language from DW_AT_language. If the
10402 compilation is from a C file generated by language preprocessors, do
10403 not set the language if it was already deduced by start_subfile. */
10404 if (!(cu->language == language_c
10405 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10406 COMPUNIT_FILETABS (cust)->language = cu->language;
10407
10408 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10409 produce DW_AT_location with location lists but it can be possibly
10410 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10411 there were bugs in prologue debug info, fixed later in GCC-4.5
10412 by "unwind info for epilogues" patch (which is not directly related).
10413
10414 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10415 needed, it would be wrong due to missing DW_AT_producer there.
10416
10417 Still one can confuse GDB by using non-standard GCC compilation
10418 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10419 */
10420 if (cu->has_loclist && gcc_4_minor >= 5)
10421 cust->locations_valid = 1;
10422
10423 if (gcc_4_minor >= 5)
10424 cust->epilogue_unwind_valid = 1;
10425
10426 cust->call_site_htab = cu->call_site_htab;
10427 }
10428
10429 if (dwarf2_per_objfile->using_index)
10430 per_cu->v.quick->compunit_symtab = cust;
10431 else
10432 {
10433 struct partial_symtab *pst = per_cu->v.psymtab;
10434 pst->compunit_symtab = cust;
10435 pst->readin = 1;
10436 }
10437
10438 /* Push it for inclusion processing later. */
10439 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
10440 }
10441
10442 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10443 already been loaded into memory. */
10444
10445 static void
10446 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10447 enum language pretend_language)
10448 {
10449 struct dwarf2_cu *cu = per_cu->cu;
10450 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10451 struct objfile *objfile = dwarf2_per_objfile->objfile;
10452 struct compunit_symtab *cust;
10453 struct signatured_type *sig_type;
10454
10455 gdb_assert (per_cu->is_debug_types);
10456 sig_type = (struct signatured_type *) per_cu;
10457
10458 buildsym_init ();
10459 scoped_free_pendings free_pending;
10460
10461 /* Clear the list here in case something was left over. */
10462 cu->method_list.clear ();
10463
10464 cu->list_in_scope = &file_symbols;
10465
10466 cu->language = pretend_language;
10467 cu->language_defn = language_def (cu->language);
10468
10469 /* The symbol tables are set up in read_type_unit_scope. */
10470 process_die (cu->dies, cu);
10471
10472 /* For now fudge the Go package. */
10473 if (cu->language == language_go)
10474 fixup_go_packaging (cu);
10475
10476 /* Now that we have processed all the DIEs in the CU, all the types
10477 should be complete, and it should now be safe to compute all of the
10478 physnames. */
10479 compute_delayed_physnames (cu);
10480
10481 if (cu->language == language_rust)
10482 rust_union_quirks (cu);
10483
10484 /* TUs share symbol tables.
10485 If this is the first TU to use this symtab, complete the construction
10486 of it with end_expandable_symtab. Otherwise, complete the addition of
10487 this TU's symbols to the existing symtab. */
10488 if (sig_type->type_unit_group->compunit_symtab == NULL)
10489 {
10490 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10491 sig_type->type_unit_group->compunit_symtab = cust;
10492
10493 if (cust != NULL)
10494 {
10495 /* Set symtab language to language from DW_AT_language. If the
10496 compilation is from a C file generated by language preprocessors,
10497 do not set the language if it was already deduced by
10498 start_subfile. */
10499 if (!(cu->language == language_c
10500 && COMPUNIT_FILETABS (cust)->language != language_c))
10501 COMPUNIT_FILETABS (cust)->language = cu->language;
10502 }
10503 }
10504 else
10505 {
10506 augment_type_symtab ();
10507 cust = sig_type->type_unit_group->compunit_symtab;
10508 }
10509
10510 if (dwarf2_per_objfile->using_index)
10511 per_cu->v.quick->compunit_symtab = cust;
10512 else
10513 {
10514 struct partial_symtab *pst = per_cu->v.psymtab;
10515 pst->compunit_symtab = cust;
10516 pst->readin = 1;
10517 }
10518 }
10519
10520 /* Process an imported unit DIE. */
10521
10522 static void
10523 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10524 {
10525 struct attribute *attr;
10526
10527 /* For now we don't handle imported units in type units. */
10528 if (cu->per_cu->is_debug_types)
10529 {
10530 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10531 " supported in type units [in module %s]"),
10532 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10533 }
10534
10535 attr = dwarf2_attr (die, DW_AT_import, cu);
10536 if (attr != NULL)
10537 {
10538 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10539 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10540 dwarf2_per_cu_data *per_cu
10541 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10542 cu->per_cu->dwarf2_per_objfile);
10543
10544 /* If necessary, add it to the queue and load its DIEs. */
10545 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10546 load_full_comp_unit (per_cu, cu->language);
10547
10548 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10549 per_cu);
10550 }
10551 }
10552
10553 /* RAII object that represents a process_die scope: i.e.,
10554 starts/finishes processing a DIE. */
10555 class process_die_scope
10556 {
10557 public:
10558 process_die_scope (die_info *die, dwarf2_cu *cu)
10559 : m_die (die), m_cu (cu)
10560 {
10561 /* We should only be processing DIEs not already in process. */
10562 gdb_assert (!m_die->in_process);
10563 m_die->in_process = true;
10564 }
10565
10566 ~process_die_scope ()
10567 {
10568 m_die->in_process = false;
10569
10570 /* If we're done processing the DIE for the CU that owns the line
10571 header, we don't need the line header anymore. */
10572 if (m_cu->line_header_die_owner == m_die)
10573 {
10574 delete m_cu->line_header;
10575 m_cu->line_header = NULL;
10576 m_cu->line_header_die_owner = NULL;
10577 }
10578 }
10579
10580 private:
10581 die_info *m_die;
10582 dwarf2_cu *m_cu;
10583 };
10584
10585 /* Process a die and its children. */
10586
10587 static void
10588 process_die (struct die_info *die, struct dwarf2_cu *cu)
10589 {
10590 process_die_scope scope (die, cu);
10591
10592 switch (die->tag)
10593 {
10594 case DW_TAG_padding:
10595 break;
10596 case DW_TAG_compile_unit:
10597 case DW_TAG_partial_unit:
10598 read_file_scope (die, cu);
10599 break;
10600 case DW_TAG_type_unit:
10601 read_type_unit_scope (die, cu);
10602 break;
10603 case DW_TAG_subprogram:
10604 case DW_TAG_inlined_subroutine:
10605 read_func_scope (die, cu);
10606 break;
10607 case DW_TAG_lexical_block:
10608 case DW_TAG_try_block:
10609 case DW_TAG_catch_block:
10610 read_lexical_block_scope (die, cu);
10611 break;
10612 case DW_TAG_call_site:
10613 case DW_TAG_GNU_call_site:
10614 read_call_site_scope (die, cu);
10615 break;
10616 case DW_TAG_class_type:
10617 case DW_TAG_interface_type:
10618 case DW_TAG_structure_type:
10619 case DW_TAG_union_type:
10620 process_structure_scope (die, cu);
10621 break;
10622 case DW_TAG_enumeration_type:
10623 process_enumeration_scope (die, cu);
10624 break;
10625
10626 /* These dies have a type, but processing them does not create
10627 a symbol or recurse to process the children. Therefore we can
10628 read them on-demand through read_type_die. */
10629 case DW_TAG_subroutine_type:
10630 case DW_TAG_set_type:
10631 case DW_TAG_array_type:
10632 case DW_TAG_pointer_type:
10633 case DW_TAG_ptr_to_member_type:
10634 case DW_TAG_reference_type:
10635 case DW_TAG_rvalue_reference_type:
10636 case DW_TAG_string_type:
10637 break;
10638
10639 case DW_TAG_base_type:
10640 case DW_TAG_subrange_type:
10641 case DW_TAG_typedef:
10642 /* Add a typedef symbol for the type definition, if it has a
10643 DW_AT_name. */
10644 new_symbol (die, read_type_die (die, cu), cu);
10645 break;
10646 case DW_TAG_common_block:
10647 read_common_block (die, cu);
10648 break;
10649 case DW_TAG_common_inclusion:
10650 break;
10651 case DW_TAG_namespace:
10652 cu->processing_has_namespace_info = 1;
10653 read_namespace (die, cu);
10654 break;
10655 case DW_TAG_module:
10656 cu->processing_has_namespace_info = 1;
10657 read_module (die, cu);
10658 break;
10659 case DW_TAG_imported_declaration:
10660 cu->processing_has_namespace_info = 1;
10661 if (read_namespace_alias (die, cu))
10662 break;
10663 /* The declaration is not a global namespace alias: fall through. */
10664 case DW_TAG_imported_module:
10665 cu->processing_has_namespace_info = 1;
10666 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10667 || cu->language != language_fortran))
10668 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
10669 dwarf_tag_name (die->tag));
10670 read_import_statement (die, cu);
10671 break;
10672
10673 case DW_TAG_imported_unit:
10674 process_imported_unit_die (die, cu);
10675 break;
10676
10677 case DW_TAG_variable:
10678 read_variable (die, cu);
10679 break;
10680
10681 default:
10682 new_symbol (die, NULL, cu);
10683 break;
10684 }
10685 }
10686 \f
10687 /* DWARF name computation. */
10688
10689 /* A helper function for dwarf2_compute_name which determines whether DIE
10690 needs to have the name of the scope prepended to the name listed in the
10691 die. */
10692
10693 static int
10694 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10695 {
10696 struct attribute *attr;
10697
10698 switch (die->tag)
10699 {
10700 case DW_TAG_namespace:
10701 case DW_TAG_typedef:
10702 case DW_TAG_class_type:
10703 case DW_TAG_interface_type:
10704 case DW_TAG_structure_type:
10705 case DW_TAG_union_type:
10706 case DW_TAG_enumeration_type:
10707 case DW_TAG_enumerator:
10708 case DW_TAG_subprogram:
10709 case DW_TAG_inlined_subroutine:
10710 case DW_TAG_member:
10711 case DW_TAG_imported_declaration:
10712 return 1;
10713
10714 case DW_TAG_variable:
10715 case DW_TAG_constant:
10716 /* We only need to prefix "globally" visible variables. These include
10717 any variable marked with DW_AT_external or any variable that
10718 lives in a namespace. [Variables in anonymous namespaces
10719 require prefixing, but they are not DW_AT_external.] */
10720
10721 if (dwarf2_attr (die, DW_AT_specification, cu))
10722 {
10723 struct dwarf2_cu *spec_cu = cu;
10724
10725 return die_needs_namespace (die_specification (die, &spec_cu),
10726 spec_cu);
10727 }
10728
10729 attr = dwarf2_attr (die, DW_AT_external, cu);
10730 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10731 && die->parent->tag != DW_TAG_module)
10732 return 0;
10733 /* A variable in a lexical block of some kind does not need a
10734 namespace, even though in C++ such variables may be external
10735 and have a mangled name. */
10736 if (die->parent->tag == DW_TAG_lexical_block
10737 || die->parent->tag == DW_TAG_try_block
10738 || die->parent->tag == DW_TAG_catch_block
10739 || die->parent->tag == DW_TAG_subprogram)
10740 return 0;
10741 return 1;
10742
10743 default:
10744 return 0;
10745 }
10746 }
10747
10748 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10749 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10750 defined for the given DIE. */
10751
10752 static struct attribute *
10753 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10754 {
10755 struct attribute *attr;
10756
10757 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10758 if (attr == NULL)
10759 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10760
10761 return attr;
10762 }
10763
10764 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10765 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10766 defined for the given DIE. */
10767
10768 static const char *
10769 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10770 {
10771 const char *linkage_name;
10772
10773 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10774 if (linkage_name == NULL)
10775 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10776
10777 return linkage_name;
10778 }
10779
10780 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10781 compute the physname for the object, which include a method's:
10782 - formal parameters (C++),
10783 - receiver type (Go),
10784
10785 The term "physname" is a bit confusing.
10786 For C++, for example, it is the demangled name.
10787 For Go, for example, it's the mangled name.
10788
10789 For Ada, return the DIE's linkage name rather than the fully qualified
10790 name. PHYSNAME is ignored..
10791
10792 The result is allocated on the objfile_obstack and canonicalized. */
10793
10794 static const char *
10795 dwarf2_compute_name (const char *name,
10796 struct die_info *die, struct dwarf2_cu *cu,
10797 int physname)
10798 {
10799 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10800
10801 if (name == NULL)
10802 name = dwarf2_name (die, cu);
10803
10804 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10805 but otherwise compute it by typename_concat inside GDB.
10806 FIXME: Actually this is not really true, or at least not always true.
10807 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10808 Fortran names because there is no mangling standard. So new_symbol
10809 will set the demangled name to the result of dwarf2_full_name, and it is
10810 the demangled name that GDB uses if it exists. */
10811 if (cu->language == language_ada
10812 || (cu->language == language_fortran && physname))
10813 {
10814 /* For Ada unit, we prefer the linkage name over the name, as
10815 the former contains the exported name, which the user expects
10816 to be able to reference. Ideally, we want the user to be able
10817 to reference this entity using either natural or linkage name,
10818 but we haven't started looking at this enhancement yet. */
10819 const char *linkage_name = dw2_linkage_name (die, cu);
10820
10821 if (linkage_name != NULL)
10822 return linkage_name;
10823 }
10824
10825 /* These are the only languages we know how to qualify names in. */
10826 if (name != NULL
10827 && (cu->language == language_cplus
10828 || cu->language == language_fortran || cu->language == language_d
10829 || cu->language == language_rust))
10830 {
10831 if (die_needs_namespace (die, cu))
10832 {
10833 const char *prefix;
10834 const char *canonical_name = NULL;
10835
10836 string_file buf;
10837
10838 prefix = determine_prefix (die, cu);
10839 if (*prefix != '\0')
10840 {
10841 char *prefixed_name = typename_concat (NULL, prefix, name,
10842 physname, cu);
10843
10844 buf.puts (prefixed_name);
10845 xfree (prefixed_name);
10846 }
10847 else
10848 buf.puts (name);
10849
10850 /* Template parameters may be specified in the DIE's DW_AT_name, or
10851 as children with DW_TAG_template_type_param or
10852 DW_TAG_value_type_param. If the latter, add them to the name
10853 here. If the name already has template parameters, then
10854 skip this step; some versions of GCC emit both, and
10855 it is more efficient to use the pre-computed name.
10856
10857 Something to keep in mind about this process: it is very
10858 unlikely, or in some cases downright impossible, to produce
10859 something that will match the mangled name of a function.
10860 If the definition of the function has the same debug info,
10861 we should be able to match up with it anyway. But fallbacks
10862 using the minimal symbol, for instance to find a method
10863 implemented in a stripped copy of libstdc++, will not work.
10864 If we do not have debug info for the definition, we will have to
10865 match them up some other way.
10866
10867 When we do name matching there is a related problem with function
10868 templates; two instantiated function templates are allowed to
10869 differ only by their return types, which we do not add here. */
10870
10871 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10872 {
10873 struct attribute *attr;
10874 struct die_info *child;
10875 int first = 1;
10876
10877 die->building_fullname = 1;
10878
10879 for (child = die->child; child != NULL; child = child->sibling)
10880 {
10881 struct type *type;
10882 LONGEST value;
10883 const gdb_byte *bytes;
10884 struct dwarf2_locexpr_baton *baton;
10885 struct value *v;
10886
10887 if (child->tag != DW_TAG_template_type_param
10888 && child->tag != DW_TAG_template_value_param)
10889 continue;
10890
10891 if (first)
10892 {
10893 buf.puts ("<");
10894 first = 0;
10895 }
10896 else
10897 buf.puts (", ");
10898
10899 attr = dwarf2_attr (child, DW_AT_type, cu);
10900 if (attr == NULL)
10901 {
10902 complaint (&symfile_complaints,
10903 _("template parameter missing DW_AT_type"));
10904 buf.puts ("UNKNOWN_TYPE");
10905 continue;
10906 }
10907 type = die_type (child, cu);
10908
10909 if (child->tag == DW_TAG_template_type_param)
10910 {
10911 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
10912 continue;
10913 }
10914
10915 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10916 if (attr == NULL)
10917 {
10918 complaint (&symfile_complaints,
10919 _("template parameter missing "
10920 "DW_AT_const_value"));
10921 buf.puts ("UNKNOWN_VALUE");
10922 continue;
10923 }
10924
10925 dwarf2_const_value_attr (attr, type, name,
10926 &cu->comp_unit_obstack, cu,
10927 &value, &bytes, &baton);
10928
10929 if (TYPE_NOSIGN (type))
10930 /* GDB prints characters as NUMBER 'CHAR'. If that's
10931 changed, this can use value_print instead. */
10932 c_printchar (value, type, &buf);
10933 else
10934 {
10935 struct value_print_options opts;
10936
10937 if (baton != NULL)
10938 v = dwarf2_evaluate_loc_desc (type, NULL,
10939 baton->data,
10940 baton->size,
10941 baton->per_cu);
10942 else if (bytes != NULL)
10943 {
10944 v = allocate_value (type);
10945 memcpy (value_contents_writeable (v), bytes,
10946 TYPE_LENGTH (type));
10947 }
10948 else
10949 v = value_from_longest (type, value);
10950
10951 /* Specify decimal so that we do not depend on
10952 the radix. */
10953 get_formatted_print_options (&opts, 'd');
10954 opts.raw = 1;
10955 value_print (v, &buf, &opts);
10956 release_value (v);
10957 }
10958 }
10959
10960 die->building_fullname = 0;
10961
10962 if (!first)
10963 {
10964 /* Close the argument list, with a space if necessary
10965 (nested templates). */
10966 if (!buf.empty () && buf.string ().back () == '>')
10967 buf.puts (" >");
10968 else
10969 buf.puts (">");
10970 }
10971 }
10972
10973 /* For C++ methods, append formal parameter type
10974 information, if PHYSNAME. */
10975
10976 if (physname && die->tag == DW_TAG_subprogram
10977 && cu->language == language_cplus)
10978 {
10979 struct type *type = read_type_die (die, cu);
10980
10981 c_type_print_args (type, &buf, 1, cu->language,
10982 &type_print_raw_options);
10983
10984 if (cu->language == language_cplus)
10985 {
10986 /* Assume that an artificial first parameter is
10987 "this", but do not crash if it is not. RealView
10988 marks unnamed (and thus unused) parameters as
10989 artificial; there is no way to differentiate
10990 the two cases. */
10991 if (TYPE_NFIELDS (type) > 0
10992 && TYPE_FIELD_ARTIFICIAL (type, 0)
10993 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10994 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10995 0))))
10996 buf.puts (" const");
10997 }
10998 }
10999
11000 const std::string &intermediate_name = buf.string ();
11001
11002 if (cu->language == language_cplus)
11003 canonical_name
11004 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11005 &objfile->per_bfd->storage_obstack);
11006
11007 /* If we only computed INTERMEDIATE_NAME, or if
11008 INTERMEDIATE_NAME is already canonical, then we need to
11009 copy it to the appropriate obstack. */
11010 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11011 name = ((const char *)
11012 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11013 intermediate_name.c_str (),
11014 intermediate_name.length ()));
11015 else
11016 name = canonical_name;
11017 }
11018 }
11019
11020 return name;
11021 }
11022
11023 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11024 If scope qualifiers are appropriate they will be added. The result
11025 will be allocated on the storage_obstack, or NULL if the DIE does
11026 not have a name. NAME may either be from a previous call to
11027 dwarf2_name or NULL.
11028
11029 The output string will be canonicalized (if C++). */
11030
11031 static const char *
11032 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11033 {
11034 return dwarf2_compute_name (name, die, cu, 0);
11035 }
11036
11037 /* Construct a physname for the given DIE in CU. NAME may either be
11038 from a previous call to dwarf2_name or NULL. The result will be
11039 allocated on the objfile_objstack or NULL if the DIE does not have a
11040 name.
11041
11042 The output string will be canonicalized (if C++). */
11043
11044 static const char *
11045 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11046 {
11047 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11048 const char *retval, *mangled = NULL, *canon = NULL;
11049 int need_copy = 1;
11050
11051 /* In this case dwarf2_compute_name is just a shortcut not building anything
11052 on its own. */
11053 if (!die_needs_namespace (die, cu))
11054 return dwarf2_compute_name (name, die, cu, 1);
11055
11056 mangled = dw2_linkage_name (die, cu);
11057
11058 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11059 See https://github.com/rust-lang/rust/issues/32925. */
11060 if (cu->language == language_rust && mangled != NULL
11061 && strchr (mangled, '{') != NULL)
11062 mangled = NULL;
11063
11064 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11065 has computed. */
11066 gdb::unique_xmalloc_ptr<char> demangled;
11067 if (mangled != NULL)
11068 {
11069
11070 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11071 {
11072 /* Do nothing (do not demangle the symbol name). */
11073 }
11074 else if (cu->language == language_go)
11075 {
11076 /* This is a lie, but we already lie to the caller new_symbol.
11077 new_symbol assumes we return the mangled name.
11078 This just undoes that lie until things are cleaned up. */
11079 }
11080 else
11081 {
11082 /* Use DMGL_RET_DROP for C++ template functions to suppress
11083 their return type. It is easier for GDB users to search
11084 for such functions as `name(params)' than `long name(params)'.
11085 In such case the minimal symbol names do not match the full
11086 symbol names but for template functions there is never a need
11087 to look up their definition from their declaration so
11088 the only disadvantage remains the minimal symbol variant
11089 `long name(params)' does not have the proper inferior type. */
11090 demangled.reset (gdb_demangle (mangled,
11091 (DMGL_PARAMS | DMGL_ANSI
11092 | DMGL_RET_DROP)));
11093 }
11094 if (demangled)
11095 canon = demangled.get ();
11096 else
11097 {
11098 canon = mangled;
11099 need_copy = 0;
11100 }
11101 }
11102
11103 if (canon == NULL || check_physname)
11104 {
11105 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11106
11107 if (canon != NULL && strcmp (physname, canon) != 0)
11108 {
11109 /* It may not mean a bug in GDB. The compiler could also
11110 compute DW_AT_linkage_name incorrectly. But in such case
11111 GDB would need to be bug-to-bug compatible. */
11112
11113 complaint (&symfile_complaints,
11114 _("Computed physname <%s> does not match demangled <%s> "
11115 "(from linkage <%s>) - DIE at %s [in module %s]"),
11116 physname, canon, mangled, sect_offset_str (die->sect_off),
11117 objfile_name (objfile));
11118
11119 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11120 is available here - over computed PHYSNAME. It is safer
11121 against both buggy GDB and buggy compilers. */
11122
11123 retval = canon;
11124 }
11125 else
11126 {
11127 retval = physname;
11128 need_copy = 0;
11129 }
11130 }
11131 else
11132 retval = canon;
11133
11134 if (need_copy)
11135 retval = ((const char *)
11136 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11137 retval, strlen (retval)));
11138
11139 return retval;
11140 }
11141
11142 /* Inspect DIE in CU for a namespace alias. If one exists, record
11143 a new symbol for it.
11144
11145 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11146
11147 static int
11148 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11149 {
11150 struct attribute *attr;
11151
11152 /* If the die does not have a name, this is not a namespace
11153 alias. */
11154 attr = dwarf2_attr (die, DW_AT_name, cu);
11155 if (attr != NULL)
11156 {
11157 int num;
11158 struct die_info *d = die;
11159 struct dwarf2_cu *imported_cu = cu;
11160
11161 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11162 keep inspecting DIEs until we hit the underlying import. */
11163 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11164 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11165 {
11166 attr = dwarf2_attr (d, DW_AT_import, cu);
11167 if (attr == NULL)
11168 break;
11169
11170 d = follow_die_ref (d, attr, &imported_cu);
11171 if (d->tag != DW_TAG_imported_declaration)
11172 break;
11173 }
11174
11175 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11176 {
11177 complaint (&symfile_complaints,
11178 _("DIE at %s has too many recursively imported "
11179 "declarations"), sect_offset_str (d->sect_off));
11180 return 0;
11181 }
11182
11183 if (attr != NULL)
11184 {
11185 struct type *type;
11186 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11187
11188 type = get_die_type_at_offset (sect_off, cu->per_cu);
11189 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11190 {
11191 /* This declaration is a global namespace alias. Add
11192 a symbol for it whose type is the aliased namespace. */
11193 new_symbol (die, type, cu);
11194 return 1;
11195 }
11196 }
11197 }
11198
11199 return 0;
11200 }
11201
11202 /* Return the using directives repository (global or local?) to use in the
11203 current context for LANGUAGE.
11204
11205 For Ada, imported declarations can materialize renamings, which *may* be
11206 global. However it is impossible (for now?) in DWARF to distinguish
11207 "external" imported declarations and "static" ones. As all imported
11208 declarations seem to be static in all other languages, make them all CU-wide
11209 global only in Ada. */
11210
11211 static struct using_direct **
11212 using_directives (enum language language)
11213 {
11214 if (language == language_ada && context_stack_depth == 0)
11215 return &global_using_directives;
11216 else
11217 return &local_using_directives;
11218 }
11219
11220 /* Read the import statement specified by the given die and record it. */
11221
11222 static void
11223 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11224 {
11225 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11226 struct attribute *import_attr;
11227 struct die_info *imported_die, *child_die;
11228 struct dwarf2_cu *imported_cu;
11229 const char *imported_name;
11230 const char *imported_name_prefix;
11231 const char *canonical_name;
11232 const char *import_alias;
11233 const char *imported_declaration = NULL;
11234 const char *import_prefix;
11235 std::vector<const char *> excludes;
11236
11237 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11238 if (import_attr == NULL)
11239 {
11240 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11241 dwarf_tag_name (die->tag));
11242 return;
11243 }
11244
11245 imported_cu = cu;
11246 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11247 imported_name = dwarf2_name (imported_die, imported_cu);
11248 if (imported_name == NULL)
11249 {
11250 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11251
11252 The import in the following code:
11253 namespace A
11254 {
11255 typedef int B;
11256 }
11257
11258 int main ()
11259 {
11260 using A::B;
11261 B b;
11262 return b;
11263 }
11264
11265 ...
11266 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11267 <52> DW_AT_decl_file : 1
11268 <53> DW_AT_decl_line : 6
11269 <54> DW_AT_import : <0x75>
11270 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11271 <59> DW_AT_name : B
11272 <5b> DW_AT_decl_file : 1
11273 <5c> DW_AT_decl_line : 2
11274 <5d> DW_AT_type : <0x6e>
11275 ...
11276 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11277 <76> DW_AT_byte_size : 4
11278 <77> DW_AT_encoding : 5 (signed)
11279
11280 imports the wrong die ( 0x75 instead of 0x58 ).
11281 This case will be ignored until the gcc bug is fixed. */
11282 return;
11283 }
11284
11285 /* Figure out the local name after import. */
11286 import_alias = dwarf2_name (die, cu);
11287
11288 /* Figure out where the statement is being imported to. */
11289 import_prefix = determine_prefix (die, cu);
11290
11291 /* Figure out what the scope of the imported die is and prepend it
11292 to the name of the imported die. */
11293 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11294
11295 if (imported_die->tag != DW_TAG_namespace
11296 && imported_die->tag != DW_TAG_module)
11297 {
11298 imported_declaration = imported_name;
11299 canonical_name = imported_name_prefix;
11300 }
11301 else if (strlen (imported_name_prefix) > 0)
11302 canonical_name = obconcat (&objfile->objfile_obstack,
11303 imported_name_prefix,
11304 (cu->language == language_d ? "." : "::"),
11305 imported_name, (char *) NULL);
11306 else
11307 canonical_name = imported_name;
11308
11309 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11310 for (child_die = die->child; child_die && child_die->tag;
11311 child_die = sibling_die (child_die))
11312 {
11313 /* DWARF-4: A Fortran use statement with a “rename list” may be
11314 represented by an imported module entry with an import attribute
11315 referring to the module and owned entries corresponding to those
11316 entities that are renamed as part of being imported. */
11317
11318 if (child_die->tag != DW_TAG_imported_declaration)
11319 {
11320 complaint (&symfile_complaints,
11321 _("child DW_TAG_imported_declaration expected "
11322 "- DIE at %s [in module %s]"),
11323 sect_offset_str (child_die->sect_off),
11324 objfile_name (objfile));
11325 continue;
11326 }
11327
11328 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11329 if (import_attr == NULL)
11330 {
11331 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11332 dwarf_tag_name (child_die->tag));
11333 continue;
11334 }
11335
11336 imported_cu = cu;
11337 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11338 &imported_cu);
11339 imported_name = dwarf2_name (imported_die, imported_cu);
11340 if (imported_name == NULL)
11341 {
11342 complaint (&symfile_complaints,
11343 _("child DW_TAG_imported_declaration has unknown "
11344 "imported name - DIE at %s [in module %s]"),
11345 sect_offset_str (child_die->sect_off),
11346 objfile_name (objfile));
11347 continue;
11348 }
11349
11350 excludes.push_back (imported_name);
11351
11352 process_die (child_die, cu);
11353 }
11354
11355 add_using_directive (using_directives (cu->language),
11356 import_prefix,
11357 canonical_name,
11358 import_alias,
11359 imported_declaration,
11360 excludes,
11361 0,
11362 &objfile->objfile_obstack);
11363 }
11364
11365 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11366 types, but gives them a size of zero. Starting with version 14,
11367 ICC is compatible with GCC. */
11368
11369 static int
11370 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11371 {
11372 if (!cu->checked_producer)
11373 check_producer (cu);
11374
11375 return cu->producer_is_icc_lt_14;
11376 }
11377
11378 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11379 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11380 this, it was first present in GCC release 4.3.0. */
11381
11382 static int
11383 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11384 {
11385 if (!cu->checked_producer)
11386 check_producer (cu);
11387
11388 return cu->producer_is_gcc_lt_4_3;
11389 }
11390
11391 static file_and_directory
11392 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11393 {
11394 file_and_directory res;
11395
11396 /* Find the filename. Do not use dwarf2_name here, since the filename
11397 is not a source language identifier. */
11398 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11399 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11400
11401 if (res.comp_dir == NULL
11402 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11403 && IS_ABSOLUTE_PATH (res.name))
11404 {
11405 res.comp_dir_storage = ldirname (res.name);
11406 if (!res.comp_dir_storage.empty ())
11407 res.comp_dir = res.comp_dir_storage.c_str ();
11408 }
11409 if (res.comp_dir != NULL)
11410 {
11411 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11412 directory, get rid of it. */
11413 const char *cp = strchr (res.comp_dir, ':');
11414
11415 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11416 res.comp_dir = cp + 1;
11417 }
11418
11419 if (res.name == NULL)
11420 res.name = "<unknown>";
11421
11422 return res;
11423 }
11424
11425 /* Handle DW_AT_stmt_list for a compilation unit.
11426 DIE is the DW_TAG_compile_unit die for CU.
11427 COMP_DIR is the compilation directory. LOWPC is passed to
11428 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11429
11430 static void
11431 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11432 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11433 {
11434 struct dwarf2_per_objfile *dwarf2_per_objfile
11435 = cu->per_cu->dwarf2_per_objfile;
11436 struct objfile *objfile = dwarf2_per_objfile->objfile;
11437 struct attribute *attr;
11438 struct line_header line_header_local;
11439 hashval_t line_header_local_hash;
11440 void **slot;
11441 int decode_mapping;
11442
11443 gdb_assert (! cu->per_cu->is_debug_types);
11444
11445 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11446 if (attr == NULL)
11447 return;
11448
11449 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11450
11451 /* The line header hash table is only created if needed (it exists to
11452 prevent redundant reading of the line table for partial_units).
11453 If we're given a partial_unit, we'll need it. If we're given a
11454 compile_unit, then use the line header hash table if it's already
11455 created, but don't create one just yet. */
11456
11457 if (dwarf2_per_objfile->line_header_hash == NULL
11458 && die->tag == DW_TAG_partial_unit)
11459 {
11460 dwarf2_per_objfile->line_header_hash
11461 = htab_create_alloc_ex (127, line_header_hash_voidp,
11462 line_header_eq_voidp,
11463 free_line_header_voidp,
11464 &objfile->objfile_obstack,
11465 hashtab_obstack_allocate,
11466 dummy_obstack_deallocate);
11467 }
11468
11469 line_header_local.sect_off = line_offset;
11470 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11471 line_header_local_hash = line_header_hash (&line_header_local);
11472 if (dwarf2_per_objfile->line_header_hash != NULL)
11473 {
11474 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11475 &line_header_local,
11476 line_header_local_hash, NO_INSERT);
11477
11478 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11479 is not present in *SLOT (since if there is something in *SLOT then
11480 it will be for a partial_unit). */
11481 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11482 {
11483 gdb_assert (*slot != NULL);
11484 cu->line_header = (struct line_header *) *slot;
11485 return;
11486 }
11487 }
11488
11489 /* dwarf_decode_line_header does not yet provide sufficient information.
11490 We always have to call also dwarf_decode_lines for it. */
11491 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11492 if (lh == NULL)
11493 return;
11494
11495 cu->line_header = lh.release ();
11496 cu->line_header_die_owner = die;
11497
11498 if (dwarf2_per_objfile->line_header_hash == NULL)
11499 slot = NULL;
11500 else
11501 {
11502 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11503 &line_header_local,
11504 line_header_local_hash, INSERT);
11505 gdb_assert (slot != NULL);
11506 }
11507 if (slot != NULL && *slot == NULL)
11508 {
11509 /* This newly decoded line number information unit will be owned
11510 by line_header_hash hash table. */
11511 *slot = cu->line_header;
11512 cu->line_header_die_owner = NULL;
11513 }
11514 else
11515 {
11516 /* We cannot free any current entry in (*slot) as that struct line_header
11517 may be already used by multiple CUs. Create only temporary decoded
11518 line_header for this CU - it may happen at most once for each line
11519 number information unit. And if we're not using line_header_hash
11520 then this is what we want as well. */
11521 gdb_assert (die->tag != DW_TAG_partial_unit);
11522 }
11523 decode_mapping = (die->tag != DW_TAG_partial_unit);
11524 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11525 decode_mapping);
11526
11527 }
11528
11529 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11530
11531 static void
11532 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11533 {
11534 struct dwarf2_per_objfile *dwarf2_per_objfile
11535 = cu->per_cu->dwarf2_per_objfile;
11536 struct objfile *objfile = dwarf2_per_objfile->objfile;
11537 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11538 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11539 CORE_ADDR highpc = ((CORE_ADDR) 0);
11540 struct attribute *attr;
11541 struct die_info *child_die;
11542 CORE_ADDR baseaddr;
11543
11544 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11545
11546 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11547
11548 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11549 from finish_block. */
11550 if (lowpc == ((CORE_ADDR) -1))
11551 lowpc = highpc;
11552 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11553
11554 file_and_directory fnd = find_file_and_directory (die, cu);
11555
11556 prepare_one_comp_unit (cu, die, cu->language);
11557
11558 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11559 standardised yet. As a workaround for the language detection we fall
11560 back to the DW_AT_producer string. */
11561 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11562 cu->language = language_opencl;
11563
11564 /* Similar hack for Go. */
11565 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11566 set_cu_language (DW_LANG_Go, cu);
11567
11568 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
11569
11570 /* Decode line number information if present. We do this before
11571 processing child DIEs, so that the line header table is available
11572 for DW_AT_decl_file. */
11573 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11574
11575 /* Process all dies in compilation unit. */
11576 if (die->child != NULL)
11577 {
11578 child_die = die->child;
11579 while (child_die && child_die->tag)
11580 {
11581 process_die (child_die, cu);
11582 child_die = sibling_die (child_die);
11583 }
11584 }
11585
11586 /* Decode macro information, if present. Dwarf 2 macro information
11587 refers to information in the line number info statement program
11588 header, so we can only read it if we've read the header
11589 successfully. */
11590 attr = dwarf2_attr (die, DW_AT_macros, cu);
11591 if (attr == NULL)
11592 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11593 if (attr && cu->line_header)
11594 {
11595 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11596 complaint (&symfile_complaints,
11597 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11598
11599 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11600 }
11601 else
11602 {
11603 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11604 if (attr && cu->line_header)
11605 {
11606 unsigned int macro_offset = DW_UNSND (attr);
11607
11608 dwarf_decode_macros (cu, macro_offset, 0);
11609 }
11610 }
11611 }
11612
11613 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11614 Create the set of symtabs used by this TU, or if this TU is sharing
11615 symtabs with another TU and the symtabs have already been created
11616 then restore those symtabs in the line header.
11617 We don't need the pc/line-number mapping for type units. */
11618
11619 static void
11620 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
11621 {
11622 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11623 struct type_unit_group *tu_group;
11624 int first_time;
11625 struct attribute *attr;
11626 unsigned int i;
11627 struct signatured_type *sig_type;
11628
11629 gdb_assert (per_cu->is_debug_types);
11630 sig_type = (struct signatured_type *) per_cu;
11631
11632 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11633
11634 /* If we're using .gdb_index (includes -readnow) then
11635 per_cu->type_unit_group may not have been set up yet. */
11636 if (sig_type->type_unit_group == NULL)
11637 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11638 tu_group = sig_type->type_unit_group;
11639
11640 /* If we've already processed this stmt_list there's no real need to
11641 do it again, we could fake it and just recreate the part we need
11642 (file name,index -> symtab mapping). If data shows this optimization
11643 is useful we can do it then. */
11644 first_time = tu_group->compunit_symtab == NULL;
11645
11646 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11647 debug info. */
11648 line_header_up lh;
11649 if (attr != NULL)
11650 {
11651 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11652 lh = dwarf_decode_line_header (line_offset, cu);
11653 }
11654 if (lh == NULL)
11655 {
11656 if (first_time)
11657 dwarf2_start_symtab (cu, "", NULL, 0);
11658 else
11659 {
11660 gdb_assert (tu_group->symtabs == NULL);
11661 restart_symtab (tu_group->compunit_symtab, "", 0);
11662 }
11663 return;
11664 }
11665
11666 cu->line_header = lh.release ();
11667 cu->line_header_die_owner = die;
11668
11669 if (first_time)
11670 {
11671 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
11672
11673 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11674 still initializing it, and our caller (a few levels up)
11675 process_full_type_unit still needs to know if this is the first
11676 time. */
11677
11678 tu_group->num_symtabs = cu->line_header->file_names.size ();
11679 tu_group->symtabs = XNEWVEC (struct symtab *,
11680 cu->line_header->file_names.size ());
11681
11682 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11683 {
11684 file_entry &fe = cu->line_header->file_names[i];
11685
11686 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
11687
11688 if (current_subfile->symtab == NULL)
11689 {
11690 /* NOTE: start_subfile will recognize when it's been
11691 passed a file it has already seen. So we can't
11692 assume there's a simple mapping from
11693 cu->line_header->file_names to subfiles, plus
11694 cu->line_header->file_names may contain dups. */
11695 current_subfile->symtab
11696 = allocate_symtab (cust, current_subfile->name);
11697 }
11698
11699 fe.symtab = current_subfile->symtab;
11700 tu_group->symtabs[i] = fe.symtab;
11701 }
11702 }
11703 else
11704 {
11705 restart_symtab (tu_group->compunit_symtab, "", 0);
11706
11707 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11708 {
11709 file_entry &fe = cu->line_header->file_names[i];
11710
11711 fe.symtab = tu_group->symtabs[i];
11712 }
11713 }
11714
11715 /* The main symtab is allocated last. Type units don't have DW_AT_name
11716 so they don't have a "real" (so to speak) symtab anyway.
11717 There is later code that will assign the main symtab to all symbols
11718 that don't have one. We need to handle the case of a symbol with a
11719 missing symtab (DW_AT_decl_file) anyway. */
11720 }
11721
11722 /* Process DW_TAG_type_unit.
11723 For TUs we want to skip the first top level sibling if it's not the
11724 actual type being defined by this TU. In this case the first top
11725 level sibling is there to provide context only. */
11726
11727 static void
11728 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11729 {
11730 struct die_info *child_die;
11731
11732 prepare_one_comp_unit (cu, die, language_minimal);
11733
11734 /* Initialize (or reinitialize) the machinery for building symtabs.
11735 We do this before processing child DIEs, so that the line header table
11736 is available for DW_AT_decl_file. */
11737 setup_type_unit_groups (die, cu);
11738
11739 if (die->child != NULL)
11740 {
11741 child_die = die->child;
11742 while (child_die && child_die->tag)
11743 {
11744 process_die (child_die, cu);
11745 child_die = sibling_die (child_die);
11746 }
11747 }
11748 }
11749 \f
11750 /* DWO/DWP files.
11751
11752 http://gcc.gnu.org/wiki/DebugFission
11753 http://gcc.gnu.org/wiki/DebugFissionDWP
11754
11755 To simplify handling of both DWO files ("object" files with the DWARF info)
11756 and DWP files (a file with the DWOs packaged up into one file), we treat
11757 DWP files as having a collection of virtual DWO files. */
11758
11759 static hashval_t
11760 hash_dwo_file (const void *item)
11761 {
11762 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11763 hashval_t hash;
11764
11765 hash = htab_hash_string (dwo_file->dwo_name);
11766 if (dwo_file->comp_dir != NULL)
11767 hash += htab_hash_string (dwo_file->comp_dir);
11768 return hash;
11769 }
11770
11771 static int
11772 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11773 {
11774 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11775 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11776
11777 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11778 return 0;
11779 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11780 return lhs->comp_dir == rhs->comp_dir;
11781 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11782 }
11783
11784 /* Allocate a hash table for DWO files. */
11785
11786 static htab_t
11787 allocate_dwo_file_hash_table (struct objfile *objfile)
11788 {
11789 return htab_create_alloc_ex (41,
11790 hash_dwo_file,
11791 eq_dwo_file,
11792 NULL,
11793 &objfile->objfile_obstack,
11794 hashtab_obstack_allocate,
11795 dummy_obstack_deallocate);
11796 }
11797
11798 /* Lookup DWO file DWO_NAME. */
11799
11800 static void **
11801 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11802 const char *dwo_name,
11803 const char *comp_dir)
11804 {
11805 struct dwo_file find_entry;
11806 void **slot;
11807
11808 if (dwarf2_per_objfile->dwo_files == NULL)
11809 dwarf2_per_objfile->dwo_files
11810 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11811
11812 memset (&find_entry, 0, sizeof (find_entry));
11813 find_entry.dwo_name = dwo_name;
11814 find_entry.comp_dir = comp_dir;
11815 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11816
11817 return slot;
11818 }
11819
11820 static hashval_t
11821 hash_dwo_unit (const void *item)
11822 {
11823 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11824
11825 /* This drops the top 32 bits of the id, but is ok for a hash. */
11826 return dwo_unit->signature;
11827 }
11828
11829 static int
11830 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11831 {
11832 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11833 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11834
11835 /* The signature is assumed to be unique within the DWO file.
11836 So while object file CU dwo_id's always have the value zero,
11837 that's OK, assuming each object file DWO file has only one CU,
11838 and that's the rule for now. */
11839 return lhs->signature == rhs->signature;
11840 }
11841
11842 /* Allocate a hash table for DWO CUs,TUs.
11843 There is one of these tables for each of CUs,TUs for each DWO file. */
11844
11845 static htab_t
11846 allocate_dwo_unit_table (struct objfile *objfile)
11847 {
11848 /* Start out with a pretty small number.
11849 Generally DWO files contain only one CU and maybe some TUs. */
11850 return htab_create_alloc_ex (3,
11851 hash_dwo_unit,
11852 eq_dwo_unit,
11853 NULL,
11854 &objfile->objfile_obstack,
11855 hashtab_obstack_allocate,
11856 dummy_obstack_deallocate);
11857 }
11858
11859 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11860
11861 struct create_dwo_cu_data
11862 {
11863 struct dwo_file *dwo_file;
11864 struct dwo_unit dwo_unit;
11865 };
11866
11867 /* die_reader_func for create_dwo_cu. */
11868
11869 static void
11870 create_dwo_cu_reader (const struct die_reader_specs *reader,
11871 const gdb_byte *info_ptr,
11872 struct die_info *comp_unit_die,
11873 int has_children,
11874 void *datap)
11875 {
11876 struct dwarf2_cu *cu = reader->cu;
11877 sect_offset sect_off = cu->per_cu->sect_off;
11878 struct dwarf2_section_info *section = cu->per_cu->section;
11879 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11880 struct dwo_file *dwo_file = data->dwo_file;
11881 struct dwo_unit *dwo_unit = &data->dwo_unit;
11882 struct attribute *attr;
11883
11884 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11885 if (attr == NULL)
11886 {
11887 complaint (&symfile_complaints,
11888 _("Dwarf Error: debug entry at offset %s is missing"
11889 " its dwo_id [in module %s]"),
11890 sect_offset_str (sect_off), dwo_file->dwo_name);
11891 return;
11892 }
11893
11894 dwo_unit->dwo_file = dwo_file;
11895 dwo_unit->signature = DW_UNSND (attr);
11896 dwo_unit->section = section;
11897 dwo_unit->sect_off = sect_off;
11898 dwo_unit->length = cu->per_cu->length;
11899
11900 if (dwarf_read_debug)
11901 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11902 sect_offset_str (sect_off),
11903 hex_string (dwo_unit->signature));
11904 }
11905
11906 /* Create the dwo_units for the CUs in a DWO_FILE.
11907 Note: This function processes DWO files only, not DWP files. */
11908
11909 static void
11910 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11911 struct dwo_file &dwo_file, dwarf2_section_info &section,
11912 htab_t &cus_htab)
11913 {
11914 struct objfile *objfile = dwarf2_per_objfile->objfile;
11915 const gdb_byte *info_ptr, *end_ptr;
11916
11917 dwarf2_read_section (objfile, &section);
11918 info_ptr = section.buffer;
11919
11920 if (info_ptr == NULL)
11921 return;
11922
11923 if (dwarf_read_debug)
11924 {
11925 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11926 get_section_name (&section),
11927 get_section_file_name (&section));
11928 }
11929
11930 end_ptr = info_ptr + section.size;
11931 while (info_ptr < end_ptr)
11932 {
11933 struct dwarf2_per_cu_data per_cu;
11934 struct create_dwo_cu_data create_dwo_cu_data;
11935 struct dwo_unit *dwo_unit;
11936 void **slot;
11937 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11938
11939 memset (&create_dwo_cu_data.dwo_unit, 0,
11940 sizeof (create_dwo_cu_data.dwo_unit));
11941 memset (&per_cu, 0, sizeof (per_cu));
11942 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11943 per_cu.is_debug_types = 0;
11944 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11945 per_cu.section = &section;
11946 create_dwo_cu_data.dwo_file = &dwo_file;
11947
11948 init_cutu_and_read_dies_no_follow (
11949 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11950 info_ptr += per_cu.length;
11951
11952 // If the unit could not be parsed, skip it.
11953 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11954 continue;
11955
11956 if (cus_htab == NULL)
11957 cus_htab = allocate_dwo_unit_table (objfile);
11958
11959 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11960 *dwo_unit = create_dwo_cu_data.dwo_unit;
11961 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11962 gdb_assert (slot != NULL);
11963 if (*slot != NULL)
11964 {
11965 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11966 sect_offset dup_sect_off = dup_cu->sect_off;
11967
11968 complaint (&symfile_complaints,
11969 _("debug cu entry at offset %s is duplicate to"
11970 " the entry at offset %s, signature %s"),
11971 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11972 hex_string (dwo_unit->signature));
11973 }
11974 *slot = (void *)dwo_unit;
11975 }
11976 }
11977
11978 /* DWP file .debug_{cu,tu}_index section format:
11979 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11980
11981 DWP Version 1:
11982
11983 Both index sections have the same format, and serve to map a 64-bit
11984 signature to a set of section numbers. Each section begins with a header,
11985 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11986 indexes, and a pool of 32-bit section numbers. The index sections will be
11987 aligned at 8-byte boundaries in the file.
11988
11989 The index section header consists of:
11990
11991 V, 32 bit version number
11992 -, 32 bits unused
11993 N, 32 bit number of compilation units or type units in the index
11994 M, 32 bit number of slots in the hash table
11995
11996 Numbers are recorded using the byte order of the application binary.
11997
11998 The hash table begins at offset 16 in the section, and consists of an array
11999 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12000 order of the application binary). Unused slots in the hash table are 0.
12001 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12002
12003 The parallel table begins immediately after the hash table
12004 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12005 array of 32-bit indexes (using the byte order of the application binary),
12006 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12007 table contains a 32-bit index into the pool of section numbers. For unused
12008 hash table slots, the corresponding entry in the parallel table will be 0.
12009
12010 The pool of section numbers begins immediately following the hash table
12011 (at offset 16 + 12 * M from the beginning of the section). The pool of
12012 section numbers consists of an array of 32-bit words (using the byte order
12013 of the application binary). Each item in the array is indexed starting
12014 from 0. The hash table entry provides the index of the first section
12015 number in the set. Additional section numbers in the set follow, and the
12016 set is terminated by a 0 entry (section number 0 is not used in ELF).
12017
12018 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12019 section must be the first entry in the set, and the .debug_abbrev.dwo must
12020 be the second entry. Other members of the set may follow in any order.
12021
12022 ---
12023
12024 DWP Version 2:
12025
12026 DWP Version 2 combines all the .debug_info, etc. sections into one,
12027 and the entries in the index tables are now offsets into these sections.
12028 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12029 section.
12030
12031 Index Section Contents:
12032 Header
12033 Hash Table of Signatures dwp_hash_table.hash_table
12034 Parallel Table of Indices dwp_hash_table.unit_table
12035 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12036 Table of Section Sizes dwp_hash_table.v2.sizes
12037
12038 The index section header consists of:
12039
12040 V, 32 bit version number
12041 L, 32 bit number of columns in the table of section offsets
12042 N, 32 bit number of compilation units or type units in the index
12043 M, 32 bit number of slots in the hash table
12044
12045 Numbers are recorded using the byte order of the application binary.
12046
12047 The hash table has the same format as version 1.
12048 The parallel table of indices has the same format as version 1,
12049 except that the entries are origin-1 indices into the table of sections
12050 offsets and the table of section sizes.
12051
12052 The table of offsets begins immediately following the parallel table
12053 (at offset 16 + 12 * M from the beginning of the section). The table is
12054 a two-dimensional array of 32-bit words (using the byte order of the
12055 application binary), with L columns and N+1 rows, in row-major order.
12056 Each row in the array is indexed starting from 0. The first row provides
12057 a key to the remaining rows: each column in this row provides an identifier
12058 for a debug section, and the offsets in the same column of subsequent rows
12059 refer to that section. The section identifiers are:
12060
12061 DW_SECT_INFO 1 .debug_info.dwo
12062 DW_SECT_TYPES 2 .debug_types.dwo
12063 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12064 DW_SECT_LINE 4 .debug_line.dwo
12065 DW_SECT_LOC 5 .debug_loc.dwo
12066 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12067 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12068 DW_SECT_MACRO 8 .debug_macro.dwo
12069
12070 The offsets provided by the CU and TU index sections are the base offsets
12071 for the contributions made by each CU or TU to the corresponding section
12072 in the package file. Each CU and TU header contains an abbrev_offset
12073 field, used to find the abbreviations table for that CU or TU within the
12074 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12075 be interpreted as relative to the base offset given in the index section.
12076 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12077 should be interpreted as relative to the base offset for .debug_line.dwo,
12078 and offsets into other debug sections obtained from DWARF attributes should
12079 also be interpreted as relative to the corresponding base offset.
12080
12081 The table of sizes begins immediately following the table of offsets.
12082 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12083 with L columns and N rows, in row-major order. Each row in the array is
12084 indexed starting from 1 (row 0 is shared by the two tables).
12085
12086 ---
12087
12088 Hash table lookup is handled the same in version 1 and 2:
12089
12090 We assume that N and M will not exceed 2^32 - 1.
12091 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12092
12093 Given a 64-bit compilation unit signature or a type signature S, an entry
12094 in the hash table is located as follows:
12095
12096 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12097 the low-order k bits all set to 1.
12098
12099 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12100
12101 3) If the hash table entry at index H matches the signature, use that
12102 entry. If the hash table entry at index H is unused (all zeroes),
12103 terminate the search: the signature is not present in the table.
12104
12105 4) Let H = (H + H') modulo M. Repeat at Step 3.
12106
12107 Because M > N and H' and M are relatively prime, the search is guaranteed
12108 to stop at an unused slot or find the match. */
12109
12110 /* Create a hash table to map DWO IDs to their CU/TU entry in
12111 .debug_{info,types}.dwo in DWP_FILE.
12112 Returns NULL if there isn't one.
12113 Note: This function processes DWP files only, not DWO files. */
12114
12115 static struct dwp_hash_table *
12116 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12117 struct dwp_file *dwp_file, int is_debug_types)
12118 {
12119 struct objfile *objfile = dwarf2_per_objfile->objfile;
12120 bfd *dbfd = dwp_file->dbfd;
12121 const gdb_byte *index_ptr, *index_end;
12122 struct dwarf2_section_info *index;
12123 uint32_t version, nr_columns, nr_units, nr_slots;
12124 struct dwp_hash_table *htab;
12125
12126 if (is_debug_types)
12127 index = &dwp_file->sections.tu_index;
12128 else
12129 index = &dwp_file->sections.cu_index;
12130
12131 if (dwarf2_section_empty_p (index))
12132 return NULL;
12133 dwarf2_read_section (objfile, index);
12134
12135 index_ptr = index->buffer;
12136 index_end = index_ptr + index->size;
12137
12138 version = read_4_bytes (dbfd, index_ptr);
12139 index_ptr += 4;
12140 if (version == 2)
12141 nr_columns = read_4_bytes (dbfd, index_ptr);
12142 else
12143 nr_columns = 0;
12144 index_ptr += 4;
12145 nr_units = read_4_bytes (dbfd, index_ptr);
12146 index_ptr += 4;
12147 nr_slots = read_4_bytes (dbfd, index_ptr);
12148 index_ptr += 4;
12149
12150 if (version != 1 && version != 2)
12151 {
12152 error (_("Dwarf Error: unsupported DWP file version (%s)"
12153 " [in module %s]"),
12154 pulongest (version), dwp_file->name);
12155 }
12156 if (nr_slots != (nr_slots & -nr_slots))
12157 {
12158 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12159 " is not power of 2 [in module %s]"),
12160 pulongest (nr_slots), dwp_file->name);
12161 }
12162
12163 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12164 htab->version = version;
12165 htab->nr_columns = nr_columns;
12166 htab->nr_units = nr_units;
12167 htab->nr_slots = nr_slots;
12168 htab->hash_table = index_ptr;
12169 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12170
12171 /* Exit early if the table is empty. */
12172 if (nr_slots == 0 || nr_units == 0
12173 || (version == 2 && nr_columns == 0))
12174 {
12175 /* All must be zero. */
12176 if (nr_slots != 0 || nr_units != 0
12177 || (version == 2 && nr_columns != 0))
12178 {
12179 complaint (&symfile_complaints,
12180 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12181 " all zero [in modules %s]"),
12182 dwp_file->name);
12183 }
12184 return htab;
12185 }
12186
12187 if (version == 1)
12188 {
12189 htab->section_pool.v1.indices =
12190 htab->unit_table + sizeof (uint32_t) * nr_slots;
12191 /* It's harder to decide whether the section is too small in v1.
12192 V1 is deprecated anyway so we punt. */
12193 }
12194 else
12195 {
12196 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12197 int *ids = htab->section_pool.v2.section_ids;
12198 /* Reverse map for error checking. */
12199 int ids_seen[DW_SECT_MAX + 1];
12200 int i;
12201
12202 if (nr_columns < 2)
12203 {
12204 error (_("Dwarf Error: bad DWP hash table, too few columns"
12205 " in section table [in module %s]"),
12206 dwp_file->name);
12207 }
12208 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12209 {
12210 error (_("Dwarf Error: bad DWP hash table, too many columns"
12211 " in section table [in module %s]"),
12212 dwp_file->name);
12213 }
12214 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12215 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12216 for (i = 0; i < nr_columns; ++i)
12217 {
12218 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12219
12220 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12221 {
12222 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12223 " in section table [in module %s]"),
12224 id, dwp_file->name);
12225 }
12226 if (ids_seen[id] != -1)
12227 {
12228 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12229 " id %d in section table [in module %s]"),
12230 id, dwp_file->name);
12231 }
12232 ids_seen[id] = i;
12233 ids[i] = id;
12234 }
12235 /* Must have exactly one info or types section. */
12236 if (((ids_seen[DW_SECT_INFO] != -1)
12237 + (ids_seen[DW_SECT_TYPES] != -1))
12238 != 1)
12239 {
12240 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12241 " DWO info/types section [in module %s]"),
12242 dwp_file->name);
12243 }
12244 /* Must have an abbrev section. */
12245 if (ids_seen[DW_SECT_ABBREV] == -1)
12246 {
12247 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12248 " section [in module %s]"),
12249 dwp_file->name);
12250 }
12251 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12252 htab->section_pool.v2.sizes =
12253 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12254 * nr_units * nr_columns);
12255 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12256 * nr_units * nr_columns))
12257 > index_end)
12258 {
12259 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12260 " [in module %s]"),
12261 dwp_file->name);
12262 }
12263 }
12264
12265 return htab;
12266 }
12267
12268 /* Update SECTIONS with the data from SECTP.
12269
12270 This function is like the other "locate" section routines that are
12271 passed to bfd_map_over_sections, but in this context the sections to
12272 read comes from the DWP V1 hash table, not the full ELF section table.
12273
12274 The result is non-zero for success, or zero if an error was found. */
12275
12276 static int
12277 locate_v1_virtual_dwo_sections (asection *sectp,
12278 struct virtual_v1_dwo_sections *sections)
12279 {
12280 const struct dwop_section_names *names = &dwop_section_names;
12281
12282 if (section_is_p (sectp->name, &names->abbrev_dwo))
12283 {
12284 /* There can be only one. */
12285 if (sections->abbrev.s.section != NULL)
12286 return 0;
12287 sections->abbrev.s.section = sectp;
12288 sections->abbrev.size = bfd_get_section_size (sectp);
12289 }
12290 else if (section_is_p (sectp->name, &names->info_dwo)
12291 || section_is_p (sectp->name, &names->types_dwo))
12292 {
12293 /* There can be only one. */
12294 if (sections->info_or_types.s.section != NULL)
12295 return 0;
12296 sections->info_or_types.s.section = sectp;
12297 sections->info_or_types.size = bfd_get_section_size (sectp);
12298 }
12299 else if (section_is_p (sectp->name, &names->line_dwo))
12300 {
12301 /* There can be only one. */
12302 if (sections->line.s.section != NULL)
12303 return 0;
12304 sections->line.s.section = sectp;
12305 sections->line.size = bfd_get_section_size (sectp);
12306 }
12307 else if (section_is_p (sectp->name, &names->loc_dwo))
12308 {
12309 /* There can be only one. */
12310 if (sections->loc.s.section != NULL)
12311 return 0;
12312 sections->loc.s.section = sectp;
12313 sections->loc.size = bfd_get_section_size (sectp);
12314 }
12315 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12316 {
12317 /* There can be only one. */
12318 if (sections->macinfo.s.section != NULL)
12319 return 0;
12320 sections->macinfo.s.section = sectp;
12321 sections->macinfo.size = bfd_get_section_size (sectp);
12322 }
12323 else if (section_is_p (sectp->name, &names->macro_dwo))
12324 {
12325 /* There can be only one. */
12326 if (sections->macro.s.section != NULL)
12327 return 0;
12328 sections->macro.s.section = sectp;
12329 sections->macro.size = bfd_get_section_size (sectp);
12330 }
12331 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12332 {
12333 /* There can be only one. */
12334 if (sections->str_offsets.s.section != NULL)
12335 return 0;
12336 sections->str_offsets.s.section = sectp;
12337 sections->str_offsets.size = bfd_get_section_size (sectp);
12338 }
12339 else
12340 {
12341 /* No other kind of section is valid. */
12342 return 0;
12343 }
12344
12345 return 1;
12346 }
12347
12348 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12349 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12350 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12351 This is for DWP version 1 files. */
12352
12353 static struct dwo_unit *
12354 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12355 struct dwp_file *dwp_file,
12356 uint32_t unit_index,
12357 const char *comp_dir,
12358 ULONGEST signature, int is_debug_types)
12359 {
12360 struct objfile *objfile = dwarf2_per_objfile->objfile;
12361 const struct dwp_hash_table *dwp_htab =
12362 is_debug_types ? dwp_file->tus : dwp_file->cus;
12363 bfd *dbfd = dwp_file->dbfd;
12364 const char *kind = is_debug_types ? "TU" : "CU";
12365 struct dwo_file *dwo_file;
12366 struct dwo_unit *dwo_unit;
12367 struct virtual_v1_dwo_sections sections;
12368 void **dwo_file_slot;
12369 int i;
12370
12371 gdb_assert (dwp_file->version == 1);
12372
12373 if (dwarf_read_debug)
12374 {
12375 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12376 kind,
12377 pulongest (unit_index), hex_string (signature),
12378 dwp_file->name);
12379 }
12380
12381 /* Fetch the sections of this DWO unit.
12382 Put a limit on the number of sections we look for so that bad data
12383 doesn't cause us to loop forever. */
12384
12385 #define MAX_NR_V1_DWO_SECTIONS \
12386 (1 /* .debug_info or .debug_types */ \
12387 + 1 /* .debug_abbrev */ \
12388 + 1 /* .debug_line */ \
12389 + 1 /* .debug_loc */ \
12390 + 1 /* .debug_str_offsets */ \
12391 + 1 /* .debug_macro or .debug_macinfo */ \
12392 + 1 /* trailing zero */)
12393
12394 memset (&sections, 0, sizeof (sections));
12395
12396 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12397 {
12398 asection *sectp;
12399 uint32_t section_nr =
12400 read_4_bytes (dbfd,
12401 dwp_htab->section_pool.v1.indices
12402 + (unit_index + i) * sizeof (uint32_t));
12403
12404 if (section_nr == 0)
12405 break;
12406 if (section_nr >= dwp_file->num_sections)
12407 {
12408 error (_("Dwarf Error: bad DWP hash table, section number too large"
12409 " [in module %s]"),
12410 dwp_file->name);
12411 }
12412
12413 sectp = dwp_file->elf_sections[section_nr];
12414 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12415 {
12416 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12417 " [in module %s]"),
12418 dwp_file->name);
12419 }
12420 }
12421
12422 if (i < 2
12423 || dwarf2_section_empty_p (&sections.info_or_types)
12424 || dwarf2_section_empty_p (&sections.abbrev))
12425 {
12426 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12427 " [in module %s]"),
12428 dwp_file->name);
12429 }
12430 if (i == MAX_NR_V1_DWO_SECTIONS)
12431 {
12432 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12433 " [in module %s]"),
12434 dwp_file->name);
12435 }
12436
12437 /* It's easier for the rest of the code if we fake a struct dwo_file and
12438 have dwo_unit "live" in that. At least for now.
12439
12440 The DWP file can be made up of a random collection of CUs and TUs.
12441 However, for each CU + set of TUs that came from the same original DWO
12442 file, we can combine them back into a virtual DWO file to save space
12443 (fewer struct dwo_file objects to allocate). Remember that for really
12444 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12445
12446 std::string virtual_dwo_name =
12447 string_printf ("virtual-dwo/%d-%d-%d-%d",
12448 get_section_id (&sections.abbrev),
12449 get_section_id (&sections.line),
12450 get_section_id (&sections.loc),
12451 get_section_id (&sections.str_offsets));
12452 /* Can we use an existing virtual DWO file? */
12453 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12454 virtual_dwo_name.c_str (),
12455 comp_dir);
12456 /* Create one if necessary. */
12457 if (*dwo_file_slot == NULL)
12458 {
12459 if (dwarf_read_debug)
12460 {
12461 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12462 virtual_dwo_name.c_str ());
12463 }
12464 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12465 dwo_file->dwo_name
12466 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12467 virtual_dwo_name.c_str (),
12468 virtual_dwo_name.size ());
12469 dwo_file->comp_dir = comp_dir;
12470 dwo_file->sections.abbrev = sections.abbrev;
12471 dwo_file->sections.line = sections.line;
12472 dwo_file->sections.loc = sections.loc;
12473 dwo_file->sections.macinfo = sections.macinfo;
12474 dwo_file->sections.macro = sections.macro;
12475 dwo_file->sections.str_offsets = sections.str_offsets;
12476 /* The "str" section is global to the entire DWP file. */
12477 dwo_file->sections.str = dwp_file->sections.str;
12478 /* The info or types section is assigned below to dwo_unit,
12479 there's no need to record it in dwo_file.
12480 Also, we can't simply record type sections in dwo_file because
12481 we record a pointer into the vector in dwo_unit. As we collect more
12482 types we'll grow the vector and eventually have to reallocate space
12483 for it, invalidating all copies of pointers into the previous
12484 contents. */
12485 *dwo_file_slot = dwo_file;
12486 }
12487 else
12488 {
12489 if (dwarf_read_debug)
12490 {
12491 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12492 virtual_dwo_name.c_str ());
12493 }
12494 dwo_file = (struct dwo_file *) *dwo_file_slot;
12495 }
12496
12497 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12498 dwo_unit->dwo_file = dwo_file;
12499 dwo_unit->signature = signature;
12500 dwo_unit->section =
12501 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12502 *dwo_unit->section = sections.info_or_types;
12503 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12504
12505 return dwo_unit;
12506 }
12507
12508 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12509 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12510 piece within that section used by a TU/CU, return a virtual section
12511 of just that piece. */
12512
12513 static struct dwarf2_section_info
12514 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12515 struct dwarf2_section_info *section,
12516 bfd_size_type offset, bfd_size_type size)
12517 {
12518 struct dwarf2_section_info result;
12519 asection *sectp;
12520
12521 gdb_assert (section != NULL);
12522 gdb_assert (!section->is_virtual);
12523
12524 memset (&result, 0, sizeof (result));
12525 result.s.containing_section = section;
12526 result.is_virtual = 1;
12527
12528 if (size == 0)
12529 return result;
12530
12531 sectp = get_section_bfd_section (section);
12532
12533 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12534 bounds of the real section. This is a pretty-rare event, so just
12535 flag an error (easier) instead of a warning and trying to cope. */
12536 if (sectp == NULL
12537 || offset + size > bfd_get_section_size (sectp))
12538 {
12539 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12540 " in section %s [in module %s]"),
12541 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12542 objfile_name (dwarf2_per_objfile->objfile));
12543 }
12544
12545 result.virtual_offset = offset;
12546 result.size = size;
12547 return result;
12548 }
12549
12550 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12551 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12552 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12553 This is for DWP version 2 files. */
12554
12555 static struct dwo_unit *
12556 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12557 struct dwp_file *dwp_file,
12558 uint32_t unit_index,
12559 const char *comp_dir,
12560 ULONGEST signature, int is_debug_types)
12561 {
12562 struct objfile *objfile = dwarf2_per_objfile->objfile;
12563 const struct dwp_hash_table *dwp_htab =
12564 is_debug_types ? dwp_file->tus : dwp_file->cus;
12565 bfd *dbfd = dwp_file->dbfd;
12566 const char *kind = is_debug_types ? "TU" : "CU";
12567 struct dwo_file *dwo_file;
12568 struct dwo_unit *dwo_unit;
12569 struct virtual_v2_dwo_sections sections;
12570 void **dwo_file_slot;
12571 int i;
12572
12573 gdb_assert (dwp_file->version == 2);
12574
12575 if (dwarf_read_debug)
12576 {
12577 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12578 kind,
12579 pulongest (unit_index), hex_string (signature),
12580 dwp_file->name);
12581 }
12582
12583 /* Fetch the section offsets of this DWO unit. */
12584
12585 memset (&sections, 0, sizeof (sections));
12586
12587 for (i = 0; i < dwp_htab->nr_columns; ++i)
12588 {
12589 uint32_t offset = read_4_bytes (dbfd,
12590 dwp_htab->section_pool.v2.offsets
12591 + (((unit_index - 1) * dwp_htab->nr_columns
12592 + i)
12593 * sizeof (uint32_t)));
12594 uint32_t size = read_4_bytes (dbfd,
12595 dwp_htab->section_pool.v2.sizes
12596 + (((unit_index - 1) * dwp_htab->nr_columns
12597 + i)
12598 * sizeof (uint32_t)));
12599
12600 switch (dwp_htab->section_pool.v2.section_ids[i])
12601 {
12602 case DW_SECT_INFO:
12603 case DW_SECT_TYPES:
12604 sections.info_or_types_offset = offset;
12605 sections.info_or_types_size = size;
12606 break;
12607 case DW_SECT_ABBREV:
12608 sections.abbrev_offset = offset;
12609 sections.abbrev_size = size;
12610 break;
12611 case DW_SECT_LINE:
12612 sections.line_offset = offset;
12613 sections.line_size = size;
12614 break;
12615 case DW_SECT_LOC:
12616 sections.loc_offset = offset;
12617 sections.loc_size = size;
12618 break;
12619 case DW_SECT_STR_OFFSETS:
12620 sections.str_offsets_offset = offset;
12621 sections.str_offsets_size = size;
12622 break;
12623 case DW_SECT_MACINFO:
12624 sections.macinfo_offset = offset;
12625 sections.macinfo_size = size;
12626 break;
12627 case DW_SECT_MACRO:
12628 sections.macro_offset = offset;
12629 sections.macro_size = size;
12630 break;
12631 }
12632 }
12633
12634 /* It's easier for the rest of the code if we fake a struct dwo_file and
12635 have dwo_unit "live" in that. At least for now.
12636
12637 The DWP file can be made up of a random collection of CUs and TUs.
12638 However, for each CU + set of TUs that came from the same original DWO
12639 file, we can combine them back into a virtual DWO file to save space
12640 (fewer struct dwo_file objects to allocate). Remember that for really
12641 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12642
12643 std::string virtual_dwo_name =
12644 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12645 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12646 (long) (sections.line_size ? sections.line_offset : 0),
12647 (long) (sections.loc_size ? sections.loc_offset : 0),
12648 (long) (sections.str_offsets_size
12649 ? sections.str_offsets_offset : 0));
12650 /* Can we use an existing virtual DWO file? */
12651 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12652 virtual_dwo_name.c_str (),
12653 comp_dir);
12654 /* Create one if necessary. */
12655 if (*dwo_file_slot == NULL)
12656 {
12657 if (dwarf_read_debug)
12658 {
12659 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12660 virtual_dwo_name.c_str ());
12661 }
12662 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12663 dwo_file->dwo_name
12664 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12665 virtual_dwo_name.c_str (),
12666 virtual_dwo_name.size ());
12667 dwo_file->comp_dir = comp_dir;
12668 dwo_file->sections.abbrev =
12669 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12670 sections.abbrev_offset, sections.abbrev_size);
12671 dwo_file->sections.line =
12672 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12673 sections.line_offset, sections.line_size);
12674 dwo_file->sections.loc =
12675 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12676 sections.loc_offset, sections.loc_size);
12677 dwo_file->sections.macinfo =
12678 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12679 sections.macinfo_offset, sections.macinfo_size);
12680 dwo_file->sections.macro =
12681 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12682 sections.macro_offset, sections.macro_size);
12683 dwo_file->sections.str_offsets =
12684 create_dwp_v2_section (dwarf2_per_objfile,
12685 &dwp_file->sections.str_offsets,
12686 sections.str_offsets_offset,
12687 sections.str_offsets_size);
12688 /* The "str" section is global to the entire DWP file. */
12689 dwo_file->sections.str = dwp_file->sections.str;
12690 /* The info or types section is assigned below to dwo_unit,
12691 there's no need to record it in dwo_file.
12692 Also, we can't simply record type sections in dwo_file because
12693 we record a pointer into the vector in dwo_unit. As we collect more
12694 types we'll grow the vector and eventually have to reallocate space
12695 for it, invalidating all copies of pointers into the previous
12696 contents. */
12697 *dwo_file_slot = dwo_file;
12698 }
12699 else
12700 {
12701 if (dwarf_read_debug)
12702 {
12703 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12704 virtual_dwo_name.c_str ());
12705 }
12706 dwo_file = (struct dwo_file *) *dwo_file_slot;
12707 }
12708
12709 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12710 dwo_unit->dwo_file = dwo_file;
12711 dwo_unit->signature = signature;
12712 dwo_unit->section =
12713 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12714 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12715 is_debug_types
12716 ? &dwp_file->sections.types
12717 : &dwp_file->sections.info,
12718 sections.info_or_types_offset,
12719 sections.info_or_types_size);
12720 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12721
12722 return dwo_unit;
12723 }
12724
12725 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12726 Returns NULL if the signature isn't found. */
12727
12728 static struct dwo_unit *
12729 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12730 struct dwp_file *dwp_file, const char *comp_dir,
12731 ULONGEST signature, int is_debug_types)
12732 {
12733 const struct dwp_hash_table *dwp_htab =
12734 is_debug_types ? dwp_file->tus : dwp_file->cus;
12735 bfd *dbfd = dwp_file->dbfd;
12736 uint32_t mask = dwp_htab->nr_slots - 1;
12737 uint32_t hash = signature & mask;
12738 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12739 unsigned int i;
12740 void **slot;
12741 struct dwo_unit find_dwo_cu;
12742
12743 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12744 find_dwo_cu.signature = signature;
12745 slot = htab_find_slot (is_debug_types
12746 ? dwp_file->loaded_tus
12747 : dwp_file->loaded_cus,
12748 &find_dwo_cu, INSERT);
12749
12750 if (*slot != NULL)
12751 return (struct dwo_unit *) *slot;
12752
12753 /* Use a for loop so that we don't loop forever on bad debug info. */
12754 for (i = 0; i < dwp_htab->nr_slots; ++i)
12755 {
12756 ULONGEST signature_in_table;
12757
12758 signature_in_table =
12759 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12760 if (signature_in_table == signature)
12761 {
12762 uint32_t unit_index =
12763 read_4_bytes (dbfd,
12764 dwp_htab->unit_table + hash * sizeof (uint32_t));
12765
12766 if (dwp_file->version == 1)
12767 {
12768 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12769 dwp_file, unit_index,
12770 comp_dir, signature,
12771 is_debug_types);
12772 }
12773 else
12774 {
12775 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12776 dwp_file, unit_index,
12777 comp_dir, signature,
12778 is_debug_types);
12779 }
12780 return (struct dwo_unit *) *slot;
12781 }
12782 if (signature_in_table == 0)
12783 return NULL;
12784 hash = (hash + hash2) & mask;
12785 }
12786
12787 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12788 " [in module %s]"),
12789 dwp_file->name);
12790 }
12791
12792 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12793 Open the file specified by FILE_NAME and hand it off to BFD for
12794 preliminary analysis. Return a newly initialized bfd *, which
12795 includes a canonicalized copy of FILE_NAME.
12796 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12797 SEARCH_CWD is true if the current directory is to be searched.
12798 It will be searched before debug-file-directory.
12799 If successful, the file is added to the bfd include table of the
12800 objfile's bfd (see gdb_bfd_record_inclusion).
12801 If unable to find/open the file, return NULL.
12802 NOTE: This function is derived from symfile_bfd_open. */
12803
12804 static gdb_bfd_ref_ptr
12805 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12806 const char *file_name, int is_dwp, int search_cwd)
12807 {
12808 int desc;
12809 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12810 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12811 to debug_file_directory. */
12812 const char *search_path;
12813 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12814
12815 gdb::unique_xmalloc_ptr<char> search_path_holder;
12816 if (search_cwd)
12817 {
12818 if (*debug_file_directory != '\0')
12819 {
12820 search_path_holder.reset (concat (".", dirname_separator_string,
12821 debug_file_directory,
12822 (char *) NULL));
12823 search_path = search_path_holder.get ();
12824 }
12825 else
12826 search_path = ".";
12827 }
12828 else
12829 search_path = debug_file_directory;
12830
12831 openp_flags flags = OPF_RETURN_REALPATH;
12832 if (is_dwp)
12833 flags |= OPF_SEARCH_IN_PATH;
12834
12835 gdb::unique_xmalloc_ptr<char> absolute_name;
12836 desc = openp (search_path, flags, file_name,
12837 O_RDONLY | O_BINARY, &absolute_name);
12838 if (desc < 0)
12839 return NULL;
12840
12841 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12842 gnutarget, desc));
12843 if (sym_bfd == NULL)
12844 return NULL;
12845 bfd_set_cacheable (sym_bfd.get (), 1);
12846
12847 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12848 return NULL;
12849
12850 /* Success. Record the bfd as having been included by the objfile's bfd.
12851 This is important because things like demangled_names_hash lives in the
12852 objfile's per_bfd space and may have references to things like symbol
12853 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12854 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12855
12856 return sym_bfd;
12857 }
12858
12859 /* Try to open DWO file FILE_NAME.
12860 COMP_DIR is the DW_AT_comp_dir attribute.
12861 The result is the bfd handle of the file.
12862 If there is a problem finding or opening the file, return NULL.
12863 Upon success, the canonicalized path of the file is stored in the bfd,
12864 same as symfile_bfd_open. */
12865
12866 static gdb_bfd_ref_ptr
12867 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12868 const char *file_name, const char *comp_dir)
12869 {
12870 if (IS_ABSOLUTE_PATH (file_name))
12871 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12872 0 /*is_dwp*/, 0 /*search_cwd*/);
12873
12874 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12875
12876 if (comp_dir != NULL)
12877 {
12878 char *path_to_try = concat (comp_dir, SLASH_STRING,
12879 file_name, (char *) NULL);
12880
12881 /* NOTE: If comp_dir is a relative path, this will also try the
12882 search path, which seems useful. */
12883 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12884 path_to_try,
12885 0 /*is_dwp*/,
12886 1 /*search_cwd*/));
12887 xfree (path_to_try);
12888 if (abfd != NULL)
12889 return abfd;
12890 }
12891
12892 /* That didn't work, try debug-file-directory, which, despite its name,
12893 is a list of paths. */
12894
12895 if (*debug_file_directory == '\0')
12896 return NULL;
12897
12898 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12899 0 /*is_dwp*/, 1 /*search_cwd*/);
12900 }
12901
12902 /* This function is mapped across the sections and remembers the offset and
12903 size of each of the DWO debugging sections we are interested in. */
12904
12905 static void
12906 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12907 {
12908 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12909 const struct dwop_section_names *names = &dwop_section_names;
12910
12911 if (section_is_p (sectp->name, &names->abbrev_dwo))
12912 {
12913 dwo_sections->abbrev.s.section = sectp;
12914 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12915 }
12916 else if (section_is_p (sectp->name, &names->info_dwo))
12917 {
12918 dwo_sections->info.s.section = sectp;
12919 dwo_sections->info.size = bfd_get_section_size (sectp);
12920 }
12921 else if (section_is_p (sectp->name, &names->line_dwo))
12922 {
12923 dwo_sections->line.s.section = sectp;
12924 dwo_sections->line.size = bfd_get_section_size (sectp);
12925 }
12926 else if (section_is_p (sectp->name, &names->loc_dwo))
12927 {
12928 dwo_sections->loc.s.section = sectp;
12929 dwo_sections->loc.size = bfd_get_section_size (sectp);
12930 }
12931 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12932 {
12933 dwo_sections->macinfo.s.section = sectp;
12934 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12935 }
12936 else if (section_is_p (sectp->name, &names->macro_dwo))
12937 {
12938 dwo_sections->macro.s.section = sectp;
12939 dwo_sections->macro.size = bfd_get_section_size (sectp);
12940 }
12941 else if (section_is_p (sectp->name, &names->str_dwo))
12942 {
12943 dwo_sections->str.s.section = sectp;
12944 dwo_sections->str.size = bfd_get_section_size (sectp);
12945 }
12946 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12947 {
12948 dwo_sections->str_offsets.s.section = sectp;
12949 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12950 }
12951 else if (section_is_p (sectp->name, &names->types_dwo))
12952 {
12953 struct dwarf2_section_info type_section;
12954
12955 memset (&type_section, 0, sizeof (type_section));
12956 type_section.s.section = sectp;
12957 type_section.size = bfd_get_section_size (sectp);
12958 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
12959 &type_section);
12960 }
12961 }
12962
12963 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12964 by PER_CU. This is for the non-DWP case.
12965 The result is NULL if DWO_NAME can't be found. */
12966
12967 static struct dwo_file *
12968 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12969 const char *dwo_name, const char *comp_dir)
12970 {
12971 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12972 struct objfile *objfile = dwarf2_per_objfile->objfile;
12973
12974 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
12975 if (dbfd == NULL)
12976 {
12977 if (dwarf_read_debug)
12978 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12979 return NULL;
12980 }
12981
12982 /* We use a unique pointer here, despite the obstack allocation,
12983 because a dwo_file needs some cleanup if it is abandoned. */
12984 dwo_file_up dwo_file (OBSTACK_ZALLOC (&objfile->objfile_obstack,
12985 struct dwo_file));
12986 dwo_file->dwo_name = dwo_name;
12987 dwo_file->comp_dir = comp_dir;
12988 dwo_file->dbfd = dbfd.release ();
12989
12990 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
12991 &dwo_file->sections);
12992
12993 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
12994 dwo_file->cus);
12995
12996 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12997 dwo_file->sections.types, dwo_file->tus);
12998
12999 if (dwarf_read_debug)
13000 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13001
13002 return dwo_file.release ();
13003 }
13004
13005 /* This function is mapped across the sections and remembers the offset and
13006 size of each of the DWP debugging sections common to version 1 and 2 that
13007 we are interested in. */
13008
13009 static void
13010 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13011 void *dwp_file_ptr)
13012 {
13013 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13014 const struct dwop_section_names *names = &dwop_section_names;
13015 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13016
13017 /* Record the ELF section number for later lookup: this is what the
13018 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13019 gdb_assert (elf_section_nr < dwp_file->num_sections);
13020 dwp_file->elf_sections[elf_section_nr] = sectp;
13021
13022 /* Look for specific sections that we need. */
13023 if (section_is_p (sectp->name, &names->str_dwo))
13024 {
13025 dwp_file->sections.str.s.section = sectp;
13026 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13027 }
13028 else if (section_is_p (sectp->name, &names->cu_index))
13029 {
13030 dwp_file->sections.cu_index.s.section = sectp;
13031 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13032 }
13033 else if (section_is_p (sectp->name, &names->tu_index))
13034 {
13035 dwp_file->sections.tu_index.s.section = sectp;
13036 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13037 }
13038 }
13039
13040 /* This function is mapped across the sections and remembers the offset and
13041 size of each of the DWP version 2 debugging sections that we are interested
13042 in. This is split into a separate function because we don't know if we
13043 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13044
13045 static void
13046 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13047 {
13048 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13049 const struct dwop_section_names *names = &dwop_section_names;
13050 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13051
13052 /* Record the ELF section number for later lookup: this is what the
13053 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13054 gdb_assert (elf_section_nr < dwp_file->num_sections);
13055 dwp_file->elf_sections[elf_section_nr] = sectp;
13056
13057 /* Look for specific sections that we need. */
13058 if (section_is_p (sectp->name, &names->abbrev_dwo))
13059 {
13060 dwp_file->sections.abbrev.s.section = sectp;
13061 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13062 }
13063 else if (section_is_p (sectp->name, &names->info_dwo))
13064 {
13065 dwp_file->sections.info.s.section = sectp;
13066 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13067 }
13068 else if (section_is_p (sectp->name, &names->line_dwo))
13069 {
13070 dwp_file->sections.line.s.section = sectp;
13071 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13072 }
13073 else if (section_is_p (sectp->name, &names->loc_dwo))
13074 {
13075 dwp_file->sections.loc.s.section = sectp;
13076 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13077 }
13078 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13079 {
13080 dwp_file->sections.macinfo.s.section = sectp;
13081 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13082 }
13083 else if (section_is_p (sectp->name, &names->macro_dwo))
13084 {
13085 dwp_file->sections.macro.s.section = sectp;
13086 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13087 }
13088 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13089 {
13090 dwp_file->sections.str_offsets.s.section = sectp;
13091 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13092 }
13093 else if (section_is_p (sectp->name, &names->types_dwo))
13094 {
13095 dwp_file->sections.types.s.section = sectp;
13096 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13097 }
13098 }
13099
13100 /* Hash function for dwp_file loaded CUs/TUs. */
13101
13102 static hashval_t
13103 hash_dwp_loaded_cutus (const void *item)
13104 {
13105 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13106
13107 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13108 return dwo_unit->signature;
13109 }
13110
13111 /* Equality function for dwp_file loaded CUs/TUs. */
13112
13113 static int
13114 eq_dwp_loaded_cutus (const void *a, const void *b)
13115 {
13116 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13117 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13118
13119 return dua->signature == dub->signature;
13120 }
13121
13122 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13123
13124 static htab_t
13125 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13126 {
13127 return htab_create_alloc_ex (3,
13128 hash_dwp_loaded_cutus,
13129 eq_dwp_loaded_cutus,
13130 NULL,
13131 &objfile->objfile_obstack,
13132 hashtab_obstack_allocate,
13133 dummy_obstack_deallocate);
13134 }
13135
13136 /* Try to open DWP file FILE_NAME.
13137 The result is the bfd handle of the file.
13138 If there is a problem finding or opening the file, return NULL.
13139 Upon success, the canonicalized path of the file is stored in the bfd,
13140 same as symfile_bfd_open. */
13141
13142 static gdb_bfd_ref_ptr
13143 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13144 const char *file_name)
13145 {
13146 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13147 1 /*is_dwp*/,
13148 1 /*search_cwd*/));
13149 if (abfd != NULL)
13150 return abfd;
13151
13152 /* Work around upstream bug 15652.
13153 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13154 [Whether that's a "bug" is debatable, but it is getting in our way.]
13155 We have no real idea where the dwp file is, because gdb's realpath-ing
13156 of the executable's path may have discarded the needed info.
13157 [IWBN if the dwp file name was recorded in the executable, akin to
13158 .gnu_debuglink, but that doesn't exist yet.]
13159 Strip the directory from FILE_NAME and search again. */
13160 if (*debug_file_directory != '\0')
13161 {
13162 /* Don't implicitly search the current directory here.
13163 If the user wants to search "." to handle this case,
13164 it must be added to debug-file-directory. */
13165 return try_open_dwop_file (dwarf2_per_objfile,
13166 lbasename (file_name), 1 /*is_dwp*/,
13167 0 /*search_cwd*/);
13168 }
13169
13170 return NULL;
13171 }
13172
13173 /* Initialize the use of the DWP file for the current objfile.
13174 By convention the name of the DWP file is ${objfile}.dwp.
13175 The result is NULL if it can't be found. */
13176
13177 static struct dwp_file *
13178 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13179 {
13180 struct objfile *objfile = dwarf2_per_objfile->objfile;
13181 struct dwp_file *dwp_file;
13182
13183 /* Try to find first .dwp for the binary file before any symbolic links
13184 resolving. */
13185
13186 /* If the objfile is a debug file, find the name of the real binary
13187 file and get the name of dwp file from there. */
13188 std::string dwp_name;
13189 if (objfile->separate_debug_objfile_backlink != NULL)
13190 {
13191 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13192 const char *backlink_basename = lbasename (backlink->original_name);
13193
13194 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13195 }
13196 else
13197 dwp_name = objfile->original_name;
13198
13199 dwp_name += ".dwp";
13200
13201 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13202 if (dbfd == NULL
13203 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13204 {
13205 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13206 dwp_name = objfile_name (objfile);
13207 dwp_name += ".dwp";
13208 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13209 }
13210
13211 if (dbfd == NULL)
13212 {
13213 if (dwarf_read_debug)
13214 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13215 return NULL;
13216 }
13217 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
13218 dwp_file->name = bfd_get_filename (dbfd.get ());
13219 dwp_file->dbfd = dbfd.release ();
13220
13221 /* +1: section 0 is unused */
13222 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
13223 dwp_file->elf_sections =
13224 OBSTACK_CALLOC (&objfile->objfile_obstack,
13225 dwp_file->num_sections, asection *);
13226
13227 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13228 dwp_file);
13229
13230 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 0);
13231
13232 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 1);
13233
13234 /* The DWP file version is stored in the hash table. Oh well. */
13235 if (dwp_file->cus && dwp_file->tus
13236 && dwp_file->cus->version != dwp_file->tus->version)
13237 {
13238 /* Technically speaking, we should try to limp along, but this is
13239 pretty bizarre. We use pulongest here because that's the established
13240 portability solution (e.g, we cannot use %u for uint32_t). */
13241 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13242 " TU version %s [in DWP file %s]"),
13243 pulongest (dwp_file->cus->version),
13244 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13245 }
13246
13247 if (dwp_file->cus)
13248 dwp_file->version = dwp_file->cus->version;
13249 else if (dwp_file->tus)
13250 dwp_file->version = dwp_file->tus->version;
13251 else
13252 dwp_file->version = 2;
13253
13254 if (dwp_file->version == 2)
13255 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13256 dwp_file);
13257
13258 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13259 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13260
13261 if (dwarf_read_debug)
13262 {
13263 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13264 fprintf_unfiltered (gdb_stdlog,
13265 " %s CUs, %s TUs\n",
13266 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13267 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13268 }
13269
13270 return dwp_file;
13271 }
13272
13273 /* Wrapper around open_and_init_dwp_file, only open it once. */
13274
13275 static struct dwp_file *
13276 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13277 {
13278 if (! dwarf2_per_objfile->dwp_checked)
13279 {
13280 dwarf2_per_objfile->dwp_file
13281 = open_and_init_dwp_file (dwarf2_per_objfile);
13282 dwarf2_per_objfile->dwp_checked = 1;
13283 }
13284 return dwarf2_per_objfile->dwp_file;
13285 }
13286
13287 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13288 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13289 or in the DWP file for the objfile, referenced by THIS_UNIT.
13290 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13291 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13292
13293 This is called, for example, when wanting to read a variable with a
13294 complex location. Therefore we don't want to do file i/o for every call.
13295 Therefore we don't want to look for a DWO file on every call.
13296 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13297 then we check if we've already seen DWO_NAME, and only THEN do we check
13298 for a DWO file.
13299
13300 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13301 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13302
13303 static struct dwo_unit *
13304 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13305 const char *dwo_name, const char *comp_dir,
13306 ULONGEST signature, int is_debug_types)
13307 {
13308 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13309 struct objfile *objfile = dwarf2_per_objfile->objfile;
13310 const char *kind = is_debug_types ? "TU" : "CU";
13311 void **dwo_file_slot;
13312 struct dwo_file *dwo_file;
13313 struct dwp_file *dwp_file;
13314
13315 /* First see if there's a DWP file.
13316 If we have a DWP file but didn't find the DWO inside it, don't
13317 look for the original DWO file. It makes gdb behave differently
13318 depending on whether one is debugging in the build tree. */
13319
13320 dwp_file = get_dwp_file (dwarf2_per_objfile);
13321 if (dwp_file != NULL)
13322 {
13323 const struct dwp_hash_table *dwp_htab =
13324 is_debug_types ? dwp_file->tus : dwp_file->cus;
13325
13326 if (dwp_htab != NULL)
13327 {
13328 struct dwo_unit *dwo_cutu =
13329 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13330 signature, is_debug_types);
13331
13332 if (dwo_cutu != NULL)
13333 {
13334 if (dwarf_read_debug)
13335 {
13336 fprintf_unfiltered (gdb_stdlog,
13337 "Virtual DWO %s %s found: @%s\n",
13338 kind, hex_string (signature),
13339 host_address_to_string (dwo_cutu));
13340 }
13341 return dwo_cutu;
13342 }
13343 }
13344 }
13345 else
13346 {
13347 /* No DWP file, look for the DWO file. */
13348
13349 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13350 dwo_name, comp_dir);
13351 if (*dwo_file_slot == NULL)
13352 {
13353 /* Read in the file and build a table of the CUs/TUs it contains. */
13354 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13355 }
13356 /* NOTE: This will be NULL if unable to open the file. */
13357 dwo_file = (struct dwo_file *) *dwo_file_slot;
13358
13359 if (dwo_file != NULL)
13360 {
13361 struct dwo_unit *dwo_cutu = NULL;
13362
13363 if (is_debug_types && dwo_file->tus)
13364 {
13365 struct dwo_unit find_dwo_cutu;
13366
13367 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13368 find_dwo_cutu.signature = signature;
13369 dwo_cutu
13370 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13371 }
13372 else if (!is_debug_types && dwo_file->cus)
13373 {
13374 struct dwo_unit find_dwo_cutu;
13375
13376 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13377 find_dwo_cutu.signature = signature;
13378 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13379 &find_dwo_cutu);
13380 }
13381
13382 if (dwo_cutu != NULL)
13383 {
13384 if (dwarf_read_debug)
13385 {
13386 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13387 kind, dwo_name, hex_string (signature),
13388 host_address_to_string (dwo_cutu));
13389 }
13390 return dwo_cutu;
13391 }
13392 }
13393 }
13394
13395 /* We didn't find it. This could mean a dwo_id mismatch, or
13396 someone deleted the DWO/DWP file, or the search path isn't set up
13397 correctly to find the file. */
13398
13399 if (dwarf_read_debug)
13400 {
13401 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13402 kind, dwo_name, hex_string (signature));
13403 }
13404
13405 /* This is a warning and not a complaint because it can be caused by
13406 pilot error (e.g., user accidentally deleting the DWO). */
13407 {
13408 /* Print the name of the DWP file if we looked there, helps the user
13409 better diagnose the problem. */
13410 std::string dwp_text;
13411
13412 if (dwp_file != NULL)
13413 dwp_text = string_printf (" [in DWP file %s]",
13414 lbasename (dwp_file->name));
13415
13416 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13417 " [in module %s]"),
13418 kind, dwo_name, hex_string (signature),
13419 dwp_text.c_str (),
13420 this_unit->is_debug_types ? "TU" : "CU",
13421 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13422 }
13423 return NULL;
13424 }
13425
13426 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13427 See lookup_dwo_cutu_unit for details. */
13428
13429 static struct dwo_unit *
13430 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13431 const char *dwo_name, const char *comp_dir,
13432 ULONGEST signature)
13433 {
13434 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13435 }
13436
13437 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13438 See lookup_dwo_cutu_unit for details. */
13439
13440 static struct dwo_unit *
13441 lookup_dwo_type_unit (struct signatured_type *this_tu,
13442 const char *dwo_name, const char *comp_dir)
13443 {
13444 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13445 }
13446
13447 /* Traversal function for queue_and_load_all_dwo_tus. */
13448
13449 static int
13450 queue_and_load_dwo_tu (void **slot, void *info)
13451 {
13452 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13453 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13454 ULONGEST signature = dwo_unit->signature;
13455 struct signatured_type *sig_type =
13456 lookup_dwo_signatured_type (per_cu->cu, signature);
13457
13458 if (sig_type != NULL)
13459 {
13460 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13461
13462 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13463 a real dependency of PER_CU on SIG_TYPE. That is detected later
13464 while processing PER_CU. */
13465 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13466 load_full_type_unit (sig_cu);
13467 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13468 }
13469
13470 return 1;
13471 }
13472
13473 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13474 The DWO may have the only definition of the type, though it may not be
13475 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13476 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13477
13478 static void
13479 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13480 {
13481 struct dwo_unit *dwo_unit;
13482 struct dwo_file *dwo_file;
13483
13484 gdb_assert (!per_cu->is_debug_types);
13485 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13486 gdb_assert (per_cu->cu != NULL);
13487
13488 dwo_unit = per_cu->cu->dwo_unit;
13489 gdb_assert (dwo_unit != NULL);
13490
13491 dwo_file = dwo_unit->dwo_file;
13492 if (dwo_file->tus != NULL)
13493 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13494 }
13495
13496 /* Free all resources associated with DWO_FILE.
13497 Close the DWO file and munmap the sections. */
13498
13499 static void
13500 free_dwo_file (struct dwo_file *dwo_file)
13501 {
13502 /* Note: dbfd is NULL for virtual DWO files. */
13503 gdb_bfd_unref (dwo_file->dbfd);
13504
13505 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13506 }
13507
13508 /* Traversal function for free_dwo_files. */
13509
13510 static int
13511 free_dwo_file_from_slot (void **slot, void *info)
13512 {
13513 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13514
13515 free_dwo_file (dwo_file);
13516
13517 return 1;
13518 }
13519
13520 /* Free all resources associated with DWO_FILES. */
13521
13522 static void
13523 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13524 {
13525 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
13526 }
13527 \f
13528 /* Read in various DIEs. */
13529
13530 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13531 Inherit only the children of the DW_AT_abstract_origin DIE not being
13532 already referenced by DW_AT_abstract_origin from the children of the
13533 current DIE. */
13534
13535 static void
13536 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13537 {
13538 struct die_info *child_die;
13539 sect_offset *offsetp;
13540 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13541 struct die_info *origin_die;
13542 /* Iterator of the ORIGIN_DIE children. */
13543 struct die_info *origin_child_die;
13544 struct attribute *attr;
13545 struct dwarf2_cu *origin_cu;
13546 struct pending **origin_previous_list_in_scope;
13547
13548 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13549 if (!attr)
13550 return;
13551
13552 /* Note that following die references may follow to a die in a
13553 different cu. */
13554
13555 origin_cu = cu;
13556 origin_die = follow_die_ref (die, attr, &origin_cu);
13557
13558 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13559 symbols in. */
13560 origin_previous_list_in_scope = origin_cu->list_in_scope;
13561 origin_cu->list_in_scope = cu->list_in_scope;
13562
13563 if (die->tag != origin_die->tag
13564 && !(die->tag == DW_TAG_inlined_subroutine
13565 && origin_die->tag == DW_TAG_subprogram))
13566 complaint (&symfile_complaints,
13567 _("DIE %s and its abstract origin %s have different tags"),
13568 sect_offset_str (die->sect_off),
13569 sect_offset_str (origin_die->sect_off));
13570
13571 std::vector<sect_offset> offsets;
13572
13573 for (child_die = die->child;
13574 child_die && child_die->tag;
13575 child_die = sibling_die (child_die))
13576 {
13577 struct die_info *child_origin_die;
13578 struct dwarf2_cu *child_origin_cu;
13579
13580 /* We are trying to process concrete instance entries:
13581 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13582 it's not relevant to our analysis here. i.e. detecting DIEs that are
13583 present in the abstract instance but not referenced in the concrete
13584 one. */
13585 if (child_die->tag == DW_TAG_call_site
13586 || child_die->tag == DW_TAG_GNU_call_site)
13587 continue;
13588
13589 /* For each CHILD_DIE, find the corresponding child of
13590 ORIGIN_DIE. If there is more than one layer of
13591 DW_AT_abstract_origin, follow them all; there shouldn't be,
13592 but GCC versions at least through 4.4 generate this (GCC PR
13593 40573). */
13594 child_origin_die = child_die;
13595 child_origin_cu = cu;
13596 while (1)
13597 {
13598 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13599 child_origin_cu);
13600 if (attr == NULL)
13601 break;
13602 child_origin_die = follow_die_ref (child_origin_die, attr,
13603 &child_origin_cu);
13604 }
13605
13606 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13607 counterpart may exist. */
13608 if (child_origin_die != child_die)
13609 {
13610 if (child_die->tag != child_origin_die->tag
13611 && !(child_die->tag == DW_TAG_inlined_subroutine
13612 && child_origin_die->tag == DW_TAG_subprogram))
13613 complaint (&symfile_complaints,
13614 _("Child DIE %s and its abstract origin %s have "
13615 "different tags"),
13616 sect_offset_str (child_die->sect_off),
13617 sect_offset_str (child_origin_die->sect_off));
13618 if (child_origin_die->parent != origin_die)
13619 complaint (&symfile_complaints,
13620 _("Child DIE %s and its abstract origin %s have "
13621 "different parents"),
13622 sect_offset_str (child_die->sect_off),
13623 sect_offset_str (child_origin_die->sect_off));
13624 else
13625 offsets.push_back (child_origin_die->sect_off);
13626 }
13627 }
13628 std::sort (offsets.begin (), offsets.end ());
13629 sect_offset *offsets_end = offsets.data () + offsets.size ();
13630 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13631 if (offsetp[-1] == *offsetp)
13632 complaint (&symfile_complaints,
13633 _("Multiple children of DIE %s refer "
13634 "to DIE %s as their abstract origin"),
13635 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13636
13637 offsetp = offsets.data ();
13638 origin_child_die = origin_die->child;
13639 while (origin_child_die && origin_child_die->tag)
13640 {
13641 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13642 while (offsetp < offsets_end
13643 && *offsetp < origin_child_die->sect_off)
13644 offsetp++;
13645 if (offsetp >= offsets_end
13646 || *offsetp > origin_child_die->sect_off)
13647 {
13648 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13649 Check whether we're already processing ORIGIN_CHILD_DIE.
13650 This can happen with mutually referenced abstract_origins.
13651 PR 16581. */
13652 if (!origin_child_die->in_process)
13653 process_die (origin_child_die, origin_cu);
13654 }
13655 origin_child_die = sibling_die (origin_child_die);
13656 }
13657 origin_cu->list_in_scope = origin_previous_list_in_scope;
13658 }
13659
13660 static void
13661 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13662 {
13663 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13664 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13665 struct context_stack *newobj;
13666 CORE_ADDR lowpc;
13667 CORE_ADDR highpc;
13668 struct die_info *child_die;
13669 struct attribute *attr, *call_line, *call_file;
13670 const char *name;
13671 CORE_ADDR baseaddr;
13672 struct block *block;
13673 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13674 std::vector<struct symbol *> template_args;
13675 struct template_symbol *templ_func = NULL;
13676
13677 if (inlined_func)
13678 {
13679 /* If we do not have call site information, we can't show the
13680 caller of this inlined function. That's too confusing, so
13681 only use the scope for local variables. */
13682 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13683 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13684 if (call_line == NULL || call_file == NULL)
13685 {
13686 read_lexical_block_scope (die, cu);
13687 return;
13688 }
13689 }
13690
13691 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13692
13693 name = dwarf2_name (die, cu);
13694
13695 /* Ignore functions with missing or empty names. These are actually
13696 illegal according to the DWARF standard. */
13697 if (name == NULL)
13698 {
13699 complaint (&symfile_complaints,
13700 _("missing name for subprogram DIE at %s"),
13701 sect_offset_str (die->sect_off));
13702 return;
13703 }
13704
13705 /* Ignore functions with missing or invalid low and high pc attributes. */
13706 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13707 <= PC_BOUNDS_INVALID)
13708 {
13709 attr = dwarf2_attr (die, DW_AT_external, cu);
13710 if (!attr || !DW_UNSND (attr))
13711 complaint (&symfile_complaints,
13712 _("cannot get low and high bounds "
13713 "for subprogram DIE at %s"),
13714 sect_offset_str (die->sect_off));
13715 return;
13716 }
13717
13718 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13719 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13720
13721 /* If we have any template arguments, then we must allocate a
13722 different sort of symbol. */
13723 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13724 {
13725 if (child_die->tag == DW_TAG_template_type_param
13726 || child_die->tag == DW_TAG_template_value_param)
13727 {
13728 templ_func = allocate_template_symbol (objfile);
13729 templ_func->subclass = SYMBOL_TEMPLATE;
13730 break;
13731 }
13732 }
13733
13734 newobj = push_context (0, lowpc);
13735 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13736 (struct symbol *) templ_func);
13737
13738 /* If there is a location expression for DW_AT_frame_base, record
13739 it. */
13740 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13741 if (attr)
13742 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13743
13744 /* If there is a location for the static link, record it. */
13745 newobj->static_link = NULL;
13746 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13747 if (attr)
13748 {
13749 newobj->static_link
13750 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13751 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13752 }
13753
13754 cu->list_in_scope = &local_symbols;
13755
13756 if (die->child != NULL)
13757 {
13758 child_die = die->child;
13759 while (child_die && child_die->tag)
13760 {
13761 if (child_die->tag == DW_TAG_template_type_param
13762 || child_die->tag == DW_TAG_template_value_param)
13763 {
13764 struct symbol *arg = new_symbol (child_die, NULL, cu);
13765
13766 if (arg != NULL)
13767 template_args.push_back (arg);
13768 }
13769 else
13770 process_die (child_die, cu);
13771 child_die = sibling_die (child_die);
13772 }
13773 }
13774
13775 inherit_abstract_dies (die, cu);
13776
13777 /* If we have a DW_AT_specification, we might need to import using
13778 directives from the context of the specification DIE. See the
13779 comment in determine_prefix. */
13780 if (cu->language == language_cplus
13781 && dwarf2_attr (die, DW_AT_specification, cu))
13782 {
13783 struct dwarf2_cu *spec_cu = cu;
13784 struct die_info *spec_die = die_specification (die, &spec_cu);
13785
13786 while (spec_die)
13787 {
13788 child_die = spec_die->child;
13789 while (child_die && child_die->tag)
13790 {
13791 if (child_die->tag == DW_TAG_imported_module)
13792 process_die (child_die, spec_cu);
13793 child_die = sibling_die (child_die);
13794 }
13795
13796 /* In some cases, GCC generates specification DIEs that
13797 themselves contain DW_AT_specification attributes. */
13798 spec_die = die_specification (spec_die, &spec_cu);
13799 }
13800 }
13801
13802 newobj = pop_context ();
13803 /* Make a block for the local symbols within. */
13804 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
13805 newobj->static_link, lowpc, highpc);
13806
13807 /* For C++, set the block's scope. */
13808 if ((cu->language == language_cplus
13809 || cu->language == language_fortran
13810 || cu->language == language_d
13811 || cu->language == language_rust)
13812 && cu->processing_has_namespace_info)
13813 block_set_scope (block, determine_prefix (die, cu),
13814 &objfile->objfile_obstack);
13815
13816 /* If we have address ranges, record them. */
13817 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13818
13819 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
13820
13821 /* Attach template arguments to function. */
13822 if (!template_args.empty ())
13823 {
13824 gdb_assert (templ_func != NULL);
13825
13826 templ_func->n_template_arguments = template_args.size ();
13827 templ_func->template_arguments
13828 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13829 templ_func->n_template_arguments);
13830 memcpy (templ_func->template_arguments,
13831 template_args.data (),
13832 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13833 }
13834
13835 /* In C++, we can have functions nested inside functions (e.g., when
13836 a function declares a class that has methods). This means that
13837 when we finish processing a function scope, we may need to go
13838 back to building a containing block's symbol lists. */
13839 local_symbols = newobj->locals;
13840 local_using_directives = newobj->local_using_directives;
13841
13842 /* If we've finished processing a top-level function, subsequent
13843 symbols go in the file symbol list. */
13844 if (outermost_context_p ())
13845 cu->list_in_scope = &file_symbols;
13846 }
13847
13848 /* Process all the DIES contained within a lexical block scope. Start
13849 a new scope, process the dies, and then close the scope. */
13850
13851 static void
13852 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13853 {
13854 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13855 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13856 struct context_stack *newobj;
13857 CORE_ADDR lowpc, highpc;
13858 struct die_info *child_die;
13859 CORE_ADDR baseaddr;
13860
13861 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13862
13863 /* Ignore blocks with missing or invalid low and high pc attributes. */
13864 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13865 as multiple lexical blocks? Handling children in a sane way would
13866 be nasty. Might be easier to properly extend generic blocks to
13867 describe ranges. */
13868 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13869 {
13870 case PC_BOUNDS_NOT_PRESENT:
13871 /* DW_TAG_lexical_block has no attributes, process its children as if
13872 there was no wrapping by that DW_TAG_lexical_block.
13873 GCC does no longer produces such DWARF since GCC r224161. */
13874 for (child_die = die->child;
13875 child_die != NULL && child_die->tag;
13876 child_die = sibling_die (child_die))
13877 process_die (child_die, cu);
13878 return;
13879 case PC_BOUNDS_INVALID:
13880 return;
13881 }
13882 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13883 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13884
13885 push_context (0, lowpc);
13886 if (die->child != NULL)
13887 {
13888 child_die = die->child;
13889 while (child_die && child_die->tag)
13890 {
13891 process_die (child_die, cu);
13892 child_die = sibling_die (child_die);
13893 }
13894 }
13895 inherit_abstract_dies (die, cu);
13896 newobj = pop_context ();
13897
13898 if (local_symbols != NULL || local_using_directives != NULL)
13899 {
13900 struct block *block
13901 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
13902 newobj->start_addr, highpc);
13903
13904 /* Note that recording ranges after traversing children, as we
13905 do here, means that recording a parent's ranges entails
13906 walking across all its children's ranges as they appear in
13907 the address map, which is quadratic behavior.
13908
13909 It would be nicer to record the parent's ranges before
13910 traversing its children, simply overriding whatever you find
13911 there. But since we don't even decide whether to create a
13912 block until after we've traversed its children, that's hard
13913 to do. */
13914 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13915 }
13916 local_symbols = newobj->locals;
13917 local_using_directives = newobj->local_using_directives;
13918 }
13919
13920 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13921
13922 static void
13923 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13924 {
13925 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13926 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13927 CORE_ADDR pc, baseaddr;
13928 struct attribute *attr;
13929 struct call_site *call_site, call_site_local;
13930 void **slot;
13931 int nparams;
13932 struct die_info *child_die;
13933
13934 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13935
13936 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13937 if (attr == NULL)
13938 {
13939 /* This was a pre-DWARF-5 GNU extension alias
13940 for DW_AT_call_return_pc. */
13941 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13942 }
13943 if (!attr)
13944 {
13945 complaint (&symfile_complaints,
13946 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
13947 "DIE %s [in module %s]"),
13948 sect_offset_str (die->sect_off), objfile_name (objfile));
13949 return;
13950 }
13951 pc = attr_value_as_address (attr) + baseaddr;
13952 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13953
13954 if (cu->call_site_htab == NULL)
13955 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13956 NULL, &objfile->objfile_obstack,
13957 hashtab_obstack_allocate, NULL);
13958 call_site_local.pc = pc;
13959 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13960 if (*slot != NULL)
13961 {
13962 complaint (&symfile_complaints,
13963 _("Duplicate PC %s for DW_TAG_call_site "
13964 "DIE %s [in module %s]"),
13965 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13966 objfile_name (objfile));
13967 return;
13968 }
13969
13970 /* Count parameters at the caller. */
13971
13972 nparams = 0;
13973 for (child_die = die->child; child_die && child_die->tag;
13974 child_die = sibling_die (child_die))
13975 {
13976 if (child_die->tag != DW_TAG_call_site_parameter
13977 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13978 {
13979 complaint (&symfile_complaints,
13980 _("Tag %d is not DW_TAG_call_site_parameter in "
13981 "DW_TAG_call_site child DIE %s [in module %s]"),
13982 child_die->tag, sect_offset_str (child_die->sect_off),
13983 objfile_name (objfile));
13984 continue;
13985 }
13986
13987 nparams++;
13988 }
13989
13990 call_site
13991 = ((struct call_site *)
13992 obstack_alloc (&objfile->objfile_obstack,
13993 sizeof (*call_site)
13994 + (sizeof (*call_site->parameter) * (nparams - 1))));
13995 *slot = call_site;
13996 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13997 call_site->pc = pc;
13998
13999 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14000 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14001 {
14002 struct die_info *func_die;
14003
14004 /* Skip also over DW_TAG_inlined_subroutine. */
14005 for (func_die = die->parent;
14006 func_die && func_die->tag != DW_TAG_subprogram
14007 && func_die->tag != DW_TAG_subroutine_type;
14008 func_die = func_die->parent);
14009
14010 /* DW_AT_call_all_calls is a superset
14011 of DW_AT_call_all_tail_calls. */
14012 if (func_die
14013 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14014 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14015 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14016 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14017 {
14018 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14019 not complete. But keep CALL_SITE for look ups via call_site_htab,
14020 both the initial caller containing the real return address PC and
14021 the final callee containing the current PC of a chain of tail
14022 calls do not need to have the tail call list complete. But any
14023 function candidate for a virtual tail call frame searched via
14024 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14025 determined unambiguously. */
14026 }
14027 else
14028 {
14029 struct type *func_type = NULL;
14030
14031 if (func_die)
14032 func_type = get_die_type (func_die, cu);
14033 if (func_type != NULL)
14034 {
14035 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14036
14037 /* Enlist this call site to the function. */
14038 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14039 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14040 }
14041 else
14042 complaint (&symfile_complaints,
14043 _("Cannot find function owning DW_TAG_call_site "
14044 "DIE %s [in module %s]"),
14045 sect_offset_str (die->sect_off), objfile_name (objfile));
14046 }
14047 }
14048
14049 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14050 if (attr == NULL)
14051 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14052 if (attr == NULL)
14053 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14054 if (attr == NULL)
14055 {
14056 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14057 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14058 }
14059 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14060 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14061 /* Keep NULL DWARF_BLOCK. */;
14062 else if (attr_form_is_block (attr))
14063 {
14064 struct dwarf2_locexpr_baton *dlbaton;
14065
14066 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14067 dlbaton->data = DW_BLOCK (attr)->data;
14068 dlbaton->size = DW_BLOCK (attr)->size;
14069 dlbaton->per_cu = cu->per_cu;
14070
14071 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14072 }
14073 else if (attr_form_is_ref (attr))
14074 {
14075 struct dwarf2_cu *target_cu = cu;
14076 struct die_info *target_die;
14077
14078 target_die = follow_die_ref (die, attr, &target_cu);
14079 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14080 if (die_is_declaration (target_die, target_cu))
14081 {
14082 const char *target_physname;
14083
14084 /* Prefer the mangled name; otherwise compute the demangled one. */
14085 target_physname = dw2_linkage_name (target_die, target_cu);
14086 if (target_physname == NULL)
14087 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14088 if (target_physname == NULL)
14089 complaint (&symfile_complaints,
14090 _("DW_AT_call_target target DIE has invalid "
14091 "physname, for referencing DIE %s [in module %s]"),
14092 sect_offset_str (die->sect_off), objfile_name (objfile));
14093 else
14094 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14095 }
14096 else
14097 {
14098 CORE_ADDR lowpc;
14099
14100 /* DW_AT_entry_pc should be preferred. */
14101 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14102 <= PC_BOUNDS_INVALID)
14103 complaint (&symfile_complaints,
14104 _("DW_AT_call_target target DIE has invalid "
14105 "low pc, for referencing DIE %s [in module %s]"),
14106 sect_offset_str (die->sect_off), objfile_name (objfile));
14107 else
14108 {
14109 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14110 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14111 }
14112 }
14113 }
14114 else
14115 complaint (&symfile_complaints,
14116 _("DW_TAG_call_site DW_AT_call_target is neither "
14117 "block nor reference, for DIE %s [in module %s]"),
14118 sect_offset_str (die->sect_off), objfile_name (objfile));
14119
14120 call_site->per_cu = cu->per_cu;
14121
14122 for (child_die = die->child;
14123 child_die && child_die->tag;
14124 child_die = sibling_die (child_die))
14125 {
14126 struct call_site_parameter *parameter;
14127 struct attribute *loc, *origin;
14128
14129 if (child_die->tag != DW_TAG_call_site_parameter
14130 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14131 {
14132 /* Already printed the complaint above. */
14133 continue;
14134 }
14135
14136 gdb_assert (call_site->parameter_count < nparams);
14137 parameter = &call_site->parameter[call_site->parameter_count];
14138
14139 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14140 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14141 register is contained in DW_AT_call_value. */
14142
14143 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14144 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14145 if (origin == NULL)
14146 {
14147 /* This was a pre-DWARF-5 GNU extension alias
14148 for DW_AT_call_parameter. */
14149 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14150 }
14151 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14152 {
14153 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14154
14155 sect_offset sect_off
14156 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14157 if (!offset_in_cu_p (&cu->header, sect_off))
14158 {
14159 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14160 binding can be done only inside one CU. Such referenced DIE
14161 therefore cannot be even moved to DW_TAG_partial_unit. */
14162 complaint (&symfile_complaints,
14163 _("DW_AT_call_parameter offset is not in CU for "
14164 "DW_TAG_call_site child DIE %s [in module %s]"),
14165 sect_offset_str (child_die->sect_off),
14166 objfile_name (objfile));
14167 continue;
14168 }
14169 parameter->u.param_cu_off
14170 = (cu_offset) (sect_off - cu->header.sect_off);
14171 }
14172 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14173 {
14174 complaint (&symfile_complaints,
14175 _("No DW_FORM_block* DW_AT_location for "
14176 "DW_TAG_call_site child DIE %s [in module %s]"),
14177 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14178 continue;
14179 }
14180 else
14181 {
14182 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14183 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14184 if (parameter->u.dwarf_reg != -1)
14185 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14186 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14187 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14188 &parameter->u.fb_offset))
14189 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14190 else
14191 {
14192 complaint (&symfile_complaints,
14193 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14194 "for DW_FORM_block* DW_AT_location is supported for "
14195 "DW_TAG_call_site child DIE %s "
14196 "[in module %s]"),
14197 sect_offset_str (child_die->sect_off),
14198 objfile_name (objfile));
14199 continue;
14200 }
14201 }
14202
14203 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14204 if (attr == NULL)
14205 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14206 if (!attr_form_is_block (attr))
14207 {
14208 complaint (&symfile_complaints,
14209 _("No DW_FORM_block* DW_AT_call_value for "
14210 "DW_TAG_call_site child DIE %s [in module %s]"),
14211 sect_offset_str (child_die->sect_off),
14212 objfile_name (objfile));
14213 continue;
14214 }
14215 parameter->value = DW_BLOCK (attr)->data;
14216 parameter->value_size = DW_BLOCK (attr)->size;
14217
14218 /* Parameters are not pre-cleared by memset above. */
14219 parameter->data_value = NULL;
14220 parameter->data_value_size = 0;
14221 call_site->parameter_count++;
14222
14223 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14224 if (attr == NULL)
14225 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14226 if (attr)
14227 {
14228 if (!attr_form_is_block (attr))
14229 complaint (&symfile_complaints,
14230 _("No DW_FORM_block* DW_AT_call_data_value for "
14231 "DW_TAG_call_site child DIE %s [in module %s]"),
14232 sect_offset_str (child_die->sect_off),
14233 objfile_name (objfile));
14234 else
14235 {
14236 parameter->data_value = DW_BLOCK (attr)->data;
14237 parameter->data_value_size = DW_BLOCK (attr)->size;
14238 }
14239 }
14240 }
14241 }
14242
14243 /* Helper function for read_variable. If DIE represents a virtual
14244 table, then return the type of the concrete object that is
14245 associated with the virtual table. Otherwise, return NULL. */
14246
14247 static struct type *
14248 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14249 {
14250 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14251 if (attr == NULL)
14252 return NULL;
14253
14254 /* Find the type DIE. */
14255 struct die_info *type_die = NULL;
14256 struct dwarf2_cu *type_cu = cu;
14257
14258 if (attr_form_is_ref (attr))
14259 type_die = follow_die_ref (die, attr, &type_cu);
14260 if (type_die == NULL)
14261 return NULL;
14262
14263 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14264 return NULL;
14265 return die_containing_type (type_die, type_cu);
14266 }
14267
14268 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14269
14270 static void
14271 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14272 {
14273 struct rust_vtable_symbol *storage = NULL;
14274
14275 if (cu->language == language_rust)
14276 {
14277 struct type *containing_type = rust_containing_type (die, cu);
14278
14279 if (containing_type != NULL)
14280 {
14281 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14282
14283 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14284 struct rust_vtable_symbol);
14285 initialize_objfile_symbol (storage);
14286 storage->concrete_type = containing_type;
14287 storage->subclass = SYMBOL_RUST_VTABLE;
14288 }
14289 }
14290
14291 new_symbol (die, NULL, cu, storage);
14292 }
14293
14294 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14295 reading .debug_rnglists.
14296 Callback's type should be:
14297 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14298 Return true if the attributes are present and valid, otherwise,
14299 return false. */
14300
14301 template <typename Callback>
14302 static bool
14303 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14304 Callback &&callback)
14305 {
14306 struct dwarf2_per_objfile *dwarf2_per_objfile
14307 = cu->per_cu->dwarf2_per_objfile;
14308 struct objfile *objfile = dwarf2_per_objfile->objfile;
14309 bfd *obfd = objfile->obfd;
14310 /* Base address selection entry. */
14311 CORE_ADDR base;
14312 int found_base;
14313 const gdb_byte *buffer;
14314 CORE_ADDR baseaddr;
14315 bool overflow = false;
14316
14317 found_base = cu->base_known;
14318 base = cu->base_address;
14319
14320 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14321 if (offset >= dwarf2_per_objfile->rnglists.size)
14322 {
14323 complaint (&symfile_complaints,
14324 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14325 offset);
14326 return false;
14327 }
14328 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14329
14330 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14331
14332 while (1)
14333 {
14334 /* Initialize it due to a false compiler warning. */
14335 CORE_ADDR range_beginning = 0, range_end = 0;
14336 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14337 + dwarf2_per_objfile->rnglists.size);
14338 unsigned int bytes_read;
14339
14340 if (buffer == buf_end)
14341 {
14342 overflow = true;
14343 break;
14344 }
14345 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14346 switch (rlet)
14347 {
14348 case DW_RLE_end_of_list:
14349 break;
14350 case DW_RLE_base_address:
14351 if (buffer + cu->header.addr_size > buf_end)
14352 {
14353 overflow = true;
14354 break;
14355 }
14356 base = read_address (obfd, buffer, cu, &bytes_read);
14357 found_base = 1;
14358 buffer += bytes_read;
14359 break;
14360 case DW_RLE_start_length:
14361 if (buffer + cu->header.addr_size > buf_end)
14362 {
14363 overflow = true;
14364 break;
14365 }
14366 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14367 buffer += bytes_read;
14368 range_end = (range_beginning
14369 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14370 buffer += bytes_read;
14371 if (buffer > buf_end)
14372 {
14373 overflow = true;
14374 break;
14375 }
14376 break;
14377 case DW_RLE_offset_pair:
14378 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14379 buffer += bytes_read;
14380 if (buffer > buf_end)
14381 {
14382 overflow = true;
14383 break;
14384 }
14385 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14386 buffer += bytes_read;
14387 if (buffer > buf_end)
14388 {
14389 overflow = true;
14390 break;
14391 }
14392 break;
14393 case DW_RLE_start_end:
14394 if (buffer + 2 * cu->header.addr_size > buf_end)
14395 {
14396 overflow = true;
14397 break;
14398 }
14399 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14400 buffer += bytes_read;
14401 range_end = read_address (obfd, buffer, cu, &bytes_read);
14402 buffer += bytes_read;
14403 break;
14404 default:
14405 complaint (&symfile_complaints,
14406 _("Invalid .debug_rnglists data (no base address)"));
14407 return false;
14408 }
14409 if (rlet == DW_RLE_end_of_list || overflow)
14410 break;
14411 if (rlet == DW_RLE_base_address)
14412 continue;
14413
14414 if (!found_base)
14415 {
14416 /* We have no valid base address for the ranges
14417 data. */
14418 complaint (&symfile_complaints,
14419 _("Invalid .debug_rnglists data (no base address)"));
14420 return false;
14421 }
14422
14423 if (range_beginning > range_end)
14424 {
14425 /* Inverted range entries are invalid. */
14426 complaint (&symfile_complaints,
14427 _("Invalid .debug_rnglists data (inverted range)"));
14428 return false;
14429 }
14430
14431 /* Empty range entries have no effect. */
14432 if (range_beginning == range_end)
14433 continue;
14434
14435 range_beginning += base;
14436 range_end += base;
14437
14438 /* A not-uncommon case of bad debug info.
14439 Don't pollute the addrmap with bad data. */
14440 if (range_beginning + baseaddr == 0
14441 && !dwarf2_per_objfile->has_section_at_zero)
14442 {
14443 complaint (&symfile_complaints,
14444 _(".debug_rnglists entry has start address of zero"
14445 " [in module %s]"), objfile_name (objfile));
14446 continue;
14447 }
14448
14449 callback (range_beginning, range_end);
14450 }
14451
14452 if (overflow)
14453 {
14454 complaint (&symfile_complaints,
14455 _("Offset %d is not terminated "
14456 "for DW_AT_ranges attribute"),
14457 offset);
14458 return false;
14459 }
14460
14461 return true;
14462 }
14463
14464 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14465 Callback's type should be:
14466 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14467 Return 1 if the attributes are present and valid, otherwise, return 0. */
14468
14469 template <typename Callback>
14470 static int
14471 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14472 Callback &&callback)
14473 {
14474 struct dwarf2_per_objfile *dwarf2_per_objfile
14475 = cu->per_cu->dwarf2_per_objfile;
14476 struct objfile *objfile = dwarf2_per_objfile->objfile;
14477 struct comp_unit_head *cu_header = &cu->header;
14478 bfd *obfd = objfile->obfd;
14479 unsigned int addr_size = cu_header->addr_size;
14480 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14481 /* Base address selection entry. */
14482 CORE_ADDR base;
14483 int found_base;
14484 unsigned int dummy;
14485 const gdb_byte *buffer;
14486 CORE_ADDR baseaddr;
14487
14488 if (cu_header->version >= 5)
14489 return dwarf2_rnglists_process (offset, cu, callback);
14490
14491 found_base = cu->base_known;
14492 base = cu->base_address;
14493
14494 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14495 if (offset >= dwarf2_per_objfile->ranges.size)
14496 {
14497 complaint (&symfile_complaints,
14498 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14499 offset);
14500 return 0;
14501 }
14502 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14503
14504 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14505
14506 while (1)
14507 {
14508 CORE_ADDR range_beginning, range_end;
14509
14510 range_beginning = read_address (obfd, buffer, cu, &dummy);
14511 buffer += addr_size;
14512 range_end = read_address (obfd, buffer, cu, &dummy);
14513 buffer += addr_size;
14514 offset += 2 * addr_size;
14515
14516 /* An end of list marker is a pair of zero addresses. */
14517 if (range_beginning == 0 && range_end == 0)
14518 /* Found the end of list entry. */
14519 break;
14520
14521 /* Each base address selection entry is a pair of 2 values.
14522 The first is the largest possible address, the second is
14523 the base address. Check for a base address here. */
14524 if ((range_beginning & mask) == mask)
14525 {
14526 /* If we found the largest possible address, then we already
14527 have the base address in range_end. */
14528 base = range_end;
14529 found_base = 1;
14530 continue;
14531 }
14532
14533 if (!found_base)
14534 {
14535 /* We have no valid base address for the ranges
14536 data. */
14537 complaint (&symfile_complaints,
14538 _("Invalid .debug_ranges data (no base address)"));
14539 return 0;
14540 }
14541
14542 if (range_beginning > range_end)
14543 {
14544 /* Inverted range entries are invalid. */
14545 complaint (&symfile_complaints,
14546 _("Invalid .debug_ranges data (inverted range)"));
14547 return 0;
14548 }
14549
14550 /* Empty range entries have no effect. */
14551 if (range_beginning == range_end)
14552 continue;
14553
14554 range_beginning += base;
14555 range_end += base;
14556
14557 /* A not-uncommon case of bad debug info.
14558 Don't pollute the addrmap with bad data. */
14559 if (range_beginning + baseaddr == 0
14560 && !dwarf2_per_objfile->has_section_at_zero)
14561 {
14562 complaint (&symfile_complaints,
14563 _(".debug_ranges entry has start address of zero"
14564 " [in module %s]"), objfile_name (objfile));
14565 continue;
14566 }
14567
14568 callback (range_beginning, range_end);
14569 }
14570
14571 return 1;
14572 }
14573
14574 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14575 Return 1 if the attributes are present and valid, otherwise, return 0.
14576 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14577
14578 static int
14579 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14580 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14581 struct partial_symtab *ranges_pst)
14582 {
14583 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14584 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14585 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14586 SECT_OFF_TEXT (objfile));
14587 int low_set = 0;
14588 CORE_ADDR low = 0;
14589 CORE_ADDR high = 0;
14590 int retval;
14591
14592 retval = dwarf2_ranges_process (offset, cu,
14593 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14594 {
14595 if (ranges_pst != NULL)
14596 {
14597 CORE_ADDR lowpc;
14598 CORE_ADDR highpc;
14599
14600 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14601 range_beginning + baseaddr);
14602 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14603 range_end + baseaddr);
14604 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14605 ranges_pst);
14606 }
14607
14608 /* FIXME: This is recording everything as a low-high
14609 segment of consecutive addresses. We should have a
14610 data structure for discontiguous block ranges
14611 instead. */
14612 if (! low_set)
14613 {
14614 low = range_beginning;
14615 high = range_end;
14616 low_set = 1;
14617 }
14618 else
14619 {
14620 if (range_beginning < low)
14621 low = range_beginning;
14622 if (range_end > high)
14623 high = range_end;
14624 }
14625 });
14626 if (!retval)
14627 return 0;
14628
14629 if (! low_set)
14630 /* If the first entry is an end-of-list marker, the range
14631 describes an empty scope, i.e. no instructions. */
14632 return 0;
14633
14634 if (low_return)
14635 *low_return = low;
14636 if (high_return)
14637 *high_return = high;
14638 return 1;
14639 }
14640
14641 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14642 definition for the return value. *LOWPC and *HIGHPC are set iff
14643 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14644
14645 static enum pc_bounds_kind
14646 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14647 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14648 struct partial_symtab *pst)
14649 {
14650 struct dwarf2_per_objfile *dwarf2_per_objfile
14651 = cu->per_cu->dwarf2_per_objfile;
14652 struct attribute *attr;
14653 struct attribute *attr_high;
14654 CORE_ADDR low = 0;
14655 CORE_ADDR high = 0;
14656 enum pc_bounds_kind ret;
14657
14658 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14659 if (attr_high)
14660 {
14661 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14662 if (attr)
14663 {
14664 low = attr_value_as_address (attr);
14665 high = attr_value_as_address (attr_high);
14666 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14667 high += low;
14668 }
14669 else
14670 /* Found high w/o low attribute. */
14671 return PC_BOUNDS_INVALID;
14672
14673 /* Found consecutive range of addresses. */
14674 ret = PC_BOUNDS_HIGH_LOW;
14675 }
14676 else
14677 {
14678 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14679 if (attr != NULL)
14680 {
14681 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14682 We take advantage of the fact that DW_AT_ranges does not appear
14683 in DW_TAG_compile_unit of DWO files. */
14684 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14685 unsigned int ranges_offset = (DW_UNSND (attr)
14686 + (need_ranges_base
14687 ? cu->ranges_base
14688 : 0));
14689
14690 /* Value of the DW_AT_ranges attribute is the offset in the
14691 .debug_ranges section. */
14692 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14693 return PC_BOUNDS_INVALID;
14694 /* Found discontinuous range of addresses. */
14695 ret = PC_BOUNDS_RANGES;
14696 }
14697 else
14698 return PC_BOUNDS_NOT_PRESENT;
14699 }
14700
14701 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14702 if (high <= low)
14703 return PC_BOUNDS_INVALID;
14704
14705 /* When using the GNU linker, .gnu.linkonce. sections are used to
14706 eliminate duplicate copies of functions and vtables and such.
14707 The linker will arbitrarily choose one and discard the others.
14708 The AT_*_pc values for such functions refer to local labels in
14709 these sections. If the section from that file was discarded, the
14710 labels are not in the output, so the relocs get a value of 0.
14711 If this is a discarded function, mark the pc bounds as invalid,
14712 so that GDB will ignore it. */
14713 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14714 return PC_BOUNDS_INVALID;
14715
14716 *lowpc = low;
14717 if (highpc)
14718 *highpc = high;
14719 return ret;
14720 }
14721
14722 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14723 its low and high PC addresses. Do nothing if these addresses could not
14724 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14725 and HIGHPC to the high address if greater than HIGHPC. */
14726
14727 static void
14728 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14729 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14730 struct dwarf2_cu *cu)
14731 {
14732 CORE_ADDR low, high;
14733 struct die_info *child = die->child;
14734
14735 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14736 {
14737 *lowpc = std::min (*lowpc, low);
14738 *highpc = std::max (*highpc, high);
14739 }
14740
14741 /* If the language does not allow nested subprograms (either inside
14742 subprograms or lexical blocks), we're done. */
14743 if (cu->language != language_ada)
14744 return;
14745
14746 /* Check all the children of the given DIE. If it contains nested
14747 subprograms, then check their pc bounds. Likewise, we need to
14748 check lexical blocks as well, as they may also contain subprogram
14749 definitions. */
14750 while (child && child->tag)
14751 {
14752 if (child->tag == DW_TAG_subprogram
14753 || child->tag == DW_TAG_lexical_block)
14754 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14755 child = sibling_die (child);
14756 }
14757 }
14758
14759 /* Get the low and high pc's represented by the scope DIE, and store
14760 them in *LOWPC and *HIGHPC. If the correct values can't be
14761 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14762
14763 static void
14764 get_scope_pc_bounds (struct die_info *die,
14765 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14766 struct dwarf2_cu *cu)
14767 {
14768 CORE_ADDR best_low = (CORE_ADDR) -1;
14769 CORE_ADDR best_high = (CORE_ADDR) 0;
14770 CORE_ADDR current_low, current_high;
14771
14772 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14773 >= PC_BOUNDS_RANGES)
14774 {
14775 best_low = current_low;
14776 best_high = current_high;
14777 }
14778 else
14779 {
14780 struct die_info *child = die->child;
14781
14782 while (child && child->tag)
14783 {
14784 switch (child->tag) {
14785 case DW_TAG_subprogram:
14786 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14787 break;
14788 case DW_TAG_namespace:
14789 case DW_TAG_module:
14790 /* FIXME: carlton/2004-01-16: Should we do this for
14791 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14792 that current GCC's always emit the DIEs corresponding
14793 to definitions of methods of classes as children of a
14794 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14795 the DIEs giving the declarations, which could be
14796 anywhere). But I don't see any reason why the
14797 standards says that they have to be there. */
14798 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14799
14800 if (current_low != ((CORE_ADDR) -1))
14801 {
14802 best_low = std::min (best_low, current_low);
14803 best_high = std::max (best_high, current_high);
14804 }
14805 break;
14806 default:
14807 /* Ignore. */
14808 break;
14809 }
14810
14811 child = sibling_die (child);
14812 }
14813 }
14814
14815 *lowpc = best_low;
14816 *highpc = best_high;
14817 }
14818
14819 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14820 in DIE. */
14821
14822 static void
14823 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14824 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14825 {
14826 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14827 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14828 struct attribute *attr;
14829 struct attribute *attr_high;
14830
14831 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14832 if (attr_high)
14833 {
14834 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14835 if (attr)
14836 {
14837 CORE_ADDR low = attr_value_as_address (attr);
14838 CORE_ADDR high = attr_value_as_address (attr_high);
14839
14840 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14841 high += low;
14842
14843 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14844 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14845 record_block_range (block, low, high - 1);
14846 }
14847 }
14848
14849 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14850 if (attr)
14851 {
14852 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14853 We take advantage of the fact that DW_AT_ranges does not appear
14854 in DW_TAG_compile_unit of DWO files. */
14855 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14856
14857 /* The value of the DW_AT_ranges attribute is the offset of the
14858 address range list in the .debug_ranges section. */
14859 unsigned long offset = (DW_UNSND (attr)
14860 + (need_ranges_base ? cu->ranges_base : 0));
14861
14862 dwarf2_ranges_process (offset, cu,
14863 [&] (CORE_ADDR start, CORE_ADDR end)
14864 {
14865 start += baseaddr;
14866 end += baseaddr;
14867 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14868 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14869 record_block_range (block, start, end - 1);
14870 });
14871 }
14872 }
14873
14874 /* Check whether the producer field indicates either of GCC < 4.6, or the
14875 Intel C/C++ compiler, and cache the result in CU. */
14876
14877 static void
14878 check_producer (struct dwarf2_cu *cu)
14879 {
14880 int major, minor;
14881
14882 if (cu->producer == NULL)
14883 {
14884 /* For unknown compilers expect their behavior is DWARF version
14885 compliant.
14886
14887 GCC started to support .debug_types sections by -gdwarf-4 since
14888 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14889 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14890 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14891 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14892 }
14893 else if (producer_is_gcc (cu->producer, &major, &minor))
14894 {
14895 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14896 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14897 }
14898 else if (producer_is_icc (cu->producer, &major, &minor))
14899 cu->producer_is_icc_lt_14 = major < 14;
14900 else
14901 {
14902 /* For other non-GCC compilers, expect their behavior is DWARF version
14903 compliant. */
14904 }
14905
14906 cu->checked_producer = 1;
14907 }
14908
14909 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14910 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14911 during 4.6.0 experimental. */
14912
14913 static int
14914 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14915 {
14916 if (!cu->checked_producer)
14917 check_producer (cu);
14918
14919 return cu->producer_is_gxx_lt_4_6;
14920 }
14921
14922 /* Return the default accessibility type if it is not overriden by
14923 DW_AT_accessibility. */
14924
14925 static enum dwarf_access_attribute
14926 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14927 {
14928 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14929 {
14930 /* The default DWARF 2 accessibility for members is public, the default
14931 accessibility for inheritance is private. */
14932
14933 if (die->tag != DW_TAG_inheritance)
14934 return DW_ACCESS_public;
14935 else
14936 return DW_ACCESS_private;
14937 }
14938 else
14939 {
14940 /* DWARF 3+ defines the default accessibility a different way. The same
14941 rules apply now for DW_TAG_inheritance as for the members and it only
14942 depends on the container kind. */
14943
14944 if (die->parent->tag == DW_TAG_class_type)
14945 return DW_ACCESS_private;
14946 else
14947 return DW_ACCESS_public;
14948 }
14949 }
14950
14951 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14952 offset. If the attribute was not found return 0, otherwise return
14953 1. If it was found but could not properly be handled, set *OFFSET
14954 to 0. */
14955
14956 static int
14957 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14958 LONGEST *offset)
14959 {
14960 struct attribute *attr;
14961
14962 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14963 if (attr != NULL)
14964 {
14965 *offset = 0;
14966
14967 /* Note that we do not check for a section offset first here.
14968 This is because DW_AT_data_member_location is new in DWARF 4,
14969 so if we see it, we can assume that a constant form is really
14970 a constant and not a section offset. */
14971 if (attr_form_is_constant (attr))
14972 *offset = dwarf2_get_attr_constant_value (attr, 0);
14973 else if (attr_form_is_section_offset (attr))
14974 dwarf2_complex_location_expr_complaint ();
14975 else if (attr_form_is_block (attr))
14976 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14977 else
14978 dwarf2_complex_location_expr_complaint ();
14979
14980 return 1;
14981 }
14982
14983 return 0;
14984 }
14985
14986 /* Add an aggregate field to the field list. */
14987
14988 static void
14989 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14990 struct dwarf2_cu *cu)
14991 {
14992 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14993 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14994 struct nextfield *new_field;
14995 struct attribute *attr;
14996 struct field *fp;
14997 const char *fieldname = "";
14998
14999 if (die->tag == DW_TAG_inheritance)
15000 {
15001 fip->baseclasses.emplace_back ();
15002 new_field = &fip->baseclasses.back ();
15003 }
15004 else
15005 {
15006 fip->fields.emplace_back ();
15007 new_field = &fip->fields.back ();
15008 }
15009
15010 fip->nfields++;
15011
15012 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15013 if (attr)
15014 new_field->accessibility = DW_UNSND (attr);
15015 else
15016 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15017 if (new_field->accessibility != DW_ACCESS_public)
15018 fip->non_public_fields = 1;
15019
15020 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15021 if (attr)
15022 new_field->virtuality = DW_UNSND (attr);
15023 else
15024 new_field->virtuality = DW_VIRTUALITY_none;
15025
15026 fp = &new_field->field;
15027
15028 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15029 {
15030 LONGEST offset;
15031
15032 /* Data member other than a C++ static data member. */
15033
15034 /* Get type of field. */
15035 fp->type = die_type (die, cu);
15036
15037 SET_FIELD_BITPOS (*fp, 0);
15038
15039 /* Get bit size of field (zero if none). */
15040 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15041 if (attr)
15042 {
15043 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15044 }
15045 else
15046 {
15047 FIELD_BITSIZE (*fp) = 0;
15048 }
15049
15050 /* Get bit offset of field. */
15051 if (handle_data_member_location (die, cu, &offset))
15052 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15053 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15054 if (attr)
15055 {
15056 if (gdbarch_bits_big_endian (gdbarch))
15057 {
15058 /* For big endian bits, the DW_AT_bit_offset gives the
15059 additional bit offset from the MSB of the containing
15060 anonymous object to the MSB of the field. We don't
15061 have to do anything special since we don't need to
15062 know the size of the anonymous object. */
15063 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15064 }
15065 else
15066 {
15067 /* For little endian bits, compute the bit offset to the
15068 MSB of the anonymous object, subtract off the number of
15069 bits from the MSB of the field to the MSB of the
15070 object, and then subtract off the number of bits of
15071 the field itself. The result is the bit offset of
15072 the LSB of the field. */
15073 int anonymous_size;
15074 int bit_offset = DW_UNSND (attr);
15075
15076 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15077 if (attr)
15078 {
15079 /* The size of the anonymous object containing
15080 the bit field is explicit, so use the
15081 indicated size (in bytes). */
15082 anonymous_size = DW_UNSND (attr);
15083 }
15084 else
15085 {
15086 /* The size of the anonymous object containing
15087 the bit field must be inferred from the type
15088 attribute of the data member containing the
15089 bit field. */
15090 anonymous_size = TYPE_LENGTH (fp->type);
15091 }
15092 SET_FIELD_BITPOS (*fp,
15093 (FIELD_BITPOS (*fp)
15094 + anonymous_size * bits_per_byte
15095 - bit_offset - FIELD_BITSIZE (*fp)));
15096 }
15097 }
15098 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15099 if (attr != NULL)
15100 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15101 + dwarf2_get_attr_constant_value (attr, 0)));
15102
15103 /* Get name of field. */
15104 fieldname = dwarf2_name (die, cu);
15105 if (fieldname == NULL)
15106 fieldname = "";
15107
15108 /* The name is already allocated along with this objfile, so we don't
15109 need to duplicate it for the type. */
15110 fp->name = fieldname;
15111
15112 /* Change accessibility for artificial fields (e.g. virtual table
15113 pointer or virtual base class pointer) to private. */
15114 if (dwarf2_attr (die, DW_AT_artificial, cu))
15115 {
15116 FIELD_ARTIFICIAL (*fp) = 1;
15117 new_field->accessibility = DW_ACCESS_private;
15118 fip->non_public_fields = 1;
15119 }
15120 }
15121 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15122 {
15123 /* C++ static member. */
15124
15125 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15126 is a declaration, but all versions of G++ as of this writing
15127 (so through at least 3.2.1) incorrectly generate
15128 DW_TAG_variable tags. */
15129
15130 const char *physname;
15131
15132 /* Get name of field. */
15133 fieldname = dwarf2_name (die, cu);
15134 if (fieldname == NULL)
15135 return;
15136
15137 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15138 if (attr
15139 /* Only create a symbol if this is an external value.
15140 new_symbol checks this and puts the value in the global symbol
15141 table, which we want. If it is not external, new_symbol
15142 will try to put the value in cu->list_in_scope which is wrong. */
15143 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15144 {
15145 /* A static const member, not much different than an enum as far as
15146 we're concerned, except that we can support more types. */
15147 new_symbol (die, NULL, cu);
15148 }
15149
15150 /* Get physical name. */
15151 physname = dwarf2_physname (fieldname, die, cu);
15152
15153 /* The name is already allocated along with this objfile, so we don't
15154 need to duplicate it for the type. */
15155 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15156 FIELD_TYPE (*fp) = die_type (die, cu);
15157 FIELD_NAME (*fp) = fieldname;
15158 }
15159 else if (die->tag == DW_TAG_inheritance)
15160 {
15161 LONGEST offset;
15162
15163 /* C++ base class field. */
15164 if (handle_data_member_location (die, cu, &offset))
15165 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15166 FIELD_BITSIZE (*fp) = 0;
15167 FIELD_TYPE (*fp) = die_type (die, cu);
15168 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15169 }
15170 else if (die->tag == DW_TAG_variant_part)
15171 {
15172 /* process_structure_scope will treat this DIE as a union. */
15173 process_structure_scope (die, cu);
15174
15175 /* The variant part is relative to the start of the enclosing
15176 structure. */
15177 SET_FIELD_BITPOS (*fp, 0);
15178 fp->type = get_die_type (die, cu);
15179 fp->artificial = 1;
15180 fp->name = "<<variant>>";
15181 }
15182 else
15183 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15184 }
15185
15186 /* Can the type given by DIE define another type? */
15187
15188 static bool
15189 type_can_define_types (const struct die_info *die)
15190 {
15191 switch (die->tag)
15192 {
15193 case DW_TAG_typedef:
15194 case DW_TAG_class_type:
15195 case DW_TAG_structure_type:
15196 case DW_TAG_union_type:
15197 case DW_TAG_enumeration_type:
15198 return true;
15199
15200 default:
15201 return false;
15202 }
15203 }
15204
15205 /* Add a type definition defined in the scope of the FIP's class. */
15206
15207 static void
15208 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15209 struct dwarf2_cu *cu)
15210 {
15211 struct decl_field fp;
15212 memset (&fp, 0, sizeof (fp));
15213
15214 gdb_assert (type_can_define_types (die));
15215
15216 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15217 fp.name = dwarf2_name (die, cu);
15218 fp.type = read_type_die (die, cu);
15219
15220 /* Save accessibility. */
15221 enum dwarf_access_attribute accessibility;
15222 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15223 if (attr != NULL)
15224 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15225 else
15226 accessibility = dwarf2_default_access_attribute (die, cu);
15227 switch (accessibility)
15228 {
15229 case DW_ACCESS_public:
15230 /* The assumed value if neither private nor protected. */
15231 break;
15232 case DW_ACCESS_private:
15233 fp.is_private = 1;
15234 break;
15235 case DW_ACCESS_protected:
15236 fp.is_protected = 1;
15237 break;
15238 default:
15239 complaint (&symfile_complaints,
15240 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15241 }
15242
15243 if (die->tag == DW_TAG_typedef)
15244 fip->typedef_field_list.push_back (fp);
15245 else
15246 fip->nested_types_list.push_back (fp);
15247 }
15248
15249 /* Create the vector of fields, and attach it to the type. */
15250
15251 static void
15252 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15253 struct dwarf2_cu *cu)
15254 {
15255 int nfields = fip->nfields;
15256
15257 /* Record the field count, allocate space for the array of fields,
15258 and create blank accessibility bitfields if necessary. */
15259 TYPE_NFIELDS (type) = nfields;
15260 TYPE_FIELDS (type) = (struct field *)
15261 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15262
15263 if (fip->non_public_fields && cu->language != language_ada)
15264 {
15265 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15266
15267 TYPE_FIELD_PRIVATE_BITS (type) =
15268 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15269 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15270
15271 TYPE_FIELD_PROTECTED_BITS (type) =
15272 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15273 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15274
15275 TYPE_FIELD_IGNORE_BITS (type) =
15276 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15277 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15278 }
15279
15280 /* If the type has baseclasses, allocate and clear a bit vector for
15281 TYPE_FIELD_VIRTUAL_BITS. */
15282 if (!fip->baseclasses.empty () && cu->language != language_ada)
15283 {
15284 int num_bytes = B_BYTES (fip->baseclasses.size ());
15285 unsigned char *pointer;
15286
15287 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15288 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15289 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15290 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15291 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15292 }
15293
15294 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15295 {
15296 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15297
15298 for (int index = 0; index < nfields; ++index)
15299 {
15300 struct nextfield &field = fip->fields[index];
15301
15302 if (field.variant.is_discriminant)
15303 di->discriminant_index = index;
15304 else if (field.variant.default_branch)
15305 di->default_index = index;
15306 else
15307 di->discriminants[index] = field.variant.discriminant_value;
15308 }
15309 }
15310
15311 /* Copy the saved-up fields into the field vector. */
15312 for (int i = 0; i < nfields; ++i)
15313 {
15314 struct nextfield &field
15315 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15316 : fip->fields[i - fip->baseclasses.size ()]);
15317
15318 TYPE_FIELD (type, i) = field.field;
15319 switch (field.accessibility)
15320 {
15321 case DW_ACCESS_private:
15322 if (cu->language != language_ada)
15323 SET_TYPE_FIELD_PRIVATE (type, i);
15324 break;
15325
15326 case DW_ACCESS_protected:
15327 if (cu->language != language_ada)
15328 SET_TYPE_FIELD_PROTECTED (type, i);
15329 break;
15330
15331 case DW_ACCESS_public:
15332 break;
15333
15334 default:
15335 /* Unknown accessibility. Complain and treat it as public. */
15336 {
15337 complaint (&symfile_complaints, _("unsupported accessibility %d"),
15338 field.accessibility);
15339 }
15340 break;
15341 }
15342 if (i < fip->baseclasses.size ())
15343 {
15344 switch (field.virtuality)
15345 {
15346 case DW_VIRTUALITY_virtual:
15347 case DW_VIRTUALITY_pure_virtual:
15348 if (cu->language == language_ada)
15349 error (_("unexpected virtuality in component of Ada type"));
15350 SET_TYPE_FIELD_VIRTUAL (type, i);
15351 break;
15352 }
15353 }
15354 }
15355 }
15356
15357 /* Return true if this member function is a constructor, false
15358 otherwise. */
15359
15360 static int
15361 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15362 {
15363 const char *fieldname;
15364 const char *type_name;
15365 int len;
15366
15367 if (die->parent == NULL)
15368 return 0;
15369
15370 if (die->parent->tag != DW_TAG_structure_type
15371 && die->parent->tag != DW_TAG_union_type
15372 && die->parent->tag != DW_TAG_class_type)
15373 return 0;
15374
15375 fieldname = dwarf2_name (die, cu);
15376 type_name = dwarf2_name (die->parent, cu);
15377 if (fieldname == NULL || type_name == NULL)
15378 return 0;
15379
15380 len = strlen (fieldname);
15381 return (strncmp (fieldname, type_name, len) == 0
15382 && (type_name[len] == '\0' || type_name[len] == '<'));
15383 }
15384
15385 /* Add a member function to the proper fieldlist. */
15386
15387 static void
15388 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15389 struct type *type, struct dwarf2_cu *cu)
15390 {
15391 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15392 struct attribute *attr;
15393 int i;
15394 struct fnfieldlist *flp = nullptr;
15395 struct fn_field *fnp;
15396 const char *fieldname;
15397 struct type *this_type;
15398 enum dwarf_access_attribute accessibility;
15399
15400 if (cu->language == language_ada)
15401 error (_("unexpected member function in Ada type"));
15402
15403 /* Get name of member function. */
15404 fieldname = dwarf2_name (die, cu);
15405 if (fieldname == NULL)
15406 return;
15407
15408 /* Look up member function name in fieldlist. */
15409 for (i = 0; i < fip->fnfieldlists.size (); i++)
15410 {
15411 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15412 {
15413 flp = &fip->fnfieldlists[i];
15414 break;
15415 }
15416 }
15417
15418 /* Create a new fnfieldlist if necessary. */
15419 if (flp == nullptr)
15420 {
15421 fip->fnfieldlists.emplace_back ();
15422 flp = &fip->fnfieldlists.back ();
15423 flp->name = fieldname;
15424 i = fip->fnfieldlists.size () - 1;
15425 }
15426
15427 /* Create a new member function field and add it to the vector of
15428 fnfieldlists. */
15429 flp->fnfields.emplace_back ();
15430 fnp = &flp->fnfields.back ();
15431
15432 /* Delay processing of the physname until later. */
15433 if (cu->language == language_cplus)
15434 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15435 die, cu);
15436 else
15437 {
15438 const char *physname = dwarf2_physname (fieldname, die, cu);
15439 fnp->physname = physname ? physname : "";
15440 }
15441
15442 fnp->type = alloc_type (objfile);
15443 this_type = read_type_die (die, cu);
15444 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15445 {
15446 int nparams = TYPE_NFIELDS (this_type);
15447
15448 /* TYPE is the domain of this method, and THIS_TYPE is the type
15449 of the method itself (TYPE_CODE_METHOD). */
15450 smash_to_method_type (fnp->type, type,
15451 TYPE_TARGET_TYPE (this_type),
15452 TYPE_FIELDS (this_type),
15453 TYPE_NFIELDS (this_type),
15454 TYPE_VARARGS (this_type));
15455
15456 /* Handle static member functions.
15457 Dwarf2 has no clean way to discern C++ static and non-static
15458 member functions. G++ helps GDB by marking the first
15459 parameter for non-static member functions (which is the this
15460 pointer) as artificial. We obtain this information from
15461 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15462 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15463 fnp->voffset = VOFFSET_STATIC;
15464 }
15465 else
15466 complaint (&symfile_complaints, _("member function type missing for '%s'"),
15467 dwarf2_full_name (fieldname, die, cu));
15468
15469 /* Get fcontext from DW_AT_containing_type if present. */
15470 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15471 fnp->fcontext = die_containing_type (die, cu);
15472
15473 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15474 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15475
15476 /* Get accessibility. */
15477 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15478 if (attr)
15479 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15480 else
15481 accessibility = dwarf2_default_access_attribute (die, cu);
15482 switch (accessibility)
15483 {
15484 case DW_ACCESS_private:
15485 fnp->is_private = 1;
15486 break;
15487 case DW_ACCESS_protected:
15488 fnp->is_protected = 1;
15489 break;
15490 }
15491
15492 /* Check for artificial methods. */
15493 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15494 if (attr && DW_UNSND (attr) != 0)
15495 fnp->is_artificial = 1;
15496
15497 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15498
15499 /* Get index in virtual function table if it is a virtual member
15500 function. For older versions of GCC, this is an offset in the
15501 appropriate virtual table, as specified by DW_AT_containing_type.
15502 For everyone else, it is an expression to be evaluated relative
15503 to the object address. */
15504
15505 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15506 if (attr)
15507 {
15508 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15509 {
15510 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15511 {
15512 /* Old-style GCC. */
15513 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15514 }
15515 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15516 || (DW_BLOCK (attr)->size > 1
15517 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15518 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15519 {
15520 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15521 if ((fnp->voffset % cu->header.addr_size) != 0)
15522 dwarf2_complex_location_expr_complaint ();
15523 else
15524 fnp->voffset /= cu->header.addr_size;
15525 fnp->voffset += 2;
15526 }
15527 else
15528 dwarf2_complex_location_expr_complaint ();
15529
15530 if (!fnp->fcontext)
15531 {
15532 /* If there is no `this' field and no DW_AT_containing_type,
15533 we cannot actually find a base class context for the
15534 vtable! */
15535 if (TYPE_NFIELDS (this_type) == 0
15536 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15537 {
15538 complaint (&symfile_complaints,
15539 _("cannot determine context for virtual member "
15540 "function \"%s\" (offset %s)"),
15541 fieldname, sect_offset_str (die->sect_off));
15542 }
15543 else
15544 {
15545 fnp->fcontext
15546 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15547 }
15548 }
15549 }
15550 else if (attr_form_is_section_offset (attr))
15551 {
15552 dwarf2_complex_location_expr_complaint ();
15553 }
15554 else
15555 {
15556 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15557 fieldname);
15558 }
15559 }
15560 else
15561 {
15562 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15563 if (attr && DW_UNSND (attr))
15564 {
15565 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15566 complaint (&symfile_complaints,
15567 _("Member function \"%s\" (offset %s) is virtual "
15568 "but the vtable offset is not specified"),
15569 fieldname, sect_offset_str (die->sect_off));
15570 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15571 TYPE_CPLUS_DYNAMIC (type) = 1;
15572 }
15573 }
15574 }
15575
15576 /* Create the vector of member function fields, and attach it to the type. */
15577
15578 static void
15579 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15580 struct dwarf2_cu *cu)
15581 {
15582 if (cu->language == language_ada)
15583 error (_("unexpected member functions in Ada type"));
15584
15585 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15586 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15587 TYPE_ALLOC (type,
15588 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15589
15590 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15591 {
15592 struct fnfieldlist &nf = fip->fnfieldlists[i];
15593 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15594
15595 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15596 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15597 fn_flp->fn_fields = (struct fn_field *)
15598 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15599
15600 for (int k = 0; k < nf.fnfields.size (); ++k)
15601 fn_flp->fn_fields[k] = nf.fnfields[k];
15602 }
15603
15604 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15605 }
15606
15607 /* Returns non-zero if NAME is the name of a vtable member in CU's
15608 language, zero otherwise. */
15609 static int
15610 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15611 {
15612 static const char vptr[] = "_vptr";
15613
15614 /* Look for the C++ form of the vtable. */
15615 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15616 return 1;
15617
15618 return 0;
15619 }
15620
15621 /* GCC outputs unnamed structures that are really pointers to member
15622 functions, with the ABI-specified layout. If TYPE describes
15623 such a structure, smash it into a member function type.
15624
15625 GCC shouldn't do this; it should just output pointer to member DIEs.
15626 This is GCC PR debug/28767. */
15627
15628 static void
15629 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15630 {
15631 struct type *pfn_type, *self_type, *new_type;
15632
15633 /* Check for a structure with no name and two children. */
15634 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15635 return;
15636
15637 /* Check for __pfn and __delta members. */
15638 if (TYPE_FIELD_NAME (type, 0) == NULL
15639 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15640 || TYPE_FIELD_NAME (type, 1) == NULL
15641 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15642 return;
15643
15644 /* Find the type of the method. */
15645 pfn_type = TYPE_FIELD_TYPE (type, 0);
15646 if (pfn_type == NULL
15647 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15648 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15649 return;
15650
15651 /* Look for the "this" argument. */
15652 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15653 if (TYPE_NFIELDS (pfn_type) == 0
15654 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15655 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15656 return;
15657
15658 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15659 new_type = alloc_type (objfile);
15660 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15661 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15662 TYPE_VARARGS (pfn_type));
15663 smash_to_methodptr_type (type, new_type);
15664 }
15665
15666
15667 /* Called when we find the DIE that starts a structure or union scope
15668 (definition) to create a type for the structure or union. Fill in
15669 the type's name and general properties; the members will not be
15670 processed until process_structure_scope. A symbol table entry for
15671 the type will also not be done until process_structure_scope (assuming
15672 the type has a name).
15673
15674 NOTE: we need to call these functions regardless of whether or not the
15675 DIE has a DW_AT_name attribute, since it might be an anonymous
15676 structure or union. This gets the type entered into our set of
15677 user defined types. */
15678
15679 static struct type *
15680 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15681 {
15682 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15683 struct type *type;
15684 struct attribute *attr;
15685 const char *name;
15686
15687 /* If the definition of this type lives in .debug_types, read that type.
15688 Don't follow DW_AT_specification though, that will take us back up
15689 the chain and we want to go down. */
15690 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15691 if (attr)
15692 {
15693 type = get_DW_AT_signature_type (die, attr, cu);
15694
15695 /* The type's CU may not be the same as CU.
15696 Ensure TYPE is recorded with CU in die_type_hash. */
15697 return set_die_type (die, type, cu);
15698 }
15699
15700 type = alloc_type (objfile);
15701 INIT_CPLUS_SPECIFIC (type);
15702
15703 name = dwarf2_name (die, cu);
15704 if (name != NULL)
15705 {
15706 if (cu->language == language_cplus
15707 || cu->language == language_d
15708 || cu->language == language_rust)
15709 {
15710 const char *full_name = dwarf2_full_name (name, die, cu);
15711
15712 /* dwarf2_full_name might have already finished building the DIE's
15713 type. If so, there is no need to continue. */
15714 if (get_die_type (die, cu) != NULL)
15715 return get_die_type (die, cu);
15716
15717 TYPE_TAG_NAME (type) = full_name;
15718 if (die->tag == DW_TAG_structure_type
15719 || die->tag == DW_TAG_class_type)
15720 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15721 }
15722 else
15723 {
15724 /* The name is already allocated along with this objfile, so
15725 we don't need to duplicate it for the type. */
15726 TYPE_TAG_NAME (type) = name;
15727 if (die->tag == DW_TAG_class_type)
15728 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15729 }
15730 }
15731
15732 if (die->tag == DW_TAG_structure_type)
15733 {
15734 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15735 }
15736 else if (die->tag == DW_TAG_union_type)
15737 {
15738 TYPE_CODE (type) = TYPE_CODE_UNION;
15739 }
15740 else if (die->tag == DW_TAG_variant_part)
15741 {
15742 TYPE_CODE (type) = TYPE_CODE_UNION;
15743 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15744 }
15745 else
15746 {
15747 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15748 }
15749
15750 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15751 TYPE_DECLARED_CLASS (type) = 1;
15752
15753 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15754 if (attr)
15755 {
15756 if (attr_form_is_constant (attr))
15757 TYPE_LENGTH (type) = DW_UNSND (attr);
15758 else
15759 {
15760 /* For the moment, dynamic type sizes are not supported
15761 by GDB's struct type. The actual size is determined
15762 on-demand when resolving the type of a given object,
15763 so set the type's length to zero for now. Otherwise,
15764 we record an expression as the length, and that expression
15765 could lead to a very large value, which could eventually
15766 lead to us trying to allocate that much memory when creating
15767 a value of that type. */
15768 TYPE_LENGTH (type) = 0;
15769 }
15770 }
15771 else
15772 {
15773 TYPE_LENGTH (type) = 0;
15774 }
15775
15776 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15777 {
15778 /* ICC<14 does not output the required DW_AT_declaration on
15779 incomplete types, but gives them a size of zero. */
15780 TYPE_STUB (type) = 1;
15781 }
15782 else
15783 TYPE_STUB_SUPPORTED (type) = 1;
15784
15785 if (die_is_declaration (die, cu))
15786 TYPE_STUB (type) = 1;
15787 else if (attr == NULL && die->child == NULL
15788 && producer_is_realview (cu->producer))
15789 /* RealView does not output the required DW_AT_declaration
15790 on incomplete types. */
15791 TYPE_STUB (type) = 1;
15792
15793 /* We need to add the type field to the die immediately so we don't
15794 infinitely recurse when dealing with pointers to the structure
15795 type within the structure itself. */
15796 set_die_type (die, type, cu);
15797
15798 /* set_die_type should be already done. */
15799 set_descriptive_type (type, die, cu);
15800
15801 return type;
15802 }
15803
15804 /* A helper for process_structure_scope that handles a single member
15805 DIE. */
15806
15807 static void
15808 handle_struct_member_die (struct die_info *child_die, struct type *type,
15809 struct field_info *fi,
15810 std::vector<struct symbol *> *template_args,
15811 struct dwarf2_cu *cu)
15812 {
15813 if (child_die->tag == DW_TAG_member
15814 || child_die->tag == DW_TAG_variable
15815 || child_die->tag == DW_TAG_variant_part)
15816 {
15817 /* NOTE: carlton/2002-11-05: A C++ static data member
15818 should be a DW_TAG_member that is a declaration, but
15819 all versions of G++ as of this writing (so through at
15820 least 3.2.1) incorrectly generate DW_TAG_variable
15821 tags for them instead. */
15822 dwarf2_add_field (fi, child_die, cu);
15823 }
15824 else if (child_die->tag == DW_TAG_subprogram)
15825 {
15826 /* Rust doesn't have member functions in the C++ sense.
15827 However, it does emit ordinary functions as children
15828 of a struct DIE. */
15829 if (cu->language == language_rust)
15830 read_func_scope (child_die, cu);
15831 else
15832 {
15833 /* C++ member function. */
15834 dwarf2_add_member_fn (fi, child_die, type, cu);
15835 }
15836 }
15837 else if (child_die->tag == DW_TAG_inheritance)
15838 {
15839 /* C++ base class field. */
15840 dwarf2_add_field (fi, child_die, cu);
15841 }
15842 else if (type_can_define_types (child_die))
15843 dwarf2_add_type_defn (fi, child_die, cu);
15844 else if (child_die->tag == DW_TAG_template_type_param
15845 || child_die->tag == DW_TAG_template_value_param)
15846 {
15847 struct symbol *arg = new_symbol (child_die, NULL, cu);
15848
15849 if (arg != NULL)
15850 template_args->push_back (arg);
15851 }
15852 else if (child_die->tag == DW_TAG_variant)
15853 {
15854 /* In a variant we want to get the discriminant and also add a
15855 field for our sole member child. */
15856 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15857
15858 for (struct die_info *variant_child = child_die->child;
15859 variant_child != NULL;
15860 variant_child = sibling_die (variant_child))
15861 {
15862 if (variant_child->tag == DW_TAG_member)
15863 {
15864 handle_struct_member_die (variant_child, type, fi,
15865 template_args, cu);
15866 /* Only handle the one. */
15867 break;
15868 }
15869 }
15870
15871 /* We don't handle this but we might as well report it if we see
15872 it. */
15873 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15874 complaint (&symfile_complaints,
15875 _("DW_AT_discr_list is not supported yet"
15876 " - DIE at %s [in module %s]"),
15877 sect_offset_str (child_die->sect_off),
15878 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15879
15880 /* The first field was just added, so we can stash the
15881 discriminant there. */
15882 gdb_assert (!fi->fields.empty ());
15883 if (discr == NULL)
15884 fi->fields.back ().variant.default_branch = true;
15885 else
15886 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15887 }
15888 }
15889
15890 /* Finish creating a structure or union type, including filling in
15891 its members and creating a symbol for it. */
15892
15893 static void
15894 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15895 {
15896 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15897 struct die_info *child_die;
15898 struct type *type;
15899
15900 type = get_die_type (die, cu);
15901 if (type == NULL)
15902 type = read_structure_type (die, cu);
15903
15904 /* When reading a DW_TAG_variant_part, we need to notice when we
15905 read the discriminant member, so we can record it later in the
15906 discriminant_info. */
15907 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15908 sect_offset discr_offset;
15909
15910 if (is_variant_part)
15911 {
15912 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15913 if (discr == NULL)
15914 {
15915 /* Maybe it's a univariant form, an extension we support.
15916 In this case arrange not to check the offset. */
15917 is_variant_part = false;
15918 }
15919 else if (attr_form_is_ref (discr))
15920 {
15921 struct dwarf2_cu *target_cu = cu;
15922 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15923
15924 discr_offset = target_die->sect_off;
15925 }
15926 else
15927 {
15928 complaint (&symfile_complaints,
15929 _("DW_AT_discr does not have DIE reference form"
15930 " - DIE at %s [in module %s]"),
15931 sect_offset_str (die->sect_off),
15932 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15933 is_variant_part = false;
15934 }
15935 }
15936
15937 if (die->child != NULL && ! die_is_declaration (die, cu))
15938 {
15939 struct field_info fi;
15940 std::vector<struct symbol *> template_args;
15941
15942 child_die = die->child;
15943
15944 while (child_die && child_die->tag)
15945 {
15946 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15947
15948 if (is_variant_part && discr_offset == child_die->sect_off)
15949 fi.fields.back ().variant.is_discriminant = true;
15950
15951 child_die = sibling_die (child_die);
15952 }
15953
15954 /* Attach template arguments to type. */
15955 if (!template_args.empty ())
15956 {
15957 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15958 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15959 TYPE_TEMPLATE_ARGUMENTS (type)
15960 = XOBNEWVEC (&objfile->objfile_obstack,
15961 struct symbol *,
15962 TYPE_N_TEMPLATE_ARGUMENTS (type));
15963 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15964 template_args.data (),
15965 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15966 * sizeof (struct symbol *)));
15967 }
15968
15969 /* Attach fields and member functions to the type. */
15970 if (fi.nfields)
15971 dwarf2_attach_fields_to_type (&fi, type, cu);
15972 if (!fi.fnfieldlists.empty ())
15973 {
15974 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15975
15976 /* Get the type which refers to the base class (possibly this
15977 class itself) which contains the vtable pointer for the current
15978 class from the DW_AT_containing_type attribute. This use of
15979 DW_AT_containing_type is a GNU extension. */
15980
15981 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15982 {
15983 struct type *t = die_containing_type (die, cu);
15984
15985 set_type_vptr_basetype (type, t);
15986 if (type == t)
15987 {
15988 int i;
15989
15990 /* Our own class provides vtbl ptr. */
15991 for (i = TYPE_NFIELDS (t) - 1;
15992 i >= TYPE_N_BASECLASSES (t);
15993 --i)
15994 {
15995 const char *fieldname = TYPE_FIELD_NAME (t, i);
15996
15997 if (is_vtable_name (fieldname, cu))
15998 {
15999 set_type_vptr_fieldno (type, i);
16000 break;
16001 }
16002 }
16003
16004 /* Complain if virtual function table field not found. */
16005 if (i < TYPE_N_BASECLASSES (t))
16006 complaint (&symfile_complaints,
16007 _("virtual function table pointer "
16008 "not found when defining class '%s'"),
16009 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
16010 "");
16011 }
16012 else
16013 {
16014 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16015 }
16016 }
16017 else if (cu->producer
16018 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16019 {
16020 /* The IBM XLC compiler does not provide direct indication
16021 of the containing type, but the vtable pointer is
16022 always named __vfp. */
16023
16024 int i;
16025
16026 for (i = TYPE_NFIELDS (type) - 1;
16027 i >= TYPE_N_BASECLASSES (type);
16028 --i)
16029 {
16030 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16031 {
16032 set_type_vptr_fieldno (type, i);
16033 set_type_vptr_basetype (type, type);
16034 break;
16035 }
16036 }
16037 }
16038 }
16039
16040 /* Copy fi.typedef_field_list linked list elements content into the
16041 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16042 if (!fi.typedef_field_list.empty ())
16043 {
16044 int count = fi.typedef_field_list.size ();
16045
16046 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16047 TYPE_TYPEDEF_FIELD_ARRAY (type)
16048 = ((struct decl_field *)
16049 TYPE_ALLOC (type,
16050 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16051 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16052
16053 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16054 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16055 }
16056
16057 /* Copy fi.nested_types_list linked list elements content into the
16058 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16059 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16060 {
16061 int count = fi.nested_types_list.size ();
16062
16063 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16064 TYPE_NESTED_TYPES_ARRAY (type)
16065 = ((struct decl_field *)
16066 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16067 TYPE_NESTED_TYPES_COUNT (type) = count;
16068
16069 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16070 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16071 }
16072 }
16073
16074 quirk_gcc_member_function_pointer (type, objfile);
16075 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16076 cu->rust_unions.push_back (type);
16077
16078 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16079 snapshots) has been known to create a die giving a declaration
16080 for a class that has, as a child, a die giving a definition for a
16081 nested class. So we have to process our children even if the
16082 current die is a declaration. Normally, of course, a declaration
16083 won't have any children at all. */
16084
16085 child_die = die->child;
16086
16087 while (child_die != NULL && child_die->tag)
16088 {
16089 if (child_die->tag == DW_TAG_member
16090 || child_die->tag == DW_TAG_variable
16091 || child_die->tag == DW_TAG_inheritance
16092 || child_die->tag == DW_TAG_template_value_param
16093 || child_die->tag == DW_TAG_template_type_param)
16094 {
16095 /* Do nothing. */
16096 }
16097 else
16098 process_die (child_die, cu);
16099
16100 child_die = sibling_die (child_die);
16101 }
16102
16103 /* Do not consider external references. According to the DWARF standard,
16104 these DIEs are identified by the fact that they have no byte_size
16105 attribute, and a declaration attribute. */
16106 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16107 || !die_is_declaration (die, cu))
16108 new_symbol (die, type, cu);
16109 }
16110
16111 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16112 update TYPE using some information only available in DIE's children. */
16113
16114 static void
16115 update_enumeration_type_from_children (struct die_info *die,
16116 struct type *type,
16117 struct dwarf2_cu *cu)
16118 {
16119 struct die_info *child_die;
16120 int unsigned_enum = 1;
16121 int flag_enum = 1;
16122 ULONGEST mask = 0;
16123
16124 auto_obstack obstack;
16125
16126 for (child_die = die->child;
16127 child_die != NULL && child_die->tag;
16128 child_die = sibling_die (child_die))
16129 {
16130 struct attribute *attr;
16131 LONGEST value;
16132 const gdb_byte *bytes;
16133 struct dwarf2_locexpr_baton *baton;
16134 const char *name;
16135
16136 if (child_die->tag != DW_TAG_enumerator)
16137 continue;
16138
16139 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16140 if (attr == NULL)
16141 continue;
16142
16143 name = dwarf2_name (child_die, cu);
16144 if (name == NULL)
16145 name = "<anonymous enumerator>";
16146
16147 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16148 &value, &bytes, &baton);
16149 if (value < 0)
16150 {
16151 unsigned_enum = 0;
16152 flag_enum = 0;
16153 }
16154 else if ((mask & value) != 0)
16155 flag_enum = 0;
16156 else
16157 mask |= value;
16158
16159 /* If we already know that the enum type is neither unsigned, nor
16160 a flag type, no need to look at the rest of the enumerates. */
16161 if (!unsigned_enum && !flag_enum)
16162 break;
16163 }
16164
16165 if (unsigned_enum)
16166 TYPE_UNSIGNED (type) = 1;
16167 if (flag_enum)
16168 TYPE_FLAG_ENUM (type) = 1;
16169 }
16170
16171 /* Given a DW_AT_enumeration_type die, set its type. We do not
16172 complete the type's fields yet, or create any symbols. */
16173
16174 static struct type *
16175 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16176 {
16177 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16178 struct type *type;
16179 struct attribute *attr;
16180 const char *name;
16181
16182 /* If the definition of this type lives in .debug_types, read that type.
16183 Don't follow DW_AT_specification though, that will take us back up
16184 the chain and we want to go down. */
16185 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16186 if (attr)
16187 {
16188 type = get_DW_AT_signature_type (die, attr, cu);
16189
16190 /* The type's CU may not be the same as CU.
16191 Ensure TYPE is recorded with CU in die_type_hash. */
16192 return set_die_type (die, type, cu);
16193 }
16194
16195 type = alloc_type (objfile);
16196
16197 TYPE_CODE (type) = TYPE_CODE_ENUM;
16198 name = dwarf2_full_name (NULL, die, cu);
16199 if (name != NULL)
16200 TYPE_TAG_NAME (type) = name;
16201
16202 attr = dwarf2_attr (die, DW_AT_type, cu);
16203 if (attr != NULL)
16204 {
16205 struct type *underlying_type = die_type (die, cu);
16206
16207 TYPE_TARGET_TYPE (type) = underlying_type;
16208 }
16209
16210 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16211 if (attr)
16212 {
16213 TYPE_LENGTH (type) = DW_UNSND (attr);
16214 }
16215 else
16216 {
16217 TYPE_LENGTH (type) = 0;
16218 }
16219
16220 /* The enumeration DIE can be incomplete. In Ada, any type can be
16221 declared as private in the package spec, and then defined only
16222 inside the package body. Such types are known as Taft Amendment
16223 Types. When another package uses such a type, an incomplete DIE
16224 may be generated by the compiler. */
16225 if (die_is_declaration (die, cu))
16226 TYPE_STUB (type) = 1;
16227
16228 /* Finish the creation of this type by using the enum's children.
16229 We must call this even when the underlying type has been provided
16230 so that we can determine if we're looking at a "flag" enum. */
16231 update_enumeration_type_from_children (die, type, cu);
16232
16233 /* If this type has an underlying type that is not a stub, then we
16234 may use its attributes. We always use the "unsigned" attribute
16235 in this situation, because ordinarily we guess whether the type
16236 is unsigned -- but the guess can be wrong and the underlying type
16237 can tell us the reality. However, we defer to a local size
16238 attribute if one exists, because this lets the compiler override
16239 the underlying type if needed. */
16240 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16241 {
16242 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16243 if (TYPE_LENGTH (type) == 0)
16244 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16245 }
16246
16247 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16248
16249 return set_die_type (die, type, cu);
16250 }
16251
16252 /* Given a pointer to a die which begins an enumeration, process all
16253 the dies that define the members of the enumeration, and create the
16254 symbol for the enumeration type.
16255
16256 NOTE: We reverse the order of the element list. */
16257
16258 static void
16259 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16260 {
16261 struct type *this_type;
16262
16263 this_type = get_die_type (die, cu);
16264 if (this_type == NULL)
16265 this_type = read_enumeration_type (die, cu);
16266
16267 if (die->child != NULL)
16268 {
16269 struct die_info *child_die;
16270 struct symbol *sym;
16271 struct field *fields = NULL;
16272 int num_fields = 0;
16273 const char *name;
16274
16275 child_die = die->child;
16276 while (child_die && child_die->tag)
16277 {
16278 if (child_die->tag != DW_TAG_enumerator)
16279 {
16280 process_die (child_die, cu);
16281 }
16282 else
16283 {
16284 name = dwarf2_name (child_die, cu);
16285 if (name)
16286 {
16287 sym = new_symbol (child_die, this_type, cu);
16288
16289 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16290 {
16291 fields = (struct field *)
16292 xrealloc (fields,
16293 (num_fields + DW_FIELD_ALLOC_CHUNK)
16294 * sizeof (struct field));
16295 }
16296
16297 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16298 FIELD_TYPE (fields[num_fields]) = NULL;
16299 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16300 FIELD_BITSIZE (fields[num_fields]) = 0;
16301
16302 num_fields++;
16303 }
16304 }
16305
16306 child_die = sibling_die (child_die);
16307 }
16308
16309 if (num_fields)
16310 {
16311 TYPE_NFIELDS (this_type) = num_fields;
16312 TYPE_FIELDS (this_type) = (struct field *)
16313 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16314 memcpy (TYPE_FIELDS (this_type), fields,
16315 sizeof (struct field) * num_fields);
16316 xfree (fields);
16317 }
16318 }
16319
16320 /* If we are reading an enum from a .debug_types unit, and the enum
16321 is a declaration, and the enum is not the signatured type in the
16322 unit, then we do not want to add a symbol for it. Adding a
16323 symbol would in some cases obscure the true definition of the
16324 enum, giving users an incomplete type when the definition is
16325 actually available. Note that we do not want to do this for all
16326 enums which are just declarations, because C++0x allows forward
16327 enum declarations. */
16328 if (cu->per_cu->is_debug_types
16329 && die_is_declaration (die, cu))
16330 {
16331 struct signatured_type *sig_type;
16332
16333 sig_type = (struct signatured_type *) cu->per_cu;
16334 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16335 if (sig_type->type_offset_in_section != die->sect_off)
16336 return;
16337 }
16338
16339 new_symbol (die, this_type, cu);
16340 }
16341
16342 /* Extract all information from a DW_TAG_array_type DIE and put it in
16343 the DIE's type field. For now, this only handles one dimensional
16344 arrays. */
16345
16346 static struct type *
16347 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16348 {
16349 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16350 struct die_info *child_die;
16351 struct type *type;
16352 struct type *element_type, *range_type, *index_type;
16353 struct attribute *attr;
16354 const char *name;
16355 struct dynamic_prop *byte_stride_prop = NULL;
16356 unsigned int bit_stride = 0;
16357
16358 element_type = die_type (die, cu);
16359
16360 /* The die_type call above may have already set the type for this DIE. */
16361 type = get_die_type (die, cu);
16362 if (type)
16363 return type;
16364
16365 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16366 if (attr != NULL)
16367 {
16368 int stride_ok;
16369
16370 byte_stride_prop
16371 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16372 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16373 if (!stride_ok)
16374 {
16375 complaint (&symfile_complaints,
16376 _("unable to read array DW_AT_byte_stride "
16377 " - DIE at %s [in module %s]"),
16378 sect_offset_str (die->sect_off),
16379 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16380 /* Ignore this attribute. We will likely not be able to print
16381 arrays of this type correctly, but there is little we can do
16382 to help if we cannot read the attribute's value. */
16383 byte_stride_prop = NULL;
16384 }
16385 }
16386
16387 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16388 if (attr != NULL)
16389 bit_stride = DW_UNSND (attr);
16390
16391 /* Irix 6.2 native cc creates array types without children for
16392 arrays with unspecified length. */
16393 if (die->child == NULL)
16394 {
16395 index_type = objfile_type (objfile)->builtin_int;
16396 range_type = create_static_range_type (NULL, index_type, 0, -1);
16397 type = create_array_type_with_stride (NULL, element_type, range_type,
16398 byte_stride_prop, bit_stride);
16399 return set_die_type (die, type, cu);
16400 }
16401
16402 std::vector<struct type *> range_types;
16403 child_die = die->child;
16404 while (child_die && child_die->tag)
16405 {
16406 if (child_die->tag == DW_TAG_subrange_type)
16407 {
16408 struct type *child_type = read_type_die (child_die, cu);
16409
16410 if (child_type != NULL)
16411 {
16412 /* The range type was succesfully read. Save it for the
16413 array type creation. */
16414 range_types.push_back (child_type);
16415 }
16416 }
16417 child_die = sibling_die (child_die);
16418 }
16419
16420 /* Dwarf2 dimensions are output from left to right, create the
16421 necessary array types in backwards order. */
16422
16423 type = element_type;
16424
16425 if (read_array_order (die, cu) == DW_ORD_col_major)
16426 {
16427 int i = 0;
16428
16429 while (i < range_types.size ())
16430 type = create_array_type_with_stride (NULL, type, range_types[i++],
16431 byte_stride_prop, bit_stride);
16432 }
16433 else
16434 {
16435 size_t ndim = range_types.size ();
16436 while (ndim-- > 0)
16437 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16438 byte_stride_prop, bit_stride);
16439 }
16440
16441 /* Understand Dwarf2 support for vector types (like they occur on
16442 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16443 array type. This is not part of the Dwarf2/3 standard yet, but a
16444 custom vendor extension. The main difference between a regular
16445 array and the vector variant is that vectors are passed by value
16446 to functions. */
16447 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16448 if (attr)
16449 make_vector_type (type);
16450
16451 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16452 implementation may choose to implement triple vectors using this
16453 attribute. */
16454 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16455 if (attr)
16456 {
16457 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16458 TYPE_LENGTH (type) = DW_UNSND (attr);
16459 else
16460 complaint (&symfile_complaints,
16461 _("DW_AT_byte_size for array type smaller "
16462 "than the total size of elements"));
16463 }
16464
16465 name = dwarf2_name (die, cu);
16466 if (name)
16467 TYPE_NAME (type) = name;
16468
16469 /* Install the type in the die. */
16470 set_die_type (die, type, cu);
16471
16472 /* set_die_type should be already done. */
16473 set_descriptive_type (type, die, cu);
16474
16475 return type;
16476 }
16477
16478 static enum dwarf_array_dim_ordering
16479 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16480 {
16481 struct attribute *attr;
16482
16483 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16484
16485 if (attr)
16486 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16487
16488 /* GNU F77 is a special case, as at 08/2004 array type info is the
16489 opposite order to the dwarf2 specification, but data is still
16490 laid out as per normal fortran.
16491
16492 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16493 version checking. */
16494
16495 if (cu->language == language_fortran
16496 && cu->producer && strstr (cu->producer, "GNU F77"))
16497 {
16498 return DW_ORD_row_major;
16499 }
16500
16501 switch (cu->language_defn->la_array_ordering)
16502 {
16503 case array_column_major:
16504 return DW_ORD_col_major;
16505 case array_row_major:
16506 default:
16507 return DW_ORD_row_major;
16508 };
16509 }
16510
16511 /* Extract all information from a DW_TAG_set_type DIE and put it in
16512 the DIE's type field. */
16513
16514 static struct type *
16515 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16516 {
16517 struct type *domain_type, *set_type;
16518 struct attribute *attr;
16519
16520 domain_type = die_type (die, cu);
16521
16522 /* The die_type call above may have already set the type for this DIE. */
16523 set_type = get_die_type (die, cu);
16524 if (set_type)
16525 return set_type;
16526
16527 set_type = create_set_type (NULL, domain_type);
16528
16529 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16530 if (attr)
16531 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16532
16533 return set_die_type (die, set_type, cu);
16534 }
16535
16536 /* A helper for read_common_block that creates a locexpr baton.
16537 SYM is the symbol which we are marking as computed.
16538 COMMON_DIE is the DIE for the common block.
16539 COMMON_LOC is the location expression attribute for the common
16540 block itself.
16541 MEMBER_LOC is the location expression attribute for the particular
16542 member of the common block that we are processing.
16543 CU is the CU from which the above come. */
16544
16545 static void
16546 mark_common_block_symbol_computed (struct symbol *sym,
16547 struct die_info *common_die,
16548 struct attribute *common_loc,
16549 struct attribute *member_loc,
16550 struct dwarf2_cu *cu)
16551 {
16552 struct dwarf2_per_objfile *dwarf2_per_objfile
16553 = cu->per_cu->dwarf2_per_objfile;
16554 struct objfile *objfile = dwarf2_per_objfile->objfile;
16555 struct dwarf2_locexpr_baton *baton;
16556 gdb_byte *ptr;
16557 unsigned int cu_off;
16558 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16559 LONGEST offset = 0;
16560
16561 gdb_assert (common_loc && member_loc);
16562 gdb_assert (attr_form_is_block (common_loc));
16563 gdb_assert (attr_form_is_block (member_loc)
16564 || attr_form_is_constant (member_loc));
16565
16566 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16567 baton->per_cu = cu->per_cu;
16568 gdb_assert (baton->per_cu);
16569
16570 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16571
16572 if (attr_form_is_constant (member_loc))
16573 {
16574 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16575 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16576 }
16577 else
16578 baton->size += DW_BLOCK (member_loc)->size;
16579
16580 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16581 baton->data = ptr;
16582
16583 *ptr++ = DW_OP_call4;
16584 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16585 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16586 ptr += 4;
16587
16588 if (attr_form_is_constant (member_loc))
16589 {
16590 *ptr++ = DW_OP_addr;
16591 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16592 ptr += cu->header.addr_size;
16593 }
16594 else
16595 {
16596 /* We have to copy the data here, because DW_OP_call4 will only
16597 use a DW_AT_location attribute. */
16598 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16599 ptr += DW_BLOCK (member_loc)->size;
16600 }
16601
16602 *ptr++ = DW_OP_plus;
16603 gdb_assert (ptr - baton->data == baton->size);
16604
16605 SYMBOL_LOCATION_BATON (sym) = baton;
16606 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16607 }
16608
16609 /* Create appropriate locally-scoped variables for all the
16610 DW_TAG_common_block entries. Also create a struct common_block
16611 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16612 is used to sepate the common blocks name namespace from regular
16613 variable names. */
16614
16615 static void
16616 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16617 {
16618 struct attribute *attr;
16619
16620 attr = dwarf2_attr (die, DW_AT_location, cu);
16621 if (attr)
16622 {
16623 /* Support the .debug_loc offsets. */
16624 if (attr_form_is_block (attr))
16625 {
16626 /* Ok. */
16627 }
16628 else if (attr_form_is_section_offset (attr))
16629 {
16630 dwarf2_complex_location_expr_complaint ();
16631 attr = NULL;
16632 }
16633 else
16634 {
16635 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16636 "common block member");
16637 attr = NULL;
16638 }
16639 }
16640
16641 if (die->child != NULL)
16642 {
16643 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16644 struct die_info *child_die;
16645 size_t n_entries = 0, size;
16646 struct common_block *common_block;
16647 struct symbol *sym;
16648
16649 for (child_die = die->child;
16650 child_die && child_die->tag;
16651 child_die = sibling_die (child_die))
16652 ++n_entries;
16653
16654 size = (sizeof (struct common_block)
16655 + (n_entries - 1) * sizeof (struct symbol *));
16656 common_block
16657 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16658 size);
16659 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16660 common_block->n_entries = 0;
16661
16662 for (child_die = die->child;
16663 child_die && child_die->tag;
16664 child_die = sibling_die (child_die))
16665 {
16666 /* Create the symbol in the DW_TAG_common_block block in the current
16667 symbol scope. */
16668 sym = new_symbol (child_die, NULL, cu);
16669 if (sym != NULL)
16670 {
16671 struct attribute *member_loc;
16672
16673 common_block->contents[common_block->n_entries++] = sym;
16674
16675 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16676 cu);
16677 if (member_loc)
16678 {
16679 /* GDB has handled this for a long time, but it is
16680 not specified by DWARF. It seems to have been
16681 emitted by gfortran at least as recently as:
16682 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16683 complaint (&symfile_complaints,
16684 _("Variable in common block has "
16685 "DW_AT_data_member_location "
16686 "- DIE at %s [in module %s]"),
16687 sect_offset_str (child_die->sect_off),
16688 objfile_name (objfile));
16689
16690 if (attr_form_is_section_offset (member_loc))
16691 dwarf2_complex_location_expr_complaint ();
16692 else if (attr_form_is_constant (member_loc)
16693 || attr_form_is_block (member_loc))
16694 {
16695 if (attr)
16696 mark_common_block_symbol_computed (sym, die, attr,
16697 member_loc, cu);
16698 }
16699 else
16700 dwarf2_complex_location_expr_complaint ();
16701 }
16702 }
16703 }
16704
16705 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16706 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16707 }
16708 }
16709
16710 /* Create a type for a C++ namespace. */
16711
16712 static struct type *
16713 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16714 {
16715 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16716 const char *previous_prefix, *name;
16717 int is_anonymous;
16718 struct type *type;
16719
16720 /* For extensions, reuse the type of the original namespace. */
16721 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16722 {
16723 struct die_info *ext_die;
16724 struct dwarf2_cu *ext_cu = cu;
16725
16726 ext_die = dwarf2_extension (die, &ext_cu);
16727 type = read_type_die (ext_die, ext_cu);
16728
16729 /* EXT_CU may not be the same as CU.
16730 Ensure TYPE is recorded with CU in die_type_hash. */
16731 return set_die_type (die, type, cu);
16732 }
16733
16734 name = namespace_name (die, &is_anonymous, cu);
16735
16736 /* Now build the name of the current namespace. */
16737
16738 previous_prefix = determine_prefix (die, cu);
16739 if (previous_prefix[0] != '\0')
16740 name = typename_concat (&objfile->objfile_obstack,
16741 previous_prefix, name, 0, cu);
16742
16743 /* Create the type. */
16744 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16745 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16746
16747 return set_die_type (die, type, cu);
16748 }
16749
16750 /* Read a namespace scope. */
16751
16752 static void
16753 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16754 {
16755 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16756 int is_anonymous;
16757
16758 /* Add a symbol associated to this if we haven't seen the namespace
16759 before. Also, add a using directive if it's an anonymous
16760 namespace. */
16761
16762 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16763 {
16764 struct type *type;
16765
16766 type = read_type_die (die, cu);
16767 new_symbol (die, type, cu);
16768
16769 namespace_name (die, &is_anonymous, cu);
16770 if (is_anonymous)
16771 {
16772 const char *previous_prefix = determine_prefix (die, cu);
16773
16774 std::vector<const char *> excludes;
16775 add_using_directive (using_directives (cu->language),
16776 previous_prefix, TYPE_NAME (type), NULL,
16777 NULL, excludes, 0, &objfile->objfile_obstack);
16778 }
16779 }
16780
16781 if (die->child != NULL)
16782 {
16783 struct die_info *child_die = die->child;
16784
16785 while (child_die && child_die->tag)
16786 {
16787 process_die (child_die, cu);
16788 child_die = sibling_die (child_die);
16789 }
16790 }
16791 }
16792
16793 /* Read a Fortran module as type. This DIE can be only a declaration used for
16794 imported module. Still we need that type as local Fortran "use ... only"
16795 declaration imports depend on the created type in determine_prefix. */
16796
16797 static struct type *
16798 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16799 {
16800 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16801 const char *module_name;
16802 struct type *type;
16803
16804 module_name = dwarf2_name (die, cu);
16805 if (!module_name)
16806 complaint (&symfile_complaints,
16807 _("DW_TAG_module has no name, offset %s"),
16808 sect_offset_str (die->sect_off));
16809 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16810
16811 /* determine_prefix uses TYPE_TAG_NAME. */
16812 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16813
16814 return set_die_type (die, type, cu);
16815 }
16816
16817 /* Read a Fortran module. */
16818
16819 static void
16820 read_module (struct die_info *die, struct dwarf2_cu *cu)
16821 {
16822 struct die_info *child_die = die->child;
16823 struct type *type;
16824
16825 type = read_type_die (die, cu);
16826 new_symbol (die, type, cu);
16827
16828 while (child_die && child_die->tag)
16829 {
16830 process_die (child_die, cu);
16831 child_die = sibling_die (child_die);
16832 }
16833 }
16834
16835 /* Return the name of the namespace represented by DIE. Set
16836 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16837 namespace. */
16838
16839 static const char *
16840 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16841 {
16842 struct die_info *current_die;
16843 const char *name = NULL;
16844
16845 /* Loop through the extensions until we find a name. */
16846
16847 for (current_die = die;
16848 current_die != NULL;
16849 current_die = dwarf2_extension (die, &cu))
16850 {
16851 /* We don't use dwarf2_name here so that we can detect the absence
16852 of a name -> anonymous namespace. */
16853 name = dwarf2_string_attr (die, DW_AT_name, cu);
16854
16855 if (name != NULL)
16856 break;
16857 }
16858
16859 /* Is it an anonymous namespace? */
16860
16861 *is_anonymous = (name == NULL);
16862 if (*is_anonymous)
16863 name = CP_ANONYMOUS_NAMESPACE_STR;
16864
16865 return name;
16866 }
16867
16868 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16869 the user defined type vector. */
16870
16871 static struct type *
16872 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16873 {
16874 struct gdbarch *gdbarch
16875 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16876 struct comp_unit_head *cu_header = &cu->header;
16877 struct type *type;
16878 struct attribute *attr_byte_size;
16879 struct attribute *attr_address_class;
16880 int byte_size, addr_class;
16881 struct type *target_type;
16882
16883 target_type = die_type (die, cu);
16884
16885 /* The die_type call above may have already set the type for this DIE. */
16886 type = get_die_type (die, cu);
16887 if (type)
16888 return type;
16889
16890 type = lookup_pointer_type (target_type);
16891
16892 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16893 if (attr_byte_size)
16894 byte_size = DW_UNSND (attr_byte_size);
16895 else
16896 byte_size = cu_header->addr_size;
16897
16898 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16899 if (attr_address_class)
16900 addr_class = DW_UNSND (attr_address_class);
16901 else
16902 addr_class = DW_ADDR_none;
16903
16904 /* If the pointer size or address class is different than the
16905 default, create a type variant marked as such and set the
16906 length accordingly. */
16907 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
16908 {
16909 if (gdbarch_address_class_type_flags_p (gdbarch))
16910 {
16911 int type_flags;
16912
16913 type_flags = gdbarch_address_class_type_flags
16914 (gdbarch, byte_size, addr_class);
16915 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16916 == 0);
16917 type = make_type_with_address_space (type, type_flags);
16918 }
16919 else if (TYPE_LENGTH (type) != byte_size)
16920 {
16921 complaint (&symfile_complaints,
16922 _("invalid pointer size %d"), byte_size);
16923 }
16924 else
16925 {
16926 /* Should we also complain about unhandled address classes? */
16927 }
16928 }
16929
16930 TYPE_LENGTH (type) = byte_size;
16931 return set_die_type (die, type, cu);
16932 }
16933
16934 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16935 the user defined type vector. */
16936
16937 static struct type *
16938 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16939 {
16940 struct type *type;
16941 struct type *to_type;
16942 struct type *domain;
16943
16944 to_type = die_type (die, cu);
16945 domain = die_containing_type (die, cu);
16946
16947 /* The calls above may have already set the type for this DIE. */
16948 type = get_die_type (die, cu);
16949 if (type)
16950 return type;
16951
16952 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16953 type = lookup_methodptr_type (to_type);
16954 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16955 {
16956 struct type *new_type
16957 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16958
16959 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16960 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16961 TYPE_VARARGS (to_type));
16962 type = lookup_methodptr_type (new_type);
16963 }
16964 else
16965 type = lookup_memberptr_type (to_type, domain);
16966
16967 return set_die_type (die, type, cu);
16968 }
16969
16970 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16971 the user defined type vector. */
16972
16973 static struct type *
16974 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16975 enum type_code refcode)
16976 {
16977 struct comp_unit_head *cu_header = &cu->header;
16978 struct type *type, *target_type;
16979 struct attribute *attr;
16980
16981 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16982
16983 target_type = die_type (die, cu);
16984
16985 /* The die_type call above may have already set the type for this DIE. */
16986 type = get_die_type (die, cu);
16987 if (type)
16988 return type;
16989
16990 type = lookup_reference_type (target_type, refcode);
16991 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16992 if (attr)
16993 {
16994 TYPE_LENGTH (type) = DW_UNSND (attr);
16995 }
16996 else
16997 {
16998 TYPE_LENGTH (type) = cu_header->addr_size;
16999 }
17000 return set_die_type (die, type, cu);
17001 }
17002
17003 /* Add the given cv-qualifiers to the element type of the array. GCC
17004 outputs DWARF type qualifiers that apply to an array, not the
17005 element type. But GDB relies on the array element type to carry
17006 the cv-qualifiers. This mimics section 6.7.3 of the C99
17007 specification. */
17008
17009 static struct type *
17010 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17011 struct type *base_type, int cnst, int voltl)
17012 {
17013 struct type *el_type, *inner_array;
17014
17015 base_type = copy_type (base_type);
17016 inner_array = base_type;
17017
17018 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17019 {
17020 TYPE_TARGET_TYPE (inner_array) =
17021 copy_type (TYPE_TARGET_TYPE (inner_array));
17022 inner_array = TYPE_TARGET_TYPE (inner_array);
17023 }
17024
17025 el_type = TYPE_TARGET_TYPE (inner_array);
17026 cnst |= TYPE_CONST (el_type);
17027 voltl |= TYPE_VOLATILE (el_type);
17028 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17029
17030 return set_die_type (die, base_type, cu);
17031 }
17032
17033 static struct type *
17034 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17035 {
17036 struct type *base_type, *cv_type;
17037
17038 base_type = die_type (die, cu);
17039
17040 /* The die_type call above may have already set the type for this DIE. */
17041 cv_type = get_die_type (die, cu);
17042 if (cv_type)
17043 return cv_type;
17044
17045 /* In case the const qualifier is applied to an array type, the element type
17046 is so qualified, not the array type (section 6.7.3 of C99). */
17047 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17048 return add_array_cv_type (die, cu, base_type, 1, 0);
17049
17050 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17051 return set_die_type (die, cv_type, cu);
17052 }
17053
17054 static struct type *
17055 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17056 {
17057 struct type *base_type, *cv_type;
17058
17059 base_type = die_type (die, cu);
17060
17061 /* The die_type call above may have already set the type for this DIE. */
17062 cv_type = get_die_type (die, cu);
17063 if (cv_type)
17064 return cv_type;
17065
17066 /* In case the volatile qualifier is applied to an array type, the
17067 element type is so qualified, not the array type (section 6.7.3
17068 of C99). */
17069 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17070 return add_array_cv_type (die, cu, base_type, 0, 1);
17071
17072 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17073 return set_die_type (die, cv_type, cu);
17074 }
17075
17076 /* Handle DW_TAG_restrict_type. */
17077
17078 static struct type *
17079 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17080 {
17081 struct type *base_type, *cv_type;
17082
17083 base_type = die_type (die, cu);
17084
17085 /* The die_type call above may have already set the type for this DIE. */
17086 cv_type = get_die_type (die, cu);
17087 if (cv_type)
17088 return cv_type;
17089
17090 cv_type = make_restrict_type (base_type);
17091 return set_die_type (die, cv_type, cu);
17092 }
17093
17094 /* Handle DW_TAG_atomic_type. */
17095
17096 static struct type *
17097 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17098 {
17099 struct type *base_type, *cv_type;
17100
17101 base_type = die_type (die, cu);
17102
17103 /* The die_type call above may have already set the type for this DIE. */
17104 cv_type = get_die_type (die, cu);
17105 if (cv_type)
17106 return cv_type;
17107
17108 cv_type = make_atomic_type (base_type);
17109 return set_die_type (die, cv_type, cu);
17110 }
17111
17112 /* Extract all information from a DW_TAG_string_type DIE and add to
17113 the user defined type vector. It isn't really a user defined type,
17114 but it behaves like one, with other DIE's using an AT_user_def_type
17115 attribute to reference it. */
17116
17117 static struct type *
17118 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17119 {
17120 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17121 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17122 struct type *type, *range_type, *index_type, *char_type;
17123 struct attribute *attr;
17124 unsigned int length;
17125
17126 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17127 if (attr)
17128 {
17129 length = DW_UNSND (attr);
17130 }
17131 else
17132 {
17133 /* Check for the DW_AT_byte_size attribute. */
17134 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17135 if (attr)
17136 {
17137 length = DW_UNSND (attr);
17138 }
17139 else
17140 {
17141 length = 1;
17142 }
17143 }
17144
17145 index_type = objfile_type (objfile)->builtin_int;
17146 range_type = create_static_range_type (NULL, index_type, 1, length);
17147 char_type = language_string_char_type (cu->language_defn, gdbarch);
17148 type = create_string_type (NULL, char_type, range_type);
17149
17150 return set_die_type (die, type, cu);
17151 }
17152
17153 /* Assuming that DIE corresponds to a function, returns nonzero
17154 if the function is prototyped. */
17155
17156 static int
17157 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17158 {
17159 struct attribute *attr;
17160
17161 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17162 if (attr && (DW_UNSND (attr) != 0))
17163 return 1;
17164
17165 /* The DWARF standard implies that the DW_AT_prototyped attribute
17166 is only meaninful for C, but the concept also extends to other
17167 languages that allow unprototyped functions (Eg: Objective C).
17168 For all other languages, assume that functions are always
17169 prototyped. */
17170 if (cu->language != language_c
17171 && cu->language != language_objc
17172 && cu->language != language_opencl)
17173 return 1;
17174
17175 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17176 prototyped and unprototyped functions; default to prototyped,
17177 since that is more common in modern code (and RealView warns
17178 about unprototyped functions). */
17179 if (producer_is_realview (cu->producer))
17180 return 1;
17181
17182 return 0;
17183 }
17184
17185 /* Handle DIES due to C code like:
17186
17187 struct foo
17188 {
17189 int (*funcp)(int a, long l);
17190 int b;
17191 };
17192
17193 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17194
17195 static struct type *
17196 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17197 {
17198 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17199 struct type *type; /* Type that this function returns. */
17200 struct type *ftype; /* Function that returns above type. */
17201 struct attribute *attr;
17202
17203 type = die_type (die, cu);
17204
17205 /* The die_type call above may have already set the type for this DIE. */
17206 ftype = get_die_type (die, cu);
17207 if (ftype)
17208 return ftype;
17209
17210 ftype = lookup_function_type (type);
17211
17212 if (prototyped_function_p (die, cu))
17213 TYPE_PROTOTYPED (ftype) = 1;
17214
17215 /* Store the calling convention in the type if it's available in
17216 the subroutine die. Otherwise set the calling convention to
17217 the default value DW_CC_normal. */
17218 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17219 if (attr)
17220 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17221 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17222 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17223 else
17224 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17225
17226 /* Record whether the function returns normally to its caller or not
17227 if the DWARF producer set that information. */
17228 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17229 if (attr && (DW_UNSND (attr) != 0))
17230 TYPE_NO_RETURN (ftype) = 1;
17231
17232 /* We need to add the subroutine type to the die immediately so
17233 we don't infinitely recurse when dealing with parameters
17234 declared as the same subroutine type. */
17235 set_die_type (die, ftype, cu);
17236
17237 if (die->child != NULL)
17238 {
17239 struct type *void_type = objfile_type (objfile)->builtin_void;
17240 struct die_info *child_die;
17241 int nparams, iparams;
17242
17243 /* Count the number of parameters.
17244 FIXME: GDB currently ignores vararg functions, but knows about
17245 vararg member functions. */
17246 nparams = 0;
17247 child_die = die->child;
17248 while (child_die && child_die->tag)
17249 {
17250 if (child_die->tag == DW_TAG_formal_parameter)
17251 nparams++;
17252 else if (child_die->tag == DW_TAG_unspecified_parameters)
17253 TYPE_VARARGS (ftype) = 1;
17254 child_die = sibling_die (child_die);
17255 }
17256
17257 /* Allocate storage for parameters and fill them in. */
17258 TYPE_NFIELDS (ftype) = nparams;
17259 TYPE_FIELDS (ftype) = (struct field *)
17260 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17261
17262 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17263 even if we error out during the parameters reading below. */
17264 for (iparams = 0; iparams < nparams; iparams++)
17265 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17266
17267 iparams = 0;
17268 child_die = die->child;
17269 while (child_die && child_die->tag)
17270 {
17271 if (child_die->tag == DW_TAG_formal_parameter)
17272 {
17273 struct type *arg_type;
17274
17275 /* DWARF version 2 has no clean way to discern C++
17276 static and non-static member functions. G++ helps
17277 GDB by marking the first parameter for non-static
17278 member functions (which is the this pointer) as
17279 artificial. We pass this information to
17280 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17281
17282 DWARF version 3 added DW_AT_object_pointer, which GCC
17283 4.5 does not yet generate. */
17284 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17285 if (attr)
17286 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17287 else
17288 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17289 arg_type = die_type (child_die, cu);
17290
17291 /* RealView does not mark THIS as const, which the testsuite
17292 expects. GCC marks THIS as const in method definitions,
17293 but not in the class specifications (GCC PR 43053). */
17294 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17295 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17296 {
17297 int is_this = 0;
17298 struct dwarf2_cu *arg_cu = cu;
17299 const char *name = dwarf2_name (child_die, cu);
17300
17301 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17302 if (attr)
17303 {
17304 /* If the compiler emits this, use it. */
17305 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17306 is_this = 1;
17307 }
17308 else if (name && strcmp (name, "this") == 0)
17309 /* Function definitions will have the argument names. */
17310 is_this = 1;
17311 else if (name == NULL && iparams == 0)
17312 /* Declarations may not have the names, so like
17313 elsewhere in GDB, assume an artificial first
17314 argument is "this". */
17315 is_this = 1;
17316
17317 if (is_this)
17318 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17319 arg_type, 0);
17320 }
17321
17322 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17323 iparams++;
17324 }
17325 child_die = sibling_die (child_die);
17326 }
17327 }
17328
17329 return ftype;
17330 }
17331
17332 static struct type *
17333 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17334 {
17335 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17336 const char *name = NULL;
17337 struct type *this_type, *target_type;
17338
17339 name = dwarf2_full_name (NULL, die, cu);
17340 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17341 TYPE_TARGET_STUB (this_type) = 1;
17342 set_die_type (die, this_type, cu);
17343 target_type = die_type (die, cu);
17344 if (target_type != this_type)
17345 TYPE_TARGET_TYPE (this_type) = target_type;
17346 else
17347 {
17348 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17349 spec and cause infinite loops in GDB. */
17350 complaint (&symfile_complaints,
17351 _("Self-referential DW_TAG_typedef "
17352 "- DIE at %s [in module %s]"),
17353 sect_offset_str (die->sect_off), objfile_name (objfile));
17354 TYPE_TARGET_TYPE (this_type) = NULL;
17355 }
17356 return this_type;
17357 }
17358
17359 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17360 (which may be different from NAME) to the architecture back-end to allow
17361 it to guess the correct format if necessary. */
17362
17363 static struct type *
17364 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17365 const char *name_hint)
17366 {
17367 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17368 const struct floatformat **format;
17369 struct type *type;
17370
17371 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17372 if (format)
17373 type = init_float_type (objfile, bits, name, format);
17374 else
17375 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17376
17377 return type;
17378 }
17379
17380 /* Find a representation of a given base type and install
17381 it in the TYPE field of the die. */
17382
17383 static struct type *
17384 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17385 {
17386 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17387 struct type *type;
17388 struct attribute *attr;
17389 int encoding = 0, bits = 0;
17390 const char *name;
17391
17392 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17393 if (attr)
17394 {
17395 encoding = DW_UNSND (attr);
17396 }
17397 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17398 if (attr)
17399 {
17400 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17401 }
17402 name = dwarf2_name (die, cu);
17403 if (!name)
17404 {
17405 complaint (&symfile_complaints,
17406 _("DW_AT_name missing from DW_TAG_base_type"));
17407 }
17408
17409 switch (encoding)
17410 {
17411 case DW_ATE_address:
17412 /* Turn DW_ATE_address into a void * pointer. */
17413 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17414 type = init_pointer_type (objfile, bits, name, type);
17415 break;
17416 case DW_ATE_boolean:
17417 type = init_boolean_type (objfile, bits, 1, name);
17418 break;
17419 case DW_ATE_complex_float:
17420 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
17421 type = init_complex_type (objfile, name, type);
17422 break;
17423 case DW_ATE_decimal_float:
17424 type = init_decfloat_type (objfile, bits, name);
17425 break;
17426 case DW_ATE_float:
17427 type = dwarf2_init_float_type (objfile, bits, name, name);
17428 break;
17429 case DW_ATE_signed:
17430 type = init_integer_type (objfile, bits, 0, name);
17431 break;
17432 case DW_ATE_unsigned:
17433 if (cu->language == language_fortran
17434 && name
17435 && startswith (name, "character("))
17436 type = init_character_type (objfile, bits, 1, name);
17437 else
17438 type = init_integer_type (objfile, bits, 1, name);
17439 break;
17440 case DW_ATE_signed_char:
17441 if (cu->language == language_ada || cu->language == language_m2
17442 || cu->language == language_pascal
17443 || cu->language == language_fortran)
17444 type = init_character_type (objfile, bits, 0, name);
17445 else
17446 type = init_integer_type (objfile, bits, 0, name);
17447 break;
17448 case DW_ATE_unsigned_char:
17449 if (cu->language == language_ada || cu->language == language_m2
17450 || cu->language == language_pascal
17451 || cu->language == language_fortran
17452 || cu->language == language_rust)
17453 type = init_character_type (objfile, bits, 1, name);
17454 else
17455 type = init_integer_type (objfile, bits, 1, name);
17456 break;
17457 case DW_ATE_UTF:
17458 {
17459 gdbarch *arch = get_objfile_arch (objfile);
17460
17461 if (bits == 16)
17462 type = builtin_type (arch)->builtin_char16;
17463 else if (bits == 32)
17464 type = builtin_type (arch)->builtin_char32;
17465 else
17466 {
17467 complaint (&symfile_complaints,
17468 _("unsupported DW_ATE_UTF bit size: '%d'"),
17469 bits);
17470 type = init_integer_type (objfile, bits, 1, name);
17471 }
17472 return set_die_type (die, type, cu);
17473 }
17474 break;
17475
17476 default:
17477 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17478 dwarf_type_encoding_name (encoding));
17479 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17480 break;
17481 }
17482
17483 if (name && strcmp (name, "char") == 0)
17484 TYPE_NOSIGN (type) = 1;
17485
17486 return set_die_type (die, type, cu);
17487 }
17488
17489 /* Parse dwarf attribute if it's a block, reference or constant and put the
17490 resulting value of the attribute into struct bound_prop.
17491 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17492
17493 static int
17494 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17495 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17496 {
17497 struct dwarf2_property_baton *baton;
17498 struct obstack *obstack
17499 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17500
17501 if (attr == NULL || prop == NULL)
17502 return 0;
17503
17504 if (attr_form_is_block (attr))
17505 {
17506 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17507 baton->referenced_type = NULL;
17508 baton->locexpr.per_cu = cu->per_cu;
17509 baton->locexpr.size = DW_BLOCK (attr)->size;
17510 baton->locexpr.data = DW_BLOCK (attr)->data;
17511 prop->data.baton = baton;
17512 prop->kind = PROP_LOCEXPR;
17513 gdb_assert (prop->data.baton != NULL);
17514 }
17515 else if (attr_form_is_ref (attr))
17516 {
17517 struct dwarf2_cu *target_cu = cu;
17518 struct die_info *target_die;
17519 struct attribute *target_attr;
17520
17521 target_die = follow_die_ref (die, attr, &target_cu);
17522 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17523 if (target_attr == NULL)
17524 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17525 target_cu);
17526 if (target_attr == NULL)
17527 return 0;
17528
17529 switch (target_attr->name)
17530 {
17531 case DW_AT_location:
17532 if (attr_form_is_section_offset (target_attr))
17533 {
17534 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17535 baton->referenced_type = die_type (target_die, target_cu);
17536 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17537 prop->data.baton = baton;
17538 prop->kind = PROP_LOCLIST;
17539 gdb_assert (prop->data.baton != NULL);
17540 }
17541 else if (attr_form_is_block (target_attr))
17542 {
17543 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17544 baton->referenced_type = die_type (target_die, target_cu);
17545 baton->locexpr.per_cu = cu->per_cu;
17546 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17547 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17548 prop->data.baton = baton;
17549 prop->kind = PROP_LOCEXPR;
17550 gdb_assert (prop->data.baton != NULL);
17551 }
17552 else
17553 {
17554 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17555 "dynamic property");
17556 return 0;
17557 }
17558 break;
17559 case DW_AT_data_member_location:
17560 {
17561 LONGEST offset;
17562
17563 if (!handle_data_member_location (target_die, target_cu,
17564 &offset))
17565 return 0;
17566
17567 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17568 baton->referenced_type = read_type_die (target_die->parent,
17569 target_cu);
17570 baton->offset_info.offset = offset;
17571 baton->offset_info.type = die_type (target_die, target_cu);
17572 prop->data.baton = baton;
17573 prop->kind = PROP_ADDR_OFFSET;
17574 break;
17575 }
17576 }
17577 }
17578 else if (attr_form_is_constant (attr))
17579 {
17580 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17581 prop->kind = PROP_CONST;
17582 }
17583 else
17584 {
17585 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17586 dwarf2_name (die, cu));
17587 return 0;
17588 }
17589
17590 return 1;
17591 }
17592
17593 /* Read the given DW_AT_subrange DIE. */
17594
17595 static struct type *
17596 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17597 {
17598 struct type *base_type, *orig_base_type;
17599 struct type *range_type;
17600 struct attribute *attr;
17601 struct dynamic_prop low, high;
17602 int low_default_is_valid;
17603 int high_bound_is_count = 0;
17604 const char *name;
17605 LONGEST negative_mask;
17606
17607 orig_base_type = die_type (die, cu);
17608 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17609 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17610 creating the range type, but we use the result of check_typedef
17611 when examining properties of the type. */
17612 base_type = check_typedef (orig_base_type);
17613
17614 /* The die_type call above may have already set the type for this DIE. */
17615 range_type = get_die_type (die, cu);
17616 if (range_type)
17617 return range_type;
17618
17619 low.kind = PROP_CONST;
17620 high.kind = PROP_CONST;
17621 high.data.const_val = 0;
17622
17623 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17624 omitting DW_AT_lower_bound. */
17625 switch (cu->language)
17626 {
17627 case language_c:
17628 case language_cplus:
17629 low.data.const_val = 0;
17630 low_default_is_valid = 1;
17631 break;
17632 case language_fortran:
17633 low.data.const_val = 1;
17634 low_default_is_valid = 1;
17635 break;
17636 case language_d:
17637 case language_objc:
17638 case language_rust:
17639 low.data.const_val = 0;
17640 low_default_is_valid = (cu->header.version >= 4);
17641 break;
17642 case language_ada:
17643 case language_m2:
17644 case language_pascal:
17645 low.data.const_val = 1;
17646 low_default_is_valid = (cu->header.version >= 4);
17647 break;
17648 default:
17649 low.data.const_val = 0;
17650 low_default_is_valid = 0;
17651 break;
17652 }
17653
17654 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17655 if (attr)
17656 attr_to_dynamic_prop (attr, die, cu, &low);
17657 else if (!low_default_is_valid)
17658 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
17659 "- DIE at %s [in module %s]"),
17660 sect_offset_str (die->sect_off),
17661 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17662
17663 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
17664 if (!attr_to_dynamic_prop (attr, die, cu, &high))
17665 {
17666 attr = dwarf2_attr (die, DW_AT_count, cu);
17667 if (attr_to_dynamic_prop (attr, die, cu, &high))
17668 {
17669 /* If bounds are constant do the final calculation here. */
17670 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17671 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17672 else
17673 high_bound_is_count = 1;
17674 }
17675 }
17676
17677 /* Dwarf-2 specifications explicitly allows to create subrange types
17678 without specifying a base type.
17679 In that case, the base type must be set to the type of
17680 the lower bound, upper bound or count, in that order, if any of these
17681 three attributes references an object that has a type.
17682 If no base type is found, the Dwarf-2 specifications say that
17683 a signed integer type of size equal to the size of an address should
17684 be used.
17685 For the following C code: `extern char gdb_int [];'
17686 GCC produces an empty range DIE.
17687 FIXME: muller/2010-05-28: Possible references to object for low bound,
17688 high bound or count are not yet handled by this code. */
17689 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17690 {
17691 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17692 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17693 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17694 struct type *int_type = objfile_type (objfile)->builtin_int;
17695
17696 /* Test "int", "long int", and "long long int" objfile types,
17697 and select the first one having a size above or equal to the
17698 architecture address size. */
17699 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17700 base_type = int_type;
17701 else
17702 {
17703 int_type = objfile_type (objfile)->builtin_long;
17704 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17705 base_type = int_type;
17706 else
17707 {
17708 int_type = objfile_type (objfile)->builtin_long_long;
17709 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17710 base_type = int_type;
17711 }
17712 }
17713 }
17714
17715 /* Normally, the DWARF producers are expected to use a signed
17716 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17717 But this is unfortunately not always the case, as witnessed
17718 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17719 is used instead. To work around that ambiguity, we treat
17720 the bounds as signed, and thus sign-extend their values, when
17721 the base type is signed. */
17722 negative_mask =
17723 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17724 if (low.kind == PROP_CONST
17725 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17726 low.data.const_val |= negative_mask;
17727 if (high.kind == PROP_CONST
17728 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17729 high.data.const_val |= negative_mask;
17730
17731 range_type = create_range_type (NULL, orig_base_type, &low, &high);
17732
17733 if (high_bound_is_count)
17734 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17735
17736 /* Ada expects an empty array on no boundary attributes. */
17737 if (attr == NULL && cu->language != language_ada)
17738 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17739
17740 name = dwarf2_name (die, cu);
17741 if (name)
17742 TYPE_NAME (range_type) = name;
17743
17744 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17745 if (attr)
17746 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17747
17748 set_die_type (die, range_type, cu);
17749
17750 /* set_die_type should be already done. */
17751 set_descriptive_type (range_type, die, cu);
17752
17753 return range_type;
17754 }
17755
17756 static struct type *
17757 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17758 {
17759 struct type *type;
17760
17761 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17762 NULL);
17763 TYPE_NAME (type) = dwarf2_name (die, cu);
17764
17765 /* In Ada, an unspecified type is typically used when the description
17766 of the type is defered to a different unit. When encountering
17767 such a type, we treat it as a stub, and try to resolve it later on,
17768 when needed. */
17769 if (cu->language == language_ada)
17770 TYPE_STUB (type) = 1;
17771
17772 return set_die_type (die, type, cu);
17773 }
17774
17775 /* Read a single die and all its descendents. Set the die's sibling
17776 field to NULL; set other fields in the die correctly, and set all
17777 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17778 location of the info_ptr after reading all of those dies. PARENT
17779 is the parent of the die in question. */
17780
17781 static struct die_info *
17782 read_die_and_children (const struct die_reader_specs *reader,
17783 const gdb_byte *info_ptr,
17784 const gdb_byte **new_info_ptr,
17785 struct die_info *parent)
17786 {
17787 struct die_info *die;
17788 const gdb_byte *cur_ptr;
17789 int has_children;
17790
17791 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
17792 if (die == NULL)
17793 {
17794 *new_info_ptr = cur_ptr;
17795 return NULL;
17796 }
17797 store_in_ref_table (die, reader->cu);
17798
17799 if (has_children)
17800 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17801 else
17802 {
17803 die->child = NULL;
17804 *new_info_ptr = cur_ptr;
17805 }
17806
17807 die->sibling = NULL;
17808 die->parent = parent;
17809 return die;
17810 }
17811
17812 /* Read a die, all of its descendents, and all of its siblings; set
17813 all of the fields of all of the dies correctly. Arguments are as
17814 in read_die_and_children. */
17815
17816 static struct die_info *
17817 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17818 const gdb_byte *info_ptr,
17819 const gdb_byte **new_info_ptr,
17820 struct die_info *parent)
17821 {
17822 struct die_info *first_die, *last_sibling;
17823 const gdb_byte *cur_ptr;
17824
17825 cur_ptr = info_ptr;
17826 first_die = last_sibling = NULL;
17827
17828 while (1)
17829 {
17830 struct die_info *die
17831 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17832
17833 if (die == NULL)
17834 {
17835 *new_info_ptr = cur_ptr;
17836 return first_die;
17837 }
17838
17839 if (!first_die)
17840 first_die = die;
17841 else
17842 last_sibling->sibling = die;
17843
17844 last_sibling = die;
17845 }
17846 }
17847
17848 /* Read a die, all of its descendents, and all of its siblings; set
17849 all of the fields of all of the dies correctly. Arguments are as
17850 in read_die_and_children.
17851 This the main entry point for reading a DIE and all its children. */
17852
17853 static struct die_info *
17854 read_die_and_siblings (const struct die_reader_specs *reader,
17855 const gdb_byte *info_ptr,
17856 const gdb_byte **new_info_ptr,
17857 struct die_info *parent)
17858 {
17859 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17860 new_info_ptr, parent);
17861
17862 if (dwarf_die_debug)
17863 {
17864 fprintf_unfiltered (gdb_stdlog,
17865 "Read die from %s@0x%x of %s:\n",
17866 get_section_name (reader->die_section),
17867 (unsigned) (info_ptr - reader->die_section->buffer),
17868 bfd_get_filename (reader->abfd));
17869 dump_die (die, dwarf_die_debug);
17870 }
17871
17872 return die;
17873 }
17874
17875 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17876 attributes.
17877 The caller is responsible for filling in the extra attributes
17878 and updating (*DIEP)->num_attrs.
17879 Set DIEP to point to a newly allocated die with its information,
17880 except for its child, sibling, and parent fields.
17881 Set HAS_CHILDREN to tell whether the die has children or not. */
17882
17883 static const gdb_byte *
17884 read_full_die_1 (const struct die_reader_specs *reader,
17885 struct die_info **diep, const gdb_byte *info_ptr,
17886 int *has_children, int num_extra_attrs)
17887 {
17888 unsigned int abbrev_number, bytes_read, i;
17889 struct abbrev_info *abbrev;
17890 struct die_info *die;
17891 struct dwarf2_cu *cu = reader->cu;
17892 bfd *abfd = reader->abfd;
17893
17894 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17895 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17896 info_ptr += bytes_read;
17897 if (!abbrev_number)
17898 {
17899 *diep = NULL;
17900 *has_children = 0;
17901 return info_ptr;
17902 }
17903
17904 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17905 if (!abbrev)
17906 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17907 abbrev_number,
17908 bfd_get_filename (abfd));
17909
17910 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17911 die->sect_off = sect_off;
17912 die->tag = abbrev->tag;
17913 die->abbrev = abbrev_number;
17914
17915 /* Make the result usable.
17916 The caller needs to update num_attrs after adding the extra
17917 attributes. */
17918 die->num_attrs = abbrev->num_attrs;
17919
17920 for (i = 0; i < abbrev->num_attrs; ++i)
17921 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17922 info_ptr);
17923
17924 *diep = die;
17925 *has_children = abbrev->has_children;
17926 return info_ptr;
17927 }
17928
17929 /* Read a die and all its attributes.
17930 Set DIEP to point to a newly allocated die with its information,
17931 except for its child, sibling, and parent fields.
17932 Set HAS_CHILDREN to tell whether the die has children or not. */
17933
17934 static const gdb_byte *
17935 read_full_die (const struct die_reader_specs *reader,
17936 struct die_info **diep, const gdb_byte *info_ptr,
17937 int *has_children)
17938 {
17939 const gdb_byte *result;
17940
17941 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
17942
17943 if (dwarf_die_debug)
17944 {
17945 fprintf_unfiltered (gdb_stdlog,
17946 "Read die from %s@0x%x of %s:\n",
17947 get_section_name (reader->die_section),
17948 (unsigned) (info_ptr - reader->die_section->buffer),
17949 bfd_get_filename (reader->abfd));
17950 dump_die (*diep, dwarf_die_debug);
17951 }
17952
17953 return result;
17954 }
17955 \f
17956 /* Abbreviation tables.
17957
17958 In DWARF version 2, the description of the debugging information is
17959 stored in a separate .debug_abbrev section. Before we read any
17960 dies from a section we read in all abbreviations and install them
17961 in a hash table. */
17962
17963 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
17964
17965 struct abbrev_info *
17966 abbrev_table::alloc_abbrev ()
17967 {
17968 struct abbrev_info *abbrev;
17969
17970 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
17971 memset (abbrev, 0, sizeof (struct abbrev_info));
17972
17973 return abbrev;
17974 }
17975
17976 /* Add an abbreviation to the table. */
17977
17978 void
17979 abbrev_table::add_abbrev (unsigned int abbrev_number,
17980 struct abbrev_info *abbrev)
17981 {
17982 unsigned int hash_number;
17983
17984 hash_number = abbrev_number % ABBREV_HASH_SIZE;
17985 abbrev->next = m_abbrevs[hash_number];
17986 m_abbrevs[hash_number] = abbrev;
17987 }
17988
17989 /* Look up an abbrev in the table.
17990 Returns NULL if the abbrev is not found. */
17991
17992 struct abbrev_info *
17993 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
17994 {
17995 unsigned int hash_number;
17996 struct abbrev_info *abbrev;
17997
17998 hash_number = abbrev_number % ABBREV_HASH_SIZE;
17999 abbrev = m_abbrevs[hash_number];
18000
18001 while (abbrev)
18002 {
18003 if (abbrev->number == abbrev_number)
18004 return abbrev;
18005 abbrev = abbrev->next;
18006 }
18007 return NULL;
18008 }
18009
18010 /* Read in an abbrev table. */
18011
18012 static abbrev_table_up
18013 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18014 struct dwarf2_section_info *section,
18015 sect_offset sect_off)
18016 {
18017 struct objfile *objfile = dwarf2_per_objfile->objfile;
18018 bfd *abfd = get_section_bfd_owner (section);
18019 const gdb_byte *abbrev_ptr;
18020 struct abbrev_info *cur_abbrev;
18021 unsigned int abbrev_number, bytes_read, abbrev_name;
18022 unsigned int abbrev_form;
18023 struct attr_abbrev *cur_attrs;
18024 unsigned int allocated_attrs;
18025
18026 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18027
18028 dwarf2_read_section (objfile, section);
18029 abbrev_ptr = section->buffer + to_underlying (sect_off);
18030 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18031 abbrev_ptr += bytes_read;
18032
18033 allocated_attrs = ATTR_ALLOC_CHUNK;
18034 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18035
18036 /* Loop until we reach an abbrev number of 0. */
18037 while (abbrev_number)
18038 {
18039 cur_abbrev = abbrev_table->alloc_abbrev ();
18040
18041 /* read in abbrev header */
18042 cur_abbrev->number = abbrev_number;
18043 cur_abbrev->tag
18044 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18045 abbrev_ptr += bytes_read;
18046 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18047 abbrev_ptr += 1;
18048
18049 /* now read in declarations */
18050 for (;;)
18051 {
18052 LONGEST implicit_const;
18053
18054 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18055 abbrev_ptr += bytes_read;
18056 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18057 abbrev_ptr += bytes_read;
18058 if (abbrev_form == DW_FORM_implicit_const)
18059 {
18060 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18061 &bytes_read);
18062 abbrev_ptr += bytes_read;
18063 }
18064 else
18065 {
18066 /* Initialize it due to a false compiler warning. */
18067 implicit_const = -1;
18068 }
18069
18070 if (abbrev_name == 0)
18071 break;
18072
18073 if (cur_abbrev->num_attrs == allocated_attrs)
18074 {
18075 allocated_attrs += ATTR_ALLOC_CHUNK;
18076 cur_attrs
18077 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18078 }
18079
18080 cur_attrs[cur_abbrev->num_attrs].name
18081 = (enum dwarf_attribute) abbrev_name;
18082 cur_attrs[cur_abbrev->num_attrs].form
18083 = (enum dwarf_form) abbrev_form;
18084 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18085 ++cur_abbrev->num_attrs;
18086 }
18087
18088 cur_abbrev->attrs =
18089 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18090 cur_abbrev->num_attrs);
18091 memcpy (cur_abbrev->attrs, cur_attrs,
18092 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18093
18094 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18095
18096 /* Get next abbreviation.
18097 Under Irix6 the abbreviations for a compilation unit are not
18098 always properly terminated with an abbrev number of 0.
18099 Exit loop if we encounter an abbreviation which we have
18100 already read (which means we are about to read the abbreviations
18101 for the next compile unit) or if the end of the abbreviation
18102 table is reached. */
18103 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18104 break;
18105 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18106 abbrev_ptr += bytes_read;
18107 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18108 break;
18109 }
18110
18111 xfree (cur_attrs);
18112 return abbrev_table;
18113 }
18114
18115 /* Returns nonzero if TAG represents a type that we might generate a partial
18116 symbol for. */
18117
18118 static int
18119 is_type_tag_for_partial (int tag)
18120 {
18121 switch (tag)
18122 {
18123 #if 0
18124 /* Some types that would be reasonable to generate partial symbols for,
18125 that we don't at present. */
18126 case DW_TAG_array_type:
18127 case DW_TAG_file_type:
18128 case DW_TAG_ptr_to_member_type:
18129 case DW_TAG_set_type:
18130 case DW_TAG_string_type:
18131 case DW_TAG_subroutine_type:
18132 #endif
18133 case DW_TAG_base_type:
18134 case DW_TAG_class_type:
18135 case DW_TAG_interface_type:
18136 case DW_TAG_enumeration_type:
18137 case DW_TAG_structure_type:
18138 case DW_TAG_subrange_type:
18139 case DW_TAG_typedef:
18140 case DW_TAG_union_type:
18141 return 1;
18142 default:
18143 return 0;
18144 }
18145 }
18146
18147 /* Load all DIEs that are interesting for partial symbols into memory. */
18148
18149 static struct partial_die_info *
18150 load_partial_dies (const struct die_reader_specs *reader,
18151 const gdb_byte *info_ptr, int building_psymtab)
18152 {
18153 struct dwarf2_cu *cu = reader->cu;
18154 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18155 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18156 unsigned int bytes_read;
18157 unsigned int load_all = 0;
18158 int nesting_level = 1;
18159
18160 parent_die = NULL;
18161 last_die = NULL;
18162
18163 gdb_assert (cu->per_cu != NULL);
18164 if (cu->per_cu->load_all_dies)
18165 load_all = 1;
18166
18167 cu->partial_dies
18168 = htab_create_alloc_ex (cu->header.length / 12,
18169 partial_die_hash,
18170 partial_die_eq,
18171 NULL,
18172 &cu->comp_unit_obstack,
18173 hashtab_obstack_allocate,
18174 dummy_obstack_deallocate);
18175
18176 while (1)
18177 {
18178 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18179
18180 /* A NULL abbrev means the end of a series of children. */
18181 if (abbrev == NULL)
18182 {
18183 if (--nesting_level == 0)
18184 return first_die;
18185
18186 info_ptr += bytes_read;
18187 last_die = parent_die;
18188 parent_die = parent_die->die_parent;
18189 continue;
18190 }
18191
18192 /* Check for template arguments. We never save these; if
18193 they're seen, we just mark the parent, and go on our way. */
18194 if (parent_die != NULL
18195 && cu->language == language_cplus
18196 && (abbrev->tag == DW_TAG_template_type_param
18197 || abbrev->tag == DW_TAG_template_value_param))
18198 {
18199 parent_die->has_template_arguments = 1;
18200
18201 if (!load_all)
18202 {
18203 /* We don't need a partial DIE for the template argument. */
18204 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18205 continue;
18206 }
18207 }
18208
18209 /* We only recurse into c++ subprograms looking for template arguments.
18210 Skip their other children. */
18211 if (!load_all
18212 && cu->language == language_cplus
18213 && parent_die != NULL
18214 && parent_die->tag == DW_TAG_subprogram)
18215 {
18216 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18217 continue;
18218 }
18219
18220 /* Check whether this DIE is interesting enough to save. Normally
18221 we would not be interested in members here, but there may be
18222 later variables referencing them via DW_AT_specification (for
18223 static members). */
18224 if (!load_all
18225 && !is_type_tag_for_partial (abbrev->tag)
18226 && abbrev->tag != DW_TAG_constant
18227 && abbrev->tag != DW_TAG_enumerator
18228 && abbrev->tag != DW_TAG_subprogram
18229 && abbrev->tag != DW_TAG_inlined_subroutine
18230 && abbrev->tag != DW_TAG_lexical_block
18231 && abbrev->tag != DW_TAG_variable
18232 && abbrev->tag != DW_TAG_namespace
18233 && abbrev->tag != DW_TAG_module
18234 && abbrev->tag != DW_TAG_member
18235 && abbrev->tag != DW_TAG_imported_unit
18236 && abbrev->tag != DW_TAG_imported_declaration)
18237 {
18238 /* Otherwise we skip to the next sibling, if any. */
18239 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18240 continue;
18241 }
18242
18243 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18244 abbrev);
18245
18246 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18247
18248 /* This two-pass algorithm for processing partial symbols has a
18249 high cost in cache pressure. Thus, handle some simple cases
18250 here which cover the majority of C partial symbols. DIEs
18251 which neither have specification tags in them, nor could have
18252 specification tags elsewhere pointing at them, can simply be
18253 processed and discarded.
18254
18255 This segment is also optional; scan_partial_symbols and
18256 add_partial_symbol will handle these DIEs if we chain
18257 them in normally. When compilers which do not emit large
18258 quantities of duplicate debug information are more common,
18259 this code can probably be removed. */
18260
18261 /* Any complete simple types at the top level (pretty much all
18262 of them, for a language without namespaces), can be processed
18263 directly. */
18264 if (parent_die == NULL
18265 && pdi.has_specification == 0
18266 && pdi.is_declaration == 0
18267 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18268 || pdi.tag == DW_TAG_base_type
18269 || pdi.tag == DW_TAG_subrange_type))
18270 {
18271 if (building_psymtab && pdi.name != NULL)
18272 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18273 VAR_DOMAIN, LOC_TYPEDEF,
18274 &objfile->static_psymbols,
18275 0, cu->language, objfile);
18276 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18277 continue;
18278 }
18279
18280 /* The exception for DW_TAG_typedef with has_children above is
18281 a workaround of GCC PR debug/47510. In the case of this complaint
18282 type_name_no_tag_or_error will error on such types later.
18283
18284 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18285 it could not find the child DIEs referenced later, this is checked
18286 above. In correct DWARF DW_TAG_typedef should have no children. */
18287
18288 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18289 complaint (&symfile_complaints,
18290 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18291 "- DIE at %s [in module %s]"),
18292 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18293
18294 /* If we're at the second level, and we're an enumerator, and
18295 our parent has no specification (meaning possibly lives in a
18296 namespace elsewhere), then we can add the partial symbol now
18297 instead of queueing it. */
18298 if (pdi.tag == DW_TAG_enumerator
18299 && parent_die != NULL
18300 && parent_die->die_parent == NULL
18301 && parent_die->tag == DW_TAG_enumeration_type
18302 && parent_die->has_specification == 0)
18303 {
18304 if (pdi.name == NULL)
18305 complaint (&symfile_complaints,
18306 _("malformed enumerator DIE ignored"));
18307 else if (building_psymtab)
18308 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18309 VAR_DOMAIN, LOC_CONST,
18310 cu->language == language_cplus
18311 ? &objfile->global_psymbols
18312 : &objfile->static_psymbols,
18313 0, cu->language, objfile);
18314
18315 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18316 continue;
18317 }
18318
18319 struct partial_die_info *part_die
18320 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18321
18322 /* We'll save this DIE so link it in. */
18323 part_die->die_parent = parent_die;
18324 part_die->die_sibling = NULL;
18325 part_die->die_child = NULL;
18326
18327 if (last_die && last_die == parent_die)
18328 last_die->die_child = part_die;
18329 else if (last_die)
18330 last_die->die_sibling = part_die;
18331
18332 last_die = part_die;
18333
18334 if (first_die == NULL)
18335 first_die = part_die;
18336
18337 /* Maybe add the DIE to the hash table. Not all DIEs that we
18338 find interesting need to be in the hash table, because we
18339 also have the parent/sibling/child chains; only those that we
18340 might refer to by offset later during partial symbol reading.
18341
18342 For now this means things that might have be the target of a
18343 DW_AT_specification, DW_AT_abstract_origin, or
18344 DW_AT_extension. DW_AT_extension will refer only to
18345 namespaces; DW_AT_abstract_origin refers to functions (and
18346 many things under the function DIE, but we do not recurse
18347 into function DIEs during partial symbol reading) and
18348 possibly variables as well; DW_AT_specification refers to
18349 declarations. Declarations ought to have the DW_AT_declaration
18350 flag. It happens that GCC forgets to put it in sometimes, but
18351 only for functions, not for types.
18352
18353 Adding more things than necessary to the hash table is harmless
18354 except for the performance cost. Adding too few will result in
18355 wasted time in find_partial_die, when we reread the compilation
18356 unit with load_all_dies set. */
18357
18358 if (load_all
18359 || abbrev->tag == DW_TAG_constant
18360 || abbrev->tag == DW_TAG_subprogram
18361 || abbrev->tag == DW_TAG_variable
18362 || abbrev->tag == DW_TAG_namespace
18363 || part_die->is_declaration)
18364 {
18365 void **slot;
18366
18367 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18368 to_underlying (part_die->sect_off),
18369 INSERT);
18370 *slot = part_die;
18371 }
18372
18373 /* For some DIEs we want to follow their children (if any). For C
18374 we have no reason to follow the children of structures; for other
18375 languages we have to, so that we can get at method physnames
18376 to infer fully qualified class names, for DW_AT_specification,
18377 and for C++ template arguments. For C++, we also look one level
18378 inside functions to find template arguments (if the name of the
18379 function does not already contain the template arguments).
18380
18381 For Ada, we need to scan the children of subprograms and lexical
18382 blocks as well because Ada allows the definition of nested
18383 entities that could be interesting for the debugger, such as
18384 nested subprograms for instance. */
18385 if (last_die->has_children
18386 && (load_all
18387 || last_die->tag == DW_TAG_namespace
18388 || last_die->tag == DW_TAG_module
18389 || last_die->tag == DW_TAG_enumeration_type
18390 || (cu->language == language_cplus
18391 && last_die->tag == DW_TAG_subprogram
18392 && (last_die->name == NULL
18393 || strchr (last_die->name, '<') == NULL))
18394 || (cu->language != language_c
18395 && (last_die->tag == DW_TAG_class_type
18396 || last_die->tag == DW_TAG_interface_type
18397 || last_die->tag == DW_TAG_structure_type
18398 || last_die->tag == DW_TAG_union_type))
18399 || (cu->language == language_ada
18400 && (last_die->tag == DW_TAG_subprogram
18401 || last_die->tag == DW_TAG_lexical_block))))
18402 {
18403 nesting_level++;
18404 parent_die = last_die;
18405 continue;
18406 }
18407
18408 /* Otherwise we skip to the next sibling, if any. */
18409 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18410
18411 /* Back to the top, do it again. */
18412 }
18413 }
18414
18415 partial_die_info::partial_die_info (sect_offset sect_off_,
18416 struct abbrev_info *abbrev)
18417 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18418 {
18419 }
18420
18421 /* Read a minimal amount of information into the minimal die structure.
18422 INFO_PTR should point just after the initial uleb128 of a DIE. */
18423
18424 const gdb_byte *
18425 partial_die_info::read (const struct die_reader_specs *reader,
18426 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18427 {
18428 struct dwarf2_cu *cu = reader->cu;
18429 struct dwarf2_per_objfile *dwarf2_per_objfile
18430 = cu->per_cu->dwarf2_per_objfile;
18431 unsigned int i;
18432 int has_low_pc_attr = 0;
18433 int has_high_pc_attr = 0;
18434 int high_pc_relative = 0;
18435
18436 for (i = 0; i < abbrev.num_attrs; ++i)
18437 {
18438 struct attribute attr;
18439
18440 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18441
18442 /* Store the data if it is of an attribute we want to keep in a
18443 partial symbol table. */
18444 switch (attr.name)
18445 {
18446 case DW_AT_name:
18447 switch (tag)
18448 {
18449 case DW_TAG_compile_unit:
18450 case DW_TAG_partial_unit:
18451 case DW_TAG_type_unit:
18452 /* Compilation units have a DW_AT_name that is a filename, not
18453 a source language identifier. */
18454 case DW_TAG_enumeration_type:
18455 case DW_TAG_enumerator:
18456 /* These tags always have simple identifiers already; no need
18457 to canonicalize them. */
18458 name = DW_STRING (&attr);
18459 break;
18460 default:
18461 {
18462 struct objfile *objfile = dwarf2_per_objfile->objfile;
18463
18464 name
18465 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18466 &objfile->per_bfd->storage_obstack);
18467 }
18468 break;
18469 }
18470 break;
18471 case DW_AT_linkage_name:
18472 case DW_AT_MIPS_linkage_name:
18473 /* Note that both forms of linkage name might appear. We
18474 assume they will be the same, and we only store the last
18475 one we see. */
18476 if (cu->language == language_ada)
18477 name = DW_STRING (&attr);
18478 linkage_name = DW_STRING (&attr);
18479 break;
18480 case DW_AT_low_pc:
18481 has_low_pc_attr = 1;
18482 lowpc = attr_value_as_address (&attr);
18483 break;
18484 case DW_AT_high_pc:
18485 has_high_pc_attr = 1;
18486 highpc = attr_value_as_address (&attr);
18487 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18488 high_pc_relative = 1;
18489 break;
18490 case DW_AT_location:
18491 /* Support the .debug_loc offsets. */
18492 if (attr_form_is_block (&attr))
18493 {
18494 d.locdesc = DW_BLOCK (&attr);
18495 }
18496 else if (attr_form_is_section_offset (&attr))
18497 {
18498 dwarf2_complex_location_expr_complaint ();
18499 }
18500 else
18501 {
18502 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18503 "partial symbol information");
18504 }
18505 break;
18506 case DW_AT_external:
18507 is_external = DW_UNSND (&attr);
18508 break;
18509 case DW_AT_declaration:
18510 is_declaration = DW_UNSND (&attr);
18511 break;
18512 case DW_AT_type:
18513 has_type = 1;
18514 break;
18515 case DW_AT_abstract_origin:
18516 case DW_AT_specification:
18517 case DW_AT_extension:
18518 has_specification = 1;
18519 spec_offset = dwarf2_get_ref_die_offset (&attr);
18520 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18521 || cu->per_cu->is_dwz);
18522 break;
18523 case DW_AT_sibling:
18524 /* Ignore absolute siblings, they might point outside of
18525 the current compile unit. */
18526 if (attr.form == DW_FORM_ref_addr)
18527 complaint (&symfile_complaints,
18528 _("ignoring absolute DW_AT_sibling"));
18529 else
18530 {
18531 const gdb_byte *buffer = reader->buffer;
18532 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18533 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18534
18535 if (sibling_ptr < info_ptr)
18536 complaint (&symfile_complaints,
18537 _("DW_AT_sibling points backwards"));
18538 else if (sibling_ptr > reader->buffer_end)
18539 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18540 else
18541 sibling = sibling_ptr;
18542 }
18543 break;
18544 case DW_AT_byte_size:
18545 has_byte_size = 1;
18546 break;
18547 case DW_AT_const_value:
18548 has_const_value = 1;
18549 break;
18550 case DW_AT_calling_convention:
18551 /* DWARF doesn't provide a way to identify a program's source-level
18552 entry point. DW_AT_calling_convention attributes are only meant
18553 to describe functions' calling conventions.
18554
18555 However, because it's a necessary piece of information in
18556 Fortran, and before DWARF 4 DW_CC_program was the only
18557 piece of debugging information whose definition refers to
18558 a 'main program' at all, several compilers marked Fortran
18559 main programs with DW_CC_program --- even when those
18560 functions use the standard calling conventions.
18561
18562 Although DWARF now specifies a way to provide this
18563 information, we support this practice for backward
18564 compatibility. */
18565 if (DW_UNSND (&attr) == DW_CC_program
18566 && cu->language == language_fortran)
18567 main_subprogram = 1;
18568 break;
18569 case DW_AT_inline:
18570 if (DW_UNSND (&attr) == DW_INL_inlined
18571 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18572 may_be_inlined = 1;
18573 break;
18574
18575 case DW_AT_import:
18576 if (tag == DW_TAG_imported_unit)
18577 {
18578 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18579 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18580 || cu->per_cu->is_dwz);
18581 }
18582 break;
18583
18584 case DW_AT_main_subprogram:
18585 main_subprogram = DW_UNSND (&attr);
18586 break;
18587
18588 default:
18589 break;
18590 }
18591 }
18592
18593 if (high_pc_relative)
18594 highpc += lowpc;
18595
18596 if (has_low_pc_attr && has_high_pc_attr)
18597 {
18598 /* When using the GNU linker, .gnu.linkonce. sections are used to
18599 eliminate duplicate copies of functions and vtables and such.
18600 The linker will arbitrarily choose one and discard the others.
18601 The AT_*_pc values for such functions refer to local labels in
18602 these sections. If the section from that file was discarded, the
18603 labels are not in the output, so the relocs get a value of 0.
18604 If this is a discarded function, mark the pc bounds as invalid,
18605 so that GDB will ignore it. */
18606 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18607 {
18608 struct objfile *objfile = dwarf2_per_objfile->objfile;
18609 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18610
18611 complaint (&symfile_complaints,
18612 _("DW_AT_low_pc %s is zero "
18613 "for DIE at %s [in module %s]"),
18614 paddress (gdbarch, lowpc),
18615 sect_offset_str (sect_off),
18616 objfile_name (objfile));
18617 }
18618 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18619 else if (lowpc >= highpc)
18620 {
18621 struct objfile *objfile = dwarf2_per_objfile->objfile;
18622 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18623
18624 complaint (&symfile_complaints,
18625 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18626 "for DIE at %s [in module %s]"),
18627 paddress (gdbarch, lowpc),
18628 paddress (gdbarch, highpc),
18629 sect_offset_str (sect_off),
18630 objfile_name (objfile));
18631 }
18632 else
18633 has_pc_info = 1;
18634 }
18635
18636 return info_ptr;
18637 }
18638
18639 /* Find a cached partial DIE at OFFSET in CU. */
18640
18641 struct partial_die_info *
18642 dwarf2_cu::find_partial_die (sect_offset sect_off)
18643 {
18644 struct partial_die_info *lookup_die = NULL;
18645 struct partial_die_info part_die (sect_off);
18646
18647 lookup_die = ((struct partial_die_info *)
18648 htab_find_with_hash (partial_dies, &part_die,
18649 to_underlying (sect_off)));
18650
18651 return lookup_die;
18652 }
18653
18654 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18655 except in the case of .debug_types DIEs which do not reference
18656 outside their CU (they do however referencing other types via
18657 DW_FORM_ref_sig8). */
18658
18659 static struct partial_die_info *
18660 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18661 {
18662 struct dwarf2_per_objfile *dwarf2_per_objfile
18663 = cu->per_cu->dwarf2_per_objfile;
18664 struct objfile *objfile = dwarf2_per_objfile->objfile;
18665 struct dwarf2_per_cu_data *per_cu = NULL;
18666 struct partial_die_info *pd = NULL;
18667
18668 if (offset_in_dwz == cu->per_cu->is_dwz
18669 && offset_in_cu_p (&cu->header, sect_off))
18670 {
18671 pd = cu->find_partial_die (sect_off);
18672 if (pd != NULL)
18673 return pd;
18674 /* We missed recording what we needed.
18675 Load all dies and try again. */
18676 per_cu = cu->per_cu;
18677 }
18678 else
18679 {
18680 /* TUs don't reference other CUs/TUs (except via type signatures). */
18681 if (cu->per_cu->is_debug_types)
18682 {
18683 error (_("Dwarf Error: Type Unit at offset %s contains"
18684 " external reference to offset %s [in module %s].\n"),
18685 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18686 bfd_get_filename (objfile->obfd));
18687 }
18688 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18689 dwarf2_per_objfile);
18690
18691 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18692 load_partial_comp_unit (per_cu);
18693
18694 per_cu->cu->last_used = 0;
18695 pd = per_cu->cu->find_partial_die (sect_off);
18696 }
18697
18698 /* If we didn't find it, and not all dies have been loaded,
18699 load them all and try again. */
18700
18701 if (pd == NULL && per_cu->load_all_dies == 0)
18702 {
18703 per_cu->load_all_dies = 1;
18704
18705 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18706 THIS_CU->cu may already be in use. So we can't just free it and
18707 replace its DIEs with the ones we read in. Instead, we leave those
18708 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18709 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18710 set. */
18711 load_partial_comp_unit (per_cu);
18712
18713 pd = per_cu->cu->find_partial_die (sect_off);
18714 }
18715
18716 if (pd == NULL)
18717 internal_error (__FILE__, __LINE__,
18718 _("could not find partial DIE %s "
18719 "in cache [from module %s]\n"),
18720 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18721 return pd;
18722 }
18723
18724 /* See if we can figure out if the class lives in a namespace. We do
18725 this by looking for a member function; its demangled name will
18726 contain namespace info, if there is any. */
18727
18728 static void
18729 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18730 struct dwarf2_cu *cu)
18731 {
18732 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18733 what template types look like, because the demangler
18734 frequently doesn't give the same name as the debug info. We
18735 could fix this by only using the demangled name to get the
18736 prefix (but see comment in read_structure_type). */
18737
18738 struct partial_die_info *real_pdi;
18739 struct partial_die_info *child_pdi;
18740
18741 /* If this DIE (this DIE's specification, if any) has a parent, then
18742 we should not do this. We'll prepend the parent's fully qualified
18743 name when we create the partial symbol. */
18744
18745 real_pdi = struct_pdi;
18746 while (real_pdi->has_specification)
18747 real_pdi = find_partial_die (real_pdi->spec_offset,
18748 real_pdi->spec_is_dwz, cu);
18749
18750 if (real_pdi->die_parent != NULL)
18751 return;
18752
18753 for (child_pdi = struct_pdi->die_child;
18754 child_pdi != NULL;
18755 child_pdi = child_pdi->die_sibling)
18756 {
18757 if (child_pdi->tag == DW_TAG_subprogram
18758 && child_pdi->linkage_name != NULL)
18759 {
18760 char *actual_class_name
18761 = language_class_name_from_physname (cu->language_defn,
18762 child_pdi->linkage_name);
18763 if (actual_class_name != NULL)
18764 {
18765 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18766 struct_pdi->name
18767 = ((const char *)
18768 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18769 actual_class_name,
18770 strlen (actual_class_name)));
18771 xfree (actual_class_name);
18772 }
18773 break;
18774 }
18775 }
18776 }
18777
18778 void
18779 partial_die_info::fixup (struct dwarf2_cu *cu)
18780 {
18781 /* Once we've fixed up a die, there's no point in doing so again.
18782 This also avoids a memory leak if we were to call
18783 guess_partial_die_structure_name multiple times. */
18784 if (fixup_called)
18785 return;
18786
18787 /* If we found a reference attribute and the DIE has no name, try
18788 to find a name in the referred to DIE. */
18789
18790 if (name == NULL && has_specification)
18791 {
18792 struct partial_die_info *spec_die;
18793
18794 spec_die = find_partial_die (spec_offset, spec_is_dwz, cu);
18795
18796 spec_die->fixup (cu);
18797
18798 if (spec_die->name)
18799 {
18800 name = spec_die->name;
18801
18802 /* Copy DW_AT_external attribute if it is set. */
18803 if (spec_die->is_external)
18804 is_external = spec_die->is_external;
18805 }
18806 }
18807
18808 /* Set default names for some unnamed DIEs. */
18809
18810 if (name == NULL && tag == DW_TAG_namespace)
18811 name = CP_ANONYMOUS_NAMESPACE_STR;
18812
18813 /* If there is no parent die to provide a namespace, and there are
18814 children, see if we can determine the namespace from their linkage
18815 name. */
18816 if (cu->language == language_cplus
18817 && !VEC_empty (dwarf2_section_info_def,
18818 cu->per_cu->dwarf2_per_objfile->types)
18819 && die_parent == NULL
18820 && has_children
18821 && (tag == DW_TAG_class_type
18822 || tag == DW_TAG_structure_type
18823 || tag == DW_TAG_union_type))
18824 guess_partial_die_structure_name (this, cu);
18825
18826 /* GCC might emit a nameless struct or union that has a linkage
18827 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18828 if (name == NULL
18829 && (tag == DW_TAG_class_type
18830 || tag == DW_TAG_interface_type
18831 || tag == DW_TAG_structure_type
18832 || tag == DW_TAG_union_type)
18833 && linkage_name != NULL)
18834 {
18835 char *demangled;
18836
18837 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
18838 if (demangled)
18839 {
18840 const char *base;
18841
18842 /* Strip any leading namespaces/classes, keep only the base name.
18843 DW_AT_name for named DIEs does not contain the prefixes. */
18844 base = strrchr (demangled, ':');
18845 if (base && base > demangled && base[-1] == ':')
18846 base++;
18847 else
18848 base = demangled;
18849
18850 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18851 name
18852 = ((const char *)
18853 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18854 base, strlen (base)));
18855 xfree (demangled);
18856 }
18857 }
18858
18859 fixup_called = 1;
18860 }
18861
18862 /* Read an attribute value described by an attribute form. */
18863
18864 static const gdb_byte *
18865 read_attribute_value (const struct die_reader_specs *reader,
18866 struct attribute *attr, unsigned form,
18867 LONGEST implicit_const, const gdb_byte *info_ptr)
18868 {
18869 struct dwarf2_cu *cu = reader->cu;
18870 struct dwarf2_per_objfile *dwarf2_per_objfile
18871 = cu->per_cu->dwarf2_per_objfile;
18872 struct objfile *objfile = dwarf2_per_objfile->objfile;
18873 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18874 bfd *abfd = reader->abfd;
18875 struct comp_unit_head *cu_header = &cu->header;
18876 unsigned int bytes_read;
18877 struct dwarf_block *blk;
18878
18879 attr->form = (enum dwarf_form) form;
18880 switch (form)
18881 {
18882 case DW_FORM_ref_addr:
18883 if (cu->header.version == 2)
18884 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18885 else
18886 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18887 &cu->header, &bytes_read);
18888 info_ptr += bytes_read;
18889 break;
18890 case DW_FORM_GNU_ref_alt:
18891 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18892 info_ptr += bytes_read;
18893 break;
18894 case DW_FORM_addr:
18895 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18896 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18897 info_ptr += bytes_read;
18898 break;
18899 case DW_FORM_block2:
18900 blk = dwarf_alloc_block (cu);
18901 blk->size = read_2_bytes (abfd, info_ptr);
18902 info_ptr += 2;
18903 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18904 info_ptr += blk->size;
18905 DW_BLOCK (attr) = blk;
18906 break;
18907 case DW_FORM_block4:
18908 blk = dwarf_alloc_block (cu);
18909 blk->size = read_4_bytes (abfd, info_ptr);
18910 info_ptr += 4;
18911 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18912 info_ptr += blk->size;
18913 DW_BLOCK (attr) = blk;
18914 break;
18915 case DW_FORM_data2:
18916 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18917 info_ptr += 2;
18918 break;
18919 case DW_FORM_data4:
18920 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18921 info_ptr += 4;
18922 break;
18923 case DW_FORM_data8:
18924 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18925 info_ptr += 8;
18926 break;
18927 case DW_FORM_data16:
18928 blk = dwarf_alloc_block (cu);
18929 blk->size = 16;
18930 blk->data = read_n_bytes (abfd, info_ptr, 16);
18931 info_ptr += 16;
18932 DW_BLOCK (attr) = blk;
18933 break;
18934 case DW_FORM_sec_offset:
18935 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18936 info_ptr += bytes_read;
18937 break;
18938 case DW_FORM_string:
18939 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18940 DW_STRING_IS_CANONICAL (attr) = 0;
18941 info_ptr += bytes_read;
18942 break;
18943 case DW_FORM_strp:
18944 if (!cu->per_cu->is_dwz)
18945 {
18946 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18947 abfd, info_ptr, cu_header,
18948 &bytes_read);
18949 DW_STRING_IS_CANONICAL (attr) = 0;
18950 info_ptr += bytes_read;
18951 break;
18952 }
18953 /* FALLTHROUGH */
18954 case DW_FORM_line_strp:
18955 if (!cu->per_cu->is_dwz)
18956 {
18957 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18958 abfd, info_ptr,
18959 cu_header, &bytes_read);
18960 DW_STRING_IS_CANONICAL (attr) = 0;
18961 info_ptr += bytes_read;
18962 break;
18963 }
18964 /* FALLTHROUGH */
18965 case DW_FORM_GNU_strp_alt:
18966 {
18967 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18968 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
18969 &bytes_read);
18970
18971 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18972 dwz, str_offset);
18973 DW_STRING_IS_CANONICAL (attr) = 0;
18974 info_ptr += bytes_read;
18975 }
18976 break;
18977 case DW_FORM_exprloc:
18978 case DW_FORM_block:
18979 blk = dwarf_alloc_block (cu);
18980 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18981 info_ptr += bytes_read;
18982 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18983 info_ptr += blk->size;
18984 DW_BLOCK (attr) = blk;
18985 break;
18986 case DW_FORM_block1:
18987 blk = dwarf_alloc_block (cu);
18988 blk->size = read_1_byte (abfd, info_ptr);
18989 info_ptr += 1;
18990 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18991 info_ptr += blk->size;
18992 DW_BLOCK (attr) = blk;
18993 break;
18994 case DW_FORM_data1:
18995 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18996 info_ptr += 1;
18997 break;
18998 case DW_FORM_flag:
18999 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19000 info_ptr += 1;
19001 break;
19002 case DW_FORM_flag_present:
19003 DW_UNSND (attr) = 1;
19004 break;
19005 case DW_FORM_sdata:
19006 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19007 info_ptr += bytes_read;
19008 break;
19009 case DW_FORM_udata:
19010 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19011 info_ptr += bytes_read;
19012 break;
19013 case DW_FORM_ref1:
19014 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19015 + read_1_byte (abfd, info_ptr));
19016 info_ptr += 1;
19017 break;
19018 case DW_FORM_ref2:
19019 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19020 + read_2_bytes (abfd, info_ptr));
19021 info_ptr += 2;
19022 break;
19023 case DW_FORM_ref4:
19024 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19025 + read_4_bytes (abfd, info_ptr));
19026 info_ptr += 4;
19027 break;
19028 case DW_FORM_ref8:
19029 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19030 + read_8_bytes (abfd, info_ptr));
19031 info_ptr += 8;
19032 break;
19033 case DW_FORM_ref_sig8:
19034 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19035 info_ptr += 8;
19036 break;
19037 case DW_FORM_ref_udata:
19038 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19039 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19040 info_ptr += bytes_read;
19041 break;
19042 case DW_FORM_indirect:
19043 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19044 info_ptr += bytes_read;
19045 if (form == DW_FORM_implicit_const)
19046 {
19047 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19048 info_ptr += bytes_read;
19049 }
19050 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19051 info_ptr);
19052 break;
19053 case DW_FORM_implicit_const:
19054 DW_SND (attr) = implicit_const;
19055 break;
19056 case DW_FORM_GNU_addr_index:
19057 if (reader->dwo_file == NULL)
19058 {
19059 /* For now flag a hard error.
19060 Later we can turn this into a complaint. */
19061 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19062 dwarf_form_name (form),
19063 bfd_get_filename (abfd));
19064 }
19065 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19066 info_ptr += bytes_read;
19067 break;
19068 case DW_FORM_GNU_str_index:
19069 if (reader->dwo_file == NULL)
19070 {
19071 /* For now flag a hard error.
19072 Later we can turn this into a complaint if warranted. */
19073 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19074 dwarf_form_name (form),
19075 bfd_get_filename (abfd));
19076 }
19077 {
19078 ULONGEST str_index =
19079 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19080
19081 DW_STRING (attr) = read_str_index (reader, str_index);
19082 DW_STRING_IS_CANONICAL (attr) = 0;
19083 info_ptr += bytes_read;
19084 }
19085 break;
19086 default:
19087 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19088 dwarf_form_name (form),
19089 bfd_get_filename (abfd));
19090 }
19091
19092 /* Super hack. */
19093 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19094 attr->form = DW_FORM_GNU_ref_alt;
19095
19096 /* We have seen instances where the compiler tried to emit a byte
19097 size attribute of -1 which ended up being encoded as an unsigned
19098 0xffffffff. Although 0xffffffff is technically a valid size value,
19099 an object of this size seems pretty unlikely so we can relatively
19100 safely treat these cases as if the size attribute was invalid and
19101 treat them as zero by default. */
19102 if (attr->name == DW_AT_byte_size
19103 && form == DW_FORM_data4
19104 && DW_UNSND (attr) >= 0xffffffff)
19105 {
19106 complaint
19107 (&symfile_complaints,
19108 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19109 hex_string (DW_UNSND (attr)));
19110 DW_UNSND (attr) = 0;
19111 }
19112
19113 return info_ptr;
19114 }
19115
19116 /* Read an attribute described by an abbreviated attribute. */
19117
19118 static const gdb_byte *
19119 read_attribute (const struct die_reader_specs *reader,
19120 struct attribute *attr, struct attr_abbrev *abbrev,
19121 const gdb_byte *info_ptr)
19122 {
19123 attr->name = abbrev->name;
19124 return read_attribute_value (reader, attr, abbrev->form,
19125 abbrev->implicit_const, info_ptr);
19126 }
19127
19128 /* Read dwarf information from a buffer. */
19129
19130 static unsigned int
19131 read_1_byte (bfd *abfd, const gdb_byte *buf)
19132 {
19133 return bfd_get_8 (abfd, buf);
19134 }
19135
19136 static int
19137 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19138 {
19139 return bfd_get_signed_8 (abfd, buf);
19140 }
19141
19142 static unsigned int
19143 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19144 {
19145 return bfd_get_16 (abfd, buf);
19146 }
19147
19148 static int
19149 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19150 {
19151 return bfd_get_signed_16 (abfd, buf);
19152 }
19153
19154 static unsigned int
19155 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19156 {
19157 return bfd_get_32 (abfd, buf);
19158 }
19159
19160 static int
19161 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19162 {
19163 return bfd_get_signed_32 (abfd, buf);
19164 }
19165
19166 static ULONGEST
19167 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19168 {
19169 return bfd_get_64 (abfd, buf);
19170 }
19171
19172 static CORE_ADDR
19173 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19174 unsigned int *bytes_read)
19175 {
19176 struct comp_unit_head *cu_header = &cu->header;
19177 CORE_ADDR retval = 0;
19178
19179 if (cu_header->signed_addr_p)
19180 {
19181 switch (cu_header->addr_size)
19182 {
19183 case 2:
19184 retval = bfd_get_signed_16 (abfd, buf);
19185 break;
19186 case 4:
19187 retval = bfd_get_signed_32 (abfd, buf);
19188 break;
19189 case 8:
19190 retval = bfd_get_signed_64 (abfd, buf);
19191 break;
19192 default:
19193 internal_error (__FILE__, __LINE__,
19194 _("read_address: bad switch, signed [in module %s]"),
19195 bfd_get_filename (abfd));
19196 }
19197 }
19198 else
19199 {
19200 switch (cu_header->addr_size)
19201 {
19202 case 2:
19203 retval = bfd_get_16 (abfd, buf);
19204 break;
19205 case 4:
19206 retval = bfd_get_32 (abfd, buf);
19207 break;
19208 case 8:
19209 retval = bfd_get_64 (abfd, buf);
19210 break;
19211 default:
19212 internal_error (__FILE__, __LINE__,
19213 _("read_address: bad switch, "
19214 "unsigned [in module %s]"),
19215 bfd_get_filename (abfd));
19216 }
19217 }
19218
19219 *bytes_read = cu_header->addr_size;
19220 return retval;
19221 }
19222
19223 /* Read the initial length from a section. The (draft) DWARF 3
19224 specification allows the initial length to take up either 4 bytes
19225 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19226 bytes describe the length and all offsets will be 8 bytes in length
19227 instead of 4.
19228
19229 An older, non-standard 64-bit format is also handled by this
19230 function. The older format in question stores the initial length
19231 as an 8-byte quantity without an escape value. Lengths greater
19232 than 2^32 aren't very common which means that the initial 4 bytes
19233 is almost always zero. Since a length value of zero doesn't make
19234 sense for the 32-bit format, this initial zero can be considered to
19235 be an escape value which indicates the presence of the older 64-bit
19236 format. As written, the code can't detect (old format) lengths
19237 greater than 4GB. If it becomes necessary to handle lengths
19238 somewhat larger than 4GB, we could allow other small values (such
19239 as the non-sensical values of 1, 2, and 3) to also be used as
19240 escape values indicating the presence of the old format.
19241
19242 The value returned via bytes_read should be used to increment the
19243 relevant pointer after calling read_initial_length().
19244
19245 [ Note: read_initial_length() and read_offset() are based on the
19246 document entitled "DWARF Debugging Information Format", revision
19247 3, draft 8, dated November 19, 2001. This document was obtained
19248 from:
19249
19250 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19251
19252 This document is only a draft and is subject to change. (So beware.)
19253
19254 Details regarding the older, non-standard 64-bit format were
19255 determined empirically by examining 64-bit ELF files produced by
19256 the SGI toolchain on an IRIX 6.5 machine.
19257
19258 - Kevin, July 16, 2002
19259 ] */
19260
19261 static LONGEST
19262 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19263 {
19264 LONGEST length = bfd_get_32 (abfd, buf);
19265
19266 if (length == 0xffffffff)
19267 {
19268 length = bfd_get_64 (abfd, buf + 4);
19269 *bytes_read = 12;
19270 }
19271 else if (length == 0)
19272 {
19273 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19274 length = bfd_get_64 (abfd, buf);
19275 *bytes_read = 8;
19276 }
19277 else
19278 {
19279 *bytes_read = 4;
19280 }
19281
19282 return length;
19283 }
19284
19285 /* Cover function for read_initial_length.
19286 Returns the length of the object at BUF, and stores the size of the
19287 initial length in *BYTES_READ and stores the size that offsets will be in
19288 *OFFSET_SIZE.
19289 If the initial length size is not equivalent to that specified in
19290 CU_HEADER then issue a complaint.
19291 This is useful when reading non-comp-unit headers. */
19292
19293 static LONGEST
19294 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19295 const struct comp_unit_head *cu_header,
19296 unsigned int *bytes_read,
19297 unsigned int *offset_size)
19298 {
19299 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19300
19301 gdb_assert (cu_header->initial_length_size == 4
19302 || cu_header->initial_length_size == 8
19303 || cu_header->initial_length_size == 12);
19304
19305 if (cu_header->initial_length_size != *bytes_read)
19306 complaint (&symfile_complaints,
19307 _("intermixed 32-bit and 64-bit DWARF sections"));
19308
19309 *offset_size = (*bytes_read == 4) ? 4 : 8;
19310 return length;
19311 }
19312
19313 /* Read an offset from the data stream. The size of the offset is
19314 given by cu_header->offset_size. */
19315
19316 static LONGEST
19317 read_offset (bfd *abfd, const gdb_byte *buf,
19318 const struct comp_unit_head *cu_header,
19319 unsigned int *bytes_read)
19320 {
19321 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19322
19323 *bytes_read = cu_header->offset_size;
19324 return offset;
19325 }
19326
19327 /* Read an offset from the data stream. */
19328
19329 static LONGEST
19330 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19331 {
19332 LONGEST retval = 0;
19333
19334 switch (offset_size)
19335 {
19336 case 4:
19337 retval = bfd_get_32 (abfd, buf);
19338 break;
19339 case 8:
19340 retval = bfd_get_64 (abfd, buf);
19341 break;
19342 default:
19343 internal_error (__FILE__, __LINE__,
19344 _("read_offset_1: bad switch [in module %s]"),
19345 bfd_get_filename (abfd));
19346 }
19347
19348 return retval;
19349 }
19350
19351 static const gdb_byte *
19352 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19353 {
19354 /* If the size of a host char is 8 bits, we can return a pointer
19355 to the buffer, otherwise we have to copy the data to a buffer
19356 allocated on the temporary obstack. */
19357 gdb_assert (HOST_CHAR_BIT == 8);
19358 return buf;
19359 }
19360
19361 static const char *
19362 read_direct_string (bfd *abfd, const gdb_byte *buf,
19363 unsigned int *bytes_read_ptr)
19364 {
19365 /* If the size of a host char is 8 bits, we can return a pointer
19366 to the string, otherwise we have to copy the string to a buffer
19367 allocated on the temporary obstack. */
19368 gdb_assert (HOST_CHAR_BIT == 8);
19369 if (*buf == '\0')
19370 {
19371 *bytes_read_ptr = 1;
19372 return NULL;
19373 }
19374 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19375 return (const char *) buf;
19376 }
19377
19378 /* Return pointer to string at section SECT offset STR_OFFSET with error
19379 reporting strings FORM_NAME and SECT_NAME. */
19380
19381 static const char *
19382 read_indirect_string_at_offset_from (struct objfile *objfile,
19383 bfd *abfd, LONGEST str_offset,
19384 struct dwarf2_section_info *sect,
19385 const char *form_name,
19386 const char *sect_name)
19387 {
19388 dwarf2_read_section (objfile, sect);
19389 if (sect->buffer == NULL)
19390 error (_("%s used without %s section [in module %s]"),
19391 form_name, sect_name, bfd_get_filename (abfd));
19392 if (str_offset >= sect->size)
19393 error (_("%s pointing outside of %s section [in module %s]"),
19394 form_name, sect_name, bfd_get_filename (abfd));
19395 gdb_assert (HOST_CHAR_BIT == 8);
19396 if (sect->buffer[str_offset] == '\0')
19397 return NULL;
19398 return (const char *) (sect->buffer + str_offset);
19399 }
19400
19401 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19402
19403 static const char *
19404 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19405 bfd *abfd, LONGEST str_offset)
19406 {
19407 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19408 abfd, str_offset,
19409 &dwarf2_per_objfile->str,
19410 "DW_FORM_strp", ".debug_str");
19411 }
19412
19413 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19414
19415 static const char *
19416 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19417 bfd *abfd, LONGEST str_offset)
19418 {
19419 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19420 abfd, str_offset,
19421 &dwarf2_per_objfile->line_str,
19422 "DW_FORM_line_strp",
19423 ".debug_line_str");
19424 }
19425
19426 /* Read a string at offset STR_OFFSET in the .debug_str section from
19427 the .dwz file DWZ. Throw an error if the offset is too large. If
19428 the string consists of a single NUL byte, return NULL; otherwise
19429 return a pointer to the string. */
19430
19431 static const char *
19432 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19433 LONGEST str_offset)
19434 {
19435 dwarf2_read_section (objfile, &dwz->str);
19436
19437 if (dwz->str.buffer == NULL)
19438 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19439 "section [in module %s]"),
19440 bfd_get_filename (dwz->dwz_bfd));
19441 if (str_offset >= dwz->str.size)
19442 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19443 ".debug_str section [in module %s]"),
19444 bfd_get_filename (dwz->dwz_bfd));
19445 gdb_assert (HOST_CHAR_BIT == 8);
19446 if (dwz->str.buffer[str_offset] == '\0')
19447 return NULL;
19448 return (const char *) (dwz->str.buffer + str_offset);
19449 }
19450
19451 /* Return pointer to string at .debug_str offset as read from BUF.
19452 BUF is assumed to be in a compilation unit described by CU_HEADER.
19453 Return *BYTES_READ_PTR count of bytes read from BUF. */
19454
19455 static const char *
19456 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19457 const gdb_byte *buf,
19458 const struct comp_unit_head *cu_header,
19459 unsigned int *bytes_read_ptr)
19460 {
19461 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19462
19463 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19464 }
19465
19466 /* Return pointer to string at .debug_line_str offset as read from BUF.
19467 BUF is assumed to be in a compilation unit described by CU_HEADER.
19468 Return *BYTES_READ_PTR count of bytes read from BUF. */
19469
19470 static const char *
19471 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19472 bfd *abfd, const gdb_byte *buf,
19473 const struct comp_unit_head *cu_header,
19474 unsigned int *bytes_read_ptr)
19475 {
19476 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19477
19478 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19479 str_offset);
19480 }
19481
19482 ULONGEST
19483 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19484 unsigned int *bytes_read_ptr)
19485 {
19486 ULONGEST result;
19487 unsigned int num_read;
19488 int shift;
19489 unsigned char byte;
19490
19491 result = 0;
19492 shift = 0;
19493 num_read = 0;
19494 while (1)
19495 {
19496 byte = bfd_get_8 (abfd, buf);
19497 buf++;
19498 num_read++;
19499 result |= ((ULONGEST) (byte & 127) << shift);
19500 if ((byte & 128) == 0)
19501 {
19502 break;
19503 }
19504 shift += 7;
19505 }
19506 *bytes_read_ptr = num_read;
19507 return result;
19508 }
19509
19510 static LONGEST
19511 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19512 unsigned int *bytes_read_ptr)
19513 {
19514 LONGEST result;
19515 int shift, num_read;
19516 unsigned char byte;
19517
19518 result = 0;
19519 shift = 0;
19520 num_read = 0;
19521 while (1)
19522 {
19523 byte = bfd_get_8 (abfd, buf);
19524 buf++;
19525 num_read++;
19526 result |= ((LONGEST) (byte & 127) << shift);
19527 shift += 7;
19528 if ((byte & 128) == 0)
19529 {
19530 break;
19531 }
19532 }
19533 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19534 result |= -(((LONGEST) 1) << shift);
19535 *bytes_read_ptr = num_read;
19536 return result;
19537 }
19538
19539 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19540 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19541 ADDR_SIZE is the size of addresses from the CU header. */
19542
19543 static CORE_ADDR
19544 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19545 unsigned int addr_index, ULONGEST addr_base, int addr_size)
19546 {
19547 struct objfile *objfile = dwarf2_per_objfile->objfile;
19548 bfd *abfd = objfile->obfd;
19549 const gdb_byte *info_ptr;
19550
19551 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19552 if (dwarf2_per_objfile->addr.buffer == NULL)
19553 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19554 objfile_name (objfile));
19555 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19556 error (_("DW_FORM_addr_index pointing outside of "
19557 ".debug_addr section [in module %s]"),
19558 objfile_name (objfile));
19559 info_ptr = (dwarf2_per_objfile->addr.buffer
19560 + addr_base + addr_index * addr_size);
19561 if (addr_size == 4)
19562 return bfd_get_32 (abfd, info_ptr);
19563 else
19564 return bfd_get_64 (abfd, info_ptr);
19565 }
19566
19567 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19568
19569 static CORE_ADDR
19570 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19571 {
19572 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19573 cu->addr_base, cu->header.addr_size);
19574 }
19575
19576 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19577
19578 static CORE_ADDR
19579 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19580 unsigned int *bytes_read)
19581 {
19582 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19583 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19584
19585 return read_addr_index (cu, addr_index);
19586 }
19587
19588 /* Data structure to pass results from dwarf2_read_addr_index_reader
19589 back to dwarf2_read_addr_index. */
19590
19591 struct dwarf2_read_addr_index_data
19592 {
19593 ULONGEST addr_base;
19594 int addr_size;
19595 };
19596
19597 /* die_reader_func for dwarf2_read_addr_index. */
19598
19599 static void
19600 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19601 const gdb_byte *info_ptr,
19602 struct die_info *comp_unit_die,
19603 int has_children,
19604 void *data)
19605 {
19606 struct dwarf2_cu *cu = reader->cu;
19607 struct dwarf2_read_addr_index_data *aidata =
19608 (struct dwarf2_read_addr_index_data *) data;
19609
19610 aidata->addr_base = cu->addr_base;
19611 aidata->addr_size = cu->header.addr_size;
19612 }
19613
19614 /* Given an index in .debug_addr, fetch the value.
19615 NOTE: This can be called during dwarf expression evaluation,
19616 long after the debug information has been read, and thus per_cu->cu
19617 may no longer exist. */
19618
19619 CORE_ADDR
19620 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19621 unsigned int addr_index)
19622 {
19623 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19624 struct dwarf2_cu *cu = per_cu->cu;
19625 ULONGEST addr_base;
19626 int addr_size;
19627
19628 /* We need addr_base and addr_size.
19629 If we don't have PER_CU->cu, we have to get it.
19630 Nasty, but the alternative is storing the needed info in PER_CU,
19631 which at this point doesn't seem justified: it's not clear how frequently
19632 it would get used and it would increase the size of every PER_CU.
19633 Entry points like dwarf2_per_cu_addr_size do a similar thing
19634 so we're not in uncharted territory here.
19635 Alas we need to be a bit more complicated as addr_base is contained
19636 in the DIE.
19637
19638 We don't need to read the entire CU(/TU).
19639 We just need the header and top level die.
19640
19641 IWBN to use the aging mechanism to let us lazily later discard the CU.
19642 For now we skip this optimization. */
19643
19644 if (cu != NULL)
19645 {
19646 addr_base = cu->addr_base;
19647 addr_size = cu->header.addr_size;
19648 }
19649 else
19650 {
19651 struct dwarf2_read_addr_index_data aidata;
19652
19653 /* Note: We can't use init_cutu_and_read_dies_simple here,
19654 we need addr_base. */
19655 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
19656 dwarf2_read_addr_index_reader, &aidata);
19657 addr_base = aidata.addr_base;
19658 addr_size = aidata.addr_size;
19659 }
19660
19661 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19662 addr_size);
19663 }
19664
19665 /* Given a DW_FORM_GNU_str_index, fetch the string.
19666 This is only used by the Fission support. */
19667
19668 static const char *
19669 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19670 {
19671 struct dwarf2_cu *cu = reader->cu;
19672 struct dwarf2_per_objfile *dwarf2_per_objfile
19673 = cu->per_cu->dwarf2_per_objfile;
19674 struct objfile *objfile = dwarf2_per_objfile->objfile;
19675 const char *objf_name = objfile_name (objfile);
19676 bfd *abfd = objfile->obfd;
19677 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19678 struct dwarf2_section_info *str_offsets_section =
19679 &reader->dwo_file->sections.str_offsets;
19680 const gdb_byte *info_ptr;
19681 ULONGEST str_offset;
19682 static const char form_name[] = "DW_FORM_GNU_str_index";
19683
19684 dwarf2_read_section (objfile, str_section);
19685 dwarf2_read_section (objfile, str_offsets_section);
19686 if (str_section->buffer == NULL)
19687 error (_("%s used without .debug_str.dwo section"
19688 " in CU at offset %s [in module %s]"),
19689 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19690 if (str_offsets_section->buffer == NULL)
19691 error (_("%s used without .debug_str_offsets.dwo section"
19692 " in CU at offset %s [in module %s]"),
19693 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19694 if (str_index * cu->header.offset_size >= str_offsets_section->size)
19695 error (_("%s pointing outside of .debug_str_offsets.dwo"
19696 " section in CU at offset %s [in module %s]"),
19697 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19698 info_ptr = (str_offsets_section->buffer
19699 + str_index * cu->header.offset_size);
19700 if (cu->header.offset_size == 4)
19701 str_offset = bfd_get_32 (abfd, info_ptr);
19702 else
19703 str_offset = bfd_get_64 (abfd, info_ptr);
19704 if (str_offset >= str_section->size)
19705 error (_("Offset from %s pointing outside of"
19706 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19707 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19708 return (const char *) (str_section->buffer + str_offset);
19709 }
19710
19711 /* Return the length of an LEB128 number in BUF. */
19712
19713 static int
19714 leb128_size (const gdb_byte *buf)
19715 {
19716 const gdb_byte *begin = buf;
19717 gdb_byte byte;
19718
19719 while (1)
19720 {
19721 byte = *buf++;
19722 if ((byte & 128) == 0)
19723 return buf - begin;
19724 }
19725 }
19726
19727 static void
19728 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19729 {
19730 switch (lang)
19731 {
19732 case DW_LANG_C89:
19733 case DW_LANG_C99:
19734 case DW_LANG_C11:
19735 case DW_LANG_C:
19736 case DW_LANG_UPC:
19737 cu->language = language_c;
19738 break;
19739 case DW_LANG_Java:
19740 case DW_LANG_C_plus_plus:
19741 case DW_LANG_C_plus_plus_11:
19742 case DW_LANG_C_plus_plus_14:
19743 cu->language = language_cplus;
19744 break;
19745 case DW_LANG_D:
19746 cu->language = language_d;
19747 break;
19748 case DW_LANG_Fortran77:
19749 case DW_LANG_Fortran90:
19750 case DW_LANG_Fortran95:
19751 case DW_LANG_Fortran03:
19752 case DW_LANG_Fortran08:
19753 cu->language = language_fortran;
19754 break;
19755 case DW_LANG_Go:
19756 cu->language = language_go;
19757 break;
19758 case DW_LANG_Mips_Assembler:
19759 cu->language = language_asm;
19760 break;
19761 case DW_LANG_Ada83:
19762 case DW_LANG_Ada95:
19763 cu->language = language_ada;
19764 break;
19765 case DW_LANG_Modula2:
19766 cu->language = language_m2;
19767 break;
19768 case DW_LANG_Pascal83:
19769 cu->language = language_pascal;
19770 break;
19771 case DW_LANG_ObjC:
19772 cu->language = language_objc;
19773 break;
19774 case DW_LANG_Rust:
19775 case DW_LANG_Rust_old:
19776 cu->language = language_rust;
19777 break;
19778 case DW_LANG_Cobol74:
19779 case DW_LANG_Cobol85:
19780 default:
19781 cu->language = language_minimal;
19782 break;
19783 }
19784 cu->language_defn = language_def (cu->language);
19785 }
19786
19787 /* Return the named attribute or NULL if not there. */
19788
19789 static struct attribute *
19790 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19791 {
19792 for (;;)
19793 {
19794 unsigned int i;
19795 struct attribute *spec = NULL;
19796
19797 for (i = 0; i < die->num_attrs; ++i)
19798 {
19799 if (die->attrs[i].name == name)
19800 return &die->attrs[i];
19801 if (die->attrs[i].name == DW_AT_specification
19802 || die->attrs[i].name == DW_AT_abstract_origin)
19803 spec = &die->attrs[i];
19804 }
19805
19806 if (!spec)
19807 break;
19808
19809 die = follow_die_ref (die, spec, &cu);
19810 }
19811
19812 return NULL;
19813 }
19814
19815 /* Return the named attribute or NULL if not there,
19816 but do not follow DW_AT_specification, etc.
19817 This is for use in contexts where we're reading .debug_types dies.
19818 Following DW_AT_specification, DW_AT_abstract_origin will take us
19819 back up the chain, and we want to go down. */
19820
19821 static struct attribute *
19822 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19823 {
19824 unsigned int i;
19825
19826 for (i = 0; i < die->num_attrs; ++i)
19827 if (die->attrs[i].name == name)
19828 return &die->attrs[i];
19829
19830 return NULL;
19831 }
19832
19833 /* Return the string associated with a string-typed attribute, or NULL if it
19834 is either not found or is of an incorrect type. */
19835
19836 static const char *
19837 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19838 {
19839 struct attribute *attr;
19840 const char *str = NULL;
19841
19842 attr = dwarf2_attr (die, name, cu);
19843
19844 if (attr != NULL)
19845 {
19846 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19847 || attr->form == DW_FORM_string
19848 || attr->form == DW_FORM_GNU_str_index
19849 || attr->form == DW_FORM_GNU_strp_alt)
19850 str = DW_STRING (attr);
19851 else
19852 complaint (&symfile_complaints,
19853 _("string type expected for attribute %s for "
19854 "DIE at %s in module %s"),
19855 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19856 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19857 }
19858
19859 return str;
19860 }
19861
19862 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19863 and holds a non-zero value. This function should only be used for
19864 DW_FORM_flag or DW_FORM_flag_present attributes. */
19865
19866 static int
19867 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19868 {
19869 struct attribute *attr = dwarf2_attr (die, name, cu);
19870
19871 return (attr && DW_UNSND (attr));
19872 }
19873
19874 static int
19875 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19876 {
19877 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19878 which value is non-zero. However, we have to be careful with
19879 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19880 (via dwarf2_flag_true_p) follows this attribute. So we may
19881 end up accidently finding a declaration attribute that belongs
19882 to a different DIE referenced by the specification attribute,
19883 even though the given DIE does not have a declaration attribute. */
19884 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19885 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19886 }
19887
19888 /* Return the die giving the specification for DIE, if there is
19889 one. *SPEC_CU is the CU containing DIE on input, and the CU
19890 containing the return value on output. If there is no
19891 specification, but there is an abstract origin, that is
19892 returned. */
19893
19894 static struct die_info *
19895 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19896 {
19897 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19898 *spec_cu);
19899
19900 if (spec_attr == NULL)
19901 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19902
19903 if (spec_attr == NULL)
19904 return NULL;
19905 else
19906 return follow_die_ref (die, spec_attr, spec_cu);
19907 }
19908
19909 /* Stub for free_line_header to match void * callback types. */
19910
19911 static void
19912 free_line_header_voidp (void *arg)
19913 {
19914 struct line_header *lh = (struct line_header *) arg;
19915
19916 delete lh;
19917 }
19918
19919 void
19920 line_header::add_include_dir (const char *include_dir)
19921 {
19922 if (dwarf_line_debug >= 2)
19923 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19924 include_dirs.size () + 1, include_dir);
19925
19926 include_dirs.push_back (include_dir);
19927 }
19928
19929 void
19930 line_header::add_file_name (const char *name,
19931 dir_index d_index,
19932 unsigned int mod_time,
19933 unsigned int length)
19934 {
19935 if (dwarf_line_debug >= 2)
19936 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
19937 (unsigned) file_names.size () + 1, name);
19938
19939 file_names.emplace_back (name, d_index, mod_time, length);
19940 }
19941
19942 /* A convenience function to find the proper .debug_line section for a CU. */
19943
19944 static struct dwarf2_section_info *
19945 get_debug_line_section (struct dwarf2_cu *cu)
19946 {
19947 struct dwarf2_section_info *section;
19948 struct dwarf2_per_objfile *dwarf2_per_objfile
19949 = cu->per_cu->dwarf2_per_objfile;
19950
19951 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19952 DWO file. */
19953 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19954 section = &cu->dwo_unit->dwo_file->sections.line;
19955 else if (cu->per_cu->is_dwz)
19956 {
19957 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19958
19959 section = &dwz->line;
19960 }
19961 else
19962 section = &dwarf2_per_objfile->line;
19963
19964 return section;
19965 }
19966
19967 /* Read directory or file name entry format, starting with byte of
19968 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19969 entries count and the entries themselves in the described entry
19970 format. */
19971
19972 static void
19973 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19974 bfd *abfd, const gdb_byte **bufp,
19975 struct line_header *lh,
19976 const struct comp_unit_head *cu_header,
19977 void (*callback) (struct line_header *lh,
19978 const char *name,
19979 dir_index d_index,
19980 unsigned int mod_time,
19981 unsigned int length))
19982 {
19983 gdb_byte format_count, formati;
19984 ULONGEST data_count, datai;
19985 const gdb_byte *buf = *bufp;
19986 const gdb_byte *format_header_data;
19987 unsigned int bytes_read;
19988
19989 format_count = read_1_byte (abfd, buf);
19990 buf += 1;
19991 format_header_data = buf;
19992 for (formati = 0; formati < format_count; formati++)
19993 {
19994 read_unsigned_leb128 (abfd, buf, &bytes_read);
19995 buf += bytes_read;
19996 read_unsigned_leb128 (abfd, buf, &bytes_read);
19997 buf += bytes_read;
19998 }
19999
20000 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20001 buf += bytes_read;
20002 for (datai = 0; datai < data_count; datai++)
20003 {
20004 const gdb_byte *format = format_header_data;
20005 struct file_entry fe;
20006
20007 for (formati = 0; formati < format_count; formati++)
20008 {
20009 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20010 format += bytes_read;
20011
20012 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20013 format += bytes_read;
20014
20015 gdb::optional<const char *> string;
20016 gdb::optional<unsigned int> uint;
20017
20018 switch (form)
20019 {
20020 case DW_FORM_string:
20021 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20022 buf += bytes_read;
20023 break;
20024
20025 case DW_FORM_line_strp:
20026 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20027 abfd, buf,
20028 cu_header,
20029 &bytes_read));
20030 buf += bytes_read;
20031 break;
20032
20033 case DW_FORM_data1:
20034 uint.emplace (read_1_byte (abfd, buf));
20035 buf += 1;
20036 break;
20037
20038 case DW_FORM_data2:
20039 uint.emplace (read_2_bytes (abfd, buf));
20040 buf += 2;
20041 break;
20042
20043 case DW_FORM_data4:
20044 uint.emplace (read_4_bytes (abfd, buf));
20045 buf += 4;
20046 break;
20047
20048 case DW_FORM_data8:
20049 uint.emplace (read_8_bytes (abfd, buf));
20050 buf += 8;
20051 break;
20052
20053 case DW_FORM_udata:
20054 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20055 buf += bytes_read;
20056 break;
20057
20058 case DW_FORM_block:
20059 /* It is valid only for DW_LNCT_timestamp which is ignored by
20060 current GDB. */
20061 break;
20062 }
20063
20064 switch (content_type)
20065 {
20066 case DW_LNCT_path:
20067 if (string.has_value ())
20068 fe.name = *string;
20069 break;
20070 case DW_LNCT_directory_index:
20071 if (uint.has_value ())
20072 fe.d_index = (dir_index) *uint;
20073 break;
20074 case DW_LNCT_timestamp:
20075 if (uint.has_value ())
20076 fe.mod_time = *uint;
20077 break;
20078 case DW_LNCT_size:
20079 if (uint.has_value ())
20080 fe.length = *uint;
20081 break;
20082 case DW_LNCT_MD5:
20083 break;
20084 default:
20085 complaint (&symfile_complaints,
20086 _("Unknown format content type %s"),
20087 pulongest (content_type));
20088 }
20089 }
20090
20091 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20092 }
20093
20094 *bufp = buf;
20095 }
20096
20097 /* Read the statement program header starting at OFFSET in
20098 .debug_line, or .debug_line.dwo. Return a pointer
20099 to a struct line_header, allocated using xmalloc.
20100 Returns NULL if there is a problem reading the header, e.g., if it
20101 has a version we don't understand.
20102
20103 NOTE: the strings in the include directory and file name tables of
20104 the returned object point into the dwarf line section buffer,
20105 and must not be freed. */
20106
20107 static line_header_up
20108 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20109 {
20110 const gdb_byte *line_ptr;
20111 unsigned int bytes_read, offset_size;
20112 int i;
20113 const char *cur_dir, *cur_file;
20114 struct dwarf2_section_info *section;
20115 bfd *abfd;
20116 struct dwarf2_per_objfile *dwarf2_per_objfile
20117 = cu->per_cu->dwarf2_per_objfile;
20118
20119 section = get_debug_line_section (cu);
20120 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20121 if (section->buffer == NULL)
20122 {
20123 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20124 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20125 else
20126 complaint (&symfile_complaints, _("missing .debug_line section"));
20127 return 0;
20128 }
20129
20130 /* We can't do this until we know the section is non-empty.
20131 Only then do we know we have such a section. */
20132 abfd = get_section_bfd_owner (section);
20133
20134 /* Make sure that at least there's room for the total_length field.
20135 That could be 12 bytes long, but we're just going to fudge that. */
20136 if (to_underlying (sect_off) + 4 >= section->size)
20137 {
20138 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20139 return 0;
20140 }
20141
20142 line_header_up lh (new line_header ());
20143
20144 lh->sect_off = sect_off;
20145 lh->offset_in_dwz = cu->per_cu->is_dwz;
20146
20147 line_ptr = section->buffer + to_underlying (sect_off);
20148
20149 /* Read in the header. */
20150 lh->total_length =
20151 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20152 &bytes_read, &offset_size);
20153 line_ptr += bytes_read;
20154 if (line_ptr + lh->total_length > (section->buffer + section->size))
20155 {
20156 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20157 return 0;
20158 }
20159 lh->statement_program_end = line_ptr + lh->total_length;
20160 lh->version = read_2_bytes (abfd, line_ptr);
20161 line_ptr += 2;
20162 if (lh->version > 5)
20163 {
20164 /* This is a version we don't understand. The format could have
20165 changed in ways we don't handle properly so just punt. */
20166 complaint (&symfile_complaints,
20167 _("unsupported version in .debug_line section"));
20168 return NULL;
20169 }
20170 if (lh->version >= 5)
20171 {
20172 gdb_byte segment_selector_size;
20173
20174 /* Skip address size. */
20175 read_1_byte (abfd, line_ptr);
20176 line_ptr += 1;
20177
20178 segment_selector_size = read_1_byte (abfd, line_ptr);
20179 line_ptr += 1;
20180 if (segment_selector_size != 0)
20181 {
20182 complaint (&symfile_complaints,
20183 _("unsupported segment selector size %u "
20184 "in .debug_line section"),
20185 segment_selector_size);
20186 return NULL;
20187 }
20188 }
20189 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20190 line_ptr += offset_size;
20191 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20192 line_ptr += 1;
20193 if (lh->version >= 4)
20194 {
20195 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20196 line_ptr += 1;
20197 }
20198 else
20199 lh->maximum_ops_per_instruction = 1;
20200
20201 if (lh->maximum_ops_per_instruction == 0)
20202 {
20203 lh->maximum_ops_per_instruction = 1;
20204 complaint (&symfile_complaints,
20205 _("invalid maximum_ops_per_instruction "
20206 "in `.debug_line' section"));
20207 }
20208
20209 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20210 line_ptr += 1;
20211 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20212 line_ptr += 1;
20213 lh->line_range = read_1_byte (abfd, line_ptr);
20214 line_ptr += 1;
20215 lh->opcode_base = read_1_byte (abfd, line_ptr);
20216 line_ptr += 1;
20217 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20218
20219 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20220 for (i = 1; i < lh->opcode_base; ++i)
20221 {
20222 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20223 line_ptr += 1;
20224 }
20225
20226 if (lh->version >= 5)
20227 {
20228 /* Read directory table. */
20229 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20230 &cu->header,
20231 [] (struct line_header *lh, const char *name,
20232 dir_index d_index, unsigned int mod_time,
20233 unsigned int length)
20234 {
20235 lh->add_include_dir (name);
20236 });
20237
20238 /* Read file name table. */
20239 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20240 &cu->header,
20241 [] (struct line_header *lh, const char *name,
20242 dir_index d_index, unsigned int mod_time,
20243 unsigned int length)
20244 {
20245 lh->add_file_name (name, d_index, mod_time, length);
20246 });
20247 }
20248 else
20249 {
20250 /* Read directory table. */
20251 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20252 {
20253 line_ptr += bytes_read;
20254 lh->add_include_dir (cur_dir);
20255 }
20256 line_ptr += bytes_read;
20257
20258 /* Read file name table. */
20259 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20260 {
20261 unsigned int mod_time, length;
20262 dir_index d_index;
20263
20264 line_ptr += bytes_read;
20265 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20266 line_ptr += bytes_read;
20267 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20268 line_ptr += bytes_read;
20269 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20270 line_ptr += bytes_read;
20271
20272 lh->add_file_name (cur_file, d_index, mod_time, length);
20273 }
20274 line_ptr += bytes_read;
20275 }
20276 lh->statement_program_start = line_ptr;
20277
20278 if (line_ptr > (section->buffer + section->size))
20279 complaint (&symfile_complaints,
20280 _("line number info header doesn't "
20281 "fit in `.debug_line' section"));
20282
20283 return lh;
20284 }
20285
20286 /* Subroutine of dwarf_decode_lines to simplify it.
20287 Return the file name of the psymtab for included file FILE_INDEX
20288 in line header LH of PST.
20289 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20290 If space for the result is malloc'd, *NAME_HOLDER will be set.
20291 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20292
20293 static const char *
20294 psymtab_include_file_name (const struct line_header *lh, int file_index,
20295 const struct partial_symtab *pst,
20296 const char *comp_dir,
20297 gdb::unique_xmalloc_ptr<char> *name_holder)
20298 {
20299 const file_entry &fe = lh->file_names[file_index];
20300 const char *include_name = fe.name;
20301 const char *include_name_to_compare = include_name;
20302 const char *pst_filename;
20303 int file_is_pst;
20304
20305 const char *dir_name = fe.include_dir (lh);
20306
20307 gdb::unique_xmalloc_ptr<char> hold_compare;
20308 if (!IS_ABSOLUTE_PATH (include_name)
20309 && (dir_name != NULL || comp_dir != NULL))
20310 {
20311 /* Avoid creating a duplicate psymtab for PST.
20312 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20313 Before we do the comparison, however, we need to account
20314 for DIR_NAME and COMP_DIR.
20315 First prepend dir_name (if non-NULL). If we still don't
20316 have an absolute path prepend comp_dir (if non-NULL).
20317 However, the directory we record in the include-file's
20318 psymtab does not contain COMP_DIR (to match the
20319 corresponding symtab(s)).
20320
20321 Example:
20322
20323 bash$ cd /tmp
20324 bash$ gcc -g ./hello.c
20325 include_name = "hello.c"
20326 dir_name = "."
20327 DW_AT_comp_dir = comp_dir = "/tmp"
20328 DW_AT_name = "./hello.c"
20329
20330 */
20331
20332 if (dir_name != NULL)
20333 {
20334 name_holder->reset (concat (dir_name, SLASH_STRING,
20335 include_name, (char *) NULL));
20336 include_name = name_holder->get ();
20337 include_name_to_compare = include_name;
20338 }
20339 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20340 {
20341 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20342 include_name, (char *) NULL));
20343 include_name_to_compare = hold_compare.get ();
20344 }
20345 }
20346
20347 pst_filename = pst->filename;
20348 gdb::unique_xmalloc_ptr<char> copied_name;
20349 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20350 {
20351 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20352 pst_filename, (char *) NULL));
20353 pst_filename = copied_name.get ();
20354 }
20355
20356 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20357
20358 if (file_is_pst)
20359 return NULL;
20360 return include_name;
20361 }
20362
20363 /* State machine to track the state of the line number program. */
20364
20365 class lnp_state_machine
20366 {
20367 public:
20368 /* Initialize a machine state for the start of a line number
20369 program. */
20370 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20371
20372 file_entry *current_file ()
20373 {
20374 /* lh->file_names is 0-based, but the file name numbers in the
20375 statement program are 1-based. */
20376 return m_line_header->file_name_at (m_file);
20377 }
20378
20379 /* Record the line in the state machine. END_SEQUENCE is true if
20380 we're processing the end of a sequence. */
20381 void record_line (bool end_sequence);
20382
20383 /* Check address and if invalid nop-out the rest of the lines in this
20384 sequence. */
20385 void check_line_address (struct dwarf2_cu *cu,
20386 const gdb_byte *line_ptr,
20387 CORE_ADDR lowpc, CORE_ADDR address);
20388
20389 void handle_set_discriminator (unsigned int discriminator)
20390 {
20391 m_discriminator = discriminator;
20392 m_line_has_non_zero_discriminator |= discriminator != 0;
20393 }
20394
20395 /* Handle DW_LNE_set_address. */
20396 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20397 {
20398 m_op_index = 0;
20399 address += baseaddr;
20400 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20401 }
20402
20403 /* Handle DW_LNS_advance_pc. */
20404 void handle_advance_pc (CORE_ADDR adjust);
20405
20406 /* Handle a special opcode. */
20407 void handle_special_opcode (unsigned char op_code);
20408
20409 /* Handle DW_LNS_advance_line. */
20410 void handle_advance_line (int line_delta)
20411 {
20412 advance_line (line_delta);
20413 }
20414
20415 /* Handle DW_LNS_set_file. */
20416 void handle_set_file (file_name_index file);
20417
20418 /* Handle DW_LNS_negate_stmt. */
20419 void handle_negate_stmt ()
20420 {
20421 m_is_stmt = !m_is_stmt;
20422 }
20423
20424 /* Handle DW_LNS_const_add_pc. */
20425 void handle_const_add_pc ();
20426
20427 /* Handle DW_LNS_fixed_advance_pc. */
20428 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20429 {
20430 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20431 m_op_index = 0;
20432 }
20433
20434 /* Handle DW_LNS_copy. */
20435 void handle_copy ()
20436 {
20437 record_line (false);
20438 m_discriminator = 0;
20439 }
20440
20441 /* Handle DW_LNE_end_sequence. */
20442 void handle_end_sequence ()
20443 {
20444 m_record_line_callback = ::record_line;
20445 }
20446
20447 private:
20448 /* Advance the line by LINE_DELTA. */
20449 void advance_line (int line_delta)
20450 {
20451 m_line += line_delta;
20452
20453 if (line_delta != 0)
20454 m_line_has_non_zero_discriminator = m_discriminator != 0;
20455 }
20456
20457 gdbarch *m_gdbarch;
20458
20459 /* True if we're recording lines.
20460 Otherwise we're building partial symtabs and are just interested in
20461 finding include files mentioned by the line number program. */
20462 bool m_record_lines_p;
20463
20464 /* The line number header. */
20465 line_header *m_line_header;
20466
20467 /* These are part of the standard DWARF line number state machine,
20468 and initialized according to the DWARF spec. */
20469
20470 unsigned char m_op_index = 0;
20471 /* The line table index (1-based) of the current file. */
20472 file_name_index m_file = (file_name_index) 1;
20473 unsigned int m_line = 1;
20474
20475 /* These are initialized in the constructor. */
20476
20477 CORE_ADDR m_address;
20478 bool m_is_stmt;
20479 unsigned int m_discriminator;
20480
20481 /* Additional bits of state we need to track. */
20482
20483 /* The last file that we called dwarf2_start_subfile for.
20484 This is only used for TLLs. */
20485 unsigned int m_last_file = 0;
20486 /* The last file a line number was recorded for. */
20487 struct subfile *m_last_subfile = NULL;
20488
20489 /* The function to call to record a line. */
20490 record_line_ftype *m_record_line_callback = NULL;
20491
20492 /* The last line number that was recorded, used to coalesce
20493 consecutive entries for the same line. This can happen, for
20494 example, when discriminators are present. PR 17276. */
20495 unsigned int m_last_line = 0;
20496 bool m_line_has_non_zero_discriminator = false;
20497 };
20498
20499 void
20500 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20501 {
20502 CORE_ADDR addr_adj = (((m_op_index + adjust)
20503 / m_line_header->maximum_ops_per_instruction)
20504 * m_line_header->minimum_instruction_length);
20505 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20506 m_op_index = ((m_op_index + adjust)
20507 % m_line_header->maximum_ops_per_instruction);
20508 }
20509
20510 void
20511 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20512 {
20513 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20514 CORE_ADDR addr_adj = (((m_op_index
20515 + (adj_opcode / m_line_header->line_range))
20516 / m_line_header->maximum_ops_per_instruction)
20517 * m_line_header->minimum_instruction_length);
20518 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20519 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20520 % m_line_header->maximum_ops_per_instruction);
20521
20522 int line_delta = (m_line_header->line_base
20523 + (adj_opcode % m_line_header->line_range));
20524 advance_line (line_delta);
20525 record_line (false);
20526 m_discriminator = 0;
20527 }
20528
20529 void
20530 lnp_state_machine::handle_set_file (file_name_index file)
20531 {
20532 m_file = file;
20533
20534 const file_entry *fe = current_file ();
20535 if (fe == NULL)
20536 dwarf2_debug_line_missing_file_complaint ();
20537 else if (m_record_lines_p)
20538 {
20539 const char *dir = fe->include_dir (m_line_header);
20540
20541 m_last_subfile = current_subfile;
20542 m_line_has_non_zero_discriminator = m_discriminator != 0;
20543 dwarf2_start_subfile (fe->name, dir);
20544 }
20545 }
20546
20547 void
20548 lnp_state_machine::handle_const_add_pc ()
20549 {
20550 CORE_ADDR adjust
20551 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20552
20553 CORE_ADDR addr_adj
20554 = (((m_op_index + adjust)
20555 / m_line_header->maximum_ops_per_instruction)
20556 * m_line_header->minimum_instruction_length);
20557
20558 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20559 m_op_index = ((m_op_index + adjust)
20560 % m_line_header->maximum_ops_per_instruction);
20561 }
20562
20563 /* Ignore this record_line request. */
20564
20565 static void
20566 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20567 {
20568 return;
20569 }
20570
20571 /* Return non-zero if we should add LINE to the line number table.
20572 LINE is the line to add, LAST_LINE is the last line that was added,
20573 LAST_SUBFILE is the subfile for LAST_LINE.
20574 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20575 had a non-zero discriminator.
20576
20577 We have to be careful in the presence of discriminators.
20578 E.g., for this line:
20579
20580 for (i = 0; i < 100000; i++);
20581
20582 clang can emit four line number entries for that one line,
20583 each with a different discriminator.
20584 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20585
20586 However, we want gdb to coalesce all four entries into one.
20587 Otherwise the user could stepi into the middle of the line and
20588 gdb would get confused about whether the pc really was in the
20589 middle of the line.
20590
20591 Things are further complicated by the fact that two consecutive
20592 line number entries for the same line is a heuristic used by gcc
20593 to denote the end of the prologue. So we can't just discard duplicate
20594 entries, we have to be selective about it. The heuristic we use is
20595 that we only collapse consecutive entries for the same line if at least
20596 one of those entries has a non-zero discriminator. PR 17276.
20597
20598 Note: Addresses in the line number state machine can never go backwards
20599 within one sequence, thus this coalescing is ok. */
20600
20601 static int
20602 dwarf_record_line_p (unsigned int line, unsigned int last_line,
20603 int line_has_non_zero_discriminator,
20604 struct subfile *last_subfile)
20605 {
20606 if (current_subfile != last_subfile)
20607 return 1;
20608 if (line != last_line)
20609 return 1;
20610 /* Same line for the same file that we've seen already.
20611 As a last check, for pr 17276, only record the line if the line
20612 has never had a non-zero discriminator. */
20613 if (!line_has_non_zero_discriminator)
20614 return 1;
20615 return 0;
20616 }
20617
20618 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20619 in the line table of subfile SUBFILE. */
20620
20621 static void
20622 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20623 unsigned int line, CORE_ADDR address,
20624 record_line_ftype p_record_line)
20625 {
20626 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20627
20628 if (dwarf_line_debug)
20629 {
20630 fprintf_unfiltered (gdb_stdlog,
20631 "Recording line %u, file %s, address %s\n",
20632 line, lbasename (subfile->name),
20633 paddress (gdbarch, address));
20634 }
20635
20636 (*p_record_line) (subfile, line, addr);
20637 }
20638
20639 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20640 Mark the end of a set of line number records.
20641 The arguments are the same as for dwarf_record_line_1.
20642 If SUBFILE is NULL the request is ignored. */
20643
20644 static void
20645 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20646 CORE_ADDR address, record_line_ftype p_record_line)
20647 {
20648 if (subfile == NULL)
20649 return;
20650
20651 if (dwarf_line_debug)
20652 {
20653 fprintf_unfiltered (gdb_stdlog,
20654 "Finishing current line, file %s, address %s\n",
20655 lbasename (subfile->name),
20656 paddress (gdbarch, address));
20657 }
20658
20659 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20660 }
20661
20662 void
20663 lnp_state_machine::record_line (bool end_sequence)
20664 {
20665 if (dwarf_line_debug)
20666 {
20667 fprintf_unfiltered (gdb_stdlog,
20668 "Processing actual line %u: file %u,"
20669 " address %s, is_stmt %u, discrim %u\n",
20670 m_line, to_underlying (m_file),
20671 paddress (m_gdbarch, m_address),
20672 m_is_stmt, m_discriminator);
20673 }
20674
20675 file_entry *fe = current_file ();
20676
20677 if (fe == NULL)
20678 dwarf2_debug_line_missing_file_complaint ();
20679 /* For now we ignore lines not starting on an instruction boundary.
20680 But not when processing end_sequence for compatibility with the
20681 previous version of the code. */
20682 else if (m_op_index == 0 || end_sequence)
20683 {
20684 fe->included_p = 1;
20685 if (m_record_lines_p && m_is_stmt)
20686 {
20687 if (m_last_subfile != current_subfile || end_sequence)
20688 {
20689 dwarf_finish_line (m_gdbarch, m_last_subfile,
20690 m_address, m_record_line_callback);
20691 }
20692
20693 if (!end_sequence)
20694 {
20695 if (dwarf_record_line_p (m_line, m_last_line,
20696 m_line_has_non_zero_discriminator,
20697 m_last_subfile))
20698 {
20699 dwarf_record_line_1 (m_gdbarch, current_subfile,
20700 m_line, m_address,
20701 m_record_line_callback);
20702 }
20703 m_last_subfile = current_subfile;
20704 m_last_line = m_line;
20705 }
20706 }
20707 }
20708 }
20709
20710 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20711 bool record_lines_p)
20712 {
20713 m_gdbarch = arch;
20714 m_record_lines_p = record_lines_p;
20715 m_line_header = lh;
20716
20717 m_record_line_callback = ::record_line;
20718
20719 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20720 was a line entry for it so that the backend has a chance to adjust it
20721 and also record it in case it needs it. This is currently used by MIPS
20722 code, cf. `mips_adjust_dwarf2_line'. */
20723 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20724 m_is_stmt = lh->default_is_stmt;
20725 m_discriminator = 0;
20726 }
20727
20728 void
20729 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20730 const gdb_byte *line_ptr,
20731 CORE_ADDR lowpc, CORE_ADDR address)
20732 {
20733 /* If address < lowpc then it's not a usable value, it's outside the
20734 pc range of the CU. However, we restrict the test to only address
20735 values of zero to preserve GDB's previous behaviour which is to
20736 handle the specific case of a function being GC'd by the linker. */
20737
20738 if (address == 0 && address < lowpc)
20739 {
20740 /* This line table is for a function which has been
20741 GCd by the linker. Ignore it. PR gdb/12528 */
20742
20743 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20744 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20745
20746 complaint (&symfile_complaints,
20747 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20748 line_offset, objfile_name (objfile));
20749 m_record_line_callback = noop_record_line;
20750 /* Note: record_line_callback is left as noop_record_line until
20751 we see DW_LNE_end_sequence. */
20752 }
20753 }
20754
20755 /* Subroutine of dwarf_decode_lines to simplify it.
20756 Process the line number information in LH.
20757 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20758 program in order to set included_p for every referenced header. */
20759
20760 static void
20761 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20762 const int decode_for_pst_p, CORE_ADDR lowpc)
20763 {
20764 const gdb_byte *line_ptr, *extended_end;
20765 const gdb_byte *line_end;
20766 unsigned int bytes_read, extended_len;
20767 unsigned char op_code, extended_op;
20768 CORE_ADDR baseaddr;
20769 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20770 bfd *abfd = objfile->obfd;
20771 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20772 /* True if we're recording line info (as opposed to building partial
20773 symtabs and just interested in finding include files mentioned by
20774 the line number program). */
20775 bool record_lines_p = !decode_for_pst_p;
20776
20777 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20778
20779 line_ptr = lh->statement_program_start;
20780 line_end = lh->statement_program_end;
20781
20782 /* Read the statement sequences until there's nothing left. */
20783 while (line_ptr < line_end)
20784 {
20785 /* The DWARF line number program state machine. Reset the state
20786 machine at the start of each sequence. */
20787 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20788 bool end_sequence = false;
20789
20790 if (record_lines_p)
20791 {
20792 /* Start a subfile for the current file of the state
20793 machine. */
20794 const file_entry *fe = state_machine.current_file ();
20795
20796 if (fe != NULL)
20797 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
20798 }
20799
20800 /* Decode the table. */
20801 while (line_ptr < line_end && !end_sequence)
20802 {
20803 op_code = read_1_byte (abfd, line_ptr);
20804 line_ptr += 1;
20805
20806 if (op_code >= lh->opcode_base)
20807 {
20808 /* Special opcode. */
20809 state_machine.handle_special_opcode (op_code);
20810 }
20811 else switch (op_code)
20812 {
20813 case DW_LNS_extended_op:
20814 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20815 &bytes_read);
20816 line_ptr += bytes_read;
20817 extended_end = line_ptr + extended_len;
20818 extended_op = read_1_byte (abfd, line_ptr);
20819 line_ptr += 1;
20820 switch (extended_op)
20821 {
20822 case DW_LNE_end_sequence:
20823 state_machine.handle_end_sequence ();
20824 end_sequence = true;
20825 break;
20826 case DW_LNE_set_address:
20827 {
20828 CORE_ADDR address
20829 = read_address (abfd, line_ptr, cu, &bytes_read);
20830 line_ptr += bytes_read;
20831
20832 state_machine.check_line_address (cu, line_ptr,
20833 lowpc, address);
20834 state_machine.handle_set_address (baseaddr, address);
20835 }
20836 break;
20837 case DW_LNE_define_file:
20838 {
20839 const char *cur_file;
20840 unsigned int mod_time, length;
20841 dir_index dindex;
20842
20843 cur_file = read_direct_string (abfd, line_ptr,
20844 &bytes_read);
20845 line_ptr += bytes_read;
20846 dindex = (dir_index)
20847 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20848 line_ptr += bytes_read;
20849 mod_time =
20850 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20851 line_ptr += bytes_read;
20852 length =
20853 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20854 line_ptr += bytes_read;
20855 lh->add_file_name (cur_file, dindex, mod_time, length);
20856 }
20857 break;
20858 case DW_LNE_set_discriminator:
20859 {
20860 /* The discriminator is not interesting to the
20861 debugger; just ignore it. We still need to
20862 check its value though:
20863 if there are consecutive entries for the same
20864 (non-prologue) line we want to coalesce them.
20865 PR 17276. */
20866 unsigned int discr
20867 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20868 line_ptr += bytes_read;
20869
20870 state_machine.handle_set_discriminator (discr);
20871 }
20872 break;
20873 default:
20874 complaint (&symfile_complaints,
20875 _("mangled .debug_line section"));
20876 return;
20877 }
20878 /* Make sure that we parsed the extended op correctly. If e.g.
20879 we expected a different address size than the producer used,
20880 we may have read the wrong number of bytes. */
20881 if (line_ptr != extended_end)
20882 {
20883 complaint (&symfile_complaints,
20884 _("mangled .debug_line section"));
20885 return;
20886 }
20887 break;
20888 case DW_LNS_copy:
20889 state_machine.handle_copy ();
20890 break;
20891 case DW_LNS_advance_pc:
20892 {
20893 CORE_ADDR adjust
20894 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20895 line_ptr += bytes_read;
20896
20897 state_machine.handle_advance_pc (adjust);
20898 }
20899 break;
20900 case DW_LNS_advance_line:
20901 {
20902 int line_delta
20903 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20904 line_ptr += bytes_read;
20905
20906 state_machine.handle_advance_line (line_delta);
20907 }
20908 break;
20909 case DW_LNS_set_file:
20910 {
20911 file_name_index file
20912 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20913 &bytes_read);
20914 line_ptr += bytes_read;
20915
20916 state_machine.handle_set_file (file);
20917 }
20918 break;
20919 case DW_LNS_set_column:
20920 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20921 line_ptr += bytes_read;
20922 break;
20923 case DW_LNS_negate_stmt:
20924 state_machine.handle_negate_stmt ();
20925 break;
20926 case DW_LNS_set_basic_block:
20927 break;
20928 /* Add to the address register of the state machine the
20929 address increment value corresponding to special opcode
20930 255. I.e., this value is scaled by the minimum
20931 instruction length since special opcode 255 would have
20932 scaled the increment. */
20933 case DW_LNS_const_add_pc:
20934 state_machine.handle_const_add_pc ();
20935 break;
20936 case DW_LNS_fixed_advance_pc:
20937 {
20938 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20939 line_ptr += 2;
20940
20941 state_machine.handle_fixed_advance_pc (addr_adj);
20942 }
20943 break;
20944 default:
20945 {
20946 /* Unknown standard opcode, ignore it. */
20947 int i;
20948
20949 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20950 {
20951 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20952 line_ptr += bytes_read;
20953 }
20954 }
20955 }
20956 }
20957
20958 if (!end_sequence)
20959 dwarf2_debug_line_missing_end_sequence_complaint ();
20960
20961 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20962 in which case we still finish recording the last line). */
20963 state_machine.record_line (true);
20964 }
20965 }
20966
20967 /* Decode the Line Number Program (LNP) for the given line_header
20968 structure and CU. The actual information extracted and the type
20969 of structures created from the LNP depends on the value of PST.
20970
20971 1. If PST is NULL, then this procedure uses the data from the program
20972 to create all necessary symbol tables, and their linetables.
20973
20974 2. If PST is not NULL, this procedure reads the program to determine
20975 the list of files included by the unit represented by PST, and
20976 builds all the associated partial symbol tables.
20977
20978 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20979 It is used for relative paths in the line table.
20980 NOTE: When processing partial symtabs (pst != NULL),
20981 comp_dir == pst->dirname.
20982
20983 NOTE: It is important that psymtabs have the same file name (via strcmp)
20984 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20985 symtab we don't use it in the name of the psymtabs we create.
20986 E.g. expand_line_sal requires this when finding psymtabs to expand.
20987 A good testcase for this is mb-inline.exp.
20988
20989 LOWPC is the lowest address in CU (or 0 if not known).
20990
20991 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20992 for its PC<->lines mapping information. Otherwise only the filename
20993 table is read in. */
20994
20995 static void
20996 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20997 struct dwarf2_cu *cu, struct partial_symtab *pst,
20998 CORE_ADDR lowpc, int decode_mapping)
20999 {
21000 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21001 const int decode_for_pst_p = (pst != NULL);
21002
21003 if (decode_mapping)
21004 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21005
21006 if (decode_for_pst_p)
21007 {
21008 int file_index;
21009
21010 /* Now that we're done scanning the Line Header Program, we can
21011 create the psymtab of each included file. */
21012 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
21013 if (lh->file_names[file_index].included_p == 1)
21014 {
21015 gdb::unique_xmalloc_ptr<char> name_holder;
21016 const char *include_name =
21017 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21018 &name_holder);
21019 if (include_name != NULL)
21020 dwarf2_create_include_psymtab (include_name, pst, objfile);
21021 }
21022 }
21023 else
21024 {
21025 /* Make sure a symtab is created for every file, even files
21026 which contain only variables (i.e. no code with associated
21027 line numbers). */
21028 struct compunit_symtab *cust = buildsym_compunit_symtab ();
21029 int i;
21030
21031 for (i = 0; i < lh->file_names.size (); i++)
21032 {
21033 file_entry &fe = lh->file_names[i];
21034
21035 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
21036
21037 if (current_subfile->symtab == NULL)
21038 {
21039 current_subfile->symtab
21040 = allocate_symtab (cust, current_subfile->name);
21041 }
21042 fe.symtab = current_subfile->symtab;
21043 }
21044 }
21045 }
21046
21047 /* Start a subfile for DWARF. FILENAME is the name of the file and
21048 DIRNAME the name of the source directory which contains FILENAME
21049 or NULL if not known.
21050 This routine tries to keep line numbers from identical absolute and
21051 relative file names in a common subfile.
21052
21053 Using the `list' example from the GDB testsuite, which resides in
21054 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21055 of /srcdir/list0.c yields the following debugging information for list0.c:
21056
21057 DW_AT_name: /srcdir/list0.c
21058 DW_AT_comp_dir: /compdir
21059 files.files[0].name: list0.h
21060 files.files[0].dir: /srcdir
21061 files.files[1].name: list0.c
21062 files.files[1].dir: /srcdir
21063
21064 The line number information for list0.c has to end up in a single
21065 subfile, so that `break /srcdir/list0.c:1' works as expected.
21066 start_subfile will ensure that this happens provided that we pass the
21067 concatenation of files.files[1].dir and files.files[1].name as the
21068 subfile's name. */
21069
21070 static void
21071 dwarf2_start_subfile (const char *filename, const char *dirname)
21072 {
21073 char *copy = NULL;
21074
21075 /* In order not to lose the line information directory,
21076 we concatenate it to the filename when it makes sense.
21077 Note that the Dwarf3 standard says (speaking of filenames in line
21078 information): ``The directory index is ignored for file names
21079 that represent full path names''. Thus ignoring dirname in the
21080 `else' branch below isn't an issue. */
21081
21082 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21083 {
21084 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21085 filename = copy;
21086 }
21087
21088 start_subfile (filename);
21089
21090 if (copy != NULL)
21091 xfree (copy);
21092 }
21093
21094 /* Start a symtab for DWARF.
21095 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21096
21097 static struct compunit_symtab *
21098 dwarf2_start_symtab (struct dwarf2_cu *cu,
21099 const char *name, const char *comp_dir, CORE_ADDR low_pc)
21100 {
21101 struct compunit_symtab *cust
21102 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21103 low_pc, cu->language);
21104
21105 record_debugformat ("DWARF 2");
21106 record_producer (cu->producer);
21107
21108 /* We assume that we're processing GCC output. */
21109 processing_gcc_compilation = 2;
21110
21111 cu->processing_has_namespace_info = 0;
21112
21113 return cust;
21114 }
21115
21116 static void
21117 var_decode_location (struct attribute *attr, struct symbol *sym,
21118 struct dwarf2_cu *cu)
21119 {
21120 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21121 struct comp_unit_head *cu_header = &cu->header;
21122
21123 /* NOTE drow/2003-01-30: There used to be a comment and some special
21124 code here to turn a symbol with DW_AT_external and a
21125 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21126 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21127 with some versions of binutils) where shared libraries could have
21128 relocations against symbols in their debug information - the
21129 minimal symbol would have the right address, but the debug info
21130 would not. It's no longer necessary, because we will explicitly
21131 apply relocations when we read in the debug information now. */
21132
21133 /* A DW_AT_location attribute with no contents indicates that a
21134 variable has been optimized away. */
21135 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21136 {
21137 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21138 return;
21139 }
21140
21141 /* Handle one degenerate form of location expression specially, to
21142 preserve GDB's previous behavior when section offsets are
21143 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21144 then mark this symbol as LOC_STATIC. */
21145
21146 if (attr_form_is_block (attr)
21147 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21148 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21149 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21150 && (DW_BLOCK (attr)->size
21151 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21152 {
21153 unsigned int dummy;
21154
21155 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21156 SYMBOL_VALUE_ADDRESS (sym) =
21157 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21158 else
21159 SYMBOL_VALUE_ADDRESS (sym) =
21160 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21161 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21162 fixup_symbol_section (sym, objfile);
21163 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21164 SYMBOL_SECTION (sym));
21165 return;
21166 }
21167
21168 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21169 expression evaluator, and use LOC_COMPUTED only when necessary
21170 (i.e. when the value of a register or memory location is
21171 referenced, or a thread-local block, etc.). Then again, it might
21172 not be worthwhile. I'm assuming that it isn't unless performance
21173 or memory numbers show me otherwise. */
21174
21175 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21176
21177 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21178 cu->has_loclist = 1;
21179 }
21180
21181 /* Given a pointer to a DWARF information entry, figure out if we need
21182 to make a symbol table entry for it, and if so, create a new entry
21183 and return a pointer to it.
21184 If TYPE is NULL, determine symbol type from the die, otherwise
21185 used the passed type.
21186 If SPACE is not NULL, use it to hold the new symbol. If it is
21187 NULL, allocate a new symbol on the objfile's obstack. */
21188
21189 static struct symbol *
21190 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21191 struct symbol *space)
21192 {
21193 struct dwarf2_per_objfile *dwarf2_per_objfile
21194 = cu->per_cu->dwarf2_per_objfile;
21195 struct objfile *objfile = dwarf2_per_objfile->objfile;
21196 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21197 struct symbol *sym = NULL;
21198 const char *name;
21199 struct attribute *attr = NULL;
21200 struct attribute *attr2 = NULL;
21201 CORE_ADDR baseaddr;
21202 struct pending **list_to_add = NULL;
21203
21204 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21205
21206 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21207
21208 name = dwarf2_name (die, cu);
21209 if (name)
21210 {
21211 const char *linkagename;
21212 int suppress_add = 0;
21213
21214 if (space)
21215 sym = space;
21216 else
21217 sym = allocate_symbol (objfile);
21218 OBJSTAT (objfile, n_syms++);
21219
21220 /* Cache this symbol's name and the name's demangled form (if any). */
21221 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21222 linkagename = dwarf2_physname (name, die, cu);
21223 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21224
21225 /* Fortran does not have mangling standard and the mangling does differ
21226 between gfortran, iFort etc. */
21227 if (cu->language == language_fortran
21228 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21229 symbol_set_demangled_name (&(sym->ginfo),
21230 dwarf2_full_name (name, die, cu),
21231 NULL);
21232
21233 /* Default assumptions.
21234 Use the passed type or decode it from the die. */
21235 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21236 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21237 if (type != NULL)
21238 SYMBOL_TYPE (sym) = type;
21239 else
21240 SYMBOL_TYPE (sym) = die_type (die, cu);
21241 attr = dwarf2_attr (die,
21242 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21243 cu);
21244 if (attr)
21245 {
21246 SYMBOL_LINE (sym) = DW_UNSND (attr);
21247 }
21248
21249 attr = dwarf2_attr (die,
21250 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21251 cu);
21252 if (attr)
21253 {
21254 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21255 struct file_entry *fe;
21256
21257 if (cu->line_header != NULL)
21258 fe = cu->line_header->file_name_at (file_index);
21259 else
21260 fe = NULL;
21261
21262 if (fe == NULL)
21263 complaint (&symfile_complaints,
21264 _("file index out of range"));
21265 else
21266 symbol_set_symtab (sym, fe->symtab);
21267 }
21268
21269 switch (die->tag)
21270 {
21271 case DW_TAG_label:
21272 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21273 if (attr)
21274 {
21275 CORE_ADDR addr;
21276
21277 addr = attr_value_as_address (attr);
21278 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21279 SYMBOL_VALUE_ADDRESS (sym) = addr;
21280 }
21281 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21282 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21283 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21284 add_symbol_to_list (sym, cu->list_in_scope);
21285 break;
21286 case DW_TAG_subprogram:
21287 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21288 finish_block. */
21289 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21290 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21291 if ((attr2 && (DW_UNSND (attr2) != 0))
21292 || cu->language == language_ada)
21293 {
21294 /* Subprograms marked external are stored as a global symbol.
21295 Ada subprograms, whether marked external or not, are always
21296 stored as a global symbol, because we want to be able to
21297 access them globally. For instance, we want to be able
21298 to break on a nested subprogram without having to
21299 specify the context. */
21300 list_to_add = &global_symbols;
21301 }
21302 else
21303 {
21304 list_to_add = cu->list_in_scope;
21305 }
21306 break;
21307 case DW_TAG_inlined_subroutine:
21308 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21309 finish_block. */
21310 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21311 SYMBOL_INLINED (sym) = 1;
21312 list_to_add = cu->list_in_scope;
21313 break;
21314 case DW_TAG_template_value_param:
21315 suppress_add = 1;
21316 /* Fall through. */
21317 case DW_TAG_constant:
21318 case DW_TAG_variable:
21319 case DW_TAG_member:
21320 /* Compilation with minimal debug info may result in
21321 variables with missing type entries. Change the
21322 misleading `void' type to something sensible. */
21323 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21324 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21325
21326 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21327 /* In the case of DW_TAG_member, we should only be called for
21328 static const members. */
21329 if (die->tag == DW_TAG_member)
21330 {
21331 /* dwarf2_add_field uses die_is_declaration,
21332 so we do the same. */
21333 gdb_assert (die_is_declaration (die, cu));
21334 gdb_assert (attr);
21335 }
21336 if (attr)
21337 {
21338 dwarf2_const_value (attr, sym, cu);
21339 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21340 if (!suppress_add)
21341 {
21342 if (attr2 && (DW_UNSND (attr2) != 0))
21343 list_to_add = &global_symbols;
21344 else
21345 list_to_add = cu->list_in_scope;
21346 }
21347 break;
21348 }
21349 attr = dwarf2_attr (die, DW_AT_location, cu);
21350 if (attr)
21351 {
21352 var_decode_location (attr, sym, cu);
21353 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21354
21355 /* Fortran explicitly imports any global symbols to the local
21356 scope by DW_TAG_common_block. */
21357 if (cu->language == language_fortran && die->parent
21358 && die->parent->tag == DW_TAG_common_block)
21359 attr2 = NULL;
21360
21361 if (SYMBOL_CLASS (sym) == LOC_STATIC
21362 && SYMBOL_VALUE_ADDRESS (sym) == 0
21363 && !dwarf2_per_objfile->has_section_at_zero)
21364 {
21365 /* When a static variable is eliminated by the linker,
21366 the corresponding debug information is not stripped
21367 out, but the variable address is set to null;
21368 do not add such variables into symbol table. */
21369 }
21370 else if (attr2 && (DW_UNSND (attr2) != 0))
21371 {
21372 /* Workaround gfortran PR debug/40040 - it uses
21373 DW_AT_location for variables in -fPIC libraries which may
21374 get overriden by other libraries/executable and get
21375 a different address. Resolve it by the minimal symbol
21376 which may come from inferior's executable using copy
21377 relocation. Make this workaround only for gfortran as for
21378 other compilers GDB cannot guess the minimal symbol
21379 Fortran mangling kind. */
21380 if (cu->language == language_fortran && die->parent
21381 && die->parent->tag == DW_TAG_module
21382 && cu->producer
21383 && startswith (cu->producer, "GNU Fortran"))
21384 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21385
21386 /* A variable with DW_AT_external is never static,
21387 but it may be block-scoped. */
21388 list_to_add = (cu->list_in_scope == &file_symbols
21389 ? &global_symbols : cu->list_in_scope);
21390 }
21391 else
21392 list_to_add = cu->list_in_scope;
21393 }
21394 else
21395 {
21396 /* We do not know the address of this symbol.
21397 If it is an external symbol and we have type information
21398 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21399 The address of the variable will then be determined from
21400 the minimal symbol table whenever the variable is
21401 referenced. */
21402 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21403
21404 /* Fortran explicitly imports any global symbols to the local
21405 scope by DW_TAG_common_block. */
21406 if (cu->language == language_fortran && die->parent
21407 && die->parent->tag == DW_TAG_common_block)
21408 {
21409 /* SYMBOL_CLASS doesn't matter here because
21410 read_common_block is going to reset it. */
21411 if (!suppress_add)
21412 list_to_add = cu->list_in_scope;
21413 }
21414 else if (attr2 && (DW_UNSND (attr2) != 0)
21415 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21416 {
21417 /* A variable with DW_AT_external is never static, but it
21418 may be block-scoped. */
21419 list_to_add = (cu->list_in_scope == &file_symbols
21420 ? &global_symbols : cu->list_in_scope);
21421
21422 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21423 }
21424 else if (!die_is_declaration (die, cu))
21425 {
21426 /* Use the default LOC_OPTIMIZED_OUT class. */
21427 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21428 if (!suppress_add)
21429 list_to_add = cu->list_in_scope;
21430 }
21431 }
21432 break;
21433 case DW_TAG_formal_parameter:
21434 /* If we are inside a function, mark this as an argument. If
21435 not, we might be looking at an argument to an inlined function
21436 when we do not have enough information to show inlined frames;
21437 pretend it's a local variable in that case so that the user can
21438 still see it. */
21439 if (context_stack_depth > 0
21440 && context_stack[context_stack_depth - 1].name != NULL)
21441 SYMBOL_IS_ARGUMENT (sym) = 1;
21442 attr = dwarf2_attr (die, DW_AT_location, cu);
21443 if (attr)
21444 {
21445 var_decode_location (attr, sym, cu);
21446 }
21447 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21448 if (attr)
21449 {
21450 dwarf2_const_value (attr, sym, cu);
21451 }
21452
21453 list_to_add = cu->list_in_scope;
21454 break;
21455 case DW_TAG_unspecified_parameters:
21456 /* From varargs functions; gdb doesn't seem to have any
21457 interest in this information, so just ignore it for now.
21458 (FIXME?) */
21459 break;
21460 case DW_TAG_template_type_param:
21461 suppress_add = 1;
21462 /* Fall through. */
21463 case DW_TAG_class_type:
21464 case DW_TAG_interface_type:
21465 case DW_TAG_structure_type:
21466 case DW_TAG_union_type:
21467 case DW_TAG_set_type:
21468 case DW_TAG_enumeration_type:
21469 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21470 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21471
21472 {
21473 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21474 really ever be static objects: otherwise, if you try
21475 to, say, break of a class's method and you're in a file
21476 which doesn't mention that class, it won't work unless
21477 the check for all static symbols in lookup_symbol_aux
21478 saves you. See the OtherFileClass tests in
21479 gdb.c++/namespace.exp. */
21480
21481 if (!suppress_add)
21482 {
21483 list_to_add = (cu->list_in_scope == &file_symbols
21484 && cu->language == language_cplus
21485 ? &global_symbols : cu->list_in_scope);
21486
21487 /* The semantics of C++ state that "struct foo {
21488 ... }" also defines a typedef for "foo". */
21489 if (cu->language == language_cplus
21490 || cu->language == language_ada
21491 || cu->language == language_d
21492 || cu->language == language_rust)
21493 {
21494 /* The symbol's name is already allocated along
21495 with this objfile, so we don't need to
21496 duplicate it for the type. */
21497 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21498 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21499 }
21500 }
21501 }
21502 break;
21503 case DW_TAG_typedef:
21504 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21505 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21506 list_to_add = cu->list_in_scope;
21507 break;
21508 case DW_TAG_base_type:
21509 case DW_TAG_subrange_type:
21510 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21511 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21512 list_to_add = cu->list_in_scope;
21513 break;
21514 case DW_TAG_enumerator:
21515 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21516 if (attr)
21517 {
21518 dwarf2_const_value (attr, sym, cu);
21519 }
21520 {
21521 /* NOTE: carlton/2003-11-10: See comment above in the
21522 DW_TAG_class_type, etc. block. */
21523
21524 list_to_add = (cu->list_in_scope == &file_symbols
21525 && cu->language == language_cplus
21526 ? &global_symbols : cu->list_in_scope);
21527 }
21528 break;
21529 case DW_TAG_imported_declaration:
21530 case DW_TAG_namespace:
21531 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21532 list_to_add = &global_symbols;
21533 break;
21534 case DW_TAG_module:
21535 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21536 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21537 list_to_add = &global_symbols;
21538 break;
21539 case DW_TAG_common_block:
21540 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21541 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21542 add_symbol_to_list (sym, cu->list_in_scope);
21543 break;
21544 default:
21545 /* Not a tag we recognize. Hopefully we aren't processing
21546 trash data, but since we must specifically ignore things
21547 we don't recognize, there is nothing else we should do at
21548 this point. */
21549 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
21550 dwarf_tag_name (die->tag));
21551 break;
21552 }
21553
21554 if (suppress_add)
21555 {
21556 sym->hash_next = objfile->template_symbols;
21557 objfile->template_symbols = sym;
21558 list_to_add = NULL;
21559 }
21560
21561 if (list_to_add != NULL)
21562 add_symbol_to_list (sym, list_to_add);
21563
21564 /* For the benefit of old versions of GCC, check for anonymous
21565 namespaces based on the demangled name. */
21566 if (!cu->processing_has_namespace_info
21567 && cu->language == language_cplus)
21568 cp_scan_for_anonymous_namespaces (sym, objfile);
21569 }
21570 return (sym);
21571 }
21572
21573 /* Given an attr with a DW_FORM_dataN value in host byte order,
21574 zero-extend it as appropriate for the symbol's type. The DWARF
21575 standard (v4) is not entirely clear about the meaning of using
21576 DW_FORM_dataN for a constant with a signed type, where the type is
21577 wider than the data. The conclusion of a discussion on the DWARF
21578 list was that this is unspecified. We choose to always zero-extend
21579 because that is the interpretation long in use by GCC. */
21580
21581 static gdb_byte *
21582 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21583 struct dwarf2_cu *cu, LONGEST *value, int bits)
21584 {
21585 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21586 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21587 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21588 LONGEST l = DW_UNSND (attr);
21589
21590 if (bits < sizeof (*value) * 8)
21591 {
21592 l &= ((LONGEST) 1 << bits) - 1;
21593 *value = l;
21594 }
21595 else if (bits == sizeof (*value) * 8)
21596 *value = l;
21597 else
21598 {
21599 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21600 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21601 return bytes;
21602 }
21603
21604 return NULL;
21605 }
21606
21607 /* Read a constant value from an attribute. Either set *VALUE, or if
21608 the value does not fit in *VALUE, set *BYTES - either already
21609 allocated on the objfile obstack, or newly allocated on OBSTACK,
21610 or, set *BATON, if we translated the constant to a location
21611 expression. */
21612
21613 static void
21614 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21615 const char *name, struct obstack *obstack,
21616 struct dwarf2_cu *cu,
21617 LONGEST *value, const gdb_byte **bytes,
21618 struct dwarf2_locexpr_baton **baton)
21619 {
21620 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21621 struct comp_unit_head *cu_header = &cu->header;
21622 struct dwarf_block *blk;
21623 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21624 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21625
21626 *value = 0;
21627 *bytes = NULL;
21628 *baton = NULL;
21629
21630 switch (attr->form)
21631 {
21632 case DW_FORM_addr:
21633 case DW_FORM_GNU_addr_index:
21634 {
21635 gdb_byte *data;
21636
21637 if (TYPE_LENGTH (type) != cu_header->addr_size)
21638 dwarf2_const_value_length_mismatch_complaint (name,
21639 cu_header->addr_size,
21640 TYPE_LENGTH (type));
21641 /* Symbols of this form are reasonably rare, so we just
21642 piggyback on the existing location code rather than writing
21643 a new implementation of symbol_computed_ops. */
21644 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21645 (*baton)->per_cu = cu->per_cu;
21646 gdb_assert ((*baton)->per_cu);
21647
21648 (*baton)->size = 2 + cu_header->addr_size;
21649 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21650 (*baton)->data = data;
21651
21652 data[0] = DW_OP_addr;
21653 store_unsigned_integer (&data[1], cu_header->addr_size,
21654 byte_order, DW_ADDR (attr));
21655 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21656 }
21657 break;
21658 case DW_FORM_string:
21659 case DW_FORM_strp:
21660 case DW_FORM_GNU_str_index:
21661 case DW_FORM_GNU_strp_alt:
21662 /* DW_STRING is already allocated on the objfile obstack, point
21663 directly to it. */
21664 *bytes = (const gdb_byte *) DW_STRING (attr);
21665 break;
21666 case DW_FORM_block1:
21667 case DW_FORM_block2:
21668 case DW_FORM_block4:
21669 case DW_FORM_block:
21670 case DW_FORM_exprloc:
21671 case DW_FORM_data16:
21672 blk = DW_BLOCK (attr);
21673 if (TYPE_LENGTH (type) != blk->size)
21674 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21675 TYPE_LENGTH (type));
21676 *bytes = blk->data;
21677 break;
21678
21679 /* The DW_AT_const_value attributes are supposed to carry the
21680 symbol's value "represented as it would be on the target
21681 architecture." By the time we get here, it's already been
21682 converted to host endianness, so we just need to sign- or
21683 zero-extend it as appropriate. */
21684 case DW_FORM_data1:
21685 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21686 break;
21687 case DW_FORM_data2:
21688 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21689 break;
21690 case DW_FORM_data4:
21691 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21692 break;
21693 case DW_FORM_data8:
21694 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21695 break;
21696
21697 case DW_FORM_sdata:
21698 case DW_FORM_implicit_const:
21699 *value = DW_SND (attr);
21700 break;
21701
21702 case DW_FORM_udata:
21703 *value = DW_UNSND (attr);
21704 break;
21705
21706 default:
21707 complaint (&symfile_complaints,
21708 _("unsupported const value attribute form: '%s'"),
21709 dwarf_form_name (attr->form));
21710 *value = 0;
21711 break;
21712 }
21713 }
21714
21715
21716 /* Copy constant value from an attribute to a symbol. */
21717
21718 static void
21719 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21720 struct dwarf2_cu *cu)
21721 {
21722 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21723 LONGEST value;
21724 const gdb_byte *bytes;
21725 struct dwarf2_locexpr_baton *baton;
21726
21727 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21728 SYMBOL_PRINT_NAME (sym),
21729 &objfile->objfile_obstack, cu,
21730 &value, &bytes, &baton);
21731
21732 if (baton != NULL)
21733 {
21734 SYMBOL_LOCATION_BATON (sym) = baton;
21735 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21736 }
21737 else if (bytes != NULL)
21738 {
21739 SYMBOL_VALUE_BYTES (sym) = bytes;
21740 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21741 }
21742 else
21743 {
21744 SYMBOL_VALUE (sym) = value;
21745 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21746 }
21747 }
21748
21749 /* Return the type of the die in question using its DW_AT_type attribute. */
21750
21751 static struct type *
21752 die_type (struct die_info *die, struct dwarf2_cu *cu)
21753 {
21754 struct attribute *type_attr;
21755
21756 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21757 if (!type_attr)
21758 {
21759 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21760 /* A missing DW_AT_type represents a void type. */
21761 return objfile_type (objfile)->builtin_void;
21762 }
21763
21764 return lookup_die_type (die, type_attr, cu);
21765 }
21766
21767 /* True iff CU's producer generates GNAT Ada auxiliary information
21768 that allows to find parallel types through that information instead
21769 of having to do expensive parallel lookups by type name. */
21770
21771 static int
21772 need_gnat_info (struct dwarf2_cu *cu)
21773 {
21774 /* Assume that the Ada compiler was GNAT, which always produces
21775 the auxiliary information. */
21776 return (cu->language == language_ada);
21777 }
21778
21779 /* Return the auxiliary type of the die in question using its
21780 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21781 attribute is not present. */
21782
21783 static struct type *
21784 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21785 {
21786 struct attribute *type_attr;
21787
21788 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21789 if (!type_attr)
21790 return NULL;
21791
21792 return lookup_die_type (die, type_attr, cu);
21793 }
21794
21795 /* If DIE has a descriptive_type attribute, then set the TYPE's
21796 descriptive type accordingly. */
21797
21798 static void
21799 set_descriptive_type (struct type *type, struct die_info *die,
21800 struct dwarf2_cu *cu)
21801 {
21802 struct type *descriptive_type = die_descriptive_type (die, cu);
21803
21804 if (descriptive_type)
21805 {
21806 ALLOCATE_GNAT_AUX_TYPE (type);
21807 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21808 }
21809 }
21810
21811 /* Return the containing type of the die in question using its
21812 DW_AT_containing_type attribute. */
21813
21814 static struct type *
21815 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21816 {
21817 struct attribute *type_attr;
21818 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21819
21820 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21821 if (!type_attr)
21822 error (_("Dwarf Error: Problem turning containing type into gdb type "
21823 "[in module %s]"), objfile_name (objfile));
21824
21825 return lookup_die_type (die, type_attr, cu);
21826 }
21827
21828 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21829
21830 static struct type *
21831 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21832 {
21833 struct dwarf2_per_objfile *dwarf2_per_objfile
21834 = cu->per_cu->dwarf2_per_objfile;
21835 struct objfile *objfile = dwarf2_per_objfile->objfile;
21836 char *message, *saved;
21837
21838 message = xstrprintf (_("<unknown type in %s, CU %s, DIE %s>"),
21839 objfile_name (objfile),
21840 sect_offset_str (cu->header.sect_off),
21841 sect_offset_str (die->sect_off));
21842 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21843 message, strlen (message));
21844 xfree (message);
21845
21846 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21847 }
21848
21849 /* Look up the type of DIE in CU using its type attribute ATTR.
21850 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21851 DW_AT_containing_type.
21852 If there is no type substitute an error marker. */
21853
21854 static struct type *
21855 lookup_die_type (struct die_info *die, const struct attribute *attr,
21856 struct dwarf2_cu *cu)
21857 {
21858 struct dwarf2_per_objfile *dwarf2_per_objfile
21859 = cu->per_cu->dwarf2_per_objfile;
21860 struct objfile *objfile = dwarf2_per_objfile->objfile;
21861 struct type *this_type;
21862
21863 gdb_assert (attr->name == DW_AT_type
21864 || attr->name == DW_AT_GNAT_descriptive_type
21865 || attr->name == DW_AT_containing_type);
21866
21867 /* First see if we have it cached. */
21868
21869 if (attr->form == DW_FORM_GNU_ref_alt)
21870 {
21871 struct dwarf2_per_cu_data *per_cu;
21872 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21873
21874 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21875 dwarf2_per_objfile);
21876 this_type = get_die_type_at_offset (sect_off, per_cu);
21877 }
21878 else if (attr_form_is_ref (attr))
21879 {
21880 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21881
21882 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21883 }
21884 else if (attr->form == DW_FORM_ref_sig8)
21885 {
21886 ULONGEST signature = DW_SIGNATURE (attr);
21887
21888 return get_signatured_type (die, signature, cu);
21889 }
21890 else
21891 {
21892 complaint (&symfile_complaints,
21893 _("Dwarf Error: Bad type attribute %s in DIE"
21894 " at %s [in module %s]"),
21895 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21896 objfile_name (objfile));
21897 return build_error_marker_type (cu, die);
21898 }
21899
21900 /* If not cached we need to read it in. */
21901
21902 if (this_type == NULL)
21903 {
21904 struct die_info *type_die = NULL;
21905 struct dwarf2_cu *type_cu = cu;
21906
21907 if (attr_form_is_ref (attr))
21908 type_die = follow_die_ref (die, attr, &type_cu);
21909 if (type_die == NULL)
21910 return build_error_marker_type (cu, die);
21911 /* If we find the type now, it's probably because the type came
21912 from an inter-CU reference and the type's CU got expanded before
21913 ours. */
21914 this_type = read_type_die (type_die, type_cu);
21915 }
21916
21917 /* If we still don't have a type use an error marker. */
21918
21919 if (this_type == NULL)
21920 return build_error_marker_type (cu, die);
21921
21922 return this_type;
21923 }
21924
21925 /* Return the type in DIE, CU.
21926 Returns NULL for invalid types.
21927
21928 This first does a lookup in die_type_hash,
21929 and only reads the die in if necessary.
21930
21931 NOTE: This can be called when reading in partial or full symbols. */
21932
21933 static struct type *
21934 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21935 {
21936 struct type *this_type;
21937
21938 this_type = get_die_type (die, cu);
21939 if (this_type)
21940 return this_type;
21941
21942 return read_type_die_1 (die, cu);
21943 }
21944
21945 /* Read the type in DIE, CU.
21946 Returns NULL for invalid types. */
21947
21948 static struct type *
21949 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21950 {
21951 struct type *this_type = NULL;
21952
21953 switch (die->tag)
21954 {
21955 case DW_TAG_class_type:
21956 case DW_TAG_interface_type:
21957 case DW_TAG_structure_type:
21958 case DW_TAG_union_type:
21959 this_type = read_structure_type (die, cu);
21960 break;
21961 case DW_TAG_enumeration_type:
21962 this_type = read_enumeration_type (die, cu);
21963 break;
21964 case DW_TAG_subprogram:
21965 case DW_TAG_subroutine_type:
21966 case DW_TAG_inlined_subroutine:
21967 this_type = read_subroutine_type (die, cu);
21968 break;
21969 case DW_TAG_array_type:
21970 this_type = read_array_type (die, cu);
21971 break;
21972 case DW_TAG_set_type:
21973 this_type = read_set_type (die, cu);
21974 break;
21975 case DW_TAG_pointer_type:
21976 this_type = read_tag_pointer_type (die, cu);
21977 break;
21978 case DW_TAG_ptr_to_member_type:
21979 this_type = read_tag_ptr_to_member_type (die, cu);
21980 break;
21981 case DW_TAG_reference_type:
21982 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21983 break;
21984 case DW_TAG_rvalue_reference_type:
21985 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21986 break;
21987 case DW_TAG_const_type:
21988 this_type = read_tag_const_type (die, cu);
21989 break;
21990 case DW_TAG_volatile_type:
21991 this_type = read_tag_volatile_type (die, cu);
21992 break;
21993 case DW_TAG_restrict_type:
21994 this_type = read_tag_restrict_type (die, cu);
21995 break;
21996 case DW_TAG_string_type:
21997 this_type = read_tag_string_type (die, cu);
21998 break;
21999 case DW_TAG_typedef:
22000 this_type = read_typedef (die, cu);
22001 break;
22002 case DW_TAG_subrange_type:
22003 this_type = read_subrange_type (die, cu);
22004 break;
22005 case DW_TAG_base_type:
22006 this_type = read_base_type (die, cu);
22007 break;
22008 case DW_TAG_unspecified_type:
22009 this_type = read_unspecified_type (die, cu);
22010 break;
22011 case DW_TAG_namespace:
22012 this_type = read_namespace_type (die, cu);
22013 break;
22014 case DW_TAG_module:
22015 this_type = read_module_type (die, cu);
22016 break;
22017 case DW_TAG_atomic_type:
22018 this_type = read_tag_atomic_type (die, cu);
22019 break;
22020 default:
22021 complaint (&symfile_complaints,
22022 _("unexpected tag in read_type_die: '%s'"),
22023 dwarf_tag_name (die->tag));
22024 break;
22025 }
22026
22027 return this_type;
22028 }
22029
22030 /* See if we can figure out if the class lives in a namespace. We do
22031 this by looking for a member function; its demangled name will
22032 contain namespace info, if there is any.
22033 Return the computed name or NULL.
22034 Space for the result is allocated on the objfile's obstack.
22035 This is the full-die version of guess_partial_die_structure_name.
22036 In this case we know DIE has no useful parent. */
22037
22038 static char *
22039 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22040 {
22041 struct die_info *spec_die;
22042 struct dwarf2_cu *spec_cu;
22043 struct die_info *child;
22044 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22045
22046 spec_cu = cu;
22047 spec_die = die_specification (die, &spec_cu);
22048 if (spec_die != NULL)
22049 {
22050 die = spec_die;
22051 cu = spec_cu;
22052 }
22053
22054 for (child = die->child;
22055 child != NULL;
22056 child = child->sibling)
22057 {
22058 if (child->tag == DW_TAG_subprogram)
22059 {
22060 const char *linkage_name = dw2_linkage_name (child, cu);
22061
22062 if (linkage_name != NULL)
22063 {
22064 char *actual_name
22065 = language_class_name_from_physname (cu->language_defn,
22066 linkage_name);
22067 char *name = NULL;
22068
22069 if (actual_name != NULL)
22070 {
22071 const char *die_name = dwarf2_name (die, cu);
22072
22073 if (die_name != NULL
22074 && strcmp (die_name, actual_name) != 0)
22075 {
22076 /* Strip off the class name from the full name.
22077 We want the prefix. */
22078 int die_name_len = strlen (die_name);
22079 int actual_name_len = strlen (actual_name);
22080
22081 /* Test for '::' as a sanity check. */
22082 if (actual_name_len > die_name_len + 2
22083 && actual_name[actual_name_len
22084 - die_name_len - 1] == ':')
22085 name = (char *) obstack_copy0 (
22086 &objfile->per_bfd->storage_obstack,
22087 actual_name, actual_name_len - die_name_len - 2);
22088 }
22089 }
22090 xfree (actual_name);
22091 return name;
22092 }
22093 }
22094 }
22095
22096 return NULL;
22097 }
22098
22099 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22100 prefix part in such case. See
22101 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22102
22103 static const char *
22104 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22105 {
22106 struct attribute *attr;
22107 const char *base;
22108
22109 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22110 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22111 return NULL;
22112
22113 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22114 return NULL;
22115
22116 attr = dw2_linkage_name_attr (die, cu);
22117 if (attr == NULL || DW_STRING (attr) == NULL)
22118 return NULL;
22119
22120 /* dwarf2_name had to be already called. */
22121 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22122
22123 /* Strip the base name, keep any leading namespaces/classes. */
22124 base = strrchr (DW_STRING (attr), ':');
22125 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22126 return "";
22127
22128 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22129 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
22130 DW_STRING (attr),
22131 &base[-1] - DW_STRING (attr));
22132 }
22133
22134 /* Return the name of the namespace/class that DIE is defined within,
22135 or "" if we can't tell. The caller should not xfree the result.
22136
22137 For example, if we're within the method foo() in the following
22138 code:
22139
22140 namespace N {
22141 class C {
22142 void foo () {
22143 }
22144 };
22145 }
22146
22147 then determine_prefix on foo's die will return "N::C". */
22148
22149 static const char *
22150 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22151 {
22152 struct dwarf2_per_objfile *dwarf2_per_objfile
22153 = cu->per_cu->dwarf2_per_objfile;
22154 struct die_info *parent, *spec_die;
22155 struct dwarf2_cu *spec_cu;
22156 struct type *parent_type;
22157 const char *retval;
22158
22159 if (cu->language != language_cplus
22160 && cu->language != language_fortran && cu->language != language_d
22161 && cu->language != language_rust)
22162 return "";
22163
22164 retval = anonymous_struct_prefix (die, cu);
22165 if (retval)
22166 return retval;
22167
22168 /* We have to be careful in the presence of DW_AT_specification.
22169 For example, with GCC 3.4, given the code
22170
22171 namespace N {
22172 void foo() {
22173 // Definition of N::foo.
22174 }
22175 }
22176
22177 then we'll have a tree of DIEs like this:
22178
22179 1: DW_TAG_compile_unit
22180 2: DW_TAG_namespace // N
22181 3: DW_TAG_subprogram // declaration of N::foo
22182 4: DW_TAG_subprogram // definition of N::foo
22183 DW_AT_specification // refers to die #3
22184
22185 Thus, when processing die #4, we have to pretend that we're in
22186 the context of its DW_AT_specification, namely the contex of die
22187 #3. */
22188 spec_cu = cu;
22189 spec_die = die_specification (die, &spec_cu);
22190 if (spec_die == NULL)
22191 parent = die->parent;
22192 else
22193 {
22194 parent = spec_die->parent;
22195 cu = spec_cu;
22196 }
22197
22198 if (parent == NULL)
22199 return "";
22200 else if (parent->building_fullname)
22201 {
22202 const char *name;
22203 const char *parent_name;
22204
22205 /* It has been seen on RealView 2.2 built binaries,
22206 DW_TAG_template_type_param types actually _defined_ as
22207 children of the parent class:
22208
22209 enum E {};
22210 template class <class Enum> Class{};
22211 Class<enum E> class_e;
22212
22213 1: DW_TAG_class_type (Class)
22214 2: DW_TAG_enumeration_type (E)
22215 3: DW_TAG_enumerator (enum1:0)
22216 3: DW_TAG_enumerator (enum2:1)
22217 ...
22218 2: DW_TAG_template_type_param
22219 DW_AT_type DW_FORM_ref_udata (E)
22220
22221 Besides being broken debug info, it can put GDB into an
22222 infinite loop. Consider:
22223
22224 When we're building the full name for Class<E>, we'll start
22225 at Class, and go look over its template type parameters,
22226 finding E. We'll then try to build the full name of E, and
22227 reach here. We're now trying to build the full name of E,
22228 and look over the parent DIE for containing scope. In the
22229 broken case, if we followed the parent DIE of E, we'd again
22230 find Class, and once again go look at its template type
22231 arguments, etc., etc. Simply don't consider such parent die
22232 as source-level parent of this die (it can't be, the language
22233 doesn't allow it), and break the loop here. */
22234 name = dwarf2_name (die, cu);
22235 parent_name = dwarf2_name (parent, cu);
22236 complaint (&symfile_complaints,
22237 _("template param type '%s' defined within parent '%s'"),
22238 name ? name : "<unknown>",
22239 parent_name ? parent_name : "<unknown>");
22240 return "";
22241 }
22242 else
22243 switch (parent->tag)
22244 {
22245 case DW_TAG_namespace:
22246 parent_type = read_type_die (parent, cu);
22247 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22248 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22249 Work around this problem here. */
22250 if (cu->language == language_cplus
22251 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22252 return "";
22253 /* We give a name to even anonymous namespaces. */
22254 return TYPE_TAG_NAME (parent_type);
22255 case DW_TAG_class_type:
22256 case DW_TAG_interface_type:
22257 case DW_TAG_structure_type:
22258 case DW_TAG_union_type:
22259 case DW_TAG_module:
22260 parent_type = read_type_die (parent, cu);
22261 if (TYPE_TAG_NAME (parent_type) != NULL)
22262 return TYPE_TAG_NAME (parent_type);
22263 else
22264 /* An anonymous structure is only allowed non-static data
22265 members; no typedefs, no member functions, et cetera.
22266 So it does not need a prefix. */
22267 return "";
22268 case DW_TAG_compile_unit:
22269 case DW_TAG_partial_unit:
22270 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22271 if (cu->language == language_cplus
22272 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
22273 && die->child != NULL
22274 && (die->tag == DW_TAG_class_type
22275 || die->tag == DW_TAG_structure_type
22276 || die->tag == DW_TAG_union_type))
22277 {
22278 char *name = guess_full_die_structure_name (die, cu);
22279 if (name != NULL)
22280 return name;
22281 }
22282 return "";
22283 case DW_TAG_enumeration_type:
22284 parent_type = read_type_die (parent, cu);
22285 if (TYPE_DECLARED_CLASS (parent_type))
22286 {
22287 if (TYPE_TAG_NAME (parent_type) != NULL)
22288 return TYPE_TAG_NAME (parent_type);
22289 return "";
22290 }
22291 /* Fall through. */
22292 default:
22293 return determine_prefix (parent, cu);
22294 }
22295 }
22296
22297 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22298 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22299 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22300 an obconcat, otherwise allocate storage for the result. The CU argument is
22301 used to determine the language and hence, the appropriate separator. */
22302
22303 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22304
22305 static char *
22306 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22307 int physname, struct dwarf2_cu *cu)
22308 {
22309 const char *lead = "";
22310 const char *sep;
22311
22312 if (suffix == NULL || suffix[0] == '\0'
22313 || prefix == NULL || prefix[0] == '\0')
22314 sep = "";
22315 else if (cu->language == language_d)
22316 {
22317 /* For D, the 'main' function could be defined in any module, but it
22318 should never be prefixed. */
22319 if (strcmp (suffix, "D main") == 0)
22320 {
22321 prefix = "";
22322 sep = "";
22323 }
22324 else
22325 sep = ".";
22326 }
22327 else if (cu->language == language_fortran && physname)
22328 {
22329 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22330 DW_AT_MIPS_linkage_name is preferred and used instead. */
22331
22332 lead = "__";
22333 sep = "_MOD_";
22334 }
22335 else
22336 sep = "::";
22337
22338 if (prefix == NULL)
22339 prefix = "";
22340 if (suffix == NULL)
22341 suffix = "";
22342
22343 if (obs == NULL)
22344 {
22345 char *retval
22346 = ((char *)
22347 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22348
22349 strcpy (retval, lead);
22350 strcat (retval, prefix);
22351 strcat (retval, sep);
22352 strcat (retval, suffix);
22353 return retval;
22354 }
22355 else
22356 {
22357 /* We have an obstack. */
22358 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22359 }
22360 }
22361
22362 /* Return sibling of die, NULL if no sibling. */
22363
22364 static struct die_info *
22365 sibling_die (struct die_info *die)
22366 {
22367 return die->sibling;
22368 }
22369
22370 /* Get name of a die, return NULL if not found. */
22371
22372 static const char *
22373 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22374 struct obstack *obstack)
22375 {
22376 if (name && cu->language == language_cplus)
22377 {
22378 std::string canon_name = cp_canonicalize_string (name);
22379
22380 if (!canon_name.empty ())
22381 {
22382 if (canon_name != name)
22383 name = (const char *) obstack_copy0 (obstack,
22384 canon_name.c_str (),
22385 canon_name.length ());
22386 }
22387 }
22388
22389 return name;
22390 }
22391
22392 /* Get name of a die, return NULL if not found.
22393 Anonymous namespaces are converted to their magic string. */
22394
22395 static const char *
22396 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22397 {
22398 struct attribute *attr;
22399 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22400
22401 attr = dwarf2_attr (die, DW_AT_name, cu);
22402 if ((!attr || !DW_STRING (attr))
22403 && die->tag != DW_TAG_namespace
22404 && die->tag != DW_TAG_class_type
22405 && die->tag != DW_TAG_interface_type
22406 && die->tag != DW_TAG_structure_type
22407 && die->tag != DW_TAG_union_type)
22408 return NULL;
22409
22410 switch (die->tag)
22411 {
22412 case DW_TAG_compile_unit:
22413 case DW_TAG_partial_unit:
22414 /* Compilation units have a DW_AT_name that is a filename, not
22415 a source language identifier. */
22416 case DW_TAG_enumeration_type:
22417 case DW_TAG_enumerator:
22418 /* These tags always have simple identifiers already; no need
22419 to canonicalize them. */
22420 return DW_STRING (attr);
22421
22422 case DW_TAG_namespace:
22423 if (attr != NULL && DW_STRING (attr) != NULL)
22424 return DW_STRING (attr);
22425 return CP_ANONYMOUS_NAMESPACE_STR;
22426
22427 case DW_TAG_class_type:
22428 case DW_TAG_interface_type:
22429 case DW_TAG_structure_type:
22430 case DW_TAG_union_type:
22431 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22432 structures or unions. These were of the form "._%d" in GCC 4.1,
22433 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22434 and GCC 4.4. We work around this problem by ignoring these. */
22435 if (attr && DW_STRING (attr)
22436 && (startswith (DW_STRING (attr), "._")
22437 || startswith (DW_STRING (attr), "<anonymous")))
22438 return NULL;
22439
22440 /* GCC might emit a nameless typedef that has a linkage name. See
22441 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22442 if (!attr || DW_STRING (attr) == NULL)
22443 {
22444 char *demangled = NULL;
22445
22446 attr = dw2_linkage_name_attr (die, cu);
22447 if (attr == NULL || DW_STRING (attr) == NULL)
22448 return NULL;
22449
22450 /* Avoid demangling DW_STRING (attr) the second time on a second
22451 call for the same DIE. */
22452 if (!DW_STRING_IS_CANONICAL (attr))
22453 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22454
22455 if (demangled)
22456 {
22457 const char *base;
22458
22459 /* FIXME: we already did this for the partial symbol... */
22460 DW_STRING (attr)
22461 = ((const char *)
22462 obstack_copy0 (&objfile->per_bfd->storage_obstack,
22463 demangled, strlen (demangled)));
22464 DW_STRING_IS_CANONICAL (attr) = 1;
22465 xfree (demangled);
22466
22467 /* Strip any leading namespaces/classes, keep only the base name.
22468 DW_AT_name for named DIEs does not contain the prefixes. */
22469 base = strrchr (DW_STRING (attr), ':');
22470 if (base && base > DW_STRING (attr) && base[-1] == ':')
22471 return &base[1];
22472 else
22473 return DW_STRING (attr);
22474 }
22475 }
22476 break;
22477
22478 default:
22479 break;
22480 }
22481
22482 if (!DW_STRING_IS_CANONICAL (attr))
22483 {
22484 DW_STRING (attr)
22485 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22486 &objfile->per_bfd->storage_obstack);
22487 DW_STRING_IS_CANONICAL (attr) = 1;
22488 }
22489 return DW_STRING (attr);
22490 }
22491
22492 /* Return the die that this die in an extension of, or NULL if there
22493 is none. *EXT_CU is the CU containing DIE on input, and the CU
22494 containing the return value on output. */
22495
22496 static struct die_info *
22497 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22498 {
22499 struct attribute *attr;
22500
22501 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22502 if (attr == NULL)
22503 return NULL;
22504
22505 return follow_die_ref (die, attr, ext_cu);
22506 }
22507
22508 /* Convert a DIE tag into its string name. */
22509
22510 static const char *
22511 dwarf_tag_name (unsigned tag)
22512 {
22513 const char *name = get_DW_TAG_name (tag);
22514
22515 if (name == NULL)
22516 return "DW_TAG_<unknown>";
22517
22518 return name;
22519 }
22520
22521 /* Convert a DWARF attribute code into its string name. */
22522
22523 static const char *
22524 dwarf_attr_name (unsigned attr)
22525 {
22526 const char *name;
22527
22528 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22529 if (attr == DW_AT_MIPS_fde)
22530 return "DW_AT_MIPS_fde";
22531 #else
22532 if (attr == DW_AT_HP_block_index)
22533 return "DW_AT_HP_block_index";
22534 #endif
22535
22536 name = get_DW_AT_name (attr);
22537
22538 if (name == NULL)
22539 return "DW_AT_<unknown>";
22540
22541 return name;
22542 }
22543
22544 /* Convert a DWARF value form code into its string name. */
22545
22546 static const char *
22547 dwarf_form_name (unsigned form)
22548 {
22549 const char *name = get_DW_FORM_name (form);
22550
22551 if (name == NULL)
22552 return "DW_FORM_<unknown>";
22553
22554 return name;
22555 }
22556
22557 static const char *
22558 dwarf_bool_name (unsigned mybool)
22559 {
22560 if (mybool)
22561 return "TRUE";
22562 else
22563 return "FALSE";
22564 }
22565
22566 /* Convert a DWARF type code into its string name. */
22567
22568 static const char *
22569 dwarf_type_encoding_name (unsigned enc)
22570 {
22571 const char *name = get_DW_ATE_name (enc);
22572
22573 if (name == NULL)
22574 return "DW_ATE_<unknown>";
22575
22576 return name;
22577 }
22578
22579 static void
22580 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22581 {
22582 unsigned int i;
22583
22584 print_spaces (indent, f);
22585 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22586 dwarf_tag_name (die->tag), die->abbrev,
22587 sect_offset_str (die->sect_off));
22588
22589 if (die->parent != NULL)
22590 {
22591 print_spaces (indent, f);
22592 fprintf_unfiltered (f, " parent at offset: %s\n",
22593 sect_offset_str (die->parent->sect_off));
22594 }
22595
22596 print_spaces (indent, f);
22597 fprintf_unfiltered (f, " has children: %s\n",
22598 dwarf_bool_name (die->child != NULL));
22599
22600 print_spaces (indent, f);
22601 fprintf_unfiltered (f, " attributes:\n");
22602
22603 for (i = 0; i < die->num_attrs; ++i)
22604 {
22605 print_spaces (indent, f);
22606 fprintf_unfiltered (f, " %s (%s) ",
22607 dwarf_attr_name (die->attrs[i].name),
22608 dwarf_form_name (die->attrs[i].form));
22609
22610 switch (die->attrs[i].form)
22611 {
22612 case DW_FORM_addr:
22613 case DW_FORM_GNU_addr_index:
22614 fprintf_unfiltered (f, "address: ");
22615 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22616 break;
22617 case DW_FORM_block2:
22618 case DW_FORM_block4:
22619 case DW_FORM_block:
22620 case DW_FORM_block1:
22621 fprintf_unfiltered (f, "block: size %s",
22622 pulongest (DW_BLOCK (&die->attrs[i])->size));
22623 break;
22624 case DW_FORM_exprloc:
22625 fprintf_unfiltered (f, "expression: size %s",
22626 pulongest (DW_BLOCK (&die->attrs[i])->size));
22627 break;
22628 case DW_FORM_data16:
22629 fprintf_unfiltered (f, "constant of 16 bytes");
22630 break;
22631 case DW_FORM_ref_addr:
22632 fprintf_unfiltered (f, "ref address: ");
22633 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22634 break;
22635 case DW_FORM_GNU_ref_alt:
22636 fprintf_unfiltered (f, "alt ref address: ");
22637 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22638 break;
22639 case DW_FORM_ref1:
22640 case DW_FORM_ref2:
22641 case DW_FORM_ref4:
22642 case DW_FORM_ref8:
22643 case DW_FORM_ref_udata:
22644 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22645 (long) (DW_UNSND (&die->attrs[i])));
22646 break;
22647 case DW_FORM_data1:
22648 case DW_FORM_data2:
22649 case DW_FORM_data4:
22650 case DW_FORM_data8:
22651 case DW_FORM_udata:
22652 case DW_FORM_sdata:
22653 fprintf_unfiltered (f, "constant: %s",
22654 pulongest (DW_UNSND (&die->attrs[i])));
22655 break;
22656 case DW_FORM_sec_offset:
22657 fprintf_unfiltered (f, "section offset: %s",
22658 pulongest (DW_UNSND (&die->attrs[i])));
22659 break;
22660 case DW_FORM_ref_sig8:
22661 fprintf_unfiltered (f, "signature: %s",
22662 hex_string (DW_SIGNATURE (&die->attrs[i])));
22663 break;
22664 case DW_FORM_string:
22665 case DW_FORM_strp:
22666 case DW_FORM_line_strp:
22667 case DW_FORM_GNU_str_index:
22668 case DW_FORM_GNU_strp_alt:
22669 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22670 DW_STRING (&die->attrs[i])
22671 ? DW_STRING (&die->attrs[i]) : "",
22672 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22673 break;
22674 case DW_FORM_flag:
22675 if (DW_UNSND (&die->attrs[i]))
22676 fprintf_unfiltered (f, "flag: TRUE");
22677 else
22678 fprintf_unfiltered (f, "flag: FALSE");
22679 break;
22680 case DW_FORM_flag_present:
22681 fprintf_unfiltered (f, "flag: TRUE");
22682 break;
22683 case DW_FORM_indirect:
22684 /* The reader will have reduced the indirect form to
22685 the "base form" so this form should not occur. */
22686 fprintf_unfiltered (f,
22687 "unexpected attribute form: DW_FORM_indirect");
22688 break;
22689 case DW_FORM_implicit_const:
22690 fprintf_unfiltered (f, "constant: %s",
22691 plongest (DW_SND (&die->attrs[i])));
22692 break;
22693 default:
22694 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22695 die->attrs[i].form);
22696 break;
22697 }
22698 fprintf_unfiltered (f, "\n");
22699 }
22700 }
22701
22702 static void
22703 dump_die_for_error (struct die_info *die)
22704 {
22705 dump_die_shallow (gdb_stderr, 0, die);
22706 }
22707
22708 static void
22709 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22710 {
22711 int indent = level * 4;
22712
22713 gdb_assert (die != NULL);
22714
22715 if (level >= max_level)
22716 return;
22717
22718 dump_die_shallow (f, indent, die);
22719
22720 if (die->child != NULL)
22721 {
22722 print_spaces (indent, f);
22723 fprintf_unfiltered (f, " Children:");
22724 if (level + 1 < max_level)
22725 {
22726 fprintf_unfiltered (f, "\n");
22727 dump_die_1 (f, level + 1, max_level, die->child);
22728 }
22729 else
22730 {
22731 fprintf_unfiltered (f,
22732 " [not printed, max nesting level reached]\n");
22733 }
22734 }
22735
22736 if (die->sibling != NULL && level > 0)
22737 {
22738 dump_die_1 (f, level, max_level, die->sibling);
22739 }
22740 }
22741
22742 /* This is called from the pdie macro in gdbinit.in.
22743 It's not static so gcc will keep a copy callable from gdb. */
22744
22745 void
22746 dump_die (struct die_info *die, int max_level)
22747 {
22748 dump_die_1 (gdb_stdlog, 0, max_level, die);
22749 }
22750
22751 static void
22752 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22753 {
22754 void **slot;
22755
22756 slot = htab_find_slot_with_hash (cu->die_hash, die,
22757 to_underlying (die->sect_off),
22758 INSERT);
22759
22760 *slot = die;
22761 }
22762
22763 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22764 required kind. */
22765
22766 static sect_offset
22767 dwarf2_get_ref_die_offset (const struct attribute *attr)
22768 {
22769 if (attr_form_is_ref (attr))
22770 return (sect_offset) DW_UNSND (attr);
22771
22772 complaint (&symfile_complaints,
22773 _("unsupported die ref attribute form: '%s'"),
22774 dwarf_form_name (attr->form));
22775 return {};
22776 }
22777
22778 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22779 * the value held by the attribute is not constant. */
22780
22781 static LONGEST
22782 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22783 {
22784 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22785 return DW_SND (attr);
22786 else if (attr->form == DW_FORM_udata
22787 || attr->form == DW_FORM_data1
22788 || attr->form == DW_FORM_data2
22789 || attr->form == DW_FORM_data4
22790 || attr->form == DW_FORM_data8)
22791 return DW_UNSND (attr);
22792 else
22793 {
22794 /* For DW_FORM_data16 see attr_form_is_constant. */
22795 complaint (&symfile_complaints,
22796 _("Attribute value is not a constant (%s)"),
22797 dwarf_form_name (attr->form));
22798 return default_value;
22799 }
22800 }
22801
22802 /* Follow reference or signature attribute ATTR of SRC_DIE.
22803 On entry *REF_CU is the CU of SRC_DIE.
22804 On exit *REF_CU is the CU of the result. */
22805
22806 static struct die_info *
22807 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22808 struct dwarf2_cu **ref_cu)
22809 {
22810 struct die_info *die;
22811
22812 if (attr_form_is_ref (attr))
22813 die = follow_die_ref (src_die, attr, ref_cu);
22814 else if (attr->form == DW_FORM_ref_sig8)
22815 die = follow_die_sig (src_die, attr, ref_cu);
22816 else
22817 {
22818 dump_die_for_error (src_die);
22819 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22820 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22821 }
22822
22823 return die;
22824 }
22825
22826 /* Follow reference OFFSET.
22827 On entry *REF_CU is the CU of the source die referencing OFFSET.
22828 On exit *REF_CU is the CU of the result.
22829 Returns NULL if OFFSET is invalid. */
22830
22831 static struct die_info *
22832 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22833 struct dwarf2_cu **ref_cu)
22834 {
22835 struct die_info temp_die;
22836 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22837 struct dwarf2_per_objfile *dwarf2_per_objfile
22838 = cu->per_cu->dwarf2_per_objfile;
22839
22840 gdb_assert (cu->per_cu != NULL);
22841
22842 target_cu = cu;
22843
22844 if (cu->per_cu->is_debug_types)
22845 {
22846 /* .debug_types CUs cannot reference anything outside their CU.
22847 If they need to, they have to reference a signatured type via
22848 DW_FORM_ref_sig8. */
22849 if (!offset_in_cu_p (&cu->header, sect_off))
22850 return NULL;
22851 }
22852 else if (offset_in_dwz != cu->per_cu->is_dwz
22853 || !offset_in_cu_p (&cu->header, sect_off))
22854 {
22855 struct dwarf2_per_cu_data *per_cu;
22856
22857 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22858 dwarf2_per_objfile);
22859
22860 /* If necessary, add it to the queue and load its DIEs. */
22861 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22862 load_full_comp_unit (per_cu, cu->language);
22863
22864 target_cu = per_cu->cu;
22865 }
22866 else if (cu->dies == NULL)
22867 {
22868 /* We're loading full DIEs during partial symbol reading. */
22869 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22870 load_full_comp_unit (cu->per_cu, language_minimal);
22871 }
22872
22873 *ref_cu = target_cu;
22874 temp_die.sect_off = sect_off;
22875 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22876 &temp_die,
22877 to_underlying (sect_off));
22878 }
22879
22880 /* Follow reference attribute ATTR of SRC_DIE.
22881 On entry *REF_CU is the CU of SRC_DIE.
22882 On exit *REF_CU is the CU of the result. */
22883
22884 static struct die_info *
22885 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22886 struct dwarf2_cu **ref_cu)
22887 {
22888 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22889 struct dwarf2_cu *cu = *ref_cu;
22890 struct die_info *die;
22891
22892 die = follow_die_offset (sect_off,
22893 (attr->form == DW_FORM_GNU_ref_alt
22894 || cu->per_cu->is_dwz),
22895 ref_cu);
22896 if (!die)
22897 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22898 "at %s [in module %s]"),
22899 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22900 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22901
22902 return die;
22903 }
22904
22905 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22906 Returned value is intended for DW_OP_call*. Returned
22907 dwarf2_locexpr_baton->data has lifetime of
22908 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
22909
22910 struct dwarf2_locexpr_baton
22911 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22912 struct dwarf2_per_cu_data *per_cu,
22913 CORE_ADDR (*get_frame_pc) (void *baton),
22914 void *baton)
22915 {
22916 struct dwarf2_cu *cu;
22917 struct die_info *die;
22918 struct attribute *attr;
22919 struct dwarf2_locexpr_baton retval;
22920 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22921 struct objfile *objfile = dwarf2_per_objfile->objfile;
22922
22923 if (per_cu->cu == NULL)
22924 load_cu (per_cu);
22925 cu = per_cu->cu;
22926 if (cu == NULL)
22927 {
22928 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22929 Instead just throw an error, not much else we can do. */
22930 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22931 sect_offset_str (sect_off), objfile_name (objfile));
22932 }
22933
22934 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22935 if (!die)
22936 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22937 sect_offset_str (sect_off), objfile_name (objfile));
22938
22939 attr = dwarf2_attr (die, DW_AT_location, cu);
22940 if (!attr)
22941 {
22942 /* DWARF: "If there is no such attribute, then there is no effect.".
22943 DATA is ignored if SIZE is 0. */
22944
22945 retval.data = NULL;
22946 retval.size = 0;
22947 }
22948 else if (attr_form_is_section_offset (attr))
22949 {
22950 struct dwarf2_loclist_baton loclist_baton;
22951 CORE_ADDR pc = (*get_frame_pc) (baton);
22952 size_t size;
22953
22954 fill_in_loclist_baton (cu, &loclist_baton, attr);
22955
22956 retval.data = dwarf2_find_location_expression (&loclist_baton,
22957 &size, pc);
22958 retval.size = size;
22959 }
22960 else
22961 {
22962 if (!attr_form_is_block (attr))
22963 error (_("Dwarf Error: DIE at %s referenced in module %s "
22964 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22965 sect_offset_str (sect_off), objfile_name (objfile));
22966
22967 retval.data = DW_BLOCK (attr)->data;
22968 retval.size = DW_BLOCK (attr)->size;
22969 }
22970 retval.per_cu = cu->per_cu;
22971
22972 age_cached_comp_units (dwarf2_per_objfile);
22973
22974 return retval;
22975 }
22976
22977 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
22978 offset. */
22979
22980 struct dwarf2_locexpr_baton
22981 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22982 struct dwarf2_per_cu_data *per_cu,
22983 CORE_ADDR (*get_frame_pc) (void *baton),
22984 void *baton)
22985 {
22986 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22987
22988 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22989 }
22990
22991 /* Write a constant of a given type as target-ordered bytes into
22992 OBSTACK. */
22993
22994 static const gdb_byte *
22995 write_constant_as_bytes (struct obstack *obstack,
22996 enum bfd_endian byte_order,
22997 struct type *type,
22998 ULONGEST value,
22999 LONGEST *len)
23000 {
23001 gdb_byte *result;
23002
23003 *len = TYPE_LENGTH (type);
23004 result = (gdb_byte *) obstack_alloc (obstack, *len);
23005 store_unsigned_integer (result, *len, byte_order, value);
23006
23007 return result;
23008 }
23009
23010 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23011 pointer to the constant bytes and set LEN to the length of the
23012 data. If memory is needed, allocate it on OBSTACK. If the DIE
23013 does not have a DW_AT_const_value, return NULL. */
23014
23015 const gdb_byte *
23016 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23017 struct dwarf2_per_cu_data *per_cu,
23018 struct obstack *obstack,
23019 LONGEST *len)
23020 {
23021 struct dwarf2_cu *cu;
23022 struct die_info *die;
23023 struct attribute *attr;
23024 const gdb_byte *result = NULL;
23025 struct type *type;
23026 LONGEST value;
23027 enum bfd_endian byte_order;
23028 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23029
23030 if (per_cu->cu == NULL)
23031 load_cu (per_cu);
23032 cu = per_cu->cu;
23033 if (cu == NULL)
23034 {
23035 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23036 Instead just throw an error, not much else we can do. */
23037 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23038 sect_offset_str (sect_off), objfile_name (objfile));
23039 }
23040
23041 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23042 if (!die)
23043 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23044 sect_offset_str (sect_off), objfile_name (objfile));
23045
23046 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23047 if (attr == NULL)
23048 return NULL;
23049
23050 byte_order = (bfd_big_endian (objfile->obfd)
23051 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23052
23053 switch (attr->form)
23054 {
23055 case DW_FORM_addr:
23056 case DW_FORM_GNU_addr_index:
23057 {
23058 gdb_byte *tem;
23059
23060 *len = cu->header.addr_size;
23061 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23062 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23063 result = tem;
23064 }
23065 break;
23066 case DW_FORM_string:
23067 case DW_FORM_strp:
23068 case DW_FORM_GNU_str_index:
23069 case DW_FORM_GNU_strp_alt:
23070 /* DW_STRING is already allocated on the objfile obstack, point
23071 directly to it. */
23072 result = (const gdb_byte *) DW_STRING (attr);
23073 *len = strlen (DW_STRING (attr));
23074 break;
23075 case DW_FORM_block1:
23076 case DW_FORM_block2:
23077 case DW_FORM_block4:
23078 case DW_FORM_block:
23079 case DW_FORM_exprloc:
23080 case DW_FORM_data16:
23081 result = DW_BLOCK (attr)->data;
23082 *len = DW_BLOCK (attr)->size;
23083 break;
23084
23085 /* The DW_AT_const_value attributes are supposed to carry the
23086 symbol's value "represented as it would be on the target
23087 architecture." By the time we get here, it's already been
23088 converted to host endianness, so we just need to sign- or
23089 zero-extend it as appropriate. */
23090 case DW_FORM_data1:
23091 type = die_type (die, cu);
23092 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23093 if (result == NULL)
23094 result = write_constant_as_bytes (obstack, byte_order,
23095 type, value, len);
23096 break;
23097 case DW_FORM_data2:
23098 type = die_type (die, cu);
23099 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23100 if (result == NULL)
23101 result = write_constant_as_bytes (obstack, byte_order,
23102 type, value, len);
23103 break;
23104 case DW_FORM_data4:
23105 type = die_type (die, cu);
23106 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23107 if (result == NULL)
23108 result = write_constant_as_bytes (obstack, byte_order,
23109 type, value, len);
23110 break;
23111 case DW_FORM_data8:
23112 type = die_type (die, cu);
23113 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23114 if (result == NULL)
23115 result = write_constant_as_bytes (obstack, byte_order,
23116 type, value, len);
23117 break;
23118
23119 case DW_FORM_sdata:
23120 case DW_FORM_implicit_const:
23121 type = die_type (die, cu);
23122 result = write_constant_as_bytes (obstack, byte_order,
23123 type, DW_SND (attr), len);
23124 break;
23125
23126 case DW_FORM_udata:
23127 type = die_type (die, cu);
23128 result = write_constant_as_bytes (obstack, byte_order,
23129 type, DW_UNSND (attr), len);
23130 break;
23131
23132 default:
23133 complaint (&symfile_complaints,
23134 _("unsupported const value attribute form: '%s'"),
23135 dwarf_form_name (attr->form));
23136 break;
23137 }
23138
23139 return result;
23140 }
23141
23142 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23143 valid type for this die is found. */
23144
23145 struct type *
23146 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23147 struct dwarf2_per_cu_data *per_cu)
23148 {
23149 struct dwarf2_cu *cu;
23150 struct die_info *die;
23151
23152 if (per_cu->cu == NULL)
23153 load_cu (per_cu);
23154 cu = per_cu->cu;
23155 if (!cu)
23156 return NULL;
23157
23158 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23159 if (!die)
23160 return NULL;
23161
23162 return die_type (die, cu);
23163 }
23164
23165 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23166 PER_CU. */
23167
23168 struct type *
23169 dwarf2_get_die_type (cu_offset die_offset,
23170 struct dwarf2_per_cu_data *per_cu)
23171 {
23172 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23173 return get_die_type_at_offset (die_offset_sect, per_cu);
23174 }
23175
23176 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23177 On entry *REF_CU is the CU of SRC_DIE.
23178 On exit *REF_CU is the CU of the result.
23179 Returns NULL if the referenced DIE isn't found. */
23180
23181 static struct die_info *
23182 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23183 struct dwarf2_cu **ref_cu)
23184 {
23185 struct die_info temp_die;
23186 struct dwarf2_cu *sig_cu;
23187 struct die_info *die;
23188
23189 /* While it might be nice to assert sig_type->type == NULL here,
23190 we can get here for DW_AT_imported_declaration where we need
23191 the DIE not the type. */
23192
23193 /* If necessary, add it to the queue and load its DIEs. */
23194
23195 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23196 read_signatured_type (sig_type);
23197
23198 sig_cu = sig_type->per_cu.cu;
23199 gdb_assert (sig_cu != NULL);
23200 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23201 temp_die.sect_off = sig_type->type_offset_in_section;
23202 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23203 to_underlying (temp_die.sect_off));
23204 if (die)
23205 {
23206 struct dwarf2_per_objfile *dwarf2_per_objfile
23207 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23208
23209 /* For .gdb_index version 7 keep track of included TUs.
23210 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23211 if (dwarf2_per_objfile->index_table != NULL
23212 && dwarf2_per_objfile->index_table->version <= 7)
23213 {
23214 VEC_safe_push (dwarf2_per_cu_ptr,
23215 (*ref_cu)->per_cu->imported_symtabs,
23216 sig_cu->per_cu);
23217 }
23218
23219 *ref_cu = sig_cu;
23220 return die;
23221 }
23222
23223 return NULL;
23224 }
23225
23226 /* Follow signatured type referenced by ATTR in SRC_DIE.
23227 On entry *REF_CU is the CU of SRC_DIE.
23228 On exit *REF_CU is the CU of the result.
23229 The result is the DIE of the type.
23230 If the referenced type cannot be found an error is thrown. */
23231
23232 static struct die_info *
23233 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23234 struct dwarf2_cu **ref_cu)
23235 {
23236 ULONGEST signature = DW_SIGNATURE (attr);
23237 struct signatured_type *sig_type;
23238 struct die_info *die;
23239
23240 gdb_assert (attr->form == DW_FORM_ref_sig8);
23241
23242 sig_type = lookup_signatured_type (*ref_cu, signature);
23243 /* sig_type will be NULL if the signatured type is missing from
23244 the debug info. */
23245 if (sig_type == NULL)
23246 {
23247 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23248 " from DIE at %s [in module %s]"),
23249 hex_string (signature), sect_offset_str (src_die->sect_off),
23250 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23251 }
23252
23253 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23254 if (die == NULL)
23255 {
23256 dump_die_for_error (src_die);
23257 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23258 " from DIE at %s [in module %s]"),
23259 hex_string (signature), sect_offset_str (src_die->sect_off),
23260 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23261 }
23262
23263 return die;
23264 }
23265
23266 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23267 reading in and processing the type unit if necessary. */
23268
23269 static struct type *
23270 get_signatured_type (struct die_info *die, ULONGEST signature,
23271 struct dwarf2_cu *cu)
23272 {
23273 struct dwarf2_per_objfile *dwarf2_per_objfile
23274 = cu->per_cu->dwarf2_per_objfile;
23275 struct signatured_type *sig_type;
23276 struct dwarf2_cu *type_cu;
23277 struct die_info *type_die;
23278 struct type *type;
23279
23280 sig_type = lookup_signatured_type (cu, signature);
23281 /* sig_type will be NULL if the signatured type is missing from
23282 the debug info. */
23283 if (sig_type == NULL)
23284 {
23285 complaint (&symfile_complaints,
23286 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23287 " from DIE at %s [in module %s]"),
23288 hex_string (signature), sect_offset_str (die->sect_off),
23289 objfile_name (dwarf2_per_objfile->objfile));
23290 return build_error_marker_type (cu, die);
23291 }
23292
23293 /* If we already know the type we're done. */
23294 if (sig_type->type != NULL)
23295 return sig_type->type;
23296
23297 type_cu = cu;
23298 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23299 if (type_die != NULL)
23300 {
23301 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23302 is created. This is important, for example, because for c++ classes
23303 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23304 type = read_type_die (type_die, type_cu);
23305 if (type == NULL)
23306 {
23307 complaint (&symfile_complaints,
23308 _("Dwarf Error: Cannot build signatured type %s"
23309 " referenced from DIE at %s [in module %s]"),
23310 hex_string (signature), sect_offset_str (die->sect_off),
23311 objfile_name (dwarf2_per_objfile->objfile));
23312 type = build_error_marker_type (cu, die);
23313 }
23314 }
23315 else
23316 {
23317 complaint (&symfile_complaints,
23318 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23319 " from DIE at %s [in module %s]"),
23320 hex_string (signature), sect_offset_str (die->sect_off),
23321 objfile_name (dwarf2_per_objfile->objfile));
23322 type = build_error_marker_type (cu, die);
23323 }
23324 sig_type->type = type;
23325
23326 return type;
23327 }
23328
23329 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23330 reading in and processing the type unit if necessary. */
23331
23332 static struct type *
23333 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23334 struct dwarf2_cu *cu) /* ARI: editCase function */
23335 {
23336 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23337 if (attr_form_is_ref (attr))
23338 {
23339 struct dwarf2_cu *type_cu = cu;
23340 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23341
23342 return read_type_die (type_die, type_cu);
23343 }
23344 else if (attr->form == DW_FORM_ref_sig8)
23345 {
23346 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23347 }
23348 else
23349 {
23350 struct dwarf2_per_objfile *dwarf2_per_objfile
23351 = cu->per_cu->dwarf2_per_objfile;
23352
23353 complaint (&symfile_complaints,
23354 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23355 " at %s [in module %s]"),
23356 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23357 objfile_name (dwarf2_per_objfile->objfile));
23358 return build_error_marker_type (cu, die);
23359 }
23360 }
23361
23362 /* Load the DIEs associated with type unit PER_CU into memory. */
23363
23364 static void
23365 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23366 {
23367 struct signatured_type *sig_type;
23368
23369 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23370 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23371
23372 /* We have the per_cu, but we need the signatured_type.
23373 Fortunately this is an easy translation. */
23374 gdb_assert (per_cu->is_debug_types);
23375 sig_type = (struct signatured_type *) per_cu;
23376
23377 gdb_assert (per_cu->cu == NULL);
23378
23379 read_signatured_type (sig_type);
23380
23381 gdb_assert (per_cu->cu != NULL);
23382 }
23383
23384 /* die_reader_func for read_signatured_type.
23385 This is identical to load_full_comp_unit_reader,
23386 but is kept separate for now. */
23387
23388 static void
23389 read_signatured_type_reader (const struct die_reader_specs *reader,
23390 const gdb_byte *info_ptr,
23391 struct die_info *comp_unit_die,
23392 int has_children,
23393 void *data)
23394 {
23395 struct dwarf2_cu *cu = reader->cu;
23396
23397 gdb_assert (cu->die_hash == NULL);
23398 cu->die_hash =
23399 htab_create_alloc_ex (cu->header.length / 12,
23400 die_hash,
23401 die_eq,
23402 NULL,
23403 &cu->comp_unit_obstack,
23404 hashtab_obstack_allocate,
23405 dummy_obstack_deallocate);
23406
23407 if (has_children)
23408 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23409 &info_ptr, comp_unit_die);
23410 cu->dies = comp_unit_die;
23411 /* comp_unit_die is not stored in die_hash, no need. */
23412
23413 /* We try not to read any attributes in this function, because not
23414 all CUs needed for references have been loaded yet, and symbol
23415 table processing isn't initialized. But we have to set the CU language,
23416 or we won't be able to build types correctly.
23417 Similarly, if we do not read the producer, we can not apply
23418 producer-specific interpretation. */
23419 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23420 }
23421
23422 /* Read in a signatured type and build its CU and DIEs.
23423 If the type is a stub for the real type in a DWO file,
23424 read in the real type from the DWO file as well. */
23425
23426 static void
23427 read_signatured_type (struct signatured_type *sig_type)
23428 {
23429 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23430
23431 gdb_assert (per_cu->is_debug_types);
23432 gdb_assert (per_cu->cu == NULL);
23433
23434 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23435 read_signatured_type_reader, NULL);
23436 sig_type->per_cu.tu_read = 1;
23437 }
23438
23439 /* Decode simple location descriptions.
23440 Given a pointer to a dwarf block that defines a location, compute
23441 the location and return the value.
23442
23443 NOTE drow/2003-11-18: This function is called in two situations
23444 now: for the address of static or global variables (partial symbols
23445 only) and for offsets into structures which are expected to be
23446 (more or less) constant. The partial symbol case should go away,
23447 and only the constant case should remain. That will let this
23448 function complain more accurately. A few special modes are allowed
23449 without complaint for global variables (for instance, global
23450 register values and thread-local values).
23451
23452 A location description containing no operations indicates that the
23453 object is optimized out. The return value is 0 for that case.
23454 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23455 callers will only want a very basic result and this can become a
23456 complaint.
23457
23458 Note that stack[0] is unused except as a default error return. */
23459
23460 static CORE_ADDR
23461 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23462 {
23463 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23464 size_t i;
23465 size_t size = blk->size;
23466 const gdb_byte *data = blk->data;
23467 CORE_ADDR stack[64];
23468 int stacki;
23469 unsigned int bytes_read, unsnd;
23470 gdb_byte op;
23471
23472 i = 0;
23473 stacki = 0;
23474 stack[stacki] = 0;
23475 stack[++stacki] = 0;
23476
23477 while (i < size)
23478 {
23479 op = data[i++];
23480 switch (op)
23481 {
23482 case DW_OP_lit0:
23483 case DW_OP_lit1:
23484 case DW_OP_lit2:
23485 case DW_OP_lit3:
23486 case DW_OP_lit4:
23487 case DW_OP_lit5:
23488 case DW_OP_lit6:
23489 case DW_OP_lit7:
23490 case DW_OP_lit8:
23491 case DW_OP_lit9:
23492 case DW_OP_lit10:
23493 case DW_OP_lit11:
23494 case DW_OP_lit12:
23495 case DW_OP_lit13:
23496 case DW_OP_lit14:
23497 case DW_OP_lit15:
23498 case DW_OP_lit16:
23499 case DW_OP_lit17:
23500 case DW_OP_lit18:
23501 case DW_OP_lit19:
23502 case DW_OP_lit20:
23503 case DW_OP_lit21:
23504 case DW_OP_lit22:
23505 case DW_OP_lit23:
23506 case DW_OP_lit24:
23507 case DW_OP_lit25:
23508 case DW_OP_lit26:
23509 case DW_OP_lit27:
23510 case DW_OP_lit28:
23511 case DW_OP_lit29:
23512 case DW_OP_lit30:
23513 case DW_OP_lit31:
23514 stack[++stacki] = op - DW_OP_lit0;
23515 break;
23516
23517 case DW_OP_reg0:
23518 case DW_OP_reg1:
23519 case DW_OP_reg2:
23520 case DW_OP_reg3:
23521 case DW_OP_reg4:
23522 case DW_OP_reg5:
23523 case DW_OP_reg6:
23524 case DW_OP_reg7:
23525 case DW_OP_reg8:
23526 case DW_OP_reg9:
23527 case DW_OP_reg10:
23528 case DW_OP_reg11:
23529 case DW_OP_reg12:
23530 case DW_OP_reg13:
23531 case DW_OP_reg14:
23532 case DW_OP_reg15:
23533 case DW_OP_reg16:
23534 case DW_OP_reg17:
23535 case DW_OP_reg18:
23536 case DW_OP_reg19:
23537 case DW_OP_reg20:
23538 case DW_OP_reg21:
23539 case DW_OP_reg22:
23540 case DW_OP_reg23:
23541 case DW_OP_reg24:
23542 case DW_OP_reg25:
23543 case DW_OP_reg26:
23544 case DW_OP_reg27:
23545 case DW_OP_reg28:
23546 case DW_OP_reg29:
23547 case DW_OP_reg30:
23548 case DW_OP_reg31:
23549 stack[++stacki] = op - DW_OP_reg0;
23550 if (i < size)
23551 dwarf2_complex_location_expr_complaint ();
23552 break;
23553
23554 case DW_OP_regx:
23555 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23556 i += bytes_read;
23557 stack[++stacki] = unsnd;
23558 if (i < size)
23559 dwarf2_complex_location_expr_complaint ();
23560 break;
23561
23562 case DW_OP_addr:
23563 stack[++stacki] = read_address (objfile->obfd, &data[i],
23564 cu, &bytes_read);
23565 i += bytes_read;
23566 break;
23567
23568 case DW_OP_const1u:
23569 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23570 i += 1;
23571 break;
23572
23573 case DW_OP_const1s:
23574 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23575 i += 1;
23576 break;
23577
23578 case DW_OP_const2u:
23579 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23580 i += 2;
23581 break;
23582
23583 case DW_OP_const2s:
23584 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23585 i += 2;
23586 break;
23587
23588 case DW_OP_const4u:
23589 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23590 i += 4;
23591 break;
23592
23593 case DW_OP_const4s:
23594 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23595 i += 4;
23596 break;
23597
23598 case DW_OP_const8u:
23599 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23600 i += 8;
23601 break;
23602
23603 case DW_OP_constu:
23604 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23605 &bytes_read);
23606 i += bytes_read;
23607 break;
23608
23609 case DW_OP_consts:
23610 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23611 i += bytes_read;
23612 break;
23613
23614 case DW_OP_dup:
23615 stack[stacki + 1] = stack[stacki];
23616 stacki++;
23617 break;
23618
23619 case DW_OP_plus:
23620 stack[stacki - 1] += stack[stacki];
23621 stacki--;
23622 break;
23623
23624 case DW_OP_plus_uconst:
23625 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23626 &bytes_read);
23627 i += bytes_read;
23628 break;
23629
23630 case DW_OP_minus:
23631 stack[stacki - 1] -= stack[stacki];
23632 stacki--;
23633 break;
23634
23635 case DW_OP_deref:
23636 /* If we're not the last op, then we definitely can't encode
23637 this using GDB's address_class enum. This is valid for partial
23638 global symbols, although the variable's address will be bogus
23639 in the psymtab. */
23640 if (i < size)
23641 dwarf2_complex_location_expr_complaint ();
23642 break;
23643
23644 case DW_OP_GNU_push_tls_address:
23645 case DW_OP_form_tls_address:
23646 /* The top of the stack has the offset from the beginning
23647 of the thread control block at which the variable is located. */
23648 /* Nothing should follow this operator, so the top of stack would
23649 be returned. */
23650 /* This is valid for partial global symbols, but the variable's
23651 address will be bogus in the psymtab. Make it always at least
23652 non-zero to not look as a variable garbage collected by linker
23653 which have DW_OP_addr 0. */
23654 if (i < size)
23655 dwarf2_complex_location_expr_complaint ();
23656 stack[stacki]++;
23657 break;
23658
23659 case DW_OP_GNU_uninit:
23660 break;
23661
23662 case DW_OP_GNU_addr_index:
23663 case DW_OP_GNU_const_index:
23664 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23665 &bytes_read);
23666 i += bytes_read;
23667 break;
23668
23669 default:
23670 {
23671 const char *name = get_DW_OP_name (op);
23672
23673 if (name)
23674 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
23675 name);
23676 else
23677 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
23678 op);
23679 }
23680
23681 return (stack[stacki]);
23682 }
23683
23684 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23685 outside of the allocated space. Also enforce minimum>0. */
23686 if (stacki >= ARRAY_SIZE (stack) - 1)
23687 {
23688 complaint (&symfile_complaints,
23689 _("location description stack overflow"));
23690 return 0;
23691 }
23692
23693 if (stacki <= 0)
23694 {
23695 complaint (&symfile_complaints,
23696 _("location description stack underflow"));
23697 return 0;
23698 }
23699 }
23700 return (stack[stacki]);
23701 }
23702
23703 /* memory allocation interface */
23704
23705 static struct dwarf_block *
23706 dwarf_alloc_block (struct dwarf2_cu *cu)
23707 {
23708 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23709 }
23710
23711 static struct die_info *
23712 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23713 {
23714 struct die_info *die;
23715 size_t size = sizeof (struct die_info);
23716
23717 if (num_attrs > 1)
23718 size += (num_attrs - 1) * sizeof (struct attribute);
23719
23720 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23721 memset (die, 0, sizeof (struct die_info));
23722 return (die);
23723 }
23724
23725 \f
23726 /* Macro support. */
23727
23728 /* Return file name relative to the compilation directory of file number I in
23729 *LH's file name table. The result is allocated using xmalloc; the caller is
23730 responsible for freeing it. */
23731
23732 static char *
23733 file_file_name (int file, struct line_header *lh)
23734 {
23735 /* Is the file number a valid index into the line header's file name
23736 table? Remember that file numbers start with one, not zero. */
23737 if (1 <= file && file <= lh->file_names.size ())
23738 {
23739 const file_entry &fe = lh->file_names[file - 1];
23740
23741 if (!IS_ABSOLUTE_PATH (fe.name))
23742 {
23743 const char *dir = fe.include_dir (lh);
23744 if (dir != NULL)
23745 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23746 }
23747 return xstrdup (fe.name);
23748 }
23749 else
23750 {
23751 /* The compiler produced a bogus file number. We can at least
23752 record the macro definitions made in the file, even if we
23753 won't be able to find the file by name. */
23754 char fake_name[80];
23755
23756 xsnprintf (fake_name, sizeof (fake_name),
23757 "<bad macro file number %d>", file);
23758
23759 complaint (&symfile_complaints,
23760 _("bad file number in macro information (%d)"),
23761 file);
23762
23763 return xstrdup (fake_name);
23764 }
23765 }
23766
23767 /* Return the full name of file number I in *LH's file name table.
23768 Use COMP_DIR as the name of the current directory of the
23769 compilation. The result is allocated using xmalloc; the caller is
23770 responsible for freeing it. */
23771 static char *
23772 file_full_name (int file, struct line_header *lh, const char *comp_dir)
23773 {
23774 /* Is the file number a valid index into the line header's file name
23775 table? Remember that file numbers start with one, not zero. */
23776 if (1 <= file && file <= lh->file_names.size ())
23777 {
23778 char *relative = file_file_name (file, lh);
23779
23780 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23781 return relative;
23782 return reconcat (relative, comp_dir, SLASH_STRING,
23783 relative, (char *) NULL);
23784 }
23785 else
23786 return file_file_name (file, lh);
23787 }
23788
23789
23790 static struct macro_source_file *
23791 macro_start_file (int file, int line,
23792 struct macro_source_file *current_file,
23793 struct line_header *lh)
23794 {
23795 /* File name relative to the compilation directory of this source file. */
23796 char *file_name = file_file_name (file, lh);
23797
23798 if (! current_file)
23799 {
23800 /* Note: We don't create a macro table for this compilation unit
23801 at all until we actually get a filename. */
23802 struct macro_table *macro_table = get_macro_table ();
23803
23804 /* If we have no current file, then this must be the start_file
23805 directive for the compilation unit's main source file. */
23806 current_file = macro_set_main (macro_table, file_name);
23807 macro_define_special (macro_table);
23808 }
23809 else
23810 current_file = macro_include (current_file, line, file_name);
23811
23812 xfree (file_name);
23813
23814 return current_file;
23815 }
23816
23817 static const char *
23818 consume_improper_spaces (const char *p, const char *body)
23819 {
23820 if (*p == ' ')
23821 {
23822 complaint (&symfile_complaints,
23823 _("macro definition contains spaces "
23824 "in formal argument list:\n`%s'"),
23825 body);
23826
23827 while (*p == ' ')
23828 p++;
23829 }
23830
23831 return p;
23832 }
23833
23834
23835 static void
23836 parse_macro_definition (struct macro_source_file *file, int line,
23837 const char *body)
23838 {
23839 const char *p;
23840
23841 /* The body string takes one of two forms. For object-like macro
23842 definitions, it should be:
23843
23844 <macro name> " " <definition>
23845
23846 For function-like macro definitions, it should be:
23847
23848 <macro name> "() " <definition>
23849 or
23850 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23851
23852 Spaces may appear only where explicitly indicated, and in the
23853 <definition>.
23854
23855 The Dwarf 2 spec says that an object-like macro's name is always
23856 followed by a space, but versions of GCC around March 2002 omit
23857 the space when the macro's definition is the empty string.
23858
23859 The Dwarf 2 spec says that there should be no spaces between the
23860 formal arguments in a function-like macro's formal argument list,
23861 but versions of GCC around March 2002 include spaces after the
23862 commas. */
23863
23864
23865 /* Find the extent of the macro name. The macro name is terminated
23866 by either a space or null character (for an object-like macro) or
23867 an opening paren (for a function-like macro). */
23868 for (p = body; *p; p++)
23869 if (*p == ' ' || *p == '(')
23870 break;
23871
23872 if (*p == ' ' || *p == '\0')
23873 {
23874 /* It's an object-like macro. */
23875 int name_len = p - body;
23876 char *name = savestring (body, name_len);
23877 const char *replacement;
23878
23879 if (*p == ' ')
23880 replacement = body + name_len + 1;
23881 else
23882 {
23883 dwarf2_macro_malformed_definition_complaint (body);
23884 replacement = body + name_len;
23885 }
23886
23887 macro_define_object (file, line, name, replacement);
23888
23889 xfree (name);
23890 }
23891 else if (*p == '(')
23892 {
23893 /* It's a function-like macro. */
23894 char *name = savestring (body, p - body);
23895 int argc = 0;
23896 int argv_size = 1;
23897 char **argv = XNEWVEC (char *, argv_size);
23898
23899 p++;
23900
23901 p = consume_improper_spaces (p, body);
23902
23903 /* Parse the formal argument list. */
23904 while (*p && *p != ')')
23905 {
23906 /* Find the extent of the current argument name. */
23907 const char *arg_start = p;
23908
23909 while (*p && *p != ',' && *p != ')' && *p != ' ')
23910 p++;
23911
23912 if (! *p || p == arg_start)
23913 dwarf2_macro_malformed_definition_complaint (body);
23914 else
23915 {
23916 /* Make sure argv has room for the new argument. */
23917 if (argc >= argv_size)
23918 {
23919 argv_size *= 2;
23920 argv = XRESIZEVEC (char *, argv, argv_size);
23921 }
23922
23923 argv[argc++] = savestring (arg_start, p - arg_start);
23924 }
23925
23926 p = consume_improper_spaces (p, body);
23927
23928 /* Consume the comma, if present. */
23929 if (*p == ',')
23930 {
23931 p++;
23932
23933 p = consume_improper_spaces (p, body);
23934 }
23935 }
23936
23937 if (*p == ')')
23938 {
23939 p++;
23940
23941 if (*p == ' ')
23942 /* Perfectly formed definition, no complaints. */
23943 macro_define_function (file, line, name,
23944 argc, (const char **) argv,
23945 p + 1);
23946 else if (*p == '\0')
23947 {
23948 /* Complain, but do define it. */
23949 dwarf2_macro_malformed_definition_complaint (body);
23950 macro_define_function (file, line, name,
23951 argc, (const char **) argv,
23952 p);
23953 }
23954 else
23955 /* Just complain. */
23956 dwarf2_macro_malformed_definition_complaint (body);
23957 }
23958 else
23959 /* Just complain. */
23960 dwarf2_macro_malformed_definition_complaint (body);
23961
23962 xfree (name);
23963 {
23964 int i;
23965
23966 for (i = 0; i < argc; i++)
23967 xfree (argv[i]);
23968 }
23969 xfree (argv);
23970 }
23971 else
23972 dwarf2_macro_malformed_definition_complaint (body);
23973 }
23974
23975 /* Skip some bytes from BYTES according to the form given in FORM.
23976 Returns the new pointer. */
23977
23978 static const gdb_byte *
23979 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
23980 enum dwarf_form form,
23981 unsigned int offset_size,
23982 struct dwarf2_section_info *section)
23983 {
23984 unsigned int bytes_read;
23985
23986 switch (form)
23987 {
23988 case DW_FORM_data1:
23989 case DW_FORM_flag:
23990 ++bytes;
23991 break;
23992
23993 case DW_FORM_data2:
23994 bytes += 2;
23995 break;
23996
23997 case DW_FORM_data4:
23998 bytes += 4;
23999 break;
24000
24001 case DW_FORM_data8:
24002 bytes += 8;
24003 break;
24004
24005 case DW_FORM_data16:
24006 bytes += 16;
24007 break;
24008
24009 case DW_FORM_string:
24010 read_direct_string (abfd, bytes, &bytes_read);
24011 bytes += bytes_read;
24012 break;
24013
24014 case DW_FORM_sec_offset:
24015 case DW_FORM_strp:
24016 case DW_FORM_GNU_strp_alt:
24017 bytes += offset_size;
24018 break;
24019
24020 case DW_FORM_block:
24021 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24022 bytes += bytes_read;
24023 break;
24024
24025 case DW_FORM_block1:
24026 bytes += 1 + read_1_byte (abfd, bytes);
24027 break;
24028 case DW_FORM_block2:
24029 bytes += 2 + read_2_bytes (abfd, bytes);
24030 break;
24031 case DW_FORM_block4:
24032 bytes += 4 + read_4_bytes (abfd, bytes);
24033 break;
24034
24035 case DW_FORM_sdata:
24036 case DW_FORM_udata:
24037 case DW_FORM_GNU_addr_index:
24038 case DW_FORM_GNU_str_index:
24039 bytes = gdb_skip_leb128 (bytes, buffer_end);
24040 if (bytes == NULL)
24041 {
24042 dwarf2_section_buffer_overflow_complaint (section);
24043 return NULL;
24044 }
24045 break;
24046
24047 case DW_FORM_implicit_const:
24048 break;
24049
24050 default:
24051 {
24052 complaint (&symfile_complaints,
24053 _("invalid form 0x%x in `%s'"),
24054 form, get_section_name (section));
24055 return NULL;
24056 }
24057 }
24058
24059 return bytes;
24060 }
24061
24062 /* A helper for dwarf_decode_macros that handles skipping an unknown
24063 opcode. Returns an updated pointer to the macro data buffer; or,
24064 on error, issues a complaint and returns NULL. */
24065
24066 static const gdb_byte *
24067 skip_unknown_opcode (unsigned int opcode,
24068 const gdb_byte **opcode_definitions,
24069 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24070 bfd *abfd,
24071 unsigned int offset_size,
24072 struct dwarf2_section_info *section)
24073 {
24074 unsigned int bytes_read, i;
24075 unsigned long arg;
24076 const gdb_byte *defn;
24077
24078 if (opcode_definitions[opcode] == NULL)
24079 {
24080 complaint (&symfile_complaints,
24081 _("unrecognized DW_MACFINO opcode 0x%x"),
24082 opcode);
24083 return NULL;
24084 }
24085
24086 defn = opcode_definitions[opcode];
24087 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24088 defn += bytes_read;
24089
24090 for (i = 0; i < arg; ++i)
24091 {
24092 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24093 (enum dwarf_form) defn[i], offset_size,
24094 section);
24095 if (mac_ptr == NULL)
24096 {
24097 /* skip_form_bytes already issued the complaint. */
24098 return NULL;
24099 }
24100 }
24101
24102 return mac_ptr;
24103 }
24104
24105 /* A helper function which parses the header of a macro section.
24106 If the macro section is the extended (for now called "GNU") type,
24107 then this updates *OFFSET_SIZE. Returns a pointer to just after
24108 the header, or issues a complaint and returns NULL on error. */
24109
24110 static const gdb_byte *
24111 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24112 bfd *abfd,
24113 const gdb_byte *mac_ptr,
24114 unsigned int *offset_size,
24115 int section_is_gnu)
24116 {
24117 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24118
24119 if (section_is_gnu)
24120 {
24121 unsigned int version, flags;
24122
24123 version = read_2_bytes (abfd, mac_ptr);
24124 if (version != 4 && version != 5)
24125 {
24126 complaint (&symfile_complaints,
24127 _("unrecognized version `%d' in .debug_macro section"),
24128 version);
24129 return NULL;
24130 }
24131 mac_ptr += 2;
24132
24133 flags = read_1_byte (abfd, mac_ptr);
24134 ++mac_ptr;
24135 *offset_size = (flags & 1) ? 8 : 4;
24136
24137 if ((flags & 2) != 0)
24138 /* We don't need the line table offset. */
24139 mac_ptr += *offset_size;
24140
24141 /* Vendor opcode descriptions. */
24142 if ((flags & 4) != 0)
24143 {
24144 unsigned int i, count;
24145
24146 count = read_1_byte (abfd, mac_ptr);
24147 ++mac_ptr;
24148 for (i = 0; i < count; ++i)
24149 {
24150 unsigned int opcode, bytes_read;
24151 unsigned long arg;
24152
24153 opcode = read_1_byte (abfd, mac_ptr);
24154 ++mac_ptr;
24155 opcode_definitions[opcode] = mac_ptr;
24156 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24157 mac_ptr += bytes_read;
24158 mac_ptr += arg;
24159 }
24160 }
24161 }
24162
24163 return mac_ptr;
24164 }
24165
24166 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24167 including DW_MACRO_import. */
24168
24169 static void
24170 dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24171 bfd *abfd,
24172 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24173 struct macro_source_file *current_file,
24174 struct line_header *lh,
24175 struct dwarf2_section_info *section,
24176 int section_is_gnu, int section_is_dwz,
24177 unsigned int offset_size,
24178 htab_t include_hash)
24179 {
24180 struct objfile *objfile = dwarf2_per_objfile->objfile;
24181 enum dwarf_macro_record_type macinfo_type;
24182 int at_commandline;
24183 const gdb_byte *opcode_definitions[256];
24184
24185 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24186 &offset_size, section_is_gnu);
24187 if (mac_ptr == NULL)
24188 {
24189 /* We already issued a complaint. */
24190 return;
24191 }
24192
24193 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24194 GDB is still reading the definitions from command line. First
24195 DW_MACINFO_start_file will need to be ignored as it was already executed
24196 to create CURRENT_FILE for the main source holding also the command line
24197 definitions. On first met DW_MACINFO_start_file this flag is reset to
24198 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24199
24200 at_commandline = 1;
24201
24202 do
24203 {
24204 /* Do we at least have room for a macinfo type byte? */
24205 if (mac_ptr >= mac_end)
24206 {
24207 dwarf2_section_buffer_overflow_complaint (section);
24208 break;
24209 }
24210
24211 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24212 mac_ptr++;
24213
24214 /* Note that we rely on the fact that the corresponding GNU and
24215 DWARF constants are the same. */
24216 DIAGNOSTIC_PUSH
24217 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24218 switch (macinfo_type)
24219 {
24220 /* A zero macinfo type indicates the end of the macro
24221 information. */
24222 case 0:
24223 break;
24224
24225 case DW_MACRO_define:
24226 case DW_MACRO_undef:
24227 case DW_MACRO_define_strp:
24228 case DW_MACRO_undef_strp:
24229 case DW_MACRO_define_sup:
24230 case DW_MACRO_undef_sup:
24231 {
24232 unsigned int bytes_read;
24233 int line;
24234 const char *body;
24235 int is_define;
24236
24237 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24238 mac_ptr += bytes_read;
24239
24240 if (macinfo_type == DW_MACRO_define
24241 || macinfo_type == DW_MACRO_undef)
24242 {
24243 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24244 mac_ptr += bytes_read;
24245 }
24246 else
24247 {
24248 LONGEST str_offset;
24249
24250 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24251 mac_ptr += offset_size;
24252
24253 if (macinfo_type == DW_MACRO_define_sup
24254 || macinfo_type == DW_MACRO_undef_sup
24255 || section_is_dwz)
24256 {
24257 struct dwz_file *dwz
24258 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24259
24260 body = read_indirect_string_from_dwz (objfile,
24261 dwz, str_offset);
24262 }
24263 else
24264 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24265 abfd, str_offset);
24266 }
24267
24268 is_define = (macinfo_type == DW_MACRO_define
24269 || macinfo_type == DW_MACRO_define_strp
24270 || macinfo_type == DW_MACRO_define_sup);
24271 if (! current_file)
24272 {
24273 /* DWARF violation as no main source is present. */
24274 complaint (&symfile_complaints,
24275 _("debug info with no main source gives macro %s "
24276 "on line %d: %s"),
24277 is_define ? _("definition") : _("undefinition"),
24278 line, body);
24279 break;
24280 }
24281 if ((line == 0 && !at_commandline)
24282 || (line != 0 && at_commandline))
24283 complaint (&symfile_complaints,
24284 _("debug info gives %s macro %s with %s line %d: %s"),
24285 at_commandline ? _("command-line") : _("in-file"),
24286 is_define ? _("definition") : _("undefinition"),
24287 line == 0 ? _("zero") : _("non-zero"), line, body);
24288
24289 if (is_define)
24290 parse_macro_definition (current_file, line, body);
24291 else
24292 {
24293 gdb_assert (macinfo_type == DW_MACRO_undef
24294 || macinfo_type == DW_MACRO_undef_strp
24295 || macinfo_type == DW_MACRO_undef_sup);
24296 macro_undef (current_file, line, body);
24297 }
24298 }
24299 break;
24300
24301 case DW_MACRO_start_file:
24302 {
24303 unsigned int bytes_read;
24304 int line, file;
24305
24306 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24307 mac_ptr += bytes_read;
24308 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24309 mac_ptr += bytes_read;
24310
24311 if ((line == 0 && !at_commandline)
24312 || (line != 0 && at_commandline))
24313 complaint (&symfile_complaints,
24314 _("debug info gives source %d included "
24315 "from %s at %s line %d"),
24316 file, at_commandline ? _("command-line") : _("file"),
24317 line == 0 ? _("zero") : _("non-zero"), line);
24318
24319 if (at_commandline)
24320 {
24321 /* This DW_MACRO_start_file was executed in the
24322 pass one. */
24323 at_commandline = 0;
24324 }
24325 else
24326 current_file = macro_start_file (file, line, current_file, lh);
24327 }
24328 break;
24329
24330 case DW_MACRO_end_file:
24331 if (! current_file)
24332 complaint (&symfile_complaints,
24333 _("macro debug info has an unmatched "
24334 "`close_file' directive"));
24335 else
24336 {
24337 current_file = current_file->included_by;
24338 if (! current_file)
24339 {
24340 enum dwarf_macro_record_type next_type;
24341
24342 /* GCC circa March 2002 doesn't produce the zero
24343 type byte marking the end of the compilation
24344 unit. Complain if it's not there, but exit no
24345 matter what. */
24346
24347 /* Do we at least have room for a macinfo type byte? */
24348 if (mac_ptr >= mac_end)
24349 {
24350 dwarf2_section_buffer_overflow_complaint (section);
24351 return;
24352 }
24353
24354 /* We don't increment mac_ptr here, so this is just
24355 a look-ahead. */
24356 next_type
24357 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24358 mac_ptr);
24359 if (next_type != 0)
24360 complaint (&symfile_complaints,
24361 _("no terminating 0-type entry for "
24362 "macros in `.debug_macinfo' section"));
24363
24364 return;
24365 }
24366 }
24367 break;
24368
24369 case DW_MACRO_import:
24370 case DW_MACRO_import_sup:
24371 {
24372 LONGEST offset;
24373 void **slot;
24374 bfd *include_bfd = abfd;
24375 struct dwarf2_section_info *include_section = section;
24376 const gdb_byte *include_mac_end = mac_end;
24377 int is_dwz = section_is_dwz;
24378 const gdb_byte *new_mac_ptr;
24379
24380 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24381 mac_ptr += offset_size;
24382
24383 if (macinfo_type == DW_MACRO_import_sup)
24384 {
24385 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24386
24387 dwarf2_read_section (objfile, &dwz->macro);
24388
24389 include_section = &dwz->macro;
24390 include_bfd = get_section_bfd_owner (include_section);
24391 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24392 is_dwz = 1;
24393 }
24394
24395 new_mac_ptr = include_section->buffer + offset;
24396 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24397
24398 if (*slot != NULL)
24399 {
24400 /* This has actually happened; see
24401 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24402 complaint (&symfile_complaints,
24403 _("recursive DW_MACRO_import in "
24404 ".debug_macro section"));
24405 }
24406 else
24407 {
24408 *slot = (void *) new_mac_ptr;
24409
24410 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24411 include_bfd, new_mac_ptr,
24412 include_mac_end, current_file, lh,
24413 section, section_is_gnu, is_dwz,
24414 offset_size, include_hash);
24415
24416 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24417 }
24418 }
24419 break;
24420
24421 case DW_MACINFO_vendor_ext:
24422 if (!section_is_gnu)
24423 {
24424 unsigned int bytes_read;
24425
24426 /* This reads the constant, but since we don't recognize
24427 any vendor extensions, we ignore it. */
24428 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24429 mac_ptr += bytes_read;
24430 read_direct_string (abfd, mac_ptr, &bytes_read);
24431 mac_ptr += bytes_read;
24432
24433 /* We don't recognize any vendor extensions. */
24434 break;
24435 }
24436 /* FALLTHROUGH */
24437
24438 default:
24439 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24440 mac_ptr, mac_end, abfd, offset_size,
24441 section);
24442 if (mac_ptr == NULL)
24443 return;
24444 break;
24445 }
24446 DIAGNOSTIC_POP
24447 } while (macinfo_type != 0);
24448 }
24449
24450 static void
24451 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24452 int section_is_gnu)
24453 {
24454 struct dwarf2_per_objfile *dwarf2_per_objfile
24455 = cu->per_cu->dwarf2_per_objfile;
24456 struct objfile *objfile = dwarf2_per_objfile->objfile;
24457 struct line_header *lh = cu->line_header;
24458 bfd *abfd;
24459 const gdb_byte *mac_ptr, *mac_end;
24460 struct macro_source_file *current_file = 0;
24461 enum dwarf_macro_record_type macinfo_type;
24462 unsigned int offset_size = cu->header.offset_size;
24463 const gdb_byte *opcode_definitions[256];
24464 void **slot;
24465 struct dwarf2_section_info *section;
24466 const char *section_name;
24467
24468 if (cu->dwo_unit != NULL)
24469 {
24470 if (section_is_gnu)
24471 {
24472 section = &cu->dwo_unit->dwo_file->sections.macro;
24473 section_name = ".debug_macro.dwo";
24474 }
24475 else
24476 {
24477 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24478 section_name = ".debug_macinfo.dwo";
24479 }
24480 }
24481 else
24482 {
24483 if (section_is_gnu)
24484 {
24485 section = &dwarf2_per_objfile->macro;
24486 section_name = ".debug_macro";
24487 }
24488 else
24489 {
24490 section = &dwarf2_per_objfile->macinfo;
24491 section_name = ".debug_macinfo";
24492 }
24493 }
24494
24495 dwarf2_read_section (objfile, section);
24496 if (section->buffer == NULL)
24497 {
24498 complaint (&symfile_complaints, _("missing %s section"), section_name);
24499 return;
24500 }
24501 abfd = get_section_bfd_owner (section);
24502
24503 /* First pass: Find the name of the base filename.
24504 This filename is needed in order to process all macros whose definition
24505 (or undefinition) comes from the command line. These macros are defined
24506 before the first DW_MACINFO_start_file entry, and yet still need to be
24507 associated to the base file.
24508
24509 To determine the base file name, we scan the macro definitions until we
24510 reach the first DW_MACINFO_start_file entry. We then initialize
24511 CURRENT_FILE accordingly so that any macro definition found before the
24512 first DW_MACINFO_start_file can still be associated to the base file. */
24513
24514 mac_ptr = section->buffer + offset;
24515 mac_end = section->buffer + section->size;
24516
24517 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24518 &offset_size, section_is_gnu);
24519 if (mac_ptr == NULL)
24520 {
24521 /* We already issued a complaint. */
24522 return;
24523 }
24524
24525 do
24526 {
24527 /* Do we at least have room for a macinfo type byte? */
24528 if (mac_ptr >= mac_end)
24529 {
24530 /* Complaint is printed during the second pass as GDB will probably
24531 stop the first pass earlier upon finding
24532 DW_MACINFO_start_file. */
24533 break;
24534 }
24535
24536 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24537 mac_ptr++;
24538
24539 /* Note that we rely on the fact that the corresponding GNU and
24540 DWARF constants are the same. */
24541 DIAGNOSTIC_PUSH
24542 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24543 switch (macinfo_type)
24544 {
24545 /* A zero macinfo type indicates the end of the macro
24546 information. */
24547 case 0:
24548 break;
24549
24550 case DW_MACRO_define:
24551 case DW_MACRO_undef:
24552 /* Only skip the data by MAC_PTR. */
24553 {
24554 unsigned int bytes_read;
24555
24556 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24557 mac_ptr += bytes_read;
24558 read_direct_string (abfd, mac_ptr, &bytes_read);
24559 mac_ptr += bytes_read;
24560 }
24561 break;
24562
24563 case DW_MACRO_start_file:
24564 {
24565 unsigned int bytes_read;
24566 int line, file;
24567
24568 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24569 mac_ptr += bytes_read;
24570 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24571 mac_ptr += bytes_read;
24572
24573 current_file = macro_start_file (file, line, current_file, lh);
24574 }
24575 break;
24576
24577 case DW_MACRO_end_file:
24578 /* No data to skip by MAC_PTR. */
24579 break;
24580
24581 case DW_MACRO_define_strp:
24582 case DW_MACRO_undef_strp:
24583 case DW_MACRO_define_sup:
24584 case DW_MACRO_undef_sup:
24585 {
24586 unsigned int bytes_read;
24587
24588 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24589 mac_ptr += bytes_read;
24590 mac_ptr += offset_size;
24591 }
24592 break;
24593
24594 case DW_MACRO_import:
24595 case DW_MACRO_import_sup:
24596 /* Note that, according to the spec, a transparent include
24597 chain cannot call DW_MACRO_start_file. So, we can just
24598 skip this opcode. */
24599 mac_ptr += offset_size;
24600 break;
24601
24602 case DW_MACINFO_vendor_ext:
24603 /* Only skip the data by MAC_PTR. */
24604 if (!section_is_gnu)
24605 {
24606 unsigned int bytes_read;
24607
24608 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24609 mac_ptr += bytes_read;
24610 read_direct_string (abfd, mac_ptr, &bytes_read);
24611 mac_ptr += bytes_read;
24612 }
24613 /* FALLTHROUGH */
24614
24615 default:
24616 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24617 mac_ptr, mac_end, abfd, offset_size,
24618 section);
24619 if (mac_ptr == NULL)
24620 return;
24621 break;
24622 }
24623 DIAGNOSTIC_POP
24624 } while (macinfo_type != 0 && current_file == NULL);
24625
24626 /* Second pass: Process all entries.
24627
24628 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24629 command-line macro definitions/undefinitions. This flag is unset when we
24630 reach the first DW_MACINFO_start_file entry. */
24631
24632 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24633 htab_eq_pointer,
24634 NULL, xcalloc, xfree));
24635 mac_ptr = section->buffer + offset;
24636 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24637 *slot = (void *) mac_ptr;
24638 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24639 abfd, mac_ptr, mac_end,
24640 current_file, lh, section,
24641 section_is_gnu, 0, offset_size,
24642 include_hash.get ());
24643 }
24644
24645 /* Check if the attribute's form is a DW_FORM_block*
24646 if so return true else false. */
24647
24648 static int
24649 attr_form_is_block (const struct attribute *attr)
24650 {
24651 return (attr == NULL ? 0 :
24652 attr->form == DW_FORM_block1
24653 || attr->form == DW_FORM_block2
24654 || attr->form == DW_FORM_block4
24655 || attr->form == DW_FORM_block
24656 || attr->form == DW_FORM_exprloc);
24657 }
24658
24659 /* Return non-zero if ATTR's value is a section offset --- classes
24660 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24661 You may use DW_UNSND (attr) to retrieve such offsets.
24662
24663 Section 7.5.4, "Attribute Encodings", explains that no attribute
24664 may have a value that belongs to more than one of these classes; it
24665 would be ambiguous if we did, because we use the same forms for all
24666 of them. */
24667
24668 static int
24669 attr_form_is_section_offset (const struct attribute *attr)
24670 {
24671 return (attr->form == DW_FORM_data4
24672 || attr->form == DW_FORM_data8
24673 || attr->form == DW_FORM_sec_offset);
24674 }
24675
24676 /* Return non-zero if ATTR's value falls in the 'constant' class, or
24677 zero otherwise. When this function returns true, you can apply
24678 dwarf2_get_attr_constant_value to it.
24679
24680 However, note that for some attributes you must check
24681 attr_form_is_section_offset before using this test. DW_FORM_data4
24682 and DW_FORM_data8 are members of both the constant class, and of
24683 the classes that contain offsets into other debug sections
24684 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24685 that, if an attribute's can be either a constant or one of the
24686 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
24687 taken as section offsets, not constants.
24688
24689 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24690 cannot handle that. */
24691
24692 static int
24693 attr_form_is_constant (const struct attribute *attr)
24694 {
24695 switch (attr->form)
24696 {
24697 case DW_FORM_sdata:
24698 case DW_FORM_udata:
24699 case DW_FORM_data1:
24700 case DW_FORM_data2:
24701 case DW_FORM_data4:
24702 case DW_FORM_data8:
24703 case DW_FORM_implicit_const:
24704 return 1;
24705 default:
24706 return 0;
24707 }
24708 }
24709
24710
24711 /* DW_ADDR is always stored already as sect_offset; despite for the forms
24712 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24713
24714 static int
24715 attr_form_is_ref (const struct attribute *attr)
24716 {
24717 switch (attr->form)
24718 {
24719 case DW_FORM_ref_addr:
24720 case DW_FORM_ref1:
24721 case DW_FORM_ref2:
24722 case DW_FORM_ref4:
24723 case DW_FORM_ref8:
24724 case DW_FORM_ref_udata:
24725 case DW_FORM_GNU_ref_alt:
24726 return 1;
24727 default:
24728 return 0;
24729 }
24730 }
24731
24732 /* Return the .debug_loc section to use for CU.
24733 For DWO files use .debug_loc.dwo. */
24734
24735 static struct dwarf2_section_info *
24736 cu_debug_loc_section (struct dwarf2_cu *cu)
24737 {
24738 struct dwarf2_per_objfile *dwarf2_per_objfile
24739 = cu->per_cu->dwarf2_per_objfile;
24740
24741 if (cu->dwo_unit)
24742 {
24743 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24744
24745 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24746 }
24747 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24748 : &dwarf2_per_objfile->loc);
24749 }
24750
24751 /* A helper function that fills in a dwarf2_loclist_baton. */
24752
24753 static void
24754 fill_in_loclist_baton (struct dwarf2_cu *cu,
24755 struct dwarf2_loclist_baton *baton,
24756 const struct attribute *attr)
24757 {
24758 struct dwarf2_per_objfile *dwarf2_per_objfile
24759 = cu->per_cu->dwarf2_per_objfile;
24760 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24761
24762 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
24763
24764 baton->per_cu = cu->per_cu;
24765 gdb_assert (baton->per_cu);
24766 /* We don't know how long the location list is, but make sure we
24767 don't run off the edge of the section. */
24768 baton->size = section->size - DW_UNSND (attr);
24769 baton->data = section->buffer + DW_UNSND (attr);
24770 baton->base_address = cu->base_address;
24771 baton->from_dwo = cu->dwo_unit != NULL;
24772 }
24773
24774 static void
24775 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24776 struct dwarf2_cu *cu, int is_block)
24777 {
24778 struct dwarf2_per_objfile *dwarf2_per_objfile
24779 = cu->per_cu->dwarf2_per_objfile;
24780 struct objfile *objfile = dwarf2_per_objfile->objfile;
24781 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24782
24783 if (attr_form_is_section_offset (attr)
24784 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24785 the section. If so, fall through to the complaint in the
24786 other branch. */
24787 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
24788 {
24789 struct dwarf2_loclist_baton *baton;
24790
24791 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24792
24793 fill_in_loclist_baton (cu, baton, attr);
24794
24795 if (cu->base_known == 0)
24796 complaint (&symfile_complaints,
24797 _("Location list used without "
24798 "specifying the CU base address."));
24799
24800 SYMBOL_ACLASS_INDEX (sym) = (is_block
24801 ? dwarf2_loclist_block_index
24802 : dwarf2_loclist_index);
24803 SYMBOL_LOCATION_BATON (sym) = baton;
24804 }
24805 else
24806 {
24807 struct dwarf2_locexpr_baton *baton;
24808
24809 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24810 baton->per_cu = cu->per_cu;
24811 gdb_assert (baton->per_cu);
24812
24813 if (attr_form_is_block (attr))
24814 {
24815 /* Note that we're just copying the block's data pointer
24816 here, not the actual data. We're still pointing into the
24817 info_buffer for SYM's objfile; right now we never release
24818 that buffer, but when we do clean up properly this may
24819 need to change. */
24820 baton->size = DW_BLOCK (attr)->size;
24821 baton->data = DW_BLOCK (attr)->data;
24822 }
24823 else
24824 {
24825 dwarf2_invalid_attrib_class_complaint ("location description",
24826 SYMBOL_NATURAL_NAME (sym));
24827 baton->size = 0;
24828 }
24829
24830 SYMBOL_ACLASS_INDEX (sym) = (is_block
24831 ? dwarf2_locexpr_block_index
24832 : dwarf2_locexpr_index);
24833 SYMBOL_LOCATION_BATON (sym) = baton;
24834 }
24835 }
24836
24837 /* Return the OBJFILE associated with the compilation unit CU. If CU
24838 came from a separate debuginfo file, then the master objfile is
24839 returned. */
24840
24841 struct objfile *
24842 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24843 {
24844 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24845
24846 /* Return the master objfile, so that we can report and look up the
24847 correct file containing this variable. */
24848 if (objfile->separate_debug_objfile_backlink)
24849 objfile = objfile->separate_debug_objfile_backlink;
24850
24851 return objfile;
24852 }
24853
24854 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24855 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24856 CU_HEADERP first. */
24857
24858 static const struct comp_unit_head *
24859 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24860 struct dwarf2_per_cu_data *per_cu)
24861 {
24862 const gdb_byte *info_ptr;
24863
24864 if (per_cu->cu)
24865 return &per_cu->cu->header;
24866
24867 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24868
24869 memset (cu_headerp, 0, sizeof (*cu_headerp));
24870 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24871 rcuh_kind::COMPILE);
24872
24873 return cu_headerp;
24874 }
24875
24876 /* Return the address size given in the compilation unit header for CU. */
24877
24878 int
24879 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24880 {
24881 struct comp_unit_head cu_header_local;
24882 const struct comp_unit_head *cu_headerp;
24883
24884 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24885
24886 return cu_headerp->addr_size;
24887 }
24888
24889 /* Return the offset size given in the compilation unit header for CU. */
24890
24891 int
24892 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24893 {
24894 struct comp_unit_head cu_header_local;
24895 const struct comp_unit_head *cu_headerp;
24896
24897 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24898
24899 return cu_headerp->offset_size;
24900 }
24901
24902 /* See its dwarf2loc.h declaration. */
24903
24904 int
24905 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24906 {
24907 struct comp_unit_head cu_header_local;
24908 const struct comp_unit_head *cu_headerp;
24909
24910 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24911
24912 if (cu_headerp->version == 2)
24913 return cu_headerp->addr_size;
24914 else
24915 return cu_headerp->offset_size;
24916 }
24917
24918 /* Return the text offset of the CU. The returned offset comes from
24919 this CU's objfile. If this objfile came from a separate debuginfo
24920 file, then the offset may be different from the corresponding
24921 offset in the parent objfile. */
24922
24923 CORE_ADDR
24924 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24925 {
24926 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24927
24928 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24929 }
24930
24931 /* Return DWARF version number of PER_CU. */
24932
24933 short
24934 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24935 {
24936 return per_cu->dwarf_version;
24937 }
24938
24939 /* Locate the .debug_info compilation unit from CU's objfile which contains
24940 the DIE at OFFSET. Raises an error on failure. */
24941
24942 static struct dwarf2_per_cu_data *
24943 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24944 unsigned int offset_in_dwz,
24945 struct dwarf2_per_objfile *dwarf2_per_objfile)
24946 {
24947 struct dwarf2_per_cu_data *this_cu;
24948 int low, high;
24949 const sect_offset *cu_off;
24950
24951 low = 0;
24952 high = dwarf2_per_objfile->n_comp_units - 1;
24953 while (high > low)
24954 {
24955 struct dwarf2_per_cu_data *mid_cu;
24956 int mid = low + (high - low) / 2;
24957
24958 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
24959 cu_off = &mid_cu->sect_off;
24960 if (mid_cu->is_dwz > offset_in_dwz
24961 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
24962 high = mid;
24963 else
24964 low = mid + 1;
24965 }
24966 gdb_assert (low == high);
24967 this_cu = dwarf2_per_objfile->all_comp_units[low];
24968 cu_off = &this_cu->sect_off;
24969 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
24970 {
24971 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24972 error (_("Dwarf Error: could not find partial DIE containing "
24973 "offset %s [in module %s]"),
24974 sect_offset_str (sect_off),
24975 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
24976
24977 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24978 <= sect_off);
24979 return dwarf2_per_objfile->all_comp_units[low-1];
24980 }
24981 else
24982 {
24983 this_cu = dwarf2_per_objfile->all_comp_units[low];
24984 if (low == dwarf2_per_objfile->n_comp_units - 1
24985 && sect_off >= this_cu->sect_off + this_cu->length)
24986 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24987 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24988 return this_cu;
24989 }
24990 }
24991
24992 /* Initialize dwarf2_cu CU, owned by PER_CU. */
24993
24994 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24995 : per_cu (per_cu_),
24996 mark (0),
24997 has_loclist (0),
24998 checked_producer (0),
24999 producer_is_gxx_lt_4_6 (0),
25000 producer_is_gcc_lt_4_3 (0),
25001 producer_is_icc_lt_14 (0),
25002 processing_has_namespace_info (0)
25003 {
25004 per_cu->cu = this;
25005 }
25006
25007 /* Destroy a dwarf2_cu. */
25008
25009 dwarf2_cu::~dwarf2_cu ()
25010 {
25011 per_cu->cu = NULL;
25012 }
25013
25014 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25015
25016 static void
25017 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25018 enum language pretend_language)
25019 {
25020 struct attribute *attr;
25021
25022 /* Set the language we're debugging. */
25023 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25024 if (attr)
25025 set_cu_language (DW_UNSND (attr), cu);
25026 else
25027 {
25028 cu->language = pretend_language;
25029 cu->language_defn = language_def (cu->language);
25030 }
25031
25032 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25033 }
25034
25035 /* Increase the age counter on each cached compilation unit, and free
25036 any that are too old. */
25037
25038 static void
25039 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25040 {
25041 struct dwarf2_per_cu_data *per_cu, **last_chain;
25042
25043 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25044 per_cu = dwarf2_per_objfile->read_in_chain;
25045 while (per_cu != NULL)
25046 {
25047 per_cu->cu->last_used ++;
25048 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25049 dwarf2_mark (per_cu->cu);
25050 per_cu = per_cu->cu->read_in_chain;
25051 }
25052
25053 per_cu = dwarf2_per_objfile->read_in_chain;
25054 last_chain = &dwarf2_per_objfile->read_in_chain;
25055 while (per_cu != NULL)
25056 {
25057 struct dwarf2_per_cu_data *next_cu;
25058
25059 next_cu = per_cu->cu->read_in_chain;
25060
25061 if (!per_cu->cu->mark)
25062 {
25063 delete per_cu->cu;
25064 *last_chain = next_cu;
25065 }
25066 else
25067 last_chain = &per_cu->cu->read_in_chain;
25068
25069 per_cu = next_cu;
25070 }
25071 }
25072
25073 /* Remove a single compilation unit from the cache. */
25074
25075 static void
25076 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25077 {
25078 struct dwarf2_per_cu_data *per_cu, **last_chain;
25079 struct dwarf2_per_objfile *dwarf2_per_objfile
25080 = target_per_cu->dwarf2_per_objfile;
25081
25082 per_cu = dwarf2_per_objfile->read_in_chain;
25083 last_chain = &dwarf2_per_objfile->read_in_chain;
25084 while (per_cu != NULL)
25085 {
25086 struct dwarf2_per_cu_data *next_cu;
25087
25088 next_cu = per_cu->cu->read_in_chain;
25089
25090 if (per_cu == target_per_cu)
25091 {
25092 delete per_cu->cu;
25093 per_cu->cu = NULL;
25094 *last_chain = next_cu;
25095 break;
25096 }
25097 else
25098 last_chain = &per_cu->cu->read_in_chain;
25099
25100 per_cu = next_cu;
25101 }
25102 }
25103
25104 /* Release all extra memory associated with OBJFILE. */
25105
25106 void
25107 dwarf2_free_objfile (struct objfile *objfile)
25108 {
25109 struct dwarf2_per_objfile *dwarf2_per_objfile
25110 = get_dwarf2_per_objfile (objfile);
25111
25112 delete dwarf2_per_objfile;
25113 }
25114
25115 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25116 We store these in a hash table separate from the DIEs, and preserve them
25117 when the DIEs are flushed out of cache.
25118
25119 The CU "per_cu" pointer is needed because offset alone is not enough to
25120 uniquely identify the type. A file may have multiple .debug_types sections,
25121 or the type may come from a DWO file. Furthermore, while it's more logical
25122 to use per_cu->section+offset, with Fission the section with the data is in
25123 the DWO file but we don't know that section at the point we need it.
25124 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25125 because we can enter the lookup routine, get_die_type_at_offset, from
25126 outside this file, and thus won't necessarily have PER_CU->cu.
25127 Fortunately, PER_CU is stable for the life of the objfile. */
25128
25129 struct dwarf2_per_cu_offset_and_type
25130 {
25131 const struct dwarf2_per_cu_data *per_cu;
25132 sect_offset sect_off;
25133 struct type *type;
25134 };
25135
25136 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25137
25138 static hashval_t
25139 per_cu_offset_and_type_hash (const void *item)
25140 {
25141 const struct dwarf2_per_cu_offset_and_type *ofs
25142 = (const struct dwarf2_per_cu_offset_and_type *) item;
25143
25144 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25145 }
25146
25147 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25148
25149 static int
25150 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25151 {
25152 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25153 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25154 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25155 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25156
25157 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25158 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25159 }
25160
25161 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25162 table if necessary. For convenience, return TYPE.
25163
25164 The DIEs reading must have careful ordering to:
25165 * Not cause infite loops trying to read in DIEs as a prerequisite for
25166 reading current DIE.
25167 * Not trying to dereference contents of still incompletely read in types
25168 while reading in other DIEs.
25169 * Enable referencing still incompletely read in types just by a pointer to
25170 the type without accessing its fields.
25171
25172 Therefore caller should follow these rules:
25173 * Try to fetch any prerequisite types we may need to build this DIE type
25174 before building the type and calling set_die_type.
25175 * After building type call set_die_type for current DIE as soon as
25176 possible before fetching more types to complete the current type.
25177 * Make the type as complete as possible before fetching more types. */
25178
25179 static struct type *
25180 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25181 {
25182 struct dwarf2_per_objfile *dwarf2_per_objfile
25183 = cu->per_cu->dwarf2_per_objfile;
25184 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25185 struct objfile *objfile = dwarf2_per_objfile->objfile;
25186 struct attribute *attr;
25187 struct dynamic_prop prop;
25188
25189 /* For Ada types, make sure that the gnat-specific data is always
25190 initialized (if not already set). There are a few types where
25191 we should not be doing so, because the type-specific area is
25192 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25193 where the type-specific area is used to store the floatformat).
25194 But this is not a problem, because the gnat-specific information
25195 is actually not needed for these types. */
25196 if (need_gnat_info (cu)
25197 && TYPE_CODE (type) != TYPE_CODE_FUNC
25198 && TYPE_CODE (type) != TYPE_CODE_FLT
25199 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25200 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25201 && TYPE_CODE (type) != TYPE_CODE_METHOD
25202 && !HAVE_GNAT_AUX_INFO (type))
25203 INIT_GNAT_SPECIFIC (type);
25204
25205 /* Read DW_AT_allocated and set in type. */
25206 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25207 if (attr_form_is_block (attr))
25208 {
25209 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25210 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25211 }
25212 else if (attr != NULL)
25213 {
25214 complaint (&symfile_complaints,
25215 _("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25216 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25217 sect_offset_str (die->sect_off));
25218 }
25219
25220 /* Read DW_AT_associated and set in type. */
25221 attr = dwarf2_attr (die, DW_AT_associated, cu);
25222 if (attr_form_is_block (attr))
25223 {
25224 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25225 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25226 }
25227 else if (attr != NULL)
25228 {
25229 complaint (&symfile_complaints,
25230 _("DW_AT_associated has the wrong form (%s) at DIE %s"),
25231 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25232 sect_offset_str (die->sect_off));
25233 }
25234
25235 /* Read DW_AT_data_location and set in type. */
25236 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25237 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25238 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25239
25240 if (dwarf2_per_objfile->die_type_hash == NULL)
25241 {
25242 dwarf2_per_objfile->die_type_hash =
25243 htab_create_alloc_ex (127,
25244 per_cu_offset_and_type_hash,
25245 per_cu_offset_and_type_eq,
25246 NULL,
25247 &objfile->objfile_obstack,
25248 hashtab_obstack_allocate,
25249 dummy_obstack_deallocate);
25250 }
25251
25252 ofs.per_cu = cu->per_cu;
25253 ofs.sect_off = die->sect_off;
25254 ofs.type = type;
25255 slot = (struct dwarf2_per_cu_offset_and_type **)
25256 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25257 if (*slot)
25258 complaint (&symfile_complaints,
25259 _("A problem internal to GDB: DIE %s has type already set"),
25260 sect_offset_str (die->sect_off));
25261 *slot = XOBNEW (&objfile->objfile_obstack,
25262 struct dwarf2_per_cu_offset_and_type);
25263 **slot = ofs;
25264 return type;
25265 }
25266
25267 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25268 or return NULL if the die does not have a saved type. */
25269
25270 static struct type *
25271 get_die_type_at_offset (sect_offset sect_off,
25272 struct dwarf2_per_cu_data *per_cu)
25273 {
25274 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25275 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25276
25277 if (dwarf2_per_objfile->die_type_hash == NULL)
25278 return NULL;
25279
25280 ofs.per_cu = per_cu;
25281 ofs.sect_off = sect_off;
25282 slot = ((struct dwarf2_per_cu_offset_and_type *)
25283 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25284 if (slot)
25285 return slot->type;
25286 else
25287 return NULL;
25288 }
25289
25290 /* Look up the type for DIE in CU in die_type_hash,
25291 or return NULL if DIE does not have a saved type. */
25292
25293 static struct type *
25294 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25295 {
25296 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25297 }
25298
25299 /* Add a dependence relationship from CU to REF_PER_CU. */
25300
25301 static void
25302 dwarf2_add_dependence (struct dwarf2_cu *cu,
25303 struct dwarf2_per_cu_data *ref_per_cu)
25304 {
25305 void **slot;
25306
25307 if (cu->dependencies == NULL)
25308 cu->dependencies
25309 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25310 NULL, &cu->comp_unit_obstack,
25311 hashtab_obstack_allocate,
25312 dummy_obstack_deallocate);
25313
25314 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25315 if (*slot == NULL)
25316 *slot = ref_per_cu;
25317 }
25318
25319 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25320 Set the mark field in every compilation unit in the
25321 cache that we must keep because we are keeping CU. */
25322
25323 static int
25324 dwarf2_mark_helper (void **slot, void *data)
25325 {
25326 struct dwarf2_per_cu_data *per_cu;
25327
25328 per_cu = (struct dwarf2_per_cu_data *) *slot;
25329
25330 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25331 reading of the chain. As such dependencies remain valid it is not much
25332 useful to track and undo them during QUIT cleanups. */
25333 if (per_cu->cu == NULL)
25334 return 1;
25335
25336 if (per_cu->cu->mark)
25337 return 1;
25338 per_cu->cu->mark = 1;
25339
25340 if (per_cu->cu->dependencies != NULL)
25341 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25342
25343 return 1;
25344 }
25345
25346 /* Set the mark field in CU and in every other compilation unit in the
25347 cache that we must keep because we are keeping CU. */
25348
25349 static void
25350 dwarf2_mark (struct dwarf2_cu *cu)
25351 {
25352 if (cu->mark)
25353 return;
25354 cu->mark = 1;
25355 if (cu->dependencies != NULL)
25356 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25357 }
25358
25359 static void
25360 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25361 {
25362 while (per_cu)
25363 {
25364 per_cu->cu->mark = 0;
25365 per_cu = per_cu->cu->read_in_chain;
25366 }
25367 }
25368
25369 /* Trivial hash function for partial_die_info: the hash value of a DIE
25370 is its offset in .debug_info for this objfile. */
25371
25372 static hashval_t
25373 partial_die_hash (const void *item)
25374 {
25375 const struct partial_die_info *part_die
25376 = (const struct partial_die_info *) item;
25377
25378 return to_underlying (part_die->sect_off);
25379 }
25380
25381 /* Trivial comparison function for partial_die_info structures: two DIEs
25382 are equal if they have the same offset. */
25383
25384 static int
25385 partial_die_eq (const void *item_lhs, const void *item_rhs)
25386 {
25387 const struct partial_die_info *part_die_lhs
25388 = (const struct partial_die_info *) item_lhs;
25389 const struct partial_die_info *part_die_rhs
25390 = (const struct partial_die_info *) item_rhs;
25391
25392 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25393 }
25394
25395 static struct cmd_list_element *set_dwarf_cmdlist;
25396 static struct cmd_list_element *show_dwarf_cmdlist;
25397
25398 static void
25399 set_dwarf_cmd (const char *args, int from_tty)
25400 {
25401 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25402 gdb_stdout);
25403 }
25404
25405 static void
25406 show_dwarf_cmd (const char *args, int from_tty)
25407 {
25408 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25409 }
25410
25411 int dwarf_always_disassemble;
25412
25413 static void
25414 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25415 struct cmd_list_element *c, const char *value)
25416 {
25417 fprintf_filtered (file,
25418 _("Whether to always disassemble "
25419 "DWARF expressions is %s.\n"),
25420 value);
25421 }
25422
25423 static void
25424 show_check_physname (struct ui_file *file, int from_tty,
25425 struct cmd_list_element *c, const char *value)
25426 {
25427 fprintf_filtered (file,
25428 _("Whether to check \"physname\" is %s.\n"),
25429 value);
25430 }
25431
25432 void
25433 _initialize_dwarf2_read (void)
25434 {
25435
25436 dwarf2_objfile_data_key = register_objfile_data ();
25437
25438 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25439 Set DWARF specific variables.\n\
25440 Configure DWARF variables such as the cache size"),
25441 &set_dwarf_cmdlist, "maintenance set dwarf ",
25442 0/*allow-unknown*/, &maintenance_set_cmdlist);
25443
25444 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25445 Show DWARF specific variables\n\
25446 Show DWARF variables such as the cache size"),
25447 &show_dwarf_cmdlist, "maintenance show dwarf ",
25448 0/*allow-unknown*/, &maintenance_show_cmdlist);
25449
25450 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25451 &dwarf_max_cache_age, _("\
25452 Set the upper bound on the age of cached DWARF compilation units."), _("\
25453 Show the upper bound on the age of cached DWARF compilation units."), _("\
25454 A higher limit means that cached compilation units will be stored\n\
25455 in memory longer, and more total memory will be used. Zero disables\n\
25456 caching, which can slow down startup."),
25457 NULL,
25458 show_dwarf_max_cache_age,
25459 &set_dwarf_cmdlist,
25460 &show_dwarf_cmdlist);
25461
25462 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25463 &dwarf_always_disassemble, _("\
25464 Set whether `info address' always disassembles DWARF expressions."), _("\
25465 Show whether `info address' always disassembles DWARF expressions."), _("\
25466 When enabled, DWARF expressions are always printed in an assembly-like\n\
25467 syntax. When disabled, expressions will be printed in a more\n\
25468 conversational style, when possible."),
25469 NULL,
25470 show_dwarf_always_disassemble,
25471 &set_dwarf_cmdlist,
25472 &show_dwarf_cmdlist);
25473
25474 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25475 Set debugging of the DWARF reader."), _("\
25476 Show debugging of the DWARF reader."), _("\
25477 When enabled (non-zero), debugging messages are printed during DWARF\n\
25478 reading and symtab expansion. A value of 1 (one) provides basic\n\
25479 information. A value greater than 1 provides more verbose information."),
25480 NULL,
25481 NULL,
25482 &setdebuglist, &showdebuglist);
25483
25484 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25485 Set debugging of the DWARF DIE reader."), _("\
25486 Show debugging of the DWARF DIE reader."), _("\
25487 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25488 The value is the maximum depth to print."),
25489 NULL,
25490 NULL,
25491 &setdebuglist, &showdebuglist);
25492
25493 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25494 Set debugging of the dwarf line reader."), _("\
25495 Show debugging of the dwarf line reader."), _("\
25496 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25497 A value of 1 (one) provides basic information.\n\
25498 A value greater than 1 provides more verbose information."),
25499 NULL,
25500 NULL,
25501 &setdebuglist, &showdebuglist);
25502
25503 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25504 Set cross-checking of \"physname\" code against demangler."), _("\
25505 Show cross-checking of \"physname\" code against demangler."), _("\
25506 When enabled, GDB's internal \"physname\" code is checked against\n\
25507 the demangler."),
25508 NULL, show_check_physname,
25509 &setdebuglist, &showdebuglist);
25510
25511 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25512 no_class, &use_deprecated_index_sections, _("\
25513 Set whether to use deprecated gdb_index sections."), _("\
25514 Show whether to use deprecated gdb_index sections."), _("\
25515 When enabled, deprecated .gdb_index sections are used anyway.\n\
25516 Normally they are ignored either because of a missing feature or\n\
25517 performance issue.\n\
25518 Warning: This option must be enabled before gdb reads the file."),
25519 NULL,
25520 NULL,
25521 &setlist, &showlist);
25522
25523 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25524 &dwarf2_locexpr_funcs);
25525 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25526 &dwarf2_loclist_funcs);
25527
25528 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25529 &dwarf2_block_frame_base_locexpr_funcs);
25530 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25531 &dwarf2_block_frame_base_loclist_funcs);
25532
25533 #if GDB_SELF_TEST
25534 selftests::register_test ("dw2_expand_symtabs_matching",
25535 selftests::dw2_expand_symtabs_matching::run_test);
25536 #endif
25537 }
This page took 0.78969 seconds and 5 git commands to generate.