842620afb8679cfdcbbe6af8065e702a07345519
[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 (dwarf2_per_cu_data *per_cu : all_comp_units)
2141 VEC_free (dwarf2_per_cu_ptr, per_cu->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->all_comp_units.size ())
2924 {
2925 index -= this->all_comp_units.size ();
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->all_comp_units.size ());
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 {
2986 for (offset_type i = 0; i < n_elements; i += 2)
2987 {
2988 gdb_static_assert (sizeof (ULONGEST) >= 8);
2989
2990 sect_offset sect_off
2991 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2992 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2993 cu_list += 2 * 8;
2994
2995 dwarf2_per_cu_data *per_cu
2996 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2997 sect_off, length);
2998 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
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 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3011 dwarf2_per_objfile->all_comp_units.reserve
3012 ((cu_list_elements + dwz_elements) / 2);
3013
3014 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
3015 &dwarf2_per_objfile->info, 0);
3016
3017 if (dwz_elements == 0)
3018 return;
3019
3020 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3021 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
3022 &dwz->info, 1);
3023 }
3024
3025 /* Create the signatured type hash table from the index. */
3026
3027 static void
3028 create_signatured_type_table_from_index
3029 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3030 struct dwarf2_section_info *section,
3031 const gdb_byte *bytes,
3032 offset_type elements)
3033 {
3034 struct objfile *objfile = dwarf2_per_objfile->objfile;
3035
3036 dwarf2_per_objfile->n_type_units
3037 = dwarf2_per_objfile->n_allocated_type_units
3038 = elements / 3;
3039 dwarf2_per_objfile->all_type_units =
3040 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3041
3042 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3043
3044 for (offset_type i = 0; i < elements; i += 3)
3045 {
3046 struct signatured_type *sig_type;
3047 ULONGEST signature;
3048 void **slot;
3049 cu_offset type_offset_in_tu;
3050
3051 gdb_static_assert (sizeof (ULONGEST) >= 8);
3052 sect_offset sect_off
3053 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3054 type_offset_in_tu
3055 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3056 BFD_ENDIAN_LITTLE);
3057 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3058 bytes += 3 * 8;
3059
3060 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3061 struct signatured_type);
3062 sig_type->signature = signature;
3063 sig_type->type_offset_in_tu = type_offset_in_tu;
3064 sig_type->per_cu.is_debug_types = 1;
3065 sig_type->per_cu.section = section;
3066 sig_type->per_cu.sect_off = sect_off;
3067 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3068 sig_type->per_cu.v.quick
3069 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3070 struct dwarf2_per_cu_quick_data);
3071
3072 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3073 *slot = sig_type;
3074
3075 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
3076 }
3077
3078 dwarf2_per_objfile->signatured_types = sig_types_hash;
3079 }
3080
3081 /* Create the signatured type hash table from .debug_names. */
3082
3083 static void
3084 create_signatured_type_table_from_debug_names
3085 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3086 const mapped_debug_names &map,
3087 struct dwarf2_section_info *section,
3088 struct dwarf2_section_info *abbrev_section)
3089 {
3090 struct objfile *objfile = dwarf2_per_objfile->objfile;
3091
3092 dwarf2_read_section (objfile, section);
3093 dwarf2_read_section (objfile, abbrev_section);
3094
3095 dwarf2_per_objfile->n_type_units
3096 = dwarf2_per_objfile->n_allocated_type_units
3097 = map.tu_count;
3098 dwarf2_per_objfile->all_type_units
3099 = XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
3100
3101 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3102
3103 for (uint32_t i = 0; i < map.tu_count; ++i)
3104 {
3105 struct signatured_type *sig_type;
3106 void **slot;
3107
3108 sect_offset sect_off
3109 = (sect_offset) (extract_unsigned_integer
3110 (map.tu_table_reordered + i * map.offset_size,
3111 map.offset_size,
3112 map.dwarf5_byte_order));
3113
3114 comp_unit_head cu_header;
3115 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3116 abbrev_section,
3117 section->buffer + to_underlying (sect_off),
3118 rcuh_kind::TYPE);
3119
3120 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3121 struct signatured_type);
3122 sig_type->signature = cu_header.signature;
3123 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3124 sig_type->per_cu.is_debug_types = 1;
3125 sig_type->per_cu.section = section;
3126 sig_type->per_cu.sect_off = sect_off;
3127 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3128 sig_type->per_cu.v.quick
3129 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3130 struct dwarf2_per_cu_quick_data);
3131
3132 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3133 *slot = sig_type;
3134
3135 dwarf2_per_objfile->all_type_units[i] = sig_type;
3136 }
3137
3138 dwarf2_per_objfile->signatured_types = sig_types_hash;
3139 }
3140
3141 /* Read the address map data from the mapped index, and use it to
3142 populate the objfile's psymtabs_addrmap. */
3143
3144 static void
3145 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3146 struct mapped_index *index)
3147 {
3148 struct objfile *objfile = dwarf2_per_objfile->objfile;
3149 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3150 const gdb_byte *iter, *end;
3151 struct addrmap *mutable_map;
3152 CORE_ADDR baseaddr;
3153
3154 auto_obstack temp_obstack;
3155
3156 mutable_map = addrmap_create_mutable (&temp_obstack);
3157
3158 iter = index->address_table.data ();
3159 end = iter + index->address_table.size ();
3160
3161 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3162
3163 while (iter < end)
3164 {
3165 ULONGEST hi, lo, cu_index;
3166 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3167 iter += 8;
3168 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3169 iter += 8;
3170 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3171 iter += 4;
3172
3173 if (lo > hi)
3174 {
3175 complaint (&symfile_complaints,
3176 _(".gdb_index address table has invalid range (%s - %s)"),
3177 hex_string (lo), hex_string (hi));
3178 continue;
3179 }
3180
3181 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
3182 {
3183 complaint (&symfile_complaints,
3184 _(".gdb_index address table has invalid CU number %u"),
3185 (unsigned) cu_index);
3186 continue;
3187 }
3188
3189 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3190 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
3191 addrmap_set_empty (mutable_map, lo, hi - 1,
3192 dwarf2_per_objfile->get_cu (cu_index));
3193 }
3194
3195 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3196 &objfile->objfile_obstack);
3197 }
3198
3199 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3200 populate the objfile's psymtabs_addrmap. */
3201
3202 static void
3203 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3204 struct dwarf2_section_info *section)
3205 {
3206 struct objfile *objfile = dwarf2_per_objfile->objfile;
3207 bfd *abfd = objfile->obfd;
3208 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3209 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3210 SECT_OFF_TEXT (objfile));
3211
3212 auto_obstack temp_obstack;
3213 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3214
3215 std::unordered_map<sect_offset,
3216 dwarf2_per_cu_data *,
3217 gdb::hash_enum<sect_offset>>
3218 debug_info_offset_to_per_cu;
3219 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3220 {
3221 const auto insertpair
3222 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3223 if (!insertpair.second)
3224 {
3225 warning (_("Section .debug_aranges in %s has duplicate "
3226 "debug_info_offset %s, ignoring .debug_aranges."),
3227 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3228 return;
3229 }
3230 }
3231
3232 dwarf2_read_section (objfile, section);
3233
3234 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3235
3236 const gdb_byte *addr = section->buffer;
3237
3238 while (addr < section->buffer + section->size)
3239 {
3240 const gdb_byte *const entry_addr = addr;
3241 unsigned int bytes_read;
3242
3243 const LONGEST entry_length = read_initial_length (abfd, addr,
3244 &bytes_read);
3245 addr += bytes_read;
3246
3247 const gdb_byte *const entry_end = addr + entry_length;
3248 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3249 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3250 if (addr + entry_length > section->buffer + section->size)
3251 {
3252 warning (_("Section .debug_aranges in %s entry at offset %zu "
3253 "length %s exceeds section length %s, "
3254 "ignoring .debug_aranges."),
3255 objfile_name (objfile), entry_addr - section->buffer,
3256 plongest (bytes_read + entry_length),
3257 pulongest (section->size));
3258 return;
3259 }
3260
3261 /* The version number. */
3262 const uint16_t version = read_2_bytes (abfd, addr);
3263 addr += 2;
3264 if (version != 2)
3265 {
3266 warning (_("Section .debug_aranges in %s entry at offset %zu "
3267 "has unsupported version %d, ignoring .debug_aranges."),
3268 objfile_name (objfile), entry_addr - section->buffer,
3269 version);
3270 return;
3271 }
3272
3273 const uint64_t debug_info_offset
3274 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3275 addr += offset_size;
3276 const auto per_cu_it
3277 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3278 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3279 {
3280 warning (_("Section .debug_aranges in %s entry at offset %zu "
3281 "debug_info_offset %s does not exists, "
3282 "ignoring .debug_aranges."),
3283 objfile_name (objfile), entry_addr - section->buffer,
3284 pulongest (debug_info_offset));
3285 return;
3286 }
3287 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3288
3289 const uint8_t address_size = *addr++;
3290 if (address_size < 1 || address_size > 8)
3291 {
3292 warning (_("Section .debug_aranges in %s entry at offset %zu "
3293 "address_size %u is invalid, ignoring .debug_aranges."),
3294 objfile_name (objfile), entry_addr - section->buffer,
3295 address_size);
3296 return;
3297 }
3298
3299 const uint8_t segment_selector_size = *addr++;
3300 if (segment_selector_size != 0)
3301 {
3302 warning (_("Section .debug_aranges in %s entry at offset %zu "
3303 "segment_selector_size %u is not supported, "
3304 "ignoring .debug_aranges."),
3305 objfile_name (objfile), entry_addr - section->buffer,
3306 segment_selector_size);
3307 return;
3308 }
3309
3310 /* Must pad to an alignment boundary that is twice the address
3311 size. It is undocumented by the DWARF standard but GCC does
3312 use it. */
3313 for (size_t padding = ((-(addr - section->buffer))
3314 & (2 * address_size - 1));
3315 padding > 0; padding--)
3316 if (*addr++ != 0)
3317 {
3318 warning (_("Section .debug_aranges in %s entry at offset %zu "
3319 "padding is not zero, ignoring .debug_aranges."),
3320 objfile_name (objfile), entry_addr - section->buffer);
3321 return;
3322 }
3323
3324 for (;;)
3325 {
3326 if (addr + 2 * address_size > entry_end)
3327 {
3328 warning (_("Section .debug_aranges in %s entry at offset %zu "
3329 "address list is not properly terminated, "
3330 "ignoring .debug_aranges."),
3331 objfile_name (objfile), entry_addr - section->buffer);
3332 return;
3333 }
3334 ULONGEST start = extract_unsigned_integer (addr, address_size,
3335 dwarf5_byte_order);
3336 addr += address_size;
3337 ULONGEST length = extract_unsigned_integer (addr, address_size,
3338 dwarf5_byte_order);
3339 addr += address_size;
3340 if (start == 0 && length == 0)
3341 break;
3342 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3343 {
3344 /* Symbol was eliminated due to a COMDAT group. */
3345 continue;
3346 }
3347 ULONGEST end = start + length;
3348 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3349 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3350 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3351 }
3352 }
3353
3354 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3355 &objfile->objfile_obstack);
3356 }
3357
3358 /* Find a slot in the mapped index INDEX for the object named NAME.
3359 If NAME is found, set *VEC_OUT to point to the CU vector in the
3360 constant pool and return true. If NAME cannot be found, return
3361 false. */
3362
3363 static bool
3364 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3365 offset_type **vec_out)
3366 {
3367 offset_type hash;
3368 offset_type slot, step;
3369 int (*cmp) (const char *, const char *);
3370
3371 gdb::unique_xmalloc_ptr<char> without_params;
3372 if (current_language->la_language == language_cplus
3373 || current_language->la_language == language_fortran
3374 || current_language->la_language == language_d)
3375 {
3376 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3377 not contain any. */
3378
3379 if (strchr (name, '(') != NULL)
3380 {
3381 without_params = cp_remove_params (name);
3382
3383 if (without_params != NULL)
3384 name = without_params.get ();
3385 }
3386 }
3387
3388 /* Index version 4 did not support case insensitive searches. But the
3389 indices for case insensitive languages are built in lowercase, therefore
3390 simulate our NAME being searched is also lowercased. */
3391 hash = mapped_index_string_hash ((index->version == 4
3392 && case_sensitivity == case_sensitive_off
3393 ? 5 : index->version),
3394 name);
3395
3396 slot = hash & (index->symbol_table.size () - 1);
3397 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3398 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3399
3400 for (;;)
3401 {
3402 const char *str;
3403
3404 const auto &bucket = index->symbol_table[slot];
3405 if (bucket.name == 0 && bucket.vec == 0)
3406 return false;
3407
3408 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3409 if (!cmp (name, str))
3410 {
3411 *vec_out = (offset_type *) (index->constant_pool
3412 + MAYBE_SWAP (bucket.vec));
3413 return true;
3414 }
3415
3416 slot = (slot + step) & (index->symbol_table.size () - 1);
3417 }
3418 }
3419
3420 /* A helper function that reads the .gdb_index from SECTION and fills
3421 in MAP. FILENAME is the name of the file containing the section;
3422 it is used for error reporting. DEPRECATED_OK is nonzero if it is
3423 ok to use deprecated sections.
3424
3425 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3426 out parameters that are filled in with information about the CU and
3427 TU lists in the section.
3428
3429 Returns 1 if all went well, 0 otherwise. */
3430
3431 static int
3432 read_index_from_section (struct objfile *objfile,
3433 const char *filename,
3434 int deprecated_ok,
3435 struct dwarf2_section_info *section,
3436 struct mapped_index *map,
3437 const gdb_byte **cu_list,
3438 offset_type *cu_list_elements,
3439 const gdb_byte **types_list,
3440 offset_type *types_list_elements)
3441 {
3442 const gdb_byte *addr;
3443 offset_type version;
3444 offset_type *metadata;
3445 int i;
3446
3447 if (dwarf2_section_empty_p (section))
3448 return 0;
3449
3450 /* Older elfutils strip versions could keep the section in the main
3451 executable while splitting it for the separate debug info file. */
3452 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
3453 return 0;
3454
3455 dwarf2_read_section (objfile, section);
3456
3457 addr = section->buffer;
3458 /* Version check. */
3459 version = MAYBE_SWAP (*(offset_type *) addr);
3460 /* Versions earlier than 3 emitted every copy of a psymbol. This
3461 causes the index to behave very poorly for certain requests. Version 3
3462 contained incomplete addrmap. So, it seems better to just ignore such
3463 indices. */
3464 if (version < 4)
3465 {
3466 static int warning_printed = 0;
3467 if (!warning_printed)
3468 {
3469 warning (_("Skipping obsolete .gdb_index section in %s."),
3470 filename);
3471 warning_printed = 1;
3472 }
3473 return 0;
3474 }
3475 /* Index version 4 uses a different hash function than index version
3476 5 and later.
3477
3478 Versions earlier than 6 did not emit psymbols for inlined
3479 functions. Using these files will cause GDB not to be able to
3480 set breakpoints on inlined functions by name, so we ignore these
3481 indices unless the user has done
3482 "set use-deprecated-index-sections on". */
3483 if (version < 6 && !deprecated_ok)
3484 {
3485 static int warning_printed = 0;
3486 if (!warning_printed)
3487 {
3488 warning (_("\
3489 Skipping deprecated .gdb_index section in %s.\n\
3490 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3491 to use the section anyway."),
3492 filename);
3493 warning_printed = 1;
3494 }
3495 return 0;
3496 }
3497 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3498 of the TU (for symbols coming from TUs),
3499 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3500 Plus gold-generated indices can have duplicate entries for global symbols,
3501 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3502 These are just performance bugs, and we can't distinguish gdb-generated
3503 indices from gold-generated ones, so issue no warning here. */
3504
3505 /* Indexes with higher version than the one supported by GDB may be no
3506 longer backward compatible. */
3507 if (version > 8)
3508 return 0;
3509
3510 map->version = version;
3511 map->total_size = section->size;
3512
3513 metadata = (offset_type *) (addr + sizeof (offset_type));
3514
3515 i = 0;
3516 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3517 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3518 / 8);
3519 ++i;
3520
3521 *types_list = addr + MAYBE_SWAP (metadata[i]);
3522 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3523 - MAYBE_SWAP (metadata[i]))
3524 / 8);
3525 ++i;
3526
3527 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3528 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3529 map->address_table
3530 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3531 ++i;
3532
3533 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3534 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3535 map->symbol_table
3536 = gdb::array_view<mapped_index::symbol_table_slot>
3537 ((mapped_index::symbol_table_slot *) symbol_table,
3538 (mapped_index::symbol_table_slot *) symbol_table_end);
3539
3540 ++i;
3541 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3542
3543 return 1;
3544 }
3545
3546 /* Read .gdb_index. If everything went ok, initialize the "quick"
3547 elements of all the CUs and return 1. Otherwise, return 0. */
3548
3549 static int
3550 dwarf2_read_index (struct dwarf2_per_objfile *dwarf2_per_objfile)
3551 {
3552 struct mapped_index local_map, *map;
3553 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3554 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3555 struct dwz_file *dwz;
3556 struct objfile *objfile = dwarf2_per_objfile->objfile;
3557
3558 if (!read_index_from_section (objfile, objfile_name (objfile),
3559 use_deprecated_index_sections,
3560 &dwarf2_per_objfile->gdb_index, &local_map,
3561 &cu_list, &cu_list_elements,
3562 &types_list, &types_list_elements))
3563 return 0;
3564
3565 /* Don't use the index if it's empty. */
3566 if (local_map.symbol_table.empty ())
3567 return 0;
3568
3569 /* If there is a .dwz file, read it so we can get its CU list as
3570 well. */
3571 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3572 if (dwz != NULL)
3573 {
3574 struct mapped_index dwz_map;
3575 const gdb_byte *dwz_types_ignore;
3576 offset_type dwz_types_elements_ignore;
3577
3578 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3579 1,
3580 &dwz->gdb_index, &dwz_map,
3581 &dwz_list, &dwz_list_elements,
3582 &dwz_types_ignore,
3583 &dwz_types_elements_ignore))
3584 {
3585 warning (_("could not read '.gdb_index' section from %s; skipping"),
3586 bfd_get_filename (dwz->dwz_bfd));
3587 return 0;
3588 }
3589 }
3590
3591 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3592 dwz_list, dwz_list_elements);
3593
3594 if (types_list_elements)
3595 {
3596 struct dwarf2_section_info *section;
3597
3598 /* We can only handle a single .debug_types when we have an
3599 index. */
3600 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3601 return 0;
3602
3603 section = VEC_index (dwarf2_section_info_def,
3604 dwarf2_per_objfile->types, 0);
3605
3606 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3607 types_list, types_list_elements);
3608 }
3609
3610 create_addrmap_from_index (dwarf2_per_objfile, &local_map);
3611
3612 map = XOBNEW (&objfile->objfile_obstack, struct mapped_index);
3613 map = new (map) mapped_index ();
3614 *map = local_map;
3615
3616 dwarf2_per_objfile->index_table = map;
3617 dwarf2_per_objfile->using_index = 1;
3618 dwarf2_per_objfile->quick_file_names_table =
3619 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3620
3621 return 1;
3622 }
3623
3624 /* die_reader_func for dw2_get_file_names. */
3625
3626 static void
3627 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3628 const gdb_byte *info_ptr,
3629 struct die_info *comp_unit_die,
3630 int has_children,
3631 void *data)
3632 {
3633 struct dwarf2_cu *cu = reader->cu;
3634 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3635 struct dwarf2_per_objfile *dwarf2_per_objfile
3636 = cu->per_cu->dwarf2_per_objfile;
3637 struct objfile *objfile = dwarf2_per_objfile->objfile;
3638 struct dwarf2_per_cu_data *lh_cu;
3639 struct attribute *attr;
3640 int i;
3641 void **slot;
3642 struct quick_file_names *qfn;
3643
3644 gdb_assert (! this_cu->is_debug_types);
3645
3646 /* Our callers never want to match partial units -- instead they
3647 will match the enclosing full CU. */
3648 if (comp_unit_die->tag == DW_TAG_partial_unit)
3649 {
3650 this_cu->v.quick->no_file_data = 1;
3651 return;
3652 }
3653
3654 lh_cu = this_cu;
3655 slot = NULL;
3656
3657 line_header_up lh;
3658 sect_offset line_offset {};
3659
3660 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3661 if (attr)
3662 {
3663 struct quick_file_names find_entry;
3664
3665 line_offset = (sect_offset) DW_UNSND (attr);
3666
3667 /* We may have already read in this line header (TU line header sharing).
3668 If we have we're done. */
3669 find_entry.hash.dwo_unit = cu->dwo_unit;
3670 find_entry.hash.line_sect_off = line_offset;
3671 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3672 &find_entry, INSERT);
3673 if (*slot != NULL)
3674 {
3675 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3676 return;
3677 }
3678
3679 lh = dwarf_decode_line_header (line_offset, cu);
3680 }
3681 if (lh == NULL)
3682 {
3683 lh_cu->v.quick->no_file_data = 1;
3684 return;
3685 }
3686
3687 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3688 qfn->hash.dwo_unit = cu->dwo_unit;
3689 qfn->hash.line_sect_off = line_offset;
3690 gdb_assert (slot != NULL);
3691 *slot = qfn;
3692
3693 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3694
3695 qfn->num_file_names = lh->file_names.size ();
3696 qfn->file_names =
3697 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3698 for (i = 0; i < lh->file_names.size (); ++i)
3699 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3700 qfn->real_names = NULL;
3701
3702 lh_cu->v.quick->file_names = qfn;
3703 }
3704
3705 /* A helper for the "quick" functions which attempts to read the line
3706 table for THIS_CU. */
3707
3708 static struct quick_file_names *
3709 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3710 {
3711 /* This should never be called for TUs. */
3712 gdb_assert (! this_cu->is_debug_types);
3713 /* Nor type unit groups. */
3714 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3715
3716 if (this_cu->v.quick->file_names != NULL)
3717 return this_cu->v.quick->file_names;
3718 /* If we know there is no line data, no point in looking again. */
3719 if (this_cu->v.quick->no_file_data)
3720 return NULL;
3721
3722 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3723
3724 if (this_cu->v.quick->no_file_data)
3725 return NULL;
3726 return this_cu->v.quick->file_names;
3727 }
3728
3729 /* A helper for the "quick" functions which computes and caches the
3730 real path for a given file name from the line table. */
3731
3732 static const char *
3733 dw2_get_real_path (struct objfile *objfile,
3734 struct quick_file_names *qfn, int index)
3735 {
3736 if (qfn->real_names == NULL)
3737 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3738 qfn->num_file_names, const char *);
3739
3740 if (qfn->real_names[index] == NULL)
3741 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3742
3743 return qfn->real_names[index];
3744 }
3745
3746 static struct symtab *
3747 dw2_find_last_source_symtab (struct objfile *objfile)
3748 {
3749 struct dwarf2_per_objfile *dwarf2_per_objfile
3750 = get_dwarf2_per_objfile (objfile);
3751 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3752 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu);
3753
3754 if (cust == NULL)
3755 return NULL;
3756
3757 return compunit_primary_filetab (cust);
3758 }
3759
3760 /* Traversal function for dw2_forget_cached_source_info. */
3761
3762 static int
3763 dw2_free_cached_file_names (void **slot, void *info)
3764 {
3765 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3766
3767 if (file_data->real_names)
3768 {
3769 int i;
3770
3771 for (i = 0; i < file_data->num_file_names; ++i)
3772 {
3773 xfree ((void*) file_data->real_names[i]);
3774 file_data->real_names[i] = NULL;
3775 }
3776 }
3777
3778 return 1;
3779 }
3780
3781 static void
3782 dw2_forget_cached_source_info (struct objfile *objfile)
3783 {
3784 struct dwarf2_per_objfile *dwarf2_per_objfile
3785 = get_dwarf2_per_objfile (objfile);
3786
3787 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3788 dw2_free_cached_file_names, NULL);
3789 }
3790
3791 /* Helper function for dw2_map_symtabs_matching_filename that expands
3792 the symtabs and calls the iterator. */
3793
3794 static int
3795 dw2_map_expand_apply (struct objfile *objfile,
3796 struct dwarf2_per_cu_data *per_cu,
3797 const char *name, const char *real_path,
3798 gdb::function_view<bool (symtab *)> callback)
3799 {
3800 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3801
3802 /* Don't visit already-expanded CUs. */
3803 if (per_cu->v.quick->compunit_symtab)
3804 return 0;
3805
3806 /* This may expand more than one symtab, and we want to iterate over
3807 all of them. */
3808 dw2_instantiate_symtab (per_cu);
3809
3810 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3811 last_made, callback);
3812 }
3813
3814 /* Implementation of the map_symtabs_matching_filename method. */
3815
3816 static bool
3817 dw2_map_symtabs_matching_filename
3818 (struct objfile *objfile, const char *name, const char *real_path,
3819 gdb::function_view<bool (symtab *)> callback)
3820 {
3821 const char *name_basename = lbasename (name);
3822 struct dwarf2_per_objfile *dwarf2_per_objfile
3823 = get_dwarf2_per_objfile (objfile);
3824
3825 /* The rule is CUs specify all the files, including those used by
3826 any TU, so there's no need to scan TUs here. */
3827
3828 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3829 {
3830 /* We only need to look at symtabs not already expanded. */
3831 if (per_cu->v.quick->compunit_symtab)
3832 continue;
3833
3834 quick_file_names *file_data = dw2_get_file_names (per_cu);
3835 if (file_data == NULL)
3836 continue;
3837
3838 for (int j = 0; j < file_data->num_file_names; ++j)
3839 {
3840 const char *this_name = file_data->file_names[j];
3841 const char *this_real_name;
3842
3843 if (compare_filenames_for_search (this_name, name))
3844 {
3845 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3846 callback))
3847 return true;
3848 continue;
3849 }
3850
3851 /* Before we invoke realpath, which can get expensive when many
3852 files are involved, do a quick comparison of the basenames. */
3853 if (! basenames_may_differ
3854 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3855 continue;
3856
3857 this_real_name = dw2_get_real_path (objfile, file_data, j);
3858 if (compare_filenames_for_search (this_real_name, name))
3859 {
3860 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3861 callback))
3862 return true;
3863 continue;
3864 }
3865
3866 if (real_path != NULL)
3867 {
3868 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3869 gdb_assert (IS_ABSOLUTE_PATH (name));
3870 if (this_real_name != NULL
3871 && FILENAME_CMP (real_path, this_real_name) == 0)
3872 {
3873 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3874 callback))
3875 return true;
3876 continue;
3877 }
3878 }
3879 }
3880 }
3881
3882 return false;
3883 }
3884
3885 /* Struct used to manage iterating over all CUs looking for a symbol. */
3886
3887 struct dw2_symtab_iterator
3888 {
3889 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3890 struct dwarf2_per_objfile *dwarf2_per_objfile;
3891 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3892 int want_specific_block;
3893 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3894 Unused if !WANT_SPECIFIC_BLOCK. */
3895 int block_index;
3896 /* The kind of symbol we're looking for. */
3897 domain_enum domain;
3898 /* The list of CUs from the index entry of the symbol,
3899 or NULL if not found. */
3900 offset_type *vec;
3901 /* The next element in VEC to look at. */
3902 int next;
3903 /* The number of elements in VEC, or zero if there is no match. */
3904 int length;
3905 /* Have we seen a global version of the symbol?
3906 If so we can ignore all further global instances.
3907 This is to work around gold/15646, inefficient gold-generated
3908 indices. */
3909 int global_seen;
3910 };
3911
3912 /* Initialize the index symtab iterator ITER.
3913 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3914 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3915
3916 static void
3917 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3918 struct dwarf2_per_objfile *dwarf2_per_objfile,
3919 int want_specific_block,
3920 int block_index,
3921 domain_enum domain,
3922 const char *name)
3923 {
3924 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3925 iter->want_specific_block = want_specific_block;
3926 iter->block_index = block_index;
3927 iter->domain = domain;
3928 iter->next = 0;
3929 iter->global_seen = 0;
3930
3931 mapped_index *index = dwarf2_per_objfile->index_table;
3932
3933 /* index is NULL if OBJF_READNOW. */
3934 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3935 iter->length = MAYBE_SWAP (*iter->vec);
3936 else
3937 {
3938 iter->vec = NULL;
3939 iter->length = 0;
3940 }
3941 }
3942
3943 /* Return the next matching CU or NULL if there are no more. */
3944
3945 static struct dwarf2_per_cu_data *
3946 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3947 {
3948 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3949
3950 for ( ; iter->next < iter->length; ++iter->next)
3951 {
3952 offset_type cu_index_and_attrs =
3953 MAYBE_SWAP (iter->vec[iter->next + 1]);
3954 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3955 int want_static = iter->block_index != GLOBAL_BLOCK;
3956 /* This value is only valid for index versions >= 7. */
3957 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3958 gdb_index_symbol_kind symbol_kind =
3959 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3960 /* Only check the symbol attributes if they're present.
3961 Indices prior to version 7 don't record them,
3962 and indices >= 7 may elide them for certain symbols
3963 (gold does this). */
3964 int attrs_valid =
3965 (dwarf2_per_objfile->index_table->version >= 7
3966 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3967
3968 /* Don't crash on bad data. */
3969 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3970 + dwarf2_per_objfile->n_type_units))
3971 {
3972 complaint (&symfile_complaints,
3973 _(".gdb_index entry has bad CU index"
3974 " [in module %s]"),
3975 objfile_name (dwarf2_per_objfile->objfile));
3976 continue;
3977 }
3978
3979 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3980
3981 /* Skip if already read in. */
3982 if (per_cu->v.quick->compunit_symtab)
3983 continue;
3984
3985 /* Check static vs global. */
3986 if (attrs_valid)
3987 {
3988 if (iter->want_specific_block
3989 && want_static != is_static)
3990 continue;
3991 /* Work around gold/15646. */
3992 if (!is_static && iter->global_seen)
3993 continue;
3994 if (!is_static)
3995 iter->global_seen = 1;
3996 }
3997
3998 /* Only check the symbol's kind if it has one. */
3999 if (attrs_valid)
4000 {
4001 switch (iter->domain)
4002 {
4003 case VAR_DOMAIN:
4004 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4005 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4006 /* Some types are also in VAR_DOMAIN. */
4007 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4008 continue;
4009 break;
4010 case STRUCT_DOMAIN:
4011 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4012 continue;
4013 break;
4014 case LABEL_DOMAIN:
4015 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4016 continue;
4017 break;
4018 default:
4019 break;
4020 }
4021 }
4022
4023 ++iter->next;
4024 return per_cu;
4025 }
4026
4027 return NULL;
4028 }
4029
4030 static struct compunit_symtab *
4031 dw2_lookup_symbol (struct objfile *objfile, int block_index,
4032 const char *name, domain_enum domain)
4033 {
4034 struct compunit_symtab *stab_best = NULL;
4035 struct dwarf2_per_objfile *dwarf2_per_objfile
4036 = get_dwarf2_per_objfile (objfile);
4037
4038 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4039
4040 struct dw2_symtab_iterator iter;
4041 struct dwarf2_per_cu_data *per_cu;
4042
4043 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
4044
4045 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4046 {
4047 struct symbol *sym, *with_opaque = NULL;
4048 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
4049 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4050 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4051
4052 sym = block_find_symbol (block, name, domain,
4053 block_find_non_opaque_type_preferred,
4054 &with_opaque);
4055
4056 /* Some caution must be observed with overloaded functions
4057 and methods, since the index will not contain any overload
4058 information (but NAME might contain it). */
4059
4060 if (sym != NULL
4061 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4062 return stab;
4063 if (with_opaque != NULL
4064 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4065 stab_best = stab;
4066
4067 /* Keep looking through other CUs. */
4068 }
4069
4070 return stab_best;
4071 }
4072
4073 static void
4074 dw2_print_stats (struct objfile *objfile)
4075 {
4076 struct dwarf2_per_objfile *dwarf2_per_objfile
4077 = get_dwarf2_per_objfile (objfile);
4078 int total = (dwarf2_per_objfile->all_comp_units.size ()
4079 + dwarf2_per_objfile->n_type_units);
4080 int count = 0;
4081
4082 for (int i = 0; i < total; ++i)
4083 {
4084 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4085
4086 if (!per_cu->v.quick->compunit_symtab)
4087 ++count;
4088 }
4089 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4090 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4091 }
4092
4093 /* This dumps minimal information about the index.
4094 It is called via "mt print objfiles".
4095 One use is to verify .gdb_index has been loaded by the
4096 gdb.dwarf2/gdb-index.exp testcase. */
4097
4098 static void
4099 dw2_dump (struct objfile *objfile)
4100 {
4101 struct dwarf2_per_objfile *dwarf2_per_objfile
4102 = get_dwarf2_per_objfile (objfile);
4103
4104 gdb_assert (dwarf2_per_objfile->using_index);
4105 printf_filtered (".gdb_index:");
4106 if (dwarf2_per_objfile->index_table != NULL)
4107 {
4108 printf_filtered (" version %d\n",
4109 dwarf2_per_objfile->index_table->version);
4110 }
4111 else
4112 printf_filtered (" faked for \"readnow\"\n");
4113 printf_filtered ("\n");
4114 }
4115
4116 static void
4117 dw2_relocate (struct objfile *objfile,
4118 const struct section_offsets *new_offsets,
4119 const struct section_offsets *delta)
4120 {
4121 /* There's nothing to relocate here. */
4122 }
4123
4124 static void
4125 dw2_expand_symtabs_for_function (struct objfile *objfile,
4126 const char *func_name)
4127 {
4128 struct dwarf2_per_objfile *dwarf2_per_objfile
4129 = get_dwarf2_per_objfile (objfile);
4130
4131 struct dw2_symtab_iterator iter;
4132 struct dwarf2_per_cu_data *per_cu;
4133
4134 /* Note: It doesn't matter what we pass for block_index here. */
4135 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4136 func_name);
4137
4138 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4139 dw2_instantiate_symtab (per_cu);
4140
4141 }
4142
4143 static void
4144 dw2_expand_all_symtabs (struct objfile *objfile)
4145 {
4146 struct dwarf2_per_objfile *dwarf2_per_objfile
4147 = get_dwarf2_per_objfile (objfile);
4148 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
4149 + dwarf2_per_objfile->n_type_units);
4150
4151 for (int i = 0; i < total_units; ++i)
4152 {
4153 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4154
4155 dw2_instantiate_symtab (per_cu);
4156 }
4157 }
4158
4159 static void
4160 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4161 const char *fullname)
4162 {
4163 struct dwarf2_per_objfile *dwarf2_per_objfile
4164 = get_dwarf2_per_objfile (objfile);
4165
4166 /* We don't need to consider type units here.
4167 This is only called for examining code, e.g. expand_line_sal.
4168 There can be an order of magnitude (or more) more type units
4169 than comp units, and we avoid them if we can. */
4170
4171 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4172 {
4173 /* We only need to look at symtabs not already expanded. */
4174 if (per_cu->v.quick->compunit_symtab)
4175 continue;
4176
4177 quick_file_names *file_data = dw2_get_file_names (per_cu);
4178 if (file_data == NULL)
4179 continue;
4180
4181 for (int j = 0; j < file_data->num_file_names; ++j)
4182 {
4183 const char *this_fullname = file_data->file_names[j];
4184
4185 if (filename_cmp (this_fullname, fullname) == 0)
4186 {
4187 dw2_instantiate_symtab (per_cu);
4188 break;
4189 }
4190 }
4191 }
4192 }
4193
4194 static void
4195 dw2_map_matching_symbols (struct objfile *objfile,
4196 const char * name, domain_enum domain,
4197 int global,
4198 int (*callback) (struct block *,
4199 struct symbol *, void *),
4200 void *data, symbol_name_match_type match,
4201 symbol_compare_ftype *ordered_compare)
4202 {
4203 /* Currently unimplemented; used for Ada. The function can be called if the
4204 current language is Ada for a non-Ada objfile using GNU index. As Ada
4205 does not look for non-Ada symbols this function should just return. */
4206 }
4207
4208 /* Symbol name matcher for .gdb_index names.
4209
4210 Symbol names in .gdb_index have a few particularities:
4211
4212 - There's no indication of which is the language of each symbol.
4213
4214 Since each language has its own symbol name matching algorithm,
4215 and we don't know which language is the right one, we must match
4216 each symbol against all languages. This would be a potential
4217 performance problem if it were not mitigated by the
4218 mapped_index::name_components lookup table, which significantly
4219 reduces the number of times we need to call into this matcher,
4220 making it a non-issue.
4221
4222 - Symbol names in the index have no overload (parameter)
4223 information. I.e., in C++, "foo(int)" and "foo(long)" both
4224 appear as "foo" in the index, for example.
4225
4226 This means that the lookup names passed to the symbol name
4227 matcher functions must have no parameter information either
4228 because (e.g.) symbol search name "foo" does not match
4229 lookup-name "foo(int)" [while swapping search name for lookup
4230 name would match].
4231 */
4232 class gdb_index_symbol_name_matcher
4233 {
4234 public:
4235 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4236 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4237
4238 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4239 Returns true if any matcher matches. */
4240 bool matches (const char *symbol_name);
4241
4242 private:
4243 /* A reference to the lookup name we're matching against. */
4244 const lookup_name_info &m_lookup_name;
4245
4246 /* A vector holding all the different symbol name matchers, for all
4247 languages. */
4248 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4249 };
4250
4251 gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4252 (const lookup_name_info &lookup_name)
4253 : m_lookup_name (lookup_name)
4254 {
4255 /* Prepare the vector of comparison functions upfront, to avoid
4256 doing the same work for each symbol. Care is taken to avoid
4257 matching with the same matcher more than once if/when multiple
4258 languages use the same matcher function. */
4259 auto &matchers = m_symbol_name_matcher_funcs;
4260 matchers.reserve (nr_languages);
4261
4262 matchers.push_back (default_symbol_name_matcher);
4263
4264 for (int i = 0; i < nr_languages; i++)
4265 {
4266 const language_defn *lang = language_def ((enum language) i);
4267 symbol_name_matcher_ftype *name_matcher
4268 = get_symbol_name_matcher (lang, m_lookup_name);
4269
4270 /* Don't insert the same comparison routine more than once.
4271 Note that we do this linear walk instead of a seemingly
4272 cheaper sorted insert, or use a std::set or something like
4273 that, because relative order of function addresses is not
4274 stable. This is not a problem in practice because the number
4275 of supported languages is low, and the cost here is tiny
4276 compared to the number of searches we'll do afterwards using
4277 this object. */
4278 if (name_matcher != default_symbol_name_matcher
4279 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4280 == matchers.end ()))
4281 matchers.push_back (name_matcher);
4282 }
4283 }
4284
4285 bool
4286 gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4287 {
4288 for (auto matches_name : m_symbol_name_matcher_funcs)
4289 if (matches_name (symbol_name, m_lookup_name, NULL))
4290 return true;
4291
4292 return false;
4293 }
4294
4295 /* Starting from a search name, return the string that finds the upper
4296 bound of all strings that start with SEARCH_NAME in a sorted name
4297 list. Returns the empty string to indicate that the upper bound is
4298 the end of the list. */
4299
4300 static std::string
4301 make_sort_after_prefix_name (const char *search_name)
4302 {
4303 /* When looking to complete "func", we find the upper bound of all
4304 symbols that start with "func" by looking for where we'd insert
4305 the closest string that would follow "func" in lexicographical
4306 order. Usually, that's "func"-with-last-character-incremented,
4307 i.e. "fund". Mind non-ASCII characters, though. Usually those
4308 will be UTF-8 multi-byte sequences, but we can't be certain.
4309 Especially mind the 0xff character, which is a valid character in
4310 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4311 rule out compilers allowing it in identifiers. Note that
4312 conveniently, strcmp/strcasecmp are specified to compare
4313 characters interpreted as unsigned char. So what we do is treat
4314 the whole string as a base 256 number composed of a sequence of
4315 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4316 to 0, and carries 1 to the following more-significant position.
4317 If the very first character in SEARCH_NAME ends up incremented
4318 and carries/overflows, then the upper bound is the end of the
4319 list. The string after the empty string is also the empty
4320 string.
4321
4322 Some examples of this operation:
4323
4324 SEARCH_NAME => "+1" RESULT
4325
4326 "abc" => "abd"
4327 "ab\xff" => "ac"
4328 "\xff" "a" "\xff" => "\xff" "b"
4329 "\xff" => ""
4330 "\xff\xff" => ""
4331 "" => ""
4332
4333 Then, with these symbols for example:
4334
4335 func
4336 func1
4337 fund
4338
4339 completing "func" looks for symbols between "func" and
4340 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4341 which finds "func" and "func1", but not "fund".
4342
4343 And with:
4344
4345 funcÿ (Latin1 'ÿ' [0xff])
4346 funcÿ1
4347 fund
4348
4349 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4350 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4351
4352 And with:
4353
4354 ÿÿ (Latin1 'ÿ' [0xff])
4355 ÿÿ1
4356
4357 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4358 the end of the list.
4359 */
4360 std::string after = search_name;
4361 while (!after.empty () && (unsigned char) after.back () == 0xff)
4362 after.pop_back ();
4363 if (!after.empty ())
4364 after.back () = (unsigned char) after.back () + 1;
4365 return after;
4366 }
4367
4368 /* See declaration. */
4369
4370 std::pair<std::vector<name_component>::const_iterator,
4371 std::vector<name_component>::const_iterator>
4372 mapped_index_base::find_name_components_bounds
4373 (const lookup_name_info &lookup_name_without_params) const
4374 {
4375 auto *name_cmp
4376 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4377
4378 const char *cplus
4379 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
4380
4381 /* Comparison function object for lower_bound that matches against a
4382 given symbol name. */
4383 auto lookup_compare_lower = [&] (const name_component &elem,
4384 const char *name)
4385 {
4386 const char *elem_qualified = this->symbol_name_at (elem.idx);
4387 const char *elem_name = elem_qualified + elem.name_offset;
4388 return name_cmp (elem_name, name) < 0;
4389 };
4390
4391 /* Comparison function object for upper_bound that matches against a
4392 given symbol name. */
4393 auto lookup_compare_upper = [&] (const char *name,
4394 const name_component &elem)
4395 {
4396 const char *elem_qualified = this->symbol_name_at (elem.idx);
4397 const char *elem_name = elem_qualified + elem.name_offset;
4398 return name_cmp (name, elem_name) < 0;
4399 };
4400
4401 auto begin = this->name_components.begin ();
4402 auto end = this->name_components.end ();
4403
4404 /* Find the lower bound. */
4405 auto lower = [&] ()
4406 {
4407 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
4408 return begin;
4409 else
4410 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4411 } ();
4412
4413 /* Find the upper bound. */
4414 auto upper = [&] ()
4415 {
4416 if (lookup_name_without_params.completion_mode ())
4417 {
4418 /* In completion mode, we want UPPER to point past all
4419 symbols names that have the same prefix. I.e., with
4420 these symbols, and completing "func":
4421
4422 function << lower bound
4423 function1
4424 other_function << upper bound
4425
4426 We find the upper bound by looking for the insertion
4427 point of "func"-with-last-character-incremented,
4428 i.e. "fund". */
4429 std::string after = make_sort_after_prefix_name (cplus);
4430 if (after.empty ())
4431 return end;
4432 return std::lower_bound (lower, end, after.c_str (),
4433 lookup_compare_lower);
4434 }
4435 else
4436 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4437 } ();
4438
4439 return {lower, upper};
4440 }
4441
4442 /* See declaration. */
4443
4444 void
4445 mapped_index_base::build_name_components ()
4446 {
4447 if (!this->name_components.empty ())
4448 return;
4449
4450 this->name_components_casing = case_sensitivity;
4451 auto *name_cmp
4452 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4453
4454 /* The code below only knows how to break apart components of C++
4455 symbol names (and other languages that use '::' as
4456 namespace/module separator). If we add support for wild matching
4457 to some language that uses some other operator (E.g., Ada, Go and
4458 D use '.'), then we'll need to try splitting the symbol name
4459 according to that language too. Note that Ada does support wild
4460 matching, but doesn't currently support .gdb_index. */
4461 auto count = this->symbol_name_count ();
4462 for (offset_type idx = 0; idx < count; idx++)
4463 {
4464 if (this->symbol_name_slot_invalid (idx))
4465 continue;
4466
4467 const char *name = this->symbol_name_at (idx);
4468
4469 /* Add each name component to the name component table. */
4470 unsigned int previous_len = 0;
4471 for (unsigned int current_len = cp_find_first_component (name);
4472 name[current_len] != '\0';
4473 current_len += cp_find_first_component (name + current_len))
4474 {
4475 gdb_assert (name[current_len] == ':');
4476 this->name_components.push_back ({previous_len, idx});
4477 /* Skip the '::'. */
4478 current_len += 2;
4479 previous_len = current_len;
4480 }
4481 this->name_components.push_back ({previous_len, idx});
4482 }
4483
4484 /* Sort name_components elements by name. */
4485 auto name_comp_compare = [&] (const name_component &left,
4486 const name_component &right)
4487 {
4488 const char *left_qualified = this->symbol_name_at (left.idx);
4489 const char *right_qualified = this->symbol_name_at (right.idx);
4490
4491 const char *left_name = left_qualified + left.name_offset;
4492 const char *right_name = right_qualified + right.name_offset;
4493
4494 return name_cmp (left_name, right_name) < 0;
4495 };
4496
4497 std::sort (this->name_components.begin (),
4498 this->name_components.end (),
4499 name_comp_compare);
4500 }
4501
4502 /* Helper for dw2_expand_symtabs_matching that works with a
4503 mapped_index_base instead of the containing objfile. This is split
4504 to a separate function in order to be able to unit test the
4505 name_components matching using a mock mapped_index_base. For each
4506 symbol name that matches, calls MATCH_CALLBACK, passing it the
4507 symbol's index in the mapped_index_base symbol table. */
4508
4509 static void
4510 dw2_expand_symtabs_matching_symbol
4511 (mapped_index_base &index,
4512 const lookup_name_info &lookup_name_in,
4513 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4514 enum search_domain kind,
4515 gdb::function_view<void (offset_type)> match_callback)
4516 {
4517 lookup_name_info lookup_name_without_params
4518 = lookup_name_in.make_ignore_params ();
4519 gdb_index_symbol_name_matcher lookup_name_matcher
4520 (lookup_name_without_params);
4521
4522 /* Build the symbol name component sorted vector, if we haven't
4523 yet. */
4524 index.build_name_components ();
4525
4526 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4527
4528 /* Now for each symbol name in range, check to see if we have a name
4529 match, and if so, call the MATCH_CALLBACK callback. */
4530
4531 /* The same symbol may appear more than once in the range though.
4532 E.g., if we're looking for symbols that complete "w", and we have
4533 a symbol named "w1::w2", we'll find the two name components for
4534 that same symbol in the range. To be sure we only call the
4535 callback once per symbol, we first collect the symbol name
4536 indexes that matched in a temporary vector and ignore
4537 duplicates. */
4538 std::vector<offset_type> matches;
4539 matches.reserve (std::distance (bounds.first, bounds.second));
4540
4541 for (; bounds.first != bounds.second; ++bounds.first)
4542 {
4543 const char *qualified = index.symbol_name_at (bounds.first->idx);
4544
4545 if (!lookup_name_matcher.matches (qualified)
4546 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4547 continue;
4548
4549 matches.push_back (bounds.first->idx);
4550 }
4551
4552 std::sort (matches.begin (), matches.end ());
4553
4554 /* Finally call the callback, once per match. */
4555 ULONGEST prev = -1;
4556 for (offset_type idx : matches)
4557 {
4558 if (prev != idx)
4559 {
4560 match_callback (idx);
4561 prev = idx;
4562 }
4563 }
4564
4565 /* Above we use a type wider than idx's for 'prev', since 0 and
4566 (offset_type)-1 are both possible values. */
4567 static_assert (sizeof (prev) > sizeof (offset_type), "");
4568 }
4569
4570 #if GDB_SELF_TEST
4571
4572 namespace selftests { namespace dw2_expand_symtabs_matching {
4573
4574 /* A mock .gdb_index/.debug_names-like name index table, enough to
4575 exercise dw2_expand_symtabs_matching_symbol, which works with the
4576 mapped_index_base interface. Builds an index from the symbol list
4577 passed as parameter to the constructor. */
4578 class mock_mapped_index : public mapped_index_base
4579 {
4580 public:
4581 mock_mapped_index (gdb::array_view<const char *> symbols)
4582 : m_symbol_table (symbols)
4583 {}
4584
4585 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4586
4587 /* Return the number of names in the symbol table. */
4588 virtual size_t symbol_name_count () const
4589 {
4590 return m_symbol_table.size ();
4591 }
4592
4593 /* Get the name of the symbol at IDX in the symbol table. */
4594 virtual const char *symbol_name_at (offset_type idx) const
4595 {
4596 return m_symbol_table[idx];
4597 }
4598
4599 private:
4600 gdb::array_view<const char *> m_symbol_table;
4601 };
4602
4603 /* Convenience function that converts a NULL pointer to a "<null>"
4604 string, to pass to print routines. */
4605
4606 static const char *
4607 string_or_null (const char *str)
4608 {
4609 return str != NULL ? str : "<null>";
4610 }
4611
4612 /* Check if a lookup_name_info built from
4613 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4614 index. EXPECTED_LIST is the list of expected matches, in expected
4615 matching order. If no match expected, then an empty list is
4616 specified. Returns true on success. On failure prints a warning
4617 indicating the file:line that failed, and returns false. */
4618
4619 static bool
4620 check_match (const char *file, int line,
4621 mock_mapped_index &mock_index,
4622 const char *name, symbol_name_match_type match_type,
4623 bool completion_mode,
4624 std::initializer_list<const char *> expected_list)
4625 {
4626 lookup_name_info lookup_name (name, match_type, completion_mode);
4627
4628 bool matched = true;
4629
4630 auto mismatch = [&] (const char *expected_str,
4631 const char *got)
4632 {
4633 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4634 "expected=\"%s\", got=\"%s\"\n"),
4635 file, line,
4636 (match_type == symbol_name_match_type::FULL
4637 ? "FULL" : "WILD"),
4638 name, string_or_null (expected_str), string_or_null (got));
4639 matched = false;
4640 };
4641
4642 auto expected_it = expected_list.begin ();
4643 auto expected_end = expected_list.end ();
4644
4645 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4646 NULL, ALL_DOMAIN,
4647 [&] (offset_type idx)
4648 {
4649 const char *matched_name = mock_index.symbol_name_at (idx);
4650 const char *expected_str
4651 = expected_it == expected_end ? NULL : *expected_it++;
4652
4653 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4654 mismatch (expected_str, matched_name);
4655 });
4656
4657 const char *expected_str
4658 = expected_it == expected_end ? NULL : *expected_it++;
4659 if (expected_str != NULL)
4660 mismatch (expected_str, NULL);
4661
4662 return matched;
4663 }
4664
4665 /* The symbols added to the mock mapped_index for testing (in
4666 canonical form). */
4667 static const char *test_symbols[] = {
4668 "function",
4669 "std::bar",
4670 "std::zfunction",
4671 "std::zfunction2",
4672 "w1::w2",
4673 "ns::foo<char*>",
4674 "ns::foo<int>",
4675 "ns::foo<long>",
4676 "ns2::tmpl<int>::foo2",
4677 "(anonymous namespace)::A::B::C",
4678
4679 /* These are used to check that the increment-last-char in the
4680 matching algorithm for completion doesn't match "t1_fund" when
4681 completing "t1_func". */
4682 "t1_func",
4683 "t1_func1",
4684 "t1_fund",
4685 "t1_fund1",
4686
4687 /* A UTF-8 name with multi-byte sequences to make sure that
4688 cp-name-parser understands this as a single identifier ("função"
4689 is "function" in PT). */
4690 u8"u8função",
4691
4692 /* \377 (0xff) is Latin1 'ÿ'. */
4693 "yfunc\377",
4694
4695 /* \377 (0xff) is Latin1 'ÿ'. */
4696 "\377",
4697 "\377\377123",
4698
4699 /* A name with all sorts of complications. Starts with "z" to make
4700 it easier for the completion tests below. */
4701 #define Z_SYM_NAME \
4702 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4703 "::tuple<(anonymous namespace)::ui*, " \
4704 "std::default_delete<(anonymous namespace)::ui>, void>"
4705
4706 Z_SYM_NAME
4707 };
4708
4709 /* Returns true if the mapped_index_base::find_name_component_bounds
4710 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4711 in completion mode. */
4712
4713 static bool
4714 check_find_bounds_finds (mapped_index_base &index,
4715 const char *search_name,
4716 gdb::array_view<const char *> expected_syms)
4717 {
4718 lookup_name_info lookup_name (search_name,
4719 symbol_name_match_type::FULL, true);
4720
4721 auto bounds = index.find_name_components_bounds (lookup_name);
4722
4723 size_t distance = std::distance (bounds.first, bounds.second);
4724 if (distance != expected_syms.size ())
4725 return false;
4726
4727 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4728 {
4729 auto nc_elem = bounds.first + exp_elem;
4730 const char *qualified = index.symbol_name_at (nc_elem->idx);
4731 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4732 return false;
4733 }
4734
4735 return true;
4736 }
4737
4738 /* Test the lower-level mapped_index::find_name_component_bounds
4739 method. */
4740
4741 static void
4742 test_mapped_index_find_name_component_bounds ()
4743 {
4744 mock_mapped_index mock_index (test_symbols);
4745
4746 mock_index.build_name_components ();
4747
4748 /* Test the lower-level mapped_index::find_name_component_bounds
4749 method in completion mode. */
4750 {
4751 static const char *expected_syms[] = {
4752 "t1_func",
4753 "t1_func1",
4754 };
4755
4756 SELF_CHECK (check_find_bounds_finds (mock_index,
4757 "t1_func", expected_syms));
4758 }
4759
4760 /* Check that the increment-last-char in the name matching algorithm
4761 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4762 {
4763 static const char *expected_syms1[] = {
4764 "\377",
4765 "\377\377123",
4766 };
4767 SELF_CHECK (check_find_bounds_finds (mock_index,
4768 "\377", expected_syms1));
4769
4770 static const char *expected_syms2[] = {
4771 "\377\377123",
4772 };
4773 SELF_CHECK (check_find_bounds_finds (mock_index,
4774 "\377\377", expected_syms2));
4775 }
4776 }
4777
4778 /* Test dw2_expand_symtabs_matching_symbol. */
4779
4780 static void
4781 test_dw2_expand_symtabs_matching_symbol ()
4782 {
4783 mock_mapped_index mock_index (test_symbols);
4784
4785 /* We let all tests run until the end even if some fails, for debug
4786 convenience. */
4787 bool any_mismatch = false;
4788
4789 /* Create the expected symbols list (an initializer_list). Needed
4790 because lists have commas, and we need to pass them to CHECK,
4791 which is a macro. */
4792 #define EXPECT(...) { __VA_ARGS__ }
4793
4794 /* Wrapper for check_match that passes down the current
4795 __FILE__/__LINE__. */
4796 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4797 any_mismatch |= !check_match (__FILE__, __LINE__, \
4798 mock_index, \
4799 NAME, MATCH_TYPE, COMPLETION_MODE, \
4800 EXPECTED_LIST)
4801
4802 /* Identity checks. */
4803 for (const char *sym : test_symbols)
4804 {
4805 /* Should be able to match all existing symbols. */
4806 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4807 EXPECT (sym));
4808
4809 /* Should be able to match all existing symbols with
4810 parameters. */
4811 std::string with_params = std::string (sym) + "(int)";
4812 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4813 EXPECT (sym));
4814
4815 /* Should be able to match all existing symbols with
4816 parameters and qualifiers. */
4817 with_params = std::string (sym) + " ( int ) const";
4818 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4819 EXPECT (sym));
4820
4821 /* This should really find sym, but cp-name-parser.y doesn't
4822 know about lvalue/rvalue qualifiers yet. */
4823 with_params = std::string (sym) + " ( int ) &&";
4824 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4825 {});
4826 }
4827
4828 /* Check that the name matching algorithm for completion doesn't get
4829 confused with Latin1 'ÿ' / 0xff. */
4830 {
4831 static const char str[] = "\377";
4832 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4833 EXPECT ("\377", "\377\377123"));
4834 }
4835
4836 /* Check that the increment-last-char in the matching algorithm for
4837 completion doesn't match "t1_fund" when completing "t1_func". */
4838 {
4839 static const char str[] = "t1_func";
4840 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4841 EXPECT ("t1_func", "t1_func1"));
4842 }
4843
4844 /* Check that completion mode works at each prefix of the expected
4845 symbol name. */
4846 {
4847 static const char str[] = "function(int)";
4848 size_t len = strlen (str);
4849 std::string lookup;
4850
4851 for (size_t i = 1; i < len; i++)
4852 {
4853 lookup.assign (str, i);
4854 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4855 EXPECT ("function"));
4856 }
4857 }
4858
4859 /* While "w" is a prefix of both components, the match function
4860 should still only be called once. */
4861 {
4862 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4863 EXPECT ("w1::w2"));
4864 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4865 EXPECT ("w1::w2"));
4866 }
4867
4868 /* Same, with a "complicated" symbol. */
4869 {
4870 static const char str[] = Z_SYM_NAME;
4871 size_t len = strlen (str);
4872 std::string lookup;
4873
4874 for (size_t i = 1; i < len; i++)
4875 {
4876 lookup.assign (str, i);
4877 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4878 EXPECT (Z_SYM_NAME));
4879 }
4880 }
4881
4882 /* In FULL mode, an incomplete symbol doesn't match. */
4883 {
4884 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4885 {});
4886 }
4887
4888 /* A complete symbol with parameters matches any overload, since the
4889 index has no overload info. */
4890 {
4891 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4892 EXPECT ("std::zfunction", "std::zfunction2"));
4893 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4894 EXPECT ("std::zfunction", "std::zfunction2"));
4895 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4896 EXPECT ("std::zfunction", "std::zfunction2"));
4897 }
4898
4899 /* Check that whitespace is ignored appropriately. A symbol with a
4900 template argument list. */
4901 {
4902 static const char expected[] = "ns::foo<int>";
4903 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4904 EXPECT (expected));
4905 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4906 EXPECT (expected));
4907 }
4908
4909 /* Check that whitespace is ignored appropriately. A symbol with a
4910 template argument list that includes a pointer. */
4911 {
4912 static const char expected[] = "ns::foo<char*>";
4913 /* Try both completion and non-completion modes. */
4914 static const bool completion_mode[2] = {false, true};
4915 for (size_t i = 0; i < 2; i++)
4916 {
4917 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4918 completion_mode[i], EXPECT (expected));
4919 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4920 completion_mode[i], EXPECT (expected));
4921
4922 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4923 completion_mode[i], EXPECT (expected));
4924 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4925 completion_mode[i], EXPECT (expected));
4926 }
4927 }
4928
4929 {
4930 /* Check method qualifiers are ignored. */
4931 static const char expected[] = "ns::foo<char*>";
4932 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4933 symbol_name_match_type::FULL, true, EXPECT (expected));
4934 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4935 symbol_name_match_type::FULL, true, EXPECT (expected));
4936 CHECK_MATCH ("foo < char * > ( int ) const",
4937 symbol_name_match_type::WILD, true, EXPECT (expected));
4938 CHECK_MATCH ("foo < char * > ( int ) &&",
4939 symbol_name_match_type::WILD, true, EXPECT (expected));
4940 }
4941
4942 /* Test lookup names that don't match anything. */
4943 {
4944 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4945 {});
4946
4947 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4948 {});
4949 }
4950
4951 /* Some wild matching tests, exercising "(anonymous namespace)",
4952 which should not be confused with a parameter list. */
4953 {
4954 static const char *syms[] = {
4955 "A::B::C",
4956 "B::C",
4957 "C",
4958 "A :: B :: C ( int )",
4959 "B :: C ( int )",
4960 "C ( int )",
4961 };
4962
4963 for (const char *s : syms)
4964 {
4965 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4966 EXPECT ("(anonymous namespace)::A::B::C"));
4967 }
4968 }
4969
4970 {
4971 static const char expected[] = "ns2::tmpl<int>::foo2";
4972 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4973 EXPECT (expected));
4974 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4975 EXPECT (expected));
4976 }
4977
4978 SELF_CHECK (!any_mismatch);
4979
4980 #undef EXPECT
4981 #undef CHECK_MATCH
4982 }
4983
4984 static void
4985 run_test ()
4986 {
4987 test_mapped_index_find_name_component_bounds ();
4988 test_dw2_expand_symtabs_matching_symbol ();
4989 }
4990
4991 }} // namespace selftests::dw2_expand_symtabs_matching
4992
4993 #endif /* GDB_SELF_TEST */
4994
4995 /* If FILE_MATCHER is NULL or if PER_CU has
4996 dwarf2_per_cu_quick_data::MARK set (see
4997 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4998 EXPANSION_NOTIFY on it. */
4999
5000 static void
5001 dw2_expand_symtabs_matching_one
5002 (struct dwarf2_per_cu_data *per_cu,
5003 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5004 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5005 {
5006 if (file_matcher == NULL || per_cu->v.quick->mark)
5007 {
5008 bool symtab_was_null
5009 = (per_cu->v.quick->compunit_symtab == NULL);
5010
5011 dw2_instantiate_symtab (per_cu);
5012
5013 if (expansion_notify != NULL
5014 && symtab_was_null
5015 && per_cu->v.quick->compunit_symtab != NULL)
5016 expansion_notify (per_cu->v.quick->compunit_symtab);
5017 }
5018 }
5019
5020 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5021 matched, to expand corresponding CUs that were marked. IDX is the
5022 index of the symbol name that matched. */
5023
5024 static void
5025 dw2_expand_marked_cus
5026 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5027 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5028 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5029 search_domain kind)
5030 {
5031 offset_type *vec, vec_len, vec_idx;
5032 bool global_seen = false;
5033 mapped_index &index = *dwarf2_per_objfile->index_table;
5034
5035 vec = (offset_type *) (index.constant_pool
5036 + MAYBE_SWAP (index.symbol_table[idx].vec));
5037 vec_len = MAYBE_SWAP (vec[0]);
5038 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5039 {
5040 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5041 /* This value is only valid for index versions >= 7. */
5042 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5043 gdb_index_symbol_kind symbol_kind =
5044 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5045 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5046 /* Only check the symbol attributes if they're present.
5047 Indices prior to version 7 don't record them,
5048 and indices >= 7 may elide them for certain symbols
5049 (gold does this). */
5050 int attrs_valid =
5051 (index.version >= 7
5052 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5053
5054 /* Work around gold/15646. */
5055 if (attrs_valid)
5056 {
5057 if (!is_static && global_seen)
5058 continue;
5059 if (!is_static)
5060 global_seen = true;
5061 }
5062
5063 /* Only check the symbol's kind if it has one. */
5064 if (attrs_valid)
5065 {
5066 switch (kind)
5067 {
5068 case VARIABLES_DOMAIN:
5069 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5070 continue;
5071 break;
5072 case FUNCTIONS_DOMAIN:
5073 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5074 continue;
5075 break;
5076 case TYPES_DOMAIN:
5077 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5078 continue;
5079 break;
5080 default:
5081 break;
5082 }
5083 }
5084
5085 /* Don't crash on bad data. */
5086 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
5087 + dwarf2_per_objfile->n_type_units))
5088 {
5089 complaint (&symfile_complaints,
5090 _(".gdb_index entry has bad CU index"
5091 " [in module %s]"),
5092 objfile_name (dwarf2_per_objfile->objfile));
5093 continue;
5094 }
5095
5096 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
5097 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5098 expansion_notify);
5099 }
5100 }
5101
5102 /* If FILE_MATCHER is non-NULL, set all the
5103 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5104 that match FILE_MATCHER. */
5105
5106 static void
5107 dw_expand_symtabs_matching_file_matcher
5108 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5109 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5110 {
5111 if (file_matcher == NULL)
5112 return;
5113
5114 objfile *const objfile = dwarf2_per_objfile->objfile;
5115
5116 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5117 htab_eq_pointer,
5118 NULL, xcalloc, xfree));
5119 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5120 htab_eq_pointer,
5121 NULL, xcalloc, xfree));
5122
5123 /* The rule is CUs specify all the files, including those used by
5124 any TU, so there's no need to scan TUs here. */
5125
5126 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5127 {
5128 QUIT;
5129
5130 per_cu->v.quick->mark = 0;
5131
5132 /* We only need to look at symtabs not already expanded. */
5133 if (per_cu->v.quick->compunit_symtab)
5134 continue;
5135
5136 quick_file_names *file_data = dw2_get_file_names (per_cu);
5137 if (file_data == NULL)
5138 continue;
5139
5140 if (htab_find (visited_not_found.get (), file_data) != NULL)
5141 continue;
5142 else if (htab_find (visited_found.get (), file_data) != NULL)
5143 {
5144 per_cu->v.quick->mark = 1;
5145 continue;
5146 }
5147
5148 for (int j = 0; j < file_data->num_file_names; ++j)
5149 {
5150 const char *this_real_name;
5151
5152 if (file_matcher (file_data->file_names[j], false))
5153 {
5154 per_cu->v.quick->mark = 1;
5155 break;
5156 }
5157
5158 /* Before we invoke realpath, which can get expensive when many
5159 files are involved, do a quick comparison of the basenames. */
5160 if (!basenames_may_differ
5161 && !file_matcher (lbasename (file_data->file_names[j]),
5162 true))
5163 continue;
5164
5165 this_real_name = dw2_get_real_path (objfile, file_data, j);
5166 if (file_matcher (this_real_name, false))
5167 {
5168 per_cu->v.quick->mark = 1;
5169 break;
5170 }
5171 }
5172
5173 void **slot = htab_find_slot (per_cu->v.quick->mark
5174 ? visited_found.get ()
5175 : visited_not_found.get (),
5176 file_data, INSERT);
5177 *slot = file_data;
5178 }
5179 }
5180
5181 static void
5182 dw2_expand_symtabs_matching
5183 (struct objfile *objfile,
5184 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5185 const lookup_name_info &lookup_name,
5186 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5187 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5188 enum search_domain kind)
5189 {
5190 struct dwarf2_per_objfile *dwarf2_per_objfile
5191 = get_dwarf2_per_objfile (objfile);
5192
5193 /* index_table is NULL if OBJF_READNOW. */
5194 if (!dwarf2_per_objfile->index_table)
5195 return;
5196
5197 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5198
5199 mapped_index &index = *dwarf2_per_objfile->index_table;
5200
5201 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5202 symbol_matcher,
5203 kind, [&] (offset_type idx)
5204 {
5205 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5206 expansion_notify, kind);
5207 });
5208 }
5209
5210 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5211 symtab. */
5212
5213 static struct compunit_symtab *
5214 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5215 CORE_ADDR pc)
5216 {
5217 int i;
5218
5219 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5220 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5221 return cust;
5222
5223 if (cust->includes == NULL)
5224 return NULL;
5225
5226 for (i = 0; cust->includes[i]; ++i)
5227 {
5228 struct compunit_symtab *s = cust->includes[i];
5229
5230 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5231 if (s != NULL)
5232 return s;
5233 }
5234
5235 return NULL;
5236 }
5237
5238 static struct compunit_symtab *
5239 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5240 struct bound_minimal_symbol msymbol,
5241 CORE_ADDR pc,
5242 struct obj_section *section,
5243 int warn_if_readin)
5244 {
5245 struct dwarf2_per_cu_data *data;
5246 struct compunit_symtab *result;
5247
5248 if (!objfile->psymtabs_addrmap)
5249 return NULL;
5250
5251 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5252 pc);
5253 if (!data)
5254 return NULL;
5255
5256 if (warn_if_readin && data->v.quick->compunit_symtab)
5257 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5258 paddress (get_objfile_arch (objfile), pc));
5259
5260 result
5261 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
5262 pc);
5263 gdb_assert (result != NULL);
5264 return result;
5265 }
5266
5267 static void
5268 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5269 void *data, int need_fullname)
5270 {
5271 struct dwarf2_per_objfile *dwarf2_per_objfile
5272 = get_dwarf2_per_objfile (objfile);
5273
5274 if (!dwarf2_per_objfile->filenames_cache)
5275 {
5276 dwarf2_per_objfile->filenames_cache.emplace ();
5277
5278 htab_up visited (htab_create_alloc (10,
5279 htab_hash_pointer, htab_eq_pointer,
5280 NULL, xcalloc, xfree));
5281
5282 /* The rule is CUs specify all the files, including those used
5283 by any TU, so there's no need to scan TUs here. We can
5284 ignore file names coming from already-expanded CUs. */
5285
5286 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5287 {
5288 if (per_cu->v.quick->compunit_symtab)
5289 {
5290 void **slot = htab_find_slot (visited.get (),
5291 per_cu->v.quick->file_names,
5292 INSERT);
5293
5294 *slot = per_cu->v.quick->file_names;
5295 }
5296 }
5297
5298 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5299 {
5300 /* We only need to look at symtabs not already expanded. */
5301 if (per_cu->v.quick->compunit_symtab)
5302 continue;
5303
5304 quick_file_names *file_data = dw2_get_file_names (per_cu);
5305 if (file_data == NULL)
5306 continue;
5307
5308 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5309 if (*slot)
5310 {
5311 /* Already visited. */
5312 continue;
5313 }
5314 *slot = file_data;
5315
5316 for (int j = 0; j < file_data->num_file_names; ++j)
5317 {
5318 const char *filename = file_data->file_names[j];
5319 dwarf2_per_objfile->filenames_cache->seen (filename);
5320 }
5321 }
5322 }
5323
5324 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5325 {
5326 gdb::unique_xmalloc_ptr<char> this_real_name;
5327
5328 if (need_fullname)
5329 this_real_name = gdb_realpath (filename);
5330 (*fun) (filename, this_real_name.get (), data);
5331 });
5332 }
5333
5334 static int
5335 dw2_has_symbols (struct objfile *objfile)
5336 {
5337 return 1;
5338 }
5339
5340 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5341 {
5342 dw2_has_symbols,
5343 dw2_find_last_source_symtab,
5344 dw2_forget_cached_source_info,
5345 dw2_map_symtabs_matching_filename,
5346 dw2_lookup_symbol,
5347 dw2_print_stats,
5348 dw2_dump,
5349 dw2_relocate,
5350 dw2_expand_symtabs_for_function,
5351 dw2_expand_all_symtabs,
5352 dw2_expand_symtabs_with_fullname,
5353 dw2_map_matching_symbols,
5354 dw2_expand_symtabs_matching,
5355 dw2_find_pc_sect_compunit_symtab,
5356 NULL,
5357 dw2_map_symbol_filenames
5358 };
5359
5360 /* DWARF-5 debug_names reader. */
5361
5362 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5363 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5364
5365 /* A helper function that reads the .debug_names section in SECTION
5366 and fills in MAP. FILENAME is the name of the file containing the
5367 section; it is used for error reporting.
5368
5369 Returns true if all went well, false otherwise. */
5370
5371 static bool
5372 read_debug_names_from_section (struct objfile *objfile,
5373 const char *filename,
5374 struct dwarf2_section_info *section,
5375 mapped_debug_names &map)
5376 {
5377 if (dwarf2_section_empty_p (section))
5378 return false;
5379
5380 /* Older elfutils strip versions could keep the section in the main
5381 executable while splitting it for the separate debug info file. */
5382 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5383 return false;
5384
5385 dwarf2_read_section (objfile, section);
5386
5387 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5388
5389 const gdb_byte *addr = section->buffer;
5390
5391 bfd *const abfd = get_section_bfd_owner (section);
5392
5393 unsigned int bytes_read;
5394 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5395 addr += bytes_read;
5396
5397 map.dwarf5_is_dwarf64 = bytes_read != 4;
5398 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5399 if (bytes_read + length != section->size)
5400 {
5401 /* There may be multiple per-CU indices. */
5402 warning (_("Section .debug_names in %s length %s does not match "
5403 "section length %s, ignoring .debug_names."),
5404 filename, plongest (bytes_read + length),
5405 pulongest (section->size));
5406 return false;
5407 }
5408
5409 /* The version number. */
5410 uint16_t version = read_2_bytes (abfd, addr);
5411 addr += 2;
5412 if (version != 5)
5413 {
5414 warning (_("Section .debug_names in %s has unsupported version %d, "
5415 "ignoring .debug_names."),
5416 filename, version);
5417 return false;
5418 }
5419
5420 /* Padding. */
5421 uint16_t padding = read_2_bytes (abfd, addr);
5422 addr += 2;
5423 if (padding != 0)
5424 {
5425 warning (_("Section .debug_names in %s has unsupported padding %d, "
5426 "ignoring .debug_names."),
5427 filename, padding);
5428 return false;
5429 }
5430
5431 /* comp_unit_count - The number of CUs in the CU list. */
5432 map.cu_count = read_4_bytes (abfd, addr);
5433 addr += 4;
5434
5435 /* local_type_unit_count - The number of TUs in the local TU
5436 list. */
5437 map.tu_count = read_4_bytes (abfd, addr);
5438 addr += 4;
5439
5440 /* foreign_type_unit_count - The number of TUs in the foreign TU
5441 list. */
5442 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5443 addr += 4;
5444 if (foreign_tu_count != 0)
5445 {
5446 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5447 "ignoring .debug_names."),
5448 filename, static_cast<unsigned long> (foreign_tu_count));
5449 return false;
5450 }
5451
5452 /* bucket_count - The number of hash buckets in the hash lookup
5453 table. */
5454 map.bucket_count = read_4_bytes (abfd, addr);
5455 addr += 4;
5456
5457 /* name_count - The number of unique names in the index. */
5458 map.name_count = read_4_bytes (abfd, addr);
5459 addr += 4;
5460
5461 /* abbrev_table_size - The size in bytes of the abbreviations
5462 table. */
5463 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5464 addr += 4;
5465
5466 /* augmentation_string_size - The size in bytes of the augmentation
5467 string. This value is rounded up to a multiple of 4. */
5468 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5469 addr += 4;
5470 map.augmentation_is_gdb = ((augmentation_string_size
5471 == sizeof (dwarf5_augmentation))
5472 && memcmp (addr, dwarf5_augmentation,
5473 sizeof (dwarf5_augmentation)) == 0);
5474 augmentation_string_size += (-augmentation_string_size) & 3;
5475 addr += augmentation_string_size;
5476
5477 /* List of CUs */
5478 map.cu_table_reordered = addr;
5479 addr += map.cu_count * map.offset_size;
5480
5481 /* List of Local TUs */
5482 map.tu_table_reordered = addr;
5483 addr += map.tu_count * map.offset_size;
5484
5485 /* Hash Lookup Table */
5486 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5487 addr += map.bucket_count * 4;
5488 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5489 addr += map.name_count * 4;
5490
5491 /* Name Table */
5492 map.name_table_string_offs_reordered = addr;
5493 addr += map.name_count * map.offset_size;
5494 map.name_table_entry_offs_reordered = addr;
5495 addr += map.name_count * map.offset_size;
5496
5497 const gdb_byte *abbrev_table_start = addr;
5498 for (;;)
5499 {
5500 unsigned int bytes_read;
5501 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5502 addr += bytes_read;
5503 if (index_num == 0)
5504 break;
5505
5506 const auto insertpair
5507 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5508 if (!insertpair.second)
5509 {
5510 warning (_("Section .debug_names in %s has duplicate index %s, "
5511 "ignoring .debug_names."),
5512 filename, pulongest (index_num));
5513 return false;
5514 }
5515 mapped_debug_names::index_val &indexval = insertpair.first->second;
5516 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5517 addr += bytes_read;
5518
5519 for (;;)
5520 {
5521 mapped_debug_names::index_val::attr attr;
5522 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5523 addr += bytes_read;
5524 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5525 addr += bytes_read;
5526 if (attr.form == DW_FORM_implicit_const)
5527 {
5528 attr.implicit_const = read_signed_leb128 (abfd, addr,
5529 &bytes_read);
5530 addr += bytes_read;
5531 }
5532 if (attr.dw_idx == 0 && attr.form == 0)
5533 break;
5534 indexval.attr_vec.push_back (std::move (attr));
5535 }
5536 }
5537 if (addr != abbrev_table_start + abbrev_table_size)
5538 {
5539 warning (_("Section .debug_names in %s has abbreviation_table "
5540 "of size %zu vs. written as %u, ignoring .debug_names."),
5541 filename, addr - abbrev_table_start, abbrev_table_size);
5542 return false;
5543 }
5544 map.entry_pool = addr;
5545
5546 return true;
5547 }
5548
5549 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5550 list. */
5551
5552 static void
5553 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5554 const mapped_debug_names &map,
5555 dwarf2_section_info &section,
5556 bool is_dwz)
5557 {
5558 sect_offset sect_off_prev;
5559 for (uint32_t i = 0; i <= map.cu_count; ++i)
5560 {
5561 sect_offset sect_off_next;
5562 if (i < map.cu_count)
5563 {
5564 sect_off_next
5565 = (sect_offset) (extract_unsigned_integer
5566 (map.cu_table_reordered + i * map.offset_size,
5567 map.offset_size,
5568 map.dwarf5_byte_order));
5569 }
5570 else
5571 sect_off_next = (sect_offset) section.size;
5572 if (i >= 1)
5573 {
5574 const ULONGEST length = sect_off_next - sect_off_prev;
5575 dwarf2_per_cu_data *per_cu
5576 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5577 sect_off_prev, length);
5578 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5579 }
5580 sect_off_prev = sect_off_next;
5581 }
5582 }
5583
5584 /* Read the CU list from the mapped index, and use it to create all
5585 the CU objects for this dwarf2_per_objfile. */
5586
5587 static void
5588 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5589 const mapped_debug_names &map,
5590 const mapped_debug_names &dwz_map)
5591 {
5592 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5593 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5594
5595 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5596 dwarf2_per_objfile->info,
5597 false /* is_dwz */);
5598
5599 if (dwz_map.cu_count == 0)
5600 return;
5601
5602 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5603 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5604 true /* is_dwz */);
5605 }
5606
5607 /* Read .debug_names. If everything went ok, initialize the "quick"
5608 elements of all the CUs and return true. Otherwise, return false. */
5609
5610 static bool
5611 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5612 {
5613 mapped_debug_names local_map (dwarf2_per_objfile);
5614 mapped_debug_names dwz_map (dwarf2_per_objfile);
5615 struct objfile *objfile = dwarf2_per_objfile->objfile;
5616
5617 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5618 &dwarf2_per_objfile->debug_names,
5619 local_map))
5620 return false;
5621
5622 /* Don't use the index if it's empty. */
5623 if (local_map.name_count == 0)
5624 return false;
5625
5626 /* If there is a .dwz file, read it so we can get its CU list as
5627 well. */
5628 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5629 if (dwz != NULL)
5630 {
5631 if (!read_debug_names_from_section (objfile,
5632 bfd_get_filename (dwz->dwz_bfd),
5633 &dwz->debug_names, dwz_map))
5634 {
5635 warning (_("could not read '.debug_names' section from %s; skipping"),
5636 bfd_get_filename (dwz->dwz_bfd));
5637 return false;
5638 }
5639 }
5640
5641 create_cus_from_debug_names (dwarf2_per_objfile, local_map, dwz_map);
5642
5643 if (local_map.tu_count != 0)
5644 {
5645 /* We can only handle a single .debug_types when we have an
5646 index. */
5647 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5648 return false;
5649
5650 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5651 dwarf2_per_objfile->types, 0);
5652
5653 create_signatured_type_table_from_debug_names
5654 (dwarf2_per_objfile, local_map, section, &dwarf2_per_objfile->abbrev);
5655 }
5656
5657 create_addrmap_from_aranges (dwarf2_per_objfile,
5658 &dwarf2_per_objfile->debug_aranges);
5659
5660 dwarf2_per_objfile->debug_names_table.reset
5661 (new mapped_debug_names (dwarf2_per_objfile));
5662 *dwarf2_per_objfile->debug_names_table = std::move (local_map);
5663 dwarf2_per_objfile->using_index = 1;
5664 dwarf2_per_objfile->quick_file_names_table =
5665 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5666
5667 return true;
5668 }
5669
5670 /* Type used to manage iterating over all CUs looking for a symbol for
5671 .debug_names. */
5672
5673 class dw2_debug_names_iterator
5674 {
5675 public:
5676 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
5677 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
5678 dw2_debug_names_iterator (const mapped_debug_names &map,
5679 bool want_specific_block,
5680 block_enum block_index, domain_enum domain,
5681 const char *name)
5682 : m_map (map), m_want_specific_block (want_specific_block),
5683 m_block_index (block_index), m_domain (domain),
5684 m_addr (find_vec_in_debug_names (map, name))
5685 {}
5686
5687 dw2_debug_names_iterator (const mapped_debug_names &map,
5688 search_domain search, uint32_t namei)
5689 : m_map (map),
5690 m_search (search),
5691 m_addr (find_vec_in_debug_names (map, namei))
5692 {}
5693
5694 /* Return the next matching CU or NULL if there are no more. */
5695 dwarf2_per_cu_data *next ();
5696
5697 private:
5698 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5699 const char *name);
5700 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5701 uint32_t namei);
5702
5703 /* The internalized form of .debug_names. */
5704 const mapped_debug_names &m_map;
5705
5706 /* If true, only look for symbols that match BLOCK_INDEX. */
5707 const bool m_want_specific_block = false;
5708
5709 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
5710 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
5711 value. */
5712 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
5713
5714 /* The kind of symbol we're looking for. */
5715 const domain_enum m_domain = UNDEF_DOMAIN;
5716 const search_domain m_search = ALL_DOMAIN;
5717
5718 /* The list of CUs from the index entry of the symbol, or NULL if
5719 not found. */
5720 const gdb_byte *m_addr;
5721 };
5722
5723 const char *
5724 mapped_debug_names::namei_to_name (uint32_t namei) const
5725 {
5726 const ULONGEST namei_string_offs
5727 = extract_unsigned_integer ((name_table_string_offs_reordered
5728 + namei * offset_size),
5729 offset_size,
5730 dwarf5_byte_order);
5731 return read_indirect_string_at_offset
5732 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5733 }
5734
5735 /* Find a slot in .debug_names for the object named NAME. If NAME is
5736 found, return pointer to its pool data. If NAME cannot be found,
5737 return NULL. */
5738
5739 const gdb_byte *
5740 dw2_debug_names_iterator::find_vec_in_debug_names
5741 (const mapped_debug_names &map, const char *name)
5742 {
5743 int (*cmp) (const char *, const char *);
5744
5745 if (current_language->la_language == language_cplus
5746 || current_language->la_language == language_fortran
5747 || current_language->la_language == language_d)
5748 {
5749 /* NAME is already canonical. Drop any qualifiers as
5750 .debug_names does not contain any. */
5751
5752 if (strchr (name, '(') != NULL)
5753 {
5754 gdb::unique_xmalloc_ptr<char> without_params
5755 = cp_remove_params (name);
5756
5757 if (without_params != NULL)
5758 {
5759 name = without_params.get();
5760 }
5761 }
5762 }
5763
5764 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5765
5766 const uint32_t full_hash = dwarf5_djb_hash (name);
5767 uint32_t namei
5768 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5769 (map.bucket_table_reordered
5770 + (full_hash % map.bucket_count)), 4,
5771 map.dwarf5_byte_order);
5772 if (namei == 0)
5773 return NULL;
5774 --namei;
5775 if (namei >= map.name_count)
5776 {
5777 complaint (&symfile_complaints,
5778 _("Wrong .debug_names with name index %u but name_count=%u "
5779 "[in module %s]"),
5780 namei, map.name_count,
5781 objfile_name (map.dwarf2_per_objfile->objfile));
5782 return NULL;
5783 }
5784
5785 for (;;)
5786 {
5787 const uint32_t namei_full_hash
5788 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5789 (map.hash_table_reordered + namei), 4,
5790 map.dwarf5_byte_order);
5791 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5792 return NULL;
5793
5794 if (full_hash == namei_full_hash)
5795 {
5796 const char *const namei_string = map.namei_to_name (namei);
5797
5798 #if 0 /* An expensive sanity check. */
5799 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5800 {
5801 complaint (&symfile_complaints,
5802 _("Wrong .debug_names hash for string at index %u "
5803 "[in module %s]"),
5804 namei, objfile_name (dwarf2_per_objfile->objfile));
5805 return NULL;
5806 }
5807 #endif
5808
5809 if (cmp (namei_string, name) == 0)
5810 {
5811 const ULONGEST namei_entry_offs
5812 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5813 + namei * map.offset_size),
5814 map.offset_size, map.dwarf5_byte_order);
5815 return map.entry_pool + namei_entry_offs;
5816 }
5817 }
5818
5819 ++namei;
5820 if (namei >= map.name_count)
5821 return NULL;
5822 }
5823 }
5824
5825 const gdb_byte *
5826 dw2_debug_names_iterator::find_vec_in_debug_names
5827 (const mapped_debug_names &map, uint32_t namei)
5828 {
5829 if (namei >= map.name_count)
5830 {
5831 complaint (&symfile_complaints,
5832 _("Wrong .debug_names with name index %u but name_count=%u "
5833 "[in module %s]"),
5834 namei, map.name_count,
5835 objfile_name (map.dwarf2_per_objfile->objfile));
5836 return NULL;
5837 }
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 /* See dw2_debug_names_iterator. */
5847
5848 dwarf2_per_cu_data *
5849 dw2_debug_names_iterator::next ()
5850 {
5851 if (m_addr == NULL)
5852 return NULL;
5853
5854 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5855 struct objfile *objfile = dwarf2_per_objfile->objfile;
5856 bfd *const abfd = objfile->obfd;
5857
5858 again:
5859
5860 unsigned int bytes_read;
5861 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5862 m_addr += bytes_read;
5863 if (abbrev == 0)
5864 return NULL;
5865
5866 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5867 if (indexval_it == m_map.abbrev_map.cend ())
5868 {
5869 complaint (&symfile_complaints,
5870 _("Wrong .debug_names undefined abbrev code %s "
5871 "[in module %s]"),
5872 pulongest (abbrev), objfile_name (objfile));
5873 return NULL;
5874 }
5875 const mapped_debug_names::index_val &indexval = indexval_it->second;
5876 bool have_is_static = false;
5877 bool is_static;
5878 dwarf2_per_cu_data *per_cu = NULL;
5879 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5880 {
5881 ULONGEST ull;
5882 switch (attr.form)
5883 {
5884 case DW_FORM_implicit_const:
5885 ull = attr.implicit_const;
5886 break;
5887 case DW_FORM_flag_present:
5888 ull = 1;
5889 break;
5890 case DW_FORM_udata:
5891 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5892 m_addr += bytes_read;
5893 break;
5894 default:
5895 complaint (&symfile_complaints,
5896 _("Unsupported .debug_names form %s [in module %s]"),
5897 dwarf_form_name (attr.form),
5898 objfile_name (objfile));
5899 return NULL;
5900 }
5901 switch (attr.dw_idx)
5902 {
5903 case DW_IDX_compile_unit:
5904 /* Don't crash on bad data. */
5905 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5906 {
5907 complaint (&symfile_complaints,
5908 _(".debug_names entry has bad CU index %s"
5909 " [in module %s]"),
5910 pulongest (ull),
5911 objfile_name (dwarf2_per_objfile->objfile));
5912 continue;
5913 }
5914 per_cu = dwarf2_per_objfile->get_cutu (ull);
5915 break;
5916 case DW_IDX_type_unit:
5917 /* Don't crash on bad data. */
5918 if (ull >= dwarf2_per_objfile->n_type_units)
5919 {
5920 complaint (&symfile_complaints,
5921 _(".debug_names entry has bad TU index %s"
5922 " [in module %s]"),
5923 pulongest (ull),
5924 objfile_name (dwarf2_per_objfile->objfile));
5925 continue;
5926 }
5927 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5928 break;
5929 case DW_IDX_GNU_internal:
5930 if (!m_map.augmentation_is_gdb)
5931 break;
5932 have_is_static = true;
5933 is_static = true;
5934 break;
5935 case DW_IDX_GNU_external:
5936 if (!m_map.augmentation_is_gdb)
5937 break;
5938 have_is_static = true;
5939 is_static = false;
5940 break;
5941 }
5942 }
5943
5944 /* Skip if already read in. */
5945 if (per_cu->v.quick->compunit_symtab)
5946 goto again;
5947
5948 /* Check static vs global. */
5949 if (have_is_static)
5950 {
5951 const bool want_static = m_block_index != GLOBAL_BLOCK;
5952 if (m_want_specific_block && want_static != is_static)
5953 goto again;
5954 }
5955
5956 /* Match dw2_symtab_iter_next, symbol_kind
5957 and debug_names::psymbol_tag. */
5958 switch (m_domain)
5959 {
5960 case VAR_DOMAIN:
5961 switch (indexval.dwarf_tag)
5962 {
5963 case DW_TAG_variable:
5964 case DW_TAG_subprogram:
5965 /* Some types are also in VAR_DOMAIN. */
5966 case DW_TAG_typedef:
5967 case DW_TAG_structure_type:
5968 break;
5969 default:
5970 goto again;
5971 }
5972 break;
5973 case STRUCT_DOMAIN:
5974 switch (indexval.dwarf_tag)
5975 {
5976 case DW_TAG_typedef:
5977 case DW_TAG_structure_type:
5978 break;
5979 default:
5980 goto again;
5981 }
5982 break;
5983 case LABEL_DOMAIN:
5984 switch (indexval.dwarf_tag)
5985 {
5986 case 0:
5987 case DW_TAG_variable:
5988 break;
5989 default:
5990 goto again;
5991 }
5992 break;
5993 default:
5994 break;
5995 }
5996
5997 /* Match dw2_expand_symtabs_matching, symbol_kind and
5998 debug_names::psymbol_tag. */
5999 switch (m_search)
6000 {
6001 case VARIABLES_DOMAIN:
6002 switch (indexval.dwarf_tag)
6003 {
6004 case DW_TAG_variable:
6005 break;
6006 default:
6007 goto again;
6008 }
6009 break;
6010 case FUNCTIONS_DOMAIN:
6011 switch (indexval.dwarf_tag)
6012 {
6013 case DW_TAG_subprogram:
6014 break;
6015 default:
6016 goto again;
6017 }
6018 break;
6019 case TYPES_DOMAIN:
6020 switch (indexval.dwarf_tag)
6021 {
6022 case DW_TAG_typedef:
6023 case DW_TAG_structure_type:
6024 break;
6025 default:
6026 goto again;
6027 }
6028 break;
6029 default:
6030 break;
6031 }
6032
6033 return per_cu;
6034 }
6035
6036 static struct compunit_symtab *
6037 dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6038 const char *name, domain_enum domain)
6039 {
6040 const block_enum block_index = static_cast<block_enum> (block_index_int);
6041 struct dwarf2_per_objfile *dwarf2_per_objfile
6042 = get_dwarf2_per_objfile (objfile);
6043
6044 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6045 if (!mapp)
6046 {
6047 /* index is NULL if OBJF_READNOW. */
6048 return NULL;
6049 }
6050 const auto &map = *mapp;
6051
6052 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6053 block_index, domain, name);
6054
6055 struct compunit_symtab *stab_best = NULL;
6056 struct dwarf2_per_cu_data *per_cu;
6057 while ((per_cu = iter.next ()) != NULL)
6058 {
6059 struct symbol *sym, *with_opaque = NULL;
6060 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
6061 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6062 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6063
6064 sym = block_find_symbol (block, name, domain,
6065 block_find_non_opaque_type_preferred,
6066 &with_opaque);
6067
6068 /* Some caution must be observed with overloaded functions and
6069 methods, since the index will not contain any overload
6070 information (but NAME might contain it). */
6071
6072 if (sym != NULL
6073 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6074 return stab;
6075 if (with_opaque != NULL
6076 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6077 stab_best = stab;
6078
6079 /* Keep looking through other CUs. */
6080 }
6081
6082 return stab_best;
6083 }
6084
6085 /* This dumps minimal information about .debug_names. It is called
6086 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6087 uses this to verify that .debug_names has been loaded. */
6088
6089 static void
6090 dw2_debug_names_dump (struct objfile *objfile)
6091 {
6092 struct dwarf2_per_objfile *dwarf2_per_objfile
6093 = get_dwarf2_per_objfile (objfile);
6094
6095 gdb_assert (dwarf2_per_objfile->using_index);
6096 printf_filtered (".debug_names:");
6097 if (dwarf2_per_objfile->debug_names_table)
6098 printf_filtered (" exists\n");
6099 else
6100 printf_filtered (" faked for \"readnow\"\n");
6101 printf_filtered ("\n");
6102 }
6103
6104 static void
6105 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6106 const char *func_name)
6107 {
6108 struct dwarf2_per_objfile *dwarf2_per_objfile
6109 = get_dwarf2_per_objfile (objfile);
6110
6111 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6112 if (dwarf2_per_objfile->debug_names_table)
6113 {
6114 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6115
6116 /* Note: It doesn't matter what we pass for block_index here. */
6117 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6118 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
6119
6120 struct dwarf2_per_cu_data *per_cu;
6121 while ((per_cu = iter.next ()) != NULL)
6122 dw2_instantiate_symtab (per_cu);
6123 }
6124 }
6125
6126 static void
6127 dw2_debug_names_expand_symtabs_matching
6128 (struct objfile *objfile,
6129 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6130 const lookup_name_info &lookup_name,
6131 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6132 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6133 enum search_domain kind)
6134 {
6135 struct dwarf2_per_objfile *dwarf2_per_objfile
6136 = get_dwarf2_per_objfile (objfile);
6137
6138 /* debug_names_table is NULL if OBJF_READNOW. */
6139 if (!dwarf2_per_objfile->debug_names_table)
6140 return;
6141
6142 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6143
6144 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6145
6146 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6147 symbol_matcher,
6148 kind, [&] (offset_type namei)
6149 {
6150 /* The name was matched, now expand corresponding CUs that were
6151 marked. */
6152 dw2_debug_names_iterator iter (map, kind, namei);
6153
6154 struct dwarf2_per_cu_data *per_cu;
6155 while ((per_cu = iter.next ()) != NULL)
6156 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6157 expansion_notify);
6158 });
6159 }
6160
6161 const struct quick_symbol_functions dwarf2_debug_names_functions =
6162 {
6163 dw2_has_symbols,
6164 dw2_find_last_source_symtab,
6165 dw2_forget_cached_source_info,
6166 dw2_map_symtabs_matching_filename,
6167 dw2_debug_names_lookup_symbol,
6168 dw2_print_stats,
6169 dw2_debug_names_dump,
6170 dw2_relocate,
6171 dw2_debug_names_expand_symtabs_for_function,
6172 dw2_expand_all_symtabs,
6173 dw2_expand_symtabs_with_fullname,
6174 dw2_map_matching_symbols,
6175 dw2_debug_names_expand_symtabs_matching,
6176 dw2_find_pc_sect_compunit_symtab,
6177 NULL,
6178 dw2_map_symbol_filenames
6179 };
6180
6181 /* See symfile.h. */
6182
6183 bool
6184 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6185 {
6186 struct dwarf2_per_objfile *dwarf2_per_objfile
6187 = get_dwarf2_per_objfile (objfile);
6188
6189 /* If we're about to read full symbols, don't bother with the
6190 indices. In this case we also don't care if some other debug
6191 format is making psymtabs, because they are all about to be
6192 expanded anyway. */
6193 if ((objfile->flags & OBJF_READNOW))
6194 {
6195 dwarf2_per_objfile->using_index = 1;
6196 create_all_comp_units (dwarf2_per_objfile);
6197 create_all_type_units (dwarf2_per_objfile);
6198 dwarf2_per_objfile->quick_file_names_table
6199 = create_quick_file_names_table
6200 (dwarf2_per_objfile->all_comp_units.size ());
6201
6202 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6203 + dwarf2_per_objfile->n_type_units); ++i)
6204 {
6205 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6206
6207 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6208 struct dwarf2_per_cu_quick_data);
6209 }
6210
6211 /* Return 1 so that gdb sees the "quick" functions. However,
6212 these functions will be no-ops because we will have expanded
6213 all symtabs. */
6214 *index_kind = dw_index_kind::GDB_INDEX;
6215 return true;
6216 }
6217
6218 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6219 {
6220 *index_kind = dw_index_kind::DEBUG_NAMES;
6221 return true;
6222 }
6223
6224 if (dwarf2_read_index (dwarf2_per_objfile))
6225 {
6226 *index_kind = dw_index_kind::GDB_INDEX;
6227 return true;
6228 }
6229
6230 return false;
6231 }
6232
6233 \f
6234
6235 /* Build a partial symbol table. */
6236
6237 void
6238 dwarf2_build_psymtabs (struct objfile *objfile)
6239 {
6240 struct dwarf2_per_objfile *dwarf2_per_objfile
6241 = get_dwarf2_per_objfile (objfile);
6242
6243 if (objfile->global_psymbols.capacity () == 0
6244 && objfile->static_psymbols.capacity () == 0)
6245 init_psymbol_list (objfile, 1024);
6246
6247 TRY
6248 {
6249 /* This isn't really ideal: all the data we allocate on the
6250 objfile's obstack is still uselessly kept around. However,
6251 freeing it seems unsafe. */
6252 psymtab_discarder psymtabs (objfile);
6253 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6254 psymtabs.keep ();
6255 }
6256 CATCH (except, RETURN_MASK_ERROR)
6257 {
6258 exception_print (gdb_stderr, except);
6259 }
6260 END_CATCH
6261 }
6262
6263 /* Return the total length of the CU described by HEADER. */
6264
6265 static unsigned int
6266 get_cu_length (const struct comp_unit_head *header)
6267 {
6268 return header->initial_length_size + header->length;
6269 }
6270
6271 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6272
6273 static inline bool
6274 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6275 {
6276 sect_offset bottom = cu_header->sect_off;
6277 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6278
6279 return sect_off >= bottom && sect_off < top;
6280 }
6281
6282 /* Find the base address of the compilation unit for range lists and
6283 location lists. It will normally be specified by DW_AT_low_pc.
6284 In DWARF-3 draft 4, the base address could be overridden by
6285 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6286 compilation units with discontinuous ranges. */
6287
6288 static void
6289 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6290 {
6291 struct attribute *attr;
6292
6293 cu->base_known = 0;
6294 cu->base_address = 0;
6295
6296 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6297 if (attr)
6298 {
6299 cu->base_address = attr_value_as_address (attr);
6300 cu->base_known = 1;
6301 }
6302 else
6303 {
6304 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6305 if (attr)
6306 {
6307 cu->base_address = attr_value_as_address (attr);
6308 cu->base_known = 1;
6309 }
6310 }
6311 }
6312
6313 /* Read in the comp unit header information from the debug_info at info_ptr.
6314 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6315 NOTE: This leaves members offset, first_die_offset to be filled in
6316 by the caller. */
6317
6318 static const gdb_byte *
6319 read_comp_unit_head (struct comp_unit_head *cu_header,
6320 const gdb_byte *info_ptr,
6321 struct dwarf2_section_info *section,
6322 rcuh_kind section_kind)
6323 {
6324 int signed_addr;
6325 unsigned int bytes_read;
6326 const char *filename = get_section_file_name (section);
6327 bfd *abfd = get_section_bfd_owner (section);
6328
6329 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6330 cu_header->initial_length_size = bytes_read;
6331 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6332 info_ptr += bytes_read;
6333 cu_header->version = read_2_bytes (abfd, info_ptr);
6334 info_ptr += 2;
6335 if (cu_header->version < 5)
6336 switch (section_kind)
6337 {
6338 case rcuh_kind::COMPILE:
6339 cu_header->unit_type = DW_UT_compile;
6340 break;
6341 case rcuh_kind::TYPE:
6342 cu_header->unit_type = DW_UT_type;
6343 break;
6344 default:
6345 internal_error (__FILE__, __LINE__,
6346 _("read_comp_unit_head: invalid section_kind"));
6347 }
6348 else
6349 {
6350 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6351 (read_1_byte (abfd, info_ptr));
6352 info_ptr += 1;
6353 switch (cu_header->unit_type)
6354 {
6355 case DW_UT_compile:
6356 if (section_kind != rcuh_kind::COMPILE)
6357 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6358 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6359 filename);
6360 break;
6361 case DW_UT_type:
6362 section_kind = rcuh_kind::TYPE;
6363 break;
6364 default:
6365 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6366 "(is %d, should be %d or %d) [in module %s]"),
6367 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6368 }
6369
6370 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6371 info_ptr += 1;
6372 }
6373 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6374 cu_header,
6375 &bytes_read);
6376 info_ptr += bytes_read;
6377 if (cu_header->version < 5)
6378 {
6379 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6380 info_ptr += 1;
6381 }
6382 signed_addr = bfd_get_sign_extend_vma (abfd);
6383 if (signed_addr < 0)
6384 internal_error (__FILE__, __LINE__,
6385 _("read_comp_unit_head: dwarf from non elf file"));
6386 cu_header->signed_addr_p = signed_addr;
6387
6388 if (section_kind == rcuh_kind::TYPE)
6389 {
6390 LONGEST type_offset;
6391
6392 cu_header->signature = read_8_bytes (abfd, info_ptr);
6393 info_ptr += 8;
6394
6395 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6396 info_ptr += bytes_read;
6397 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6398 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6399 error (_("Dwarf Error: Too big type_offset in compilation unit "
6400 "header (is %s) [in module %s]"), plongest (type_offset),
6401 filename);
6402 }
6403
6404 return info_ptr;
6405 }
6406
6407 /* Helper function that returns the proper abbrev section for
6408 THIS_CU. */
6409
6410 static struct dwarf2_section_info *
6411 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6412 {
6413 struct dwarf2_section_info *abbrev;
6414 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6415
6416 if (this_cu->is_dwz)
6417 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6418 else
6419 abbrev = &dwarf2_per_objfile->abbrev;
6420
6421 return abbrev;
6422 }
6423
6424 /* Subroutine of read_and_check_comp_unit_head and
6425 read_and_check_type_unit_head to simplify them.
6426 Perform various error checking on the header. */
6427
6428 static void
6429 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6430 struct comp_unit_head *header,
6431 struct dwarf2_section_info *section,
6432 struct dwarf2_section_info *abbrev_section)
6433 {
6434 const char *filename = get_section_file_name (section);
6435
6436 if (header->version < 2 || header->version > 5)
6437 error (_("Dwarf Error: wrong version in compilation unit header "
6438 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
6439 filename);
6440
6441 if (to_underlying (header->abbrev_sect_off)
6442 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6443 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6444 "(offset %s + 6) [in module %s]"),
6445 sect_offset_str (header->abbrev_sect_off),
6446 sect_offset_str (header->sect_off),
6447 filename);
6448
6449 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6450 avoid potential 32-bit overflow. */
6451 if (((ULONGEST) header->sect_off + get_cu_length (header))
6452 > section->size)
6453 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6454 "(offset %s + 0) [in module %s]"),
6455 header->length, sect_offset_str (header->sect_off),
6456 filename);
6457 }
6458
6459 /* Read in a CU/TU header and perform some basic error checking.
6460 The contents of the header are stored in HEADER.
6461 The result is a pointer to the start of the first DIE. */
6462
6463 static const gdb_byte *
6464 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6465 struct comp_unit_head *header,
6466 struct dwarf2_section_info *section,
6467 struct dwarf2_section_info *abbrev_section,
6468 const gdb_byte *info_ptr,
6469 rcuh_kind section_kind)
6470 {
6471 const gdb_byte *beg_of_comp_unit = info_ptr;
6472
6473 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6474
6475 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6476
6477 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6478
6479 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6480 abbrev_section);
6481
6482 return info_ptr;
6483 }
6484
6485 /* Fetch the abbreviation table offset from a comp or type unit header. */
6486
6487 static sect_offset
6488 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6489 struct dwarf2_section_info *section,
6490 sect_offset sect_off)
6491 {
6492 bfd *abfd = get_section_bfd_owner (section);
6493 const gdb_byte *info_ptr;
6494 unsigned int initial_length_size, offset_size;
6495 uint16_t version;
6496
6497 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6498 info_ptr = section->buffer + to_underlying (sect_off);
6499 read_initial_length (abfd, info_ptr, &initial_length_size);
6500 offset_size = initial_length_size == 4 ? 4 : 8;
6501 info_ptr += initial_length_size;
6502
6503 version = read_2_bytes (abfd, info_ptr);
6504 info_ptr += 2;
6505 if (version >= 5)
6506 {
6507 /* Skip unit type and address size. */
6508 info_ptr += 2;
6509 }
6510
6511 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6512 }
6513
6514 /* Allocate a new partial symtab for file named NAME and mark this new
6515 partial symtab as being an include of PST. */
6516
6517 static void
6518 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6519 struct objfile *objfile)
6520 {
6521 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6522
6523 if (!IS_ABSOLUTE_PATH (subpst->filename))
6524 {
6525 /* It shares objfile->objfile_obstack. */
6526 subpst->dirname = pst->dirname;
6527 }
6528
6529 subpst->textlow = 0;
6530 subpst->texthigh = 0;
6531
6532 subpst->dependencies
6533 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
6534 subpst->dependencies[0] = pst;
6535 subpst->number_of_dependencies = 1;
6536
6537 subpst->globals_offset = 0;
6538 subpst->n_global_syms = 0;
6539 subpst->statics_offset = 0;
6540 subpst->n_static_syms = 0;
6541 subpst->compunit_symtab = NULL;
6542 subpst->read_symtab = pst->read_symtab;
6543 subpst->readin = 0;
6544
6545 /* No private part is necessary for include psymtabs. This property
6546 can be used to differentiate between such include psymtabs and
6547 the regular ones. */
6548 subpst->read_symtab_private = NULL;
6549 }
6550
6551 /* Read the Line Number Program data and extract the list of files
6552 included by the source file represented by PST. Build an include
6553 partial symtab for each of these included files. */
6554
6555 static void
6556 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6557 struct die_info *die,
6558 struct partial_symtab *pst)
6559 {
6560 line_header_up lh;
6561 struct attribute *attr;
6562
6563 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6564 if (attr)
6565 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6566 if (lh == NULL)
6567 return; /* No linetable, so no includes. */
6568
6569 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
6570 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
6571 }
6572
6573 static hashval_t
6574 hash_signatured_type (const void *item)
6575 {
6576 const struct signatured_type *sig_type
6577 = (const struct signatured_type *) item;
6578
6579 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6580 return sig_type->signature;
6581 }
6582
6583 static int
6584 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6585 {
6586 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6587 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6588
6589 return lhs->signature == rhs->signature;
6590 }
6591
6592 /* Allocate a hash table for signatured types. */
6593
6594 static htab_t
6595 allocate_signatured_type_table (struct objfile *objfile)
6596 {
6597 return htab_create_alloc_ex (41,
6598 hash_signatured_type,
6599 eq_signatured_type,
6600 NULL,
6601 &objfile->objfile_obstack,
6602 hashtab_obstack_allocate,
6603 dummy_obstack_deallocate);
6604 }
6605
6606 /* A helper function to add a signatured type CU to a table. */
6607
6608 static int
6609 add_signatured_type_cu_to_table (void **slot, void *datum)
6610 {
6611 struct signatured_type *sigt = (struct signatured_type *) *slot;
6612 struct signatured_type ***datap = (struct signatured_type ***) datum;
6613
6614 **datap = sigt;
6615 ++*datap;
6616
6617 return 1;
6618 }
6619
6620 /* A helper for create_debug_types_hash_table. Read types from SECTION
6621 and fill them into TYPES_HTAB. It will process only type units,
6622 therefore DW_UT_type. */
6623
6624 static void
6625 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6626 struct dwo_file *dwo_file,
6627 dwarf2_section_info *section, htab_t &types_htab,
6628 rcuh_kind section_kind)
6629 {
6630 struct objfile *objfile = dwarf2_per_objfile->objfile;
6631 struct dwarf2_section_info *abbrev_section;
6632 bfd *abfd;
6633 const gdb_byte *info_ptr, *end_ptr;
6634
6635 abbrev_section = (dwo_file != NULL
6636 ? &dwo_file->sections.abbrev
6637 : &dwarf2_per_objfile->abbrev);
6638
6639 if (dwarf_read_debug)
6640 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6641 get_section_name (section),
6642 get_section_file_name (abbrev_section));
6643
6644 dwarf2_read_section (objfile, section);
6645 info_ptr = section->buffer;
6646
6647 if (info_ptr == NULL)
6648 return;
6649
6650 /* We can't set abfd until now because the section may be empty or
6651 not present, in which case the bfd is unknown. */
6652 abfd = get_section_bfd_owner (section);
6653
6654 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6655 because we don't need to read any dies: the signature is in the
6656 header. */
6657
6658 end_ptr = info_ptr + section->size;
6659 while (info_ptr < end_ptr)
6660 {
6661 struct signatured_type *sig_type;
6662 struct dwo_unit *dwo_tu;
6663 void **slot;
6664 const gdb_byte *ptr = info_ptr;
6665 struct comp_unit_head header;
6666 unsigned int length;
6667
6668 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6669
6670 /* Initialize it due to a false compiler warning. */
6671 header.signature = -1;
6672 header.type_cu_offset_in_tu = (cu_offset) -1;
6673
6674 /* We need to read the type's signature in order to build the hash
6675 table, but we don't need anything else just yet. */
6676
6677 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6678 abbrev_section, ptr, section_kind);
6679
6680 length = get_cu_length (&header);
6681
6682 /* Skip dummy type units. */
6683 if (ptr >= info_ptr + length
6684 || peek_abbrev_code (abfd, ptr) == 0
6685 || header.unit_type != DW_UT_type)
6686 {
6687 info_ptr += length;
6688 continue;
6689 }
6690
6691 if (types_htab == NULL)
6692 {
6693 if (dwo_file)
6694 types_htab = allocate_dwo_unit_table (objfile);
6695 else
6696 types_htab = allocate_signatured_type_table (objfile);
6697 }
6698
6699 if (dwo_file)
6700 {
6701 sig_type = NULL;
6702 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6703 struct dwo_unit);
6704 dwo_tu->dwo_file = dwo_file;
6705 dwo_tu->signature = header.signature;
6706 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6707 dwo_tu->section = section;
6708 dwo_tu->sect_off = sect_off;
6709 dwo_tu->length = length;
6710 }
6711 else
6712 {
6713 /* N.B.: type_offset is not usable if this type uses a DWO file.
6714 The real type_offset is in the DWO file. */
6715 dwo_tu = NULL;
6716 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6717 struct signatured_type);
6718 sig_type->signature = header.signature;
6719 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6720 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6721 sig_type->per_cu.is_debug_types = 1;
6722 sig_type->per_cu.section = section;
6723 sig_type->per_cu.sect_off = sect_off;
6724 sig_type->per_cu.length = length;
6725 }
6726
6727 slot = htab_find_slot (types_htab,
6728 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6729 INSERT);
6730 gdb_assert (slot != NULL);
6731 if (*slot != NULL)
6732 {
6733 sect_offset dup_sect_off;
6734
6735 if (dwo_file)
6736 {
6737 const struct dwo_unit *dup_tu
6738 = (const struct dwo_unit *) *slot;
6739
6740 dup_sect_off = dup_tu->sect_off;
6741 }
6742 else
6743 {
6744 const struct signatured_type *dup_tu
6745 = (const struct signatured_type *) *slot;
6746
6747 dup_sect_off = dup_tu->per_cu.sect_off;
6748 }
6749
6750 complaint (&symfile_complaints,
6751 _("debug type entry at offset %s is duplicate to"
6752 " the entry at offset %s, signature %s"),
6753 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6754 hex_string (header.signature));
6755 }
6756 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6757
6758 if (dwarf_read_debug > 1)
6759 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6760 sect_offset_str (sect_off),
6761 hex_string (header.signature));
6762
6763 info_ptr += length;
6764 }
6765 }
6766
6767 /* Create the hash table of all entries in the .debug_types
6768 (or .debug_types.dwo) section(s).
6769 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6770 otherwise it is NULL.
6771
6772 The result is a pointer to the hash table or NULL if there are no types.
6773
6774 Note: This function processes DWO files only, not DWP files. */
6775
6776 static void
6777 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6778 struct dwo_file *dwo_file,
6779 VEC (dwarf2_section_info_def) *types,
6780 htab_t &types_htab)
6781 {
6782 int ix;
6783 struct dwarf2_section_info *section;
6784
6785 if (VEC_empty (dwarf2_section_info_def, types))
6786 return;
6787
6788 for (ix = 0;
6789 VEC_iterate (dwarf2_section_info_def, types, ix, section);
6790 ++ix)
6791 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
6792 types_htab, rcuh_kind::TYPE);
6793 }
6794
6795 /* Create the hash table of all entries in the .debug_types section,
6796 and initialize all_type_units.
6797 The result is zero if there is an error (e.g. missing .debug_types section),
6798 otherwise non-zero. */
6799
6800 static int
6801 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6802 {
6803 htab_t types_htab = NULL;
6804 struct signatured_type **iter;
6805
6806 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6807 &dwarf2_per_objfile->info, types_htab,
6808 rcuh_kind::COMPILE);
6809 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6810 dwarf2_per_objfile->types, types_htab);
6811 if (types_htab == NULL)
6812 {
6813 dwarf2_per_objfile->signatured_types = NULL;
6814 return 0;
6815 }
6816
6817 dwarf2_per_objfile->signatured_types = types_htab;
6818
6819 dwarf2_per_objfile->n_type_units
6820 = dwarf2_per_objfile->n_allocated_type_units
6821 = htab_elements (types_htab);
6822 dwarf2_per_objfile->all_type_units =
6823 XNEWVEC (struct signatured_type *, dwarf2_per_objfile->n_type_units);
6824 iter = &dwarf2_per_objfile->all_type_units[0];
6825 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
6826 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
6827 == dwarf2_per_objfile->n_type_units);
6828
6829 return 1;
6830 }
6831
6832 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6833 If SLOT is non-NULL, it is the entry to use in the hash table.
6834 Otherwise we find one. */
6835
6836 static struct signatured_type *
6837 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6838 void **slot)
6839 {
6840 struct objfile *objfile = dwarf2_per_objfile->objfile;
6841 int n_type_units = dwarf2_per_objfile->n_type_units;
6842 struct signatured_type *sig_type;
6843
6844 gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
6845 ++n_type_units;
6846 if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
6847 {
6848 if (dwarf2_per_objfile->n_allocated_type_units == 0)
6849 dwarf2_per_objfile->n_allocated_type_units = 1;
6850 dwarf2_per_objfile->n_allocated_type_units *= 2;
6851 dwarf2_per_objfile->all_type_units
6852 = XRESIZEVEC (struct signatured_type *,
6853 dwarf2_per_objfile->all_type_units,
6854 dwarf2_per_objfile->n_allocated_type_units);
6855 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6856 }
6857 dwarf2_per_objfile->n_type_units = n_type_units;
6858
6859 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6860 struct signatured_type);
6861 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
6862 sig_type->signature = sig;
6863 sig_type->per_cu.is_debug_types = 1;
6864 if (dwarf2_per_objfile->using_index)
6865 {
6866 sig_type->per_cu.v.quick =
6867 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6868 struct dwarf2_per_cu_quick_data);
6869 }
6870
6871 if (slot == NULL)
6872 {
6873 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6874 sig_type, INSERT);
6875 }
6876 gdb_assert (*slot == NULL);
6877 *slot = sig_type;
6878 /* The rest of sig_type must be filled in by the caller. */
6879 return sig_type;
6880 }
6881
6882 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6883 Fill in SIG_ENTRY with DWO_ENTRY. */
6884
6885 static void
6886 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6887 struct signatured_type *sig_entry,
6888 struct dwo_unit *dwo_entry)
6889 {
6890 /* Make sure we're not clobbering something we don't expect to. */
6891 gdb_assert (! sig_entry->per_cu.queued);
6892 gdb_assert (sig_entry->per_cu.cu == NULL);
6893 if (dwarf2_per_objfile->using_index)
6894 {
6895 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6896 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6897 }
6898 else
6899 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6900 gdb_assert (sig_entry->signature == dwo_entry->signature);
6901 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6902 gdb_assert (sig_entry->type_unit_group == NULL);
6903 gdb_assert (sig_entry->dwo_unit == NULL);
6904
6905 sig_entry->per_cu.section = dwo_entry->section;
6906 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6907 sig_entry->per_cu.length = dwo_entry->length;
6908 sig_entry->per_cu.reading_dwo_directly = 1;
6909 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6910 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6911 sig_entry->dwo_unit = dwo_entry;
6912 }
6913
6914 /* Subroutine of lookup_signatured_type.
6915 If we haven't read the TU yet, create the signatured_type data structure
6916 for a TU to be read in directly from a DWO file, bypassing the stub.
6917 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6918 using .gdb_index, then when reading a CU we want to stay in the DWO file
6919 containing that CU. Otherwise we could end up reading several other DWO
6920 files (due to comdat folding) to process the transitive closure of all the
6921 mentioned TUs, and that can be slow. The current DWO file will have every
6922 type signature that it needs.
6923 We only do this for .gdb_index because in the psymtab case we already have
6924 to read all the DWOs to build the type unit groups. */
6925
6926 static struct signatured_type *
6927 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6928 {
6929 struct dwarf2_per_objfile *dwarf2_per_objfile
6930 = cu->per_cu->dwarf2_per_objfile;
6931 struct objfile *objfile = dwarf2_per_objfile->objfile;
6932 struct dwo_file *dwo_file;
6933 struct dwo_unit find_dwo_entry, *dwo_entry;
6934 struct signatured_type find_sig_entry, *sig_entry;
6935 void **slot;
6936
6937 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6938
6939 /* If TU skeletons have been removed then we may not have read in any
6940 TUs yet. */
6941 if (dwarf2_per_objfile->signatured_types == NULL)
6942 {
6943 dwarf2_per_objfile->signatured_types
6944 = allocate_signatured_type_table (objfile);
6945 }
6946
6947 /* We only ever need to read in one copy of a signatured type.
6948 Use the global signatured_types array to do our own comdat-folding
6949 of types. If this is the first time we're reading this TU, and
6950 the TU has an entry in .gdb_index, replace the recorded data from
6951 .gdb_index with this TU. */
6952
6953 find_sig_entry.signature = sig;
6954 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6955 &find_sig_entry, INSERT);
6956 sig_entry = (struct signatured_type *) *slot;
6957
6958 /* We can get here with the TU already read, *or* in the process of being
6959 read. Don't reassign the global entry to point to this DWO if that's
6960 the case. Also note that if the TU is already being read, it may not
6961 have come from a DWO, the program may be a mix of Fission-compiled
6962 code and non-Fission-compiled code. */
6963
6964 /* Have we already tried to read this TU?
6965 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6966 needn't exist in the global table yet). */
6967 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6968 return sig_entry;
6969
6970 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6971 dwo_unit of the TU itself. */
6972 dwo_file = cu->dwo_unit->dwo_file;
6973
6974 /* Ok, this is the first time we're reading this TU. */
6975 if (dwo_file->tus == NULL)
6976 return NULL;
6977 find_dwo_entry.signature = sig;
6978 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
6979 if (dwo_entry == NULL)
6980 return NULL;
6981
6982 /* If the global table doesn't have an entry for this TU, add one. */
6983 if (sig_entry == NULL)
6984 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6985
6986 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6987 sig_entry->per_cu.tu_read = 1;
6988 return sig_entry;
6989 }
6990
6991 /* Subroutine of lookup_signatured_type.
6992 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6993 then try the DWP file. If the TU stub (skeleton) has been removed then
6994 it won't be in .gdb_index. */
6995
6996 static struct signatured_type *
6997 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6998 {
6999 struct dwarf2_per_objfile *dwarf2_per_objfile
7000 = cu->per_cu->dwarf2_per_objfile;
7001 struct objfile *objfile = dwarf2_per_objfile->objfile;
7002 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7003 struct dwo_unit *dwo_entry;
7004 struct signatured_type find_sig_entry, *sig_entry;
7005 void **slot;
7006
7007 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7008 gdb_assert (dwp_file != NULL);
7009
7010 /* If TU skeletons have been removed then we may not have read in any
7011 TUs yet. */
7012 if (dwarf2_per_objfile->signatured_types == NULL)
7013 {
7014 dwarf2_per_objfile->signatured_types
7015 = allocate_signatured_type_table (objfile);
7016 }
7017
7018 find_sig_entry.signature = sig;
7019 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7020 &find_sig_entry, INSERT);
7021 sig_entry = (struct signatured_type *) *slot;
7022
7023 /* Have we already tried to read this TU?
7024 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7025 needn't exist in the global table yet). */
7026 if (sig_entry != NULL)
7027 return sig_entry;
7028
7029 if (dwp_file->tus == NULL)
7030 return NULL;
7031 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7032 sig, 1 /* is_debug_types */);
7033 if (dwo_entry == NULL)
7034 return NULL;
7035
7036 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7037 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7038
7039 return sig_entry;
7040 }
7041
7042 /* Lookup a signature based type for DW_FORM_ref_sig8.
7043 Returns NULL if signature SIG is not present in the table.
7044 It is up to the caller to complain about this. */
7045
7046 static struct signatured_type *
7047 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7048 {
7049 struct dwarf2_per_objfile *dwarf2_per_objfile
7050 = cu->per_cu->dwarf2_per_objfile;
7051
7052 if (cu->dwo_unit
7053 && dwarf2_per_objfile->using_index)
7054 {
7055 /* We're in a DWO/DWP file, and we're using .gdb_index.
7056 These cases require special processing. */
7057 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7058 return lookup_dwo_signatured_type (cu, sig);
7059 else
7060 return lookup_dwp_signatured_type (cu, sig);
7061 }
7062 else
7063 {
7064 struct signatured_type find_entry, *entry;
7065
7066 if (dwarf2_per_objfile->signatured_types == NULL)
7067 return NULL;
7068 find_entry.signature = sig;
7069 entry = ((struct signatured_type *)
7070 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7071 return entry;
7072 }
7073 }
7074 \f
7075 /* Low level DIE reading support. */
7076
7077 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7078
7079 static void
7080 init_cu_die_reader (struct die_reader_specs *reader,
7081 struct dwarf2_cu *cu,
7082 struct dwarf2_section_info *section,
7083 struct dwo_file *dwo_file,
7084 struct abbrev_table *abbrev_table)
7085 {
7086 gdb_assert (section->readin && section->buffer != NULL);
7087 reader->abfd = get_section_bfd_owner (section);
7088 reader->cu = cu;
7089 reader->dwo_file = dwo_file;
7090 reader->die_section = section;
7091 reader->buffer = section->buffer;
7092 reader->buffer_end = section->buffer + section->size;
7093 reader->comp_dir = NULL;
7094 reader->abbrev_table = abbrev_table;
7095 }
7096
7097 /* Subroutine of init_cutu_and_read_dies to simplify it.
7098 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7099 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7100 already.
7101
7102 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7103 from it to the DIE in the DWO. If NULL we are skipping the stub.
7104 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7105 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7106 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7107 STUB_COMP_DIR may be non-NULL.
7108 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7109 are filled in with the info of the DIE from the DWO file.
7110 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7111 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7112 kept around for at least as long as *RESULT_READER.
7113
7114 The result is non-zero if a valid (non-dummy) DIE was found. */
7115
7116 static int
7117 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7118 struct dwo_unit *dwo_unit,
7119 struct die_info *stub_comp_unit_die,
7120 const char *stub_comp_dir,
7121 struct die_reader_specs *result_reader,
7122 const gdb_byte **result_info_ptr,
7123 struct die_info **result_comp_unit_die,
7124 int *result_has_children,
7125 abbrev_table_up *result_dwo_abbrev_table)
7126 {
7127 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7128 struct objfile *objfile = dwarf2_per_objfile->objfile;
7129 struct dwarf2_cu *cu = this_cu->cu;
7130 bfd *abfd;
7131 const gdb_byte *begin_info_ptr, *info_ptr;
7132 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7133 int i,num_extra_attrs;
7134 struct dwarf2_section_info *dwo_abbrev_section;
7135 struct attribute *attr;
7136 struct die_info *comp_unit_die;
7137
7138 /* At most one of these may be provided. */
7139 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7140
7141 /* These attributes aren't processed until later:
7142 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7143 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7144 referenced later. However, these attributes are found in the stub
7145 which we won't have later. In order to not impose this complication
7146 on the rest of the code, we read them here and copy them to the
7147 DWO CU/TU die. */
7148
7149 stmt_list = NULL;
7150 low_pc = NULL;
7151 high_pc = NULL;
7152 ranges = NULL;
7153 comp_dir = NULL;
7154
7155 if (stub_comp_unit_die != NULL)
7156 {
7157 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7158 DWO file. */
7159 if (! this_cu->is_debug_types)
7160 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7161 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7162 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7163 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7164 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7165
7166 /* There should be a DW_AT_addr_base attribute here (if needed).
7167 We need the value before we can process DW_FORM_GNU_addr_index. */
7168 cu->addr_base = 0;
7169 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7170 if (attr)
7171 cu->addr_base = DW_UNSND (attr);
7172
7173 /* There should be a DW_AT_ranges_base attribute here (if needed).
7174 We need the value before we can process DW_AT_ranges. */
7175 cu->ranges_base = 0;
7176 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7177 if (attr)
7178 cu->ranges_base = DW_UNSND (attr);
7179 }
7180 else if (stub_comp_dir != NULL)
7181 {
7182 /* Reconstruct the comp_dir attribute to simplify the code below. */
7183 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7184 comp_dir->name = DW_AT_comp_dir;
7185 comp_dir->form = DW_FORM_string;
7186 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7187 DW_STRING (comp_dir) = stub_comp_dir;
7188 }
7189
7190 /* Set up for reading the DWO CU/TU. */
7191 cu->dwo_unit = dwo_unit;
7192 dwarf2_section_info *section = dwo_unit->section;
7193 dwarf2_read_section (objfile, section);
7194 abfd = get_section_bfd_owner (section);
7195 begin_info_ptr = info_ptr = (section->buffer
7196 + to_underlying (dwo_unit->sect_off));
7197 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7198
7199 if (this_cu->is_debug_types)
7200 {
7201 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7202
7203 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7204 &cu->header, section,
7205 dwo_abbrev_section,
7206 info_ptr, rcuh_kind::TYPE);
7207 /* This is not an assert because it can be caused by bad debug info. */
7208 if (sig_type->signature != cu->header.signature)
7209 {
7210 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7211 " TU at offset %s [in module %s]"),
7212 hex_string (sig_type->signature),
7213 hex_string (cu->header.signature),
7214 sect_offset_str (dwo_unit->sect_off),
7215 bfd_get_filename (abfd));
7216 }
7217 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7218 /* For DWOs coming from DWP files, we don't know the CU length
7219 nor the type's offset in the TU until now. */
7220 dwo_unit->length = get_cu_length (&cu->header);
7221 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7222
7223 /* Establish the type offset that can be used to lookup the type.
7224 For DWO files, we don't know it until now. */
7225 sig_type->type_offset_in_section
7226 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7227 }
7228 else
7229 {
7230 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7231 &cu->header, section,
7232 dwo_abbrev_section,
7233 info_ptr, rcuh_kind::COMPILE);
7234 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7235 /* For DWOs coming from DWP files, we don't know the CU length
7236 until now. */
7237 dwo_unit->length = get_cu_length (&cu->header);
7238 }
7239
7240 *result_dwo_abbrev_table
7241 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7242 cu->header.abbrev_sect_off);
7243 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7244 result_dwo_abbrev_table->get ());
7245
7246 /* Read in the die, but leave space to copy over the attributes
7247 from the stub. This has the benefit of simplifying the rest of
7248 the code - all the work to maintain the illusion of a single
7249 DW_TAG_{compile,type}_unit DIE is done here. */
7250 num_extra_attrs = ((stmt_list != NULL)
7251 + (low_pc != NULL)
7252 + (high_pc != NULL)
7253 + (ranges != NULL)
7254 + (comp_dir != NULL));
7255 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7256 result_has_children, num_extra_attrs);
7257
7258 /* Copy over the attributes from the stub to the DIE we just read in. */
7259 comp_unit_die = *result_comp_unit_die;
7260 i = comp_unit_die->num_attrs;
7261 if (stmt_list != NULL)
7262 comp_unit_die->attrs[i++] = *stmt_list;
7263 if (low_pc != NULL)
7264 comp_unit_die->attrs[i++] = *low_pc;
7265 if (high_pc != NULL)
7266 comp_unit_die->attrs[i++] = *high_pc;
7267 if (ranges != NULL)
7268 comp_unit_die->attrs[i++] = *ranges;
7269 if (comp_dir != NULL)
7270 comp_unit_die->attrs[i++] = *comp_dir;
7271 comp_unit_die->num_attrs += num_extra_attrs;
7272
7273 if (dwarf_die_debug)
7274 {
7275 fprintf_unfiltered (gdb_stdlog,
7276 "Read die from %s@0x%x of %s:\n",
7277 get_section_name (section),
7278 (unsigned) (begin_info_ptr - section->buffer),
7279 bfd_get_filename (abfd));
7280 dump_die (comp_unit_die, dwarf_die_debug);
7281 }
7282
7283 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7284 TUs by skipping the stub and going directly to the entry in the DWO file.
7285 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7286 to get it via circuitous means. Blech. */
7287 if (comp_dir != NULL)
7288 result_reader->comp_dir = DW_STRING (comp_dir);
7289
7290 /* Skip dummy compilation units. */
7291 if (info_ptr >= begin_info_ptr + dwo_unit->length
7292 || peek_abbrev_code (abfd, info_ptr) == 0)
7293 return 0;
7294
7295 *result_info_ptr = info_ptr;
7296 return 1;
7297 }
7298
7299 /* Subroutine of init_cutu_and_read_dies to simplify it.
7300 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7301 Returns NULL if the specified DWO unit cannot be found. */
7302
7303 static struct dwo_unit *
7304 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7305 struct die_info *comp_unit_die)
7306 {
7307 struct dwarf2_cu *cu = this_cu->cu;
7308 ULONGEST signature;
7309 struct dwo_unit *dwo_unit;
7310 const char *comp_dir, *dwo_name;
7311
7312 gdb_assert (cu != NULL);
7313
7314 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7315 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7316 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7317
7318 if (this_cu->is_debug_types)
7319 {
7320 struct signatured_type *sig_type;
7321
7322 /* Since this_cu is the first member of struct signatured_type,
7323 we can go from a pointer to one to a pointer to the other. */
7324 sig_type = (struct signatured_type *) this_cu;
7325 signature = sig_type->signature;
7326 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7327 }
7328 else
7329 {
7330 struct attribute *attr;
7331
7332 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7333 if (! attr)
7334 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7335 " [in module %s]"),
7336 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7337 signature = DW_UNSND (attr);
7338 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7339 signature);
7340 }
7341
7342 return dwo_unit;
7343 }
7344
7345 /* Subroutine of init_cutu_and_read_dies to simplify it.
7346 See it for a description of the parameters.
7347 Read a TU directly from a DWO file, bypassing the stub. */
7348
7349 static void
7350 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7351 int use_existing_cu, int keep,
7352 die_reader_func_ftype *die_reader_func,
7353 void *data)
7354 {
7355 std::unique_ptr<dwarf2_cu> new_cu;
7356 struct signatured_type *sig_type;
7357 struct die_reader_specs reader;
7358 const gdb_byte *info_ptr;
7359 struct die_info *comp_unit_die;
7360 int has_children;
7361 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7362
7363 /* Verify we can do the following downcast, and that we have the
7364 data we need. */
7365 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7366 sig_type = (struct signatured_type *) this_cu;
7367 gdb_assert (sig_type->dwo_unit != NULL);
7368
7369 if (use_existing_cu && this_cu->cu != NULL)
7370 {
7371 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7372 /* There's no need to do the rereading_dwo_cu handling that
7373 init_cutu_and_read_dies does since we don't read the stub. */
7374 }
7375 else
7376 {
7377 /* If !use_existing_cu, this_cu->cu must be NULL. */
7378 gdb_assert (this_cu->cu == NULL);
7379 new_cu.reset (new dwarf2_cu (this_cu));
7380 }
7381
7382 /* A future optimization, if needed, would be to use an existing
7383 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7384 could share abbrev tables. */
7385
7386 /* The abbreviation table used by READER, this must live at least as long as
7387 READER. */
7388 abbrev_table_up dwo_abbrev_table;
7389
7390 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7391 NULL /* stub_comp_unit_die */,
7392 sig_type->dwo_unit->dwo_file->comp_dir,
7393 &reader, &info_ptr,
7394 &comp_unit_die, &has_children,
7395 &dwo_abbrev_table) == 0)
7396 {
7397 /* Dummy die. */
7398 return;
7399 }
7400
7401 /* All the "real" work is done here. */
7402 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7403
7404 /* This duplicates the code in init_cutu_and_read_dies,
7405 but the alternative is making the latter more complex.
7406 This function is only for the special case of using DWO files directly:
7407 no point in overly complicating the general case just to handle this. */
7408 if (new_cu != NULL && keep)
7409 {
7410 /* Link this CU into read_in_chain. */
7411 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7412 dwarf2_per_objfile->read_in_chain = this_cu;
7413 /* The chain owns it now. */
7414 new_cu.release ();
7415 }
7416 }
7417
7418 /* Initialize a CU (or TU) and read its DIEs.
7419 If the CU defers to a DWO file, read the DWO file as well.
7420
7421 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7422 Otherwise the table specified in the comp unit header is read in and used.
7423 This is an optimization for when we already have the abbrev table.
7424
7425 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7426 Otherwise, a new CU is allocated with xmalloc.
7427
7428 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7429 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7430
7431 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7432 linker) then DIE_READER_FUNC will not get called. */
7433
7434 static void
7435 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7436 struct abbrev_table *abbrev_table,
7437 int use_existing_cu, int keep,
7438 die_reader_func_ftype *die_reader_func,
7439 void *data)
7440 {
7441 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7442 struct objfile *objfile = dwarf2_per_objfile->objfile;
7443 struct dwarf2_section_info *section = this_cu->section;
7444 bfd *abfd = get_section_bfd_owner (section);
7445 struct dwarf2_cu *cu;
7446 const gdb_byte *begin_info_ptr, *info_ptr;
7447 struct die_reader_specs reader;
7448 struct die_info *comp_unit_die;
7449 int has_children;
7450 struct attribute *attr;
7451 struct signatured_type *sig_type = NULL;
7452 struct dwarf2_section_info *abbrev_section;
7453 /* Non-zero if CU currently points to a DWO file and we need to
7454 reread it. When this happens we need to reread the skeleton die
7455 before we can reread the DWO file (this only applies to CUs, not TUs). */
7456 int rereading_dwo_cu = 0;
7457
7458 if (dwarf_die_debug)
7459 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7460 this_cu->is_debug_types ? "type" : "comp",
7461 sect_offset_str (this_cu->sect_off));
7462
7463 if (use_existing_cu)
7464 gdb_assert (keep);
7465
7466 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7467 file (instead of going through the stub), short-circuit all of this. */
7468 if (this_cu->reading_dwo_directly)
7469 {
7470 /* Narrow down the scope of possibilities to have to understand. */
7471 gdb_assert (this_cu->is_debug_types);
7472 gdb_assert (abbrev_table == NULL);
7473 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7474 die_reader_func, data);
7475 return;
7476 }
7477
7478 /* This is cheap if the section is already read in. */
7479 dwarf2_read_section (objfile, section);
7480
7481 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7482
7483 abbrev_section = get_abbrev_section_for_cu (this_cu);
7484
7485 std::unique_ptr<dwarf2_cu> new_cu;
7486 if (use_existing_cu && this_cu->cu != NULL)
7487 {
7488 cu = this_cu->cu;
7489 /* If this CU is from a DWO file we need to start over, we need to
7490 refetch the attributes from the skeleton CU.
7491 This could be optimized by retrieving those attributes from when we
7492 were here the first time: the previous comp_unit_die was stored in
7493 comp_unit_obstack. But there's no data yet that we need this
7494 optimization. */
7495 if (cu->dwo_unit != NULL)
7496 rereading_dwo_cu = 1;
7497 }
7498 else
7499 {
7500 /* If !use_existing_cu, this_cu->cu must be NULL. */
7501 gdb_assert (this_cu->cu == NULL);
7502 new_cu.reset (new dwarf2_cu (this_cu));
7503 cu = new_cu.get ();
7504 }
7505
7506 /* Get the header. */
7507 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7508 {
7509 /* We already have the header, there's no need to read it in again. */
7510 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7511 }
7512 else
7513 {
7514 if (this_cu->is_debug_types)
7515 {
7516 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7517 &cu->header, section,
7518 abbrev_section, info_ptr,
7519 rcuh_kind::TYPE);
7520
7521 /* Since per_cu is the first member of struct signatured_type,
7522 we can go from a pointer to one to a pointer to the other. */
7523 sig_type = (struct signatured_type *) this_cu;
7524 gdb_assert (sig_type->signature == cu->header.signature);
7525 gdb_assert (sig_type->type_offset_in_tu
7526 == cu->header.type_cu_offset_in_tu);
7527 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7528
7529 /* LENGTH has not been set yet for type units if we're
7530 using .gdb_index. */
7531 this_cu->length = get_cu_length (&cu->header);
7532
7533 /* Establish the type offset that can be used to lookup the type. */
7534 sig_type->type_offset_in_section =
7535 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7536
7537 this_cu->dwarf_version = cu->header.version;
7538 }
7539 else
7540 {
7541 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7542 &cu->header, section,
7543 abbrev_section,
7544 info_ptr,
7545 rcuh_kind::COMPILE);
7546
7547 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7548 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7549 this_cu->dwarf_version = cu->header.version;
7550 }
7551 }
7552
7553 /* Skip dummy compilation units. */
7554 if (info_ptr >= begin_info_ptr + this_cu->length
7555 || peek_abbrev_code (abfd, info_ptr) == 0)
7556 return;
7557
7558 /* If we don't have them yet, read the abbrevs for this compilation unit.
7559 And if we need to read them now, make sure they're freed when we're
7560 done (own the table through ABBREV_TABLE_HOLDER). */
7561 abbrev_table_up abbrev_table_holder;
7562 if (abbrev_table != NULL)
7563 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7564 else
7565 {
7566 abbrev_table_holder
7567 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7568 cu->header.abbrev_sect_off);
7569 abbrev_table = abbrev_table_holder.get ();
7570 }
7571
7572 /* Read the top level CU/TU die. */
7573 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7574 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7575
7576 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7577 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7578 table from the DWO file and pass the ownership over to us. It will be
7579 referenced from READER, so we must make sure to free it after we're done
7580 with READER.
7581
7582 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7583 DWO CU, that this test will fail (the attribute will not be present). */
7584 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7585 abbrev_table_up dwo_abbrev_table;
7586 if (attr)
7587 {
7588 struct dwo_unit *dwo_unit;
7589 struct die_info *dwo_comp_unit_die;
7590
7591 if (has_children)
7592 {
7593 complaint (&symfile_complaints,
7594 _("compilation unit with DW_AT_GNU_dwo_name"
7595 " has children (offset %s) [in module %s]"),
7596 sect_offset_str (this_cu->sect_off),
7597 bfd_get_filename (abfd));
7598 }
7599 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7600 if (dwo_unit != NULL)
7601 {
7602 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7603 comp_unit_die, NULL,
7604 &reader, &info_ptr,
7605 &dwo_comp_unit_die, &has_children,
7606 &dwo_abbrev_table) == 0)
7607 {
7608 /* Dummy die. */
7609 return;
7610 }
7611 comp_unit_die = dwo_comp_unit_die;
7612 }
7613 else
7614 {
7615 /* Yikes, we couldn't find the rest of the DIE, we only have
7616 the stub. A complaint has already been logged. There's
7617 not much more we can do except pass on the stub DIE to
7618 die_reader_func. We don't want to throw an error on bad
7619 debug info. */
7620 }
7621 }
7622
7623 /* All of the above is setup for this call. Yikes. */
7624 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7625
7626 /* Done, clean up. */
7627 if (new_cu != NULL && keep)
7628 {
7629 /* Link this CU into read_in_chain. */
7630 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7631 dwarf2_per_objfile->read_in_chain = this_cu;
7632 /* The chain owns it now. */
7633 new_cu.release ();
7634 }
7635 }
7636
7637 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7638 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7639 to have already done the lookup to find the DWO file).
7640
7641 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7642 THIS_CU->is_debug_types, but nothing else.
7643
7644 We fill in THIS_CU->length.
7645
7646 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7647 linker) then DIE_READER_FUNC will not get called.
7648
7649 THIS_CU->cu is always freed when done.
7650 This is done in order to not leave THIS_CU->cu in a state where we have
7651 to care whether it refers to the "main" CU or the DWO CU. */
7652
7653 static void
7654 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7655 struct dwo_file *dwo_file,
7656 die_reader_func_ftype *die_reader_func,
7657 void *data)
7658 {
7659 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7660 struct objfile *objfile = dwarf2_per_objfile->objfile;
7661 struct dwarf2_section_info *section = this_cu->section;
7662 bfd *abfd = get_section_bfd_owner (section);
7663 struct dwarf2_section_info *abbrev_section;
7664 const gdb_byte *begin_info_ptr, *info_ptr;
7665 struct die_reader_specs reader;
7666 struct die_info *comp_unit_die;
7667 int has_children;
7668
7669 if (dwarf_die_debug)
7670 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7671 this_cu->is_debug_types ? "type" : "comp",
7672 sect_offset_str (this_cu->sect_off));
7673
7674 gdb_assert (this_cu->cu == NULL);
7675
7676 abbrev_section = (dwo_file != NULL
7677 ? &dwo_file->sections.abbrev
7678 : get_abbrev_section_for_cu (this_cu));
7679
7680 /* This is cheap if the section is already read in. */
7681 dwarf2_read_section (objfile, section);
7682
7683 struct dwarf2_cu cu (this_cu);
7684
7685 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7686 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7687 &cu.header, section,
7688 abbrev_section, info_ptr,
7689 (this_cu->is_debug_types
7690 ? rcuh_kind::TYPE
7691 : rcuh_kind::COMPILE));
7692
7693 this_cu->length = get_cu_length (&cu.header);
7694
7695 /* Skip dummy compilation units. */
7696 if (info_ptr >= begin_info_ptr + this_cu->length
7697 || peek_abbrev_code (abfd, info_ptr) == 0)
7698 return;
7699
7700 abbrev_table_up abbrev_table
7701 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7702 cu.header.abbrev_sect_off);
7703
7704 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
7705 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7706
7707 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7708 }
7709
7710 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7711 does not lookup the specified DWO file.
7712 This cannot be used to read DWO files.
7713
7714 THIS_CU->cu is always freed when done.
7715 This is done in order to not leave THIS_CU->cu in a state where we have
7716 to care whether it refers to the "main" CU or the DWO CU.
7717 We can revisit this if the data shows there's a performance issue. */
7718
7719 static void
7720 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7721 die_reader_func_ftype *die_reader_func,
7722 void *data)
7723 {
7724 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
7725 }
7726 \f
7727 /* Type Unit Groups.
7728
7729 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7730 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7731 so that all types coming from the same compilation (.o file) are grouped
7732 together. A future step could be to put the types in the same symtab as
7733 the CU the types ultimately came from. */
7734
7735 static hashval_t
7736 hash_type_unit_group (const void *item)
7737 {
7738 const struct type_unit_group *tu_group
7739 = (const struct type_unit_group *) item;
7740
7741 return hash_stmt_list_entry (&tu_group->hash);
7742 }
7743
7744 static int
7745 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7746 {
7747 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7748 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7749
7750 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7751 }
7752
7753 /* Allocate a hash table for type unit groups. */
7754
7755 static htab_t
7756 allocate_type_unit_groups_table (struct objfile *objfile)
7757 {
7758 return htab_create_alloc_ex (3,
7759 hash_type_unit_group,
7760 eq_type_unit_group,
7761 NULL,
7762 &objfile->objfile_obstack,
7763 hashtab_obstack_allocate,
7764 dummy_obstack_deallocate);
7765 }
7766
7767 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7768 partial symtabs. We combine several TUs per psymtab to not let the size
7769 of any one psymtab grow too big. */
7770 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7771 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7772
7773 /* Helper routine for get_type_unit_group.
7774 Create the type_unit_group object used to hold one or more TUs. */
7775
7776 static struct type_unit_group *
7777 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7778 {
7779 struct dwarf2_per_objfile *dwarf2_per_objfile
7780 = cu->per_cu->dwarf2_per_objfile;
7781 struct objfile *objfile = dwarf2_per_objfile->objfile;
7782 struct dwarf2_per_cu_data *per_cu;
7783 struct type_unit_group *tu_group;
7784
7785 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7786 struct type_unit_group);
7787 per_cu = &tu_group->per_cu;
7788 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7789
7790 if (dwarf2_per_objfile->using_index)
7791 {
7792 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7793 struct dwarf2_per_cu_quick_data);
7794 }
7795 else
7796 {
7797 unsigned int line_offset = to_underlying (line_offset_struct);
7798 struct partial_symtab *pst;
7799 char *name;
7800
7801 /* Give the symtab a useful name for debug purposes. */
7802 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7803 name = xstrprintf ("<type_units_%d>",
7804 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7805 else
7806 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
7807
7808 pst = create_partial_symtab (per_cu, name);
7809 pst->anonymous = 1;
7810
7811 xfree (name);
7812 }
7813
7814 tu_group->hash.dwo_unit = cu->dwo_unit;
7815 tu_group->hash.line_sect_off = line_offset_struct;
7816
7817 return tu_group;
7818 }
7819
7820 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7821 STMT_LIST is a DW_AT_stmt_list attribute. */
7822
7823 static struct type_unit_group *
7824 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7825 {
7826 struct dwarf2_per_objfile *dwarf2_per_objfile
7827 = cu->per_cu->dwarf2_per_objfile;
7828 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7829 struct type_unit_group *tu_group;
7830 void **slot;
7831 unsigned int line_offset;
7832 struct type_unit_group type_unit_group_for_lookup;
7833
7834 if (dwarf2_per_objfile->type_unit_groups == NULL)
7835 {
7836 dwarf2_per_objfile->type_unit_groups =
7837 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7838 }
7839
7840 /* Do we need to create a new group, or can we use an existing one? */
7841
7842 if (stmt_list)
7843 {
7844 line_offset = DW_UNSND (stmt_list);
7845 ++tu_stats->nr_symtab_sharers;
7846 }
7847 else
7848 {
7849 /* Ugh, no stmt_list. Rare, but we have to handle it.
7850 We can do various things here like create one group per TU or
7851 spread them over multiple groups to split up the expansion work.
7852 To avoid worst case scenarios (too many groups or too large groups)
7853 we, umm, group them in bunches. */
7854 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7855 | (tu_stats->nr_stmt_less_type_units
7856 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7857 ++tu_stats->nr_stmt_less_type_units;
7858 }
7859
7860 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7861 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7862 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7863 &type_unit_group_for_lookup, INSERT);
7864 if (*slot != NULL)
7865 {
7866 tu_group = (struct type_unit_group *) *slot;
7867 gdb_assert (tu_group != NULL);
7868 }
7869 else
7870 {
7871 sect_offset line_offset_struct = (sect_offset) line_offset;
7872 tu_group = create_type_unit_group (cu, line_offset_struct);
7873 *slot = tu_group;
7874 ++tu_stats->nr_symtabs;
7875 }
7876
7877 return tu_group;
7878 }
7879 \f
7880 /* Partial symbol tables. */
7881
7882 /* Create a psymtab named NAME and assign it to PER_CU.
7883
7884 The caller must fill in the following details:
7885 dirname, textlow, texthigh. */
7886
7887 static struct partial_symtab *
7888 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7889 {
7890 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7891 struct partial_symtab *pst;
7892
7893 pst = start_psymtab_common (objfile, name, 0,
7894 objfile->global_psymbols,
7895 objfile->static_psymbols);
7896
7897 pst->psymtabs_addrmap_supported = 1;
7898
7899 /* This is the glue that links PST into GDB's symbol API. */
7900 pst->read_symtab_private = per_cu;
7901 pst->read_symtab = dwarf2_read_symtab;
7902 per_cu->v.psymtab = pst;
7903
7904 return pst;
7905 }
7906
7907 /* The DATA object passed to process_psymtab_comp_unit_reader has this
7908 type. */
7909
7910 struct process_psymtab_comp_unit_data
7911 {
7912 /* True if we are reading a DW_TAG_partial_unit. */
7913
7914 int want_partial_unit;
7915
7916 /* The "pretend" language that is used if the CU doesn't declare a
7917 language. */
7918
7919 enum language pretend_language;
7920 };
7921
7922 /* die_reader_func for process_psymtab_comp_unit. */
7923
7924 static void
7925 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7926 const gdb_byte *info_ptr,
7927 struct die_info *comp_unit_die,
7928 int has_children,
7929 void *data)
7930 {
7931 struct dwarf2_cu *cu = reader->cu;
7932 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7933 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7934 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7935 CORE_ADDR baseaddr;
7936 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7937 struct partial_symtab *pst;
7938 enum pc_bounds_kind cu_bounds_kind;
7939 const char *filename;
7940 struct process_psymtab_comp_unit_data *info
7941 = (struct process_psymtab_comp_unit_data *) data;
7942
7943 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
7944 return;
7945
7946 gdb_assert (! per_cu->is_debug_types);
7947
7948 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
7949
7950 cu->list_in_scope = &file_symbols;
7951
7952 /* Allocate a new partial symbol table structure. */
7953 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7954 if (filename == NULL)
7955 filename = "";
7956
7957 pst = create_partial_symtab (per_cu, filename);
7958
7959 /* This must be done before calling dwarf2_build_include_psymtabs. */
7960 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7961
7962 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7963
7964 dwarf2_find_base_address (comp_unit_die, cu);
7965
7966 /* Possibly set the default values of LOWPC and HIGHPC from
7967 `DW_AT_ranges'. */
7968 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7969 &best_highpc, cu, pst);
7970 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7971 /* Store the contiguous range if it is not empty; it can be empty for
7972 CUs with no code. */
7973 addrmap_set_empty (objfile->psymtabs_addrmap,
7974 gdbarch_adjust_dwarf2_addr (gdbarch,
7975 best_lowpc + baseaddr),
7976 gdbarch_adjust_dwarf2_addr (gdbarch,
7977 best_highpc + baseaddr) - 1,
7978 pst);
7979
7980 /* Check if comp unit has_children.
7981 If so, read the rest of the partial symbols from this comp unit.
7982 If not, there's no more debug_info for this comp unit. */
7983 if (has_children)
7984 {
7985 struct partial_die_info *first_die;
7986 CORE_ADDR lowpc, highpc;
7987
7988 lowpc = ((CORE_ADDR) -1);
7989 highpc = ((CORE_ADDR) 0);
7990
7991 first_die = load_partial_dies (reader, info_ptr, 1);
7992
7993 scan_partial_symbols (first_die, &lowpc, &highpc,
7994 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7995
7996 /* If we didn't find a lowpc, set it to highpc to avoid
7997 complaints from `maint check'. */
7998 if (lowpc == ((CORE_ADDR) -1))
7999 lowpc = highpc;
8000
8001 /* If the compilation unit didn't have an explicit address range,
8002 then use the information extracted from its child dies. */
8003 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8004 {
8005 best_lowpc = lowpc;
8006 best_highpc = highpc;
8007 }
8008 }
8009 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
8010 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
8011
8012 end_psymtab_common (objfile, pst);
8013
8014 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8015 {
8016 int i;
8017 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8018 struct dwarf2_per_cu_data *iter;
8019
8020 /* Fill in 'dependencies' here; we fill in 'users' in a
8021 post-pass. */
8022 pst->number_of_dependencies = len;
8023 pst->dependencies =
8024 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8025 for (i = 0;
8026 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8027 i, iter);
8028 ++i)
8029 pst->dependencies[i] = iter->v.psymtab;
8030
8031 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8032 }
8033
8034 /* Get the list of files included in the current compilation unit,
8035 and build a psymtab for each of them. */
8036 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8037
8038 if (dwarf_read_debug)
8039 {
8040 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8041
8042 fprintf_unfiltered (gdb_stdlog,
8043 "Psymtab for %s unit @%s: %s - %s"
8044 ", %d global, %d static syms\n",
8045 per_cu->is_debug_types ? "type" : "comp",
8046 sect_offset_str (per_cu->sect_off),
8047 paddress (gdbarch, pst->textlow),
8048 paddress (gdbarch, pst->texthigh),
8049 pst->n_global_syms, pst->n_static_syms);
8050 }
8051 }
8052
8053 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8054 Process compilation unit THIS_CU for a psymtab. */
8055
8056 static void
8057 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8058 int want_partial_unit,
8059 enum language pretend_language)
8060 {
8061 /* If this compilation unit was already read in, free the
8062 cached copy in order to read it in again. This is
8063 necessary because we skipped some symbols when we first
8064 read in the compilation unit (see load_partial_dies).
8065 This problem could be avoided, but the benefit is unclear. */
8066 if (this_cu->cu != NULL)
8067 free_one_cached_comp_unit (this_cu);
8068
8069 if (this_cu->is_debug_types)
8070 init_cutu_and_read_dies (this_cu, NULL, 0, 0, build_type_psymtabs_reader,
8071 NULL);
8072 else
8073 {
8074 process_psymtab_comp_unit_data info;
8075 info.want_partial_unit = want_partial_unit;
8076 info.pretend_language = pretend_language;
8077 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
8078 process_psymtab_comp_unit_reader, &info);
8079 }
8080
8081 /* Age out any secondary CUs. */
8082 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8083 }
8084
8085 /* Reader function for build_type_psymtabs. */
8086
8087 static void
8088 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8089 const gdb_byte *info_ptr,
8090 struct die_info *type_unit_die,
8091 int has_children,
8092 void *data)
8093 {
8094 struct dwarf2_per_objfile *dwarf2_per_objfile
8095 = reader->cu->per_cu->dwarf2_per_objfile;
8096 struct objfile *objfile = dwarf2_per_objfile->objfile;
8097 struct dwarf2_cu *cu = reader->cu;
8098 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8099 struct signatured_type *sig_type;
8100 struct type_unit_group *tu_group;
8101 struct attribute *attr;
8102 struct partial_die_info *first_die;
8103 CORE_ADDR lowpc, highpc;
8104 struct partial_symtab *pst;
8105
8106 gdb_assert (data == NULL);
8107 gdb_assert (per_cu->is_debug_types);
8108 sig_type = (struct signatured_type *) per_cu;
8109
8110 if (! has_children)
8111 return;
8112
8113 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8114 tu_group = get_type_unit_group (cu, attr);
8115
8116 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
8117
8118 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8119 cu->list_in_scope = &file_symbols;
8120 pst = create_partial_symtab (per_cu, "");
8121 pst->anonymous = 1;
8122
8123 first_die = load_partial_dies (reader, info_ptr, 1);
8124
8125 lowpc = (CORE_ADDR) -1;
8126 highpc = (CORE_ADDR) 0;
8127 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8128
8129 end_psymtab_common (objfile, pst);
8130 }
8131
8132 /* Struct used to sort TUs by their abbreviation table offset. */
8133
8134 struct tu_abbrev_offset
8135 {
8136 struct signatured_type *sig_type;
8137 sect_offset abbrev_offset;
8138 };
8139
8140 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8141
8142 static bool
8143 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8144 const struct tu_abbrev_offset &b)
8145 {
8146 return a.abbrev_offset < b.abbrev_offset;
8147 }
8148
8149 /* Efficiently read all the type units.
8150 This does the bulk of the work for build_type_psymtabs.
8151
8152 The efficiency is because we sort TUs by the abbrev table they use and
8153 only read each abbrev table once. In one program there are 200K TUs
8154 sharing 8K abbrev tables.
8155
8156 The main purpose of this function is to support building the
8157 dwarf2_per_objfile->type_unit_groups table.
8158 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8159 can collapse the search space by grouping them by stmt_list.
8160 The savings can be significant, in the same program from above the 200K TUs
8161 share 8K stmt_list tables.
8162
8163 FUNC is expected to call get_type_unit_group, which will create the
8164 struct type_unit_group if necessary and add it to
8165 dwarf2_per_objfile->type_unit_groups. */
8166
8167 static void
8168 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8169 {
8170 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8171 abbrev_table_up abbrev_table;
8172 sect_offset abbrev_offset;
8173 int i;
8174
8175 /* It's up to the caller to not call us multiple times. */
8176 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8177
8178 if (dwarf2_per_objfile->n_type_units == 0)
8179 return;
8180
8181 /* TUs typically share abbrev tables, and there can be way more TUs than
8182 abbrev tables. Sort by abbrev table to reduce the number of times we
8183 read each abbrev table in.
8184 Alternatives are to punt or to maintain a cache of abbrev tables.
8185 This is simpler and efficient enough for now.
8186
8187 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8188 symtab to use). Typically TUs with the same abbrev offset have the same
8189 stmt_list value too so in practice this should work well.
8190
8191 The basic algorithm here is:
8192
8193 sort TUs by abbrev table
8194 for each TU with same abbrev table:
8195 read abbrev table if first user
8196 read TU top level DIE
8197 [IWBN if DWO skeletons had DW_AT_stmt_list]
8198 call FUNC */
8199
8200 if (dwarf_read_debug)
8201 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8202
8203 /* Sort in a separate table to maintain the order of all_type_units
8204 for .gdb_index: TU indices directly index all_type_units. */
8205 std::vector<struct tu_abbrev_offset> sorted_by_abbrev
8206 (dwarf2_per_objfile->n_type_units);
8207 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8208 {
8209 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
8210
8211 sorted_by_abbrev[i].sig_type = sig_type;
8212 sorted_by_abbrev[i].abbrev_offset =
8213 read_abbrev_offset (dwarf2_per_objfile,
8214 sig_type->per_cu.section,
8215 sig_type->per_cu.sect_off);
8216 }
8217 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8218 sort_tu_by_abbrev_offset);
8219
8220 abbrev_offset = (sect_offset) ~(unsigned) 0;
8221
8222 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
8223 {
8224 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
8225
8226 /* Switch to the next abbrev table if necessary. */
8227 if (abbrev_table == NULL
8228 || tu->abbrev_offset != abbrev_offset)
8229 {
8230 abbrev_offset = tu->abbrev_offset;
8231 abbrev_table =
8232 abbrev_table_read_table (dwarf2_per_objfile,
8233 &dwarf2_per_objfile->abbrev,
8234 abbrev_offset);
8235 ++tu_stats->nr_uniq_abbrev_tables;
8236 }
8237
8238 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table.get (),
8239 0, 0, build_type_psymtabs_reader, NULL);
8240 }
8241 }
8242
8243 /* Print collected type unit statistics. */
8244
8245 static void
8246 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8247 {
8248 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8249
8250 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8251 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
8252 dwarf2_per_objfile->n_type_units);
8253 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8254 tu_stats->nr_uniq_abbrev_tables);
8255 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8256 tu_stats->nr_symtabs);
8257 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8258 tu_stats->nr_symtab_sharers);
8259 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8260 tu_stats->nr_stmt_less_type_units);
8261 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8262 tu_stats->nr_all_type_units_reallocs);
8263 }
8264
8265 /* Traversal function for build_type_psymtabs. */
8266
8267 static int
8268 build_type_psymtab_dependencies (void **slot, void *info)
8269 {
8270 struct dwarf2_per_objfile *dwarf2_per_objfile
8271 = (struct dwarf2_per_objfile *) info;
8272 struct objfile *objfile = dwarf2_per_objfile->objfile;
8273 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8274 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8275 struct partial_symtab *pst = per_cu->v.psymtab;
8276 int len = VEC_length (sig_type_ptr, tu_group->tus);
8277 struct signatured_type *iter;
8278 int i;
8279
8280 gdb_assert (len > 0);
8281 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8282
8283 pst->number_of_dependencies = len;
8284 pst->dependencies =
8285 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
8286 for (i = 0;
8287 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
8288 ++i)
8289 {
8290 gdb_assert (iter->per_cu.is_debug_types);
8291 pst->dependencies[i] = iter->per_cu.v.psymtab;
8292 iter->type_unit_group = tu_group;
8293 }
8294
8295 VEC_free (sig_type_ptr, tu_group->tus);
8296
8297 return 1;
8298 }
8299
8300 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8301 Build partial symbol tables for the .debug_types comp-units. */
8302
8303 static void
8304 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8305 {
8306 if (! create_all_type_units (dwarf2_per_objfile))
8307 return;
8308
8309 build_type_psymtabs_1 (dwarf2_per_objfile);
8310 }
8311
8312 /* Traversal function for process_skeletonless_type_unit.
8313 Read a TU in a DWO file and build partial symbols for it. */
8314
8315 static int
8316 process_skeletonless_type_unit (void **slot, void *info)
8317 {
8318 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8319 struct dwarf2_per_objfile *dwarf2_per_objfile
8320 = (struct dwarf2_per_objfile *) info;
8321 struct signatured_type find_entry, *entry;
8322
8323 /* If this TU doesn't exist in the global table, add it and read it in. */
8324
8325 if (dwarf2_per_objfile->signatured_types == NULL)
8326 {
8327 dwarf2_per_objfile->signatured_types
8328 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8329 }
8330
8331 find_entry.signature = dwo_unit->signature;
8332 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8333 INSERT);
8334 /* If we've already seen this type there's nothing to do. What's happening
8335 is we're doing our own version of comdat-folding here. */
8336 if (*slot != NULL)
8337 return 1;
8338
8339 /* This does the job that create_all_type_units would have done for
8340 this TU. */
8341 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8342 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8343 *slot = entry;
8344
8345 /* This does the job that build_type_psymtabs_1 would have done. */
8346 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
8347 build_type_psymtabs_reader, NULL);
8348
8349 return 1;
8350 }
8351
8352 /* Traversal function for process_skeletonless_type_units. */
8353
8354 static int
8355 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8356 {
8357 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8358
8359 if (dwo_file->tus != NULL)
8360 {
8361 htab_traverse_noresize (dwo_file->tus,
8362 process_skeletonless_type_unit, info);
8363 }
8364
8365 return 1;
8366 }
8367
8368 /* Scan all TUs of DWO files, verifying we've processed them.
8369 This is needed in case a TU was emitted without its skeleton.
8370 Note: This can't be done until we know what all the DWO files are. */
8371
8372 static void
8373 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8374 {
8375 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8376 if (get_dwp_file (dwarf2_per_objfile) == NULL
8377 && dwarf2_per_objfile->dwo_files != NULL)
8378 {
8379 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8380 process_dwo_file_for_skeletonless_type_units,
8381 dwarf2_per_objfile);
8382 }
8383 }
8384
8385 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8386
8387 static void
8388 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8389 {
8390 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8391 {
8392 struct partial_symtab *pst = per_cu->v.psymtab;
8393
8394 if (pst == NULL)
8395 continue;
8396
8397 for (int j = 0; j < pst->number_of_dependencies; ++j)
8398 {
8399 /* Set the 'user' field only if it is not already set. */
8400 if (pst->dependencies[j]->user == NULL)
8401 pst->dependencies[j]->user = pst;
8402 }
8403 }
8404 }
8405
8406 /* Build the partial symbol table by doing a quick pass through the
8407 .debug_info and .debug_abbrev sections. */
8408
8409 static void
8410 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8411 {
8412 struct objfile *objfile = dwarf2_per_objfile->objfile;
8413
8414 if (dwarf_read_debug)
8415 {
8416 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8417 objfile_name (objfile));
8418 }
8419
8420 dwarf2_per_objfile->reading_partial_symbols = 1;
8421
8422 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8423
8424 /* Any cached compilation units will be linked by the per-objfile
8425 read_in_chain. Make sure to free them when we're done. */
8426 free_cached_comp_units freer (dwarf2_per_objfile);
8427
8428 build_type_psymtabs (dwarf2_per_objfile);
8429
8430 create_all_comp_units (dwarf2_per_objfile);
8431
8432 /* Create a temporary address map on a temporary obstack. We later
8433 copy this to the final obstack. */
8434 auto_obstack temp_obstack;
8435
8436 scoped_restore save_psymtabs_addrmap
8437 = make_scoped_restore (&objfile->psymtabs_addrmap,
8438 addrmap_create_mutable (&temp_obstack));
8439
8440 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8441 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8442
8443 /* This has to wait until we read the CUs, we need the list of DWOs. */
8444 process_skeletonless_type_units (dwarf2_per_objfile);
8445
8446 /* Now that all TUs have been processed we can fill in the dependencies. */
8447 if (dwarf2_per_objfile->type_unit_groups != NULL)
8448 {
8449 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8450 build_type_psymtab_dependencies, dwarf2_per_objfile);
8451 }
8452
8453 if (dwarf_read_debug)
8454 print_tu_stats (dwarf2_per_objfile);
8455
8456 set_partial_user (dwarf2_per_objfile);
8457
8458 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8459 &objfile->objfile_obstack);
8460 /* At this point we want to keep the address map. */
8461 save_psymtabs_addrmap.release ();
8462
8463 if (dwarf_read_debug)
8464 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8465 objfile_name (objfile));
8466 }
8467
8468 /* die_reader_func for load_partial_comp_unit. */
8469
8470 static void
8471 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8472 const gdb_byte *info_ptr,
8473 struct die_info *comp_unit_die,
8474 int has_children,
8475 void *data)
8476 {
8477 struct dwarf2_cu *cu = reader->cu;
8478
8479 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8480
8481 /* Check if comp unit has_children.
8482 If so, read the rest of the partial symbols from this comp unit.
8483 If not, there's no more debug_info for this comp unit. */
8484 if (has_children)
8485 load_partial_dies (reader, info_ptr, 0);
8486 }
8487
8488 /* Load the partial DIEs for a secondary CU into memory.
8489 This is also used when rereading a primary CU with load_all_dies. */
8490
8491 static void
8492 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8493 {
8494 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
8495 load_partial_comp_unit_reader, NULL);
8496 }
8497
8498 static void
8499 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8500 struct dwarf2_section_info *section,
8501 struct dwarf2_section_info *abbrev_section,
8502 unsigned int is_dwz)
8503 {
8504 const gdb_byte *info_ptr;
8505 struct objfile *objfile = dwarf2_per_objfile->objfile;
8506
8507 if (dwarf_read_debug)
8508 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8509 get_section_name (section),
8510 get_section_file_name (section));
8511
8512 dwarf2_read_section (objfile, section);
8513
8514 info_ptr = section->buffer;
8515
8516 while (info_ptr < section->buffer + section->size)
8517 {
8518 struct dwarf2_per_cu_data *this_cu;
8519
8520 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8521
8522 comp_unit_head cu_header;
8523 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8524 abbrev_section, info_ptr,
8525 rcuh_kind::COMPILE);
8526
8527 /* Save the compilation unit for later lookup. */
8528 if (cu_header.unit_type != DW_UT_type)
8529 {
8530 this_cu = XOBNEW (&objfile->objfile_obstack,
8531 struct dwarf2_per_cu_data);
8532 memset (this_cu, 0, sizeof (*this_cu));
8533 }
8534 else
8535 {
8536 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8537 struct signatured_type);
8538 memset (sig_type, 0, sizeof (*sig_type));
8539 sig_type->signature = cu_header.signature;
8540 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8541 this_cu = &sig_type->per_cu;
8542 }
8543 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8544 this_cu->sect_off = sect_off;
8545 this_cu->length = cu_header.length + cu_header.initial_length_size;
8546 this_cu->is_dwz = is_dwz;
8547 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8548 this_cu->section = section;
8549
8550 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8551
8552 info_ptr = info_ptr + this_cu->length;
8553 }
8554 }
8555
8556 /* Create a list of all compilation units in OBJFILE.
8557 This is only done for -readnow and building partial symtabs. */
8558
8559 static void
8560 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8561 {
8562 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8563 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8564 &dwarf2_per_objfile->abbrev, 0);
8565
8566 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8567 if (dwz != NULL)
8568 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8569 1);
8570 }
8571
8572 /* Process all loaded DIEs for compilation unit CU, starting at
8573 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8574 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8575 DW_AT_ranges). See the comments of add_partial_subprogram on how
8576 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8577
8578 static void
8579 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8580 CORE_ADDR *highpc, int set_addrmap,
8581 struct dwarf2_cu *cu)
8582 {
8583 struct partial_die_info *pdi;
8584
8585 /* Now, march along the PDI's, descending into ones which have
8586 interesting children but skipping the children of the other ones,
8587 until we reach the end of the compilation unit. */
8588
8589 pdi = first_die;
8590
8591 while (pdi != NULL)
8592 {
8593 pdi->fixup (cu);
8594
8595 /* Anonymous namespaces or modules have no name but have interesting
8596 children, so we need to look at them. Ditto for anonymous
8597 enums. */
8598
8599 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8600 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8601 || pdi->tag == DW_TAG_imported_unit
8602 || pdi->tag == DW_TAG_inlined_subroutine)
8603 {
8604 switch (pdi->tag)
8605 {
8606 case DW_TAG_subprogram:
8607 case DW_TAG_inlined_subroutine:
8608 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8609 break;
8610 case DW_TAG_constant:
8611 case DW_TAG_variable:
8612 case DW_TAG_typedef:
8613 case DW_TAG_union_type:
8614 if (!pdi->is_declaration)
8615 {
8616 add_partial_symbol (pdi, cu);
8617 }
8618 break;
8619 case DW_TAG_class_type:
8620 case DW_TAG_interface_type:
8621 case DW_TAG_structure_type:
8622 if (!pdi->is_declaration)
8623 {
8624 add_partial_symbol (pdi, cu);
8625 }
8626 if ((cu->language == language_rust
8627 || cu->language == language_cplus) && pdi->has_children)
8628 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8629 set_addrmap, cu);
8630 break;
8631 case DW_TAG_enumeration_type:
8632 if (!pdi->is_declaration)
8633 add_partial_enumeration (pdi, cu);
8634 break;
8635 case DW_TAG_base_type:
8636 case DW_TAG_subrange_type:
8637 /* File scope base type definitions are added to the partial
8638 symbol table. */
8639 add_partial_symbol (pdi, cu);
8640 break;
8641 case DW_TAG_namespace:
8642 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8643 break;
8644 case DW_TAG_module:
8645 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8646 break;
8647 case DW_TAG_imported_unit:
8648 {
8649 struct dwarf2_per_cu_data *per_cu;
8650
8651 /* For now we don't handle imported units in type units. */
8652 if (cu->per_cu->is_debug_types)
8653 {
8654 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8655 " supported in type units [in module %s]"),
8656 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8657 }
8658
8659 per_cu = dwarf2_find_containing_comp_unit
8660 (pdi->d.sect_off, pdi->is_dwz,
8661 cu->per_cu->dwarf2_per_objfile);
8662
8663 /* Go read the partial unit, if needed. */
8664 if (per_cu->v.psymtab == NULL)
8665 process_psymtab_comp_unit (per_cu, 1, cu->language);
8666
8667 VEC_safe_push (dwarf2_per_cu_ptr,
8668 cu->per_cu->imported_symtabs, per_cu);
8669 }
8670 break;
8671 case DW_TAG_imported_declaration:
8672 add_partial_symbol (pdi, cu);
8673 break;
8674 default:
8675 break;
8676 }
8677 }
8678
8679 /* If the die has a sibling, skip to the sibling. */
8680
8681 pdi = pdi->die_sibling;
8682 }
8683 }
8684
8685 /* Functions used to compute the fully scoped name of a partial DIE.
8686
8687 Normally, this is simple. For C++, the parent DIE's fully scoped
8688 name is concatenated with "::" and the partial DIE's name.
8689 Enumerators are an exception; they use the scope of their parent
8690 enumeration type, i.e. the name of the enumeration type is not
8691 prepended to the enumerator.
8692
8693 There are two complexities. One is DW_AT_specification; in this
8694 case "parent" means the parent of the target of the specification,
8695 instead of the direct parent of the DIE. The other is compilers
8696 which do not emit DW_TAG_namespace; in this case we try to guess
8697 the fully qualified name of structure types from their members'
8698 linkage names. This must be done using the DIE's children rather
8699 than the children of any DW_AT_specification target. We only need
8700 to do this for structures at the top level, i.e. if the target of
8701 any DW_AT_specification (if any; otherwise the DIE itself) does not
8702 have a parent. */
8703
8704 /* Compute the scope prefix associated with PDI's parent, in
8705 compilation unit CU. The result will be allocated on CU's
8706 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8707 field. NULL is returned if no prefix is necessary. */
8708 static const char *
8709 partial_die_parent_scope (struct partial_die_info *pdi,
8710 struct dwarf2_cu *cu)
8711 {
8712 const char *grandparent_scope;
8713 struct partial_die_info *parent, *real_pdi;
8714
8715 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8716 then this means the parent of the specification DIE. */
8717
8718 real_pdi = pdi;
8719 while (real_pdi->has_specification)
8720 real_pdi = find_partial_die (real_pdi->spec_offset,
8721 real_pdi->spec_is_dwz, cu);
8722
8723 parent = real_pdi->die_parent;
8724 if (parent == NULL)
8725 return NULL;
8726
8727 if (parent->scope_set)
8728 return parent->scope;
8729
8730 parent->fixup (cu);
8731
8732 grandparent_scope = partial_die_parent_scope (parent, cu);
8733
8734 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8735 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8736 Work around this problem here. */
8737 if (cu->language == language_cplus
8738 && parent->tag == DW_TAG_namespace
8739 && strcmp (parent->name, "::") == 0
8740 && grandparent_scope == NULL)
8741 {
8742 parent->scope = NULL;
8743 parent->scope_set = 1;
8744 return NULL;
8745 }
8746
8747 if (pdi->tag == DW_TAG_enumerator)
8748 /* Enumerators should not get the name of the enumeration as a prefix. */
8749 parent->scope = grandparent_scope;
8750 else if (parent->tag == DW_TAG_namespace
8751 || parent->tag == DW_TAG_module
8752 || parent->tag == DW_TAG_structure_type
8753 || parent->tag == DW_TAG_class_type
8754 || parent->tag == DW_TAG_interface_type
8755 || parent->tag == DW_TAG_union_type
8756 || parent->tag == DW_TAG_enumeration_type)
8757 {
8758 if (grandparent_scope == NULL)
8759 parent->scope = parent->name;
8760 else
8761 parent->scope = typename_concat (&cu->comp_unit_obstack,
8762 grandparent_scope,
8763 parent->name, 0, cu);
8764 }
8765 else
8766 {
8767 /* FIXME drow/2004-04-01: What should we be doing with
8768 function-local names? For partial symbols, we should probably be
8769 ignoring them. */
8770 complaint (&symfile_complaints,
8771 _("unhandled containing DIE tag %d for DIE at %s"),
8772 parent->tag, sect_offset_str (pdi->sect_off));
8773 parent->scope = grandparent_scope;
8774 }
8775
8776 parent->scope_set = 1;
8777 return parent->scope;
8778 }
8779
8780 /* Return the fully scoped name associated with PDI, from compilation unit
8781 CU. The result will be allocated with malloc. */
8782
8783 static char *
8784 partial_die_full_name (struct partial_die_info *pdi,
8785 struct dwarf2_cu *cu)
8786 {
8787 const char *parent_scope;
8788
8789 /* If this is a template instantiation, we can not work out the
8790 template arguments from partial DIEs. So, unfortunately, we have
8791 to go through the full DIEs. At least any work we do building
8792 types here will be reused if full symbols are loaded later. */
8793 if (pdi->has_template_arguments)
8794 {
8795 pdi->fixup (cu);
8796
8797 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8798 {
8799 struct die_info *die;
8800 struct attribute attr;
8801 struct dwarf2_cu *ref_cu = cu;
8802
8803 /* DW_FORM_ref_addr is using section offset. */
8804 attr.name = (enum dwarf_attribute) 0;
8805 attr.form = DW_FORM_ref_addr;
8806 attr.u.unsnd = to_underlying (pdi->sect_off);
8807 die = follow_die_ref (NULL, &attr, &ref_cu);
8808
8809 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8810 }
8811 }
8812
8813 parent_scope = partial_die_parent_scope (pdi, cu);
8814 if (parent_scope == NULL)
8815 return NULL;
8816 else
8817 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
8818 }
8819
8820 static void
8821 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8822 {
8823 struct dwarf2_per_objfile *dwarf2_per_objfile
8824 = cu->per_cu->dwarf2_per_objfile;
8825 struct objfile *objfile = dwarf2_per_objfile->objfile;
8826 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8827 CORE_ADDR addr = 0;
8828 const char *actual_name = NULL;
8829 CORE_ADDR baseaddr;
8830 char *built_actual_name;
8831
8832 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8833
8834 built_actual_name = partial_die_full_name (pdi, cu);
8835 if (built_actual_name != NULL)
8836 actual_name = built_actual_name;
8837
8838 if (actual_name == NULL)
8839 actual_name = pdi->name;
8840
8841 switch (pdi->tag)
8842 {
8843 case DW_TAG_inlined_subroutine:
8844 case DW_TAG_subprogram:
8845 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
8846 if (pdi->is_external || cu->language == language_ada)
8847 {
8848 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8849 of the global scope. But in Ada, we want to be able to access
8850 nested procedures globally. So all Ada subprograms are stored
8851 in the global scope. */
8852 add_psymbol_to_list (actual_name, strlen (actual_name),
8853 built_actual_name != NULL,
8854 VAR_DOMAIN, LOC_BLOCK,
8855 &objfile->global_psymbols,
8856 addr, cu->language, objfile);
8857 }
8858 else
8859 {
8860 add_psymbol_to_list (actual_name, strlen (actual_name),
8861 built_actual_name != NULL,
8862 VAR_DOMAIN, LOC_BLOCK,
8863 &objfile->static_psymbols,
8864 addr, cu->language, objfile);
8865 }
8866
8867 if (pdi->main_subprogram && actual_name != NULL)
8868 set_objfile_main_name (objfile, actual_name, cu->language);
8869 break;
8870 case DW_TAG_constant:
8871 {
8872 std::vector<partial_symbol *> *list;
8873
8874 if (pdi->is_external)
8875 list = &objfile->global_psymbols;
8876 else
8877 list = &objfile->static_psymbols;
8878 add_psymbol_to_list (actual_name, strlen (actual_name),
8879 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8880 list, 0, cu->language, objfile);
8881 }
8882 break;
8883 case DW_TAG_variable:
8884 if (pdi->d.locdesc)
8885 addr = decode_locdesc (pdi->d.locdesc, cu);
8886
8887 if (pdi->d.locdesc
8888 && addr == 0
8889 && !dwarf2_per_objfile->has_section_at_zero)
8890 {
8891 /* A global or static variable may also have been stripped
8892 out by the linker if unused, in which case its address
8893 will be nullified; do not add such variables into partial
8894 symbol table then. */
8895 }
8896 else if (pdi->is_external)
8897 {
8898 /* Global Variable.
8899 Don't enter into the minimal symbol tables as there is
8900 a minimal symbol table entry from the ELF symbols already.
8901 Enter into partial symbol table if it has a location
8902 descriptor or a type.
8903 If the location descriptor is missing, new_symbol will create
8904 a LOC_UNRESOLVED symbol, the address of the variable will then
8905 be determined from the minimal symbol table whenever the variable
8906 is referenced.
8907 The address for the partial symbol table entry is not
8908 used by GDB, but it comes in handy for debugging partial symbol
8909 table building. */
8910
8911 if (pdi->d.locdesc || pdi->has_type)
8912 add_psymbol_to_list (actual_name, strlen (actual_name),
8913 built_actual_name != NULL,
8914 VAR_DOMAIN, LOC_STATIC,
8915 &objfile->global_psymbols,
8916 addr + baseaddr,
8917 cu->language, objfile);
8918 }
8919 else
8920 {
8921 int has_loc = pdi->d.locdesc != NULL;
8922
8923 /* Static Variable. Skip symbols whose value we cannot know (those
8924 without location descriptors or constant values). */
8925 if (!has_loc && !pdi->has_const_value)
8926 {
8927 xfree (built_actual_name);
8928 return;
8929 }
8930
8931 add_psymbol_to_list (actual_name, strlen (actual_name),
8932 built_actual_name != NULL,
8933 VAR_DOMAIN, LOC_STATIC,
8934 &objfile->static_psymbols,
8935 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
8936 cu->language, objfile);
8937 }
8938 break;
8939 case DW_TAG_typedef:
8940 case DW_TAG_base_type:
8941 case DW_TAG_subrange_type:
8942 add_psymbol_to_list (actual_name, strlen (actual_name),
8943 built_actual_name != NULL,
8944 VAR_DOMAIN, LOC_TYPEDEF,
8945 &objfile->static_psymbols,
8946 0, cu->language, objfile);
8947 break;
8948 case DW_TAG_imported_declaration:
8949 case DW_TAG_namespace:
8950 add_psymbol_to_list (actual_name, strlen (actual_name),
8951 built_actual_name != NULL,
8952 VAR_DOMAIN, LOC_TYPEDEF,
8953 &objfile->global_psymbols,
8954 0, cu->language, objfile);
8955 break;
8956 case DW_TAG_module:
8957 add_psymbol_to_list (actual_name, strlen (actual_name),
8958 built_actual_name != NULL,
8959 MODULE_DOMAIN, LOC_TYPEDEF,
8960 &objfile->global_psymbols,
8961 0, cu->language, objfile);
8962 break;
8963 case DW_TAG_class_type:
8964 case DW_TAG_interface_type:
8965 case DW_TAG_structure_type:
8966 case DW_TAG_union_type:
8967 case DW_TAG_enumeration_type:
8968 /* Skip external references. The DWARF standard says in the section
8969 about "Structure, Union, and Class Type Entries": "An incomplete
8970 structure, union or class type is represented by a structure,
8971 union or class entry that does not have a byte size attribute
8972 and that has a DW_AT_declaration attribute." */
8973 if (!pdi->has_byte_size && pdi->is_declaration)
8974 {
8975 xfree (built_actual_name);
8976 return;
8977 }
8978
8979 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8980 static vs. global. */
8981 add_psymbol_to_list (actual_name, strlen (actual_name),
8982 built_actual_name != NULL,
8983 STRUCT_DOMAIN, LOC_TYPEDEF,
8984 cu->language == language_cplus
8985 ? &objfile->global_psymbols
8986 : &objfile->static_psymbols,
8987 0, cu->language, objfile);
8988
8989 break;
8990 case DW_TAG_enumerator:
8991 add_psymbol_to_list (actual_name, strlen (actual_name),
8992 built_actual_name != NULL,
8993 VAR_DOMAIN, LOC_CONST,
8994 cu->language == language_cplus
8995 ? &objfile->global_psymbols
8996 : &objfile->static_psymbols,
8997 0, cu->language, objfile);
8998 break;
8999 default:
9000 break;
9001 }
9002
9003 xfree (built_actual_name);
9004 }
9005
9006 /* Read a partial die corresponding to a namespace; also, add a symbol
9007 corresponding to that namespace to the symbol table. NAMESPACE is
9008 the name of the enclosing namespace. */
9009
9010 static void
9011 add_partial_namespace (struct partial_die_info *pdi,
9012 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9013 int set_addrmap, struct dwarf2_cu *cu)
9014 {
9015 /* Add a symbol for the namespace. */
9016
9017 add_partial_symbol (pdi, cu);
9018
9019 /* Now scan partial symbols in that namespace. */
9020
9021 if (pdi->has_children)
9022 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9023 }
9024
9025 /* Read a partial die corresponding to a Fortran module. */
9026
9027 static void
9028 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9029 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9030 {
9031 /* Add a symbol for the namespace. */
9032
9033 add_partial_symbol (pdi, cu);
9034
9035 /* Now scan partial symbols in that module. */
9036
9037 if (pdi->has_children)
9038 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9039 }
9040
9041 /* Read a partial die corresponding to a subprogram or an inlined
9042 subprogram and create a partial symbol for that subprogram.
9043 When the CU language allows it, this routine also defines a partial
9044 symbol for each nested subprogram that this subprogram contains.
9045 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9046 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9047
9048 PDI may also be a lexical block, in which case we simply search
9049 recursively for subprograms defined inside that lexical block.
9050 Again, this is only performed when the CU language allows this
9051 type of definitions. */
9052
9053 static void
9054 add_partial_subprogram (struct partial_die_info *pdi,
9055 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9056 int set_addrmap, struct dwarf2_cu *cu)
9057 {
9058 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9059 {
9060 if (pdi->has_pc_info)
9061 {
9062 if (pdi->lowpc < *lowpc)
9063 *lowpc = pdi->lowpc;
9064 if (pdi->highpc > *highpc)
9065 *highpc = pdi->highpc;
9066 if (set_addrmap)
9067 {
9068 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9069 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9070 CORE_ADDR baseaddr;
9071 CORE_ADDR highpc;
9072 CORE_ADDR lowpc;
9073
9074 baseaddr = ANOFFSET (objfile->section_offsets,
9075 SECT_OFF_TEXT (objfile));
9076 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9077 pdi->lowpc + baseaddr);
9078 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9079 pdi->highpc + baseaddr);
9080 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9081 cu->per_cu->v.psymtab);
9082 }
9083 }
9084
9085 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9086 {
9087 if (!pdi->is_declaration)
9088 /* Ignore subprogram DIEs that do not have a name, they are
9089 illegal. Do not emit a complaint at this point, we will
9090 do so when we convert this psymtab into a symtab. */
9091 if (pdi->name)
9092 add_partial_symbol (pdi, cu);
9093 }
9094 }
9095
9096 if (! pdi->has_children)
9097 return;
9098
9099 if (cu->language == language_ada)
9100 {
9101 pdi = pdi->die_child;
9102 while (pdi != NULL)
9103 {
9104 pdi->fixup (cu);
9105 if (pdi->tag == DW_TAG_subprogram
9106 || pdi->tag == DW_TAG_inlined_subroutine
9107 || pdi->tag == DW_TAG_lexical_block)
9108 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9109 pdi = pdi->die_sibling;
9110 }
9111 }
9112 }
9113
9114 /* Read a partial die corresponding to an enumeration type. */
9115
9116 static void
9117 add_partial_enumeration (struct partial_die_info *enum_pdi,
9118 struct dwarf2_cu *cu)
9119 {
9120 struct partial_die_info *pdi;
9121
9122 if (enum_pdi->name != NULL)
9123 add_partial_symbol (enum_pdi, cu);
9124
9125 pdi = enum_pdi->die_child;
9126 while (pdi)
9127 {
9128 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9129 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
9130 else
9131 add_partial_symbol (pdi, cu);
9132 pdi = pdi->die_sibling;
9133 }
9134 }
9135
9136 /* Return the initial uleb128 in the die at INFO_PTR. */
9137
9138 static unsigned int
9139 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9140 {
9141 unsigned int bytes_read;
9142
9143 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9144 }
9145
9146 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9147 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9148
9149 Return the corresponding abbrev, or NULL if the number is zero (indicating
9150 an empty DIE). In either case *BYTES_READ will be set to the length of
9151 the initial number. */
9152
9153 static struct abbrev_info *
9154 peek_die_abbrev (const die_reader_specs &reader,
9155 const gdb_byte *info_ptr, unsigned int *bytes_read)
9156 {
9157 dwarf2_cu *cu = reader.cu;
9158 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9159 unsigned int abbrev_number
9160 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9161
9162 if (abbrev_number == 0)
9163 return NULL;
9164
9165 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9166 if (!abbrev)
9167 {
9168 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9169 " at offset %s [in module %s]"),
9170 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9171 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9172 }
9173
9174 return abbrev;
9175 }
9176
9177 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9178 Returns a pointer to the end of a series of DIEs, terminated by an empty
9179 DIE. Any children of the skipped DIEs will also be skipped. */
9180
9181 static const gdb_byte *
9182 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9183 {
9184 while (1)
9185 {
9186 unsigned int bytes_read;
9187 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9188
9189 if (abbrev == NULL)
9190 return info_ptr + bytes_read;
9191 else
9192 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9193 }
9194 }
9195
9196 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9197 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9198 abbrev corresponding to that skipped uleb128 should be passed in
9199 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9200 children. */
9201
9202 static const gdb_byte *
9203 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9204 struct abbrev_info *abbrev)
9205 {
9206 unsigned int bytes_read;
9207 struct attribute attr;
9208 bfd *abfd = reader->abfd;
9209 struct dwarf2_cu *cu = reader->cu;
9210 const gdb_byte *buffer = reader->buffer;
9211 const gdb_byte *buffer_end = reader->buffer_end;
9212 unsigned int form, i;
9213
9214 for (i = 0; i < abbrev->num_attrs; i++)
9215 {
9216 /* The only abbrev we care about is DW_AT_sibling. */
9217 if (abbrev->attrs[i].name == DW_AT_sibling)
9218 {
9219 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9220 if (attr.form == DW_FORM_ref_addr)
9221 complaint (&symfile_complaints,
9222 _("ignoring absolute DW_AT_sibling"));
9223 else
9224 {
9225 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9226 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9227
9228 if (sibling_ptr < info_ptr)
9229 complaint (&symfile_complaints,
9230 _("DW_AT_sibling points backwards"));
9231 else if (sibling_ptr > reader->buffer_end)
9232 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9233 else
9234 return sibling_ptr;
9235 }
9236 }
9237
9238 /* If it isn't DW_AT_sibling, skip this attribute. */
9239 form = abbrev->attrs[i].form;
9240 skip_attribute:
9241 switch (form)
9242 {
9243 case DW_FORM_ref_addr:
9244 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9245 and later it is offset sized. */
9246 if (cu->header.version == 2)
9247 info_ptr += cu->header.addr_size;
9248 else
9249 info_ptr += cu->header.offset_size;
9250 break;
9251 case DW_FORM_GNU_ref_alt:
9252 info_ptr += cu->header.offset_size;
9253 break;
9254 case DW_FORM_addr:
9255 info_ptr += cu->header.addr_size;
9256 break;
9257 case DW_FORM_data1:
9258 case DW_FORM_ref1:
9259 case DW_FORM_flag:
9260 info_ptr += 1;
9261 break;
9262 case DW_FORM_flag_present:
9263 case DW_FORM_implicit_const:
9264 break;
9265 case DW_FORM_data2:
9266 case DW_FORM_ref2:
9267 info_ptr += 2;
9268 break;
9269 case DW_FORM_data4:
9270 case DW_FORM_ref4:
9271 info_ptr += 4;
9272 break;
9273 case DW_FORM_data8:
9274 case DW_FORM_ref8:
9275 case DW_FORM_ref_sig8:
9276 info_ptr += 8;
9277 break;
9278 case DW_FORM_data16:
9279 info_ptr += 16;
9280 break;
9281 case DW_FORM_string:
9282 read_direct_string (abfd, info_ptr, &bytes_read);
9283 info_ptr += bytes_read;
9284 break;
9285 case DW_FORM_sec_offset:
9286 case DW_FORM_strp:
9287 case DW_FORM_GNU_strp_alt:
9288 info_ptr += cu->header.offset_size;
9289 break;
9290 case DW_FORM_exprloc:
9291 case DW_FORM_block:
9292 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9293 info_ptr += bytes_read;
9294 break;
9295 case DW_FORM_block1:
9296 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9297 break;
9298 case DW_FORM_block2:
9299 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9300 break;
9301 case DW_FORM_block4:
9302 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9303 break;
9304 case DW_FORM_sdata:
9305 case DW_FORM_udata:
9306 case DW_FORM_ref_udata:
9307 case DW_FORM_GNU_addr_index:
9308 case DW_FORM_GNU_str_index:
9309 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9310 break;
9311 case DW_FORM_indirect:
9312 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9313 info_ptr += bytes_read;
9314 /* We need to continue parsing from here, so just go back to
9315 the top. */
9316 goto skip_attribute;
9317
9318 default:
9319 error (_("Dwarf Error: Cannot handle %s "
9320 "in DWARF reader [in module %s]"),
9321 dwarf_form_name (form),
9322 bfd_get_filename (abfd));
9323 }
9324 }
9325
9326 if (abbrev->has_children)
9327 return skip_children (reader, info_ptr);
9328 else
9329 return info_ptr;
9330 }
9331
9332 /* Locate ORIG_PDI's sibling.
9333 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9334
9335 static const gdb_byte *
9336 locate_pdi_sibling (const struct die_reader_specs *reader,
9337 struct partial_die_info *orig_pdi,
9338 const gdb_byte *info_ptr)
9339 {
9340 /* Do we know the sibling already? */
9341
9342 if (orig_pdi->sibling)
9343 return orig_pdi->sibling;
9344
9345 /* Are there any children to deal with? */
9346
9347 if (!orig_pdi->has_children)
9348 return info_ptr;
9349
9350 /* Skip the children the long way. */
9351
9352 return skip_children (reader, info_ptr);
9353 }
9354
9355 /* Expand this partial symbol table into a full symbol table. SELF is
9356 not NULL. */
9357
9358 static void
9359 dwarf2_read_symtab (struct partial_symtab *self,
9360 struct objfile *objfile)
9361 {
9362 struct dwarf2_per_objfile *dwarf2_per_objfile
9363 = get_dwarf2_per_objfile (objfile);
9364
9365 if (self->readin)
9366 {
9367 warning (_("bug: psymtab for %s is already read in."),
9368 self->filename);
9369 }
9370 else
9371 {
9372 if (info_verbose)
9373 {
9374 printf_filtered (_("Reading in symbols for %s..."),
9375 self->filename);
9376 gdb_flush (gdb_stdout);
9377 }
9378
9379 /* If this psymtab is constructed from a debug-only objfile, the
9380 has_section_at_zero flag will not necessarily be correct. We
9381 can get the correct value for this flag by looking at the data
9382 associated with the (presumably stripped) associated objfile. */
9383 if (objfile->separate_debug_objfile_backlink)
9384 {
9385 struct dwarf2_per_objfile *dpo_backlink
9386 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9387
9388 dwarf2_per_objfile->has_section_at_zero
9389 = dpo_backlink->has_section_at_zero;
9390 }
9391
9392 dwarf2_per_objfile->reading_partial_symbols = 0;
9393
9394 psymtab_to_symtab_1 (self);
9395
9396 /* Finish up the debug error message. */
9397 if (info_verbose)
9398 printf_filtered (_("done.\n"));
9399 }
9400
9401 process_cu_includes (dwarf2_per_objfile);
9402 }
9403 \f
9404 /* Reading in full CUs. */
9405
9406 /* Add PER_CU to the queue. */
9407
9408 static void
9409 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9410 enum language pretend_language)
9411 {
9412 struct dwarf2_queue_item *item;
9413
9414 per_cu->queued = 1;
9415 item = XNEW (struct dwarf2_queue_item);
9416 item->per_cu = per_cu;
9417 item->pretend_language = pretend_language;
9418 item->next = NULL;
9419
9420 if (dwarf2_queue == NULL)
9421 dwarf2_queue = item;
9422 else
9423 dwarf2_queue_tail->next = item;
9424
9425 dwarf2_queue_tail = item;
9426 }
9427
9428 /* If PER_CU is not yet queued, add it to the queue.
9429 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9430 dependency.
9431 The result is non-zero if PER_CU was queued, otherwise the result is zero
9432 meaning either PER_CU is already queued or it is already loaded.
9433
9434 N.B. There is an invariant here that if a CU is queued then it is loaded.
9435 The caller is required to load PER_CU if we return non-zero. */
9436
9437 static int
9438 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9439 struct dwarf2_per_cu_data *per_cu,
9440 enum language pretend_language)
9441 {
9442 /* We may arrive here during partial symbol reading, if we need full
9443 DIEs to process an unusual case (e.g. template arguments). Do
9444 not queue PER_CU, just tell our caller to load its DIEs. */
9445 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9446 {
9447 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9448 return 1;
9449 return 0;
9450 }
9451
9452 /* Mark the dependence relation so that we don't flush PER_CU
9453 too early. */
9454 if (dependent_cu != NULL)
9455 dwarf2_add_dependence (dependent_cu, per_cu);
9456
9457 /* If it's already on the queue, we have nothing to do. */
9458 if (per_cu->queued)
9459 return 0;
9460
9461 /* If the compilation unit is already loaded, just mark it as
9462 used. */
9463 if (per_cu->cu != NULL)
9464 {
9465 per_cu->cu->last_used = 0;
9466 return 0;
9467 }
9468
9469 /* Add it to the queue. */
9470 queue_comp_unit (per_cu, pretend_language);
9471
9472 return 1;
9473 }
9474
9475 /* Process the queue. */
9476
9477 static void
9478 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9479 {
9480 struct dwarf2_queue_item *item, *next_item;
9481
9482 if (dwarf_read_debug)
9483 {
9484 fprintf_unfiltered (gdb_stdlog,
9485 "Expanding one or more symtabs of objfile %s ...\n",
9486 objfile_name (dwarf2_per_objfile->objfile));
9487 }
9488
9489 /* The queue starts out with one item, but following a DIE reference
9490 may load a new CU, adding it to the end of the queue. */
9491 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9492 {
9493 if ((dwarf2_per_objfile->using_index
9494 ? !item->per_cu->v.quick->compunit_symtab
9495 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9496 /* Skip dummy CUs. */
9497 && item->per_cu->cu != NULL)
9498 {
9499 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9500 unsigned int debug_print_threshold;
9501 char buf[100];
9502
9503 if (per_cu->is_debug_types)
9504 {
9505 struct signatured_type *sig_type =
9506 (struct signatured_type *) per_cu;
9507
9508 sprintf (buf, "TU %s at offset %s",
9509 hex_string (sig_type->signature),
9510 sect_offset_str (per_cu->sect_off));
9511 /* There can be 100s of TUs.
9512 Only print them in verbose mode. */
9513 debug_print_threshold = 2;
9514 }
9515 else
9516 {
9517 sprintf (buf, "CU at offset %s",
9518 sect_offset_str (per_cu->sect_off));
9519 debug_print_threshold = 1;
9520 }
9521
9522 if (dwarf_read_debug >= debug_print_threshold)
9523 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9524
9525 if (per_cu->is_debug_types)
9526 process_full_type_unit (per_cu, item->pretend_language);
9527 else
9528 process_full_comp_unit (per_cu, item->pretend_language);
9529
9530 if (dwarf_read_debug >= debug_print_threshold)
9531 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9532 }
9533
9534 item->per_cu->queued = 0;
9535 next_item = item->next;
9536 xfree (item);
9537 }
9538
9539 dwarf2_queue_tail = NULL;
9540
9541 if (dwarf_read_debug)
9542 {
9543 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9544 objfile_name (dwarf2_per_objfile->objfile));
9545 }
9546 }
9547
9548 /* Read in full symbols for PST, and anything it depends on. */
9549
9550 static void
9551 psymtab_to_symtab_1 (struct partial_symtab *pst)
9552 {
9553 struct dwarf2_per_cu_data *per_cu;
9554 int i;
9555
9556 if (pst->readin)
9557 return;
9558
9559 for (i = 0; i < pst->number_of_dependencies; i++)
9560 if (!pst->dependencies[i]->readin
9561 && pst->dependencies[i]->user == NULL)
9562 {
9563 /* Inform about additional files that need to be read in. */
9564 if (info_verbose)
9565 {
9566 /* FIXME: i18n: Need to make this a single string. */
9567 fputs_filtered (" ", gdb_stdout);
9568 wrap_here ("");
9569 fputs_filtered ("and ", gdb_stdout);
9570 wrap_here ("");
9571 printf_filtered ("%s...", pst->dependencies[i]->filename);
9572 wrap_here (""); /* Flush output. */
9573 gdb_flush (gdb_stdout);
9574 }
9575 psymtab_to_symtab_1 (pst->dependencies[i]);
9576 }
9577
9578 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9579
9580 if (per_cu == NULL)
9581 {
9582 /* It's an include file, no symbols to read for it.
9583 Everything is in the parent symtab. */
9584 pst->readin = 1;
9585 return;
9586 }
9587
9588 dw2_do_instantiate_symtab (per_cu);
9589 }
9590
9591 /* Trivial hash function for die_info: the hash value of a DIE
9592 is its offset in .debug_info for this objfile. */
9593
9594 static hashval_t
9595 die_hash (const void *item)
9596 {
9597 const struct die_info *die = (const struct die_info *) item;
9598
9599 return to_underlying (die->sect_off);
9600 }
9601
9602 /* Trivial comparison function for die_info structures: two DIEs
9603 are equal if they have the same offset. */
9604
9605 static int
9606 die_eq (const void *item_lhs, const void *item_rhs)
9607 {
9608 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9609 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9610
9611 return die_lhs->sect_off == die_rhs->sect_off;
9612 }
9613
9614 /* die_reader_func for load_full_comp_unit.
9615 This is identical to read_signatured_type_reader,
9616 but is kept separate for now. */
9617
9618 static void
9619 load_full_comp_unit_reader (const struct die_reader_specs *reader,
9620 const gdb_byte *info_ptr,
9621 struct die_info *comp_unit_die,
9622 int has_children,
9623 void *data)
9624 {
9625 struct dwarf2_cu *cu = reader->cu;
9626 enum language *language_ptr = (enum language *) data;
9627
9628 gdb_assert (cu->die_hash == NULL);
9629 cu->die_hash =
9630 htab_create_alloc_ex (cu->header.length / 12,
9631 die_hash,
9632 die_eq,
9633 NULL,
9634 &cu->comp_unit_obstack,
9635 hashtab_obstack_allocate,
9636 dummy_obstack_deallocate);
9637
9638 if (has_children)
9639 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9640 &info_ptr, comp_unit_die);
9641 cu->dies = comp_unit_die;
9642 /* comp_unit_die is not stored in die_hash, no need. */
9643
9644 /* We try not to read any attributes in this function, because not
9645 all CUs needed for references have been loaded yet, and symbol
9646 table processing isn't initialized. But we have to set the CU language,
9647 or we won't be able to build types correctly.
9648 Similarly, if we do not read the producer, we can not apply
9649 producer-specific interpretation. */
9650 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
9651 }
9652
9653 /* Load the DIEs associated with PER_CU into memory. */
9654
9655 static void
9656 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9657 enum language pretend_language)
9658 {
9659 gdb_assert (! this_cu->is_debug_types);
9660
9661 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
9662 load_full_comp_unit_reader, &pretend_language);
9663 }
9664
9665 /* Add a DIE to the delayed physname list. */
9666
9667 static void
9668 add_to_method_list (struct type *type, int fnfield_index, int index,
9669 const char *name, struct die_info *die,
9670 struct dwarf2_cu *cu)
9671 {
9672 struct delayed_method_info mi;
9673 mi.type = type;
9674 mi.fnfield_index = fnfield_index;
9675 mi.index = index;
9676 mi.name = name;
9677 mi.die = die;
9678 cu->method_list.push_back (mi);
9679 }
9680
9681 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9682 "const" / "volatile". If so, decrements LEN by the length of the
9683 modifier and return true. Otherwise return false. */
9684
9685 template<size_t N>
9686 static bool
9687 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9688 {
9689 size_t mod_len = sizeof (mod) - 1;
9690 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9691 {
9692 len -= mod_len;
9693 return true;
9694 }
9695 return false;
9696 }
9697
9698 /* Compute the physnames of any methods on the CU's method list.
9699
9700 The computation of method physnames is delayed in order to avoid the
9701 (bad) condition that one of the method's formal parameters is of an as yet
9702 incomplete type. */
9703
9704 static void
9705 compute_delayed_physnames (struct dwarf2_cu *cu)
9706 {
9707 /* Only C++ delays computing physnames. */
9708 if (cu->method_list.empty ())
9709 return;
9710 gdb_assert (cu->language == language_cplus);
9711
9712 for (struct delayed_method_info &mi : cu->method_list)
9713 {
9714 const char *physname;
9715 struct fn_fieldlist *fn_flp
9716 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9717 physname = dwarf2_physname (mi.name, mi.die, cu);
9718 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9719 = physname ? physname : "";
9720
9721 /* Since there's no tag to indicate whether a method is a
9722 const/volatile overload, extract that information out of the
9723 demangled name. */
9724 if (physname != NULL)
9725 {
9726 size_t len = strlen (physname);
9727
9728 while (1)
9729 {
9730 if (physname[len] == ')') /* shortcut */
9731 break;
9732 else if (check_modifier (physname, len, " const"))
9733 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9734 else if (check_modifier (physname, len, " volatile"))
9735 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9736 else
9737 break;
9738 }
9739 }
9740 }
9741
9742 /* The list is no longer needed. */
9743 cu->method_list.clear ();
9744 }
9745
9746 /* Go objects should be embedded in a DW_TAG_module DIE,
9747 and it's not clear if/how imported objects will appear.
9748 To keep Go support simple until that's worked out,
9749 go back through what we've read and create something usable.
9750 We could do this while processing each DIE, and feels kinda cleaner,
9751 but that way is more invasive.
9752 This is to, for example, allow the user to type "p var" or "b main"
9753 without having to specify the package name, and allow lookups
9754 of module.object to work in contexts that use the expression
9755 parser. */
9756
9757 static void
9758 fixup_go_packaging (struct dwarf2_cu *cu)
9759 {
9760 char *package_name = NULL;
9761 struct pending *list;
9762 int i;
9763
9764 for (list = global_symbols; list != NULL; list = list->next)
9765 {
9766 for (i = 0; i < list->nsyms; ++i)
9767 {
9768 struct symbol *sym = list->symbol[i];
9769
9770 if (SYMBOL_LANGUAGE (sym) == language_go
9771 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9772 {
9773 char *this_package_name = go_symbol_package_name (sym);
9774
9775 if (this_package_name == NULL)
9776 continue;
9777 if (package_name == NULL)
9778 package_name = this_package_name;
9779 else
9780 {
9781 struct objfile *objfile
9782 = cu->per_cu->dwarf2_per_objfile->objfile;
9783 if (strcmp (package_name, this_package_name) != 0)
9784 complaint (&symfile_complaints,
9785 _("Symtab %s has objects from two different Go packages: %s and %s"),
9786 (symbol_symtab (sym) != NULL
9787 ? symtab_to_filename_for_display
9788 (symbol_symtab (sym))
9789 : objfile_name (objfile)),
9790 this_package_name, package_name);
9791 xfree (this_package_name);
9792 }
9793 }
9794 }
9795 }
9796
9797 if (package_name != NULL)
9798 {
9799 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9800 const char *saved_package_name
9801 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
9802 package_name,
9803 strlen (package_name));
9804 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9805 saved_package_name);
9806 struct symbol *sym;
9807
9808 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9809
9810 sym = allocate_symbol (objfile);
9811 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
9812 SYMBOL_SET_NAMES (sym, saved_package_name,
9813 strlen (saved_package_name), 0, objfile);
9814 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9815 e.g., "main" finds the "main" module and not C's main(). */
9816 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9817 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9818 SYMBOL_TYPE (sym) = type;
9819
9820 add_symbol_to_list (sym, &global_symbols);
9821
9822 xfree (package_name);
9823 }
9824 }
9825
9826 /* Allocate a fully-qualified name consisting of the two parts on the
9827 obstack. */
9828
9829 static const char *
9830 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9831 {
9832 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9833 }
9834
9835 /* A helper that allocates a struct discriminant_info to attach to a
9836 union type. */
9837
9838 static struct discriminant_info *
9839 alloc_discriminant_info (struct type *type, int discriminant_index,
9840 int default_index)
9841 {
9842 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9843 gdb_assert (discriminant_index == -1
9844 || (discriminant_index >= 0
9845 && discriminant_index < TYPE_NFIELDS (type)));
9846 gdb_assert (default_index == -1
9847 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9848
9849 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9850
9851 struct discriminant_info *disc
9852 = ((struct discriminant_info *)
9853 TYPE_ZALLOC (type,
9854 offsetof (struct discriminant_info, discriminants)
9855 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9856 disc->default_index = default_index;
9857 disc->discriminant_index = discriminant_index;
9858
9859 struct dynamic_prop prop;
9860 prop.kind = PROP_UNDEFINED;
9861 prop.data.baton = disc;
9862
9863 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9864
9865 return disc;
9866 }
9867
9868 /* Some versions of rustc emitted enums in an unusual way.
9869
9870 Ordinary enums were emitted as unions. The first element of each
9871 structure in the union was named "RUST$ENUM$DISR". This element
9872 held the discriminant.
9873
9874 These versions of Rust also implemented the "non-zero"
9875 optimization. When the enum had two values, and one is empty and
9876 the other holds a pointer that cannot be zero, the pointer is used
9877 as the discriminant, with a zero value meaning the empty variant.
9878 Here, the union's first member is of the form
9879 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9880 where the fieldnos are the indices of the fields that should be
9881 traversed in order to find the field (which may be several fields deep)
9882 and the variantname is the name of the variant of the case when the
9883 field is zero.
9884
9885 This function recognizes whether TYPE is of one of these forms,
9886 and, if so, smashes it to be a variant type. */
9887
9888 static void
9889 quirk_rust_enum (struct type *type, struct objfile *objfile)
9890 {
9891 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9892
9893 /* We don't need to deal with empty enums. */
9894 if (TYPE_NFIELDS (type) == 0)
9895 return;
9896
9897 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9898 if (TYPE_NFIELDS (type) == 1
9899 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9900 {
9901 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9902
9903 /* Decode the field name to find the offset of the
9904 discriminant. */
9905 ULONGEST bit_offset = 0;
9906 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9907 while (name[0] >= '0' && name[0] <= '9')
9908 {
9909 char *tail;
9910 unsigned long index = strtoul (name, &tail, 10);
9911 name = tail;
9912 if (*name != '$'
9913 || index >= TYPE_NFIELDS (field_type)
9914 || (TYPE_FIELD_LOC_KIND (field_type, index)
9915 != FIELD_LOC_KIND_BITPOS))
9916 {
9917 complaint (&symfile_complaints,
9918 _("Could not parse Rust enum encoding string \"%s\""
9919 "[in module %s]"),
9920 TYPE_FIELD_NAME (type, 0),
9921 objfile_name (objfile));
9922 return;
9923 }
9924 ++name;
9925
9926 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9927 field_type = TYPE_FIELD_TYPE (field_type, index);
9928 }
9929
9930 /* Make a union to hold the variants. */
9931 struct type *union_type = alloc_type (objfile);
9932 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9933 TYPE_NFIELDS (union_type) = 3;
9934 TYPE_FIELDS (union_type)
9935 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9936 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9937
9938 /* Put the discriminant must at index 0. */
9939 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9940 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9941 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9942 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9943
9944 /* The order of fields doesn't really matter, so put the real
9945 field at index 1 and the data-less field at index 2. */
9946 struct discriminant_info *disc
9947 = alloc_discriminant_info (union_type, 0, 1);
9948 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9949 TYPE_FIELD_NAME (union_type, 1)
9950 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9951 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9952 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9953 TYPE_FIELD_NAME (union_type, 1));
9954
9955 const char *dataless_name
9956 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9957 name);
9958 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9959 dataless_name);
9960 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9961 /* NAME points into the original discriminant name, which
9962 already has the correct lifetime. */
9963 TYPE_FIELD_NAME (union_type, 2) = name;
9964 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9965 disc->discriminants[2] = 0;
9966
9967 /* Smash this type to be a structure type. We have to do this
9968 because the type has already been recorded. */
9969 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9970 TYPE_NFIELDS (type) = 1;
9971 TYPE_FIELDS (type)
9972 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9973
9974 /* Install the variant part. */
9975 TYPE_FIELD_TYPE (type, 0) = union_type;
9976 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9977 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9978 }
9979 else if (TYPE_NFIELDS (type) == 1)
9980 {
9981 /* We assume that a union with a single field is a univariant
9982 enum. */
9983 /* Smash this type to be a structure type. We have to do this
9984 because the type has already been recorded. */
9985 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9986
9987 /* Make a union to hold the variants. */
9988 struct type *union_type = alloc_type (objfile);
9989 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9990 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9991 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9992 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9993
9994 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9995 const char *variant_name
9996 = rust_last_path_segment (TYPE_NAME (field_type));
9997 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9998 TYPE_NAME (field_type)
9999 = rust_fully_qualify (&objfile->objfile_obstack,
10000 TYPE_NAME (type), variant_name);
10001
10002 /* Install the union in the outer struct type. */
10003 TYPE_NFIELDS (type) = 1;
10004 TYPE_FIELDS (type)
10005 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10006 TYPE_FIELD_TYPE (type, 0) = union_type;
10007 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10008 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10009
10010 alloc_discriminant_info (union_type, -1, 0);
10011 }
10012 else
10013 {
10014 struct type *disr_type = nullptr;
10015 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10016 {
10017 disr_type = TYPE_FIELD_TYPE (type, i);
10018
10019 if (TYPE_NFIELDS (disr_type) == 0)
10020 {
10021 /* Could be data-less variant, so keep going. */
10022 }
10023 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10024 "RUST$ENUM$DISR") != 0)
10025 {
10026 /* Not a Rust enum. */
10027 return;
10028 }
10029 else
10030 {
10031 /* Found one. */
10032 break;
10033 }
10034 }
10035
10036 /* If we got here without a discriminant, then it's probably
10037 just a union. */
10038 if (disr_type == nullptr)
10039 return;
10040
10041 /* Smash this type to be a structure type. We have to do this
10042 because the type has already been recorded. */
10043 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10044
10045 /* Make a union to hold the variants. */
10046 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10047 struct type *union_type = alloc_type (objfile);
10048 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10049 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10050 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10051 TYPE_FIELDS (union_type)
10052 = (struct field *) TYPE_ZALLOC (union_type,
10053 (TYPE_NFIELDS (union_type)
10054 * sizeof (struct field)));
10055
10056 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10057 TYPE_NFIELDS (type) * sizeof (struct field));
10058
10059 /* Install the discriminant at index 0 in the union. */
10060 TYPE_FIELD (union_type, 0) = *disr_field;
10061 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10062 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10063
10064 /* Install the union in the outer struct type. */
10065 TYPE_FIELD_TYPE (type, 0) = union_type;
10066 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10067 TYPE_NFIELDS (type) = 1;
10068
10069 /* Set the size and offset of the union type. */
10070 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10071
10072 /* We need a way to find the correct discriminant given a
10073 variant name. For convenience we build a map here. */
10074 struct type *enum_type = FIELD_TYPE (*disr_field);
10075 std::unordered_map<std::string, ULONGEST> discriminant_map;
10076 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10077 {
10078 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10079 {
10080 const char *name
10081 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10082 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10083 }
10084 }
10085
10086 int n_fields = TYPE_NFIELDS (union_type);
10087 struct discriminant_info *disc
10088 = alloc_discriminant_info (union_type, 0, -1);
10089 /* Skip the discriminant here. */
10090 for (int i = 1; i < n_fields; ++i)
10091 {
10092 /* Find the final word in the name of this variant's type.
10093 That name can be used to look up the correct
10094 discriminant. */
10095 const char *variant_name
10096 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10097 i)));
10098
10099 auto iter = discriminant_map.find (variant_name);
10100 if (iter != discriminant_map.end ())
10101 disc->discriminants[i] = iter->second;
10102
10103 /* Remove the discriminant field. */
10104 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10105 --TYPE_NFIELDS (sub_type);
10106 ++TYPE_FIELDS (sub_type);
10107 TYPE_FIELD_NAME (union_type, i) = variant_name;
10108 TYPE_NAME (sub_type)
10109 = rust_fully_qualify (&objfile->objfile_obstack,
10110 TYPE_NAME (type), variant_name);
10111 }
10112 }
10113 }
10114
10115 /* Rewrite some Rust unions to be structures with variants parts. */
10116
10117 static void
10118 rust_union_quirks (struct dwarf2_cu *cu)
10119 {
10120 gdb_assert (cu->language == language_rust);
10121 for (struct type *type : cu->rust_unions)
10122 quirk_rust_enum (type, cu->per_cu->dwarf2_per_objfile->objfile);
10123 }
10124
10125 /* Return the symtab for PER_CU. This works properly regardless of
10126 whether we're using the index or psymtabs. */
10127
10128 static struct compunit_symtab *
10129 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10130 {
10131 return (per_cu->dwarf2_per_objfile->using_index
10132 ? per_cu->v.quick->compunit_symtab
10133 : per_cu->v.psymtab->compunit_symtab);
10134 }
10135
10136 /* A helper function for computing the list of all symbol tables
10137 included by PER_CU. */
10138
10139 static void
10140 recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
10141 htab_t all_children, htab_t all_type_symtabs,
10142 struct dwarf2_per_cu_data *per_cu,
10143 struct compunit_symtab *immediate_parent)
10144 {
10145 void **slot;
10146 int ix;
10147 struct compunit_symtab *cust;
10148 struct dwarf2_per_cu_data *iter;
10149
10150 slot = htab_find_slot (all_children, per_cu, INSERT);
10151 if (*slot != NULL)
10152 {
10153 /* This inclusion and its children have been processed. */
10154 return;
10155 }
10156
10157 *slot = per_cu;
10158 /* Only add a CU if it has a symbol table. */
10159 cust = get_compunit_symtab (per_cu);
10160 if (cust != NULL)
10161 {
10162 /* If this is a type unit only add its symbol table if we haven't
10163 seen it yet (type unit per_cu's can share symtabs). */
10164 if (per_cu->is_debug_types)
10165 {
10166 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10167 if (*slot == NULL)
10168 {
10169 *slot = cust;
10170 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10171 if (cust->user == NULL)
10172 cust->user = immediate_parent;
10173 }
10174 }
10175 else
10176 {
10177 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10178 if (cust->user == NULL)
10179 cust->user = immediate_parent;
10180 }
10181 }
10182
10183 for (ix = 0;
10184 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
10185 ++ix)
10186 {
10187 recursively_compute_inclusions (result, all_children,
10188 all_type_symtabs, iter, cust);
10189 }
10190 }
10191
10192 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10193 PER_CU. */
10194
10195 static void
10196 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10197 {
10198 gdb_assert (! per_cu->is_debug_types);
10199
10200 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
10201 {
10202 int ix, len;
10203 struct dwarf2_per_cu_data *per_cu_iter;
10204 struct compunit_symtab *compunit_symtab_iter;
10205 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
10206 htab_t all_children, all_type_symtabs;
10207 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10208
10209 /* If we don't have a symtab, we can just skip this case. */
10210 if (cust == NULL)
10211 return;
10212
10213 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10214 NULL, xcalloc, xfree);
10215 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10216 NULL, xcalloc, xfree);
10217
10218 for (ix = 0;
10219 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
10220 ix, per_cu_iter);
10221 ++ix)
10222 {
10223 recursively_compute_inclusions (&result_symtabs, all_children,
10224 all_type_symtabs, per_cu_iter,
10225 cust);
10226 }
10227
10228 /* Now we have a transitive closure of all the included symtabs. */
10229 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10230 cust->includes
10231 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10232 struct compunit_symtab *, len + 1);
10233 for (ix = 0;
10234 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10235 compunit_symtab_iter);
10236 ++ix)
10237 cust->includes[ix] = compunit_symtab_iter;
10238 cust->includes[len] = NULL;
10239
10240 VEC_free (compunit_symtab_ptr, result_symtabs);
10241 htab_delete (all_children);
10242 htab_delete (all_type_symtabs);
10243 }
10244 }
10245
10246 /* Compute the 'includes' field for the symtabs of all the CUs we just
10247 read. */
10248
10249 static void
10250 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10251 {
10252 int ix;
10253 struct dwarf2_per_cu_data *iter;
10254
10255 for (ix = 0;
10256 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10257 ix, iter);
10258 ++ix)
10259 {
10260 if (! iter->is_debug_types)
10261 compute_compunit_symtab_includes (iter);
10262 }
10263
10264 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10265 }
10266
10267 /* Generate full symbol information for PER_CU, whose DIEs have
10268 already been loaded into memory. */
10269
10270 static void
10271 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10272 enum language pretend_language)
10273 {
10274 struct dwarf2_cu *cu = per_cu->cu;
10275 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10276 struct objfile *objfile = dwarf2_per_objfile->objfile;
10277 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10278 CORE_ADDR lowpc, highpc;
10279 struct compunit_symtab *cust;
10280 CORE_ADDR baseaddr;
10281 struct block *static_block;
10282 CORE_ADDR addr;
10283
10284 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10285
10286 buildsym_init ();
10287 scoped_free_pendings free_pending;
10288
10289 /* Clear the list here in case something was left over. */
10290 cu->method_list.clear ();
10291
10292 cu->list_in_scope = &file_symbols;
10293
10294 cu->language = pretend_language;
10295 cu->language_defn = language_def (cu->language);
10296
10297 /* Do line number decoding in read_file_scope () */
10298 process_die (cu->dies, cu);
10299
10300 /* For now fudge the Go package. */
10301 if (cu->language == language_go)
10302 fixup_go_packaging (cu);
10303
10304 /* Now that we have processed all the DIEs in the CU, all the types
10305 should be complete, and it should now be safe to compute all of the
10306 physnames. */
10307 compute_delayed_physnames (cu);
10308
10309 if (cu->language == language_rust)
10310 rust_union_quirks (cu);
10311
10312 /* Some compilers don't define a DW_AT_high_pc attribute for the
10313 compilation unit. If the DW_AT_high_pc is missing, synthesize
10314 it, by scanning the DIE's below the compilation unit. */
10315 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10316
10317 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10318 static_block = end_symtab_get_static_block (addr, 0, 1);
10319
10320 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10321 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10322 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10323 addrmap to help ensure it has an accurate map of pc values belonging to
10324 this comp unit. */
10325 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10326
10327 cust = end_symtab_from_static_block (static_block,
10328 SECT_OFF_TEXT (objfile), 0);
10329
10330 if (cust != NULL)
10331 {
10332 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10333
10334 /* Set symtab language to language from DW_AT_language. If the
10335 compilation is from a C file generated by language preprocessors, do
10336 not set the language if it was already deduced by start_subfile. */
10337 if (!(cu->language == language_c
10338 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10339 COMPUNIT_FILETABS (cust)->language = cu->language;
10340
10341 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10342 produce DW_AT_location with location lists but it can be possibly
10343 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10344 there were bugs in prologue debug info, fixed later in GCC-4.5
10345 by "unwind info for epilogues" patch (which is not directly related).
10346
10347 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10348 needed, it would be wrong due to missing DW_AT_producer there.
10349
10350 Still one can confuse GDB by using non-standard GCC compilation
10351 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10352 */
10353 if (cu->has_loclist && gcc_4_minor >= 5)
10354 cust->locations_valid = 1;
10355
10356 if (gcc_4_minor >= 5)
10357 cust->epilogue_unwind_valid = 1;
10358
10359 cust->call_site_htab = cu->call_site_htab;
10360 }
10361
10362 if (dwarf2_per_objfile->using_index)
10363 per_cu->v.quick->compunit_symtab = cust;
10364 else
10365 {
10366 struct partial_symtab *pst = per_cu->v.psymtab;
10367 pst->compunit_symtab = cust;
10368 pst->readin = 1;
10369 }
10370
10371 /* Push it for inclusion processing later. */
10372 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
10373 }
10374
10375 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10376 already been loaded into memory. */
10377
10378 static void
10379 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10380 enum language pretend_language)
10381 {
10382 struct dwarf2_cu *cu = per_cu->cu;
10383 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10384 struct objfile *objfile = dwarf2_per_objfile->objfile;
10385 struct compunit_symtab *cust;
10386 struct signatured_type *sig_type;
10387
10388 gdb_assert (per_cu->is_debug_types);
10389 sig_type = (struct signatured_type *) per_cu;
10390
10391 buildsym_init ();
10392 scoped_free_pendings free_pending;
10393
10394 /* Clear the list here in case something was left over. */
10395 cu->method_list.clear ();
10396
10397 cu->list_in_scope = &file_symbols;
10398
10399 cu->language = pretend_language;
10400 cu->language_defn = language_def (cu->language);
10401
10402 /* The symbol tables are set up in read_type_unit_scope. */
10403 process_die (cu->dies, cu);
10404
10405 /* For now fudge the Go package. */
10406 if (cu->language == language_go)
10407 fixup_go_packaging (cu);
10408
10409 /* Now that we have processed all the DIEs in the CU, all the types
10410 should be complete, and it should now be safe to compute all of the
10411 physnames. */
10412 compute_delayed_physnames (cu);
10413
10414 if (cu->language == language_rust)
10415 rust_union_quirks (cu);
10416
10417 /* TUs share symbol tables.
10418 If this is the first TU to use this symtab, complete the construction
10419 of it with end_expandable_symtab. Otherwise, complete the addition of
10420 this TU's symbols to the existing symtab. */
10421 if (sig_type->type_unit_group->compunit_symtab == NULL)
10422 {
10423 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10424 sig_type->type_unit_group->compunit_symtab = cust;
10425
10426 if (cust != NULL)
10427 {
10428 /* Set symtab language to language from DW_AT_language. If the
10429 compilation is from a C file generated by language preprocessors,
10430 do not set the language if it was already deduced by
10431 start_subfile. */
10432 if (!(cu->language == language_c
10433 && COMPUNIT_FILETABS (cust)->language != language_c))
10434 COMPUNIT_FILETABS (cust)->language = cu->language;
10435 }
10436 }
10437 else
10438 {
10439 augment_type_symtab ();
10440 cust = sig_type->type_unit_group->compunit_symtab;
10441 }
10442
10443 if (dwarf2_per_objfile->using_index)
10444 per_cu->v.quick->compunit_symtab = cust;
10445 else
10446 {
10447 struct partial_symtab *pst = per_cu->v.psymtab;
10448 pst->compunit_symtab = cust;
10449 pst->readin = 1;
10450 }
10451 }
10452
10453 /* Process an imported unit DIE. */
10454
10455 static void
10456 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10457 {
10458 struct attribute *attr;
10459
10460 /* For now we don't handle imported units in type units. */
10461 if (cu->per_cu->is_debug_types)
10462 {
10463 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10464 " supported in type units [in module %s]"),
10465 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10466 }
10467
10468 attr = dwarf2_attr (die, DW_AT_import, cu);
10469 if (attr != NULL)
10470 {
10471 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10472 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10473 dwarf2_per_cu_data *per_cu
10474 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10475 cu->per_cu->dwarf2_per_objfile);
10476
10477 /* If necessary, add it to the queue and load its DIEs. */
10478 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10479 load_full_comp_unit (per_cu, cu->language);
10480
10481 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
10482 per_cu);
10483 }
10484 }
10485
10486 /* RAII object that represents a process_die scope: i.e.,
10487 starts/finishes processing a DIE. */
10488 class process_die_scope
10489 {
10490 public:
10491 process_die_scope (die_info *die, dwarf2_cu *cu)
10492 : m_die (die), m_cu (cu)
10493 {
10494 /* We should only be processing DIEs not already in process. */
10495 gdb_assert (!m_die->in_process);
10496 m_die->in_process = true;
10497 }
10498
10499 ~process_die_scope ()
10500 {
10501 m_die->in_process = false;
10502
10503 /* If we're done processing the DIE for the CU that owns the line
10504 header, we don't need the line header anymore. */
10505 if (m_cu->line_header_die_owner == m_die)
10506 {
10507 delete m_cu->line_header;
10508 m_cu->line_header = NULL;
10509 m_cu->line_header_die_owner = NULL;
10510 }
10511 }
10512
10513 private:
10514 die_info *m_die;
10515 dwarf2_cu *m_cu;
10516 };
10517
10518 /* Process a die and its children. */
10519
10520 static void
10521 process_die (struct die_info *die, struct dwarf2_cu *cu)
10522 {
10523 process_die_scope scope (die, cu);
10524
10525 switch (die->tag)
10526 {
10527 case DW_TAG_padding:
10528 break;
10529 case DW_TAG_compile_unit:
10530 case DW_TAG_partial_unit:
10531 read_file_scope (die, cu);
10532 break;
10533 case DW_TAG_type_unit:
10534 read_type_unit_scope (die, cu);
10535 break;
10536 case DW_TAG_subprogram:
10537 case DW_TAG_inlined_subroutine:
10538 read_func_scope (die, cu);
10539 break;
10540 case DW_TAG_lexical_block:
10541 case DW_TAG_try_block:
10542 case DW_TAG_catch_block:
10543 read_lexical_block_scope (die, cu);
10544 break;
10545 case DW_TAG_call_site:
10546 case DW_TAG_GNU_call_site:
10547 read_call_site_scope (die, cu);
10548 break;
10549 case DW_TAG_class_type:
10550 case DW_TAG_interface_type:
10551 case DW_TAG_structure_type:
10552 case DW_TAG_union_type:
10553 process_structure_scope (die, cu);
10554 break;
10555 case DW_TAG_enumeration_type:
10556 process_enumeration_scope (die, cu);
10557 break;
10558
10559 /* These dies have a type, but processing them does not create
10560 a symbol or recurse to process the children. Therefore we can
10561 read them on-demand through read_type_die. */
10562 case DW_TAG_subroutine_type:
10563 case DW_TAG_set_type:
10564 case DW_TAG_array_type:
10565 case DW_TAG_pointer_type:
10566 case DW_TAG_ptr_to_member_type:
10567 case DW_TAG_reference_type:
10568 case DW_TAG_rvalue_reference_type:
10569 case DW_TAG_string_type:
10570 break;
10571
10572 case DW_TAG_base_type:
10573 case DW_TAG_subrange_type:
10574 case DW_TAG_typedef:
10575 /* Add a typedef symbol for the type definition, if it has a
10576 DW_AT_name. */
10577 new_symbol (die, read_type_die (die, cu), cu);
10578 break;
10579 case DW_TAG_common_block:
10580 read_common_block (die, cu);
10581 break;
10582 case DW_TAG_common_inclusion:
10583 break;
10584 case DW_TAG_namespace:
10585 cu->processing_has_namespace_info = 1;
10586 read_namespace (die, cu);
10587 break;
10588 case DW_TAG_module:
10589 cu->processing_has_namespace_info = 1;
10590 read_module (die, cu);
10591 break;
10592 case DW_TAG_imported_declaration:
10593 cu->processing_has_namespace_info = 1;
10594 if (read_namespace_alias (die, cu))
10595 break;
10596 /* The declaration is not a global namespace alias: fall through. */
10597 case DW_TAG_imported_module:
10598 cu->processing_has_namespace_info = 1;
10599 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10600 || cu->language != language_fortran))
10601 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
10602 dwarf_tag_name (die->tag));
10603 read_import_statement (die, cu);
10604 break;
10605
10606 case DW_TAG_imported_unit:
10607 process_imported_unit_die (die, cu);
10608 break;
10609
10610 case DW_TAG_variable:
10611 read_variable (die, cu);
10612 break;
10613
10614 default:
10615 new_symbol (die, NULL, cu);
10616 break;
10617 }
10618 }
10619 \f
10620 /* DWARF name computation. */
10621
10622 /* A helper function for dwarf2_compute_name which determines whether DIE
10623 needs to have the name of the scope prepended to the name listed in the
10624 die. */
10625
10626 static int
10627 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10628 {
10629 struct attribute *attr;
10630
10631 switch (die->tag)
10632 {
10633 case DW_TAG_namespace:
10634 case DW_TAG_typedef:
10635 case DW_TAG_class_type:
10636 case DW_TAG_interface_type:
10637 case DW_TAG_structure_type:
10638 case DW_TAG_union_type:
10639 case DW_TAG_enumeration_type:
10640 case DW_TAG_enumerator:
10641 case DW_TAG_subprogram:
10642 case DW_TAG_inlined_subroutine:
10643 case DW_TAG_member:
10644 case DW_TAG_imported_declaration:
10645 return 1;
10646
10647 case DW_TAG_variable:
10648 case DW_TAG_constant:
10649 /* We only need to prefix "globally" visible variables. These include
10650 any variable marked with DW_AT_external or any variable that
10651 lives in a namespace. [Variables in anonymous namespaces
10652 require prefixing, but they are not DW_AT_external.] */
10653
10654 if (dwarf2_attr (die, DW_AT_specification, cu))
10655 {
10656 struct dwarf2_cu *spec_cu = cu;
10657
10658 return die_needs_namespace (die_specification (die, &spec_cu),
10659 spec_cu);
10660 }
10661
10662 attr = dwarf2_attr (die, DW_AT_external, cu);
10663 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10664 && die->parent->tag != DW_TAG_module)
10665 return 0;
10666 /* A variable in a lexical block of some kind does not need a
10667 namespace, even though in C++ such variables may be external
10668 and have a mangled name. */
10669 if (die->parent->tag == DW_TAG_lexical_block
10670 || die->parent->tag == DW_TAG_try_block
10671 || die->parent->tag == DW_TAG_catch_block
10672 || die->parent->tag == DW_TAG_subprogram)
10673 return 0;
10674 return 1;
10675
10676 default:
10677 return 0;
10678 }
10679 }
10680
10681 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10682 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10683 defined for the given DIE. */
10684
10685 static struct attribute *
10686 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10687 {
10688 struct attribute *attr;
10689
10690 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10691 if (attr == NULL)
10692 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10693
10694 return attr;
10695 }
10696
10697 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10698 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10699 defined for the given DIE. */
10700
10701 static const char *
10702 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10703 {
10704 const char *linkage_name;
10705
10706 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10707 if (linkage_name == NULL)
10708 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10709
10710 return linkage_name;
10711 }
10712
10713 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10714 compute the physname for the object, which include a method's:
10715 - formal parameters (C++),
10716 - receiver type (Go),
10717
10718 The term "physname" is a bit confusing.
10719 For C++, for example, it is the demangled name.
10720 For Go, for example, it's the mangled name.
10721
10722 For Ada, return the DIE's linkage name rather than the fully qualified
10723 name. PHYSNAME is ignored..
10724
10725 The result is allocated on the objfile_obstack and canonicalized. */
10726
10727 static const char *
10728 dwarf2_compute_name (const char *name,
10729 struct die_info *die, struct dwarf2_cu *cu,
10730 int physname)
10731 {
10732 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10733
10734 if (name == NULL)
10735 name = dwarf2_name (die, cu);
10736
10737 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10738 but otherwise compute it by typename_concat inside GDB.
10739 FIXME: Actually this is not really true, or at least not always true.
10740 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10741 Fortran names because there is no mangling standard. So new_symbol
10742 will set the demangled name to the result of dwarf2_full_name, and it is
10743 the demangled name that GDB uses if it exists. */
10744 if (cu->language == language_ada
10745 || (cu->language == language_fortran && physname))
10746 {
10747 /* For Ada unit, we prefer the linkage name over the name, as
10748 the former contains the exported name, which the user expects
10749 to be able to reference. Ideally, we want the user to be able
10750 to reference this entity using either natural or linkage name,
10751 but we haven't started looking at this enhancement yet. */
10752 const char *linkage_name = dw2_linkage_name (die, cu);
10753
10754 if (linkage_name != NULL)
10755 return linkage_name;
10756 }
10757
10758 /* These are the only languages we know how to qualify names in. */
10759 if (name != NULL
10760 && (cu->language == language_cplus
10761 || cu->language == language_fortran || cu->language == language_d
10762 || cu->language == language_rust))
10763 {
10764 if (die_needs_namespace (die, cu))
10765 {
10766 const char *prefix;
10767 const char *canonical_name = NULL;
10768
10769 string_file buf;
10770
10771 prefix = determine_prefix (die, cu);
10772 if (*prefix != '\0')
10773 {
10774 char *prefixed_name = typename_concat (NULL, prefix, name,
10775 physname, cu);
10776
10777 buf.puts (prefixed_name);
10778 xfree (prefixed_name);
10779 }
10780 else
10781 buf.puts (name);
10782
10783 /* Template parameters may be specified in the DIE's DW_AT_name, or
10784 as children with DW_TAG_template_type_param or
10785 DW_TAG_value_type_param. If the latter, add them to the name
10786 here. If the name already has template parameters, then
10787 skip this step; some versions of GCC emit both, and
10788 it is more efficient to use the pre-computed name.
10789
10790 Something to keep in mind about this process: it is very
10791 unlikely, or in some cases downright impossible, to produce
10792 something that will match the mangled name of a function.
10793 If the definition of the function has the same debug info,
10794 we should be able to match up with it anyway. But fallbacks
10795 using the minimal symbol, for instance to find a method
10796 implemented in a stripped copy of libstdc++, will not work.
10797 If we do not have debug info for the definition, we will have to
10798 match them up some other way.
10799
10800 When we do name matching there is a related problem with function
10801 templates; two instantiated function templates are allowed to
10802 differ only by their return types, which we do not add here. */
10803
10804 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10805 {
10806 struct attribute *attr;
10807 struct die_info *child;
10808 int first = 1;
10809
10810 die->building_fullname = 1;
10811
10812 for (child = die->child; child != NULL; child = child->sibling)
10813 {
10814 struct type *type;
10815 LONGEST value;
10816 const gdb_byte *bytes;
10817 struct dwarf2_locexpr_baton *baton;
10818 struct value *v;
10819
10820 if (child->tag != DW_TAG_template_type_param
10821 && child->tag != DW_TAG_template_value_param)
10822 continue;
10823
10824 if (first)
10825 {
10826 buf.puts ("<");
10827 first = 0;
10828 }
10829 else
10830 buf.puts (", ");
10831
10832 attr = dwarf2_attr (child, DW_AT_type, cu);
10833 if (attr == NULL)
10834 {
10835 complaint (&symfile_complaints,
10836 _("template parameter missing DW_AT_type"));
10837 buf.puts ("UNKNOWN_TYPE");
10838 continue;
10839 }
10840 type = die_type (child, cu);
10841
10842 if (child->tag == DW_TAG_template_type_param)
10843 {
10844 c_print_type (type, "", &buf, -1, 0, &type_print_raw_options);
10845 continue;
10846 }
10847
10848 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10849 if (attr == NULL)
10850 {
10851 complaint (&symfile_complaints,
10852 _("template parameter missing "
10853 "DW_AT_const_value"));
10854 buf.puts ("UNKNOWN_VALUE");
10855 continue;
10856 }
10857
10858 dwarf2_const_value_attr (attr, type, name,
10859 &cu->comp_unit_obstack, cu,
10860 &value, &bytes, &baton);
10861
10862 if (TYPE_NOSIGN (type))
10863 /* GDB prints characters as NUMBER 'CHAR'. If that's
10864 changed, this can use value_print instead. */
10865 c_printchar (value, type, &buf);
10866 else
10867 {
10868 struct value_print_options opts;
10869
10870 if (baton != NULL)
10871 v = dwarf2_evaluate_loc_desc (type, NULL,
10872 baton->data,
10873 baton->size,
10874 baton->per_cu);
10875 else if (bytes != NULL)
10876 {
10877 v = allocate_value (type);
10878 memcpy (value_contents_writeable (v), bytes,
10879 TYPE_LENGTH (type));
10880 }
10881 else
10882 v = value_from_longest (type, value);
10883
10884 /* Specify decimal so that we do not depend on
10885 the radix. */
10886 get_formatted_print_options (&opts, 'd');
10887 opts.raw = 1;
10888 value_print (v, &buf, &opts);
10889 release_value (v);
10890 }
10891 }
10892
10893 die->building_fullname = 0;
10894
10895 if (!first)
10896 {
10897 /* Close the argument list, with a space if necessary
10898 (nested templates). */
10899 if (!buf.empty () && buf.string ().back () == '>')
10900 buf.puts (" >");
10901 else
10902 buf.puts (">");
10903 }
10904 }
10905
10906 /* For C++ methods, append formal parameter type
10907 information, if PHYSNAME. */
10908
10909 if (physname && die->tag == DW_TAG_subprogram
10910 && cu->language == language_cplus)
10911 {
10912 struct type *type = read_type_die (die, cu);
10913
10914 c_type_print_args (type, &buf, 1, cu->language,
10915 &type_print_raw_options);
10916
10917 if (cu->language == language_cplus)
10918 {
10919 /* Assume that an artificial first parameter is
10920 "this", but do not crash if it is not. RealView
10921 marks unnamed (and thus unused) parameters as
10922 artificial; there is no way to differentiate
10923 the two cases. */
10924 if (TYPE_NFIELDS (type) > 0
10925 && TYPE_FIELD_ARTIFICIAL (type, 0)
10926 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10927 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10928 0))))
10929 buf.puts (" const");
10930 }
10931 }
10932
10933 const std::string &intermediate_name = buf.string ();
10934
10935 if (cu->language == language_cplus)
10936 canonical_name
10937 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10938 &objfile->per_bfd->storage_obstack);
10939
10940 /* If we only computed INTERMEDIATE_NAME, or if
10941 INTERMEDIATE_NAME is already canonical, then we need to
10942 copy it to the appropriate obstack. */
10943 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10944 name = ((const char *)
10945 obstack_copy0 (&objfile->per_bfd->storage_obstack,
10946 intermediate_name.c_str (),
10947 intermediate_name.length ()));
10948 else
10949 name = canonical_name;
10950 }
10951 }
10952
10953 return name;
10954 }
10955
10956 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10957 If scope qualifiers are appropriate they will be added. The result
10958 will be allocated on the storage_obstack, or NULL if the DIE does
10959 not have a name. NAME may either be from a previous call to
10960 dwarf2_name or NULL.
10961
10962 The output string will be canonicalized (if C++). */
10963
10964 static const char *
10965 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10966 {
10967 return dwarf2_compute_name (name, die, cu, 0);
10968 }
10969
10970 /* Construct a physname for the given DIE in CU. NAME may either be
10971 from a previous call to dwarf2_name or NULL. The result will be
10972 allocated on the objfile_objstack or NULL if the DIE does not have a
10973 name.
10974
10975 The output string will be canonicalized (if C++). */
10976
10977 static const char *
10978 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10979 {
10980 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10981 const char *retval, *mangled = NULL, *canon = NULL;
10982 int need_copy = 1;
10983
10984 /* In this case dwarf2_compute_name is just a shortcut not building anything
10985 on its own. */
10986 if (!die_needs_namespace (die, cu))
10987 return dwarf2_compute_name (name, die, cu, 1);
10988
10989 mangled = dw2_linkage_name (die, cu);
10990
10991 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10992 See https://github.com/rust-lang/rust/issues/32925. */
10993 if (cu->language == language_rust && mangled != NULL
10994 && strchr (mangled, '{') != NULL)
10995 mangled = NULL;
10996
10997 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10998 has computed. */
10999 gdb::unique_xmalloc_ptr<char> demangled;
11000 if (mangled != NULL)
11001 {
11002
11003 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11004 {
11005 /* Do nothing (do not demangle the symbol name). */
11006 }
11007 else if (cu->language == language_go)
11008 {
11009 /* This is a lie, but we already lie to the caller new_symbol.
11010 new_symbol assumes we return the mangled name.
11011 This just undoes that lie until things are cleaned up. */
11012 }
11013 else
11014 {
11015 /* Use DMGL_RET_DROP for C++ template functions to suppress
11016 their return type. It is easier for GDB users to search
11017 for such functions as `name(params)' than `long name(params)'.
11018 In such case the minimal symbol names do not match the full
11019 symbol names but for template functions there is never a need
11020 to look up their definition from their declaration so
11021 the only disadvantage remains the minimal symbol variant
11022 `long name(params)' does not have the proper inferior type. */
11023 demangled.reset (gdb_demangle (mangled,
11024 (DMGL_PARAMS | DMGL_ANSI
11025 | DMGL_RET_DROP)));
11026 }
11027 if (demangled)
11028 canon = demangled.get ();
11029 else
11030 {
11031 canon = mangled;
11032 need_copy = 0;
11033 }
11034 }
11035
11036 if (canon == NULL || check_physname)
11037 {
11038 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11039
11040 if (canon != NULL && strcmp (physname, canon) != 0)
11041 {
11042 /* It may not mean a bug in GDB. The compiler could also
11043 compute DW_AT_linkage_name incorrectly. But in such case
11044 GDB would need to be bug-to-bug compatible. */
11045
11046 complaint (&symfile_complaints,
11047 _("Computed physname <%s> does not match demangled <%s> "
11048 "(from linkage <%s>) - DIE at %s [in module %s]"),
11049 physname, canon, mangled, sect_offset_str (die->sect_off),
11050 objfile_name (objfile));
11051
11052 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11053 is available here - over computed PHYSNAME. It is safer
11054 against both buggy GDB and buggy compilers. */
11055
11056 retval = canon;
11057 }
11058 else
11059 {
11060 retval = physname;
11061 need_copy = 0;
11062 }
11063 }
11064 else
11065 retval = canon;
11066
11067 if (need_copy)
11068 retval = ((const char *)
11069 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11070 retval, strlen (retval)));
11071
11072 return retval;
11073 }
11074
11075 /* Inspect DIE in CU for a namespace alias. If one exists, record
11076 a new symbol for it.
11077
11078 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11079
11080 static int
11081 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11082 {
11083 struct attribute *attr;
11084
11085 /* If the die does not have a name, this is not a namespace
11086 alias. */
11087 attr = dwarf2_attr (die, DW_AT_name, cu);
11088 if (attr != NULL)
11089 {
11090 int num;
11091 struct die_info *d = die;
11092 struct dwarf2_cu *imported_cu = cu;
11093
11094 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11095 keep inspecting DIEs until we hit the underlying import. */
11096 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11097 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11098 {
11099 attr = dwarf2_attr (d, DW_AT_import, cu);
11100 if (attr == NULL)
11101 break;
11102
11103 d = follow_die_ref (d, attr, &imported_cu);
11104 if (d->tag != DW_TAG_imported_declaration)
11105 break;
11106 }
11107
11108 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11109 {
11110 complaint (&symfile_complaints,
11111 _("DIE at %s has too many recursively imported "
11112 "declarations"), sect_offset_str (d->sect_off));
11113 return 0;
11114 }
11115
11116 if (attr != NULL)
11117 {
11118 struct type *type;
11119 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11120
11121 type = get_die_type_at_offset (sect_off, cu->per_cu);
11122 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11123 {
11124 /* This declaration is a global namespace alias. Add
11125 a symbol for it whose type is the aliased namespace. */
11126 new_symbol (die, type, cu);
11127 return 1;
11128 }
11129 }
11130 }
11131
11132 return 0;
11133 }
11134
11135 /* Return the using directives repository (global or local?) to use in the
11136 current context for LANGUAGE.
11137
11138 For Ada, imported declarations can materialize renamings, which *may* be
11139 global. However it is impossible (for now?) in DWARF to distinguish
11140 "external" imported declarations and "static" ones. As all imported
11141 declarations seem to be static in all other languages, make them all CU-wide
11142 global only in Ada. */
11143
11144 static struct using_direct **
11145 using_directives (enum language language)
11146 {
11147 if (language == language_ada && context_stack_depth == 0)
11148 return &global_using_directives;
11149 else
11150 return &local_using_directives;
11151 }
11152
11153 /* Read the import statement specified by the given die and record it. */
11154
11155 static void
11156 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11157 {
11158 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11159 struct attribute *import_attr;
11160 struct die_info *imported_die, *child_die;
11161 struct dwarf2_cu *imported_cu;
11162 const char *imported_name;
11163 const char *imported_name_prefix;
11164 const char *canonical_name;
11165 const char *import_alias;
11166 const char *imported_declaration = NULL;
11167 const char *import_prefix;
11168 std::vector<const char *> excludes;
11169
11170 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11171 if (import_attr == NULL)
11172 {
11173 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11174 dwarf_tag_name (die->tag));
11175 return;
11176 }
11177
11178 imported_cu = cu;
11179 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11180 imported_name = dwarf2_name (imported_die, imported_cu);
11181 if (imported_name == NULL)
11182 {
11183 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11184
11185 The import in the following code:
11186 namespace A
11187 {
11188 typedef int B;
11189 }
11190
11191 int main ()
11192 {
11193 using A::B;
11194 B b;
11195 return b;
11196 }
11197
11198 ...
11199 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11200 <52> DW_AT_decl_file : 1
11201 <53> DW_AT_decl_line : 6
11202 <54> DW_AT_import : <0x75>
11203 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11204 <59> DW_AT_name : B
11205 <5b> DW_AT_decl_file : 1
11206 <5c> DW_AT_decl_line : 2
11207 <5d> DW_AT_type : <0x6e>
11208 ...
11209 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11210 <76> DW_AT_byte_size : 4
11211 <77> DW_AT_encoding : 5 (signed)
11212
11213 imports the wrong die ( 0x75 instead of 0x58 ).
11214 This case will be ignored until the gcc bug is fixed. */
11215 return;
11216 }
11217
11218 /* Figure out the local name after import. */
11219 import_alias = dwarf2_name (die, cu);
11220
11221 /* Figure out where the statement is being imported to. */
11222 import_prefix = determine_prefix (die, cu);
11223
11224 /* Figure out what the scope of the imported die is and prepend it
11225 to the name of the imported die. */
11226 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11227
11228 if (imported_die->tag != DW_TAG_namespace
11229 && imported_die->tag != DW_TAG_module)
11230 {
11231 imported_declaration = imported_name;
11232 canonical_name = imported_name_prefix;
11233 }
11234 else if (strlen (imported_name_prefix) > 0)
11235 canonical_name = obconcat (&objfile->objfile_obstack,
11236 imported_name_prefix,
11237 (cu->language == language_d ? "." : "::"),
11238 imported_name, (char *) NULL);
11239 else
11240 canonical_name = imported_name;
11241
11242 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11243 for (child_die = die->child; child_die && child_die->tag;
11244 child_die = sibling_die (child_die))
11245 {
11246 /* DWARF-4: A Fortran use statement with a “rename list” may be
11247 represented by an imported module entry with an import attribute
11248 referring to the module and owned entries corresponding to those
11249 entities that are renamed as part of being imported. */
11250
11251 if (child_die->tag != DW_TAG_imported_declaration)
11252 {
11253 complaint (&symfile_complaints,
11254 _("child DW_TAG_imported_declaration expected "
11255 "- DIE at %s [in module %s]"),
11256 sect_offset_str (child_die->sect_off),
11257 objfile_name (objfile));
11258 continue;
11259 }
11260
11261 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11262 if (import_attr == NULL)
11263 {
11264 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
11265 dwarf_tag_name (child_die->tag));
11266 continue;
11267 }
11268
11269 imported_cu = cu;
11270 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11271 &imported_cu);
11272 imported_name = dwarf2_name (imported_die, imported_cu);
11273 if (imported_name == NULL)
11274 {
11275 complaint (&symfile_complaints,
11276 _("child DW_TAG_imported_declaration has unknown "
11277 "imported name - DIE at %s [in module %s]"),
11278 sect_offset_str (child_die->sect_off),
11279 objfile_name (objfile));
11280 continue;
11281 }
11282
11283 excludes.push_back (imported_name);
11284
11285 process_die (child_die, cu);
11286 }
11287
11288 add_using_directive (using_directives (cu->language),
11289 import_prefix,
11290 canonical_name,
11291 import_alias,
11292 imported_declaration,
11293 excludes,
11294 0,
11295 &objfile->objfile_obstack);
11296 }
11297
11298 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11299 types, but gives them a size of zero. Starting with version 14,
11300 ICC is compatible with GCC. */
11301
11302 static int
11303 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11304 {
11305 if (!cu->checked_producer)
11306 check_producer (cu);
11307
11308 return cu->producer_is_icc_lt_14;
11309 }
11310
11311 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11312 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11313 this, it was first present in GCC release 4.3.0. */
11314
11315 static int
11316 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11317 {
11318 if (!cu->checked_producer)
11319 check_producer (cu);
11320
11321 return cu->producer_is_gcc_lt_4_3;
11322 }
11323
11324 static file_and_directory
11325 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11326 {
11327 file_and_directory res;
11328
11329 /* Find the filename. Do not use dwarf2_name here, since the filename
11330 is not a source language identifier. */
11331 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11332 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11333
11334 if (res.comp_dir == NULL
11335 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11336 && IS_ABSOLUTE_PATH (res.name))
11337 {
11338 res.comp_dir_storage = ldirname (res.name);
11339 if (!res.comp_dir_storage.empty ())
11340 res.comp_dir = res.comp_dir_storage.c_str ();
11341 }
11342 if (res.comp_dir != NULL)
11343 {
11344 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11345 directory, get rid of it. */
11346 const char *cp = strchr (res.comp_dir, ':');
11347
11348 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11349 res.comp_dir = cp + 1;
11350 }
11351
11352 if (res.name == NULL)
11353 res.name = "<unknown>";
11354
11355 return res;
11356 }
11357
11358 /* Handle DW_AT_stmt_list for a compilation unit.
11359 DIE is the DW_TAG_compile_unit die for CU.
11360 COMP_DIR is the compilation directory. LOWPC is passed to
11361 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11362
11363 static void
11364 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11365 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11366 {
11367 struct dwarf2_per_objfile *dwarf2_per_objfile
11368 = cu->per_cu->dwarf2_per_objfile;
11369 struct objfile *objfile = dwarf2_per_objfile->objfile;
11370 struct attribute *attr;
11371 struct line_header line_header_local;
11372 hashval_t line_header_local_hash;
11373 void **slot;
11374 int decode_mapping;
11375
11376 gdb_assert (! cu->per_cu->is_debug_types);
11377
11378 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11379 if (attr == NULL)
11380 return;
11381
11382 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11383
11384 /* The line header hash table is only created if needed (it exists to
11385 prevent redundant reading of the line table for partial_units).
11386 If we're given a partial_unit, we'll need it. If we're given a
11387 compile_unit, then use the line header hash table if it's already
11388 created, but don't create one just yet. */
11389
11390 if (dwarf2_per_objfile->line_header_hash == NULL
11391 && die->tag == DW_TAG_partial_unit)
11392 {
11393 dwarf2_per_objfile->line_header_hash
11394 = htab_create_alloc_ex (127, line_header_hash_voidp,
11395 line_header_eq_voidp,
11396 free_line_header_voidp,
11397 &objfile->objfile_obstack,
11398 hashtab_obstack_allocate,
11399 dummy_obstack_deallocate);
11400 }
11401
11402 line_header_local.sect_off = line_offset;
11403 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11404 line_header_local_hash = line_header_hash (&line_header_local);
11405 if (dwarf2_per_objfile->line_header_hash != NULL)
11406 {
11407 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11408 &line_header_local,
11409 line_header_local_hash, NO_INSERT);
11410
11411 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11412 is not present in *SLOT (since if there is something in *SLOT then
11413 it will be for a partial_unit). */
11414 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11415 {
11416 gdb_assert (*slot != NULL);
11417 cu->line_header = (struct line_header *) *slot;
11418 return;
11419 }
11420 }
11421
11422 /* dwarf_decode_line_header does not yet provide sufficient information.
11423 We always have to call also dwarf_decode_lines for it. */
11424 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11425 if (lh == NULL)
11426 return;
11427
11428 cu->line_header = lh.release ();
11429 cu->line_header_die_owner = die;
11430
11431 if (dwarf2_per_objfile->line_header_hash == NULL)
11432 slot = NULL;
11433 else
11434 {
11435 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11436 &line_header_local,
11437 line_header_local_hash, INSERT);
11438 gdb_assert (slot != NULL);
11439 }
11440 if (slot != NULL && *slot == NULL)
11441 {
11442 /* This newly decoded line number information unit will be owned
11443 by line_header_hash hash table. */
11444 *slot = cu->line_header;
11445 cu->line_header_die_owner = NULL;
11446 }
11447 else
11448 {
11449 /* We cannot free any current entry in (*slot) as that struct line_header
11450 may be already used by multiple CUs. Create only temporary decoded
11451 line_header for this CU - it may happen at most once for each line
11452 number information unit. And if we're not using line_header_hash
11453 then this is what we want as well. */
11454 gdb_assert (die->tag != DW_TAG_partial_unit);
11455 }
11456 decode_mapping = (die->tag != DW_TAG_partial_unit);
11457 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11458 decode_mapping);
11459
11460 }
11461
11462 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11463
11464 static void
11465 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11466 {
11467 struct dwarf2_per_objfile *dwarf2_per_objfile
11468 = cu->per_cu->dwarf2_per_objfile;
11469 struct objfile *objfile = dwarf2_per_objfile->objfile;
11470 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11471 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11472 CORE_ADDR highpc = ((CORE_ADDR) 0);
11473 struct attribute *attr;
11474 struct die_info *child_die;
11475 CORE_ADDR baseaddr;
11476
11477 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11478
11479 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11480
11481 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11482 from finish_block. */
11483 if (lowpc == ((CORE_ADDR) -1))
11484 lowpc = highpc;
11485 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11486
11487 file_and_directory fnd = find_file_and_directory (die, cu);
11488
11489 prepare_one_comp_unit (cu, die, cu->language);
11490
11491 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11492 standardised yet. As a workaround for the language detection we fall
11493 back to the DW_AT_producer string. */
11494 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11495 cu->language = language_opencl;
11496
11497 /* Similar hack for Go. */
11498 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11499 set_cu_language (DW_LANG_Go, cu);
11500
11501 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
11502
11503 /* Decode line number information if present. We do this before
11504 processing child DIEs, so that the line header table is available
11505 for DW_AT_decl_file. */
11506 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11507
11508 /* Process all dies in compilation unit. */
11509 if (die->child != NULL)
11510 {
11511 child_die = die->child;
11512 while (child_die && child_die->tag)
11513 {
11514 process_die (child_die, cu);
11515 child_die = sibling_die (child_die);
11516 }
11517 }
11518
11519 /* Decode macro information, if present. Dwarf 2 macro information
11520 refers to information in the line number info statement program
11521 header, so we can only read it if we've read the header
11522 successfully. */
11523 attr = dwarf2_attr (die, DW_AT_macros, cu);
11524 if (attr == NULL)
11525 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11526 if (attr && cu->line_header)
11527 {
11528 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11529 complaint (&symfile_complaints,
11530 _("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11531
11532 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11533 }
11534 else
11535 {
11536 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11537 if (attr && cu->line_header)
11538 {
11539 unsigned int macro_offset = DW_UNSND (attr);
11540
11541 dwarf_decode_macros (cu, macro_offset, 0);
11542 }
11543 }
11544 }
11545
11546 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11547 Create the set of symtabs used by this TU, or if this TU is sharing
11548 symtabs with another TU and the symtabs have already been created
11549 then restore those symtabs in the line header.
11550 We don't need the pc/line-number mapping for type units. */
11551
11552 static void
11553 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
11554 {
11555 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11556 struct type_unit_group *tu_group;
11557 int first_time;
11558 struct attribute *attr;
11559 unsigned int i;
11560 struct signatured_type *sig_type;
11561
11562 gdb_assert (per_cu->is_debug_types);
11563 sig_type = (struct signatured_type *) per_cu;
11564
11565 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11566
11567 /* If we're using .gdb_index (includes -readnow) then
11568 per_cu->type_unit_group may not have been set up yet. */
11569 if (sig_type->type_unit_group == NULL)
11570 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11571 tu_group = sig_type->type_unit_group;
11572
11573 /* If we've already processed this stmt_list there's no real need to
11574 do it again, we could fake it and just recreate the part we need
11575 (file name,index -> symtab mapping). If data shows this optimization
11576 is useful we can do it then. */
11577 first_time = tu_group->compunit_symtab == NULL;
11578
11579 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11580 debug info. */
11581 line_header_up lh;
11582 if (attr != NULL)
11583 {
11584 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11585 lh = dwarf_decode_line_header (line_offset, cu);
11586 }
11587 if (lh == NULL)
11588 {
11589 if (first_time)
11590 dwarf2_start_symtab (cu, "", NULL, 0);
11591 else
11592 {
11593 gdb_assert (tu_group->symtabs == NULL);
11594 restart_symtab (tu_group->compunit_symtab, "", 0);
11595 }
11596 return;
11597 }
11598
11599 cu->line_header = lh.release ();
11600 cu->line_header_die_owner = die;
11601
11602 if (first_time)
11603 {
11604 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
11605
11606 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11607 still initializing it, and our caller (a few levels up)
11608 process_full_type_unit still needs to know if this is the first
11609 time. */
11610
11611 tu_group->num_symtabs = cu->line_header->file_names.size ();
11612 tu_group->symtabs = XNEWVEC (struct symtab *,
11613 cu->line_header->file_names.size ());
11614
11615 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11616 {
11617 file_entry &fe = cu->line_header->file_names[i];
11618
11619 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
11620
11621 if (current_subfile->symtab == NULL)
11622 {
11623 /* NOTE: start_subfile will recognize when it's been
11624 passed a file it has already seen. So we can't
11625 assume there's a simple mapping from
11626 cu->line_header->file_names to subfiles, plus
11627 cu->line_header->file_names may contain dups. */
11628 current_subfile->symtab
11629 = allocate_symtab (cust, current_subfile->name);
11630 }
11631
11632 fe.symtab = current_subfile->symtab;
11633 tu_group->symtabs[i] = fe.symtab;
11634 }
11635 }
11636 else
11637 {
11638 restart_symtab (tu_group->compunit_symtab, "", 0);
11639
11640 for (i = 0; i < cu->line_header->file_names.size (); ++i)
11641 {
11642 file_entry &fe = cu->line_header->file_names[i];
11643
11644 fe.symtab = tu_group->symtabs[i];
11645 }
11646 }
11647
11648 /* The main symtab is allocated last. Type units don't have DW_AT_name
11649 so they don't have a "real" (so to speak) symtab anyway.
11650 There is later code that will assign the main symtab to all symbols
11651 that don't have one. We need to handle the case of a symbol with a
11652 missing symtab (DW_AT_decl_file) anyway. */
11653 }
11654
11655 /* Process DW_TAG_type_unit.
11656 For TUs we want to skip the first top level sibling if it's not the
11657 actual type being defined by this TU. In this case the first top
11658 level sibling is there to provide context only. */
11659
11660 static void
11661 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11662 {
11663 struct die_info *child_die;
11664
11665 prepare_one_comp_unit (cu, die, language_minimal);
11666
11667 /* Initialize (or reinitialize) the machinery for building symtabs.
11668 We do this before processing child DIEs, so that the line header table
11669 is available for DW_AT_decl_file. */
11670 setup_type_unit_groups (die, cu);
11671
11672 if (die->child != NULL)
11673 {
11674 child_die = die->child;
11675 while (child_die && child_die->tag)
11676 {
11677 process_die (child_die, cu);
11678 child_die = sibling_die (child_die);
11679 }
11680 }
11681 }
11682 \f
11683 /* DWO/DWP files.
11684
11685 http://gcc.gnu.org/wiki/DebugFission
11686 http://gcc.gnu.org/wiki/DebugFissionDWP
11687
11688 To simplify handling of both DWO files ("object" files with the DWARF info)
11689 and DWP files (a file with the DWOs packaged up into one file), we treat
11690 DWP files as having a collection of virtual DWO files. */
11691
11692 static hashval_t
11693 hash_dwo_file (const void *item)
11694 {
11695 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11696 hashval_t hash;
11697
11698 hash = htab_hash_string (dwo_file->dwo_name);
11699 if (dwo_file->comp_dir != NULL)
11700 hash += htab_hash_string (dwo_file->comp_dir);
11701 return hash;
11702 }
11703
11704 static int
11705 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11706 {
11707 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11708 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11709
11710 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11711 return 0;
11712 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11713 return lhs->comp_dir == rhs->comp_dir;
11714 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11715 }
11716
11717 /* Allocate a hash table for DWO files. */
11718
11719 static htab_t
11720 allocate_dwo_file_hash_table (struct objfile *objfile)
11721 {
11722 return htab_create_alloc_ex (41,
11723 hash_dwo_file,
11724 eq_dwo_file,
11725 NULL,
11726 &objfile->objfile_obstack,
11727 hashtab_obstack_allocate,
11728 dummy_obstack_deallocate);
11729 }
11730
11731 /* Lookup DWO file DWO_NAME. */
11732
11733 static void **
11734 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11735 const char *dwo_name,
11736 const char *comp_dir)
11737 {
11738 struct dwo_file find_entry;
11739 void **slot;
11740
11741 if (dwarf2_per_objfile->dwo_files == NULL)
11742 dwarf2_per_objfile->dwo_files
11743 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11744
11745 memset (&find_entry, 0, sizeof (find_entry));
11746 find_entry.dwo_name = dwo_name;
11747 find_entry.comp_dir = comp_dir;
11748 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11749
11750 return slot;
11751 }
11752
11753 static hashval_t
11754 hash_dwo_unit (const void *item)
11755 {
11756 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11757
11758 /* This drops the top 32 bits of the id, but is ok for a hash. */
11759 return dwo_unit->signature;
11760 }
11761
11762 static int
11763 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11764 {
11765 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11766 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11767
11768 /* The signature is assumed to be unique within the DWO file.
11769 So while object file CU dwo_id's always have the value zero,
11770 that's OK, assuming each object file DWO file has only one CU,
11771 and that's the rule for now. */
11772 return lhs->signature == rhs->signature;
11773 }
11774
11775 /* Allocate a hash table for DWO CUs,TUs.
11776 There is one of these tables for each of CUs,TUs for each DWO file. */
11777
11778 static htab_t
11779 allocate_dwo_unit_table (struct objfile *objfile)
11780 {
11781 /* Start out with a pretty small number.
11782 Generally DWO files contain only one CU and maybe some TUs. */
11783 return htab_create_alloc_ex (3,
11784 hash_dwo_unit,
11785 eq_dwo_unit,
11786 NULL,
11787 &objfile->objfile_obstack,
11788 hashtab_obstack_allocate,
11789 dummy_obstack_deallocate);
11790 }
11791
11792 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11793
11794 struct create_dwo_cu_data
11795 {
11796 struct dwo_file *dwo_file;
11797 struct dwo_unit dwo_unit;
11798 };
11799
11800 /* die_reader_func for create_dwo_cu. */
11801
11802 static void
11803 create_dwo_cu_reader (const struct die_reader_specs *reader,
11804 const gdb_byte *info_ptr,
11805 struct die_info *comp_unit_die,
11806 int has_children,
11807 void *datap)
11808 {
11809 struct dwarf2_cu *cu = reader->cu;
11810 sect_offset sect_off = cu->per_cu->sect_off;
11811 struct dwarf2_section_info *section = cu->per_cu->section;
11812 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11813 struct dwo_file *dwo_file = data->dwo_file;
11814 struct dwo_unit *dwo_unit = &data->dwo_unit;
11815 struct attribute *attr;
11816
11817 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11818 if (attr == NULL)
11819 {
11820 complaint (&symfile_complaints,
11821 _("Dwarf Error: debug entry at offset %s is missing"
11822 " its dwo_id [in module %s]"),
11823 sect_offset_str (sect_off), dwo_file->dwo_name);
11824 return;
11825 }
11826
11827 dwo_unit->dwo_file = dwo_file;
11828 dwo_unit->signature = DW_UNSND (attr);
11829 dwo_unit->section = section;
11830 dwo_unit->sect_off = sect_off;
11831 dwo_unit->length = cu->per_cu->length;
11832
11833 if (dwarf_read_debug)
11834 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11835 sect_offset_str (sect_off),
11836 hex_string (dwo_unit->signature));
11837 }
11838
11839 /* Create the dwo_units for the CUs in a DWO_FILE.
11840 Note: This function processes DWO files only, not DWP files. */
11841
11842 static void
11843 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11844 struct dwo_file &dwo_file, dwarf2_section_info &section,
11845 htab_t &cus_htab)
11846 {
11847 struct objfile *objfile = dwarf2_per_objfile->objfile;
11848 const gdb_byte *info_ptr, *end_ptr;
11849
11850 dwarf2_read_section (objfile, &section);
11851 info_ptr = section.buffer;
11852
11853 if (info_ptr == NULL)
11854 return;
11855
11856 if (dwarf_read_debug)
11857 {
11858 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11859 get_section_name (&section),
11860 get_section_file_name (&section));
11861 }
11862
11863 end_ptr = info_ptr + section.size;
11864 while (info_ptr < end_ptr)
11865 {
11866 struct dwarf2_per_cu_data per_cu;
11867 struct create_dwo_cu_data create_dwo_cu_data;
11868 struct dwo_unit *dwo_unit;
11869 void **slot;
11870 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11871
11872 memset (&create_dwo_cu_data.dwo_unit, 0,
11873 sizeof (create_dwo_cu_data.dwo_unit));
11874 memset (&per_cu, 0, sizeof (per_cu));
11875 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11876 per_cu.is_debug_types = 0;
11877 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11878 per_cu.section = &section;
11879 create_dwo_cu_data.dwo_file = &dwo_file;
11880
11881 init_cutu_and_read_dies_no_follow (
11882 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11883 info_ptr += per_cu.length;
11884
11885 // If the unit could not be parsed, skip it.
11886 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11887 continue;
11888
11889 if (cus_htab == NULL)
11890 cus_htab = allocate_dwo_unit_table (objfile);
11891
11892 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11893 *dwo_unit = create_dwo_cu_data.dwo_unit;
11894 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11895 gdb_assert (slot != NULL);
11896 if (*slot != NULL)
11897 {
11898 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11899 sect_offset dup_sect_off = dup_cu->sect_off;
11900
11901 complaint (&symfile_complaints,
11902 _("debug cu entry at offset %s is duplicate to"
11903 " the entry at offset %s, signature %s"),
11904 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11905 hex_string (dwo_unit->signature));
11906 }
11907 *slot = (void *)dwo_unit;
11908 }
11909 }
11910
11911 /* DWP file .debug_{cu,tu}_index section format:
11912 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11913
11914 DWP Version 1:
11915
11916 Both index sections have the same format, and serve to map a 64-bit
11917 signature to a set of section numbers. Each section begins with a header,
11918 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11919 indexes, and a pool of 32-bit section numbers. The index sections will be
11920 aligned at 8-byte boundaries in the file.
11921
11922 The index section header consists of:
11923
11924 V, 32 bit version number
11925 -, 32 bits unused
11926 N, 32 bit number of compilation units or type units in the index
11927 M, 32 bit number of slots in the hash table
11928
11929 Numbers are recorded using the byte order of the application binary.
11930
11931 The hash table begins at offset 16 in the section, and consists of an array
11932 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11933 order of the application binary). Unused slots in the hash table are 0.
11934 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11935
11936 The parallel table begins immediately after the hash table
11937 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11938 array of 32-bit indexes (using the byte order of the application binary),
11939 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11940 table contains a 32-bit index into the pool of section numbers. For unused
11941 hash table slots, the corresponding entry in the parallel table will be 0.
11942
11943 The pool of section numbers begins immediately following the hash table
11944 (at offset 16 + 12 * M from the beginning of the section). The pool of
11945 section numbers consists of an array of 32-bit words (using the byte order
11946 of the application binary). Each item in the array is indexed starting
11947 from 0. The hash table entry provides the index of the first section
11948 number in the set. Additional section numbers in the set follow, and the
11949 set is terminated by a 0 entry (section number 0 is not used in ELF).
11950
11951 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11952 section must be the first entry in the set, and the .debug_abbrev.dwo must
11953 be the second entry. Other members of the set may follow in any order.
11954
11955 ---
11956
11957 DWP Version 2:
11958
11959 DWP Version 2 combines all the .debug_info, etc. sections into one,
11960 and the entries in the index tables are now offsets into these sections.
11961 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11962 section.
11963
11964 Index Section Contents:
11965 Header
11966 Hash Table of Signatures dwp_hash_table.hash_table
11967 Parallel Table of Indices dwp_hash_table.unit_table
11968 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11969 Table of Section Sizes dwp_hash_table.v2.sizes
11970
11971 The index section header consists of:
11972
11973 V, 32 bit version number
11974 L, 32 bit number of columns in the table of section offsets
11975 N, 32 bit number of compilation units or type units in the index
11976 M, 32 bit number of slots in the hash table
11977
11978 Numbers are recorded using the byte order of the application binary.
11979
11980 The hash table has the same format as version 1.
11981 The parallel table of indices has the same format as version 1,
11982 except that the entries are origin-1 indices into the table of sections
11983 offsets and the table of section sizes.
11984
11985 The table of offsets begins immediately following the parallel table
11986 (at offset 16 + 12 * M from the beginning of the section). The table is
11987 a two-dimensional array of 32-bit words (using the byte order of the
11988 application binary), with L columns and N+1 rows, in row-major order.
11989 Each row in the array is indexed starting from 0. The first row provides
11990 a key to the remaining rows: each column in this row provides an identifier
11991 for a debug section, and the offsets in the same column of subsequent rows
11992 refer to that section. The section identifiers are:
11993
11994 DW_SECT_INFO 1 .debug_info.dwo
11995 DW_SECT_TYPES 2 .debug_types.dwo
11996 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11997 DW_SECT_LINE 4 .debug_line.dwo
11998 DW_SECT_LOC 5 .debug_loc.dwo
11999 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12000 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12001 DW_SECT_MACRO 8 .debug_macro.dwo
12002
12003 The offsets provided by the CU and TU index sections are the base offsets
12004 for the contributions made by each CU or TU to the corresponding section
12005 in the package file. Each CU and TU header contains an abbrev_offset
12006 field, used to find the abbreviations table for that CU or TU within the
12007 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12008 be interpreted as relative to the base offset given in the index section.
12009 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12010 should be interpreted as relative to the base offset for .debug_line.dwo,
12011 and offsets into other debug sections obtained from DWARF attributes should
12012 also be interpreted as relative to the corresponding base offset.
12013
12014 The table of sizes begins immediately following the table of offsets.
12015 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12016 with L columns and N rows, in row-major order. Each row in the array is
12017 indexed starting from 1 (row 0 is shared by the two tables).
12018
12019 ---
12020
12021 Hash table lookup is handled the same in version 1 and 2:
12022
12023 We assume that N and M will not exceed 2^32 - 1.
12024 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12025
12026 Given a 64-bit compilation unit signature or a type signature S, an entry
12027 in the hash table is located as follows:
12028
12029 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12030 the low-order k bits all set to 1.
12031
12032 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12033
12034 3) If the hash table entry at index H matches the signature, use that
12035 entry. If the hash table entry at index H is unused (all zeroes),
12036 terminate the search: the signature is not present in the table.
12037
12038 4) Let H = (H + H') modulo M. Repeat at Step 3.
12039
12040 Because M > N and H' and M are relatively prime, the search is guaranteed
12041 to stop at an unused slot or find the match. */
12042
12043 /* Create a hash table to map DWO IDs to their CU/TU entry in
12044 .debug_{info,types}.dwo in DWP_FILE.
12045 Returns NULL if there isn't one.
12046 Note: This function processes DWP files only, not DWO files. */
12047
12048 static struct dwp_hash_table *
12049 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12050 struct dwp_file *dwp_file, int is_debug_types)
12051 {
12052 struct objfile *objfile = dwarf2_per_objfile->objfile;
12053 bfd *dbfd = dwp_file->dbfd;
12054 const gdb_byte *index_ptr, *index_end;
12055 struct dwarf2_section_info *index;
12056 uint32_t version, nr_columns, nr_units, nr_slots;
12057 struct dwp_hash_table *htab;
12058
12059 if (is_debug_types)
12060 index = &dwp_file->sections.tu_index;
12061 else
12062 index = &dwp_file->sections.cu_index;
12063
12064 if (dwarf2_section_empty_p (index))
12065 return NULL;
12066 dwarf2_read_section (objfile, index);
12067
12068 index_ptr = index->buffer;
12069 index_end = index_ptr + index->size;
12070
12071 version = read_4_bytes (dbfd, index_ptr);
12072 index_ptr += 4;
12073 if (version == 2)
12074 nr_columns = read_4_bytes (dbfd, index_ptr);
12075 else
12076 nr_columns = 0;
12077 index_ptr += 4;
12078 nr_units = read_4_bytes (dbfd, index_ptr);
12079 index_ptr += 4;
12080 nr_slots = read_4_bytes (dbfd, index_ptr);
12081 index_ptr += 4;
12082
12083 if (version != 1 && version != 2)
12084 {
12085 error (_("Dwarf Error: unsupported DWP file version (%s)"
12086 " [in module %s]"),
12087 pulongest (version), dwp_file->name);
12088 }
12089 if (nr_slots != (nr_slots & -nr_slots))
12090 {
12091 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12092 " is not power of 2 [in module %s]"),
12093 pulongest (nr_slots), dwp_file->name);
12094 }
12095
12096 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12097 htab->version = version;
12098 htab->nr_columns = nr_columns;
12099 htab->nr_units = nr_units;
12100 htab->nr_slots = nr_slots;
12101 htab->hash_table = index_ptr;
12102 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12103
12104 /* Exit early if the table is empty. */
12105 if (nr_slots == 0 || nr_units == 0
12106 || (version == 2 && nr_columns == 0))
12107 {
12108 /* All must be zero. */
12109 if (nr_slots != 0 || nr_units != 0
12110 || (version == 2 && nr_columns != 0))
12111 {
12112 complaint (&symfile_complaints,
12113 _("Empty DWP but nr_slots,nr_units,nr_columns not"
12114 " all zero [in modules %s]"),
12115 dwp_file->name);
12116 }
12117 return htab;
12118 }
12119
12120 if (version == 1)
12121 {
12122 htab->section_pool.v1.indices =
12123 htab->unit_table + sizeof (uint32_t) * nr_slots;
12124 /* It's harder to decide whether the section is too small in v1.
12125 V1 is deprecated anyway so we punt. */
12126 }
12127 else
12128 {
12129 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12130 int *ids = htab->section_pool.v2.section_ids;
12131 /* Reverse map for error checking. */
12132 int ids_seen[DW_SECT_MAX + 1];
12133 int i;
12134
12135 if (nr_columns < 2)
12136 {
12137 error (_("Dwarf Error: bad DWP hash table, too few columns"
12138 " in section table [in module %s]"),
12139 dwp_file->name);
12140 }
12141 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12142 {
12143 error (_("Dwarf Error: bad DWP hash table, too many columns"
12144 " in section table [in module %s]"),
12145 dwp_file->name);
12146 }
12147 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12148 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12149 for (i = 0; i < nr_columns; ++i)
12150 {
12151 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12152
12153 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12154 {
12155 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12156 " in section table [in module %s]"),
12157 id, dwp_file->name);
12158 }
12159 if (ids_seen[id] != -1)
12160 {
12161 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12162 " id %d in section table [in module %s]"),
12163 id, dwp_file->name);
12164 }
12165 ids_seen[id] = i;
12166 ids[i] = id;
12167 }
12168 /* Must have exactly one info or types section. */
12169 if (((ids_seen[DW_SECT_INFO] != -1)
12170 + (ids_seen[DW_SECT_TYPES] != -1))
12171 != 1)
12172 {
12173 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12174 " DWO info/types section [in module %s]"),
12175 dwp_file->name);
12176 }
12177 /* Must have an abbrev section. */
12178 if (ids_seen[DW_SECT_ABBREV] == -1)
12179 {
12180 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12181 " section [in module %s]"),
12182 dwp_file->name);
12183 }
12184 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12185 htab->section_pool.v2.sizes =
12186 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12187 * nr_units * nr_columns);
12188 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12189 * nr_units * nr_columns))
12190 > index_end)
12191 {
12192 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12193 " [in module %s]"),
12194 dwp_file->name);
12195 }
12196 }
12197
12198 return htab;
12199 }
12200
12201 /* Update SECTIONS with the data from SECTP.
12202
12203 This function is like the other "locate" section routines that are
12204 passed to bfd_map_over_sections, but in this context the sections to
12205 read comes from the DWP V1 hash table, not the full ELF section table.
12206
12207 The result is non-zero for success, or zero if an error was found. */
12208
12209 static int
12210 locate_v1_virtual_dwo_sections (asection *sectp,
12211 struct virtual_v1_dwo_sections *sections)
12212 {
12213 const struct dwop_section_names *names = &dwop_section_names;
12214
12215 if (section_is_p (sectp->name, &names->abbrev_dwo))
12216 {
12217 /* There can be only one. */
12218 if (sections->abbrev.s.section != NULL)
12219 return 0;
12220 sections->abbrev.s.section = sectp;
12221 sections->abbrev.size = bfd_get_section_size (sectp);
12222 }
12223 else if (section_is_p (sectp->name, &names->info_dwo)
12224 || section_is_p (sectp->name, &names->types_dwo))
12225 {
12226 /* There can be only one. */
12227 if (sections->info_or_types.s.section != NULL)
12228 return 0;
12229 sections->info_or_types.s.section = sectp;
12230 sections->info_or_types.size = bfd_get_section_size (sectp);
12231 }
12232 else if (section_is_p (sectp->name, &names->line_dwo))
12233 {
12234 /* There can be only one. */
12235 if (sections->line.s.section != NULL)
12236 return 0;
12237 sections->line.s.section = sectp;
12238 sections->line.size = bfd_get_section_size (sectp);
12239 }
12240 else if (section_is_p (sectp->name, &names->loc_dwo))
12241 {
12242 /* There can be only one. */
12243 if (sections->loc.s.section != NULL)
12244 return 0;
12245 sections->loc.s.section = sectp;
12246 sections->loc.size = bfd_get_section_size (sectp);
12247 }
12248 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12249 {
12250 /* There can be only one. */
12251 if (sections->macinfo.s.section != NULL)
12252 return 0;
12253 sections->macinfo.s.section = sectp;
12254 sections->macinfo.size = bfd_get_section_size (sectp);
12255 }
12256 else if (section_is_p (sectp->name, &names->macro_dwo))
12257 {
12258 /* There can be only one. */
12259 if (sections->macro.s.section != NULL)
12260 return 0;
12261 sections->macro.s.section = sectp;
12262 sections->macro.size = bfd_get_section_size (sectp);
12263 }
12264 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12265 {
12266 /* There can be only one. */
12267 if (sections->str_offsets.s.section != NULL)
12268 return 0;
12269 sections->str_offsets.s.section = sectp;
12270 sections->str_offsets.size = bfd_get_section_size (sectp);
12271 }
12272 else
12273 {
12274 /* No other kind of section is valid. */
12275 return 0;
12276 }
12277
12278 return 1;
12279 }
12280
12281 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12282 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12283 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12284 This is for DWP version 1 files. */
12285
12286 static struct dwo_unit *
12287 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12288 struct dwp_file *dwp_file,
12289 uint32_t unit_index,
12290 const char *comp_dir,
12291 ULONGEST signature, int is_debug_types)
12292 {
12293 struct objfile *objfile = dwarf2_per_objfile->objfile;
12294 const struct dwp_hash_table *dwp_htab =
12295 is_debug_types ? dwp_file->tus : dwp_file->cus;
12296 bfd *dbfd = dwp_file->dbfd;
12297 const char *kind = is_debug_types ? "TU" : "CU";
12298 struct dwo_file *dwo_file;
12299 struct dwo_unit *dwo_unit;
12300 struct virtual_v1_dwo_sections sections;
12301 void **dwo_file_slot;
12302 int i;
12303
12304 gdb_assert (dwp_file->version == 1);
12305
12306 if (dwarf_read_debug)
12307 {
12308 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12309 kind,
12310 pulongest (unit_index), hex_string (signature),
12311 dwp_file->name);
12312 }
12313
12314 /* Fetch the sections of this DWO unit.
12315 Put a limit on the number of sections we look for so that bad data
12316 doesn't cause us to loop forever. */
12317
12318 #define MAX_NR_V1_DWO_SECTIONS \
12319 (1 /* .debug_info or .debug_types */ \
12320 + 1 /* .debug_abbrev */ \
12321 + 1 /* .debug_line */ \
12322 + 1 /* .debug_loc */ \
12323 + 1 /* .debug_str_offsets */ \
12324 + 1 /* .debug_macro or .debug_macinfo */ \
12325 + 1 /* trailing zero */)
12326
12327 memset (&sections, 0, sizeof (sections));
12328
12329 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12330 {
12331 asection *sectp;
12332 uint32_t section_nr =
12333 read_4_bytes (dbfd,
12334 dwp_htab->section_pool.v1.indices
12335 + (unit_index + i) * sizeof (uint32_t));
12336
12337 if (section_nr == 0)
12338 break;
12339 if (section_nr >= dwp_file->num_sections)
12340 {
12341 error (_("Dwarf Error: bad DWP hash table, section number too large"
12342 " [in module %s]"),
12343 dwp_file->name);
12344 }
12345
12346 sectp = dwp_file->elf_sections[section_nr];
12347 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12348 {
12349 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12350 " [in module %s]"),
12351 dwp_file->name);
12352 }
12353 }
12354
12355 if (i < 2
12356 || dwarf2_section_empty_p (&sections.info_or_types)
12357 || dwarf2_section_empty_p (&sections.abbrev))
12358 {
12359 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12360 " [in module %s]"),
12361 dwp_file->name);
12362 }
12363 if (i == MAX_NR_V1_DWO_SECTIONS)
12364 {
12365 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12366 " [in module %s]"),
12367 dwp_file->name);
12368 }
12369
12370 /* It's easier for the rest of the code if we fake a struct dwo_file and
12371 have dwo_unit "live" in that. At least for now.
12372
12373 The DWP file can be made up of a random collection of CUs and TUs.
12374 However, for each CU + set of TUs that came from the same original DWO
12375 file, we can combine them back into a virtual DWO file to save space
12376 (fewer struct dwo_file objects to allocate). Remember that for really
12377 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12378
12379 std::string virtual_dwo_name =
12380 string_printf ("virtual-dwo/%d-%d-%d-%d",
12381 get_section_id (&sections.abbrev),
12382 get_section_id (&sections.line),
12383 get_section_id (&sections.loc),
12384 get_section_id (&sections.str_offsets));
12385 /* Can we use an existing virtual DWO file? */
12386 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12387 virtual_dwo_name.c_str (),
12388 comp_dir);
12389 /* Create one if necessary. */
12390 if (*dwo_file_slot == NULL)
12391 {
12392 if (dwarf_read_debug)
12393 {
12394 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12395 virtual_dwo_name.c_str ());
12396 }
12397 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12398 dwo_file->dwo_name
12399 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12400 virtual_dwo_name.c_str (),
12401 virtual_dwo_name.size ());
12402 dwo_file->comp_dir = comp_dir;
12403 dwo_file->sections.abbrev = sections.abbrev;
12404 dwo_file->sections.line = sections.line;
12405 dwo_file->sections.loc = sections.loc;
12406 dwo_file->sections.macinfo = sections.macinfo;
12407 dwo_file->sections.macro = sections.macro;
12408 dwo_file->sections.str_offsets = sections.str_offsets;
12409 /* The "str" section is global to the entire DWP file. */
12410 dwo_file->sections.str = dwp_file->sections.str;
12411 /* The info or types section is assigned below to dwo_unit,
12412 there's no need to record it in dwo_file.
12413 Also, we can't simply record type sections in dwo_file because
12414 we record a pointer into the vector in dwo_unit. As we collect more
12415 types we'll grow the vector and eventually have to reallocate space
12416 for it, invalidating all copies of pointers into the previous
12417 contents. */
12418 *dwo_file_slot = dwo_file;
12419 }
12420 else
12421 {
12422 if (dwarf_read_debug)
12423 {
12424 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12425 virtual_dwo_name.c_str ());
12426 }
12427 dwo_file = (struct dwo_file *) *dwo_file_slot;
12428 }
12429
12430 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12431 dwo_unit->dwo_file = dwo_file;
12432 dwo_unit->signature = signature;
12433 dwo_unit->section =
12434 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12435 *dwo_unit->section = sections.info_or_types;
12436 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12437
12438 return dwo_unit;
12439 }
12440
12441 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12442 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12443 piece within that section used by a TU/CU, return a virtual section
12444 of just that piece. */
12445
12446 static struct dwarf2_section_info
12447 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12448 struct dwarf2_section_info *section,
12449 bfd_size_type offset, bfd_size_type size)
12450 {
12451 struct dwarf2_section_info result;
12452 asection *sectp;
12453
12454 gdb_assert (section != NULL);
12455 gdb_assert (!section->is_virtual);
12456
12457 memset (&result, 0, sizeof (result));
12458 result.s.containing_section = section;
12459 result.is_virtual = 1;
12460
12461 if (size == 0)
12462 return result;
12463
12464 sectp = get_section_bfd_section (section);
12465
12466 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12467 bounds of the real section. This is a pretty-rare event, so just
12468 flag an error (easier) instead of a warning and trying to cope. */
12469 if (sectp == NULL
12470 || offset + size > bfd_get_section_size (sectp))
12471 {
12472 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12473 " in section %s [in module %s]"),
12474 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12475 objfile_name (dwarf2_per_objfile->objfile));
12476 }
12477
12478 result.virtual_offset = offset;
12479 result.size = size;
12480 return result;
12481 }
12482
12483 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12484 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12485 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12486 This is for DWP version 2 files. */
12487
12488 static struct dwo_unit *
12489 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12490 struct dwp_file *dwp_file,
12491 uint32_t unit_index,
12492 const char *comp_dir,
12493 ULONGEST signature, int is_debug_types)
12494 {
12495 struct objfile *objfile = dwarf2_per_objfile->objfile;
12496 const struct dwp_hash_table *dwp_htab =
12497 is_debug_types ? dwp_file->tus : dwp_file->cus;
12498 bfd *dbfd = dwp_file->dbfd;
12499 const char *kind = is_debug_types ? "TU" : "CU";
12500 struct dwo_file *dwo_file;
12501 struct dwo_unit *dwo_unit;
12502 struct virtual_v2_dwo_sections sections;
12503 void **dwo_file_slot;
12504 int i;
12505
12506 gdb_assert (dwp_file->version == 2);
12507
12508 if (dwarf_read_debug)
12509 {
12510 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12511 kind,
12512 pulongest (unit_index), hex_string (signature),
12513 dwp_file->name);
12514 }
12515
12516 /* Fetch the section offsets of this DWO unit. */
12517
12518 memset (&sections, 0, sizeof (sections));
12519
12520 for (i = 0; i < dwp_htab->nr_columns; ++i)
12521 {
12522 uint32_t offset = read_4_bytes (dbfd,
12523 dwp_htab->section_pool.v2.offsets
12524 + (((unit_index - 1) * dwp_htab->nr_columns
12525 + i)
12526 * sizeof (uint32_t)));
12527 uint32_t size = read_4_bytes (dbfd,
12528 dwp_htab->section_pool.v2.sizes
12529 + (((unit_index - 1) * dwp_htab->nr_columns
12530 + i)
12531 * sizeof (uint32_t)));
12532
12533 switch (dwp_htab->section_pool.v2.section_ids[i])
12534 {
12535 case DW_SECT_INFO:
12536 case DW_SECT_TYPES:
12537 sections.info_or_types_offset = offset;
12538 sections.info_or_types_size = size;
12539 break;
12540 case DW_SECT_ABBREV:
12541 sections.abbrev_offset = offset;
12542 sections.abbrev_size = size;
12543 break;
12544 case DW_SECT_LINE:
12545 sections.line_offset = offset;
12546 sections.line_size = size;
12547 break;
12548 case DW_SECT_LOC:
12549 sections.loc_offset = offset;
12550 sections.loc_size = size;
12551 break;
12552 case DW_SECT_STR_OFFSETS:
12553 sections.str_offsets_offset = offset;
12554 sections.str_offsets_size = size;
12555 break;
12556 case DW_SECT_MACINFO:
12557 sections.macinfo_offset = offset;
12558 sections.macinfo_size = size;
12559 break;
12560 case DW_SECT_MACRO:
12561 sections.macro_offset = offset;
12562 sections.macro_size = size;
12563 break;
12564 }
12565 }
12566
12567 /* It's easier for the rest of the code if we fake a struct dwo_file and
12568 have dwo_unit "live" in that. At least for now.
12569
12570 The DWP file can be made up of a random collection of CUs and TUs.
12571 However, for each CU + set of TUs that came from the same original DWO
12572 file, we can combine them back into a virtual DWO file to save space
12573 (fewer struct dwo_file objects to allocate). Remember that for really
12574 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12575
12576 std::string virtual_dwo_name =
12577 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12578 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12579 (long) (sections.line_size ? sections.line_offset : 0),
12580 (long) (sections.loc_size ? sections.loc_offset : 0),
12581 (long) (sections.str_offsets_size
12582 ? sections.str_offsets_offset : 0));
12583 /* Can we use an existing virtual DWO file? */
12584 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12585 virtual_dwo_name.c_str (),
12586 comp_dir);
12587 /* Create one if necessary. */
12588 if (*dwo_file_slot == NULL)
12589 {
12590 if (dwarf_read_debug)
12591 {
12592 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12593 virtual_dwo_name.c_str ());
12594 }
12595 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
12596 dwo_file->dwo_name
12597 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
12598 virtual_dwo_name.c_str (),
12599 virtual_dwo_name.size ());
12600 dwo_file->comp_dir = comp_dir;
12601 dwo_file->sections.abbrev =
12602 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12603 sections.abbrev_offset, sections.abbrev_size);
12604 dwo_file->sections.line =
12605 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12606 sections.line_offset, sections.line_size);
12607 dwo_file->sections.loc =
12608 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12609 sections.loc_offset, sections.loc_size);
12610 dwo_file->sections.macinfo =
12611 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12612 sections.macinfo_offset, sections.macinfo_size);
12613 dwo_file->sections.macro =
12614 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12615 sections.macro_offset, sections.macro_size);
12616 dwo_file->sections.str_offsets =
12617 create_dwp_v2_section (dwarf2_per_objfile,
12618 &dwp_file->sections.str_offsets,
12619 sections.str_offsets_offset,
12620 sections.str_offsets_size);
12621 /* The "str" section is global to the entire DWP file. */
12622 dwo_file->sections.str = dwp_file->sections.str;
12623 /* The info or types section is assigned below to dwo_unit,
12624 there's no need to record it in dwo_file.
12625 Also, we can't simply record type sections in dwo_file because
12626 we record a pointer into the vector in dwo_unit. As we collect more
12627 types we'll grow the vector and eventually have to reallocate space
12628 for it, invalidating all copies of pointers into the previous
12629 contents. */
12630 *dwo_file_slot = dwo_file;
12631 }
12632 else
12633 {
12634 if (dwarf_read_debug)
12635 {
12636 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12637 virtual_dwo_name.c_str ());
12638 }
12639 dwo_file = (struct dwo_file *) *dwo_file_slot;
12640 }
12641
12642 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12643 dwo_unit->dwo_file = dwo_file;
12644 dwo_unit->signature = signature;
12645 dwo_unit->section =
12646 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12647 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12648 is_debug_types
12649 ? &dwp_file->sections.types
12650 : &dwp_file->sections.info,
12651 sections.info_or_types_offset,
12652 sections.info_or_types_size);
12653 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12654
12655 return dwo_unit;
12656 }
12657
12658 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12659 Returns NULL if the signature isn't found. */
12660
12661 static struct dwo_unit *
12662 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12663 struct dwp_file *dwp_file, const char *comp_dir,
12664 ULONGEST signature, int is_debug_types)
12665 {
12666 const struct dwp_hash_table *dwp_htab =
12667 is_debug_types ? dwp_file->tus : dwp_file->cus;
12668 bfd *dbfd = dwp_file->dbfd;
12669 uint32_t mask = dwp_htab->nr_slots - 1;
12670 uint32_t hash = signature & mask;
12671 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12672 unsigned int i;
12673 void **slot;
12674 struct dwo_unit find_dwo_cu;
12675
12676 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12677 find_dwo_cu.signature = signature;
12678 slot = htab_find_slot (is_debug_types
12679 ? dwp_file->loaded_tus
12680 : dwp_file->loaded_cus,
12681 &find_dwo_cu, INSERT);
12682
12683 if (*slot != NULL)
12684 return (struct dwo_unit *) *slot;
12685
12686 /* Use a for loop so that we don't loop forever on bad debug info. */
12687 for (i = 0; i < dwp_htab->nr_slots; ++i)
12688 {
12689 ULONGEST signature_in_table;
12690
12691 signature_in_table =
12692 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12693 if (signature_in_table == signature)
12694 {
12695 uint32_t unit_index =
12696 read_4_bytes (dbfd,
12697 dwp_htab->unit_table + hash * sizeof (uint32_t));
12698
12699 if (dwp_file->version == 1)
12700 {
12701 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12702 dwp_file, unit_index,
12703 comp_dir, signature,
12704 is_debug_types);
12705 }
12706 else
12707 {
12708 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12709 dwp_file, unit_index,
12710 comp_dir, signature,
12711 is_debug_types);
12712 }
12713 return (struct dwo_unit *) *slot;
12714 }
12715 if (signature_in_table == 0)
12716 return NULL;
12717 hash = (hash + hash2) & mask;
12718 }
12719
12720 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12721 " [in module %s]"),
12722 dwp_file->name);
12723 }
12724
12725 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12726 Open the file specified by FILE_NAME and hand it off to BFD for
12727 preliminary analysis. Return a newly initialized bfd *, which
12728 includes a canonicalized copy of FILE_NAME.
12729 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12730 SEARCH_CWD is true if the current directory is to be searched.
12731 It will be searched before debug-file-directory.
12732 If successful, the file is added to the bfd include table of the
12733 objfile's bfd (see gdb_bfd_record_inclusion).
12734 If unable to find/open the file, return NULL.
12735 NOTE: This function is derived from symfile_bfd_open. */
12736
12737 static gdb_bfd_ref_ptr
12738 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12739 const char *file_name, int is_dwp, int search_cwd)
12740 {
12741 int desc;
12742 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12743 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12744 to debug_file_directory. */
12745 const char *search_path;
12746 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12747
12748 gdb::unique_xmalloc_ptr<char> search_path_holder;
12749 if (search_cwd)
12750 {
12751 if (*debug_file_directory != '\0')
12752 {
12753 search_path_holder.reset (concat (".", dirname_separator_string,
12754 debug_file_directory,
12755 (char *) NULL));
12756 search_path = search_path_holder.get ();
12757 }
12758 else
12759 search_path = ".";
12760 }
12761 else
12762 search_path = debug_file_directory;
12763
12764 openp_flags flags = OPF_RETURN_REALPATH;
12765 if (is_dwp)
12766 flags |= OPF_SEARCH_IN_PATH;
12767
12768 gdb::unique_xmalloc_ptr<char> absolute_name;
12769 desc = openp (search_path, flags, file_name,
12770 O_RDONLY | O_BINARY, &absolute_name);
12771 if (desc < 0)
12772 return NULL;
12773
12774 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12775 gnutarget, desc));
12776 if (sym_bfd == NULL)
12777 return NULL;
12778 bfd_set_cacheable (sym_bfd.get (), 1);
12779
12780 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12781 return NULL;
12782
12783 /* Success. Record the bfd as having been included by the objfile's bfd.
12784 This is important because things like demangled_names_hash lives in the
12785 objfile's per_bfd space and may have references to things like symbol
12786 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12787 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12788
12789 return sym_bfd;
12790 }
12791
12792 /* Try to open DWO file FILE_NAME.
12793 COMP_DIR is the DW_AT_comp_dir attribute.
12794 The result is the bfd handle of the file.
12795 If there is a problem finding or opening the file, return NULL.
12796 Upon success, the canonicalized path of the file is stored in the bfd,
12797 same as symfile_bfd_open. */
12798
12799 static gdb_bfd_ref_ptr
12800 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12801 const char *file_name, const char *comp_dir)
12802 {
12803 if (IS_ABSOLUTE_PATH (file_name))
12804 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12805 0 /*is_dwp*/, 0 /*search_cwd*/);
12806
12807 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12808
12809 if (comp_dir != NULL)
12810 {
12811 char *path_to_try = concat (comp_dir, SLASH_STRING,
12812 file_name, (char *) NULL);
12813
12814 /* NOTE: If comp_dir is a relative path, this will also try the
12815 search path, which seems useful. */
12816 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12817 path_to_try,
12818 0 /*is_dwp*/,
12819 1 /*search_cwd*/));
12820 xfree (path_to_try);
12821 if (abfd != NULL)
12822 return abfd;
12823 }
12824
12825 /* That didn't work, try debug-file-directory, which, despite its name,
12826 is a list of paths. */
12827
12828 if (*debug_file_directory == '\0')
12829 return NULL;
12830
12831 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12832 0 /*is_dwp*/, 1 /*search_cwd*/);
12833 }
12834
12835 /* This function is mapped across the sections and remembers the offset and
12836 size of each of the DWO debugging sections we are interested in. */
12837
12838 static void
12839 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12840 {
12841 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12842 const struct dwop_section_names *names = &dwop_section_names;
12843
12844 if (section_is_p (sectp->name, &names->abbrev_dwo))
12845 {
12846 dwo_sections->abbrev.s.section = sectp;
12847 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12848 }
12849 else if (section_is_p (sectp->name, &names->info_dwo))
12850 {
12851 dwo_sections->info.s.section = sectp;
12852 dwo_sections->info.size = bfd_get_section_size (sectp);
12853 }
12854 else if (section_is_p (sectp->name, &names->line_dwo))
12855 {
12856 dwo_sections->line.s.section = sectp;
12857 dwo_sections->line.size = bfd_get_section_size (sectp);
12858 }
12859 else if (section_is_p (sectp->name, &names->loc_dwo))
12860 {
12861 dwo_sections->loc.s.section = sectp;
12862 dwo_sections->loc.size = bfd_get_section_size (sectp);
12863 }
12864 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12865 {
12866 dwo_sections->macinfo.s.section = sectp;
12867 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12868 }
12869 else if (section_is_p (sectp->name, &names->macro_dwo))
12870 {
12871 dwo_sections->macro.s.section = sectp;
12872 dwo_sections->macro.size = bfd_get_section_size (sectp);
12873 }
12874 else if (section_is_p (sectp->name, &names->str_dwo))
12875 {
12876 dwo_sections->str.s.section = sectp;
12877 dwo_sections->str.size = bfd_get_section_size (sectp);
12878 }
12879 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12880 {
12881 dwo_sections->str_offsets.s.section = sectp;
12882 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12883 }
12884 else if (section_is_p (sectp->name, &names->types_dwo))
12885 {
12886 struct dwarf2_section_info type_section;
12887
12888 memset (&type_section, 0, sizeof (type_section));
12889 type_section.s.section = sectp;
12890 type_section.size = bfd_get_section_size (sectp);
12891 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
12892 &type_section);
12893 }
12894 }
12895
12896 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12897 by PER_CU. This is for the non-DWP case.
12898 The result is NULL if DWO_NAME can't be found. */
12899
12900 static struct dwo_file *
12901 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12902 const char *dwo_name, const char *comp_dir)
12903 {
12904 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12905 struct objfile *objfile = dwarf2_per_objfile->objfile;
12906
12907 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
12908 if (dbfd == NULL)
12909 {
12910 if (dwarf_read_debug)
12911 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12912 return NULL;
12913 }
12914
12915 /* We use a unique pointer here, despite the obstack allocation,
12916 because a dwo_file needs some cleanup if it is abandoned. */
12917 dwo_file_up dwo_file (OBSTACK_ZALLOC (&objfile->objfile_obstack,
12918 struct dwo_file));
12919 dwo_file->dwo_name = dwo_name;
12920 dwo_file->comp_dir = comp_dir;
12921 dwo_file->dbfd = dbfd.release ();
12922
12923 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
12924 &dwo_file->sections);
12925
12926 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
12927 dwo_file->cus);
12928
12929 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12930 dwo_file->sections.types, dwo_file->tus);
12931
12932 if (dwarf_read_debug)
12933 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12934
12935 return dwo_file.release ();
12936 }
12937
12938 /* This function is mapped across the sections and remembers the offset and
12939 size of each of the DWP debugging sections common to version 1 and 2 that
12940 we are interested in. */
12941
12942 static void
12943 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12944 void *dwp_file_ptr)
12945 {
12946 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12947 const struct dwop_section_names *names = &dwop_section_names;
12948 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12949
12950 /* Record the ELF section number for later lookup: this is what the
12951 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12952 gdb_assert (elf_section_nr < dwp_file->num_sections);
12953 dwp_file->elf_sections[elf_section_nr] = sectp;
12954
12955 /* Look for specific sections that we need. */
12956 if (section_is_p (sectp->name, &names->str_dwo))
12957 {
12958 dwp_file->sections.str.s.section = sectp;
12959 dwp_file->sections.str.size = bfd_get_section_size (sectp);
12960 }
12961 else if (section_is_p (sectp->name, &names->cu_index))
12962 {
12963 dwp_file->sections.cu_index.s.section = sectp;
12964 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
12965 }
12966 else if (section_is_p (sectp->name, &names->tu_index))
12967 {
12968 dwp_file->sections.tu_index.s.section = sectp;
12969 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
12970 }
12971 }
12972
12973 /* This function is mapped across the sections and remembers the offset and
12974 size of each of the DWP version 2 debugging sections that we are interested
12975 in. This is split into a separate function because we don't know if we
12976 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12977
12978 static void
12979 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12980 {
12981 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12982 const struct dwop_section_names *names = &dwop_section_names;
12983 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12984
12985 /* Record the ELF section number for later lookup: this is what the
12986 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12987 gdb_assert (elf_section_nr < dwp_file->num_sections);
12988 dwp_file->elf_sections[elf_section_nr] = sectp;
12989
12990 /* Look for specific sections that we need. */
12991 if (section_is_p (sectp->name, &names->abbrev_dwo))
12992 {
12993 dwp_file->sections.abbrev.s.section = sectp;
12994 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
12995 }
12996 else if (section_is_p (sectp->name, &names->info_dwo))
12997 {
12998 dwp_file->sections.info.s.section = sectp;
12999 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13000 }
13001 else if (section_is_p (sectp->name, &names->line_dwo))
13002 {
13003 dwp_file->sections.line.s.section = sectp;
13004 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13005 }
13006 else if (section_is_p (sectp->name, &names->loc_dwo))
13007 {
13008 dwp_file->sections.loc.s.section = sectp;
13009 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13010 }
13011 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13012 {
13013 dwp_file->sections.macinfo.s.section = sectp;
13014 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13015 }
13016 else if (section_is_p (sectp->name, &names->macro_dwo))
13017 {
13018 dwp_file->sections.macro.s.section = sectp;
13019 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13020 }
13021 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13022 {
13023 dwp_file->sections.str_offsets.s.section = sectp;
13024 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13025 }
13026 else if (section_is_p (sectp->name, &names->types_dwo))
13027 {
13028 dwp_file->sections.types.s.section = sectp;
13029 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13030 }
13031 }
13032
13033 /* Hash function for dwp_file loaded CUs/TUs. */
13034
13035 static hashval_t
13036 hash_dwp_loaded_cutus (const void *item)
13037 {
13038 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13039
13040 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13041 return dwo_unit->signature;
13042 }
13043
13044 /* Equality function for dwp_file loaded CUs/TUs. */
13045
13046 static int
13047 eq_dwp_loaded_cutus (const void *a, const void *b)
13048 {
13049 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13050 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13051
13052 return dua->signature == dub->signature;
13053 }
13054
13055 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13056
13057 static htab_t
13058 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13059 {
13060 return htab_create_alloc_ex (3,
13061 hash_dwp_loaded_cutus,
13062 eq_dwp_loaded_cutus,
13063 NULL,
13064 &objfile->objfile_obstack,
13065 hashtab_obstack_allocate,
13066 dummy_obstack_deallocate);
13067 }
13068
13069 /* Try to open DWP file FILE_NAME.
13070 The result is the bfd handle of the file.
13071 If there is a problem finding or opening the file, return NULL.
13072 Upon success, the canonicalized path of the file is stored in the bfd,
13073 same as symfile_bfd_open. */
13074
13075 static gdb_bfd_ref_ptr
13076 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13077 const char *file_name)
13078 {
13079 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13080 1 /*is_dwp*/,
13081 1 /*search_cwd*/));
13082 if (abfd != NULL)
13083 return abfd;
13084
13085 /* Work around upstream bug 15652.
13086 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13087 [Whether that's a "bug" is debatable, but it is getting in our way.]
13088 We have no real idea where the dwp file is, because gdb's realpath-ing
13089 of the executable's path may have discarded the needed info.
13090 [IWBN if the dwp file name was recorded in the executable, akin to
13091 .gnu_debuglink, but that doesn't exist yet.]
13092 Strip the directory from FILE_NAME and search again. */
13093 if (*debug_file_directory != '\0')
13094 {
13095 /* Don't implicitly search the current directory here.
13096 If the user wants to search "." to handle this case,
13097 it must be added to debug-file-directory. */
13098 return try_open_dwop_file (dwarf2_per_objfile,
13099 lbasename (file_name), 1 /*is_dwp*/,
13100 0 /*search_cwd*/);
13101 }
13102
13103 return NULL;
13104 }
13105
13106 /* Initialize the use of the DWP file for the current objfile.
13107 By convention the name of the DWP file is ${objfile}.dwp.
13108 The result is NULL if it can't be found. */
13109
13110 static struct dwp_file *
13111 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13112 {
13113 struct objfile *objfile = dwarf2_per_objfile->objfile;
13114 struct dwp_file *dwp_file;
13115
13116 /* Try to find first .dwp for the binary file before any symbolic links
13117 resolving. */
13118
13119 /* If the objfile is a debug file, find the name of the real binary
13120 file and get the name of dwp file from there. */
13121 std::string dwp_name;
13122 if (objfile->separate_debug_objfile_backlink != NULL)
13123 {
13124 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13125 const char *backlink_basename = lbasename (backlink->original_name);
13126
13127 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13128 }
13129 else
13130 dwp_name = objfile->original_name;
13131
13132 dwp_name += ".dwp";
13133
13134 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13135 if (dbfd == NULL
13136 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13137 {
13138 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13139 dwp_name = objfile_name (objfile);
13140 dwp_name += ".dwp";
13141 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13142 }
13143
13144 if (dbfd == NULL)
13145 {
13146 if (dwarf_read_debug)
13147 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13148 return NULL;
13149 }
13150 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
13151 dwp_file->name = bfd_get_filename (dbfd.get ());
13152 dwp_file->dbfd = dbfd.release ();
13153
13154 /* +1: section 0 is unused */
13155 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
13156 dwp_file->elf_sections =
13157 OBSTACK_CALLOC (&objfile->objfile_obstack,
13158 dwp_file->num_sections, asection *);
13159
13160 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_common_dwp_sections,
13161 dwp_file);
13162
13163 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 0);
13164
13165 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file, 1);
13166
13167 /* The DWP file version is stored in the hash table. Oh well. */
13168 if (dwp_file->cus && dwp_file->tus
13169 && dwp_file->cus->version != dwp_file->tus->version)
13170 {
13171 /* Technically speaking, we should try to limp along, but this is
13172 pretty bizarre. We use pulongest here because that's the established
13173 portability solution (e.g, we cannot use %u for uint32_t). */
13174 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13175 " TU version %s [in DWP file %s]"),
13176 pulongest (dwp_file->cus->version),
13177 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13178 }
13179
13180 if (dwp_file->cus)
13181 dwp_file->version = dwp_file->cus->version;
13182 else if (dwp_file->tus)
13183 dwp_file->version = dwp_file->tus->version;
13184 else
13185 dwp_file->version = 2;
13186
13187 if (dwp_file->version == 2)
13188 bfd_map_over_sections (dwp_file->dbfd, dwarf2_locate_v2_dwp_sections,
13189 dwp_file);
13190
13191 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13192 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13193
13194 if (dwarf_read_debug)
13195 {
13196 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13197 fprintf_unfiltered (gdb_stdlog,
13198 " %s CUs, %s TUs\n",
13199 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13200 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13201 }
13202
13203 return dwp_file;
13204 }
13205
13206 /* Wrapper around open_and_init_dwp_file, only open it once. */
13207
13208 static struct dwp_file *
13209 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13210 {
13211 if (! dwarf2_per_objfile->dwp_checked)
13212 {
13213 dwarf2_per_objfile->dwp_file
13214 = open_and_init_dwp_file (dwarf2_per_objfile);
13215 dwarf2_per_objfile->dwp_checked = 1;
13216 }
13217 return dwarf2_per_objfile->dwp_file;
13218 }
13219
13220 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13221 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13222 or in the DWP file for the objfile, referenced by THIS_UNIT.
13223 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13224 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13225
13226 This is called, for example, when wanting to read a variable with a
13227 complex location. Therefore we don't want to do file i/o for every call.
13228 Therefore we don't want to look for a DWO file on every call.
13229 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13230 then we check if we've already seen DWO_NAME, and only THEN do we check
13231 for a DWO file.
13232
13233 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13234 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13235
13236 static struct dwo_unit *
13237 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13238 const char *dwo_name, const char *comp_dir,
13239 ULONGEST signature, int is_debug_types)
13240 {
13241 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13242 struct objfile *objfile = dwarf2_per_objfile->objfile;
13243 const char *kind = is_debug_types ? "TU" : "CU";
13244 void **dwo_file_slot;
13245 struct dwo_file *dwo_file;
13246 struct dwp_file *dwp_file;
13247
13248 /* First see if there's a DWP file.
13249 If we have a DWP file but didn't find the DWO inside it, don't
13250 look for the original DWO file. It makes gdb behave differently
13251 depending on whether one is debugging in the build tree. */
13252
13253 dwp_file = get_dwp_file (dwarf2_per_objfile);
13254 if (dwp_file != NULL)
13255 {
13256 const struct dwp_hash_table *dwp_htab =
13257 is_debug_types ? dwp_file->tus : dwp_file->cus;
13258
13259 if (dwp_htab != NULL)
13260 {
13261 struct dwo_unit *dwo_cutu =
13262 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13263 signature, is_debug_types);
13264
13265 if (dwo_cutu != NULL)
13266 {
13267 if (dwarf_read_debug)
13268 {
13269 fprintf_unfiltered (gdb_stdlog,
13270 "Virtual DWO %s %s found: @%s\n",
13271 kind, hex_string (signature),
13272 host_address_to_string (dwo_cutu));
13273 }
13274 return dwo_cutu;
13275 }
13276 }
13277 }
13278 else
13279 {
13280 /* No DWP file, look for the DWO file. */
13281
13282 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13283 dwo_name, comp_dir);
13284 if (*dwo_file_slot == NULL)
13285 {
13286 /* Read in the file and build a table of the CUs/TUs it contains. */
13287 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13288 }
13289 /* NOTE: This will be NULL if unable to open the file. */
13290 dwo_file = (struct dwo_file *) *dwo_file_slot;
13291
13292 if (dwo_file != NULL)
13293 {
13294 struct dwo_unit *dwo_cutu = NULL;
13295
13296 if (is_debug_types && dwo_file->tus)
13297 {
13298 struct dwo_unit find_dwo_cutu;
13299
13300 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13301 find_dwo_cutu.signature = signature;
13302 dwo_cutu
13303 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13304 }
13305 else if (!is_debug_types && dwo_file->cus)
13306 {
13307 struct dwo_unit find_dwo_cutu;
13308
13309 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13310 find_dwo_cutu.signature = signature;
13311 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13312 &find_dwo_cutu);
13313 }
13314
13315 if (dwo_cutu != NULL)
13316 {
13317 if (dwarf_read_debug)
13318 {
13319 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13320 kind, dwo_name, hex_string (signature),
13321 host_address_to_string (dwo_cutu));
13322 }
13323 return dwo_cutu;
13324 }
13325 }
13326 }
13327
13328 /* We didn't find it. This could mean a dwo_id mismatch, or
13329 someone deleted the DWO/DWP file, or the search path isn't set up
13330 correctly to find the file. */
13331
13332 if (dwarf_read_debug)
13333 {
13334 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13335 kind, dwo_name, hex_string (signature));
13336 }
13337
13338 /* This is a warning and not a complaint because it can be caused by
13339 pilot error (e.g., user accidentally deleting the DWO). */
13340 {
13341 /* Print the name of the DWP file if we looked there, helps the user
13342 better diagnose the problem. */
13343 std::string dwp_text;
13344
13345 if (dwp_file != NULL)
13346 dwp_text = string_printf (" [in DWP file %s]",
13347 lbasename (dwp_file->name));
13348
13349 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13350 " [in module %s]"),
13351 kind, dwo_name, hex_string (signature),
13352 dwp_text.c_str (),
13353 this_unit->is_debug_types ? "TU" : "CU",
13354 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13355 }
13356 return NULL;
13357 }
13358
13359 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13360 See lookup_dwo_cutu_unit for details. */
13361
13362 static struct dwo_unit *
13363 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13364 const char *dwo_name, const char *comp_dir,
13365 ULONGEST signature)
13366 {
13367 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13368 }
13369
13370 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13371 See lookup_dwo_cutu_unit for details. */
13372
13373 static struct dwo_unit *
13374 lookup_dwo_type_unit (struct signatured_type *this_tu,
13375 const char *dwo_name, const char *comp_dir)
13376 {
13377 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13378 }
13379
13380 /* Traversal function for queue_and_load_all_dwo_tus. */
13381
13382 static int
13383 queue_and_load_dwo_tu (void **slot, void *info)
13384 {
13385 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13386 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13387 ULONGEST signature = dwo_unit->signature;
13388 struct signatured_type *sig_type =
13389 lookup_dwo_signatured_type (per_cu->cu, signature);
13390
13391 if (sig_type != NULL)
13392 {
13393 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13394
13395 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13396 a real dependency of PER_CU on SIG_TYPE. That is detected later
13397 while processing PER_CU. */
13398 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13399 load_full_type_unit (sig_cu);
13400 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13401 }
13402
13403 return 1;
13404 }
13405
13406 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13407 The DWO may have the only definition of the type, though it may not be
13408 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13409 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13410
13411 static void
13412 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13413 {
13414 struct dwo_unit *dwo_unit;
13415 struct dwo_file *dwo_file;
13416
13417 gdb_assert (!per_cu->is_debug_types);
13418 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13419 gdb_assert (per_cu->cu != NULL);
13420
13421 dwo_unit = per_cu->cu->dwo_unit;
13422 gdb_assert (dwo_unit != NULL);
13423
13424 dwo_file = dwo_unit->dwo_file;
13425 if (dwo_file->tus != NULL)
13426 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13427 }
13428
13429 /* Free all resources associated with DWO_FILE.
13430 Close the DWO file and munmap the sections. */
13431
13432 static void
13433 free_dwo_file (struct dwo_file *dwo_file)
13434 {
13435 /* Note: dbfd is NULL for virtual DWO files. */
13436 gdb_bfd_unref (dwo_file->dbfd);
13437
13438 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13439 }
13440
13441 /* Traversal function for free_dwo_files. */
13442
13443 static int
13444 free_dwo_file_from_slot (void **slot, void *info)
13445 {
13446 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
13447
13448 free_dwo_file (dwo_file);
13449
13450 return 1;
13451 }
13452
13453 /* Free all resources associated with DWO_FILES. */
13454
13455 static void
13456 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13457 {
13458 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
13459 }
13460 \f
13461 /* Read in various DIEs. */
13462
13463 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13464 Inherit only the children of the DW_AT_abstract_origin DIE not being
13465 already referenced by DW_AT_abstract_origin from the children of the
13466 current DIE. */
13467
13468 static void
13469 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13470 {
13471 struct die_info *child_die;
13472 sect_offset *offsetp;
13473 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13474 struct die_info *origin_die;
13475 /* Iterator of the ORIGIN_DIE children. */
13476 struct die_info *origin_child_die;
13477 struct attribute *attr;
13478 struct dwarf2_cu *origin_cu;
13479 struct pending **origin_previous_list_in_scope;
13480
13481 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13482 if (!attr)
13483 return;
13484
13485 /* Note that following die references may follow to a die in a
13486 different cu. */
13487
13488 origin_cu = cu;
13489 origin_die = follow_die_ref (die, attr, &origin_cu);
13490
13491 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13492 symbols in. */
13493 origin_previous_list_in_scope = origin_cu->list_in_scope;
13494 origin_cu->list_in_scope = cu->list_in_scope;
13495
13496 if (die->tag != origin_die->tag
13497 && !(die->tag == DW_TAG_inlined_subroutine
13498 && origin_die->tag == DW_TAG_subprogram))
13499 complaint (&symfile_complaints,
13500 _("DIE %s and its abstract origin %s have different tags"),
13501 sect_offset_str (die->sect_off),
13502 sect_offset_str (origin_die->sect_off));
13503
13504 std::vector<sect_offset> offsets;
13505
13506 for (child_die = die->child;
13507 child_die && child_die->tag;
13508 child_die = sibling_die (child_die))
13509 {
13510 struct die_info *child_origin_die;
13511 struct dwarf2_cu *child_origin_cu;
13512
13513 /* We are trying to process concrete instance entries:
13514 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13515 it's not relevant to our analysis here. i.e. detecting DIEs that are
13516 present in the abstract instance but not referenced in the concrete
13517 one. */
13518 if (child_die->tag == DW_TAG_call_site
13519 || child_die->tag == DW_TAG_GNU_call_site)
13520 continue;
13521
13522 /* For each CHILD_DIE, find the corresponding child of
13523 ORIGIN_DIE. If there is more than one layer of
13524 DW_AT_abstract_origin, follow them all; there shouldn't be,
13525 but GCC versions at least through 4.4 generate this (GCC PR
13526 40573). */
13527 child_origin_die = child_die;
13528 child_origin_cu = cu;
13529 while (1)
13530 {
13531 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13532 child_origin_cu);
13533 if (attr == NULL)
13534 break;
13535 child_origin_die = follow_die_ref (child_origin_die, attr,
13536 &child_origin_cu);
13537 }
13538
13539 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13540 counterpart may exist. */
13541 if (child_origin_die != child_die)
13542 {
13543 if (child_die->tag != child_origin_die->tag
13544 && !(child_die->tag == DW_TAG_inlined_subroutine
13545 && child_origin_die->tag == DW_TAG_subprogram))
13546 complaint (&symfile_complaints,
13547 _("Child DIE %s and its abstract origin %s have "
13548 "different tags"),
13549 sect_offset_str (child_die->sect_off),
13550 sect_offset_str (child_origin_die->sect_off));
13551 if (child_origin_die->parent != origin_die)
13552 complaint (&symfile_complaints,
13553 _("Child DIE %s and its abstract origin %s have "
13554 "different parents"),
13555 sect_offset_str (child_die->sect_off),
13556 sect_offset_str (child_origin_die->sect_off));
13557 else
13558 offsets.push_back (child_origin_die->sect_off);
13559 }
13560 }
13561 std::sort (offsets.begin (), offsets.end ());
13562 sect_offset *offsets_end = offsets.data () + offsets.size ();
13563 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13564 if (offsetp[-1] == *offsetp)
13565 complaint (&symfile_complaints,
13566 _("Multiple children of DIE %s refer "
13567 "to DIE %s as their abstract origin"),
13568 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13569
13570 offsetp = offsets.data ();
13571 origin_child_die = origin_die->child;
13572 while (origin_child_die && origin_child_die->tag)
13573 {
13574 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13575 while (offsetp < offsets_end
13576 && *offsetp < origin_child_die->sect_off)
13577 offsetp++;
13578 if (offsetp >= offsets_end
13579 || *offsetp > origin_child_die->sect_off)
13580 {
13581 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13582 Check whether we're already processing ORIGIN_CHILD_DIE.
13583 This can happen with mutually referenced abstract_origins.
13584 PR 16581. */
13585 if (!origin_child_die->in_process)
13586 process_die (origin_child_die, origin_cu);
13587 }
13588 origin_child_die = sibling_die (origin_child_die);
13589 }
13590 origin_cu->list_in_scope = origin_previous_list_in_scope;
13591 }
13592
13593 static void
13594 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13595 {
13596 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13597 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13598 struct context_stack *newobj;
13599 CORE_ADDR lowpc;
13600 CORE_ADDR highpc;
13601 struct die_info *child_die;
13602 struct attribute *attr, *call_line, *call_file;
13603 const char *name;
13604 CORE_ADDR baseaddr;
13605 struct block *block;
13606 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13607 std::vector<struct symbol *> template_args;
13608 struct template_symbol *templ_func = NULL;
13609
13610 if (inlined_func)
13611 {
13612 /* If we do not have call site information, we can't show the
13613 caller of this inlined function. That's too confusing, so
13614 only use the scope for local variables. */
13615 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13616 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13617 if (call_line == NULL || call_file == NULL)
13618 {
13619 read_lexical_block_scope (die, cu);
13620 return;
13621 }
13622 }
13623
13624 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13625
13626 name = dwarf2_name (die, cu);
13627
13628 /* Ignore functions with missing or empty names. These are actually
13629 illegal according to the DWARF standard. */
13630 if (name == NULL)
13631 {
13632 complaint (&symfile_complaints,
13633 _("missing name for subprogram DIE at %s"),
13634 sect_offset_str (die->sect_off));
13635 return;
13636 }
13637
13638 /* Ignore functions with missing or invalid low and high pc attributes. */
13639 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13640 <= PC_BOUNDS_INVALID)
13641 {
13642 attr = dwarf2_attr (die, DW_AT_external, cu);
13643 if (!attr || !DW_UNSND (attr))
13644 complaint (&symfile_complaints,
13645 _("cannot get low and high bounds "
13646 "for subprogram DIE at %s"),
13647 sect_offset_str (die->sect_off));
13648 return;
13649 }
13650
13651 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13652 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13653
13654 /* If we have any template arguments, then we must allocate a
13655 different sort of symbol. */
13656 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13657 {
13658 if (child_die->tag == DW_TAG_template_type_param
13659 || child_die->tag == DW_TAG_template_value_param)
13660 {
13661 templ_func = allocate_template_symbol (objfile);
13662 templ_func->subclass = SYMBOL_TEMPLATE;
13663 break;
13664 }
13665 }
13666
13667 newobj = push_context (0, lowpc);
13668 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13669 (struct symbol *) templ_func);
13670
13671 /* If there is a location expression for DW_AT_frame_base, record
13672 it. */
13673 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13674 if (attr)
13675 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13676
13677 /* If there is a location for the static link, record it. */
13678 newobj->static_link = NULL;
13679 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13680 if (attr)
13681 {
13682 newobj->static_link
13683 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13684 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13685 }
13686
13687 cu->list_in_scope = &local_symbols;
13688
13689 if (die->child != NULL)
13690 {
13691 child_die = die->child;
13692 while (child_die && child_die->tag)
13693 {
13694 if (child_die->tag == DW_TAG_template_type_param
13695 || child_die->tag == DW_TAG_template_value_param)
13696 {
13697 struct symbol *arg = new_symbol (child_die, NULL, cu);
13698
13699 if (arg != NULL)
13700 template_args.push_back (arg);
13701 }
13702 else
13703 process_die (child_die, cu);
13704 child_die = sibling_die (child_die);
13705 }
13706 }
13707
13708 inherit_abstract_dies (die, cu);
13709
13710 /* If we have a DW_AT_specification, we might need to import using
13711 directives from the context of the specification DIE. See the
13712 comment in determine_prefix. */
13713 if (cu->language == language_cplus
13714 && dwarf2_attr (die, DW_AT_specification, cu))
13715 {
13716 struct dwarf2_cu *spec_cu = cu;
13717 struct die_info *spec_die = die_specification (die, &spec_cu);
13718
13719 while (spec_die)
13720 {
13721 child_die = spec_die->child;
13722 while (child_die && child_die->tag)
13723 {
13724 if (child_die->tag == DW_TAG_imported_module)
13725 process_die (child_die, spec_cu);
13726 child_die = sibling_die (child_die);
13727 }
13728
13729 /* In some cases, GCC generates specification DIEs that
13730 themselves contain DW_AT_specification attributes. */
13731 spec_die = die_specification (spec_die, &spec_cu);
13732 }
13733 }
13734
13735 newobj = pop_context ();
13736 /* Make a block for the local symbols within. */
13737 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
13738 newobj->static_link, lowpc, highpc);
13739
13740 /* For C++, set the block's scope. */
13741 if ((cu->language == language_cplus
13742 || cu->language == language_fortran
13743 || cu->language == language_d
13744 || cu->language == language_rust)
13745 && cu->processing_has_namespace_info)
13746 block_set_scope (block, determine_prefix (die, cu),
13747 &objfile->objfile_obstack);
13748
13749 /* If we have address ranges, record them. */
13750 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13751
13752 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
13753
13754 /* Attach template arguments to function. */
13755 if (!template_args.empty ())
13756 {
13757 gdb_assert (templ_func != NULL);
13758
13759 templ_func->n_template_arguments = template_args.size ();
13760 templ_func->template_arguments
13761 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13762 templ_func->n_template_arguments);
13763 memcpy (templ_func->template_arguments,
13764 template_args.data (),
13765 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13766 }
13767
13768 /* In C++, we can have functions nested inside functions (e.g., when
13769 a function declares a class that has methods). This means that
13770 when we finish processing a function scope, we may need to go
13771 back to building a containing block's symbol lists. */
13772 local_symbols = newobj->locals;
13773 local_using_directives = newobj->local_using_directives;
13774
13775 /* If we've finished processing a top-level function, subsequent
13776 symbols go in the file symbol list. */
13777 if (outermost_context_p ())
13778 cu->list_in_scope = &file_symbols;
13779 }
13780
13781 /* Process all the DIES contained within a lexical block scope. Start
13782 a new scope, process the dies, and then close the scope. */
13783
13784 static void
13785 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13786 {
13787 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13788 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13789 struct context_stack *newobj;
13790 CORE_ADDR lowpc, highpc;
13791 struct die_info *child_die;
13792 CORE_ADDR baseaddr;
13793
13794 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13795
13796 /* Ignore blocks with missing or invalid low and high pc attributes. */
13797 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13798 as multiple lexical blocks? Handling children in a sane way would
13799 be nasty. Might be easier to properly extend generic blocks to
13800 describe ranges. */
13801 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13802 {
13803 case PC_BOUNDS_NOT_PRESENT:
13804 /* DW_TAG_lexical_block has no attributes, process its children as if
13805 there was no wrapping by that DW_TAG_lexical_block.
13806 GCC does no longer produces such DWARF since GCC r224161. */
13807 for (child_die = die->child;
13808 child_die != NULL && child_die->tag;
13809 child_die = sibling_die (child_die))
13810 process_die (child_die, cu);
13811 return;
13812 case PC_BOUNDS_INVALID:
13813 return;
13814 }
13815 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13816 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13817
13818 push_context (0, lowpc);
13819 if (die->child != NULL)
13820 {
13821 child_die = die->child;
13822 while (child_die && child_die->tag)
13823 {
13824 process_die (child_die, cu);
13825 child_die = sibling_die (child_die);
13826 }
13827 }
13828 inherit_abstract_dies (die, cu);
13829 newobj = pop_context ();
13830
13831 if (local_symbols != NULL || local_using_directives != NULL)
13832 {
13833 struct block *block
13834 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
13835 newobj->start_addr, highpc);
13836
13837 /* Note that recording ranges after traversing children, as we
13838 do here, means that recording a parent's ranges entails
13839 walking across all its children's ranges as they appear in
13840 the address map, which is quadratic behavior.
13841
13842 It would be nicer to record the parent's ranges before
13843 traversing its children, simply overriding whatever you find
13844 there. But since we don't even decide whether to create a
13845 block until after we've traversed its children, that's hard
13846 to do. */
13847 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13848 }
13849 local_symbols = newobj->locals;
13850 local_using_directives = newobj->local_using_directives;
13851 }
13852
13853 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13854
13855 static void
13856 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13857 {
13858 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13859 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13860 CORE_ADDR pc, baseaddr;
13861 struct attribute *attr;
13862 struct call_site *call_site, call_site_local;
13863 void **slot;
13864 int nparams;
13865 struct die_info *child_die;
13866
13867 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13868
13869 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13870 if (attr == NULL)
13871 {
13872 /* This was a pre-DWARF-5 GNU extension alias
13873 for DW_AT_call_return_pc. */
13874 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13875 }
13876 if (!attr)
13877 {
13878 complaint (&symfile_complaints,
13879 _("missing DW_AT_call_return_pc for DW_TAG_call_site "
13880 "DIE %s [in module %s]"),
13881 sect_offset_str (die->sect_off), objfile_name (objfile));
13882 return;
13883 }
13884 pc = attr_value_as_address (attr) + baseaddr;
13885 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13886
13887 if (cu->call_site_htab == NULL)
13888 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13889 NULL, &objfile->objfile_obstack,
13890 hashtab_obstack_allocate, NULL);
13891 call_site_local.pc = pc;
13892 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13893 if (*slot != NULL)
13894 {
13895 complaint (&symfile_complaints,
13896 _("Duplicate PC %s for DW_TAG_call_site "
13897 "DIE %s [in module %s]"),
13898 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13899 objfile_name (objfile));
13900 return;
13901 }
13902
13903 /* Count parameters at the caller. */
13904
13905 nparams = 0;
13906 for (child_die = die->child; child_die && child_die->tag;
13907 child_die = sibling_die (child_die))
13908 {
13909 if (child_die->tag != DW_TAG_call_site_parameter
13910 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13911 {
13912 complaint (&symfile_complaints,
13913 _("Tag %d is not DW_TAG_call_site_parameter in "
13914 "DW_TAG_call_site child DIE %s [in module %s]"),
13915 child_die->tag, sect_offset_str (child_die->sect_off),
13916 objfile_name (objfile));
13917 continue;
13918 }
13919
13920 nparams++;
13921 }
13922
13923 call_site
13924 = ((struct call_site *)
13925 obstack_alloc (&objfile->objfile_obstack,
13926 sizeof (*call_site)
13927 + (sizeof (*call_site->parameter) * (nparams - 1))));
13928 *slot = call_site;
13929 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13930 call_site->pc = pc;
13931
13932 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13933 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13934 {
13935 struct die_info *func_die;
13936
13937 /* Skip also over DW_TAG_inlined_subroutine. */
13938 for (func_die = die->parent;
13939 func_die && func_die->tag != DW_TAG_subprogram
13940 && func_die->tag != DW_TAG_subroutine_type;
13941 func_die = func_die->parent);
13942
13943 /* DW_AT_call_all_calls is a superset
13944 of DW_AT_call_all_tail_calls. */
13945 if (func_die
13946 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13947 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13948 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13949 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13950 {
13951 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13952 not complete. But keep CALL_SITE for look ups via call_site_htab,
13953 both the initial caller containing the real return address PC and
13954 the final callee containing the current PC of a chain of tail
13955 calls do not need to have the tail call list complete. But any
13956 function candidate for a virtual tail call frame searched via
13957 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13958 determined unambiguously. */
13959 }
13960 else
13961 {
13962 struct type *func_type = NULL;
13963
13964 if (func_die)
13965 func_type = get_die_type (func_die, cu);
13966 if (func_type != NULL)
13967 {
13968 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13969
13970 /* Enlist this call site to the function. */
13971 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13972 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13973 }
13974 else
13975 complaint (&symfile_complaints,
13976 _("Cannot find function owning DW_TAG_call_site "
13977 "DIE %s [in module %s]"),
13978 sect_offset_str (die->sect_off), objfile_name (objfile));
13979 }
13980 }
13981
13982 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13983 if (attr == NULL)
13984 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13985 if (attr == NULL)
13986 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13987 if (attr == NULL)
13988 {
13989 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13990 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13991 }
13992 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13993 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
13994 /* Keep NULL DWARF_BLOCK. */;
13995 else if (attr_form_is_block (attr))
13996 {
13997 struct dwarf2_locexpr_baton *dlbaton;
13998
13999 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14000 dlbaton->data = DW_BLOCK (attr)->data;
14001 dlbaton->size = DW_BLOCK (attr)->size;
14002 dlbaton->per_cu = cu->per_cu;
14003
14004 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14005 }
14006 else if (attr_form_is_ref (attr))
14007 {
14008 struct dwarf2_cu *target_cu = cu;
14009 struct die_info *target_die;
14010
14011 target_die = follow_die_ref (die, attr, &target_cu);
14012 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14013 if (die_is_declaration (target_die, target_cu))
14014 {
14015 const char *target_physname;
14016
14017 /* Prefer the mangled name; otherwise compute the demangled one. */
14018 target_physname = dw2_linkage_name (target_die, target_cu);
14019 if (target_physname == NULL)
14020 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14021 if (target_physname == NULL)
14022 complaint (&symfile_complaints,
14023 _("DW_AT_call_target target DIE has invalid "
14024 "physname, for referencing DIE %s [in module %s]"),
14025 sect_offset_str (die->sect_off), objfile_name (objfile));
14026 else
14027 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14028 }
14029 else
14030 {
14031 CORE_ADDR lowpc;
14032
14033 /* DW_AT_entry_pc should be preferred. */
14034 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14035 <= PC_BOUNDS_INVALID)
14036 complaint (&symfile_complaints,
14037 _("DW_AT_call_target target DIE has invalid "
14038 "low pc, for referencing DIE %s [in module %s]"),
14039 sect_offset_str (die->sect_off), objfile_name (objfile));
14040 else
14041 {
14042 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14043 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14044 }
14045 }
14046 }
14047 else
14048 complaint (&symfile_complaints,
14049 _("DW_TAG_call_site DW_AT_call_target is neither "
14050 "block nor reference, for DIE %s [in module %s]"),
14051 sect_offset_str (die->sect_off), objfile_name (objfile));
14052
14053 call_site->per_cu = cu->per_cu;
14054
14055 for (child_die = die->child;
14056 child_die && child_die->tag;
14057 child_die = sibling_die (child_die))
14058 {
14059 struct call_site_parameter *parameter;
14060 struct attribute *loc, *origin;
14061
14062 if (child_die->tag != DW_TAG_call_site_parameter
14063 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14064 {
14065 /* Already printed the complaint above. */
14066 continue;
14067 }
14068
14069 gdb_assert (call_site->parameter_count < nparams);
14070 parameter = &call_site->parameter[call_site->parameter_count];
14071
14072 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14073 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14074 register is contained in DW_AT_call_value. */
14075
14076 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14077 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14078 if (origin == NULL)
14079 {
14080 /* This was a pre-DWARF-5 GNU extension alias
14081 for DW_AT_call_parameter. */
14082 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14083 }
14084 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14085 {
14086 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14087
14088 sect_offset sect_off
14089 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14090 if (!offset_in_cu_p (&cu->header, sect_off))
14091 {
14092 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14093 binding can be done only inside one CU. Such referenced DIE
14094 therefore cannot be even moved to DW_TAG_partial_unit. */
14095 complaint (&symfile_complaints,
14096 _("DW_AT_call_parameter offset is not in CU for "
14097 "DW_TAG_call_site child DIE %s [in module %s]"),
14098 sect_offset_str (child_die->sect_off),
14099 objfile_name (objfile));
14100 continue;
14101 }
14102 parameter->u.param_cu_off
14103 = (cu_offset) (sect_off - cu->header.sect_off);
14104 }
14105 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14106 {
14107 complaint (&symfile_complaints,
14108 _("No DW_FORM_block* DW_AT_location for "
14109 "DW_TAG_call_site child DIE %s [in module %s]"),
14110 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14111 continue;
14112 }
14113 else
14114 {
14115 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14116 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14117 if (parameter->u.dwarf_reg != -1)
14118 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14119 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14120 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14121 &parameter->u.fb_offset))
14122 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14123 else
14124 {
14125 complaint (&symfile_complaints,
14126 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
14127 "for DW_FORM_block* DW_AT_location is supported for "
14128 "DW_TAG_call_site child DIE %s "
14129 "[in module %s]"),
14130 sect_offset_str (child_die->sect_off),
14131 objfile_name (objfile));
14132 continue;
14133 }
14134 }
14135
14136 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14137 if (attr == NULL)
14138 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14139 if (!attr_form_is_block (attr))
14140 {
14141 complaint (&symfile_complaints,
14142 _("No DW_FORM_block* DW_AT_call_value for "
14143 "DW_TAG_call_site child DIE %s [in module %s]"),
14144 sect_offset_str (child_die->sect_off),
14145 objfile_name (objfile));
14146 continue;
14147 }
14148 parameter->value = DW_BLOCK (attr)->data;
14149 parameter->value_size = DW_BLOCK (attr)->size;
14150
14151 /* Parameters are not pre-cleared by memset above. */
14152 parameter->data_value = NULL;
14153 parameter->data_value_size = 0;
14154 call_site->parameter_count++;
14155
14156 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14157 if (attr == NULL)
14158 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14159 if (attr)
14160 {
14161 if (!attr_form_is_block (attr))
14162 complaint (&symfile_complaints,
14163 _("No DW_FORM_block* DW_AT_call_data_value 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 else
14168 {
14169 parameter->data_value = DW_BLOCK (attr)->data;
14170 parameter->data_value_size = DW_BLOCK (attr)->size;
14171 }
14172 }
14173 }
14174 }
14175
14176 /* Helper function for read_variable. If DIE represents a virtual
14177 table, then return the type of the concrete object that is
14178 associated with the virtual table. Otherwise, return NULL. */
14179
14180 static struct type *
14181 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14182 {
14183 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14184 if (attr == NULL)
14185 return NULL;
14186
14187 /* Find the type DIE. */
14188 struct die_info *type_die = NULL;
14189 struct dwarf2_cu *type_cu = cu;
14190
14191 if (attr_form_is_ref (attr))
14192 type_die = follow_die_ref (die, attr, &type_cu);
14193 if (type_die == NULL)
14194 return NULL;
14195
14196 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14197 return NULL;
14198 return die_containing_type (type_die, type_cu);
14199 }
14200
14201 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14202
14203 static void
14204 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14205 {
14206 struct rust_vtable_symbol *storage = NULL;
14207
14208 if (cu->language == language_rust)
14209 {
14210 struct type *containing_type = rust_containing_type (die, cu);
14211
14212 if (containing_type != NULL)
14213 {
14214 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14215
14216 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14217 struct rust_vtable_symbol);
14218 initialize_objfile_symbol (storage);
14219 storage->concrete_type = containing_type;
14220 storage->subclass = SYMBOL_RUST_VTABLE;
14221 }
14222 }
14223
14224 new_symbol (die, NULL, cu, storage);
14225 }
14226
14227 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14228 reading .debug_rnglists.
14229 Callback's type should be:
14230 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14231 Return true if the attributes are present and valid, otherwise,
14232 return false. */
14233
14234 template <typename Callback>
14235 static bool
14236 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14237 Callback &&callback)
14238 {
14239 struct dwarf2_per_objfile *dwarf2_per_objfile
14240 = cu->per_cu->dwarf2_per_objfile;
14241 struct objfile *objfile = dwarf2_per_objfile->objfile;
14242 bfd *obfd = objfile->obfd;
14243 /* Base address selection entry. */
14244 CORE_ADDR base;
14245 int found_base;
14246 const gdb_byte *buffer;
14247 CORE_ADDR baseaddr;
14248 bool overflow = false;
14249
14250 found_base = cu->base_known;
14251 base = cu->base_address;
14252
14253 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14254 if (offset >= dwarf2_per_objfile->rnglists.size)
14255 {
14256 complaint (&symfile_complaints,
14257 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14258 offset);
14259 return false;
14260 }
14261 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14262
14263 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14264
14265 while (1)
14266 {
14267 /* Initialize it due to a false compiler warning. */
14268 CORE_ADDR range_beginning = 0, range_end = 0;
14269 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14270 + dwarf2_per_objfile->rnglists.size);
14271 unsigned int bytes_read;
14272
14273 if (buffer == buf_end)
14274 {
14275 overflow = true;
14276 break;
14277 }
14278 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14279 switch (rlet)
14280 {
14281 case DW_RLE_end_of_list:
14282 break;
14283 case DW_RLE_base_address:
14284 if (buffer + cu->header.addr_size > buf_end)
14285 {
14286 overflow = true;
14287 break;
14288 }
14289 base = read_address (obfd, buffer, cu, &bytes_read);
14290 found_base = 1;
14291 buffer += bytes_read;
14292 break;
14293 case DW_RLE_start_length:
14294 if (buffer + cu->header.addr_size > buf_end)
14295 {
14296 overflow = true;
14297 break;
14298 }
14299 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14300 buffer += bytes_read;
14301 range_end = (range_beginning
14302 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14303 buffer += bytes_read;
14304 if (buffer > buf_end)
14305 {
14306 overflow = true;
14307 break;
14308 }
14309 break;
14310 case DW_RLE_offset_pair:
14311 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14312 buffer += bytes_read;
14313 if (buffer > buf_end)
14314 {
14315 overflow = true;
14316 break;
14317 }
14318 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14319 buffer += bytes_read;
14320 if (buffer > buf_end)
14321 {
14322 overflow = true;
14323 break;
14324 }
14325 break;
14326 case DW_RLE_start_end:
14327 if (buffer + 2 * cu->header.addr_size > buf_end)
14328 {
14329 overflow = true;
14330 break;
14331 }
14332 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14333 buffer += bytes_read;
14334 range_end = read_address (obfd, buffer, cu, &bytes_read);
14335 buffer += bytes_read;
14336 break;
14337 default:
14338 complaint (&symfile_complaints,
14339 _("Invalid .debug_rnglists data (no base address)"));
14340 return false;
14341 }
14342 if (rlet == DW_RLE_end_of_list || overflow)
14343 break;
14344 if (rlet == DW_RLE_base_address)
14345 continue;
14346
14347 if (!found_base)
14348 {
14349 /* We have no valid base address for the ranges
14350 data. */
14351 complaint (&symfile_complaints,
14352 _("Invalid .debug_rnglists data (no base address)"));
14353 return false;
14354 }
14355
14356 if (range_beginning > range_end)
14357 {
14358 /* Inverted range entries are invalid. */
14359 complaint (&symfile_complaints,
14360 _("Invalid .debug_rnglists data (inverted range)"));
14361 return false;
14362 }
14363
14364 /* Empty range entries have no effect. */
14365 if (range_beginning == range_end)
14366 continue;
14367
14368 range_beginning += base;
14369 range_end += base;
14370
14371 /* A not-uncommon case of bad debug info.
14372 Don't pollute the addrmap with bad data. */
14373 if (range_beginning + baseaddr == 0
14374 && !dwarf2_per_objfile->has_section_at_zero)
14375 {
14376 complaint (&symfile_complaints,
14377 _(".debug_rnglists entry has start address of zero"
14378 " [in module %s]"), objfile_name (objfile));
14379 continue;
14380 }
14381
14382 callback (range_beginning, range_end);
14383 }
14384
14385 if (overflow)
14386 {
14387 complaint (&symfile_complaints,
14388 _("Offset %d is not terminated "
14389 "for DW_AT_ranges attribute"),
14390 offset);
14391 return false;
14392 }
14393
14394 return true;
14395 }
14396
14397 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14398 Callback's type should be:
14399 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14400 Return 1 if the attributes are present and valid, otherwise, return 0. */
14401
14402 template <typename Callback>
14403 static int
14404 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14405 Callback &&callback)
14406 {
14407 struct dwarf2_per_objfile *dwarf2_per_objfile
14408 = cu->per_cu->dwarf2_per_objfile;
14409 struct objfile *objfile = dwarf2_per_objfile->objfile;
14410 struct comp_unit_head *cu_header = &cu->header;
14411 bfd *obfd = objfile->obfd;
14412 unsigned int addr_size = cu_header->addr_size;
14413 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14414 /* Base address selection entry. */
14415 CORE_ADDR base;
14416 int found_base;
14417 unsigned int dummy;
14418 const gdb_byte *buffer;
14419 CORE_ADDR baseaddr;
14420
14421 if (cu_header->version >= 5)
14422 return dwarf2_rnglists_process (offset, cu, callback);
14423
14424 found_base = cu->base_known;
14425 base = cu->base_address;
14426
14427 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14428 if (offset >= dwarf2_per_objfile->ranges.size)
14429 {
14430 complaint (&symfile_complaints,
14431 _("Offset %d out of bounds for DW_AT_ranges attribute"),
14432 offset);
14433 return 0;
14434 }
14435 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14436
14437 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14438
14439 while (1)
14440 {
14441 CORE_ADDR range_beginning, range_end;
14442
14443 range_beginning = read_address (obfd, buffer, cu, &dummy);
14444 buffer += addr_size;
14445 range_end = read_address (obfd, buffer, cu, &dummy);
14446 buffer += addr_size;
14447 offset += 2 * addr_size;
14448
14449 /* An end of list marker is a pair of zero addresses. */
14450 if (range_beginning == 0 && range_end == 0)
14451 /* Found the end of list entry. */
14452 break;
14453
14454 /* Each base address selection entry is a pair of 2 values.
14455 The first is the largest possible address, the second is
14456 the base address. Check for a base address here. */
14457 if ((range_beginning & mask) == mask)
14458 {
14459 /* If we found the largest possible address, then we already
14460 have the base address in range_end. */
14461 base = range_end;
14462 found_base = 1;
14463 continue;
14464 }
14465
14466 if (!found_base)
14467 {
14468 /* We have no valid base address for the ranges
14469 data. */
14470 complaint (&symfile_complaints,
14471 _("Invalid .debug_ranges data (no base address)"));
14472 return 0;
14473 }
14474
14475 if (range_beginning > range_end)
14476 {
14477 /* Inverted range entries are invalid. */
14478 complaint (&symfile_complaints,
14479 _("Invalid .debug_ranges data (inverted range)"));
14480 return 0;
14481 }
14482
14483 /* Empty range entries have no effect. */
14484 if (range_beginning == range_end)
14485 continue;
14486
14487 range_beginning += base;
14488 range_end += base;
14489
14490 /* A not-uncommon case of bad debug info.
14491 Don't pollute the addrmap with bad data. */
14492 if (range_beginning + baseaddr == 0
14493 && !dwarf2_per_objfile->has_section_at_zero)
14494 {
14495 complaint (&symfile_complaints,
14496 _(".debug_ranges entry has start address of zero"
14497 " [in module %s]"), objfile_name (objfile));
14498 continue;
14499 }
14500
14501 callback (range_beginning, range_end);
14502 }
14503
14504 return 1;
14505 }
14506
14507 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14508 Return 1 if the attributes are present and valid, otherwise, return 0.
14509 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14510
14511 static int
14512 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14513 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14514 struct partial_symtab *ranges_pst)
14515 {
14516 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14517 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14518 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14519 SECT_OFF_TEXT (objfile));
14520 int low_set = 0;
14521 CORE_ADDR low = 0;
14522 CORE_ADDR high = 0;
14523 int retval;
14524
14525 retval = dwarf2_ranges_process (offset, cu,
14526 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14527 {
14528 if (ranges_pst != NULL)
14529 {
14530 CORE_ADDR lowpc;
14531 CORE_ADDR highpc;
14532
14533 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14534 range_beginning + baseaddr);
14535 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14536 range_end + baseaddr);
14537 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14538 ranges_pst);
14539 }
14540
14541 /* FIXME: This is recording everything as a low-high
14542 segment of consecutive addresses. We should have a
14543 data structure for discontiguous block ranges
14544 instead. */
14545 if (! low_set)
14546 {
14547 low = range_beginning;
14548 high = range_end;
14549 low_set = 1;
14550 }
14551 else
14552 {
14553 if (range_beginning < low)
14554 low = range_beginning;
14555 if (range_end > high)
14556 high = range_end;
14557 }
14558 });
14559 if (!retval)
14560 return 0;
14561
14562 if (! low_set)
14563 /* If the first entry is an end-of-list marker, the range
14564 describes an empty scope, i.e. no instructions. */
14565 return 0;
14566
14567 if (low_return)
14568 *low_return = low;
14569 if (high_return)
14570 *high_return = high;
14571 return 1;
14572 }
14573
14574 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14575 definition for the return value. *LOWPC and *HIGHPC are set iff
14576 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14577
14578 static enum pc_bounds_kind
14579 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14580 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14581 struct partial_symtab *pst)
14582 {
14583 struct dwarf2_per_objfile *dwarf2_per_objfile
14584 = cu->per_cu->dwarf2_per_objfile;
14585 struct attribute *attr;
14586 struct attribute *attr_high;
14587 CORE_ADDR low = 0;
14588 CORE_ADDR high = 0;
14589 enum pc_bounds_kind ret;
14590
14591 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14592 if (attr_high)
14593 {
14594 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14595 if (attr)
14596 {
14597 low = attr_value_as_address (attr);
14598 high = attr_value_as_address (attr_high);
14599 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14600 high += low;
14601 }
14602 else
14603 /* Found high w/o low attribute. */
14604 return PC_BOUNDS_INVALID;
14605
14606 /* Found consecutive range of addresses. */
14607 ret = PC_BOUNDS_HIGH_LOW;
14608 }
14609 else
14610 {
14611 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14612 if (attr != NULL)
14613 {
14614 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14615 We take advantage of the fact that DW_AT_ranges does not appear
14616 in DW_TAG_compile_unit of DWO files. */
14617 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14618 unsigned int ranges_offset = (DW_UNSND (attr)
14619 + (need_ranges_base
14620 ? cu->ranges_base
14621 : 0));
14622
14623 /* Value of the DW_AT_ranges attribute is the offset in the
14624 .debug_ranges section. */
14625 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14626 return PC_BOUNDS_INVALID;
14627 /* Found discontinuous range of addresses. */
14628 ret = PC_BOUNDS_RANGES;
14629 }
14630 else
14631 return PC_BOUNDS_NOT_PRESENT;
14632 }
14633
14634 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14635 if (high <= low)
14636 return PC_BOUNDS_INVALID;
14637
14638 /* When using the GNU linker, .gnu.linkonce. sections are used to
14639 eliminate duplicate copies of functions and vtables and such.
14640 The linker will arbitrarily choose one and discard the others.
14641 The AT_*_pc values for such functions refer to local labels in
14642 these sections. If the section from that file was discarded, the
14643 labels are not in the output, so the relocs get a value of 0.
14644 If this is a discarded function, mark the pc bounds as invalid,
14645 so that GDB will ignore it. */
14646 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14647 return PC_BOUNDS_INVALID;
14648
14649 *lowpc = low;
14650 if (highpc)
14651 *highpc = high;
14652 return ret;
14653 }
14654
14655 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14656 its low and high PC addresses. Do nothing if these addresses could not
14657 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14658 and HIGHPC to the high address if greater than HIGHPC. */
14659
14660 static void
14661 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14662 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14663 struct dwarf2_cu *cu)
14664 {
14665 CORE_ADDR low, high;
14666 struct die_info *child = die->child;
14667
14668 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14669 {
14670 *lowpc = std::min (*lowpc, low);
14671 *highpc = std::max (*highpc, high);
14672 }
14673
14674 /* If the language does not allow nested subprograms (either inside
14675 subprograms or lexical blocks), we're done. */
14676 if (cu->language != language_ada)
14677 return;
14678
14679 /* Check all the children of the given DIE. If it contains nested
14680 subprograms, then check their pc bounds. Likewise, we need to
14681 check lexical blocks as well, as they may also contain subprogram
14682 definitions. */
14683 while (child && child->tag)
14684 {
14685 if (child->tag == DW_TAG_subprogram
14686 || child->tag == DW_TAG_lexical_block)
14687 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14688 child = sibling_die (child);
14689 }
14690 }
14691
14692 /* Get the low and high pc's represented by the scope DIE, and store
14693 them in *LOWPC and *HIGHPC. If the correct values can't be
14694 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14695
14696 static void
14697 get_scope_pc_bounds (struct die_info *die,
14698 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14699 struct dwarf2_cu *cu)
14700 {
14701 CORE_ADDR best_low = (CORE_ADDR) -1;
14702 CORE_ADDR best_high = (CORE_ADDR) 0;
14703 CORE_ADDR current_low, current_high;
14704
14705 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14706 >= PC_BOUNDS_RANGES)
14707 {
14708 best_low = current_low;
14709 best_high = current_high;
14710 }
14711 else
14712 {
14713 struct die_info *child = die->child;
14714
14715 while (child && child->tag)
14716 {
14717 switch (child->tag) {
14718 case DW_TAG_subprogram:
14719 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14720 break;
14721 case DW_TAG_namespace:
14722 case DW_TAG_module:
14723 /* FIXME: carlton/2004-01-16: Should we do this for
14724 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14725 that current GCC's always emit the DIEs corresponding
14726 to definitions of methods of classes as children of a
14727 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14728 the DIEs giving the declarations, which could be
14729 anywhere). But I don't see any reason why the
14730 standards says that they have to be there. */
14731 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14732
14733 if (current_low != ((CORE_ADDR) -1))
14734 {
14735 best_low = std::min (best_low, current_low);
14736 best_high = std::max (best_high, current_high);
14737 }
14738 break;
14739 default:
14740 /* Ignore. */
14741 break;
14742 }
14743
14744 child = sibling_die (child);
14745 }
14746 }
14747
14748 *lowpc = best_low;
14749 *highpc = best_high;
14750 }
14751
14752 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14753 in DIE. */
14754
14755 static void
14756 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14757 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14758 {
14759 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14760 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14761 struct attribute *attr;
14762 struct attribute *attr_high;
14763
14764 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14765 if (attr_high)
14766 {
14767 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14768 if (attr)
14769 {
14770 CORE_ADDR low = attr_value_as_address (attr);
14771 CORE_ADDR high = attr_value_as_address (attr_high);
14772
14773 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14774 high += low;
14775
14776 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14777 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14778 record_block_range (block, low, high - 1);
14779 }
14780 }
14781
14782 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14783 if (attr)
14784 {
14785 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14786 We take advantage of the fact that DW_AT_ranges does not appear
14787 in DW_TAG_compile_unit of DWO files. */
14788 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14789
14790 /* The value of the DW_AT_ranges attribute is the offset of the
14791 address range list in the .debug_ranges section. */
14792 unsigned long offset = (DW_UNSND (attr)
14793 + (need_ranges_base ? cu->ranges_base : 0));
14794
14795 dwarf2_ranges_process (offset, cu,
14796 [&] (CORE_ADDR start, CORE_ADDR end)
14797 {
14798 start += baseaddr;
14799 end += baseaddr;
14800 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14801 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14802 record_block_range (block, start, end - 1);
14803 });
14804 }
14805 }
14806
14807 /* Check whether the producer field indicates either of GCC < 4.6, or the
14808 Intel C/C++ compiler, and cache the result in CU. */
14809
14810 static void
14811 check_producer (struct dwarf2_cu *cu)
14812 {
14813 int major, minor;
14814
14815 if (cu->producer == NULL)
14816 {
14817 /* For unknown compilers expect their behavior is DWARF version
14818 compliant.
14819
14820 GCC started to support .debug_types sections by -gdwarf-4 since
14821 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14822 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14823 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14824 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14825 }
14826 else if (producer_is_gcc (cu->producer, &major, &minor))
14827 {
14828 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14829 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14830 }
14831 else if (producer_is_icc (cu->producer, &major, &minor))
14832 cu->producer_is_icc_lt_14 = major < 14;
14833 else
14834 {
14835 /* For other non-GCC compilers, expect their behavior is DWARF version
14836 compliant. */
14837 }
14838
14839 cu->checked_producer = 1;
14840 }
14841
14842 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14843 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14844 during 4.6.0 experimental. */
14845
14846 static int
14847 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14848 {
14849 if (!cu->checked_producer)
14850 check_producer (cu);
14851
14852 return cu->producer_is_gxx_lt_4_6;
14853 }
14854
14855 /* Return the default accessibility type if it is not overriden by
14856 DW_AT_accessibility. */
14857
14858 static enum dwarf_access_attribute
14859 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14860 {
14861 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14862 {
14863 /* The default DWARF 2 accessibility for members is public, the default
14864 accessibility for inheritance is private. */
14865
14866 if (die->tag != DW_TAG_inheritance)
14867 return DW_ACCESS_public;
14868 else
14869 return DW_ACCESS_private;
14870 }
14871 else
14872 {
14873 /* DWARF 3+ defines the default accessibility a different way. The same
14874 rules apply now for DW_TAG_inheritance as for the members and it only
14875 depends on the container kind. */
14876
14877 if (die->parent->tag == DW_TAG_class_type)
14878 return DW_ACCESS_private;
14879 else
14880 return DW_ACCESS_public;
14881 }
14882 }
14883
14884 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14885 offset. If the attribute was not found return 0, otherwise return
14886 1. If it was found but could not properly be handled, set *OFFSET
14887 to 0. */
14888
14889 static int
14890 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14891 LONGEST *offset)
14892 {
14893 struct attribute *attr;
14894
14895 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14896 if (attr != NULL)
14897 {
14898 *offset = 0;
14899
14900 /* Note that we do not check for a section offset first here.
14901 This is because DW_AT_data_member_location is new in DWARF 4,
14902 so if we see it, we can assume that a constant form is really
14903 a constant and not a section offset. */
14904 if (attr_form_is_constant (attr))
14905 *offset = dwarf2_get_attr_constant_value (attr, 0);
14906 else if (attr_form_is_section_offset (attr))
14907 dwarf2_complex_location_expr_complaint ();
14908 else if (attr_form_is_block (attr))
14909 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14910 else
14911 dwarf2_complex_location_expr_complaint ();
14912
14913 return 1;
14914 }
14915
14916 return 0;
14917 }
14918
14919 /* Add an aggregate field to the field list. */
14920
14921 static void
14922 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14923 struct dwarf2_cu *cu)
14924 {
14925 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14926 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14927 struct nextfield *new_field;
14928 struct attribute *attr;
14929 struct field *fp;
14930 const char *fieldname = "";
14931
14932 if (die->tag == DW_TAG_inheritance)
14933 {
14934 fip->baseclasses.emplace_back ();
14935 new_field = &fip->baseclasses.back ();
14936 }
14937 else
14938 {
14939 fip->fields.emplace_back ();
14940 new_field = &fip->fields.back ();
14941 }
14942
14943 fip->nfields++;
14944
14945 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14946 if (attr)
14947 new_field->accessibility = DW_UNSND (attr);
14948 else
14949 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14950 if (new_field->accessibility != DW_ACCESS_public)
14951 fip->non_public_fields = 1;
14952
14953 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14954 if (attr)
14955 new_field->virtuality = DW_UNSND (attr);
14956 else
14957 new_field->virtuality = DW_VIRTUALITY_none;
14958
14959 fp = &new_field->field;
14960
14961 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14962 {
14963 LONGEST offset;
14964
14965 /* Data member other than a C++ static data member. */
14966
14967 /* Get type of field. */
14968 fp->type = die_type (die, cu);
14969
14970 SET_FIELD_BITPOS (*fp, 0);
14971
14972 /* Get bit size of field (zero if none). */
14973 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14974 if (attr)
14975 {
14976 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14977 }
14978 else
14979 {
14980 FIELD_BITSIZE (*fp) = 0;
14981 }
14982
14983 /* Get bit offset of field. */
14984 if (handle_data_member_location (die, cu, &offset))
14985 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14986 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14987 if (attr)
14988 {
14989 if (gdbarch_bits_big_endian (gdbarch))
14990 {
14991 /* For big endian bits, the DW_AT_bit_offset gives the
14992 additional bit offset from the MSB of the containing
14993 anonymous object to the MSB of the field. We don't
14994 have to do anything special since we don't need to
14995 know the size of the anonymous object. */
14996 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14997 }
14998 else
14999 {
15000 /* For little endian bits, compute the bit offset to the
15001 MSB of the anonymous object, subtract off the number of
15002 bits from the MSB of the field to the MSB of the
15003 object, and then subtract off the number of bits of
15004 the field itself. The result is the bit offset of
15005 the LSB of the field. */
15006 int anonymous_size;
15007 int bit_offset = DW_UNSND (attr);
15008
15009 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15010 if (attr)
15011 {
15012 /* The size of the anonymous object containing
15013 the bit field is explicit, so use the
15014 indicated size (in bytes). */
15015 anonymous_size = DW_UNSND (attr);
15016 }
15017 else
15018 {
15019 /* The size of the anonymous object containing
15020 the bit field must be inferred from the type
15021 attribute of the data member containing the
15022 bit field. */
15023 anonymous_size = TYPE_LENGTH (fp->type);
15024 }
15025 SET_FIELD_BITPOS (*fp,
15026 (FIELD_BITPOS (*fp)
15027 + anonymous_size * bits_per_byte
15028 - bit_offset - FIELD_BITSIZE (*fp)));
15029 }
15030 }
15031 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15032 if (attr != NULL)
15033 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15034 + dwarf2_get_attr_constant_value (attr, 0)));
15035
15036 /* Get name of field. */
15037 fieldname = dwarf2_name (die, cu);
15038 if (fieldname == NULL)
15039 fieldname = "";
15040
15041 /* The name is already allocated along with this objfile, so we don't
15042 need to duplicate it for the type. */
15043 fp->name = fieldname;
15044
15045 /* Change accessibility for artificial fields (e.g. virtual table
15046 pointer or virtual base class pointer) to private. */
15047 if (dwarf2_attr (die, DW_AT_artificial, cu))
15048 {
15049 FIELD_ARTIFICIAL (*fp) = 1;
15050 new_field->accessibility = DW_ACCESS_private;
15051 fip->non_public_fields = 1;
15052 }
15053 }
15054 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15055 {
15056 /* C++ static member. */
15057
15058 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15059 is a declaration, but all versions of G++ as of this writing
15060 (so through at least 3.2.1) incorrectly generate
15061 DW_TAG_variable tags. */
15062
15063 const char *physname;
15064
15065 /* Get name of field. */
15066 fieldname = dwarf2_name (die, cu);
15067 if (fieldname == NULL)
15068 return;
15069
15070 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15071 if (attr
15072 /* Only create a symbol if this is an external value.
15073 new_symbol checks this and puts the value in the global symbol
15074 table, which we want. If it is not external, new_symbol
15075 will try to put the value in cu->list_in_scope which is wrong. */
15076 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15077 {
15078 /* A static const member, not much different than an enum as far as
15079 we're concerned, except that we can support more types. */
15080 new_symbol (die, NULL, cu);
15081 }
15082
15083 /* Get physical name. */
15084 physname = dwarf2_physname (fieldname, die, cu);
15085
15086 /* The name is already allocated along with this objfile, so we don't
15087 need to duplicate it for the type. */
15088 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15089 FIELD_TYPE (*fp) = die_type (die, cu);
15090 FIELD_NAME (*fp) = fieldname;
15091 }
15092 else if (die->tag == DW_TAG_inheritance)
15093 {
15094 LONGEST offset;
15095
15096 /* C++ base class field. */
15097 if (handle_data_member_location (die, cu, &offset))
15098 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15099 FIELD_BITSIZE (*fp) = 0;
15100 FIELD_TYPE (*fp) = die_type (die, cu);
15101 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
15102 }
15103 else if (die->tag == DW_TAG_variant_part)
15104 {
15105 /* process_structure_scope will treat this DIE as a union. */
15106 process_structure_scope (die, cu);
15107
15108 /* The variant part is relative to the start of the enclosing
15109 structure. */
15110 SET_FIELD_BITPOS (*fp, 0);
15111 fp->type = get_die_type (die, cu);
15112 fp->artificial = 1;
15113 fp->name = "<<variant>>";
15114 }
15115 else
15116 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15117 }
15118
15119 /* Can the type given by DIE define another type? */
15120
15121 static bool
15122 type_can_define_types (const struct die_info *die)
15123 {
15124 switch (die->tag)
15125 {
15126 case DW_TAG_typedef:
15127 case DW_TAG_class_type:
15128 case DW_TAG_structure_type:
15129 case DW_TAG_union_type:
15130 case DW_TAG_enumeration_type:
15131 return true;
15132
15133 default:
15134 return false;
15135 }
15136 }
15137
15138 /* Add a type definition defined in the scope of the FIP's class. */
15139
15140 static void
15141 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15142 struct dwarf2_cu *cu)
15143 {
15144 struct decl_field fp;
15145 memset (&fp, 0, sizeof (fp));
15146
15147 gdb_assert (type_can_define_types (die));
15148
15149 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15150 fp.name = dwarf2_name (die, cu);
15151 fp.type = read_type_die (die, cu);
15152
15153 /* Save accessibility. */
15154 enum dwarf_access_attribute accessibility;
15155 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15156 if (attr != NULL)
15157 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15158 else
15159 accessibility = dwarf2_default_access_attribute (die, cu);
15160 switch (accessibility)
15161 {
15162 case DW_ACCESS_public:
15163 /* The assumed value if neither private nor protected. */
15164 break;
15165 case DW_ACCESS_private:
15166 fp.is_private = 1;
15167 break;
15168 case DW_ACCESS_protected:
15169 fp.is_protected = 1;
15170 break;
15171 default:
15172 complaint (&symfile_complaints,
15173 _("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15174 }
15175
15176 if (die->tag == DW_TAG_typedef)
15177 fip->typedef_field_list.push_back (fp);
15178 else
15179 fip->nested_types_list.push_back (fp);
15180 }
15181
15182 /* Create the vector of fields, and attach it to the type. */
15183
15184 static void
15185 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15186 struct dwarf2_cu *cu)
15187 {
15188 int nfields = fip->nfields;
15189
15190 /* Record the field count, allocate space for the array of fields,
15191 and create blank accessibility bitfields if necessary. */
15192 TYPE_NFIELDS (type) = nfields;
15193 TYPE_FIELDS (type) = (struct field *)
15194 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15195
15196 if (fip->non_public_fields && cu->language != language_ada)
15197 {
15198 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15199
15200 TYPE_FIELD_PRIVATE_BITS (type) =
15201 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15202 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15203
15204 TYPE_FIELD_PROTECTED_BITS (type) =
15205 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15206 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15207
15208 TYPE_FIELD_IGNORE_BITS (type) =
15209 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15210 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15211 }
15212
15213 /* If the type has baseclasses, allocate and clear a bit vector for
15214 TYPE_FIELD_VIRTUAL_BITS. */
15215 if (!fip->baseclasses.empty () && cu->language != language_ada)
15216 {
15217 int num_bytes = B_BYTES (fip->baseclasses.size ());
15218 unsigned char *pointer;
15219
15220 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15221 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15222 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15223 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15224 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15225 }
15226
15227 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15228 {
15229 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15230
15231 for (int index = 0; index < nfields; ++index)
15232 {
15233 struct nextfield &field = fip->fields[index];
15234
15235 if (field.variant.is_discriminant)
15236 di->discriminant_index = index;
15237 else if (field.variant.default_branch)
15238 di->default_index = index;
15239 else
15240 di->discriminants[index] = field.variant.discriminant_value;
15241 }
15242 }
15243
15244 /* Copy the saved-up fields into the field vector. */
15245 for (int i = 0; i < nfields; ++i)
15246 {
15247 struct nextfield &field
15248 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15249 : fip->fields[i - fip->baseclasses.size ()]);
15250
15251 TYPE_FIELD (type, i) = field.field;
15252 switch (field.accessibility)
15253 {
15254 case DW_ACCESS_private:
15255 if (cu->language != language_ada)
15256 SET_TYPE_FIELD_PRIVATE (type, i);
15257 break;
15258
15259 case DW_ACCESS_protected:
15260 if (cu->language != language_ada)
15261 SET_TYPE_FIELD_PROTECTED (type, i);
15262 break;
15263
15264 case DW_ACCESS_public:
15265 break;
15266
15267 default:
15268 /* Unknown accessibility. Complain and treat it as public. */
15269 {
15270 complaint (&symfile_complaints, _("unsupported accessibility %d"),
15271 field.accessibility);
15272 }
15273 break;
15274 }
15275 if (i < fip->baseclasses.size ())
15276 {
15277 switch (field.virtuality)
15278 {
15279 case DW_VIRTUALITY_virtual:
15280 case DW_VIRTUALITY_pure_virtual:
15281 if (cu->language == language_ada)
15282 error (_("unexpected virtuality in component of Ada type"));
15283 SET_TYPE_FIELD_VIRTUAL (type, i);
15284 break;
15285 }
15286 }
15287 }
15288 }
15289
15290 /* Return true if this member function is a constructor, false
15291 otherwise. */
15292
15293 static int
15294 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15295 {
15296 const char *fieldname;
15297 const char *type_name;
15298 int len;
15299
15300 if (die->parent == NULL)
15301 return 0;
15302
15303 if (die->parent->tag != DW_TAG_structure_type
15304 && die->parent->tag != DW_TAG_union_type
15305 && die->parent->tag != DW_TAG_class_type)
15306 return 0;
15307
15308 fieldname = dwarf2_name (die, cu);
15309 type_name = dwarf2_name (die->parent, cu);
15310 if (fieldname == NULL || type_name == NULL)
15311 return 0;
15312
15313 len = strlen (fieldname);
15314 return (strncmp (fieldname, type_name, len) == 0
15315 && (type_name[len] == '\0' || type_name[len] == '<'));
15316 }
15317
15318 /* Add a member function to the proper fieldlist. */
15319
15320 static void
15321 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15322 struct type *type, struct dwarf2_cu *cu)
15323 {
15324 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15325 struct attribute *attr;
15326 int i;
15327 struct fnfieldlist *flp = nullptr;
15328 struct fn_field *fnp;
15329 const char *fieldname;
15330 struct type *this_type;
15331 enum dwarf_access_attribute accessibility;
15332
15333 if (cu->language == language_ada)
15334 error (_("unexpected member function in Ada type"));
15335
15336 /* Get name of member function. */
15337 fieldname = dwarf2_name (die, cu);
15338 if (fieldname == NULL)
15339 return;
15340
15341 /* Look up member function name in fieldlist. */
15342 for (i = 0; i < fip->fnfieldlists.size (); i++)
15343 {
15344 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15345 {
15346 flp = &fip->fnfieldlists[i];
15347 break;
15348 }
15349 }
15350
15351 /* Create a new fnfieldlist if necessary. */
15352 if (flp == nullptr)
15353 {
15354 fip->fnfieldlists.emplace_back ();
15355 flp = &fip->fnfieldlists.back ();
15356 flp->name = fieldname;
15357 i = fip->fnfieldlists.size () - 1;
15358 }
15359
15360 /* Create a new member function field and add it to the vector of
15361 fnfieldlists. */
15362 flp->fnfields.emplace_back ();
15363 fnp = &flp->fnfields.back ();
15364
15365 /* Delay processing of the physname until later. */
15366 if (cu->language == language_cplus)
15367 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15368 die, cu);
15369 else
15370 {
15371 const char *physname = dwarf2_physname (fieldname, die, cu);
15372 fnp->physname = physname ? physname : "";
15373 }
15374
15375 fnp->type = alloc_type (objfile);
15376 this_type = read_type_die (die, cu);
15377 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15378 {
15379 int nparams = TYPE_NFIELDS (this_type);
15380
15381 /* TYPE is the domain of this method, and THIS_TYPE is the type
15382 of the method itself (TYPE_CODE_METHOD). */
15383 smash_to_method_type (fnp->type, type,
15384 TYPE_TARGET_TYPE (this_type),
15385 TYPE_FIELDS (this_type),
15386 TYPE_NFIELDS (this_type),
15387 TYPE_VARARGS (this_type));
15388
15389 /* Handle static member functions.
15390 Dwarf2 has no clean way to discern C++ static and non-static
15391 member functions. G++ helps GDB by marking the first
15392 parameter for non-static member functions (which is the this
15393 pointer) as artificial. We obtain this information from
15394 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15395 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15396 fnp->voffset = VOFFSET_STATIC;
15397 }
15398 else
15399 complaint (&symfile_complaints, _("member function type missing for '%s'"),
15400 dwarf2_full_name (fieldname, die, cu));
15401
15402 /* Get fcontext from DW_AT_containing_type if present. */
15403 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15404 fnp->fcontext = die_containing_type (die, cu);
15405
15406 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15407 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15408
15409 /* Get accessibility. */
15410 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15411 if (attr)
15412 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15413 else
15414 accessibility = dwarf2_default_access_attribute (die, cu);
15415 switch (accessibility)
15416 {
15417 case DW_ACCESS_private:
15418 fnp->is_private = 1;
15419 break;
15420 case DW_ACCESS_protected:
15421 fnp->is_protected = 1;
15422 break;
15423 }
15424
15425 /* Check for artificial methods. */
15426 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15427 if (attr && DW_UNSND (attr) != 0)
15428 fnp->is_artificial = 1;
15429
15430 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15431
15432 /* Get index in virtual function table if it is a virtual member
15433 function. For older versions of GCC, this is an offset in the
15434 appropriate virtual table, as specified by DW_AT_containing_type.
15435 For everyone else, it is an expression to be evaluated relative
15436 to the object address. */
15437
15438 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15439 if (attr)
15440 {
15441 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15442 {
15443 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15444 {
15445 /* Old-style GCC. */
15446 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15447 }
15448 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15449 || (DW_BLOCK (attr)->size > 1
15450 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15451 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15452 {
15453 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15454 if ((fnp->voffset % cu->header.addr_size) != 0)
15455 dwarf2_complex_location_expr_complaint ();
15456 else
15457 fnp->voffset /= cu->header.addr_size;
15458 fnp->voffset += 2;
15459 }
15460 else
15461 dwarf2_complex_location_expr_complaint ();
15462
15463 if (!fnp->fcontext)
15464 {
15465 /* If there is no `this' field and no DW_AT_containing_type,
15466 we cannot actually find a base class context for the
15467 vtable! */
15468 if (TYPE_NFIELDS (this_type) == 0
15469 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15470 {
15471 complaint (&symfile_complaints,
15472 _("cannot determine context for virtual member "
15473 "function \"%s\" (offset %s)"),
15474 fieldname, sect_offset_str (die->sect_off));
15475 }
15476 else
15477 {
15478 fnp->fcontext
15479 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15480 }
15481 }
15482 }
15483 else if (attr_form_is_section_offset (attr))
15484 {
15485 dwarf2_complex_location_expr_complaint ();
15486 }
15487 else
15488 {
15489 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15490 fieldname);
15491 }
15492 }
15493 else
15494 {
15495 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15496 if (attr && DW_UNSND (attr))
15497 {
15498 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15499 complaint (&symfile_complaints,
15500 _("Member function \"%s\" (offset %s) is virtual "
15501 "but the vtable offset is not specified"),
15502 fieldname, sect_offset_str (die->sect_off));
15503 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15504 TYPE_CPLUS_DYNAMIC (type) = 1;
15505 }
15506 }
15507 }
15508
15509 /* Create the vector of member function fields, and attach it to the type. */
15510
15511 static void
15512 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15513 struct dwarf2_cu *cu)
15514 {
15515 if (cu->language == language_ada)
15516 error (_("unexpected member functions in Ada type"));
15517
15518 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15519 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15520 TYPE_ALLOC (type,
15521 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15522
15523 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15524 {
15525 struct fnfieldlist &nf = fip->fnfieldlists[i];
15526 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15527
15528 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15529 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15530 fn_flp->fn_fields = (struct fn_field *)
15531 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15532
15533 for (int k = 0; k < nf.fnfields.size (); ++k)
15534 fn_flp->fn_fields[k] = nf.fnfields[k];
15535 }
15536
15537 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15538 }
15539
15540 /* Returns non-zero if NAME is the name of a vtable member in CU's
15541 language, zero otherwise. */
15542 static int
15543 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15544 {
15545 static const char vptr[] = "_vptr";
15546
15547 /* Look for the C++ form of the vtable. */
15548 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15549 return 1;
15550
15551 return 0;
15552 }
15553
15554 /* GCC outputs unnamed structures that are really pointers to member
15555 functions, with the ABI-specified layout. If TYPE describes
15556 such a structure, smash it into a member function type.
15557
15558 GCC shouldn't do this; it should just output pointer to member DIEs.
15559 This is GCC PR debug/28767. */
15560
15561 static void
15562 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15563 {
15564 struct type *pfn_type, *self_type, *new_type;
15565
15566 /* Check for a structure with no name and two children. */
15567 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15568 return;
15569
15570 /* Check for __pfn and __delta members. */
15571 if (TYPE_FIELD_NAME (type, 0) == NULL
15572 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15573 || TYPE_FIELD_NAME (type, 1) == NULL
15574 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15575 return;
15576
15577 /* Find the type of the method. */
15578 pfn_type = TYPE_FIELD_TYPE (type, 0);
15579 if (pfn_type == NULL
15580 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15581 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15582 return;
15583
15584 /* Look for the "this" argument. */
15585 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15586 if (TYPE_NFIELDS (pfn_type) == 0
15587 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15588 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15589 return;
15590
15591 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15592 new_type = alloc_type (objfile);
15593 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15594 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15595 TYPE_VARARGS (pfn_type));
15596 smash_to_methodptr_type (type, new_type);
15597 }
15598
15599
15600 /* Called when we find the DIE that starts a structure or union scope
15601 (definition) to create a type for the structure or union. Fill in
15602 the type's name and general properties; the members will not be
15603 processed until process_structure_scope. A symbol table entry for
15604 the type will also not be done until process_structure_scope (assuming
15605 the type has a name).
15606
15607 NOTE: we need to call these functions regardless of whether or not the
15608 DIE has a DW_AT_name attribute, since it might be an anonymous
15609 structure or union. This gets the type entered into our set of
15610 user defined types. */
15611
15612 static struct type *
15613 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15614 {
15615 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15616 struct type *type;
15617 struct attribute *attr;
15618 const char *name;
15619
15620 /* If the definition of this type lives in .debug_types, read that type.
15621 Don't follow DW_AT_specification though, that will take us back up
15622 the chain and we want to go down. */
15623 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15624 if (attr)
15625 {
15626 type = get_DW_AT_signature_type (die, attr, cu);
15627
15628 /* The type's CU may not be the same as CU.
15629 Ensure TYPE is recorded with CU in die_type_hash. */
15630 return set_die_type (die, type, cu);
15631 }
15632
15633 type = alloc_type (objfile);
15634 INIT_CPLUS_SPECIFIC (type);
15635
15636 name = dwarf2_name (die, cu);
15637 if (name != NULL)
15638 {
15639 if (cu->language == language_cplus
15640 || cu->language == language_d
15641 || cu->language == language_rust)
15642 {
15643 const char *full_name = dwarf2_full_name (name, die, cu);
15644
15645 /* dwarf2_full_name might have already finished building the DIE's
15646 type. If so, there is no need to continue. */
15647 if (get_die_type (die, cu) != NULL)
15648 return get_die_type (die, cu);
15649
15650 TYPE_TAG_NAME (type) = full_name;
15651 if (die->tag == DW_TAG_structure_type
15652 || die->tag == DW_TAG_class_type)
15653 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15654 }
15655 else
15656 {
15657 /* The name is already allocated along with this objfile, so
15658 we don't need to duplicate it for the type. */
15659 TYPE_TAG_NAME (type) = name;
15660 if (die->tag == DW_TAG_class_type)
15661 TYPE_NAME (type) = TYPE_TAG_NAME (type);
15662 }
15663 }
15664
15665 if (die->tag == DW_TAG_structure_type)
15666 {
15667 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15668 }
15669 else if (die->tag == DW_TAG_union_type)
15670 {
15671 TYPE_CODE (type) = TYPE_CODE_UNION;
15672 }
15673 else if (die->tag == DW_TAG_variant_part)
15674 {
15675 TYPE_CODE (type) = TYPE_CODE_UNION;
15676 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15677 }
15678 else
15679 {
15680 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15681 }
15682
15683 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15684 TYPE_DECLARED_CLASS (type) = 1;
15685
15686 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15687 if (attr)
15688 {
15689 if (attr_form_is_constant (attr))
15690 TYPE_LENGTH (type) = DW_UNSND (attr);
15691 else
15692 {
15693 /* For the moment, dynamic type sizes are not supported
15694 by GDB's struct type. The actual size is determined
15695 on-demand when resolving the type of a given object,
15696 so set the type's length to zero for now. Otherwise,
15697 we record an expression as the length, and that expression
15698 could lead to a very large value, which could eventually
15699 lead to us trying to allocate that much memory when creating
15700 a value of that type. */
15701 TYPE_LENGTH (type) = 0;
15702 }
15703 }
15704 else
15705 {
15706 TYPE_LENGTH (type) = 0;
15707 }
15708
15709 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15710 {
15711 /* ICC<14 does not output the required DW_AT_declaration on
15712 incomplete types, but gives them a size of zero. */
15713 TYPE_STUB (type) = 1;
15714 }
15715 else
15716 TYPE_STUB_SUPPORTED (type) = 1;
15717
15718 if (die_is_declaration (die, cu))
15719 TYPE_STUB (type) = 1;
15720 else if (attr == NULL && die->child == NULL
15721 && producer_is_realview (cu->producer))
15722 /* RealView does not output the required DW_AT_declaration
15723 on incomplete types. */
15724 TYPE_STUB (type) = 1;
15725
15726 /* We need to add the type field to the die immediately so we don't
15727 infinitely recurse when dealing with pointers to the structure
15728 type within the structure itself. */
15729 set_die_type (die, type, cu);
15730
15731 /* set_die_type should be already done. */
15732 set_descriptive_type (type, die, cu);
15733
15734 return type;
15735 }
15736
15737 /* A helper for process_structure_scope that handles a single member
15738 DIE. */
15739
15740 static void
15741 handle_struct_member_die (struct die_info *child_die, struct type *type,
15742 struct field_info *fi,
15743 std::vector<struct symbol *> *template_args,
15744 struct dwarf2_cu *cu)
15745 {
15746 if (child_die->tag == DW_TAG_member
15747 || child_die->tag == DW_TAG_variable
15748 || child_die->tag == DW_TAG_variant_part)
15749 {
15750 /* NOTE: carlton/2002-11-05: A C++ static data member
15751 should be a DW_TAG_member that is a declaration, but
15752 all versions of G++ as of this writing (so through at
15753 least 3.2.1) incorrectly generate DW_TAG_variable
15754 tags for them instead. */
15755 dwarf2_add_field (fi, child_die, cu);
15756 }
15757 else if (child_die->tag == DW_TAG_subprogram)
15758 {
15759 /* Rust doesn't have member functions in the C++ sense.
15760 However, it does emit ordinary functions as children
15761 of a struct DIE. */
15762 if (cu->language == language_rust)
15763 read_func_scope (child_die, cu);
15764 else
15765 {
15766 /* C++ member function. */
15767 dwarf2_add_member_fn (fi, child_die, type, cu);
15768 }
15769 }
15770 else if (child_die->tag == DW_TAG_inheritance)
15771 {
15772 /* C++ base class field. */
15773 dwarf2_add_field (fi, child_die, cu);
15774 }
15775 else if (type_can_define_types (child_die))
15776 dwarf2_add_type_defn (fi, child_die, cu);
15777 else if (child_die->tag == DW_TAG_template_type_param
15778 || child_die->tag == DW_TAG_template_value_param)
15779 {
15780 struct symbol *arg = new_symbol (child_die, NULL, cu);
15781
15782 if (arg != NULL)
15783 template_args->push_back (arg);
15784 }
15785 else if (child_die->tag == DW_TAG_variant)
15786 {
15787 /* In a variant we want to get the discriminant and also add a
15788 field for our sole member child. */
15789 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15790
15791 for (struct die_info *variant_child = child_die->child;
15792 variant_child != NULL;
15793 variant_child = sibling_die (variant_child))
15794 {
15795 if (variant_child->tag == DW_TAG_member)
15796 {
15797 handle_struct_member_die (variant_child, type, fi,
15798 template_args, cu);
15799 /* Only handle the one. */
15800 break;
15801 }
15802 }
15803
15804 /* We don't handle this but we might as well report it if we see
15805 it. */
15806 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15807 complaint (&symfile_complaints,
15808 _("DW_AT_discr_list is not supported yet"
15809 " - DIE at %s [in module %s]"),
15810 sect_offset_str (child_die->sect_off),
15811 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15812
15813 /* The first field was just added, so we can stash the
15814 discriminant there. */
15815 gdb_assert (!fi->fields.empty ());
15816 if (discr == NULL)
15817 fi->fields.back ().variant.default_branch = true;
15818 else
15819 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15820 }
15821 }
15822
15823 /* Finish creating a structure or union type, including filling in
15824 its members and creating a symbol for it. */
15825
15826 static void
15827 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15828 {
15829 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15830 struct die_info *child_die;
15831 struct type *type;
15832
15833 type = get_die_type (die, cu);
15834 if (type == NULL)
15835 type = read_structure_type (die, cu);
15836
15837 /* When reading a DW_TAG_variant_part, we need to notice when we
15838 read the discriminant member, so we can record it later in the
15839 discriminant_info. */
15840 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15841 sect_offset discr_offset;
15842
15843 if (is_variant_part)
15844 {
15845 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15846 if (discr == NULL)
15847 {
15848 /* Maybe it's a univariant form, an extension we support.
15849 In this case arrange not to check the offset. */
15850 is_variant_part = false;
15851 }
15852 else if (attr_form_is_ref (discr))
15853 {
15854 struct dwarf2_cu *target_cu = cu;
15855 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15856
15857 discr_offset = target_die->sect_off;
15858 }
15859 else
15860 {
15861 complaint (&symfile_complaints,
15862 _("DW_AT_discr does not have DIE reference form"
15863 " - DIE at %s [in module %s]"),
15864 sect_offset_str (die->sect_off),
15865 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15866 is_variant_part = false;
15867 }
15868 }
15869
15870 if (die->child != NULL && ! die_is_declaration (die, cu))
15871 {
15872 struct field_info fi;
15873 std::vector<struct symbol *> template_args;
15874
15875 child_die = die->child;
15876
15877 while (child_die && child_die->tag)
15878 {
15879 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15880
15881 if (is_variant_part && discr_offset == child_die->sect_off)
15882 fi.fields.back ().variant.is_discriminant = true;
15883
15884 child_die = sibling_die (child_die);
15885 }
15886
15887 /* Attach template arguments to type. */
15888 if (!template_args.empty ())
15889 {
15890 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15891 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15892 TYPE_TEMPLATE_ARGUMENTS (type)
15893 = XOBNEWVEC (&objfile->objfile_obstack,
15894 struct symbol *,
15895 TYPE_N_TEMPLATE_ARGUMENTS (type));
15896 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15897 template_args.data (),
15898 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15899 * sizeof (struct symbol *)));
15900 }
15901
15902 /* Attach fields and member functions to the type. */
15903 if (fi.nfields)
15904 dwarf2_attach_fields_to_type (&fi, type, cu);
15905 if (!fi.fnfieldlists.empty ())
15906 {
15907 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15908
15909 /* Get the type which refers to the base class (possibly this
15910 class itself) which contains the vtable pointer for the current
15911 class from the DW_AT_containing_type attribute. This use of
15912 DW_AT_containing_type is a GNU extension. */
15913
15914 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15915 {
15916 struct type *t = die_containing_type (die, cu);
15917
15918 set_type_vptr_basetype (type, t);
15919 if (type == t)
15920 {
15921 int i;
15922
15923 /* Our own class provides vtbl ptr. */
15924 for (i = TYPE_NFIELDS (t) - 1;
15925 i >= TYPE_N_BASECLASSES (t);
15926 --i)
15927 {
15928 const char *fieldname = TYPE_FIELD_NAME (t, i);
15929
15930 if (is_vtable_name (fieldname, cu))
15931 {
15932 set_type_vptr_fieldno (type, i);
15933 break;
15934 }
15935 }
15936
15937 /* Complain if virtual function table field not found. */
15938 if (i < TYPE_N_BASECLASSES (t))
15939 complaint (&symfile_complaints,
15940 _("virtual function table pointer "
15941 "not found when defining class '%s'"),
15942 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
15943 "");
15944 }
15945 else
15946 {
15947 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15948 }
15949 }
15950 else if (cu->producer
15951 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15952 {
15953 /* The IBM XLC compiler does not provide direct indication
15954 of the containing type, but the vtable pointer is
15955 always named __vfp. */
15956
15957 int i;
15958
15959 for (i = TYPE_NFIELDS (type) - 1;
15960 i >= TYPE_N_BASECLASSES (type);
15961 --i)
15962 {
15963 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15964 {
15965 set_type_vptr_fieldno (type, i);
15966 set_type_vptr_basetype (type, type);
15967 break;
15968 }
15969 }
15970 }
15971 }
15972
15973 /* Copy fi.typedef_field_list linked list elements content into the
15974 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15975 if (!fi.typedef_field_list.empty ())
15976 {
15977 int count = fi.typedef_field_list.size ();
15978
15979 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15980 TYPE_TYPEDEF_FIELD_ARRAY (type)
15981 = ((struct decl_field *)
15982 TYPE_ALLOC (type,
15983 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15984 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15985
15986 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15987 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15988 }
15989
15990 /* Copy fi.nested_types_list linked list elements content into the
15991 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15992 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15993 {
15994 int count = fi.nested_types_list.size ();
15995
15996 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15997 TYPE_NESTED_TYPES_ARRAY (type)
15998 = ((struct decl_field *)
15999 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16000 TYPE_NESTED_TYPES_COUNT (type) = count;
16001
16002 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16003 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16004 }
16005 }
16006
16007 quirk_gcc_member_function_pointer (type, objfile);
16008 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16009 cu->rust_unions.push_back (type);
16010
16011 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16012 snapshots) has been known to create a die giving a declaration
16013 for a class that has, as a child, a die giving a definition for a
16014 nested class. So we have to process our children even if the
16015 current die is a declaration. Normally, of course, a declaration
16016 won't have any children at all. */
16017
16018 child_die = die->child;
16019
16020 while (child_die != NULL && child_die->tag)
16021 {
16022 if (child_die->tag == DW_TAG_member
16023 || child_die->tag == DW_TAG_variable
16024 || child_die->tag == DW_TAG_inheritance
16025 || child_die->tag == DW_TAG_template_value_param
16026 || child_die->tag == DW_TAG_template_type_param)
16027 {
16028 /* Do nothing. */
16029 }
16030 else
16031 process_die (child_die, cu);
16032
16033 child_die = sibling_die (child_die);
16034 }
16035
16036 /* Do not consider external references. According to the DWARF standard,
16037 these DIEs are identified by the fact that they have no byte_size
16038 attribute, and a declaration attribute. */
16039 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16040 || !die_is_declaration (die, cu))
16041 new_symbol (die, type, cu);
16042 }
16043
16044 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16045 update TYPE using some information only available in DIE's children. */
16046
16047 static void
16048 update_enumeration_type_from_children (struct die_info *die,
16049 struct type *type,
16050 struct dwarf2_cu *cu)
16051 {
16052 struct die_info *child_die;
16053 int unsigned_enum = 1;
16054 int flag_enum = 1;
16055 ULONGEST mask = 0;
16056
16057 auto_obstack obstack;
16058
16059 for (child_die = die->child;
16060 child_die != NULL && child_die->tag;
16061 child_die = sibling_die (child_die))
16062 {
16063 struct attribute *attr;
16064 LONGEST value;
16065 const gdb_byte *bytes;
16066 struct dwarf2_locexpr_baton *baton;
16067 const char *name;
16068
16069 if (child_die->tag != DW_TAG_enumerator)
16070 continue;
16071
16072 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16073 if (attr == NULL)
16074 continue;
16075
16076 name = dwarf2_name (child_die, cu);
16077 if (name == NULL)
16078 name = "<anonymous enumerator>";
16079
16080 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16081 &value, &bytes, &baton);
16082 if (value < 0)
16083 {
16084 unsigned_enum = 0;
16085 flag_enum = 0;
16086 }
16087 else if ((mask & value) != 0)
16088 flag_enum = 0;
16089 else
16090 mask |= value;
16091
16092 /* If we already know that the enum type is neither unsigned, nor
16093 a flag type, no need to look at the rest of the enumerates. */
16094 if (!unsigned_enum && !flag_enum)
16095 break;
16096 }
16097
16098 if (unsigned_enum)
16099 TYPE_UNSIGNED (type) = 1;
16100 if (flag_enum)
16101 TYPE_FLAG_ENUM (type) = 1;
16102 }
16103
16104 /* Given a DW_AT_enumeration_type die, set its type. We do not
16105 complete the type's fields yet, or create any symbols. */
16106
16107 static struct type *
16108 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16109 {
16110 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16111 struct type *type;
16112 struct attribute *attr;
16113 const char *name;
16114
16115 /* If the definition of this type lives in .debug_types, read that type.
16116 Don't follow DW_AT_specification though, that will take us back up
16117 the chain and we want to go down. */
16118 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16119 if (attr)
16120 {
16121 type = get_DW_AT_signature_type (die, attr, cu);
16122
16123 /* The type's CU may not be the same as CU.
16124 Ensure TYPE is recorded with CU in die_type_hash. */
16125 return set_die_type (die, type, cu);
16126 }
16127
16128 type = alloc_type (objfile);
16129
16130 TYPE_CODE (type) = TYPE_CODE_ENUM;
16131 name = dwarf2_full_name (NULL, die, cu);
16132 if (name != NULL)
16133 TYPE_TAG_NAME (type) = name;
16134
16135 attr = dwarf2_attr (die, DW_AT_type, cu);
16136 if (attr != NULL)
16137 {
16138 struct type *underlying_type = die_type (die, cu);
16139
16140 TYPE_TARGET_TYPE (type) = underlying_type;
16141 }
16142
16143 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16144 if (attr)
16145 {
16146 TYPE_LENGTH (type) = DW_UNSND (attr);
16147 }
16148 else
16149 {
16150 TYPE_LENGTH (type) = 0;
16151 }
16152
16153 /* The enumeration DIE can be incomplete. In Ada, any type can be
16154 declared as private in the package spec, and then defined only
16155 inside the package body. Such types are known as Taft Amendment
16156 Types. When another package uses such a type, an incomplete DIE
16157 may be generated by the compiler. */
16158 if (die_is_declaration (die, cu))
16159 TYPE_STUB (type) = 1;
16160
16161 /* Finish the creation of this type by using the enum's children.
16162 We must call this even when the underlying type has been provided
16163 so that we can determine if we're looking at a "flag" enum. */
16164 update_enumeration_type_from_children (die, type, cu);
16165
16166 /* If this type has an underlying type that is not a stub, then we
16167 may use its attributes. We always use the "unsigned" attribute
16168 in this situation, because ordinarily we guess whether the type
16169 is unsigned -- but the guess can be wrong and the underlying type
16170 can tell us the reality. However, we defer to a local size
16171 attribute if one exists, because this lets the compiler override
16172 the underlying type if needed. */
16173 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16174 {
16175 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16176 if (TYPE_LENGTH (type) == 0)
16177 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16178 }
16179
16180 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16181
16182 return set_die_type (die, type, cu);
16183 }
16184
16185 /* Given a pointer to a die which begins an enumeration, process all
16186 the dies that define the members of the enumeration, and create the
16187 symbol for the enumeration type.
16188
16189 NOTE: We reverse the order of the element list. */
16190
16191 static void
16192 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16193 {
16194 struct type *this_type;
16195
16196 this_type = get_die_type (die, cu);
16197 if (this_type == NULL)
16198 this_type = read_enumeration_type (die, cu);
16199
16200 if (die->child != NULL)
16201 {
16202 struct die_info *child_die;
16203 struct symbol *sym;
16204 struct field *fields = NULL;
16205 int num_fields = 0;
16206 const char *name;
16207
16208 child_die = die->child;
16209 while (child_die && child_die->tag)
16210 {
16211 if (child_die->tag != DW_TAG_enumerator)
16212 {
16213 process_die (child_die, cu);
16214 }
16215 else
16216 {
16217 name = dwarf2_name (child_die, cu);
16218 if (name)
16219 {
16220 sym = new_symbol (child_die, this_type, cu);
16221
16222 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16223 {
16224 fields = (struct field *)
16225 xrealloc (fields,
16226 (num_fields + DW_FIELD_ALLOC_CHUNK)
16227 * sizeof (struct field));
16228 }
16229
16230 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16231 FIELD_TYPE (fields[num_fields]) = NULL;
16232 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16233 FIELD_BITSIZE (fields[num_fields]) = 0;
16234
16235 num_fields++;
16236 }
16237 }
16238
16239 child_die = sibling_die (child_die);
16240 }
16241
16242 if (num_fields)
16243 {
16244 TYPE_NFIELDS (this_type) = num_fields;
16245 TYPE_FIELDS (this_type) = (struct field *)
16246 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16247 memcpy (TYPE_FIELDS (this_type), fields,
16248 sizeof (struct field) * num_fields);
16249 xfree (fields);
16250 }
16251 }
16252
16253 /* If we are reading an enum from a .debug_types unit, and the enum
16254 is a declaration, and the enum is not the signatured type in the
16255 unit, then we do not want to add a symbol for it. Adding a
16256 symbol would in some cases obscure the true definition of the
16257 enum, giving users an incomplete type when the definition is
16258 actually available. Note that we do not want to do this for all
16259 enums which are just declarations, because C++0x allows forward
16260 enum declarations. */
16261 if (cu->per_cu->is_debug_types
16262 && die_is_declaration (die, cu))
16263 {
16264 struct signatured_type *sig_type;
16265
16266 sig_type = (struct signatured_type *) cu->per_cu;
16267 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16268 if (sig_type->type_offset_in_section != die->sect_off)
16269 return;
16270 }
16271
16272 new_symbol (die, this_type, cu);
16273 }
16274
16275 /* Extract all information from a DW_TAG_array_type DIE and put it in
16276 the DIE's type field. For now, this only handles one dimensional
16277 arrays. */
16278
16279 static struct type *
16280 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16281 {
16282 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16283 struct die_info *child_die;
16284 struct type *type;
16285 struct type *element_type, *range_type, *index_type;
16286 struct attribute *attr;
16287 const char *name;
16288 struct dynamic_prop *byte_stride_prop = NULL;
16289 unsigned int bit_stride = 0;
16290
16291 element_type = die_type (die, cu);
16292
16293 /* The die_type call above may have already set the type for this DIE. */
16294 type = get_die_type (die, cu);
16295 if (type)
16296 return type;
16297
16298 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16299 if (attr != NULL)
16300 {
16301 int stride_ok;
16302
16303 byte_stride_prop
16304 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16305 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16306 if (!stride_ok)
16307 {
16308 complaint (&symfile_complaints,
16309 _("unable to read array DW_AT_byte_stride "
16310 " - DIE at %s [in module %s]"),
16311 sect_offset_str (die->sect_off),
16312 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16313 /* Ignore this attribute. We will likely not be able to print
16314 arrays of this type correctly, but there is little we can do
16315 to help if we cannot read the attribute's value. */
16316 byte_stride_prop = NULL;
16317 }
16318 }
16319
16320 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16321 if (attr != NULL)
16322 bit_stride = DW_UNSND (attr);
16323
16324 /* Irix 6.2 native cc creates array types without children for
16325 arrays with unspecified length. */
16326 if (die->child == NULL)
16327 {
16328 index_type = objfile_type (objfile)->builtin_int;
16329 range_type = create_static_range_type (NULL, index_type, 0, -1);
16330 type = create_array_type_with_stride (NULL, element_type, range_type,
16331 byte_stride_prop, bit_stride);
16332 return set_die_type (die, type, cu);
16333 }
16334
16335 std::vector<struct type *> range_types;
16336 child_die = die->child;
16337 while (child_die && child_die->tag)
16338 {
16339 if (child_die->tag == DW_TAG_subrange_type)
16340 {
16341 struct type *child_type = read_type_die (child_die, cu);
16342
16343 if (child_type != NULL)
16344 {
16345 /* The range type was succesfully read. Save it for the
16346 array type creation. */
16347 range_types.push_back (child_type);
16348 }
16349 }
16350 child_die = sibling_die (child_die);
16351 }
16352
16353 /* Dwarf2 dimensions are output from left to right, create the
16354 necessary array types in backwards order. */
16355
16356 type = element_type;
16357
16358 if (read_array_order (die, cu) == DW_ORD_col_major)
16359 {
16360 int i = 0;
16361
16362 while (i < range_types.size ())
16363 type = create_array_type_with_stride (NULL, type, range_types[i++],
16364 byte_stride_prop, bit_stride);
16365 }
16366 else
16367 {
16368 size_t ndim = range_types.size ();
16369 while (ndim-- > 0)
16370 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16371 byte_stride_prop, bit_stride);
16372 }
16373
16374 /* Understand Dwarf2 support for vector types (like they occur on
16375 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16376 array type. This is not part of the Dwarf2/3 standard yet, but a
16377 custom vendor extension. The main difference between a regular
16378 array and the vector variant is that vectors are passed by value
16379 to functions. */
16380 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16381 if (attr)
16382 make_vector_type (type);
16383
16384 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16385 implementation may choose to implement triple vectors using this
16386 attribute. */
16387 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16388 if (attr)
16389 {
16390 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16391 TYPE_LENGTH (type) = DW_UNSND (attr);
16392 else
16393 complaint (&symfile_complaints,
16394 _("DW_AT_byte_size for array type smaller "
16395 "than the total size of elements"));
16396 }
16397
16398 name = dwarf2_name (die, cu);
16399 if (name)
16400 TYPE_NAME (type) = name;
16401
16402 /* Install the type in the die. */
16403 set_die_type (die, type, cu);
16404
16405 /* set_die_type should be already done. */
16406 set_descriptive_type (type, die, cu);
16407
16408 return type;
16409 }
16410
16411 static enum dwarf_array_dim_ordering
16412 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16413 {
16414 struct attribute *attr;
16415
16416 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16417
16418 if (attr)
16419 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16420
16421 /* GNU F77 is a special case, as at 08/2004 array type info is the
16422 opposite order to the dwarf2 specification, but data is still
16423 laid out as per normal fortran.
16424
16425 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16426 version checking. */
16427
16428 if (cu->language == language_fortran
16429 && cu->producer && strstr (cu->producer, "GNU F77"))
16430 {
16431 return DW_ORD_row_major;
16432 }
16433
16434 switch (cu->language_defn->la_array_ordering)
16435 {
16436 case array_column_major:
16437 return DW_ORD_col_major;
16438 case array_row_major:
16439 default:
16440 return DW_ORD_row_major;
16441 };
16442 }
16443
16444 /* Extract all information from a DW_TAG_set_type DIE and put it in
16445 the DIE's type field. */
16446
16447 static struct type *
16448 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16449 {
16450 struct type *domain_type, *set_type;
16451 struct attribute *attr;
16452
16453 domain_type = die_type (die, cu);
16454
16455 /* The die_type call above may have already set the type for this DIE. */
16456 set_type = get_die_type (die, cu);
16457 if (set_type)
16458 return set_type;
16459
16460 set_type = create_set_type (NULL, domain_type);
16461
16462 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16463 if (attr)
16464 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16465
16466 return set_die_type (die, set_type, cu);
16467 }
16468
16469 /* A helper for read_common_block that creates a locexpr baton.
16470 SYM is the symbol which we are marking as computed.
16471 COMMON_DIE is the DIE for the common block.
16472 COMMON_LOC is the location expression attribute for the common
16473 block itself.
16474 MEMBER_LOC is the location expression attribute for the particular
16475 member of the common block that we are processing.
16476 CU is the CU from which the above come. */
16477
16478 static void
16479 mark_common_block_symbol_computed (struct symbol *sym,
16480 struct die_info *common_die,
16481 struct attribute *common_loc,
16482 struct attribute *member_loc,
16483 struct dwarf2_cu *cu)
16484 {
16485 struct dwarf2_per_objfile *dwarf2_per_objfile
16486 = cu->per_cu->dwarf2_per_objfile;
16487 struct objfile *objfile = dwarf2_per_objfile->objfile;
16488 struct dwarf2_locexpr_baton *baton;
16489 gdb_byte *ptr;
16490 unsigned int cu_off;
16491 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16492 LONGEST offset = 0;
16493
16494 gdb_assert (common_loc && member_loc);
16495 gdb_assert (attr_form_is_block (common_loc));
16496 gdb_assert (attr_form_is_block (member_loc)
16497 || attr_form_is_constant (member_loc));
16498
16499 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16500 baton->per_cu = cu->per_cu;
16501 gdb_assert (baton->per_cu);
16502
16503 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16504
16505 if (attr_form_is_constant (member_loc))
16506 {
16507 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16508 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16509 }
16510 else
16511 baton->size += DW_BLOCK (member_loc)->size;
16512
16513 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16514 baton->data = ptr;
16515
16516 *ptr++ = DW_OP_call4;
16517 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16518 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16519 ptr += 4;
16520
16521 if (attr_form_is_constant (member_loc))
16522 {
16523 *ptr++ = DW_OP_addr;
16524 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16525 ptr += cu->header.addr_size;
16526 }
16527 else
16528 {
16529 /* We have to copy the data here, because DW_OP_call4 will only
16530 use a DW_AT_location attribute. */
16531 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16532 ptr += DW_BLOCK (member_loc)->size;
16533 }
16534
16535 *ptr++ = DW_OP_plus;
16536 gdb_assert (ptr - baton->data == baton->size);
16537
16538 SYMBOL_LOCATION_BATON (sym) = baton;
16539 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16540 }
16541
16542 /* Create appropriate locally-scoped variables for all the
16543 DW_TAG_common_block entries. Also create a struct common_block
16544 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16545 is used to sepate the common blocks name namespace from regular
16546 variable names. */
16547
16548 static void
16549 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16550 {
16551 struct attribute *attr;
16552
16553 attr = dwarf2_attr (die, DW_AT_location, cu);
16554 if (attr)
16555 {
16556 /* Support the .debug_loc offsets. */
16557 if (attr_form_is_block (attr))
16558 {
16559 /* Ok. */
16560 }
16561 else if (attr_form_is_section_offset (attr))
16562 {
16563 dwarf2_complex_location_expr_complaint ();
16564 attr = NULL;
16565 }
16566 else
16567 {
16568 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16569 "common block member");
16570 attr = NULL;
16571 }
16572 }
16573
16574 if (die->child != NULL)
16575 {
16576 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16577 struct die_info *child_die;
16578 size_t n_entries = 0, size;
16579 struct common_block *common_block;
16580 struct symbol *sym;
16581
16582 for (child_die = die->child;
16583 child_die && child_die->tag;
16584 child_die = sibling_die (child_die))
16585 ++n_entries;
16586
16587 size = (sizeof (struct common_block)
16588 + (n_entries - 1) * sizeof (struct symbol *));
16589 common_block
16590 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16591 size);
16592 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16593 common_block->n_entries = 0;
16594
16595 for (child_die = die->child;
16596 child_die && child_die->tag;
16597 child_die = sibling_die (child_die))
16598 {
16599 /* Create the symbol in the DW_TAG_common_block block in the current
16600 symbol scope. */
16601 sym = new_symbol (child_die, NULL, cu);
16602 if (sym != NULL)
16603 {
16604 struct attribute *member_loc;
16605
16606 common_block->contents[common_block->n_entries++] = sym;
16607
16608 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16609 cu);
16610 if (member_loc)
16611 {
16612 /* GDB has handled this for a long time, but it is
16613 not specified by DWARF. It seems to have been
16614 emitted by gfortran at least as recently as:
16615 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16616 complaint (&symfile_complaints,
16617 _("Variable in common block has "
16618 "DW_AT_data_member_location "
16619 "- DIE at %s [in module %s]"),
16620 sect_offset_str (child_die->sect_off),
16621 objfile_name (objfile));
16622
16623 if (attr_form_is_section_offset (member_loc))
16624 dwarf2_complex_location_expr_complaint ();
16625 else if (attr_form_is_constant (member_loc)
16626 || attr_form_is_block (member_loc))
16627 {
16628 if (attr)
16629 mark_common_block_symbol_computed (sym, die, attr,
16630 member_loc, cu);
16631 }
16632 else
16633 dwarf2_complex_location_expr_complaint ();
16634 }
16635 }
16636 }
16637
16638 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16639 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16640 }
16641 }
16642
16643 /* Create a type for a C++ namespace. */
16644
16645 static struct type *
16646 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16647 {
16648 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16649 const char *previous_prefix, *name;
16650 int is_anonymous;
16651 struct type *type;
16652
16653 /* For extensions, reuse the type of the original namespace. */
16654 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16655 {
16656 struct die_info *ext_die;
16657 struct dwarf2_cu *ext_cu = cu;
16658
16659 ext_die = dwarf2_extension (die, &ext_cu);
16660 type = read_type_die (ext_die, ext_cu);
16661
16662 /* EXT_CU may not be the same as CU.
16663 Ensure TYPE is recorded with CU in die_type_hash. */
16664 return set_die_type (die, type, cu);
16665 }
16666
16667 name = namespace_name (die, &is_anonymous, cu);
16668
16669 /* Now build the name of the current namespace. */
16670
16671 previous_prefix = determine_prefix (die, cu);
16672 if (previous_prefix[0] != '\0')
16673 name = typename_concat (&objfile->objfile_obstack,
16674 previous_prefix, name, 0, cu);
16675
16676 /* Create the type. */
16677 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16678 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16679
16680 return set_die_type (die, type, cu);
16681 }
16682
16683 /* Read a namespace scope. */
16684
16685 static void
16686 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16687 {
16688 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16689 int is_anonymous;
16690
16691 /* Add a symbol associated to this if we haven't seen the namespace
16692 before. Also, add a using directive if it's an anonymous
16693 namespace. */
16694
16695 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16696 {
16697 struct type *type;
16698
16699 type = read_type_die (die, cu);
16700 new_symbol (die, type, cu);
16701
16702 namespace_name (die, &is_anonymous, cu);
16703 if (is_anonymous)
16704 {
16705 const char *previous_prefix = determine_prefix (die, cu);
16706
16707 std::vector<const char *> excludes;
16708 add_using_directive (using_directives (cu->language),
16709 previous_prefix, TYPE_NAME (type), NULL,
16710 NULL, excludes, 0, &objfile->objfile_obstack);
16711 }
16712 }
16713
16714 if (die->child != NULL)
16715 {
16716 struct die_info *child_die = die->child;
16717
16718 while (child_die && child_die->tag)
16719 {
16720 process_die (child_die, cu);
16721 child_die = sibling_die (child_die);
16722 }
16723 }
16724 }
16725
16726 /* Read a Fortran module as type. This DIE can be only a declaration used for
16727 imported module. Still we need that type as local Fortran "use ... only"
16728 declaration imports depend on the created type in determine_prefix. */
16729
16730 static struct type *
16731 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16732 {
16733 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16734 const char *module_name;
16735 struct type *type;
16736
16737 module_name = dwarf2_name (die, cu);
16738 if (!module_name)
16739 complaint (&symfile_complaints,
16740 _("DW_TAG_module has no name, offset %s"),
16741 sect_offset_str (die->sect_off));
16742 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16743
16744 /* determine_prefix uses TYPE_TAG_NAME. */
16745 TYPE_TAG_NAME (type) = TYPE_NAME (type);
16746
16747 return set_die_type (die, type, cu);
16748 }
16749
16750 /* Read a Fortran module. */
16751
16752 static void
16753 read_module (struct die_info *die, struct dwarf2_cu *cu)
16754 {
16755 struct die_info *child_die = die->child;
16756 struct type *type;
16757
16758 type = read_type_die (die, cu);
16759 new_symbol (die, type, cu);
16760
16761 while (child_die && child_die->tag)
16762 {
16763 process_die (child_die, cu);
16764 child_die = sibling_die (child_die);
16765 }
16766 }
16767
16768 /* Return the name of the namespace represented by DIE. Set
16769 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16770 namespace. */
16771
16772 static const char *
16773 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16774 {
16775 struct die_info *current_die;
16776 const char *name = NULL;
16777
16778 /* Loop through the extensions until we find a name. */
16779
16780 for (current_die = die;
16781 current_die != NULL;
16782 current_die = dwarf2_extension (die, &cu))
16783 {
16784 /* We don't use dwarf2_name here so that we can detect the absence
16785 of a name -> anonymous namespace. */
16786 name = dwarf2_string_attr (die, DW_AT_name, cu);
16787
16788 if (name != NULL)
16789 break;
16790 }
16791
16792 /* Is it an anonymous namespace? */
16793
16794 *is_anonymous = (name == NULL);
16795 if (*is_anonymous)
16796 name = CP_ANONYMOUS_NAMESPACE_STR;
16797
16798 return name;
16799 }
16800
16801 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16802 the user defined type vector. */
16803
16804 static struct type *
16805 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16806 {
16807 struct gdbarch *gdbarch
16808 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16809 struct comp_unit_head *cu_header = &cu->header;
16810 struct type *type;
16811 struct attribute *attr_byte_size;
16812 struct attribute *attr_address_class;
16813 int byte_size, addr_class;
16814 struct type *target_type;
16815
16816 target_type = die_type (die, cu);
16817
16818 /* The die_type call above may have already set the type for this DIE. */
16819 type = get_die_type (die, cu);
16820 if (type)
16821 return type;
16822
16823 type = lookup_pointer_type (target_type);
16824
16825 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16826 if (attr_byte_size)
16827 byte_size = DW_UNSND (attr_byte_size);
16828 else
16829 byte_size = cu_header->addr_size;
16830
16831 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16832 if (attr_address_class)
16833 addr_class = DW_UNSND (attr_address_class);
16834 else
16835 addr_class = DW_ADDR_none;
16836
16837 /* If the pointer size or address class is different than the
16838 default, create a type variant marked as such and set the
16839 length accordingly. */
16840 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
16841 {
16842 if (gdbarch_address_class_type_flags_p (gdbarch))
16843 {
16844 int type_flags;
16845
16846 type_flags = gdbarch_address_class_type_flags
16847 (gdbarch, byte_size, addr_class);
16848 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16849 == 0);
16850 type = make_type_with_address_space (type, type_flags);
16851 }
16852 else if (TYPE_LENGTH (type) != byte_size)
16853 {
16854 complaint (&symfile_complaints,
16855 _("invalid pointer size %d"), byte_size);
16856 }
16857 else
16858 {
16859 /* Should we also complain about unhandled address classes? */
16860 }
16861 }
16862
16863 TYPE_LENGTH (type) = byte_size;
16864 return set_die_type (die, type, cu);
16865 }
16866
16867 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16868 the user defined type vector. */
16869
16870 static struct type *
16871 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16872 {
16873 struct type *type;
16874 struct type *to_type;
16875 struct type *domain;
16876
16877 to_type = die_type (die, cu);
16878 domain = die_containing_type (die, cu);
16879
16880 /* The calls above may have already set the type for this DIE. */
16881 type = get_die_type (die, cu);
16882 if (type)
16883 return type;
16884
16885 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16886 type = lookup_methodptr_type (to_type);
16887 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16888 {
16889 struct type *new_type
16890 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16891
16892 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16893 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16894 TYPE_VARARGS (to_type));
16895 type = lookup_methodptr_type (new_type);
16896 }
16897 else
16898 type = lookup_memberptr_type (to_type, domain);
16899
16900 return set_die_type (die, type, cu);
16901 }
16902
16903 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16904 the user defined type vector. */
16905
16906 static struct type *
16907 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16908 enum type_code refcode)
16909 {
16910 struct comp_unit_head *cu_header = &cu->header;
16911 struct type *type, *target_type;
16912 struct attribute *attr;
16913
16914 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16915
16916 target_type = die_type (die, cu);
16917
16918 /* The die_type call above may have already set the type for this DIE. */
16919 type = get_die_type (die, cu);
16920 if (type)
16921 return type;
16922
16923 type = lookup_reference_type (target_type, refcode);
16924 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16925 if (attr)
16926 {
16927 TYPE_LENGTH (type) = DW_UNSND (attr);
16928 }
16929 else
16930 {
16931 TYPE_LENGTH (type) = cu_header->addr_size;
16932 }
16933 return set_die_type (die, type, cu);
16934 }
16935
16936 /* Add the given cv-qualifiers to the element type of the array. GCC
16937 outputs DWARF type qualifiers that apply to an array, not the
16938 element type. But GDB relies on the array element type to carry
16939 the cv-qualifiers. This mimics section 6.7.3 of the C99
16940 specification. */
16941
16942 static struct type *
16943 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16944 struct type *base_type, int cnst, int voltl)
16945 {
16946 struct type *el_type, *inner_array;
16947
16948 base_type = copy_type (base_type);
16949 inner_array = base_type;
16950
16951 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16952 {
16953 TYPE_TARGET_TYPE (inner_array) =
16954 copy_type (TYPE_TARGET_TYPE (inner_array));
16955 inner_array = TYPE_TARGET_TYPE (inner_array);
16956 }
16957
16958 el_type = TYPE_TARGET_TYPE (inner_array);
16959 cnst |= TYPE_CONST (el_type);
16960 voltl |= TYPE_VOLATILE (el_type);
16961 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16962
16963 return set_die_type (die, base_type, cu);
16964 }
16965
16966 static struct type *
16967 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16968 {
16969 struct type *base_type, *cv_type;
16970
16971 base_type = die_type (die, cu);
16972
16973 /* The die_type call above may have already set the type for this DIE. */
16974 cv_type = get_die_type (die, cu);
16975 if (cv_type)
16976 return cv_type;
16977
16978 /* In case the const qualifier is applied to an array type, the element type
16979 is so qualified, not the array type (section 6.7.3 of C99). */
16980 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16981 return add_array_cv_type (die, cu, base_type, 1, 0);
16982
16983 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16984 return set_die_type (die, cv_type, cu);
16985 }
16986
16987 static struct type *
16988 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16989 {
16990 struct type *base_type, *cv_type;
16991
16992 base_type = die_type (die, cu);
16993
16994 /* The die_type call above may have already set the type for this DIE. */
16995 cv_type = get_die_type (die, cu);
16996 if (cv_type)
16997 return cv_type;
16998
16999 /* In case the volatile qualifier is applied to an array type, the
17000 element type is so qualified, not the array type (section 6.7.3
17001 of C99). */
17002 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17003 return add_array_cv_type (die, cu, base_type, 0, 1);
17004
17005 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17006 return set_die_type (die, cv_type, cu);
17007 }
17008
17009 /* Handle DW_TAG_restrict_type. */
17010
17011 static struct type *
17012 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17013 {
17014 struct type *base_type, *cv_type;
17015
17016 base_type = die_type (die, cu);
17017
17018 /* The die_type call above may have already set the type for this DIE. */
17019 cv_type = get_die_type (die, cu);
17020 if (cv_type)
17021 return cv_type;
17022
17023 cv_type = make_restrict_type (base_type);
17024 return set_die_type (die, cv_type, cu);
17025 }
17026
17027 /* Handle DW_TAG_atomic_type. */
17028
17029 static struct type *
17030 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17031 {
17032 struct type *base_type, *cv_type;
17033
17034 base_type = die_type (die, cu);
17035
17036 /* The die_type call above may have already set the type for this DIE. */
17037 cv_type = get_die_type (die, cu);
17038 if (cv_type)
17039 return cv_type;
17040
17041 cv_type = make_atomic_type (base_type);
17042 return set_die_type (die, cv_type, cu);
17043 }
17044
17045 /* Extract all information from a DW_TAG_string_type DIE and add to
17046 the user defined type vector. It isn't really a user defined type,
17047 but it behaves like one, with other DIE's using an AT_user_def_type
17048 attribute to reference it. */
17049
17050 static struct type *
17051 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17052 {
17053 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17054 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17055 struct type *type, *range_type, *index_type, *char_type;
17056 struct attribute *attr;
17057 unsigned int length;
17058
17059 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17060 if (attr)
17061 {
17062 length = DW_UNSND (attr);
17063 }
17064 else
17065 {
17066 /* Check for the DW_AT_byte_size attribute. */
17067 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17068 if (attr)
17069 {
17070 length = DW_UNSND (attr);
17071 }
17072 else
17073 {
17074 length = 1;
17075 }
17076 }
17077
17078 index_type = objfile_type (objfile)->builtin_int;
17079 range_type = create_static_range_type (NULL, index_type, 1, length);
17080 char_type = language_string_char_type (cu->language_defn, gdbarch);
17081 type = create_string_type (NULL, char_type, range_type);
17082
17083 return set_die_type (die, type, cu);
17084 }
17085
17086 /* Assuming that DIE corresponds to a function, returns nonzero
17087 if the function is prototyped. */
17088
17089 static int
17090 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17091 {
17092 struct attribute *attr;
17093
17094 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17095 if (attr && (DW_UNSND (attr) != 0))
17096 return 1;
17097
17098 /* The DWARF standard implies that the DW_AT_prototyped attribute
17099 is only meaninful for C, but the concept also extends to other
17100 languages that allow unprototyped functions (Eg: Objective C).
17101 For all other languages, assume that functions are always
17102 prototyped. */
17103 if (cu->language != language_c
17104 && cu->language != language_objc
17105 && cu->language != language_opencl)
17106 return 1;
17107
17108 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17109 prototyped and unprototyped functions; default to prototyped,
17110 since that is more common in modern code (and RealView warns
17111 about unprototyped functions). */
17112 if (producer_is_realview (cu->producer))
17113 return 1;
17114
17115 return 0;
17116 }
17117
17118 /* Handle DIES due to C code like:
17119
17120 struct foo
17121 {
17122 int (*funcp)(int a, long l);
17123 int b;
17124 };
17125
17126 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17127
17128 static struct type *
17129 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17130 {
17131 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17132 struct type *type; /* Type that this function returns. */
17133 struct type *ftype; /* Function that returns above type. */
17134 struct attribute *attr;
17135
17136 type = die_type (die, cu);
17137
17138 /* The die_type call above may have already set the type for this DIE. */
17139 ftype = get_die_type (die, cu);
17140 if (ftype)
17141 return ftype;
17142
17143 ftype = lookup_function_type (type);
17144
17145 if (prototyped_function_p (die, cu))
17146 TYPE_PROTOTYPED (ftype) = 1;
17147
17148 /* Store the calling convention in the type if it's available in
17149 the subroutine die. Otherwise set the calling convention to
17150 the default value DW_CC_normal. */
17151 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17152 if (attr)
17153 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17154 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17155 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17156 else
17157 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17158
17159 /* Record whether the function returns normally to its caller or not
17160 if the DWARF producer set that information. */
17161 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17162 if (attr && (DW_UNSND (attr) != 0))
17163 TYPE_NO_RETURN (ftype) = 1;
17164
17165 /* We need to add the subroutine type to the die immediately so
17166 we don't infinitely recurse when dealing with parameters
17167 declared as the same subroutine type. */
17168 set_die_type (die, ftype, cu);
17169
17170 if (die->child != NULL)
17171 {
17172 struct type *void_type = objfile_type (objfile)->builtin_void;
17173 struct die_info *child_die;
17174 int nparams, iparams;
17175
17176 /* Count the number of parameters.
17177 FIXME: GDB currently ignores vararg functions, but knows about
17178 vararg member functions. */
17179 nparams = 0;
17180 child_die = die->child;
17181 while (child_die && child_die->tag)
17182 {
17183 if (child_die->tag == DW_TAG_formal_parameter)
17184 nparams++;
17185 else if (child_die->tag == DW_TAG_unspecified_parameters)
17186 TYPE_VARARGS (ftype) = 1;
17187 child_die = sibling_die (child_die);
17188 }
17189
17190 /* Allocate storage for parameters and fill them in. */
17191 TYPE_NFIELDS (ftype) = nparams;
17192 TYPE_FIELDS (ftype) = (struct field *)
17193 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17194
17195 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17196 even if we error out during the parameters reading below. */
17197 for (iparams = 0; iparams < nparams; iparams++)
17198 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17199
17200 iparams = 0;
17201 child_die = die->child;
17202 while (child_die && child_die->tag)
17203 {
17204 if (child_die->tag == DW_TAG_formal_parameter)
17205 {
17206 struct type *arg_type;
17207
17208 /* DWARF version 2 has no clean way to discern C++
17209 static and non-static member functions. G++ helps
17210 GDB by marking the first parameter for non-static
17211 member functions (which is the this pointer) as
17212 artificial. We pass this information to
17213 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17214
17215 DWARF version 3 added DW_AT_object_pointer, which GCC
17216 4.5 does not yet generate. */
17217 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17218 if (attr)
17219 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17220 else
17221 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17222 arg_type = die_type (child_die, cu);
17223
17224 /* RealView does not mark THIS as const, which the testsuite
17225 expects. GCC marks THIS as const in method definitions,
17226 but not in the class specifications (GCC PR 43053). */
17227 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17228 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17229 {
17230 int is_this = 0;
17231 struct dwarf2_cu *arg_cu = cu;
17232 const char *name = dwarf2_name (child_die, cu);
17233
17234 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17235 if (attr)
17236 {
17237 /* If the compiler emits this, use it. */
17238 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17239 is_this = 1;
17240 }
17241 else if (name && strcmp (name, "this") == 0)
17242 /* Function definitions will have the argument names. */
17243 is_this = 1;
17244 else if (name == NULL && iparams == 0)
17245 /* Declarations may not have the names, so like
17246 elsewhere in GDB, assume an artificial first
17247 argument is "this". */
17248 is_this = 1;
17249
17250 if (is_this)
17251 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17252 arg_type, 0);
17253 }
17254
17255 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17256 iparams++;
17257 }
17258 child_die = sibling_die (child_die);
17259 }
17260 }
17261
17262 return ftype;
17263 }
17264
17265 static struct type *
17266 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17267 {
17268 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17269 const char *name = NULL;
17270 struct type *this_type, *target_type;
17271
17272 name = dwarf2_full_name (NULL, die, cu);
17273 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17274 TYPE_TARGET_STUB (this_type) = 1;
17275 set_die_type (die, this_type, cu);
17276 target_type = die_type (die, cu);
17277 if (target_type != this_type)
17278 TYPE_TARGET_TYPE (this_type) = target_type;
17279 else
17280 {
17281 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17282 spec and cause infinite loops in GDB. */
17283 complaint (&symfile_complaints,
17284 _("Self-referential DW_TAG_typedef "
17285 "- DIE at %s [in module %s]"),
17286 sect_offset_str (die->sect_off), objfile_name (objfile));
17287 TYPE_TARGET_TYPE (this_type) = NULL;
17288 }
17289 return this_type;
17290 }
17291
17292 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17293 (which may be different from NAME) to the architecture back-end to allow
17294 it to guess the correct format if necessary. */
17295
17296 static struct type *
17297 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17298 const char *name_hint)
17299 {
17300 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17301 const struct floatformat **format;
17302 struct type *type;
17303
17304 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17305 if (format)
17306 type = init_float_type (objfile, bits, name, format);
17307 else
17308 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17309
17310 return type;
17311 }
17312
17313 /* Find a representation of a given base type and install
17314 it in the TYPE field of the die. */
17315
17316 static struct type *
17317 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17318 {
17319 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17320 struct type *type;
17321 struct attribute *attr;
17322 int encoding = 0, bits = 0;
17323 const char *name;
17324
17325 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17326 if (attr)
17327 {
17328 encoding = DW_UNSND (attr);
17329 }
17330 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17331 if (attr)
17332 {
17333 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17334 }
17335 name = dwarf2_name (die, cu);
17336 if (!name)
17337 {
17338 complaint (&symfile_complaints,
17339 _("DW_AT_name missing from DW_TAG_base_type"));
17340 }
17341
17342 switch (encoding)
17343 {
17344 case DW_ATE_address:
17345 /* Turn DW_ATE_address into a void * pointer. */
17346 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17347 type = init_pointer_type (objfile, bits, name, type);
17348 break;
17349 case DW_ATE_boolean:
17350 type = init_boolean_type (objfile, bits, 1, name);
17351 break;
17352 case DW_ATE_complex_float:
17353 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
17354 type = init_complex_type (objfile, name, type);
17355 break;
17356 case DW_ATE_decimal_float:
17357 type = init_decfloat_type (objfile, bits, name);
17358 break;
17359 case DW_ATE_float:
17360 type = dwarf2_init_float_type (objfile, bits, name, name);
17361 break;
17362 case DW_ATE_signed:
17363 type = init_integer_type (objfile, bits, 0, name);
17364 break;
17365 case DW_ATE_unsigned:
17366 if (cu->language == language_fortran
17367 && name
17368 && startswith (name, "character("))
17369 type = init_character_type (objfile, bits, 1, name);
17370 else
17371 type = init_integer_type (objfile, bits, 1, name);
17372 break;
17373 case DW_ATE_signed_char:
17374 if (cu->language == language_ada || cu->language == language_m2
17375 || cu->language == language_pascal
17376 || cu->language == language_fortran)
17377 type = init_character_type (objfile, bits, 0, name);
17378 else
17379 type = init_integer_type (objfile, bits, 0, name);
17380 break;
17381 case DW_ATE_unsigned_char:
17382 if (cu->language == language_ada || cu->language == language_m2
17383 || cu->language == language_pascal
17384 || cu->language == language_fortran
17385 || cu->language == language_rust)
17386 type = init_character_type (objfile, bits, 1, name);
17387 else
17388 type = init_integer_type (objfile, bits, 1, name);
17389 break;
17390 case DW_ATE_UTF:
17391 {
17392 gdbarch *arch = get_objfile_arch (objfile);
17393
17394 if (bits == 16)
17395 type = builtin_type (arch)->builtin_char16;
17396 else if (bits == 32)
17397 type = builtin_type (arch)->builtin_char32;
17398 else
17399 {
17400 complaint (&symfile_complaints,
17401 _("unsupported DW_ATE_UTF bit size: '%d'"),
17402 bits);
17403 type = init_integer_type (objfile, bits, 1, name);
17404 }
17405 return set_die_type (die, type, cu);
17406 }
17407 break;
17408
17409 default:
17410 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
17411 dwarf_type_encoding_name (encoding));
17412 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17413 break;
17414 }
17415
17416 if (name && strcmp (name, "char") == 0)
17417 TYPE_NOSIGN (type) = 1;
17418
17419 return set_die_type (die, type, cu);
17420 }
17421
17422 /* Parse dwarf attribute if it's a block, reference or constant and put the
17423 resulting value of the attribute into struct bound_prop.
17424 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17425
17426 static int
17427 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17428 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17429 {
17430 struct dwarf2_property_baton *baton;
17431 struct obstack *obstack
17432 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17433
17434 if (attr == NULL || prop == NULL)
17435 return 0;
17436
17437 if (attr_form_is_block (attr))
17438 {
17439 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17440 baton->referenced_type = NULL;
17441 baton->locexpr.per_cu = cu->per_cu;
17442 baton->locexpr.size = DW_BLOCK (attr)->size;
17443 baton->locexpr.data = DW_BLOCK (attr)->data;
17444 prop->data.baton = baton;
17445 prop->kind = PROP_LOCEXPR;
17446 gdb_assert (prop->data.baton != NULL);
17447 }
17448 else if (attr_form_is_ref (attr))
17449 {
17450 struct dwarf2_cu *target_cu = cu;
17451 struct die_info *target_die;
17452 struct attribute *target_attr;
17453
17454 target_die = follow_die_ref (die, attr, &target_cu);
17455 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17456 if (target_attr == NULL)
17457 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17458 target_cu);
17459 if (target_attr == NULL)
17460 return 0;
17461
17462 switch (target_attr->name)
17463 {
17464 case DW_AT_location:
17465 if (attr_form_is_section_offset (target_attr))
17466 {
17467 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17468 baton->referenced_type = die_type (target_die, target_cu);
17469 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17470 prop->data.baton = baton;
17471 prop->kind = PROP_LOCLIST;
17472 gdb_assert (prop->data.baton != NULL);
17473 }
17474 else if (attr_form_is_block (target_attr))
17475 {
17476 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17477 baton->referenced_type = die_type (target_die, target_cu);
17478 baton->locexpr.per_cu = cu->per_cu;
17479 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17480 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17481 prop->data.baton = baton;
17482 prop->kind = PROP_LOCEXPR;
17483 gdb_assert (prop->data.baton != NULL);
17484 }
17485 else
17486 {
17487 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17488 "dynamic property");
17489 return 0;
17490 }
17491 break;
17492 case DW_AT_data_member_location:
17493 {
17494 LONGEST offset;
17495
17496 if (!handle_data_member_location (target_die, target_cu,
17497 &offset))
17498 return 0;
17499
17500 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17501 baton->referenced_type = read_type_die (target_die->parent,
17502 target_cu);
17503 baton->offset_info.offset = offset;
17504 baton->offset_info.type = die_type (target_die, target_cu);
17505 prop->data.baton = baton;
17506 prop->kind = PROP_ADDR_OFFSET;
17507 break;
17508 }
17509 }
17510 }
17511 else if (attr_form_is_constant (attr))
17512 {
17513 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17514 prop->kind = PROP_CONST;
17515 }
17516 else
17517 {
17518 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17519 dwarf2_name (die, cu));
17520 return 0;
17521 }
17522
17523 return 1;
17524 }
17525
17526 /* Read the given DW_AT_subrange DIE. */
17527
17528 static struct type *
17529 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17530 {
17531 struct type *base_type, *orig_base_type;
17532 struct type *range_type;
17533 struct attribute *attr;
17534 struct dynamic_prop low, high;
17535 int low_default_is_valid;
17536 int high_bound_is_count = 0;
17537 const char *name;
17538 LONGEST negative_mask;
17539
17540 orig_base_type = die_type (die, cu);
17541 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17542 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17543 creating the range type, but we use the result of check_typedef
17544 when examining properties of the type. */
17545 base_type = check_typedef (orig_base_type);
17546
17547 /* The die_type call above may have already set the type for this DIE. */
17548 range_type = get_die_type (die, cu);
17549 if (range_type)
17550 return range_type;
17551
17552 low.kind = PROP_CONST;
17553 high.kind = PROP_CONST;
17554 high.data.const_val = 0;
17555
17556 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17557 omitting DW_AT_lower_bound. */
17558 switch (cu->language)
17559 {
17560 case language_c:
17561 case language_cplus:
17562 low.data.const_val = 0;
17563 low_default_is_valid = 1;
17564 break;
17565 case language_fortran:
17566 low.data.const_val = 1;
17567 low_default_is_valid = 1;
17568 break;
17569 case language_d:
17570 case language_objc:
17571 case language_rust:
17572 low.data.const_val = 0;
17573 low_default_is_valid = (cu->header.version >= 4);
17574 break;
17575 case language_ada:
17576 case language_m2:
17577 case language_pascal:
17578 low.data.const_val = 1;
17579 low_default_is_valid = (cu->header.version >= 4);
17580 break;
17581 default:
17582 low.data.const_val = 0;
17583 low_default_is_valid = 0;
17584 break;
17585 }
17586
17587 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17588 if (attr)
17589 attr_to_dynamic_prop (attr, die, cu, &low);
17590 else if (!low_default_is_valid)
17591 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
17592 "- DIE at %s [in module %s]"),
17593 sect_offset_str (die->sect_off),
17594 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17595
17596 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
17597 if (!attr_to_dynamic_prop (attr, die, cu, &high))
17598 {
17599 attr = dwarf2_attr (die, DW_AT_count, cu);
17600 if (attr_to_dynamic_prop (attr, die, cu, &high))
17601 {
17602 /* If bounds are constant do the final calculation here. */
17603 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17604 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17605 else
17606 high_bound_is_count = 1;
17607 }
17608 }
17609
17610 /* Dwarf-2 specifications explicitly allows to create subrange types
17611 without specifying a base type.
17612 In that case, the base type must be set to the type of
17613 the lower bound, upper bound or count, in that order, if any of these
17614 three attributes references an object that has a type.
17615 If no base type is found, the Dwarf-2 specifications say that
17616 a signed integer type of size equal to the size of an address should
17617 be used.
17618 For the following C code: `extern char gdb_int [];'
17619 GCC produces an empty range DIE.
17620 FIXME: muller/2010-05-28: Possible references to object for low bound,
17621 high bound or count are not yet handled by this code. */
17622 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17623 {
17624 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17625 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17626 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17627 struct type *int_type = objfile_type (objfile)->builtin_int;
17628
17629 /* Test "int", "long int", and "long long int" objfile types,
17630 and select the first one having a size above or equal to the
17631 architecture address size. */
17632 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17633 base_type = int_type;
17634 else
17635 {
17636 int_type = objfile_type (objfile)->builtin_long;
17637 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17638 base_type = int_type;
17639 else
17640 {
17641 int_type = objfile_type (objfile)->builtin_long_long;
17642 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17643 base_type = int_type;
17644 }
17645 }
17646 }
17647
17648 /* Normally, the DWARF producers are expected to use a signed
17649 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17650 But this is unfortunately not always the case, as witnessed
17651 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17652 is used instead. To work around that ambiguity, we treat
17653 the bounds as signed, and thus sign-extend their values, when
17654 the base type is signed. */
17655 negative_mask =
17656 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17657 if (low.kind == PROP_CONST
17658 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17659 low.data.const_val |= negative_mask;
17660 if (high.kind == PROP_CONST
17661 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17662 high.data.const_val |= negative_mask;
17663
17664 range_type = create_range_type (NULL, orig_base_type, &low, &high);
17665
17666 if (high_bound_is_count)
17667 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17668
17669 /* Ada expects an empty array on no boundary attributes. */
17670 if (attr == NULL && cu->language != language_ada)
17671 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17672
17673 name = dwarf2_name (die, cu);
17674 if (name)
17675 TYPE_NAME (range_type) = name;
17676
17677 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17678 if (attr)
17679 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17680
17681 set_die_type (die, range_type, cu);
17682
17683 /* set_die_type should be already done. */
17684 set_descriptive_type (range_type, die, cu);
17685
17686 return range_type;
17687 }
17688
17689 static struct type *
17690 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17691 {
17692 struct type *type;
17693
17694 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17695 NULL);
17696 TYPE_NAME (type) = dwarf2_name (die, cu);
17697
17698 /* In Ada, an unspecified type is typically used when the description
17699 of the type is defered to a different unit. When encountering
17700 such a type, we treat it as a stub, and try to resolve it later on,
17701 when needed. */
17702 if (cu->language == language_ada)
17703 TYPE_STUB (type) = 1;
17704
17705 return set_die_type (die, type, cu);
17706 }
17707
17708 /* Read a single die and all its descendents. Set the die's sibling
17709 field to NULL; set other fields in the die correctly, and set all
17710 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17711 location of the info_ptr after reading all of those dies. PARENT
17712 is the parent of the die in question. */
17713
17714 static struct die_info *
17715 read_die_and_children (const struct die_reader_specs *reader,
17716 const gdb_byte *info_ptr,
17717 const gdb_byte **new_info_ptr,
17718 struct die_info *parent)
17719 {
17720 struct die_info *die;
17721 const gdb_byte *cur_ptr;
17722 int has_children;
17723
17724 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
17725 if (die == NULL)
17726 {
17727 *new_info_ptr = cur_ptr;
17728 return NULL;
17729 }
17730 store_in_ref_table (die, reader->cu);
17731
17732 if (has_children)
17733 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17734 else
17735 {
17736 die->child = NULL;
17737 *new_info_ptr = cur_ptr;
17738 }
17739
17740 die->sibling = NULL;
17741 die->parent = parent;
17742 return die;
17743 }
17744
17745 /* Read a die, all of its descendents, and all of its siblings; set
17746 all of the fields of all of the dies correctly. Arguments are as
17747 in read_die_and_children. */
17748
17749 static struct die_info *
17750 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17751 const gdb_byte *info_ptr,
17752 const gdb_byte **new_info_ptr,
17753 struct die_info *parent)
17754 {
17755 struct die_info *first_die, *last_sibling;
17756 const gdb_byte *cur_ptr;
17757
17758 cur_ptr = info_ptr;
17759 first_die = last_sibling = NULL;
17760
17761 while (1)
17762 {
17763 struct die_info *die
17764 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17765
17766 if (die == NULL)
17767 {
17768 *new_info_ptr = cur_ptr;
17769 return first_die;
17770 }
17771
17772 if (!first_die)
17773 first_die = die;
17774 else
17775 last_sibling->sibling = die;
17776
17777 last_sibling = die;
17778 }
17779 }
17780
17781 /* Read a die, all of its descendents, and all of its siblings; set
17782 all of the fields of all of the dies correctly. Arguments are as
17783 in read_die_and_children.
17784 This the main entry point for reading a DIE and all its children. */
17785
17786 static struct die_info *
17787 read_die_and_siblings (const struct die_reader_specs *reader,
17788 const gdb_byte *info_ptr,
17789 const gdb_byte **new_info_ptr,
17790 struct die_info *parent)
17791 {
17792 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17793 new_info_ptr, parent);
17794
17795 if (dwarf_die_debug)
17796 {
17797 fprintf_unfiltered (gdb_stdlog,
17798 "Read die from %s@0x%x of %s:\n",
17799 get_section_name (reader->die_section),
17800 (unsigned) (info_ptr - reader->die_section->buffer),
17801 bfd_get_filename (reader->abfd));
17802 dump_die (die, dwarf_die_debug);
17803 }
17804
17805 return die;
17806 }
17807
17808 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17809 attributes.
17810 The caller is responsible for filling in the extra attributes
17811 and updating (*DIEP)->num_attrs.
17812 Set DIEP to point to a newly allocated die with its information,
17813 except for its child, sibling, and parent fields.
17814 Set HAS_CHILDREN to tell whether the die has children or not. */
17815
17816 static const gdb_byte *
17817 read_full_die_1 (const struct die_reader_specs *reader,
17818 struct die_info **diep, const gdb_byte *info_ptr,
17819 int *has_children, int num_extra_attrs)
17820 {
17821 unsigned int abbrev_number, bytes_read, i;
17822 struct abbrev_info *abbrev;
17823 struct die_info *die;
17824 struct dwarf2_cu *cu = reader->cu;
17825 bfd *abfd = reader->abfd;
17826
17827 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17828 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17829 info_ptr += bytes_read;
17830 if (!abbrev_number)
17831 {
17832 *diep = NULL;
17833 *has_children = 0;
17834 return info_ptr;
17835 }
17836
17837 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17838 if (!abbrev)
17839 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17840 abbrev_number,
17841 bfd_get_filename (abfd));
17842
17843 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17844 die->sect_off = sect_off;
17845 die->tag = abbrev->tag;
17846 die->abbrev = abbrev_number;
17847
17848 /* Make the result usable.
17849 The caller needs to update num_attrs after adding the extra
17850 attributes. */
17851 die->num_attrs = abbrev->num_attrs;
17852
17853 for (i = 0; i < abbrev->num_attrs; ++i)
17854 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17855 info_ptr);
17856
17857 *diep = die;
17858 *has_children = abbrev->has_children;
17859 return info_ptr;
17860 }
17861
17862 /* Read a die and all its attributes.
17863 Set DIEP to point to a newly allocated die with its information,
17864 except for its child, sibling, and parent fields.
17865 Set HAS_CHILDREN to tell whether the die has children or not. */
17866
17867 static const gdb_byte *
17868 read_full_die (const struct die_reader_specs *reader,
17869 struct die_info **diep, const gdb_byte *info_ptr,
17870 int *has_children)
17871 {
17872 const gdb_byte *result;
17873
17874 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
17875
17876 if (dwarf_die_debug)
17877 {
17878 fprintf_unfiltered (gdb_stdlog,
17879 "Read die from %s@0x%x of %s:\n",
17880 get_section_name (reader->die_section),
17881 (unsigned) (info_ptr - reader->die_section->buffer),
17882 bfd_get_filename (reader->abfd));
17883 dump_die (*diep, dwarf_die_debug);
17884 }
17885
17886 return result;
17887 }
17888 \f
17889 /* Abbreviation tables.
17890
17891 In DWARF version 2, the description of the debugging information is
17892 stored in a separate .debug_abbrev section. Before we read any
17893 dies from a section we read in all abbreviations and install them
17894 in a hash table. */
17895
17896 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
17897
17898 struct abbrev_info *
17899 abbrev_table::alloc_abbrev ()
17900 {
17901 struct abbrev_info *abbrev;
17902
17903 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
17904 memset (abbrev, 0, sizeof (struct abbrev_info));
17905
17906 return abbrev;
17907 }
17908
17909 /* Add an abbreviation to the table. */
17910
17911 void
17912 abbrev_table::add_abbrev (unsigned int abbrev_number,
17913 struct abbrev_info *abbrev)
17914 {
17915 unsigned int hash_number;
17916
17917 hash_number = abbrev_number % ABBREV_HASH_SIZE;
17918 abbrev->next = m_abbrevs[hash_number];
17919 m_abbrevs[hash_number] = abbrev;
17920 }
17921
17922 /* Look up an abbrev in the table.
17923 Returns NULL if the abbrev is not found. */
17924
17925 struct abbrev_info *
17926 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
17927 {
17928 unsigned int hash_number;
17929 struct abbrev_info *abbrev;
17930
17931 hash_number = abbrev_number % ABBREV_HASH_SIZE;
17932 abbrev = m_abbrevs[hash_number];
17933
17934 while (abbrev)
17935 {
17936 if (abbrev->number == abbrev_number)
17937 return abbrev;
17938 abbrev = abbrev->next;
17939 }
17940 return NULL;
17941 }
17942
17943 /* Read in an abbrev table. */
17944
17945 static abbrev_table_up
17946 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
17947 struct dwarf2_section_info *section,
17948 sect_offset sect_off)
17949 {
17950 struct objfile *objfile = dwarf2_per_objfile->objfile;
17951 bfd *abfd = get_section_bfd_owner (section);
17952 const gdb_byte *abbrev_ptr;
17953 struct abbrev_info *cur_abbrev;
17954 unsigned int abbrev_number, bytes_read, abbrev_name;
17955 unsigned int abbrev_form;
17956 struct attr_abbrev *cur_attrs;
17957 unsigned int allocated_attrs;
17958
17959 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
17960
17961 dwarf2_read_section (objfile, section);
17962 abbrev_ptr = section->buffer + to_underlying (sect_off);
17963 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17964 abbrev_ptr += bytes_read;
17965
17966 allocated_attrs = ATTR_ALLOC_CHUNK;
17967 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
17968
17969 /* Loop until we reach an abbrev number of 0. */
17970 while (abbrev_number)
17971 {
17972 cur_abbrev = abbrev_table->alloc_abbrev ();
17973
17974 /* read in abbrev header */
17975 cur_abbrev->number = abbrev_number;
17976 cur_abbrev->tag
17977 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17978 abbrev_ptr += bytes_read;
17979 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
17980 abbrev_ptr += 1;
17981
17982 /* now read in declarations */
17983 for (;;)
17984 {
17985 LONGEST implicit_const;
17986
17987 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17988 abbrev_ptr += bytes_read;
17989 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17990 abbrev_ptr += bytes_read;
17991 if (abbrev_form == DW_FORM_implicit_const)
17992 {
17993 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
17994 &bytes_read);
17995 abbrev_ptr += bytes_read;
17996 }
17997 else
17998 {
17999 /* Initialize it due to a false compiler warning. */
18000 implicit_const = -1;
18001 }
18002
18003 if (abbrev_name == 0)
18004 break;
18005
18006 if (cur_abbrev->num_attrs == allocated_attrs)
18007 {
18008 allocated_attrs += ATTR_ALLOC_CHUNK;
18009 cur_attrs
18010 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18011 }
18012
18013 cur_attrs[cur_abbrev->num_attrs].name
18014 = (enum dwarf_attribute) abbrev_name;
18015 cur_attrs[cur_abbrev->num_attrs].form
18016 = (enum dwarf_form) abbrev_form;
18017 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18018 ++cur_abbrev->num_attrs;
18019 }
18020
18021 cur_abbrev->attrs =
18022 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18023 cur_abbrev->num_attrs);
18024 memcpy (cur_abbrev->attrs, cur_attrs,
18025 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18026
18027 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18028
18029 /* Get next abbreviation.
18030 Under Irix6 the abbreviations for a compilation unit are not
18031 always properly terminated with an abbrev number of 0.
18032 Exit loop if we encounter an abbreviation which we have
18033 already read (which means we are about to read the abbreviations
18034 for the next compile unit) or if the end of the abbreviation
18035 table is reached. */
18036 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18037 break;
18038 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18039 abbrev_ptr += bytes_read;
18040 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18041 break;
18042 }
18043
18044 xfree (cur_attrs);
18045 return abbrev_table;
18046 }
18047
18048 /* Returns nonzero if TAG represents a type that we might generate a partial
18049 symbol for. */
18050
18051 static int
18052 is_type_tag_for_partial (int tag)
18053 {
18054 switch (tag)
18055 {
18056 #if 0
18057 /* Some types that would be reasonable to generate partial symbols for,
18058 that we don't at present. */
18059 case DW_TAG_array_type:
18060 case DW_TAG_file_type:
18061 case DW_TAG_ptr_to_member_type:
18062 case DW_TAG_set_type:
18063 case DW_TAG_string_type:
18064 case DW_TAG_subroutine_type:
18065 #endif
18066 case DW_TAG_base_type:
18067 case DW_TAG_class_type:
18068 case DW_TAG_interface_type:
18069 case DW_TAG_enumeration_type:
18070 case DW_TAG_structure_type:
18071 case DW_TAG_subrange_type:
18072 case DW_TAG_typedef:
18073 case DW_TAG_union_type:
18074 return 1;
18075 default:
18076 return 0;
18077 }
18078 }
18079
18080 /* Load all DIEs that are interesting for partial symbols into memory. */
18081
18082 static struct partial_die_info *
18083 load_partial_dies (const struct die_reader_specs *reader,
18084 const gdb_byte *info_ptr, int building_psymtab)
18085 {
18086 struct dwarf2_cu *cu = reader->cu;
18087 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18088 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18089 unsigned int bytes_read;
18090 unsigned int load_all = 0;
18091 int nesting_level = 1;
18092
18093 parent_die = NULL;
18094 last_die = NULL;
18095
18096 gdb_assert (cu->per_cu != NULL);
18097 if (cu->per_cu->load_all_dies)
18098 load_all = 1;
18099
18100 cu->partial_dies
18101 = htab_create_alloc_ex (cu->header.length / 12,
18102 partial_die_hash,
18103 partial_die_eq,
18104 NULL,
18105 &cu->comp_unit_obstack,
18106 hashtab_obstack_allocate,
18107 dummy_obstack_deallocate);
18108
18109 while (1)
18110 {
18111 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18112
18113 /* A NULL abbrev means the end of a series of children. */
18114 if (abbrev == NULL)
18115 {
18116 if (--nesting_level == 0)
18117 return first_die;
18118
18119 info_ptr += bytes_read;
18120 last_die = parent_die;
18121 parent_die = parent_die->die_parent;
18122 continue;
18123 }
18124
18125 /* Check for template arguments. We never save these; if
18126 they're seen, we just mark the parent, and go on our way. */
18127 if (parent_die != NULL
18128 && cu->language == language_cplus
18129 && (abbrev->tag == DW_TAG_template_type_param
18130 || abbrev->tag == DW_TAG_template_value_param))
18131 {
18132 parent_die->has_template_arguments = 1;
18133
18134 if (!load_all)
18135 {
18136 /* We don't need a partial DIE for the template argument. */
18137 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18138 continue;
18139 }
18140 }
18141
18142 /* We only recurse into c++ subprograms looking for template arguments.
18143 Skip their other children. */
18144 if (!load_all
18145 && cu->language == language_cplus
18146 && parent_die != NULL
18147 && parent_die->tag == DW_TAG_subprogram)
18148 {
18149 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18150 continue;
18151 }
18152
18153 /* Check whether this DIE is interesting enough to save. Normally
18154 we would not be interested in members here, but there may be
18155 later variables referencing them via DW_AT_specification (for
18156 static members). */
18157 if (!load_all
18158 && !is_type_tag_for_partial (abbrev->tag)
18159 && abbrev->tag != DW_TAG_constant
18160 && abbrev->tag != DW_TAG_enumerator
18161 && abbrev->tag != DW_TAG_subprogram
18162 && abbrev->tag != DW_TAG_inlined_subroutine
18163 && abbrev->tag != DW_TAG_lexical_block
18164 && abbrev->tag != DW_TAG_variable
18165 && abbrev->tag != DW_TAG_namespace
18166 && abbrev->tag != DW_TAG_module
18167 && abbrev->tag != DW_TAG_member
18168 && abbrev->tag != DW_TAG_imported_unit
18169 && abbrev->tag != DW_TAG_imported_declaration)
18170 {
18171 /* Otherwise we skip to the next sibling, if any. */
18172 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18173 continue;
18174 }
18175
18176 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18177 abbrev);
18178
18179 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18180
18181 /* This two-pass algorithm for processing partial symbols has a
18182 high cost in cache pressure. Thus, handle some simple cases
18183 here which cover the majority of C partial symbols. DIEs
18184 which neither have specification tags in them, nor could have
18185 specification tags elsewhere pointing at them, can simply be
18186 processed and discarded.
18187
18188 This segment is also optional; scan_partial_symbols and
18189 add_partial_symbol will handle these DIEs if we chain
18190 them in normally. When compilers which do not emit large
18191 quantities of duplicate debug information are more common,
18192 this code can probably be removed. */
18193
18194 /* Any complete simple types at the top level (pretty much all
18195 of them, for a language without namespaces), can be processed
18196 directly. */
18197 if (parent_die == NULL
18198 && pdi.has_specification == 0
18199 && pdi.is_declaration == 0
18200 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18201 || pdi.tag == DW_TAG_base_type
18202 || pdi.tag == DW_TAG_subrange_type))
18203 {
18204 if (building_psymtab && pdi.name != NULL)
18205 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18206 VAR_DOMAIN, LOC_TYPEDEF,
18207 &objfile->static_psymbols,
18208 0, cu->language, objfile);
18209 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18210 continue;
18211 }
18212
18213 /* The exception for DW_TAG_typedef with has_children above is
18214 a workaround of GCC PR debug/47510. In the case of this complaint
18215 type_name_no_tag_or_error will error on such types later.
18216
18217 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18218 it could not find the child DIEs referenced later, this is checked
18219 above. In correct DWARF DW_TAG_typedef should have no children. */
18220
18221 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18222 complaint (&symfile_complaints,
18223 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18224 "- DIE at %s [in module %s]"),
18225 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18226
18227 /* If we're at the second level, and we're an enumerator, and
18228 our parent has no specification (meaning possibly lives in a
18229 namespace elsewhere), then we can add the partial symbol now
18230 instead of queueing it. */
18231 if (pdi.tag == DW_TAG_enumerator
18232 && parent_die != NULL
18233 && parent_die->die_parent == NULL
18234 && parent_die->tag == DW_TAG_enumeration_type
18235 && parent_die->has_specification == 0)
18236 {
18237 if (pdi.name == NULL)
18238 complaint (&symfile_complaints,
18239 _("malformed enumerator DIE ignored"));
18240 else if (building_psymtab)
18241 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
18242 VAR_DOMAIN, LOC_CONST,
18243 cu->language == language_cplus
18244 ? &objfile->global_psymbols
18245 : &objfile->static_psymbols,
18246 0, cu->language, objfile);
18247
18248 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18249 continue;
18250 }
18251
18252 struct partial_die_info *part_die
18253 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18254
18255 /* We'll save this DIE so link it in. */
18256 part_die->die_parent = parent_die;
18257 part_die->die_sibling = NULL;
18258 part_die->die_child = NULL;
18259
18260 if (last_die && last_die == parent_die)
18261 last_die->die_child = part_die;
18262 else if (last_die)
18263 last_die->die_sibling = part_die;
18264
18265 last_die = part_die;
18266
18267 if (first_die == NULL)
18268 first_die = part_die;
18269
18270 /* Maybe add the DIE to the hash table. Not all DIEs that we
18271 find interesting need to be in the hash table, because we
18272 also have the parent/sibling/child chains; only those that we
18273 might refer to by offset later during partial symbol reading.
18274
18275 For now this means things that might have be the target of a
18276 DW_AT_specification, DW_AT_abstract_origin, or
18277 DW_AT_extension. DW_AT_extension will refer only to
18278 namespaces; DW_AT_abstract_origin refers to functions (and
18279 many things under the function DIE, but we do not recurse
18280 into function DIEs during partial symbol reading) and
18281 possibly variables as well; DW_AT_specification refers to
18282 declarations. Declarations ought to have the DW_AT_declaration
18283 flag. It happens that GCC forgets to put it in sometimes, but
18284 only for functions, not for types.
18285
18286 Adding more things than necessary to the hash table is harmless
18287 except for the performance cost. Adding too few will result in
18288 wasted time in find_partial_die, when we reread the compilation
18289 unit with load_all_dies set. */
18290
18291 if (load_all
18292 || abbrev->tag == DW_TAG_constant
18293 || abbrev->tag == DW_TAG_subprogram
18294 || abbrev->tag == DW_TAG_variable
18295 || abbrev->tag == DW_TAG_namespace
18296 || part_die->is_declaration)
18297 {
18298 void **slot;
18299
18300 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18301 to_underlying (part_die->sect_off),
18302 INSERT);
18303 *slot = part_die;
18304 }
18305
18306 /* For some DIEs we want to follow their children (if any). For C
18307 we have no reason to follow the children of structures; for other
18308 languages we have to, so that we can get at method physnames
18309 to infer fully qualified class names, for DW_AT_specification,
18310 and for C++ template arguments. For C++, we also look one level
18311 inside functions to find template arguments (if the name of the
18312 function does not already contain the template arguments).
18313
18314 For Ada, we need to scan the children of subprograms and lexical
18315 blocks as well because Ada allows the definition of nested
18316 entities that could be interesting for the debugger, such as
18317 nested subprograms for instance. */
18318 if (last_die->has_children
18319 && (load_all
18320 || last_die->tag == DW_TAG_namespace
18321 || last_die->tag == DW_TAG_module
18322 || last_die->tag == DW_TAG_enumeration_type
18323 || (cu->language == language_cplus
18324 && last_die->tag == DW_TAG_subprogram
18325 && (last_die->name == NULL
18326 || strchr (last_die->name, '<') == NULL))
18327 || (cu->language != language_c
18328 && (last_die->tag == DW_TAG_class_type
18329 || last_die->tag == DW_TAG_interface_type
18330 || last_die->tag == DW_TAG_structure_type
18331 || last_die->tag == DW_TAG_union_type))
18332 || (cu->language == language_ada
18333 && (last_die->tag == DW_TAG_subprogram
18334 || last_die->tag == DW_TAG_lexical_block))))
18335 {
18336 nesting_level++;
18337 parent_die = last_die;
18338 continue;
18339 }
18340
18341 /* Otherwise we skip to the next sibling, if any. */
18342 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18343
18344 /* Back to the top, do it again. */
18345 }
18346 }
18347
18348 partial_die_info::partial_die_info (sect_offset sect_off_,
18349 struct abbrev_info *abbrev)
18350 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18351 {
18352 }
18353
18354 /* Read a minimal amount of information into the minimal die structure.
18355 INFO_PTR should point just after the initial uleb128 of a DIE. */
18356
18357 const gdb_byte *
18358 partial_die_info::read (const struct die_reader_specs *reader,
18359 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18360 {
18361 struct dwarf2_cu *cu = reader->cu;
18362 struct dwarf2_per_objfile *dwarf2_per_objfile
18363 = cu->per_cu->dwarf2_per_objfile;
18364 unsigned int i;
18365 int has_low_pc_attr = 0;
18366 int has_high_pc_attr = 0;
18367 int high_pc_relative = 0;
18368
18369 for (i = 0; i < abbrev.num_attrs; ++i)
18370 {
18371 struct attribute attr;
18372
18373 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18374
18375 /* Store the data if it is of an attribute we want to keep in a
18376 partial symbol table. */
18377 switch (attr.name)
18378 {
18379 case DW_AT_name:
18380 switch (tag)
18381 {
18382 case DW_TAG_compile_unit:
18383 case DW_TAG_partial_unit:
18384 case DW_TAG_type_unit:
18385 /* Compilation units have a DW_AT_name that is a filename, not
18386 a source language identifier. */
18387 case DW_TAG_enumeration_type:
18388 case DW_TAG_enumerator:
18389 /* These tags always have simple identifiers already; no need
18390 to canonicalize them. */
18391 name = DW_STRING (&attr);
18392 break;
18393 default:
18394 {
18395 struct objfile *objfile = dwarf2_per_objfile->objfile;
18396
18397 name
18398 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18399 &objfile->per_bfd->storage_obstack);
18400 }
18401 break;
18402 }
18403 break;
18404 case DW_AT_linkage_name:
18405 case DW_AT_MIPS_linkage_name:
18406 /* Note that both forms of linkage name might appear. We
18407 assume they will be the same, and we only store the last
18408 one we see. */
18409 if (cu->language == language_ada)
18410 name = DW_STRING (&attr);
18411 linkage_name = DW_STRING (&attr);
18412 break;
18413 case DW_AT_low_pc:
18414 has_low_pc_attr = 1;
18415 lowpc = attr_value_as_address (&attr);
18416 break;
18417 case DW_AT_high_pc:
18418 has_high_pc_attr = 1;
18419 highpc = attr_value_as_address (&attr);
18420 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18421 high_pc_relative = 1;
18422 break;
18423 case DW_AT_location:
18424 /* Support the .debug_loc offsets. */
18425 if (attr_form_is_block (&attr))
18426 {
18427 d.locdesc = DW_BLOCK (&attr);
18428 }
18429 else if (attr_form_is_section_offset (&attr))
18430 {
18431 dwarf2_complex_location_expr_complaint ();
18432 }
18433 else
18434 {
18435 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18436 "partial symbol information");
18437 }
18438 break;
18439 case DW_AT_external:
18440 is_external = DW_UNSND (&attr);
18441 break;
18442 case DW_AT_declaration:
18443 is_declaration = DW_UNSND (&attr);
18444 break;
18445 case DW_AT_type:
18446 has_type = 1;
18447 break;
18448 case DW_AT_abstract_origin:
18449 case DW_AT_specification:
18450 case DW_AT_extension:
18451 has_specification = 1;
18452 spec_offset = dwarf2_get_ref_die_offset (&attr);
18453 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18454 || cu->per_cu->is_dwz);
18455 break;
18456 case DW_AT_sibling:
18457 /* Ignore absolute siblings, they might point outside of
18458 the current compile unit. */
18459 if (attr.form == DW_FORM_ref_addr)
18460 complaint (&symfile_complaints,
18461 _("ignoring absolute DW_AT_sibling"));
18462 else
18463 {
18464 const gdb_byte *buffer = reader->buffer;
18465 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18466 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18467
18468 if (sibling_ptr < info_ptr)
18469 complaint (&symfile_complaints,
18470 _("DW_AT_sibling points backwards"));
18471 else if (sibling_ptr > reader->buffer_end)
18472 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18473 else
18474 sibling = sibling_ptr;
18475 }
18476 break;
18477 case DW_AT_byte_size:
18478 has_byte_size = 1;
18479 break;
18480 case DW_AT_const_value:
18481 has_const_value = 1;
18482 break;
18483 case DW_AT_calling_convention:
18484 /* DWARF doesn't provide a way to identify a program's source-level
18485 entry point. DW_AT_calling_convention attributes are only meant
18486 to describe functions' calling conventions.
18487
18488 However, because it's a necessary piece of information in
18489 Fortran, and before DWARF 4 DW_CC_program was the only
18490 piece of debugging information whose definition refers to
18491 a 'main program' at all, several compilers marked Fortran
18492 main programs with DW_CC_program --- even when those
18493 functions use the standard calling conventions.
18494
18495 Although DWARF now specifies a way to provide this
18496 information, we support this practice for backward
18497 compatibility. */
18498 if (DW_UNSND (&attr) == DW_CC_program
18499 && cu->language == language_fortran)
18500 main_subprogram = 1;
18501 break;
18502 case DW_AT_inline:
18503 if (DW_UNSND (&attr) == DW_INL_inlined
18504 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18505 may_be_inlined = 1;
18506 break;
18507
18508 case DW_AT_import:
18509 if (tag == DW_TAG_imported_unit)
18510 {
18511 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18512 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18513 || cu->per_cu->is_dwz);
18514 }
18515 break;
18516
18517 case DW_AT_main_subprogram:
18518 main_subprogram = DW_UNSND (&attr);
18519 break;
18520
18521 default:
18522 break;
18523 }
18524 }
18525
18526 if (high_pc_relative)
18527 highpc += lowpc;
18528
18529 if (has_low_pc_attr && has_high_pc_attr)
18530 {
18531 /* When using the GNU linker, .gnu.linkonce. sections are used to
18532 eliminate duplicate copies of functions and vtables and such.
18533 The linker will arbitrarily choose one and discard the others.
18534 The AT_*_pc values for such functions refer to local labels in
18535 these sections. If the section from that file was discarded, the
18536 labels are not in the output, so the relocs get a value of 0.
18537 If this is a discarded function, mark the pc bounds as invalid,
18538 so that GDB will ignore it. */
18539 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18540 {
18541 struct objfile *objfile = dwarf2_per_objfile->objfile;
18542 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18543
18544 complaint (&symfile_complaints,
18545 _("DW_AT_low_pc %s is zero "
18546 "for DIE at %s [in module %s]"),
18547 paddress (gdbarch, lowpc),
18548 sect_offset_str (sect_off),
18549 objfile_name (objfile));
18550 }
18551 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18552 else if (lowpc >= highpc)
18553 {
18554 struct objfile *objfile = dwarf2_per_objfile->objfile;
18555 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18556
18557 complaint (&symfile_complaints,
18558 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18559 "for DIE at %s [in module %s]"),
18560 paddress (gdbarch, lowpc),
18561 paddress (gdbarch, highpc),
18562 sect_offset_str (sect_off),
18563 objfile_name (objfile));
18564 }
18565 else
18566 has_pc_info = 1;
18567 }
18568
18569 return info_ptr;
18570 }
18571
18572 /* Find a cached partial DIE at OFFSET in CU. */
18573
18574 struct partial_die_info *
18575 dwarf2_cu::find_partial_die (sect_offset sect_off)
18576 {
18577 struct partial_die_info *lookup_die = NULL;
18578 struct partial_die_info part_die (sect_off);
18579
18580 lookup_die = ((struct partial_die_info *)
18581 htab_find_with_hash (partial_dies, &part_die,
18582 to_underlying (sect_off)));
18583
18584 return lookup_die;
18585 }
18586
18587 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18588 except in the case of .debug_types DIEs which do not reference
18589 outside their CU (they do however referencing other types via
18590 DW_FORM_ref_sig8). */
18591
18592 static struct partial_die_info *
18593 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18594 {
18595 struct dwarf2_per_objfile *dwarf2_per_objfile
18596 = cu->per_cu->dwarf2_per_objfile;
18597 struct objfile *objfile = dwarf2_per_objfile->objfile;
18598 struct dwarf2_per_cu_data *per_cu = NULL;
18599 struct partial_die_info *pd = NULL;
18600
18601 if (offset_in_dwz == cu->per_cu->is_dwz
18602 && offset_in_cu_p (&cu->header, sect_off))
18603 {
18604 pd = cu->find_partial_die (sect_off);
18605 if (pd != NULL)
18606 return pd;
18607 /* We missed recording what we needed.
18608 Load all dies and try again. */
18609 per_cu = cu->per_cu;
18610 }
18611 else
18612 {
18613 /* TUs don't reference other CUs/TUs (except via type signatures). */
18614 if (cu->per_cu->is_debug_types)
18615 {
18616 error (_("Dwarf Error: Type Unit at offset %s contains"
18617 " external reference to offset %s [in module %s].\n"),
18618 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18619 bfd_get_filename (objfile->obfd));
18620 }
18621 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18622 dwarf2_per_objfile);
18623
18624 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18625 load_partial_comp_unit (per_cu);
18626
18627 per_cu->cu->last_used = 0;
18628 pd = per_cu->cu->find_partial_die (sect_off);
18629 }
18630
18631 /* If we didn't find it, and not all dies have been loaded,
18632 load them all and try again. */
18633
18634 if (pd == NULL && per_cu->load_all_dies == 0)
18635 {
18636 per_cu->load_all_dies = 1;
18637
18638 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18639 THIS_CU->cu may already be in use. So we can't just free it and
18640 replace its DIEs with the ones we read in. Instead, we leave those
18641 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18642 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18643 set. */
18644 load_partial_comp_unit (per_cu);
18645
18646 pd = per_cu->cu->find_partial_die (sect_off);
18647 }
18648
18649 if (pd == NULL)
18650 internal_error (__FILE__, __LINE__,
18651 _("could not find partial DIE %s "
18652 "in cache [from module %s]\n"),
18653 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18654 return pd;
18655 }
18656
18657 /* See if we can figure out if the class lives in a namespace. We do
18658 this by looking for a member function; its demangled name will
18659 contain namespace info, if there is any. */
18660
18661 static void
18662 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18663 struct dwarf2_cu *cu)
18664 {
18665 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18666 what template types look like, because the demangler
18667 frequently doesn't give the same name as the debug info. We
18668 could fix this by only using the demangled name to get the
18669 prefix (but see comment in read_structure_type). */
18670
18671 struct partial_die_info *real_pdi;
18672 struct partial_die_info *child_pdi;
18673
18674 /* If this DIE (this DIE's specification, if any) has a parent, then
18675 we should not do this. We'll prepend the parent's fully qualified
18676 name when we create the partial symbol. */
18677
18678 real_pdi = struct_pdi;
18679 while (real_pdi->has_specification)
18680 real_pdi = find_partial_die (real_pdi->spec_offset,
18681 real_pdi->spec_is_dwz, cu);
18682
18683 if (real_pdi->die_parent != NULL)
18684 return;
18685
18686 for (child_pdi = struct_pdi->die_child;
18687 child_pdi != NULL;
18688 child_pdi = child_pdi->die_sibling)
18689 {
18690 if (child_pdi->tag == DW_TAG_subprogram
18691 && child_pdi->linkage_name != NULL)
18692 {
18693 char *actual_class_name
18694 = language_class_name_from_physname (cu->language_defn,
18695 child_pdi->linkage_name);
18696 if (actual_class_name != NULL)
18697 {
18698 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18699 struct_pdi->name
18700 = ((const char *)
18701 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18702 actual_class_name,
18703 strlen (actual_class_name)));
18704 xfree (actual_class_name);
18705 }
18706 break;
18707 }
18708 }
18709 }
18710
18711 void
18712 partial_die_info::fixup (struct dwarf2_cu *cu)
18713 {
18714 /* Once we've fixed up a die, there's no point in doing so again.
18715 This also avoids a memory leak if we were to call
18716 guess_partial_die_structure_name multiple times. */
18717 if (fixup_called)
18718 return;
18719
18720 /* If we found a reference attribute and the DIE has no name, try
18721 to find a name in the referred to DIE. */
18722
18723 if (name == NULL && has_specification)
18724 {
18725 struct partial_die_info *spec_die;
18726
18727 spec_die = find_partial_die (spec_offset, spec_is_dwz, cu);
18728
18729 spec_die->fixup (cu);
18730
18731 if (spec_die->name)
18732 {
18733 name = spec_die->name;
18734
18735 /* Copy DW_AT_external attribute if it is set. */
18736 if (spec_die->is_external)
18737 is_external = spec_die->is_external;
18738 }
18739 }
18740
18741 /* Set default names for some unnamed DIEs. */
18742
18743 if (name == NULL && tag == DW_TAG_namespace)
18744 name = CP_ANONYMOUS_NAMESPACE_STR;
18745
18746 /* If there is no parent die to provide a namespace, and there are
18747 children, see if we can determine the namespace from their linkage
18748 name. */
18749 if (cu->language == language_cplus
18750 && !VEC_empty (dwarf2_section_info_def,
18751 cu->per_cu->dwarf2_per_objfile->types)
18752 && die_parent == NULL
18753 && has_children
18754 && (tag == DW_TAG_class_type
18755 || tag == DW_TAG_structure_type
18756 || tag == DW_TAG_union_type))
18757 guess_partial_die_structure_name (this, cu);
18758
18759 /* GCC might emit a nameless struct or union that has a linkage
18760 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18761 if (name == NULL
18762 && (tag == DW_TAG_class_type
18763 || tag == DW_TAG_interface_type
18764 || tag == DW_TAG_structure_type
18765 || tag == DW_TAG_union_type)
18766 && linkage_name != NULL)
18767 {
18768 char *demangled;
18769
18770 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
18771 if (demangled)
18772 {
18773 const char *base;
18774
18775 /* Strip any leading namespaces/classes, keep only the base name.
18776 DW_AT_name for named DIEs does not contain the prefixes. */
18777 base = strrchr (demangled, ':');
18778 if (base && base > demangled && base[-1] == ':')
18779 base++;
18780 else
18781 base = demangled;
18782
18783 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18784 name
18785 = ((const char *)
18786 obstack_copy0 (&objfile->per_bfd->storage_obstack,
18787 base, strlen (base)));
18788 xfree (demangled);
18789 }
18790 }
18791
18792 fixup_called = 1;
18793 }
18794
18795 /* Read an attribute value described by an attribute form. */
18796
18797 static const gdb_byte *
18798 read_attribute_value (const struct die_reader_specs *reader,
18799 struct attribute *attr, unsigned form,
18800 LONGEST implicit_const, const gdb_byte *info_ptr)
18801 {
18802 struct dwarf2_cu *cu = reader->cu;
18803 struct dwarf2_per_objfile *dwarf2_per_objfile
18804 = cu->per_cu->dwarf2_per_objfile;
18805 struct objfile *objfile = dwarf2_per_objfile->objfile;
18806 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18807 bfd *abfd = reader->abfd;
18808 struct comp_unit_head *cu_header = &cu->header;
18809 unsigned int bytes_read;
18810 struct dwarf_block *blk;
18811
18812 attr->form = (enum dwarf_form) form;
18813 switch (form)
18814 {
18815 case DW_FORM_ref_addr:
18816 if (cu->header.version == 2)
18817 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18818 else
18819 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18820 &cu->header, &bytes_read);
18821 info_ptr += bytes_read;
18822 break;
18823 case DW_FORM_GNU_ref_alt:
18824 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18825 info_ptr += bytes_read;
18826 break;
18827 case DW_FORM_addr:
18828 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
18829 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18830 info_ptr += bytes_read;
18831 break;
18832 case DW_FORM_block2:
18833 blk = dwarf_alloc_block (cu);
18834 blk->size = read_2_bytes (abfd, info_ptr);
18835 info_ptr += 2;
18836 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18837 info_ptr += blk->size;
18838 DW_BLOCK (attr) = blk;
18839 break;
18840 case DW_FORM_block4:
18841 blk = dwarf_alloc_block (cu);
18842 blk->size = read_4_bytes (abfd, info_ptr);
18843 info_ptr += 4;
18844 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18845 info_ptr += blk->size;
18846 DW_BLOCK (attr) = blk;
18847 break;
18848 case DW_FORM_data2:
18849 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18850 info_ptr += 2;
18851 break;
18852 case DW_FORM_data4:
18853 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18854 info_ptr += 4;
18855 break;
18856 case DW_FORM_data8:
18857 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18858 info_ptr += 8;
18859 break;
18860 case DW_FORM_data16:
18861 blk = dwarf_alloc_block (cu);
18862 blk->size = 16;
18863 blk->data = read_n_bytes (abfd, info_ptr, 16);
18864 info_ptr += 16;
18865 DW_BLOCK (attr) = blk;
18866 break;
18867 case DW_FORM_sec_offset:
18868 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18869 info_ptr += bytes_read;
18870 break;
18871 case DW_FORM_string:
18872 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18873 DW_STRING_IS_CANONICAL (attr) = 0;
18874 info_ptr += bytes_read;
18875 break;
18876 case DW_FORM_strp:
18877 if (!cu->per_cu->is_dwz)
18878 {
18879 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18880 abfd, info_ptr, cu_header,
18881 &bytes_read);
18882 DW_STRING_IS_CANONICAL (attr) = 0;
18883 info_ptr += bytes_read;
18884 break;
18885 }
18886 /* FALLTHROUGH */
18887 case DW_FORM_line_strp:
18888 if (!cu->per_cu->is_dwz)
18889 {
18890 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18891 abfd, info_ptr,
18892 cu_header, &bytes_read);
18893 DW_STRING_IS_CANONICAL (attr) = 0;
18894 info_ptr += bytes_read;
18895 break;
18896 }
18897 /* FALLTHROUGH */
18898 case DW_FORM_GNU_strp_alt:
18899 {
18900 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18901 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
18902 &bytes_read);
18903
18904 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18905 dwz, str_offset);
18906 DW_STRING_IS_CANONICAL (attr) = 0;
18907 info_ptr += bytes_read;
18908 }
18909 break;
18910 case DW_FORM_exprloc:
18911 case DW_FORM_block:
18912 blk = dwarf_alloc_block (cu);
18913 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18914 info_ptr += bytes_read;
18915 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18916 info_ptr += blk->size;
18917 DW_BLOCK (attr) = blk;
18918 break;
18919 case DW_FORM_block1:
18920 blk = dwarf_alloc_block (cu);
18921 blk->size = read_1_byte (abfd, info_ptr);
18922 info_ptr += 1;
18923 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18924 info_ptr += blk->size;
18925 DW_BLOCK (attr) = blk;
18926 break;
18927 case DW_FORM_data1:
18928 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18929 info_ptr += 1;
18930 break;
18931 case DW_FORM_flag:
18932 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18933 info_ptr += 1;
18934 break;
18935 case DW_FORM_flag_present:
18936 DW_UNSND (attr) = 1;
18937 break;
18938 case DW_FORM_sdata:
18939 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18940 info_ptr += bytes_read;
18941 break;
18942 case DW_FORM_udata:
18943 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18944 info_ptr += bytes_read;
18945 break;
18946 case DW_FORM_ref1:
18947 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18948 + read_1_byte (abfd, info_ptr));
18949 info_ptr += 1;
18950 break;
18951 case DW_FORM_ref2:
18952 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18953 + read_2_bytes (abfd, info_ptr));
18954 info_ptr += 2;
18955 break;
18956 case DW_FORM_ref4:
18957 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18958 + read_4_bytes (abfd, info_ptr));
18959 info_ptr += 4;
18960 break;
18961 case DW_FORM_ref8:
18962 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18963 + read_8_bytes (abfd, info_ptr));
18964 info_ptr += 8;
18965 break;
18966 case DW_FORM_ref_sig8:
18967 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18968 info_ptr += 8;
18969 break;
18970 case DW_FORM_ref_udata:
18971 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18972 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18973 info_ptr += bytes_read;
18974 break;
18975 case DW_FORM_indirect:
18976 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18977 info_ptr += bytes_read;
18978 if (form == DW_FORM_implicit_const)
18979 {
18980 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18981 info_ptr += bytes_read;
18982 }
18983 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18984 info_ptr);
18985 break;
18986 case DW_FORM_implicit_const:
18987 DW_SND (attr) = implicit_const;
18988 break;
18989 case DW_FORM_GNU_addr_index:
18990 if (reader->dwo_file == NULL)
18991 {
18992 /* For now flag a hard error.
18993 Later we can turn this into a complaint. */
18994 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
18995 dwarf_form_name (form),
18996 bfd_get_filename (abfd));
18997 }
18998 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
18999 info_ptr += bytes_read;
19000 break;
19001 case DW_FORM_GNU_str_index:
19002 if (reader->dwo_file == NULL)
19003 {
19004 /* For now flag a hard error.
19005 Later we can turn this into a complaint if warranted. */
19006 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19007 dwarf_form_name (form),
19008 bfd_get_filename (abfd));
19009 }
19010 {
19011 ULONGEST str_index =
19012 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19013
19014 DW_STRING (attr) = read_str_index (reader, str_index);
19015 DW_STRING_IS_CANONICAL (attr) = 0;
19016 info_ptr += bytes_read;
19017 }
19018 break;
19019 default:
19020 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19021 dwarf_form_name (form),
19022 bfd_get_filename (abfd));
19023 }
19024
19025 /* Super hack. */
19026 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19027 attr->form = DW_FORM_GNU_ref_alt;
19028
19029 /* We have seen instances where the compiler tried to emit a byte
19030 size attribute of -1 which ended up being encoded as an unsigned
19031 0xffffffff. Although 0xffffffff is technically a valid size value,
19032 an object of this size seems pretty unlikely so we can relatively
19033 safely treat these cases as if the size attribute was invalid and
19034 treat them as zero by default. */
19035 if (attr->name == DW_AT_byte_size
19036 && form == DW_FORM_data4
19037 && DW_UNSND (attr) >= 0xffffffff)
19038 {
19039 complaint
19040 (&symfile_complaints,
19041 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19042 hex_string (DW_UNSND (attr)));
19043 DW_UNSND (attr) = 0;
19044 }
19045
19046 return info_ptr;
19047 }
19048
19049 /* Read an attribute described by an abbreviated attribute. */
19050
19051 static const gdb_byte *
19052 read_attribute (const struct die_reader_specs *reader,
19053 struct attribute *attr, struct attr_abbrev *abbrev,
19054 const gdb_byte *info_ptr)
19055 {
19056 attr->name = abbrev->name;
19057 return read_attribute_value (reader, attr, abbrev->form,
19058 abbrev->implicit_const, info_ptr);
19059 }
19060
19061 /* Read dwarf information from a buffer. */
19062
19063 static unsigned int
19064 read_1_byte (bfd *abfd, const gdb_byte *buf)
19065 {
19066 return bfd_get_8 (abfd, buf);
19067 }
19068
19069 static int
19070 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19071 {
19072 return bfd_get_signed_8 (abfd, buf);
19073 }
19074
19075 static unsigned int
19076 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19077 {
19078 return bfd_get_16 (abfd, buf);
19079 }
19080
19081 static int
19082 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19083 {
19084 return bfd_get_signed_16 (abfd, buf);
19085 }
19086
19087 static unsigned int
19088 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19089 {
19090 return bfd_get_32 (abfd, buf);
19091 }
19092
19093 static int
19094 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19095 {
19096 return bfd_get_signed_32 (abfd, buf);
19097 }
19098
19099 static ULONGEST
19100 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19101 {
19102 return bfd_get_64 (abfd, buf);
19103 }
19104
19105 static CORE_ADDR
19106 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19107 unsigned int *bytes_read)
19108 {
19109 struct comp_unit_head *cu_header = &cu->header;
19110 CORE_ADDR retval = 0;
19111
19112 if (cu_header->signed_addr_p)
19113 {
19114 switch (cu_header->addr_size)
19115 {
19116 case 2:
19117 retval = bfd_get_signed_16 (abfd, buf);
19118 break;
19119 case 4:
19120 retval = bfd_get_signed_32 (abfd, buf);
19121 break;
19122 case 8:
19123 retval = bfd_get_signed_64 (abfd, buf);
19124 break;
19125 default:
19126 internal_error (__FILE__, __LINE__,
19127 _("read_address: bad switch, signed [in module %s]"),
19128 bfd_get_filename (abfd));
19129 }
19130 }
19131 else
19132 {
19133 switch (cu_header->addr_size)
19134 {
19135 case 2:
19136 retval = bfd_get_16 (abfd, buf);
19137 break;
19138 case 4:
19139 retval = bfd_get_32 (abfd, buf);
19140 break;
19141 case 8:
19142 retval = bfd_get_64 (abfd, buf);
19143 break;
19144 default:
19145 internal_error (__FILE__, __LINE__,
19146 _("read_address: bad switch, "
19147 "unsigned [in module %s]"),
19148 bfd_get_filename (abfd));
19149 }
19150 }
19151
19152 *bytes_read = cu_header->addr_size;
19153 return retval;
19154 }
19155
19156 /* Read the initial length from a section. The (draft) DWARF 3
19157 specification allows the initial length to take up either 4 bytes
19158 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19159 bytes describe the length and all offsets will be 8 bytes in length
19160 instead of 4.
19161
19162 An older, non-standard 64-bit format is also handled by this
19163 function. The older format in question stores the initial length
19164 as an 8-byte quantity without an escape value. Lengths greater
19165 than 2^32 aren't very common which means that the initial 4 bytes
19166 is almost always zero. Since a length value of zero doesn't make
19167 sense for the 32-bit format, this initial zero can be considered to
19168 be an escape value which indicates the presence of the older 64-bit
19169 format. As written, the code can't detect (old format) lengths
19170 greater than 4GB. If it becomes necessary to handle lengths
19171 somewhat larger than 4GB, we could allow other small values (such
19172 as the non-sensical values of 1, 2, and 3) to also be used as
19173 escape values indicating the presence of the old format.
19174
19175 The value returned via bytes_read should be used to increment the
19176 relevant pointer after calling read_initial_length().
19177
19178 [ Note: read_initial_length() and read_offset() are based on the
19179 document entitled "DWARF Debugging Information Format", revision
19180 3, draft 8, dated November 19, 2001. This document was obtained
19181 from:
19182
19183 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19184
19185 This document is only a draft and is subject to change. (So beware.)
19186
19187 Details regarding the older, non-standard 64-bit format were
19188 determined empirically by examining 64-bit ELF files produced by
19189 the SGI toolchain on an IRIX 6.5 machine.
19190
19191 - Kevin, July 16, 2002
19192 ] */
19193
19194 static LONGEST
19195 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19196 {
19197 LONGEST length = bfd_get_32 (abfd, buf);
19198
19199 if (length == 0xffffffff)
19200 {
19201 length = bfd_get_64 (abfd, buf + 4);
19202 *bytes_read = 12;
19203 }
19204 else if (length == 0)
19205 {
19206 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19207 length = bfd_get_64 (abfd, buf);
19208 *bytes_read = 8;
19209 }
19210 else
19211 {
19212 *bytes_read = 4;
19213 }
19214
19215 return length;
19216 }
19217
19218 /* Cover function for read_initial_length.
19219 Returns the length of the object at BUF, and stores the size of the
19220 initial length in *BYTES_READ and stores the size that offsets will be in
19221 *OFFSET_SIZE.
19222 If the initial length size is not equivalent to that specified in
19223 CU_HEADER then issue a complaint.
19224 This is useful when reading non-comp-unit headers. */
19225
19226 static LONGEST
19227 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19228 const struct comp_unit_head *cu_header,
19229 unsigned int *bytes_read,
19230 unsigned int *offset_size)
19231 {
19232 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19233
19234 gdb_assert (cu_header->initial_length_size == 4
19235 || cu_header->initial_length_size == 8
19236 || cu_header->initial_length_size == 12);
19237
19238 if (cu_header->initial_length_size != *bytes_read)
19239 complaint (&symfile_complaints,
19240 _("intermixed 32-bit and 64-bit DWARF sections"));
19241
19242 *offset_size = (*bytes_read == 4) ? 4 : 8;
19243 return length;
19244 }
19245
19246 /* Read an offset from the data stream. The size of the offset is
19247 given by cu_header->offset_size. */
19248
19249 static LONGEST
19250 read_offset (bfd *abfd, const gdb_byte *buf,
19251 const struct comp_unit_head *cu_header,
19252 unsigned int *bytes_read)
19253 {
19254 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19255
19256 *bytes_read = cu_header->offset_size;
19257 return offset;
19258 }
19259
19260 /* Read an offset from the data stream. */
19261
19262 static LONGEST
19263 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19264 {
19265 LONGEST retval = 0;
19266
19267 switch (offset_size)
19268 {
19269 case 4:
19270 retval = bfd_get_32 (abfd, buf);
19271 break;
19272 case 8:
19273 retval = bfd_get_64 (abfd, buf);
19274 break;
19275 default:
19276 internal_error (__FILE__, __LINE__,
19277 _("read_offset_1: bad switch [in module %s]"),
19278 bfd_get_filename (abfd));
19279 }
19280
19281 return retval;
19282 }
19283
19284 static const gdb_byte *
19285 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19286 {
19287 /* If the size of a host char is 8 bits, we can return a pointer
19288 to the buffer, otherwise we have to copy the data to a buffer
19289 allocated on the temporary obstack. */
19290 gdb_assert (HOST_CHAR_BIT == 8);
19291 return buf;
19292 }
19293
19294 static const char *
19295 read_direct_string (bfd *abfd, const gdb_byte *buf,
19296 unsigned int *bytes_read_ptr)
19297 {
19298 /* If the size of a host char is 8 bits, we can return a pointer
19299 to the string, otherwise we have to copy the string to a buffer
19300 allocated on the temporary obstack. */
19301 gdb_assert (HOST_CHAR_BIT == 8);
19302 if (*buf == '\0')
19303 {
19304 *bytes_read_ptr = 1;
19305 return NULL;
19306 }
19307 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19308 return (const char *) buf;
19309 }
19310
19311 /* Return pointer to string at section SECT offset STR_OFFSET with error
19312 reporting strings FORM_NAME and SECT_NAME. */
19313
19314 static const char *
19315 read_indirect_string_at_offset_from (struct objfile *objfile,
19316 bfd *abfd, LONGEST str_offset,
19317 struct dwarf2_section_info *sect,
19318 const char *form_name,
19319 const char *sect_name)
19320 {
19321 dwarf2_read_section (objfile, sect);
19322 if (sect->buffer == NULL)
19323 error (_("%s used without %s section [in module %s]"),
19324 form_name, sect_name, bfd_get_filename (abfd));
19325 if (str_offset >= sect->size)
19326 error (_("%s pointing outside of %s section [in module %s]"),
19327 form_name, sect_name, bfd_get_filename (abfd));
19328 gdb_assert (HOST_CHAR_BIT == 8);
19329 if (sect->buffer[str_offset] == '\0')
19330 return NULL;
19331 return (const char *) (sect->buffer + str_offset);
19332 }
19333
19334 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19335
19336 static const char *
19337 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19338 bfd *abfd, LONGEST str_offset)
19339 {
19340 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19341 abfd, str_offset,
19342 &dwarf2_per_objfile->str,
19343 "DW_FORM_strp", ".debug_str");
19344 }
19345
19346 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19347
19348 static const char *
19349 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19350 bfd *abfd, LONGEST str_offset)
19351 {
19352 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19353 abfd, str_offset,
19354 &dwarf2_per_objfile->line_str,
19355 "DW_FORM_line_strp",
19356 ".debug_line_str");
19357 }
19358
19359 /* Read a string at offset STR_OFFSET in the .debug_str section from
19360 the .dwz file DWZ. Throw an error if the offset is too large. If
19361 the string consists of a single NUL byte, return NULL; otherwise
19362 return a pointer to the string. */
19363
19364 static const char *
19365 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19366 LONGEST str_offset)
19367 {
19368 dwarf2_read_section (objfile, &dwz->str);
19369
19370 if (dwz->str.buffer == NULL)
19371 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19372 "section [in module %s]"),
19373 bfd_get_filename (dwz->dwz_bfd));
19374 if (str_offset >= dwz->str.size)
19375 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19376 ".debug_str section [in module %s]"),
19377 bfd_get_filename (dwz->dwz_bfd));
19378 gdb_assert (HOST_CHAR_BIT == 8);
19379 if (dwz->str.buffer[str_offset] == '\0')
19380 return NULL;
19381 return (const char *) (dwz->str.buffer + str_offset);
19382 }
19383
19384 /* Return pointer to string at .debug_str offset as read from BUF.
19385 BUF is assumed to be in a compilation unit described by CU_HEADER.
19386 Return *BYTES_READ_PTR count of bytes read from BUF. */
19387
19388 static const char *
19389 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19390 const gdb_byte *buf,
19391 const struct comp_unit_head *cu_header,
19392 unsigned int *bytes_read_ptr)
19393 {
19394 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19395
19396 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19397 }
19398
19399 /* Return pointer to string at .debug_line_str offset as read from BUF.
19400 BUF is assumed to be in a compilation unit described by CU_HEADER.
19401 Return *BYTES_READ_PTR count of bytes read from BUF. */
19402
19403 static const char *
19404 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19405 bfd *abfd, const gdb_byte *buf,
19406 const struct comp_unit_head *cu_header,
19407 unsigned int *bytes_read_ptr)
19408 {
19409 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19410
19411 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19412 str_offset);
19413 }
19414
19415 ULONGEST
19416 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19417 unsigned int *bytes_read_ptr)
19418 {
19419 ULONGEST result;
19420 unsigned int num_read;
19421 int shift;
19422 unsigned char byte;
19423
19424 result = 0;
19425 shift = 0;
19426 num_read = 0;
19427 while (1)
19428 {
19429 byte = bfd_get_8 (abfd, buf);
19430 buf++;
19431 num_read++;
19432 result |= ((ULONGEST) (byte & 127) << shift);
19433 if ((byte & 128) == 0)
19434 {
19435 break;
19436 }
19437 shift += 7;
19438 }
19439 *bytes_read_ptr = num_read;
19440 return result;
19441 }
19442
19443 static LONGEST
19444 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19445 unsigned int *bytes_read_ptr)
19446 {
19447 LONGEST result;
19448 int shift, num_read;
19449 unsigned char byte;
19450
19451 result = 0;
19452 shift = 0;
19453 num_read = 0;
19454 while (1)
19455 {
19456 byte = bfd_get_8 (abfd, buf);
19457 buf++;
19458 num_read++;
19459 result |= ((LONGEST) (byte & 127) << shift);
19460 shift += 7;
19461 if ((byte & 128) == 0)
19462 {
19463 break;
19464 }
19465 }
19466 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19467 result |= -(((LONGEST) 1) << shift);
19468 *bytes_read_ptr = num_read;
19469 return result;
19470 }
19471
19472 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19473 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19474 ADDR_SIZE is the size of addresses from the CU header. */
19475
19476 static CORE_ADDR
19477 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19478 unsigned int addr_index, ULONGEST addr_base, int addr_size)
19479 {
19480 struct objfile *objfile = dwarf2_per_objfile->objfile;
19481 bfd *abfd = objfile->obfd;
19482 const gdb_byte *info_ptr;
19483
19484 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19485 if (dwarf2_per_objfile->addr.buffer == NULL)
19486 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19487 objfile_name (objfile));
19488 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19489 error (_("DW_FORM_addr_index pointing outside of "
19490 ".debug_addr section [in module %s]"),
19491 objfile_name (objfile));
19492 info_ptr = (dwarf2_per_objfile->addr.buffer
19493 + addr_base + addr_index * addr_size);
19494 if (addr_size == 4)
19495 return bfd_get_32 (abfd, info_ptr);
19496 else
19497 return bfd_get_64 (abfd, info_ptr);
19498 }
19499
19500 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19501
19502 static CORE_ADDR
19503 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19504 {
19505 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19506 cu->addr_base, cu->header.addr_size);
19507 }
19508
19509 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19510
19511 static CORE_ADDR
19512 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19513 unsigned int *bytes_read)
19514 {
19515 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19516 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19517
19518 return read_addr_index (cu, addr_index);
19519 }
19520
19521 /* Data structure to pass results from dwarf2_read_addr_index_reader
19522 back to dwarf2_read_addr_index. */
19523
19524 struct dwarf2_read_addr_index_data
19525 {
19526 ULONGEST addr_base;
19527 int addr_size;
19528 };
19529
19530 /* die_reader_func for dwarf2_read_addr_index. */
19531
19532 static void
19533 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19534 const gdb_byte *info_ptr,
19535 struct die_info *comp_unit_die,
19536 int has_children,
19537 void *data)
19538 {
19539 struct dwarf2_cu *cu = reader->cu;
19540 struct dwarf2_read_addr_index_data *aidata =
19541 (struct dwarf2_read_addr_index_data *) data;
19542
19543 aidata->addr_base = cu->addr_base;
19544 aidata->addr_size = cu->header.addr_size;
19545 }
19546
19547 /* Given an index in .debug_addr, fetch the value.
19548 NOTE: This can be called during dwarf expression evaluation,
19549 long after the debug information has been read, and thus per_cu->cu
19550 may no longer exist. */
19551
19552 CORE_ADDR
19553 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19554 unsigned int addr_index)
19555 {
19556 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19557 struct dwarf2_cu *cu = per_cu->cu;
19558 ULONGEST addr_base;
19559 int addr_size;
19560
19561 /* We need addr_base and addr_size.
19562 If we don't have PER_CU->cu, we have to get it.
19563 Nasty, but the alternative is storing the needed info in PER_CU,
19564 which at this point doesn't seem justified: it's not clear how frequently
19565 it would get used and it would increase the size of every PER_CU.
19566 Entry points like dwarf2_per_cu_addr_size do a similar thing
19567 so we're not in uncharted territory here.
19568 Alas we need to be a bit more complicated as addr_base is contained
19569 in the DIE.
19570
19571 We don't need to read the entire CU(/TU).
19572 We just need the header and top level die.
19573
19574 IWBN to use the aging mechanism to let us lazily later discard the CU.
19575 For now we skip this optimization. */
19576
19577 if (cu != NULL)
19578 {
19579 addr_base = cu->addr_base;
19580 addr_size = cu->header.addr_size;
19581 }
19582 else
19583 {
19584 struct dwarf2_read_addr_index_data aidata;
19585
19586 /* Note: We can't use init_cutu_and_read_dies_simple here,
19587 we need addr_base. */
19588 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
19589 dwarf2_read_addr_index_reader, &aidata);
19590 addr_base = aidata.addr_base;
19591 addr_size = aidata.addr_size;
19592 }
19593
19594 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19595 addr_size);
19596 }
19597
19598 /* Given a DW_FORM_GNU_str_index, fetch the string.
19599 This is only used by the Fission support. */
19600
19601 static const char *
19602 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19603 {
19604 struct dwarf2_cu *cu = reader->cu;
19605 struct dwarf2_per_objfile *dwarf2_per_objfile
19606 = cu->per_cu->dwarf2_per_objfile;
19607 struct objfile *objfile = dwarf2_per_objfile->objfile;
19608 const char *objf_name = objfile_name (objfile);
19609 bfd *abfd = objfile->obfd;
19610 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19611 struct dwarf2_section_info *str_offsets_section =
19612 &reader->dwo_file->sections.str_offsets;
19613 const gdb_byte *info_ptr;
19614 ULONGEST str_offset;
19615 static const char form_name[] = "DW_FORM_GNU_str_index";
19616
19617 dwarf2_read_section (objfile, str_section);
19618 dwarf2_read_section (objfile, str_offsets_section);
19619 if (str_section->buffer == NULL)
19620 error (_("%s used without .debug_str.dwo section"
19621 " in CU at offset %s [in module %s]"),
19622 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19623 if (str_offsets_section->buffer == NULL)
19624 error (_("%s used without .debug_str_offsets.dwo section"
19625 " in CU at offset %s [in module %s]"),
19626 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19627 if (str_index * cu->header.offset_size >= str_offsets_section->size)
19628 error (_("%s pointing outside of .debug_str_offsets.dwo"
19629 " section in CU at offset %s [in module %s]"),
19630 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19631 info_ptr = (str_offsets_section->buffer
19632 + str_index * cu->header.offset_size);
19633 if (cu->header.offset_size == 4)
19634 str_offset = bfd_get_32 (abfd, info_ptr);
19635 else
19636 str_offset = bfd_get_64 (abfd, info_ptr);
19637 if (str_offset >= str_section->size)
19638 error (_("Offset from %s pointing outside of"
19639 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19640 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19641 return (const char *) (str_section->buffer + str_offset);
19642 }
19643
19644 /* Return the length of an LEB128 number in BUF. */
19645
19646 static int
19647 leb128_size (const gdb_byte *buf)
19648 {
19649 const gdb_byte *begin = buf;
19650 gdb_byte byte;
19651
19652 while (1)
19653 {
19654 byte = *buf++;
19655 if ((byte & 128) == 0)
19656 return buf - begin;
19657 }
19658 }
19659
19660 static void
19661 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19662 {
19663 switch (lang)
19664 {
19665 case DW_LANG_C89:
19666 case DW_LANG_C99:
19667 case DW_LANG_C11:
19668 case DW_LANG_C:
19669 case DW_LANG_UPC:
19670 cu->language = language_c;
19671 break;
19672 case DW_LANG_Java:
19673 case DW_LANG_C_plus_plus:
19674 case DW_LANG_C_plus_plus_11:
19675 case DW_LANG_C_plus_plus_14:
19676 cu->language = language_cplus;
19677 break;
19678 case DW_LANG_D:
19679 cu->language = language_d;
19680 break;
19681 case DW_LANG_Fortran77:
19682 case DW_LANG_Fortran90:
19683 case DW_LANG_Fortran95:
19684 case DW_LANG_Fortran03:
19685 case DW_LANG_Fortran08:
19686 cu->language = language_fortran;
19687 break;
19688 case DW_LANG_Go:
19689 cu->language = language_go;
19690 break;
19691 case DW_LANG_Mips_Assembler:
19692 cu->language = language_asm;
19693 break;
19694 case DW_LANG_Ada83:
19695 case DW_LANG_Ada95:
19696 cu->language = language_ada;
19697 break;
19698 case DW_LANG_Modula2:
19699 cu->language = language_m2;
19700 break;
19701 case DW_LANG_Pascal83:
19702 cu->language = language_pascal;
19703 break;
19704 case DW_LANG_ObjC:
19705 cu->language = language_objc;
19706 break;
19707 case DW_LANG_Rust:
19708 case DW_LANG_Rust_old:
19709 cu->language = language_rust;
19710 break;
19711 case DW_LANG_Cobol74:
19712 case DW_LANG_Cobol85:
19713 default:
19714 cu->language = language_minimal;
19715 break;
19716 }
19717 cu->language_defn = language_def (cu->language);
19718 }
19719
19720 /* Return the named attribute or NULL if not there. */
19721
19722 static struct attribute *
19723 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19724 {
19725 for (;;)
19726 {
19727 unsigned int i;
19728 struct attribute *spec = NULL;
19729
19730 for (i = 0; i < die->num_attrs; ++i)
19731 {
19732 if (die->attrs[i].name == name)
19733 return &die->attrs[i];
19734 if (die->attrs[i].name == DW_AT_specification
19735 || die->attrs[i].name == DW_AT_abstract_origin)
19736 spec = &die->attrs[i];
19737 }
19738
19739 if (!spec)
19740 break;
19741
19742 die = follow_die_ref (die, spec, &cu);
19743 }
19744
19745 return NULL;
19746 }
19747
19748 /* Return the named attribute or NULL if not there,
19749 but do not follow DW_AT_specification, etc.
19750 This is for use in contexts where we're reading .debug_types dies.
19751 Following DW_AT_specification, DW_AT_abstract_origin will take us
19752 back up the chain, and we want to go down. */
19753
19754 static struct attribute *
19755 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19756 {
19757 unsigned int i;
19758
19759 for (i = 0; i < die->num_attrs; ++i)
19760 if (die->attrs[i].name == name)
19761 return &die->attrs[i];
19762
19763 return NULL;
19764 }
19765
19766 /* Return the string associated with a string-typed attribute, or NULL if it
19767 is either not found or is of an incorrect type. */
19768
19769 static const char *
19770 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19771 {
19772 struct attribute *attr;
19773 const char *str = NULL;
19774
19775 attr = dwarf2_attr (die, name, cu);
19776
19777 if (attr != NULL)
19778 {
19779 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19780 || attr->form == DW_FORM_string
19781 || attr->form == DW_FORM_GNU_str_index
19782 || attr->form == DW_FORM_GNU_strp_alt)
19783 str = DW_STRING (attr);
19784 else
19785 complaint (&symfile_complaints,
19786 _("string type expected for attribute %s for "
19787 "DIE at %s in module %s"),
19788 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19789 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19790 }
19791
19792 return str;
19793 }
19794
19795 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19796 and holds a non-zero value. This function should only be used for
19797 DW_FORM_flag or DW_FORM_flag_present attributes. */
19798
19799 static int
19800 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19801 {
19802 struct attribute *attr = dwarf2_attr (die, name, cu);
19803
19804 return (attr && DW_UNSND (attr));
19805 }
19806
19807 static int
19808 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19809 {
19810 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19811 which value is non-zero. However, we have to be careful with
19812 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19813 (via dwarf2_flag_true_p) follows this attribute. So we may
19814 end up accidently finding a declaration attribute that belongs
19815 to a different DIE referenced by the specification attribute,
19816 even though the given DIE does not have a declaration attribute. */
19817 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19818 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19819 }
19820
19821 /* Return the die giving the specification for DIE, if there is
19822 one. *SPEC_CU is the CU containing DIE on input, and the CU
19823 containing the return value on output. If there is no
19824 specification, but there is an abstract origin, that is
19825 returned. */
19826
19827 static struct die_info *
19828 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19829 {
19830 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19831 *spec_cu);
19832
19833 if (spec_attr == NULL)
19834 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19835
19836 if (spec_attr == NULL)
19837 return NULL;
19838 else
19839 return follow_die_ref (die, spec_attr, spec_cu);
19840 }
19841
19842 /* Stub for free_line_header to match void * callback types. */
19843
19844 static void
19845 free_line_header_voidp (void *arg)
19846 {
19847 struct line_header *lh = (struct line_header *) arg;
19848
19849 delete lh;
19850 }
19851
19852 void
19853 line_header::add_include_dir (const char *include_dir)
19854 {
19855 if (dwarf_line_debug >= 2)
19856 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19857 include_dirs.size () + 1, include_dir);
19858
19859 include_dirs.push_back (include_dir);
19860 }
19861
19862 void
19863 line_header::add_file_name (const char *name,
19864 dir_index d_index,
19865 unsigned int mod_time,
19866 unsigned int length)
19867 {
19868 if (dwarf_line_debug >= 2)
19869 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
19870 (unsigned) file_names.size () + 1, name);
19871
19872 file_names.emplace_back (name, d_index, mod_time, length);
19873 }
19874
19875 /* A convenience function to find the proper .debug_line section for a CU. */
19876
19877 static struct dwarf2_section_info *
19878 get_debug_line_section (struct dwarf2_cu *cu)
19879 {
19880 struct dwarf2_section_info *section;
19881 struct dwarf2_per_objfile *dwarf2_per_objfile
19882 = cu->per_cu->dwarf2_per_objfile;
19883
19884 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19885 DWO file. */
19886 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19887 section = &cu->dwo_unit->dwo_file->sections.line;
19888 else if (cu->per_cu->is_dwz)
19889 {
19890 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19891
19892 section = &dwz->line;
19893 }
19894 else
19895 section = &dwarf2_per_objfile->line;
19896
19897 return section;
19898 }
19899
19900 /* Read directory or file name entry format, starting with byte of
19901 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19902 entries count and the entries themselves in the described entry
19903 format. */
19904
19905 static void
19906 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19907 bfd *abfd, const gdb_byte **bufp,
19908 struct line_header *lh,
19909 const struct comp_unit_head *cu_header,
19910 void (*callback) (struct line_header *lh,
19911 const char *name,
19912 dir_index d_index,
19913 unsigned int mod_time,
19914 unsigned int length))
19915 {
19916 gdb_byte format_count, formati;
19917 ULONGEST data_count, datai;
19918 const gdb_byte *buf = *bufp;
19919 const gdb_byte *format_header_data;
19920 unsigned int bytes_read;
19921
19922 format_count = read_1_byte (abfd, buf);
19923 buf += 1;
19924 format_header_data = buf;
19925 for (formati = 0; formati < format_count; formati++)
19926 {
19927 read_unsigned_leb128 (abfd, buf, &bytes_read);
19928 buf += bytes_read;
19929 read_unsigned_leb128 (abfd, buf, &bytes_read);
19930 buf += bytes_read;
19931 }
19932
19933 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19934 buf += bytes_read;
19935 for (datai = 0; datai < data_count; datai++)
19936 {
19937 const gdb_byte *format = format_header_data;
19938 struct file_entry fe;
19939
19940 for (formati = 0; formati < format_count; formati++)
19941 {
19942 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
19943 format += bytes_read;
19944
19945 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
19946 format += bytes_read;
19947
19948 gdb::optional<const char *> string;
19949 gdb::optional<unsigned int> uint;
19950
19951 switch (form)
19952 {
19953 case DW_FORM_string:
19954 string.emplace (read_direct_string (abfd, buf, &bytes_read));
19955 buf += bytes_read;
19956 break;
19957
19958 case DW_FORM_line_strp:
19959 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
19960 abfd, buf,
19961 cu_header,
19962 &bytes_read));
19963 buf += bytes_read;
19964 break;
19965
19966 case DW_FORM_data1:
19967 uint.emplace (read_1_byte (abfd, buf));
19968 buf += 1;
19969 break;
19970
19971 case DW_FORM_data2:
19972 uint.emplace (read_2_bytes (abfd, buf));
19973 buf += 2;
19974 break;
19975
19976 case DW_FORM_data4:
19977 uint.emplace (read_4_bytes (abfd, buf));
19978 buf += 4;
19979 break;
19980
19981 case DW_FORM_data8:
19982 uint.emplace (read_8_bytes (abfd, buf));
19983 buf += 8;
19984 break;
19985
19986 case DW_FORM_udata:
19987 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
19988 buf += bytes_read;
19989 break;
19990
19991 case DW_FORM_block:
19992 /* It is valid only for DW_LNCT_timestamp which is ignored by
19993 current GDB. */
19994 break;
19995 }
19996
19997 switch (content_type)
19998 {
19999 case DW_LNCT_path:
20000 if (string.has_value ())
20001 fe.name = *string;
20002 break;
20003 case DW_LNCT_directory_index:
20004 if (uint.has_value ())
20005 fe.d_index = (dir_index) *uint;
20006 break;
20007 case DW_LNCT_timestamp:
20008 if (uint.has_value ())
20009 fe.mod_time = *uint;
20010 break;
20011 case DW_LNCT_size:
20012 if (uint.has_value ())
20013 fe.length = *uint;
20014 break;
20015 case DW_LNCT_MD5:
20016 break;
20017 default:
20018 complaint (&symfile_complaints,
20019 _("Unknown format content type %s"),
20020 pulongest (content_type));
20021 }
20022 }
20023
20024 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20025 }
20026
20027 *bufp = buf;
20028 }
20029
20030 /* Read the statement program header starting at OFFSET in
20031 .debug_line, or .debug_line.dwo. Return a pointer
20032 to a struct line_header, allocated using xmalloc.
20033 Returns NULL if there is a problem reading the header, e.g., if it
20034 has a version we don't understand.
20035
20036 NOTE: the strings in the include directory and file name tables of
20037 the returned object point into the dwarf line section buffer,
20038 and must not be freed. */
20039
20040 static line_header_up
20041 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20042 {
20043 const gdb_byte *line_ptr;
20044 unsigned int bytes_read, offset_size;
20045 int i;
20046 const char *cur_dir, *cur_file;
20047 struct dwarf2_section_info *section;
20048 bfd *abfd;
20049 struct dwarf2_per_objfile *dwarf2_per_objfile
20050 = cu->per_cu->dwarf2_per_objfile;
20051
20052 section = get_debug_line_section (cu);
20053 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20054 if (section->buffer == NULL)
20055 {
20056 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20057 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
20058 else
20059 complaint (&symfile_complaints, _("missing .debug_line section"));
20060 return 0;
20061 }
20062
20063 /* We can't do this until we know the section is non-empty.
20064 Only then do we know we have such a section. */
20065 abfd = get_section_bfd_owner (section);
20066
20067 /* Make sure that at least there's room for the total_length field.
20068 That could be 12 bytes long, but we're just going to fudge that. */
20069 if (to_underlying (sect_off) + 4 >= section->size)
20070 {
20071 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20072 return 0;
20073 }
20074
20075 line_header_up lh (new line_header ());
20076
20077 lh->sect_off = sect_off;
20078 lh->offset_in_dwz = cu->per_cu->is_dwz;
20079
20080 line_ptr = section->buffer + to_underlying (sect_off);
20081
20082 /* Read in the header. */
20083 lh->total_length =
20084 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20085 &bytes_read, &offset_size);
20086 line_ptr += bytes_read;
20087 if (line_ptr + lh->total_length > (section->buffer + section->size))
20088 {
20089 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20090 return 0;
20091 }
20092 lh->statement_program_end = line_ptr + lh->total_length;
20093 lh->version = read_2_bytes (abfd, line_ptr);
20094 line_ptr += 2;
20095 if (lh->version > 5)
20096 {
20097 /* This is a version we don't understand. The format could have
20098 changed in ways we don't handle properly so just punt. */
20099 complaint (&symfile_complaints,
20100 _("unsupported version in .debug_line section"));
20101 return NULL;
20102 }
20103 if (lh->version >= 5)
20104 {
20105 gdb_byte segment_selector_size;
20106
20107 /* Skip address size. */
20108 read_1_byte (abfd, line_ptr);
20109 line_ptr += 1;
20110
20111 segment_selector_size = read_1_byte (abfd, line_ptr);
20112 line_ptr += 1;
20113 if (segment_selector_size != 0)
20114 {
20115 complaint (&symfile_complaints,
20116 _("unsupported segment selector size %u "
20117 "in .debug_line section"),
20118 segment_selector_size);
20119 return NULL;
20120 }
20121 }
20122 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20123 line_ptr += offset_size;
20124 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20125 line_ptr += 1;
20126 if (lh->version >= 4)
20127 {
20128 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20129 line_ptr += 1;
20130 }
20131 else
20132 lh->maximum_ops_per_instruction = 1;
20133
20134 if (lh->maximum_ops_per_instruction == 0)
20135 {
20136 lh->maximum_ops_per_instruction = 1;
20137 complaint (&symfile_complaints,
20138 _("invalid maximum_ops_per_instruction "
20139 "in `.debug_line' section"));
20140 }
20141
20142 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20143 line_ptr += 1;
20144 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20145 line_ptr += 1;
20146 lh->line_range = read_1_byte (abfd, line_ptr);
20147 line_ptr += 1;
20148 lh->opcode_base = read_1_byte (abfd, line_ptr);
20149 line_ptr += 1;
20150 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20151
20152 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20153 for (i = 1; i < lh->opcode_base; ++i)
20154 {
20155 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20156 line_ptr += 1;
20157 }
20158
20159 if (lh->version >= 5)
20160 {
20161 /* Read directory table. */
20162 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20163 &cu->header,
20164 [] (struct line_header *lh, const char *name,
20165 dir_index d_index, unsigned int mod_time,
20166 unsigned int length)
20167 {
20168 lh->add_include_dir (name);
20169 });
20170
20171 /* Read file name table. */
20172 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20173 &cu->header,
20174 [] (struct line_header *lh, const char *name,
20175 dir_index d_index, unsigned int mod_time,
20176 unsigned int length)
20177 {
20178 lh->add_file_name (name, d_index, mod_time, length);
20179 });
20180 }
20181 else
20182 {
20183 /* Read directory table. */
20184 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20185 {
20186 line_ptr += bytes_read;
20187 lh->add_include_dir (cur_dir);
20188 }
20189 line_ptr += bytes_read;
20190
20191 /* Read file name table. */
20192 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20193 {
20194 unsigned int mod_time, length;
20195 dir_index d_index;
20196
20197 line_ptr += bytes_read;
20198 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20199 line_ptr += bytes_read;
20200 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20201 line_ptr += bytes_read;
20202 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20203 line_ptr += bytes_read;
20204
20205 lh->add_file_name (cur_file, d_index, mod_time, length);
20206 }
20207 line_ptr += bytes_read;
20208 }
20209 lh->statement_program_start = line_ptr;
20210
20211 if (line_ptr > (section->buffer + section->size))
20212 complaint (&symfile_complaints,
20213 _("line number info header doesn't "
20214 "fit in `.debug_line' section"));
20215
20216 return lh;
20217 }
20218
20219 /* Subroutine of dwarf_decode_lines to simplify it.
20220 Return the file name of the psymtab for included file FILE_INDEX
20221 in line header LH of PST.
20222 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20223 If space for the result is malloc'd, *NAME_HOLDER will be set.
20224 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20225
20226 static const char *
20227 psymtab_include_file_name (const struct line_header *lh, int file_index,
20228 const struct partial_symtab *pst,
20229 const char *comp_dir,
20230 gdb::unique_xmalloc_ptr<char> *name_holder)
20231 {
20232 const file_entry &fe = lh->file_names[file_index];
20233 const char *include_name = fe.name;
20234 const char *include_name_to_compare = include_name;
20235 const char *pst_filename;
20236 int file_is_pst;
20237
20238 const char *dir_name = fe.include_dir (lh);
20239
20240 gdb::unique_xmalloc_ptr<char> hold_compare;
20241 if (!IS_ABSOLUTE_PATH (include_name)
20242 && (dir_name != NULL || comp_dir != NULL))
20243 {
20244 /* Avoid creating a duplicate psymtab for PST.
20245 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20246 Before we do the comparison, however, we need to account
20247 for DIR_NAME and COMP_DIR.
20248 First prepend dir_name (if non-NULL). If we still don't
20249 have an absolute path prepend comp_dir (if non-NULL).
20250 However, the directory we record in the include-file's
20251 psymtab does not contain COMP_DIR (to match the
20252 corresponding symtab(s)).
20253
20254 Example:
20255
20256 bash$ cd /tmp
20257 bash$ gcc -g ./hello.c
20258 include_name = "hello.c"
20259 dir_name = "."
20260 DW_AT_comp_dir = comp_dir = "/tmp"
20261 DW_AT_name = "./hello.c"
20262
20263 */
20264
20265 if (dir_name != NULL)
20266 {
20267 name_holder->reset (concat (dir_name, SLASH_STRING,
20268 include_name, (char *) NULL));
20269 include_name = name_holder->get ();
20270 include_name_to_compare = include_name;
20271 }
20272 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20273 {
20274 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20275 include_name, (char *) NULL));
20276 include_name_to_compare = hold_compare.get ();
20277 }
20278 }
20279
20280 pst_filename = pst->filename;
20281 gdb::unique_xmalloc_ptr<char> copied_name;
20282 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20283 {
20284 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20285 pst_filename, (char *) NULL));
20286 pst_filename = copied_name.get ();
20287 }
20288
20289 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20290
20291 if (file_is_pst)
20292 return NULL;
20293 return include_name;
20294 }
20295
20296 /* State machine to track the state of the line number program. */
20297
20298 class lnp_state_machine
20299 {
20300 public:
20301 /* Initialize a machine state for the start of a line number
20302 program. */
20303 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20304
20305 file_entry *current_file ()
20306 {
20307 /* lh->file_names is 0-based, but the file name numbers in the
20308 statement program are 1-based. */
20309 return m_line_header->file_name_at (m_file);
20310 }
20311
20312 /* Record the line in the state machine. END_SEQUENCE is true if
20313 we're processing the end of a sequence. */
20314 void record_line (bool end_sequence);
20315
20316 /* Check address and if invalid nop-out the rest of the lines in this
20317 sequence. */
20318 void check_line_address (struct dwarf2_cu *cu,
20319 const gdb_byte *line_ptr,
20320 CORE_ADDR lowpc, CORE_ADDR address);
20321
20322 void handle_set_discriminator (unsigned int discriminator)
20323 {
20324 m_discriminator = discriminator;
20325 m_line_has_non_zero_discriminator |= discriminator != 0;
20326 }
20327
20328 /* Handle DW_LNE_set_address. */
20329 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20330 {
20331 m_op_index = 0;
20332 address += baseaddr;
20333 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20334 }
20335
20336 /* Handle DW_LNS_advance_pc. */
20337 void handle_advance_pc (CORE_ADDR adjust);
20338
20339 /* Handle a special opcode. */
20340 void handle_special_opcode (unsigned char op_code);
20341
20342 /* Handle DW_LNS_advance_line. */
20343 void handle_advance_line (int line_delta)
20344 {
20345 advance_line (line_delta);
20346 }
20347
20348 /* Handle DW_LNS_set_file. */
20349 void handle_set_file (file_name_index file);
20350
20351 /* Handle DW_LNS_negate_stmt. */
20352 void handle_negate_stmt ()
20353 {
20354 m_is_stmt = !m_is_stmt;
20355 }
20356
20357 /* Handle DW_LNS_const_add_pc. */
20358 void handle_const_add_pc ();
20359
20360 /* Handle DW_LNS_fixed_advance_pc. */
20361 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20362 {
20363 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20364 m_op_index = 0;
20365 }
20366
20367 /* Handle DW_LNS_copy. */
20368 void handle_copy ()
20369 {
20370 record_line (false);
20371 m_discriminator = 0;
20372 }
20373
20374 /* Handle DW_LNE_end_sequence. */
20375 void handle_end_sequence ()
20376 {
20377 m_record_line_callback = ::record_line;
20378 }
20379
20380 private:
20381 /* Advance the line by LINE_DELTA. */
20382 void advance_line (int line_delta)
20383 {
20384 m_line += line_delta;
20385
20386 if (line_delta != 0)
20387 m_line_has_non_zero_discriminator = m_discriminator != 0;
20388 }
20389
20390 gdbarch *m_gdbarch;
20391
20392 /* True if we're recording lines.
20393 Otherwise we're building partial symtabs and are just interested in
20394 finding include files mentioned by the line number program. */
20395 bool m_record_lines_p;
20396
20397 /* The line number header. */
20398 line_header *m_line_header;
20399
20400 /* These are part of the standard DWARF line number state machine,
20401 and initialized according to the DWARF spec. */
20402
20403 unsigned char m_op_index = 0;
20404 /* The line table index (1-based) of the current file. */
20405 file_name_index m_file = (file_name_index) 1;
20406 unsigned int m_line = 1;
20407
20408 /* These are initialized in the constructor. */
20409
20410 CORE_ADDR m_address;
20411 bool m_is_stmt;
20412 unsigned int m_discriminator;
20413
20414 /* Additional bits of state we need to track. */
20415
20416 /* The last file that we called dwarf2_start_subfile for.
20417 This is only used for TLLs. */
20418 unsigned int m_last_file = 0;
20419 /* The last file a line number was recorded for. */
20420 struct subfile *m_last_subfile = NULL;
20421
20422 /* The function to call to record a line. */
20423 record_line_ftype *m_record_line_callback = NULL;
20424
20425 /* The last line number that was recorded, used to coalesce
20426 consecutive entries for the same line. This can happen, for
20427 example, when discriminators are present. PR 17276. */
20428 unsigned int m_last_line = 0;
20429 bool m_line_has_non_zero_discriminator = false;
20430 };
20431
20432 void
20433 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20434 {
20435 CORE_ADDR addr_adj = (((m_op_index + adjust)
20436 / m_line_header->maximum_ops_per_instruction)
20437 * m_line_header->minimum_instruction_length);
20438 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20439 m_op_index = ((m_op_index + adjust)
20440 % m_line_header->maximum_ops_per_instruction);
20441 }
20442
20443 void
20444 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20445 {
20446 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20447 CORE_ADDR addr_adj = (((m_op_index
20448 + (adj_opcode / m_line_header->line_range))
20449 / m_line_header->maximum_ops_per_instruction)
20450 * m_line_header->minimum_instruction_length);
20451 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20452 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20453 % m_line_header->maximum_ops_per_instruction);
20454
20455 int line_delta = (m_line_header->line_base
20456 + (adj_opcode % m_line_header->line_range));
20457 advance_line (line_delta);
20458 record_line (false);
20459 m_discriminator = 0;
20460 }
20461
20462 void
20463 lnp_state_machine::handle_set_file (file_name_index file)
20464 {
20465 m_file = file;
20466
20467 const file_entry *fe = current_file ();
20468 if (fe == NULL)
20469 dwarf2_debug_line_missing_file_complaint ();
20470 else if (m_record_lines_p)
20471 {
20472 const char *dir = fe->include_dir (m_line_header);
20473
20474 m_last_subfile = current_subfile;
20475 m_line_has_non_zero_discriminator = m_discriminator != 0;
20476 dwarf2_start_subfile (fe->name, dir);
20477 }
20478 }
20479
20480 void
20481 lnp_state_machine::handle_const_add_pc ()
20482 {
20483 CORE_ADDR adjust
20484 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20485
20486 CORE_ADDR addr_adj
20487 = (((m_op_index + adjust)
20488 / m_line_header->maximum_ops_per_instruction)
20489 * m_line_header->minimum_instruction_length);
20490
20491 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20492 m_op_index = ((m_op_index + adjust)
20493 % m_line_header->maximum_ops_per_instruction);
20494 }
20495
20496 /* Ignore this record_line request. */
20497
20498 static void
20499 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20500 {
20501 return;
20502 }
20503
20504 /* Return non-zero if we should add LINE to the line number table.
20505 LINE is the line to add, LAST_LINE is the last line that was added,
20506 LAST_SUBFILE is the subfile for LAST_LINE.
20507 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20508 had a non-zero discriminator.
20509
20510 We have to be careful in the presence of discriminators.
20511 E.g., for this line:
20512
20513 for (i = 0; i < 100000; i++);
20514
20515 clang can emit four line number entries for that one line,
20516 each with a different discriminator.
20517 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20518
20519 However, we want gdb to coalesce all four entries into one.
20520 Otherwise the user could stepi into the middle of the line and
20521 gdb would get confused about whether the pc really was in the
20522 middle of the line.
20523
20524 Things are further complicated by the fact that two consecutive
20525 line number entries for the same line is a heuristic used by gcc
20526 to denote the end of the prologue. So we can't just discard duplicate
20527 entries, we have to be selective about it. The heuristic we use is
20528 that we only collapse consecutive entries for the same line if at least
20529 one of those entries has a non-zero discriminator. PR 17276.
20530
20531 Note: Addresses in the line number state machine can never go backwards
20532 within one sequence, thus this coalescing is ok. */
20533
20534 static int
20535 dwarf_record_line_p (unsigned int line, unsigned int last_line,
20536 int line_has_non_zero_discriminator,
20537 struct subfile *last_subfile)
20538 {
20539 if (current_subfile != last_subfile)
20540 return 1;
20541 if (line != last_line)
20542 return 1;
20543 /* Same line for the same file that we've seen already.
20544 As a last check, for pr 17276, only record the line if the line
20545 has never had a non-zero discriminator. */
20546 if (!line_has_non_zero_discriminator)
20547 return 1;
20548 return 0;
20549 }
20550
20551 /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20552 in the line table of subfile SUBFILE. */
20553
20554 static void
20555 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20556 unsigned int line, CORE_ADDR address,
20557 record_line_ftype p_record_line)
20558 {
20559 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20560
20561 if (dwarf_line_debug)
20562 {
20563 fprintf_unfiltered (gdb_stdlog,
20564 "Recording line %u, file %s, address %s\n",
20565 line, lbasename (subfile->name),
20566 paddress (gdbarch, address));
20567 }
20568
20569 (*p_record_line) (subfile, line, addr);
20570 }
20571
20572 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20573 Mark the end of a set of line number records.
20574 The arguments are the same as for dwarf_record_line_1.
20575 If SUBFILE is NULL the request is ignored. */
20576
20577 static void
20578 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20579 CORE_ADDR address, record_line_ftype p_record_line)
20580 {
20581 if (subfile == NULL)
20582 return;
20583
20584 if (dwarf_line_debug)
20585 {
20586 fprintf_unfiltered (gdb_stdlog,
20587 "Finishing current line, file %s, address %s\n",
20588 lbasename (subfile->name),
20589 paddress (gdbarch, address));
20590 }
20591
20592 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20593 }
20594
20595 void
20596 lnp_state_machine::record_line (bool end_sequence)
20597 {
20598 if (dwarf_line_debug)
20599 {
20600 fprintf_unfiltered (gdb_stdlog,
20601 "Processing actual line %u: file %u,"
20602 " address %s, is_stmt %u, discrim %u\n",
20603 m_line, to_underlying (m_file),
20604 paddress (m_gdbarch, m_address),
20605 m_is_stmt, m_discriminator);
20606 }
20607
20608 file_entry *fe = current_file ();
20609
20610 if (fe == NULL)
20611 dwarf2_debug_line_missing_file_complaint ();
20612 /* For now we ignore lines not starting on an instruction boundary.
20613 But not when processing end_sequence for compatibility with the
20614 previous version of the code. */
20615 else if (m_op_index == 0 || end_sequence)
20616 {
20617 fe->included_p = 1;
20618 if (m_record_lines_p && m_is_stmt)
20619 {
20620 if (m_last_subfile != current_subfile || end_sequence)
20621 {
20622 dwarf_finish_line (m_gdbarch, m_last_subfile,
20623 m_address, m_record_line_callback);
20624 }
20625
20626 if (!end_sequence)
20627 {
20628 if (dwarf_record_line_p (m_line, m_last_line,
20629 m_line_has_non_zero_discriminator,
20630 m_last_subfile))
20631 {
20632 dwarf_record_line_1 (m_gdbarch, current_subfile,
20633 m_line, m_address,
20634 m_record_line_callback);
20635 }
20636 m_last_subfile = current_subfile;
20637 m_last_line = m_line;
20638 }
20639 }
20640 }
20641 }
20642
20643 lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20644 bool record_lines_p)
20645 {
20646 m_gdbarch = arch;
20647 m_record_lines_p = record_lines_p;
20648 m_line_header = lh;
20649
20650 m_record_line_callback = ::record_line;
20651
20652 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20653 was a line entry for it so that the backend has a chance to adjust it
20654 and also record it in case it needs it. This is currently used by MIPS
20655 code, cf. `mips_adjust_dwarf2_line'. */
20656 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20657 m_is_stmt = lh->default_is_stmt;
20658 m_discriminator = 0;
20659 }
20660
20661 void
20662 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20663 const gdb_byte *line_ptr,
20664 CORE_ADDR lowpc, CORE_ADDR address)
20665 {
20666 /* If address < lowpc then it's not a usable value, it's outside the
20667 pc range of the CU. However, we restrict the test to only address
20668 values of zero to preserve GDB's previous behaviour which is to
20669 handle the specific case of a function being GC'd by the linker. */
20670
20671 if (address == 0 && address < lowpc)
20672 {
20673 /* This line table is for a function which has been
20674 GCd by the linker. Ignore it. PR gdb/12528 */
20675
20676 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20677 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20678
20679 complaint (&symfile_complaints,
20680 _(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20681 line_offset, objfile_name (objfile));
20682 m_record_line_callback = noop_record_line;
20683 /* Note: record_line_callback is left as noop_record_line until
20684 we see DW_LNE_end_sequence. */
20685 }
20686 }
20687
20688 /* Subroutine of dwarf_decode_lines to simplify it.
20689 Process the line number information in LH.
20690 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20691 program in order to set included_p for every referenced header. */
20692
20693 static void
20694 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20695 const int decode_for_pst_p, CORE_ADDR lowpc)
20696 {
20697 const gdb_byte *line_ptr, *extended_end;
20698 const gdb_byte *line_end;
20699 unsigned int bytes_read, extended_len;
20700 unsigned char op_code, extended_op;
20701 CORE_ADDR baseaddr;
20702 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20703 bfd *abfd = objfile->obfd;
20704 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20705 /* True if we're recording line info (as opposed to building partial
20706 symtabs and just interested in finding include files mentioned by
20707 the line number program). */
20708 bool record_lines_p = !decode_for_pst_p;
20709
20710 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20711
20712 line_ptr = lh->statement_program_start;
20713 line_end = lh->statement_program_end;
20714
20715 /* Read the statement sequences until there's nothing left. */
20716 while (line_ptr < line_end)
20717 {
20718 /* The DWARF line number program state machine. Reset the state
20719 machine at the start of each sequence. */
20720 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20721 bool end_sequence = false;
20722
20723 if (record_lines_p)
20724 {
20725 /* Start a subfile for the current file of the state
20726 machine. */
20727 const file_entry *fe = state_machine.current_file ();
20728
20729 if (fe != NULL)
20730 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
20731 }
20732
20733 /* Decode the table. */
20734 while (line_ptr < line_end && !end_sequence)
20735 {
20736 op_code = read_1_byte (abfd, line_ptr);
20737 line_ptr += 1;
20738
20739 if (op_code >= lh->opcode_base)
20740 {
20741 /* Special opcode. */
20742 state_machine.handle_special_opcode (op_code);
20743 }
20744 else switch (op_code)
20745 {
20746 case DW_LNS_extended_op:
20747 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20748 &bytes_read);
20749 line_ptr += bytes_read;
20750 extended_end = line_ptr + extended_len;
20751 extended_op = read_1_byte (abfd, line_ptr);
20752 line_ptr += 1;
20753 switch (extended_op)
20754 {
20755 case DW_LNE_end_sequence:
20756 state_machine.handle_end_sequence ();
20757 end_sequence = true;
20758 break;
20759 case DW_LNE_set_address:
20760 {
20761 CORE_ADDR address
20762 = read_address (abfd, line_ptr, cu, &bytes_read);
20763 line_ptr += bytes_read;
20764
20765 state_machine.check_line_address (cu, line_ptr,
20766 lowpc, address);
20767 state_machine.handle_set_address (baseaddr, address);
20768 }
20769 break;
20770 case DW_LNE_define_file:
20771 {
20772 const char *cur_file;
20773 unsigned int mod_time, length;
20774 dir_index dindex;
20775
20776 cur_file = read_direct_string (abfd, line_ptr,
20777 &bytes_read);
20778 line_ptr += bytes_read;
20779 dindex = (dir_index)
20780 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20781 line_ptr += bytes_read;
20782 mod_time =
20783 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20784 line_ptr += bytes_read;
20785 length =
20786 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20787 line_ptr += bytes_read;
20788 lh->add_file_name (cur_file, dindex, mod_time, length);
20789 }
20790 break;
20791 case DW_LNE_set_discriminator:
20792 {
20793 /* The discriminator is not interesting to the
20794 debugger; just ignore it. We still need to
20795 check its value though:
20796 if there are consecutive entries for the same
20797 (non-prologue) line we want to coalesce them.
20798 PR 17276. */
20799 unsigned int discr
20800 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20801 line_ptr += bytes_read;
20802
20803 state_machine.handle_set_discriminator (discr);
20804 }
20805 break;
20806 default:
20807 complaint (&symfile_complaints,
20808 _("mangled .debug_line section"));
20809 return;
20810 }
20811 /* Make sure that we parsed the extended op correctly. If e.g.
20812 we expected a different address size than the producer used,
20813 we may have read the wrong number of bytes. */
20814 if (line_ptr != extended_end)
20815 {
20816 complaint (&symfile_complaints,
20817 _("mangled .debug_line section"));
20818 return;
20819 }
20820 break;
20821 case DW_LNS_copy:
20822 state_machine.handle_copy ();
20823 break;
20824 case DW_LNS_advance_pc:
20825 {
20826 CORE_ADDR adjust
20827 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20828 line_ptr += bytes_read;
20829
20830 state_machine.handle_advance_pc (adjust);
20831 }
20832 break;
20833 case DW_LNS_advance_line:
20834 {
20835 int line_delta
20836 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20837 line_ptr += bytes_read;
20838
20839 state_machine.handle_advance_line (line_delta);
20840 }
20841 break;
20842 case DW_LNS_set_file:
20843 {
20844 file_name_index file
20845 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20846 &bytes_read);
20847 line_ptr += bytes_read;
20848
20849 state_machine.handle_set_file (file);
20850 }
20851 break;
20852 case DW_LNS_set_column:
20853 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20854 line_ptr += bytes_read;
20855 break;
20856 case DW_LNS_negate_stmt:
20857 state_machine.handle_negate_stmt ();
20858 break;
20859 case DW_LNS_set_basic_block:
20860 break;
20861 /* Add to the address register of the state machine the
20862 address increment value corresponding to special opcode
20863 255. I.e., this value is scaled by the minimum
20864 instruction length since special opcode 255 would have
20865 scaled the increment. */
20866 case DW_LNS_const_add_pc:
20867 state_machine.handle_const_add_pc ();
20868 break;
20869 case DW_LNS_fixed_advance_pc:
20870 {
20871 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20872 line_ptr += 2;
20873
20874 state_machine.handle_fixed_advance_pc (addr_adj);
20875 }
20876 break;
20877 default:
20878 {
20879 /* Unknown standard opcode, ignore it. */
20880 int i;
20881
20882 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20883 {
20884 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20885 line_ptr += bytes_read;
20886 }
20887 }
20888 }
20889 }
20890
20891 if (!end_sequence)
20892 dwarf2_debug_line_missing_end_sequence_complaint ();
20893
20894 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20895 in which case we still finish recording the last line). */
20896 state_machine.record_line (true);
20897 }
20898 }
20899
20900 /* Decode the Line Number Program (LNP) for the given line_header
20901 structure and CU. The actual information extracted and the type
20902 of structures created from the LNP depends on the value of PST.
20903
20904 1. If PST is NULL, then this procedure uses the data from the program
20905 to create all necessary symbol tables, and their linetables.
20906
20907 2. If PST is not NULL, this procedure reads the program to determine
20908 the list of files included by the unit represented by PST, and
20909 builds all the associated partial symbol tables.
20910
20911 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20912 It is used for relative paths in the line table.
20913 NOTE: When processing partial symtabs (pst != NULL),
20914 comp_dir == pst->dirname.
20915
20916 NOTE: It is important that psymtabs have the same file name (via strcmp)
20917 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20918 symtab we don't use it in the name of the psymtabs we create.
20919 E.g. expand_line_sal requires this when finding psymtabs to expand.
20920 A good testcase for this is mb-inline.exp.
20921
20922 LOWPC is the lowest address in CU (or 0 if not known).
20923
20924 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20925 for its PC<->lines mapping information. Otherwise only the filename
20926 table is read in. */
20927
20928 static void
20929 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20930 struct dwarf2_cu *cu, struct partial_symtab *pst,
20931 CORE_ADDR lowpc, int decode_mapping)
20932 {
20933 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20934 const int decode_for_pst_p = (pst != NULL);
20935
20936 if (decode_mapping)
20937 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20938
20939 if (decode_for_pst_p)
20940 {
20941 int file_index;
20942
20943 /* Now that we're done scanning the Line Header Program, we can
20944 create the psymtab of each included file. */
20945 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
20946 if (lh->file_names[file_index].included_p == 1)
20947 {
20948 gdb::unique_xmalloc_ptr<char> name_holder;
20949 const char *include_name =
20950 psymtab_include_file_name (lh, file_index, pst, comp_dir,
20951 &name_holder);
20952 if (include_name != NULL)
20953 dwarf2_create_include_psymtab (include_name, pst, objfile);
20954 }
20955 }
20956 else
20957 {
20958 /* Make sure a symtab is created for every file, even files
20959 which contain only variables (i.e. no code with associated
20960 line numbers). */
20961 struct compunit_symtab *cust = buildsym_compunit_symtab ();
20962 int i;
20963
20964 for (i = 0; i < lh->file_names.size (); i++)
20965 {
20966 file_entry &fe = lh->file_names[i];
20967
20968 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
20969
20970 if (current_subfile->symtab == NULL)
20971 {
20972 current_subfile->symtab
20973 = allocate_symtab (cust, current_subfile->name);
20974 }
20975 fe.symtab = current_subfile->symtab;
20976 }
20977 }
20978 }
20979
20980 /* Start a subfile for DWARF. FILENAME is the name of the file and
20981 DIRNAME the name of the source directory which contains FILENAME
20982 or NULL if not known.
20983 This routine tries to keep line numbers from identical absolute and
20984 relative file names in a common subfile.
20985
20986 Using the `list' example from the GDB testsuite, which resides in
20987 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20988 of /srcdir/list0.c yields the following debugging information for list0.c:
20989
20990 DW_AT_name: /srcdir/list0.c
20991 DW_AT_comp_dir: /compdir
20992 files.files[0].name: list0.h
20993 files.files[0].dir: /srcdir
20994 files.files[1].name: list0.c
20995 files.files[1].dir: /srcdir
20996
20997 The line number information for list0.c has to end up in a single
20998 subfile, so that `break /srcdir/list0.c:1' works as expected.
20999 start_subfile will ensure that this happens provided that we pass the
21000 concatenation of files.files[1].dir and files.files[1].name as the
21001 subfile's name. */
21002
21003 static void
21004 dwarf2_start_subfile (const char *filename, const char *dirname)
21005 {
21006 char *copy = NULL;
21007
21008 /* In order not to lose the line information directory,
21009 we concatenate it to the filename when it makes sense.
21010 Note that the Dwarf3 standard says (speaking of filenames in line
21011 information): ``The directory index is ignored for file names
21012 that represent full path names''. Thus ignoring dirname in the
21013 `else' branch below isn't an issue. */
21014
21015 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21016 {
21017 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21018 filename = copy;
21019 }
21020
21021 start_subfile (filename);
21022
21023 if (copy != NULL)
21024 xfree (copy);
21025 }
21026
21027 /* Start a symtab for DWARF.
21028 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21029
21030 static struct compunit_symtab *
21031 dwarf2_start_symtab (struct dwarf2_cu *cu,
21032 const char *name, const char *comp_dir, CORE_ADDR low_pc)
21033 {
21034 struct compunit_symtab *cust
21035 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21036 low_pc, cu->language);
21037
21038 record_debugformat ("DWARF 2");
21039 record_producer (cu->producer);
21040
21041 /* We assume that we're processing GCC output. */
21042 processing_gcc_compilation = 2;
21043
21044 cu->processing_has_namespace_info = 0;
21045
21046 return cust;
21047 }
21048
21049 static void
21050 var_decode_location (struct attribute *attr, struct symbol *sym,
21051 struct dwarf2_cu *cu)
21052 {
21053 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21054 struct comp_unit_head *cu_header = &cu->header;
21055
21056 /* NOTE drow/2003-01-30: There used to be a comment and some special
21057 code here to turn a symbol with DW_AT_external and a
21058 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21059 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21060 with some versions of binutils) where shared libraries could have
21061 relocations against symbols in their debug information - the
21062 minimal symbol would have the right address, but the debug info
21063 would not. It's no longer necessary, because we will explicitly
21064 apply relocations when we read in the debug information now. */
21065
21066 /* A DW_AT_location attribute with no contents indicates that a
21067 variable has been optimized away. */
21068 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21069 {
21070 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21071 return;
21072 }
21073
21074 /* Handle one degenerate form of location expression specially, to
21075 preserve GDB's previous behavior when section offsets are
21076 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21077 then mark this symbol as LOC_STATIC. */
21078
21079 if (attr_form_is_block (attr)
21080 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21081 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21082 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21083 && (DW_BLOCK (attr)->size
21084 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21085 {
21086 unsigned int dummy;
21087
21088 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21089 SYMBOL_VALUE_ADDRESS (sym) =
21090 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21091 else
21092 SYMBOL_VALUE_ADDRESS (sym) =
21093 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
21094 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21095 fixup_symbol_section (sym, objfile);
21096 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21097 SYMBOL_SECTION (sym));
21098 return;
21099 }
21100
21101 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21102 expression evaluator, and use LOC_COMPUTED only when necessary
21103 (i.e. when the value of a register or memory location is
21104 referenced, or a thread-local block, etc.). Then again, it might
21105 not be worthwhile. I'm assuming that it isn't unless performance
21106 or memory numbers show me otherwise. */
21107
21108 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21109
21110 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21111 cu->has_loclist = 1;
21112 }
21113
21114 /* Given a pointer to a DWARF information entry, figure out if we need
21115 to make a symbol table entry for it, and if so, create a new entry
21116 and return a pointer to it.
21117 If TYPE is NULL, determine symbol type from the die, otherwise
21118 used the passed type.
21119 If SPACE is not NULL, use it to hold the new symbol. If it is
21120 NULL, allocate a new symbol on the objfile's obstack. */
21121
21122 static struct symbol *
21123 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21124 struct symbol *space)
21125 {
21126 struct dwarf2_per_objfile *dwarf2_per_objfile
21127 = cu->per_cu->dwarf2_per_objfile;
21128 struct objfile *objfile = dwarf2_per_objfile->objfile;
21129 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21130 struct symbol *sym = NULL;
21131 const char *name;
21132 struct attribute *attr = NULL;
21133 struct attribute *attr2 = NULL;
21134 CORE_ADDR baseaddr;
21135 struct pending **list_to_add = NULL;
21136
21137 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21138
21139 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21140
21141 name = dwarf2_name (die, cu);
21142 if (name)
21143 {
21144 const char *linkagename;
21145 int suppress_add = 0;
21146
21147 if (space)
21148 sym = space;
21149 else
21150 sym = allocate_symbol (objfile);
21151 OBJSTAT (objfile, n_syms++);
21152
21153 /* Cache this symbol's name and the name's demangled form (if any). */
21154 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21155 linkagename = dwarf2_physname (name, die, cu);
21156 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21157
21158 /* Fortran does not have mangling standard and the mangling does differ
21159 between gfortran, iFort etc. */
21160 if (cu->language == language_fortran
21161 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21162 symbol_set_demangled_name (&(sym->ginfo),
21163 dwarf2_full_name (name, die, cu),
21164 NULL);
21165
21166 /* Default assumptions.
21167 Use the passed type or decode it from the die. */
21168 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21169 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21170 if (type != NULL)
21171 SYMBOL_TYPE (sym) = type;
21172 else
21173 SYMBOL_TYPE (sym) = die_type (die, cu);
21174 attr = dwarf2_attr (die,
21175 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21176 cu);
21177 if (attr)
21178 {
21179 SYMBOL_LINE (sym) = DW_UNSND (attr);
21180 }
21181
21182 attr = dwarf2_attr (die,
21183 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21184 cu);
21185 if (attr)
21186 {
21187 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21188 struct file_entry *fe;
21189
21190 if (cu->line_header != NULL)
21191 fe = cu->line_header->file_name_at (file_index);
21192 else
21193 fe = NULL;
21194
21195 if (fe == NULL)
21196 complaint (&symfile_complaints,
21197 _("file index out of range"));
21198 else
21199 symbol_set_symtab (sym, fe->symtab);
21200 }
21201
21202 switch (die->tag)
21203 {
21204 case DW_TAG_label:
21205 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21206 if (attr)
21207 {
21208 CORE_ADDR addr;
21209
21210 addr = attr_value_as_address (attr);
21211 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21212 SYMBOL_VALUE_ADDRESS (sym) = addr;
21213 }
21214 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21215 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21216 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21217 add_symbol_to_list (sym, cu->list_in_scope);
21218 break;
21219 case DW_TAG_subprogram:
21220 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21221 finish_block. */
21222 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21223 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21224 if ((attr2 && (DW_UNSND (attr2) != 0))
21225 || cu->language == language_ada)
21226 {
21227 /* Subprograms marked external are stored as a global symbol.
21228 Ada subprograms, whether marked external or not, are always
21229 stored as a global symbol, because we want to be able to
21230 access them globally. For instance, we want to be able
21231 to break on a nested subprogram without having to
21232 specify the context. */
21233 list_to_add = &global_symbols;
21234 }
21235 else
21236 {
21237 list_to_add = cu->list_in_scope;
21238 }
21239 break;
21240 case DW_TAG_inlined_subroutine:
21241 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21242 finish_block. */
21243 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21244 SYMBOL_INLINED (sym) = 1;
21245 list_to_add = cu->list_in_scope;
21246 break;
21247 case DW_TAG_template_value_param:
21248 suppress_add = 1;
21249 /* Fall through. */
21250 case DW_TAG_constant:
21251 case DW_TAG_variable:
21252 case DW_TAG_member:
21253 /* Compilation with minimal debug info may result in
21254 variables with missing type entries. Change the
21255 misleading `void' type to something sensible. */
21256 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21257 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21258
21259 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21260 /* In the case of DW_TAG_member, we should only be called for
21261 static const members. */
21262 if (die->tag == DW_TAG_member)
21263 {
21264 /* dwarf2_add_field uses die_is_declaration,
21265 so we do the same. */
21266 gdb_assert (die_is_declaration (die, cu));
21267 gdb_assert (attr);
21268 }
21269 if (attr)
21270 {
21271 dwarf2_const_value (attr, sym, cu);
21272 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21273 if (!suppress_add)
21274 {
21275 if (attr2 && (DW_UNSND (attr2) != 0))
21276 list_to_add = &global_symbols;
21277 else
21278 list_to_add = cu->list_in_scope;
21279 }
21280 break;
21281 }
21282 attr = dwarf2_attr (die, DW_AT_location, cu);
21283 if (attr)
21284 {
21285 var_decode_location (attr, sym, cu);
21286 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21287
21288 /* Fortran explicitly imports any global symbols to the local
21289 scope by DW_TAG_common_block. */
21290 if (cu->language == language_fortran && die->parent
21291 && die->parent->tag == DW_TAG_common_block)
21292 attr2 = NULL;
21293
21294 if (SYMBOL_CLASS (sym) == LOC_STATIC
21295 && SYMBOL_VALUE_ADDRESS (sym) == 0
21296 && !dwarf2_per_objfile->has_section_at_zero)
21297 {
21298 /* When a static variable is eliminated by the linker,
21299 the corresponding debug information is not stripped
21300 out, but the variable address is set to null;
21301 do not add such variables into symbol table. */
21302 }
21303 else if (attr2 && (DW_UNSND (attr2) != 0))
21304 {
21305 /* Workaround gfortran PR debug/40040 - it uses
21306 DW_AT_location for variables in -fPIC libraries which may
21307 get overriden by other libraries/executable and get
21308 a different address. Resolve it by the minimal symbol
21309 which may come from inferior's executable using copy
21310 relocation. Make this workaround only for gfortran as for
21311 other compilers GDB cannot guess the minimal symbol
21312 Fortran mangling kind. */
21313 if (cu->language == language_fortran && die->parent
21314 && die->parent->tag == DW_TAG_module
21315 && cu->producer
21316 && startswith (cu->producer, "GNU Fortran"))
21317 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21318
21319 /* A variable with DW_AT_external is never static,
21320 but it may be block-scoped. */
21321 list_to_add = (cu->list_in_scope == &file_symbols
21322 ? &global_symbols : cu->list_in_scope);
21323 }
21324 else
21325 list_to_add = cu->list_in_scope;
21326 }
21327 else
21328 {
21329 /* We do not know the address of this symbol.
21330 If it is an external symbol and we have type information
21331 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21332 The address of the variable will then be determined from
21333 the minimal symbol table whenever the variable is
21334 referenced. */
21335 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21336
21337 /* Fortran explicitly imports any global symbols to the local
21338 scope by DW_TAG_common_block. */
21339 if (cu->language == language_fortran && die->parent
21340 && die->parent->tag == DW_TAG_common_block)
21341 {
21342 /* SYMBOL_CLASS doesn't matter here because
21343 read_common_block is going to reset it. */
21344 if (!suppress_add)
21345 list_to_add = cu->list_in_scope;
21346 }
21347 else if (attr2 && (DW_UNSND (attr2) != 0)
21348 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21349 {
21350 /* A variable with DW_AT_external is never static, but it
21351 may be block-scoped. */
21352 list_to_add = (cu->list_in_scope == &file_symbols
21353 ? &global_symbols : cu->list_in_scope);
21354
21355 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21356 }
21357 else if (!die_is_declaration (die, cu))
21358 {
21359 /* Use the default LOC_OPTIMIZED_OUT class. */
21360 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21361 if (!suppress_add)
21362 list_to_add = cu->list_in_scope;
21363 }
21364 }
21365 break;
21366 case DW_TAG_formal_parameter:
21367 /* If we are inside a function, mark this as an argument. If
21368 not, we might be looking at an argument to an inlined function
21369 when we do not have enough information to show inlined frames;
21370 pretend it's a local variable in that case so that the user can
21371 still see it. */
21372 if (context_stack_depth > 0
21373 && context_stack[context_stack_depth - 1].name != NULL)
21374 SYMBOL_IS_ARGUMENT (sym) = 1;
21375 attr = dwarf2_attr (die, DW_AT_location, cu);
21376 if (attr)
21377 {
21378 var_decode_location (attr, sym, cu);
21379 }
21380 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21381 if (attr)
21382 {
21383 dwarf2_const_value (attr, sym, cu);
21384 }
21385
21386 list_to_add = cu->list_in_scope;
21387 break;
21388 case DW_TAG_unspecified_parameters:
21389 /* From varargs functions; gdb doesn't seem to have any
21390 interest in this information, so just ignore it for now.
21391 (FIXME?) */
21392 break;
21393 case DW_TAG_template_type_param:
21394 suppress_add = 1;
21395 /* Fall through. */
21396 case DW_TAG_class_type:
21397 case DW_TAG_interface_type:
21398 case DW_TAG_structure_type:
21399 case DW_TAG_union_type:
21400 case DW_TAG_set_type:
21401 case DW_TAG_enumeration_type:
21402 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21403 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21404
21405 {
21406 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21407 really ever be static objects: otherwise, if you try
21408 to, say, break of a class's method and you're in a file
21409 which doesn't mention that class, it won't work unless
21410 the check for all static symbols in lookup_symbol_aux
21411 saves you. See the OtherFileClass tests in
21412 gdb.c++/namespace.exp. */
21413
21414 if (!suppress_add)
21415 {
21416 list_to_add = (cu->list_in_scope == &file_symbols
21417 && cu->language == language_cplus
21418 ? &global_symbols : cu->list_in_scope);
21419
21420 /* The semantics of C++ state that "struct foo {
21421 ... }" also defines a typedef for "foo". */
21422 if (cu->language == language_cplus
21423 || cu->language == language_ada
21424 || cu->language == language_d
21425 || cu->language == language_rust)
21426 {
21427 /* The symbol's name is already allocated along
21428 with this objfile, so we don't need to
21429 duplicate it for the type. */
21430 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21431 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21432 }
21433 }
21434 }
21435 break;
21436 case DW_TAG_typedef:
21437 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21438 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21439 list_to_add = cu->list_in_scope;
21440 break;
21441 case DW_TAG_base_type:
21442 case DW_TAG_subrange_type:
21443 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21444 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21445 list_to_add = cu->list_in_scope;
21446 break;
21447 case DW_TAG_enumerator:
21448 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21449 if (attr)
21450 {
21451 dwarf2_const_value (attr, sym, cu);
21452 }
21453 {
21454 /* NOTE: carlton/2003-11-10: See comment above in the
21455 DW_TAG_class_type, etc. block. */
21456
21457 list_to_add = (cu->list_in_scope == &file_symbols
21458 && cu->language == language_cplus
21459 ? &global_symbols : cu->list_in_scope);
21460 }
21461 break;
21462 case DW_TAG_imported_declaration:
21463 case DW_TAG_namespace:
21464 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21465 list_to_add = &global_symbols;
21466 break;
21467 case DW_TAG_module:
21468 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21469 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21470 list_to_add = &global_symbols;
21471 break;
21472 case DW_TAG_common_block:
21473 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21474 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21475 add_symbol_to_list (sym, cu->list_in_scope);
21476 break;
21477 default:
21478 /* Not a tag we recognize. Hopefully we aren't processing
21479 trash data, but since we must specifically ignore things
21480 we don't recognize, there is nothing else we should do at
21481 this point. */
21482 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
21483 dwarf_tag_name (die->tag));
21484 break;
21485 }
21486
21487 if (suppress_add)
21488 {
21489 sym->hash_next = objfile->template_symbols;
21490 objfile->template_symbols = sym;
21491 list_to_add = NULL;
21492 }
21493
21494 if (list_to_add != NULL)
21495 add_symbol_to_list (sym, list_to_add);
21496
21497 /* For the benefit of old versions of GCC, check for anonymous
21498 namespaces based on the demangled name. */
21499 if (!cu->processing_has_namespace_info
21500 && cu->language == language_cplus)
21501 cp_scan_for_anonymous_namespaces (sym, objfile);
21502 }
21503 return (sym);
21504 }
21505
21506 /* Given an attr with a DW_FORM_dataN value in host byte order,
21507 zero-extend it as appropriate for the symbol's type. The DWARF
21508 standard (v4) is not entirely clear about the meaning of using
21509 DW_FORM_dataN for a constant with a signed type, where the type is
21510 wider than the data. The conclusion of a discussion on the DWARF
21511 list was that this is unspecified. We choose to always zero-extend
21512 because that is the interpretation long in use by GCC. */
21513
21514 static gdb_byte *
21515 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21516 struct dwarf2_cu *cu, LONGEST *value, int bits)
21517 {
21518 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21519 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21520 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21521 LONGEST l = DW_UNSND (attr);
21522
21523 if (bits < sizeof (*value) * 8)
21524 {
21525 l &= ((LONGEST) 1 << bits) - 1;
21526 *value = l;
21527 }
21528 else if (bits == sizeof (*value) * 8)
21529 *value = l;
21530 else
21531 {
21532 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21533 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21534 return bytes;
21535 }
21536
21537 return NULL;
21538 }
21539
21540 /* Read a constant value from an attribute. Either set *VALUE, or if
21541 the value does not fit in *VALUE, set *BYTES - either already
21542 allocated on the objfile obstack, or newly allocated on OBSTACK,
21543 or, set *BATON, if we translated the constant to a location
21544 expression. */
21545
21546 static void
21547 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21548 const char *name, struct obstack *obstack,
21549 struct dwarf2_cu *cu,
21550 LONGEST *value, const gdb_byte **bytes,
21551 struct dwarf2_locexpr_baton **baton)
21552 {
21553 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21554 struct comp_unit_head *cu_header = &cu->header;
21555 struct dwarf_block *blk;
21556 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21557 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21558
21559 *value = 0;
21560 *bytes = NULL;
21561 *baton = NULL;
21562
21563 switch (attr->form)
21564 {
21565 case DW_FORM_addr:
21566 case DW_FORM_GNU_addr_index:
21567 {
21568 gdb_byte *data;
21569
21570 if (TYPE_LENGTH (type) != cu_header->addr_size)
21571 dwarf2_const_value_length_mismatch_complaint (name,
21572 cu_header->addr_size,
21573 TYPE_LENGTH (type));
21574 /* Symbols of this form are reasonably rare, so we just
21575 piggyback on the existing location code rather than writing
21576 a new implementation of symbol_computed_ops. */
21577 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21578 (*baton)->per_cu = cu->per_cu;
21579 gdb_assert ((*baton)->per_cu);
21580
21581 (*baton)->size = 2 + cu_header->addr_size;
21582 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21583 (*baton)->data = data;
21584
21585 data[0] = DW_OP_addr;
21586 store_unsigned_integer (&data[1], cu_header->addr_size,
21587 byte_order, DW_ADDR (attr));
21588 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21589 }
21590 break;
21591 case DW_FORM_string:
21592 case DW_FORM_strp:
21593 case DW_FORM_GNU_str_index:
21594 case DW_FORM_GNU_strp_alt:
21595 /* DW_STRING is already allocated on the objfile obstack, point
21596 directly to it. */
21597 *bytes = (const gdb_byte *) DW_STRING (attr);
21598 break;
21599 case DW_FORM_block1:
21600 case DW_FORM_block2:
21601 case DW_FORM_block4:
21602 case DW_FORM_block:
21603 case DW_FORM_exprloc:
21604 case DW_FORM_data16:
21605 blk = DW_BLOCK (attr);
21606 if (TYPE_LENGTH (type) != blk->size)
21607 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21608 TYPE_LENGTH (type));
21609 *bytes = blk->data;
21610 break;
21611
21612 /* The DW_AT_const_value attributes are supposed to carry the
21613 symbol's value "represented as it would be on the target
21614 architecture." By the time we get here, it's already been
21615 converted to host endianness, so we just need to sign- or
21616 zero-extend it as appropriate. */
21617 case DW_FORM_data1:
21618 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21619 break;
21620 case DW_FORM_data2:
21621 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21622 break;
21623 case DW_FORM_data4:
21624 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21625 break;
21626 case DW_FORM_data8:
21627 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21628 break;
21629
21630 case DW_FORM_sdata:
21631 case DW_FORM_implicit_const:
21632 *value = DW_SND (attr);
21633 break;
21634
21635 case DW_FORM_udata:
21636 *value = DW_UNSND (attr);
21637 break;
21638
21639 default:
21640 complaint (&symfile_complaints,
21641 _("unsupported const value attribute form: '%s'"),
21642 dwarf_form_name (attr->form));
21643 *value = 0;
21644 break;
21645 }
21646 }
21647
21648
21649 /* Copy constant value from an attribute to a symbol. */
21650
21651 static void
21652 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21653 struct dwarf2_cu *cu)
21654 {
21655 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21656 LONGEST value;
21657 const gdb_byte *bytes;
21658 struct dwarf2_locexpr_baton *baton;
21659
21660 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21661 SYMBOL_PRINT_NAME (sym),
21662 &objfile->objfile_obstack, cu,
21663 &value, &bytes, &baton);
21664
21665 if (baton != NULL)
21666 {
21667 SYMBOL_LOCATION_BATON (sym) = baton;
21668 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21669 }
21670 else if (bytes != NULL)
21671 {
21672 SYMBOL_VALUE_BYTES (sym) = bytes;
21673 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21674 }
21675 else
21676 {
21677 SYMBOL_VALUE (sym) = value;
21678 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21679 }
21680 }
21681
21682 /* Return the type of the die in question using its DW_AT_type attribute. */
21683
21684 static struct type *
21685 die_type (struct die_info *die, struct dwarf2_cu *cu)
21686 {
21687 struct attribute *type_attr;
21688
21689 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21690 if (!type_attr)
21691 {
21692 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21693 /* A missing DW_AT_type represents a void type. */
21694 return objfile_type (objfile)->builtin_void;
21695 }
21696
21697 return lookup_die_type (die, type_attr, cu);
21698 }
21699
21700 /* True iff CU's producer generates GNAT Ada auxiliary information
21701 that allows to find parallel types through that information instead
21702 of having to do expensive parallel lookups by type name. */
21703
21704 static int
21705 need_gnat_info (struct dwarf2_cu *cu)
21706 {
21707 /* Assume that the Ada compiler was GNAT, which always produces
21708 the auxiliary information. */
21709 return (cu->language == language_ada);
21710 }
21711
21712 /* Return the auxiliary type of the die in question using its
21713 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21714 attribute is not present. */
21715
21716 static struct type *
21717 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21718 {
21719 struct attribute *type_attr;
21720
21721 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21722 if (!type_attr)
21723 return NULL;
21724
21725 return lookup_die_type (die, type_attr, cu);
21726 }
21727
21728 /* If DIE has a descriptive_type attribute, then set the TYPE's
21729 descriptive type accordingly. */
21730
21731 static void
21732 set_descriptive_type (struct type *type, struct die_info *die,
21733 struct dwarf2_cu *cu)
21734 {
21735 struct type *descriptive_type = die_descriptive_type (die, cu);
21736
21737 if (descriptive_type)
21738 {
21739 ALLOCATE_GNAT_AUX_TYPE (type);
21740 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21741 }
21742 }
21743
21744 /* Return the containing type of the die in question using its
21745 DW_AT_containing_type attribute. */
21746
21747 static struct type *
21748 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21749 {
21750 struct attribute *type_attr;
21751 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21752
21753 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21754 if (!type_attr)
21755 error (_("Dwarf Error: Problem turning containing type into gdb type "
21756 "[in module %s]"), objfile_name (objfile));
21757
21758 return lookup_die_type (die, type_attr, cu);
21759 }
21760
21761 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21762
21763 static struct type *
21764 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21765 {
21766 struct dwarf2_per_objfile *dwarf2_per_objfile
21767 = cu->per_cu->dwarf2_per_objfile;
21768 struct objfile *objfile = dwarf2_per_objfile->objfile;
21769 char *message, *saved;
21770
21771 message = xstrprintf (_("<unknown type in %s, CU %s, DIE %s>"),
21772 objfile_name (objfile),
21773 sect_offset_str (cu->header.sect_off),
21774 sect_offset_str (die->sect_off));
21775 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21776 message, strlen (message));
21777 xfree (message);
21778
21779 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21780 }
21781
21782 /* Look up the type of DIE in CU using its type attribute ATTR.
21783 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21784 DW_AT_containing_type.
21785 If there is no type substitute an error marker. */
21786
21787 static struct type *
21788 lookup_die_type (struct die_info *die, const struct attribute *attr,
21789 struct dwarf2_cu *cu)
21790 {
21791 struct dwarf2_per_objfile *dwarf2_per_objfile
21792 = cu->per_cu->dwarf2_per_objfile;
21793 struct objfile *objfile = dwarf2_per_objfile->objfile;
21794 struct type *this_type;
21795
21796 gdb_assert (attr->name == DW_AT_type
21797 || attr->name == DW_AT_GNAT_descriptive_type
21798 || attr->name == DW_AT_containing_type);
21799
21800 /* First see if we have it cached. */
21801
21802 if (attr->form == DW_FORM_GNU_ref_alt)
21803 {
21804 struct dwarf2_per_cu_data *per_cu;
21805 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21806
21807 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21808 dwarf2_per_objfile);
21809 this_type = get_die_type_at_offset (sect_off, per_cu);
21810 }
21811 else if (attr_form_is_ref (attr))
21812 {
21813 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21814
21815 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21816 }
21817 else if (attr->form == DW_FORM_ref_sig8)
21818 {
21819 ULONGEST signature = DW_SIGNATURE (attr);
21820
21821 return get_signatured_type (die, signature, cu);
21822 }
21823 else
21824 {
21825 complaint (&symfile_complaints,
21826 _("Dwarf Error: Bad type attribute %s in DIE"
21827 " at %s [in module %s]"),
21828 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21829 objfile_name (objfile));
21830 return build_error_marker_type (cu, die);
21831 }
21832
21833 /* If not cached we need to read it in. */
21834
21835 if (this_type == NULL)
21836 {
21837 struct die_info *type_die = NULL;
21838 struct dwarf2_cu *type_cu = cu;
21839
21840 if (attr_form_is_ref (attr))
21841 type_die = follow_die_ref (die, attr, &type_cu);
21842 if (type_die == NULL)
21843 return build_error_marker_type (cu, die);
21844 /* If we find the type now, it's probably because the type came
21845 from an inter-CU reference and the type's CU got expanded before
21846 ours. */
21847 this_type = read_type_die (type_die, type_cu);
21848 }
21849
21850 /* If we still don't have a type use an error marker. */
21851
21852 if (this_type == NULL)
21853 return build_error_marker_type (cu, die);
21854
21855 return this_type;
21856 }
21857
21858 /* Return the type in DIE, CU.
21859 Returns NULL for invalid types.
21860
21861 This first does a lookup in die_type_hash,
21862 and only reads the die in if necessary.
21863
21864 NOTE: This can be called when reading in partial or full symbols. */
21865
21866 static struct type *
21867 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21868 {
21869 struct type *this_type;
21870
21871 this_type = get_die_type (die, cu);
21872 if (this_type)
21873 return this_type;
21874
21875 return read_type_die_1 (die, cu);
21876 }
21877
21878 /* Read the type in DIE, CU.
21879 Returns NULL for invalid types. */
21880
21881 static struct type *
21882 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21883 {
21884 struct type *this_type = NULL;
21885
21886 switch (die->tag)
21887 {
21888 case DW_TAG_class_type:
21889 case DW_TAG_interface_type:
21890 case DW_TAG_structure_type:
21891 case DW_TAG_union_type:
21892 this_type = read_structure_type (die, cu);
21893 break;
21894 case DW_TAG_enumeration_type:
21895 this_type = read_enumeration_type (die, cu);
21896 break;
21897 case DW_TAG_subprogram:
21898 case DW_TAG_subroutine_type:
21899 case DW_TAG_inlined_subroutine:
21900 this_type = read_subroutine_type (die, cu);
21901 break;
21902 case DW_TAG_array_type:
21903 this_type = read_array_type (die, cu);
21904 break;
21905 case DW_TAG_set_type:
21906 this_type = read_set_type (die, cu);
21907 break;
21908 case DW_TAG_pointer_type:
21909 this_type = read_tag_pointer_type (die, cu);
21910 break;
21911 case DW_TAG_ptr_to_member_type:
21912 this_type = read_tag_ptr_to_member_type (die, cu);
21913 break;
21914 case DW_TAG_reference_type:
21915 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21916 break;
21917 case DW_TAG_rvalue_reference_type:
21918 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21919 break;
21920 case DW_TAG_const_type:
21921 this_type = read_tag_const_type (die, cu);
21922 break;
21923 case DW_TAG_volatile_type:
21924 this_type = read_tag_volatile_type (die, cu);
21925 break;
21926 case DW_TAG_restrict_type:
21927 this_type = read_tag_restrict_type (die, cu);
21928 break;
21929 case DW_TAG_string_type:
21930 this_type = read_tag_string_type (die, cu);
21931 break;
21932 case DW_TAG_typedef:
21933 this_type = read_typedef (die, cu);
21934 break;
21935 case DW_TAG_subrange_type:
21936 this_type = read_subrange_type (die, cu);
21937 break;
21938 case DW_TAG_base_type:
21939 this_type = read_base_type (die, cu);
21940 break;
21941 case DW_TAG_unspecified_type:
21942 this_type = read_unspecified_type (die, cu);
21943 break;
21944 case DW_TAG_namespace:
21945 this_type = read_namespace_type (die, cu);
21946 break;
21947 case DW_TAG_module:
21948 this_type = read_module_type (die, cu);
21949 break;
21950 case DW_TAG_atomic_type:
21951 this_type = read_tag_atomic_type (die, cu);
21952 break;
21953 default:
21954 complaint (&symfile_complaints,
21955 _("unexpected tag in read_type_die: '%s'"),
21956 dwarf_tag_name (die->tag));
21957 break;
21958 }
21959
21960 return this_type;
21961 }
21962
21963 /* See if we can figure out if the class lives in a namespace. We do
21964 this by looking for a member function; its demangled name will
21965 contain namespace info, if there is any.
21966 Return the computed name or NULL.
21967 Space for the result is allocated on the objfile's obstack.
21968 This is the full-die version of guess_partial_die_structure_name.
21969 In this case we know DIE has no useful parent. */
21970
21971 static char *
21972 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21973 {
21974 struct die_info *spec_die;
21975 struct dwarf2_cu *spec_cu;
21976 struct die_info *child;
21977 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21978
21979 spec_cu = cu;
21980 spec_die = die_specification (die, &spec_cu);
21981 if (spec_die != NULL)
21982 {
21983 die = spec_die;
21984 cu = spec_cu;
21985 }
21986
21987 for (child = die->child;
21988 child != NULL;
21989 child = child->sibling)
21990 {
21991 if (child->tag == DW_TAG_subprogram)
21992 {
21993 const char *linkage_name = dw2_linkage_name (child, cu);
21994
21995 if (linkage_name != NULL)
21996 {
21997 char *actual_name
21998 = language_class_name_from_physname (cu->language_defn,
21999 linkage_name);
22000 char *name = NULL;
22001
22002 if (actual_name != NULL)
22003 {
22004 const char *die_name = dwarf2_name (die, cu);
22005
22006 if (die_name != NULL
22007 && strcmp (die_name, actual_name) != 0)
22008 {
22009 /* Strip off the class name from the full name.
22010 We want the prefix. */
22011 int die_name_len = strlen (die_name);
22012 int actual_name_len = strlen (actual_name);
22013
22014 /* Test for '::' as a sanity check. */
22015 if (actual_name_len > die_name_len + 2
22016 && actual_name[actual_name_len
22017 - die_name_len - 1] == ':')
22018 name = (char *) obstack_copy0 (
22019 &objfile->per_bfd->storage_obstack,
22020 actual_name, actual_name_len - die_name_len - 2);
22021 }
22022 }
22023 xfree (actual_name);
22024 return name;
22025 }
22026 }
22027 }
22028
22029 return NULL;
22030 }
22031
22032 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22033 prefix part in such case. See
22034 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22035
22036 static const char *
22037 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22038 {
22039 struct attribute *attr;
22040 const char *base;
22041
22042 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22043 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22044 return NULL;
22045
22046 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22047 return NULL;
22048
22049 attr = dw2_linkage_name_attr (die, cu);
22050 if (attr == NULL || DW_STRING (attr) == NULL)
22051 return NULL;
22052
22053 /* dwarf2_name had to be already called. */
22054 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22055
22056 /* Strip the base name, keep any leading namespaces/classes. */
22057 base = strrchr (DW_STRING (attr), ':');
22058 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22059 return "";
22060
22061 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22062 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
22063 DW_STRING (attr),
22064 &base[-1] - DW_STRING (attr));
22065 }
22066
22067 /* Return the name of the namespace/class that DIE is defined within,
22068 or "" if we can't tell. The caller should not xfree the result.
22069
22070 For example, if we're within the method foo() in the following
22071 code:
22072
22073 namespace N {
22074 class C {
22075 void foo () {
22076 }
22077 };
22078 }
22079
22080 then determine_prefix on foo's die will return "N::C". */
22081
22082 static const char *
22083 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22084 {
22085 struct dwarf2_per_objfile *dwarf2_per_objfile
22086 = cu->per_cu->dwarf2_per_objfile;
22087 struct die_info *parent, *spec_die;
22088 struct dwarf2_cu *spec_cu;
22089 struct type *parent_type;
22090 const char *retval;
22091
22092 if (cu->language != language_cplus
22093 && cu->language != language_fortran && cu->language != language_d
22094 && cu->language != language_rust)
22095 return "";
22096
22097 retval = anonymous_struct_prefix (die, cu);
22098 if (retval)
22099 return retval;
22100
22101 /* We have to be careful in the presence of DW_AT_specification.
22102 For example, with GCC 3.4, given the code
22103
22104 namespace N {
22105 void foo() {
22106 // Definition of N::foo.
22107 }
22108 }
22109
22110 then we'll have a tree of DIEs like this:
22111
22112 1: DW_TAG_compile_unit
22113 2: DW_TAG_namespace // N
22114 3: DW_TAG_subprogram // declaration of N::foo
22115 4: DW_TAG_subprogram // definition of N::foo
22116 DW_AT_specification // refers to die #3
22117
22118 Thus, when processing die #4, we have to pretend that we're in
22119 the context of its DW_AT_specification, namely the contex of die
22120 #3. */
22121 spec_cu = cu;
22122 spec_die = die_specification (die, &spec_cu);
22123 if (spec_die == NULL)
22124 parent = die->parent;
22125 else
22126 {
22127 parent = spec_die->parent;
22128 cu = spec_cu;
22129 }
22130
22131 if (parent == NULL)
22132 return "";
22133 else if (parent->building_fullname)
22134 {
22135 const char *name;
22136 const char *parent_name;
22137
22138 /* It has been seen on RealView 2.2 built binaries,
22139 DW_TAG_template_type_param types actually _defined_ as
22140 children of the parent class:
22141
22142 enum E {};
22143 template class <class Enum> Class{};
22144 Class<enum E> class_e;
22145
22146 1: DW_TAG_class_type (Class)
22147 2: DW_TAG_enumeration_type (E)
22148 3: DW_TAG_enumerator (enum1:0)
22149 3: DW_TAG_enumerator (enum2:1)
22150 ...
22151 2: DW_TAG_template_type_param
22152 DW_AT_type DW_FORM_ref_udata (E)
22153
22154 Besides being broken debug info, it can put GDB into an
22155 infinite loop. Consider:
22156
22157 When we're building the full name for Class<E>, we'll start
22158 at Class, and go look over its template type parameters,
22159 finding E. We'll then try to build the full name of E, and
22160 reach here. We're now trying to build the full name of E,
22161 and look over the parent DIE for containing scope. In the
22162 broken case, if we followed the parent DIE of E, we'd again
22163 find Class, and once again go look at its template type
22164 arguments, etc., etc. Simply don't consider such parent die
22165 as source-level parent of this die (it can't be, the language
22166 doesn't allow it), and break the loop here. */
22167 name = dwarf2_name (die, cu);
22168 parent_name = dwarf2_name (parent, cu);
22169 complaint (&symfile_complaints,
22170 _("template param type '%s' defined within parent '%s'"),
22171 name ? name : "<unknown>",
22172 parent_name ? parent_name : "<unknown>");
22173 return "";
22174 }
22175 else
22176 switch (parent->tag)
22177 {
22178 case DW_TAG_namespace:
22179 parent_type = read_type_die (parent, cu);
22180 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22181 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22182 Work around this problem here. */
22183 if (cu->language == language_cplus
22184 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
22185 return "";
22186 /* We give a name to even anonymous namespaces. */
22187 return TYPE_TAG_NAME (parent_type);
22188 case DW_TAG_class_type:
22189 case DW_TAG_interface_type:
22190 case DW_TAG_structure_type:
22191 case DW_TAG_union_type:
22192 case DW_TAG_module:
22193 parent_type = read_type_die (parent, cu);
22194 if (TYPE_TAG_NAME (parent_type) != NULL)
22195 return TYPE_TAG_NAME (parent_type);
22196 else
22197 /* An anonymous structure is only allowed non-static data
22198 members; no typedefs, no member functions, et cetera.
22199 So it does not need a prefix. */
22200 return "";
22201 case DW_TAG_compile_unit:
22202 case DW_TAG_partial_unit:
22203 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22204 if (cu->language == language_cplus
22205 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
22206 && die->child != NULL
22207 && (die->tag == DW_TAG_class_type
22208 || die->tag == DW_TAG_structure_type
22209 || die->tag == DW_TAG_union_type))
22210 {
22211 char *name = guess_full_die_structure_name (die, cu);
22212 if (name != NULL)
22213 return name;
22214 }
22215 return "";
22216 case DW_TAG_enumeration_type:
22217 parent_type = read_type_die (parent, cu);
22218 if (TYPE_DECLARED_CLASS (parent_type))
22219 {
22220 if (TYPE_TAG_NAME (parent_type) != NULL)
22221 return TYPE_TAG_NAME (parent_type);
22222 return "";
22223 }
22224 /* Fall through. */
22225 default:
22226 return determine_prefix (parent, cu);
22227 }
22228 }
22229
22230 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22231 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22232 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22233 an obconcat, otherwise allocate storage for the result. The CU argument is
22234 used to determine the language and hence, the appropriate separator. */
22235
22236 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22237
22238 static char *
22239 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22240 int physname, struct dwarf2_cu *cu)
22241 {
22242 const char *lead = "";
22243 const char *sep;
22244
22245 if (suffix == NULL || suffix[0] == '\0'
22246 || prefix == NULL || prefix[0] == '\0')
22247 sep = "";
22248 else if (cu->language == language_d)
22249 {
22250 /* For D, the 'main' function could be defined in any module, but it
22251 should never be prefixed. */
22252 if (strcmp (suffix, "D main") == 0)
22253 {
22254 prefix = "";
22255 sep = "";
22256 }
22257 else
22258 sep = ".";
22259 }
22260 else if (cu->language == language_fortran && physname)
22261 {
22262 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22263 DW_AT_MIPS_linkage_name is preferred and used instead. */
22264
22265 lead = "__";
22266 sep = "_MOD_";
22267 }
22268 else
22269 sep = "::";
22270
22271 if (prefix == NULL)
22272 prefix = "";
22273 if (suffix == NULL)
22274 suffix = "";
22275
22276 if (obs == NULL)
22277 {
22278 char *retval
22279 = ((char *)
22280 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22281
22282 strcpy (retval, lead);
22283 strcat (retval, prefix);
22284 strcat (retval, sep);
22285 strcat (retval, suffix);
22286 return retval;
22287 }
22288 else
22289 {
22290 /* We have an obstack. */
22291 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22292 }
22293 }
22294
22295 /* Return sibling of die, NULL if no sibling. */
22296
22297 static struct die_info *
22298 sibling_die (struct die_info *die)
22299 {
22300 return die->sibling;
22301 }
22302
22303 /* Get name of a die, return NULL if not found. */
22304
22305 static const char *
22306 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22307 struct obstack *obstack)
22308 {
22309 if (name && cu->language == language_cplus)
22310 {
22311 std::string canon_name = cp_canonicalize_string (name);
22312
22313 if (!canon_name.empty ())
22314 {
22315 if (canon_name != name)
22316 name = (const char *) obstack_copy0 (obstack,
22317 canon_name.c_str (),
22318 canon_name.length ());
22319 }
22320 }
22321
22322 return name;
22323 }
22324
22325 /* Get name of a die, return NULL if not found.
22326 Anonymous namespaces are converted to their magic string. */
22327
22328 static const char *
22329 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22330 {
22331 struct attribute *attr;
22332 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22333
22334 attr = dwarf2_attr (die, DW_AT_name, cu);
22335 if ((!attr || !DW_STRING (attr))
22336 && die->tag != DW_TAG_namespace
22337 && die->tag != DW_TAG_class_type
22338 && die->tag != DW_TAG_interface_type
22339 && die->tag != DW_TAG_structure_type
22340 && die->tag != DW_TAG_union_type)
22341 return NULL;
22342
22343 switch (die->tag)
22344 {
22345 case DW_TAG_compile_unit:
22346 case DW_TAG_partial_unit:
22347 /* Compilation units have a DW_AT_name that is a filename, not
22348 a source language identifier. */
22349 case DW_TAG_enumeration_type:
22350 case DW_TAG_enumerator:
22351 /* These tags always have simple identifiers already; no need
22352 to canonicalize them. */
22353 return DW_STRING (attr);
22354
22355 case DW_TAG_namespace:
22356 if (attr != NULL && DW_STRING (attr) != NULL)
22357 return DW_STRING (attr);
22358 return CP_ANONYMOUS_NAMESPACE_STR;
22359
22360 case DW_TAG_class_type:
22361 case DW_TAG_interface_type:
22362 case DW_TAG_structure_type:
22363 case DW_TAG_union_type:
22364 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22365 structures or unions. These were of the form "._%d" in GCC 4.1,
22366 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22367 and GCC 4.4. We work around this problem by ignoring these. */
22368 if (attr && DW_STRING (attr)
22369 && (startswith (DW_STRING (attr), "._")
22370 || startswith (DW_STRING (attr), "<anonymous")))
22371 return NULL;
22372
22373 /* GCC might emit a nameless typedef that has a linkage name. See
22374 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22375 if (!attr || DW_STRING (attr) == NULL)
22376 {
22377 char *demangled = NULL;
22378
22379 attr = dw2_linkage_name_attr (die, cu);
22380 if (attr == NULL || DW_STRING (attr) == NULL)
22381 return NULL;
22382
22383 /* Avoid demangling DW_STRING (attr) the second time on a second
22384 call for the same DIE. */
22385 if (!DW_STRING_IS_CANONICAL (attr))
22386 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22387
22388 if (demangled)
22389 {
22390 const char *base;
22391
22392 /* FIXME: we already did this for the partial symbol... */
22393 DW_STRING (attr)
22394 = ((const char *)
22395 obstack_copy0 (&objfile->per_bfd->storage_obstack,
22396 demangled, strlen (demangled)));
22397 DW_STRING_IS_CANONICAL (attr) = 1;
22398 xfree (demangled);
22399
22400 /* Strip any leading namespaces/classes, keep only the base name.
22401 DW_AT_name for named DIEs does not contain the prefixes. */
22402 base = strrchr (DW_STRING (attr), ':');
22403 if (base && base > DW_STRING (attr) && base[-1] == ':')
22404 return &base[1];
22405 else
22406 return DW_STRING (attr);
22407 }
22408 }
22409 break;
22410
22411 default:
22412 break;
22413 }
22414
22415 if (!DW_STRING_IS_CANONICAL (attr))
22416 {
22417 DW_STRING (attr)
22418 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22419 &objfile->per_bfd->storage_obstack);
22420 DW_STRING_IS_CANONICAL (attr) = 1;
22421 }
22422 return DW_STRING (attr);
22423 }
22424
22425 /* Return the die that this die in an extension of, or NULL if there
22426 is none. *EXT_CU is the CU containing DIE on input, and the CU
22427 containing the return value on output. */
22428
22429 static struct die_info *
22430 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22431 {
22432 struct attribute *attr;
22433
22434 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22435 if (attr == NULL)
22436 return NULL;
22437
22438 return follow_die_ref (die, attr, ext_cu);
22439 }
22440
22441 /* Convert a DIE tag into its string name. */
22442
22443 static const char *
22444 dwarf_tag_name (unsigned tag)
22445 {
22446 const char *name = get_DW_TAG_name (tag);
22447
22448 if (name == NULL)
22449 return "DW_TAG_<unknown>";
22450
22451 return name;
22452 }
22453
22454 /* Convert a DWARF attribute code into its string name. */
22455
22456 static const char *
22457 dwarf_attr_name (unsigned attr)
22458 {
22459 const char *name;
22460
22461 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22462 if (attr == DW_AT_MIPS_fde)
22463 return "DW_AT_MIPS_fde";
22464 #else
22465 if (attr == DW_AT_HP_block_index)
22466 return "DW_AT_HP_block_index";
22467 #endif
22468
22469 name = get_DW_AT_name (attr);
22470
22471 if (name == NULL)
22472 return "DW_AT_<unknown>";
22473
22474 return name;
22475 }
22476
22477 /* Convert a DWARF value form code into its string name. */
22478
22479 static const char *
22480 dwarf_form_name (unsigned form)
22481 {
22482 const char *name = get_DW_FORM_name (form);
22483
22484 if (name == NULL)
22485 return "DW_FORM_<unknown>";
22486
22487 return name;
22488 }
22489
22490 static const char *
22491 dwarf_bool_name (unsigned mybool)
22492 {
22493 if (mybool)
22494 return "TRUE";
22495 else
22496 return "FALSE";
22497 }
22498
22499 /* Convert a DWARF type code into its string name. */
22500
22501 static const char *
22502 dwarf_type_encoding_name (unsigned enc)
22503 {
22504 const char *name = get_DW_ATE_name (enc);
22505
22506 if (name == NULL)
22507 return "DW_ATE_<unknown>";
22508
22509 return name;
22510 }
22511
22512 static void
22513 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22514 {
22515 unsigned int i;
22516
22517 print_spaces (indent, f);
22518 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22519 dwarf_tag_name (die->tag), die->abbrev,
22520 sect_offset_str (die->sect_off));
22521
22522 if (die->parent != NULL)
22523 {
22524 print_spaces (indent, f);
22525 fprintf_unfiltered (f, " parent at offset: %s\n",
22526 sect_offset_str (die->parent->sect_off));
22527 }
22528
22529 print_spaces (indent, f);
22530 fprintf_unfiltered (f, " has children: %s\n",
22531 dwarf_bool_name (die->child != NULL));
22532
22533 print_spaces (indent, f);
22534 fprintf_unfiltered (f, " attributes:\n");
22535
22536 for (i = 0; i < die->num_attrs; ++i)
22537 {
22538 print_spaces (indent, f);
22539 fprintf_unfiltered (f, " %s (%s) ",
22540 dwarf_attr_name (die->attrs[i].name),
22541 dwarf_form_name (die->attrs[i].form));
22542
22543 switch (die->attrs[i].form)
22544 {
22545 case DW_FORM_addr:
22546 case DW_FORM_GNU_addr_index:
22547 fprintf_unfiltered (f, "address: ");
22548 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22549 break;
22550 case DW_FORM_block2:
22551 case DW_FORM_block4:
22552 case DW_FORM_block:
22553 case DW_FORM_block1:
22554 fprintf_unfiltered (f, "block: size %s",
22555 pulongest (DW_BLOCK (&die->attrs[i])->size));
22556 break;
22557 case DW_FORM_exprloc:
22558 fprintf_unfiltered (f, "expression: size %s",
22559 pulongest (DW_BLOCK (&die->attrs[i])->size));
22560 break;
22561 case DW_FORM_data16:
22562 fprintf_unfiltered (f, "constant of 16 bytes");
22563 break;
22564 case DW_FORM_ref_addr:
22565 fprintf_unfiltered (f, "ref address: ");
22566 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22567 break;
22568 case DW_FORM_GNU_ref_alt:
22569 fprintf_unfiltered (f, "alt ref address: ");
22570 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22571 break;
22572 case DW_FORM_ref1:
22573 case DW_FORM_ref2:
22574 case DW_FORM_ref4:
22575 case DW_FORM_ref8:
22576 case DW_FORM_ref_udata:
22577 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22578 (long) (DW_UNSND (&die->attrs[i])));
22579 break;
22580 case DW_FORM_data1:
22581 case DW_FORM_data2:
22582 case DW_FORM_data4:
22583 case DW_FORM_data8:
22584 case DW_FORM_udata:
22585 case DW_FORM_sdata:
22586 fprintf_unfiltered (f, "constant: %s",
22587 pulongest (DW_UNSND (&die->attrs[i])));
22588 break;
22589 case DW_FORM_sec_offset:
22590 fprintf_unfiltered (f, "section offset: %s",
22591 pulongest (DW_UNSND (&die->attrs[i])));
22592 break;
22593 case DW_FORM_ref_sig8:
22594 fprintf_unfiltered (f, "signature: %s",
22595 hex_string (DW_SIGNATURE (&die->attrs[i])));
22596 break;
22597 case DW_FORM_string:
22598 case DW_FORM_strp:
22599 case DW_FORM_line_strp:
22600 case DW_FORM_GNU_str_index:
22601 case DW_FORM_GNU_strp_alt:
22602 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22603 DW_STRING (&die->attrs[i])
22604 ? DW_STRING (&die->attrs[i]) : "",
22605 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22606 break;
22607 case DW_FORM_flag:
22608 if (DW_UNSND (&die->attrs[i]))
22609 fprintf_unfiltered (f, "flag: TRUE");
22610 else
22611 fprintf_unfiltered (f, "flag: FALSE");
22612 break;
22613 case DW_FORM_flag_present:
22614 fprintf_unfiltered (f, "flag: TRUE");
22615 break;
22616 case DW_FORM_indirect:
22617 /* The reader will have reduced the indirect form to
22618 the "base form" so this form should not occur. */
22619 fprintf_unfiltered (f,
22620 "unexpected attribute form: DW_FORM_indirect");
22621 break;
22622 case DW_FORM_implicit_const:
22623 fprintf_unfiltered (f, "constant: %s",
22624 plongest (DW_SND (&die->attrs[i])));
22625 break;
22626 default:
22627 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22628 die->attrs[i].form);
22629 break;
22630 }
22631 fprintf_unfiltered (f, "\n");
22632 }
22633 }
22634
22635 static void
22636 dump_die_for_error (struct die_info *die)
22637 {
22638 dump_die_shallow (gdb_stderr, 0, die);
22639 }
22640
22641 static void
22642 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22643 {
22644 int indent = level * 4;
22645
22646 gdb_assert (die != NULL);
22647
22648 if (level >= max_level)
22649 return;
22650
22651 dump_die_shallow (f, indent, die);
22652
22653 if (die->child != NULL)
22654 {
22655 print_spaces (indent, f);
22656 fprintf_unfiltered (f, " Children:");
22657 if (level + 1 < max_level)
22658 {
22659 fprintf_unfiltered (f, "\n");
22660 dump_die_1 (f, level + 1, max_level, die->child);
22661 }
22662 else
22663 {
22664 fprintf_unfiltered (f,
22665 " [not printed, max nesting level reached]\n");
22666 }
22667 }
22668
22669 if (die->sibling != NULL && level > 0)
22670 {
22671 dump_die_1 (f, level, max_level, die->sibling);
22672 }
22673 }
22674
22675 /* This is called from the pdie macro in gdbinit.in.
22676 It's not static so gcc will keep a copy callable from gdb. */
22677
22678 void
22679 dump_die (struct die_info *die, int max_level)
22680 {
22681 dump_die_1 (gdb_stdlog, 0, max_level, die);
22682 }
22683
22684 static void
22685 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22686 {
22687 void **slot;
22688
22689 slot = htab_find_slot_with_hash (cu->die_hash, die,
22690 to_underlying (die->sect_off),
22691 INSERT);
22692
22693 *slot = die;
22694 }
22695
22696 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22697 required kind. */
22698
22699 static sect_offset
22700 dwarf2_get_ref_die_offset (const struct attribute *attr)
22701 {
22702 if (attr_form_is_ref (attr))
22703 return (sect_offset) DW_UNSND (attr);
22704
22705 complaint (&symfile_complaints,
22706 _("unsupported die ref attribute form: '%s'"),
22707 dwarf_form_name (attr->form));
22708 return {};
22709 }
22710
22711 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22712 * the value held by the attribute is not constant. */
22713
22714 static LONGEST
22715 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22716 {
22717 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22718 return DW_SND (attr);
22719 else if (attr->form == DW_FORM_udata
22720 || attr->form == DW_FORM_data1
22721 || attr->form == DW_FORM_data2
22722 || attr->form == DW_FORM_data4
22723 || attr->form == DW_FORM_data8)
22724 return DW_UNSND (attr);
22725 else
22726 {
22727 /* For DW_FORM_data16 see attr_form_is_constant. */
22728 complaint (&symfile_complaints,
22729 _("Attribute value is not a constant (%s)"),
22730 dwarf_form_name (attr->form));
22731 return default_value;
22732 }
22733 }
22734
22735 /* Follow reference or signature attribute ATTR of SRC_DIE.
22736 On entry *REF_CU is the CU of SRC_DIE.
22737 On exit *REF_CU is the CU of the result. */
22738
22739 static struct die_info *
22740 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22741 struct dwarf2_cu **ref_cu)
22742 {
22743 struct die_info *die;
22744
22745 if (attr_form_is_ref (attr))
22746 die = follow_die_ref (src_die, attr, ref_cu);
22747 else if (attr->form == DW_FORM_ref_sig8)
22748 die = follow_die_sig (src_die, attr, ref_cu);
22749 else
22750 {
22751 dump_die_for_error (src_die);
22752 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22753 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22754 }
22755
22756 return die;
22757 }
22758
22759 /* Follow reference OFFSET.
22760 On entry *REF_CU is the CU of the source die referencing OFFSET.
22761 On exit *REF_CU is the CU of the result.
22762 Returns NULL if OFFSET is invalid. */
22763
22764 static struct die_info *
22765 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22766 struct dwarf2_cu **ref_cu)
22767 {
22768 struct die_info temp_die;
22769 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22770 struct dwarf2_per_objfile *dwarf2_per_objfile
22771 = cu->per_cu->dwarf2_per_objfile;
22772
22773 gdb_assert (cu->per_cu != NULL);
22774
22775 target_cu = cu;
22776
22777 if (cu->per_cu->is_debug_types)
22778 {
22779 /* .debug_types CUs cannot reference anything outside their CU.
22780 If they need to, they have to reference a signatured type via
22781 DW_FORM_ref_sig8. */
22782 if (!offset_in_cu_p (&cu->header, sect_off))
22783 return NULL;
22784 }
22785 else if (offset_in_dwz != cu->per_cu->is_dwz
22786 || !offset_in_cu_p (&cu->header, sect_off))
22787 {
22788 struct dwarf2_per_cu_data *per_cu;
22789
22790 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22791 dwarf2_per_objfile);
22792
22793 /* If necessary, add it to the queue and load its DIEs. */
22794 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22795 load_full_comp_unit (per_cu, cu->language);
22796
22797 target_cu = per_cu->cu;
22798 }
22799 else if (cu->dies == NULL)
22800 {
22801 /* We're loading full DIEs during partial symbol reading. */
22802 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22803 load_full_comp_unit (cu->per_cu, language_minimal);
22804 }
22805
22806 *ref_cu = target_cu;
22807 temp_die.sect_off = sect_off;
22808 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22809 &temp_die,
22810 to_underlying (sect_off));
22811 }
22812
22813 /* Follow reference attribute ATTR of SRC_DIE.
22814 On entry *REF_CU is the CU of SRC_DIE.
22815 On exit *REF_CU is the CU of the result. */
22816
22817 static struct die_info *
22818 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22819 struct dwarf2_cu **ref_cu)
22820 {
22821 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22822 struct dwarf2_cu *cu = *ref_cu;
22823 struct die_info *die;
22824
22825 die = follow_die_offset (sect_off,
22826 (attr->form == DW_FORM_GNU_ref_alt
22827 || cu->per_cu->is_dwz),
22828 ref_cu);
22829 if (!die)
22830 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22831 "at %s [in module %s]"),
22832 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22833 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22834
22835 return die;
22836 }
22837
22838 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22839 Returned value is intended for DW_OP_call*. Returned
22840 dwarf2_locexpr_baton->data has lifetime of
22841 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
22842
22843 struct dwarf2_locexpr_baton
22844 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22845 struct dwarf2_per_cu_data *per_cu,
22846 CORE_ADDR (*get_frame_pc) (void *baton),
22847 void *baton)
22848 {
22849 struct dwarf2_cu *cu;
22850 struct die_info *die;
22851 struct attribute *attr;
22852 struct dwarf2_locexpr_baton retval;
22853 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22854 struct objfile *objfile = dwarf2_per_objfile->objfile;
22855
22856 if (per_cu->cu == NULL)
22857 load_cu (per_cu);
22858 cu = per_cu->cu;
22859 if (cu == NULL)
22860 {
22861 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22862 Instead just throw an error, not much else we can do. */
22863 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22864 sect_offset_str (sect_off), objfile_name (objfile));
22865 }
22866
22867 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22868 if (!die)
22869 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22870 sect_offset_str (sect_off), objfile_name (objfile));
22871
22872 attr = dwarf2_attr (die, DW_AT_location, cu);
22873 if (!attr)
22874 {
22875 /* DWARF: "If there is no such attribute, then there is no effect.".
22876 DATA is ignored if SIZE is 0. */
22877
22878 retval.data = NULL;
22879 retval.size = 0;
22880 }
22881 else if (attr_form_is_section_offset (attr))
22882 {
22883 struct dwarf2_loclist_baton loclist_baton;
22884 CORE_ADDR pc = (*get_frame_pc) (baton);
22885 size_t size;
22886
22887 fill_in_loclist_baton (cu, &loclist_baton, attr);
22888
22889 retval.data = dwarf2_find_location_expression (&loclist_baton,
22890 &size, pc);
22891 retval.size = size;
22892 }
22893 else
22894 {
22895 if (!attr_form_is_block (attr))
22896 error (_("Dwarf Error: DIE at %s referenced in module %s "
22897 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22898 sect_offset_str (sect_off), objfile_name (objfile));
22899
22900 retval.data = DW_BLOCK (attr)->data;
22901 retval.size = DW_BLOCK (attr)->size;
22902 }
22903 retval.per_cu = cu->per_cu;
22904
22905 age_cached_comp_units (dwarf2_per_objfile);
22906
22907 return retval;
22908 }
22909
22910 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
22911 offset. */
22912
22913 struct dwarf2_locexpr_baton
22914 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22915 struct dwarf2_per_cu_data *per_cu,
22916 CORE_ADDR (*get_frame_pc) (void *baton),
22917 void *baton)
22918 {
22919 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22920
22921 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22922 }
22923
22924 /* Write a constant of a given type as target-ordered bytes into
22925 OBSTACK. */
22926
22927 static const gdb_byte *
22928 write_constant_as_bytes (struct obstack *obstack,
22929 enum bfd_endian byte_order,
22930 struct type *type,
22931 ULONGEST value,
22932 LONGEST *len)
22933 {
22934 gdb_byte *result;
22935
22936 *len = TYPE_LENGTH (type);
22937 result = (gdb_byte *) obstack_alloc (obstack, *len);
22938 store_unsigned_integer (result, *len, byte_order, value);
22939
22940 return result;
22941 }
22942
22943 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
22944 pointer to the constant bytes and set LEN to the length of the
22945 data. If memory is needed, allocate it on OBSTACK. If the DIE
22946 does not have a DW_AT_const_value, return NULL. */
22947
22948 const gdb_byte *
22949 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22950 struct dwarf2_per_cu_data *per_cu,
22951 struct obstack *obstack,
22952 LONGEST *len)
22953 {
22954 struct dwarf2_cu *cu;
22955 struct die_info *die;
22956 struct attribute *attr;
22957 const gdb_byte *result = NULL;
22958 struct type *type;
22959 LONGEST value;
22960 enum bfd_endian byte_order;
22961 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22962
22963 if (per_cu->cu == NULL)
22964 load_cu (per_cu);
22965 cu = per_cu->cu;
22966 if (cu == NULL)
22967 {
22968 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22969 Instead just throw an error, not much else we can do. */
22970 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22971 sect_offset_str (sect_off), objfile_name (objfile));
22972 }
22973
22974 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22975 if (!die)
22976 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22977 sect_offset_str (sect_off), objfile_name (objfile));
22978
22979 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22980 if (attr == NULL)
22981 return NULL;
22982
22983 byte_order = (bfd_big_endian (objfile->obfd)
22984 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22985
22986 switch (attr->form)
22987 {
22988 case DW_FORM_addr:
22989 case DW_FORM_GNU_addr_index:
22990 {
22991 gdb_byte *tem;
22992
22993 *len = cu->header.addr_size;
22994 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22995 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22996 result = tem;
22997 }
22998 break;
22999 case DW_FORM_string:
23000 case DW_FORM_strp:
23001 case DW_FORM_GNU_str_index:
23002 case DW_FORM_GNU_strp_alt:
23003 /* DW_STRING is already allocated on the objfile obstack, point
23004 directly to it. */
23005 result = (const gdb_byte *) DW_STRING (attr);
23006 *len = strlen (DW_STRING (attr));
23007 break;
23008 case DW_FORM_block1:
23009 case DW_FORM_block2:
23010 case DW_FORM_block4:
23011 case DW_FORM_block:
23012 case DW_FORM_exprloc:
23013 case DW_FORM_data16:
23014 result = DW_BLOCK (attr)->data;
23015 *len = DW_BLOCK (attr)->size;
23016 break;
23017
23018 /* The DW_AT_const_value attributes are supposed to carry the
23019 symbol's value "represented as it would be on the target
23020 architecture." By the time we get here, it's already been
23021 converted to host endianness, so we just need to sign- or
23022 zero-extend it as appropriate. */
23023 case DW_FORM_data1:
23024 type = die_type (die, cu);
23025 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23026 if (result == NULL)
23027 result = write_constant_as_bytes (obstack, byte_order,
23028 type, value, len);
23029 break;
23030 case DW_FORM_data2:
23031 type = die_type (die, cu);
23032 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23033 if (result == NULL)
23034 result = write_constant_as_bytes (obstack, byte_order,
23035 type, value, len);
23036 break;
23037 case DW_FORM_data4:
23038 type = die_type (die, cu);
23039 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23040 if (result == NULL)
23041 result = write_constant_as_bytes (obstack, byte_order,
23042 type, value, len);
23043 break;
23044 case DW_FORM_data8:
23045 type = die_type (die, cu);
23046 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23047 if (result == NULL)
23048 result = write_constant_as_bytes (obstack, byte_order,
23049 type, value, len);
23050 break;
23051
23052 case DW_FORM_sdata:
23053 case DW_FORM_implicit_const:
23054 type = die_type (die, cu);
23055 result = write_constant_as_bytes (obstack, byte_order,
23056 type, DW_SND (attr), len);
23057 break;
23058
23059 case DW_FORM_udata:
23060 type = die_type (die, cu);
23061 result = write_constant_as_bytes (obstack, byte_order,
23062 type, DW_UNSND (attr), len);
23063 break;
23064
23065 default:
23066 complaint (&symfile_complaints,
23067 _("unsupported const value attribute form: '%s'"),
23068 dwarf_form_name (attr->form));
23069 break;
23070 }
23071
23072 return result;
23073 }
23074
23075 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23076 valid type for this die is found. */
23077
23078 struct type *
23079 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23080 struct dwarf2_per_cu_data *per_cu)
23081 {
23082 struct dwarf2_cu *cu;
23083 struct die_info *die;
23084
23085 if (per_cu->cu == NULL)
23086 load_cu (per_cu);
23087 cu = per_cu->cu;
23088 if (!cu)
23089 return NULL;
23090
23091 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23092 if (!die)
23093 return NULL;
23094
23095 return die_type (die, cu);
23096 }
23097
23098 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23099 PER_CU. */
23100
23101 struct type *
23102 dwarf2_get_die_type (cu_offset die_offset,
23103 struct dwarf2_per_cu_data *per_cu)
23104 {
23105 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23106 return get_die_type_at_offset (die_offset_sect, per_cu);
23107 }
23108
23109 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23110 On entry *REF_CU is the CU of SRC_DIE.
23111 On exit *REF_CU is the CU of the result.
23112 Returns NULL if the referenced DIE isn't found. */
23113
23114 static struct die_info *
23115 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23116 struct dwarf2_cu **ref_cu)
23117 {
23118 struct die_info temp_die;
23119 struct dwarf2_cu *sig_cu;
23120 struct die_info *die;
23121
23122 /* While it might be nice to assert sig_type->type == NULL here,
23123 we can get here for DW_AT_imported_declaration where we need
23124 the DIE not the type. */
23125
23126 /* If necessary, add it to the queue and load its DIEs. */
23127
23128 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23129 read_signatured_type (sig_type);
23130
23131 sig_cu = sig_type->per_cu.cu;
23132 gdb_assert (sig_cu != NULL);
23133 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23134 temp_die.sect_off = sig_type->type_offset_in_section;
23135 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23136 to_underlying (temp_die.sect_off));
23137 if (die)
23138 {
23139 struct dwarf2_per_objfile *dwarf2_per_objfile
23140 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23141
23142 /* For .gdb_index version 7 keep track of included TUs.
23143 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23144 if (dwarf2_per_objfile->index_table != NULL
23145 && dwarf2_per_objfile->index_table->version <= 7)
23146 {
23147 VEC_safe_push (dwarf2_per_cu_ptr,
23148 (*ref_cu)->per_cu->imported_symtabs,
23149 sig_cu->per_cu);
23150 }
23151
23152 *ref_cu = sig_cu;
23153 return die;
23154 }
23155
23156 return NULL;
23157 }
23158
23159 /* Follow signatured type referenced by ATTR in SRC_DIE.
23160 On entry *REF_CU is the CU of SRC_DIE.
23161 On exit *REF_CU is the CU of the result.
23162 The result is the DIE of the type.
23163 If the referenced type cannot be found an error is thrown. */
23164
23165 static struct die_info *
23166 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23167 struct dwarf2_cu **ref_cu)
23168 {
23169 ULONGEST signature = DW_SIGNATURE (attr);
23170 struct signatured_type *sig_type;
23171 struct die_info *die;
23172
23173 gdb_assert (attr->form == DW_FORM_ref_sig8);
23174
23175 sig_type = lookup_signatured_type (*ref_cu, signature);
23176 /* sig_type will be NULL if the signatured type is missing from
23177 the debug info. */
23178 if (sig_type == NULL)
23179 {
23180 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23181 " from DIE at %s [in module %s]"),
23182 hex_string (signature), sect_offset_str (src_die->sect_off),
23183 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23184 }
23185
23186 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23187 if (die == NULL)
23188 {
23189 dump_die_for_error (src_die);
23190 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23191 " from DIE at %s [in module %s]"),
23192 hex_string (signature), sect_offset_str (src_die->sect_off),
23193 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23194 }
23195
23196 return die;
23197 }
23198
23199 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23200 reading in and processing the type unit if necessary. */
23201
23202 static struct type *
23203 get_signatured_type (struct die_info *die, ULONGEST signature,
23204 struct dwarf2_cu *cu)
23205 {
23206 struct dwarf2_per_objfile *dwarf2_per_objfile
23207 = cu->per_cu->dwarf2_per_objfile;
23208 struct signatured_type *sig_type;
23209 struct dwarf2_cu *type_cu;
23210 struct die_info *type_die;
23211 struct type *type;
23212
23213 sig_type = lookup_signatured_type (cu, signature);
23214 /* sig_type will be NULL if the signatured type is missing from
23215 the debug info. */
23216 if (sig_type == NULL)
23217 {
23218 complaint (&symfile_complaints,
23219 _("Dwarf Error: Cannot find signatured DIE %s referenced"
23220 " from DIE at %s [in module %s]"),
23221 hex_string (signature), sect_offset_str (die->sect_off),
23222 objfile_name (dwarf2_per_objfile->objfile));
23223 return build_error_marker_type (cu, die);
23224 }
23225
23226 /* If we already know the type we're done. */
23227 if (sig_type->type != NULL)
23228 return sig_type->type;
23229
23230 type_cu = cu;
23231 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23232 if (type_die != NULL)
23233 {
23234 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23235 is created. This is important, for example, because for c++ classes
23236 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23237 type = read_type_die (type_die, type_cu);
23238 if (type == NULL)
23239 {
23240 complaint (&symfile_complaints,
23241 _("Dwarf Error: Cannot build signatured type %s"
23242 " referenced from DIE at %s [in module %s]"),
23243 hex_string (signature), sect_offset_str (die->sect_off),
23244 objfile_name (dwarf2_per_objfile->objfile));
23245 type = build_error_marker_type (cu, die);
23246 }
23247 }
23248 else
23249 {
23250 complaint (&symfile_complaints,
23251 _("Dwarf Error: Problem reading signatured DIE %s referenced"
23252 " from DIE at %s [in module %s]"),
23253 hex_string (signature), sect_offset_str (die->sect_off),
23254 objfile_name (dwarf2_per_objfile->objfile));
23255 type = build_error_marker_type (cu, die);
23256 }
23257 sig_type->type = type;
23258
23259 return type;
23260 }
23261
23262 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23263 reading in and processing the type unit if necessary. */
23264
23265 static struct type *
23266 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23267 struct dwarf2_cu *cu) /* ARI: editCase function */
23268 {
23269 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23270 if (attr_form_is_ref (attr))
23271 {
23272 struct dwarf2_cu *type_cu = cu;
23273 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23274
23275 return read_type_die (type_die, type_cu);
23276 }
23277 else if (attr->form == DW_FORM_ref_sig8)
23278 {
23279 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23280 }
23281 else
23282 {
23283 struct dwarf2_per_objfile *dwarf2_per_objfile
23284 = cu->per_cu->dwarf2_per_objfile;
23285
23286 complaint (&symfile_complaints,
23287 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23288 " at %s [in module %s]"),
23289 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23290 objfile_name (dwarf2_per_objfile->objfile));
23291 return build_error_marker_type (cu, die);
23292 }
23293 }
23294
23295 /* Load the DIEs associated with type unit PER_CU into memory. */
23296
23297 static void
23298 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23299 {
23300 struct signatured_type *sig_type;
23301
23302 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23303 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23304
23305 /* We have the per_cu, but we need the signatured_type.
23306 Fortunately this is an easy translation. */
23307 gdb_assert (per_cu->is_debug_types);
23308 sig_type = (struct signatured_type *) per_cu;
23309
23310 gdb_assert (per_cu->cu == NULL);
23311
23312 read_signatured_type (sig_type);
23313
23314 gdb_assert (per_cu->cu != NULL);
23315 }
23316
23317 /* die_reader_func for read_signatured_type.
23318 This is identical to load_full_comp_unit_reader,
23319 but is kept separate for now. */
23320
23321 static void
23322 read_signatured_type_reader (const struct die_reader_specs *reader,
23323 const gdb_byte *info_ptr,
23324 struct die_info *comp_unit_die,
23325 int has_children,
23326 void *data)
23327 {
23328 struct dwarf2_cu *cu = reader->cu;
23329
23330 gdb_assert (cu->die_hash == NULL);
23331 cu->die_hash =
23332 htab_create_alloc_ex (cu->header.length / 12,
23333 die_hash,
23334 die_eq,
23335 NULL,
23336 &cu->comp_unit_obstack,
23337 hashtab_obstack_allocate,
23338 dummy_obstack_deallocate);
23339
23340 if (has_children)
23341 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23342 &info_ptr, comp_unit_die);
23343 cu->dies = comp_unit_die;
23344 /* comp_unit_die is not stored in die_hash, no need. */
23345
23346 /* We try not to read any attributes in this function, because not
23347 all CUs needed for references have been loaded yet, and symbol
23348 table processing isn't initialized. But we have to set the CU language,
23349 or we won't be able to build types correctly.
23350 Similarly, if we do not read the producer, we can not apply
23351 producer-specific interpretation. */
23352 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23353 }
23354
23355 /* Read in a signatured type and build its CU and DIEs.
23356 If the type is a stub for the real type in a DWO file,
23357 read in the real type from the DWO file as well. */
23358
23359 static void
23360 read_signatured_type (struct signatured_type *sig_type)
23361 {
23362 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23363
23364 gdb_assert (per_cu->is_debug_types);
23365 gdb_assert (per_cu->cu == NULL);
23366
23367 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
23368 read_signatured_type_reader, NULL);
23369 sig_type->per_cu.tu_read = 1;
23370 }
23371
23372 /* Decode simple location descriptions.
23373 Given a pointer to a dwarf block that defines a location, compute
23374 the location and return the value.
23375
23376 NOTE drow/2003-11-18: This function is called in two situations
23377 now: for the address of static or global variables (partial symbols
23378 only) and for offsets into structures which are expected to be
23379 (more or less) constant. The partial symbol case should go away,
23380 and only the constant case should remain. That will let this
23381 function complain more accurately. A few special modes are allowed
23382 without complaint for global variables (for instance, global
23383 register values and thread-local values).
23384
23385 A location description containing no operations indicates that the
23386 object is optimized out. The return value is 0 for that case.
23387 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23388 callers will only want a very basic result and this can become a
23389 complaint.
23390
23391 Note that stack[0] is unused except as a default error return. */
23392
23393 static CORE_ADDR
23394 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23395 {
23396 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23397 size_t i;
23398 size_t size = blk->size;
23399 const gdb_byte *data = blk->data;
23400 CORE_ADDR stack[64];
23401 int stacki;
23402 unsigned int bytes_read, unsnd;
23403 gdb_byte op;
23404
23405 i = 0;
23406 stacki = 0;
23407 stack[stacki] = 0;
23408 stack[++stacki] = 0;
23409
23410 while (i < size)
23411 {
23412 op = data[i++];
23413 switch (op)
23414 {
23415 case DW_OP_lit0:
23416 case DW_OP_lit1:
23417 case DW_OP_lit2:
23418 case DW_OP_lit3:
23419 case DW_OP_lit4:
23420 case DW_OP_lit5:
23421 case DW_OP_lit6:
23422 case DW_OP_lit7:
23423 case DW_OP_lit8:
23424 case DW_OP_lit9:
23425 case DW_OP_lit10:
23426 case DW_OP_lit11:
23427 case DW_OP_lit12:
23428 case DW_OP_lit13:
23429 case DW_OP_lit14:
23430 case DW_OP_lit15:
23431 case DW_OP_lit16:
23432 case DW_OP_lit17:
23433 case DW_OP_lit18:
23434 case DW_OP_lit19:
23435 case DW_OP_lit20:
23436 case DW_OP_lit21:
23437 case DW_OP_lit22:
23438 case DW_OP_lit23:
23439 case DW_OP_lit24:
23440 case DW_OP_lit25:
23441 case DW_OP_lit26:
23442 case DW_OP_lit27:
23443 case DW_OP_lit28:
23444 case DW_OP_lit29:
23445 case DW_OP_lit30:
23446 case DW_OP_lit31:
23447 stack[++stacki] = op - DW_OP_lit0;
23448 break;
23449
23450 case DW_OP_reg0:
23451 case DW_OP_reg1:
23452 case DW_OP_reg2:
23453 case DW_OP_reg3:
23454 case DW_OP_reg4:
23455 case DW_OP_reg5:
23456 case DW_OP_reg6:
23457 case DW_OP_reg7:
23458 case DW_OP_reg8:
23459 case DW_OP_reg9:
23460 case DW_OP_reg10:
23461 case DW_OP_reg11:
23462 case DW_OP_reg12:
23463 case DW_OP_reg13:
23464 case DW_OP_reg14:
23465 case DW_OP_reg15:
23466 case DW_OP_reg16:
23467 case DW_OP_reg17:
23468 case DW_OP_reg18:
23469 case DW_OP_reg19:
23470 case DW_OP_reg20:
23471 case DW_OP_reg21:
23472 case DW_OP_reg22:
23473 case DW_OP_reg23:
23474 case DW_OP_reg24:
23475 case DW_OP_reg25:
23476 case DW_OP_reg26:
23477 case DW_OP_reg27:
23478 case DW_OP_reg28:
23479 case DW_OP_reg29:
23480 case DW_OP_reg30:
23481 case DW_OP_reg31:
23482 stack[++stacki] = op - DW_OP_reg0;
23483 if (i < size)
23484 dwarf2_complex_location_expr_complaint ();
23485 break;
23486
23487 case DW_OP_regx:
23488 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23489 i += bytes_read;
23490 stack[++stacki] = unsnd;
23491 if (i < size)
23492 dwarf2_complex_location_expr_complaint ();
23493 break;
23494
23495 case DW_OP_addr:
23496 stack[++stacki] = read_address (objfile->obfd, &data[i],
23497 cu, &bytes_read);
23498 i += bytes_read;
23499 break;
23500
23501 case DW_OP_const1u:
23502 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23503 i += 1;
23504 break;
23505
23506 case DW_OP_const1s:
23507 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23508 i += 1;
23509 break;
23510
23511 case DW_OP_const2u:
23512 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23513 i += 2;
23514 break;
23515
23516 case DW_OP_const2s:
23517 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23518 i += 2;
23519 break;
23520
23521 case DW_OP_const4u:
23522 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23523 i += 4;
23524 break;
23525
23526 case DW_OP_const4s:
23527 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23528 i += 4;
23529 break;
23530
23531 case DW_OP_const8u:
23532 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23533 i += 8;
23534 break;
23535
23536 case DW_OP_constu:
23537 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23538 &bytes_read);
23539 i += bytes_read;
23540 break;
23541
23542 case DW_OP_consts:
23543 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23544 i += bytes_read;
23545 break;
23546
23547 case DW_OP_dup:
23548 stack[stacki + 1] = stack[stacki];
23549 stacki++;
23550 break;
23551
23552 case DW_OP_plus:
23553 stack[stacki - 1] += stack[stacki];
23554 stacki--;
23555 break;
23556
23557 case DW_OP_plus_uconst:
23558 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23559 &bytes_read);
23560 i += bytes_read;
23561 break;
23562
23563 case DW_OP_minus:
23564 stack[stacki - 1] -= stack[stacki];
23565 stacki--;
23566 break;
23567
23568 case DW_OP_deref:
23569 /* If we're not the last op, then we definitely can't encode
23570 this using GDB's address_class enum. This is valid for partial
23571 global symbols, although the variable's address will be bogus
23572 in the psymtab. */
23573 if (i < size)
23574 dwarf2_complex_location_expr_complaint ();
23575 break;
23576
23577 case DW_OP_GNU_push_tls_address:
23578 case DW_OP_form_tls_address:
23579 /* The top of the stack has the offset from the beginning
23580 of the thread control block at which the variable is located. */
23581 /* Nothing should follow this operator, so the top of stack would
23582 be returned. */
23583 /* This is valid for partial global symbols, but the variable's
23584 address will be bogus in the psymtab. Make it always at least
23585 non-zero to not look as a variable garbage collected by linker
23586 which have DW_OP_addr 0. */
23587 if (i < size)
23588 dwarf2_complex_location_expr_complaint ();
23589 stack[stacki]++;
23590 break;
23591
23592 case DW_OP_GNU_uninit:
23593 break;
23594
23595 case DW_OP_GNU_addr_index:
23596 case DW_OP_GNU_const_index:
23597 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23598 &bytes_read);
23599 i += bytes_read;
23600 break;
23601
23602 default:
23603 {
23604 const char *name = get_DW_OP_name (op);
23605
23606 if (name)
23607 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
23608 name);
23609 else
23610 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
23611 op);
23612 }
23613
23614 return (stack[stacki]);
23615 }
23616
23617 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23618 outside of the allocated space. Also enforce minimum>0. */
23619 if (stacki >= ARRAY_SIZE (stack) - 1)
23620 {
23621 complaint (&symfile_complaints,
23622 _("location description stack overflow"));
23623 return 0;
23624 }
23625
23626 if (stacki <= 0)
23627 {
23628 complaint (&symfile_complaints,
23629 _("location description stack underflow"));
23630 return 0;
23631 }
23632 }
23633 return (stack[stacki]);
23634 }
23635
23636 /* memory allocation interface */
23637
23638 static struct dwarf_block *
23639 dwarf_alloc_block (struct dwarf2_cu *cu)
23640 {
23641 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23642 }
23643
23644 static struct die_info *
23645 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23646 {
23647 struct die_info *die;
23648 size_t size = sizeof (struct die_info);
23649
23650 if (num_attrs > 1)
23651 size += (num_attrs - 1) * sizeof (struct attribute);
23652
23653 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23654 memset (die, 0, sizeof (struct die_info));
23655 return (die);
23656 }
23657
23658 \f
23659 /* Macro support. */
23660
23661 /* Return file name relative to the compilation directory of file number I in
23662 *LH's file name table. The result is allocated using xmalloc; the caller is
23663 responsible for freeing it. */
23664
23665 static char *
23666 file_file_name (int file, struct line_header *lh)
23667 {
23668 /* Is the file number a valid index into the line header's file name
23669 table? Remember that file numbers start with one, not zero. */
23670 if (1 <= file && file <= lh->file_names.size ())
23671 {
23672 const file_entry &fe = lh->file_names[file - 1];
23673
23674 if (!IS_ABSOLUTE_PATH (fe.name))
23675 {
23676 const char *dir = fe.include_dir (lh);
23677 if (dir != NULL)
23678 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23679 }
23680 return xstrdup (fe.name);
23681 }
23682 else
23683 {
23684 /* The compiler produced a bogus file number. We can at least
23685 record the macro definitions made in the file, even if we
23686 won't be able to find the file by name. */
23687 char fake_name[80];
23688
23689 xsnprintf (fake_name, sizeof (fake_name),
23690 "<bad macro file number %d>", file);
23691
23692 complaint (&symfile_complaints,
23693 _("bad file number in macro information (%d)"),
23694 file);
23695
23696 return xstrdup (fake_name);
23697 }
23698 }
23699
23700 /* Return the full name of file number I in *LH's file name table.
23701 Use COMP_DIR as the name of the current directory of the
23702 compilation. The result is allocated using xmalloc; the caller is
23703 responsible for freeing it. */
23704 static char *
23705 file_full_name (int file, struct line_header *lh, const char *comp_dir)
23706 {
23707 /* Is the file number a valid index into the line header's file name
23708 table? Remember that file numbers start with one, not zero. */
23709 if (1 <= file && file <= lh->file_names.size ())
23710 {
23711 char *relative = file_file_name (file, lh);
23712
23713 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23714 return relative;
23715 return reconcat (relative, comp_dir, SLASH_STRING,
23716 relative, (char *) NULL);
23717 }
23718 else
23719 return file_file_name (file, lh);
23720 }
23721
23722
23723 static struct macro_source_file *
23724 macro_start_file (int file, int line,
23725 struct macro_source_file *current_file,
23726 struct line_header *lh)
23727 {
23728 /* File name relative to the compilation directory of this source file. */
23729 char *file_name = file_file_name (file, lh);
23730
23731 if (! current_file)
23732 {
23733 /* Note: We don't create a macro table for this compilation unit
23734 at all until we actually get a filename. */
23735 struct macro_table *macro_table = get_macro_table ();
23736
23737 /* If we have no current file, then this must be the start_file
23738 directive for the compilation unit's main source file. */
23739 current_file = macro_set_main (macro_table, file_name);
23740 macro_define_special (macro_table);
23741 }
23742 else
23743 current_file = macro_include (current_file, line, file_name);
23744
23745 xfree (file_name);
23746
23747 return current_file;
23748 }
23749
23750 static const char *
23751 consume_improper_spaces (const char *p, const char *body)
23752 {
23753 if (*p == ' ')
23754 {
23755 complaint (&symfile_complaints,
23756 _("macro definition contains spaces "
23757 "in formal argument list:\n`%s'"),
23758 body);
23759
23760 while (*p == ' ')
23761 p++;
23762 }
23763
23764 return p;
23765 }
23766
23767
23768 static void
23769 parse_macro_definition (struct macro_source_file *file, int line,
23770 const char *body)
23771 {
23772 const char *p;
23773
23774 /* The body string takes one of two forms. For object-like macro
23775 definitions, it should be:
23776
23777 <macro name> " " <definition>
23778
23779 For function-like macro definitions, it should be:
23780
23781 <macro name> "() " <definition>
23782 or
23783 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23784
23785 Spaces may appear only where explicitly indicated, and in the
23786 <definition>.
23787
23788 The Dwarf 2 spec says that an object-like macro's name is always
23789 followed by a space, but versions of GCC around March 2002 omit
23790 the space when the macro's definition is the empty string.
23791
23792 The Dwarf 2 spec says that there should be no spaces between the
23793 formal arguments in a function-like macro's formal argument list,
23794 but versions of GCC around March 2002 include spaces after the
23795 commas. */
23796
23797
23798 /* Find the extent of the macro name. The macro name is terminated
23799 by either a space or null character (for an object-like macro) or
23800 an opening paren (for a function-like macro). */
23801 for (p = body; *p; p++)
23802 if (*p == ' ' || *p == '(')
23803 break;
23804
23805 if (*p == ' ' || *p == '\0')
23806 {
23807 /* It's an object-like macro. */
23808 int name_len = p - body;
23809 char *name = savestring (body, name_len);
23810 const char *replacement;
23811
23812 if (*p == ' ')
23813 replacement = body + name_len + 1;
23814 else
23815 {
23816 dwarf2_macro_malformed_definition_complaint (body);
23817 replacement = body + name_len;
23818 }
23819
23820 macro_define_object (file, line, name, replacement);
23821
23822 xfree (name);
23823 }
23824 else if (*p == '(')
23825 {
23826 /* It's a function-like macro. */
23827 char *name = savestring (body, p - body);
23828 int argc = 0;
23829 int argv_size = 1;
23830 char **argv = XNEWVEC (char *, argv_size);
23831
23832 p++;
23833
23834 p = consume_improper_spaces (p, body);
23835
23836 /* Parse the formal argument list. */
23837 while (*p && *p != ')')
23838 {
23839 /* Find the extent of the current argument name. */
23840 const char *arg_start = p;
23841
23842 while (*p && *p != ',' && *p != ')' && *p != ' ')
23843 p++;
23844
23845 if (! *p || p == arg_start)
23846 dwarf2_macro_malformed_definition_complaint (body);
23847 else
23848 {
23849 /* Make sure argv has room for the new argument. */
23850 if (argc >= argv_size)
23851 {
23852 argv_size *= 2;
23853 argv = XRESIZEVEC (char *, argv, argv_size);
23854 }
23855
23856 argv[argc++] = savestring (arg_start, p - arg_start);
23857 }
23858
23859 p = consume_improper_spaces (p, body);
23860
23861 /* Consume the comma, if present. */
23862 if (*p == ',')
23863 {
23864 p++;
23865
23866 p = consume_improper_spaces (p, body);
23867 }
23868 }
23869
23870 if (*p == ')')
23871 {
23872 p++;
23873
23874 if (*p == ' ')
23875 /* Perfectly formed definition, no complaints. */
23876 macro_define_function (file, line, name,
23877 argc, (const char **) argv,
23878 p + 1);
23879 else if (*p == '\0')
23880 {
23881 /* Complain, but do define it. */
23882 dwarf2_macro_malformed_definition_complaint (body);
23883 macro_define_function (file, line, name,
23884 argc, (const char **) argv,
23885 p);
23886 }
23887 else
23888 /* Just complain. */
23889 dwarf2_macro_malformed_definition_complaint (body);
23890 }
23891 else
23892 /* Just complain. */
23893 dwarf2_macro_malformed_definition_complaint (body);
23894
23895 xfree (name);
23896 {
23897 int i;
23898
23899 for (i = 0; i < argc; i++)
23900 xfree (argv[i]);
23901 }
23902 xfree (argv);
23903 }
23904 else
23905 dwarf2_macro_malformed_definition_complaint (body);
23906 }
23907
23908 /* Skip some bytes from BYTES according to the form given in FORM.
23909 Returns the new pointer. */
23910
23911 static const gdb_byte *
23912 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
23913 enum dwarf_form form,
23914 unsigned int offset_size,
23915 struct dwarf2_section_info *section)
23916 {
23917 unsigned int bytes_read;
23918
23919 switch (form)
23920 {
23921 case DW_FORM_data1:
23922 case DW_FORM_flag:
23923 ++bytes;
23924 break;
23925
23926 case DW_FORM_data2:
23927 bytes += 2;
23928 break;
23929
23930 case DW_FORM_data4:
23931 bytes += 4;
23932 break;
23933
23934 case DW_FORM_data8:
23935 bytes += 8;
23936 break;
23937
23938 case DW_FORM_data16:
23939 bytes += 16;
23940 break;
23941
23942 case DW_FORM_string:
23943 read_direct_string (abfd, bytes, &bytes_read);
23944 bytes += bytes_read;
23945 break;
23946
23947 case DW_FORM_sec_offset:
23948 case DW_FORM_strp:
23949 case DW_FORM_GNU_strp_alt:
23950 bytes += offset_size;
23951 break;
23952
23953 case DW_FORM_block:
23954 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23955 bytes += bytes_read;
23956 break;
23957
23958 case DW_FORM_block1:
23959 bytes += 1 + read_1_byte (abfd, bytes);
23960 break;
23961 case DW_FORM_block2:
23962 bytes += 2 + read_2_bytes (abfd, bytes);
23963 break;
23964 case DW_FORM_block4:
23965 bytes += 4 + read_4_bytes (abfd, bytes);
23966 break;
23967
23968 case DW_FORM_sdata:
23969 case DW_FORM_udata:
23970 case DW_FORM_GNU_addr_index:
23971 case DW_FORM_GNU_str_index:
23972 bytes = gdb_skip_leb128 (bytes, buffer_end);
23973 if (bytes == NULL)
23974 {
23975 dwarf2_section_buffer_overflow_complaint (section);
23976 return NULL;
23977 }
23978 break;
23979
23980 case DW_FORM_implicit_const:
23981 break;
23982
23983 default:
23984 {
23985 complaint (&symfile_complaints,
23986 _("invalid form 0x%x in `%s'"),
23987 form, get_section_name (section));
23988 return NULL;
23989 }
23990 }
23991
23992 return bytes;
23993 }
23994
23995 /* A helper for dwarf_decode_macros that handles skipping an unknown
23996 opcode. Returns an updated pointer to the macro data buffer; or,
23997 on error, issues a complaint and returns NULL. */
23998
23999 static const gdb_byte *
24000 skip_unknown_opcode (unsigned int opcode,
24001 const gdb_byte **opcode_definitions,
24002 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24003 bfd *abfd,
24004 unsigned int offset_size,
24005 struct dwarf2_section_info *section)
24006 {
24007 unsigned int bytes_read, i;
24008 unsigned long arg;
24009 const gdb_byte *defn;
24010
24011 if (opcode_definitions[opcode] == NULL)
24012 {
24013 complaint (&symfile_complaints,
24014 _("unrecognized DW_MACFINO opcode 0x%x"),
24015 opcode);
24016 return NULL;
24017 }
24018
24019 defn = opcode_definitions[opcode];
24020 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24021 defn += bytes_read;
24022
24023 for (i = 0; i < arg; ++i)
24024 {
24025 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24026 (enum dwarf_form) defn[i], offset_size,
24027 section);
24028 if (mac_ptr == NULL)
24029 {
24030 /* skip_form_bytes already issued the complaint. */
24031 return NULL;
24032 }
24033 }
24034
24035 return mac_ptr;
24036 }
24037
24038 /* A helper function which parses the header of a macro section.
24039 If the macro section is the extended (for now called "GNU") type,
24040 then this updates *OFFSET_SIZE. Returns a pointer to just after
24041 the header, or issues a complaint and returns NULL on error. */
24042
24043 static const gdb_byte *
24044 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24045 bfd *abfd,
24046 const gdb_byte *mac_ptr,
24047 unsigned int *offset_size,
24048 int section_is_gnu)
24049 {
24050 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24051
24052 if (section_is_gnu)
24053 {
24054 unsigned int version, flags;
24055
24056 version = read_2_bytes (abfd, mac_ptr);
24057 if (version != 4 && version != 5)
24058 {
24059 complaint (&symfile_complaints,
24060 _("unrecognized version `%d' in .debug_macro section"),
24061 version);
24062 return NULL;
24063 }
24064 mac_ptr += 2;
24065
24066 flags = read_1_byte (abfd, mac_ptr);
24067 ++mac_ptr;
24068 *offset_size = (flags & 1) ? 8 : 4;
24069
24070 if ((flags & 2) != 0)
24071 /* We don't need the line table offset. */
24072 mac_ptr += *offset_size;
24073
24074 /* Vendor opcode descriptions. */
24075 if ((flags & 4) != 0)
24076 {
24077 unsigned int i, count;
24078
24079 count = read_1_byte (abfd, mac_ptr);
24080 ++mac_ptr;
24081 for (i = 0; i < count; ++i)
24082 {
24083 unsigned int opcode, bytes_read;
24084 unsigned long arg;
24085
24086 opcode = read_1_byte (abfd, mac_ptr);
24087 ++mac_ptr;
24088 opcode_definitions[opcode] = mac_ptr;
24089 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24090 mac_ptr += bytes_read;
24091 mac_ptr += arg;
24092 }
24093 }
24094 }
24095
24096 return mac_ptr;
24097 }
24098
24099 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24100 including DW_MACRO_import. */
24101
24102 static void
24103 dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24104 bfd *abfd,
24105 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24106 struct macro_source_file *current_file,
24107 struct line_header *lh,
24108 struct dwarf2_section_info *section,
24109 int section_is_gnu, int section_is_dwz,
24110 unsigned int offset_size,
24111 htab_t include_hash)
24112 {
24113 struct objfile *objfile = dwarf2_per_objfile->objfile;
24114 enum dwarf_macro_record_type macinfo_type;
24115 int at_commandline;
24116 const gdb_byte *opcode_definitions[256];
24117
24118 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24119 &offset_size, section_is_gnu);
24120 if (mac_ptr == NULL)
24121 {
24122 /* We already issued a complaint. */
24123 return;
24124 }
24125
24126 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24127 GDB is still reading the definitions from command line. First
24128 DW_MACINFO_start_file will need to be ignored as it was already executed
24129 to create CURRENT_FILE for the main source holding also the command line
24130 definitions. On first met DW_MACINFO_start_file this flag is reset to
24131 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24132
24133 at_commandline = 1;
24134
24135 do
24136 {
24137 /* Do we at least have room for a macinfo type byte? */
24138 if (mac_ptr >= mac_end)
24139 {
24140 dwarf2_section_buffer_overflow_complaint (section);
24141 break;
24142 }
24143
24144 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24145 mac_ptr++;
24146
24147 /* Note that we rely on the fact that the corresponding GNU and
24148 DWARF constants are the same. */
24149 DIAGNOSTIC_PUSH
24150 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24151 switch (macinfo_type)
24152 {
24153 /* A zero macinfo type indicates the end of the macro
24154 information. */
24155 case 0:
24156 break;
24157
24158 case DW_MACRO_define:
24159 case DW_MACRO_undef:
24160 case DW_MACRO_define_strp:
24161 case DW_MACRO_undef_strp:
24162 case DW_MACRO_define_sup:
24163 case DW_MACRO_undef_sup:
24164 {
24165 unsigned int bytes_read;
24166 int line;
24167 const char *body;
24168 int is_define;
24169
24170 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24171 mac_ptr += bytes_read;
24172
24173 if (macinfo_type == DW_MACRO_define
24174 || macinfo_type == DW_MACRO_undef)
24175 {
24176 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24177 mac_ptr += bytes_read;
24178 }
24179 else
24180 {
24181 LONGEST str_offset;
24182
24183 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24184 mac_ptr += offset_size;
24185
24186 if (macinfo_type == DW_MACRO_define_sup
24187 || macinfo_type == DW_MACRO_undef_sup
24188 || section_is_dwz)
24189 {
24190 struct dwz_file *dwz
24191 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24192
24193 body = read_indirect_string_from_dwz (objfile,
24194 dwz, str_offset);
24195 }
24196 else
24197 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24198 abfd, str_offset);
24199 }
24200
24201 is_define = (macinfo_type == DW_MACRO_define
24202 || macinfo_type == DW_MACRO_define_strp
24203 || macinfo_type == DW_MACRO_define_sup);
24204 if (! current_file)
24205 {
24206 /* DWARF violation as no main source is present. */
24207 complaint (&symfile_complaints,
24208 _("debug info with no main source gives macro %s "
24209 "on line %d: %s"),
24210 is_define ? _("definition") : _("undefinition"),
24211 line, body);
24212 break;
24213 }
24214 if ((line == 0 && !at_commandline)
24215 || (line != 0 && at_commandline))
24216 complaint (&symfile_complaints,
24217 _("debug info gives %s macro %s with %s line %d: %s"),
24218 at_commandline ? _("command-line") : _("in-file"),
24219 is_define ? _("definition") : _("undefinition"),
24220 line == 0 ? _("zero") : _("non-zero"), line, body);
24221
24222 if (is_define)
24223 parse_macro_definition (current_file, line, body);
24224 else
24225 {
24226 gdb_assert (macinfo_type == DW_MACRO_undef
24227 || macinfo_type == DW_MACRO_undef_strp
24228 || macinfo_type == DW_MACRO_undef_sup);
24229 macro_undef (current_file, line, body);
24230 }
24231 }
24232 break;
24233
24234 case DW_MACRO_start_file:
24235 {
24236 unsigned int bytes_read;
24237 int line, file;
24238
24239 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24240 mac_ptr += bytes_read;
24241 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24242 mac_ptr += bytes_read;
24243
24244 if ((line == 0 && !at_commandline)
24245 || (line != 0 && at_commandline))
24246 complaint (&symfile_complaints,
24247 _("debug info gives source %d included "
24248 "from %s at %s line %d"),
24249 file, at_commandline ? _("command-line") : _("file"),
24250 line == 0 ? _("zero") : _("non-zero"), line);
24251
24252 if (at_commandline)
24253 {
24254 /* This DW_MACRO_start_file was executed in the
24255 pass one. */
24256 at_commandline = 0;
24257 }
24258 else
24259 current_file = macro_start_file (file, line, current_file, lh);
24260 }
24261 break;
24262
24263 case DW_MACRO_end_file:
24264 if (! current_file)
24265 complaint (&symfile_complaints,
24266 _("macro debug info has an unmatched "
24267 "`close_file' directive"));
24268 else
24269 {
24270 current_file = current_file->included_by;
24271 if (! current_file)
24272 {
24273 enum dwarf_macro_record_type next_type;
24274
24275 /* GCC circa March 2002 doesn't produce the zero
24276 type byte marking the end of the compilation
24277 unit. Complain if it's not there, but exit no
24278 matter what. */
24279
24280 /* Do we at least have room for a macinfo type byte? */
24281 if (mac_ptr >= mac_end)
24282 {
24283 dwarf2_section_buffer_overflow_complaint (section);
24284 return;
24285 }
24286
24287 /* We don't increment mac_ptr here, so this is just
24288 a look-ahead. */
24289 next_type
24290 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24291 mac_ptr);
24292 if (next_type != 0)
24293 complaint (&symfile_complaints,
24294 _("no terminating 0-type entry for "
24295 "macros in `.debug_macinfo' section"));
24296
24297 return;
24298 }
24299 }
24300 break;
24301
24302 case DW_MACRO_import:
24303 case DW_MACRO_import_sup:
24304 {
24305 LONGEST offset;
24306 void **slot;
24307 bfd *include_bfd = abfd;
24308 struct dwarf2_section_info *include_section = section;
24309 const gdb_byte *include_mac_end = mac_end;
24310 int is_dwz = section_is_dwz;
24311 const gdb_byte *new_mac_ptr;
24312
24313 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24314 mac_ptr += offset_size;
24315
24316 if (macinfo_type == DW_MACRO_import_sup)
24317 {
24318 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24319
24320 dwarf2_read_section (objfile, &dwz->macro);
24321
24322 include_section = &dwz->macro;
24323 include_bfd = get_section_bfd_owner (include_section);
24324 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24325 is_dwz = 1;
24326 }
24327
24328 new_mac_ptr = include_section->buffer + offset;
24329 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24330
24331 if (*slot != NULL)
24332 {
24333 /* This has actually happened; see
24334 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24335 complaint (&symfile_complaints,
24336 _("recursive DW_MACRO_import in "
24337 ".debug_macro section"));
24338 }
24339 else
24340 {
24341 *slot = (void *) new_mac_ptr;
24342
24343 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24344 include_bfd, new_mac_ptr,
24345 include_mac_end, current_file, lh,
24346 section, section_is_gnu, is_dwz,
24347 offset_size, include_hash);
24348
24349 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24350 }
24351 }
24352 break;
24353
24354 case DW_MACINFO_vendor_ext:
24355 if (!section_is_gnu)
24356 {
24357 unsigned int bytes_read;
24358
24359 /* This reads the constant, but since we don't recognize
24360 any vendor extensions, we ignore it. */
24361 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24362 mac_ptr += bytes_read;
24363 read_direct_string (abfd, mac_ptr, &bytes_read);
24364 mac_ptr += bytes_read;
24365
24366 /* We don't recognize any vendor extensions. */
24367 break;
24368 }
24369 /* FALLTHROUGH */
24370
24371 default:
24372 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24373 mac_ptr, mac_end, abfd, offset_size,
24374 section);
24375 if (mac_ptr == NULL)
24376 return;
24377 break;
24378 }
24379 DIAGNOSTIC_POP
24380 } while (macinfo_type != 0);
24381 }
24382
24383 static void
24384 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24385 int section_is_gnu)
24386 {
24387 struct dwarf2_per_objfile *dwarf2_per_objfile
24388 = cu->per_cu->dwarf2_per_objfile;
24389 struct objfile *objfile = dwarf2_per_objfile->objfile;
24390 struct line_header *lh = cu->line_header;
24391 bfd *abfd;
24392 const gdb_byte *mac_ptr, *mac_end;
24393 struct macro_source_file *current_file = 0;
24394 enum dwarf_macro_record_type macinfo_type;
24395 unsigned int offset_size = cu->header.offset_size;
24396 const gdb_byte *opcode_definitions[256];
24397 void **slot;
24398 struct dwarf2_section_info *section;
24399 const char *section_name;
24400
24401 if (cu->dwo_unit != NULL)
24402 {
24403 if (section_is_gnu)
24404 {
24405 section = &cu->dwo_unit->dwo_file->sections.macro;
24406 section_name = ".debug_macro.dwo";
24407 }
24408 else
24409 {
24410 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24411 section_name = ".debug_macinfo.dwo";
24412 }
24413 }
24414 else
24415 {
24416 if (section_is_gnu)
24417 {
24418 section = &dwarf2_per_objfile->macro;
24419 section_name = ".debug_macro";
24420 }
24421 else
24422 {
24423 section = &dwarf2_per_objfile->macinfo;
24424 section_name = ".debug_macinfo";
24425 }
24426 }
24427
24428 dwarf2_read_section (objfile, section);
24429 if (section->buffer == NULL)
24430 {
24431 complaint (&symfile_complaints, _("missing %s section"), section_name);
24432 return;
24433 }
24434 abfd = get_section_bfd_owner (section);
24435
24436 /* First pass: Find the name of the base filename.
24437 This filename is needed in order to process all macros whose definition
24438 (or undefinition) comes from the command line. These macros are defined
24439 before the first DW_MACINFO_start_file entry, and yet still need to be
24440 associated to the base file.
24441
24442 To determine the base file name, we scan the macro definitions until we
24443 reach the first DW_MACINFO_start_file entry. We then initialize
24444 CURRENT_FILE accordingly so that any macro definition found before the
24445 first DW_MACINFO_start_file can still be associated to the base file. */
24446
24447 mac_ptr = section->buffer + offset;
24448 mac_end = section->buffer + section->size;
24449
24450 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24451 &offset_size, section_is_gnu);
24452 if (mac_ptr == NULL)
24453 {
24454 /* We already issued a complaint. */
24455 return;
24456 }
24457
24458 do
24459 {
24460 /* Do we at least have room for a macinfo type byte? */
24461 if (mac_ptr >= mac_end)
24462 {
24463 /* Complaint is printed during the second pass as GDB will probably
24464 stop the first pass earlier upon finding
24465 DW_MACINFO_start_file. */
24466 break;
24467 }
24468
24469 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24470 mac_ptr++;
24471
24472 /* Note that we rely on the fact that the corresponding GNU and
24473 DWARF constants are the same. */
24474 DIAGNOSTIC_PUSH
24475 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24476 switch (macinfo_type)
24477 {
24478 /* A zero macinfo type indicates the end of the macro
24479 information. */
24480 case 0:
24481 break;
24482
24483 case DW_MACRO_define:
24484 case DW_MACRO_undef:
24485 /* Only skip the data by MAC_PTR. */
24486 {
24487 unsigned int bytes_read;
24488
24489 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24490 mac_ptr += bytes_read;
24491 read_direct_string (abfd, mac_ptr, &bytes_read);
24492 mac_ptr += bytes_read;
24493 }
24494 break;
24495
24496 case DW_MACRO_start_file:
24497 {
24498 unsigned int bytes_read;
24499 int line, file;
24500
24501 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24502 mac_ptr += bytes_read;
24503 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24504 mac_ptr += bytes_read;
24505
24506 current_file = macro_start_file (file, line, current_file, lh);
24507 }
24508 break;
24509
24510 case DW_MACRO_end_file:
24511 /* No data to skip by MAC_PTR. */
24512 break;
24513
24514 case DW_MACRO_define_strp:
24515 case DW_MACRO_undef_strp:
24516 case DW_MACRO_define_sup:
24517 case DW_MACRO_undef_sup:
24518 {
24519 unsigned int bytes_read;
24520
24521 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24522 mac_ptr += bytes_read;
24523 mac_ptr += offset_size;
24524 }
24525 break;
24526
24527 case DW_MACRO_import:
24528 case DW_MACRO_import_sup:
24529 /* Note that, according to the spec, a transparent include
24530 chain cannot call DW_MACRO_start_file. So, we can just
24531 skip this opcode. */
24532 mac_ptr += offset_size;
24533 break;
24534
24535 case DW_MACINFO_vendor_ext:
24536 /* Only skip the data by MAC_PTR. */
24537 if (!section_is_gnu)
24538 {
24539 unsigned int bytes_read;
24540
24541 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24542 mac_ptr += bytes_read;
24543 read_direct_string (abfd, mac_ptr, &bytes_read);
24544 mac_ptr += bytes_read;
24545 }
24546 /* FALLTHROUGH */
24547
24548 default:
24549 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24550 mac_ptr, mac_end, abfd, offset_size,
24551 section);
24552 if (mac_ptr == NULL)
24553 return;
24554 break;
24555 }
24556 DIAGNOSTIC_POP
24557 } while (macinfo_type != 0 && current_file == NULL);
24558
24559 /* Second pass: Process all entries.
24560
24561 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24562 command-line macro definitions/undefinitions. This flag is unset when we
24563 reach the first DW_MACINFO_start_file entry. */
24564
24565 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24566 htab_eq_pointer,
24567 NULL, xcalloc, xfree));
24568 mac_ptr = section->buffer + offset;
24569 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
24570 *slot = (void *) mac_ptr;
24571 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24572 abfd, mac_ptr, mac_end,
24573 current_file, lh, section,
24574 section_is_gnu, 0, offset_size,
24575 include_hash.get ());
24576 }
24577
24578 /* Check if the attribute's form is a DW_FORM_block*
24579 if so return true else false. */
24580
24581 static int
24582 attr_form_is_block (const struct attribute *attr)
24583 {
24584 return (attr == NULL ? 0 :
24585 attr->form == DW_FORM_block1
24586 || attr->form == DW_FORM_block2
24587 || attr->form == DW_FORM_block4
24588 || attr->form == DW_FORM_block
24589 || attr->form == DW_FORM_exprloc);
24590 }
24591
24592 /* Return non-zero if ATTR's value is a section offset --- classes
24593 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24594 You may use DW_UNSND (attr) to retrieve such offsets.
24595
24596 Section 7.5.4, "Attribute Encodings", explains that no attribute
24597 may have a value that belongs to more than one of these classes; it
24598 would be ambiguous if we did, because we use the same forms for all
24599 of them. */
24600
24601 static int
24602 attr_form_is_section_offset (const struct attribute *attr)
24603 {
24604 return (attr->form == DW_FORM_data4
24605 || attr->form == DW_FORM_data8
24606 || attr->form == DW_FORM_sec_offset);
24607 }
24608
24609 /* Return non-zero if ATTR's value falls in the 'constant' class, or
24610 zero otherwise. When this function returns true, you can apply
24611 dwarf2_get_attr_constant_value to it.
24612
24613 However, note that for some attributes you must check
24614 attr_form_is_section_offset before using this test. DW_FORM_data4
24615 and DW_FORM_data8 are members of both the constant class, and of
24616 the classes that contain offsets into other debug sections
24617 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24618 that, if an attribute's can be either a constant or one of the
24619 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
24620 taken as section offsets, not constants.
24621
24622 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24623 cannot handle that. */
24624
24625 static int
24626 attr_form_is_constant (const struct attribute *attr)
24627 {
24628 switch (attr->form)
24629 {
24630 case DW_FORM_sdata:
24631 case DW_FORM_udata:
24632 case DW_FORM_data1:
24633 case DW_FORM_data2:
24634 case DW_FORM_data4:
24635 case DW_FORM_data8:
24636 case DW_FORM_implicit_const:
24637 return 1;
24638 default:
24639 return 0;
24640 }
24641 }
24642
24643
24644 /* DW_ADDR is always stored already as sect_offset; despite for the forms
24645 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24646
24647 static int
24648 attr_form_is_ref (const struct attribute *attr)
24649 {
24650 switch (attr->form)
24651 {
24652 case DW_FORM_ref_addr:
24653 case DW_FORM_ref1:
24654 case DW_FORM_ref2:
24655 case DW_FORM_ref4:
24656 case DW_FORM_ref8:
24657 case DW_FORM_ref_udata:
24658 case DW_FORM_GNU_ref_alt:
24659 return 1;
24660 default:
24661 return 0;
24662 }
24663 }
24664
24665 /* Return the .debug_loc section to use for CU.
24666 For DWO files use .debug_loc.dwo. */
24667
24668 static struct dwarf2_section_info *
24669 cu_debug_loc_section (struct dwarf2_cu *cu)
24670 {
24671 struct dwarf2_per_objfile *dwarf2_per_objfile
24672 = cu->per_cu->dwarf2_per_objfile;
24673
24674 if (cu->dwo_unit)
24675 {
24676 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24677
24678 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24679 }
24680 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24681 : &dwarf2_per_objfile->loc);
24682 }
24683
24684 /* A helper function that fills in a dwarf2_loclist_baton. */
24685
24686 static void
24687 fill_in_loclist_baton (struct dwarf2_cu *cu,
24688 struct dwarf2_loclist_baton *baton,
24689 const struct attribute *attr)
24690 {
24691 struct dwarf2_per_objfile *dwarf2_per_objfile
24692 = cu->per_cu->dwarf2_per_objfile;
24693 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24694
24695 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
24696
24697 baton->per_cu = cu->per_cu;
24698 gdb_assert (baton->per_cu);
24699 /* We don't know how long the location list is, but make sure we
24700 don't run off the edge of the section. */
24701 baton->size = section->size - DW_UNSND (attr);
24702 baton->data = section->buffer + DW_UNSND (attr);
24703 baton->base_address = cu->base_address;
24704 baton->from_dwo = cu->dwo_unit != NULL;
24705 }
24706
24707 static void
24708 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24709 struct dwarf2_cu *cu, int is_block)
24710 {
24711 struct dwarf2_per_objfile *dwarf2_per_objfile
24712 = cu->per_cu->dwarf2_per_objfile;
24713 struct objfile *objfile = dwarf2_per_objfile->objfile;
24714 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24715
24716 if (attr_form_is_section_offset (attr)
24717 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24718 the section. If so, fall through to the complaint in the
24719 other branch. */
24720 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
24721 {
24722 struct dwarf2_loclist_baton *baton;
24723
24724 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24725
24726 fill_in_loclist_baton (cu, baton, attr);
24727
24728 if (cu->base_known == 0)
24729 complaint (&symfile_complaints,
24730 _("Location list used without "
24731 "specifying the CU base address."));
24732
24733 SYMBOL_ACLASS_INDEX (sym) = (is_block
24734 ? dwarf2_loclist_block_index
24735 : dwarf2_loclist_index);
24736 SYMBOL_LOCATION_BATON (sym) = baton;
24737 }
24738 else
24739 {
24740 struct dwarf2_locexpr_baton *baton;
24741
24742 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24743 baton->per_cu = cu->per_cu;
24744 gdb_assert (baton->per_cu);
24745
24746 if (attr_form_is_block (attr))
24747 {
24748 /* Note that we're just copying the block's data pointer
24749 here, not the actual data. We're still pointing into the
24750 info_buffer for SYM's objfile; right now we never release
24751 that buffer, but when we do clean up properly this may
24752 need to change. */
24753 baton->size = DW_BLOCK (attr)->size;
24754 baton->data = DW_BLOCK (attr)->data;
24755 }
24756 else
24757 {
24758 dwarf2_invalid_attrib_class_complaint ("location description",
24759 SYMBOL_NATURAL_NAME (sym));
24760 baton->size = 0;
24761 }
24762
24763 SYMBOL_ACLASS_INDEX (sym) = (is_block
24764 ? dwarf2_locexpr_block_index
24765 : dwarf2_locexpr_index);
24766 SYMBOL_LOCATION_BATON (sym) = baton;
24767 }
24768 }
24769
24770 /* Return the OBJFILE associated with the compilation unit CU. If CU
24771 came from a separate debuginfo file, then the master objfile is
24772 returned. */
24773
24774 struct objfile *
24775 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24776 {
24777 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24778
24779 /* Return the master objfile, so that we can report and look up the
24780 correct file containing this variable. */
24781 if (objfile->separate_debug_objfile_backlink)
24782 objfile = objfile->separate_debug_objfile_backlink;
24783
24784 return objfile;
24785 }
24786
24787 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24788 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24789 CU_HEADERP first. */
24790
24791 static const struct comp_unit_head *
24792 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24793 struct dwarf2_per_cu_data *per_cu)
24794 {
24795 const gdb_byte *info_ptr;
24796
24797 if (per_cu->cu)
24798 return &per_cu->cu->header;
24799
24800 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24801
24802 memset (cu_headerp, 0, sizeof (*cu_headerp));
24803 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24804 rcuh_kind::COMPILE);
24805
24806 return cu_headerp;
24807 }
24808
24809 /* Return the address size given in the compilation unit header for CU. */
24810
24811 int
24812 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24813 {
24814 struct comp_unit_head cu_header_local;
24815 const struct comp_unit_head *cu_headerp;
24816
24817 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24818
24819 return cu_headerp->addr_size;
24820 }
24821
24822 /* Return the offset size given in the compilation unit header for CU. */
24823
24824 int
24825 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24826 {
24827 struct comp_unit_head cu_header_local;
24828 const struct comp_unit_head *cu_headerp;
24829
24830 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24831
24832 return cu_headerp->offset_size;
24833 }
24834
24835 /* See its dwarf2loc.h declaration. */
24836
24837 int
24838 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24839 {
24840 struct comp_unit_head cu_header_local;
24841 const struct comp_unit_head *cu_headerp;
24842
24843 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24844
24845 if (cu_headerp->version == 2)
24846 return cu_headerp->addr_size;
24847 else
24848 return cu_headerp->offset_size;
24849 }
24850
24851 /* Return the text offset of the CU. The returned offset comes from
24852 this CU's objfile. If this objfile came from a separate debuginfo
24853 file, then the offset may be different from the corresponding
24854 offset in the parent objfile. */
24855
24856 CORE_ADDR
24857 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24858 {
24859 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24860
24861 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24862 }
24863
24864 /* Return DWARF version number of PER_CU. */
24865
24866 short
24867 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24868 {
24869 return per_cu->dwarf_version;
24870 }
24871
24872 /* Locate the .debug_info compilation unit from CU's objfile which contains
24873 the DIE at OFFSET. Raises an error on failure. */
24874
24875 static struct dwarf2_per_cu_data *
24876 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24877 unsigned int offset_in_dwz,
24878 struct dwarf2_per_objfile *dwarf2_per_objfile)
24879 {
24880 struct dwarf2_per_cu_data *this_cu;
24881 int low, high;
24882 const sect_offset *cu_off;
24883
24884 low = 0;
24885 high = dwarf2_per_objfile->all_comp_units.size () - 1;
24886 while (high > low)
24887 {
24888 struct dwarf2_per_cu_data *mid_cu;
24889 int mid = low + (high - low) / 2;
24890
24891 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
24892 cu_off = &mid_cu->sect_off;
24893 if (mid_cu->is_dwz > offset_in_dwz
24894 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
24895 high = mid;
24896 else
24897 low = mid + 1;
24898 }
24899 gdb_assert (low == high);
24900 this_cu = dwarf2_per_objfile->all_comp_units[low];
24901 cu_off = &this_cu->sect_off;
24902 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
24903 {
24904 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24905 error (_("Dwarf Error: could not find partial DIE containing "
24906 "offset %s [in module %s]"),
24907 sect_offset_str (sect_off),
24908 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
24909
24910 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24911 <= sect_off);
24912 return dwarf2_per_objfile->all_comp_units[low-1];
24913 }
24914 else
24915 {
24916 this_cu = dwarf2_per_objfile->all_comp_units[low];
24917 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
24918 && sect_off >= this_cu->sect_off + this_cu->length)
24919 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24920 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24921 return this_cu;
24922 }
24923 }
24924
24925 /* Initialize dwarf2_cu CU, owned by PER_CU. */
24926
24927 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24928 : per_cu (per_cu_),
24929 mark (0),
24930 has_loclist (0),
24931 checked_producer (0),
24932 producer_is_gxx_lt_4_6 (0),
24933 producer_is_gcc_lt_4_3 (0),
24934 producer_is_icc_lt_14 (0),
24935 processing_has_namespace_info (0)
24936 {
24937 per_cu->cu = this;
24938 }
24939
24940 /* Destroy a dwarf2_cu. */
24941
24942 dwarf2_cu::~dwarf2_cu ()
24943 {
24944 per_cu->cu = NULL;
24945 }
24946
24947 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24948
24949 static void
24950 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24951 enum language pretend_language)
24952 {
24953 struct attribute *attr;
24954
24955 /* Set the language we're debugging. */
24956 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24957 if (attr)
24958 set_cu_language (DW_UNSND (attr), cu);
24959 else
24960 {
24961 cu->language = pretend_language;
24962 cu->language_defn = language_def (cu->language);
24963 }
24964
24965 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24966 }
24967
24968 /* Increase the age counter on each cached compilation unit, and free
24969 any that are too old. */
24970
24971 static void
24972 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
24973 {
24974 struct dwarf2_per_cu_data *per_cu, **last_chain;
24975
24976 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
24977 per_cu = dwarf2_per_objfile->read_in_chain;
24978 while (per_cu != NULL)
24979 {
24980 per_cu->cu->last_used ++;
24981 if (per_cu->cu->last_used <= dwarf_max_cache_age)
24982 dwarf2_mark (per_cu->cu);
24983 per_cu = per_cu->cu->read_in_chain;
24984 }
24985
24986 per_cu = dwarf2_per_objfile->read_in_chain;
24987 last_chain = &dwarf2_per_objfile->read_in_chain;
24988 while (per_cu != NULL)
24989 {
24990 struct dwarf2_per_cu_data *next_cu;
24991
24992 next_cu = per_cu->cu->read_in_chain;
24993
24994 if (!per_cu->cu->mark)
24995 {
24996 delete per_cu->cu;
24997 *last_chain = next_cu;
24998 }
24999 else
25000 last_chain = &per_cu->cu->read_in_chain;
25001
25002 per_cu = next_cu;
25003 }
25004 }
25005
25006 /* Remove a single compilation unit from the cache. */
25007
25008 static void
25009 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25010 {
25011 struct dwarf2_per_cu_data *per_cu, **last_chain;
25012 struct dwarf2_per_objfile *dwarf2_per_objfile
25013 = target_per_cu->dwarf2_per_objfile;
25014
25015 per_cu = dwarf2_per_objfile->read_in_chain;
25016 last_chain = &dwarf2_per_objfile->read_in_chain;
25017 while (per_cu != NULL)
25018 {
25019 struct dwarf2_per_cu_data *next_cu;
25020
25021 next_cu = per_cu->cu->read_in_chain;
25022
25023 if (per_cu == target_per_cu)
25024 {
25025 delete per_cu->cu;
25026 per_cu->cu = NULL;
25027 *last_chain = next_cu;
25028 break;
25029 }
25030 else
25031 last_chain = &per_cu->cu->read_in_chain;
25032
25033 per_cu = next_cu;
25034 }
25035 }
25036
25037 /* Release all extra memory associated with OBJFILE. */
25038
25039 void
25040 dwarf2_free_objfile (struct objfile *objfile)
25041 {
25042 struct dwarf2_per_objfile *dwarf2_per_objfile
25043 = get_dwarf2_per_objfile (objfile);
25044
25045 delete dwarf2_per_objfile;
25046 }
25047
25048 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25049 We store these in a hash table separate from the DIEs, and preserve them
25050 when the DIEs are flushed out of cache.
25051
25052 The CU "per_cu" pointer is needed because offset alone is not enough to
25053 uniquely identify the type. A file may have multiple .debug_types sections,
25054 or the type may come from a DWO file. Furthermore, while it's more logical
25055 to use per_cu->section+offset, with Fission the section with the data is in
25056 the DWO file but we don't know that section at the point we need it.
25057 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25058 because we can enter the lookup routine, get_die_type_at_offset, from
25059 outside this file, and thus won't necessarily have PER_CU->cu.
25060 Fortunately, PER_CU is stable for the life of the objfile. */
25061
25062 struct dwarf2_per_cu_offset_and_type
25063 {
25064 const struct dwarf2_per_cu_data *per_cu;
25065 sect_offset sect_off;
25066 struct type *type;
25067 };
25068
25069 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25070
25071 static hashval_t
25072 per_cu_offset_and_type_hash (const void *item)
25073 {
25074 const struct dwarf2_per_cu_offset_and_type *ofs
25075 = (const struct dwarf2_per_cu_offset_and_type *) item;
25076
25077 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25078 }
25079
25080 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25081
25082 static int
25083 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25084 {
25085 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25086 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25087 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25088 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25089
25090 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25091 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25092 }
25093
25094 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25095 table if necessary. For convenience, return TYPE.
25096
25097 The DIEs reading must have careful ordering to:
25098 * Not cause infite loops trying to read in DIEs as a prerequisite for
25099 reading current DIE.
25100 * Not trying to dereference contents of still incompletely read in types
25101 while reading in other DIEs.
25102 * Enable referencing still incompletely read in types just by a pointer to
25103 the type without accessing its fields.
25104
25105 Therefore caller should follow these rules:
25106 * Try to fetch any prerequisite types we may need to build this DIE type
25107 before building the type and calling set_die_type.
25108 * After building type call set_die_type for current DIE as soon as
25109 possible before fetching more types to complete the current type.
25110 * Make the type as complete as possible before fetching more types. */
25111
25112 static struct type *
25113 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25114 {
25115 struct dwarf2_per_objfile *dwarf2_per_objfile
25116 = cu->per_cu->dwarf2_per_objfile;
25117 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25118 struct objfile *objfile = dwarf2_per_objfile->objfile;
25119 struct attribute *attr;
25120 struct dynamic_prop prop;
25121
25122 /* For Ada types, make sure that the gnat-specific data is always
25123 initialized (if not already set). There are a few types where
25124 we should not be doing so, because the type-specific area is
25125 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25126 where the type-specific area is used to store the floatformat).
25127 But this is not a problem, because the gnat-specific information
25128 is actually not needed for these types. */
25129 if (need_gnat_info (cu)
25130 && TYPE_CODE (type) != TYPE_CODE_FUNC
25131 && TYPE_CODE (type) != TYPE_CODE_FLT
25132 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25133 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25134 && TYPE_CODE (type) != TYPE_CODE_METHOD
25135 && !HAVE_GNAT_AUX_INFO (type))
25136 INIT_GNAT_SPECIFIC (type);
25137
25138 /* Read DW_AT_allocated and set in type. */
25139 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25140 if (attr_form_is_block (attr))
25141 {
25142 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25143 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25144 }
25145 else if (attr != NULL)
25146 {
25147 complaint (&symfile_complaints,
25148 _("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25149 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25150 sect_offset_str (die->sect_off));
25151 }
25152
25153 /* Read DW_AT_associated and set in type. */
25154 attr = dwarf2_attr (die, DW_AT_associated, cu);
25155 if (attr_form_is_block (attr))
25156 {
25157 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25158 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25159 }
25160 else if (attr != NULL)
25161 {
25162 complaint (&symfile_complaints,
25163 _("DW_AT_associated has the wrong form (%s) at DIE %s"),
25164 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25165 sect_offset_str (die->sect_off));
25166 }
25167
25168 /* Read DW_AT_data_location and set in type. */
25169 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25170 if (attr_to_dynamic_prop (attr, die, cu, &prop))
25171 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25172
25173 if (dwarf2_per_objfile->die_type_hash == NULL)
25174 {
25175 dwarf2_per_objfile->die_type_hash =
25176 htab_create_alloc_ex (127,
25177 per_cu_offset_and_type_hash,
25178 per_cu_offset_and_type_eq,
25179 NULL,
25180 &objfile->objfile_obstack,
25181 hashtab_obstack_allocate,
25182 dummy_obstack_deallocate);
25183 }
25184
25185 ofs.per_cu = cu->per_cu;
25186 ofs.sect_off = die->sect_off;
25187 ofs.type = type;
25188 slot = (struct dwarf2_per_cu_offset_and_type **)
25189 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25190 if (*slot)
25191 complaint (&symfile_complaints,
25192 _("A problem internal to GDB: DIE %s has type already set"),
25193 sect_offset_str (die->sect_off));
25194 *slot = XOBNEW (&objfile->objfile_obstack,
25195 struct dwarf2_per_cu_offset_and_type);
25196 **slot = ofs;
25197 return type;
25198 }
25199
25200 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25201 or return NULL if the die does not have a saved type. */
25202
25203 static struct type *
25204 get_die_type_at_offset (sect_offset sect_off,
25205 struct dwarf2_per_cu_data *per_cu)
25206 {
25207 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25208 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25209
25210 if (dwarf2_per_objfile->die_type_hash == NULL)
25211 return NULL;
25212
25213 ofs.per_cu = per_cu;
25214 ofs.sect_off = sect_off;
25215 slot = ((struct dwarf2_per_cu_offset_and_type *)
25216 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25217 if (slot)
25218 return slot->type;
25219 else
25220 return NULL;
25221 }
25222
25223 /* Look up the type for DIE in CU in die_type_hash,
25224 or return NULL if DIE does not have a saved type. */
25225
25226 static struct type *
25227 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25228 {
25229 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25230 }
25231
25232 /* Add a dependence relationship from CU to REF_PER_CU. */
25233
25234 static void
25235 dwarf2_add_dependence (struct dwarf2_cu *cu,
25236 struct dwarf2_per_cu_data *ref_per_cu)
25237 {
25238 void **slot;
25239
25240 if (cu->dependencies == NULL)
25241 cu->dependencies
25242 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25243 NULL, &cu->comp_unit_obstack,
25244 hashtab_obstack_allocate,
25245 dummy_obstack_deallocate);
25246
25247 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25248 if (*slot == NULL)
25249 *slot = ref_per_cu;
25250 }
25251
25252 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25253 Set the mark field in every compilation unit in the
25254 cache that we must keep because we are keeping CU. */
25255
25256 static int
25257 dwarf2_mark_helper (void **slot, void *data)
25258 {
25259 struct dwarf2_per_cu_data *per_cu;
25260
25261 per_cu = (struct dwarf2_per_cu_data *) *slot;
25262
25263 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25264 reading of the chain. As such dependencies remain valid it is not much
25265 useful to track and undo them during QUIT cleanups. */
25266 if (per_cu->cu == NULL)
25267 return 1;
25268
25269 if (per_cu->cu->mark)
25270 return 1;
25271 per_cu->cu->mark = 1;
25272
25273 if (per_cu->cu->dependencies != NULL)
25274 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25275
25276 return 1;
25277 }
25278
25279 /* Set the mark field in CU and in every other compilation unit in the
25280 cache that we must keep because we are keeping CU. */
25281
25282 static void
25283 dwarf2_mark (struct dwarf2_cu *cu)
25284 {
25285 if (cu->mark)
25286 return;
25287 cu->mark = 1;
25288 if (cu->dependencies != NULL)
25289 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25290 }
25291
25292 static void
25293 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25294 {
25295 while (per_cu)
25296 {
25297 per_cu->cu->mark = 0;
25298 per_cu = per_cu->cu->read_in_chain;
25299 }
25300 }
25301
25302 /* Trivial hash function for partial_die_info: the hash value of a DIE
25303 is its offset in .debug_info for this objfile. */
25304
25305 static hashval_t
25306 partial_die_hash (const void *item)
25307 {
25308 const struct partial_die_info *part_die
25309 = (const struct partial_die_info *) item;
25310
25311 return to_underlying (part_die->sect_off);
25312 }
25313
25314 /* Trivial comparison function for partial_die_info structures: two DIEs
25315 are equal if they have the same offset. */
25316
25317 static int
25318 partial_die_eq (const void *item_lhs, const void *item_rhs)
25319 {
25320 const struct partial_die_info *part_die_lhs
25321 = (const struct partial_die_info *) item_lhs;
25322 const struct partial_die_info *part_die_rhs
25323 = (const struct partial_die_info *) item_rhs;
25324
25325 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25326 }
25327
25328 static struct cmd_list_element *set_dwarf_cmdlist;
25329 static struct cmd_list_element *show_dwarf_cmdlist;
25330
25331 static void
25332 set_dwarf_cmd (const char *args, int from_tty)
25333 {
25334 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25335 gdb_stdout);
25336 }
25337
25338 static void
25339 show_dwarf_cmd (const char *args, int from_tty)
25340 {
25341 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25342 }
25343
25344 int dwarf_always_disassemble;
25345
25346 static void
25347 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25348 struct cmd_list_element *c, const char *value)
25349 {
25350 fprintf_filtered (file,
25351 _("Whether to always disassemble "
25352 "DWARF expressions is %s.\n"),
25353 value);
25354 }
25355
25356 static void
25357 show_check_physname (struct ui_file *file, int from_tty,
25358 struct cmd_list_element *c, const char *value)
25359 {
25360 fprintf_filtered (file,
25361 _("Whether to check \"physname\" is %s.\n"),
25362 value);
25363 }
25364
25365 void
25366 _initialize_dwarf2_read (void)
25367 {
25368
25369 dwarf2_objfile_data_key = register_objfile_data ();
25370
25371 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25372 Set DWARF specific variables.\n\
25373 Configure DWARF variables such as the cache size"),
25374 &set_dwarf_cmdlist, "maintenance set dwarf ",
25375 0/*allow-unknown*/, &maintenance_set_cmdlist);
25376
25377 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25378 Show DWARF specific variables\n\
25379 Show DWARF variables such as the cache size"),
25380 &show_dwarf_cmdlist, "maintenance show dwarf ",
25381 0/*allow-unknown*/, &maintenance_show_cmdlist);
25382
25383 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25384 &dwarf_max_cache_age, _("\
25385 Set the upper bound on the age of cached DWARF compilation units."), _("\
25386 Show the upper bound on the age of cached DWARF compilation units."), _("\
25387 A higher limit means that cached compilation units will be stored\n\
25388 in memory longer, and more total memory will be used. Zero disables\n\
25389 caching, which can slow down startup."),
25390 NULL,
25391 show_dwarf_max_cache_age,
25392 &set_dwarf_cmdlist,
25393 &show_dwarf_cmdlist);
25394
25395 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25396 &dwarf_always_disassemble, _("\
25397 Set whether `info address' always disassembles DWARF expressions."), _("\
25398 Show whether `info address' always disassembles DWARF expressions."), _("\
25399 When enabled, DWARF expressions are always printed in an assembly-like\n\
25400 syntax. When disabled, expressions will be printed in a more\n\
25401 conversational style, when possible."),
25402 NULL,
25403 show_dwarf_always_disassemble,
25404 &set_dwarf_cmdlist,
25405 &show_dwarf_cmdlist);
25406
25407 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25408 Set debugging of the DWARF reader."), _("\
25409 Show debugging of the DWARF reader."), _("\
25410 When enabled (non-zero), debugging messages are printed during DWARF\n\
25411 reading and symtab expansion. A value of 1 (one) provides basic\n\
25412 information. A value greater than 1 provides more verbose information."),
25413 NULL,
25414 NULL,
25415 &setdebuglist, &showdebuglist);
25416
25417 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25418 Set debugging of the DWARF DIE reader."), _("\
25419 Show debugging of the DWARF DIE reader."), _("\
25420 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25421 The value is the maximum depth to print."),
25422 NULL,
25423 NULL,
25424 &setdebuglist, &showdebuglist);
25425
25426 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25427 Set debugging of the dwarf line reader."), _("\
25428 Show debugging of the dwarf line reader."), _("\
25429 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25430 A value of 1 (one) provides basic information.\n\
25431 A value greater than 1 provides more verbose information."),
25432 NULL,
25433 NULL,
25434 &setdebuglist, &showdebuglist);
25435
25436 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25437 Set cross-checking of \"physname\" code against demangler."), _("\
25438 Show cross-checking of \"physname\" code against demangler."), _("\
25439 When enabled, GDB's internal \"physname\" code is checked against\n\
25440 the demangler."),
25441 NULL, show_check_physname,
25442 &setdebuglist, &showdebuglist);
25443
25444 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25445 no_class, &use_deprecated_index_sections, _("\
25446 Set whether to use deprecated gdb_index sections."), _("\
25447 Show whether to use deprecated gdb_index sections."), _("\
25448 When enabled, deprecated .gdb_index sections are used anyway.\n\
25449 Normally they are ignored either because of a missing feature or\n\
25450 performance issue.\n\
25451 Warning: This option must be enabled before gdb reads the file."),
25452 NULL,
25453 NULL,
25454 &setlist, &showlist);
25455
25456 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25457 &dwarf2_locexpr_funcs);
25458 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25459 &dwarf2_loclist_funcs);
25460
25461 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25462 &dwarf2_block_frame_base_locexpr_funcs);
25463 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25464 &dwarf2_block_frame_base_loclist_funcs);
25465
25466 #if GDB_SELF_TEST
25467 selftests::register_test ("dw2_expand_symtabs_matching",
25468 selftests::dw2_expand_symtabs_matching::run_test);
25469 #endif
25470 }
This page took 0.519448 seconds and 3 git commands to generate.