NEWS and documentation for $_gdb_setting and $_gdb_setting_str.
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2019 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-cache.h"
34 #include "dwarf-index-common.h"
35 #include "bfd.h"
36 #include "elf-bfd.h"
37 #include "symtab.h"
38 #include "gdbtypes.h"
39 #include "objfiles.h"
40 #include "dwarf2.h"
41 #include "buildsym.h"
42 #include "demangle.h"
43 #include "gdb-demangle.h"
44 #include "filenames.h" /* for DOSish file names */
45 #include "macrotab.h"
46 #include "language.h"
47 #include "complaints.h"
48 #include "dwarf2expr.h"
49 #include "dwarf2loc.h"
50 #include "cp-support.h"
51 #include "hashtab.h"
52 #include "command.h"
53 #include "gdbcmd.h"
54 #include "block.h"
55 #include "addrmap.h"
56 #include "typeprint.h"
57 #include "psympriv.h"
58 #include "c-lang.h"
59 #include "go-lang.h"
60 #include "valprint.h"
61 #include "gdbcore.h" /* for gnutarget */
62 #include "gdb/gdb-index.h"
63 #include "gdb_bfd.h"
64 #include "f-lang.h"
65 #include "source.h"
66 #include "build-id.h"
67 #include "namespace.h"
68 #include "gdbsupport/function-view.h"
69 #include "gdbsupport/gdb_optional.h"
70 #include "gdbsupport/underlying.h"
71 #include "gdbsupport/hash_enum.h"
72 #include "filename-seen-cache.h"
73 #include "producer.h"
74 #include <fcntl.h>
75 #include <algorithm>
76 #include <unordered_map>
77 #include "gdbsupport/selftest.h"
78 #include "rust-lang.h"
79 #include "gdbsupport/pathstuff.h"
80
81 /* When == 1, print basic high level tracing messages.
82 When > 1, be more verbose.
83 This is in contrast to the low level DIE reading of dwarf_die_debug. */
84 static unsigned int dwarf_read_debug = 0;
85
86 /* When non-zero, dump DIEs after they are read in. */
87 static unsigned int dwarf_die_debug = 0;
88
89 /* When non-zero, dump line number entries as they are read in. */
90 static unsigned int dwarf_line_debug = 0;
91
92 /* When true, cross-check physname against demangler. */
93 static bool check_physname = false;
94
95 /* When true, do not reject deprecated .gdb_index sections. */
96 static bool use_deprecated_index_sections = false;
97
98 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
99
100 /* The "aclass" indices for various kinds of computed DWARF symbols. */
101
102 static int dwarf2_locexpr_index;
103 static int dwarf2_loclist_index;
104 static int dwarf2_locexpr_block_index;
105 static int dwarf2_loclist_block_index;
106
107 /* An index into a (C++) symbol name component in a symbol name as
108 recorded in the mapped_index's symbol table. For each C++ symbol
109 in the symbol table, we record one entry for the start of each
110 component in the symbol in a table of name components, and then
111 sort the table, in order to be able to binary search symbol names,
112 ignoring leading namespaces, both completion and regular look up.
113 For example, for symbol "A::B::C", we'll have an entry that points
114 to "A::B::C", another that points to "B::C", and another for "C".
115 Note that function symbols in GDB index have no parameter
116 information, just the function/method names. You can convert a
117 name_component to a "const char *" using the
118 'mapped_index::symbol_name_at(offset_type)' method. */
119
120 struct name_component
121 {
122 /* Offset in the symbol name where the component starts. Stored as
123 a (32-bit) offset instead of a pointer to save memory and improve
124 locality on 64-bit architectures. */
125 offset_type name_offset;
126
127 /* The symbol's index in the symbol and constant pool tables of a
128 mapped_index. */
129 offset_type idx;
130 };
131
132 /* Base class containing bits shared by both .gdb_index and
133 .debug_name indexes. */
134
135 struct mapped_index_base
136 {
137 mapped_index_base () = default;
138 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
139
140 /* The name_component table (a sorted vector). See name_component's
141 description above. */
142 std::vector<name_component> name_components;
143
144 /* How NAME_COMPONENTS is sorted. */
145 enum case_sensitivity name_components_casing;
146
147 /* Return the number of names in the symbol table. */
148 virtual size_t symbol_name_count () const = 0;
149
150 /* Get the name of the symbol at IDX in the symbol table. */
151 virtual const char *symbol_name_at (offset_type idx) const = 0;
152
153 /* Return whether the name at IDX in the symbol table should be
154 ignored. */
155 virtual bool symbol_name_slot_invalid (offset_type idx) const
156 {
157 return false;
158 }
159
160 /* Build the symbol name component sorted vector, if we haven't
161 yet. */
162 void build_name_components ();
163
164 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
165 possible matches for LN_NO_PARAMS in the name component
166 vector. */
167 std::pair<std::vector<name_component>::const_iterator,
168 std::vector<name_component>::const_iterator>
169 find_name_components_bounds (const lookup_name_info &ln_no_params,
170 enum language lang) const;
171
172 /* Prevent deleting/destroying via a base class pointer. */
173 protected:
174 ~mapped_index_base() = default;
175 };
176
177 /* A description of the mapped index. The file format is described in
178 a comment by the code that writes the index. */
179 struct mapped_index final : public mapped_index_base
180 {
181 /* A slot/bucket in the symbol table hash. */
182 struct symbol_table_slot
183 {
184 const offset_type name;
185 const offset_type vec;
186 };
187
188 /* Index data format version. */
189 int version = 0;
190
191 /* The address table data. */
192 gdb::array_view<const gdb_byte> address_table;
193
194 /* The symbol table, implemented as a hash table. */
195 gdb::array_view<symbol_table_slot> symbol_table;
196
197 /* A pointer to the constant pool. */
198 const char *constant_pool = nullptr;
199
200 bool symbol_name_slot_invalid (offset_type idx) const override
201 {
202 const auto &bucket = this->symbol_table[idx];
203 return bucket.name == 0 && bucket.vec == 0;
204 }
205
206 /* Convenience method to get at the name of the symbol at IDX in the
207 symbol table. */
208 const char *symbol_name_at (offset_type idx) const override
209 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
210
211 size_t symbol_name_count () const override
212 { return this->symbol_table.size (); }
213 };
214
215 /* A description of the mapped .debug_names.
216 Uninitialized map has CU_COUNT 0. */
217 struct mapped_debug_names final : public mapped_index_base
218 {
219 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
220 : dwarf2_per_objfile (dwarf2_per_objfile_)
221 {}
222
223 struct dwarf2_per_objfile *dwarf2_per_objfile;
224 bfd_endian dwarf5_byte_order;
225 bool dwarf5_is_dwarf64;
226 bool augmentation_is_gdb;
227 uint8_t offset_size;
228 uint32_t cu_count = 0;
229 uint32_t tu_count, bucket_count, name_count;
230 const gdb_byte *cu_table_reordered, *tu_table_reordered;
231 const uint32_t *bucket_table_reordered, *hash_table_reordered;
232 const gdb_byte *name_table_string_offs_reordered;
233 const gdb_byte *name_table_entry_offs_reordered;
234 const gdb_byte *entry_pool;
235
236 struct index_val
237 {
238 ULONGEST dwarf_tag;
239 struct attr
240 {
241 /* Attribute name DW_IDX_*. */
242 ULONGEST dw_idx;
243
244 /* Attribute form DW_FORM_*. */
245 ULONGEST form;
246
247 /* Value if FORM is DW_FORM_implicit_const. */
248 LONGEST implicit_const;
249 };
250 std::vector<attr> attr_vec;
251 };
252
253 std::unordered_map<ULONGEST, index_val> abbrev_map;
254
255 const char *namei_to_name (uint32_t namei) const;
256
257 /* Implementation of the mapped_index_base virtual interface, for
258 the name_components cache. */
259
260 const char *symbol_name_at (offset_type idx) const override
261 { return namei_to_name (idx); }
262
263 size_t symbol_name_count () const override
264 { return this->name_count; }
265 };
266
267 /* See dwarf2read.h. */
268
269 dwarf2_per_objfile *
270 get_dwarf2_per_objfile (struct objfile *objfile)
271 {
272 return dwarf2_objfile_data_key.get (objfile);
273 }
274
275 /* Default names of the debugging sections. */
276
277 /* Note that if the debugging section has been compressed, it might
278 have a name like .zdebug_info. */
279
280 static const struct dwarf2_debug_sections dwarf2_elf_names =
281 {
282 { ".debug_info", ".zdebug_info" },
283 { ".debug_abbrev", ".zdebug_abbrev" },
284 { ".debug_line", ".zdebug_line" },
285 { ".debug_loc", ".zdebug_loc" },
286 { ".debug_loclists", ".zdebug_loclists" },
287 { ".debug_macinfo", ".zdebug_macinfo" },
288 { ".debug_macro", ".zdebug_macro" },
289 { ".debug_str", ".zdebug_str" },
290 { ".debug_line_str", ".zdebug_line_str" },
291 { ".debug_ranges", ".zdebug_ranges" },
292 { ".debug_rnglists", ".zdebug_rnglists" },
293 { ".debug_types", ".zdebug_types" },
294 { ".debug_addr", ".zdebug_addr" },
295 { ".debug_frame", ".zdebug_frame" },
296 { ".eh_frame", NULL },
297 { ".gdb_index", ".zgdb_index" },
298 { ".debug_names", ".zdebug_names" },
299 { ".debug_aranges", ".zdebug_aranges" },
300 23
301 };
302
303 /* List of DWO/DWP sections. */
304
305 static const struct dwop_section_names
306 {
307 struct dwarf2_section_names abbrev_dwo;
308 struct dwarf2_section_names info_dwo;
309 struct dwarf2_section_names line_dwo;
310 struct dwarf2_section_names loc_dwo;
311 struct dwarf2_section_names loclists_dwo;
312 struct dwarf2_section_names macinfo_dwo;
313 struct dwarf2_section_names macro_dwo;
314 struct dwarf2_section_names str_dwo;
315 struct dwarf2_section_names str_offsets_dwo;
316 struct dwarf2_section_names types_dwo;
317 struct dwarf2_section_names cu_index;
318 struct dwarf2_section_names tu_index;
319 }
320 dwop_section_names =
321 {
322 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
323 { ".debug_info.dwo", ".zdebug_info.dwo" },
324 { ".debug_line.dwo", ".zdebug_line.dwo" },
325 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
326 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
327 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
328 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
329 { ".debug_str.dwo", ".zdebug_str.dwo" },
330 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
331 { ".debug_types.dwo", ".zdebug_types.dwo" },
332 { ".debug_cu_index", ".zdebug_cu_index" },
333 { ".debug_tu_index", ".zdebug_tu_index" },
334 };
335
336 /* local data types */
337
338 /* The data in a compilation unit header, after target2host
339 translation, looks like this. */
340 struct comp_unit_head
341 {
342 unsigned int length;
343 short version;
344 unsigned char addr_size;
345 unsigned char signed_addr_p;
346 sect_offset abbrev_sect_off;
347
348 /* Size of file offsets; either 4 or 8. */
349 unsigned int offset_size;
350
351 /* Size of the length field; either 4 or 12. */
352 unsigned int initial_length_size;
353
354 enum dwarf_unit_type unit_type;
355
356 /* Offset to the first byte of this compilation unit header in the
357 .debug_info section, for resolving relative reference dies. */
358 sect_offset sect_off;
359
360 /* Offset to first die in this cu from the start of the cu.
361 This will be the first byte following the compilation unit header. */
362 cu_offset first_die_cu_offset;
363
364
365 /* 64-bit signature of this unit. For type units, it denotes the signature of
366 the type (DW_UT_type in DWARF 4, additionally DW_UT_split_type in DWARF 5).
367 Also used in DWARF 5, to denote the dwo id when the unit type is
368 DW_UT_skeleton or DW_UT_split_compile. */
369 ULONGEST signature;
370
371 /* For types, offset in the type's DIE of the type defined by this TU. */
372 cu_offset type_cu_offset_in_tu;
373 };
374
375 /* Type used for delaying computation of method physnames.
376 See comments for compute_delayed_physnames. */
377 struct delayed_method_info
378 {
379 /* The type to which the method is attached, i.e., its parent class. */
380 struct type *type;
381
382 /* The index of the method in the type's function fieldlists. */
383 int fnfield_index;
384
385 /* The index of the method in the fieldlist. */
386 int index;
387
388 /* The name of the DIE. */
389 const char *name;
390
391 /* The DIE associated with this method. */
392 struct die_info *die;
393 };
394
395 /* Internal state when decoding a particular compilation unit. */
396 struct dwarf2_cu
397 {
398 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
399 ~dwarf2_cu ();
400
401 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
402
403 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
404 Create the set of symtabs used by this TU, or if this TU is sharing
405 symtabs with another TU and the symtabs have already been created
406 then restore those symtabs in the line header.
407 We don't need the pc/line-number mapping for type units. */
408 void setup_type_unit_groups (struct die_info *die);
409
410 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
411 buildsym_compunit constructor. */
412 struct compunit_symtab *start_symtab (const char *name,
413 const char *comp_dir,
414 CORE_ADDR low_pc);
415
416 /* Reset the builder. */
417 void reset_builder () { m_builder.reset (); }
418
419 /* The header of the compilation unit. */
420 struct comp_unit_head header {};
421
422 /* Base address of this compilation unit. */
423 CORE_ADDR base_address = 0;
424
425 /* Non-zero if base_address has been set. */
426 int base_known = 0;
427
428 /* The language we are debugging. */
429 enum language language = language_unknown;
430 const struct language_defn *language_defn = nullptr;
431
432 const char *producer = nullptr;
433
434 private:
435 /* The symtab builder for this CU. This is only non-NULL when full
436 symbols are being read. */
437 std::unique_ptr<buildsym_compunit> m_builder;
438
439 public:
440 /* The generic symbol table building routines have separate lists for
441 file scope symbols and all all other scopes (local scopes). So
442 we need to select the right one to pass to add_symbol_to_list().
443 We do it by keeping a pointer to the correct list in list_in_scope.
444
445 FIXME: The original dwarf code just treated the file scope as the
446 first local scope, and all other local scopes as nested local
447 scopes, and worked fine. Check to see if we really need to
448 distinguish these in buildsym.c. */
449 struct pending **list_in_scope = nullptr;
450
451 /* Hash table holding all the loaded partial DIEs
452 with partial_die->offset.SECT_OFF as hash. */
453 htab_t partial_dies = nullptr;
454
455 /* Storage for things with the same lifetime as this read-in compilation
456 unit, including partial DIEs. */
457 auto_obstack comp_unit_obstack;
458
459 /* When multiple dwarf2_cu structures are living in memory, this field
460 chains them all together, so that they can be released efficiently.
461 We will probably also want a generation counter so that most-recently-used
462 compilation units are cached... */
463 struct dwarf2_per_cu_data *read_in_chain = nullptr;
464
465 /* Backlink to our per_cu entry. */
466 struct dwarf2_per_cu_data *per_cu;
467
468 /* How many compilation units ago was this CU last referenced? */
469 int last_used = 0;
470
471 /* A hash table of DIE cu_offset for following references with
472 die_info->offset.sect_off as hash. */
473 htab_t die_hash = nullptr;
474
475 /* Full DIEs if read in. */
476 struct die_info *dies = nullptr;
477
478 /* A set of pointers to dwarf2_per_cu_data objects for compilation
479 units referenced by this one. Only set during full symbol processing;
480 partial symbol tables do not have dependencies. */
481 htab_t dependencies = nullptr;
482
483 /* Header data from the line table, during full symbol processing. */
484 struct line_header *line_header = nullptr;
485 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
486 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
487 this is the DW_TAG_compile_unit die for this CU. We'll hold on
488 to the line header as long as this DIE is being processed. See
489 process_die_scope. */
490 die_info *line_header_die_owner = nullptr;
491
492 /* A list of methods which need to have physnames computed
493 after all type information has been read. */
494 std::vector<delayed_method_info> method_list;
495
496 /* To be copied to symtab->call_site_htab. */
497 htab_t call_site_htab = nullptr;
498
499 /* Non-NULL if this CU came from a DWO file.
500 There is an invariant here that is important to remember:
501 Except for attributes copied from the top level DIE in the "main"
502 (or "stub") file in preparation for reading the DWO file
503 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
504 Either there isn't a DWO file (in which case this is NULL and the point
505 is moot), or there is and either we're not going to read it (in which
506 case this is NULL) or there is and we are reading it (in which case this
507 is non-NULL). */
508 struct dwo_unit *dwo_unit = nullptr;
509
510 /* The DW_AT_addr_base attribute if present, zero otherwise
511 (zero is a valid value though).
512 Note this value comes from the Fission stub CU/TU's DIE. */
513 ULONGEST addr_base = 0;
514
515 /* The DW_AT_ranges_base attribute if present, zero otherwise
516 (zero is a valid value though).
517 Note this value comes from the Fission stub CU/TU's DIE.
518 Also note that the value is zero in the non-DWO case so this value can
519 be used without needing to know whether DWO files are in use or not.
520 N.B. This does not apply to DW_AT_ranges appearing in
521 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
522 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
523 DW_AT_ranges_base *would* have to be applied, and we'd have to care
524 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
525 ULONGEST ranges_base = 0;
526
527 /* When reading debug info generated by older versions of rustc, we
528 have to rewrite some union types to be struct types with a
529 variant part. This rewriting must be done after the CU is fully
530 read in, because otherwise at the point of rewriting some struct
531 type might not have been fully processed. So, we keep a list of
532 all such types here and process them after expansion. */
533 std::vector<struct type *> rust_unions;
534
535 /* Mark used when releasing cached dies. */
536 bool mark : 1;
537
538 /* This CU references .debug_loc. See the symtab->locations_valid field.
539 This test is imperfect as there may exist optimized debug code not using
540 any location list and still facing inlining issues if handled as
541 unoptimized code. For a future better test see GCC PR other/32998. */
542 bool has_loclist : 1;
543
544 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
545 if all the producer_is_* fields are valid. This information is cached
546 because profiling CU expansion showed excessive time spent in
547 producer_is_gxx_lt_4_6. */
548 bool checked_producer : 1;
549 bool producer_is_gxx_lt_4_6 : 1;
550 bool producer_is_gcc_lt_4_3 : 1;
551 bool producer_is_icc : 1;
552 bool producer_is_icc_lt_14 : 1;
553 bool producer_is_codewarrior : 1;
554
555 /* When true, the file that we're processing is known to have
556 debugging info for C++ namespaces. GCC 3.3.x did not produce
557 this information, but later versions do. */
558
559 bool processing_has_namespace_info : 1;
560
561 struct partial_die_info *find_partial_die (sect_offset sect_off);
562
563 /* If this CU was inherited by another CU (via specification,
564 abstract_origin, etc), this is the ancestor CU. */
565 dwarf2_cu *ancestor;
566
567 /* Get the buildsym_compunit for this CU. */
568 buildsym_compunit *get_builder ()
569 {
570 /* If this CU has a builder associated with it, use that. */
571 if (m_builder != nullptr)
572 return m_builder.get ();
573
574 /* Otherwise, search ancestors for a valid builder. */
575 if (ancestor != nullptr)
576 return ancestor->get_builder ();
577
578 return nullptr;
579 }
580 };
581
582 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
583 This includes type_unit_group and quick_file_names. */
584
585 struct stmt_list_hash
586 {
587 /* The DWO unit this table is from or NULL if there is none. */
588 struct dwo_unit *dwo_unit;
589
590 /* Offset in .debug_line or .debug_line.dwo. */
591 sect_offset line_sect_off;
592 };
593
594 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
595 an object of this type. */
596
597 struct type_unit_group
598 {
599 /* dwarf2read.c's main "handle" on a TU symtab.
600 To simplify things we create an artificial CU that "includes" all the
601 type units using this stmt_list so that the rest of the code still has
602 a "per_cu" handle on the symtab.
603 This PER_CU is recognized by having no section. */
604 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
605 struct dwarf2_per_cu_data per_cu;
606
607 /* The TUs that share this DW_AT_stmt_list entry.
608 This is added to while parsing type units to build partial symtabs,
609 and is deleted afterwards and not used again. */
610 std::vector<signatured_type *> *tus;
611
612 /* The compunit symtab.
613 Type units in a group needn't all be defined in the same source file,
614 so we create an essentially anonymous symtab as the compunit symtab. */
615 struct compunit_symtab *compunit_symtab;
616
617 /* The data used to construct the hash key. */
618 struct stmt_list_hash hash;
619
620 /* The number of symtabs from the line header.
621 The value here must match line_header.num_file_names. */
622 unsigned int num_symtabs;
623
624 /* The symbol tables for this TU (obtained from the files listed in
625 DW_AT_stmt_list).
626 WARNING: The order of entries here must match the order of entries
627 in the line header. After the first TU using this type_unit_group, the
628 line header for the subsequent TUs is recreated from this. This is done
629 because we need to use the same symtabs for each TU using the same
630 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
631 there's no guarantee the line header doesn't have duplicate entries. */
632 struct symtab **symtabs;
633 };
634
635 /* These sections are what may appear in a (real or virtual) DWO file. */
636
637 struct dwo_sections
638 {
639 struct dwarf2_section_info abbrev;
640 struct dwarf2_section_info line;
641 struct dwarf2_section_info loc;
642 struct dwarf2_section_info loclists;
643 struct dwarf2_section_info macinfo;
644 struct dwarf2_section_info macro;
645 struct dwarf2_section_info str;
646 struct dwarf2_section_info str_offsets;
647 /* In the case of a virtual DWO file, these two are unused. */
648 struct dwarf2_section_info info;
649 std::vector<dwarf2_section_info> types;
650 };
651
652 /* CUs/TUs in DWP/DWO files. */
653
654 struct dwo_unit
655 {
656 /* Backlink to the containing struct dwo_file. */
657 struct dwo_file *dwo_file;
658
659 /* The "id" that distinguishes this CU/TU.
660 .debug_info calls this "dwo_id", .debug_types calls this "signature".
661 Since signatures came first, we stick with it for consistency. */
662 ULONGEST signature;
663
664 /* The section this CU/TU lives in, in the DWO file. */
665 struct dwarf2_section_info *section;
666
667 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
668 sect_offset sect_off;
669 unsigned int length;
670
671 /* For types, offset in the type's DIE of the type defined by this TU. */
672 cu_offset type_offset_in_tu;
673 };
674
675 /* include/dwarf2.h defines the DWP section codes.
676 It defines a max value but it doesn't define a min value, which we
677 use for error checking, so provide one. */
678
679 enum dwp_v2_section_ids
680 {
681 DW_SECT_MIN = 1
682 };
683
684 /* Data for one DWO file.
685
686 This includes virtual DWO files (a virtual DWO file is a DWO file as it
687 appears in a DWP file). DWP files don't really have DWO files per se -
688 comdat folding of types "loses" the DWO file they came from, and from
689 a high level view DWP files appear to contain a mass of random types.
690 However, to maintain consistency with the non-DWP case we pretend DWP
691 files contain virtual DWO files, and we assign each TU with one virtual
692 DWO file (generally based on the line and abbrev section offsets -
693 a heuristic that seems to work in practice). */
694
695 struct dwo_file
696 {
697 dwo_file () = default;
698 DISABLE_COPY_AND_ASSIGN (dwo_file);
699
700 /* The DW_AT_GNU_dwo_name attribute.
701 For virtual DWO files the name is constructed from the section offsets
702 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
703 from related CU+TUs. */
704 const char *dwo_name = nullptr;
705
706 /* The DW_AT_comp_dir attribute. */
707 const char *comp_dir = nullptr;
708
709 /* The bfd, when the file is open. Otherwise this is NULL.
710 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
711 gdb_bfd_ref_ptr dbfd;
712
713 /* The sections that make up this DWO file.
714 Remember that for virtual DWO files in DWP V2, these are virtual
715 sections (for lack of a better name). */
716 struct dwo_sections sections {};
717
718 /* The CUs in the file.
719 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
720 an extension to handle LLVM's Link Time Optimization output (where
721 multiple source files may be compiled into a single object/dwo pair). */
722 htab_t cus {};
723
724 /* Table of TUs in the file.
725 Each element is a struct dwo_unit. */
726 htab_t tus {};
727 };
728
729 /* These sections are what may appear in a DWP file. */
730
731 struct dwp_sections
732 {
733 /* These are used by both DWP version 1 and 2. */
734 struct dwarf2_section_info str;
735 struct dwarf2_section_info cu_index;
736 struct dwarf2_section_info tu_index;
737
738 /* These are only used by DWP version 2 files.
739 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
740 sections are referenced by section number, and are not recorded here.
741 In DWP version 2 there is at most one copy of all these sections, each
742 section being (effectively) comprised of the concatenation of all of the
743 individual sections that exist in the version 1 format.
744 To keep the code simple we treat each of these concatenated pieces as a
745 section itself (a virtual section?). */
746 struct dwarf2_section_info abbrev;
747 struct dwarf2_section_info info;
748 struct dwarf2_section_info line;
749 struct dwarf2_section_info loc;
750 struct dwarf2_section_info macinfo;
751 struct dwarf2_section_info macro;
752 struct dwarf2_section_info str_offsets;
753 struct dwarf2_section_info types;
754 };
755
756 /* These sections are what may appear in a virtual DWO file in DWP version 1.
757 A virtual DWO file is a DWO file as it appears in a DWP file. */
758
759 struct virtual_v1_dwo_sections
760 {
761 struct dwarf2_section_info abbrev;
762 struct dwarf2_section_info line;
763 struct dwarf2_section_info loc;
764 struct dwarf2_section_info macinfo;
765 struct dwarf2_section_info macro;
766 struct dwarf2_section_info str_offsets;
767 /* Each DWP hash table entry records one CU or one TU.
768 That is recorded here, and copied to dwo_unit.section. */
769 struct dwarf2_section_info info_or_types;
770 };
771
772 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
773 In version 2, the sections of the DWO files are concatenated together
774 and stored in one section of that name. Thus each ELF section contains
775 several "virtual" sections. */
776
777 struct virtual_v2_dwo_sections
778 {
779 bfd_size_type abbrev_offset;
780 bfd_size_type abbrev_size;
781
782 bfd_size_type line_offset;
783 bfd_size_type line_size;
784
785 bfd_size_type loc_offset;
786 bfd_size_type loc_size;
787
788 bfd_size_type macinfo_offset;
789 bfd_size_type macinfo_size;
790
791 bfd_size_type macro_offset;
792 bfd_size_type macro_size;
793
794 bfd_size_type str_offsets_offset;
795 bfd_size_type str_offsets_size;
796
797 /* Each DWP hash table entry records one CU or one TU.
798 That is recorded here, and copied to dwo_unit.section. */
799 bfd_size_type info_or_types_offset;
800 bfd_size_type info_or_types_size;
801 };
802
803 /* Contents of DWP hash tables. */
804
805 struct dwp_hash_table
806 {
807 uint32_t version, nr_columns;
808 uint32_t nr_units, nr_slots;
809 const gdb_byte *hash_table, *unit_table;
810 union
811 {
812 struct
813 {
814 const gdb_byte *indices;
815 } v1;
816 struct
817 {
818 /* This is indexed by column number and gives the id of the section
819 in that column. */
820 #define MAX_NR_V2_DWO_SECTIONS \
821 (1 /* .debug_info or .debug_types */ \
822 + 1 /* .debug_abbrev */ \
823 + 1 /* .debug_line */ \
824 + 1 /* .debug_loc */ \
825 + 1 /* .debug_str_offsets */ \
826 + 1 /* .debug_macro or .debug_macinfo */)
827 int section_ids[MAX_NR_V2_DWO_SECTIONS];
828 const gdb_byte *offsets;
829 const gdb_byte *sizes;
830 } v2;
831 } section_pool;
832 };
833
834 /* Data for one DWP file. */
835
836 struct dwp_file
837 {
838 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
839 : name (name_),
840 dbfd (std::move (abfd))
841 {
842 }
843
844 /* Name of the file. */
845 const char *name;
846
847 /* File format version. */
848 int version = 0;
849
850 /* The bfd. */
851 gdb_bfd_ref_ptr dbfd;
852
853 /* Section info for this file. */
854 struct dwp_sections sections {};
855
856 /* Table of CUs in the file. */
857 const struct dwp_hash_table *cus = nullptr;
858
859 /* Table of TUs in the file. */
860 const struct dwp_hash_table *tus = nullptr;
861
862 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
863 htab_t loaded_cus {};
864 htab_t loaded_tus {};
865
866 /* Table to map ELF section numbers to their sections.
867 This is only needed for the DWP V1 file format. */
868 unsigned int num_sections = 0;
869 asection **elf_sections = nullptr;
870 };
871
872 /* Struct used to pass misc. parameters to read_die_and_children, et
873 al. which are used for both .debug_info and .debug_types dies.
874 All parameters here are unchanging for the life of the call. This
875 struct exists to abstract away the constant parameters of die reading. */
876
877 struct die_reader_specs
878 {
879 /* The bfd of die_section. */
880 bfd* abfd;
881
882 /* The CU of the DIE we are parsing. */
883 struct dwarf2_cu *cu;
884
885 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
886 struct dwo_file *dwo_file;
887
888 /* The section the die comes from.
889 This is either .debug_info or .debug_types, or the .dwo variants. */
890 struct dwarf2_section_info *die_section;
891
892 /* die_section->buffer. */
893 const gdb_byte *buffer;
894
895 /* The end of the buffer. */
896 const gdb_byte *buffer_end;
897
898 /* The value of the DW_AT_comp_dir attribute. */
899 const char *comp_dir;
900
901 /* The abbreviation table to use when reading the DIEs. */
902 struct abbrev_table *abbrev_table;
903 };
904
905 /* Type of function passed to init_cutu_and_read_dies, et.al. */
906 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
907 const gdb_byte *info_ptr,
908 struct die_info *comp_unit_die,
909 int has_children,
910 void *data);
911
912 /* dir_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5 and
913 later. */
914 typedef int dir_index;
915
916 /* file_name_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5
917 and later. */
918 typedef int file_name_index;
919
920 struct file_entry
921 {
922 file_entry () = default;
923
924 file_entry (const char *name_, dir_index d_index_,
925 unsigned int mod_time_, unsigned int length_)
926 : name (name_),
927 d_index (d_index_),
928 mod_time (mod_time_),
929 length (length_)
930 {}
931
932 /* Return the include directory at D_INDEX stored in LH. Returns
933 NULL if D_INDEX is out of bounds. */
934 const char *include_dir (const line_header *lh) const;
935
936 /* The file name. Note this is an observing pointer. The memory is
937 owned by debug_line_buffer. */
938 const char *name {};
939
940 /* The directory index (1-based). */
941 dir_index d_index {};
942
943 unsigned int mod_time {};
944
945 unsigned int length {};
946
947 /* True if referenced by the Line Number Program. */
948 bool included_p {};
949
950 /* The associated symbol table, if any. */
951 struct symtab *symtab {};
952 };
953
954 /* The line number information for a compilation unit (found in the
955 .debug_line section) begins with a "statement program header",
956 which contains the following information. */
957 struct line_header
958 {
959 line_header ()
960 : offset_in_dwz {}
961 {}
962
963 /* Add an entry to the include directory table. */
964 void add_include_dir (const char *include_dir);
965
966 /* Add an entry to the file name table. */
967 void add_file_name (const char *name, dir_index d_index,
968 unsigned int mod_time, unsigned int length);
969
970 /* Return the include dir at INDEX (0-based in DWARF 5 and 1-based before).
971 Returns NULL if INDEX is out of bounds. */
972 const char *include_dir_at (dir_index index) const
973 {
974 int vec_index;
975 if (version >= 5)
976 vec_index = index;
977 else
978 vec_index = index - 1;
979 if (vec_index < 0 || vec_index >= m_include_dirs.size ())
980 return NULL;
981 return m_include_dirs[vec_index];
982 }
983
984 bool is_valid_file_index (int file_index)
985 {
986 if (version >= 5)
987 return 0 <= file_index && file_index < file_names_size ();
988 return 1 <= file_index && file_index <= file_names_size ();
989 }
990
991 /* Return the file name at INDEX (0-based in DWARF 5 and 1-based before).
992 Returns NULL if INDEX is out of bounds. */
993 file_entry *file_name_at (file_name_index index)
994 {
995 int vec_index;
996 if (version >= 5)
997 vec_index = index;
998 else
999 vec_index = index - 1;
1000 if (vec_index < 0 || vec_index >= m_file_names.size ())
1001 return NULL;
1002 return &m_file_names[vec_index];
1003 }
1004
1005 /* The indexes are 0-based in DWARF 5 and 1-based in DWARF 4. Therefore,
1006 this method should only be used to iterate through all file entries in an
1007 index-agnostic manner. */
1008 std::vector<file_entry> &file_names ()
1009 { return m_file_names; }
1010
1011 /* Offset of line number information in .debug_line section. */
1012 sect_offset sect_off {};
1013
1014 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1015 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1016
1017 unsigned int total_length {};
1018 unsigned short version {};
1019 unsigned int header_length {};
1020 unsigned char minimum_instruction_length {};
1021 unsigned char maximum_ops_per_instruction {};
1022 unsigned char default_is_stmt {};
1023 int line_base {};
1024 unsigned char line_range {};
1025 unsigned char opcode_base {};
1026
1027 /* standard_opcode_lengths[i] is the number of operands for the
1028 standard opcode whose value is i. This means that
1029 standard_opcode_lengths[0] is unused, and the last meaningful
1030 element is standard_opcode_lengths[opcode_base - 1]. */
1031 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1032
1033 int file_names_size ()
1034 { return m_file_names.size(); }
1035
1036 /* The start and end of the statement program following this
1037 header. These point into dwarf2_per_objfile->line_buffer. */
1038 const gdb_byte *statement_program_start {}, *statement_program_end {};
1039
1040 private:
1041 /* The include_directories table. Note these are observing
1042 pointers. The memory is owned by debug_line_buffer. */
1043 std::vector<const char *> m_include_dirs;
1044
1045 /* The file_names table. This is private because the meaning of indexes
1046 differs among DWARF versions (The first valid index is 1 in DWARF 4 and
1047 before, and is 0 in DWARF 5 and later). So the client should use
1048 file_name_at method for access. */
1049 std::vector<file_entry> m_file_names;
1050 };
1051
1052 typedef std::unique_ptr<line_header> line_header_up;
1053
1054 const char *
1055 file_entry::include_dir (const line_header *lh) const
1056 {
1057 return lh->include_dir_at (d_index);
1058 }
1059
1060 /* When we construct a partial symbol table entry we only
1061 need this much information. */
1062 struct partial_die_info : public allocate_on_obstack
1063 {
1064 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1065
1066 /* Disable assign but still keep copy ctor, which is needed
1067 load_partial_dies. */
1068 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1069
1070 /* Adjust the partial die before generating a symbol for it. This
1071 function may set the is_external flag or change the DIE's
1072 name. */
1073 void fixup (struct dwarf2_cu *cu);
1074
1075 /* Read a minimal amount of information into the minimal die
1076 structure. */
1077 const gdb_byte *read (const struct die_reader_specs *reader,
1078 const struct abbrev_info &abbrev,
1079 const gdb_byte *info_ptr);
1080
1081 /* Offset of this DIE. */
1082 const sect_offset sect_off;
1083
1084 /* DWARF-2 tag for this DIE. */
1085 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1086
1087 /* Assorted flags describing the data found in this DIE. */
1088 const unsigned int has_children : 1;
1089
1090 unsigned int is_external : 1;
1091 unsigned int is_declaration : 1;
1092 unsigned int has_type : 1;
1093 unsigned int has_specification : 1;
1094 unsigned int has_pc_info : 1;
1095 unsigned int may_be_inlined : 1;
1096
1097 /* This DIE has been marked DW_AT_main_subprogram. */
1098 unsigned int main_subprogram : 1;
1099
1100 /* Flag set if the SCOPE field of this structure has been
1101 computed. */
1102 unsigned int scope_set : 1;
1103
1104 /* Flag set if the DIE has a byte_size attribute. */
1105 unsigned int has_byte_size : 1;
1106
1107 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1108 unsigned int has_const_value : 1;
1109
1110 /* Flag set if any of the DIE's children are template arguments. */
1111 unsigned int has_template_arguments : 1;
1112
1113 /* Flag set if fixup has been called on this die. */
1114 unsigned int fixup_called : 1;
1115
1116 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1117 unsigned int is_dwz : 1;
1118
1119 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1120 unsigned int spec_is_dwz : 1;
1121
1122 /* The name of this DIE. Normally the value of DW_AT_name, but
1123 sometimes a default name for unnamed DIEs. */
1124 const char *name = nullptr;
1125
1126 /* The linkage name, if present. */
1127 const char *linkage_name = nullptr;
1128
1129 /* The scope to prepend to our children. This is generally
1130 allocated on the comp_unit_obstack, so will disappear
1131 when this compilation unit leaves the cache. */
1132 const char *scope = nullptr;
1133
1134 /* Some data associated with the partial DIE. The tag determines
1135 which field is live. */
1136 union
1137 {
1138 /* The location description associated with this DIE, if any. */
1139 struct dwarf_block *locdesc;
1140 /* The offset of an import, for DW_TAG_imported_unit. */
1141 sect_offset sect_off;
1142 } d {};
1143
1144 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1145 CORE_ADDR lowpc = 0;
1146 CORE_ADDR highpc = 0;
1147
1148 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1149 DW_AT_sibling, if any. */
1150 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1151 could return DW_AT_sibling values to its caller load_partial_dies. */
1152 const gdb_byte *sibling = nullptr;
1153
1154 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1155 DW_AT_specification (or DW_AT_abstract_origin or
1156 DW_AT_extension). */
1157 sect_offset spec_offset {};
1158
1159 /* Pointers to this DIE's parent, first child, and next sibling,
1160 if any. */
1161 struct partial_die_info *die_parent = nullptr;
1162 struct partial_die_info *die_child = nullptr;
1163 struct partial_die_info *die_sibling = nullptr;
1164
1165 friend struct partial_die_info *
1166 dwarf2_cu::find_partial_die (sect_offset sect_off);
1167
1168 private:
1169 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1170 partial_die_info (sect_offset sect_off)
1171 : partial_die_info (sect_off, DW_TAG_padding, 0)
1172 {
1173 }
1174
1175 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1176 int has_children_)
1177 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1178 {
1179 is_external = 0;
1180 is_declaration = 0;
1181 has_type = 0;
1182 has_specification = 0;
1183 has_pc_info = 0;
1184 may_be_inlined = 0;
1185 main_subprogram = 0;
1186 scope_set = 0;
1187 has_byte_size = 0;
1188 has_const_value = 0;
1189 has_template_arguments = 0;
1190 fixup_called = 0;
1191 is_dwz = 0;
1192 spec_is_dwz = 0;
1193 }
1194 };
1195
1196 /* This data structure holds the information of an abbrev. */
1197 struct abbrev_info
1198 {
1199 unsigned int number; /* number identifying abbrev */
1200 enum dwarf_tag tag; /* dwarf tag */
1201 unsigned short has_children; /* boolean */
1202 unsigned short num_attrs; /* number of attributes */
1203 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1204 struct abbrev_info *next; /* next in chain */
1205 };
1206
1207 struct attr_abbrev
1208 {
1209 ENUM_BITFIELD(dwarf_attribute) name : 16;
1210 ENUM_BITFIELD(dwarf_form) form : 16;
1211
1212 /* It is valid only if FORM is DW_FORM_implicit_const. */
1213 LONGEST implicit_const;
1214 };
1215
1216 /* Size of abbrev_table.abbrev_hash_table. */
1217 #define ABBREV_HASH_SIZE 121
1218
1219 /* Top level data structure to contain an abbreviation table. */
1220
1221 struct abbrev_table
1222 {
1223 explicit abbrev_table (sect_offset off)
1224 : sect_off (off)
1225 {
1226 m_abbrevs =
1227 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1228 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1229 }
1230
1231 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1232
1233 /* Allocate space for a struct abbrev_info object in
1234 ABBREV_TABLE. */
1235 struct abbrev_info *alloc_abbrev ();
1236
1237 /* Add an abbreviation to the table. */
1238 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1239
1240 /* Look up an abbrev in the table.
1241 Returns NULL if the abbrev is not found. */
1242
1243 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1244
1245
1246 /* Where the abbrev table came from.
1247 This is used as a sanity check when the table is used. */
1248 const sect_offset sect_off;
1249
1250 /* Storage for the abbrev table. */
1251 auto_obstack abbrev_obstack;
1252
1253 private:
1254
1255 /* Hash table of abbrevs.
1256 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1257 It could be statically allocated, but the previous code didn't so we
1258 don't either. */
1259 struct abbrev_info **m_abbrevs;
1260 };
1261
1262 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1263
1264 /* Attributes have a name and a value. */
1265 struct attribute
1266 {
1267 ENUM_BITFIELD(dwarf_attribute) name : 16;
1268 ENUM_BITFIELD(dwarf_form) form : 15;
1269
1270 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1271 field should be in u.str (existing only for DW_STRING) but it is kept
1272 here for better struct attribute alignment. */
1273 unsigned int string_is_canonical : 1;
1274
1275 union
1276 {
1277 const char *str;
1278 struct dwarf_block *blk;
1279 ULONGEST unsnd;
1280 LONGEST snd;
1281 CORE_ADDR addr;
1282 ULONGEST signature;
1283 }
1284 u;
1285 };
1286
1287 /* This data structure holds a complete die structure. */
1288 struct die_info
1289 {
1290 /* DWARF-2 tag for this DIE. */
1291 ENUM_BITFIELD(dwarf_tag) tag : 16;
1292
1293 /* Number of attributes */
1294 unsigned char num_attrs;
1295
1296 /* True if we're presently building the full type name for the
1297 type derived from this DIE. */
1298 unsigned char building_fullname : 1;
1299
1300 /* True if this die is in process. PR 16581. */
1301 unsigned char in_process : 1;
1302
1303 /* Abbrev number */
1304 unsigned int abbrev;
1305
1306 /* Offset in .debug_info or .debug_types section. */
1307 sect_offset sect_off;
1308
1309 /* The dies in a compilation unit form an n-ary tree. PARENT
1310 points to this die's parent; CHILD points to the first child of
1311 this node; and all the children of a given node are chained
1312 together via their SIBLING fields. */
1313 struct die_info *child; /* Its first child, if any. */
1314 struct die_info *sibling; /* Its next sibling, if any. */
1315 struct die_info *parent; /* Its parent, if any. */
1316
1317 /* An array of attributes, with NUM_ATTRS elements. There may be
1318 zero, but it's not common and zero-sized arrays are not
1319 sufficiently portable C. */
1320 struct attribute attrs[1];
1321 };
1322
1323 /* Get at parts of an attribute structure. */
1324
1325 #define DW_STRING(attr) ((attr)->u.str)
1326 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1327 #define DW_UNSND(attr) ((attr)->u.unsnd)
1328 #define DW_BLOCK(attr) ((attr)->u.blk)
1329 #define DW_SND(attr) ((attr)->u.snd)
1330 #define DW_ADDR(attr) ((attr)->u.addr)
1331 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1332
1333 /* Blocks are a bunch of untyped bytes. */
1334 struct dwarf_block
1335 {
1336 size_t size;
1337
1338 /* Valid only if SIZE is not zero. */
1339 const gdb_byte *data;
1340 };
1341
1342 #ifndef ATTR_ALLOC_CHUNK
1343 #define ATTR_ALLOC_CHUNK 4
1344 #endif
1345
1346 /* Allocate fields for structs, unions and enums in this size. */
1347 #ifndef DW_FIELD_ALLOC_CHUNK
1348 #define DW_FIELD_ALLOC_CHUNK 4
1349 #endif
1350
1351 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1352 but this would require a corresponding change in unpack_field_as_long
1353 and friends. */
1354 static int bits_per_byte = 8;
1355
1356 /* When reading a variant or variant part, we track a bit more
1357 information about the field, and store it in an object of this
1358 type. */
1359
1360 struct variant_field
1361 {
1362 /* If we see a DW_TAG_variant, then this will be the discriminant
1363 value. */
1364 ULONGEST discriminant_value;
1365 /* If we see a DW_TAG_variant, then this will be set if this is the
1366 default branch. */
1367 bool default_branch;
1368 /* While reading a DW_TAG_variant_part, this will be set if this
1369 field is the discriminant. */
1370 bool is_discriminant;
1371 };
1372
1373 struct nextfield
1374 {
1375 int accessibility = 0;
1376 int virtuality = 0;
1377 /* Extra information to describe a variant or variant part. */
1378 struct variant_field variant {};
1379 struct field field {};
1380 };
1381
1382 struct fnfieldlist
1383 {
1384 const char *name = nullptr;
1385 std::vector<struct fn_field> fnfields;
1386 };
1387
1388 /* The routines that read and process dies for a C struct or C++ class
1389 pass lists of data member fields and lists of member function fields
1390 in an instance of a field_info structure, as defined below. */
1391 struct field_info
1392 {
1393 /* List of data member and baseclasses fields. */
1394 std::vector<struct nextfield> fields;
1395 std::vector<struct nextfield> baseclasses;
1396
1397 /* Number of fields (including baseclasses). */
1398 int nfields = 0;
1399
1400 /* Set if the accessibility of one of the fields is not public. */
1401 int non_public_fields = 0;
1402
1403 /* Member function fieldlist array, contains name of possibly overloaded
1404 member function, number of overloaded member functions and a pointer
1405 to the head of the member function field chain. */
1406 std::vector<struct fnfieldlist> fnfieldlists;
1407
1408 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1409 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1410 std::vector<struct decl_field> typedef_field_list;
1411
1412 /* Nested types defined by this class and the number of elements in this
1413 list. */
1414 std::vector<struct decl_field> nested_types_list;
1415 };
1416
1417 /* One item on the queue of compilation units to read in full symbols
1418 for. */
1419 struct dwarf2_queue_item
1420 {
1421 struct dwarf2_per_cu_data *per_cu;
1422 enum language pretend_language;
1423 struct dwarf2_queue_item *next;
1424 };
1425
1426 /* The current queue. */
1427 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1428
1429 /* Loaded secondary compilation units are kept in memory until they
1430 have not been referenced for the processing of this many
1431 compilation units. Set this to zero to disable caching. Cache
1432 sizes of up to at least twenty will improve startup time for
1433 typical inter-CU-reference binaries, at an obvious memory cost. */
1434 static int dwarf_max_cache_age = 5;
1435 static void
1436 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1437 struct cmd_list_element *c, const char *value)
1438 {
1439 fprintf_filtered (file, _("The upper bound on the age of cached "
1440 "DWARF compilation units is %s.\n"),
1441 value);
1442 }
1443 \f
1444 /* local function prototypes */
1445
1446 static const char *get_section_name (const struct dwarf2_section_info *);
1447
1448 static const char *get_section_file_name (const struct dwarf2_section_info *);
1449
1450 static void dwarf2_find_base_address (struct die_info *die,
1451 struct dwarf2_cu *cu);
1452
1453 static struct partial_symtab *create_partial_symtab
1454 (struct dwarf2_per_cu_data *per_cu, const char *name);
1455
1456 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1457 const gdb_byte *info_ptr,
1458 struct die_info *type_unit_die,
1459 int has_children, void *data);
1460
1461 static void dwarf2_build_psymtabs_hard
1462 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1463
1464 static void scan_partial_symbols (struct partial_die_info *,
1465 CORE_ADDR *, CORE_ADDR *,
1466 int, struct dwarf2_cu *);
1467
1468 static void add_partial_symbol (struct partial_die_info *,
1469 struct dwarf2_cu *);
1470
1471 static void add_partial_namespace (struct partial_die_info *pdi,
1472 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1473 int set_addrmap, struct dwarf2_cu *cu);
1474
1475 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1476 CORE_ADDR *highpc, int set_addrmap,
1477 struct dwarf2_cu *cu);
1478
1479 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1480 struct dwarf2_cu *cu);
1481
1482 static void add_partial_subprogram (struct partial_die_info *pdi,
1483 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1484 int need_pc, struct dwarf2_cu *cu);
1485
1486 static void dwarf2_read_symtab (struct partial_symtab *,
1487 struct objfile *);
1488
1489 static void psymtab_to_symtab_1 (struct partial_symtab *);
1490
1491 static abbrev_table_up abbrev_table_read_table
1492 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1493 sect_offset);
1494
1495 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1496
1497 static struct partial_die_info *load_partial_dies
1498 (const struct die_reader_specs *, const gdb_byte *, int);
1499
1500 /* A pair of partial_die_info and compilation unit. */
1501 struct cu_partial_die_info
1502 {
1503 /* The compilation unit of the partial_die_info. */
1504 struct dwarf2_cu *cu;
1505 /* A partial_die_info. */
1506 struct partial_die_info *pdi;
1507
1508 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1509 : cu (cu),
1510 pdi (pdi)
1511 { /* Nothing. */ }
1512
1513 private:
1514 cu_partial_die_info () = delete;
1515 };
1516
1517 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1518 struct dwarf2_cu *);
1519
1520 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1521 struct attribute *, struct attr_abbrev *,
1522 const gdb_byte *);
1523
1524 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1525
1526 static int read_1_signed_byte (bfd *, const gdb_byte *);
1527
1528 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1529
1530 /* Read the next three bytes (little-endian order) as an unsigned integer. */
1531 static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1532
1533 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1534
1535 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1536
1537 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1538 unsigned int *);
1539
1540 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1541
1542 static LONGEST read_checked_initial_length_and_offset
1543 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1544 unsigned int *, unsigned int *);
1545
1546 static LONGEST read_offset (bfd *, const gdb_byte *,
1547 const struct comp_unit_head *,
1548 unsigned int *);
1549
1550 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1551
1552 static sect_offset read_abbrev_offset
1553 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1554 struct dwarf2_section_info *, sect_offset);
1555
1556 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1557
1558 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1559
1560 static const char *read_indirect_string
1561 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1562 const struct comp_unit_head *, unsigned int *);
1563
1564 static const char *read_indirect_line_string
1565 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1566 const struct comp_unit_head *, unsigned int *);
1567
1568 static const char *read_indirect_string_at_offset
1569 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1570 LONGEST str_offset);
1571
1572 static const char *read_indirect_string_from_dwz
1573 (struct objfile *objfile, struct dwz_file *, LONGEST);
1574
1575 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1576
1577 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1578 const gdb_byte *,
1579 unsigned int *);
1580
1581 static const char *read_str_index (const struct die_reader_specs *reader,
1582 ULONGEST str_index);
1583
1584 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1585
1586 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1587 struct dwarf2_cu *);
1588
1589 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1590 unsigned int);
1591
1592 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1593 struct dwarf2_cu *cu);
1594
1595 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1596
1597 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1598 struct dwarf2_cu *cu);
1599
1600 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1601
1602 static struct die_info *die_specification (struct die_info *die,
1603 struct dwarf2_cu **);
1604
1605 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1606 struct dwarf2_cu *cu);
1607
1608 static void dwarf_decode_lines (struct line_header *, const char *,
1609 struct dwarf2_cu *, struct partial_symtab *,
1610 CORE_ADDR, int decode_mapping);
1611
1612 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1613 const char *);
1614
1615 static struct symbol *new_symbol (struct die_info *, struct type *,
1616 struct dwarf2_cu *, struct symbol * = NULL);
1617
1618 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1619 struct dwarf2_cu *);
1620
1621 static void dwarf2_const_value_attr (const struct attribute *attr,
1622 struct type *type,
1623 const char *name,
1624 struct obstack *obstack,
1625 struct dwarf2_cu *cu, LONGEST *value,
1626 const gdb_byte **bytes,
1627 struct dwarf2_locexpr_baton **baton);
1628
1629 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1630
1631 static int need_gnat_info (struct dwarf2_cu *);
1632
1633 static struct type *die_descriptive_type (struct die_info *,
1634 struct dwarf2_cu *);
1635
1636 static void set_descriptive_type (struct type *, struct die_info *,
1637 struct dwarf2_cu *);
1638
1639 static struct type *die_containing_type (struct die_info *,
1640 struct dwarf2_cu *);
1641
1642 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1643 struct dwarf2_cu *);
1644
1645 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1646
1647 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1648
1649 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1650
1651 static char *typename_concat (struct obstack *obs, const char *prefix,
1652 const char *suffix, int physname,
1653 struct dwarf2_cu *cu);
1654
1655 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1656
1657 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1658
1659 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1660
1661 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1662
1663 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1664
1665 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1666
1667 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1668 struct dwarf2_cu *, struct partial_symtab *);
1669
1670 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1671 values. Keep the items ordered with increasing constraints compliance. */
1672 enum pc_bounds_kind
1673 {
1674 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1675 PC_BOUNDS_NOT_PRESENT,
1676
1677 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1678 were present but they do not form a valid range of PC addresses. */
1679 PC_BOUNDS_INVALID,
1680
1681 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1682 PC_BOUNDS_RANGES,
1683
1684 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1685 PC_BOUNDS_HIGH_LOW,
1686 };
1687
1688 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1689 CORE_ADDR *, CORE_ADDR *,
1690 struct dwarf2_cu *,
1691 struct partial_symtab *);
1692
1693 static void get_scope_pc_bounds (struct die_info *,
1694 CORE_ADDR *, CORE_ADDR *,
1695 struct dwarf2_cu *);
1696
1697 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1698 CORE_ADDR, struct dwarf2_cu *);
1699
1700 static void dwarf2_add_field (struct field_info *, struct die_info *,
1701 struct dwarf2_cu *);
1702
1703 static void dwarf2_attach_fields_to_type (struct field_info *,
1704 struct type *, struct dwarf2_cu *);
1705
1706 static void dwarf2_add_member_fn (struct field_info *,
1707 struct die_info *, struct type *,
1708 struct dwarf2_cu *);
1709
1710 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1711 struct type *,
1712 struct dwarf2_cu *);
1713
1714 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1715
1716 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1717
1718 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1719
1720 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1721
1722 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1723
1724 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1725
1726 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1727
1728 static struct type *read_module_type (struct die_info *die,
1729 struct dwarf2_cu *cu);
1730
1731 static const char *namespace_name (struct die_info *die,
1732 int *is_anonymous, struct dwarf2_cu *);
1733
1734 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1735
1736 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1737
1738 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1739 struct dwarf2_cu *);
1740
1741 static struct die_info *read_die_and_siblings_1
1742 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1743 struct die_info *);
1744
1745 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1746 const gdb_byte *info_ptr,
1747 const gdb_byte **new_info_ptr,
1748 struct die_info *parent);
1749
1750 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1751 struct die_info **, const gdb_byte *,
1752 int *, int);
1753
1754 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1755 struct die_info **, const gdb_byte *,
1756 int *);
1757
1758 static void process_die (struct die_info *, struct dwarf2_cu *);
1759
1760 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1761 struct obstack *);
1762
1763 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1764
1765 static const char *dwarf2_full_name (const char *name,
1766 struct die_info *die,
1767 struct dwarf2_cu *cu);
1768
1769 static const char *dwarf2_physname (const char *name, struct die_info *die,
1770 struct dwarf2_cu *cu);
1771
1772 static struct die_info *dwarf2_extension (struct die_info *die,
1773 struct dwarf2_cu **);
1774
1775 static const char *dwarf_tag_name (unsigned int);
1776
1777 static const char *dwarf_attr_name (unsigned int);
1778
1779 static const char *dwarf_unit_type_name (int unit_type);
1780
1781 static const char *dwarf_form_name (unsigned int);
1782
1783 static const char *dwarf_bool_name (unsigned int);
1784
1785 static const char *dwarf_type_encoding_name (unsigned int);
1786
1787 static struct die_info *sibling_die (struct die_info *);
1788
1789 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1790
1791 static void dump_die_for_error (struct die_info *);
1792
1793 static void dump_die_1 (struct ui_file *, int level, int max_level,
1794 struct die_info *);
1795
1796 /*static*/ void dump_die (struct die_info *, int max_level);
1797
1798 static void store_in_ref_table (struct die_info *,
1799 struct dwarf2_cu *);
1800
1801 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1802
1803 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1804
1805 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1806 const struct attribute *,
1807 struct dwarf2_cu **);
1808
1809 static struct die_info *follow_die_ref (struct die_info *,
1810 const struct attribute *,
1811 struct dwarf2_cu **);
1812
1813 static struct die_info *follow_die_sig (struct die_info *,
1814 const struct attribute *,
1815 struct dwarf2_cu **);
1816
1817 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1818 struct dwarf2_cu *);
1819
1820 static struct type *get_DW_AT_signature_type (struct die_info *,
1821 const struct attribute *,
1822 struct dwarf2_cu *);
1823
1824 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1825
1826 static void read_signatured_type (struct signatured_type *);
1827
1828 static int attr_to_dynamic_prop (const struct attribute *attr,
1829 struct die_info *die, struct dwarf2_cu *cu,
1830 struct dynamic_prop *prop, struct type *type);
1831
1832 /* memory allocation interface */
1833
1834 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1835
1836 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1837
1838 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1839
1840 static int attr_form_is_block (const struct attribute *);
1841
1842 static int attr_form_is_section_offset (const struct attribute *);
1843
1844 static int attr_form_is_constant (const struct attribute *);
1845
1846 static int attr_form_is_ref (const struct attribute *);
1847
1848 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1849 struct dwarf2_loclist_baton *baton,
1850 const struct attribute *attr);
1851
1852 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1853 struct symbol *sym,
1854 struct dwarf2_cu *cu,
1855 int is_block);
1856
1857 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1858 const gdb_byte *info_ptr,
1859 struct abbrev_info *abbrev);
1860
1861 static hashval_t partial_die_hash (const void *item);
1862
1863 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1864
1865 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1866 (sect_offset sect_off, unsigned int offset_in_dwz,
1867 struct dwarf2_per_objfile *dwarf2_per_objfile);
1868
1869 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1870 struct die_info *comp_unit_die,
1871 enum language pretend_language);
1872
1873 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1874
1875 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1876
1877 static struct type *set_die_type (struct die_info *, struct type *,
1878 struct dwarf2_cu *);
1879
1880 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1881
1882 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1883
1884 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1885 enum language);
1886
1887 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1888 enum language);
1889
1890 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1891 enum language);
1892
1893 static void dwarf2_add_dependence (struct dwarf2_cu *,
1894 struct dwarf2_per_cu_data *);
1895
1896 static void dwarf2_mark (struct dwarf2_cu *);
1897
1898 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1899
1900 static struct type *get_die_type_at_offset (sect_offset,
1901 struct dwarf2_per_cu_data *);
1902
1903 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1904
1905 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1906 enum language pretend_language);
1907
1908 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1909
1910 static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1911 static struct type *dwarf2_per_cu_addr_sized_int_type
1912 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
1913
1914 /* Class, the destructor of which frees all allocated queue entries. This
1915 will only have work to do if an error was thrown while processing the
1916 dwarf. If no error was thrown then the queue entries should have all
1917 been processed, and freed, as we went along. */
1918
1919 class dwarf2_queue_guard
1920 {
1921 public:
1922 dwarf2_queue_guard () = default;
1923
1924 /* Free any entries remaining on the queue. There should only be
1925 entries left if we hit an error while processing the dwarf. */
1926 ~dwarf2_queue_guard ()
1927 {
1928 struct dwarf2_queue_item *item, *last;
1929
1930 item = dwarf2_queue;
1931 while (item)
1932 {
1933 /* Anything still marked queued is likely to be in an
1934 inconsistent state, so discard it. */
1935 if (item->per_cu->queued)
1936 {
1937 if (item->per_cu->cu != NULL)
1938 free_one_cached_comp_unit (item->per_cu);
1939 item->per_cu->queued = 0;
1940 }
1941
1942 last = item;
1943 item = item->next;
1944 xfree (last);
1945 }
1946
1947 dwarf2_queue = dwarf2_queue_tail = NULL;
1948 }
1949 };
1950
1951 /* The return type of find_file_and_directory. Note, the enclosed
1952 string pointers are only valid while this object is valid. */
1953
1954 struct file_and_directory
1955 {
1956 /* The filename. This is never NULL. */
1957 const char *name;
1958
1959 /* The compilation directory. NULL if not known. If we needed to
1960 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1961 points directly to the DW_AT_comp_dir string attribute owned by
1962 the obstack that owns the DIE. */
1963 const char *comp_dir;
1964
1965 /* If we needed to build a new string for comp_dir, this is what
1966 owns the storage. */
1967 std::string comp_dir_storage;
1968 };
1969
1970 static file_and_directory find_file_and_directory (struct die_info *die,
1971 struct dwarf2_cu *cu);
1972
1973 static char *file_full_name (int file, struct line_header *lh,
1974 const char *comp_dir);
1975
1976 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1977 enum class rcuh_kind { COMPILE, TYPE };
1978
1979 static const gdb_byte *read_and_check_comp_unit_head
1980 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1981 struct comp_unit_head *header,
1982 struct dwarf2_section_info *section,
1983 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1984 rcuh_kind section_kind);
1985
1986 static void init_cutu_and_read_dies
1987 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1988 int use_existing_cu, int keep, bool skip_partial,
1989 die_reader_func_ftype *die_reader_func, void *data);
1990
1991 static void init_cutu_and_read_dies_simple
1992 (struct dwarf2_per_cu_data *this_cu,
1993 die_reader_func_ftype *die_reader_func, void *data);
1994
1995 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1996
1997 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1998
1999 static struct dwo_unit *lookup_dwo_unit_in_dwp
2000 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2001 struct dwp_file *dwp_file, const char *comp_dir,
2002 ULONGEST signature, int is_debug_types);
2003
2004 static struct dwp_file *get_dwp_file
2005 (struct dwarf2_per_objfile *dwarf2_per_objfile);
2006
2007 static struct dwo_unit *lookup_dwo_comp_unit
2008 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2009
2010 static struct dwo_unit *lookup_dwo_type_unit
2011 (struct signatured_type *, const char *, const char *);
2012
2013 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2014
2015 /* A unique pointer to a dwo_file. */
2016
2017 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
2018
2019 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2020
2021 static void check_producer (struct dwarf2_cu *cu);
2022
2023 static void free_line_header_voidp (void *arg);
2024 \f
2025 /* Various complaints about symbol reading that don't abort the process. */
2026
2027 static void
2028 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2029 {
2030 complaint (_("statement list doesn't fit in .debug_line section"));
2031 }
2032
2033 static void
2034 dwarf2_debug_line_missing_file_complaint (void)
2035 {
2036 complaint (_(".debug_line section has line data without a file"));
2037 }
2038
2039 static void
2040 dwarf2_debug_line_missing_end_sequence_complaint (void)
2041 {
2042 complaint (_(".debug_line section has line "
2043 "program sequence without an end"));
2044 }
2045
2046 static void
2047 dwarf2_complex_location_expr_complaint (void)
2048 {
2049 complaint (_("location expression too complex"));
2050 }
2051
2052 static void
2053 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2054 int arg3)
2055 {
2056 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
2057 arg1, arg2, arg3);
2058 }
2059
2060 static void
2061 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2062 {
2063 complaint (_("debug info runs off end of %s section"
2064 " [in module %s]"),
2065 get_section_name (section),
2066 get_section_file_name (section));
2067 }
2068
2069 static void
2070 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2071 {
2072 complaint (_("macro debug info contains a "
2073 "malformed macro definition:\n`%s'"),
2074 arg1);
2075 }
2076
2077 static void
2078 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2079 {
2080 complaint (_("invalid attribute class or form for '%s' in '%s'"),
2081 arg1, arg2);
2082 }
2083
2084 /* Hash function for line_header_hash. */
2085
2086 static hashval_t
2087 line_header_hash (const struct line_header *ofs)
2088 {
2089 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2090 }
2091
2092 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2093
2094 static hashval_t
2095 line_header_hash_voidp (const void *item)
2096 {
2097 const struct line_header *ofs = (const struct line_header *) item;
2098
2099 return line_header_hash (ofs);
2100 }
2101
2102 /* Equality function for line_header_hash. */
2103
2104 static int
2105 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2106 {
2107 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2108 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2109
2110 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2111 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2112 }
2113
2114 \f
2115
2116 /* Read the given attribute value as an address, taking the attribute's
2117 form into account. */
2118
2119 static CORE_ADDR
2120 attr_value_as_address (struct attribute *attr)
2121 {
2122 CORE_ADDR addr;
2123
2124 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2125 && attr->form != DW_FORM_GNU_addr_index)
2126 {
2127 /* Aside from a few clearly defined exceptions, attributes that
2128 contain an address must always be in DW_FORM_addr form.
2129 Unfortunately, some compilers happen to be violating this
2130 requirement by encoding addresses using other forms, such
2131 as DW_FORM_data4 for example. For those broken compilers,
2132 we try to do our best, without any guarantee of success,
2133 to interpret the address correctly. It would also be nice
2134 to generate a complaint, but that would require us to maintain
2135 a list of legitimate cases where a non-address form is allowed,
2136 as well as update callers to pass in at least the CU's DWARF
2137 version. This is more overhead than what we're willing to
2138 expand for a pretty rare case. */
2139 addr = DW_UNSND (attr);
2140 }
2141 else
2142 addr = DW_ADDR (attr);
2143
2144 return addr;
2145 }
2146
2147 /* See declaration. */
2148
2149 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2150 const dwarf2_debug_sections *names,
2151 bool can_copy_)
2152 : objfile (objfile_),
2153 can_copy (can_copy_)
2154 {
2155 if (names == NULL)
2156 names = &dwarf2_elf_names;
2157
2158 bfd *obfd = objfile->obfd;
2159
2160 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2161 locate_sections (obfd, sec, *names);
2162 }
2163
2164 dwarf2_per_objfile::~dwarf2_per_objfile ()
2165 {
2166 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2167 free_cached_comp_units ();
2168
2169 if (quick_file_names_table)
2170 htab_delete (quick_file_names_table);
2171
2172 if (line_header_hash)
2173 htab_delete (line_header_hash);
2174
2175 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2176 per_cu->imported_symtabs_free ();
2177
2178 for (signatured_type *sig_type : all_type_units)
2179 sig_type->per_cu.imported_symtabs_free ();
2180
2181 /* Everything else should be on the objfile obstack. */
2182 }
2183
2184 /* See declaration. */
2185
2186 void
2187 dwarf2_per_objfile::free_cached_comp_units ()
2188 {
2189 dwarf2_per_cu_data *per_cu = read_in_chain;
2190 dwarf2_per_cu_data **last_chain = &read_in_chain;
2191 while (per_cu != NULL)
2192 {
2193 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2194
2195 delete per_cu->cu;
2196 *last_chain = next_cu;
2197 per_cu = next_cu;
2198 }
2199 }
2200
2201 /* A helper class that calls free_cached_comp_units on
2202 destruction. */
2203
2204 class free_cached_comp_units
2205 {
2206 public:
2207
2208 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2209 : m_per_objfile (per_objfile)
2210 {
2211 }
2212
2213 ~free_cached_comp_units ()
2214 {
2215 m_per_objfile->free_cached_comp_units ();
2216 }
2217
2218 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2219
2220 private:
2221
2222 dwarf2_per_objfile *m_per_objfile;
2223 };
2224
2225 /* Try to locate the sections we need for DWARF 2 debugging
2226 information and return true if we have enough to do something.
2227 NAMES points to the dwarf2 section names, or is NULL if the standard
2228 ELF names are used. CAN_COPY is true for formats where symbol
2229 interposition is possible and so symbol values must follow copy
2230 relocation rules. */
2231
2232 int
2233 dwarf2_has_info (struct objfile *objfile,
2234 const struct dwarf2_debug_sections *names,
2235 bool can_copy)
2236 {
2237 if (objfile->flags & OBJF_READNEVER)
2238 return 0;
2239
2240 struct dwarf2_per_objfile *dwarf2_per_objfile
2241 = get_dwarf2_per_objfile (objfile);
2242
2243 if (dwarf2_per_objfile == NULL)
2244 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
2245 names,
2246 can_copy);
2247
2248 return (!dwarf2_per_objfile->info.is_virtual
2249 && dwarf2_per_objfile->info.s.section != NULL
2250 && !dwarf2_per_objfile->abbrev.is_virtual
2251 && dwarf2_per_objfile->abbrev.s.section != NULL);
2252 }
2253
2254 /* Return the containing section of virtual section SECTION. */
2255
2256 static struct dwarf2_section_info *
2257 get_containing_section (const struct dwarf2_section_info *section)
2258 {
2259 gdb_assert (section->is_virtual);
2260 return section->s.containing_section;
2261 }
2262
2263 /* Return the bfd owner of SECTION. */
2264
2265 static struct bfd *
2266 get_section_bfd_owner (const struct dwarf2_section_info *section)
2267 {
2268 if (section->is_virtual)
2269 {
2270 section = get_containing_section (section);
2271 gdb_assert (!section->is_virtual);
2272 }
2273 return section->s.section->owner;
2274 }
2275
2276 /* Return the bfd section of SECTION.
2277 Returns NULL if the section is not present. */
2278
2279 static asection *
2280 get_section_bfd_section (const struct dwarf2_section_info *section)
2281 {
2282 if (section->is_virtual)
2283 {
2284 section = get_containing_section (section);
2285 gdb_assert (!section->is_virtual);
2286 }
2287 return section->s.section;
2288 }
2289
2290 /* Return the name of SECTION. */
2291
2292 static const char *
2293 get_section_name (const struct dwarf2_section_info *section)
2294 {
2295 asection *sectp = get_section_bfd_section (section);
2296
2297 gdb_assert (sectp != NULL);
2298 return bfd_section_name (sectp);
2299 }
2300
2301 /* Return the name of the file SECTION is in. */
2302
2303 static const char *
2304 get_section_file_name (const struct dwarf2_section_info *section)
2305 {
2306 bfd *abfd = get_section_bfd_owner (section);
2307
2308 return bfd_get_filename (abfd);
2309 }
2310
2311 /* Return the id of SECTION.
2312 Returns 0 if SECTION doesn't exist. */
2313
2314 static int
2315 get_section_id (const struct dwarf2_section_info *section)
2316 {
2317 asection *sectp = get_section_bfd_section (section);
2318
2319 if (sectp == NULL)
2320 return 0;
2321 return sectp->id;
2322 }
2323
2324 /* Return the flags of SECTION.
2325 SECTION (or containing section if this is a virtual section) must exist. */
2326
2327 static int
2328 get_section_flags (const struct dwarf2_section_info *section)
2329 {
2330 asection *sectp = get_section_bfd_section (section);
2331
2332 gdb_assert (sectp != NULL);
2333 return bfd_section_flags (sectp);
2334 }
2335
2336 /* When loading sections, we look either for uncompressed section or for
2337 compressed section names. */
2338
2339 static int
2340 section_is_p (const char *section_name,
2341 const struct dwarf2_section_names *names)
2342 {
2343 if (names->normal != NULL
2344 && strcmp (section_name, names->normal) == 0)
2345 return 1;
2346 if (names->compressed != NULL
2347 && strcmp (section_name, names->compressed) == 0)
2348 return 1;
2349 return 0;
2350 }
2351
2352 /* See declaration. */
2353
2354 void
2355 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2356 const dwarf2_debug_sections &names)
2357 {
2358 flagword aflag = bfd_section_flags (sectp);
2359
2360 if ((aflag & SEC_HAS_CONTENTS) == 0)
2361 {
2362 }
2363 else if (elf_section_data (sectp)->this_hdr.sh_size
2364 > bfd_get_file_size (abfd))
2365 {
2366 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2367 warning (_("Discarding section %s which has a section size (%s"
2368 ") larger than the file size [in module %s]"),
2369 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2370 bfd_get_filename (abfd));
2371 }
2372 else if (section_is_p (sectp->name, &names.info))
2373 {
2374 this->info.s.section = sectp;
2375 this->info.size = bfd_section_size (sectp);
2376 }
2377 else if (section_is_p (sectp->name, &names.abbrev))
2378 {
2379 this->abbrev.s.section = sectp;
2380 this->abbrev.size = bfd_section_size (sectp);
2381 }
2382 else if (section_is_p (sectp->name, &names.line))
2383 {
2384 this->line.s.section = sectp;
2385 this->line.size = bfd_section_size (sectp);
2386 }
2387 else if (section_is_p (sectp->name, &names.loc))
2388 {
2389 this->loc.s.section = sectp;
2390 this->loc.size = bfd_section_size (sectp);
2391 }
2392 else if (section_is_p (sectp->name, &names.loclists))
2393 {
2394 this->loclists.s.section = sectp;
2395 this->loclists.size = bfd_section_size (sectp);
2396 }
2397 else if (section_is_p (sectp->name, &names.macinfo))
2398 {
2399 this->macinfo.s.section = sectp;
2400 this->macinfo.size = bfd_section_size (sectp);
2401 }
2402 else if (section_is_p (sectp->name, &names.macro))
2403 {
2404 this->macro.s.section = sectp;
2405 this->macro.size = bfd_section_size (sectp);
2406 }
2407 else if (section_is_p (sectp->name, &names.str))
2408 {
2409 this->str.s.section = sectp;
2410 this->str.size = bfd_section_size (sectp);
2411 }
2412 else if (section_is_p (sectp->name, &names.line_str))
2413 {
2414 this->line_str.s.section = sectp;
2415 this->line_str.size = bfd_section_size (sectp);
2416 }
2417 else if (section_is_p (sectp->name, &names.addr))
2418 {
2419 this->addr.s.section = sectp;
2420 this->addr.size = bfd_section_size (sectp);
2421 }
2422 else if (section_is_p (sectp->name, &names.frame))
2423 {
2424 this->frame.s.section = sectp;
2425 this->frame.size = bfd_section_size (sectp);
2426 }
2427 else if (section_is_p (sectp->name, &names.eh_frame))
2428 {
2429 this->eh_frame.s.section = sectp;
2430 this->eh_frame.size = bfd_section_size (sectp);
2431 }
2432 else if (section_is_p (sectp->name, &names.ranges))
2433 {
2434 this->ranges.s.section = sectp;
2435 this->ranges.size = bfd_section_size (sectp);
2436 }
2437 else if (section_is_p (sectp->name, &names.rnglists))
2438 {
2439 this->rnglists.s.section = sectp;
2440 this->rnglists.size = bfd_section_size (sectp);
2441 }
2442 else if (section_is_p (sectp->name, &names.types))
2443 {
2444 struct dwarf2_section_info type_section;
2445
2446 memset (&type_section, 0, sizeof (type_section));
2447 type_section.s.section = sectp;
2448 type_section.size = bfd_section_size (sectp);
2449
2450 this->types.push_back (type_section);
2451 }
2452 else if (section_is_p (sectp->name, &names.gdb_index))
2453 {
2454 this->gdb_index.s.section = sectp;
2455 this->gdb_index.size = bfd_section_size (sectp);
2456 }
2457 else if (section_is_p (sectp->name, &names.debug_names))
2458 {
2459 this->debug_names.s.section = sectp;
2460 this->debug_names.size = bfd_section_size (sectp);
2461 }
2462 else if (section_is_p (sectp->name, &names.debug_aranges))
2463 {
2464 this->debug_aranges.s.section = sectp;
2465 this->debug_aranges.size = bfd_section_size (sectp);
2466 }
2467
2468 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2469 && bfd_section_vma (sectp) == 0)
2470 this->has_section_at_zero = true;
2471 }
2472
2473 /* A helper function that decides whether a section is empty,
2474 or not present. */
2475
2476 static int
2477 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2478 {
2479 if (section->is_virtual)
2480 return section->size == 0;
2481 return section->s.section == NULL || section->size == 0;
2482 }
2483
2484 /* See dwarf2read.h. */
2485
2486 void
2487 dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
2488 {
2489 asection *sectp;
2490 bfd *abfd;
2491 gdb_byte *buf, *retbuf;
2492
2493 if (info->readin)
2494 return;
2495 info->buffer = NULL;
2496 info->readin = true;
2497
2498 if (dwarf2_section_empty_p (info))
2499 return;
2500
2501 sectp = get_section_bfd_section (info);
2502
2503 /* If this is a virtual section we need to read in the real one first. */
2504 if (info->is_virtual)
2505 {
2506 struct dwarf2_section_info *containing_section =
2507 get_containing_section (info);
2508
2509 gdb_assert (sectp != NULL);
2510 if ((sectp->flags & SEC_RELOC) != 0)
2511 {
2512 error (_("Dwarf Error: DWP format V2 with relocations is not"
2513 " supported in section %s [in module %s]"),
2514 get_section_name (info), get_section_file_name (info));
2515 }
2516 dwarf2_read_section (objfile, containing_section);
2517 /* Other code should have already caught virtual sections that don't
2518 fit. */
2519 gdb_assert (info->virtual_offset + info->size
2520 <= containing_section->size);
2521 /* If the real section is empty or there was a problem reading the
2522 section we shouldn't get here. */
2523 gdb_assert (containing_section->buffer != NULL);
2524 info->buffer = containing_section->buffer + info->virtual_offset;
2525 return;
2526 }
2527
2528 /* If the section has relocations, we must read it ourselves.
2529 Otherwise we attach it to the BFD. */
2530 if ((sectp->flags & SEC_RELOC) == 0)
2531 {
2532 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2533 return;
2534 }
2535
2536 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2537 info->buffer = buf;
2538
2539 /* When debugging .o files, we may need to apply relocations; see
2540 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2541 We never compress sections in .o files, so we only need to
2542 try this when the section is not compressed. */
2543 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2544 if (retbuf != NULL)
2545 {
2546 info->buffer = retbuf;
2547 return;
2548 }
2549
2550 abfd = get_section_bfd_owner (info);
2551 gdb_assert (abfd != NULL);
2552
2553 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2554 || bfd_bread (buf, info->size, abfd) != info->size)
2555 {
2556 error (_("Dwarf Error: Can't read DWARF data"
2557 " in section %s [in module %s]"),
2558 bfd_section_name (sectp), bfd_get_filename (abfd));
2559 }
2560 }
2561
2562 /* A helper function that returns the size of a section in a safe way.
2563 If you are positive that the section has been read before using the
2564 size, then it is safe to refer to the dwarf2_section_info object's
2565 "size" field directly. In other cases, you must call this
2566 function, because for compressed sections the size field is not set
2567 correctly until the section has been read. */
2568
2569 static bfd_size_type
2570 dwarf2_section_size (struct objfile *objfile,
2571 struct dwarf2_section_info *info)
2572 {
2573 if (!info->readin)
2574 dwarf2_read_section (objfile, info);
2575 return info->size;
2576 }
2577
2578 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2579 SECTION_NAME. */
2580
2581 void
2582 dwarf2_get_section_info (struct objfile *objfile,
2583 enum dwarf2_section_enum sect,
2584 asection **sectp, const gdb_byte **bufp,
2585 bfd_size_type *sizep)
2586 {
2587 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2588 struct dwarf2_section_info *info;
2589
2590 /* We may see an objfile without any DWARF, in which case we just
2591 return nothing. */
2592 if (data == NULL)
2593 {
2594 *sectp = NULL;
2595 *bufp = NULL;
2596 *sizep = 0;
2597 return;
2598 }
2599 switch (sect)
2600 {
2601 case DWARF2_DEBUG_FRAME:
2602 info = &data->frame;
2603 break;
2604 case DWARF2_EH_FRAME:
2605 info = &data->eh_frame;
2606 break;
2607 default:
2608 gdb_assert_not_reached ("unexpected section");
2609 }
2610
2611 dwarf2_read_section (objfile, info);
2612
2613 *sectp = get_section_bfd_section (info);
2614 *bufp = info->buffer;
2615 *sizep = info->size;
2616 }
2617
2618 /* A helper function to find the sections for a .dwz file. */
2619
2620 static void
2621 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2622 {
2623 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2624
2625 /* Note that we only support the standard ELF names, because .dwz
2626 is ELF-only (at the time of writing). */
2627 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2628 {
2629 dwz_file->abbrev.s.section = sectp;
2630 dwz_file->abbrev.size = bfd_section_size (sectp);
2631 }
2632 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2633 {
2634 dwz_file->info.s.section = sectp;
2635 dwz_file->info.size = bfd_section_size (sectp);
2636 }
2637 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2638 {
2639 dwz_file->str.s.section = sectp;
2640 dwz_file->str.size = bfd_section_size (sectp);
2641 }
2642 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2643 {
2644 dwz_file->line.s.section = sectp;
2645 dwz_file->line.size = bfd_section_size (sectp);
2646 }
2647 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2648 {
2649 dwz_file->macro.s.section = sectp;
2650 dwz_file->macro.size = bfd_section_size (sectp);
2651 }
2652 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2653 {
2654 dwz_file->gdb_index.s.section = sectp;
2655 dwz_file->gdb_index.size = bfd_section_size (sectp);
2656 }
2657 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2658 {
2659 dwz_file->debug_names.s.section = sectp;
2660 dwz_file->debug_names.size = bfd_section_size (sectp);
2661 }
2662 }
2663
2664 /* See dwarf2read.h. */
2665
2666 struct dwz_file *
2667 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2668 {
2669 const char *filename;
2670 bfd_size_type buildid_len_arg;
2671 size_t buildid_len;
2672 bfd_byte *buildid;
2673
2674 if (dwarf2_per_objfile->dwz_file != NULL)
2675 return dwarf2_per_objfile->dwz_file.get ();
2676
2677 bfd_set_error (bfd_error_no_error);
2678 gdb::unique_xmalloc_ptr<char> data
2679 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2680 &buildid_len_arg, &buildid));
2681 if (data == NULL)
2682 {
2683 if (bfd_get_error () == bfd_error_no_error)
2684 return NULL;
2685 error (_("could not read '.gnu_debugaltlink' section: %s"),
2686 bfd_errmsg (bfd_get_error ()));
2687 }
2688
2689 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2690
2691 buildid_len = (size_t) buildid_len_arg;
2692
2693 filename = data.get ();
2694
2695 std::string abs_storage;
2696 if (!IS_ABSOLUTE_PATH (filename))
2697 {
2698 gdb::unique_xmalloc_ptr<char> abs
2699 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2700
2701 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2702 filename = abs_storage.c_str ();
2703 }
2704
2705 /* First try the file name given in the section. If that doesn't
2706 work, try to use the build-id instead. */
2707 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2708 if (dwz_bfd != NULL)
2709 {
2710 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2711 dwz_bfd.reset (nullptr);
2712 }
2713
2714 if (dwz_bfd == NULL)
2715 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2716
2717 if (dwz_bfd == NULL)
2718 error (_("could not find '.gnu_debugaltlink' file for %s"),
2719 objfile_name (dwarf2_per_objfile->objfile));
2720
2721 std::unique_ptr<struct dwz_file> result
2722 (new struct dwz_file (std::move (dwz_bfd)));
2723
2724 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2725 result.get ());
2726
2727 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2728 result->dwz_bfd.get ());
2729 dwarf2_per_objfile->dwz_file = std::move (result);
2730 return dwarf2_per_objfile->dwz_file.get ();
2731 }
2732 \f
2733 /* DWARF quick_symbols_functions support. */
2734
2735 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2736 unique line tables, so we maintain a separate table of all .debug_line
2737 derived entries to support the sharing.
2738 All the quick functions need is the list of file names. We discard the
2739 line_header when we're done and don't need to record it here. */
2740 struct quick_file_names
2741 {
2742 /* The data used to construct the hash key. */
2743 struct stmt_list_hash hash;
2744
2745 /* The number of entries in file_names, real_names. */
2746 unsigned int num_file_names;
2747
2748 /* The file names from the line table, after being run through
2749 file_full_name. */
2750 const char **file_names;
2751
2752 /* The file names from the line table after being run through
2753 gdb_realpath. These are computed lazily. */
2754 const char **real_names;
2755 };
2756
2757 /* When using the index (and thus not using psymtabs), each CU has an
2758 object of this type. This is used to hold information needed by
2759 the various "quick" methods. */
2760 struct dwarf2_per_cu_quick_data
2761 {
2762 /* The file table. This can be NULL if there was no file table
2763 or it's currently not read in.
2764 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2765 struct quick_file_names *file_names;
2766
2767 /* The corresponding symbol table. This is NULL if symbols for this
2768 CU have not yet been read. */
2769 struct compunit_symtab *compunit_symtab;
2770
2771 /* A temporary mark bit used when iterating over all CUs in
2772 expand_symtabs_matching. */
2773 unsigned int mark : 1;
2774
2775 /* True if we've tried to read the file table and found there isn't one.
2776 There will be no point in trying to read it again next time. */
2777 unsigned int no_file_data : 1;
2778 };
2779
2780 /* Utility hash function for a stmt_list_hash. */
2781
2782 static hashval_t
2783 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2784 {
2785 hashval_t v = 0;
2786
2787 if (stmt_list_hash->dwo_unit != NULL)
2788 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2789 v += to_underlying (stmt_list_hash->line_sect_off);
2790 return v;
2791 }
2792
2793 /* Utility equality function for a stmt_list_hash. */
2794
2795 static int
2796 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2797 const struct stmt_list_hash *rhs)
2798 {
2799 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2800 return 0;
2801 if (lhs->dwo_unit != NULL
2802 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2803 return 0;
2804
2805 return lhs->line_sect_off == rhs->line_sect_off;
2806 }
2807
2808 /* Hash function for a quick_file_names. */
2809
2810 static hashval_t
2811 hash_file_name_entry (const void *e)
2812 {
2813 const struct quick_file_names *file_data
2814 = (const struct quick_file_names *) e;
2815
2816 return hash_stmt_list_entry (&file_data->hash);
2817 }
2818
2819 /* Equality function for a quick_file_names. */
2820
2821 static int
2822 eq_file_name_entry (const void *a, const void *b)
2823 {
2824 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2825 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2826
2827 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2828 }
2829
2830 /* Delete function for a quick_file_names. */
2831
2832 static void
2833 delete_file_name_entry (void *e)
2834 {
2835 struct quick_file_names *file_data = (struct quick_file_names *) e;
2836 int i;
2837
2838 for (i = 0; i < file_data->num_file_names; ++i)
2839 {
2840 xfree ((void*) file_data->file_names[i]);
2841 if (file_data->real_names)
2842 xfree ((void*) file_data->real_names[i]);
2843 }
2844
2845 /* The space for the struct itself lives on objfile_obstack,
2846 so we don't free it here. */
2847 }
2848
2849 /* Create a quick_file_names hash table. */
2850
2851 static htab_t
2852 create_quick_file_names_table (unsigned int nr_initial_entries)
2853 {
2854 return htab_create_alloc (nr_initial_entries,
2855 hash_file_name_entry, eq_file_name_entry,
2856 delete_file_name_entry, xcalloc, xfree);
2857 }
2858
2859 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2860 have to be created afterwards. You should call age_cached_comp_units after
2861 processing PER_CU->CU. dw2_setup must have been already called. */
2862
2863 static void
2864 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2865 {
2866 if (per_cu->is_debug_types)
2867 load_full_type_unit (per_cu);
2868 else
2869 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2870
2871 if (per_cu->cu == NULL)
2872 return; /* Dummy CU. */
2873
2874 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2875 }
2876
2877 /* Read in the symbols for PER_CU. */
2878
2879 static void
2880 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2881 {
2882 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2883
2884 /* Skip type_unit_groups, reading the type units they contain
2885 is handled elsewhere. */
2886 if (IS_TYPE_UNIT_GROUP (per_cu))
2887 return;
2888
2889 /* The destructor of dwarf2_queue_guard frees any entries left on
2890 the queue. After this point we're guaranteed to leave this function
2891 with the dwarf queue empty. */
2892 dwarf2_queue_guard q_guard;
2893
2894 if (dwarf2_per_objfile->using_index
2895 ? per_cu->v.quick->compunit_symtab == NULL
2896 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2897 {
2898 queue_comp_unit (per_cu, language_minimal);
2899 load_cu (per_cu, skip_partial);
2900
2901 /* If we just loaded a CU from a DWO, and we're working with an index
2902 that may badly handle TUs, load all the TUs in that DWO as well.
2903 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2904 if (!per_cu->is_debug_types
2905 && per_cu->cu != NULL
2906 && per_cu->cu->dwo_unit != NULL
2907 && dwarf2_per_objfile->index_table != NULL
2908 && dwarf2_per_objfile->index_table->version <= 7
2909 /* DWP files aren't supported yet. */
2910 && get_dwp_file (dwarf2_per_objfile) == NULL)
2911 queue_and_load_all_dwo_tus (per_cu);
2912 }
2913
2914 process_queue (dwarf2_per_objfile);
2915
2916 /* Age the cache, releasing compilation units that have not
2917 been used recently. */
2918 age_cached_comp_units (dwarf2_per_objfile);
2919 }
2920
2921 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2922 the objfile from which this CU came. Returns the resulting symbol
2923 table. */
2924
2925 static struct compunit_symtab *
2926 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2927 {
2928 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2929
2930 gdb_assert (dwarf2_per_objfile->using_index);
2931 if (!per_cu->v.quick->compunit_symtab)
2932 {
2933 free_cached_comp_units freer (dwarf2_per_objfile);
2934 scoped_restore decrementer = increment_reading_symtab ();
2935 dw2_do_instantiate_symtab (per_cu, skip_partial);
2936 process_cu_includes (dwarf2_per_objfile);
2937 }
2938
2939 return per_cu->v.quick->compunit_symtab;
2940 }
2941
2942 /* See declaration. */
2943
2944 dwarf2_per_cu_data *
2945 dwarf2_per_objfile::get_cutu (int index)
2946 {
2947 if (index >= this->all_comp_units.size ())
2948 {
2949 index -= this->all_comp_units.size ();
2950 gdb_assert (index < this->all_type_units.size ());
2951 return &this->all_type_units[index]->per_cu;
2952 }
2953
2954 return this->all_comp_units[index];
2955 }
2956
2957 /* See declaration. */
2958
2959 dwarf2_per_cu_data *
2960 dwarf2_per_objfile::get_cu (int index)
2961 {
2962 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2963
2964 return this->all_comp_units[index];
2965 }
2966
2967 /* See declaration. */
2968
2969 signatured_type *
2970 dwarf2_per_objfile::get_tu (int index)
2971 {
2972 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2973
2974 return this->all_type_units[index];
2975 }
2976
2977 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2978 objfile_obstack, and constructed with the specified field
2979 values. */
2980
2981 static dwarf2_per_cu_data *
2982 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2983 struct dwarf2_section_info *section,
2984 int is_dwz,
2985 sect_offset sect_off, ULONGEST length)
2986 {
2987 struct objfile *objfile = dwarf2_per_objfile->objfile;
2988 dwarf2_per_cu_data *the_cu
2989 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2990 struct dwarf2_per_cu_data);
2991 the_cu->sect_off = sect_off;
2992 the_cu->length = length;
2993 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2994 the_cu->section = section;
2995 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2996 struct dwarf2_per_cu_quick_data);
2997 the_cu->is_dwz = is_dwz;
2998 return the_cu;
2999 }
3000
3001 /* A helper for create_cus_from_index that handles a given list of
3002 CUs. */
3003
3004 static void
3005 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3006 const gdb_byte *cu_list, offset_type n_elements,
3007 struct dwarf2_section_info *section,
3008 int is_dwz)
3009 {
3010 for (offset_type i = 0; i < n_elements; i += 2)
3011 {
3012 gdb_static_assert (sizeof (ULONGEST) >= 8);
3013
3014 sect_offset sect_off
3015 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3016 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3017 cu_list += 2 * 8;
3018
3019 dwarf2_per_cu_data *per_cu
3020 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3021 sect_off, length);
3022 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
3023 }
3024 }
3025
3026 /* Read the CU list from the mapped index, and use it to create all
3027 the CU objects for this objfile. */
3028
3029 static void
3030 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3031 const gdb_byte *cu_list, offset_type cu_list_elements,
3032 const gdb_byte *dwz_list, offset_type dwz_elements)
3033 {
3034 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3035 dwarf2_per_objfile->all_comp_units.reserve
3036 ((cu_list_elements + dwz_elements) / 2);
3037
3038 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
3039 &dwarf2_per_objfile->info, 0);
3040
3041 if (dwz_elements == 0)
3042 return;
3043
3044 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3045 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
3046 &dwz->info, 1);
3047 }
3048
3049 /* Create the signatured type hash table from the index. */
3050
3051 static void
3052 create_signatured_type_table_from_index
3053 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3054 struct dwarf2_section_info *section,
3055 const gdb_byte *bytes,
3056 offset_type elements)
3057 {
3058 struct objfile *objfile = dwarf2_per_objfile->objfile;
3059
3060 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3061 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
3062
3063 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3064
3065 for (offset_type i = 0; i < elements; i += 3)
3066 {
3067 struct signatured_type *sig_type;
3068 ULONGEST signature;
3069 void **slot;
3070 cu_offset type_offset_in_tu;
3071
3072 gdb_static_assert (sizeof (ULONGEST) >= 8);
3073 sect_offset sect_off
3074 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3075 type_offset_in_tu
3076 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3077 BFD_ENDIAN_LITTLE);
3078 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3079 bytes += 3 * 8;
3080
3081 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3082 struct signatured_type);
3083 sig_type->signature = signature;
3084 sig_type->type_offset_in_tu = type_offset_in_tu;
3085 sig_type->per_cu.is_debug_types = 1;
3086 sig_type->per_cu.section = section;
3087 sig_type->per_cu.sect_off = sect_off;
3088 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3089 sig_type->per_cu.v.quick
3090 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3091 struct dwarf2_per_cu_quick_data);
3092
3093 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3094 *slot = sig_type;
3095
3096 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3097 }
3098
3099 dwarf2_per_objfile->signatured_types = sig_types_hash;
3100 }
3101
3102 /* Create the signatured type hash table from .debug_names. */
3103
3104 static void
3105 create_signatured_type_table_from_debug_names
3106 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3107 const mapped_debug_names &map,
3108 struct dwarf2_section_info *section,
3109 struct dwarf2_section_info *abbrev_section)
3110 {
3111 struct objfile *objfile = dwarf2_per_objfile->objfile;
3112
3113 dwarf2_read_section (objfile, section);
3114 dwarf2_read_section (objfile, abbrev_section);
3115
3116 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3117 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
3118
3119 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3120
3121 for (uint32_t i = 0; i < map.tu_count; ++i)
3122 {
3123 struct signatured_type *sig_type;
3124 void **slot;
3125
3126 sect_offset sect_off
3127 = (sect_offset) (extract_unsigned_integer
3128 (map.tu_table_reordered + i * map.offset_size,
3129 map.offset_size,
3130 map.dwarf5_byte_order));
3131
3132 comp_unit_head cu_header;
3133 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3134 abbrev_section,
3135 section->buffer + to_underlying (sect_off),
3136 rcuh_kind::TYPE);
3137
3138 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3139 struct signatured_type);
3140 sig_type->signature = cu_header.signature;
3141 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3142 sig_type->per_cu.is_debug_types = 1;
3143 sig_type->per_cu.section = section;
3144 sig_type->per_cu.sect_off = sect_off;
3145 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3146 sig_type->per_cu.v.quick
3147 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3148 struct dwarf2_per_cu_quick_data);
3149
3150 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3151 *slot = sig_type;
3152
3153 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3154 }
3155
3156 dwarf2_per_objfile->signatured_types = sig_types_hash;
3157 }
3158
3159 /* Read the address map data from the mapped index, and use it to
3160 populate the objfile's psymtabs_addrmap. */
3161
3162 static void
3163 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3164 struct mapped_index *index)
3165 {
3166 struct objfile *objfile = dwarf2_per_objfile->objfile;
3167 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3168 const gdb_byte *iter, *end;
3169 struct addrmap *mutable_map;
3170 CORE_ADDR baseaddr;
3171
3172 auto_obstack temp_obstack;
3173
3174 mutable_map = addrmap_create_mutable (&temp_obstack);
3175
3176 iter = index->address_table.data ();
3177 end = iter + index->address_table.size ();
3178
3179 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3180
3181 while (iter < end)
3182 {
3183 ULONGEST hi, lo, cu_index;
3184 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3185 iter += 8;
3186 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3187 iter += 8;
3188 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3189 iter += 4;
3190
3191 if (lo > hi)
3192 {
3193 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
3194 hex_string (lo), hex_string (hi));
3195 continue;
3196 }
3197
3198 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
3199 {
3200 complaint (_(".gdb_index address table has invalid CU number %u"),
3201 (unsigned) cu_index);
3202 continue;
3203 }
3204
3205 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3206 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
3207 addrmap_set_empty (mutable_map, lo, hi - 1,
3208 dwarf2_per_objfile->get_cu (cu_index));
3209 }
3210
3211 objfile->partial_symtabs->psymtabs_addrmap
3212 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3213 }
3214
3215 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3216 populate the objfile's psymtabs_addrmap. */
3217
3218 static void
3219 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3220 struct dwarf2_section_info *section)
3221 {
3222 struct objfile *objfile = dwarf2_per_objfile->objfile;
3223 bfd *abfd = objfile->obfd;
3224 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3225 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3226 SECT_OFF_TEXT (objfile));
3227
3228 auto_obstack temp_obstack;
3229 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3230
3231 std::unordered_map<sect_offset,
3232 dwarf2_per_cu_data *,
3233 gdb::hash_enum<sect_offset>>
3234 debug_info_offset_to_per_cu;
3235 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3236 {
3237 const auto insertpair
3238 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3239 if (!insertpair.second)
3240 {
3241 warning (_("Section .debug_aranges in %s has duplicate "
3242 "debug_info_offset %s, ignoring .debug_aranges."),
3243 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3244 return;
3245 }
3246 }
3247
3248 dwarf2_read_section (objfile, section);
3249
3250 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3251
3252 const gdb_byte *addr = section->buffer;
3253
3254 while (addr < section->buffer + section->size)
3255 {
3256 const gdb_byte *const entry_addr = addr;
3257 unsigned int bytes_read;
3258
3259 const LONGEST entry_length = read_initial_length (abfd, addr,
3260 &bytes_read);
3261 addr += bytes_read;
3262
3263 const gdb_byte *const entry_end = addr + entry_length;
3264 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3265 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3266 if (addr + entry_length > section->buffer + section->size)
3267 {
3268 warning (_("Section .debug_aranges in %s entry at offset %s "
3269 "length %s exceeds section length %s, "
3270 "ignoring .debug_aranges."),
3271 objfile_name (objfile),
3272 plongest (entry_addr - section->buffer),
3273 plongest (bytes_read + entry_length),
3274 pulongest (section->size));
3275 return;
3276 }
3277
3278 /* The version number. */
3279 const uint16_t version = read_2_bytes (abfd, addr);
3280 addr += 2;
3281 if (version != 2)
3282 {
3283 warning (_("Section .debug_aranges in %s entry at offset %s "
3284 "has unsupported version %d, ignoring .debug_aranges."),
3285 objfile_name (objfile),
3286 plongest (entry_addr - section->buffer), version);
3287 return;
3288 }
3289
3290 const uint64_t debug_info_offset
3291 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3292 addr += offset_size;
3293 const auto per_cu_it
3294 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3295 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3296 {
3297 warning (_("Section .debug_aranges in %s entry at offset %s "
3298 "debug_info_offset %s does not exists, "
3299 "ignoring .debug_aranges."),
3300 objfile_name (objfile),
3301 plongest (entry_addr - section->buffer),
3302 pulongest (debug_info_offset));
3303 return;
3304 }
3305 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3306
3307 const uint8_t address_size = *addr++;
3308 if (address_size < 1 || address_size > 8)
3309 {
3310 warning (_("Section .debug_aranges in %s entry at offset %s "
3311 "address_size %u is invalid, ignoring .debug_aranges."),
3312 objfile_name (objfile),
3313 plongest (entry_addr - section->buffer), address_size);
3314 return;
3315 }
3316
3317 const uint8_t segment_selector_size = *addr++;
3318 if (segment_selector_size != 0)
3319 {
3320 warning (_("Section .debug_aranges in %s entry at offset %s "
3321 "segment_selector_size %u is not supported, "
3322 "ignoring .debug_aranges."),
3323 objfile_name (objfile),
3324 plongest (entry_addr - section->buffer),
3325 segment_selector_size);
3326 return;
3327 }
3328
3329 /* Must pad to an alignment boundary that is twice the address
3330 size. It is undocumented by the DWARF standard but GCC does
3331 use it. */
3332 for (size_t padding = ((-(addr - section->buffer))
3333 & (2 * address_size - 1));
3334 padding > 0; padding--)
3335 if (*addr++ != 0)
3336 {
3337 warning (_("Section .debug_aranges in %s entry at offset %s "
3338 "padding is not zero, ignoring .debug_aranges."),
3339 objfile_name (objfile),
3340 plongest (entry_addr - section->buffer));
3341 return;
3342 }
3343
3344 for (;;)
3345 {
3346 if (addr + 2 * address_size > entry_end)
3347 {
3348 warning (_("Section .debug_aranges in %s entry at offset %s "
3349 "address list is not properly terminated, "
3350 "ignoring .debug_aranges."),
3351 objfile_name (objfile),
3352 plongest (entry_addr - section->buffer));
3353 return;
3354 }
3355 ULONGEST start = extract_unsigned_integer (addr, address_size,
3356 dwarf5_byte_order);
3357 addr += address_size;
3358 ULONGEST length = extract_unsigned_integer (addr, address_size,
3359 dwarf5_byte_order);
3360 addr += address_size;
3361 if (start == 0 && length == 0)
3362 break;
3363 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3364 {
3365 /* Symbol was eliminated due to a COMDAT group. */
3366 continue;
3367 }
3368 ULONGEST end = start + length;
3369 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3370 - baseaddr);
3371 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3372 - baseaddr);
3373 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3374 }
3375 }
3376
3377 objfile->partial_symtabs->psymtabs_addrmap
3378 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3379 }
3380
3381 /* Find a slot in the mapped index INDEX for the object named NAME.
3382 If NAME is found, set *VEC_OUT to point to the CU vector in the
3383 constant pool and return true. If NAME cannot be found, return
3384 false. */
3385
3386 static bool
3387 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3388 offset_type **vec_out)
3389 {
3390 offset_type hash;
3391 offset_type slot, step;
3392 int (*cmp) (const char *, const char *);
3393
3394 gdb::unique_xmalloc_ptr<char> without_params;
3395 if (current_language->la_language == language_cplus
3396 || current_language->la_language == language_fortran
3397 || current_language->la_language == language_d)
3398 {
3399 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3400 not contain any. */
3401
3402 if (strchr (name, '(') != NULL)
3403 {
3404 without_params = cp_remove_params (name);
3405
3406 if (without_params != NULL)
3407 name = without_params.get ();
3408 }
3409 }
3410
3411 /* Index version 4 did not support case insensitive searches. But the
3412 indices for case insensitive languages are built in lowercase, therefore
3413 simulate our NAME being searched is also lowercased. */
3414 hash = mapped_index_string_hash ((index->version == 4
3415 && case_sensitivity == case_sensitive_off
3416 ? 5 : index->version),
3417 name);
3418
3419 slot = hash & (index->symbol_table.size () - 1);
3420 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3421 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3422
3423 for (;;)
3424 {
3425 const char *str;
3426
3427 const auto &bucket = index->symbol_table[slot];
3428 if (bucket.name == 0 && bucket.vec == 0)
3429 return false;
3430
3431 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3432 if (!cmp (name, str))
3433 {
3434 *vec_out = (offset_type *) (index->constant_pool
3435 + MAYBE_SWAP (bucket.vec));
3436 return true;
3437 }
3438
3439 slot = (slot + step) & (index->symbol_table.size () - 1);
3440 }
3441 }
3442
3443 /* A helper function that reads the .gdb_index from BUFFER and fills
3444 in MAP. FILENAME is the name of the file containing the data;
3445 it is used for error reporting. DEPRECATED_OK is true if it is
3446 ok to use deprecated sections.
3447
3448 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3449 out parameters that are filled in with information about the CU and
3450 TU lists in the section.
3451
3452 Returns true if all went well, false otherwise. */
3453
3454 static bool
3455 read_gdb_index_from_buffer (struct objfile *objfile,
3456 const char *filename,
3457 bool deprecated_ok,
3458 gdb::array_view<const gdb_byte> buffer,
3459 struct mapped_index *map,
3460 const gdb_byte **cu_list,
3461 offset_type *cu_list_elements,
3462 const gdb_byte **types_list,
3463 offset_type *types_list_elements)
3464 {
3465 const gdb_byte *addr = &buffer[0];
3466
3467 /* Version check. */
3468 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3469 /* Versions earlier than 3 emitted every copy of a psymbol. This
3470 causes the index to behave very poorly for certain requests. Version 3
3471 contained incomplete addrmap. So, it seems better to just ignore such
3472 indices. */
3473 if (version < 4)
3474 {
3475 static int warning_printed = 0;
3476 if (!warning_printed)
3477 {
3478 warning (_("Skipping obsolete .gdb_index section in %s."),
3479 filename);
3480 warning_printed = 1;
3481 }
3482 return 0;
3483 }
3484 /* Index version 4 uses a different hash function than index version
3485 5 and later.
3486
3487 Versions earlier than 6 did not emit psymbols for inlined
3488 functions. Using these files will cause GDB not to be able to
3489 set breakpoints on inlined functions by name, so we ignore these
3490 indices unless the user has done
3491 "set use-deprecated-index-sections on". */
3492 if (version < 6 && !deprecated_ok)
3493 {
3494 static int warning_printed = 0;
3495 if (!warning_printed)
3496 {
3497 warning (_("\
3498 Skipping deprecated .gdb_index section in %s.\n\
3499 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3500 to use the section anyway."),
3501 filename);
3502 warning_printed = 1;
3503 }
3504 return 0;
3505 }
3506 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3507 of the TU (for symbols coming from TUs),
3508 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3509 Plus gold-generated indices can have duplicate entries for global symbols,
3510 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3511 These are just performance bugs, and we can't distinguish gdb-generated
3512 indices from gold-generated ones, so issue no warning here. */
3513
3514 /* Indexes with higher version than the one supported by GDB may be no
3515 longer backward compatible. */
3516 if (version > 8)
3517 return 0;
3518
3519 map->version = version;
3520
3521 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3522
3523 int i = 0;
3524 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3525 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3526 / 8);
3527 ++i;
3528
3529 *types_list = addr + MAYBE_SWAP (metadata[i]);
3530 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3531 - MAYBE_SWAP (metadata[i]))
3532 / 8);
3533 ++i;
3534
3535 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3536 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3537 map->address_table
3538 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3539 ++i;
3540
3541 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3542 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3543 map->symbol_table
3544 = gdb::array_view<mapped_index::symbol_table_slot>
3545 ((mapped_index::symbol_table_slot *) symbol_table,
3546 (mapped_index::symbol_table_slot *) symbol_table_end);
3547
3548 ++i;
3549 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3550
3551 return 1;
3552 }
3553
3554 /* Callback types for dwarf2_read_gdb_index. */
3555
3556 typedef gdb::function_view
3557 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3558 get_gdb_index_contents_ftype;
3559 typedef gdb::function_view
3560 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3561 get_gdb_index_contents_dwz_ftype;
3562
3563 /* Read .gdb_index. If everything went ok, initialize the "quick"
3564 elements of all the CUs and return 1. Otherwise, return 0. */
3565
3566 static int
3567 dwarf2_read_gdb_index
3568 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3569 get_gdb_index_contents_ftype get_gdb_index_contents,
3570 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3571 {
3572 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3573 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3574 struct dwz_file *dwz;
3575 struct objfile *objfile = dwarf2_per_objfile->objfile;
3576
3577 gdb::array_view<const gdb_byte> main_index_contents
3578 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3579
3580 if (main_index_contents.empty ())
3581 return 0;
3582
3583 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3584 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3585 use_deprecated_index_sections,
3586 main_index_contents, map.get (), &cu_list,
3587 &cu_list_elements, &types_list,
3588 &types_list_elements))
3589 return 0;
3590
3591 /* Don't use the index if it's empty. */
3592 if (map->symbol_table.empty ())
3593 return 0;
3594
3595 /* If there is a .dwz file, read it so we can get its CU list as
3596 well. */
3597 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3598 if (dwz != NULL)
3599 {
3600 struct mapped_index dwz_map;
3601 const gdb_byte *dwz_types_ignore;
3602 offset_type dwz_types_elements_ignore;
3603
3604 gdb::array_view<const gdb_byte> dwz_index_content
3605 = get_gdb_index_contents_dwz (objfile, dwz);
3606
3607 if (dwz_index_content.empty ())
3608 return 0;
3609
3610 if (!read_gdb_index_from_buffer (objfile,
3611 bfd_get_filename (dwz->dwz_bfd.get ()),
3612 1, dwz_index_content, &dwz_map,
3613 &dwz_list, &dwz_list_elements,
3614 &dwz_types_ignore,
3615 &dwz_types_elements_ignore))
3616 {
3617 warning (_("could not read '.gdb_index' section from %s; skipping"),
3618 bfd_get_filename (dwz->dwz_bfd.get ()));
3619 return 0;
3620 }
3621 }
3622
3623 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3624 dwz_list, dwz_list_elements);
3625
3626 if (types_list_elements)
3627 {
3628 /* We can only handle a single .debug_types when we have an
3629 index. */
3630 if (dwarf2_per_objfile->types.size () != 1)
3631 return 0;
3632
3633 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3634
3635 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3636 types_list, types_list_elements);
3637 }
3638
3639 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3640
3641 dwarf2_per_objfile->index_table = std::move (map);
3642 dwarf2_per_objfile->using_index = 1;
3643 dwarf2_per_objfile->quick_file_names_table =
3644 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3645
3646 return 1;
3647 }
3648
3649 /* die_reader_func for dw2_get_file_names. */
3650
3651 static void
3652 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3653 const gdb_byte *info_ptr,
3654 struct die_info *comp_unit_die,
3655 int has_children,
3656 void *data)
3657 {
3658 struct dwarf2_cu *cu = reader->cu;
3659 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3660 struct dwarf2_per_objfile *dwarf2_per_objfile
3661 = cu->per_cu->dwarf2_per_objfile;
3662 struct objfile *objfile = dwarf2_per_objfile->objfile;
3663 struct dwarf2_per_cu_data *lh_cu;
3664 struct attribute *attr;
3665 void **slot;
3666 struct quick_file_names *qfn;
3667
3668 gdb_assert (! this_cu->is_debug_types);
3669
3670 /* Our callers never want to match partial units -- instead they
3671 will match the enclosing full CU. */
3672 if (comp_unit_die->tag == DW_TAG_partial_unit)
3673 {
3674 this_cu->v.quick->no_file_data = 1;
3675 return;
3676 }
3677
3678 lh_cu = this_cu;
3679 slot = NULL;
3680
3681 line_header_up lh;
3682 sect_offset line_offset {};
3683
3684 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3685 if (attr)
3686 {
3687 struct quick_file_names find_entry;
3688
3689 line_offset = (sect_offset) DW_UNSND (attr);
3690
3691 /* We may have already read in this line header (TU line header sharing).
3692 If we have we're done. */
3693 find_entry.hash.dwo_unit = cu->dwo_unit;
3694 find_entry.hash.line_sect_off = line_offset;
3695 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3696 &find_entry, INSERT);
3697 if (*slot != NULL)
3698 {
3699 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3700 return;
3701 }
3702
3703 lh = dwarf_decode_line_header (line_offset, cu);
3704 }
3705 if (lh == NULL)
3706 {
3707 lh_cu->v.quick->no_file_data = 1;
3708 return;
3709 }
3710
3711 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3712 qfn->hash.dwo_unit = cu->dwo_unit;
3713 qfn->hash.line_sect_off = line_offset;
3714 gdb_assert (slot != NULL);
3715 *slot = qfn;
3716
3717 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3718
3719 int offset = 0;
3720 if (strcmp (fnd.name, "<unknown>") != 0)
3721 ++offset;
3722
3723 qfn->num_file_names = offset + lh->file_names_size ();
3724 qfn->file_names =
3725 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3726 if (offset != 0)
3727 qfn->file_names[0] = xstrdup (fnd.name);
3728 for (int i = 0; i < lh->file_names_size (); ++i)
3729 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3730 qfn->real_names = NULL;
3731
3732 lh_cu->v.quick->file_names = qfn;
3733 }
3734
3735 /* A helper for the "quick" functions which attempts to read the line
3736 table for THIS_CU. */
3737
3738 static struct quick_file_names *
3739 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3740 {
3741 /* This should never be called for TUs. */
3742 gdb_assert (! this_cu->is_debug_types);
3743 /* Nor type unit groups. */
3744 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3745
3746 if (this_cu->v.quick->file_names != NULL)
3747 return this_cu->v.quick->file_names;
3748 /* If we know there is no line data, no point in looking again. */
3749 if (this_cu->v.quick->no_file_data)
3750 return NULL;
3751
3752 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3753
3754 if (this_cu->v.quick->no_file_data)
3755 return NULL;
3756 return this_cu->v.quick->file_names;
3757 }
3758
3759 /* A helper for the "quick" functions which computes and caches the
3760 real path for a given file name from the line table. */
3761
3762 static const char *
3763 dw2_get_real_path (struct objfile *objfile,
3764 struct quick_file_names *qfn, int index)
3765 {
3766 if (qfn->real_names == NULL)
3767 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3768 qfn->num_file_names, const char *);
3769
3770 if (qfn->real_names[index] == NULL)
3771 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3772
3773 return qfn->real_names[index];
3774 }
3775
3776 static struct symtab *
3777 dw2_find_last_source_symtab (struct objfile *objfile)
3778 {
3779 struct dwarf2_per_objfile *dwarf2_per_objfile
3780 = get_dwarf2_per_objfile (objfile);
3781 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3782 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3783
3784 if (cust == NULL)
3785 return NULL;
3786
3787 return compunit_primary_filetab (cust);
3788 }
3789
3790 /* Traversal function for dw2_forget_cached_source_info. */
3791
3792 static int
3793 dw2_free_cached_file_names (void **slot, void *info)
3794 {
3795 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3796
3797 if (file_data->real_names)
3798 {
3799 int i;
3800
3801 for (i = 0; i < file_data->num_file_names; ++i)
3802 {
3803 xfree ((void*) file_data->real_names[i]);
3804 file_data->real_names[i] = NULL;
3805 }
3806 }
3807
3808 return 1;
3809 }
3810
3811 static void
3812 dw2_forget_cached_source_info (struct objfile *objfile)
3813 {
3814 struct dwarf2_per_objfile *dwarf2_per_objfile
3815 = get_dwarf2_per_objfile (objfile);
3816
3817 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3818 dw2_free_cached_file_names, NULL);
3819 }
3820
3821 /* Helper function for dw2_map_symtabs_matching_filename that expands
3822 the symtabs and calls the iterator. */
3823
3824 static int
3825 dw2_map_expand_apply (struct objfile *objfile,
3826 struct dwarf2_per_cu_data *per_cu,
3827 const char *name, const char *real_path,
3828 gdb::function_view<bool (symtab *)> callback)
3829 {
3830 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3831
3832 /* Don't visit already-expanded CUs. */
3833 if (per_cu->v.quick->compunit_symtab)
3834 return 0;
3835
3836 /* This may expand more than one symtab, and we want to iterate over
3837 all of them. */
3838 dw2_instantiate_symtab (per_cu, false);
3839
3840 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3841 last_made, callback);
3842 }
3843
3844 /* Implementation of the map_symtabs_matching_filename method. */
3845
3846 static bool
3847 dw2_map_symtabs_matching_filename
3848 (struct objfile *objfile, const char *name, const char *real_path,
3849 gdb::function_view<bool (symtab *)> callback)
3850 {
3851 const char *name_basename = lbasename (name);
3852 struct dwarf2_per_objfile *dwarf2_per_objfile
3853 = get_dwarf2_per_objfile (objfile);
3854
3855 /* The rule is CUs specify all the files, including those used by
3856 any TU, so there's no need to scan TUs here. */
3857
3858 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3859 {
3860 /* We only need to look at symtabs not already expanded. */
3861 if (per_cu->v.quick->compunit_symtab)
3862 continue;
3863
3864 quick_file_names *file_data = dw2_get_file_names (per_cu);
3865 if (file_data == NULL)
3866 continue;
3867
3868 for (int j = 0; j < file_data->num_file_names; ++j)
3869 {
3870 const char *this_name = file_data->file_names[j];
3871 const char *this_real_name;
3872
3873 if (compare_filenames_for_search (this_name, name))
3874 {
3875 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3876 callback))
3877 return true;
3878 continue;
3879 }
3880
3881 /* Before we invoke realpath, which can get expensive when many
3882 files are involved, do a quick comparison of the basenames. */
3883 if (! basenames_may_differ
3884 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3885 continue;
3886
3887 this_real_name = dw2_get_real_path (objfile, file_data, j);
3888 if (compare_filenames_for_search (this_real_name, name))
3889 {
3890 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3891 callback))
3892 return true;
3893 continue;
3894 }
3895
3896 if (real_path != NULL)
3897 {
3898 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3899 gdb_assert (IS_ABSOLUTE_PATH (name));
3900 if (this_real_name != NULL
3901 && FILENAME_CMP (real_path, this_real_name) == 0)
3902 {
3903 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3904 callback))
3905 return true;
3906 continue;
3907 }
3908 }
3909 }
3910 }
3911
3912 return false;
3913 }
3914
3915 /* Struct used to manage iterating over all CUs looking for a symbol. */
3916
3917 struct dw2_symtab_iterator
3918 {
3919 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3920 struct dwarf2_per_objfile *dwarf2_per_objfile;
3921 /* If set, only look for symbols that match that block. Valid values are
3922 GLOBAL_BLOCK and STATIC_BLOCK. */
3923 gdb::optional<block_enum> block_index;
3924 /* The kind of symbol we're looking for. */
3925 domain_enum domain;
3926 /* The list of CUs from the index entry of the symbol,
3927 or NULL if not found. */
3928 offset_type *vec;
3929 /* The next element in VEC to look at. */
3930 int next;
3931 /* The number of elements in VEC, or zero if there is no match. */
3932 int length;
3933 /* Have we seen a global version of the symbol?
3934 If so we can ignore all further global instances.
3935 This is to work around gold/15646, inefficient gold-generated
3936 indices. */
3937 int global_seen;
3938 };
3939
3940 /* Initialize the index symtab iterator ITER. */
3941
3942 static void
3943 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3944 struct dwarf2_per_objfile *dwarf2_per_objfile,
3945 gdb::optional<block_enum> block_index,
3946 domain_enum domain,
3947 const char *name)
3948 {
3949 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3950 iter->block_index = block_index;
3951 iter->domain = domain;
3952 iter->next = 0;
3953 iter->global_seen = 0;
3954
3955 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3956
3957 /* index is NULL if OBJF_READNOW. */
3958 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3959 iter->length = MAYBE_SWAP (*iter->vec);
3960 else
3961 {
3962 iter->vec = NULL;
3963 iter->length = 0;
3964 }
3965 }
3966
3967 /* Return the next matching CU or NULL if there are no more. */
3968
3969 static struct dwarf2_per_cu_data *
3970 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3971 {
3972 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3973
3974 for ( ; iter->next < iter->length; ++iter->next)
3975 {
3976 offset_type cu_index_and_attrs =
3977 MAYBE_SWAP (iter->vec[iter->next + 1]);
3978 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3979 gdb_index_symbol_kind symbol_kind =
3980 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3981 /* Only check the symbol attributes if they're present.
3982 Indices prior to version 7 don't record them,
3983 and indices >= 7 may elide them for certain symbols
3984 (gold does this). */
3985 int attrs_valid =
3986 (dwarf2_per_objfile->index_table->version >= 7
3987 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3988
3989 /* Don't crash on bad data. */
3990 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3991 + dwarf2_per_objfile->all_type_units.size ()))
3992 {
3993 complaint (_(".gdb_index entry has bad CU index"
3994 " [in module %s]"),
3995 objfile_name (dwarf2_per_objfile->objfile));
3996 continue;
3997 }
3998
3999 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4000
4001 /* Skip if already read in. */
4002 if (per_cu->v.quick->compunit_symtab)
4003 continue;
4004
4005 /* Check static vs global. */
4006 if (attrs_valid)
4007 {
4008 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4009
4010 if (iter->block_index.has_value ())
4011 {
4012 bool want_static = *iter->block_index == STATIC_BLOCK;
4013
4014 if (is_static != want_static)
4015 continue;
4016 }
4017
4018 /* Work around gold/15646. */
4019 if (!is_static && iter->global_seen)
4020 continue;
4021 if (!is_static)
4022 iter->global_seen = 1;
4023 }
4024
4025 /* Only check the symbol's kind if it has one. */
4026 if (attrs_valid)
4027 {
4028 switch (iter->domain)
4029 {
4030 case VAR_DOMAIN:
4031 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4032 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4033 /* Some types are also in VAR_DOMAIN. */
4034 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4035 continue;
4036 break;
4037 case STRUCT_DOMAIN:
4038 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4039 continue;
4040 break;
4041 case LABEL_DOMAIN:
4042 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4043 continue;
4044 break;
4045 default:
4046 break;
4047 }
4048 }
4049
4050 ++iter->next;
4051 return per_cu;
4052 }
4053
4054 return NULL;
4055 }
4056
4057 static struct compunit_symtab *
4058 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
4059 const char *name, domain_enum domain)
4060 {
4061 struct compunit_symtab *stab_best = NULL;
4062 struct dwarf2_per_objfile *dwarf2_per_objfile
4063 = get_dwarf2_per_objfile (objfile);
4064
4065 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4066
4067 struct dw2_symtab_iterator iter;
4068 struct dwarf2_per_cu_data *per_cu;
4069
4070 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
4071
4072 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4073 {
4074 struct symbol *sym, *with_opaque = NULL;
4075 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
4076 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4077 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4078
4079 sym = block_find_symbol (block, name, domain,
4080 block_find_non_opaque_type_preferred,
4081 &with_opaque);
4082
4083 /* Some caution must be observed with overloaded functions
4084 and methods, since the index will not contain any overload
4085 information (but NAME might contain it). */
4086
4087 if (sym != NULL
4088 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4089 return stab;
4090 if (with_opaque != NULL
4091 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4092 stab_best = stab;
4093
4094 /* Keep looking through other CUs. */
4095 }
4096
4097 return stab_best;
4098 }
4099
4100 static void
4101 dw2_print_stats (struct objfile *objfile)
4102 {
4103 struct dwarf2_per_objfile *dwarf2_per_objfile
4104 = get_dwarf2_per_objfile (objfile);
4105 int total = (dwarf2_per_objfile->all_comp_units.size ()
4106 + dwarf2_per_objfile->all_type_units.size ());
4107 int count = 0;
4108
4109 for (int i = 0; i < total; ++i)
4110 {
4111 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4112
4113 if (!per_cu->v.quick->compunit_symtab)
4114 ++count;
4115 }
4116 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4117 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4118 }
4119
4120 /* This dumps minimal information about the index.
4121 It is called via "mt print objfiles".
4122 One use is to verify .gdb_index has been loaded by the
4123 gdb.dwarf2/gdb-index.exp testcase. */
4124
4125 static void
4126 dw2_dump (struct objfile *objfile)
4127 {
4128 struct dwarf2_per_objfile *dwarf2_per_objfile
4129 = get_dwarf2_per_objfile (objfile);
4130
4131 gdb_assert (dwarf2_per_objfile->using_index);
4132 printf_filtered (".gdb_index:");
4133 if (dwarf2_per_objfile->index_table != NULL)
4134 {
4135 printf_filtered (" version %d\n",
4136 dwarf2_per_objfile->index_table->version);
4137 }
4138 else
4139 printf_filtered (" faked for \"readnow\"\n");
4140 printf_filtered ("\n");
4141 }
4142
4143 static void
4144 dw2_expand_symtabs_for_function (struct objfile *objfile,
4145 const char *func_name)
4146 {
4147 struct dwarf2_per_objfile *dwarf2_per_objfile
4148 = get_dwarf2_per_objfile (objfile);
4149
4150 struct dw2_symtab_iterator iter;
4151 struct dwarf2_per_cu_data *per_cu;
4152
4153 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
4154
4155 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4156 dw2_instantiate_symtab (per_cu, false);
4157
4158 }
4159
4160 static void
4161 dw2_expand_all_symtabs (struct objfile *objfile)
4162 {
4163 struct dwarf2_per_objfile *dwarf2_per_objfile
4164 = get_dwarf2_per_objfile (objfile);
4165 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
4166 + dwarf2_per_objfile->all_type_units.size ());
4167
4168 for (int i = 0; i < total_units; ++i)
4169 {
4170 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4171
4172 /* We don't want to directly expand a partial CU, because if we
4173 read it with the wrong language, then assertion failures can
4174 be triggered later on. See PR symtab/23010. So, tell
4175 dw2_instantiate_symtab to skip partial CUs -- any important
4176 partial CU will be read via DW_TAG_imported_unit anyway. */
4177 dw2_instantiate_symtab (per_cu, true);
4178 }
4179 }
4180
4181 static void
4182 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4183 const char *fullname)
4184 {
4185 struct dwarf2_per_objfile *dwarf2_per_objfile
4186 = get_dwarf2_per_objfile (objfile);
4187
4188 /* We don't need to consider type units here.
4189 This is only called for examining code, e.g. expand_line_sal.
4190 There can be an order of magnitude (or more) more type units
4191 than comp units, and we avoid them if we can. */
4192
4193 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4194 {
4195 /* We only need to look at symtabs not already expanded. */
4196 if (per_cu->v.quick->compunit_symtab)
4197 continue;
4198
4199 quick_file_names *file_data = dw2_get_file_names (per_cu);
4200 if (file_data == NULL)
4201 continue;
4202
4203 for (int j = 0; j < file_data->num_file_names; ++j)
4204 {
4205 const char *this_fullname = file_data->file_names[j];
4206
4207 if (filename_cmp (this_fullname, fullname) == 0)
4208 {
4209 dw2_instantiate_symtab (per_cu, false);
4210 break;
4211 }
4212 }
4213 }
4214 }
4215
4216 static void
4217 dw2_map_matching_symbols
4218 (struct objfile *objfile,
4219 const lookup_name_info &name, domain_enum domain,
4220 int global,
4221 gdb::function_view<symbol_found_callback_ftype> callback,
4222 symbol_compare_ftype *ordered_compare)
4223 {
4224 /* Currently unimplemented; used for Ada. The function can be called if the
4225 current language is Ada for a non-Ada objfile using GNU index. As Ada
4226 does not look for non-Ada symbols this function should just return. */
4227 }
4228
4229 /* Starting from a search name, return the string that finds the upper
4230 bound of all strings that start with SEARCH_NAME in a sorted name
4231 list. Returns the empty string to indicate that the upper bound is
4232 the end of the list. */
4233
4234 static std::string
4235 make_sort_after_prefix_name (const char *search_name)
4236 {
4237 /* When looking to complete "func", we find the upper bound of all
4238 symbols that start with "func" by looking for where we'd insert
4239 the closest string that would follow "func" in lexicographical
4240 order. Usually, that's "func"-with-last-character-incremented,
4241 i.e. "fund". Mind non-ASCII characters, though. Usually those
4242 will be UTF-8 multi-byte sequences, but we can't be certain.
4243 Especially mind the 0xff character, which is a valid character in
4244 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4245 rule out compilers allowing it in identifiers. Note that
4246 conveniently, strcmp/strcasecmp are specified to compare
4247 characters interpreted as unsigned char. So what we do is treat
4248 the whole string as a base 256 number composed of a sequence of
4249 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4250 to 0, and carries 1 to the following more-significant position.
4251 If the very first character in SEARCH_NAME ends up incremented
4252 and carries/overflows, then the upper bound is the end of the
4253 list. The string after the empty string is also the empty
4254 string.
4255
4256 Some examples of this operation:
4257
4258 SEARCH_NAME => "+1" RESULT
4259
4260 "abc" => "abd"
4261 "ab\xff" => "ac"
4262 "\xff" "a" "\xff" => "\xff" "b"
4263 "\xff" => ""
4264 "\xff\xff" => ""
4265 "" => ""
4266
4267 Then, with these symbols for example:
4268
4269 func
4270 func1
4271 fund
4272
4273 completing "func" looks for symbols between "func" and
4274 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4275 which finds "func" and "func1", but not "fund".
4276
4277 And with:
4278
4279 funcÿ (Latin1 'ÿ' [0xff])
4280 funcÿ1
4281 fund
4282
4283 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4284 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4285
4286 And with:
4287
4288 ÿÿ (Latin1 'ÿ' [0xff])
4289 ÿÿ1
4290
4291 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4292 the end of the list.
4293 */
4294 std::string after = search_name;
4295 while (!after.empty () && (unsigned char) after.back () == 0xff)
4296 after.pop_back ();
4297 if (!after.empty ())
4298 after.back () = (unsigned char) after.back () + 1;
4299 return after;
4300 }
4301
4302 /* See declaration. */
4303
4304 std::pair<std::vector<name_component>::const_iterator,
4305 std::vector<name_component>::const_iterator>
4306 mapped_index_base::find_name_components_bounds
4307 (const lookup_name_info &lookup_name_without_params, language lang) const
4308 {
4309 auto *name_cmp
4310 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4311
4312 const char *lang_name
4313 = lookup_name_without_params.language_lookup_name (lang).c_str ();
4314
4315 /* Comparison function object for lower_bound that matches against a
4316 given symbol name. */
4317 auto lookup_compare_lower = [&] (const name_component &elem,
4318 const char *name)
4319 {
4320 const char *elem_qualified = this->symbol_name_at (elem.idx);
4321 const char *elem_name = elem_qualified + elem.name_offset;
4322 return name_cmp (elem_name, name) < 0;
4323 };
4324
4325 /* Comparison function object for upper_bound that matches against a
4326 given symbol name. */
4327 auto lookup_compare_upper = [&] (const char *name,
4328 const name_component &elem)
4329 {
4330 const char *elem_qualified = this->symbol_name_at (elem.idx);
4331 const char *elem_name = elem_qualified + elem.name_offset;
4332 return name_cmp (name, elem_name) < 0;
4333 };
4334
4335 auto begin = this->name_components.begin ();
4336 auto end = this->name_components.end ();
4337
4338 /* Find the lower bound. */
4339 auto lower = [&] ()
4340 {
4341 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4342 return begin;
4343 else
4344 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4345 } ();
4346
4347 /* Find the upper bound. */
4348 auto upper = [&] ()
4349 {
4350 if (lookup_name_without_params.completion_mode ())
4351 {
4352 /* In completion mode, we want UPPER to point past all
4353 symbols names that have the same prefix. I.e., with
4354 these symbols, and completing "func":
4355
4356 function << lower bound
4357 function1
4358 other_function << upper bound
4359
4360 We find the upper bound by looking for the insertion
4361 point of "func"-with-last-character-incremented,
4362 i.e. "fund". */
4363 std::string after = make_sort_after_prefix_name (lang_name);
4364 if (after.empty ())
4365 return end;
4366 return std::lower_bound (lower, end, after.c_str (),
4367 lookup_compare_lower);
4368 }
4369 else
4370 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4371 } ();
4372
4373 return {lower, upper};
4374 }
4375
4376 /* See declaration. */
4377
4378 void
4379 mapped_index_base::build_name_components ()
4380 {
4381 if (!this->name_components.empty ())
4382 return;
4383
4384 this->name_components_casing = case_sensitivity;
4385 auto *name_cmp
4386 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4387
4388 /* The code below only knows how to break apart components of C++
4389 symbol names (and other languages that use '::' as
4390 namespace/module separator) and Ada symbol names. */
4391 auto count = this->symbol_name_count ();
4392 for (offset_type idx = 0; idx < count; idx++)
4393 {
4394 if (this->symbol_name_slot_invalid (idx))
4395 continue;
4396
4397 const char *name = this->symbol_name_at (idx);
4398
4399 /* Add each name component to the name component table. */
4400 unsigned int previous_len = 0;
4401
4402 if (strstr (name, "::") != nullptr)
4403 {
4404 for (unsigned int current_len = cp_find_first_component (name);
4405 name[current_len] != '\0';
4406 current_len += cp_find_first_component (name + current_len))
4407 {
4408 gdb_assert (name[current_len] == ':');
4409 this->name_components.push_back ({previous_len, idx});
4410 /* Skip the '::'. */
4411 current_len += 2;
4412 previous_len = current_len;
4413 }
4414 }
4415 else
4416 {
4417 /* Handle the Ada encoded (aka mangled) form here. */
4418 for (const char *iter = strstr (name, "__");
4419 iter != nullptr;
4420 iter = strstr (iter, "__"))
4421 {
4422 this->name_components.push_back ({previous_len, idx});
4423 iter += 2;
4424 previous_len = iter - name;
4425 }
4426 }
4427
4428 this->name_components.push_back ({previous_len, idx});
4429 }
4430
4431 /* Sort name_components elements by name. */
4432 auto name_comp_compare = [&] (const name_component &left,
4433 const name_component &right)
4434 {
4435 const char *left_qualified = this->symbol_name_at (left.idx);
4436 const char *right_qualified = this->symbol_name_at (right.idx);
4437
4438 const char *left_name = left_qualified + left.name_offset;
4439 const char *right_name = right_qualified + right.name_offset;
4440
4441 return name_cmp (left_name, right_name) < 0;
4442 };
4443
4444 std::sort (this->name_components.begin (),
4445 this->name_components.end (),
4446 name_comp_compare);
4447 }
4448
4449 /* Helper for dw2_expand_symtabs_matching that works with a
4450 mapped_index_base instead of the containing objfile. This is split
4451 to a separate function in order to be able to unit test the
4452 name_components matching using a mock mapped_index_base. For each
4453 symbol name that matches, calls MATCH_CALLBACK, passing it the
4454 symbol's index in the mapped_index_base symbol table. */
4455
4456 static void
4457 dw2_expand_symtabs_matching_symbol
4458 (mapped_index_base &index,
4459 const lookup_name_info &lookup_name_in,
4460 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4461 enum search_domain kind,
4462 gdb::function_view<bool (offset_type)> match_callback)
4463 {
4464 lookup_name_info lookup_name_without_params
4465 = lookup_name_in.make_ignore_params ();
4466
4467 /* Build the symbol name component sorted vector, if we haven't
4468 yet. */
4469 index.build_name_components ();
4470
4471 /* The same symbol may appear more than once in the range though.
4472 E.g., if we're looking for symbols that complete "w", and we have
4473 a symbol named "w1::w2", we'll find the two name components for
4474 that same symbol in the range. To be sure we only call the
4475 callback once per symbol, we first collect the symbol name
4476 indexes that matched in a temporary vector and ignore
4477 duplicates. */
4478 std::vector<offset_type> matches;
4479
4480 struct name_and_matcher
4481 {
4482 symbol_name_matcher_ftype *matcher;
4483 const std::string &name;
4484
4485 bool operator== (const name_and_matcher &other) const
4486 {
4487 return matcher == other.matcher && name == other.name;
4488 }
4489 };
4490
4491 /* A vector holding all the different symbol name matchers, for all
4492 languages. */
4493 std::vector<name_and_matcher> matchers;
4494
4495 for (int i = 0; i < nr_languages; i++)
4496 {
4497 enum language lang_e = (enum language) i;
4498
4499 const language_defn *lang = language_def (lang_e);
4500 symbol_name_matcher_ftype *name_matcher
4501 = get_symbol_name_matcher (lang, lookup_name_without_params);
4502
4503 name_and_matcher key {
4504 name_matcher,
4505 lookup_name_without_params.language_lookup_name (lang_e)
4506 };
4507
4508 /* Don't insert the same comparison routine more than once.
4509 Note that we do this linear walk. This is not a problem in
4510 practice because the number of supported languages is
4511 low. */
4512 if (std::find (matchers.begin (), matchers.end (), key)
4513 != matchers.end ())
4514 continue;
4515 matchers.push_back (std::move (key));
4516
4517 auto bounds
4518 = index.find_name_components_bounds (lookup_name_without_params,
4519 lang_e);
4520
4521 /* Now for each symbol name in range, check to see if we have a name
4522 match, and if so, call the MATCH_CALLBACK callback. */
4523
4524 for (; bounds.first != bounds.second; ++bounds.first)
4525 {
4526 const char *qualified = index.symbol_name_at (bounds.first->idx);
4527
4528 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4529 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4530 continue;
4531
4532 matches.push_back (bounds.first->idx);
4533 }
4534 }
4535
4536 std::sort (matches.begin (), matches.end ());
4537
4538 /* Finally call the callback, once per match. */
4539 ULONGEST prev = -1;
4540 for (offset_type idx : matches)
4541 {
4542 if (prev != idx)
4543 {
4544 if (!match_callback (idx))
4545 break;
4546 prev = idx;
4547 }
4548 }
4549
4550 /* Above we use a type wider than idx's for 'prev', since 0 and
4551 (offset_type)-1 are both possible values. */
4552 static_assert (sizeof (prev) > sizeof (offset_type), "");
4553 }
4554
4555 #if GDB_SELF_TEST
4556
4557 namespace selftests { namespace dw2_expand_symtabs_matching {
4558
4559 /* A mock .gdb_index/.debug_names-like name index table, enough to
4560 exercise dw2_expand_symtabs_matching_symbol, which works with the
4561 mapped_index_base interface. Builds an index from the symbol list
4562 passed as parameter to the constructor. */
4563 class mock_mapped_index : public mapped_index_base
4564 {
4565 public:
4566 mock_mapped_index (gdb::array_view<const char *> symbols)
4567 : m_symbol_table (symbols)
4568 {}
4569
4570 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4571
4572 /* Return the number of names in the symbol table. */
4573 size_t symbol_name_count () const override
4574 {
4575 return m_symbol_table.size ();
4576 }
4577
4578 /* Get the name of the symbol at IDX in the symbol table. */
4579 const char *symbol_name_at (offset_type idx) const override
4580 {
4581 return m_symbol_table[idx];
4582 }
4583
4584 private:
4585 gdb::array_view<const char *> m_symbol_table;
4586 };
4587
4588 /* Convenience function that converts a NULL pointer to a "<null>"
4589 string, to pass to print routines. */
4590
4591 static const char *
4592 string_or_null (const char *str)
4593 {
4594 return str != NULL ? str : "<null>";
4595 }
4596
4597 /* Check if a lookup_name_info built from
4598 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4599 index. EXPECTED_LIST is the list of expected matches, in expected
4600 matching order. If no match expected, then an empty list is
4601 specified. Returns true on success. On failure prints a warning
4602 indicating the file:line that failed, and returns false. */
4603
4604 static bool
4605 check_match (const char *file, int line,
4606 mock_mapped_index &mock_index,
4607 const char *name, symbol_name_match_type match_type,
4608 bool completion_mode,
4609 std::initializer_list<const char *> expected_list)
4610 {
4611 lookup_name_info lookup_name (name, match_type, completion_mode);
4612
4613 bool matched = true;
4614
4615 auto mismatch = [&] (const char *expected_str,
4616 const char *got)
4617 {
4618 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4619 "expected=\"%s\", got=\"%s\"\n"),
4620 file, line,
4621 (match_type == symbol_name_match_type::FULL
4622 ? "FULL" : "WILD"),
4623 name, string_or_null (expected_str), string_or_null (got));
4624 matched = false;
4625 };
4626
4627 auto expected_it = expected_list.begin ();
4628 auto expected_end = expected_list.end ();
4629
4630 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4631 NULL, ALL_DOMAIN,
4632 [&] (offset_type idx)
4633 {
4634 const char *matched_name = mock_index.symbol_name_at (idx);
4635 const char *expected_str
4636 = expected_it == expected_end ? NULL : *expected_it++;
4637
4638 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4639 mismatch (expected_str, matched_name);
4640 return true;
4641 });
4642
4643 const char *expected_str
4644 = expected_it == expected_end ? NULL : *expected_it++;
4645 if (expected_str != NULL)
4646 mismatch (expected_str, NULL);
4647
4648 return matched;
4649 }
4650
4651 /* The symbols added to the mock mapped_index for testing (in
4652 canonical form). */
4653 static const char *test_symbols[] = {
4654 "function",
4655 "std::bar",
4656 "std::zfunction",
4657 "std::zfunction2",
4658 "w1::w2",
4659 "ns::foo<char*>",
4660 "ns::foo<int>",
4661 "ns::foo<long>",
4662 "ns2::tmpl<int>::foo2",
4663 "(anonymous namespace)::A::B::C",
4664
4665 /* These are used to check that the increment-last-char in the
4666 matching algorithm for completion doesn't match "t1_fund" when
4667 completing "t1_func". */
4668 "t1_func",
4669 "t1_func1",
4670 "t1_fund",
4671 "t1_fund1",
4672
4673 /* A UTF-8 name with multi-byte sequences to make sure that
4674 cp-name-parser understands this as a single identifier ("função"
4675 is "function" in PT). */
4676 u8"u8função",
4677
4678 /* \377 (0xff) is Latin1 'ÿ'. */
4679 "yfunc\377",
4680
4681 /* \377 (0xff) is Latin1 'ÿ'. */
4682 "\377",
4683 "\377\377123",
4684
4685 /* A name with all sorts of complications. Starts with "z" to make
4686 it easier for the completion tests below. */
4687 #define Z_SYM_NAME \
4688 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4689 "::tuple<(anonymous namespace)::ui*, " \
4690 "std::default_delete<(anonymous namespace)::ui>, void>"
4691
4692 Z_SYM_NAME
4693 };
4694
4695 /* Returns true if the mapped_index_base::find_name_component_bounds
4696 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4697 in completion mode. */
4698
4699 static bool
4700 check_find_bounds_finds (mapped_index_base &index,
4701 const char *search_name,
4702 gdb::array_view<const char *> expected_syms)
4703 {
4704 lookup_name_info lookup_name (search_name,
4705 symbol_name_match_type::FULL, true);
4706
4707 auto bounds = index.find_name_components_bounds (lookup_name,
4708 language_cplus);
4709
4710 size_t distance = std::distance (bounds.first, bounds.second);
4711 if (distance != expected_syms.size ())
4712 return false;
4713
4714 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4715 {
4716 auto nc_elem = bounds.first + exp_elem;
4717 const char *qualified = index.symbol_name_at (nc_elem->idx);
4718 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4719 return false;
4720 }
4721
4722 return true;
4723 }
4724
4725 /* Test the lower-level mapped_index::find_name_component_bounds
4726 method. */
4727
4728 static void
4729 test_mapped_index_find_name_component_bounds ()
4730 {
4731 mock_mapped_index mock_index (test_symbols);
4732
4733 mock_index.build_name_components ();
4734
4735 /* Test the lower-level mapped_index::find_name_component_bounds
4736 method in completion mode. */
4737 {
4738 static const char *expected_syms[] = {
4739 "t1_func",
4740 "t1_func1",
4741 };
4742
4743 SELF_CHECK (check_find_bounds_finds (mock_index,
4744 "t1_func", expected_syms));
4745 }
4746
4747 /* Check that the increment-last-char in the name matching algorithm
4748 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4749 {
4750 static const char *expected_syms1[] = {
4751 "\377",
4752 "\377\377123",
4753 };
4754 SELF_CHECK (check_find_bounds_finds (mock_index,
4755 "\377", expected_syms1));
4756
4757 static const char *expected_syms2[] = {
4758 "\377\377123",
4759 };
4760 SELF_CHECK (check_find_bounds_finds (mock_index,
4761 "\377\377", expected_syms2));
4762 }
4763 }
4764
4765 /* Test dw2_expand_symtabs_matching_symbol. */
4766
4767 static void
4768 test_dw2_expand_symtabs_matching_symbol ()
4769 {
4770 mock_mapped_index mock_index (test_symbols);
4771
4772 /* We let all tests run until the end even if some fails, for debug
4773 convenience. */
4774 bool any_mismatch = false;
4775
4776 /* Create the expected symbols list (an initializer_list). Needed
4777 because lists have commas, and we need to pass them to CHECK,
4778 which is a macro. */
4779 #define EXPECT(...) { __VA_ARGS__ }
4780
4781 /* Wrapper for check_match that passes down the current
4782 __FILE__/__LINE__. */
4783 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4784 any_mismatch |= !check_match (__FILE__, __LINE__, \
4785 mock_index, \
4786 NAME, MATCH_TYPE, COMPLETION_MODE, \
4787 EXPECTED_LIST)
4788
4789 /* Identity checks. */
4790 for (const char *sym : test_symbols)
4791 {
4792 /* Should be able to match all existing symbols. */
4793 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4794 EXPECT (sym));
4795
4796 /* Should be able to match all existing symbols with
4797 parameters. */
4798 std::string with_params = std::string (sym) + "(int)";
4799 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4800 EXPECT (sym));
4801
4802 /* Should be able to match all existing symbols with
4803 parameters and qualifiers. */
4804 with_params = std::string (sym) + " ( int ) const";
4805 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4806 EXPECT (sym));
4807
4808 /* This should really find sym, but cp-name-parser.y doesn't
4809 know about lvalue/rvalue qualifiers yet. */
4810 with_params = std::string (sym) + " ( int ) &&";
4811 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4812 {});
4813 }
4814
4815 /* Check that the name matching algorithm for completion doesn't get
4816 confused with Latin1 'ÿ' / 0xff. */
4817 {
4818 static const char str[] = "\377";
4819 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4820 EXPECT ("\377", "\377\377123"));
4821 }
4822
4823 /* Check that the increment-last-char in the matching algorithm for
4824 completion doesn't match "t1_fund" when completing "t1_func". */
4825 {
4826 static const char str[] = "t1_func";
4827 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4828 EXPECT ("t1_func", "t1_func1"));
4829 }
4830
4831 /* Check that completion mode works at each prefix of the expected
4832 symbol name. */
4833 {
4834 static const char str[] = "function(int)";
4835 size_t len = strlen (str);
4836 std::string lookup;
4837
4838 for (size_t i = 1; i < len; i++)
4839 {
4840 lookup.assign (str, i);
4841 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4842 EXPECT ("function"));
4843 }
4844 }
4845
4846 /* While "w" is a prefix of both components, the match function
4847 should still only be called once. */
4848 {
4849 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4850 EXPECT ("w1::w2"));
4851 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4852 EXPECT ("w1::w2"));
4853 }
4854
4855 /* Same, with a "complicated" symbol. */
4856 {
4857 static const char str[] = Z_SYM_NAME;
4858 size_t len = strlen (str);
4859 std::string lookup;
4860
4861 for (size_t i = 1; i < len; i++)
4862 {
4863 lookup.assign (str, i);
4864 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4865 EXPECT (Z_SYM_NAME));
4866 }
4867 }
4868
4869 /* In FULL mode, an incomplete symbol doesn't match. */
4870 {
4871 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4872 {});
4873 }
4874
4875 /* A complete symbol with parameters matches any overload, since the
4876 index has no overload info. */
4877 {
4878 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4879 EXPECT ("std::zfunction", "std::zfunction2"));
4880 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4881 EXPECT ("std::zfunction", "std::zfunction2"));
4882 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4883 EXPECT ("std::zfunction", "std::zfunction2"));
4884 }
4885
4886 /* Check that whitespace is ignored appropriately. A symbol with a
4887 template argument list. */
4888 {
4889 static const char expected[] = "ns::foo<int>";
4890 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4891 EXPECT (expected));
4892 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4893 EXPECT (expected));
4894 }
4895
4896 /* Check that whitespace is ignored appropriately. A symbol with a
4897 template argument list that includes a pointer. */
4898 {
4899 static const char expected[] = "ns::foo<char*>";
4900 /* Try both completion and non-completion modes. */
4901 static const bool completion_mode[2] = {false, true};
4902 for (size_t i = 0; i < 2; i++)
4903 {
4904 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4905 completion_mode[i], EXPECT (expected));
4906 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4907 completion_mode[i], EXPECT (expected));
4908
4909 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4910 completion_mode[i], EXPECT (expected));
4911 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4912 completion_mode[i], EXPECT (expected));
4913 }
4914 }
4915
4916 {
4917 /* Check method qualifiers are ignored. */
4918 static const char expected[] = "ns::foo<char*>";
4919 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4920 symbol_name_match_type::FULL, true, EXPECT (expected));
4921 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4922 symbol_name_match_type::FULL, true, EXPECT (expected));
4923 CHECK_MATCH ("foo < char * > ( int ) const",
4924 symbol_name_match_type::WILD, true, EXPECT (expected));
4925 CHECK_MATCH ("foo < char * > ( int ) &&",
4926 symbol_name_match_type::WILD, true, EXPECT (expected));
4927 }
4928
4929 /* Test lookup names that don't match anything. */
4930 {
4931 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4932 {});
4933
4934 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4935 {});
4936 }
4937
4938 /* Some wild matching tests, exercising "(anonymous namespace)",
4939 which should not be confused with a parameter list. */
4940 {
4941 static const char *syms[] = {
4942 "A::B::C",
4943 "B::C",
4944 "C",
4945 "A :: B :: C ( int )",
4946 "B :: C ( int )",
4947 "C ( int )",
4948 };
4949
4950 for (const char *s : syms)
4951 {
4952 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4953 EXPECT ("(anonymous namespace)::A::B::C"));
4954 }
4955 }
4956
4957 {
4958 static const char expected[] = "ns2::tmpl<int>::foo2";
4959 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4960 EXPECT (expected));
4961 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4962 EXPECT (expected));
4963 }
4964
4965 SELF_CHECK (!any_mismatch);
4966
4967 #undef EXPECT
4968 #undef CHECK_MATCH
4969 }
4970
4971 static void
4972 run_test ()
4973 {
4974 test_mapped_index_find_name_component_bounds ();
4975 test_dw2_expand_symtabs_matching_symbol ();
4976 }
4977
4978 }} // namespace selftests::dw2_expand_symtabs_matching
4979
4980 #endif /* GDB_SELF_TEST */
4981
4982 /* If FILE_MATCHER is NULL or if PER_CU has
4983 dwarf2_per_cu_quick_data::MARK set (see
4984 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4985 EXPANSION_NOTIFY on it. */
4986
4987 static void
4988 dw2_expand_symtabs_matching_one
4989 (struct dwarf2_per_cu_data *per_cu,
4990 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4991 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4992 {
4993 if (file_matcher == NULL || per_cu->v.quick->mark)
4994 {
4995 bool symtab_was_null
4996 = (per_cu->v.quick->compunit_symtab == NULL);
4997
4998 dw2_instantiate_symtab (per_cu, false);
4999
5000 if (expansion_notify != NULL
5001 && symtab_was_null
5002 && per_cu->v.quick->compunit_symtab != NULL)
5003 expansion_notify (per_cu->v.quick->compunit_symtab);
5004 }
5005 }
5006
5007 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5008 matched, to expand corresponding CUs that were marked. IDX is the
5009 index of the symbol name that matched. */
5010
5011 static void
5012 dw2_expand_marked_cus
5013 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5014 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5015 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5016 search_domain kind)
5017 {
5018 offset_type *vec, vec_len, vec_idx;
5019 bool global_seen = false;
5020 mapped_index &index = *dwarf2_per_objfile->index_table;
5021
5022 vec = (offset_type *) (index.constant_pool
5023 + MAYBE_SWAP (index.symbol_table[idx].vec));
5024 vec_len = MAYBE_SWAP (vec[0]);
5025 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5026 {
5027 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5028 /* This value is only valid for index versions >= 7. */
5029 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5030 gdb_index_symbol_kind symbol_kind =
5031 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5032 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5033 /* Only check the symbol attributes if they're present.
5034 Indices prior to version 7 don't record them,
5035 and indices >= 7 may elide them for certain symbols
5036 (gold does this). */
5037 int attrs_valid =
5038 (index.version >= 7
5039 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5040
5041 /* Work around gold/15646. */
5042 if (attrs_valid)
5043 {
5044 if (!is_static && global_seen)
5045 continue;
5046 if (!is_static)
5047 global_seen = true;
5048 }
5049
5050 /* Only check the symbol's kind if it has one. */
5051 if (attrs_valid)
5052 {
5053 switch (kind)
5054 {
5055 case VARIABLES_DOMAIN:
5056 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5057 continue;
5058 break;
5059 case FUNCTIONS_DOMAIN:
5060 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5061 continue;
5062 break;
5063 case TYPES_DOMAIN:
5064 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5065 continue;
5066 break;
5067 default:
5068 break;
5069 }
5070 }
5071
5072 /* Don't crash on bad data. */
5073 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
5074 + dwarf2_per_objfile->all_type_units.size ()))
5075 {
5076 complaint (_(".gdb_index entry has bad CU index"
5077 " [in module %s]"),
5078 objfile_name (dwarf2_per_objfile->objfile));
5079 continue;
5080 }
5081
5082 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
5083 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5084 expansion_notify);
5085 }
5086 }
5087
5088 /* If FILE_MATCHER is non-NULL, set all the
5089 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5090 that match FILE_MATCHER. */
5091
5092 static void
5093 dw_expand_symtabs_matching_file_matcher
5094 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5095 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5096 {
5097 if (file_matcher == NULL)
5098 return;
5099
5100 objfile *const objfile = dwarf2_per_objfile->objfile;
5101
5102 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5103 htab_eq_pointer,
5104 NULL, xcalloc, xfree));
5105 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5106 htab_eq_pointer,
5107 NULL, xcalloc, xfree));
5108
5109 /* The rule is CUs specify all the files, including those used by
5110 any TU, so there's no need to scan TUs here. */
5111
5112 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5113 {
5114 QUIT;
5115
5116 per_cu->v.quick->mark = 0;
5117
5118 /* We only need to look at symtabs not already expanded. */
5119 if (per_cu->v.quick->compunit_symtab)
5120 continue;
5121
5122 quick_file_names *file_data = dw2_get_file_names (per_cu);
5123 if (file_data == NULL)
5124 continue;
5125
5126 if (htab_find (visited_not_found.get (), file_data) != NULL)
5127 continue;
5128 else if (htab_find (visited_found.get (), file_data) != NULL)
5129 {
5130 per_cu->v.quick->mark = 1;
5131 continue;
5132 }
5133
5134 for (int j = 0; j < file_data->num_file_names; ++j)
5135 {
5136 const char *this_real_name;
5137
5138 if (file_matcher (file_data->file_names[j], false))
5139 {
5140 per_cu->v.quick->mark = 1;
5141 break;
5142 }
5143
5144 /* Before we invoke realpath, which can get expensive when many
5145 files are involved, do a quick comparison of the basenames. */
5146 if (!basenames_may_differ
5147 && !file_matcher (lbasename (file_data->file_names[j]),
5148 true))
5149 continue;
5150
5151 this_real_name = dw2_get_real_path (objfile, file_data, j);
5152 if (file_matcher (this_real_name, false))
5153 {
5154 per_cu->v.quick->mark = 1;
5155 break;
5156 }
5157 }
5158
5159 void **slot = htab_find_slot (per_cu->v.quick->mark
5160 ? visited_found.get ()
5161 : visited_not_found.get (),
5162 file_data, INSERT);
5163 *slot = file_data;
5164 }
5165 }
5166
5167 static void
5168 dw2_expand_symtabs_matching
5169 (struct objfile *objfile,
5170 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5171 const lookup_name_info &lookup_name,
5172 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5173 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5174 enum search_domain kind)
5175 {
5176 struct dwarf2_per_objfile *dwarf2_per_objfile
5177 = get_dwarf2_per_objfile (objfile);
5178
5179 /* index_table is NULL if OBJF_READNOW. */
5180 if (!dwarf2_per_objfile->index_table)
5181 return;
5182
5183 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5184
5185 mapped_index &index = *dwarf2_per_objfile->index_table;
5186
5187 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5188 symbol_matcher,
5189 kind, [&] (offset_type idx)
5190 {
5191 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5192 expansion_notify, kind);
5193 return true;
5194 });
5195 }
5196
5197 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5198 symtab. */
5199
5200 static struct compunit_symtab *
5201 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5202 CORE_ADDR pc)
5203 {
5204 int i;
5205
5206 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5207 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5208 return cust;
5209
5210 if (cust->includes == NULL)
5211 return NULL;
5212
5213 for (i = 0; cust->includes[i]; ++i)
5214 {
5215 struct compunit_symtab *s = cust->includes[i];
5216
5217 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5218 if (s != NULL)
5219 return s;
5220 }
5221
5222 return NULL;
5223 }
5224
5225 static struct compunit_symtab *
5226 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5227 struct bound_minimal_symbol msymbol,
5228 CORE_ADDR pc,
5229 struct obj_section *section,
5230 int warn_if_readin)
5231 {
5232 struct dwarf2_per_cu_data *data;
5233 struct compunit_symtab *result;
5234
5235 if (!objfile->partial_symtabs->psymtabs_addrmap)
5236 return NULL;
5237
5238 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
5239 SECT_OFF_TEXT (objfile));
5240 data = (struct dwarf2_per_cu_data *) addrmap_find
5241 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
5242 if (!data)
5243 return NULL;
5244
5245 if (warn_if_readin && data->v.quick->compunit_symtab)
5246 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5247 paddress (get_objfile_arch (objfile), pc));
5248
5249 result
5250 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5251 false),
5252 pc);
5253 gdb_assert (result != NULL);
5254 return result;
5255 }
5256
5257 static void
5258 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5259 void *data, int need_fullname)
5260 {
5261 struct dwarf2_per_objfile *dwarf2_per_objfile
5262 = get_dwarf2_per_objfile (objfile);
5263
5264 if (!dwarf2_per_objfile->filenames_cache)
5265 {
5266 dwarf2_per_objfile->filenames_cache.emplace ();
5267
5268 htab_up visited (htab_create_alloc (10,
5269 htab_hash_pointer, htab_eq_pointer,
5270 NULL, xcalloc, xfree));
5271
5272 /* The rule is CUs specify all the files, including those used
5273 by any TU, so there's no need to scan TUs here. We can
5274 ignore file names coming from already-expanded CUs. */
5275
5276 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5277 {
5278 if (per_cu->v.quick->compunit_symtab)
5279 {
5280 void **slot = htab_find_slot (visited.get (),
5281 per_cu->v.quick->file_names,
5282 INSERT);
5283
5284 *slot = per_cu->v.quick->file_names;
5285 }
5286 }
5287
5288 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5289 {
5290 /* We only need to look at symtabs not already expanded. */
5291 if (per_cu->v.quick->compunit_symtab)
5292 continue;
5293
5294 quick_file_names *file_data = dw2_get_file_names (per_cu);
5295 if (file_data == NULL)
5296 continue;
5297
5298 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5299 if (*slot)
5300 {
5301 /* Already visited. */
5302 continue;
5303 }
5304 *slot = file_data;
5305
5306 for (int j = 0; j < file_data->num_file_names; ++j)
5307 {
5308 const char *filename = file_data->file_names[j];
5309 dwarf2_per_objfile->filenames_cache->seen (filename);
5310 }
5311 }
5312 }
5313
5314 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5315 {
5316 gdb::unique_xmalloc_ptr<char> this_real_name;
5317
5318 if (need_fullname)
5319 this_real_name = gdb_realpath (filename);
5320 (*fun) (filename, this_real_name.get (), data);
5321 });
5322 }
5323
5324 static int
5325 dw2_has_symbols (struct objfile *objfile)
5326 {
5327 return 1;
5328 }
5329
5330 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5331 {
5332 dw2_has_symbols,
5333 dw2_find_last_source_symtab,
5334 dw2_forget_cached_source_info,
5335 dw2_map_symtabs_matching_filename,
5336 dw2_lookup_symbol,
5337 dw2_print_stats,
5338 dw2_dump,
5339 dw2_expand_symtabs_for_function,
5340 dw2_expand_all_symtabs,
5341 dw2_expand_symtabs_with_fullname,
5342 dw2_map_matching_symbols,
5343 dw2_expand_symtabs_matching,
5344 dw2_find_pc_sect_compunit_symtab,
5345 NULL,
5346 dw2_map_symbol_filenames
5347 };
5348
5349 /* DWARF-5 debug_names reader. */
5350
5351 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5352 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5353
5354 /* A helper function that reads the .debug_names section in SECTION
5355 and fills in MAP. FILENAME is the name of the file containing the
5356 section; it is used for error reporting.
5357
5358 Returns true if all went well, false otherwise. */
5359
5360 static bool
5361 read_debug_names_from_section (struct objfile *objfile,
5362 const char *filename,
5363 struct dwarf2_section_info *section,
5364 mapped_debug_names &map)
5365 {
5366 if (dwarf2_section_empty_p (section))
5367 return false;
5368
5369 /* Older elfutils strip versions could keep the section in the main
5370 executable while splitting it for the separate debug info file. */
5371 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5372 return false;
5373
5374 dwarf2_read_section (objfile, section);
5375
5376 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5377
5378 const gdb_byte *addr = section->buffer;
5379
5380 bfd *const abfd = get_section_bfd_owner (section);
5381
5382 unsigned int bytes_read;
5383 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5384 addr += bytes_read;
5385
5386 map.dwarf5_is_dwarf64 = bytes_read != 4;
5387 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5388 if (bytes_read + length != section->size)
5389 {
5390 /* There may be multiple per-CU indices. */
5391 warning (_("Section .debug_names in %s length %s does not match "
5392 "section length %s, ignoring .debug_names."),
5393 filename, plongest (bytes_read + length),
5394 pulongest (section->size));
5395 return false;
5396 }
5397
5398 /* The version number. */
5399 uint16_t version = read_2_bytes (abfd, addr);
5400 addr += 2;
5401 if (version != 5)
5402 {
5403 warning (_("Section .debug_names in %s has unsupported version %d, "
5404 "ignoring .debug_names."),
5405 filename, version);
5406 return false;
5407 }
5408
5409 /* Padding. */
5410 uint16_t padding = read_2_bytes (abfd, addr);
5411 addr += 2;
5412 if (padding != 0)
5413 {
5414 warning (_("Section .debug_names in %s has unsupported padding %d, "
5415 "ignoring .debug_names."),
5416 filename, padding);
5417 return false;
5418 }
5419
5420 /* comp_unit_count - The number of CUs in the CU list. */
5421 map.cu_count = read_4_bytes (abfd, addr);
5422 addr += 4;
5423
5424 /* local_type_unit_count - The number of TUs in the local TU
5425 list. */
5426 map.tu_count = read_4_bytes (abfd, addr);
5427 addr += 4;
5428
5429 /* foreign_type_unit_count - The number of TUs in the foreign TU
5430 list. */
5431 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5432 addr += 4;
5433 if (foreign_tu_count != 0)
5434 {
5435 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5436 "ignoring .debug_names."),
5437 filename, static_cast<unsigned long> (foreign_tu_count));
5438 return false;
5439 }
5440
5441 /* bucket_count - The number of hash buckets in the hash lookup
5442 table. */
5443 map.bucket_count = read_4_bytes (abfd, addr);
5444 addr += 4;
5445
5446 /* name_count - The number of unique names in the index. */
5447 map.name_count = read_4_bytes (abfd, addr);
5448 addr += 4;
5449
5450 /* abbrev_table_size - The size in bytes of the abbreviations
5451 table. */
5452 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5453 addr += 4;
5454
5455 /* augmentation_string_size - The size in bytes of the augmentation
5456 string. This value is rounded up to a multiple of 4. */
5457 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5458 addr += 4;
5459 map.augmentation_is_gdb = ((augmentation_string_size
5460 == sizeof (dwarf5_augmentation))
5461 && memcmp (addr, dwarf5_augmentation,
5462 sizeof (dwarf5_augmentation)) == 0);
5463 augmentation_string_size += (-augmentation_string_size) & 3;
5464 addr += augmentation_string_size;
5465
5466 /* List of CUs */
5467 map.cu_table_reordered = addr;
5468 addr += map.cu_count * map.offset_size;
5469
5470 /* List of Local TUs */
5471 map.tu_table_reordered = addr;
5472 addr += map.tu_count * map.offset_size;
5473
5474 /* Hash Lookup Table */
5475 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5476 addr += map.bucket_count * 4;
5477 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5478 addr += map.name_count * 4;
5479
5480 /* Name Table */
5481 map.name_table_string_offs_reordered = addr;
5482 addr += map.name_count * map.offset_size;
5483 map.name_table_entry_offs_reordered = addr;
5484 addr += map.name_count * map.offset_size;
5485
5486 const gdb_byte *abbrev_table_start = addr;
5487 for (;;)
5488 {
5489 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5490 addr += bytes_read;
5491 if (index_num == 0)
5492 break;
5493
5494 const auto insertpair
5495 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5496 if (!insertpair.second)
5497 {
5498 warning (_("Section .debug_names in %s has duplicate index %s, "
5499 "ignoring .debug_names."),
5500 filename, pulongest (index_num));
5501 return false;
5502 }
5503 mapped_debug_names::index_val &indexval = insertpair.first->second;
5504 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5505 addr += bytes_read;
5506
5507 for (;;)
5508 {
5509 mapped_debug_names::index_val::attr attr;
5510 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5511 addr += bytes_read;
5512 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5513 addr += bytes_read;
5514 if (attr.form == DW_FORM_implicit_const)
5515 {
5516 attr.implicit_const = read_signed_leb128 (abfd, addr,
5517 &bytes_read);
5518 addr += bytes_read;
5519 }
5520 if (attr.dw_idx == 0 && attr.form == 0)
5521 break;
5522 indexval.attr_vec.push_back (std::move (attr));
5523 }
5524 }
5525 if (addr != abbrev_table_start + abbrev_table_size)
5526 {
5527 warning (_("Section .debug_names in %s has abbreviation_table "
5528 "of size %s vs. written as %u, ignoring .debug_names."),
5529 filename, plongest (addr - abbrev_table_start),
5530 abbrev_table_size);
5531 return false;
5532 }
5533 map.entry_pool = addr;
5534
5535 return true;
5536 }
5537
5538 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5539 list. */
5540
5541 static void
5542 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5543 const mapped_debug_names &map,
5544 dwarf2_section_info &section,
5545 bool is_dwz)
5546 {
5547 sect_offset sect_off_prev;
5548 for (uint32_t i = 0; i <= map.cu_count; ++i)
5549 {
5550 sect_offset sect_off_next;
5551 if (i < map.cu_count)
5552 {
5553 sect_off_next
5554 = (sect_offset) (extract_unsigned_integer
5555 (map.cu_table_reordered + i * map.offset_size,
5556 map.offset_size,
5557 map.dwarf5_byte_order));
5558 }
5559 else
5560 sect_off_next = (sect_offset) section.size;
5561 if (i >= 1)
5562 {
5563 const ULONGEST length = sect_off_next - sect_off_prev;
5564 dwarf2_per_cu_data *per_cu
5565 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5566 sect_off_prev, length);
5567 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5568 }
5569 sect_off_prev = sect_off_next;
5570 }
5571 }
5572
5573 /* Read the CU list from the mapped index, and use it to create all
5574 the CU objects for this dwarf2_per_objfile. */
5575
5576 static void
5577 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5578 const mapped_debug_names &map,
5579 const mapped_debug_names &dwz_map)
5580 {
5581 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5582 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5583
5584 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5585 dwarf2_per_objfile->info,
5586 false /* is_dwz */);
5587
5588 if (dwz_map.cu_count == 0)
5589 return;
5590
5591 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5592 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5593 true /* is_dwz */);
5594 }
5595
5596 /* Read .debug_names. If everything went ok, initialize the "quick"
5597 elements of all the CUs and return true. Otherwise, return false. */
5598
5599 static bool
5600 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5601 {
5602 std::unique_ptr<mapped_debug_names> map
5603 (new mapped_debug_names (dwarf2_per_objfile));
5604 mapped_debug_names dwz_map (dwarf2_per_objfile);
5605 struct objfile *objfile = dwarf2_per_objfile->objfile;
5606
5607 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5608 &dwarf2_per_objfile->debug_names,
5609 *map))
5610 return false;
5611
5612 /* Don't use the index if it's empty. */
5613 if (map->name_count == 0)
5614 return false;
5615
5616 /* If there is a .dwz file, read it so we can get its CU list as
5617 well. */
5618 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5619 if (dwz != NULL)
5620 {
5621 if (!read_debug_names_from_section (objfile,
5622 bfd_get_filename (dwz->dwz_bfd.get ()),
5623 &dwz->debug_names, dwz_map))
5624 {
5625 warning (_("could not read '.debug_names' section from %s; skipping"),
5626 bfd_get_filename (dwz->dwz_bfd.get ()));
5627 return false;
5628 }
5629 }
5630
5631 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5632
5633 if (map->tu_count != 0)
5634 {
5635 /* We can only handle a single .debug_types when we have an
5636 index. */
5637 if (dwarf2_per_objfile->types.size () != 1)
5638 return false;
5639
5640 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5641
5642 create_signatured_type_table_from_debug_names
5643 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5644 }
5645
5646 create_addrmap_from_aranges (dwarf2_per_objfile,
5647 &dwarf2_per_objfile->debug_aranges);
5648
5649 dwarf2_per_objfile->debug_names_table = std::move (map);
5650 dwarf2_per_objfile->using_index = 1;
5651 dwarf2_per_objfile->quick_file_names_table =
5652 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5653
5654 return true;
5655 }
5656
5657 /* Type used to manage iterating over all CUs looking for a symbol for
5658 .debug_names. */
5659
5660 class dw2_debug_names_iterator
5661 {
5662 public:
5663 dw2_debug_names_iterator (const mapped_debug_names &map,
5664 gdb::optional<block_enum> block_index,
5665 domain_enum domain,
5666 const char *name)
5667 : m_map (map), m_block_index (block_index), m_domain (domain),
5668 m_addr (find_vec_in_debug_names (map, name))
5669 {}
5670
5671 dw2_debug_names_iterator (const mapped_debug_names &map,
5672 search_domain search, uint32_t namei)
5673 : m_map (map),
5674 m_search (search),
5675 m_addr (find_vec_in_debug_names (map, namei))
5676 {}
5677
5678 dw2_debug_names_iterator (const mapped_debug_names &map,
5679 block_enum block_index, domain_enum domain,
5680 uint32_t namei)
5681 : m_map (map), m_block_index (block_index), m_domain (domain),
5682 m_addr (find_vec_in_debug_names (map, namei))
5683 {}
5684
5685 /* Return the next matching CU or NULL if there are no more. */
5686 dwarf2_per_cu_data *next ();
5687
5688 private:
5689 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5690 const char *name);
5691 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5692 uint32_t namei);
5693
5694 /* The internalized form of .debug_names. */
5695 const mapped_debug_names &m_map;
5696
5697 /* If set, only look for symbols that match that block. Valid values are
5698 GLOBAL_BLOCK and STATIC_BLOCK. */
5699 const gdb::optional<block_enum> m_block_index;
5700
5701 /* The kind of symbol we're looking for. */
5702 const domain_enum m_domain = UNDEF_DOMAIN;
5703 const search_domain m_search = ALL_DOMAIN;
5704
5705 /* The list of CUs from the index entry of the symbol, or NULL if
5706 not found. */
5707 const gdb_byte *m_addr;
5708 };
5709
5710 const char *
5711 mapped_debug_names::namei_to_name (uint32_t namei) const
5712 {
5713 const ULONGEST namei_string_offs
5714 = extract_unsigned_integer ((name_table_string_offs_reordered
5715 + namei * offset_size),
5716 offset_size,
5717 dwarf5_byte_order);
5718 return read_indirect_string_at_offset
5719 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5720 }
5721
5722 /* Find a slot in .debug_names for the object named NAME. If NAME is
5723 found, return pointer to its pool data. If NAME cannot be found,
5724 return NULL. */
5725
5726 const gdb_byte *
5727 dw2_debug_names_iterator::find_vec_in_debug_names
5728 (const mapped_debug_names &map, const char *name)
5729 {
5730 int (*cmp) (const char *, const char *);
5731
5732 gdb::unique_xmalloc_ptr<char> without_params;
5733 if (current_language->la_language == language_cplus
5734 || current_language->la_language == language_fortran
5735 || current_language->la_language == language_d)
5736 {
5737 /* NAME is already canonical. Drop any qualifiers as
5738 .debug_names does not contain any. */
5739
5740 if (strchr (name, '(') != NULL)
5741 {
5742 without_params = cp_remove_params (name);
5743 if (without_params != NULL)
5744 name = without_params.get ();
5745 }
5746 }
5747
5748 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5749
5750 const uint32_t full_hash = dwarf5_djb_hash (name);
5751 uint32_t namei
5752 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5753 (map.bucket_table_reordered
5754 + (full_hash % map.bucket_count)), 4,
5755 map.dwarf5_byte_order);
5756 if (namei == 0)
5757 return NULL;
5758 --namei;
5759 if (namei >= map.name_count)
5760 {
5761 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5762 "[in module %s]"),
5763 namei, map.name_count,
5764 objfile_name (map.dwarf2_per_objfile->objfile));
5765 return NULL;
5766 }
5767
5768 for (;;)
5769 {
5770 const uint32_t namei_full_hash
5771 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5772 (map.hash_table_reordered + namei), 4,
5773 map.dwarf5_byte_order);
5774 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5775 return NULL;
5776
5777 if (full_hash == namei_full_hash)
5778 {
5779 const char *const namei_string = map.namei_to_name (namei);
5780
5781 #if 0 /* An expensive sanity check. */
5782 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5783 {
5784 complaint (_("Wrong .debug_names hash for string at index %u "
5785 "[in module %s]"),
5786 namei, objfile_name (dwarf2_per_objfile->objfile));
5787 return NULL;
5788 }
5789 #endif
5790
5791 if (cmp (namei_string, name) == 0)
5792 {
5793 const ULONGEST namei_entry_offs
5794 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5795 + namei * map.offset_size),
5796 map.offset_size, map.dwarf5_byte_order);
5797 return map.entry_pool + namei_entry_offs;
5798 }
5799 }
5800
5801 ++namei;
5802 if (namei >= map.name_count)
5803 return NULL;
5804 }
5805 }
5806
5807 const gdb_byte *
5808 dw2_debug_names_iterator::find_vec_in_debug_names
5809 (const mapped_debug_names &map, uint32_t namei)
5810 {
5811 if (namei >= map.name_count)
5812 {
5813 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5814 "[in module %s]"),
5815 namei, map.name_count,
5816 objfile_name (map.dwarf2_per_objfile->objfile));
5817 return NULL;
5818 }
5819
5820 const ULONGEST namei_entry_offs
5821 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5822 + namei * map.offset_size),
5823 map.offset_size, map.dwarf5_byte_order);
5824 return map.entry_pool + namei_entry_offs;
5825 }
5826
5827 /* See dw2_debug_names_iterator. */
5828
5829 dwarf2_per_cu_data *
5830 dw2_debug_names_iterator::next ()
5831 {
5832 if (m_addr == NULL)
5833 return NULL;
5834
5835 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5836 struct objfile *objfile = dwarf2_per_objfile->objfile;
5837 bfd *const abfd = objfile->obfd;
5838
5839 again:
5840
5841 unsigned int bytes_read;
5842 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5843 m_addr += bytes_read;
5844 if (abbrev == 0)
5845 return NULL;
5846
5847 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5848 if (indexval_it == m_map.abbrev_map.cend ())
5849 {
5850 complaint (_("Wrong .debug_names undefined abbrev code %s "
5851 "[in module %s]"),
5852 pulongest (abbrev), objfile_name (objfile));
5853 return NULL;
5854 }
5855 const mapped_debug_names::index_val &indexval = indexval_it->second;
5856 enum class symbol_linkage {
5857 unknown,
5858 static_,
5859 extern_,
5860 } symbol_linkage_ = symbol_linkage::unknown;
5861 dwarf2_per_cu_data *per_cu = NULL;
5862 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5863 {
5864 ULONGEST ull;
5865 switch (attr.form)
5866 {
5867 case DW_FORM_implicit_const:
5868 ull = attr.implicit_const;
5869 break;
5870 case DW_FORM_flag_present:
5871 ull = 1;
5872 break;
5873 case DW_FORM_udata:
5874 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5875 m_addr += bytes_read;
5876 break;
5877 default:
5878 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5879 dwarf_form_name (attr.form),
5880 objfile_name (objfile));
5881 return NULL;
5882 }
5883 switch (attr.dw_idx)
5884 {
5885 case DW_IDX_compile_unit:
5886 /* Don't crash on bad data. */
5887 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5888 {
5889 complaint (_(".debug_names entry has bad CU index %s"
5890 " [in module %s]"),
5891 pulongest (ull),
5892 objfile_name (dwarf2_per_objfile->objfile));
5893 continue;
5894 }
5895 per_cu = dwarf2_per_objfile->get_cutu (ull);
5896 break;
5897 case DW_IDX_type_unit:
5898 /* Don't crash on bad data. */
5899 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5900 {
5901 complaint (_(".debug_names entry has bad TU index %s"
5902 " [in module %s]"),
5903 pulongest (ull),
5904 objfile_name (dwarf2_per_objfile->objfile));
5905 continue;
5906 }
5907 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5908 break;
5909 case DW_IDX_GNU_internal:
5910 if (!m_map.augmentation_is_gdb)
5911 break;
5912 symbol_linkage_ = symbol_linkage::static_;
5913 break;
5914 case DW_IDX_GNU_external:
5915 if (!m_map.augmentation_is_gdb)
5916 break;
5917 symbol_linkage_ = symbol_linkage::extern_;
5918 break;
5919 }
5920 }
5921
5922 /* Skip if already read in. */
5923 if (per_cu->v.quick->compunit_symtab)
5924 goto again;
5925
5926 /* Check static vs global. */
5927 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5928 {
5929 const bool want_static = *m_block_index == STATIC_BLOCK;
5930 const bool symbol_is_static =
5931 symbol_linkage_ == symbol_linkage::static_;
5932 if (want_static != symbol_is_static)
5933 goto again;
5934 }
5935
5936 /* Match dw2_symtab_iter_next, symbol_kind
5937 and debug_names::psymbol_tag. */
5938 switch (m_domain)
5939 {
5940 case VAR_DOMAIN:
5941 switch (indexval.dwarf_tag)
5942 {
5943 case DW_TAG_variable:
5944 case DW_TAG_subprogram:
5945 /* Some types are also in VAR_DOMAIN. */
5946 case DW_TAG_typedef:
5947 case DW_TAG_structure_type:
5948 break;
5949 default:
5950 goto again;
5951 }
5952 break;
5953 case STRUCT_DOMAIN:
5954 switch (indexval.dwarf_tag)
5955 {
5956 case DW_TAG_typedef:
5957 case DW_TAG_structure_type:
5958 break;
5959 default:
5960 goto again;
5961 }
5962 break;
5963 case LABEL_DOMAIN:
5964 switch (indexval.dwarf_tag)
5965 {
5966 case 0:
5967 case DW_TAG_variable:
5968 break;
5969 default:
5970 goto again;
5971 }
5972 break;
5973 default:
5974 break;
5975 }
5976
5977 /* Match dw2_expand_symtabs_matching, symbol_kind and
5978 debug_names::psymbol_tag. */
5979 switch (m_search)
5980 {
5981 case VARIABLES_DOMAIN:
5982 switch (indexval.dwarf_tag)
5983 {
5984 case DW_TAG_variable:
5985 break;
5986 default:
5987 goto again;
5988 }
5989 break;
5990 case FUNCTIONS_DOMAIN:
5991 switch (indexval.dwarf_tag)
5992 {
5993 case DW_TAG_subprogram:
5994 break;
5995 default:
5996 goto again;
5997 }
5998 break;
5999 case TYPES_DOMAIN:
6000 switch (indexval.dwarf_tag)
6001 {
6002 case DW_TAG_typedef:
6003 case DW_TAG_structure_type:
6004 break;
6005 default:
6006 goto again;
6007 }
6008 break;
6009 default:
6010 break;
6011 }
6012
6013 return per_cu;
6014 }
6015
6016 static struct compunit_symtab *
6017 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
6018 const char *name, domain_enum domain)
6019 {
6020 struct dwarf2_per_objfile *dwarf2_per_objfile
6021 = get_dwarf2_per_objfile (objfile);
6022
6023 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6024 if (!mapp)
6025 {
6026 /* index is NULL if OBJF_READNOW. */
6027 return NULL;
6028 }
6029 const auto &map = *mapp;
6030
6031 dw2_debug_names_iterator iter (map, block_index, domain, name);
6032
6033 struct compunit_symtab *stab_best = NULL;
6034 struct dwarf2_per_cu_data *per_cu;
6035 while ((per_cu = iter.next ()) != NULL)
6036 {
6037 struct symbol *sym, *with_opaque = NULL;
6038 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
6039 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6040 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6041
6042 sym = block_find_symbol (block, name, domain,
6043 block_find_non_opaque_type_preferred,
6044 &with_opaque);
6045
6046 /* Some caution must be observed with overloaded functions and
6047 methods, since the index will not contain any overload
6048 information (but NAME might contain it). */
6049
6050 if (sym != NULL
6051 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6052 return stab;
6053 if (with_opaque != NULL
6054 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6055 stab_best = stab;
6056
6057 /* Keep looking through other CUs. */
6058 }
6059
6060 return stab_best;
6061 }
6062
6063 /* This dumps minimal information about .debug_names. It is called
6064 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6065 uses this to verify that .debug_names has been loaded. */
6066
6067 static void
6068 dw2_debug_names_dump (struct objfile *objfile)
6069 {
6070 struct dwarf2_per_objfile *dwarf2_per_objfile
6071 = get_dwarf2_per_objfile (objfile);
6072
6073 gdb_assert (dwarf2_per_objfile->using_index);
6074 printf_filtered (".debug_names:");
6075 if (dwarf2_per_objfile->debug_names_table)
6076 printf_filtered (" exists\n");
6077 else
6078 printf_filtered (" faked for \"readnow\"\n");
6079 printf_filtered ("\n");
6080 }
6081
6082 static void
6083 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6084 const char *func_name)
6085 {
6086 struct dwarf2_per_objfile *dwarf2_per_objfile
6087 = get_dwarf2_per_objfile (objfile);
6088
6089 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6090 if (dwarf2_per_objfile->debug_names_table)
6091 {
6092 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6093
6094 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
6095
6096 struct dwarf2_per_cu_data *per_cu;
6097 while ((per_cu = iter.next ()) != NULL)
6098 dw2_instantiate_symtab (per_cu, false);
6099 }
6100 }
6101
6102 static void
6103 dw2_debug_names_map_matching_symbols
6104 (struct objfile *objfile,
6105 const lookup_name_info &name, domain_enum domain,
6106 int global,
6107 gdb::function_view<symbol_found_callback_ftype> callback,
6108 symbol_compare_ftype *ordered_compare)
6109 {
6110 struct dwarf2_per_objfile *dwarf2_per_objfile
6111 = get_dwarf2_per_objfile (objfile);
6112
6113 /* debug_names_table is NULL if OBJF_READNOW. */
6114 if (!dwarf2_per_objfile->debug_names_table)
6115 return;
6116
6117 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6118 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6119
6120 const char *match_name = name.ada ().lookup_name ().c_str ();
6121 auto matcher = [&] (const char *symname)
6122 {
6123 if (ordered_compare == nullptr)
6124 return true;
6125 return ordered_compare (symname, match_name) == 0;
6126 };
6127
6128 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6129 [&] (offset_type namei)
6130 {
6131 /* The name was matched, now expand corresponding CUs that were
6132 marked. */
6133 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6134
6135 struct dwarf2_per_cu_data *per_cu;
6136 while ((per_cu = iter.next ()) != NULL)
6137 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6138 return true;
6139 });
6140
6141 /* It's a shame we couldn't do this inside the
6142 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6143 that have already been expanded. Instead, this loop matches what
6144 the psymtab code does. */
6145 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6146 {
6147 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6148 if (cust != nullptr)
6149 {
6150 const struct block *block
6151 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6152 if (!iterate_over_symbols_terminated (block, name,
6153 domain, callback))
6154 break;
6155 }
6156 }
6157 }
6158
6159 static void
6160 dw2_debug_names_expand_symtabs_matching
6161 (struct objfile *objfile,
6162 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6163 const lookup_name_info &lookup_name,
6164 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6165 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6166 enum search_domain kind)
6167 {
6168 struct dwarf2_per_objfile *dwarf2_per_objfile
6169 = get_dwarf2_per_objfile (objfile);
6170
6171 /* debug_names_table is NULL if OBJF_READNOW. */
6172 if (!dwarf2_per_objfile->debug_names_table)
6173 return;
6174
6175 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6176
6177 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6178
6179 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6180 symbol_matcher,
6181 kind, [&] (offset_type namei)
6182 {
6183 /* The name was matched, now expand corresponding CUs that were
6184 marked. */
6185 dw2_debug_names_iterator iter (map, kind, namei);
6186
6187 struct dwarf2_per_cu_data *per_cu;
6188 while ((per_cu = iter.next ()) != NULL)
6189 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6190 expansion_notify);
6191 return true;
6192 });
6193 }
6194
6195 const struct quick_symbol_functions dwarf2_debug_names_functions =
6196 {
6197 dw2_has_symbols,
6198 dw2_find_last_source_symtab,
6199 dw2_forget_cached_source_info,
6200 dw2_map_symtabs_matching_filename,
6201 dw2_debug_names_lookup_symbol,
6202 dw2_print_stats,
6203 dw2_debug_names_dump,
6204 dw2_debug_names_expand_symtabs_for_function,
6205 dw2_expand_all_symtabs,
6206 dw2_expand_symtabs_with_fullname,
6207 dw2_debug_names_map_matching_symbols,
6208 dw2_debug_names_expand_symtabs_matching,
6209 dw2_find_pc_sect_compunit_symtab,
6210 NULL,
6211 dw2_map_symbol_filenames
6212 };
6213
6214 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6215 to either a dwarf2_per_objfile or dwz_file object. */
6216
6217 template <typename T>
6218 static gdb::array_view<const gdb_byte>
6219 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6220 {
6221 dwarf2_section_info *section = &section_owner->gdb_index;
6222
6223 if (dwarf2_section_empty_p (section))
6224 return {};
6225
6226 /* Older elfutils strip versions could keep the section in the main
6227 executable while splitting it for the separate debug info file. */
6228 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6229 return {};
6230
6231 dwarf2_read_section (obj, section);
6232
6233 /* dwarf2_section_info::size is a bfd_size_type, while
6234 gdb::array_view works with size_t. On 32-bit hosts, with
6235 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6236 is 32-bit. So we need an explicit narrowing conversion here.
6237 This is fine, because it's impossible to allocate or mmap an
6238 array/buffer larger than what size_t can represent. */
6239 return gdb::make_array_view (section->buffer, section->size);
6240 }
6241
6242 /* Lookup the index cache for the contents of the index associated to
6243 DWARF2_OBJ. */
6244
6245 static gdb::array_view<const gdb_byte>
6246 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6247 {
6248 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6249 if (build_id == nullptr)
6250 return {};
6251
6252 return global_index_cache.lookup_gdb_index (build_id,
6253 &dwarf2_obj->index_cache_res);
6254 }
6255
6256 /* Same as the above, but for DWZ. */
6257
6258 static gdb::array_view<const gdb_byte>
6259 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6260 {
6261 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6262 if (build_id == nullptr)
6263 return {};
6264
6265 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6266 }
6267
6268 /* See symfile.h. */
6269
6270 bool
6271 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6272 {
6273 struct dwarf2_per_objfile *dwarf2_per_objfile
6274 = get_dwarf2_per_objfile (objfile);
6275
6276 /* If we're about to read full symbols, don't bother with the
6277 indices. In this case we also don't care if some other debug
6278 format is making psymtabs, because they are all about to be
6279 expanded anyway. */
6280 if ((objfile->flags & OBJF_READNOW))
6281 {
6282 dwarf2_per_objfile->using_index = 1;
6283 create_all_comp_units (dwarf2_per_objfile);
6284 create_all_type_units (dwarf2_per_objfile);
6285 dwarf2_per_objfile->quick_file_names_table
6286 = create_quick_file_names_table
6287 (dwarf2_per_objfile->all_comp_units.size ());
6288
6289 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6290 + dwarf2_per_objfile->all_type_units.size ()); ++i)
6291 {
6292 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6293
6294 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6295 struct dwarf2_per_cu_quick_data);
6296 }
6297
6298 /* Return 1 so that gdb sees the "quick" functions. However,
6299 these functions will be no-ops because we will have expanded
6300 all symtabs. */
6301 *index_kind = dw_index_kind::GDB_INDEX;
6302 return true;
6303 }
6304
6305 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6306 {
6307 *index_kind = dw_index_kind::DEBUG_NAMES;
6308 return true;
6309 }
6310
6311 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6312 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6313 get_gdb_index_contents_from_section<dwz_file>))
6314 {
6315 *index_kind = dw_index_kind::GDB_INDEX;
6316 return true;
6317 }
6318
6319 /* ... otherwise, try to find the index in the index cache. */
6320 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6321 get_gdb_index_contents_from_cache,
6322 get_gdb_index_contents_from_cache_dwz))
6323 {
6324 global_index_cache.hit ();
6325 *index_kind = dw_index_kind::GDB_INDEX;
6326 return true;
6327 }
6328
6329 global_index_cache.miss ();
6330 return false;
6331 }
6332
6333 \f
6334
6335 /* Build a partial symbol table. */
6336
6337 void
6338 dwarf2_build_psymtabs (struct objfile *objfile)
6339 {
6340 struct dwarf2_per_objfile *dwarf2_per_objfile
6341 = get_dwarf2_per_objfile (objfile);
6342
6343 init_psymbol_list (objfile, 1024);
6344
6345 try
6346 {
6347 /* This isn't really ideal: all the data we allocate on the
6348 objfile's obstack is still uselessly kept around. However,
6349 freeing it seems unsafe. */
6350 psymtab_discarder psymtabs (objfile);
6351 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6352 psymtabs.keep ();
6353
6354 /* (maybe) store an index in the cache. */
6355 global_index_cache.store (dwarf2_per_objfile);
6356 }
6357 catch (const gdb_exception_error &except)
6358 {
6359 exception_print (gdb_stderr, except);
6360 }
6361 }
6362
6363 /* Return the total length of the CU described by HEADER. */
6364
6365 static unsigned int
6366 get_cu_length (const struct comp_unit_head *header)
6367 {
6368 return header->initial_length_size + header->length;
6369 }
6370
6371 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6372
6373 static inline bool
6374 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6375 {
6376 sect_offset bottom = cu_header->sect_off;
6377 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6378
6379 return sect_off >= bottom && sect_off < top;
6380 }
6381
6382 /* Find the base address of the compilation unit for range lists and
6383 location lists. It will normally be specified by DW_AT_low_pc.
6384 In DWARF-3 draft 4, the base address could be overridden by
6385 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6386 compilation units with discontinuous ranges. */
6387
6388 static void
6389 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6390 {
6391 struct attribute *attr;
6392
6393 cu->base_known = 0;
6394 cu->base_address = 0;
6395
6396 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6397 if (attr)
6398 {
6399 cu->base_address = attr_value_as_address (attr);
6400 cu->base_known = 1;
6401 }
6402 else
6403 {
6404 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6405 if (attr)
6406 {
6407 cu->base_address = attr_value_as_address (attr);
6408 cu->base_known = 1;
6409 }
6410 }
6411 }
6412
6413 /* Read in the comp unit header information from the debug_info at info_ptr.
6414 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6415 NOTE: This leaves members offset, first_die_offset to be filled in
6416 by the caller. */
6417
6418 static const gdb_byte *
6419 read_comp_unit_head (struct comp_unit_head *cu_header,
6420 const gdb_byte *info_ptr,
6421 struct dwarf2_section_info *section,
6422 rcuh_kind section_kind)
6423 {
6424 int signed_addr;
6425 unsigned int bytes_read;
6426 const char *filename = get_section_file_name (section);
6427 bfd *abfd = get_section_bfd_owner (section);
6428
6429 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6430 cu_header->initial_length_size = bytes_read;
6431 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6432 info_ptr += bytes_read;
6433 cu_header->version = read_2_bytes (abfd, info_ptr);
6434 if (cu_header->version < 2 || cu_header->version > 5)
6435 error (_("Dwarf Error: wrong version in compilation unit header "
6436 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6437 cu_header->version, filename);
6438 info_ptr += 2;
6439 if (cu_header->version < 5)
6440 switch (section_kind)
6441 {
6442 case rcuh_kind::COMPILE:
6443 cu_header->unit_type = DW_UT_compile;
6444 break;
6445 case rcuh_kind::TYPE:
6446 cu_header->unit_type = DW_UT_type;
6447 break;
6448 default:
6449 internal_error (__FILE__, __LINE__,
6450 _("read_comp_unit_head: invalid section_kind"));
6451 }
6452 else
6453 {
6454 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6455 (read_1_byte (abfd, info_ptr));
6456 info_ptr += 1;
6457 switch (cu_header->unit_type)
6458 {
6459 case DW_UT_compile:
6460 case DW_UT_partial:
6461 case DW_UT_skeleton:
6462 case DW_UT_split_compile:
6463 if (section_kind != rcuh_kind::COMPILE)
6464 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6465 "(is %s, should be %s) [in module %s]"),
6466 dwarf_unit_type_name (cu_header->unit_type),
6467 dwarf_unit_type_name (DW_UT_type), filename);
6468 break;
6469 case DW_UT_type:
6470 case DW_UT_split_type:
6471 section_kind = rcuh_kind::TYPE;
6472 break;
6473 default:
6474 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6475 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6476 "[in module %s]"), cu_header->unit_type,
6477 dwarf_unit_type_name (DW_UT_compile),
6478 dwarf_unit_type_name (DW_UT_skeleton),
6479 dwarf_unit_type_name (DW_UT_split_compile),
6480 dwarf_unit_type_name (DW_UT_type),
6481 dwarf_unit_type_name (DW_UT_split_type), filename);
6482 }
6483
6484 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6485 info_ptr += 1;
6486 }
6487 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6488 cu_header,
6489 &bytes_read);
6490 info_ptr += bytes_read;
6491 if (cu_header->version < 5)
6492 {
6493 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6494 info_ptr += 1;
6495 }
6496 signed_addr = bfd_get_sign_extend_vma (abfd);
6497 if (signed_addr < 0)
6498 internal_error (__FILE__, __LINE__,
6499 _("read_comp_unit_head: dwarf from non elf file"));
6500 cu_header->signed_addr_p = signed_addr;
6501
6502 bool header_has_signature = section_kind == rcuh_kind::TYPE
6503 || cu_header->unit_type == DW_UT_skeleton
6504 || cu_header->unit_type == DW_UT_split_compile;
6505
6506 if (header_has_signature)
6507 {
6508 cu_header->signature = read_8_bytes (abfd, info_ptr);
6509 info_ptr += 8;
6510 }
6511
6512 if (section_kind == rcuh_kind::TYPE)
6513 {
6514 LONGEST type_offset;
6515 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6516 info_ptr += bytes_read;
6517 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6518 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6519 error (_("Dwarf Error: Too big type_offset in compilation unit "
6520 "header (is %s) [in module %s]"), plongest (type_offset),
6521 filename);
6522 }
6523
6524 return info_ptr;
6525 }
6526
6527 /* Helper function that returns the proper abbrev section for
6528 THIS_CU. */
6529
6530 static struct dwarf2_section_info *
6531 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6532 {
6533 struct dwarf2_section_info *abbrev;
6534 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6535
6536 if (this_cu->is_dwz)
6537 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6538 else
6539 abbrev = &dwarf2_per_objfile->abbrev;
6540
6541 return abbrev;
6542 }
6543
6544 /* Subroutine of read_and_check_comp_unit_head and
6545 read_and_check_type_unit_head to simplify them.
6546 Perform various error checking on the header. */
6547
6548 static void
6549 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6550 struct comp_unit_head *header,
6551 struct dwarf2_section_info *section,
6552 struct dwarf2_section_info *abbrev_section)
6553 {
6554 const char *filename = get_section_file_name (section);
6555
6556 if (to_underlying (header->abbrev_sect_off)
6557 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6558 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6559 "(offset %s + 6) [in module %s]"),
6560 sect_offset_str (header->abbrev_sect_off),
6561 sect_offset_str (header->sect_off),
6562 filename);
6563
6564 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6565 avoid potential 32-bit overflow. */
6566 if (((ULONGEST) header->sect_off + get_cu_length (header))
6567 > section->size)
6568 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6569 "(offset %s + 0) [in module %s]"),
6570 header->length, sect_offset_str (header->sect_off),
6571 filename);
6572 }
6573
6574 /* Read in a CU/TU header and perform some basic error checking.
6575 The contents of the header are stored in HEADER.
6576 The result is a pointer to the start of the first DIE. */
6577
6578 static const gdb_byte *
6579 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6580 struct comp_unit_head *header,
6581 struct dwarf2_section_info *section,
6582 struct dwarf2_section_info *abbrev_section,
6583 const gdb_byte *info_ptr,
6584 rcuh_kind section_kind)
6585 {
6586 const gdb_byte *beg_of_comp_unit = info_ptr;
6587
6588 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6589
6590 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6591
6592 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6593
6594 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6595 abbrev_section);
6596
6597 return info_ptr;
6598 }
6599
6600 /* Fetch the abbreviation table offset from a comp or type unit header. */
6601
6602 static sect_offset
6603 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6604 struct dwarf2_section_info *section,
6605 sect_offset sect_off)
6606 {
6607 bfd *abfd = get_section_bfd_owner (section);
6608 const gdb_byte *info_ptr;
6609 unsigned int initial_length_size, offset_size;
6610 uint16_t version;
6611
6612 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6613 info_ptr = section->buffer + to_underlying (sect_off);
6614 read_initial_length (abfd, info_ptr, &initial_length_size);
6615 offset_size = initial_length_size == 4 ? 4 : 8;
6616 info_ptr += initial_length_size;
6617
6618 version = read_2_bytes (abfd, info_ptr);
6619 info_ptr += 2;
6620 if (version >= 5)
6621 {
6622 /* Skip unit type and address size. */
6623 info_ptr += 2;
6624 }
6625
6626 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6627 }
6628
6629 /* Allocate a new partial symtab for file named NAME and mark this new
6630 partial symtab as being an include of PST. */
6631
6632 static void
6633 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6634 struct objfile *objfile)
6635 {
6636 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6637
6638 if (!IS_ABSOLUTE_PATH (subpst->filename))
6639 {
6640 /* It shares objfile->objfile_obstack. */
6641 subpst->dirname = pst->dirname;
6642 }
6643
6644 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6645 subpst->dependencies[0] = pst;
6646 subpst->number_of_dependencies = 1;
6647
6648 subpst->read_symtab = pst->read_symtab;
6649
6650 /* No private part is necessary for include psymtabs. This property
6651 can be used to differentiate between such include psymtabs and
6652 the regular ones. */
6653 subpst->read_symtab_private = NULL;
6654 }
6655
6656 /* Read the Line Number Program data and extract the list of files
6657 included by the source file represented by PST. Build an include
6658 partial symtab for each of these included files. */
6659
6660 static void
6661 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6662 struct die_info *die,
6663 struct partial_symtab *pst)
6664 {
6665 line_header_up lh;
6666 struct attribute *attr;
6667
6668 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6669 if (attr)
6670 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6671 if (lh == NULL)
6672 return; /* No linetable, so no includes. */
6673
6674 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6675 that we pass in the raw text_low here; that is ok because we're
6676 only decoding the line table to make include partial symtabs, and
6677 so the addresses aren't really used. */
6678 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6679 pst->raw_text_low (), 1);
6680 }
6681
6682 static hashval_t
6683 hash_signatured_type (const void *item)
6684 {
6685 const struct signatured_type *sig_type
6686 = (const struct signatured_type *) item;
6687
6688 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6689 return sig_type->signature;
6690 }
6691
6692 static int
6693 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6694 {
6695 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6696 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6697
6698 return lhs->signature == rhs->signature;
6699 }
6700
6701 /* Allocate a hash table for signatured types. */
6702
6703 static htab_t
6704 allocate_signatured_type_table (struct objfile *objfile)
6705 {
6706 return htab_create_alloc_ex (41,
6707 hash_signatured_type,
6708 eq_signatured_type,
6709 NULL,
6710 &objfile->objfile_obstack,
6711 hashtab_obstack_allocate,
6712 dummy_obstack_deallocate);
6713 }
6714
6715 /* A helper function to add a signatured type CU to a table. */
6716
6717 static int
6718 add_signatured_type_cu_to_table (void **slot, void *datum)
6719 {
6720 struct signatured_type *sigt = (struct signatured_type *) *slot;
6721 std::vector<signatured_type *> *all_type_units
6722 = (std::vector<signatured_type *> *) datum;
6723
6724 all_type_units->push_back (sigt);
6725
6726 return 1;
6727 }
6728
6729 /* A helper for create_debug_types_hash_table. Read types from SECTION
6730 and fill them into TYPES_HTAB. It will process only type units,
6731 therefore DW_UT_type. */
6732
6733 static void
6734 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6735 struct dwo_file *dwo_file,
6736 dwarf2_section_info *section, htab_t &types_htab,
6737 rcuh_kind section_kind)
6738 {
6739 struct objfile *objfile = dwarf2_per_objfile->objfile;
6740 struct dwarf2_section_info *abbrev_section;
6741 bfd *abfd;
6742 const gdb_byte *info_ptr, *end_ptr;
6743
6744 abbrev_section = (dwo_file != NULL
6745 ? &dwo_file->sections.abbrev
6746 : &dwarf2_per_objfile->abbrev);
6747
6748 if (dwarf_read_debug)
6749 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6750 get_section_name (section),
6751 get_section_file_name (abbrev_section));
6752
6753 dwarf2_read_section (objfile, section);
6754 info_ptr = section->buffer;
6755
6756 if (info_ptr == NULL)
6757 return;
6758
6759 /* We can't set abfd until now because the section may be empty or
6760 not present, in which case the bfd is unknown. */
6761 abfd = get_section_bfd_owner (section);
6762
6763 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6764 because we don't need to read any dies: the signature is in the
6765 header. */
6766
6767 end_ptr = info_ptr + section->size;
6768 while (info_ptr < end_ptr)
6769 {
6770 struct signatured_type *sig_type;
6771 struct dwo_unit *dwo_tu;
6772 void **slot;
6773 const gdb_byte *ptr = info_ptr;
6774 struct comp_unit_head header;
6775 unsigned int length;
6776
6777 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6778
6779 /* Initialize it due to a false compiler warning. */
6780 header.signature = -1;
6781 header.type_cu_offset_in_tu = (cu_offset) -1;
6782
6783 /* We need to read the type's signature in order to build the hash
6784 table, but we don't need anything else just yet. */
6785
6786 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6787 abbrev_section, ptr, section_kind);
6788
6789 length = get_cu_length (&header);
6790
6791 /* Skip dummy type units. */
6792 if (ptr >= info_ptr + length
6793 || peek_abbrev_code (abfd, ptr) == 0
6794 || header.unit_type != DW_UT_type)
6795 {
6796 info_ptr += length;
6797 continue;
6798 }
6799
6800 if (types_htab == NULL)
6801 {
6802 if (dwo_file)
6803 types_htab = allocate_dwo_unit_table (objfile);
6804 else
6805 types_htab = allocate_signatured_type_table (objfile);
6806 }
6807
6808 if (dwo_file)
6809 {
6810 sig_type = NULL;
6811 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6812 struct dwo_unit);
6813 dwo_tu->dwo_file = dwo_file;
6814 dwo_tu->signature = header.signature;
6815 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6816 dwo_tu->section = section;
6817 dwo_tu->sect_off = sect_off;
6818 dwo_tu->length = length;
6819 }
6820 else
6821 {
6822 /* N.B.: type_offset is not usable if this type uses a DWO file.
6823 The real type_offset is in the DWO file. */
6824 dwo_tu = NULL;
6825 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6826 struct signatured_type);
6827 sig_type->signature = header.signature;
6828 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6829 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6830 sig_type->per_cu.is_debug_types = 1;
6831 sig_type->per_cu.section = section;
6832 sig_type->per_cu.sect_off = sect_off;
6833 sig_type->per_cu.length = length;
6834 }
6835
6836 slot = htab_find_slot (types_htab,
6837 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6838 INSERT);
6839 gdb_assert (slot != NULL);
6840 if (*slot != NULL)
6841 {
6842 sect_offset dup_sect_off;
6843
6844 if (dwo_file)
6845 {
6846 const struct dwo_unit *dup_tu
6847 = (const struct dwo_unit *) *slot;
6848
6849 dup_sect_off = dup_tu->sect_off;
6850 }
6851 else
6852 {
6853 const struct signatured_type *dup_tu
6854 = (const struct signatured_type *) *slot;
6855
6856 dup_sect_off = dup_tu->per_cu.sect_off;
6857 }
6858
6859 complaint (_("debug type entry at offset %s is duplicate to"
6860 " the entry at offset %s, signature %s"),
6861 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6862 hex_string (header.signature));
6863 }
6864 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6865
6866 if (dwarf_read_debug > 1)
6867 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6868 sect_offset_str (sect_off),
6869 hex_string (header.signature));
6870
6871 info_ptr += length;
6872 }
6873 }
6874
6875 /* Create the hash table of all entries in the .debug_types
6876 (or .debug_types.dwo) section(s).
6877 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6878 otherwise it is NULL.
6879
6880 The result is a pointer to the hash table or NULL if there are no types.
6881
6882 Note: This function processes DWO files only, not DWP files. */
6883
6884 static void
6885 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6886 struct dwo_file *dwo_file,
6887 gdb::array_view<dwarf2_section_info> type_sections,
6888 htab_t &types_htab)
6889 {
6890 for (dwarf2_section_info &section : type_sections)
6891 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6892 types_htab, rcuh_kind::TYPE);
6893 }
6894
6895 /* Create the hash table of all entries in the .debug_types section,
6896 and initialize all_type_units.
6897 The result is zero if there is an error (e.g. missing .debug_types section),
6898 otherwise non-zero. */
6899
6900 static int
6901 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6902 {
6903 htab_t types_htab = NULL;
6904
6905 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6906 &dwarf2_per_objfile->info, types_htab,
6907 rcuh_kind::COMPILE);
6908 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6909 dwarf2_per_objfile->types, types_htab);
6910 if (types_htab == NULL)
6911 {
6912 dwarf2_per_objfile->signatured_types = NULL;
6913 return 0;
6914 }
6915
6916 dwarf2_per_objfile->signatured_types = types_htab;
6917
6918 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6919 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6920
6921 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6922 &dwarf2_per_objfile->all_type_units);
6923
6924 return 1;
6925 }
6926
6927 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6928 If SLOT is non-NULL, it is the entry to use in the hash table.
6929 Otherwise we find one. */
6930
6931 static struct signatured_type *
6932 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6933 void **slot)
6934 {
6935 struct objfile *objfile = dwarf2_per_objfile->objfile;
6936
6937 if (dwarf2_per_objfile->all_type_units.size ()
6938 == dwarf2_per_objfile->all_type_units.capacity ())
6939 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6940
6941 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6942 struct signatured_type);
6943
6944 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6945 sig_type->signature = sig;
6946 sig_type->per_cu.is_debug_types = 1;
6947 if (dwarf2_per_objfile->using_index)
6948 {
6949 sig_type->per_cu.v.quick =
6950 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6951 struct dwarf2_per_cu_quick_data);
6952 }
6953
6954 if (slot == NULL)
6955 {
6956 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6957 sig_type, INSERT);
6958 }
6959 gdb_assert (*slot == NULL);
6960 *slot = sig_type;
6961 /* The rest of sig_type must be filled in by the caller. */
6962 return sig_type;
6963 }
6964
6965 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6966 Fill in SIG_ENTRY with DWO_ENTRY. */
6967
6968 static void
6969 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6970 struct signatured_type *sig_entry,
6971 struct dwo_unit *dwo_entry)
6972 {
6973 /* Make sure we're not clobbering something we don't expect to. */
6974 gdb_assert (! sig_entry->per_cu.queued);
6975 gdb_assert (sig_entry->per_cu.cu == NULL);
6976 if (dwarf2_per_objfile->using_index)
6977 {
6978 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6979 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6980 }
6981 else
6982 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6983 gdb_assert (sig_entry->signature == dwo_entry->signature);
6984 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6985 gdb_assert (sig_entry->type_unit_group == NULL);
6986 gdb_assert (sig_entry->dwo_unit == NULL);
6987
6988 sig_entry->per_cu.section = dwo_entry->section;
6989 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6990 sig_entry->per_cu.length = dwo_entry->length;
6991 sig_entry->per_cu.reading_dwo_directly = 1;
6992 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6993 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6994 sig_entry->dwo_unit = dwo_entry;
6995 }
6996
6997 /* Subroutine of lookup_signatured_type.
6998 If we haven't read the TU yet, create the signatured_type data structure
6999 for a TU to be read in directly from a DWO file, bypassing the stub.
7000 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7001 using .gdb_index, then when reading a CU we want to stay in the DWO file
7002 containing that CU. Otherwise we could end up reading several other DWO
7003 files (due to comdat folding) to process the transitive closure of all the
7004 mentioned TUs, and that can be slow. The current DWO file will have every
7005 type signature that it needs.
7006 We only do this for .gdb_index because in the psymtab case we already have
7007 to read all the DWOs to build the type unit groups. */
7008
7009 static struct signatured_type *
7010 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7011 {
7012 struct dwarf2_per_objfile *dwarf2_per_objfile
7013 = cu->per_cu->dwarf2_per_objfile;
7014 struct objfile *objfile = dwarf2_per_objfile->objfile;
7015 struct dwo_file *dwo_file;
7016 struct dwo_unit find_dwo_entry, *dwo_entry;
7017 struct signatured_type find_sig_entry, *sig_entry;
7018 void **slot;
7019
7020 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7021
7022 /* If TU skeletons have been removed then we may not have read in any
7023 TUs yet. */
7024 if (dwarf2_per_objfile->signatured_types == NULL)
7025 {
7026 dwarf2_per_objfile->signatured_types
7027 = allocate_signatured_type_table (objfile);
7028 }
7029
7030 /* We only ever need to read in one copy of a signatured type.
7031 Use the global signatured_types array to do our own comdat-folding
7032 of types. If this is the first time we're reading this TU, and
7033 the TU has an entry in .gdb_index, replace the recorded data from
7034 .gdb_index with this TU. */
7035
7036 find_sig_entry.signature = sig;
7037 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7038 &find_sig_entry, INSERT);
7039 sig_entry = (struct signatured_type *) *slot;
7040
7041 /* We can get here with the TU already read, *or* in the process of being
7042 read. Don't reassign the global entry to point to this DWO if that's
7043 the case. Also note that if the TU is already being read, it may not
7044 have come from a DWO, the program may be a mix of Fission-compiled
7045 code and non-Fission-compiled code. */
7046
7047 /* Have we already tried to read this TU?
7048 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7049 needn't exist in the global table yet). */
7050 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7051 return sig_entry;
7052
7053 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7054 dwo_unit of the TU itself. */
7055 dwo_file = cu->dwo_unit->dwo_file;
7056
7057 /* Ok, this is the first time we're reading this TU. */
7058 if (dwo_file->tus == NULL)
7059 return NULL;
7060 find_dwo_entry.signature = sig;
7061 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7062 if (dwo_entry == NULL)
7063 return NULL;
7064
7065 /* If the global table doesn't have an entry for this TU, add one. */
7066 if (sig_entry == NULL)
7067 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7068
7069 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7070 sig_entry->per_cu.tu_read = 1;
7071 return sig_entry;
7072 }
7073
7074 /* Subroutine of lookup_signatured_type.
7075 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7076 then try the DWP file. If the TU stub (skeleton) has been removed then
7077 it won't be in .gdb_index. */
7078
7079 static struct signatured_type *
7080 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7081 {
7082 struct dwarf2_per_objfile *dwarf2_per_objfile
7083 = cu->per_cu->dwarf2_per_objfile;
7084 struct objfile *objfile = dwarf2_per_objfile->objfile;
7085 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7086 struct dwo_unit *dwo_entry;
7087 struct signatured_type find_sig_entry, *sig_entry;
7088 void **slot;
7089
7090 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7091 gdb_assert (dwp_file != NULL);
7092
7093 /* If TU skeletons have been removed then we may not have read in any
7094 TUs yet. */
7095 if (dwarf2_per_objfile->signatured_types == NULL)
7096 {
7097 dwarf2_per_objfile->signatured_types
7098 = allocate_signatured_type_table (objfile);
7099 }
7100
7101 find_sig_entry.signature = sig;
7102 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7103 &find_sig_entry, INSERT);
7104 sig_entry = (struct signatured_type *) *slot;
7105
7106 /* Have we already tried to read this TU?
7107 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7108 needn't exist in the global table yet). */
7109 if (sig_entry != NULL)
7110 return sig_entry;
7111
7112 if (dwp_file->tus == NULL)
7113 return NULL;
7114 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7115 sig, 1 /* is_debug_types */);
7116 if (dwo_entry == NULL)
7117 return NULL;
7118
7119 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7120 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7121
7122 return sig_entry;
7123 }
7124
7125 /* Lookup a signature based type for DW_FORM_ref_sig8.
7126 Returns NULL if signature SIG is not present in the table.
7127 It is up to the caller to complain about this. */
7128
7129 static struct signatured_type *
7130 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7131 {
7132 struct dwarf2_per_objfile *dwarf2_per_objfile
7133 = cu->per_cu->dwarf2_per_objfile;
7134
7135 if (cu->dwo_unit
7136 && dwarf2_per_objfile->using_index)
7137 {
7138 /* We're in a DWO/DWP file, and we're using .gdb_index.
7139 These cases require special processing. */
7140 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7141 return lookup_dwo_signatured_type (cu, sig);
7142 else
7143 return lookup_dwp_signatured_type (cu, sig);
7144 }
7145 else
7146 {
7147 struct signatured_type find_entry, *entry;
7148
7149 if (dwarf2_per_objfile->signatured_types == NULL)
7150 return NULL;
7151 find_entry.signature = sig;
7152 entry = ((struct signatured_type *)
7153 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7154 return entry;
7155 }
7156 }
7157 \f
7158 /* Low level DIE reading support. */
7159
7160 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7161
7162 static void
7163 init_cu_die_reader (struct die_reader_specs *reader,
7164 struct dwarf2_cu *cu,
7165 struct dwarf2_section_info *section,
7166 struct dwo_file *dwo_file,
7167 struct abbrev_table *abbrev_table)
7168 {
7169 gdb_assert (section->readin && section->buffer != NULL);
7170 reader->abfd = get_section_bfd_owner (section);
7171 reader->cu = cu;
7172 reader->dwo_file = dwo_file;
7173 reader->die_section = section;
7174 reader->buffer = section->buffer;
7175 reader->buffer_end = section->buffer + section->size;
7176 reader->comp_dir = NULL;
7177 reader->abbrev_table = abbrev_table;
7178 }
7179
7180 /* Subroutine of init_cutu_and_read_dies to simplify it.
7181 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7182 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7183 already.
7184
7185 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7186 from it to the DIE in the DWO. If NULL we are skipping the stub.
7187 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7188 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7189 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7190 STUB_COMP_DIR may be non-NULL.
7191 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7192 are filled in with the info of the DIE from the DWO file.
7193 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7194 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7195 kept around for at least as long as *RESULT_READER.
7196
7197 The result is non-zero if a valid (non-dummy) DIE was found. */
7198
7199 static int
7200 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7201 struct dwo_unit *dwo_unit,
7202 struct die_info *stub_comp_unit_die,
7203 const char *stub_comp_dir,
7204 struct die_reader_specs *result_reader,
7205 const gdb_byte **result_info_ptr,
7206 struct die_info **result_comp_unit_die,
7207 int *result_has_children,
7208 abbrev_table_up *result_dwo_abbrev_table)
7209 {
7210 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7211 struct objfile *objfile = dwarf2_per_objfile->objfile;
7212 struct dwarf2_cu *cu = this_cu->cu;
7213 bfd *abfd;
7214 const gdb_byte *begin_info_ptr, *info_ptr;
7215 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7216 int i,num_extra_attrs;
7217 struct dwarf2_section_info *dwo_abbrev_section;
7218 struct attribute *attr;
7219 struct die_info *comp_unit_die;
7220
7221 /* At most one of these may be provided. */
7222 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7223
7224 /* These attributes aren't processed until later:
7225 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7226 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7227 referenced later. However, these attributes are found in the stub
7228 which we won't have later. In order to not impose this complication
7229 on the rest of the code, we read them here and copy them to the
7230 DWO CU/TU die. */
7231
7232 stmt_list = NULL;
7233 low_pc = NULL;
7234 high_pc = NULL;
7235 ranges = NULL;
7236 comp_dir = NULL;
7237
7238 if (stub_comp_unit_die != NULL)
7239 {
7240 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7241 DWO file. */
7242 if (! this_cu->is_debug_types)
7243 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7244 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7245 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7246 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7247 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7248
7249 /* There should be a DW_AT_addr_base attribute here (if needed).
7250 We need the value before we can process DW_FORM_GNU_addr_index
7251 or DW_FORM_addrx. */
7252 cu->addr_base = 0;
7253 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7254 if (attr)
7255 cu->addr_base = DW_UNSND (attr);
7256
7257 /* There should be a DW_AT_ranges_base attribute here (if needed).
7258 We need the value before we can process DW_AT_ranges. */
7259 cu->ranges_base = 0;
7260 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7261 if (attr)
7262 cu->ranges_base = DW_UNSND (attr);
7263 }
7264 else if (stub_comp_dir != NULL)
7265 {
7266 /* Reconstruct the comp_dir attribute to simplify the code below. */
7267 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7268 comp_dir->name = DW_AT_comp_dir;
7269 comp_dir->form = DW_FORM_string;
7270 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7271 DW_STRING (comp_dir) = stub_comp_dir;
7272 }
7273
7274 /* Set up for reading the DWO CU/TU. */
7275 cu->dwo_unit = dwo_unit;
7276 dwarf2_section_info *section = dwo_unit->section;
7277 dwarf2_read_section (objfile, section);
7278 abfd = get_section_bfd_owner (section);
7279 begin_info_ptr = info_ptr = (section->buffer
7280 + to_underlying (dwo_unit->sect_off));
7281 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7282
7283 if (this_cu->is_debug_types)
7284 {
7285 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7286
7287 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7288 &cu->header, section,
7289 dwo_abbrev_section,
7290 info_ptr, rcuh_kind::TYPE);
7291 /* This is not an assert because it can be caused by bad debug info. */
7292 if (sig_type->signature != cu->header.signature)
7293 {
7294 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7295 " TU at offset %s [in module %s]"),
7296 hex_string (sig_type->signature),
7297 hex_string (cu->header.signature),
7298 sect_offset_str (dwo_unit->sect_off),
7299 bfd_get_filename (abfd));
7300 }
7301 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7302 /* For DWOs coming from DWP files, we don't know the CU length
7303 nor the type's offset in the TU until now. */
7304 dwo_unit->length = get_cu_length (&cu->header);
7305 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7306
7307 /* Establish the type offset that can be used to lookup the type.
7308 For DWO files, we don't know it until now. */
7309 sig_type->type_offset_in_section
7310 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7311 }
7312 else
7313 {
7314 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7315 &cu->header, section,
7316 dwo_abbrev_section,
7317 info_ptr, rcuh_kind::COMPILE);
7318 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7319 /* For DWOs coming from DWP files, we don't know the CU length
7320 until now. */
7321 dwo_unit->length = get_cu_length (&cu->header);
7322 }
7323
7324 *result_dwo_abbrev_table
7325 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7326 cu->header.abbrev_sect_off);
7327 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7328 result_dwo_abbrev_table->get ());
7329
7330 /* Read in the die, but leave space to copy over the attributes
7331 from the stub. This has the benefit of simplifying the rest of
7332 the code - all the work to maintain the illusion of a single
7333 DW_TAG_{compile,type}_unit DIE is done here. */
7334 num_extra_attrs = ((stmt_list != NULL)
7335 + (low_pc != NULL)
7336 + (high_pc != NULL)
7337 + (ranges != NULL)
7338 + (comp_dir != NULL));
7339 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7340 result_has_children, num_extra_attrs);
7341
7342 /* Copy over the attributes from the stub to the DIE we just read in. */
7343 comp_unit_die = *result_comp_unit_die;
7344 i = comp_unit_die->num_attrs;
7345 if (stmt_list != NULL)
7346 comp_unit_die->attrs[i++] = *stmt_list;
7347 if (low_pc != NULL)
7348 comp_unit_die->attrs[i++] = *low_pc;
7349 if (high_pc != NULL)
7350 comp_unit_die->attrs[i++] = *high_pc;
7351 if (ranges != NULL)
7352 comp_unit_die->attrs[i++] = *ranges;
7353 if (comp_dir != NULL)
7354 comp_unit_die->attrs[i++] = *comp_dir;
7355 comp_unit_die->num_attrs += num_extra_attrs;
7356
7357 if (dwarf_die_debug)
7358 {
7359 fprintf_unfiltered (gdb_stdlog,
7360 "Read die from %s@0x%x of %s:\n",
7361 get_section_name (section),
7362 (unsigned) (begin_info_ptr - section->buffer),
7363 bfd_get_filename (abfd));
7364 dump_die (comp_unit_die, dwarf_die_debug);
7365 }
7366
7367 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7368 TUs by skipping the stub and going directly to the entry in the DWO file.
7369 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7370 to get it via circuitous means. Blech. */
7371 if (comp_dir != NULL)
7372 result_reader->comp_dir = DW_STRING (comp_dir);
7373
7374 /* Skip dummy compilation units. */
7375 if (info_ptr >= begin_info_ptr + dwo_unit->length
7376 || peek_abbrev_code (abfd, info_ptr) == 0)
7377 return 0;
7378
7379 *result_info_ptr = info_ptr;
7380 return 1;
7381 }
7382
7383 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7384 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7385 signature is part of the header. */
7386 static gdb::optional<ULONGEST>
7387 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7388 {
7389 if (cu->header.version >= 5)
7390 return cu->header.signature;
7391 struct attribute *attr;
7392 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7393 if (attr == nullptr)
7394 return gdb::optional<ULONGEST> ();
7395 return DW_UNSND (attr);
7396 }
7397
7398 /* Subroutine of init_cutu_and_read_dies to simplify it.
7399 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7400 Returns NULL if the specified DWO unit cannot be found. */
7401
7402 static struct dwo_unit *
7403 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7404 struct die_info *comp_unit_die)
7405 {
7406 struct dwarf2_cu *cu = this_cu->cu;
7407 struct dwo_unit *dwo_unit;
7408 const char *comp_dir, *dwo_name;
7409
7410 gdb_assert (cu != NULL);
7411
7412 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7413 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7414 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7415
7416 if (this_cu->is_debug_types)
7417 {
7418 struct signatured_type *sig_type;
7419
7420 /* Since this_cu is the first member of struct signatured_type,
7421 we can go from a pointer to one to a pointer to the other. */
7422 sig_type = (struct signatured_type *) this_cu;
7423 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7424 }
7425 else
7426 {
7427 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7428 if (!signature.has_value ())
7429 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7430 " [in module %s]"),
7431 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7432 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7433 *signature);
7434 }
7435
7436 return dwo_unit;
7437 }
7438
7439 /* Subroutine of init_cutu_and_read_dies to simplify it.
7440 See it for a description of the parameters.
7441 Read a TU directly from a DWO file, bypassing the stub. */
7442
7443 static void
7444 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7445 int use_existing_cu, int keep,
7446 die_reader_func_ftype *die_reader_func,
7447 void *data)
7448 {
7449 std::unique_ptr<dwarf2_cu> new_cu;
7450 struct signatured_type *sig_type;
7451 struct die_reader_specs reader;
7452 const gdb_byte *info_ptr;
7453 struct die_info *comp_unit_die;
7454 int has_children;
7455 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7456
7457 /* Verify we can do the following downcast, and that we have the
7458 data we need. */
7459 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7460 sig_type = (struct signatured_type *) this_cu;
7461 gdb_assert (sig_type->dwo_unit != NULL);
7462
7463 if (use_existing_cu && this_cu->cu != NULL)
7464 {
7465 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7466 /* There's no need to do the rereading_dwo_cu handling that
7467 init_cutu_and_read_dies does since we don't read the stub. */
7468 }
7469 else
7470 {
7471 /* If !use_existing_cu, this_cu->cu must be NULL. */
7472 gdb_assert (this_cu->cu == NULL);
7473 new_cu.reset (new dwarf2_cu (this_cu));
7474 }
7475
7476 /* A future optimization, if needed, would be to use an existing
7477 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7478 could share abbrev tables. */
7479
7480 /* The abbreviation table used by READER, this must live at least as long as
7481 READER. */
7482 abbrev_table_up dwo_abbrev_table;
7483
7484 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7485 NULL /* stub_comp_unit_die */,
7486 sig_type->dwo_unit->dwo_file->comp_dir,
7487 &reader, &info_ptr,
7488 &comp_unit_die, &has_children,
7489 &dwo_abbrev_table) == 0)
7490 {
7491 /* Dummy die. */
7492 return;
7493 }
7494
7495 /* All the "real" work is done here. */
7496 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7497
7498 /* This duplicates the code in init_cutu_and_read_dies,
7499 but the alternative is making the latter more complex.
7500 This function is only for the special case of using DWO files directly:
7501 no point in overly complicating the general case just to handle this. */
7502 if (new_cu != NULL && keep)
7503 {
7504 /* Link this CU into read_in_chain. */
7505 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7506 dwarf2_per_objfile->read_in_chain = this_cu;
7507 /* The chain owns it now. */
7508 new_cu.release ();
7509 }
7510 }
7511
7512 /* Initialize a CU (or TU) and read its DIEs.
7513 If the CU defers to a DWO file, read the DWO file as well.
7514
7515 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7516 Otherwise the table specified in the comp unit header is read in and used.
7517 This is an optimization for when we already have the abbrev table.
7518
7519 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7520 Otherwise, a new CU is allocated with xmalloc.
7521
7522 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7523 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7524
7525 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7526 linker) then DIE_READER_FUNC will not get called. */
7527
7528 static void
7529 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7530 struct abbrev_table *abbrev_table,
7531 int use_existing_cu, int keep,
7532 bool skip_partial,
7533 die_reader_func_ftype *die_reader_func,
7534 void *data)
7535 {
7536 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7537 struct objfile *objfile = dwarf2_per_objfile->objfile;
7538 struct dwarf2_section_info *section = this_cu->section;
7539 bfd *abfd = get_section_bfd_owner (section);
7540 struct dwarf2_cu *cu;
7541 const gdb_byte *begin_info_ptr, *info_ptr;
7542 struct die_reader_specs reader;
7543 struct die_info *comp_unit_die;
7544 int has_children;
7545 struct signatured_type *sig_type = NULL;
7546 struct dwarf2_section_info *abbrev_section;
7547 /* Non-zero if CU currently points to a DWO file and we need to
7548 reread it. When this happens we need to reread the skeleton die
7549 before we can reread the DWO file (this only applies to CUs, not TUs). */
7550 int rereading_dwo_cu = 0;
7551
7552 if (dwarf_die_debug)
7553 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7554 this_cu->is_debug_types ? "type" : "comp",
7555 sect_offset_str (this_cu->sect_off));
7556
7557 if (use_existing_cu)
7558 gdb_assert (keep);
7559
7560 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7561 file (instead of going through the stub), short-circuit all of this. */
7562 if (this_cu->reading_dwo_directly)
7563 {
7564 /* Narrow down the scope of possibilities to have to understand. */
7565 gdb_assert (this_cu->is_debug_types);
7566 gdb_assert (abbrev_table == NULL);
7567 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7568 die_reader_func, data);
7569 return;
7570 }
7571
7572 /* This is cheap if the section is already read in. */
7573 dwarf2_read_section (objfile, section);
7574
7575 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7576
7577 abbrev_section = get_abbrev_section_for_cu (this_cu);
7578
7579 std::unique_ptr<dwarf2_cu> new_cu;
7580 if (use_existing_cu && this_cu->cu != NULL)
7581 {
7582 cu = this_cu->cu;
7583 /* If this CU is from a DWO file we need to start over, we need to
7584 refetch the attributes from the skeleton CU.
7585 This could be optimized by retrieving those attributes from when we
7586 were here the first time: the previous comp_unit_die was stored in
7587 comp_unit_obstack. But there's no data yet that we need this
7588 optimization. */
7589 if (cu->dwo_unit != NULL)
7590 rereading_dwo_cu = 1;
7591 }
7592 else
7593 {
7594 /* If !use_existing_cu, this_cu->cu must be NULL. */
7595 gdb_assert (this_cu->cu == NULL);
7596 new_cu.reset (new dwarf2_cu (this_cu));
7597 cu = new_cu.get ();
7598 }
7599
7600 /* Get the header. */
7601 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7602 {
7603 /* We already have the header, there's no need to read it in again. */
7604 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7605 }
7606 else
7607 {
7608 if (this_cu->is_debug_types)
7609 {
7610 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7611 &cu->header, section,
7612 abbrev_section, info_ptr,
7613 rcuh_kind::TYPE);
7614
7615 /* Since per_cu is the first member of struct signatured_type,
7616 we can go from a pointer to one to a pointer to the other. */
7617 sig_type = (struct signatured_type *) this_cu;
7618 gdb_assert (sig_type->signature == cu->header.signature);
7619 gdb_assert (sig_type->type_offset_in_tu
7620 == cu->header.type_cu_offset_in_tu);
7621 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7622
7623 /* LENGTH has not been set yet for type units if we're
7624 using .gdb_index. */
7625 this_cu->length = get_cu_length (&cu->header);
7626
7627 /* Establish the type offset that can be used to lookup the type. */
7628 sig_type->type_offset_in_section =
7629 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7630
7631 this_cu->dwarf_version = cu->header.version;
7632 }
7633 else
7634 {
7635 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7636 &cu->header, section,
7637 abbrev_section,
7638 info_ptr,
7639 rcuh_kind::COMPILE);
7640
7641 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7642 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7643 this_cu->dwarf_version = cu->header.version;
7644 }
7645 }
7646
7647 /* Skip dummy compilation units. */
7648 if (info_ptr >= begin_info_ptr + this_cu->length
7649 || peek_abbrev_code (abfd, info_ptr) == 0)
7650 return;
7651
7652 /* If we don't have them yet, read the abbrevs for this compilation unit.
7653 And if we need to read them now, make sure they're freed when we're
7654 done (own the table through ABBREV_TABLE_HOLDER). */
7655 abbrev_table_up abbrev_table_holder;
7656 if (abbrev_table != NULL)
7657 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7658 else
7659 {
7660 abbrev_table_holder
7661 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7662 cu->header.abbrev_sect_off);
7663 abbrev_table = abbrev_table_holder.get ();
7664 }
7665
7666 /* Read the top level CU/TU die. */
7667 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7668 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7669
7670 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7671 return;
7672
7673 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7674 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7675 table from the DWO file and pass the ownership over to us. It will be
7676 referenced from READER, so we must make sure to free it after we're done
7677 with READER.
7678
7679 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7680 DWO CU, that this test will fail (the attribute will not be present). */
7681 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7682 abbrev_table_up dwo_abbrev_table;
7683 if (dwo_name != nullptr)
7684 {
7685 struct dwo_unit *dwo_unit;
7686 struct die_info *dwo_comp_unit_die;
7687
7688 if (has_children)
7689 {
7690 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7691 " has children (offset %s) [in module %s]"),
7692 sect_offset_str (this_cu->sect_off),
7693 bfd_get_filename (abfd));
7694 }
7695 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7696 if (dwo_unit != NULL)
7697 {
7698 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7699 comp_unit_die, NULL,
7700 &reader, &info_ptr,
7701 &dwo_comp_unit_die, &has_children,
7702 &dwo_abbrev_table) == 0)
7703 {
7704 /* Dummy die. */
7705 return;
7706 }
7707 comp_unit_die = dwo_comp_unit_die;
7708 }
7709 else
7710 {
7711 /* Yikes, we couldn't find the rest of the DIE, we only have
7712 the stub. A complaint has already been logged. There's
7713 not much more we can do except pass on the stub DIE to
7714 die_reader_func. We don't want to throw an error on bad
7715 debug info. */
7716 }
7717 }
7718
7719 /* All of the above is setup for this call. Yikes. */
7720 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7721
7722 /* Done, clean up. */
7723 if (new_cu != NULL && keep)
7724 {
7725 /* Link this CU into read_in_chain. */
7726 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7727 dwarf2_per_objfile->read_in_chain = this_cu;
7728 /* The chain owns it now. */
7729 new_cu.release ();
7730 }
7731 }
7732
7733 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7734 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7735 to have already done the lookup to find the DWO file).
7736
7737 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7738 THIS_CU->is_debug_types, but nothing else.
7739
7740 We fill in THIS_CU->length.
7741
7742 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7743 linker) then DIE_READER_FUNC will not get called.
7744
7745 THIS_CU->cu is always freed when done.
7746 This is done in order to not leave THIS_CU->cu in a state where we have
7747 to care whether it refers to the "main" CU or the DWO CU. */
7748
7749 static void
7750 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7751 struct dwo_file *dwo_file,
7752 die_reader_func_ftype *die_reader_func,
7753 void *data)
7754 {
7755 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7756 struct objfile *objfile = dwarf2_per_objfile->objfile;
7757 struct dwarf2_section_info *section = this_cu->section;
7758 bfd *abfd = get_section_bfd_owner (section);
7759 struct dwarf2_section_info *abbrev_section;
7760 const gdb_byte *begin_info_ptr, *info_ptr;
7761 struct die_reader_specs reader;
7762 struct die_info *comp_unit_die;
7763 int has_children;
7764
7765 if (dwarf_die_debug)
7766 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7767 this_cu->is_debug_types ? "type" : "comp",
7768 sect_offset_str (this_cu->sect_off));
7769
7770 gdb_assert (this_cu->cu == NULL);
7771
7772 abbrev_section = (dwo_file != NULL
7773 ? &dwo_file->sections.abbrev
7774 : get_abbrev_section_for_cu (this_cu));
7775
7776 /* This is cheap if the section is already read in. */
7777 dwarf2_read_section (objfile, section);
7778
7779 struct dwarf2_cu cu (this_cu);
7780
7781 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7782 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7783 &cu.header, section,
7784 abbrev_section, info_ptr,
7785 (this_cu->is_debug_types
7786 ? rcuh_kind::TYPE
7787 : rcuh_kind::COMPILE));
7788
7789 this_cu->length = get_cu_length (&cu.header);
7790
7791 /* Skip dummy compilation units. */
7792 if (info_ptr >= begin_info_ptr + this_cu->length
7793 || peek_abbrev_code (abfd, info_ptr) == 0)
7794 return;
7795
7796 abbrev_table_up abbrev_table
7797 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7798 cu.header.abbrev_sect_off);
7799
7800 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
7801 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7802
7803 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7804 }
7805
7806 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7807 does not lookup the specified DWO file.
7808 This cannot be used to read DWO files.
7809
7810 THIS_CU->cu is always freed when done.
7811 This is done in order to not leave THIS_CU->cu in a state where we have
7812 to care whether it refers to the "main" CU or the DWO CU.
7813 We can revisit this if the data shows there's a performance issue. */
7814
7815 static void
7816 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7817 die_reader_func_ftype *die_reader_func,
7818 void *data)
7819 {
7820 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
7821 }
7822 \f
7823 /* Type Unit Groups.
7824
7825 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7826 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7827 so that all types coming from the same compilation (.o file) are grouped
7828 together. A future step could be to put the types in the same symtab as
7829 the CU the types ultimately came from. */
7830
7831 static hashval_t
7832 hash_type_unit_group (const void *item)
7833 {
7834 const struct type_unit_group *tu_group
7835 = (const struct type_unit_group *) item;
7836
7837 return hash_stmt_list_entry (&tu_group->hash);
7838 }
7839
7840 static int
7841 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7842 {
7843 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7844 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7845
7846 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7847 }
7848
7849 /* Allocate a hash table for type unit groups. */
7850
7851 static htab_t
7852 allocate_type_unit_groups_table (struct objfile *objfile)
7853 {
7854 return htab_create_alloc_ex (3,
7855 hash_type_unit_group,
7856 eq_type_unit_group,
7857 NULL,
7858 &objfile->objfile_obstack,
7859 hashtab_obstack_allocate,
7860 dummy_obstack_deallocate);
7861 }
7862
7863 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7864 partial symtabs. We combine several TUs per psymtab to not let the size
7865 of any one psymtab grow too big. */
7866 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7867 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7868
7869 /* Helper routine for get_type_unit_group.
7870 Create the type_unit_group object used to hold one or more TUs. */
7871
7872 static struct type_unit_group *
7873 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7874 {
7875 struct dwarf2_per_objfile *dwarf2_per_objfile
7876 = cu->per_cu->dwarf2_per_objfile;
7877 struct objfile *objfile = dwarf2_per_objfile->objfile;
7878 struct dwarf2_per_cu_data *per_cu;
7879 struct type_unit_group *tu_group;
7880
7881 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7882 struct type_unit_group);
7883 per_cu = &tu_group->per_cu;
7884 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7885
7886 if (dwarf2_per_objfile->using_index)
7887 {
7888 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7889 struct dwarf2_per_cu_quick_data);
7890 }
7891 else
7892 {
7893 unsigned int line_offset = to_underlying (line_offset_struct);
7894 struct partial_symtab *pst;
7895 std::string name;
7896
7897 /* Give the symtab a useful name for debug purposes. */
7898 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7899 name = string_printf ("<type_units_%d>",
7900 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7901 else
7902 name = string_printf ("<type_units_at_0x%x>", line_offset);
7903
7904 pst = create_partial_symtab (per_cu, name.c_str ());
7905 pst->anonymous = 1;
7906 }
7907
7908 tu_group->hash.dwo_unit = cu->dwo_unit;
7909 tu_group->hash.line_sect_off = line_offset_struct;
7910
7911 return tu_group;
7912 }
7913
7914 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7915 STMT_LIST is a DW_AT_stmt_list attribute. */
7916
7917 static struct type_unit_group *
7918 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7919 {
7920 struct dwarf2_per_objfile *dwarf2_per_objfile
7921 = cu->per_cu->dwarf2_per_objfile;
7922 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7923 struct type_unit_group *tu_group;
7924 void **slot;
7925 unsigned int line_offset;
7926 struct type_unit_group type_unit_group_for_lookup;
7927
7928 if (dwarf2_per_objfile->type_unit_groups == NULL)
7929 {
7930 dwarf2_per_objfile->type_unit_groups =
7931 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7932 }
7933
7934 /* Do we need to create a new group, or can we use an existing one? */
7935
7936 if (stmt_list)
7937 {
7938 line_offset = DW_UNSND (stmt_list);
7939 ++tu_stats->nr_symtab_sharers;
7940 }
7941 else
7942 {
7943 /* Ugh, no stmt_list. Rare, but we have to handle it.
7944 We can do various things here like create one group per TU or
7945 spread them over multiple groups to split up the expansion work.
7946 To avoid worst case scenarios (too many groups or too large groups)
7947 we, umm, group them in bunches. */
7948 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7949 | (tu_stats->nr_stmt_less_type_units
7950 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7951 ++tu_stats->nr_stmt_less_type_units;
7952 }
7953
7954 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7955 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7956 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7957 &type_unit_group_for_lookup, INSERT);
7958 if (*slot != NULL)
7959 {
7960 tu_group = (struct type_unit_group *) *slot;
7961 gdb_assert (tu_group != NULL);
7962 }
7963 else
7964 {
7965 sect_offset line_offset_struct = (sect_offset) line_offset;
7966 tu_group = create_type_unit_group (cu, line_offset_struct);
7967 *slot = tu_group;
7968 ++tu_stats->nr_symtabs;
7969 }
7970
7971 return tu_group;
7972 }
7973 \f
7974 /* Partial symbol tables. */
7975
7976 /* Create a psymtab named NAME and assign it to PER_CU.
7977
7978 The caller must fill in the following details:
7979 dirname, textlow, texthigh. */
7980
7981 static struct partial_symtab *
7982 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7983 {
7984 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7985 struct partial_symtab *pst;
7986
7987 pst = start_psymtab_common (objfile, name, 0);
7988
7989 pst->psymtabs_addrmap_supported = 1;
7990
7991 /* This is the glue that links PST into GDB's symbol API. */
7992 pst->read_symtab_private = per_cu;
7993 pst->read_symtab = dwarf2_read_symtab;
7994 per_cu->v.psymtab = pst;
7995
7996 return pst;
7997 }
7998
7999 /* The DATA object passed to process_psymtab_comp_unit_reader has this
8000 type. */
8001
8002 struct process_psymtab_comp_unit_data
8003 {
8004 /* True if we are reading a DW_TAG_partial_unit. */
8005
8006 int want_partial_unit;
8007
8008 /* The "pretend" language that is used if the CU doesn't declare a
8009 language. */
8010
8011 enum language pretend_language;
8012 };
8013
8014 /* die_reader_func for process_psymtab_comp_unit. */
8015
8016 static void
8017 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8018 const gdb_byte *info_ptr,
8019 struct die_info *comp_unit_die,
8020 int has_children,
8021 void *data)
8022 {
8023 struct dwarf2_cu *cu = reader->cu;
8024 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8025 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8026 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8027 CORE_ADDR baseaddr;
8028 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8029 struct partial_symtab *pst;
8030 enum pc_bounds_kind cu_bounds_kind;
8031 const char *filename;
8032 struct process_psymtab_comp_unit_data *info
8033 = (struct process_psymtab_comp_unit_data *) data;
8034
8035 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8036 return;
8037
8038 gdb_assert (! per_cu->is_debug_types);
8039
8040 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8041
8042 /* Allocate a new partial symbol table structure. */
8043 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8044 if (filename == NULL)
8045 filename = "";
8046
8047 pst = create_partial_symtab (per_cu, filename);
8048
8049 /* This must be done before calling dwarf2_build_include_psymtabs. */
8050 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8051
8052 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8053
8054 dwarf2_find_base_address (comp_unit_die, cu);
8055
8056 /* Possibly set the default values of LOWPC and HIGHPC from
8057 `DW_AT_ranges'. */
8058 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8059 &best_highpc, cu, pst);
8060 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8061 {
8062 CORE_ADDR low
8063 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8064 - baseaddr);
8065 CORE_ADDR high
8066 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8067 - baseaddr - 1);
8068 /* Store the contiguous range if it is not empty; it can be
8069 empty for CUs with no code. */
8070 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8071 low, high, pst);
8072 }
8073
8074 /* Check if comp unit has_children.
8075 If so, read the rest of the partial symbols from this comp unit.
8076 If not, there's no more debug_info for this comp unit. */
8077 if (has_children)
8078 {
8079 struct partial_die_info *first_die;
8080 CORE_ADDR lowpc, highpc;
8081
8082 lowpc = ((CORE_ADDR) -1);
8083 highpc = ((CORE_ADDR) 0);
8084
8085 first_die = load_partial_dies (reader, info_ptr, 1);
8086
8087 scan_partial_symbols (first_die, &lowpc, &highpc,
8088 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8089
8090 /* If we didn't find a lowpc, set it to highpc to avoid
8091 complaints from `maint check'. */
8092 if (lowpc == ((CORE_ADDR) -1))
8093 lowpc = highpc;
8094
8095 /* If the compilation unit didn't have an explicit address range,
8096 then use the information extracted from its child dies. */
8097 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8098 {
8099 best_lowpc = lowpc;
8100 best_highpc = highpc;
8101 }
8102 }
8103 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
8104 best_lowpc + baseaddr)
8105 - baseaddr);
8106 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
8107 best_highpc + baseaddr)
8108 - baseaddr);
8109
8110 end_psymtab_common (objfile, pst);
8111
8112 if (!cu->per_cu->imported_symtabs_empty ())
8113 {
8114 int i;
8115 int len = cu->per_cu->imported_symtabs_size ();
8116
8117 /* Fill in 'dependencies' here; we fill in 'users' in a
8118 post-pass. */
8119 pst->number_of_dependencies = len;
8120 pst->dependencies
8121 = objfile->partial_symtabs->allocate_dependencies (len);
8122 for (i = 0; i < len; ++i)
8123 {
8124 pst->dependencies[i]
8125 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
8126 }
8127
8128 cu->per_cu->imported_symtabs_free ();
8129 }
8130
8131 /* Get the list of files included in the current compilation unit,
8132 and build a psymtab for each of them. */
8133 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8134
8135 if (dwarf_read_debug)
8136 fprintf_unfiltered (gdb_stdlog,
8137 "Psymtab for %s unit @%s: %s - %s"
8138 ", %d global, %d static syms\n",
8139 per_cu->is_debug_types ? "type" : "comp",
8140 sect_offset_str (per_cu->sect_off),
8141 paddress (gdbarch, pst->text_low (objfile)),
8142 paddress (gdbarch, pst->text_high (objfile)),
8143 pst->n_global_syms, pst->n_static_syms);
8144 }
8145
8146 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8147 Process compilation unit THIS_CU for a psymtab. */
8148
8149 static void
8150 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8151 int want_partial_unit,
8152 enum language pretend_language)
8153 {
8154 /* If this compilation unit was already read in, free the
8155 cached copy in order to read it in again. This is
8156 necessary because we skipped some symbols when we first
8157 read in the compilation unit (see load_partial_dies).
8158 This problem could be avoided, but the benefit is unclear. */
8159 if (this_cu->cu != NULL)
8160 free_one_cached_comp_unit (this_cu);
8161
8162 if (this_cu->is_debug_types)
8163 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8164 build_type_psymtabs_reader, NULL);
8165 else
8166 {
8167 process_psymtab_comp_unit_data info;
8168 info.want_partial_unit = want_partial_unit;
8169 info.pretend_language = pretend_language;
8170 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8171 process_psymtab_comp_unit_reader, &info);
8172 }
8173
8174 /* Age out any secondary CUs. */
8175 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8176 }
8177
8178 /* Reader function for build_type_psymtabs. */
8179
8180 static void
8181 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8182 const gdb_byte *info_ptr,
8183 struct die_info *type_unit_die,
8184 int has_children,
8185 void *data)
8186 {
8187 struct dwarf2_per_objfile *dwarf2_per_objfile
8188 = reader->cu->per_cu->dwarf2_per_objfile;
8189 struct objfile *objfile = dwarf2_per_objfile->objfile;
8190 struct dwarf2_cu *cu = reader->cu;
8191 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8192 struct signatured_type *sig_type;
8193 struct type_unit_group *tu_group;
8194 struct attribute *attr;
8195 struct partial_die_info *first_die;
8196 CORE_ADDR lowpc, highpc;
8197 struct partial_symtab *pst;
8198
8199 gdb_assert (data == NULL);
8200 gdb_assert (per_cu->is_debug_types);
8201 sig_type = (struct signatured_type *) per_cu;
8202
8203 if (! has_children)
8204 return;
8205
8206 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8207 tu_group = get_type_unit_group (cu, attr);
8208
8209 if (tu_group->tus == nullptr)
8210 tu_group->tus = new std::vector<signatured_type *>;
8211 tu_group->tus->push_back (sig_type);
8212
8213 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8214 pst = create_partial_symtab (per_cu, "");
8215 pst->anonymous = 1;
8216
8217 first_die = load_partial_dies (reader, info_ptr, 1);
8218
8219 lowpc = (CORE_ADDR) -1;
8220 highpc = (CORE_ADDR) 0;
8221 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8222
8223 end_psymtab_common (objfile, pst);
8224 }
8225
8226 /* Struct used to sort TUs by their abbreviation table offset. */
8227
8228 struct tu_abbrev_offset
8229 {
8230 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8231 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8232 {}
8233
8234 signatured_type *sig_type;
8235 sect_offset abbrev_offset;
8236 };
8237
8238 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8239
8240 static bool
8241 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8242 const struct tu_abbrev_offset &b)
8243 {
8244 return a.abbrev_offset < b.abbrev_offset;
8245 }
8246
8247 /* Efficiently read all the type units.
8248 This does the bulk of the work for build_type_psymtabs.
8249
8250 The efficiency is because we sort TUs by the abbrev table they use and
8251 only read each abbrev table once. In one program there are 200K TUs
8252 sharing 8K abbrev tables.
8253
8254 The main purpose of this function is to support building the
8255 dwarf2_per_objfile->type_unit_groups table.
8256 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8257 can collapse the search space by grouping them by stmt_list.
8258 The savings can be significant, in the same program from above the 200K TUs
8259 share 8K stmt_list tables.
8260
8261 FUNC is expected to call get_type_unit_group, which will create the
8262 struct type_unit_group if necessary and add it to
8263 dwarf2_per_objfile->type_unit_groups. */
8264
8265 static void
8266 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8267 {
8268 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8269 abbrev_table_up abbrev_table;
8270 sect_offset abbrev_offset;
8271
8272 /* It's up to the caller to not call us multiple times. */
8273 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8274
8275 if (dwarf2_per_objfile->all_type_units.empty ())
8276 return;
8277
8278 /* TUs typically share abbrev tables, and there can be way more TUs than
8279 abbrev tables. Sort by abbrev table to reduce the number of times we
8280 read each abbrev table in.
8281 Alternatives are to punt or to maintain a cache of abbrev tables.
8282 This is simpler and efficient enough for now.
8283
8284 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8285 symtab to use). Typically TUs with the same abbrev offset have the same
8286 stmt_list value too so in practice this should work well.
8287
8288 The basic algorithm here is:
8289
8290 sort TUs by abbrev table
8291 for each TU with same abbrev table:
8292 read abbrev table if first user
8293 read TU top level DIE
8294 [IWBN if DWO skeletons had DW_AT_stmt_list]
8295 call FUNC */
8296
8297 if (dwarf_read_debug)
8298 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8299
8300 /* Sort in a separate table to maintain the order of all_type_units
8301 for .gdb_index: TU indices directly index all_type_units. */
8302 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8303 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8304
8305 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8306 sorted_by_abbrev.emplace_back
8307 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8308 sig_type->per_cu.section,
8309 sig_type->per_cu.sect_off));
8310
8311 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8312 sort_tu_by_abbrev_offset);
8313
8314 abbrev_offset = (sect_offset) ~(unsigned) 0;
8315
8316 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
8317 {
8318 /* Switch to the next abbrev table if necessary. */
8319 if (abbrev_table == NULL
8320 || tu.abbrev_offset != abbrev_offset)
8321 {
8322 abbrev_offset = tu.abbrev_offset;
8323 abbrev_table =
8324 abbrev_table_read_table (dwarf2_per_objfile,
8325 &dwarf2_per_objfile->abbrev,
8326 abbrev_offset);
8327 ++tu_stats->nr_uniq_abbrev_tables;
8328 }
8329
8330 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
8331 0, 0, false, build_type_psymtabs_reader, NULL);
8332 }
8333 }
8334
8335 /* Print collected type unit statistics. */
8336
8337 static void
8338 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8339 {
8340 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8341
8342 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8343 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8344 dwarf2_per_objfile->all_type_units.size ());
8345 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8346 tu_stats->nr_uniq_abbrev_tables);
8347 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8348 tu_stats->nr_symtabs);
8349 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8350 tu_stats->nr_symtab_sharers);
8351 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8352 tu_stats->nr_stmt_less_type_units);
8353 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8354 tu_stats->nr_all_type_units_reallocs);
8355 }
8356
8357 /* Traversal function for build_type_psymtabs. */
8358
8359 static int
8360 build_type_psymtab_dependencies (void **slot, void *info)
8361 {
8362 struct dwarf2_per_objfile *dwarf2_per_objfile
8363 = (struct dwarf2_per_objfile *) info;
8364 struct objfile *objfile = dwarf2_per_objfile->objfile;
8365 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8366 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8367 struct partial_symtab *pst = per_cu->v.psymtab;
8368 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
8369 int i;
8370
8371 gdb_assert (len > 0);
8372 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8373
8374 pst->number_of_dependencies = len;
8375 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8376 for (i = 0; i < len; ++i)
8377 {
8378 struct signatured_type *iter = tu_group->tus->at (i);
8379 gdb_assert (iter->per_cu.is_debug_types);
8380 pst->dependencies[i] = iter->per_cu.v.psymtab;
8381 iter->type_unit_group = tu_group;
8382 }
8383
8384 delete tu_group->tus;
8385 tu_group->tus = nullptr;
8386
8387 return 1;
8388 }
8389
8390 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8391 Build partial symbol tables for the .debug_types comp-units. */
8392
8393 static void
8394 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8395 {
8396 if (! create_all_type_units (dwarf2_per_objfile))
8397 return;
8398
8399 build_type_psymtabs_1 (dwarf2_per_objfile);
8400 }
8401
8402 /* Traversal function for process_skeletonless_type_unit.
8403 Read a TU in a DWO file and build partial symbols for it. */
8404
8405 static int
8406 process_skeletonless_type_unit (void **slot, void *info)
8407 {
8408 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8409 struct dwarf2_per_objfile *dwarf2_per_objfile
8410 = (struct dwarf2_per_objfile *) info;
8411 struct signatured_type find_entry, *entry;
8412
8413 /* If this TU doesn't exist in the global table, add it and read it in. */
8414
8415 if (dwarf2_per_objfile->signatured_types == NULL)
8416 {
8417 dwarf2_per_objfile->signatured_types
8418 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8419 }
8420
8421 find_entry.signature = dwo_unit->signature;
8422 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8423 INSERT);
8424 /* If we've already seen this type there's nothing to do. What's happening
8425 is we're doing our own version of comdat-folding here. */
8426 if (*slot != NULL)
8427 return 1;
8428
8429 /* This does the job that create_all_type_units would have done for
8430 this TU. */
8431 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8432 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8433 *slot = entry;
8434
8435 /* This does the job that build_type_psymtabs_1 would have done. */
8436 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
8437 build_type_psymtabs_reader, NULL);
8438
8439 return 1;
8440 }
8441
8442 /* Traversal function for process_skeletonless_type_units. */
8443
8444 static int
8445 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8446 {
8447 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8448
8449 if (dwo_file->tus != NULL)
8450 {
8451 htab_traverse_noresize (dwo_file->tus,
8452 process_skeletonless_type_unit, info);
8453 }
8454
8455 return 1;
8456 }
8457
8458 /* Scan all TUs of DWO files, verifying we've processed them.
8459 This is needed in case a TU was emitted without its skeleton.
8460 Note: This can't be done until we know what all the DWO files are. */
8461
8462 static void
8463 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8464 {
8465 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8466 if (get_dwp_file (dwarf2_per_objfile) == NULL
8467 && dwarf2_per_objfile->dwo_files != NULL)
8468 {
8469 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
8470 process_dwo_file_for_skeletonless_type_units,
8471 dwarf2_per_objfile);
8472 }
8473 }
8474
8475 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8476
8477 static void
8478 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8479 {
8480 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8481 {
8482 struct partial_symtab *pst = per_cu->v.psymtab;
8483
8484 if (pst == NULL)
8485 continue;
8486
8487 for (int j = 0; j < pst->number_of_dependencies; ++j)
8488 {
8489 /* Set the 'user' field only if it is not already set. */
8490 if (pst->dependencies[j]->user == NULL)
8491 pst->dependencies[j]->user = pst;
8492 }
8493 }
8494 }
8495
8496 /* Build the partial symbol table by doing a quick pass through the
8497 .debug_info and .debug_abbrev sections. */
8498
8499 static void
8500 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8501 {
8502 struct objfile *objfile = dwarf2_per_objfile->objfile;
8503
8504 if (dwarf_read_debug)
8505 {
8506 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8507 objfile_name (objfile));
8508 }
8509
8510 dwarf2_per_objfile->reading_partial_symbols = 1;
8511
8512 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8513
8514 /* Any cached compilation units will be linked by the per-objfile
8515 read_in_chain. Make sure to free them when we're done. */
8516 free_cached_comp_units freer (dwarf2_per_objfile);
8517
8518 build_type_psymtabs (dwarf2_per_objfile);
8519
8520 create_all_comp_units (dwarf2_per_objfile);
8521
8522 /* Create a temporary address map on a temporary obstack. We later
8523 copy this to the final obstack. */
8524 auto_obstack temp_obstack;
8525
8526 scoped_restore save_psymtabs_addrmap
8527 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8528 addrmap_create_mutable (&temp_obstack));
8529
8530 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8531 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8532
8533 /* This has to wait until we read the CUs, we need the list of DWOs. */
8534 process_skeletonless_type_units (dwarf2_per_objfile);
8535
8536 /* Now that all TUs have been processed we can fill in the dependencies. */
8537 if (dwarf2_per_objfile->type_unit_groups != NULL)
8538 {
8539 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8540 build_type_psymtab_dependencies, dwarf2_per_objfile);
8541 }
8542
8543 if (dwarf_read_debug)
8544 print_tu_stats (dwarf2_per_objfile);
8545
8546 set_partial_user (dwarf2_per_objfile);
8547
8548 objfile->partial_symtabs->psymtabs_addrmap
8549 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8550 objfile->partial_symtabs->obstack ());
8551 /* At this point we want to keep the address map. */
8552 save_psymtabs_addrmap.release ();
8553
8554 if (dwarf_read_debug)
8555 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8556 objfile_name (objfile));
8557 }
8558
8559 /* die_reader_func for load_partial_comp_unit. */
8560
8561 static void
8562 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8563 const gdb_byte *info_ptr,
8564 struct die_info *comp_unit_die,
8565 int has_children,
8566 void *data)
8567 {
8568 struct dwarf2_cu *cu = reader->cu;
8569
8570 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8571
8572 /* Check if comp unit has_children.
8573 If so, read the rest of the partial symbols from this comp unit.
8574 If not, there's no more debug_info for this comp unit. */
8575 if (has_children)
8576 load_partial_dies (reader, info_ptr, 0);
8577 }
8578
8579 /* Load the partial DIEs for a secondary CU into memory.
8580 This is also used when rereading a primary CU with load_all_dies. */
8581
8582 static void
8583 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8584 {
8585 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
8586 load_partial_comp_unit_reader, NULL);
8587 }
8588
8589 static void
8590 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8591 struct dwarf2_section_info *section,
8592 struct dwarf2_section_info *abbrev_section,
8593 unsigned int is_dwz)
8594 {
8595 const gdb_byte *info_ptr;
8596 struct objfile *objfile = dwarf2_per_objfile->objfile;
8597
8598 if (dwarf_read_debug)
8599 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8600 get_section_name (section),
8601 get_section_file_name (section));
8602
8603 dwarf2_read_section (objfile, section);
8604
8605 info_ptr = section->buffer;
8606
8607 while (info_ptr < section->buffer + section->size)
8608 {
8609 struct dwarf2_per_cu_data *this_cu;
8610
8611 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8612
8613 comp_unit_head cu_header;
8614 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8615 abbrev_section, info_ptr,
8616 rcuh_kind::COMPILE);
8617
8618 /* Save the compilation unit for later lookup. */
8619 if (cu_header.unit_type != DW_UT_type)
8620 {
8621 this_cu = XOBNEW (&objfile->objfile_obstack,
8622 struct dwarf2_per_cu_data);
8623 memset (this_cu, 0, sizeof (*this_cu));
8624 }
8625 else
8626 {
8627 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8628 struct signatured_type);
8629 memset (sig_type, 0, sizeof (*sig_type));
8630 sig_type->signature = cu_header.signature;
8631 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8632 this_cu = &sig_type->per_cu;
8633 }
8634 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8635 this_cu->sect_off = sect_off;
8636 this_cu->length = cu_header.length + cu_header.initial_length_size;
8637 this_cu->is_dwz = is_dwz;
8638 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8639 this_cu->section = section;
8640
8641 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8642
8643 info_ptr = info_ptr + this_cu->length;
8644 }
8645 }
8646
8647 /* Create a list of all compilation units in OBJFILE.
8648 This is only done for -readnow and building partial symtabs. */
8649
8650 static void
8651 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8652 {
8653 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8654 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8655 &dwarf2_per_objfile->abbrev, 0);
8656
8657 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8658 if (dwz != NULL)
8659 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8660 1);
8661 }
8662
8663 /* Process all loaded DIEs for compilation unit CU, starting at
8664 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8665 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8666 DW_AT_ranges). See the comments of add_partial_subprogram on how
8667 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8668
8669 static void
8670 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8671 CORE_ADDR *highpc, int set_addrmap,
8672 struct dwarf2_cu *cu)
8673 {
8674 struct partial_die_info *pdi;
8675
8676 /* Now, march along the PDI's, descending into ones which have
8677 interesting children but skipping the children of the other ones,
8678 until we reach the end of the compilation unit. */
8679
8680 pdi = first_die;
8681
8682 while (pdi != NULL)
8683 {
8684 pdi->fixup (cu);
8685
8686 /* Anonymous namespaces or modules have no name but have interesting
8687 children, so we need to look at them. Ditto for anonymous
8688 enums. */
8689
8690 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8691 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8692 || pdi->tag == DW_TAG_imported_unit
8693 || pdi->tag == DW_TAG_inlined_subroutine)
8694 {
8695 switch (pdi->tag)
8696 {
8697 case DW_TAG_subprogram:
8698 case DW_TAG_inlined_subroutine:
8699 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8700 break;
8701 case DW_TAG_constant:
8702 case DW_TAG_variable:
8703 case DW_TAG_typedef:
8704 case DW_TAG_union_type:
8705 if (!pdi->is_declaration)
8706 {
8707 add_partial_symbol (pdi, cu);
8708 }
8709 break;
8710 case DW_TAG_class_type:
8711 case DW_TAG_interface_type:
8712 case DW_TAG_structure_type:
8713 if (!pdi->is_declaration)
8714 {
8715 add_partial_symbol (pdi, cu);
8716 }
8717 if ((cu->language == language_rust
8718 || cu->language == language_cplus) && pdi->has_children)
8719 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8720 set_addrmap, cu);
8721 break;
8722 case DW_TAG_enumeration_type:
8723 if (!pdi->is_declaration)
8724 add_partial_enumeration (pdi, cu);
8725 break;
8726 case DW_TAG_base_type:
8727 case DW_TAG_subrange_type:
8728 /* File scope base type definitions are added to the partial
8729 symbol table. */
8730 add_partial_symbol (pdi, cu);
8731 break;
8732 case DW_TAG_namespace:
8733 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8734 break;
8735 case DW_TAG_module:
8736 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8737 break;
8738 case DW_TAG_imported_unit:
8739 {
8740 struct dwarf2_per_cu_data *per_cu;
8741
8742 /* For now we don't handle imported units in type units. */
8743 if (cu->per_cu->is_debug_types)
8744 {
8745 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8746 " supported in type units [in module %s]"),
8747 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8748 }
8749
8750 per_cu = dwarf2_find_containing_comp_unit
8751 (pdi->d.sect_off, pdi->is_dwz,
8752 cu->per_cu->dwarf2_per_objfile);
8753
8754 /* Go read the partial unit, if needed. */
8755 if (per_cu->v.psymtab == NULL)
8756 process_psymtab_comp_unit (per_cu, 1, cu->language);
8757
8758 cu->per_cu->imported_symtabs_push (per_cu);
8759 }
8760 break;
8761 case DW_TAG_imported_declaration:
8762 add_partial_symbol (pdi, cu);
8763 break;
8764 default:
8765 break;
8766 }
8767 }
8768
8769 /* If the die has a sibling, skip to the sibling. */
8770
8771 pdi = pdi->die_sibling;
8772 }
8773 }
8774
8775 /* Functions used to compute the fully scoped name of a partial DIE.
8776
8777 Normally, this is simple. For C++, the parent DIE's fully scoped
8778 name is concatenated with "::" and the partial DIE's name.
8779 Enumerators are an exception; they use the scope of their parent
8780 enumeration type, i.e. the name of the enumeration type is not
8781 prepended to the enumerator.
8782
8783 There are two complexities. One is DW_AT_specification; in this
8784 case "parent" means the parent of the target of the specification,
8785 instead of the direct parent of the DIE. The other is compilers
8786 which do not emit DW_TAG_namespace; in this case we try to guess
8787 the fully qualified name of structure types from their members'
8788 linkage names. This must be done using the DIE's children rather
8789 than the children of any DW_AT_specification target. We only need
8790 to do this for structures at the top level, i.e. if the target of
8791 any DW_AT_specification (if any; otherwise the DIE itself) does not
8792 have a parent. */
8793
8794 /* Compute the scope prefix associated with PDI's parent, in
8795 compilation unit CU. The result will be allocated on CU's
8796 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8797 field. NULL is returned if no prefix is necessary. */
8798 static const char *
8799 partial_die_parent_scope (struct partial_die_info *pdi,
8800 struct dwarf2_cu *cu)
8801 {
8802 const char *grandparent_scope;
8803 struct partial_die_info *parent, *real_pdi;
8804
8805 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8806 then this means the parent of the specification DIE. */
8807
8808 real_pdi = pdi;
8809 while (real_pdi->has_specification)
8810 {
8811 auto res = find_partial_die (real_pdi->spec_offset,
8812 real_pdi->spec_is_dwz, cu);
8813 real_pdi = res.pdi;
8814 cu = res.cu;
8815 }
8816
8817 parent = real_pdi->die_parent;
8818 if (parent == NULL)
8819 return NULL;
8820
8821 if (parent->scope_set)
8822 return parent->scope;
8823
8824 parent->fixup (cu);
8825
8826 grandparent_scope = partial_die_parent_scope (parent, cu);
8827
8828 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8829 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8830 Work around this problem here. */
8831 if (cu->language == language_cplus
8832 && parent->tag == DW_TAG_namespace
8833 && strcmp (parent->name, "::") == 0
8834 && grandparent_scope == NULL)
8835 {
8836 parent->scope = NULL;
8837 parent->scope_set = 1;
8838 return NULL;
8839 }
8840
8841 /* Nested subroutines in Fortran get a prefix. */
8842 if (pdi->tag == DW_TAG_enumerator)
8843 /* Enumerators should not get the name of the enumeration as a prefix. */
8844 parent->scope = grandparent_scope;
8845 else if (parent->tag == DW_TAG_namespace
8846 || parent->tag == DW_TAG_module
8847 || parent->tag == DW_TAG_structure_type
8848 || parent->tag == DW_TAG_class_type
8849 || parent->tag == DW_TAG_interface_type
8850 || parent->tag == DW_TAG_union_type
8851 || parent->tag == DW_TAG_enumeration_type
8852 || (cu->language == language_fortran
8853 && parent->tag == DW_TAG_subprogram
8854 && pdi->tag == DW_TAG_subprogram))
8855 {
8856 if (grandparent_scope == NULL)
8857 parent->scope = parent->name;
8858 else
8859 parent->scope = typename_concat (&cu->comp_unit_obstack,
8860 grandparent_scope,
8861 parent->name, 0, cu);
8862 }
8863 else
8864 {
8865 /* FIXME drow/2004-04-01: What should we be doing with
8866 function-local names? For partial symbols, we should probably be
8867 ignoring them. */
8868 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8869 dwarf_tag_name (parent->tag),
8870 sect_offset_str (pdi->sect_off));
8871 parent->scope = grandparent_scope;
8872 }
8873
8874 parent->scope_set = 1;
8875 return parent->scope;
8876 }
8877
8878 /* Return the fully scoped name associated with PDI, from compilation unit
8879 CU. The result will be allocated with malloc. */
8880
8881 static char *
8882 partial_die_full_name (struct partial_die_info *pdi,
8883 struct dwarf2_cu *cu)
8884 {
8885 const char *parent_scope;
8886
8887 /* If this is a template instantiation, we can not work out the
8888 template arguments from partial DIEs. So, unfortunately, we have
8889 to go through the full DIEs. At least any work we do building
8890 types here will be reused if full symbols are loaded later. */
8891 if (pdi->has_template_arguments)
8892 {
8893 pdi->fixup (cu);
8894
8895 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8896 {
8897 struct die_info *die;
8898 struct attribute attr;
8899 struct dwarf2_cu *ref_cu = cu;
8900
8901 /* DW_FORM_ref_addr is using section offset. */
8902 attr.name = (enum dwarf_attribute) 0;
8903 attr.form = DW_FORM_ref_addr;
8904 attr.u.unsnd = to_underlying (pdi->sect_off);
8905 die = follow_die_ref (NULL, &attr, &ref_cu);
8906
8907 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8908 }
8909 }
8910
8911 parent_scope = partial_die_parent_scope (pdi, cu);
8912 if (parent_scope == NULL)
8913 return NULL;
8914 else
8915 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
8916 }
8917
8918 static void
8919 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8920 {
8921 struct dwarf2_per_objfile *dwarf2_per_objfile
8922 = cu->per_cu->dwarf2_per_objfile;
8923 struct objfile *objfile = dwarf2_per_objfile->objfile;
8924 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8925 CORE_ADDR addr = 0;
8926 const char *actual_name = NULL;
8927 CORE_ADDR baseaddr;
8928 char *built_actual_name;
8929
8930 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8931
8932 built_actual_name = partial_die_full_name (pdi, cu);
8933 if (built_actual_name != NULL)
8934 actual_name = built_actual_name;
8935
8936 if (actual_name == NULL)
8937 actual_name = pdi->name;
8938
8939 switch (pdi->tag)
8940 {
8941 case DW_TAG_inlined_subroutine:
8942 case DW_TAG_subprogram:
8943 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8944 - baseaddr);
8945 if (pdi->is_external
8946 || cu->language == language_ada
8947 || (cu->language == language_fortran
8948 && pdi->die_parent != NULL
8949 && pdi->die_parent->tag == DW_TAG_subprogram))
8950 {
8951 /* Normally, only "external" DIEs are part of the global scope.
8952 But in Ada and Fortran, we want to be able to access nested
8953 procedures globally. So all Ada and Fortran subprograms are
8954 stored in the global scope. */
8955 add_psymbol_to_list (actual_name,
8956 built_actual_name != NULL,
8957 VAR_DOMAIN, LOC_BLOCK,
8958 SECT_OFF_TEXT (objfile),
8959 psymbol_placement::GLOBAL,
8960 addr,
8961 cu->language, objfile);
8962 }
8963 else
8964 {
8965 add_psymbol_to_list (actual_name,
8966 built_actual_name != NULL,
8967 VAR_DOMAIN, LOC_BLOCK,
8968 SECT_OFF_TEXT (objfile),
8969 psymbol_placement::STATIC,
8970 addr, cu->language, objfile);
8971 }
8972
8973 if (pdi->main_subprogram && actual_name != NULL)
8974 set_objfile_main_name (objfile, actual_name, cu->language);
8975 break;
8976 case DW_TAG_constant:
8977 add_psymbol_to_list (actual_name,
8978 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8979 -1, (pdi->is_external
8980 ? psymbol_placement::GLOBAL
8981 : psymbol_placement::STATIC),
8982 0, cu->language, objfile);
8983 break;
8984 case DW_TAG_variable:
8985 if (pdi->d.locdesc)
8986 addr = decode_locdesc (pdi->d.locdesc, cu);
8987
8988 if (pdi->d.locdesc
8989 && addr == 0
8990 && !dwarf2_per_objfile->has_section_at_zero)
8991 {
8992 /* A global or static variable may also have been stripped
8993 out by the linker if unused, in which case its address
8994 will be nullified; do not add such variables into partial
8995 symbol table then. */
8996 }
8997 else if (pdi->is_external)
8998 {
8999 /* Global Variable.
9000 Don't enter into the minimal symbol tables as there is
9001 a minimal symbol table entry from the ELF symbols already.
9002 Enter into partial symbol table if it has a location
9003 descriptor or a type.
9004 If the location descriptor is missing, new_symbol will create
9005 a LOC_UNRESOLVED symbol, the address of the variable will then
9006 be determined from the minimal symbol table whenever the variable
9007 is referenced.
9008 The address for the partial symbol table entry is not
9009 used by GDB, but it comes in handy for debugging partial symbol
9010 table building. */
9011
9012 if (pdi->d.locdesc || pdi->has_type)
9013 add_psymbol_to_list (actual_name,
9014 built_actual_name != NULL,
9015 VAR_DOMAIN, LOC_STATIC,
9016 SECT_OFF_TEXT (objfile),
9017 psymbol_placement::GLOBAL,
9018 addr, cu->language, objfile);
9019 }
9020 else
9021 {
9022 int has_loc = pdi->d.locdesc != NULL;
9023
9024 /* Static Variable. Skip symbols whose value we cannot know (those
9025 without location descriptors or constant values). */
9026 if (!has_loc && !pdi->has_const_value)
9027 {
9028 xfree (built_actual_name);
9029 return;
9030 }
9031
9032 add_psymbol_to_list (actual_name,
9033 built_actual_name != NULL,
9034 VAR_DOMAIN, LOC_STATIC,
9035 SECT_OFF_TEXT (objfile),
9036 psymbol_placement::STATIC,
9037 has_loc ? addr : 0,
9038 cu->language, objfile);
9039 }
9040 break;
9041 case DW_TAG_typedef:
9042 case DW_TAG_base_type:
9043 case DW_TAG_subrange_type:
9044 add_psymbol_to_list (actual_name,
9045 built_actual_name != NULL,
9046 VAR_DOMAIN, LOC_TYPEDEF, -1,
9047 psymbol_placement::STATIC,
9048 0, cu->language, objfile);
9049 break;
9050 case DW_TAG_imported_declaration:
9051 case DW_TAG_namespace:
9052 add_psymbol_to_list (actual_name,
9053 built_actual_name != NULL,
9054 VAR_DOMAIN, LOC_TYPEDEF, -1,
9055 psymbol_placement::GLOBAL,
9056 0, cu->language, objfile);
9057 break;
9058 case DW_TAG_module:
9059 /* With Fortran 77 there might be a "BLOCK DATA" module
9060 available without any name. If so, we skip the module as it
9061 doesn't bring any value. */
9062 if (actual_name != nullptr)
9063 add_psymbol_to_list (actual_name,
9064 built_actual_name != NULL,
9065 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9066 psymbol_placement::GLOBAL,
9067 0, cu->language, objfile);
9068 break;
9069 case DW_TAG_class_type:
9070 case DW_TAG_interface_type:
9071 case DW_TAG_structure_type:
9072 case DW_TAG_union_type:
9073 case DW_TAG_enumeration_type:
9074 /* Skip external references. The DWARF standard says in the section
9075 about "Structure, Union, and Class Type Entries": "An incomplete
9076 structure, union or class type is represented by a structure,
9077 union or class entry that does not have a byte size attribute
9078 and that has a DW_AT_declaration attribute." */
9079 if (!pdi->has_byte_size && pdi->is_declaration)
9080 {
9081 xfree (built_actual_name);
9082 return;
9083 }
9084
9085 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9086 static vs. global. */
9087 add_psymbol_to_list (actual_name,
9088 built_actual_name != NULL,
9089 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9090 cu->language == language_cplus
9091 ? psymbol_placement::GLOBAL
9092 : psymbol_placement::STATIC,
9093 0, cu->language, objfile);
9094
9095 break;
9096 case DW_TAG_enumerator:
9097 add_psymbol_to_list (actual_name,
9098 built_actual_name != NULL,
9099 VAR_DOMAIN, LOC_CONST, -1,
9100 cu->language == language_cplus
9101 ? psymbol_placement::GLOBAL
9102 : psymbol_placement::STATIC,
9103 0, cu->language, objfile);
9104 break;
9105 default:
9106 break;
9107 }
9108
9109 xfree (built_actual_name);
9110 }
9111
9112 /* Read a partial die corresponding to a namespace; also, add a symbol
9113 corresponding to that namespace to the symbol table. NAMESPACE is
9114 the name of the enclosing namespace. */
9115
9116 static void
9117 add_partial_namespace (struct partial_die_info *pdi,
9118 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9119 int set_addrmap, struct dwarf2_cu *cu)
9120 {
9121 /* Add a symbol for the namespace. */
9122
9123 add_partial_symbol (pdi, cu);
9124
9125 /* Now scan partial symbols in that namespace. */
9126
9127 if (pdi->has_children)
9128 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9129 }
9130
9131 /* Read a partial die corresponding to a Fortran module. */
9132
9133 static void
9134 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9135 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9136 {
9137 /* Add a symbol for the namespace. */
9138
9139 add_partial_symbol (pdi, cu);
9140
9141 /* Now scan partial symbols in that module. */
9142
9143 if (pdi->has_children)
9144 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9145 }
9146
9147 /* Read a partial die corresponding to a subprogram or an inlined
9148 subprogram and create a partial symbol for that subprogram.
9149 When the CU language allows it, this routine also defines a partial
9150 symbol for each nested subprogram that this subprogram contains.
9151 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9152 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9153
9154 PDI may also be a lexical block, in which case we simply search
9155 recursively for subprograms defined inside that lexical block.
9156 Again, this is only performed when the CU language allows this
9157 type of definitions. */
9158
9159 static void
9160 add_partial_subprogram (struct partial_die_info *pdi,
9161 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9162 int set_addrmap, struct dwarf2_cu *cu)
9163 {
9164 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9165 {
9166 if (pdi->has_pc_info)
9167 {
9168 if (pdi->lowpc < *lowpc)
9169 *lowpc = pdi->lowpc;
9170 if (pdi->highpc > *highpc)
9171 *highpc = pdi->highpc;
9172 if (set_addrmap)
9173 {
9174 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9175 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9176 CORE_ADDR baseaddr;
9177 CORE_ADDR this_highpc;
9178 CORE_ADDR this_lowpc;
9179
9180 baseaddr = ANOFFSET (objfile->section_offsets,
9181 SECT_OFF_TEXT (objfile));
9182 this_lowpc
9183 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9184 pdi->lowpc + baseaddr)
9185 - baseaddr);
9186 this_highpc
9187 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9188 pdi->highpc + baseaddr)
9189 - baseaddr);
9190 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
9191 this_lowpc, this_highpc - 1,
9192 cu->per_cu->v.psymtab);
9193 }
9194 }
9195
9196 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9197 {
9198 if (!pdi->is_declaration)
9199 /* Ignore subprogram DIEs that do not have a name, they are
9200 illegal. Do not emit a complaint at this point, we will
9201 do so when we convert this psymtab into a symtab. */
9202 if (pdi->name)
9203 add_partial_symbol (pdi, cu);
9204 }
9205 }
9206
9207 if (! pdi->has_children)
9208 return;
9209
9210 if (cu->language == language_ada || cu->language == language_fortran)
9211 {
9212 pdi = pdi->die_child;
9213 while (pdi != NULL)
9214 {
9215 pdi->fixup (cu);
9216 if (pdi->tag == DW_TAG_subprogram
9217 || pdi->tag == DW_TAG_inlined_subroutine
9218 || pdi->tag == DW_TAG_lexical_block)
9219 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9220 pdi = pdi->die_sibling;
9221 }
9222 }
9223 }
9224
9225 /* Read a partial die corresponding to an enumeration type. */
9226
9227 static void
9228 add_partial_enumeration (struct partial_die_info *enum_pdi,
9229 struct dwarf2_cu *cu)
9230 {
9231 struct partial_die_info *pdi;
9232
9233 if (enum_pdi->name != NULL)
9234 add_partial_symbol (enum_pdi, cu);
9235
9236 pdi = enum_pdi->die_child;
9237 while (pdi)
9238 {
9239 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9240 complaint (_("malformed enumerator DIE ignored"));
9241 else
9242 add_partial_symbol (pdi, cu);
9243 pdi = pdi->die_sibling;
9244 }
9245 }
9246
9247 /* Return the initial uleb128 in the die at INFO_PTR. */
9248
9249 static unsigned int
9250 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9251 {
9252 unsigned int bytes_read;
9253
9254 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9255 }
9256
9257 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9258 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9259
9260 Return the corresponding abbrev, or NULL if the number is zero (indicating
9261 an empty DIE). In either case *BYTES_READ will be set to the length of
9262 the initial number. */
9263
9264 static struct abbrev_info *
9265 peek_die_abbrev (const die_reader_specs &reader,
9266 const gdb_byte *info_ptr, unsigned int *bytes_read)
9267 {
9268 dwarf2_cu *cu = reader.cu;
9269 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9270 unsigned int abbrev_number
9271 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9272
9273 if (abbrev_number == 0)
9274 return NULL;
9275
9276 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9277 if (!abbrev)
9278 {
9279 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9280 " at offset %s [in module %s]"),
9281 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9282 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9283 }
9284
9285 return abbrev;
9286 }
9287
9288 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9289 Returns a pointer to the end of a series of DIEs, terminated by an empty
9290 DIE. Any children of the skipped DIEs will also be skipped. */
9291
9292 static const gdb_byte *
9293 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9294 {
9295 while (1)
9296 {
9297 unsigned int bytes_read;
9298 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9299
9300 if (abbrev == NULL)
9301 return info_ptr + bytes_read;
9302 else
9303 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9304 }
9305 }
9306
9307 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9308 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9309 abbrev corresponding to that skipped uleb128 should be passed in
9310 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9311 children. */
9312
9313 static const gdb_byte *
9314 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9315 struct abbrev_info *abbrev)
9316 {
9317 unsigned int bytes_read;
9318 struct attribute attr;
9319 bfd *abfd = reader->abfd;
9320 struct dwarf2_cu *cu = reader->cu;
9321 const gdb_byte *buffer = reader->buffer;
9322 const gdb_byte *buffer_end = reader->buffer_end;
9323 unsigned int form, i;
9324
9325 for (i = 0; i < abbrev->num_attrs; i++)
9326 {
9327 /* The only abbrev we care about is DW_AT_sibling. */
9328 if (abbrev->attrs[i].name == DW_AT_sibling)
9329 {
9330 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9331 if (attr.form == DW_FORM_ref_addr)
9332 complaint (_("ignoring absolute DW_AT_sibling"));
9333 else
9334 {
9335 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9336 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9337
9338 if (sibling_ptr < info_ptr)
9339 complaint (_("DW_AT_sibling points backwards"));
9340 else if (sibling_ptr > reader->buffer_end)
9341 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9342 else
9343 return sibling_ptr;
9344 }
9345 }
9346
9347 /* If it isn't DW_AT_sibling, skip this attribute. */
9348 form = abbrev->attrs[i].form;
9349 skip_attribute:
9350 switch (form)
9351 {
9352 case DW_FORM_ref_addr:
9353 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9354 and later it is offset sized. */
9355 if (cu->header.version == 2)
9356 info_ptr += cu->header.addr_size;
9357 else
9358 info_ptr += cu->header.offset_size;
9359 break;
9360 case DW_FORM_GNU_ref_alt:
9361 info_ptr += cu->header.offset_size;
9362 break;
9363 case DW_FORM_addr:
9364 info_ptr += cu->header.addr_size;
9365 break;
9366 case DW_FORM_data1:
9367 case DW_FORM_ref1:
9368 case DW_FORM_flag:
9369 case DW_FORM_strx1:
9370 info_ptr += 1;
9371 break;
9372 case DW_FORM_flag_present:
9373 case DW_FORM_implicit_const:
9374 break;
9375 case DW_FORM_data2:
9376 case DW_FORM_ref2:
9377 case DW_FORM_strx2:
9378 info_ptr += 2;
9379 break;
9380 case DW_FORM_strx3:
9381 info_ptr += 3;
9382 break;
9383 case DW_FORM_data4:
9384 case DW_FORM_ref4:
9385 case DW_FORM_strx4:
9386 info_ptr += 4;
9387 break;
9388 case DW_FORM_data8:
9389 case DW_FORM_ref8:
9390 case DW_FORM_ref_sig8:
9391 info_ptr += 8;
9392 break;
9393 case DW_FORM_data16:
9394 info_ptr += 16;
9395 break;
9396 case DW_FORM_string:
9397 read_direct_string (abfd, info_ptr, &bytes_read);
9398 info_ptr += bytes_read;
9399 break;
9400 case DW_FORM_sec_offset:
9401 case DW_FORM_strp:
9402 case DW_FORM_GNU_strp_alt:
9403 info_ptr += cu->header.offset_size;
9404 break;
9405 case DW_FORM_exprloc:
9406 case DW_FORM_block:
9407 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9408 info_ptr += bytes_read;
9409 break;
9410 case DW_FORM_block1:
9411 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9412 break;
9413 case DW_FORM_block2:
9414 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9415 break;
9416 case DW_FORM_block4:
9417 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9418 break;
9419 case DW_FORM_addrx:
9420 case DW_FORM_strx:
9421 case DW_FORM_sdata:
9422 case DW_FORM_udata:
9423 case DW_FORM_ref_udata:
9424 case DW_FORM_GNU_addr_index:
9425 case DW_FORM_GNU_str_index:
9426 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9427 break;
9428 case DW_FORM_indirect:
9429 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9430 info_ptr += bytes_read;
9431 /* We need to continue parsing from here, so just go back to
9432 the top. */
9433 goto skip_attribute;
9434
9435 default:
9436 error (_("Dwarf Error: Cannot handle %s "
9437 "in DWARF reader [in module %s]"),
9438 dwarf_form_name (form),
9439 bfd_get_filename (abfd));
9440 }
9441 }
9442
9443 if (abbrev->has_children)
9444 return skip_children (reader, info_ptr);
9445 else
9446 return info_ptr;
9447 }
9448
9449 /* Locate ORIG_PDI's sibling.
9450 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9451
9452 static const gdb_byte *
9453 locate_pdi_sibling (const struct die_reader_specs *reader,
9454 struct partial_die_info *orig_pdi,
9455 const gdb_byte *info_ptr)
9456 {
9457 /* Do we know the sibling already? */
9458
9459 if (orig_pdi->sibling)
9460 return orig_pdi->sibling;
9461
9462 /* Are there any children to deal with? */
9463
9464 if (!orig_pdi->has_children)
9465 return info_ptr;
9466
9467 /* Skip the children the long way. */
9468
9469 return skip_children (reader, info_ptr);
9470 }
9471
9472 /* Expand this partial symbol table into a full symbol table. SELF is
9473 not NULL. */
9474
9475 static void
9476 dwarf2_read_symtab (struct partial_symtab *self,
9477 struct objfile *objfile)
9478 {
9479 struct dwarf2_per_objfile *dwarf2_per_objfile
9480 = get_dwarf2_per_objfile (objfile);
9481
9482 if (self->readin)
9483 {
9484 warning (_("bug: psymtab for %s is already read in."),
9485 self->filename);
9486 }
9487 else
9488 {
9489 if (info_verbose)
9490 {
9491 printf_filtered (_("Reading in symbols for %s..."),
9492 self->filename);
9493 gdb_flush (gdb_stdout);
9494 }
9495
9496 /* If this psymtab is constructed from a debug-only objfile, the
9497 has_section_at_zero flag will not necessarily be correct. We
9498 can get the correct value for this flag by looking at the data
9499 associated with the (presumably stripped) associated objfile. */
9500 if (objfile->separate_debug_objfile_backlink)
9501 {
9502 struct dwarf2_per_objfile *dpo_backlink
9503 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9504
9505 dwarf2_per_objfile->has_section_at_zero
9506 = dpo_backlink->has_section_at_zero;
9507 }
9508
9509 dwarf2_per_objfile->reading_partial_symbols = 0;
9510
9511 psymtab_to_symtab_1 (self);
9512
9513 /* Finish up the debug error message. */
9514 if (info_verbose)
9515 printf_filtered (_("done.\n"));
9516 }
9517
9518 process_cu_includes (dwarf2_per_objfile);
9519 }
9520 \f
9521 /* Reading in full CUs. */
9522
9523 /* Add PER_CU to the queue. */
9524
9525 static void
9526 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9527 enum language pretend_language)
9528 {
9529 struct dwarf2_queue_item *item;
9530
9531 per_cu->queued = 1;
9532 item = XNEW (struct dwarf2_queue_item);
9533 item->per_cu = per_cu;
9534 item->pretend_language = pretend_language;
9535 item->next = NULL;
9536
9537 if (dwarf2_queue == NULL)
9538 dwarf2_queue = item;
9539 else
9540 dwarf2_queue_tail->next = item;
9541
9542 dwarf2_queue_tail = item;
9543 }
9544
9545 /* If PER_CU is not yet queued, add it to the queue.
9546 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9547 dependency.
9548 The result is non-zero if PER_CU was queued, otherwise the result is zero
9549 meaning either PER_CU is already queued or it is already loaded.
9550
9551 N.B. There is an invariant here that if a CU is queued then it is loaded.
9552 The caller is required to load PER_CU if we return non-zero. */
9553
9554 static int
9555 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9556 struct dwarf2_per_cu_data *per_cu,
9557 enum language pretend_language)
9558 {
9559 /* We may arrive here during partial symbol reading, if we need full
9560 DIEs to process an unusual case (e.g. template arguments). Do
9561 not queue PER_CU, just tell our caller to load its DIEs. */
9562 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9563 {
9564 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9565 return 1;
9566 return 0;
9567 }
9568
9569 /* Mark the dependence relation so that we don't flush PER_CU
9570 too early. */
9571 if (dependent_cu != NULL)
9572 dwarf2_add_dependence (dependent_cu, per_cu);
9573
9574 /* If it's already on the queue, we have nothing to do. */
9575 if (per_cu->queued)
9576 return 0;
9577
9578 /* If the compilation unit is already loaded, just mark it as
9579 used. */
9580 if (per_cu->cu != NULL)
9581 {
9582 per_cu->cu->last_used = 0;
9583 return 0;
9584 }
9585
9586 /* Add it to the queue. */
9587 queue_comp_unit (per_cu, pretend_language);
9588
9589 return 1;
9590 }
9591
9592 /* Process the queue. */
9593
9594 static void
9595 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9596 {
9597 struct dwarf2_queue_item *item, *next_item;
9598
9599 if (dwarf_read_debug)
9600 {
9601 fprintf_unfiltered (gdb_stdlog,
9602 "Expanding one or more symtabs of objfile %s ...\n",
9603 objfile_name (dwarf2_per_objfile->objfile));
9604 }
9605
9606 /* The queue starts out with one item, but following a DIE reference
9607 may load a new CU, adding it to the end of the queue. */
9608 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9609 {
9610 if ((dwarf2_per_objfile->using_index
9611 ? !item->per_cu->v.quick->compunit_symtab
9612 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9613 /* Skip dummy CUs. */
9614 && item->per_cu->cu != NULL)
9615 {
9616 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9617 unsigned int debug_print_threshold;
9618 char buf[100];
9619
9620 if (per_cu->is_debug_types)
9621 {
9622 struct signatured_type *sig_type =
9623 (struct signatured_type *) per_cu;
9624
9625 sprintf (buf, "TU %s at offset %s",
9626 hex_string (sig_type->signature),
9627 sect_offset_str (per_cu->sect_off));
9628 /* There can be 100s of TUs.
9629 Only print them in verbose mode. */
9630 debug_print_threshold = 2;
9631 }
9632 else
9633 {
9634 sprintf (buf, "CU at offset %s",
9635 sect_offset_str (per_cu->sect_off));
9636 debug_print_threshold = 1;
9637 }
9638
9639 if (dwarf_read_debug >= debug_print_threshold)
9640 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9641
9642 if (per_cu->is_debug_types)
9643 process_full_type_unit (per_cu, item->pretend_language);
9644 else
9645 process_full_comp_unit (per_cu, item->pretend_language);
9646
9647 if (dwarf_read_debug >= debug_print_threshold)
9648 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9649 }
9650
9651 item->per_cu->queued = 0;
9652 next_item = item->next;
9653 xfree (item);
9654 }
9655
9656 dwarf2_queue_tail = NULL;
9657
9658 if (dwarf_read_debug)
9659 {
9660 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9661 objfile_name (dwarf2_per_objfile->objfile));
9662 }
9663 }
9664
9665 /* Read in full symbols for PST, and anything it depends on. */
9666
9667 static void
9668 psymtab_to_symtab_1 (struct partial_symtab *pst)
9669 {
9670 struct dwarf2_per_cu_data *per_cu;
9671 int i;
9672
9673 if (pst->readin)
9674 return;
9675
9676 for (i = 0; i < pst->number_of_dependencies; i++)
9677 if (!pst->dependencies[i]->readin
9678 && pst->dependencies[i]->user == NULL)
9679 {
9680 /* Inform about additional files that need to be read in. */
9681 if (info_verbose)
9682 {
9683 /* FIXME: i18n: Need to make this a single string. */
9684 fputs_filtered (" ", gdb_stdout);
9685 wrap_here ("");
9686 fputs_filtered ("and ", gdb_stdout);
9687 wrap_here ("");
9688 printf_filtered ("%s...", pst->dependencies[i]->filename);
9689 wrap_here (""); /* Flush output. */
9690 gdb_flush (gdb_stdout);
9691 }
9692 psymtab_to_symtab_1 (pst->dependencies[i]);
9693 }
9694
9695 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9696
9697 if (per_cu == NULL)
9698 {
9699 /* It's an include file, no symbols to read for it.
9700 Everything is in the parent symtab. */
9701 pst->readin = 1;
9702 return;
9703 }
9704
9705 dw2_do_instantiate_symtab (per_cu, false);
9706 }
9707
9708 /* Trivial hash function for die_info: the hash value of a DIE
9709 is its offset in .debug_info for this objfile. */
9710
9711 static hashval_t
9712 die_hash (const void *item)
9713 {
9714 const struct die_info *die = (const struct die_info *) item;
9715
9716 return to_underlying (die->sect_off);
9717 }
9718
9719 /* Trivial comparison function for die_info structures: two DIEs
9720 are equal if they have the same offset. */
9721
9722 static int
9723 die_eq (const void *item_lhs, const void *item_rhs)
9724 {
9725 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9726 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9727
9728 return die_lhs->sect_off == die_rhs->sect_off;
9729 }
9730
9731 /* die_reader_func for load_full_comp_unit.
9732 This is identical to read_signatured_type_reader,
9733 but is kept separate for now. */
9734
9735 static void
9736 load_full_comp_unit_reader (const struct die_reader_specs *reader,
9737 const gdb_byte *info_ptr,
9738 struct die_info *comp_unit_die,
9739 int has_children,
9740 void *data)
9741 {
9742 struct dwarf2_cu *cu = reader->cu;
9743 enum language *language_ptr = (enum language *) data;
9744
9745 gdb_assert (cu->die_hash == NULL);
9746 cu->die_hash =
9747 htab_create_alloc_ex (cu->header.length / 12,
9748 die_hash,
9749 die_eq,
9750 NULL,
9751 &cu->comp_unit_obstack,
9752 hashtab_obstack_allocate,
9753 dummy_obstack_deallocate);
9754
9755 if (has_children)
9756 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9757 &info_ptr, comp_unit_die);
9758 cu->dies = comp_unit_die;
9759 /* comp_unit_die is not stored in die_hash, no need. */
9760
9761 /* We try not to read any attributes in this function, because not
9762 all CUs needed for references have been loaded yet, and symbol
9763 table processing isn't initialized. But we have to set the CU language,
9764 or we won't be able to build types correctly.
9765 Similarly, if we do not read the producer, we can not apply
9766 producer-specific interpretation. */
9767 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
9768 }
9769
9770 /* Load the DIEs associated with PER_CU into memory. */
9771
9772 static void
9773 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9774 bool skip_partial,
9775 enum language pretend_language)
9776 {
9777 gdb_assert (! this_cu->is_debug_types);
9778
9779 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
9780 load_full_comp_unit_reader, &pretend_language);
9781 }
9782
9783 /* Add a DIE to the delayed physname list. */
9784
9785 static void
9786 add_to_method_list (struct type *type, int fnfield_index, int index,
9787 const char *name, struct die_info *die,
9788 struct dwarf2_cu *cu)
9789 {
9790 struct delayed_method_info mi;
9791 mi.type = type;
9792 mi.fnfield_index = fnfield_index;
9793 mi.index = index;
9794 mi.name = name;
9795 mi.die = die;
9796 cu->method_list.push_back (mi);
9797 }
9798
9799 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9800 "const" / "volatile". If so, decrements LEN by the length of the
9801 modifier and return true. Otherwise return false. */
9802
9803 template<size_t N>
9804 static bool
9805 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9806 {
9807 size_t mod_len = sizeof (mod) - 1;
9808 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9809 {
9810 len -= mod_len;
9811 return true;
9812 }
9813 return false;
9814 }
9815
9816 /* Compute the physnames of any methods on the CU's method list.
9817
9818 The computation of method physnames is delayed in order to avoid the
9819 (bad) condition that one of the method's formal parameters is of an as yet
9820 incomplete type. */
9821
9822 static void
9823 compute_delayed_physnames (struct dwarf2_cu *cu)
9824 {
9825 /* Only C++ delays computing physnames. */
9826 if (cu->method_list.empty ())
9827 return;
9828 gdb_assert (cu->language == language_cplus);
9829
9830 for (const delayed_method_info &mi : cu->method_list)
9831 {
9832 const char *physname;
9833 struct fn_fieldlist *fn_flp
9834 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9835 physname = dwarf2_physname (mi.name, mi.die, cu);
9836 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9837 = physname ? physname : "";
9838
9839 /* Since there's no tag to indicate whether a method is a
9840 const/volatile overload, extract that information out of the
9841 demangled name. */
9842 if (physname != NULL)
9843 {
9844 size_t len = strlen (physname);
9845
9846 while (1)
9847 {
9848 if (physname[len] == ')') /* shortcut */
9849 break;
9850 else if (check_modifier (physname, len, " const"))
9851 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9852 else if (check_modifier (physname, len, " volatile"))
9853 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9854 else
9855 break;
9856 }
9857 }
9858 }
9859
9860 /* The list is no longer needed. */
9861 cu->method_list.clear ();
9862 }
9863
9864 /* Go objects should be embedded in a DW_TAG_module DIE,
9865 and it's not clear if/how imported objects will appear.
9866 To keep Go support simple until that's worked out,
9867 go back through what we've read and create something usable.
9868 We could do this while processing each DIE, and feels kinda cleaner,
9869 but that way is more invasive.
9870 This is to, for example, allow the user to type "p var" or "b main"
9871 without having to specify the package name, and allow lookups
9872 of module.object to work in contexts that use the expression
9873 parser. */
9874
9875 static void
9876 fixup_go_packaging (struct dwarf2_cu *cu)
9877 {
9878 char *package_name = NULL;
9879 struct pending *list;
9880 int i;
9881
9882 for (list = *cu->get_builder ()->get_global_symbols ();
9883 list != NULL;
9884 list = list->next)
9885 {
9886 for (i = 0; i < list->nsyms; ++i)
9887 {
9888 struct symbol *sym = list->symbol[i];
9889
9890 if (SYMBOL_LANGUAGE (sym) == language_go
9891 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9892 {
9893 char *this_package_name = go_symbol_package_name (sym);
9894
9895 if (this_package_name == NULL)
9896 continue;
9897 if (package_name == NULL)
9898 package_name = this_package_name;
9899 else
9900 {
9901 struct objfile *objfile
9902 = cu->per_cu->dwarf2_per_objfile->objfile;
9903 if (strcmp (package_name, this_package_name) != 0)
9904 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9905 (symbol_symtab (sym) != NULL
9906 ? symtab_to_filename_for_display
9907 (symbol_symtab (sym))
9908 : objfile_name (objfile)),
9909 this_package_name, package_name);
9910 xfree (this_package_name);
9911 }
9912 }
9913 }
9914 }
9915
9916 if (package_name != NULL)
9917 {
9918 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9919 const char *saved_package_name
9920 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name);
9921 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9922 saved_package_name);
9923 struct symbol *sym;
9924
9925 sym = allocate_symbol (objfile);
9926 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
9927 SYMBOL_SET_NAMES (sym, saved_package_name, false, objfile);
9928 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9929 e.g., "main" finds the "main" module and not C's main(). */
9930 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9931 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9932 SYMBOL_TYPE (sym) = type;
9933
9934 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9935
9936 xfree (package_name);
9937 }
9938 }
9939
9940 /* Allocate a fully-qualified name consisting of the two parts on the
9941 obstack. */
9942
9943 static const char *
9944 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9945 {
9946 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9947 }
9948
9949 /* A helper that allocates a struct discriminant_info to attach to a
9950 union type. */
9951
9952 static struct discriminant_info *
9953 alloc_discriminant_info (struct type *type, int discriminant_index,
9954 int default_index)
9955 {
9956 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9957 gdb_assert (discriminant_index == -1
9958 || (discriminant_index >= 0
9959 && discriminant_index < TYPE_NFIELDS (type)));
9960 gdb_assert (default_index == -1
9961 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9962
9963 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9964
9965 struct discriminant_info *disc
9966 = ((struct discriminant_info *)
9967 TYPE_ZALLOC (type,
9968 offsetof (struct discriminant_info, discriminants)
9969 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9970 disc->default_index = default_index;
9971 disc->discriminant_index = discriminant_index;
9972
9973 struct dynamic_prop prop;
9974 prop.kind = PROP_UNDEFINED;
9975 prop.data.baton = disc;
9976
9977 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9978
9979 return disc;
9980 }
9981
9982 /* Some versions of rustc emitted enums in an unusual way.
9983
9984 Ordinary enums were emitted as unions. The first element of each
9985 structure in the union was named "RUST$ENUM$DISR". This element
9986 held the discriminant.
9987
9988 These versions of Rust also implemented the "non-zero"
9989 optimization. When the enum had two values, and one is empty and
9990 the other holds a pointer that cannot be zero, the pointer is used
9991 as the discriminant, with a zero value meaning the empty variant.
9992 Here, the union's first member is of the form
9993 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9994 where the fieldnos are the indices of the fields that should be
9995 traversed in order to find the field (which may be several fields deep)
9996 and the variantname is the name of the variant of the case when the
9997 field is zero.
9998
9999 This function recognizes whether TYPE is of one of these forms,
10000 and, if so, smashes it to be a variant type. */
10001
10002 static void
10003 quirk_rust_enum (struct type *type, struct objfile *objfile)
10004 {
10005 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10006
10007 /* We don't need to deal with empty enums. */
10008 if (TYPE_NFIELDS (type) == 0)
10009 return;
10010
10011 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
10012 if (TYPE_NFIELDS (type) == 1
10013 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
10014 {
10015 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10016
10017 /* Decode the field name to find the offset of the
10018 discriminant. */
10019 ULONGEST bit_offset = 0;
10020 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10021 while (name[0] >= '0' && name[0] <= '9')
10022 {
10023 char *tail;
10024 unsigned long index = strtoul (name, &tail, 10);
10025 name = tail;
10026 if (*name != '$'
10027 || index >= TYPE_NFIELDS (field_type)
10028 || (TYPE_FIELD_LOC_KIND (field_type, index)
10029 != FIELD_LOC_KIND_BITPOS))
10030 {
10031 complaint (_("Could not parse Rust enum encoding string \"%s\""
10032 "[in module %s]"),
10033 TYPE_FIELD_NAME (type, 0),
10034 objfile_name (objfile));
10035 return;
10036 }
10037 ++name;
10038
10039 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10040 field_type = TYPE_FIELD_TYPE (field_type, index);
10041 }
10042
10043 /* Make a union to hold the variants. */
10044 struct type *union_type = alloc_type (objfile);
10045 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10046 TYPE_NFIELDS (union_type) = 3;
10047 TYPE_FIELDS (union_type)
10048 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10049 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10050 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10051
10052 /* Put the discriminant must at index 0. */
10053 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10054 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10055 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10056 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10057
10058 /* The order of fields doesn't really matter, so put the real
10059 field at index 1 and the data-less field at index 2. */
10060 struct discriminant_info *disc
10061 = alloc_discriminant_info (union_type, 0, 1);
10062 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10063 TYPE_FIELD_NAME (union_type, 1)
10064 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10065 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10066 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10067 TYPE_FIELD_NAME (union_type, 1));
10068
10069 const char *dataless_name
10070 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10071 name);
10072 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10073 dataless_name);
10074 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10075 /* NAME points into the original discriminant name, which
10076 already has the correct lifetime. */
10077 TYPE_FIELD_NAME (union_type, 2) = name;
10078 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10079 disc->discriminants[2] = 0;
10080
10081 /* Smash this type to be a structure type. We have to do this
10082 because the type has already been recorded. */
10083 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10084 TYPE_NFIELDS (type) = 1;
10085 TYPE_FIELDS (type)
10086 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10087
10088 /* Install the variant part. */
10089 TYPE_FIELD_TYPE (type, 0) = union_type;
10090 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10091 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10092 }
10093 /* A union with a single anonymous field is probably an old-style
10094 univariant enum. */
10095 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
10096 {
10097 /* Smash this type to be a structure type. We have to do this
10098 because the type has already been recorded. */
10099 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10100
10101 /* Make a union to hold the variants. */
10102 struct type *union_type = alloc_type (objfile);
10103 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10104 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10105 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10106 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10107 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10108
10109 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10110 const char *variant_name
10111 = rust_last_path_segment (TYPE_NAME (field_type));
10112 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10113 TYPE_NAME (field_type)
10114 = rust_fully_qualify (&objfile->objfile_obstack,
10115 TYPE_NAME (type), variant_name);
10116
10117 /* Install the union in the outer struct type. */
10118 TYPE_NFIELDS (type) = 1;
10119 TYPE_FIELDS (type)
10120 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10121 TYPE_FIELD_TYPE (type, 0) = union_type;
10122 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10123 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10124
10125 alloc_discriminant_info (union_type, -1, 0);
10126 }
10127 else
10128 {
10129 struct type *disr_type = nullptr;
10130 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10131 {
10132 disr_type = TYPE_FIELD_TYPE (type, i);
10133
10134 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10135 {
10136 /* All fields of a true enum will be structs. */
10137 return;
10138 }
10139 else if (TYPE_NFIELDS (disr_type) == 0)
10140 {
10141 /* Could be data-less variant, so keep going. */
10142 disr_type = nullptr;
10143 }
10144 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10145 "RUST$ENUM$DISR") != 0)
10146 {
10147 /* Not a Rust enum. */
10148 return;
10149 }
10150 else
10151 {
10152 /* Found one. */
10153 break;
10154 }
10155 }
10156
10157 /* If we got here without a discriminant, then it's probably
10158 just a union. */
10159 if (disr_type == nullptr)
10160 return;
10161
10162 /* Smash this type to be a structure type. We have to do this
10163 because the type has already been recorded. */
10164 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10165
10166 /* Make a union to hold the variants. */
10167 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10168 struct type *union_type = alloc_type (objfile);
10169 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10170 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10171 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10172 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10173 TYPE_FIELDS (union_type)
10174 = (struct field *) TYPE_ZALLOC (union_type,
10175 (TYPE_NFIELDS (union_type)
10176 * sizeof (struct field)));
10177
10178 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10179 TYPE_NFIELDS (type) * sizeof (struct field));
10180
10181 /* Install the discriminant at index 0 in the union. */
10182 TYPE_FIELD (union_type, 0) = *disr_field;
10183 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10184 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10185
10186 /* Install the union in the outer struct type. */
10187 TYPE_FIELD_TYPE (type, 0) = union_type;
10188 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10189 TYPE_NFIELDS (type) = 1;
10190
10191 /* Set the size and offset of the union type. */
10192 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10193
10194 /* We need a way to find the correct discriminant given a
10195 variant name. For convenience we build a map here. */
10196 struct type *enum_type = FIELD_TYPE (*disr_field);
10197 std::unordered_map<std::string, ULONGEST> discriminant_map;
10198 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10199 {
10200 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10201 {
10202 const char *name
10203 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10204 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10205 }
10206 }
10207
10208 int n_fields = TYPE_NFIELDS (union_type);
10209 struct discriminant_info *disc
10210 = alloc_discriminant_info (union_type, 0, -1);
10211 /* Skip the discriminant here. */
10212 for (int i = 1; i < n_fields; ++i)
10213 {
10214 /* Find the final word in the name of this variant's type.
10215 That name can be used to look up the correct
10216 discriminant. */
10217 const char *variant_name
10218 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10219 i)));
10220
10221 auto iter = discriminant_map.find (variant_name);
10222 if (iter != discriminant_map.end ())
10223 disc->discriminants[i] = iter->second;
10224
10225 /* Remove the discriminant field, if it exists. */
10226 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10227 if (TYPE_NFIELDS (sub_type) > 0)
10228 {
10229 --TYPE_NFIELDS (sub_type);
10230 ++TYPE_FIELDS (sub_type);
10231 }
10232 TYPE_FIELD_NAME (union_type, i) = variant_name;
10233 TYPE_NAME (sub_type)
10234 = rust_fully_qualify (&objfile->objfile_obstack,
10235 TYPE_NAME (type), variant_name);
10236 }
10237 }
10238 }
10239
10240 /* Rewrite some Rust unions to be structures with variants parts. */
10241
10242 static void
10243 rust_union_quirks (struct dwarf2_cu *cu)
10244 {
10245 gdb_assert (cu->language == language_rust);
10246 for (type *type_ : cu->rust_unions)
10247 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
10248 /* We don't need this any more. */
10249 cu->rust_unions.clear ();
10250 }
10251
10252 /* Return the symtab for PER_CU. This works properly regardless of
10253 whether we're using the index or psymtabs. */
10254
10255 static struct compunit_symtab *
10256 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10257 {
10258 return (per_cu->dwarf2_per_objfile->using_index
10259 ? per_cu->v.quick->compunit_symtab
10260 : per_cu->v.psymtab->compunit_symtab);
10261 }
10262
10263 /* A helper function for computing the list of all symbol tables
10264 included by PER_CU. */
10265
10266 static void
10267 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
10268 htab_t all_children, htab_t all_type_symtabs,
10269 struct dwarf2_per_cu_data *per_cu,
10270 struct compunit_symtab *immediate_parent)
10271 {
10272 void **slot;
10273 struct compunit_symtab *cust;
10274
10275 slot = htab_find_slot (all_children, per_cu, INSERT);
10276 if (*slot != NULL)
10277 {
10278 /* This inclusion and its children have been processed. */
10279 return;
10280 }
10281
10282 *slot = per_cu;
10283 /* Only add a CU if it has a symbol table. */
10284 cust = get_compunit_symtab (per_cu);
10285 if (cust != NULL)
10286 {
10287 /* If this is a type unit only add its symbol table if we haven't
10288 seen it yet (type unit per_cu's can share symtabs). */
10289 if (per_cu->is_debug_types)
10290 {
10291 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10292 if (*slot == NULL)
10293 {
10294 *slot = cust;
10295 result->push_back (cust);
10296 if (cust->user == NULL)
10297 cust->user = immediate_parent;
10298 }
10299 }
10300 else
10301 {
10302 result->push_back (cust);
10303 if (cust->user == NULL)
10304 cust->user = immediate_parent;
10305 }
10306 }
10307
10308 if (!per_cu->imported_symtabs_empty ())
10309 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10310 {
10311 recursively_compute_inclusions (result, all_children,
10312 all_type_symtabs, ptr, cust);
10313 }
10314 }
10315
10316 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10317 PER_CU. */
10318
10319 static void
10320 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10321 {
10322 gdb_assert (! per_cu->is_debug_types);
10323
10324 if (!per_cu->imported_symtabs_empty ())
10325 {
10326 int len;
10327 std::vector<compunit_symtab *> result_symtabs;
10328 htab_t all_children, all_type_symtabs;
10329 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10330
10331 /* If we don't have a symtab, we can just skip this case. */
10332 if (cust == NULL)
10333 return;
10334
10335 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10336 NULL, xcalloc, xfree);
10337 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10338 NULL, xcalloc, xfree);
10339
10340 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10341 {
10342 recursively_compute_inclusions (&result_symtabs, all_children,
10343 all_type_symtabs, ptr, cust);
10344 }
10345
10346 /* Now we have a transitive closure of all the included symtabs. */
10347 len = result_symtabs.size ();
10348 cust->includes
10349 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10350 struct compunit_symtab *, len + 1);
10351 memcpy (cust->includes, result_symtabs.data (),
10352 len * sizeof (compunit_symtab *));
10353 cust->includes[len] = NULL;
10354
10355 htab_delete (all_children);
10356 htab_delete (all_type_symtabs);
10357 }
10358 }
10359
10360 /* Compute the 'includes' field for the symtabs of all the CUs we just
10361 read. */
10362
10363 static void
10364 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10365 {
10366 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
10367 {
10368 if (! iter->is_debug_types)
10369 compute_compunit_symtab_includes (iter);
10370 }
10371
10372 dwarf2_per_objfile->just_read_cus.clear ();
10373 }
10374
10375 /* Generate full symbol information for PER_CU, whose DIEs have
10376 already been loaded into memory. */
10377
10378 static void
10379 process_full_comp_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 gdbarch *gdbarch = get_objfile_arch (objfile);
10386 CORE_ADDR lowpc, highpc;
10387 struct compunit_symtab *cust;
10388 CORE_ADDR baseaddr;
10389 struct block *static_block;
10390 CORE_ADDR addr;
10391
10392 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10393
10394 /* Clear the list here in case something was left over. */
10395 cu->method_list.clear ();
10396
10397 cu->language = pretend_language;
10398 cu->language_defn = language_def (cu->language);
10399
10400 /* Do line number decoding in read_file_scope () */
10401 process_die (cu->dies, cu);
10402
10403 /* For now fudge the Go package. */
10404 if (cu->language == language_go)
10405 fixup_go_packaging (cu);
10406
10407 /* Now that we have processed all the DIEs in the CU, all the types
10408 should be complete, and it should now be safe to compute all of the
10409 physnames. */
10410 compute_delayed_physnames (cu);
10411
10412 if (cu->language == language_rust)
10413 rust_union_quirks (cu);
10414
10415 /* Some compilers don't define a DW_AT_high_pc attribute for the
10416 compilation unit. If the DW_AT_high_pc is missing, synthesize
10417 it, by scanning the DIE's below the compilation unit. */
10418 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10419
10420 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10421 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10422
10423 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10424 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10425 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10426 addrmap to help ensure it has an accurate map of pc values belonging to
10427 this comp unit. */
10428 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10429
10430 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10431 SECT_OFF_TEXT (objfile),
10432 0);
10433
10434 if (cust != NULL)
10435 {
10436 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10437
10438 /* Set symtab language to language from DW_AT_language. If the
10439 compilation is from a C file generated by language preprocessors, do
10440 not set the language if it was already deduced by start_subfile. */
10441 if (!(cu->language == language_c
10442 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10443 COMPUNIT_FILETABS (cust)->language = cu->language;
10444
10445 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10446 produce DW_AT_location with location lists but it can be possibly
10447 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10448 there were bugs in prologue debug info, fixed later in GCC-4.5
10449 by "unwind info for epilogues" patch (which is not directly related).
10450
10451 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10452 needed, it would be wrong due to missing DW_AT_producer there.
10453
10454 Still one can confuse GDB by using non-standard GCC compilation
10455 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10456 */
10457 if (cu->has_loclist && gcc_4_minor >= 5)
10458 cust->locations_valid = 1;
10459
10460 if (gcc_4_minor >= 5)
10461 cust->epilogue_unwind_valid = 1;
10462
10463 cust->call_site_htab = cu->call_site_htab;
10464 }
10465
10466 if (dwarf2_per_objfile->using_index)
10467 per_cu->v.quick->compunit_symtab = cust;
10468 else
10469 {
10470 struct partial_symtab *pst = per_cu->v.psymtab;
10471 pst->compunit_symtab = cust;
10472 pst->readin = 1;
10473 }
10474
10475 /* Push it for inclusion processing later. */
10476 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
10477
10478 /* Not needed any more. */
10479 cu->reset_builder ();
10480 }
10481
10482 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10483 already been loaded into memory. */
10484
10485 static void
10486 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10487 enum language pretend_language)
10488 {
10489 struct dwarf2_cu *cu = per_cu->cu;
10490 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10491 struct objfile *objfile = dwarf2_per_objfile->objfile;
10492 struct compunit_symtab *cust;
10493 struct signatured_type *sig_type;
10494
10495 gdb_assert (per_cu->is_debug_types);
10496 sig_type = (struct signatured_type *) per_cu;
10497
10498 /* Clear the list here in case something was left over. */
10499 cu->method_list.clear ();
10500
10501 cu->language = pretend_language;
10502 cu->language_defn = language_def (cu->language);
10503
10504 /* The symbol tables are set up in read_type_unit_scope. */
10505 process_die (cu->dies, cu);
10506
10507 /* For now fudge the Go package. */
10508 if (cu->language == language_go)
10509 fixup_go_packaging (cu);
10510
10511 /* Now that we have processed all the DIEs in the CU, all the types
10512 should be complete, and it should now be safe to compute all of the
10513 physnames. */
10514 compute_delayed_physnames (cu);
10515
10516 if (cu->language == language_rust)
10517 rust_union_quirks (cu);
10518
10519 /* TUs share symbol tables.
10520 If this is the first TU to use this symtab, complete the construction
10521 of it with end_expandable_symtab. Otherwise, complete the addition of
10522 this TU's symbols to the existing symtab. */
10523 if (sig_type->type_unit_group->compunit_symtab == NULL)
10524 {
10525 buildsym_compunit *builder = cu->get_builder ();
10526 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10527 sig_type->type_unit_group->compunit_symtab = cust;
10528
10529 if (cust != NULL)
10530 {
10531 /* Set symtab language to language from DW_AT_language. If the
10532 compilation is from a C file generated by language preprocessors,
10533 do not set the language if it was already deduced by
10534 start_subfile. */
10535 if (!(cu->language == language_c
10536 && COMPUNIT_FILETABS (cust)->language != language_c))
10537 COMPUNIT_FILETABS (cust)->language = cu->language;
10538 }
10539 }
10540 else
10541 {
10542 cu->get_builder ()->augment_type_symtab ();
10543 cust = sig_type->type_unit_group->compunit_symtab;
10544 }
10545
10546 if (dwarf2_per_objfile->using_index)
10547 per_cu->v.quick->compunit_symtab = cust;
10548 else
10549 {
10550 struct partial_symtab *pst = per_cu->v.psymtab;
10551 pst->compunit_symtab = cust;
10552 pst->readin = 1;
10553 }
10554
10555 /* Not needed any more. */
10556 cu->reset_builder ();
10557 }
10558
10559 /* Process an imported unit DIE. */
10560
10561 static void
10562 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10563 {
10564 struct attribute *attr;
10565
10566 /* For now we don't handle imported units in type units. */
10567 if (cu->per_cu->is_debug_types)
10568 {
10569 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10570 " supported in type units [in module %s]"),
10571 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10572 }
10573
10574 attr = dwarf2_attr (die, DW_AT_import, cu);
10575 if (attr != NULL)
10576 {
10577 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10578 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10579 dwarf2_per_cu_data *per_cu
10580 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10581 cu->per_cu->dwarf2_per_objfile);
10582
10583 /* If necessary, add it to the queue and load its DIEs. */
10584 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10585 load_full_comp_unit (per_cu, false, cu->language);
10586
10587 cu->per_cu->imported_symtabs_push (per_cu);
10588 }
10589 }
10590
10591 /* RAII object that represents a process_die scope: i.e.,
10592 starts/finishes processing a DIE. */
10593 class process_die_scope
10594 {
10595 public:
10596 process_die_scope (die_info *die, dwarf2_cu *cu)
10597 : m_die (die), m_cu (cu)
10598 {
10599 /* We should only be processing DIEs not already in process. */
10600 gdb_assert (!m_die->in_process);
10601 m_die->in_process = true;
10602 }
10603
10604 ~process_die_scope ()
10605 {
10606 m_die->in_process = false;
10607
10608 /* If we're done processing the DIE for the CU that owns the line
10609 header, we don't need the line header anymore. */
10610 if (m_cu->line_header_die_owner == m_die)
10611 {
10612 delete m_cu->line_header;
10613 m_cu->line_header = NULL;
10614 m_cu->line_header_die_owner = NULL;
10615 }
10616 }
10617
10618 private:
10619 die_info *m_die;
10620 dwarf2_cu *m_cu;
10621 };
10622
10623 /* Process a die and its children. */
10624
10625 static void
10626 process_die (struct die_info *die, struct dwarf2_cu *cu)
10627 {
10628 process_die_scope scope (die, cu);
10629
10630 switch (die->tag)
10631 {
10632 case DW_TAG_padding:
10633 break;
10634 case DW_TAG_compile_unit:
10635 case DW_TAG_partial_unit:
10636 read_file_scope (die, cu);
10637 break;
10638 case DW_TAG_type_unit:
10639 read_type_unit_scope (die, cu);
10640 break;
10641 case DW_TAG_subprogram:
10642 /* Nested subprograms in Fortran get a prefix. */
10643 if (cu->language == language_fortran
10644 && die->parent != NULL
10645 && die->parent->tag == DW_TAG_subprogram)
10646 cu->processing_has_namespace_info = true;
10647 /* Fall through. */
10648 case DW_TAG_inlined_subroutine:
10649 read_func_scope (die, cu);
10650 break;
10651 case DW_TAG_lexical_block:
10652 case DW_TAG_try_block:
10653 case DW_TAG_catch_block:
10654 read_lexical_block_scope (die, cu);
10655 break;
10656 case DW_TAG_call_site:
10657 case DW_TAG_GNU_call_site:
10658 read_call_site_scope (die, cu);
10659 break;
10660 case DW_TAG_class_type:
10661 case DW_TAG_interface_type:
10662 case DW_TAG_structure_type:
10663 case DW_TAG_union_type:
10664 process_structure_scope (die, cu);
10665 break;
10666 case DW_TAG_enumeration_type:
10667 process_enumeration_scope (die, cu);
10668 break;
10669
10670 /* These dies have a type, but processing them does not create
10671 a symbol or recurse to process the children. Therefore we can
10672 read them on-demand through read_type_die. */
10673 case DW_TAG_subroutine_type:
10674 case DW_TAG_set_type:
10675 case DW_TAG_array_type:
10676 case DW_TAG_pointer_type:
10677 case DW_TAG_ptr_to_member_type:
10678 case DW_TAG_reference_type:
10679 case DW_TAG_rvalue_reference_type:
10680 case DW_TAG_string_type:
10681 break;
10682
10683 case DW_TAG_base_type:
10684 case DW_TAG_subrange_type:
10685 case DW_TAG_typedef:
10686 /* Add a typedef symbol for the type definition, if it has a
10687 DW_AT_name. */
10688 new_symbol (die, read_type_die (die, cu), cu);
10689 break;
10690 case DW_TAG_common_block:
10691 read_common_block (die, cu);
10692 break;
10693 case DW_TAG_common_inclusion:
10694 break;
10695 case DW_TAG_namespace:
10696 cu->processing_has_namespace_info = true;
10697 read_namespace (die, cu);
10698 break;
10699 case DW_TAG_module:
10700 cu->processing_has_namespace_info = true;
10701 read_module (die, cu);
10702 break;
10703 case DW_TAG_imported_declaration:
10704 cu->processing_has_namespace_info = true;
10705 if (read_namespace_alias (die, cu))
10706 break;
10707 /* The declaration is not a global namespace alias. */
10708 /* Fall through. */
10709 case DW_TAG_imported_module:
10710 cu->processing_has_namespace_info = true;
10711 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10712 || cu->language != language_fortran))
10713 complaint (_("Tag '%s' has unexpected children"),
10714 dwarf_tag_name (die->tag));
10715 read_import_statement (die, cu);
10716 break;
10717
10718 case DW_TAG_imported_unit:
10719 process_imported_unit_die (die, cu);
10720 break;
10721
10722 case DW_TAG_variable:
10723 read_variable (die, cu);
10724 break;
10725
10726 default:
10727 new_symbol (die, NULL, cu);
10728 break;
10729 }
10730 }
10731 \f
10732 /* DWARF name computation. */
10733
10734 /* A helper function for dwarf2_compute_name which determines whether DIE
10735 needs to have the name of the scope prepended to the name listed in the
10736 die. */
10737
10738 static int
10739 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10740 {
10741 struct attribute *attr;
10742
10743 switch (die->tag)
10744 {
10745 case DW_TAG_namespace:
10746 case DW_TAG_typedef:
10747 case DW_TAG_class_type:
10748 case DW_TAG_interface_type:
10749 case DW_TAG_structure_type:
10750 case DW_TAG_union_type:
10751 case DW_TAG_enumeration_type:
10752 case DW_TAG_enumerator:
10753 case DW_TAG_subprogram:
10754 case DW_TAG_inlined_subroutine:
10755 case DW_TAG_member:
10756 case DW_TAG_imported_declaration:
10757 return 1;
10758
10759 case DW_TAG_variable:
10760 case DW_TAG_constant:
10761 /* We only need to prefix "globally" visible variables. These include
10762 any variable marked with DW_AT_external or any variable that
10763 lives in a namespace. [Variables in anonymous namespaces
10764 require prefixing, but they are not DW_AT_external.] */
10765
10766 if (dwarf2_attr (die, DW_AT_specification, cu))
10767 {
10768 struct dwarf2_cu *spec_cu = cu;
10769
10770 return die_needs_namespace (die_specification (die, &spec_cu),
10771 spec_cu);
10772 }
10773
10774 attr = dwarf2_attr (die, DW_AT_external, cu);
10775 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10776 && die->parent->tag != DW_TAG_module)
10777 return 0;
10778 /* A variable in a lexical block of some kind does not need a
10779 namespace, even though in C++ such variables may be external
10780 and have a mangled name. */
10781 if (die->parent->tag == DW_TAG_lexical_block
10782 || die->parent->tag == DW_TAG_try_block
10783 || die->parent->tag == DW_TAG_catch_block
10784 || die->parent->tag == DW_TAG_subprogram)
10785 return 0;
10786 return 1;
10787
10788 default:
10789 return 0;
10790 }
10791 }
10792
10793 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10794 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10795 defined for the given DIE. */
10796
10797 static struct attribute *
10798 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10799 {
10800 struct attribute *attr;
10801
10802 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10803 if (attr == NULL)
10804 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10805
10806 return attr;
10807 }
10808
10809 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10810 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10811 defined for the given DIE. */
10812
10813 static const char *
10814 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10815 {
10816 const char *linkage_name;
10817
10818 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10819 if (linkage_name == NULL)
10820 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10821
10822 return linkage_name;
10823 }
10824
10825 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10826 compute the physname for the object, which include a method's:
10827 - formal parameters (C++),
10828 - receiver type (Go),
10829
10830 The term "physname" is a bit confusing.
10831 For C++, for example, it is the demangled name.
10832 For Go, for example, it's the mangled name.
10833
10834 For Ada, return the DIE's linkage name rather than the fully qualified
10835 name. PHYSNAME is ignored..
10836
10837 The result is allocated on the objfile_obstack and canonicalized. */
10838
10839 static const char *
10840 dwarf2_compute_name (const char *name,
10841 struct die_info *die, struct dwarf2_cu *cu,
10842 int physname)
10843 {
10844 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10845
10846 if (name == NULL)
10847 name = dwarf2_name (die, cu);
10848
10849 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10850 but otherwise compute it by typename_concat inside GDB.
10851 FIXME: Actually this is not really true, or at least not always true.
10852 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10853 Fortran names because there is no mangling standard. So new_symbol
10854 will set the demangled name to the result of dwarf2_full_name, and it is
10855 the demangled name that GDB uses if it exists. */
10856 if (cu->language == language_ada
10857 || (cu->language == language_fortran && physname))
10858 {
10859 /* For Ada unit, we prefer the linkage name over the name, as
10860 the former contains the exported name, which the user expects
10861 to be able to reference. Ideally, we want the user to be able
10862 to reference this entity using either natural or linkage name,
10863 but we haven't started looking at this enhancement yet. */
10864 const char *linkage_name = dw2_linkage_name (die, cu);
10865
10866 if (linkage_name != NULL)
10867 return linkage_name;
10868 }
10869
10870 /* These are the only languages we know how to qualify names in. */
10871 if (name != NULL
10872 && (cu->language == language_cplus
10873 || cu->language == language_fortran || cu->language == language_d
10874 || cu->language == language_rust))
10875 {
10876 if (die_needs_namespace (die, cu))
10877 {
10878 const char *prefix;
10879 const char *canonical_name = NULL;
10880
10881 string_file buf;
10882
10883 prefix = determine_prefix (die, cu);
10884 if (*prefix != '\0')
10885 {
10886 char *prefixed_name = typename_concat (NULL, prefix, name,
10887 physname, cu);
10888
10889 buf.puts (prefixed_name);
10890 xfree (prefixed_name);
10891 }
10892 else
10893 buf.puts (name);
10894
10895 /* Template parameters may be specified in the DIE's DW_AT_name, or
10896 as children with DW_TAG_template_type_param or
10897 DW_TAG_value_type_param. If the latter, add them to the name
10898 here. If the name already has template parameters, then
10899 skip this step; some versions of GCC emit both, and
10900 it is more efficient to use the pre-computed name.
10901
10902 Something to keep in mind about this process: it is very
10903 unlikely, or in some cases downright impossible, to produce
10904 something that will match the mangled name of a function.
10905 If the definition of the function has the same debug info,
10906 we should be able to match up with it anyway. But fallbacks
10907 using the minimal symbol, for instance to find a method
10908 implemented in a stripped copy of libstdc++, will not work.
10909 If we do not have debug info for the definition, we will have to
10910 match them up some other way.
10911
10912 When we do name matching there is a related problem with function
10913 templates; two instantiated function templates are allowed to
10914 differ only by their return types, which we do not add here. */
10915
10916 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10917 {
10918 struct attribute *attr;
10919 struct die_info *child;
10920 int first = 1;
10921
10922 die->building_fullname = 1;
10923
10924 for (child = die->child; child != NULL; child = child->sibling)
10925 {
10926 struct type *type;
10927 LONGEST value;
10928 const gdb_byte *bytes;
10929 struct dwarf2_locexpr_baton *baton;
10930 struct value *v;
10931
10932 if (child->tag != DW_TAG_template_type_param
10933 && child->tag != DW_TAG_template_value_param)
10934 continue;
10935
10936 if (first)
10937 {
10938 buf.puts ("<");
10939 first = 0;
10940 }
10941 else
10942 buf.puts (", ");
10943
10944 attr = dwarf2_attr (child, DW_AT_type, cu);
10945 if (attr == NULL)
10946 {
10947 complaint (_("template parameter missing DW_AT_type"));
10948 buf.puts ("UNKNOWN_TYPE");
10949 continue;
10950 }
10951 type = die_type (child, cu);
10952
10953 if (child->tag == DW_TAG_template_type_param)
10954 {
10955 c_print_type (type, "", &buf, -1, 0, cu->language,
10956 &type_print_raw_options);
10957 continue;
10958 }
10959
10960 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10961 if (attr == NULL)
10962 {
10963 complaint (_("template parameter missing "
10964 "DW_AT_const_value"));
10965 buf.puts ("UNKNOWN_VALUE");
10966 continue;
10967 }
10968
10969 dwarf2_const_value_attr (attr, type, name,
10970 &cu->comp_unit_obstack, cu,
10971 &value, &bytes, &baton);
10972
10973 if (TYPE_NOSIGN (type))
10974 /* GDB prints characters as NUMBER 'CHAR'. If that's
10975 changed, this can use value_print instead. */
10976 c_printchar (value, type, &buf);
10977 else
10978 {
10979 struct value_print_options opts;
10980
10981 if (baton != NULL)
10982 v = dwarf2_evaluate_loc_desc (type, NULL,
10983 baton->data,
10984 baton->size,
10985 baton->per_cu);
10986 else if (bytes != NULL)
10987 {
10988 v = allocate_value (type);
10989 memcpy (value_contents_writeable (v), bytes,
10990 TYPE_LENGTH (type));
10991 }
10992 else
10993 v = value_from_longest (type, value);
10994
10995 /* Specify decimal so that we do not depend on
10996 the radix. */
10997 get_formatted_print_options (&opts, 'd');
10998 opts.raw = 1;
10999 value_print (v, &buf, &opts);
11000 release_value (v);
11001 }
11002 }
11003
11004 die->building_fullname = 0;
11005
11006 if (!first)
11007 {
11008 /* Close the argument list, with a space if necessary
11009 (nested templates). */
11010 if (!buf.empty () && buf.string ().back () == '>')
11011 buf.puts (" >");
11012 else
11013 buf.puts (">");
11014 }
11015 }
11016
11017 /* For C++ methods, append formal parameter type
11018 information, if PHYSNAME. */
11019
11020 if (physname && die->tag == DW_TAG_subprogram
11021 && cu->language == language_cplus)
11022 {
11023 struct type *type = read_type_die (die, cu);
11024
11025 c_type_print_args (type, &buf, 1, cu->language,
11026 &type_print_raw_options);
11027
11028 if (cu->language == language_cplus)
11029 {
11030 /* Assume that an artificial first parameter is
11031 "this", but do not crash if it is not. RealView
11032 marks unnamed (and thus unused) parameters as
11033 artificial; there is no way to differentiate
11034 the two cases. */
11035 if (TYPE_NFIELDS (type) > 0
11036 && TYPE_FIELD_ARTIFICIAL (type, 0)
11037 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11038 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11039 0))))
11040 buf.puts (" const");
11041 }
11042 }
11043
11044 const std::string &intermediate_name = buf.string ();
11045
11046 if (cu->language == language_cplus)
11047 canonical_name
11048 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11049 &objfile->per_bfd->storage_obstack);
11050
11051 /* If we only computed INTERMEDIATE_NAME, or if
11052 INTERMEDIATE_NAME is already canonical, then we need to
11053 copy it to the appropriate obstack. */
11054 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11055 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11056 intermediate_name);
11057 else
11058 name = canonical_name;
11059 }
11060 }
11061
11062 return name;
11063 }
11064
11065 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11066 If scope qualifiers are appropriate they will be added. The result
11067 will be allocated on the storage_obstack, or NULL if the DIE does
11068 not have a name. NAME may either be from a previous call to
11069 dwarf2_name or NULL.
11070
11071 The output string will be canonicalized (if C++). */
11072
11073 static const char *
11074 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11075 {
11076 return dwarf2_compute_name (name, die, cu, 0);
11077 }
11078
11079 /* Construct a physname for the given DIE in CU. NAME may either be
11080 from a previous call to dwarf2_name or NULL. The result will be
11081 allocated on the objfile_objstack or NULL if the DIE does not have a
11082 name.
11083
11084 The output string will be canonicalized (if C++). */
11085
11086 static const char *
11087 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11088 {
11089 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11090 const char *retval, *mangled = NULL, *canon = NULL;
11091 int need_copy = 1;
11092
11093 /* In this case dwarf2_compute_name is just a shortcut not building anything
11094 on its own. */
11095 if (!die_needs_namespace (die, cu))
11096 return dwarf2_compute_name (name, die, cu, 1);
11097
11098 mangled = dw2_linkage_name (die, cu);
11099
11100 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11101 See https://github.com/rust-lang/rust/issues/32925. */
11102 if (cu->language == language_rust && mangled != NULL
11103 && strchr (mangled, '{') != NULL)
11104 mangled = NULL;
11105
11106 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11107 has computed. */
11108 gdb::unique_xmalloc_ptr<char> demangled;
11109 if (mangled != NULL)
11110 {
11111
11112 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11113 {
11114 /* Do nothing (do not demangle the symbol name). */
11115 }
11116 else if (cu->language == language_go)
11117 {
11118 /* This is a lie, but we already lie to the caller new_symbol.
11119 new_symbol assumes we return the mangled name.
11120 This just undoes that lie until things are cleaned up. */
11121 }
11122 else
11123 {
11124 /* Use DMGL_RET_DROP for C++ template functions to suppress
11125 their return type. It is easier for GDB users to search
11126 for such functions as `name(params)' than `long name(params)'.
11127 In such case the minimal symbol names do not match the full
11128 symbol names but for template functions there is never a need
11129 to look up their definition from their declaration so
11130 the only disadvantage remains the minimal symbol variant
11131 `long name(params)' does not have the proper inferior type. */
11132 demangled.reset (gdb_demangle (mangled,
11133 (DMGL_PARAMS | DMGL_ANSI
11134 | DMGL_RET_DROP)));
11135 }
11136 if (demangled)
11137 canon = demangled.get ();
11138 else
11139 {
11140 canon = mangled;
11141 need_copy = 0;
11142 }
11143 }
11144
11145 if (canon == NULL || check_physname)
11146 {
11147 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11148
11149 if (canon != NULL && strcmp (physname, canon) != 0)
11150 {
11151 /* It may not mean a bug in GDB. The compiler could also
11152 compute DW_AT_linkage_name incorrectly. But in such case
11153 GDB would need to be bug-to-bug compatible. */
11154
11155 complaint (_("Computed physname <%s> does not match demangled <%s> "
11156 "(from linkage <%s>) - DIE at %s [in module %s]"),
11157 physname, canon, mangled, sect_offset_str (die->sect_off),
11158 objfile_name (objfile));
11159
11160 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11161 is available here - over computed PHYSNAME. It is safer
11162 against both buggy GDB and buggy compilers. */
11163
11164 retval = canon;
11165 }
11166 else
11167 {
11168 retval = physname;
11169 need_copy = 0;
11170 }
11171 }
11172 else
11173 retval = canon;
11174
11175 if (need_copy)
11176 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
11177
11178 return retval;
11179 }
11180
11181 /* Inspect DIE in CU for a namespace alias. If one exists, record
11182 a new symbol for it.
11183
11184 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11185
11186 static int
11187 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11188 {
11189 struct attribute *attr;
11190
11191 /* If the die does not have a name, this is not a namespace
11192 alias. */
11193 attr = dwarf2_attr (die, DW_AT_name, cu);
11194 if (attr != NULL)
11195 {
11196 int num;
11197 struct die_info *d = die;
11198 struct dwarf2_cu *imported_cu = cu;
11199
11200 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11201 keep inspecting DIEs until we hit the underlying import. */
11202 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11203 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11204 {
11205 attr = dwarf2_attr (d, DW_AT_import, cu);
11206 if (attr == NULL)
11207 break;
11208
11209 d = follow_die_ref (d, attr, &imported_cu);
11210 if (d->tag != DW_TAG_imported_declaration)
11211 break;
11212 }
11213
11214 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11215 {
11216 complaint (_("DIE at %s has too many recursively imported "
11217 "declarations"), sect_offset_str (d->sect_off));
11218 return 0;
11219 }
11220
11221 if (attr != NULL)
11222 {
11223 struct type *type;
11224 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11225
11226 type = get_die_type_at_offset (sect_off, cu->per_cu);
11227 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11228 {
11229 /* This declaration is a global namespace alias. Add
11230 a symbol for it whose type is the aliased namespace. */
11231 new_symbol (die, type, cu);
11232 return 1;
11233 }
11234 }
11235 }
11236
11237 return 0;
11238 }
11239
11240 /* Return the using directives repository (global or local?) to use in the
11241 current context for CU.
11242
11243 For Ada, imported declarations can materialize renamings, which *may* be
11244 global. However it is impossible (for now?) in DWARF to distinguish
11245 "external" imported declarations and "static" ones. As all imported
11246 declarations seem to be static in all other languages, make them all CU-wide
11247 global only in Ada. */
11248
11249 static struct using_direct **
11250 using_directives (struct dwarf2_cu *cu)
11251 {
11252 if (cu->language == language_ada
11253 && cu->get_builder ()->outermost_context_p ())
11254 return cu->get_builder ()->get_global_using_directives ();
11255 else
11256 return cu->get_builder ()->get_local_using_directives ();
11257 }
11258
11259 /* Read the import statement specified by the given die and record it. */
11260
11261 static void
11262 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11263 {
11264 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11265 struct attribute *import_attr;
11266 struct die_info *imported_die, *child_die;
11267 struct dwarf2_cu *imported_cu;
11268 const char *imported_name;
11269 const char *imported_name_prefix;
11270 const char *canonical_name;
11271 const char *import_alias;
11272 const char *imported_declaration = NULL;
11273 const char *import_prefix;
11274 std::vector<const char *> excludes;
11275
11276 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11277 if (import_attr == NULL)
11278 {
11279 complaint (_("Tag '%s' has no DW_AT_import"),
11280 dwarf_tag_name (die->tag));
11281 return;
11282 }
11283
11284 imported_cu = cu;
11285 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11286 imported_name = dwarf2_name (imported_die, imported_cu);
11287 if (imported_name == NULL)
11288 {
11289 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11290
11291 The import in the following code:
11292 namespace A
11293 {
11294 typedef int B;
11295 }
11296
11297 int main ()
11298 {
11299 using A::B;
11300 B b;
11301 return b;
11302 }
11303
11304 ...
11305 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11306 <52> DW_AT_decl_file : 1
11307 <53> DW_AT_decl_line : 6
11308 <54> DW_AT_import : <0x75>
11309 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11310 <59> DW_AT_name : B
11311 <5b> DW_AT_decl_file : 1
11312 <5c> DW_AT_decl_line : 2
11313 <5d> DW_AT_type : <0x6e>
11314 ...
11315 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11316 <76> DW_AT_byte_size : 4
11317 <77> DW_AT_encoding : 5 (signed)
11318
11319 imports the wrong die ( 0x75 instead of 0x58 ).
11320 This case will be ignored until the gcc bug is fixed. */
11321 return;
11322 }
11323
11324 /* Figure out the local name after import. */
11325 import_alias = dwarf2_name (die, cu);
11326
11327 /* Figure out where the statement is being imported to. */
11328 import_prefix = determine_prefix (die, cu);
11329
11330 /* Figure out what the scope of the imported die is and prepend it
11331 to the name of the imported die. */
11332 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11333
11334 if (imported_die->tag != DW_TAG_namespace
11335 && imported_die->tag != DW_TAG_module)
11336 {
11337 imported_declaration = imported_name;
11338 canonical_name = imported_name_prefix;
11339 }
11340 else if (strlen (imported_name_prefix) > 0)
11341 canonical_name = obconcat (&objfile->objfile_obstack,
11342 imported_name_prefix,
11343 (cu->language == language_d ? "." : "::"),
11344 imported_name, (char *) NULL);
11345 else
11346 canonical_name = imported_name;
11347
11348 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11349 for (child_die = die->child; child_die && child_die->tag;
11350 child_die = sibling_die (child_die))
11351 {
11352 /* DWARF-4: A Fortran use statement with a “rename list” may be
11353 represented by an imported module entry with an import attribute
11354 referring to the module and owned entries corresponding to those
11355 entities that are renamed as part of being imported. */
11356
11357 if (child_die->tag != DW_TAG_imported_declaration)
11358 {
11359 complaint (_("child DW_TAG_imported_declaration expected "
11360 "- DIE at %s [in module %s]"),
11361 sect_offset_str (child_die->sect_off),
11362 objfile_name (objfile));
11363 continue;
11364 }
11365
11366 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11367 if (import_attr == NULL)
11368 {
11369 complaint (_("Tag '%s' has no DW_AT_import"),
11370 dwarf_tag_name (child_die->tag));
11371 continue;
11372 }
11373
11374 imported_cu = cu;
11375 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11376 &imported_cu);
11377 imported_name = dwarf2_name (imported_die, imported_cu);
11378 if (imported_name == NULL)
11379 {
11380 complaint (_("child DW_TAG_imported_declaration has unknown "
11381 "imported name - DIE at %s [in module %s]"),
11382 sect_offset_str (child_die->sect_off),
11383 objfile_name (objfile));
11384 continue;
11385 }
11386
11387 excludes.push_back (imported_name);
11388
11389 process_die (child_die, cu);
11390 }
11391
11392 add_using_directive (using_directives (cu),
11393 import_prefix,
11394 canonical_name,
11395 import_alias,
11396 imported_declaration,
11397 excludes,
11398 0,
11399 &objfile->objfile_obstack);
11400 }
11401
11402 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11403 types, but gives them a size of zero. Starting with version 14,
11404 ICC is compatible with GCC. */
11405
11406 static bool
11407 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11408 {
11409 if (!cu->checked_producer)
11410 check_producer (cu);
11411
11412 return cu->producer_is_icc_lt_14;
11413 }
11414
11415 /* ICC generates a DW_AT_type for C void functions. This was observed on
11416 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11417 which says that void functions should not have a DW_AT_type. */
11418
11419 static bool
11420 producer_is_icc (struct dwarf2_cu *cu)
11421 {
11422 if (!cu->checked_producer)
11423 check_producer (cu);
11424
11425 return cu->producer_is_icc;
11426 }
11427
11428 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11429 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11430 this, it was first present in GCC release 4.3.0. */
11431
11432 static bool
11433 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11434 {
11435 if (!cu->checked_producer)
11436 check_producer (cu);
11437
11438 return cu->producer_is_gcc_lt_4_3;
11439 }
11440
11441 static file_and_directory
11442 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11443 {
11444 file_and_directory res;
11445
11446 /* Find the filename. Do not use dwarf2_name here, since the filename
11447 is not a source language identifier. */
11448 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11449 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11450
11451 if (res.comp_dir == NULL
11452 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11453 && IS_ABSOLUTE_PATH (res.name))
11454 {
11455 res.comp_dir_storage = ldirname (res.name);
11456 if (!res.comp_dir_storage.empty ())
11457 res.comp_dir = res.comp_dir_storage.c_str ();
11458 }
11459 if (res.comp_dir != NULL)
11460 {
11461 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11462 directory, get rid of it. */
11463 const char *cp = strchr (res.comp_dir, ':');
11464
11465 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11466 res.comp_dir = cp + 1;
11467 }
11468
11469 if (res.name == NULL)
11470 res.name = "<unknown>";
11471
11472 return res;
11473 }
11474
11475 /* Handle DW_AT_stmt_list for a compilation unit.
11476 DIE is the DW_TAG_compile_unit die for CU.
11477 COMP_DIR is the compilation directory. LOWPC is passed to
11478 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11479
11480 static void
11481 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11482 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11483 {
11484 struct dwarf2_per_objfile *dwarf2_per_objfile
11485 = cu->per_cu->dwarf2_per_objfile;
11486 struct objfile *objfile = dwarf2_per_objfile->objfile;
11487 struct attribute *attr;
11488 struct line_header line_header_local;
11489 hashval_t line_header_local_hash;
11490 void **slot;
11491 int decode_mapping;
11492
11493 gdb_assert (! cu->per_cu->is_debug_types);
11494
11495 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11496 if (attr == NULL)
11497 return;
11498
11499 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11500
11501 /* The line header hash table is only created if needed (it exists to
11502 prevent redundant reading of the line table for partial_units).
11503 If we're given a partial_unit, we'll need it. If we're given a
11504 compile_unit, then use the line header hash table if it's already
11505 created, but don't create one just yet. */
11506
11507 if (dwarf2_per_objfile->line_header_hash == NULL
11508 && die->tag == DW_TAG_partial_unit)
11509 {
11510 dwarf2_per_objfile->line_header_hash
11511 = htab_create_alloc_ex (127, line_header_hash_voidp,
11512 line_header_eq_voidp,
11513 free_line_header_voidp,
11514 &objfile->objfile_obstack,
11515 hashtab_obstack_allocate,
11516 dummy_obstack_deallocate);
11517 }
11518
11519 line_header_local.sect_off = line_offset;
11520 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11521 line_header_local_hash = line_header_hash (&line_header_local);
11522 if (dwarf2_per_objfile->line_header_hash != NULL)
11523 {
11524 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11525 &line_header_local,
11526 line_header_local_hash, NO_INSERT);
11527
11528 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11529 is not present in *SLOT (since if there is something in *SLOT then
11530 it will be for a partial_unit). */
11531 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11532 {
11533 gdb_assert (*slot != NULL);
11534 cu->line_header = (struct line_header *) *slot;
11535 return;
11536 }
11537 }
11538
11539 /* dwarf_decode_line_header does not yet provide sufficient information.
11540 We always have to call also dwarf_decode_lines for it. */
11541 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11542 if (lh == NULL)
11543 return;
11544
11545 cu->line_header = lh.release ();
11546 cu->line_header_die_owner = die;
11547
11548 if (dwarf2_per_objfile->line_header_hash == NULL)
11549 slot = NULL;
11550 else
11551 {
11552 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11553 &line_header_local,
11554 line_header_local_hash, INSERT);
11555 gdb_assert (slot != NULL);
11556 }
11557 if (slot != NULL && *slot == NULL)
11558 {
11559 /* This newly decoded line number information unit will be owned
11560 by line_header_hash hash table. */
11561 *slot = cu->line_header;
11562 cu->line_header_die_owner = NULL;
11563 }
11564 else
11565 {
11566 /* We cannot free any current entry in (*slot) as that struct line_header
11567 may be already used by multiple CUs. Create only temporary decoded
11568 line_header for this CU - it may happen at most once for each line
11569 number information unit. And if we're not using line_header_hash
11570 then this is what we want as well. */
11571 gdb_assert (die->tag != DW_TAG_partial_unit);
11572 }
11573 decode_mapping = (die->tag != DW_TAG_partial_unit);
11574 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11575 decode_mapping);
11576
11577 }
11578
11579 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11580
11581 static void
11582 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11583 {
11584 struct dwarf2_per_objfile *dwarf2_per_objfile
11585 = cu->per_cu->dwarf2_per_objfile;
11586 struct objfile *objfile = dwarf2_per_objfile->objfile;
11587 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11588 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11589 CORE_ADDR highpc = ((CORE_ADDR) 0);
11590 struct attribute *attr;
11591 struct die_info *child_die;
11592 CORE_ADDR baseaddr;
11593
11594 prepare_one_comp_unit (cu, die, cu->language);
11595 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11596
11597 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11598
11599 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11600 from finish_block. */
11601 if (lowpc == ((CORE_ADDR) -1))
11602 lowpc = highpc;
11603 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11604
11605 file_and_directory fnd = find_file_and_directory (die, cu);
11606
11607 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11608 standardised yet. As a workaround for the language detection we fall
11609 back to the DW_AT_producer string. */
11610 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11611 cu->language = language_opencl;
11612
11613 /* Similar hack for Go. */
11614 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11615 set_cu_language (DW_LANG_Go, cu);
11616
11617 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11618
11619 /* Decode line number information if present. We do this before
11620 processing child DIEs, so that the line header table is available
11621 for DW_AT_decl_file. */
11622 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11623
11624 /* Process all dies in compilation unit. */
11625 if (die->child != NULL)
11626 {
11627 child_die = die->child;
11628 while (child_die && child_die->tag)
11629 {
11630 process_die (child_die, cu);
11631 child_die = sibling_die (child_die);
11632 }
11633 }
11634
11635 /* Decode macro information, if present. Dwarf 2 macro information
11636 refers to information in the line number info statement program
11637 header, so we can only read it if we've read the header
11638 successfully. */
11639 attr = dwarf2_attr (die, DW_AT_macros, cu);
11640 if (attr == NULL)
11641 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11642 if (attr && cu->line_header)
11643 {
11644 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11645 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11646
11647 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11648 }
11649 else
11650 {
11651 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11652 if (attr && cu->line_header)
11653 {
11654 unsigned int macro_offset = DW_UNSND (attr);
11655
11656 dwarf_decode_macros (cu, macro_offset, 0);
11657 }
11658 }
11659 }
11660
11661 void
11662 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11663 {
11664 struct type_unit_group *tu_group;
11665 int first_time;
11666 struct attribute *attr;
11667 unsigned int i;
11668 struct signatured_type *sig_type;
11669
11670 gdb_assert (per_cu->is_debug_types);
11671 sig_type = (struct signatured_type *) per_cu;
11672
11673 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11674
11675 /* If we're using .gdb_index (includes -readnow) then
11676 per_cu->type_unit_group may not have been set up yet. */
11677 if (sig_type->type_unit_group == NULL)
11678 sig_type->type_unit_group = get_type_unit_group (this, attr);
11679 tu_group = sig_type->type_unit_group;
11680
11681 /* If we've already processed this stmt_list there's no real need to
11682 do it again, we could fake it and just recreate the part we need
11683 (file name,index -> symtab mapping). If data shows this optimization
11684 is useful we can do it then. */
11685 first_time = tu_group->compunit_symtab == NULL;
11686
11687 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11688 debug info. */
11689 line_header_up lh;
11690 if (attr != NULL)
11691 {
11692 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11693 lh = dwarf_decode_line_header (line_offset, this);
11694 }
11695 if (lh == NULL)
11696 {
11697 if (first_time)
11698 start_symtab ("", NULL, 0);
11699 else
11700 {
11701 gdb_assert (tu_group->symtabs == NULL);
11702 gdb_assert (m_builder == nullptr);
11703 struct compunit_symtab *cust = tu_group->compunit_symtab;
11704 m_builder.reset (new struct buildsym_compunit
11705 (COMPUNIT_OBJFILE (cust), "",
11706 COMPUNIT_DIRNAME (cust),
11707 compunit_language (cust),
11708 0, cust));
11709 }
11710 return;
11711 }
11712
11713 line_header = lh.release ();
11714 line_header_die_owner = die;
11715
11716 if (first_time)
11717 {
11718 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11719
11720 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11721 still initializing it, and our caller (a few levels up)
11722 process_full_type_unit still needs to know if this is the first
11723 time. */
11724
11725 tu_group->num_symtabs = line_header->file_names_size ();
11726 tu_group->symtabs = XNEWVEC (struct symtab *,
11727 line_header->file_names_size ());
11728
11729 auto &file_names = line_header->file_names ();
11730 for (i = 0; i < file_names.size (); ++i)
11731 {
11732 file_entry &fe = file_names[i];
11733 dwarf2_start_subfile (this, fe.name,
11734 fe.include_dir (line_header));
11735 buildsym_compunit *b = get_builder ();
11736 if (b->get_current_subfile ()->symtab == NULL)
11737 {
11738 /* NOTE: start_subfile will recognize when it's been
11739 passed a file it has already seen. So we can't
11740 assume there's a simple mapping from
11741 cu->line_header->file_names to subfiles, plus
11742 cu->line_header->file_names may contain dups. */
11743 b->get_current_subfile ()->symtab
11744 = allocate_symtab (cust, b->get_current_subfile ()->name);
11745 }
11746
11747 fe.symtab = b->get_current_subfile ()->symtab;
11748 tu_group->symtabs[i] = fe.symtab;
11749 }
11750 }
11751 else
11752 {
11753 gdb_assert (m_builder == nullptr);
11754 struct compunit_symtab *cust = tu_group->compunit_symtab;
11755 m_builder.reset (new struct buildsym_compunit
11756 (COMPUNIT_OBJFILE (cust), "",
11757 COMPUNIT_DIRNAME (cust),
11758 compunit_language (cust),
11759 0, cust));
11760
11761 auto &file_names = line_header->file_names ();
11762 for (i = 0; i < file_names.size (); ++i)
11763 {
11764 file_entry &fe = file_names[i];
11765 fe.symtab = tu_group->symtabs[i];
11766 }
11767 }
11768
11769 /* The main symtab is allocated last. Type units don't have DW_AT_name
11770 so they don't have a "real" (so to speak) symtab anyway.
11771 There is later code that will assign the main symtab to all symbols
11772 that don't have one. We need to handle the case of a symbol with a
11773 missing symtab (DW_AT_decl_file) anyway. */
11774 }
11775
11776 /* Process DW_TAG_type_unit.
11777 For TUs we want to skip the first top level sibling if it's not the
11778 actual type being defined by this TU. In this case the first top
11779 level sibling is there to provide context only. */
11780
11781 static void
11782 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11783 {
11784 struct die_info *child_die;
11785
11786 prepare_one_comp_unit (cu, die, language_minimal);
11787
11788 /* Initialize (or reinitialize) the machinery for building symtabs.
11789 We do this before processing child DIEs, so that the line header table
11790 is available for DW_AT_decl_file. */
11791 cu->setup_type_unit_groups (die);
11792
11793 if (die->child != NULL)
11794 {
11795 child_die = die->child;
11796 while (child_die && child_die->tag)
11797 {
11798 process_die (child_die, cu);
11799 child_die = sibling_die (child_die);
11800 }
11801 }
11802 }
11803 \f
11804 /* DWO/DWP files.
11805
11806 http://gcc.gnu.org/wiki/DebugFission
11807 http://gcc.gnu.org/wiki/DebugFissionDWP
11808
11809 To simplify handling of both DWO files ("object" files with the DWARF info)
11810 and DWP files (a file with the DWOs packaged up into one file), we treat
11811 DWP files as having a collection of virtual DWO files. */
11812
11813 static hashval_t
11814 hash_dwo_file (const void *item)
11815 {
11816 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11817 hashval_t hash;
11818
11819 hash = htab_hash_string (dwo_file->dwo_name);
11820 if (dwo_file->comp_dir != NULL)
11821 hash += htab_hash_string (dwo_file->comp_dir);
11822 return hash;
11823 }
11824
11825 static int
11826 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11827 {
11828 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11829 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11830
11831 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11832 return 0;
11833 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11834 return lhs->comp_dir == rhs->comp_dir;
11835 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11836 }
11837
11838 /* Allocate a hash table for DWO files. */
11839
11840 static htab_up
11841 allocate_dwo_file_hash_table (struct objfile *objfile)
11842 {
11843 auto delete_dwo_file = [] (void *item)
11844 {
11845 struct dwo_file *dwo_file = (struct dwo_file *) item;
11846
11847 delete dwo_file;
11848 };
11849
11850 return htab_up (htab_create_alloc_ex (41,
11851 hash_dwo_file,
11852 eq_dwo_file,
11853 delete_dwo_file,
11854 &objfile->objfile_obstack,
11855 hashtab_obstack_allocate,
11856 dummy_obstack_deallocate));
11857 }
11858
11859 /* Lookup DWO file DWO_NAME. */
11860
11861 static void **
11862 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11863 const char *dwo_name,
11864 const char *comp_dir)
11865 {
11866 struct dwo_file find_entry;
11867 void **slot;
11868
11869 if (dwarf2_per_objfile->dwo_files == NULL)
11870 dwarf2_per_objfile->dwo_files
11871 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11872
11873 find_entry.dwo_name = dwo_name;
11874 find_entry.comp_dir = comp_dir;
11875 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11876 INSERT);
11877
11878 return slot;
11879 }
11880
11881 static hashval_t
11882 hash_dwo_unit (const void *item)
11883 {
11884 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11885
11886 /* This drops the top 32 bits of the id, but is ok for a hash. */
11887 return dwo_unit->signature;
11888 }
11889
11890 static int
11891 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11892 {
11893 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11894 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11895
11896 /* The signature is assumed to be unique within the DWO file.
11897 So while object file CU dwo_id's always have the value zero,
11898 that's OK, assuming each object file DWO file has only one CU,
11899 and that's the rule for now. */
11900 return lhs->signature == rhs->signature;
11901 }
11902
11903 /* Allocate a hash table for DWO CUs,TUs.
11904 There is one of these tables for each of CUs,TUs for each DWO file. */
11905
11906 static htab_t
11907 allocate_dwo_unit_table (struct objfile *objfile)
11908 {
11909 /* Start out with a pretty small number.
11910 Generally DWO files contain only one CU and maybe some TUs. */
11911 return htab_create_alloc_ex (3,
11912 hash_dwo_unit,
11913 eq_dwo_unit,
11914 NULL,
11915 &objfile->objfile_obstack,
11916 hashtab_obstack_allocate,
11917 dummy_obstack_deallocate);
11918 }
11919
11920 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11921
11922 struct create_dwo_cu_data
11923 {
11924 struct dwo_file *dwo_file;
11925 struct dwo_unit dwo_unit;
11926 };
11927
11928 /* die_reader_func for create_dwo_cu. */
11929
11930 static void
11931 create_dwo_cu_reader (const struct die_reader_specs *reader,
11932 const gdb_byte *info_ptr,
11933 struct die_info *comp_unit_die,
11934 int has_children,
11935 void *datap)
11936 {
11937 struct dwarf2_cu *cu = reader->cu;
11938 sect_offset sect_off = cu->per_cu->sect_off;
11939 struct dwarf2_section_info *section = cu->per_cu->section;
11940 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11941 struct dwo_file *dwo_file = data->dwo_file;
11942 struct dwo_unit *dwo_unit = &data->dwo_unit;
11943
11944 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11945 if (!signature.has_value ())
11946 {
11947 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11948 " its dwo_id [in module %s]"),
11949 sect_offset_str (sect_off), dwo_file->dwo_name);
11950 return;
11951 }
11952
11953 dwo_unit->dwo_file = dwo_file;
11954 dwo_unit->signature = *signature;
11955 dwo_unit->section = section;
11956 dwo_unit->sect_off = sect_off;
11957 dwo_unit->length = cu->per_cu->length;
11958
11959 if (dwarf_read_debug)
11960 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11961 sect_offset_str (sect_off),
11962 hex_string (dwo_unit->signature));
11963 }
11964
11965 /* Create the dwo_units for the CUs in a DWO_FILE.
11966 Note: This function processes DWO files only, not DWP files. */
11967
11968 static void
11969 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11970 struct dwo_file &dwo_file, dwarf2_section_info &section,
11971 htab_t &cus_htab)
11972 {
11973 struct objfile *objfile = dwarf2_per_objfile->objfile;
11974 const gdb_byte *info_ptr, *end_ptr;
11975
11976 dwarf2_read_section (objfile, &section);
11977 info_ptr = section.buffer;
11978
11979 if (info_ptr == NULL)
11980 return;
11981
11982 if (dwarf_read_debug)
11983 {
11984 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11985 get_section_name (&section),
11986 get_section_file_name (&section));
11987 }
11988
11989 end_ptr = info_ptr + section.size;
11990 while (info_ptr < end_ptr)
11991 {
11992 struct dwarf2_per_cu_data per_cu;
11993 struct create_dwo_cu_data create_dwo_cu_data;
11994 struct dwo_unit *dwo_unit;
11995 void **slot;
11996 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11997
11998 memset (&create_dwo_cu_data.dwo_unit, 0,
11999 sizeof (create_dwo_cu_data.dwo_unit));
12000 memset (&per_cu, 0, sizeof (per_cu));
12001 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
12002 per_cu.is_debug_types = 0;
12003 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12004 per_cu.section = &section;
12005 create_dwo_cu_data.dwo_file = &dwo_file;
12006
12007 init_cutu_and_read_dies_no_follow (
12008 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12009 info_ptr += per_cu.length;
12010
12011 // If the unit could not be parsed, skip it.
12012 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12013 continue;
12014
12015 if (cus_htab == NULL)
12016 cus_htab = allocate_dwo_unit_table (objfile);
12017
12018 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12019 *dwo_unit = create_dwo_cu_data.dwo_unit;
12020 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12021 gdb_assert (slot != NULL);
12022 if (*slot != NULL)
12023 {
12024 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12025 sect_offset dup_sect_off = dup_cu->sect_off;
12026
12027 complaint (_("debug cu entry at offset %s is duplicate to"
12028 " the entry at offset %s, signature %s"),
12029 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
12030 hex_string (dwo_unit->signature));
12031 }
12032 *slot = (void *)dwo_unit;
12033 }
12034 }
12035
12036 /* DWP file .debug_{cu,tu}_index section format:
12037 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12038
12039 DWP Version 1:
12040
12041 Both index sections have the same format, and serve to map a 64-bit
12042 signature to a set of section numbers. Each section begins with a header,
12043 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12044 indexes, and a pool of 32-bit section numbers. The index sections will be
12045 aligned at 8-byte boundaries in the file.
12046
12047 The index section header consists of:
12048
12049 V, 32 bit version number
12050 -, 32 bits unused
12051 N, 32 bit number of compilation units or type units in the index
12052 M, 32 bit number of slots in the hash table
12053
12054 Numbers are recorded using the byte order of the application binary.
12055
12056 The hash table begins at offset 16 in the section, and consists of an array
12057 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12058 order of the application binary). Unused slots in the hash table are 0.
12059 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12060
12061 The parallel table begins immediately after the hash table
12062 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12063 array of 32-bit indexes (using the byte order of the application binary),
12064 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12065 table contains a 32-bit index into the pool of section numbers. For unused
12066 hash table slots, the corresponding entry in the parallel table will be 0.
12067
12068 The pool of section numbers begins immediately following the hash table
12069 (at offset 16 + 12 * M from the beginning of the section). The pool of
12070 section numbers consists of an array of 32-bit words (using the byte order
12071 of the application binary). Each item in the array is indexed starting
12072 from 0. The hash table entry provides the index of the first section
12073 number in the set. Additional section numbers in the set follow, and the
12074 set is terminated by a 0 entry (section number 0 is not used in ELF).
12075
12076 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12077 section must be the first entry in the set, and the .debug_abbrev.dwo must
12078 be the second entry. Other members of the set may follow in any order.
12079
12080 ---
12081
12082 DWP Version 2:
12083
12084 DWP Version 2 combines all the .debug_info, etc. sections into one,
12085 and the entries in the index tables are now offsets into these sections.
12086 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12087 section.
12088
12089 Index Section Contents:
12090 Header
12091 Hash Table of Signatures dwp_hash_table.hash_table
12092 Parallel Table of Indices dwp_hash_table.unit_table
12093 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12094 Table of Section Sizes dwp_hash_table.v2.sizes
12095
12096 The index section header consists of:
12097
12098 V, 32 bit version number
12099 L, 32 bit number of columns in the table of section offsets
12100 N, 32 bit number of compilation units or type units in the index
12101 M, 32 bit number of slots in the hash table
12102
12103 Numbers are recorded using the byte order of the application binary.
12104
12105 The hash table has the same format as version 1.
12106 The parallel table of indices has the same format as version 1,
12107 except that the entries are origin-1 indices into the table of sections
12108 offsets and the table of section sizes.
12109
12110 The table of offsets begins immediately following the parallel table
12111 (at offset 16 + 12 * M from the beginning of the section). The table is
12112 a two-dimensional array of 32-bit words (using the byte order of the
12113 application binary), with L columns and N+1 rows, in row-major order.
12114 Each row in the array is indexed starting from 0. The first row provides
12115 a key to the remaining rows: each column in this row provides an identifier
12116 for a debug section, and the offsets in the same column of subsequent rows
12117 refer to that section. The section identifiers are:
12118
12119 DW_SECT_INFO 1 .debug_info.dwo
12120 DW_SECT_TYPES 2 .debug_types.dwo
12121 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12122 DW_SECT_LINE 4 .debug_line.dwo
12123 DW_SECT_LOC 5 .debug_loc.dwo
12124 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12125 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12126 DW_SECT_MACRO 8 .debug_macro.dwo
12127
12128 The offsets provided by the CU and TU index sections are the base offsets
12129 for the contributions made by each CU or TU to the corresponding section
12130 in the package file. Each CU and TU header contains an abbrev_offset
12131 field, used to find the abbreviations table for that CU or TU within the
12132 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12133 be interpreted as relative to the base offset given in the index section.
12134 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12135 should be interpreted as relative to the base offset for .debug_line.dwo,
12136 and offsets into other debug sections obtained from DWARF attributes should
12137 also be interpreted as relative to the corresponding base offset.
12138
12139 The table of sizes begins immediately following the table of offsets.
12140 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12141 with L columns and N rows, in row-major order. Each row in the array is
12142 indexed starting from 1 (row 0 is shared by the two tables).
12143
12144 ---
12145
12146 Hash table lookup is handled the same in version 1 and 2:
12147
12148 We assume that N and M will not exceed 2^32 - 1.
12149 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12150
12151 Given a 64-bit compilation unit signature or a type signature S, an entry
12152 in the hash table is located as follows:
12153
12154 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12155 the low-order k bits all set to 1.
12156
12157 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12158
12159 3) If the hash table entry at index H matches the signature, use that
12160 entry. If the hash table entry at index H is unused (all zeroes),
12161 terminate the search: the signature is not present in the table.
12162
12163 4) Let H = (H + H') modulo M. Repeat at Step 3.
12164
12165 Because M > N and H' and M are relatively prime, the search is guaranteed
12166 to stop at an unused slot or find the match. */
12167
12168 /* Create a hash table to map DWO IDs to their CU/TU entry in
12169 .debug_{info,types}.dwo in DWP_FILE.
12170 Returns NULL if there isn't one.
12171 Note: This function processes DWP files only, not DWO files. */
12172
12173 static struct dwp_hash_table *
12174 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12175 struct dwp_file *dwp_file, int is_debug_types)
12176 {
12177 struct objfile *objfile = dwarf2_per_objfile->objfile;
12178 bfd *dbfd = dwp_file->dbfd.get ();
12179 const gdb_byte *index_ptr, *index_end;
12180 struct dwarf2_section_info *index;
12181 uint32_t version, nr_columns, nr_units, nr_slots;
12182 struct dwp_hash_table *htab;
12183
12184 if (is_debug_types)
12185 index = &dwp_file->sections.tu_index;
12186 else
12187 index = &dwp_file->sections.cu_index;
12188
12189 if (dwarf2_section_empty_p (index))
12190 return NULL;
12191 dwarf2_read_section (objfile, index);
12192
12193 index_ptr = index->buffer;
12194 index_end = index_ptr + index->size;
12195
12196 version = read_4_bytes (dbfd, index_ptr);
12197 index_ptr += 4;
12198 if (version == 2)
12199 nr_columns = read_4_bytes (dbfd, index_ptr);
12200 else
12201 nr_columns = 0;
12202 index_ptr += 4;
12203 nr_units = read_4_bytes (dbfd, index_ptr);
12204 index_ptr += 4;
12205 nr_slots = read_4_bytes (dbfd, index_ptr);
12206 index_ptr += 4;
12207
12208 if (version != 1 && version != 2)
12209 {
12210 error (_("Dwarf Error: unsupported DWP file version (%s)"
12211 " [in module %s]"),
12212 pulongest (version), dwp_file->name);
12213 }
12214 if (nr_slots != (nr_slots & -nr_slots))
12215 {
12216 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12217 " is not power of 2 [in module %s]"),
12218 pulongest (nr_slots), dwp_file->name);
12219 }
12220
12221 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12222 htab->version = version;
12223 htab->nr_columns = nr_columns;
12224 htab->nr_units = nr_units;
12225 htab->nr_slots = nr_slots;
12226 htab->hash_table = index_ptr;
12227 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12228
12229 /* Exit early if the table is empty. */
12230 if (nr_slots == 0 || nr_units == 0
12231 || (version == 2 && nr_columns == 0))
12232 {
12233 /* All must be zero. */
12234 if (nr_slots != 0 || nr_units != 0
12235 || (version == 2 && nr_columns != 0))
12236 {
12237 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
12238 " all zero [in modules %s]"),
12239 dwp_file->name);
12240 }
12241 return htab;
12242 }
12243
12244 if (version == 1)
12245 {
12246 htab->section_pool.v1.indices =
12247 htab->unit_table + sizeof (uint32_t) * nr_slots;
12248 /* It's harder to decide whether the section is too small in v1.
12249 V1 is deprecated anyway so we punt. */
12250 }
12251 else
12252 {
12253 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12254 int *ids = htab->section_pool.v2.section_ids;
12255 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
12256 /* Reverse map for error checking. */
12257 int ids_seen[DW_SECT_MAX + 1];
12258 int i;
12259
12260 if (nr_columns < 2)
12261 {
12262 error (_("Dwarf Error: bad DWP hash table, too few columns"
12263 " in section table [in module %s]"),
12264 dwp_file->name);
12265 }
12266 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12267 {
12268 error (_("Dwarf Error: bad DWP hash table, too many columns"
12269 " in section table [in module %s]"),
12270 dwp_file->name);
12271 }
12272 memset (ids, 255, sizeof_ids);
12273 memset (ids_seen, 255, sizeof (ids_seen));
12274 for (i = 0; i < nr_columns; ++i)
12275 {
12276 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12277
12278 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12279 {
12280 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12281 " in section table [in module %s]"),
12282 id, dwp_file->name);
12283 }
12284 if (ids_seen[id] != -1)
12285 {
12286 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12287 " id %d in section table [in module %s]"),
12288 id, dwp_file->name);
12289 }
12290 ids_seen[id] = i;
12291 ids[i] = id;
12292 }
12293 /* Must have exactly one info or types section. */
12294 if (((ids_seen[DW_SECT_INFO] != -1)
12295 + (ids_seen[DW_SECT_TYPES] != -1))
12296 != 1)
12297 {
12298 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12299 " DWO info/types section [in module %s]"),
12300 dwp_file->name);
12301 }
12302 /* Must have an abbrev section. */
12303 if (ids_seen[DW_SECT_ABBREV] == -1)
12304 {
12305 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12306 " section [in module %s]"),
12307 dwp_file->name);
12308 }
12309 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12310 htab->section_pool.v2.sizes =
12311 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12312 * nr_units * nr_columns);
12313 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12314 * nr_units * nr_columns))
12315 > index_end)
12316 {
12317 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12318 " [in module %s]"),
12319 dwp_file->name);
12320 }
12321 }
12322
12323 return htab;
12324 }
12325
12326 /* Update SECTIONS with the data from SECTP.
12327
12328 This function is like the other "locate" section routines that are
12329 passed to bfd_map_over_sections, but in this context the sections to
12330 read comes from the DWP V1 hash table, not the full ELF section table.
12331
12332 The result is non-zero for success, or zero if an error was found. */
12333
12334 static int
12335 locate_v1_virtual_dwo_sections (asection *sectp,
12336 struct virtual_v1_dwo_sections *sections)
12337 {
12338 const struct dwop_section_names *names = &dwop_section_names;
12339
12340 if (section_is_p (sectp->name, &names->abbrev_dwo))
12341 {
12342 /* There can be only one. */
12343 if (sections->abbrev.s.section != NULL)
12344 return 0;
12345 sections->abbrev.s.section = sectp;
12346 sections->abbrev.size = bfd_section_size (sectp);
12347 }
12348 else if (section_is_p (sectp->name, &names->info_dwo)
12349 || section_is_p (sectp->name, &names->types_dwo))
12350 {
12351 /* There can be only one. */
12352 if (sections->info_or_types.s.section != NULL)
12353 return 0;
12354 sections->info_or_types.s.section = sectp;
12355 sections->info_or_types.size = bfd_section_size (sectp);
12356 }
12357 else if (section_is_p (sectp->name, &names->line_dwo))
12358 {
12359 /* There can be only one. */
12360 if (sections->line.s.section != NULL)
12361 return 0;
12362 sections->line.s.section = sectp;
12363 sections->line.size = bfd_section_size (sectp);
12364 }
12365 else if (section_is_p (sectp->name, &names->loc_dwo))
12366 {
12367 /* There can be only one. */
12368 if (sections->loc.s.section != NULL)
12369 return 0;
12370 sections->loc.s.section = sectp;
12371 sections->loc.size = bfd_section_size (sectp);
12372 }
12373 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12374 {
12375 /* There can be only one. */
12376 if (sections->macinfo.s.section != NULL)
12377 return 0;
12378 sections->macinfo.s.section = sectp;
12379 sections->macinfo.size = bfd_section_size (sectp);
12380 }
12381 else if (section_is_p (sectp->name, &names->macro_dwo))
12382 {
12383 /* There can be only one. */
12384 if (sections->macro.s.section != NULL)
12385 return 0;
12386 sections->macro.s.section = sectp;
12387 sections->macro.size = bfd_section_size (sectp);
12388 }
12389 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12390 {
12391 /* There can be only one. */
12392 if (sections->str_offsets.s.section != NULL)
12393 return 0;
12394 sections->str_offsets.s.section = sectp;
12395 sections->str_offsets.size = bfd_section_size (sectp);
12396 }
12397 else
12398 {
12399 /* No other kind of section is valid. */
12400 return 0;
12401 }
12402
12403 return 1;
12404 }
12405
12406 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12407 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12408 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12409 This is for DWP version 1 files. */
12410
12411 static struct dwo_unit *
12412 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12413 struct dwp_file *dwp_file,
12414 uint32_t unit_index,
12415 const char *comp_dir,
12416 ULONGEST signature, int is_debug_types)
12417 {
12418 struct objfile *objfile = dwarf2_per_objfile->objfile;
12419 const struct dwp_hash_table *dwp_htab =
12420 is_debug_types ? dwp_file->tus : dwp_file->cus;
12421 bfd *dbfd = dwp_file->dbfd.get ();
12422 const char *kind = is_debug_types ? "TU" : "CU";
12423 struct dwo_file *dwo_file;
12424 struct dwo_unit *dwo_unit;
12425 struct virtual_v1_dwo_sections sections;
12426 void **dwo_file_slot;
12427 int i;
12428
12429 gdb_assert (dwp_file->version == 1);
12430
12431 if (dwarf_read_debug)
12432 {
12433 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12434 kind,
12435 pulongest (unit_index), hex_string (signature),
12436 dwp_file->name);
12437 }
12438
12439 /* Fetch the sections of this DWO unit.
12440 Put a limit on the number of sections we look for so that bad data
12441 doesn't cause us to loop forever. */
12442
12443 #define MAX_NR_V1_DWO_SECTIONS \
12444 (1 /* .debug_info or .debug_types */ \
12445 + 1 /* .debug_abbrev */ \
12446 + 1 /* .debug_line */ \
12447 + 1 /* .debug_loc */ \
12448 + 1 /* .debug_str_offsets */ \
12449 + 1 /* .debug_macro or .debug_macinfo */ \
12450 + 1 /* trailing zero */)
12451
12452 memset (&sections, 0, sizeof (sections));
12453
12454 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12455 {
12456 asection *sectp;
12457 uint32_t section_nr =
12458 read_4_bytes (dbfd,
12459 dwp_htab->section_pool.v1.indices
12460 + (unit_index + i) * sizeof (uint32_t));
12461
12462 if (section_nr == 0)
12463 break;
12464 if (section_nr >= dwp_file->num_sections)
12465 {
12466 error (_("Dwarf Error: bad DWP hash table, section number too large"
12467 " [in module %s]"),
12468 dwp_file->name);
12469 }
12470
12471 sectp = dwp_file->elf_sections[section_nr];
12472 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12473 {
12474 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12475 " [in module %s]"),
12476 dwp_file->name);
12477 }
12478 }
12479
12480 if (i < 2
12481 || dwarf2_section_empty_p (&sections.info_or_types)
12482 || dwarf2_section_empty_p (&sections.abbrev))
12483 {
12484 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12485 " [in module %s]"),
12486 dwp_file->name);
12487 }
12488 if (i == MAX_NR_V1_DWO_SECTIONS)
12489 {
12490 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12491 " [in module %s]"),
12492 dwp_file->name);
12493 }
12494
12495 /* It's easier for the rest of the code if we fake a struct dwo_file and
12496 have dwo_unit "live" in that. At least for now.
12497
12498 The DWP file can be made up of a random collection of CUs and TUs.
12499 However, for each CU + set of TUs that came from the same original DWO
12500 file, we can combine them back into a virtual DWO file to save space
12501 (fewer struct dwo_file objects to allocate). Remember that for really
12502 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12503
12504 std::string virtual_dwo_name =
12505 string_printf ("virtual-dwo/%d-%d-%d-%d",
12506 get_section_id (&sections.abbrev),
12507 get_section_id (&sections.line),
12508 get_section_id (&sections.loc),
12509 get_section_id (&sections.str_offsets));
12510 /* Can we use an existing virtual DWO file? */
12511 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12512 virtual_dwo_name.c_str (),
12513 comp_dir);
12514 /* Create one if necessary. */
12515 if (*dwo_file_slot == NULL)
12516 {
12517 if (dwarf_read_debug)
12518 {
12519 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12520 virtual_dwo_name.c_str ());
12521 }
12522 dwo_file = new struct dwo_file;
12523 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12524 virtual_dwo_name);
12525 dwo_file->comp_dir = comp_dir;
12526 dwo_file->sections.abbrev = sections.abbrev;
12527 dwo_file->sections.line = sections.line;
12528 dwo_file->sections.loc = sections.loc;
12529 dwo_file->sections.macinfo = sections.macinfo;
12530 dwo_file->sections.macro = sections.macro;
12531 dwo_file->sections.str_offsets = sections.str_offsets;
12532 /* The "str" section is global to the entire DWP file. */
12533 dwo_file->sections.str = dwp_file->sections.str;
12534 /* The info or types section is assigned below to dwo_unit,
12535 there's no need to record it in dwo_file.
12536 Also, we can't simply record type sections in dwo_file because
12537 we record a pointer into the vector in dwo_unit. As we collect more
12538 types we'll grow the vector and eventually have to reallocate space
12539 for it, invalidating all copies of pointers into the previous
12540 contents. */
12541 *dwo_file_slot = dwo_file;
12542 }
12543 else
12544 {
12545 if (dwarf_read_debug)
12546 {
12547 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12548 virtual_dwo_name.c_str ());
12549 }
12550 dwo_file = (struct dwo_file *) *dwo_file_slot;
12551 }
12552
12553 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12554 dwo_unit->dwo_file = dwo_file;
12555 dwo_unit->signature = signature;
12556 dwo_unit->section =
12557 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12558 *dwo_unit->section = sections.info_or_types;
12559 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12560
12561 return dwo_unit;
12562 }
12563
12564 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12565 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12566 piece within that section used by a TU/CU, return a virtual section
12567 of just that piece. */
12568
12569 static struct dwarf2_section_info
12570 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12571 struct dwarf2_section_info *section,
12572 bfd_size_type offset, bfd_size_type size)
12573 {
12574 struct dwarf2_section_info result;
12575 asection *sectp;
12576
12577 gdb_assert (section != NULL);
12578 gdb_assert (!section->is_virtual);
12579
12580 memset (&result, 0, sizeof (result));
12581 result.s.containing_section = section;
12582 result.is_virtual = true;
12583
12584 if (size == 0)
12585 return result;
12586
12587 sectp = get_section_bfd_section (section);
12588
12589 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12590 bounds of the real section. This is a pretty-rare event, so just
12591 flag an error (easier) instead of a warning and trying to cope. */
12592 if (sectp == NULL
12593 || offset + size > bfd_section_size (sectp))
12594 {
12595 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12596 " in section %s [in module %s]"),
12597 sectp ? bfd_section_name (sectp) : "<unknown>",
12598 objfile_name (dwarf2_per_objfile->objfile));
12599 }
12600
12601 result.virtual_offset = offset;
12602 result.size = size;
12603 return result;
12604 }
12605
12606 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12607 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12608 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12609 This is for DWP version 2 files. */
12610
12611 static struct dwo_unit *
12612 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12613 struct dwp_file *dwp_file,
12614 uint32_t unit_index,
12615 const char *comp_dir,
12616 ULONGEST signature, int is_debug_types)
12617 {
12618 struct objfile *objfile = dwarf2_per_objfile->objfile;
12619 const struct dwp_hash_table *dwp_htab =
12620 is_debug_types ? dwp_file->tus : dwp_file->cus;
12621 bfd *dbfd = dwp_file->dbfd.get ();
12622 const char *kind = is_debug_types ? "TU" : "CU";
12623 struct dwo_file *dwo_file;
12624 struct dwo_unit *dwo_unit;
12625 struct virtual_v2_dwo_sections sections;
12626 void **dwo_file_slot;
12627 int i;
12628
12629 gdb_assert (dwp_file->version == 2);
12630
12631 if (dwarf_read_debug)
12632 {
12633 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12634 kind,
12635 pulongest (unit_index), hex_string (signature),
12636 dwp_file->name);
12637 }
12638
12639 /* Fetch the section offsets of this DWO unit. */
12640
12641 memset (&sections, 0, sizeof (sections));
12642
12643 for (i = 0; i < dwp_htab->nr_columns; ++i)
12644 {
12645 uint32_t offset = read_4_bytes (dbfd,
12646 dwp_htab->section_pool.v2.offsets
12647 + (((unit_index - 1) * dwp_htab->nr_columns
12648 + i)
12649 * sizeof (uint32_t)));
12650 uint32_t size = read_4_bytes (dbfd,
12651 dwp_htab->section_pool.v2.sizes
12652 + (((unit_index - 1) * dwp_htab->nr_columns
12653 + i)
12654 * sizeof (uint32_t)));
12655
12656 switch (dwp_htab->section_pool.v2.section_ids[i])
12657 {
12658 case DW_SECT_INFO:
12659 case DW_SECT_TYPES:
12660 sections.info_or_types_offset = offset;
12661 sections.info_or_types_size = size;
12662 break;
12663 case DW_SECT_ABBREV:
12664 sections.abbrev_offset = offset;
12665 sections.abbrev_size = size;
12666 break;
12667 case DW_SECT_LINE:
12668 sections.line_offset = offset;
12669 sections.line_size = size;
12670 break;
12671 case DW_SECT_LOC:
12672 sections.loc_offset = offset;
12673 sections.loc_size = size;
12674 break;
12675 case DW_SECT_STR_OFFSETS:
12676 sections.str_offsets_offset = offset;
12677 sections.str_offsets_size = size;
12678 break;
12679 case DW_SECT_MACINFO:
12680 sections.macinfo_offset = offset;
12681 sections.macinfo_size = size;
12682 break;
12683 case DW_SECT_MACRO:
12684 sections.macro_offset = offset;
12685 sections.macro_size = size;
12686 break;
12687 }
12688 }
12689
12690 /* It's easier for the rest of the code if we fake a struct dwo_file and
12691 have dwo_unit "live" in that. At least for now.
12692
12693 The DWP file can be made up of a random collection of CUs and TUs.
12694 However, for each CU + set of TUs that came from the same original DWO
12695 file, we can combine them back into a virtual DWO file to save space
12696 (fewer struct dwo_file objects to allocate). Remember that for really
12697 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12698
12699 std::string virtual_dwo_name =
12700 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12701 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12702 (long) (sections.line_size ? sections.line_offset : 0),
12703 (long) (sections.loc_size ? sections.loc_offset : 0),
12704 (long) (sections.str_offsets_size
12705 ? sections.str_offsets_offset : 0));
12706 /* Can we use an existing virtual DWO file? */
12707 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12708 virtual_dwo_name.c_str (),
12709 comp_dir);
12710 /* Create one if necessary. */
12711 if (*dwo_file_slot == NULL)
12712 {
12713 if (dwarf_read_debug)
12714 {
12715 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12716 virtual_dwo_name.c_str ());
12717 }
12718 dwo_file = new struct dwo_file;
12719 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12720 virtual_dwo_name);
12721 dwo_file->comp_dir = comp_dir;
12722 dwo_file->sections.abbrev =
12723 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12724 sections.abbrev_offset, sections.abbrev_size);
12725 dwo_file->sections.line =
12726 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12727 sections.line_offset, sections.line_size);
12728 dwo_file->sections.loc =
12729 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12730 sections.loc_offset, sections.loc_size);
12731 dwo_file->sections.macinfo =
12732 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12733 sections.macinfo_offset, sections.macinfo_size);
12734 dwo_file->sections.macro =
12735 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12736 sections.macro_offset, sections.macro_size);
12737 dwo_file->sections.str_offsets =
12738 create_dwp_v2_section (dwarf2_per_objfile,
12739 &dwp_file->sections.str_offsets,
12740 sections.str_offsets_offset,
12741 sections.str_offsets_size);
12742 /* The "str" section is global to the entire DWP file. */
12743 dwo_file->sections.str = dwp_file->sections.str;
12744 /* The info or types section is assigned below to dwo_unit,
12745 there's no need to record it in dwo_file.
12746 Also, we can't simply record type sections in dwo_file because
12747 we record a pointer into the vector in dwo_unit. As we collect more
12748 types we'll grow the vector and eventually have to reallocate space
12749 for it, invalidating all copies of pointers into the previous
12750 contents. */
12751 *dwo_file_slot = dwo_file;
12752 }
12753 else
12754 {
12755 if (dwarf_read_debug)
12756 {
12757 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12758 virtual_dwo_name.c_str ());
12759 }
12760 dwo_file = (struct dwo_file *) *dwo_file_slot;
12761 }
12762
12763 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12764 dwo_unit->dwo_file = dwo_file;
12765 dwo_unit->signature = signature;
12766 dwo_unit->section =
12767 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12768 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12769 is_debug_types
12770 ? &dwp_file->sections.types
12771 : &dwp_file->sections.info,
12772 sections.info_or_types_offset,
12773 sections.info_or_types_size);
12774 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12775
12776 return dwo_unit;
12777 }
12778
12779 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12780 Returns NULL if the signature isn't found. */
12781
12782 static struct dwo_unit *
12783 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12784 struct dwp_file *dwp_file, const char *comp_dir,
12785 ULONGEST signature, int is_debug_types)
12786 {
12787 const struct dwp_hash_table *dwp_htab =
12788 is_debug_types ? dwp_file->tus : dwp_file->cus;
12789 bfd *dbfd = dwp_file->dbfd.get ();
12790 uint32_t mask = dwp_htab->nr_slots - 1;
12791 uint32_t hash = signature & mask;
12792 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12793 unsigned int i;
12794 void **slot;
12795 struct dwo_unit find_dwo_cu;
12796
12797 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12798 find_dwo_cu.signature = signature;
12799 slot = htab_find_slot (is_debug_types
12800 ? dwp_file->loaded_tus
12801 : dwp_file->loaded_cus,
12802 &find_dwo_cu, INSERT);
12803
12804 if (*slot != NULL)
12805 return (struct dwo_unit *) *slot;
12806
12807 /* Use a for loop so that we don't loop forever on bad debug info. */
12808 for (i = 0; i < dwp_htab->nr_slots; ++i)
12809 {
12810 ULONGEST signature_in_table;
12811
12812 signature_in_table =
12813 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12814 if (signature_in_table == signature)
12815 {
12816 uint32_t unit_index =
12817 read_4_bytes (dbfd,
12818 dwp_htab->unit_table + hash * sizeof (uint32_t));
12819
12820 if (dwp_file->version == 1)
12821 {
12822 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12823 dwp_file, unit_index,
12824 comp_dir, signature,
12825 is_debug_types);
12826 }
12827 else
12828 {
12829 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12830 dwp_file, unit_index,
12831 comp_dir, signature,
12832 is_debug_types);
12833 }
12834 return (struct dwo_unit *) *slot;
12835 }
12836 if (signature_in_table == 0)
12837 return NULL;
12838 hash = (hash + hash2) & mask;
12839 }
12840
12841 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12842 " [in module %s]"),
12843 dwp_file->name);
12844 }
12845
12846 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12847 Open the file specified by FILE_NAME and hand it off to BFD for
12848 preliminary analysis. Return a newly initialized bfd *, which
12849 includes a canonicalized copy of FILE_NAME.
12850 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12851 SEARCH_CWD is true if the current directory is to be searched.
12852 It will be searched before debug-file-directory.
12853 If successful, the file is added to the bfd include table of the
12854 objfile's bfd (see gdb_bfd_record_inclusion).
12855 If unable to find/open the file, return NULL.
12856 NOTE: This function is derived from symfile_bfd_open. */
12857
12858 static gdb_bfd_ref_ptr
12859 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12860 const char *file_name, int is_dwp, int search_cwd)
12861 {
12862 int desc;
12863 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12864 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12865 to debug_file_directory. */
12866 const char *search_path;
12867 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12868
12869 gdb::unique_xmalloc_ptr<char> search_path_holder;
12870 if (search_cwd)
12871 {
12872 if (*debug_file_directory != '\0')
12873 {
12874 search_path_holder.reset (concat (".", dirname_separator_string,
12875 debug_file_directory,
12876 (char *) NULL));
12877 search_path = search_path_holder.get ();
12878 }
12879 else
12880 search_path = ".";
12881 }
12882 else
12883 search_path = debug_file_directory;
12884
12885 openp_flags flags = OPF_RETURN_REALPATH;
12886 if (is_dwp)
12887 flags |= OPF_SEARCH_IN_PATH;
12888
12889 gdb::unique_xmalloc_ptr<char> absolute_name;
12890 desc = openp (search_path, flags, file_name,
12891 O_RDONLY | O_BINARY, &absolute_name);
12892 if (desc < 0)
12893 return NULL;
12894
12895 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12896 gnutarget, desc));
12897 if (sym_bfd == NULL)
12898 return NULL;
12899 bfd_set_cacheable (sym_bfd.get (), 1);
12900
12901 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12902 return NULL;
12903
12904 /* Success. Record the bfd as having been included by the objfile's bfd.
12905 This is important because things like demangled_names_hash lives in the
12906 objfile's per_bfd space and may have references to things like symbol
12907 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12908 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12909
12910 return sym_bfd;
12911 }
12912
12913 /* Try to open DWO file FILE_NAME.
12914 COMP_DIR is the DW_AT_comp_dir attribute.
12915 The result is the bfd handle of the file.
12916 If there is a problem finding or opening the file, return NULL.
12917 Upon success, the canonicalized path of the file is stored in the bfd,
12918 same as symfile_bfd_open. */
12919
12920 static gdb_bfd_ref_ptr
12921 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12922 const char *file_name, const char *comp_dir)
12923 {
12924 if (IS_ABSOLUTE_PATH (file_name))
12925 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12926 0 /*is_dwp*/, 0 /*search_cwd*/);
12927
12928 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12929
12930 if (comp_dir != NULL)
12931 {
12932 char *path_to_try = concat (comp_dir, SLASH_STRING,
12933 file_name, (char *) NULL);
12934
12935 /* NOTE: If comp_dir is a relative path, this will also try the
12936 search path, which seems useful. */
12937 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12938 path_to_try,
12939 0 /*is_dwp*/,
12940 1 /*search_cwd*/));
12941 xfree (path_to_try);
12942 if (abfd != NULL)
12943 return abfd;
12944 }
12945
12946 /* That didn't work, try debug-file-directory, which, despite its name,
12947 is a list of paths. */
12948
12949 if (*debug_file_directory == '\0')
12950 return NULL;
12951
12952 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12953 0 /*is_dwp*/, 1 /*search_cwd*/);
12954 }
12955
12956 /* This function is mapped across the sections and remembers the offset and
12957 size of each of the DWO debugging sections we are interested in. */
12958
12959 static void
12960 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12961 {
12962 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12963 const struct dwop_section_names *names = &dwop_section_names;
12964
12965 if (section_is_p (sectp->name, &names->abbrev_dwo))
12966 {
12967 dwo_sections->abbrev.s.section = sectp;
12968 dwo_sections->abbrev.size = bfd_section_size (sectp);
12969 }
12970 else if (section_is_p (sectp->name, &names->info_dwo))
12971 {
12972 dwo_sections->info.s.section = sectp;
12973 dwo_sections->info.size = bfd_section_size (sectp);
12974 }
12975 else if (section_is_p (sectp->name, &names->line_dwo))
12976 {
12977 dwo_sections->line.s.section = sectp;
12978 dwo_sections->line.size = bfd_section_size (sectp);
12979 }
12980 else if (section_is_p (sectp->name, &names->loc_dwo))
12981 {
12982 dwo_sections->loc.s.section = sectp;
12983 dwo_sections->loc.size = bfd_section_size (sectp);
12984 }
12985 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12986 {
12987 dwo_sections->macinfo.s.section = sectp;
12988 dwo_sections->macinfo.size = bfd_section_size (sectp);
12989 }
12990 else if (section_is_p (sectp->name, &names->macro_dwo))
12991 {
12992 dwo_sections->macro.s.section = sectp;
12993 dwo_sections->macro.size = bfd_section_size (sectp);
12994 }
12995 else if (section_is_p (sectp->name, &names->str_dwo))
12996 {
12997 dwo_sections->str.s.section = sectp;
12998 dwo_sections->str.size = bfd_section_size (sectp);
12999 }
13000 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13001 {
13002 dwo_sections->str_offsets.s.section = sectp;
13003 dwo_sections->str_offsets.size = bfd_section_size (sectp);
13004 }
13005 else if (section_is_p (sectp->name, &names->types_dwo))
13006 {
13007 struct dwarf2_section_info type_section;
13008
13009 memset (&type_section, 0, sizeof (type_section));
13010 type_section.s.section = sectp;
13011 type_section.size = bfd_section_size (sectp);
13012 dwo_sections->types.push_back (type_section);
13013 }
13014 }
13015
13016 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
13017 by PER_CU. This is for the non-DWP case.
13018 The result is NULL if DWO_NAME can't be found. */
13019
13020 static struct dwo_file *
13021 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13022 const char *dwo_name, const char *comp_dir)
13023 {
13024 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
13025
13026 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
13027 if (dbfd == NULL)
13028 {
13029 if (dwarf_read_debug)
13030 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13031 return NULL;
13032 }
13033
13034 dwo_file_up dwo_file (new struct dwo_file);
13035 dwo_file->dwo_name = dwo_name;
13036 dwo_file->comp_dir = comp_dir;
13037 dwo_file->dbfd = std::move (dbfd);
13038
13039 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
13040 &dwo_file->sections);
13041
13042 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13043 dwo_file->cus);
13044
13045 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
13046 dwo_file->sections.types, dwo_file->tus);
13047
13048 if (dwarf_read_debug)
13049 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13050
13051 return dwo_file.release ();
13052 }
13053
13054 /* This function is mapped across the sections and remembers the offset and
13055 size of each of the DWP debugging sections common to version 1 and 2 that
13056 we are interested in. */
13057
13058 static void
13059 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13060 void *dwp_file_ptr)
13061 {
13062 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13063 const struct dwop_section_names *names = &dwop_section_names;
13064 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13065
13066 /* Record the ELF section number for later lookup: this is what the
13067 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13068 gdb_assert (elf_section_nr < dwp_file->num_sections);
13069 dwp_file->elf_sections[elf_section_nr] = sectp;
13070
13071 /* Look for specific sections that we need. */
13072 if (section_is_p (sectp->name, &names->str_dwo))
13073 {
13074 dwp_file->sections.str.s.section = sectp;
13075 dwp_file->sections.str.size = bfd_section_size (sectp);
13076 }
13077 else if (section_is_p (sectp->name, &names->cu_index))
13078 {
13079 dwp_file->sections.cu_index.s.section = sectp;
13080 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
13081 }
13082 else if (section_is_p (sectp->name, &names->tu_index))
13083 {
13084 dwp_file->sections.tu_index.s.section = sectp;
13085 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
13086 }
13087 }
13088
13089 /* This function is mapped across the sections and remembers the offset and
13090 size of each of the DWP version 2 debugging sections that we are interested
13091 in. This is split into a separate function because we don't know if we
13092 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13093
13094 static void
13095 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13096 {
13097 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13098 const struct dwop_section_names *names = &dwop_section_names;
13099 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13100
13101 /* Record the ELF section number for later lookup: this is what the
13102 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13103 gdb_assert (elf_section_nr < dwp_file->num_sections);
13104 dwp_file->elf_sections[elf_section_nr] = sectp;
13105
13106 /* Look for specific sections that we need. */
13107 if (section_is_p (sectp->name, &names->abbrev_dwo))
13108 {
13109 dwp_file->sections.abbrev.s.section = sectp;
13110 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13111 }
13112 else if (section_is_p (sectp->name, &names->info_dwo))
13113 {
13114 dwp_file->sections.info.s.section = sectp;
13115 dwp_file->sections.info.size = bfd_section_size (sectp);
13116 }
13117 else if (section_is_p (sectp->name, &names->line_dwo))
13118 {
13119 dwp_file->sections.line.s.section = sectp;
13120 dwp_file->sections.line.size = bfd_section_size (sectp);
13121 }
13122 else if (section_is_p (sectp->name, &names->loc_dwo))
13123 {
13124 dwp_file->sections.loc.s.section = sectp;
13125 dwp_file->sections.loc.size = bfd_section_size (sectp);
13126 }
13127 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13128 {
13129 dwp_file->sections.macinfo.s.section = sectp;
13130 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
13131 }
13132 else if (section_is_p (sectp->name, &names->macro_dwo))
13133 {
13134 dwp_file->sections.macro.s.section = sectp;
13135 dwp_file->sections.macro.size = bfd_section_size (sectp);
13136 }
13137 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13138 {
13139 dwp_file->sections.str_offsets.s.section = sectp;
13140 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13141 }
13142 else if (section_is_p (sectp->name, &names->types_dwo))
13143 {
13144 dwp_file->sections.types.s.section = sectp;
13145 dwp_file->sections.types.size = bfd_section_size (sectp);
13146 }
13147 }
13148
13149 /* Hash function for dwp_file loaded CUs/TUs. */
13150
13151 static hashval_t
13152 hash_dwp_loaded_cutus (const void *item)
13153 {
13154 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13155
13156 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13157 return dwo_unit->signature;
13158 }
13159
13160 /* Equality function for dwp_file loaded CUs/TUs. */
13161
13162 static int
13163 eq_dwp_loaded_cutus (const void *a, const void *b)
13164 {
13165 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13166 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13167
13168 return dua->signature == dub->signature;
13169 }
13170
13171 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13172
13173 static htab_t
13174 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13175 {
13176 return htab_create_alloc_ex (3,
13177 hash_dwp_loaded_cutus,
13178 eq_dwp_loaded_cutus,
13179 NULL,
13180 &objfile->objfile_obstack,
13181 hashtab_obstack_allocate,
13182 dummy_obstack_deallocate);
13183 }
13184
13185 /* Try to open DWP file FILE_NAME.
13186 The result is the bfd handle of the file.
13187 If there is a problem finding or opening the file, return NULL.
13188 Upon success, the canonicalized path of the file is stored in the bfd,
13189 same as symfile_bfd_open. */
13190
13191 static gdb_bfd_ref_ptr
13192 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13193 const char *file_name)
13194 {
13195 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13196 1 /*is_dwp*/,
13197 1 /*search_cwd*/));
13198 if (abfd != NULL)
13199 return abfd;
13200
13201 /* Work around upstream bug 15652.
13202 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13203 [Whether that's a "bug" is debatable, but it is getting in our way.]
13204 We have no real idea where the dwp file is, because gdb's realpath-ing
13205 of the executable's path may have discarded the needed info.
13206 [IWBN if the dwp file name was recorded in the executable, akin to
13207 .gnu_debuglink, but that doesn't exist yet.]
13208 Strip the directory from FILE_NAME and search again. */
13209 if (*debug_file_directory != '\0')
13210 {
13211 /* Don't implicitly search the current directory here.
13212 If the user wants to search "." to handle this case,
13213 it must be added to debug-file-directory. */
13214 return try_open_dwop_file (dwarf2_per_objfile,
13215 lbasename (file_name), 1 /*is_dwp*/,
13216 0 /*search_cwd*/);
13217 }
13218
13219 return NULL;
13220 }
13221
13222 /* Initialize the use of the DWP file for the current objfile.
13223 By convention the name of the DWP file is ${objfile}.dwp.
13224 The result is NULL if it can't be found. */
13225
13226 static std::unique_ptr<struct dwp_file>
13227 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13228 {
13229 struct objfile *objfile = dwarf2_per_objfile->objfile;
13230
13231 /* Try to find first .dwp for the binary file before any symbolic links
13232 resolving. */
13233
13234 /* If the objfile is a debug file, find the name of the real binary
13235 file and get the name of dwp file from there. */
13236 std::string dwp_name;
13237 if (objfile->separate_debug_objfile_backlink != NULL)
13238 {
13239 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13240 const char *backlink_basename = lbasename (backlink->original_name);
13241
13242 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13243 }
13244 else
13245 dwp_name = objfile->original_name;
13246
13247 dwp_name += ".dwp";
13248
13249 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13250 if (dbfd == NULL
13251 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13252 {
13253 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13254 dwp_name = objfile_name (objfile);
13255 dwp_name += ".dwp";
13256 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13257 }
13258
13259 if (dbfd == NULL)
13260 {
13261 if (dwarf_read_debug)
13262 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13263 return std::unique_ptr<dwp_file> ();
13264 }
13265
13266 const char *name = bfd_get_filename (dbfd.get ());
13267 std::unique_ptr<struct dwp_file> dwp_file
13268 (new struct dwp_file (name, std::move (dbfd)));
13269
13270 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13271 dwp_file->elf_sections =
13272 OBSTACK_CALLOC (&objfile->objfile_obstack,
13273 dwp_file->num_sections, asection *);
13274
13275 bfd_map_over_sections (dwp_file->dbfd.get (),
13276 dwarf2_locate_common_dwp_sections,
13277 dwp_file.get ());
13278
13279 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13280 0);
13281
13282 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13283 1);
13284
13285 /* The DWP file version is stored in the hash table. Oh well. */
13286 if (dwp_file->cus && dwp_file->tus
13287 && dwp_file->cus->version != dwp_file->tus->version)
13288 {
13289 /* Technically speaking, we should try to limp along, but this is
13290 pretty bizarre. We use pulongest here because that's the established
13291 portability solution (e.g, we cannot use %u for uint32_t). */
13292 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13293 " TU version %s [in DWP file %s]"),
13294 pulongest (dwp_file->cus->version),
13295 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13296 }
13297
13298 if (dwp_file->cus)
13299 dwp_file->version = dwp_file->cus->version;
13300 else if (dwp_file->tus)
13301 dwp_file->version = dwp_file->tus->version;
13302 else
13303 dwp_file->version = 2;
13304
13305 if (dwp_file->version == 2)
13306 bfd_map_over_sections (dwp_file->dbfd.get (),
13307 dwarf2_locate_v2_dwp_sections,
13308 dwp_file.get ());
13309
13310 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13311 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13312
13313 if (dwarf_read_debug)
13314 {
13315 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13316 fprintf_unfiltered (gdb_stdlog,
13317 " %s CUs, %s TUs\n",
13318 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13319 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13320 }
13321
13322 return dwp_file;
13323 }
13324
13325 /* Wrapper around open_and_init_dwp_file, only open it once. */
13326
13327 static struct dwp_file *
13328 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13329 {
13330 if (! dwarf2_per_objfile->dwp_checked)
13331 {
13332 dwarf2_per_objfile->dwp_file
13333 = open_and_init_dwp_file (dwarf2_per_objfile);
13334 dwarf2_per_objfile->dwp_checked = 1;
13335 }
13336 return dwarf2_per_objfile->dwp_file.get ();
13337 }
13338
13339 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13340 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13341 or in the DWP file for the objfile, referenced by THIS_UNIT.
13342 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13343 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13344
13345 This is called, for example, when wanting to read a variable with a
13346 complex location. Therefore we don't want to do file i/o for every call.
13347 Therefore we don't want to look for a DWO file on every call.
13348 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13349 then we check if we've already seen DWO_NAME, and only THEN do we check
13350 for a DWO file.
13351
13352 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13353 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13354
13355 static struct dwo_unit *
13356 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13357 const char *dwo_name, const char *comp_dir,
13358 ULONGEST signature, int is_debug_types)
13359 {
13360 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13361 struct objfile *objfile = dwarf2_per_objfile->objfile;
13362 const char *kind = is_debug_types ? "TU" : "CU";
13363 void **dwo_file_slot;
13364 struct dwo_file *dwo_file;
13365 struct dwp_file *dwp_file;
13366
13367 /* First see if there's a DWP file.
13368 If we have a DWP file but didn't find the DWO inside it, don't
13369 look for the original DWO file. It makes gdb behave differently
13370 depending on whether one is debugging in the build tree. */
13371
13372 dwp_file = get_dwp_file (dwarf2_per_objfile);
13373 if (dwp_file != NULL)
13374 {
13375 const struct dwp_hash_table *dwp_htab =
13376 is_debug_types ? dwp_file->tus : dwp_file->cus;
13377
13378 if (dwp_htab != NULL)
13379 {
13380 struct dwo_unit *dwo_cutu =
13381 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13382 signature, is_debug_types);
13383
13384 if (dwo_cutu != NULL)
13385 {
13386 if (dwarf_read_debug)
13387 {
13388 fprintf_unfiltered (gdb_stdlog,
13389 "Virtual DWO %s %s found: @%s\n",
13390 kind, hex_string (signature),
13391 host_address_to_string (dwo_cutu));
13392 }
13393 return dwo_cutu;
13394 }
13395 }
13396 }
13397 else
13398 {
13399 /* No DWP file, look for the DWO file. */
13400
13401 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13402 dwo_name, comp_dir);
13403 if (*dwo_file_slot == NULL)
13404 {
13405 /* Read in the file and build a table of the CUs/TUs it contains. */
13406 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13407 }
13408 /* NOTE: This will be NULL if unable to open the file. */
13409 dwo_file = (struct dwo_file *) *dwo_file_slot;
13410
13411 if (dwo_file != NULL)
13412 {
13413 struct dwo_unit *dwo_cutu = NULL;
13414
13415 if (is_debug_types && dwo_file->tus)
13416 {
13417 struct dwo_unit find_dwo_cutu;
13418
13419 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13420 find_dwo_cutu.signature = signature;
13421 dwo_cutu
13422 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13423 }
13424 else if (!is_debug_types && dwo_file->cus)
13425 {
13426 struct dwo_unit find_dwo_cutu;
13427
13428 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13429 find_dwo_cutu.signature = signature;
13430 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13431 &find_dwo_cutu);
13432 }
13433
13434 if (dwo_cutu != NULL)
13435 {
13436 if (dwarf_read_debug)
13437 {
13438 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13439 kind, dwo_name, hex_string (signature),
13440 host_address_to_string (dwo_cutu));
13441 }
13442 return dwo_cutu;
13443 }
13444 }
13445 }
13446
13447 /* We didn't find it. This could mean a dwo_id mismatch, or
13448 someone deleted the DWO/DWP file, or the search path isn't set up
13449 correctly to find the file. */
13450
13451 if (dwarf_read_debug)
13452 {
13453 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13454 kind, dwo_name, hex_string (signature));
13455 }
13456
13457 /* This is a warning and not a complaint because it can be caused by
13458 pilot error (e.g., user accidentally deleting the DWO). */
13459 {
13460 /* Print the name of the DWP file if we looked there, helps the user
13461 better diagnose the problem. */
13462 std::string dwp_text;
13463
13464 if (dwp_file != NULL)
13465 dwp_text = string_printf (" [in DWP file %s]",
13466 lbasename (dwp_file->name));
13467
13468 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13469 " [in module %s]"),
13470 kind, dwo_name, hex_string (signature),
13471 dwp_text.c_str (),
13472 this_unit->is_debug_types ? "TU" : "CU",
13473 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13474 }
13475 return NULL;
13476 }
13477
13478 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13479 See lookup_dwo_cutu_unit for details. */
13480
13481 static struct dwo_unit *
13482 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13483 const char *dwo_name, const char *comp_dir,
13484 ULONGEST signature)
13485 {
13486 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13487 }
13488
13489 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13490 See lookup_dwo_cutu_unit for details. */
13491
13492 static struct dwo_unit *
13493 lookup_dwo_type_unit (struct signatured_type *this_tu,
13494 const char *dwo_name, const char *comp_dir)
13495 {
13496 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13497 }
13498
13499 /* Traversal function for queue_and_load_all_dwo_tus. */
13500
13501 static int
13502 queue_and_load_dwo_tu (void **slot, void *info)
13503 {
13504 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13505 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13506 ULONGEST signature = dwo_unit->signature;
13507 struct signatured_type *sig_type =
13508 lookup_dwo_signatured_type (per_cu->cu, signature);
13509
13510 if (sig_type != NULL)
13511 {
13512 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13513
13514 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13515 a real dependency of PER_CU on SIG_TYPE. That is detected later
13516 while processing PER_CU. */
13517 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13518 load_full_type_unit (sig_cu);
13519 per_cu->imported_symtabs_push (sig_cu);
13520 }
13521
13522 return 1;
13523 }
13524
13525 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13526 The DWO may have the only definition of the type, though it may not be
13527 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13528 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13529
13530 static void
13531 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13532 {
13533 struct dwo_unit *dwo_unit;
13534 struct dwo_file *dwo_file;
13535
13536 gdb_assert (!per_cu->is_debug_types);
13537 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13538 gdb_assert (per_cu->cu != NULL);
13539
13540 dwo_unit = per_cu->cu->dwo_unit;
13541 gdb_assert (dwo_unit != NULL);
13542
13543 dwo_file = dwo_unit->dwo_file;
13544 if (dwo_file->tus != NULL)
13545 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13546 }
13547
13548 /* Read in various DIEs. */
13549
13550 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13551 Inherit only the children of the DW_AT_abstract_origin DIE not being
13552 already referenced by DW_AT_abstract_origin from the children of the
13553 current DIE. */
13554
13555 static void
13556 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13557 {
13558 struct die_info *child_die;
13559 sect_offset *offsetp;
13560 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13561 struct die_info *origin_die;
13562 /* Iterator of the ORIGIN_DIE children. */
13563 struct die_info *origin_child_die;
13564 struct attribute *attr;
13565 struct dwarf2_cu *origin_cu;
13566 struct pending **origin_previous_list_in_scope;
13567
13568 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13569 if (!attr)
13570 return;
13571
13572 /* Note that following die references may follow to a die in a
13573 different cu. */
13574
13575 origin_cu = cu;
13576 origin_die = follow_die_ref (die, attr, &origin_cu);
13577
13578 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13579 symbols in. */
13580 origin_previous_list_in_scope = origin_cu->list_in_scope;
13581 origin_cu->list_in_scope = cu->list_in_scope;
13582
13583 if (die->tag != origin_die->tag
13584 && !(die->tag == DW_TAG_inlined_subroutine
13585 && origin_die->tag == DW_TAG_subprogram))
13586 complaint (_("DIE %s and its abstract origin %s have different tags"),
13587 sect_offset_str (die->sect_off),
13588 sect_offset_str (origin_die->sect_off));
13589
13590 std::vector<sect_offset> offsets;
13591
13592 for (child_die = die->child;
13593 child_die && child_die->tag;
13594 child_die = sibling_die (child_die))
13595 {
13596 struct die_info *child_origin_die;
13597 struct dwarf2_cu *child_origin_cu;
13598
13599 /* We are trying to process concrete instance entries:
13600 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13601 it's not relevant to our analysis here. i.e. detecting DIEs that are
13602 present in the abstract instance but not referenced in the concrete
13603 one. */
13604 if (child_die->tag == DW_TAG_call_site
13605 || child_die->tag == DW_TAG_GNU_call_site)
13606 continue;
13607
13608 /* For each CHILD_DIE, find the corresponding child of
13609 ORIGIN_DIE. If there is more than one layer of
13610 DW_AT_abstract_origin, follow them all; there shouldn't be,
13611 but GCC versions at least through 4.4 generate this (GCC PR
13612 40573). */
13613 child_origin_die = child_die;
13614 child_origin_cu = cu;
13615 while (1)
13616 {
13617 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13618 child_origin_cu);
13619 if (attr == NULL)
13620 break;
13621 child_origin_die = follow_die_ref (child_origin_die, attr,
13622 &child_origin_cu);
13623 }
13624
13625 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13626 counterpart may exist. */
13627 if (child_origin_die != child_die)
13628 {
13629 if (child_die->tag != child_origin_die->tag
13630 && !(child_die->tag == DW_TAG_inlined_subroutine
13631 && child_origin_die->tag == DW_TAG_subprogram))
13632 complaint (_("Child DIE %s and its abstract origin %s have "
13633 "different tags"),
13634 sect_offset_str (child_die->sect_off),
13635 sect_offset_str (child_origin_die->sect_off));
13636 if (child_origin_die->parent != origin_die)
13637 complaint (_("Child DIE %s and its abstract origin %s have "
13638 "different parents"),
13639 sect_offset_str (child_die->sect_off),
13640 sect_offset_str (child_origin_die->sect_off));
13641 else
13642 offsets.push_back (child_origin_die->sect_off);
13643 }
13644 }
13645 std::sort (offsets.begin (), offsets.end ());
13646 sect_offset *offsets_end = offsets.data () + offsets.size ();
13647 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13648 if (offsetp[-1] == *offsetp)
13649 complaint (_("Multiple children of DIE %s refer "
13650 "to DIE %s as their abstract origin"),
13651 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13652
13653 offsetp = offsets.data ();
13654 origin_child_die = origin_die->child;
13655 while (origin_child_die && origin_child_die->tag)
13656 {
13657 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13658 while (offsetp < offsets_end
13659 && *offsetp < origin_child_die->sect_off)
13660 offsetp++;
13661 if (offsetp >= offsets_end
13662 || *offsetp > origin_child_die->sect_off)
13663 {
13664 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13665 Check whether we're already processing ORIGIN_CHILD_DIE.
13666 This can happen with mutually referenced abstract_origins.
13667 PR 16581. */
13668 if (!origin_child_die->in_process)
13669 process_die (origin_child_die, origin_cu);
13670 }
13671 origin_child_die = sibling_die (origin_child_die);
13672 }
13673 origin_cu->list_in_scope = origin_previous_list_in_scope;
13674 }
13675
13676 static void
13677 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13678 {
13679 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13680 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13681 struct context_stack *newobj;
13682 CORE_ADDR lowpc;
13683 CORE_ADDR highpc;
13684 struct die_info *child_die;
13685 struct attribute *attr, *call_line, *call_file;
13686 const char *name;
13687 CORE_ADDR baseaddr;
13688 struct block *block;
13689 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13690 std::vector<struct symbol *> template_args;
13691 struct template_symbol *templ_func = NULL;
13692
13693 if (inlined_func)
13694 {
13695 /* If we do not have call site information, we can't show the
13696 caller of this inlined function. That's too confusing, so
13697 only use the scope for local variables. */
13698 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13699 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13700 if (call_line == NULL || call_file == NULL)
13701 {
13702 read_lexical_block_scope (die, cu);
13703 return;
13704 }
13705 }
13706
13707 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13708
13709 name = dwarf2_name (die, cu);
13710
13711 /* Ignore functions with missing or empty names. These are actually
13712 illegal according to the DWARF standard. */
13713 if (name == NULL)
13714 {
13715 complaint (_("missing name for subprogram DIE at %s"),
13716 sect_offset_str (die->sect_off));
13717 return;
13718 }
13719
13720 /* Ignore functions with missing or invalid low and high pc attributes. */
13721 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13722 <= PC_BOUNDS_INVALID)
13723 {
13724 attr = dwarf2_attr (die, DW_AT_external, cu);
13725 if (!attr || !DW_UNSND (attr))
13726 complaint (_("cannot get low and high bounds "
13727 "for subprogram DIE at %s"),
13728 sect_offset_str (die->sect_off));
13729 return;
13730 }
13731
13732 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13733 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13734
13735 /* If we have any template arguments, then we must allocate a
13736 different sort of symbol. */
13737 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13738 {
13739 if (child_die->tag == DW_TAG_template_type_param
13740 || child_die->tag == DW_TAG_template_value_param)
13741 {
13742 templ_func = allocate_template_symbol (objfile);
13743 templ_func->subclass = SYMBOL_TEMPLATE;
13744 break;
13745 }
13746 }
13747
13748 newobj = cu->get_builder ()->push_context (0, lowpc);
13749 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13750 (struct symbol *) templ_func);
13751
13752 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13753 set_objfile_main_name (objfile, SYMBOL_LINKAGE_NAME (newobj->name),
13754 cu->language);
13755
13756 /* If there is a location expression for DW_AT_frame_base, record
13757 it. */
13758 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13759 if (attr)
13760 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13761
13762 /* If there is a location for the static link, record it. */
13763 newobj->static_link = NULL;
13764 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13765 if (attr)
13766 {
13767 newobj->static_link
13768 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13769 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13770 dwarf2_per_cu_addr_type (cu->per_cu));
13771 }
13772
13773 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13774
13775 if (die->child != NULL)
13776 {
13777 child_die = die->child;
13778 while (child_die && child_die->tag)
13779 {
13780 if (child_die->tag == DW_TAG_template_type_param
13781 || child_die->tag == DW_TAG_template_value_param)
13782 {
13783 struct symbol *arg = new_symbol (child_die, NULL, cu);
13784
13785 if (arg != NULL)
13786 template_args.push_back (arg);
13787 }
13788 else
13789 process_die (child_die, cu);
13790 child_die = sibling_die (child_die);
13791 }
13792 }
13793
13794 inherit_abstract_dies (die, cu);
13795
13796 /* If we have a DW_AT_specification, we might need to import using
13797 directives from the context of the specification DIE. See the
13798 comment in determine_prefix. */
13799 if (cu->language == language_cplus
13800 && dwarf2_attr (die, DW_AT_specification, cu))
13801 {
13802 struct dwarf2_cu *spec_cu = cu;
13803 struct die_info *spec_die = die_specification (die, &spec_cu);
13804
13805 while (spec_die)
13806 {
13807 child_die = spec_die->child;
13808 while (child_die && child_die->tag)
13809 {
13810 if (child_die->tag == DW_TAG_imported_module)
13811 process_die (child_die, spec_cu);
13812 child_die = sibling_die (child_die);
13813 }
13814
13815 /* In some cases, GCC generates specification DIEs that
13816 themselves contain DW_AT_specification attributes. */
13817 spec_die = die_specification (spec_die, &spec_cu);
13818 }
13819 }
13820
13821 struct context_stack cstk = cu->get_builder ()->pop_context ();
13822 /* Make a block for the local symbols within. */
13823 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13824 cstk.static_link, lowpc, highpc);
13825
13826 /* For C++, set the block's scope. */
13827 if ((cu->language == language_cplus
13828 || cu->language == language_fortran
13829 || cu->language == language_d
13830 || cu->language == language_rust)
13831 && cu->processing_has_namespace_info)
13832 block_set_scope (block, determine_prefix (die, cu),
13833 &objfile->objfile_obstack);
13834
13835 /* If we have address ranges, record them. */
13836 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13837
13838 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13839
13840 /* Attach template arguments to function. */
13841 if (!template_args.empty ())
13842 {
13843 gdb_assert (templ_func != NULL);
13844
13845 templ_func->n_template_arguments = template_args.size ();
13846 templ_func->template_arguments
13847 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13848 templ_func->n_template_arguments);
13849 memcpy (templ_func->template_arguments,
13850 template_args.data (),
13851 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13852
13853 /* Make sure that the symtab is set on the new symbols. Even
13854 though they don't appear in this symtab directly, other parts
13855 of gdb assume that symbols do, and this is reasonably
13856 true. */
13857 for (symbol *sym : template_args)
13858 symbol_set_symtab (sym, symbol_symtab (templ_func));
13859 }
13860
13861 /* In C++, we can have functions nested inside functions (e.g., when
13862 a function declares a class that has methods). This means that
13863 when we finish processing a function scope, we may need to go
13864 back to building a containing block's symbol lists. */
13865 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13866 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13867
13868 /* If we've finished processing a top-level function, subsequent
13869 symbols go in the file symbol list. */
13870 if (cu->get_builder ()->outermost_context_p ())
13871 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13872 }
13873
13874 /* Process all the DIES contained within a lexical block scope. Start
13875 a new scope, process the dies, and then close the scope. */
13876
13877 static void
13878 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13879 {
13880 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13881 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13882 CORE_ADDR lowpc, highpc;
13883 struct die_info *child_die;
13884 CORE_ADDR baseaddr;
13885
13886 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13887
13888 /* Ignore blocks with missing or invalid low and high pc attributes. */
13889 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13890 as multiple lexical blocks? Handling children in a sane way would
13891 be nasty. Might be easier to properly extend generic blocks to
13892 describe ranges. */
13893 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13894 {
13895 case PC_BOUNDS_NOT_PRESENT:
13896 /* DW_TAG_lexical_block has no attributes, process its children as if
13897 there was no wrapping by that DW_TAG_lexical_block.
13898 GCC does no longer produces such DWARF since GCC r224161. */
13899 for (child_die = die->child;
13900 child_die != NULL && child_die->tag;
13901 child_die = sibling_die (child_die))
13902 process_die (child_die, cu);
13903 return;
13904 case PC_BOUNDS_INVALID:
13905 return;
13906 }
13907 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13908 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13909
13910 cu->get_builder ()->push_context (0, lowpc);
13911 if (die->child != NULL)
13912 {
13913 child_die = die->child;
13914 while (child_die && child_die->tag)
13915 {
13916 process_die (child_die, cu);
13917 child_die = sibling_die (child_die);
13918 }
13919 }
13920 inherit_abstract_dies (die, cu);
13921 struct context_stack cstk = cu->get_builder ()->pop_context ();
13922
13923 if (*cu->get_builder ()->get_local_symbols () != NULL
13924 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13925 {
13926 struct block *block
13927 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13928 cstk.start_addr, highpc);
13929
13930 /* Note that recording ranges after traversing children, as we
13931 do here, means that recording a parent's ranges entails
13932 walking across all its children's ranges as they appear in
13933 the address map, which is quadratic behavior.
13934
13935 It would be nicer to record the parent's ranges before
13936 traversing its children, simply overriding whatever you find
13937 there. But since we don't even decide whether to create a
13938 block until after we've traversed its children, that's hard
13939 to do. */
13940 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13941 }
13942 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13943 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13944 }
13945
13946 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13947
13948 static void
13949 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13950 {
13951 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13952 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13953 CORE_ADDR pc, baseaddr;
13954 struct attribute *attr;
13955 struct call_site *call_site, call_site_local;
13956 void **slot;
13957 int nparams;
13958 struct die_info *child_die;
13959
13960 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13961
13962 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13963 if (attr == NULL)
13964 {
13965 /* This was a pre-DWARF-5 GNU extension alias
13966 for DW_AT_call_return_pc. */
13967 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13968 }
13969 if (!attr)
13970 {
13971 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13972 "DIE %s [in module %s]"),
13973 sect_offset_str (die->sect_off), objfile_name (objfile));
13974 return;
13975 }
13976 pc = attr_value_as_address (attr) + baseaddr;
13977 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13978
13979 if (cu->call_site_htab == NULL)
13980 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13981 NULL, &objfile->objfile_obstack,
13982 hashtab_obstack_allocate, NULL);
13983 call_site_local.pc = pc;
13984 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13985 if (*slot != NULL)
13986 {
13987 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13988 "DIE %s [in module %s]"),
13989 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13990 objfile_name (objfile));
13991 return;
13992 }
13993
13994 /* Count parameters at the caller. */
13995
13996 nparams = 0;
13997 for (child_die = die->child; child_die && child_die->tag;
13998 child_die = sibling_die (child_die))
13999 {
14000 if (child_die->tag != DW_TAG_call_site_parameter
14001 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14002 {
14003 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
14004 "DW_TAG_call_site child DIE %s [in module %s]"),
14005 child_die->tag, sect_offset_str (child_die->sect_off),
14006 objfile_name (objfile));
14007 continue;
14008 }
14009
14010 nparams++;
14011 }
14012
14013 call_site
14014 = ((struct call_site *)
14015 obstack_alloc (&objfile->objfile_obstack,
14016 sizeof (*call_site)
14017 + (sizeof (*call_site->parameter) * (nparams - 1))));
14018 *slot = call_site;
14019 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14020 call_site->pc = pc;
14021
14022 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14023 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14024 {
14025 struct die_info *func_die;
14026
14027 /* Skip also over DW_TAG_inlined_subroutine. */
14028 for (func_die = die->parent;
14029 func_die && func_die->tag != DW_TAG_subprogram
14030 && func_die->tag != DW_TAG_subroutine_type;
14031 func_die = func_die->parent);
14032
14033 /* DW_AT_call_all_calls is a superset
14034 of DW_AT_call_all_tail_calls. */
14035 if (func_die
14036 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14037 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14038 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14039 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14040 {
14041 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14042 not complete. But keep CALL_SITE for look ups via call_site_htab,
14043 both the initial caller containing the real return address PC and
14044 the final callee containing the current PC of a chain of tail
14045 calls do not need to have the tail call list complete. But any
14046 function candidate for a virtual tail call frame searched via
14047 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14048 determined unambiguously. */
14049 }
14050 else
14051 {
14052 struct type *func_type = NULL;
14053
14054 if (func_die)
14055 func_type = get_die_type (func_die, cu);
14056 if (func_type != NULL)
14057 {
14058 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14059
14060 /* Enlist this call site to the function. */
14061 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14062 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14063 }
14064 else
14065 complaint (_("Cannot find function owning DW_TAG_call_site "
14066 "DIE %s [in module %s]"),
14067 sect_offset_str (die->sect_off), objfile_name (objfile));
14068 }
14069 }
14070
14071 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14072 if (attr == NULL)
14073 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14074 if (attr == NULL)
14075 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14076 if (attr == NULL)
14077 {
14078 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14079 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14080 }
14081 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14082 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14083 /* Keep NULL DWARF_BLOCK. */;
14084 else if (attr_form_is_block (attr))
14085 {
14086 struct dwarf2_locexpr_baton *dlbaton;
14087
14088 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14089 dlbaton->data = DW_BLOCK (attr)->data;
14090 dlbaton->size = DW_BLOCK (attr)->size;
14091 dlbaton->per_cu = cu->per_cu;
14092
14093 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14094 }
14095 else if (attr_form_is_ref (attr))
14096 {
14097 struct dwarf2_cu *target_cu = cu;
14098 struct die_info *target_die;
14099
14100 target_die = follow_die_ref (die, attr, &target_cu);
14101 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14102 if (die_is_declaration (target_die, target_cu))
14103 {
14104 const char *target_physname;
14105
14106 /* Prefer the mangled name; otherwise compute the demangled one. */
14107 target_physname = dw2_linkage_name (target_die, target_cu);
14108 if (target_physname == NULL)
14109 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14110 if (target_physname == NULL)
14111 complaint (_("DW_AT_call_target target DIE has invalid "
14112 "physname, for referencing DIE %s [in module %s]"),
14113 sect_offset_str (die->sect_off), objfile_name (objfile));
14114 else
14115 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14116 }
14117 else
14118 {
14119 CORE_ADDR lowpc;
14120
14121 /* DW_AT_entry_pc should be preferred. */
14122 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14123 <= PC_BOUNDS_INVALID)
14124 complaint (_("DW_AT_call_target target DIE has invalid "
14125 "low pc, for referencing DIE %s [in module %s]"),
14126 sect_offset_str (die->sect_off), objfile_name (objfile));
14127 else
14128 {
14129 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14130 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14131 }
14132 }
14133 }
14134 else
14135 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14136 "block nor reference, for DIE %s [in module %s]"),
14137 sect_offset_str (die->sect_off), objfile_name (objfile));
14138
14139 call_site->per_cu = cu->per_cu;
14140
14141 for (child_die = die->child;
14142 child_die && child_die->tag;
14143 child_die = sibling_die (child_die))
14144 {
14145 struct call_site_parameter *parameter;
14146 struct attribute *loc, *origin;
14147
14148 if (child_die->tag != DW_TAG_call_site_parameter
14149 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14150 {
14151 /* Already printed the complaint above. */
14152 continue;
14153 }
14154
14155 gdb_assert (call_site->parameter_count < nparams);
14156 parameter = &call_site->parameter[call_site->parameter_count];
14157
14158 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14159 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14160 register is contained in DW_AT_call_value. */
14161
14162 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14163 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14164 if (origin == NULL)
14165 {
14166 /* This was a pre-DWARF-5 GNU extension alias
14167 for DW_AT_call_parameter. */
14168 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14169 }
14170 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14171 {
14172 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14173
14174 sect_offset sect_off
14175 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14176 if (!offset_in_cu_p (&cu->header, sect_off))
14177 {
14178 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14179 binding can be done only inside one CU. Such referenced DIE
14180 therefore cannot be even moved to DW_TAG_partial_unit. */
14181 complaint (_("DW_AT_call_parameter offset is not in CU for "
14182 "DW_TAG_call_site child DIE %s [in module %s]"),
14183 sect_offset_str (child_die->sect_off),
14184 objfile_name (objfile));
14185 continue;
14186 }
14187 parameter->u.param_cu_off
14188 = (cu_offset) (sect_off - cu->header.sect_off);
14189 }
14190 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14191 {
14192 complaint (_("No DW_FORM_block* DW_AT_location for "
14193 "DW_TAG_call_site child DIE %s [in module %s]"),
14194 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14195 continue;
14196 }
14197 else
14198 {
14199 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14200 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14201 if (parameter->u.dwarf_reg != -1)
14202 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14203 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14204 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14205 &parameter->u.fb_offset))
14206 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14207 else
14208 {
14209 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14210 "for DW_FORM_block* DW_AT_location is supported for "
14211 "DW_TAG_call_site child DIE %s "
14212 "[in module %s]"),
14213 sect_offset_str (child_die->sect_off),
14214 objfile_name (objfile));
14215 continue;
14216 }
14217 }
14218
14219 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14220 if (attr == NULL)
14221 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14222 if (!attr_form_is_block (attr))
14223 {
14224 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14225 "DW_TAG_call_site child DIE %s [in module %s]"),
14226 sect_offset_str (child_die->sect_off),
14227 objfile_name (objfile));
14228 continue;
14229 }
14230 parameter->value = DW_BLOCK (attr)->data;
14231 parameter->value_size = DW_BLOCK (attr)->size;
14232
14233 /* Parameters are not pre-cleared by memset above. */
14234 parameter->data_value = NULL;
14235 parameter->data_value_size = 0;
14236 call_site->parameter_count++;
14237
14238 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14239 if (attr == NULL)
14240 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14241 if (attr)
14242 {
14243 if (!attr_form_is_block (attr))
14244 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14245 "DW_TAG_call_site child DIE %s [in module %s]"),
14246 sect_offset_str (child_die->sect_off),
14247 objfile_name (objfile));
14248 else
14249 {
14250 parameter->data_value = DW_BLOCK (attr)->data;
14251 parameter->data_value_size = DW_BLOCK (attr)->size;
14252 }
14253 }
14254 }
14255 }
14256
14257 /* Helper function for read_variable. If DIE represents a virtual
14258 table, then return the type of the concrete object that is
14259 associated with the virtual table. Otherwise, return NULL. */
14260
14261 static struct type *
14262 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14263 {
14264 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14265 if (attr == NULL)
14266 return NULL;
14267
14268 /* Find the type DIE. */
14269 struct die_info *type_die = NULL;
14270 struct dwarf2_cu *type_cu = cu;
14271
14272 if (attr_form_is_ref (attr))
14273 type_die = follow_die_ref (die, attr, &type_cu);
14274 if (type_die == NULL)
14275 return NULL;
14276
14277 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14278 return NULL;
14279 return die_containing_type (type_die, type_cu);
14280 }
14281
14282 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14283
14284 static void
14285 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14286 {
14287 struct rust_vtable_symbol *storage = NULL;
14288
14289 if (cu->language == language_rust)
14290 {
14291 struct type *containing_type = rust_containing_type (die, cu);
14292
14293 if (containing_type != NULL)
14294 {
14295 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14296
14297 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14298 struct rust_vtable_symbol);
14299 initialize_objfile_symbol (storage);
14300 storage->concrete_type = containing_type;
14301 storage->subclass = SYMBOL_RUST_VTABLE;
14302 }
14303 }
14304
14305 struct symbol *res = new_symbol (die, NULL, cu, storage);
14306 struct attribute *abstract_origin
14307 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14308 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14309 if (res == NULL && loc && abstract_origin)
14310 {
14311 /* We have a variable without a name, but with a location and an abstract
14312 origin. This may be a concrete instance of an abstract variable
14313 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14314 later. */
14315 struct dwarf2_cu *origin_cu = cu;
14316 struct die_info *origin_die
14317 = follow_die_ref (die, abstract_origin, &origin_cu);
14318 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
14319 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
14320 }
14321 }
14322
14323 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14324 reading .debug_rnglists.
14325 Callback's type should be:
14326 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14327 Return true if the attributes are present and valid, otherwise,
14328 return false. */
14329
14330 template <typename Callback>
14331 static bool
14332 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14333 Callback &&callback)
14334 {
14335 struct dwarf2_per_objfile *dwarf2_per_objfile
14336 = cu->per_cu->dwarf2_per_objfile;
14337 struct objfile *objfile = dwarf2_per_objfile->objfile;
14338 bfd *obfd = objfile->obfd;
14339 /* Base address selection entry. */
14340 CORE_ADDR base;
14341 int found_base;
14342 const gdb_byte *buffer;
14343 CORE_ADDR baseaddr;
14344 bool overflow = false;
14345
14346 found_base = cu->base_known;
14347 base = cu->base_address;
14348
14349 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14350 if (offset >= dwarf2_per_objfile->rnglists.size)
14351 {
14352 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14353 offset);
14354 return false;
14355 }
14356 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14357
14358 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14359
14360 while (1)
14361 {
14362 /* Initialize it due to a false compiler warning. */
14363 CORE_ADDR range_beginning = 0, range_end = 0;
14364 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14365 + dwarf2_per_objfile->rnglists.size);
14366 unsigned int bytes_read;
14367
14368 if (buffer == buf_end)
14369 {
14370 overflow = true;
14371 break;
14372 }
14373 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14374 switch (rlet)
14375 {
14376 case DW_RLE_end_of_list:
14377 break;
14378 case DW_RLE_base_address:
14379 if (buffer + cu->header.addr_size > buf_end)
14380 {
14381 overflow = true;
14382 break;
14383 }
14384 base = read_address (obfd, buffer, cu, &bytes_read);
14385 found_base = 1;
14386 buffer += bytes_read;
14387 break;
14388 case DW_RLE_start_length:
14389 if (buffer + cu->header.addr_size > buf_end)
14390 {
14391 overflow = true;
14392 break;
14393 }
14394 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14395 buffer += bytes_read;
14396 range_end = (range_beginning
14397 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14398 buffer += bytes_read;
14399 if (buffer > buf_end)
14400 {
14401 overflow = true;
14402 break;
14403 }
14404 break;
14405 case DW_RLE_offset_pair:
14406 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14407 buffer += bytes_read;
14408 if (buffer > buf_end)
14409 {
14410 overflow = true;
14411 break;
14412 }
14413 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14414 buffer += bytes_read;
14415 if (buffer > buf_end)
14416 {
14417 overflow = true;
14418 break;
14419 }
14420 break;
14421 case DW_RLE_start_end:
14422 if (buffer + 2 * cu->header.addr_size > buf_end)
14423 {
14424 overflow = true;
14425 break;
14426 }
14427 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14428 buffer += bytes_read;
14429 range_end = read_address (obfd, buffer, cu, &bytes_read);
14430 buffer += bytes_read;
14431 break;
14432 default:
14433 complaint (_("Invalid .debug_rnglists data (no base address)"));
14434 return false;
14435 }
14436 if (rlet == DW_RLE_end_of_list || overflow)
14437 break;
14438 if (rlet == DW_RLE_base_address)
14439 continue;
14440
14441 if (!found_base)
14442 {
14443 /* We have no valid base address for the ranges
14444 data. */
14445 complaint (_("Invalid .debug_rnglists data (no base address)"));
14446 return false;
14447 }
14448
14449 if (range_beginning > range_end)
14450 {
14451 /* Inverted range entries are invalid. */
14452 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14453 return false;
14454 }
14455
14456 /* Empty range entries have no effect. */
14457 if (range_beginning == range_end)
14458 continue;
14459
14460 range_beginning += base;
14461 range_end += base;
14462
14463 /* A not-uncommon case of bad debug info.
14464 Don't pollute the addrmap with bad data. */
14465 if (range_beginning + baseaddr == 0
14466 && !dwarf2_per_objfile->has_section_at_zero)
14467 {
14468 complaint (_(".debug_rnglists entry has start address of zero"
14469 " [in module %s]"), objfile_name (objfile));
14470 continue;
14471 }
14472
14473 callback (range_beginning, range_end);
14474 }
14475
14476 if (overflow)
14477 {
14478 complaint (_("Offset %d is not terminated "
14479 "for DW_AT_ranges attribute"),
14480 offset);
14481 return false;
14482 }
14483
14484 return true;
14485 }
14486
14487 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14488 Callback's type should be:
14489 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14490 Return 1 if the attributes are present and valid, otherwise, return 0. */
14491
14492 template <typename Callback>
14493 static int
14494 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14495 Callback &&callback)
14496 {
14497 struct dwarf2_per_objfile *dwarf2_per_objfile
14498 = cu->per_cu->dwarf2_per_objfile;
14499 struct objfile *objfile = dwarf2_per_objfile->objfile;
14500 struct comp_unit_head *cu_header = &cu->header;
14501 bfd *obfd = objfile->obfd;
14502 unsigned int addr_size = cu_header->addr_size;
14503 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14504 /* Base address selection entry. */
14505 CORE_ADDR base;
14506 int found_base;
14507 unsigned int dummy;
14508 const gdb_byte *buffer;
14509 CORE_ADDR baseaddr;
14510
14511 if (cu_header->version >= 5)
14512 return dwarf2_rnglists_process (offset, cu, callback);
14513
14514 found_base = cu->base_known;
14515 base = cu->base_address;
14516
14517 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14518 if (offset >= dwarf2_per_objfile->ranges.size)
14519 {
14520 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14521 offset);
14522 return 0;
14523 }
14524 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14525
14526 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14527
14528 while (1)
14529 {
14530 CORE_ADDR range_beginning, range_end;
14531
14532 range_beginning = read_address (obfd, buffer, cu, &dummy);
14533 buffer += addr_size;
14534 range_end = read_address (obfd, buffer, cu, &dummy);
14535 buffer += addr_size;
14536 offset += 2 * addr_size;
14537
14538 /* An end of list marker is a pair of zero addresses. */
14539 if (range_beginning == 0 && range_end == 0)
14540 /* Found the end of list entry. */
14541 break;
14542
14543 /* Each base address selection entry is a pair of 2 values.
14544 The first is the largest possible address, the second is
14545 the base address. Check for a base address here. */
14546 if ((range_beginning & mask) == mask)
14547 {
14548 /* If we found the largest possible address, then we already
14549 have the base address in range_end. */
14550 base = range_end;
14551 found_base = 1;
14552 continue;
14553 }
14554
14555 if (!found_base)
14556 {
14557 /* We have no valid base address for the ranges
14558 data. */
14559 complaint (_("Invalid .debug_ranges data (no base address)"));
14560 return 0;
14561 }
14562
14563 if (range_beginning > range_end)
14564 {
14565 /* Inverted range entries are invalid. */
14566 complaint (_("Invalid .debug_ranges data (inverted range)"));
14567 return 0;
14568 }
14569
14570 /* Empty range entries have no effect. */
14571 if (range_beginning == range_end)
14572 continue;
14573
14574 range_beginning += base;
14575 range_end += base;
14576
14577 /* A not-uncommon case of bad debug info.
14578 Don't pollute the addrmap with bad data. */
14579 if (range_beginning + baseaddr == 0
14580 && !dwarf2_per_objfile->has_section_at_zero)
14581 {
14582 complaint (_(".debug_ranges entry has start address of zero"
14583 " [in module %s]"), objfile_name (objfile));
14584 continue;
14585 }
14586
14587 callback (range_beginning, range_end);
14588 }
14589
14590 return 1;
14591 }
14592
14593 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14594 Return 1 if the attributes are present and valid, otherwise, return 0.
14595 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14596
14597 static int
14598 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14599 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14600 struct partial_symtab *ranges_pst)
14601 {
14602 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14603 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14604 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14605 SECT_OFF_TEXT (objfile));
14606 int low_set = 0;
14607 CORE_ADDR low = 0;
14608 CORE_ADDR high = 0;
14609 int retval;
14610
14611 retval = dwarf2_ranges_process (offset, cu,
14612 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14613 {
14614 if (ranges_pst != NULL)
14615 {
14616 CORE_ADDR lowpc;
14617 CORE_ADDR highpc;
14618
14619 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14620 range_beginning + baseaddr)
14621 - baseaddr);
14622 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14623 range_end + baseaddr)
14624 - baseaddr);
14625 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14626 lowpc, highpc - 1, ranges_pst);
14627 }
14628
14629 /* FIXME: This is recording everything as a low-high
14630 segment of consecutive addresses. We should have a
14631 data structure for discontiguous block ranges
14632 instead. */
14633 if (! low_set)
14634 {
14635 low = range_beginning;
14636 high = range_end;
14637 low_set = 1;
14638 }
14639 else
14640 {
14641 if (range_beginning < low)
14642 low = range_beginning;
14643 if (range_end > high)
14644 high = range_end;
14645 }
14646 });
14647 if (!retval)
14648 return 0;
14649
14650 if (! low_set)
14651 /* If the first entry is an end-of-list marker, the range
14652 describes an empty scope, i.e. no instructions. */
14653 return 0;
14654
14655 if (low_return)
14656 *low_return = low;
14657 if (high_return)
14658 *high_return = high;
14659 return 1;
14660 }
14661
14662 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14663 definition for the return value. *LOWPC and *HIGHPC are set iff
14664 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14665
14666 static enum pc_bounds_kind
14667 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14668 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14669 struct partial_symtab *pst)
14670 {
14671 struct dwarf2_per_objfile *dwarf2_per_objfile
14672 = cu->per_cu->dwarf2_per_objfile;
14673 struct attribute *attr;
14674 struct attribute *attr_high;
14675 CORE_ADDR low = 0;
14676 CORE_ADDR high = 0;
14677 enum pc_bounds_kind ret;
14678
14679 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14680 if (attr_high)
14681 {
14682 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14683 if (attr)
14684 {
14685 low = attr_value_as_address (attr);
14686 high = attr_value_as_address (attr_high);
14687 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14688 high += low;
14689 }
14690 else
14691 /* Found high w/o low attribute. */
14692 return PC_BOUNDS_INVALID;
14693
14694 /* Found consecutive range of addresses. */
14695 ret = PC_BOUNDS_HIGH_LOW;
14696 }
14697 else
14698 {
14699 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14700 if (attr != NULL)
14701 {
14702 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14703 We take advantage of the fact that DW_AT_ranges does not appear
14704 in DW_TAG_compile_unit of DWO files. */
14705 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14706 unsigned int ranges_offset = (DW_UNSND (attr)
14707 + (need_ranges_base
14708 ? cu->ranges_base
14709 : 0));
14710
14711 /* Value of the DW_AT_ranges attribute is the offset in the
14712 .debug_ranges section. */
14713 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14714 return PC_BOUNDS_INVALID;
14715 /* Found discontinuous range of addresses. */
14716 ret = PC_BOUNDS_RANGES;
14717 }
14718 else
14719 return PC_BOUNDS_NOT_PRESENT;
14720 }
14721
14722 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14723 if (high <= low)
14724 return PC_BOUNDS_INVALID;
14725
14726 /* When using the GNU linker, .gnu.linkonce. sections are used to
14727 eliminate duplicate copies of functions and vtables and such.
14728 The linker will arbitrarily choose one and discard the others.
14729 The AT_*_pc values for such functions refer to local labels in
14730 these sections. If the section from that file was discarded, the
14731 labels are not in the output, so the relocs get a value of 0.
14732 If this is a discarded function, mark the pc bounds as invalid,
14733 so that GDB will ignore it. */
14734 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14735 return PC_BOUNDS_INVALID;
14736
14737 *lowpc = low;
14738 if (highpc)
14739 *highpc = high;
14740 return ret;
14741 }
14742
14743 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14744 its low and high PC addresses. Do nothing if these addresses could not
14745 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14746 and HIGHPC to the high address if greater than HIGHPC. */
14747
14748 static void
14749 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14750 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14751 struct dwarf2_cu *cu)
14752 {
14753 CORE_ADDR low, high;
14754 struct die_info *child = die->child;
14755
14756 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14757 {
14758 *lowpc = std::min (*lowpc, low);
14759 *highpc = std::max (*highpc, high);
14760 }
14761
14762 /* If the language does not allow nested subprograms (either inside
14763 subprograms or lexical blocks), we're done. */
14764 if (cu->language != language_ada)
14765 return;
14766
14767 /* Check all the children of the given DIE. If it contains nested
14768 subprograms, then check their pc bounds. Likewise, we need to
14769 check lexical blocks as well, as they may also contain subprogram
14770 definitions. */
14771 while (child && child->tag)
14772 {
14773 if (child->tag == DW_TAG_subprogram
14774 || child->tag == DW_TAG_lexical_block)
14775 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14776 child = sibling_die (child);
14777 }
14778 }
14779
14780 /* Get the low and high pc's represented by the scope DIE, and store
14781 them in *LOWPC and *HIGHPC. If the correct values can't be
14782 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14783
14784 static void
14785 get_scope_pc_bounds (struct die_info *die,
14786 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14787 struct dwarf2_cu *cu)
14788 {
14789 CORE_ADDR best_low = (CORE_ADDR) -1;
14790 CORE_ADDR best_high = (CORE_ADDR) 0;
14791 CORE_ADDR current_low, current_high;
14792
14793 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14794 >= PC_BOUNDS_RANGES)
14795 {
14796 best_low = current_low;
14797 best_high = current_high;
14798 }
14799 else
14800 {
14801 struct die_info *child = die->child;
14802
14803 while (child && child->tag)
14804 {
14805 switch (child->tag) {
14806 case DW_TAG_subprogram:
14807 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14808 break;
14809 case DW_TAG_namespace:
14810 case DW_TAG_module:
14811 /* FIXME: carlton/2004-01-16: Should we do this for
14812 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14813 that current GCC's always emit the DIEs corresponding
14814 to definitions of methods of classes as children of a
14815 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14816 the DIEs giving the declarations, which could be
14817 anywhere). But I don't see any reason why the
14818 standards says that they have to be there. */
14819 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14820
14821 if (current_low != ((CORE_ADDR) -1))
14822 {
14823 best_low = std::min (best_low, current_low);
14824 best_high = std::max (best_high, current_high);
14825 }
14826 break;
14827 default:
14828 /* Ignore. */
14829 break;
14830 }
14831
14832 child = sibling_die (child);
14833 }
14834 }
14835
14836 *lowpc = best_low;
14837 *highpc = best_high;
14838 }
14839
14840 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14841 in DIE. */
14842
14843 static void
14844 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14845 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14846 {
14847 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14848 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14849 struct attribute *attr;
14850 struct attribute *attr_high;
14851
14852 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14853 if (attr_high)
14854 {
14855 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14856 if (attr)
14857 {
14858 CORE_ADDR low = attr_value_as_address (attr);
14859 CORE_ADDR high = attr_value_as_address (attr_high);
14860
14861 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14862 high += low;
14863
14864 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14865 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14866 cu->get_builder ()->record_block_range (block, low, high - 1);
14867 }
14868 }
14869
14870 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14871 if (attr)
14872 {
14873 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14874 We take advantage of the fact that DW_AT_ranges does not appear
14875 in DW_TAG_compile_unit of DWO files. */
14876 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14877
14878 /* The value of the DW_AT_ranges attribute is the offset of the
14879 address range list in the .debug_ranges section. */
14880 unsigned long offset = (DW_UNSND (attr)
14881 + (need_ranges_base ? cu->ranges_base : 0));
14882
14883 std::vector<blockrange> blockvec;
14884 dwarf2_ranges_process (offset, cu,
14885 [&] (CORE_ADDR start, CORE_ADDR end)
14886 {
14887 start += baseaddr;
14888 end += baseaddr;
14889 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14890 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14891 cu->get_builder ()->record_block_range (block, start, end - 1);
14892 blockvec.emplace_back (start, end);
14893 });
14894
14895 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14896 }
14897 }
14898
14899 /* Check whether the producer field indicates either of GCC < 4.6, or the
14900 Intel C/C++ compiler, and cache the result in CU. */
14901
14902 static void
14903 check_producer (struct dwarf2_cu *cu)
14904 {
14905 int major, minor;
14906
14907 if (cu->producer == NULL)
14908 {
14909 /* For unknown compilers expect their behavior is DWARF version
14910 compliant.
14911
14912 GCC started to support .debug_types sections by -gdwarf-4 since
14913 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14914 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14915 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14916 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14917 }
14918 else if (producer_is_gcc (cu->producer, &major, &minor))
14919 {
14920 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14921 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14922 }
14923 else if (producer_is_icc (cu->producer, &major, &minor))
14924 {
14925 cu->producer_is_icc = true;
14926 cu->producer_is_icc_lt_14 = major < 14;
14927 }
14928 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14929 cu->producer_is_codewarrior = true;
14930 else
14931 {
14932 /* For other non-GCC compilers, expect their behavior is DWARF version
14933 compliant. */
14934 }
14935
14936 cu->checked_producer = true;
14937 }
14938
14939 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14940 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14941 during 4.6.0 experimental. */
14942
14943 static bool
14944 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14945 {
14946 if (!cu->checked_producer)
14947 check_producer (cu);
14948
14949 return cu->producer_is_gxx_lt_4_6;
14950 }
14951
14952
14953 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14954 with incorrect is_stmt attributes. */
14955
14956 static bool
14957 producer_is_codewarrior (struct dwarf2_cu *cu)
14958 {
14959 if (!cu->checked_producer)
14960 check_producer (cu);
14961
14962 return cu->producer_is_codewarrior;
14963 }
14964
14965 /* Return the default accessibility type if it is not overridden by
14966 DW_AT_accessibility. */
14967
14968 static enum dwarf_access_attribute
14969 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14970 {
14971 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14972 {
14973 /* The default DWARF 2 accessibility for members is public, the default
14974 accessibility for inheritance is private. */
14975
14976 if (die->tag != DW_TAG_inheritance)
14977 return DW_ACCESS_public;
14978 else
14979 return DW_ACCESS_private;
14980 }
14981 else
14982 {
14983 /* DWARF 3+ defines the default accessibility a different way. The same
14984 rules apply now for DW_TAG_inheritance as for the members and it only
14985 depends on the container kind. */
14986
14987 if (die->parent->tag == DW_TAG_class_type)
14988 return DW_ACCESS_private;
14989 else
14990 return DW_ACCESS_public;
14991 }
14992 }
14993
14994 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14995 offset. If the attribute was not found return 0, otherwise return
14996 1. If it was found but could not properly be handled, set *OFFSET
14997 to 0. */
14998
14999 static int
15000 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15001 LONGEST *offset)
15002 {
15003 struct attribute *attr;
15004
15005 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15006 if (attr != NULL)
15007 {
15008 *offset = 0;
15009
15010 /* Note that we do not check for a section offset first here.
15011 This is because DW_AT_data_member_location is new in DWARF 4,
15012 so if we see it, we can assume that a constant form is really
15013 a constant and not a section offset. */
15014 if (attr_form_is_constant (attr))
15015 *offset = dwarf2_get_attr_constant_value (attr, 0);
15016 else if (attr_form_is_section_offset (attr))
15017 dwarf2_complex_location_expr_complaint ();
15018 else if (attr_form_is_block (attr))
15019 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15020 else
15021 dwarf2_complex_location_expr_complaint ();
15022
15023 return 1;
15024 }
15025
15026 return 0;
15027 }
15028
15029 /* Add an aggregate field to the field list. */
15030
15031 static void
15032 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15033 struct dwarf2_cu *cu)
15034 {
15035 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15036 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15037 struct nextfield *new_field;
15038 struct attribute *attr;
15039 struct field *fp;
15040 const char *fieldname = "";
15041
15042 if (die->tag == DW_TAG_inheritance)
15043 {
15044 fip->baseclasses.emplace_back ();
15045 new_field = &fip->baseclasses.back ();
15046 }
15047 else
15048 {
15049 fip->fields.emplace_back ();
15050 new_field = &fip->fields.back ();
15051 }
15052
15053 fip->nfields++;
15054
15055 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15056 if (attr)
15057 new_field->accessibility = DW_UNSND (attr);
15058 else
15059 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15060 if (new_field->accessibility != DW_ACCESS_public)
15061 fip->non_public_fields = 1;
15062
15063 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15064 if (attr)
15065 new_field->virtuality = DW_UNSND (attr);
15066 else
15067 new_field->virtuality = DW_VIRTUALITY_none;
15068
15069 fp = &new_field->field;
15070
15071 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15072 {
15073 LONGEST offset;
15074
15075 /* Data member other than a C++ static data member. */
15076
15077 /* Get type of field. */
15078 fp->type = die_type (die, cu);
15079
15080 SET_FIELD_BITPOS (*fp, 0);
15081
15082 /* Get bit size of field (zero if none). */
15083 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15084 if (attr)
15085 {
15086 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15087 }
15088 else
15089 {
15090 FIELD_BITSIZE (*fp) = 0;
15091 }
15092
15093 /* Get bit offset of field. */
15094 if (handle_data_member_location (die, cu, &offset))
15095 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15096 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15097 if (attr)
15098 {
15099 if (gdbarch_bits_big_endian (gdbarch))
15100 {
15101 /* For big endian bits, the DW_AT_bit_offset gives the
15102 additional bit offset from the MSB of the containing
15103 anonymous object to the MSB of the field. We don't
15104 have to do anything special since we don't need to
15105 know the size of the anonymous object. */
15106 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15107 }
15108 else
15109 {
15110 /* For little endian bits, compute the bit offset to the
15111 MSB of the anonymous object, subtract off the number of
15112 bits from the MSB of the field to the MSB of the
15113 object, and then subtract off the number of bits of
15114 the field itself. The result is the bit offset of
15115 the LSB of the field. */
15116 int anonymous_size;
15117 int bit_offset = DW_UNSND (attr);
15118
15119 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15120 if (attr)
15121 {
15122 /* The size of the anonymous object containing
15123 the bit field is explicit, so use the
15124 indicated size (in bytes). */
15125 anonymous_size = DW_UNSND (attr);
15126 }
15127 else
15128 {
15129 /* The size of the anonymous object containing
15130 the bit field must be inferred from the type
15131 attribute of the data member containing the
15132 bit field. */
15133 anonymous_size = TYPE_LENGTH (fp->type);
15134 }
15135 SET_FIELD_BITPOS (*fp,
15136 (FIELD_BITPOS (*fp)
15137 + anonymous_size * bits_per_byte
15138 - bit_offset - FIELD_BITSIZE (*fp)));
15139 }
15140 }
15141 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15142 if (attr != NULL)
15143 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15144 + dwarf2_get_attr_constant_value (attr, 0)));
15145
15146 /* Get name of field. */
15147 fieldname = dwarf2_name (die, cu);
15148 if (fieldname == NULL)
15149 fieldname = "";
15150
15151 /* The name is already allocated along with this objfile, so we don't
15152 need to duplicate it for the type. */
15153 fp->name = fieldname;
15154
15155 /* Change accessibility for artificial fields (e.g. virtual table
15156 pointer or virtual base class pointer) to private. */
15157 if (dwarf2_attr (die, DW_AT_artificial, cu))
15158 {
15159 FIELD_ARTIFICIAL (*fp) = 1;
15160 new_field->accessibility = DW_ACCESS_private;
15161 fip->non_public_fields = 1;
15162 }
15163 }
15164 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15165 {
15166 /* C++ static member. */
15167
15168 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15169 is a declaration, but all versions of G++ as of this writing
15170 (so through at least 3.2.1) incorrectly generate
15171 DW_TAG_variable tags. */
15172
15173 const char *physname;
15174
15175 /* Get name of field. */
15176 fieldname = dwarf2_name (die, cu);
15177 if (fieldname == NULL)
15178 return;
15179
15180 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15181 if (attr
15182 /* Only create a symbol if this is an external value.
15183 new_symbol checks this and puts the value in the global symbol
15184 table, which we want. If it is not external, new_symbol
15185 will try to put the value in cu->list_in_scope which is wrong. */
15186 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15187 {
15188 /* A static const member, not much different than an enum as far as
15189 we're concerned, except that we can support more types. */
15190 new_symbol (die, NULL, cu);
15191 }
15192
15193 /* Get physical name. */
15194 physname = dwarf2_physname (fieldname, die, cu);
15195
15196 /* The name is already allocated along with this objfile, so we don't
15197 need to duplicate it for the type. */
15198 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15199 FIELD_TYPE (*fp) = die_type (die, cu);
15200 FIELD_NAME (*fp) = fieldname;
15201 }
15202 else if (die->tag == DW_TAG_inheritance)
15203 {
15204 LONGEST offset;
15205
15206 /* C++ base class field. */
15207 if (handle_data_member_location (die, cu, &offset))
15208 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15209 FIELD_BITSIZE (*fp) = 0;
15210 FIELD_TYPE (*fp) = die_type (die, cu);
15211 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
15212 }
15213 else if (die->tag == DW_TAG_variant_part)
15214 {
15215 /* process_structure_scope will treat this DIE as a union. */
15216 process_structure_scope (die, cu);
15217
15218 /* The variant part is relative to the start of the enclosing
15219 structure. */
15220 SET_FIELD_BITPOS (*fp, 0);
15221 fp->type = get_die_type (die, cu);
15222 fp->artificial = 1;
15223 fp->name = "<<variant>>";
15224
15225 /* Normally a DW_TAG_variant_part won't have a size, but our
15226 representation requires one, so set it to the maximum of the
15227 child sizes. */
15228 if (TYPE_LENGTH (fp->type) == 0)
15229 {
15230 unsigned max = 0;
15231 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
15232 if (TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)) > max)
15233 max = TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i));
15234 TYPE_LENGTH (fp->type) = max;
15235 }
15236 }
15237 else
15238 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15239 }
15240
15241 /* Can the type given by DIE define another type? */
15242
15243 static bool
15244 type_can_define_types (const struct die_info *die)
15245 {
15246 switch (die->tag)
15247 {
15248 case DW_TAG_typedef:
15249 case DW_TAG_class_type:
15250 case DW_TAG_structure_type:
15251 case DW_TAG_union_type:
15252 case DW_TAG_enumeration_type:
15253 return true;
15254
15255 default:
15256 return false;
15257 }
15258 }
15259
15260 /* Add a type definition defined in the scope of the FIP's class. */
15261
15262 static void
15263 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15264 struct dwarf2_cu *cu)
15265 {
15266 struct decl_field fp;
15267 memset (&fp, 0, sizeof (fp));
15268
15269 gdb_assert (type_can_define_types (die));
15270
15271 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15272 fp.name = dwarf2_name (die, cu);
15273 fp.type = read_type_die (die, cu);
15274
15275 /* Save accessibility. */
15276 enum dwarf_access_attribute accessibility;
15277 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15278 if (attr != NULL)
15279 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15280 else
15281 accessibility = dwarf2_default_access_attribute (die, cu);
15282 switch (accessibility)
15283 {
15284 case DW_ACCESS_public:
15285 /* The assumed value if neither private nor protected. */
15286 break;
15287 case DW_ACCESS_private:
15288 fp.is_private = 1;
15289 break;
15290 case DW_ACCESS_protected:
15291 fp.is_protected = 1;
15292 break;
15293 default:
15294 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15295 }
15296
15297 if (die->tag == DW_TAG_typedef)
15298 fip->typedef_field_list.push_back (fp);
15299 else
15300 fip->nested_types_list.push_back (fp);
15301 }
15302
15303 /* Create the vector of fields, and attach it to the type. */
15304
15305 static void
15306 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15307 struct dwarf2_cu *cu)
15308 {
15309 int nfields = fip->nfields;
15310
15311 /* Record the field count, allocate space for the array of fields,
15312 and create blank accessibility bitfields if necessary. */
15313 TYPE_NFIELDS (type) = nfields;
15314 TYPE_FIELDS (type) = (struct field *)
15315 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15316
15317 if (fip->non_public_fields && cu->language != language_ada)
15318 {
15319 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15320
15321 TYPE_FIELD_PRIVATE_BITS (type) =
15322 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15323 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15324
15325 TYPE_FIELD_PROTECTED_BITS (type) =
15326 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15327 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15328
15329 TYPE_FIELD_IGNORE_BITS (type) =
15330 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15331 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15332 }
15333
15334 /* If the type has baseclasses, allocate and clear a bit vector for
15335 TYPE_FIELD_VIRTUAL_BITS. */
15336 if (!fip->baseclasses.empty () && cu->language != language_ada)
15337 {
15338 int num_bytes = B_BYTES (fip->baseclasses.size ());
15339 unsigned char *pointer;
15340
15341 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15342 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15343 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15344 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15345 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15346 }
15347
15348 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15349 {
15350 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15351
15352 for (int index = 0; index < nfields; ++index)
15353 {
15354 struct nextfield &field = fip->fields[index];
15355
15356 if (field.variant.is_discriminant)
15357 di->discriminant_index = index;
15358 else if (field.variant.default_branch)
15359 di->default_index = index;
15360 else
15361 di->discriminants[index] = field.variant.discriminant_value;
15362 }
15363 }
15364
15365 /* Copy the saved-up fields into the field vector. */
15366 for (int i = 0; i < nfields; ++i)
15367 {
15368 struct nextfield &field
15369 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15370 : fip->fields[i - fip->baseclasses.size ()]);
15371
15372 TYPE_FIELD (type, i) = field.field;
15373 switch (field.accessibility)
15374 {
15375 case DW_ACCESS_private:
15376 if (cu->language != language_ada)
15377 SET_TYPE_FIELD_PRIVATE (type, i);
15378 break;
15379
15380 case DW_ACCESS_protected:
15381 if (cu->language != language_ada)
15382 SET_TYPE_FIELD_PROTECTED (type, i);
15383 break;
15384
15385 case DW_ACCESS_public:
15386 break;
15387
15388 default:
15389 /* Unknown accessibility. Complain and treat it as public. */
15390 {
15391 complaint (_("unsupported accessibility %d"),
15392 field.accessibility);
15393 }
15394 break;
15395 }
15396 if (i < fip->baseclasses.size ())
15397 {
15398 switch (field.virtuality)
15399 {
15400 case DW_VIRTUALITY_virtual:
15401 case DW_VIRTUALITY_pure_virtual:
15402 if (cu->language == language_ada)
15403 error (_("unexpected virtuality in component of Ada type"));
15404 SET_TYPE_FIELD_VIRTUAL (type, i);
15405 break;
15406 }
15407 }
15408 }
15409 }
15410
15411 /* Return true if this member function is a constructor, false
15412 otherwise. */
15413
15414 static int
15415 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15416 {
15417 const char *fieldname;
15418 const char *type_name;
15419 int len;
15420
15421 if (die->parent == NULL)
15422 return 0;
15423
15424 if (die->parent->tag != DW_TAG_structure_type
15425 && die->parent->tag != DW_TAG_union_type
15426 && die->parent->tag != DW_TAG_class_type)
15427 return 0;
15428
15429 fieldname = dwarf2_name (die, cu);
15430 type_name = dwarf2_name (die->parent, cu);
15431 if (fieldname == NULL || type_name == NULL)
15432 return 0;
15433
15434 len = strlen (fieldname);
15435 return (strncmp (fieldname, type_name, len) == 0
15436 && (type_name[len] == '\0' || type_name[len] == '<'));
15437 }
15438
15439 /* Add a member function to the proper fieldlist. */
15440
15441 static void
15442 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15443 struct type *type, struct dwarf2_cu *cu)
15444 {
15445 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15446 struct attribute *attr;
15447 int i;
15448 struct fnfieldlist *flp = nullptr;
15449 struct fn_field *fnp;
15450 const char *fieldname;
15451 struct type *this_type;
15452 enum dwarf_access_attribute accessibility;
15453
15454 if (cu->language == language_ada)
15455 error (_("unexpected member function in Ada type"));
15456
15457 /* Get name of member function. */
15458 fieldname = dwarf2_name (die, cu);
15459 if (fieldname == NULL)
15460 return;
15461
15462 /* Look up member function name in fieldlist. */
15463 for (i = 0; i < fip->fnfieldlists.size (); i++)
15464 {
15465 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15466 {
15467 flp = &fip->fnfieldlists[i];
15468 break;
15469 }
15470 }
15471
15472 /* Create a new fnfieldlist if necessary. */
15473 if (flp == nullptr)
15474 {
15475 fip->fnfieldlists.emplace_back ();
15476 flp = &fip->fnfieldlists.back ();
15477 flp->name = fieldname;
15478 i = fip->fnfieldlists.size () - 1;
15479 }
15480
15481 /* Create a new member function field and add it to the vector of
15482 fnfieldlists. */
15483 flp->fnfields.emplace_back ();
15484 fnp = &flp->fnfields.back ();
15485
15486 /* Delay processing of the physname until later. */
15487 if (cu->language == language_cplus)
15488 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15489 die, cu);
15490 else
15491 {
15492 const char *physname = dwarf2_physname (fieldname, die, cu);
15493 fnp->physname = physname ? physname : "";
15494 }
15495
15496 fnp->type = alloc_type (objfile);
15497 this_type = read_type_die (die, cu);
15498 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15499 {
15500 int nparams = TYPE_NFIELDS (this_type);
15501
15502 /* TYPE is the domain of this method, and THIS_TYPE is the type
15503 of the method itself (TYPE_CODE_METHOD). */
15504 smash_to_method_type (fnp->type, type,
15505 TYPE_TARGET_TYPE (this_type),
15506 TYPE_FIELDS (this_type),
15507 TYPE_NFIELDS (this_type),
15508 TYPE_VARARGS (this_type));
15509
15510 /* Handle static member functions.
15511 Dwarf2 has no clean way to discern C++ static and non-static
15512 member functions. G++ helps GDB by marking the first
15513 parameter for non-static member functions (which is the this
15514 pointer) as artificial. We obtain this information from
15515 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15516 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15517 fnp->voffset = VOFFSET_STATIC;
15518 }
15519 else
15520 complaint (_("member function type missing for '%s'"),
15521 dwarf2_full_name (fieldname, die, cu));
15522
15523 /* Get fcontext from DW_AT_containing_type if present. */
15524 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15525 fnp->fcontext = die_containing_type (die, cu);
15526
15527 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15528 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15529
15530 /* Get accessibility. */
15531 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15532 if (attr)
15533 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15534 else
15535 accessibility = dwarf2_default_access_attribute (die, cu);
15536 switch (accessibility)
15537 {
15538 case DW_ACCESS_private:
15539 fnp->is_private = 1;
15540 break;
15541 case DW_ACCESS_protected:
15542 fnp->is_protected = 1;
15543 break;
15544 }
15545
15546 /* Check for artificial methods. */
15547 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15548 if (attr && DW_UNSND (attr) != 0)
15549 fnp->is_artificial = 1;
15550
15551 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15552
15553 /* Get index in virtual function table if it is a virtual member
15554 function. For older versions of GCC, this is an offset in the
15555 appropriate virtual table, as specified by DW_AT_containing_type.
15556 For everyone else, it is an expression to be evaluated relative
15557 to the object address. */
15558
15559 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15560 if (attr)
15561 {
15562 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15563 {
15564 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15565 {
15566 /* Old-style GCC. */
15567 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15568 }
15569 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15570 || (DW_BLOCK (attr)->size > 1
15571 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15572 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15573 {
15574 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15575 if ((fnp->voffset % cu->header.addr_size) != 0)
15576 dwarf2_complex_location_expr_complaint ();
15577 else
15578 fnp->voffset /= cu->header.addr_size;
15579 fnp->voffset += 2;
15580 }
15581 else
15582 dwarf2_complex_location_expr_complaint ();
15583
15584 if (!fnp->fcontext)
15585 {
15586 /* If there is no `this' field and no DW_AT_containing_type,
15587 we cannot actually find a base class context for the
15588 vtable! */
15589 if (TYPE_NFIELDS (this_type) == 0
15590 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15591 {
15592 complaint (_("cannot determine context for virtual member "
15593 "function \"%s\" (offset %s)"),
15594 fieldname, sect_offset_str (die->sect_off));
15595 }
15596 else
15597 {
15598 fnp->fcontext
15599 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15600 }
15601 }
15602 }
15603 else if (attr_form_is_section_offset (attr))
15604 {
15605 dwarf2_complex_location_expr_complaint ();
15606 }
15607 else
15608 {
15609 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15610 fieldname);
15611 }
15612 }
15613 else
15614 {
15615 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15616 if (attr && DW_UNSND (attr))
15617 {
15618 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15619 complaint (_("Member function \"%s\" (offset %s) is virtual "
15620 "but the vtable offset is not specified"),
15621 fieldname, sect_offset_str (die->sect_off));
15622 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15623 TYPE_CPLUS_DYNAMIC (type) = 1;
15624 }
15625 }
15626 }
15627
15628 /* Create the vector of member function fields, and attach it to the type. */
15629
15630 static void
15631 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15632 struct dwarf2_cu *cu)
15633 {
15634 if (cu->language == language_ada)
15635 error (_("unexpected member functions in Ada type"));
15636
15637 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15638 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15639 TYPE_ALLOC (type,
15640 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15641
15642 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15643 {
15644 struct fnfieldlist &nf = fip->fnfieldlists[i];
15645 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15646
15647 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15648 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15649 fn_flp->fn_fields = (struct fn_field *)
15650 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15651
15652 for (int k = 0; k < nf.fnfields.size (); ++k)
15653 fn_flp->fn_fields[k] = nf.fnfields[k];
15654 }
15655
15656 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15657 }
15658
15659 /* Returns non-zero if NAME is the name of a vtable member in CU's
15660 language, zero otherwise. */
15661 static int
15662 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15663 {
15664 static const char vptr[] = "_vptr";
15665
15666 /* Look for the C++ form of the vtable. */
15667 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15668 return 1;
15669
15670 return 0;
15671 }
15672
15673 /* GCC outputs unnamed structures that are really pointers to member
15674 functions, with the ABI-specified layout. If TYPE describes
15675 such a structure, smash it into a member function type.
15676
15677 GCC shouldn't do this; it should just output pointer to member DIEs.
15678 This is GCC PR debug/28767. */
15679
15680 static void
15681 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15682 {
15683 struct type *pfn_type, *self_type, *new_type;
15684
15685 /* Check for a structure with no name and two children. */
15686 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15687 return;
15688
15689 /* Check for __pfn and __delta members. */
15690 if (TYPE_FIELD_NAME (type, 0) == NULL
15691 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15692 || TYPE_FIELD_NAME (type, 1) == NULL
15693 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15694 return;
15695
15696 /* Find the type of the method. */
15697 pfn_type = TYPE_FIELD_TYPE (type, 0);
15698 if (pfn_type == NULL
15699 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15700 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15701 return;
15702
15703 /* Look for the "this" argument. */
15704 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15705 if (TYPE_NFIELDS (pfn_type) == 0
15706 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15707 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15708 return;
15709
15710 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15711 new_type = alloc_type (objfile);
15712 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15713 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15714 TYPE_VARARGS (pfn_type));
15715 smash_to_methodptr_type (type, new_type);
15716 }
15717
15718 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15719 appropriate error checking and issuing complaints if there is a
15720 problem. */
15721
15722 static ULONGEST
15723 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15724 {
15725 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15726
15727 if (attr == nullptr)
15728 return 0;
15729
15730 if (!attr_form_is_constant (attr))
15731 {
15732 complaint (_("DW_AT_alignment must have constant form"
15733 " - DIE at %s [in module %s]"),
15734 sect_offset_str (die->sect_off),
15735 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15736 return 0;
15737 }
15738
15739 ULONGEST align;
15740 if (attr->form == DW_FORM_sdata)
15741 {
15742 LONGEST val = DW_SND (attr);
15743 if (val < 0)
15744 {
15745 complaint (_("DW_AT_alignment value must not be negative"
15746 " - DIE at %s [in module %s]"),
15747 sect_offset_str (die->sect_off),
15748 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15749 return 0;
15750 }
15751 align = val;
15752 }
15753 else
15754 align = DW_UNSND (attr);
15755
15756 if (align == 0)
15757 {
15758 complaint (_("DW_AT_alignment value must not be zero"
15759 " - DIE at %s [in module %s]"),
15760 sect_offset_str (die->sect_off),
15761 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15762 return 0;
15763 }
15764 if ((align & (align - 1)) != 0)
15765 {
15766 complaint (_("DW_AT_alignment value must be a power of 2"
15767 " - DIE at %s [in module %s]"),
15768 sect_offset_str (die->sect_off),
15769 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15770 return 0;
15771 }
15772
15773 return align;
15774 }
15775
15776 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15777 the alignment for TYPE. */
15778
15779 static void
15780 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15781 struct type *type)
15782 {
15783 if (!set_type_align (type, get_alignment (cu, die)))
15784 complaint (_("DW_AT_alignment value too large"
15785 " - DIE at %s [in module %s]"),
15786 sect_offset_str (die->sect_off),
15787 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15788 }
15789
15790 /* Called when we find the DIE that starts a structure or union scope
15791 (definition) to create a type for the structure or union. Fill in
15792 the type's name and general properties; the members will not be
15793 processed until process_structure_scope. A symbol table entry for
15794 the type will also not be done until process_structure_scope (assuming
15795 the type has a name).
15796
15797 NOTE: we need to call these functions regardless of whether or not the
15798 DIE has a DW_AT_name attribute, since it might be an anonymous
15799 structure or union. This gets the type entered into our set of
15800 user defined types. */
15801
15802 static struct type *
15803 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15804 {
15805 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15806 struct type *type;
15807 struct attribute *attr;
15808 const char *name;
15809
15810 /* If the definition of this type lives in .debug_types, read that type.
15811 Don't follow DW_AT_specification though, that will take us back up
15812 the chain and we want to go down. */
15813 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15814 if (attr)
15815 {
15816 type = get_DW_AT_signature_type (die, attr, cu);
15817
15818 /* The type's CU may not be the same as CU.
15819 Ensure TYPE is recorded with CU in die_type_hash. */
15820 return set_die_type (die, type, cu);
15821 }
15822
15823 type = alloc_type (objfile);
15824 INIT_CPLUS_SPECIFIC (type);
15825
15826 name = dwarf2_name (die, cu);
15827 if (name != NULL)
15828 {
15829 if (cu->language == language_cplus
15830 || cu->language == language_d
15831 || cu->language == language_rust)
15832 {
15833 const char *full_name = dwarf2_full_name (name, die, cu);
15834
15835 /* dwarf2_full_name might have already finished building the DIE's
15836 type. If so, there is no need to continue. */
15837 if (get_die_type (die, cu) != NULL)
15838 return get_die_type (die, cu);
15839
15840 TYPE_NAME (type) = full_name;
15841 }
15842 else
15843 {
15844 /* The name is already allocated along with this objfile, so
15845 we don't need to duplicate it for the type. */
15846 TYPE_NAME (type) = name;
15847 }
15848 }
15849
15850 if (die->tag == DW_TAG_structure_type)
15851 {
15852 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15853 }
15854 else if (die->tag == DW_TAG_union_type)
15855 {
15856 TYPE_CODE (type) = TYPE_CODE_UNION;
15857 }
15858 else if (die->tag == DW_TAG_variant_part)
15859 {
15860 TYPE_CODE (type) = TYPE_CODE_UNION;
15861 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15862 }
15863 else
15864 {
15865 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15866 }
15867
15868 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15869 TYPE_DECLARED_CLASS (type) = 1;
15870
15871 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15872 if (attr)
15873 {
15874 if (attr_form_is_constant (attr))
15875 TYPE_LENGTH (type) = DW_UNSND (attr);
15876 else
15877 {
15878 /* For the moment, dynamic type sizes are not supported
15879 by GDB's struct type. The actual size is determined
15880 on-demand when resolving the type of a given object,
15881 so set the type's length to zero for now. Otherwise,
15882 we record an expression as the length, and that expression
15883 could lead to a very large value, which could eventually
15884 lead to us trying to allocate that much memory when creating
15885 a value of that type. */
15886 TYPE_LENGTH (type) = 0;
15887 }
15888 }
15889 else
15890 {
15891 TYPE_LENGTH (type) = 0;
15892 }
15893
15894 maybe_set_alignment (cu, die, type);
15895
15896 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15897 {
15898 /* ICC<14 does not output the required DW_AT_declaration on
15899 incomplete types, but gives them a size of zero. */
15900 TYPE_STUB (type) = 1;
15901 }
15902 else
15903 TYPE_STUB_SUPPORTED (type) = 1;
15904
15905 if (die_is_declaration (die, cu))
15906 TYPE_STUB (type) = 1;
15907 else if (attr == NULL && die->child == NULL
15908 && producer_is_realview (cu->producer))
15909 /* RealView does not output the required DW_AT_declaration
15910 on incomplete types. */
15911 TYPE_STUB (type) = 1;
15912
15913 /* We need to add the type field to the die immediately so we don't
15914 infinitely recurse when dealing with pointers to the structure
15915 type within the structure itself. */
15916 set_die_type (die, type, cu);
15917
15918 /* set_die_type should be already done. */
15919 set_descriptive_type (type, die, cu);
15920
15921 return type;
15922 }
15923
15924 /* A helper for process_structure_scope that handles a single member
15925 DIE. */
15926
15927 static void
15928 handle_struct_member_die (struct die_info *child_die, struct type *type,
15929 struct field_info *fi,
15930 std::vector<struct symbol *> *template_args,
15931 struct dwarf2_cu *cu)
15932 {
15933 if (child_die->tag == DW_TAG_member
15934 || child_die->tag == DW_TAG_variable
15935 || child_die->tag == DW_TAG_variant_part)
15936 {
15937 /* NOTE: carlton/2002-11-05: A C++ static data member
15938 should be a DW_TAG_member that is a declaration, but
15939 all versions of G++ as of this writing (so through at
15940 least 3.2.1) incorrectly generate DW_TAG_variable
15941 tags for them instead. */
15942 dwarf2_add_field (fi, child_die, cu);
15943 }
15944 else if (child_die->tag == DW_TAG_subprogram)
15945 {
15946 /* Rust doesn't have member functions in the C++ sense.
15947 However, it does emit ordinary functions as children
15948 of a struct DIE. */
15949 if (cu->language == language_rust)
15950 read_func_scope (child_die, cu);
15951 else
15952 {
15953 /* C++ member function. */
15954 dwarf2_add_member_fn (fi, child_die, type, cu);
15955 }
15956 }
15957 else if (child_die->tag == DW_TAG_inheritance)
15958 {
15959 /* C++ base class field. */
15960 dwarf2_add_field (fi, child_die, cu);
15961 }
15962 else if (type_can_define_types (child_die))
15963 dwarf2_add_type_defn (fi, child_die, cu);
15964 else if (child_die->tag == DW_TAG_template_type_param
15965 || child_die->tag == DW_TAG_template_value_param)
15966 {
15967 struct symbol *arg = new_symbol (child_die, NULL, cu);
15968
15969 if (arg != NULL)
15970 template_args->push_back (arg);
15971 }
15972 else if (child_die->tag == DW_TAG_variant)
15973 {
15974 /* In a variant we want to get the discriminant and also add a
15975 field for our sole member child. */
15976 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15977
15978 for (die_info *variant_child = child_die->child;
15979 variant_child != NULL;
15980 variant_child = sibling_die (variant_child))
15981 {
15982 if (variant_child->tag == DW_TAG_member)
15983 {
15984 handle_struct_member_die (variant_child, type, fi,
15985 template_args, cu);
15986 /* Only handle the one. */
15987 break;
15988 }
15989 }
15990
15991 /* We don't handle this but we might as well report it if we see
15992 it. */
15993 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15994 complaint (_("DW_AT_discr_list is not supported yet"
15995 " - DIE at %s [in module %s]"),
15996 sect_offset_str (child_die->sect_off),
15997 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15998
15999 /* The first field was just added, so we can stash the
16000 discriminant there. */
16001 gdb_assert (!fi->fields.empty ());
16002 if (discr == NULL)
16003 fi->fields.back ().variant.default_branch = true;
16004 else
16005 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
16006 }
16007 }
16008
16009 /* Finish creating a structure or union type, including filling in
16010 its members and creating a symbol for it. */
16011
16012 static void
16013 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16014 {
16015 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16016 struct die_info *child_die;
16017 struct type *type;
16018
16019 type = get_die_type (die, cu);
16020 if (type == NULL)
16021 type = read_structure_type (die, cu);
16022
16023 /* When reading a DW_TAG_variant_part, we need to notice when we
16024 read the discriminant member, so we can record it later in the
16025 discriminant_info. */
16026 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16027 sect_offset discr_offset;
16028 bool has_template_parameters = false;
16029
16030 if (is_variant_part)
16031 {
16032 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16033 if (discr == NULL)
16034 {
16035 /* Maybe it's a univariant form, an extension we support.
16036 In this case arrange not to check the offset. */
16037 is_variant_part = false;
16038 }
16039 else if (attr_form_is_ref (discr))
16040 {
16041 struct dwarf2_cu *target_cu = cu;
16042 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16043
16044 discr_offset = target_die->sect_off;
16045 }
16046 else
16047 {
16048 complaint (_("DW_AT_discr does not have DIE reference form"
16049 " - DIE at %s [in module %s]"),
16050 sect_offset_str (die->sect_off),
16051 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16052 is_variant_part = false;
16053 }
16054 }
16055
16056 if (die->child != NULL && ! die_is_declaration (die, cu))
16057 {
16058 struct field_info fi;
16059 std::vector<struct symbol *> template_args;
16060
16061 child_die = die->child;
16062
16063 while (child_die && child_die->tag)
16064 {
16065 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16066
16067 if (is_variant_part && discr_offset == child_die->sect_off)
16068 fi.fields.back ().variant.is_discriminant = true;
16069
16070 child_die = sibling_die (child_die);
16071 }
16072
16073 /* Attach template arguments to type. */
16074 if (!template_args.empty ())
16075 {
16076 has_template_parameters = true;
16077 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16078 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16079 TYPE_TEMPLATE_ARGUMENTS (type)
16080 = XOBNEWVEC (&objfile->objfile_obstack,
16081 struct symbol *,
16082 TYPE_N_TEMPLATE_ARGUMENTS (type));
16083 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16084 template_args.data (),
16085 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16086 * sizeof (struct symbol *)));
16087 }
16088
16089 /* Attach fields and member functions to the type. */
16090 if (fi.nfields)
16091 dwarf2_attach_fields_to_type (&fi, type, cu);
16092 if (!fi.fnfieldlists.empty ())
16093 {
16094 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16095
16096 /* Get the type which refers to the base class (possibly this
16097 class itself) which contains the vtable pointer for the current
16098 class from the DW_AT_containing_type attribute. This use of
16099 DW_AT_containing_type is a GNU extension. */
16100
16101 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16102 {
16103 struct type *t = die_containing_type (die, cu);
16104
16105 set_type_vptr_basetype (type, t);
16106 if (type == t)
16107 {
16108 int i;
16109
16110 /* Our own class provides vtbl ptr. */
16111 for (i = TYPE_NFIELDS (t) - 1;
16112 i >= TYPE_N_BASECLASSES (t);
16113 --i)
16114 {
16115 const char *fieldname = TYPE_FIELD_NAME (t, i);
16116
16117 if (is_vtable_name (fieldname, cu))
16118 {
16119 set_type_vptr_fieldno (type, i);
16120 break;
16121 }
16122 }
16123
16124 /* Complain if virtual function table field not found. */
16125 if (i < TYPE_N_BASECLASSES (t))
16126 complaint (_("virtual function table pointer "
16127 "not found when defining class '%s'"),
16128 TYPE_NAME (type) ? TYPE_NAME (type) : "");
16129 }
16130 else
16131 {
16132 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16133 }
16134 }
16135 else if (cu->producer
16136 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16137 {
16138 /* The IBM XLC compiler does not provide direct indication
16139 of the containing type, but the vtable pointer is
16140 always named __vfp. */
16141
16142 int i;
16143
16144 for (i = TYPE_NFIELDS (type) - 1;
16145 i >= TYPE_N_BASECLASSES (type);
16146 --i)
16147 {
16148 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16149 {
16150 set_type_vptr_fieldno (type, i);
16151 set_type_vptr_basetype (type, type);
16152 break;
16153 }
16154 }
16155 }
16156 }
16157
16158 /* Copy fi.typedef_field_list linked list elements content into the
16159 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16160 if (!fi.typedef_field_list.empty ())
16161 {
16162 int count = fi.typedef_field_list.size ();
16163
16164 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16165 TYPE_TYPEDEF_FIELD_ARRAY (type)
16166 = ((struct decl_field *)
16167 TYPE_ALLOC (type,
16168 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16169 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16170
16171 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16172 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16173 }
16174
16175 /* Copy fi.nested_types_list linked list elements content into the
16176 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16177 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16178 {
16179 int count = fi.nested_types_list.size ();
16180
16181 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16182 TYPE_NESTED_TYPES_ARRAY (type)
16183 = ((struct decl_field *)
16184 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16185 TYPE_NESTED_TYPES_COUNT (type) = count;
16186
16187 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16188 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16189 }
16190 }
16191
16192 quirk_gcc_member_function_pointer (type, objfile);
16193 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16194 cu->rust_unions.push_back (type);
16195
16196 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16197 snapshots) has been known to create a die giving a declaration
16198 for a class that has, as a child, a die giving a definition for a
16199 nested class. So we have to process our children even if the
16200 current die is a declaration. Normally, of course, a declaration
16201 won't have any children at all. */
16202
16203 child_die = die->child;
16204
16205 while (child_die != NULL && child_die->tag)
16206 {
16207 if (child_die->tag == DW_TAG_member
16208 || child_die->tag == DW_TAG_variable
16209 || child_die->tag == DW_TAG_inheritance
16210 || child_die->tag == DW_TAG_template_value_param
16211 || child_die->tag == DW_TAG_template_type_param)
16212 {
16213 /* Do nothing. */
16214 }
16215 else
16216 process_die (child_die, cu);
16217
16218 child_die = sibling_die (child_die);
16219 }
16220
16221 /* Do not consider external references. According to the DWARF standard,
16222 these DIEs are identified by the fact that they have no byte_size
16223 attribute, and a declaration attribute. */
16224 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16225 || !die_is_declaration (die, cu))
16226 {
16227 struct symbol *sym = new_symbol (die, type, cu);
16228
16229 if (has_template_parameters)
16230 {
16231 struct symtab *symtab;
16232 if (sym != nullptr)
16233 symtab = symbol_symtab (sym);
16234 else if (cu->line_header != nullptr)
16235 {
16236 /* Any related symtab will do. */
16237 symtab
16238 = cu->line_header->file_names ()[0].symtab;
16239 }
16240 else
16241 {
16242 symtab = nullptr;
16243 complaint (_("could not find suitable "
16244 "symtab for template parameter"
16245 " - DIE at %s [in module %s]"),
16246 sect_offset_str (die->sect_off),
16247 objfile_name (objfile));
16248 }
16249
16250 if (symtab != nullptr)
16251 {
16252 /* Make sure that the symtab is set on the new symbols.
16253 Even though they don't appear in this symtab directly,
16254 other parts of gdb assume that symbols do, and this is
16255 reasonably true. */
16256 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16257 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16258 }
16259 }
16260 }
16261 }
16262
16263 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16264 update TYPE using some information only available in DIE's children. */
16265
16266 static void
16267 update_enumeration_type_from_children (struct die_info *die,
16268 struct type *type,
16269 struct dwarf2_cu *cu)
16270 {
16271 struct die_info *child_die;
16272 int unsigned_enum = 1;
16273 int flag_enum = 1;
16274 ULONGEST mask = 0;
16275
16276 auto_obstack obstack;
16277
16278 for (child_die = die->child;
16279 child_die != NULL && child_die->tag;
16280 child_die = sibling_die (child_die))
16281 {
16282 struct attribute *attr;
16283 LONGEST value;
16284 const gdb_byte *bytes;
16285 struct dwarf2_locexpr_baton *baton;
16286 const char *name;
16287
16288 if (child_die->tag != DW_TAG_enumerator)
16289 continue;
16290
16291 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16292 if (attr == NULL)
16293 continue;
16294
16295 name = dwarf2_name (child_die, cu);
16296 if (name == NULL)
16297 name = "<anonymous enumerator>";
16298
16299 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16300 &value, &bytes, &baton);
16301 if (value < 0)
16302 {
16303 unsigned_enum = 0;
16304 flag_enum = 0;
16305 }
16306 else if ((mask & value) != 0)
16307 flag_enum = 0;
16308 else
16309 mask |= value;
16310
16311 /* If we already know that the enum type is neither unsigned, nor
16312 a flag type, no need to look at the rest of the enumerates. */
16313 if (!unsigned_enum && !flag_enum)
16314 break;
16315 }
16316
16317 if (unsigned_enum)
16318 TYPE_UNSIGNED (type) = 1;
16319 if (flag_enum)
16320 TYPE_FLAG_ENUM (type) = 1;
16321 }
16322
16323 /* Given a DW_AT_enumeration_type die, set its type. We do not
16324 complete the type's fields yet, or create any symbols. */
16325
16326 static struct type *
16327 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16328 {
16329 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16330 struct type *type;
16331 struct attribute *attr;
16332 const char *name;
16333
16334 /* If the definition of this type lives in .debug_types, read that type.
16335 Don't follow DW_AT_specification though, that will take us back up
16336 the chain and we want to go down. */
16337 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16338 if (attr)
16339 {
16340 type = get_DW_AT_signature_type (die, attr, cu);
16341
16342 /* The type's CU may not be the same as CU.
16343 Ensure TYPE is recorded with CU in die_type_hash. */
16344 return set_die_type (die, type, cu);
16345 }
16346
16347 type = alloc_type (objfile);
16348
16349 TYPE_CODE (type) = TYPE_CODE_ENUM;
16350 name = dwarf2_full_name (NULL, die, cu);
16351 if (name != NULL)
16352 TYPE_NAME (type) = name;
16353
16354 attr = dwarf2_attr (die, DW_AT_type, cu);
16355 if (attr != NULL)
16356 {
16357 struct type *underlying_type = die_type (die, cu);
16358
16359 TYPE_TARGET_TYPE (type) = underlying_type;
16360 }
16361
16362 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16363 if (attr)
16364 {
16365 TYPE_LENGTH (type) = DW_UNSND (attr);
16366 }
16367 else
16368 {
16369 TYPE_LENGTH (type) = 0;
16370 }
16371
16372 maybe_set_alignment (cu, die, type);
16373
16374 /* The enumeration DIE can be incomplete. In Ada, any type can be
16375 declared as private in the package spec, and then defined only
16376 inside the package body. Such types are known as Taft Amendment
16377 Types. When another package uses such a type, an incomplete DIE
16378 may be generated by the compiler. */
16379 if (die_is_declaration (die, cu))
16380 TYPE_STUB (type) = 1;
16381
16382 /* Finish the creation of this type by using the enum's children.
16383 We must call this even when the underlying type has been provided
16384 so that we can determine if we're looking at a "flag" enum. */
16385 update_enumeration_type_from_children (die, type, cu);
16386
16387 /* If this type has an underlying type that is not a stub, then we
16388 may use its attributes. We always use the "unsigned" attribute
16389 in this situation, because ordinarily we guess whether the type
16390 is unsigned -- but the guess can be wrong and the underlying type
16391 can tell us the reality. However, we defer to a local size
16392 attribute if one exists, because this lets the compiler override
16393 the underlying type if needed. */
16394 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16395 {
16396 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16397 if (TYPE_LENGTH (type) == 0)
16398 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16399 if (TYPE_RAW_ALIGN (type) == 0
16400 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16401 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16402 }
16403
16404 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16405
16406 return set_die_type (die, type, cu);
16407 }
16408
16409 /* Given a pointer to a die which begins an enumeration, process all
16410 the dies that define the members of the enumeration, and create the
16411 symbol for the enumeration type.
16412
16413 NOTE: We reverse the order of the element list. */
16414
16415 static void
16416 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16417 {
16418 struct type *this_type;
16419
16420 this_type = get_die_type (die, cu);
16421 if (this_type == NULL)
16422 this_type = read_enumeration_type (die, cu);
16423
16424 if (die->child != NULL)
16425 {
16426 struct die_info *child_die;
16427 struct symbol *sym;
16428 struct field *fields = NULL;
16429 int num_fields = 0;
16430 const char *name;
16431
16432 child_die = die->child;
16433 while (child_die && child_die->tag)
16434 {
16435 if (child_die->tag != DW_TAG_enumerator)
16436 {
16437 process_die (child_die, cu);
16438 }
16439 else
16440 {
16441 name = dwarf2_name (child_die, cu);
16442 if (name)
16443 {
16444 sym = new_symbol (child_die, this_type, cu);
16445
16446 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16447 {
16448 fields = (struct field *)
16449 xrealloc (fields,
16450 (num_fields + DW_FIELD_ALLOC_CHUNK)
16451 * sizeof (struct field));
16452 }
16453
16454 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16455 FIELD_TYPE (fields[num_fields]) = NULL;
16456 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16457 FIELD_BITSIZE (fields[num_fields]) = 0;
16458
16459 num_fields++;
16460 }
16461 }
16462
16463 child_die = sibling_die (child_die);
16464 }
16465
16466 if (num_fields)
16467 {
16468 TYPE_NFIELDS (this_type) = num_fields;
16469 TYPE_FIELDS (this_type) = (struct field *)
16470 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16471 memcpy (TYPE_FIELDS (this_type), fields,
16472 sizeof (struct field) * num_fields);
16473 xfree (fields);
16474 }
16475 }
16476
16477 /* If we are reading an enum from a .debug_types unit, and the enum
16478 is a declaration, and the enum is not the signatured type in the
16479 unit, then we do not want to add a symbol for it. Adding a
16480 symbol would in some cases obscure the true definition of the
16481 enum, giving users an incomplete type when the definition is
16482 actually available. Note that we do not want to do this for all
16483 enums which are just declarations, because C++0x allows forward
16484 enum declarations. */
16485 if (cu->per_cu->is_debug_types
16486 && die_is_declaration (die, cu))
16487 {
16488 struct signatured_type *sig_type;
16489
16490 sig_type = (struct signatured_type *) cu->per_cu;
16491 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16492 if (sig_type->type_offset_in_section != die->sect_off)
16493 return;
16494 }
16495
16496 new_symbol (die, this_type, cu);
16497 }
16498
16499 /* Extract all information from a DW_TAG_array_type DIE and put it in
16500 the DIE's type field. For now, this only handles one dimensional
16501 arrays. */
16502
16503 static struct type *
16504 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16505 {
16506 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16507 struct die_info *child_die;
16508 struct type *type;
16509 struct type *element_type, *range_type, *index_type;
16510 struct attribute *attr;
16511 const char *name;
16512 struct dynamic_prop *byte_stride_prop = NULL;
16513 unsigned int bit_stride = 0;
16514
16515 element_type = die_type (die, cu);
16516
16517 /* The die_type call above may have already set the type for this DIE. */
16518 type = get_die_type (die, cu);
16519 if (type)
16520 return type;
16521
16522 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16523 if (attr != NULL)
16524 {
16525 int stride_ok;
16526 struct type *prop_type
16527 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
16528
16529 byte_stride_prop
16530 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16531 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16532 prop_type);
16533 if (!stride_ok)
16534 {
16535 complaint (_("unable to read array DW_AT_byte_stride "
16536 " - DIE at %s [in module %s]"),
16537 sect_offset_str (die->sect_off),
16538 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16539 /* Ignore this attribute. We will likely not be able to print
16540 arrays of this type correctly, but there is little we can do
16541 to help if we cannot read the attribute's value. */
16542 byte_stride_prop = NULL;
16543 }
16544 }
16545
16546 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16547 if (attr != NULL)
16548 bit_stride = DW_UNSND (attr);
16549
16550 /* Irix 6.2 native cc creates array types without children for
16551 arrays with unspecified length. */
16552 if (die->child == NULL)
16553 {
16554 index_type = objfile_type (objfile)->builtin_int;
16555 range_type = create_static_range_type (NULL, index_type, 0, -1);
16556 type = create_array_type_with_stride (NULL, element_type, range_type,
16557 byte_stride_prop, bit_stride);
16558 return set_die_type (die, type, cu);
16559 }
16560
16561 std::vector<struct type *> range_types;
16562 child_die = die->child;
16563 while (child_die && child_die->tag)
16564 {
16565 if (child_die->tag == DW_TAG_subrange_type)
16566 {
16567 struct type *child_type = read_type_die (child_die, cu);
16568
16569 if (child_type != NULL)
16570 {
16571 /* The range type was succesfully read. Save it for the
16572 array type creation. */
16573 range_types.push_back (child_type);
16574 }
16575 }
16576 child_die = sibling_die (child_die);
16577 }
16578
16579 /* Dwarf2 dimensions are output from left to right, create the
16580 necessary array types in backwards order. */
16581
16582 type = element_type;
16583
16584 if (read_array_order (die, cu) == DW_ORD_col_major)
16585 {
16586 int i = 0;
16587
16588 while (i < range_types.size ())
16589 type = create_array_type_with_stride (NULL, type, range_types[i++],
16590 byte_stride_prop, bit_stride);
16591 }
16592 else
16593 {
16594 size_t ndim = range_types.size ();
16595 while (ndim-- > 0)
16596 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16597 byte_stride_prop, bit_stride);
16598 }
16599
16600 /* Understand Dwarf2 support for vector types (like they occur on
16601 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16602 array type. This is not part of the Dwarf2/3 standard yet, but a
16603 custom vendor extension. The main difference between a regular
16604 array and the vector variant is that vectors are passed by value
16605 to functions. */
16606 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16607 if (attr)
16608 make_vector_type (type);
16609
16610 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16611 implementation may choose to implement triple vectors using this
16612 attribute. */
16613 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16614 if (attr)
16615 {
16616 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16617 TYPE_LENGTH (type) = DW_UNSND (attr);
16618 else
16619 complaint (_("DW_AT_byte_size for array type smaller "
16620 "than the total size of elements"));
16621 }
16622
16623 name = dwarf2_name (die, cu);
16624 if (name)
16625 TYPE_NAME (type) = name;
16626
16627 maybe_set_alignment (cu, die, type);
16628
16629 /* Install the type in the die. */
16630 set_die_type (die, type, cu);
16631
16632 /* set_die_type should be already done. */
16633 set_descriptive_type (type, die, cu);
16634
16635 return type;
16636 }
16637
16638 static enum dwarf_array_dim_ordering
16639 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16640 {
16641 struct attribute *attr;
16642
16643 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16644
16645 if (attr)
16646 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16647
16648 /* GNU F77 is a special case, as at 08/2004 array type info is the
16649 opposite order to the dwarf2 specification, but data is still
16650 laid out as per normal fortran.
16651
16652 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16653 version checking. */
16654
16655 if (cu->language == language_fortran
16656 && cu->producer && strstr (cu->producer, "GNU F77"))
16657 {
16658 return DW_ORD_row_major;
16659 }
16660
16661 switch (cu->language_defn->la_array_ordering)
16662 {
16663 case array_column_major:
16664 return DW_ORD_col_major;
16665 case array_row_major:
16666 default:
16667 return DW_ORD_row_major;
16668 };
16669 }
16670
16671 /* Extract all information from a DW_TAG_set_type DIE and put it in
16672 the DIE's type field. */
16673
16674 static struct type *
16675 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16676 {
16677 struct type *domain_type, *set_type;
16678 struct attribute *attr;
16679
16680 domain_type = die_type (die, cu);
16681
16682 /* The die_type call above may have already set the type for this DIE. */
16683 set_type = get_die_type (die, cu);
16684 if (set_type)
16685 return set_type;
16686
16687 set_type = create_set_type (NULL, domain_type);
16688
16689 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16690 if (attr)
16691 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16692
16693 maybe_set_alignment (cu, die, set_type);
16694
16695 return set_die_type (die, set_type, cu);
16696 }
16697
16698 /* A helper for read_common_block that creates a locexpr baton.
16699 SYM is the symbol which we are marking as computed.
16700 COMMON_DIE is the DIE for the common block.
16701 COMMON_LOC is the location expression attribute for the common
16702 block itself.
16703 MEMBER_LOC is the location expression attribute for the particular
16704 member of the common block that we are processing.
16705 CU is the CU from which the above come. */
16706
16707 static void
16708 mark_common_block_symbol_computed (struct symbol *sym,
16709 struct die_info *common_die,
16710 struct attribute *common_loc,
16711 struct attribute *member_loc,
16712 struct dwarf2_cu *cu)
16713 {
16714 struct dwarf2_per_objfile *dwarf2_per_objfile
16715 = cu->per_cu->dwarf2_per_objfile;
16716 struct objfile *objfile = dwarf2_per_objfile->objfile;
16717 struct dwarf2_locexpr_baton *baton;
16718 gdb_byte *ptr;
16719 unsigned int cu_off;
16720 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16721 LONGEST offset = 0;
16722
16723 gdb_assert (common_loc && member_loc);
16724 gdb_assert (attr_form_is_block (common_loc));
16725 gdb_assert (attr_form_is_block (member_loc)
16726 || attr_form_is_constant (member_loc));
16727
16728 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16729 baton->per_cu = cu->per_cu;
16730 gdb_assert (baton->per_cu);
16731
16732 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16733
16734 if (attr_form_is_constant (member_loc))
16735 {
16736 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16737 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16738 }
16739 else
16740 baton->size += DW_BLOCK (member_loc)->size;
16741
16742 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16743 baton->data = ptr;
16744
16745 *ptr++ = DW_OP_call4;
16746 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16747 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16748 ptr += 4;
16749
16750 if (attr_form_is_constant (member_loc))
16751 {
16752 *ptr++ = DW_OP_addr;
16753 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16754 ptr += cu->header.addr_size;
16755 }
16756 else
16757 {
16758 /* We have to copy the data here, because DW_OP_call4 will only
16759 use a DW_AT_location attribute. */
16760 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16761 ptr += DW_BLOCK (member_loc)->size;
16762 }
16763
16764 *ptr++ = DW_OP_plus;
16765 gdb_assert (ptr - baton->data == baton->size);
16766
16767 SYMBOL_LOCATION_BATON (sym) = baton;
16768 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16769 }
16770
16771 /* Create appropriate locally-scoped variables for all the
16772 DW_TAG_common_block entries. Also create a struct common_block
16773 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16774 is used to separate the common blocks name namespace from regular
16775 variable names. */
16776
16777 static void
16778 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16779 {
16780 struct attribute *attr;
16781
16782 attr = dwarf2_attr (die, DW_AT_location, cu);
16783 if (attr)
16784 {
16785 /* Support the .debug_loc offsets. */
16786 if (attr_form_is_block (attr))
16787 {
16788 /* Ok. */
16789 }
16790 else if (attr_form_is_section_offset (attr))
16791 {
16792 dwarf2_complex_location_expr_complaint ();
16793 attr = NULL;
16794 }
16795 else
16796 {
16797 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16798 "common block member");
16799 attr = NULL;
16800 }
16801 }
16802
16803 if (die->child != NULL)
16804 {
16805 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16806 struct die_info *child_die;
16807 size_t n_entries = 0, size;
16808 struct common_block *common_block;
16809 struct symbol *sym;
16810
16811 for (child_die = die->child;
16812 child_die && child_die->tag;
16813 child_die = sibling_die (child_die))
16814 ++n_entries;
16815
16816 size = (sizeof (struct common_block)
16817 + (n_entries - 1) * sizeof (struct symbol *));
16818 common_block
16819 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16820 size);
16821 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16822 common_block->n_entries = 0;
16823
16824 for (child_die = die->child;
16825 child_die && child_die->tag;
16826 child_die = sibling_die (child_die))
16827 {
16828 /* Create the symbol in the DW_TAG_common_block block in the current
16829 symbol scope. */
16830 sym = new_symbol (child_die, NULL, cu);
16831 if (sym != NULL)
16832 {
16833 struct attribute *member_loc;
16834
16835 common_block->contents[common_block->n_entries++] = sym;
16836
16837 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16838 cu);
16839 if (member_loc)
16840 {
16841 /* GDB has handled this for a long time, but it is
16842 not specified by DWARF. It seems to have been
16843 emitted by gfortran at least as recently as:
16844 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16845 complaint (_("Variable in common block has "
16846 "DW_AT_data_member_location "
16847 "- DIE at %s [in module %s]"),
16848 sect_offset_str (child_die->sect_off),
16849 objfile_name (objfile));
16850
16851 if (attr_form_is_section_offset (member_loc))
16852 dwarf2_complex_location_expr_complaint ();
16853 else if (attr_form_is_constant (member_loc)
16854 || attr_form_is_block (member_loc))
16855 {
16856 if (attr)
16857 mark_common_block_symbol_computed (sym, die, attr,
16858 member_loc, cu);
16859 }
16860 else
16861 dwarf2_complex_location_expr_complaint ();
16862 }
16863 }
16864 }
16865
16866 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16867 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16868 }
16869 }
16870
16871 /* Create a type for a C++ namespace. */
16872
16873 static struct type *
16874 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16875 {
16876 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16877 const char *previous_prefix, *name;
16878 int is_anonymous;
16879 struct type *type;
16880
16881 /* For extensions, reuse the type of the original namespace. */
16882 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16883 {
16884 struct die_info *ext_die;
16885 struct dwarf2_cu *ext_cu = cu;
16886
16887 ext_die = dwarf2_extension (die, &ext_cu);
16888 type = read_type_die (ext_die, ext_cu);
16889
16890 /* EXT_CU may not be the same as CU.
16891 Ensure TYPE is recorded with CU in die_type_hash. */
16892 return set_die_type (die, type, cu);
16893 }
16894
16895 name = namespace_name (die, &is_anonymous, cu);
16896
16897 /* Now build the name of the current namespace. */
16898
16899 previous_prefix = determine_prefix (die, cu);
16900 if (previous_prefix[0] != '\0')
16901 name = typename_concat (&objfile->objfile_obstack,
16902 previous_prefix, name, 0, cu);
16903
16904 /* Create the type. */
16905 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16906
16907 return set_die_type (die, type, cu);
16908 }
16909
16910 /* Read a namespace scope. */
16911
16912 static void
16913 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16914 {
16915 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16916 int is_anonymous;
16917
16918 /* Add a symbol associated to this if we haven't seen the namespace
16919 before. Also, add a using directive if it's an anonymous
16920 namespace. */
16921
16922 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16923 {
16924 struct type *type;
16925
16926 type = read_type_die (die, cu);
16927 new_symbol (die, type, cu);
16928
16929 namespace_name (die, &is_anonymous, cu);
16930 if (is_anonymous)
16931 {
16932 const char *previous_prefix = determine_prefix (die, cu);
16933
16934 std::vector<const char *> excludes;
16935 add_using_directive (using_directives (cu),
16936 previous_prefix, TYPE_NAME (type), NULL,
16937 NULL, excludes, 0, &objfile->objfile_obstack);
16938 }
16939 }
16940
16941 if (die->child != NULL)
16942 {
16943 struct die_info *child_die = die->child;
16944
16945 while (child_die && child_die->tag)
16946 {
16947 process_die (child_die, cu);
16948 child_die = sibling_die (child_die);
16949 }
16950 }
16951 }
16952
16953 /* Read a Fortran module as type. This DIE can be only a declaration used for
16954 imported module. Still we need that type as local Fortran "use ... only"
16955 declaration imports depend on the created type in determine_prefix. */
16956
16957 static struct type *
16958 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16959 {
16960 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16961 const char *module_name;
16962 struct type *type;
16963
16964 module_name = dwarf2_name (die, cu);
16965 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16966
16967 return set_die_type (die, type, cu);
16968 }
16969
16970 /* Read a Fortran module. */
16971
16972 static void
16973 read_module (struct die_info *die, struct dwarf2_cu *cu)
16974 {
16975 struct die_info *child_die = die->child;
16976 struct type *type;
16977
16978 type = read_type_die (die, cu);
16979 new_symbol (die, type, cu);
16980
16981 while (child_die && child_die->tag)
16982 {
16983 process_die (child_die, cu);
16984 child_die = sibling_die (child_die);
16985 }
16986 }
16987
16988 /* Return the name of the namespace represented by DIE. Set
16989 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16990 namespace. */
16991
16992 static const char *
16993 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16994 {
16995 struct die_info *current_die;
16996 const char *name = NULL;
16997
16998 /* Loop through the extensions until we find a name. */
16999
17000 for (current_die = die;
17001 current_die != NULL;
17002 current_die = dwarf2_extension (die, &cu))
17003 {
17004 /* We don't use dwarf2_name here so that we can detect the absence
17005 of a name -> anonymous namespace. */
17006 name = dwarf2_string_attr (die, DW_AT_name, cu);
17007
17008 if (name != NULL)
17009 break;
17010 }
17011
17012 /* Is it an anonymous namespace? */
17013
17014 *is_anonymous = (name == NULL);
17015 if (*is_anonymous)
17016 name = CP_ANONYMOUS_NAMESPACE_STR;
17017
17018 return name;
17019 }
17020
17021 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17022 the user defined type vector. */
17023
17024 static struct type *
17025 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17026 {
17027 struct gdbarch *gdbarch
17028 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
17029 struct comp_unit_head *cu_header = &cu->header;
17030 struct type *type;
17031 struct attribute *attr_byte_size;
17032 struct attribute *attr_address_class;
17033 int byte_size, addr_class;
17034 struct type *target_type;
17035
17036 target_type = die_type (die, cu);
17037
17038 /* The die_type call above may have already set the type for this DIE. */
17039 type = get_die_type (die, cu);
17040 if (type)
17041 return type;
17042
17043 type = lookup_pointer_type (target_type);
17044
17045 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17046 if (attr_byte_size)
17047 byte_size = DW_UNSND (attr_byte_size);
17048 else
17049 byte_size = cu_header->addr_size;
17050
17051 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17052 if (attr_address_class)
17053 addr_class = DW_UNSND (attr_address_class);
17054 else
17055 addr_class = DW_ADDR_none;
17056
17057 ULONGEST alignment = get_alignment (cu, die);
17058
17059 /* If the pointer size, alignment, or address class is different
17060 than the default, create a type variant marked as such and set
17061 the length accordingly. */
17062 if (TYPE_LENGTH (type) != byte_size
17063 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17064 && alignment != TYPE_RAW_ALIGN (type))
17065 || addr_class != DW_ADDR_none)
17066 {
17067 if (gdbarch_address_class_type_flags_p (gdbarch))
17068 {
17069 int type_flags;
17070
17071 type_flags = gdbarch_address_class_type_flags
17072 (gdbarch, byte_size, addr_class);
17073 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17074 == 0);
17075 type = make_type_with_address_space (type, type_flags);
17076 }
17077 else if (TYPE_LENGTH (type) != byte_size)
17078 {
17079 complaint (_("invalid pointer size %d"), byte_size);
17080 }
17081 else if (TYPE_RAW_ALIGN (type) != alignment)
17082 {
17083 complaint (_("Invalid DW_AT_alignment"
17084 " - DIE at %s [in module %s]"),
17085 sect_offset_str (die->sect_off),
17086 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17087 }
17088 else
17089 {
17090 /* Should we also complain about unhandled address classes? */
17091 }
17092 }
17093
17094 TYPE_LENGTH (type) = byte_size;
17095 set_type_align (type, alignment);
17096 return set_die_type (die, type, cu);
17097 }
17098
17099 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17100 the user defined type vector. */
17101
17102 static struct type *
17103 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17104 {
17105 struct type *type;
17106 struct type *to_type;
17107 struct type *domain;
17108
17109 to_type = die_type (die, cu);
17110 domain = die_containing_type (die, cu);
17111
17112 /* The calls above may have already set the type for this DIE. */
17113 type = get_die_type (die, cu);
17114 if (type)
17115 return type;
17116
17117 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17118 type = lookup_methodptr_type (to_type);
17119 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17120 {
17121 struct type *new_type
17122 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17123
17124 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17125 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17126 TYPE_VARARGS (to_type));
17127 type = lookup_methodptr_type (new_type);
17128 }
17129 else
17130 type = lookup_memberptr_type (to_type, domain);
17131
17132 return set_die_type (die, type, cu);
17133 }
17134
17135 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17136 the user defined type vector. */
17137
17138 static struct type *
17139 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17140 enum type_code refcode)
17141 {
17142 struct comp_unit_head *cu_header = &cu->header;
17143 struct type *type, *target_type;
17144 struct attribute *attr;
17145
17146 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17147
17148 target_type = die_type (die, cu);
17149
17150 /* The die_type call above may have already set the type for this DIE. */
17151 type = get_die_type (die, cu);
17152 if (type)
17153 return type;
17154
17155 type = lookup_reference_type (target_type, refcode);
17156 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17157 if (attr)
17158 {
17159 TYPE_LENGTH (type) = DW_UNSND (attr);
17160 }
17161 else
17162 {
17163 TYPE_LENGTH (type) = cu_header->addr_size;
17164 }
17165 maybe_set_alignment (cu, die, type);
17166 return set_die_type (die, type, cu);
17167 }
17168
17169 /* Add the given cv-qualifiers to the element type of the array. GCC
17170 outputs DWARF type qualifiers that apply to an array, not the
17171 element type. But GDB relies on the array element type to carry
17172 the cv-qualifiers. This mimics section 6.7.3 of the C99
17173 specification. */
17174
17175 static struct type *
17176 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17177 struct type *base_type, int cnst, int voltl)
17178 {
17179 struct type *el_type, *inner_array;
17180
17181 base_type = copy_type (base_type);
17182 inner_array = base_type;
17183
17184 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17185 {
17186 TYPE_TARGET_TYPE (inner_array) =
17187 copy_type (TYPE_TARGET_TYPE (inner_array));
17188 inner_array = TYPE_TARGET_TYPE (inner_array);
17189 }
17190
17191 el_type = TYPE_TARGET_TYPE (inner_array);
17192 cnst |= TYPE_CONST (el_type);
17193 voltl |= TYPE_VOLATILE (el_type);
17194 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17195
17196 return set_die_type (die, base_type, cu);
17197 }
17198
17199 static struct type *
17200 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17201 {
17202 struct type *base_type, *cv_type;
17203
17204 base_type = die_type (die, cu);
17205
17206 /* The die_type call above may have already set the type for this DIE. */
17207 cv_type = get_die_type (die, cu);
17208 if (cv_type)
17209 return cv_type;
17210
17211 /* In case the const qualifier is applied to an array type, the element type
17212 is so qualified, not the array type (section 6.7.3 of C99). */
17213 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17214 return add_array_cv_type (die, cu, base_type, 1, 0);
17215
17216 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17217 return set_die_type (die, cv_type, cu);
17218 }
17219
17220 static struct type *
17221 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17222 {
17223 struct type *base_type, *cv_type;
17224
17225 base_type = die_type (die, cu);
17226
17227 /* The die_type call above may have already set the type for this DIE. */
17228 cv_type = get_die_type (die, cu);
17229 if (cv_type)
17230 return cv_type;
17231
17232 /* In case the volatile qualifier is applied to an array type, the
17233 element type is so qualified, not the array type (section 6.7.3
17234 of C99). */
17235 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17236 return add_array_cv_type (die, cu, base_type, 0, 1);
17237
17238 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17239 return set_die_type (die, cv_type, cu);
17240 }
17241
17242 /* Handle DW_TAG_restrict_type. */
17243
17244 static struct type *
17245 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17246 {
17247 struct type *base_type, *cv_type;
17248
17249 base_type = die_type (die, cu);
17250
17251 /* The die_type call above may have already set the type for this DIE. */
17252 cv_type = get_die_type (die, cu);
17253 if (cv_type)
17254 return cv_type;
17255
17256 cv_type = make_restrict_type (base_type);
17257 return set_die_type (die, cv_type, cu);
17258 }
17259
17260 /* Handle DW_TAG_atomic_type. */
17261
17262 static struct type *
17263 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17264 {
17265 struct type *base_type, *cv_type;
17266
17267 base_type = die_type (die, cu);
17268
17269 /* The die_type call above may have already set the type for this DIE. */
17270 cv_type = get_die_type (die, cu);
17271 if (cv_type)
17272 return cv_type;
17273
17274 cv_type = make_atomic_type (base_type);
17275 return set_die_type (die, cv_type, cu);
17276 }
17277
17278 /* Extract all information from a DW_TAG_string_type DIE and add to
17279 the user defined type vector. It isn't really a user defined type,
17280 but it behaves like one, with other DIE's using an AT_user_def_type
17281 attribute to reference it. */
17282
17283 static struct type *
17284 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17285 {
17286 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17287 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17288 struct type *type, *range_type, *index_type, *char_type;
17289 struct attribute *attr;
17290 unsigned int length;
17291
17292 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17293 if (attr)
17294 {
17295 length = DW_UNSND (attr);
17296 }
17297 else
17298 {
17299 /* Check for the DW_AT_byte_size attribute. */
17300 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17301 if (attr)
17302 {
17303 length = DW_UNSND (attr);
17304 }
17305 else
17306 {
17307 length = 1;
17308 }
17309 }
17310
17311 index_type = objfile_type (objfile)->builtin_int;
17312 range_type = create_static_range_type (NULL, index_type, 1, length);
17313 char_type = language_string_char_type (cu->language_defn, gdbarch);
17314 type = create_string_type (NULL, char_type, range_type);
17315
17316 return set_die_type (die, type, cu);
17317 }
17318
17319 /* Assuming that DIE corresponds to a function, returns nonzero
17320 if the function is prototyped. */
17321
17322 static int
17323 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17324 {
17325 struct attribute *attr;
17326
17327 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17328 if (attr && (DW_UNSND (attr) != 0))
17329 return 1;
17330
17331 /* The DWARF standard implies that the DW_AT_prototyped attribute
17332 is only meaningful for C, but the concept also extends to other
17333 languages that allow unprototyped functions (Eg: Objective C).
17334 For all other languages, assume that functions are always
17335 prototyped. */
17336 if (cu->language != language_c
17337 && cu->language != language_objc
17338 && cu->language != language_opencl)
17339 return 1;
17340
17341 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17342 prototyped and unprototyped functions; default to prototyped,
17343 since that is more common in modern code (and RealView warns
17344 about unprototyped functions). */
17345 if (producer_is_realview (cu->producer))
17346 return 1;
17347
17348 return 0;
17349 }
17350
17351 /* Handle DIES due to C code like:
17352
17353 struct foo
17354 {
17355 int (*funcp)(int a, long l);
17356 int b;
17357 };
17358
17359 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17360
17361 static struct type *
17362 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17363 {
17364 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17365 struct type *type; /* Type that this function returns. */
17366 struct type *ftype; /* Function that returns above type. */
17367 struct attribute *attr;
17368
17369 type = die_type (die, cu);
17370
17371 /* The die_type call above may have already set the type for this DIE. */
17372 ftype = get_die_type (die, cu);
17373 if (ftype)
17374 return ftype;
17375
17376 ftype = lookup_function_type (type);
17377
17378 if (prototyped_function_p (die, cu))
17379 TYPE_PROTOTYPED (ftype) = 1;
17380
17381 /* Store the calling convention in the type if it's available in
17382 the subroutine die. Otherwise set the calling convention to
17383 the default value DW_CC_normal. */
17384 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17385 if (attr)
17386 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17387 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17388 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17389 else
17390 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17391
17392 /* Record whether the function returns normally to its caller or not
17393 if the DWARF producer set that information. */
17394 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17395 if (attr && (DW_UNSND (attr) != 0))
17396 TYPE_NO_RETURN (ftype) = 1;
17397
17398 /* We need to add the subroutine type to the die immediately so
17399 we don't infinitely recurse when dealing with parameters
17400 declared as the same subroutine type. */
17401 set_die_type (die, ftype, cu);
17402
17403 if (die->child != NULL)
17404 {
17405 struct type *void_type = objfile_type (objfile)->builtin_void;
17406 struct die_info *child_die;
17407 int nparams, iparams;
17408
17409 /* Count the number of parameters.
17410 FIXME: GDB currently ignores vararg functions, but knows about
17411 vararg member functions. */
17412 nparams = 0;
17413 child_die = die->child;
17414 while (child_die && child_die->tag)
17415 {
17416 if (child_die->tag == DW_TAG_formal_parameter)
17417 nparams++;
17418 else if (child_die->tag == DW_TAG_unspecified_parameters)
17419 TYPE_VARARGS (ftype) = 1;
17420 child_die = sibling_die (child_die);
17421 }
17422
17423 /* Allocate storage for parameters and fill them in. */
17424 TYPE_NFIELDS (ftype) = nparams;
17425 TYPE_FIELDS (ftype) = (struct field *)
17426 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17427
17428 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17429 even if we error out during the parameters reading below. */
17430 for (iparams = 0; iparams < nparams; iparams++)
17431 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17432
17433 iparams = 0;
17434 child_die = die->child;
17435 while (child_die && child_die->tag)
17436 {
17437 if (child_die->tag == DW_TAG_formal_parameter)
17438 {
17439 struct type *arg_type;
17440
17441 /* DWARF version 2 has no clean way to discern C++
17442 static and non-static member functions. G++ helps
17443 GDB by marking the first parameter for non-static
17444 member functions (which is the this pointer) as
17445 artificial. We pass this information to
17446 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17447
17448 DWARF version 3 added DW_AT_object_pointer, which GCC
17449 4.5 does not yet generate. */
17450 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17451 if (attr)
17452 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17453 else
17454 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17455 arg_type = die_type (child_die, cu);
17456
17457 /* RealView does not mark THIS as const, which the testsuite
17458 expects. GCC marks THIS as const in method definitions,
17459 but not in the class specifications (GCC PR 43053). */
17460 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17461 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17462 {
17463 int is_this = 0;
17464 struct dwarf2_cu *arg_cu = cu;
17465 const char *name = dwarf2_name (child_die, cu);
17466
17467 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17468 if (attr)
17469 {
17470 /* If the compiler emits this, use it. */
17471 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17472 is_this = 1;
17473 }
17474 else if (name && strcmp (name, "this") == 0)
17475 /* Function definitions will have the argument names. */
17476 is_this = 1;
17477 else if (name == NULL && iparams == 0)
17478 /* Declarations may not have the names, so like
17479 elsewhere in GDB, assume an artificial first
17480 argument is "this". */
17481 is_this = 1;
17482
17483 if (is_this)
17484 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17485 arg_type, 0);
17486 }
17487
17488 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17489 iparams++;
17490 }
17491 child_die = sibling_die (child_die);
17492 }
17493 }
17494
17495 return ftype;
17496 }
17497
17498 static struct type *
17499 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17500 {
17501 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17502 const char *name = NULL;
17503 struct type *this_type, *target_type;
17504
17505 name = dwarf2_full_name (NULL, die, cu);
17506 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17507 TYPE_TARGET_STUB (this_type) = 1;
17508 set_die_type (die, this_type, cu);
17509 target_type = die_type (die, cu);
17510 if (target_type != this_type)
17511 TYPE_TARGET_TYPE (this_type) = target_type;
17512 else
17513 {
17514 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17515 spec and cause infinite loops in GDB. */
17516 complaint (_("Self-referential DW_TAG_typedef "
17517 "- DIE at %s [in module %s]"),
17518 sect_offset_str (die->sect_off), objfile_name (objfile));
17519 TYPE_TARGET_TYPE (this_type) = NULL;
17520 }
17521 return this_type;
17522 }
17523
17524 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17525 (which may be different from NAME) to the architecture back-end to allow
17526 it to guess the correct format if necessary. */
17527
17528 static struct type *
17529 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17530 const char *name_hint)
17531 {
17532 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17533 const struct floatformat **format;
17534 struct type *type;
17535
17536 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17537 if (format)
17538 type = init_float_type (objfile, bits, name, format);
17539 else
17540 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17541
17542 return type;
17543 }
17544
17545 /* Allocate an integer type of size BITS and name NAME. */
17546
17547 static struct type *
17548 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17549 int bits, int unsigned_p, const char *name)
17550 {
17551 struct type *type;
17552
17553 /* Versions of Intel's C Compiler generate an integer type called "void"
17554 instead of using DW_TAG_unspecified_type. This has been seen on
17555 at least versions 14, 17, and 18. */
17556 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17557 && strcmp (name, "void") == 0)
17558 type = objfile_type (objfile)->builtin_void;
17559 else
17560 type = init_integer_type (objfile, bits, unsigned_p, name);
17561
17562 return type;
17563 }
17564
17565 /* Initialise and return a floating point type of size BITS suitable for
17566 use as a component of a complex number. The NAME_HINT is passed through
17567 when initialising the floating point type and is the name of the complex
17568 type.
17569
17570 As DWARF doesn't currently provide an explicit name for the components
17571 of a complex number, but it can be helpful to have these components
17572 named, we try to select a suitable name based on the size of the
17573 component. */
17574 static struct type *
17575 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17576 struct objfile *objfile,
17577 int bits, const char *name_hint)
17578 {
17579 gdbarch *gdbarch = get_objfile_arch (objfile);
17580 struct type *tt = nullptr;
17581
17582 /* Try to find a suitable floating point builtin type of size BITS.
17583 We're going to use the name of this type as the name for the complex
17584 target type that we are about to create. */
17585 switch (cu->language)
17586 {
17587 case language_fortran:
17588 switch (bits)
17589 {
17590 case 32:
17591 tt = builtin_f_type (gdbarch)->builtin_real;
17592 break;
17593 case 64:
17594 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17595 break;
17596 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17597 case 128:
17598 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17599 break;
17600 }
17601 break;
17602 default:
17603 switch (bits)
17604 {
17605 case 32:
17606 tt = builtin_type (gdbarch)->builtin_float;
17607 break;
17608 case 64:
17609 tt = builtin_type (gdbarch)->builtin_double;
17610 break;
17611 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17612 case 128:
17613 tt = builtin_type (gdbarch)->builtin_long_double;
17614 break;
17615 }
17616 break;
17617 }
17618
17619 /* If the type we found doesn't match the size we were looking for, then
17620 pretend we didn't find a type at all, the complex target type we
17621 create will then be nameless. */
17622 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17623 tt = nullptr;
17624
17625 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17626 return dwarf2_init_float_type (objfile, bits, name, name_hint);
17627 }
17628
17629 /* Find a representation of a given base type and install
17630 it in the TYPE field of the die. */
17631
17632 static struct type *
17633 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17634 {
17635 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17636 struct type *type;
17637 struct attribute *attr;
17638 int encoding = 0, bits = 0;
17639 const char *name;
17640
17641 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17642 if (attr)
17643 {
17644 encoding = DW_UNSND (attr);
17645 }
17646 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17647 if (attr)
17648 {
17649 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17650 }
17651 name = dwarf2_name (die, cu);
17652 if (!name)
17653 {
17654 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17655 }
17656
17657 switch (encoding)
17658 {
17659 case DW_ATE_address:
17660 /* Turn DW_ATE_address into a void * pointer. */
17661 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17662 type = init_pointer_type (objfile, bits, name, type);
17663 break;
17664 case DW_ATE_boolean:
17665 type = init_boolean_type (objfile, bits, 1, name);
17666 break;
17667 case DW_ATE_complex_float:
17668 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name);
17669 type = init_complex_type (objfile, name, type);
17670 break;
17671 case DW_ATE_decimal_float:
17672 type = init_decfloat_type (objfile, bits, name);
17673 break;
17674 case DW_ATE_float:
17675 type = dwarf2_init_float_type (objfile, bits, name, name);
17676 break;
17677 case DW_ATE_signed:
17678 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17679 break;
17680 case DW_ATE_unsigned:
17681 if (cu->language == language_fortran
17682 && name
17683 && startswith (name, "character("))
17684 type = init_character_type (objfile, bits, 1, name);
17685 else
17686 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17687 break;
17688 case DW_ATE_signed_char:
17689 if (cu->language == language_ada || cu->language == language_m2
17690 || cu->language == language_pascal
17691 || cu->language == language_fortran)
17692 type = init_character_type (objfile, bits, 0, name);
17693 else
17694 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17695 break;
17696 case DW_ATE_unsigned_char:
17697 if (cu->language == language_ada || cu->language == language_m2
17698 || cu->language == language_pascal
17699 || cu->language == language_fortran
17700 || cu->language == language_rust)
17701 type = init_character_type (objfile, bits, 1, name);
17702 else
17703 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17704 break;
17705 case DW_ATE_UTF:
17706 {
17707 gdbarch *arch = get_objfile_arch (objfile);
17708
17709 if (bits == 16)
17710 type = builtin_type (arch)->builtin_char16;
17711 else if (bits == 32)
17712 type = builtin_type (arch)->builtin_char32;
17713 else
17714 {
17715 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17716 bits);
17717 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17718 }
17719 return set_die_type (die, type, cu);
17720 }
17721 break;
17722
17723 default:
17724 complaint (_("unsupported DW_AT_encoding: '%s'"),
17725 dwarf_type_encoding_name (encoding));
17726 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17727 break;
17728 }
17729
17730 if (name && strcmp (name, "char") == 0)
17731 TYPE_NOSIGN (type) = 1;
17732
17733 maybe_set_alignment (cu, die, type);
17734
17735 return set_die_type (die, type, cu);
17736 }
17737
17738 /* Parse dwarf attribute if it's a block, reference or constant and put the
17739 resulting value of the attribute into struct bound_prop.
17740 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17741
17742 static int
17743 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17744 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17745 struct type *default_type)
17746 {
17747 struct dwarf2_property_baton *baton;
17748 struct obstack *obstack
17749 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17750
17751 gdb_assert (default_type != NULL);
17752
17753 if (attr == NULL || prop == NULL)
17754 return 0;
17755
17756 if (attr_form_is_block (attr))
17757 {
17758 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17759 baton->property_type = default_type;
17760 baton->locexpr.per_cu = cu->per_cu;
17761 baton->locexpr.size = DW_BLOCK (attr)->size;
17762 baton->locexpr.data = DW_BLOCK (attr)->data;
17763 baton->locexpr.is_reference = false;
17764 prop->data.baton = baton;
17765 prop->kind = PROP_LOCEXPR;
17766 gdb_assert (prop->data.baton != NULL);
17767 }
17768 else if (attr_form_is_ref (attr))
17769 {
17770 struct dwarf2_cu *target_cu = cu;
17771 struct die_info *target_die;
17772 struct attribute *target_attr;
17773
17774 target_die = follow_die_ref (die, attr, &target_cu);
17775 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17776 if (target_attr == NULL)
17777 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17778 target_cu);
17779 if (target_attr == NULL)
17780 return 0;
17781
17782 switch (target_attr->name)
17783 {
17784 case DW_AT_location:
17785 if (attr_form_is_section_offset (target_attr))
17786 {
17787 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17788 baton->property_type = die_type (target_die, target_cu);
17789 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17790 prop->data.baton = baton;
17791 prop->kind = PROP_LOCLIST;
17792 gdb_assert (prop->data.baton != NULL);
17793 }
17794 else if (attr_form_is_block (target_attr))
17795 {
17796 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17797 baton->property_type = die_type (target_die, target_cu);
17798 baton->locexpr.per_cu = cu->per_cu;
17799 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17800 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17801 baton->locexpr.is_reference = true;
17802 prop->data.baton = baton;
17803 prop->kind = PROP_LOCEXPR;
17804 gdb_assert (prop->data.baton != NULL);
17805 }
17806 else
17807 {
17808 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17809 "dynamic property");
17810 return 0;
17811 }
17812 break;
17813 case DW_AT_data_member_location:
17814 {
17815 LONGEST offset;
17816
17817 if (!handle_data_member_location (target_die, target_cu,
17818 &offset))
17819 return 0;
17820
17821 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17822 baton->property_type = read_type_die (target_die->parent,
17823 target_cu);
17824 baton->offset_info.offset = offset;
17825 baton->offset_info.type = die_type (target_die, target_cu);
17826 prop->data.baton = baton;
17827 prop->kind = PROP_ADDR_OFFSET;
17828 break;
17829 }
17830 }
17831 }
17832 else if (attr_form_is_constant (attr))
17833 {
17834 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17835 prop->kind = PROP_CONST;
17836 }
17837 else
17838 {
17839 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17840 dwarf2_name (die, cu));
17841 return 0;
17842 }
17843
17844 return 1;
17845 }
17846
17847 /* Find an integer type the same size as the address size given in the
17848 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
17849 is unsigned or not. */
17850
17851 static struct type *
17852 dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
17853 bool unsigned_p)
17854 {
17855 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
17856 int addr_size = dwarf2_per_cu_addr_size (per_cu);
17857 struct type *int_type;
17858
17859 /* Helper macro to examine the various builtin types. */
17860 #define TRY_TYPE(F) \
17861 int_type = (unsigned_p \
17862 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17863 : objfile_type (objfile)->builtin_ ## F); \
17864 if (int_type != NULL && TYPE_LENGTH (int_type) == addr_size) \
17865 return int_type
17866
17867 TRY_TYPE (char);
17868 TRY_TYPE (short);
17869 TRY_TYPE (int);
17870 TRY_TYPE (long);
17871 TRY_TYPE (long_long);
17872
17873 #undef TRY_TYPE
17874
17875 gdb_assert_not_reached ("unable to find suitable integer type");
17876 }
17877
17878 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17879 present (which is valid) then compute the default type based on the
17880 compilation units address size. */
17881
17882 static struct type *
17883 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17884 {
17885 struct type *index_type = die_type (die, cu);
17886
17887 /* Dwarf-2 specifications explicitly allows to create subrange types
17888 without specifying a base type.
17889 In that case, the base type must be set to the type of
17890 the lower bound, upper bound or count, in that order, if any of these
17891 three attributes references an object that has a type.
17892 If no base type is found, the Dwarf-2 specifications say that
17893 a signed integer type of size equal to the size of an address should
17894 be used.
17895 For the following C code: `extern char gdb_int [];'
17896 GCC produces an empty range DIE.
17897 FIXME: muller/2010-05-28: Possible references to object for low bound,
17898 high bound or count are not yet handled by this code. */
17899 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17900 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17901
17902 return index_type;
17903 }
17904
17905 /* Read the given DW_AT_subrange DIE. */
17906
17907 static struct type *
17908 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17909 {
17910 struct type *base_type, *orig_base_type;
17911 struct type *range_type;
17912 struct attribute *attr;
17913 struct dynamic_prop low, high;
17914 int low_default_is_valid;
17915 int high_bound_is_count = 0;
17916 const char *name;
17917 ULONGEST negative_mask;
17918
17919 orig_base_type = read_subrange_index_type (die, cu);
17920
17921 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17922 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17923 creating the range type, but we use the result of check_typedef
17924 when examining properties of the type. */
17925 base_type = check_typedef (orig_base_type);
17926
17927 /* The die_type call above may have already set the type for this DIE. */
17928 range_type = get_die_type (die, cu);
17929 if (range_type)
17930 return range_type;
17931
17932 low.kind = PROP_CONST;
17933 high.kind = PROP_CONST;
17934 high.data.const_val = 0;
17935
17936 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17937 omitting DW_AT_lower_bound. */
17938 switch (cu->language)
17939 {
17940 case language_c:
17941 case language_cplus:
17942 low.data.const_val = 0;
17943 low_default_is_valid = 1;
17944 break;
17945 case language_fortran:
17946 low.data.const_val = 1;
17947 low_default_is_valid = 1;
17948 break;
17949 case language_d:
17950 case language_objc:
17951 case language_rust:
17952 low.data.const_val = 0;
17953 low_default_is_valid = (cu->header.version >= 4);
17954 break;
17955 case language_ada:
17956 case language_m2:
17957 case language_pascal:
17958 low.data.const_val = 1;
17959 low_default_is_valid = (cu->header.version >= 4);
17960 break;
17961 default:
17962 low.data.const_val = 0;
17963 low_default_is_valid = 0;
17964 break;
17965 }
17966
17967 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17968 if (attr)
17969 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17970 else if (!low_default_is_valid)
17971 complaint (_("Missing DW_AT_lower_bound "
17972 "- DIE at %s [in module %s]"),
17973 sect_offset_str (die->sect_off),
17974 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17975
17976 struct attribute *attr_ub, *attr_count;
17977 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17978 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17979 {
17980 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17981 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17982 {
17983 /* If bounds are constant do the final calculation here. */
17984 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17985 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17986 else
17987 high_bound_is_count = 1;
17988 }
17989 else
17990 {
17991 if (attr_ub != NULL)
17992 complaint (_("Unresolved DW_AT_upper_bound "
17993 "- DIE at %s [in module %s]"),
17994 sect_offset_str (die->sect_off),
17995 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17996 if (attr_count != NULL)
17997 complaint (_("Unresolved DW_AT_count "
17998 "- DIE at %s [in module %s]"),
17999 sect_offset_str (die->sect_off),
18000 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18001 }
18002 }
18003
18004 LONGEST bias = 0;
18005 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18006 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
18007 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
18008
18009 /* Normally, the DWARF producers are expected to use a signed
18010 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18011 But this is unfortunately not always the case, as witnessed
18012 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18013 is used instead. To work around that ambiguity, we treat
18014 the bounds as signed, and thus sign-extend their values, when
18015 the base type is signed. */
18016 negative_mask =
18017 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18018 if (low.kind == PROP_CONST
18019 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18020 low.data.const_val |= negative_mask;
18021 if (high.kind == PROP_CONST
18022 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18023 high.data.const_val |= negative_mask;
18024
18025 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18026
18027 if (high_bound_is_count)
18028 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18029
18030 /* Ada expects an empty array on no boundary attributes. */
18031 if (attr == NULL && cu->language != language_ada)
18032 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
18033
18034 name = dwarf2_name (die, cu);
18035 if (name)
18036 TYPE_NAME (range_type) = name;
18037
18038 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18039 if (attr)
18040 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18041
18042 maybe_set_alignment (cu, die, range_type);
18043
18044 set_die_type (die, range_type, cu);
18045
18046 /* set_die_type should be already done. */
18047 set_descriptive_type (range_type, die, cu);
18048
18049 return range_type;
18050 }
18051
18052 static struct type *
18053 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18054 {
18055 struct type *type;
18056
18057 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18058 NULL);
18059 TYPE_NAME (type) = dwarf2_name (die, cu);
18060
18061 /* In Ada, an unspecified type is typically used when the description
18062 of the type is deferred to a different unit. When encountering
18063 such a type, we treat it as a stub, and try to resolve it later on,
18064 when needed. */
18065 if (cu->language == language_ada)
18066 TYPE_STUB (type) = 1;
18067
18068 return set_die_type (die, type, cu);
18069 }
18070
18071 /* Read a single die and all its descendents. Set the die's sibling
18072 field to NULL; set other fields in the die correctly, and set all
18073 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18074 location of the info_ptr after reading all of those dies. PARENT
18075 is the parent of the die in question. */
18076
18077 static struct die_info *
18078 read_die_and_children (const struct die_reader_specs *reader,
18079 const gdb_byte *info_ptr,
18080 const gdb_byte **new_info_ptr,
18081 struct die_info *parent)
18082 {
18083 struct die_info *die;
18084 const gdb_byte *cur_ptr;
18085 int has_children;
18086
18087 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
18088 if (die == NULL)
18089 {
18090 *new_info_ptr = cur_ptr;
18091 return NULL;
18092 }
18093 store_in_ref_table (die, reader->cu);
18094
18095 if (has_children)
18096 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18097 else
18098 {
18099 die->child = NULL;
18100 *new_info_ptr = cur_ptr;
18101 }
18102
18103 die->sibling = NULL;
18104 die->parent = parent;
18105 return die;
18106 }
18107
18108 /* Read a die, all of its descendents, and all of its siblings; set
18109 all of the fields of all of the dies correctly. Arguments are as
18110 in read_die_and_children. */
18111
18112 static struct die_info *
18113 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18114 const gdb_byte *info_ptr,
18115 const gdb_byte **new_info_ptr,
18116 struct die_info *parent)
18117 {
18118 struct die_info *first_die, *last_sibling;
18119 const gdb_byte *cur_ptr;
18120
18121 cur_ptr = info_ptr;
18122 first_die = last_sibling = NULL;
18123
18124 while (1)
18125 {
18126 struct die_info *die
18127 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18128
18129 if (die == NULL)
18130 {
18131 *new_info_ptr = cur_ptr;
18132 return first_die;
18133 }
18134
18135 if (!first_die)
18136 first_die = die;
18137 else
18138 last_sibling->sibling = die;
18139
18140 last_sibling = die;
18141 }
18142 }
18143
18144 /* Read a die, all of its descendents, and all of its siblings; set
18145 all of the fields of all of the dies correctly. Arguments are as
18146 in read_die_and_children.
18147 This the main entry point for reading a DIE and all its children. */
18148
18149 static struct die_info *
18150 read_die_and_siblings (const struct die_reader_specs *reader,
18151 const gdb_byte *info_ptr,
18152 const gdb_byte **new_info_ptr,
18153 struct die_info *parent)
18154 {
18155 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18156 new_info_ptr, parent);
18157
18158 if (dwarf_die_debug)
18159 {
18160 fprintf_unfiltered (gdb_stdlog,
18161 "Read die from %s@0x%x of %s:\n",
18162 get_section_name (reader->die_section),
18163 (unsigned) (info_ptr - reader->die_section->buffer),
18164 bfd_get_filename (reader->abfd));
18165 dump_die (die, dwarf_die_debug);
18166 }
18167
18168 return die;
18169 }
18170
18171 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18172 attributes.
18173 The caller is responsible for filling in the extra attributes
18174 and updating (*DIEP)->num_attrs.
18175 Set DIEP to point to a newly allocated die with its information,
18176 except for its child, sibling, and parent fields.
18177 Set HAS_CHILDREN to tell whether the die has children or not. */
18178
18179 static const gdb_byte *
18180 read_full_die_1 (const struct die_reader_specs *reader,
18181 struct die_info **diep, const gdb_byte *info_ptr,
18182 int *has_children, int num_extra_attrs)
18183 {
18184 unsigned int abbrev_number, bytes_read, i;
18185 struct abbrev_info *abbrev;
18186 struct die_info *die;
18187 struct dwarf2_cu *cu = reader->cu;
18188 bfd *abfd = reader->abfd;
18189
18190 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18191 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18192 info_ptr += bytes_read;
18193 if (!abbrev_number)
18194 {
18195 *diep = NULL;
18196 *has_children = 0;
18197 return info_ptr;
18198 }
18199
18200 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18201 if (!abbrev)
18202 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18203 abbrev_number,
18204 bfd_get_filename (abfd));
18205
18206 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18207 die->sect_off = sect_off;
18208 die->tag = abbrev->tag;
18209 die->abbrev = abbrev_number;
18210
18211 /* Make the result usable.
18212 The caller needs to update num_attrs after adding the extra
18213 attributes. */
18214 die->num_attrs = abbrev->num_attrs;
18215
18216 for (i = 0; i < abbrev->num_attrs; ++i)
18217 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18218 info_ptr);
18219
18220 *diep = die;
18221 *has_children = abbrev->has_children;
18222 return info_ptr;
18223 }
18224
18225 /* Read a die and all its attributes.
18226 Set DIEP to point to a newly allocated die with its information,
18227 except for its child, sibling, and parent fields.
18228 Set HAS_CHILDREN to tell whether the die has children or not. */
18229
18230 static const gdb_byte *
18231 read_full_die (const struct die_reader_specs *reader,
18232 struct die_info **diep, const gdb_byte *info_ptr,
18233 int *has_children)
18234 {
18235 const gdb_byte *result;
18236
18237 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18238
18239 if (dwarf_die_debug)
18240 {
18241 fprintf_unfiltered (gdb_stdlog,
18242 "Read die from %s@0x%x of %s:\n",
18243 get_section_name (reader->die_section),
18244 (unsigned) (info_ptr - reader->die_section->buffer),
18245 bfd_get_filename (reader->abfd));
18246 dump_die (*diep, dwarf_die_debug);
18247 }
18248
18249 return result;
18250 }
18251 \f
18252 /* Abbreviation tables.
18253
18254 In DWARF version 2, the description of the debugging information is
18255 stored in a separate .debug_abbrev section. Before we read any
18256 dies from a section we read in all abbreviations and install them
18257 in a hash table. */
18258
18259 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18260
18261 struct abbrev_info *
18262 abbrev_table::alloc_abbrev ()
18263 {
18264 struct abbrev_info *abbrev;
18265
18266 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18267 memset (abbrev, 0, sizeof (struct abbrev_info));
18268
18269 return abbrev;
18270 }
18271
18272 /* Add an abbreviation to the table. */
18273
18274 void
18275 abbrev_table::add_abbrev (unsigned int abbrev_number,
18276 struct abbrev_info *abbrev)
18277 {
18278 unsigned int hash_number;
18279
18280 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18281 abbrev->next = m_abbrevs[hash_number];
18282 m_abbrevs[hash_number] = abbrev;
18283 }
18284
18285 /* Look up an abbrev in the table.
18286 Returns NULL if the abbrev is not found. */
18287
18288 struct abbrev_info *
18289 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18290 {
18291 unsigned int hash_number;
18292 struct abbrev_info *abbrev;
18293
18294 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18295 abbrev = m_abbrevs[hash_number];
18296
18297 while (abbrev)
18298 {
18299 if (abbrev->number == abbrev_number)
18300 return abbrev;
18301 abbrev = abbrev->next;
18302 }
18303 return NULL;
18304 }
18305
18306 /* Read in an abbrev table. */
18307
18308 static abbrev_table_up
18309 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18310 struct dwarf2_section_info *section,
18311 sect_offset sect_off)
18312 {
18313 struct objfile *objfile = dwarf2_per_objfile->objfile;
18314 bfd *abfd = get_section_bfd_owner (section);
18315 const gdb_byte *abbrev_ptr;
18316 struct abbrev_info *cur_abbrev;
18317 unsigned int abbrev_number, bytes_read, abbrev_name;
18318 unsigned int abbrev_form;
18319 struct attr_abbrev *cur_attrs;
18320 unsigned int allocated_attrs;
18321
18322 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18323
18324 dwarf2_read_section (objfile, section);
18325 abbrev_ptr = section->buffer + to_underlying (sect_off);
18326 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18327 abbrev_ptr += bytes_read;
18328
18329 allocated_attrs = ATTR_ALLOC_CHUNK;
18330 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18331
18332 /* Loop until we reach an abbrev number of 0. */
18333 while (abbrev_number)
18334 {
18335 cur_abbrev = abbrev_table->alloc_abbrev ();
18336
18337 /* read in abbrev header */
18338 cur_abbrev->number = abbrev_number;
18339 cur_abbrev->tag
18340 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18341 abbrev_ptr += bytes_read;
18342 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18343 abbrev_ptr += 1;
18344
18345 /* now read in declarations */
18346 for (;;)
18347 {
18348 LONGEST implicit_const;
18349
18350 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18351 abbrev_ptr += bytes_read;
18352 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18353 abbrev_ptr += bytes_read;
18354 if (abbrev_form == DW_FORM_implicit_const)
18355 {
18356 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18357 &bytes_read);
18358 abbrev_ptr += bytes_read;
18359 }
18360 else
18361 {
18362 /* Initialize it due to a false compiler warning. */
18363 implicit_const = -1;
18364 }
18365
18366 if (abbrev_name == 0)
18367 break;
18368
18369 if (cur_abbrev->num_attrs == allocated_attrs)
18370 {
18371 allocated_attrs += ATTR_ALLOC_CHUNK;
18372 cur_attrs
18373 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18374 }
18375
18376 cur_attrs[cur_abbrev->num_attrs].name
18377 = (enum dwarf_attribute) abbrev_name;
18378 cur_attrs[cur_abbrev->num_attrs].form
18379 = (enum dwarf_form) abbrev_form;
18380 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18381 ++cur_abbrev->num_attrs;
18382 }
18383
18384 cur_abbrev->attrs =
18385 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18386 cur_abbrev->num_attrs);
18387 memcpy (cur_abbrev->attrs, cur_attrs,
18388 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18389
18390 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18391
18392 /* Get next abbreviation.
18393 Under Irix6 the abbreviations for a compilation unit are not
18394 always properly terminated with an abbrev number of 0.
18395 Exit loop if we encounter an abbreviation which we have
18396 already read (which means we are about to read the abbreviations
18397 for the next compile unit) or if the end of the abbreviation
18398 table is reached. */
18399 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18400 break;
18401 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18402 abbrev_ptr += bytes_read;
18403 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18404 break;
18405 }
18406
18407 xfree (cur_attrs);
18408 return abbrev_table;
18409 }
18410
18411 /* Returns nonzero if TAG represents a type that we might generate a partial
18412 symbol for. */
18413
18414 static int
18415 is_type_tag_for_partial (int tag)
18416 {
18417 switch (tag)
18418 {
18419 #if 0
18420 /* Some types that would be reasonable to generate partial symbols for,
18421 that we don't at present. */
18422 case DW_TAG_array_type:
18423 case DW_TAG_file_type:
18424 case DW_TAG_ptr_to_member_type:
18425 case DW_TAG_set_type:
18426 case DW_TAG_string_type:
18427 case DW_TAG_subroutine_type:
18428 #endif
18429 case DW_TAG_base_type:
18430 case DW_TAG_class_type:
18431 case DW_TAG_interface_type:
18432 case DW_TAG_enumeration_type:
18433 case DW_TAG_structure_type:
18434 case DW_TAG_subrange_type:
18435 case DW_TAG_typedef:
18436 case DW_TAG_union_type:
18437 return 1;
18438 default:
18439 return 0;
18440 }
18441 }
18442
18443 /* Load all DIEs that are interesting for partial symbols into memory. */
18444
18445 static struct partial_die_info *
18446 load_partial_dies (const struct die_reader_specs *reader,
18447 const gdb_byte *info_ptr, int building_psymtab)
18448 {
18449 struct dwarf2_cu *cu = reader->cu;
18450 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18451 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18452 unsigned int bytes_read;
18453 unsigned int load_all = 0;
18454 int nesting_level = 1;
18455
18456 parent_die = NULL;
18457 last_die = NULL;
18458
18459 gdb_assert (cu->per_cu != NULL);
18460 if (cu->per_cu->load_all_dies)
18461 load_all = 1;
18462
18463 cu->partial_dies
18464 = htab_create_alloc_ex (cu->header.length / 12,
18465 partial_die_hash,
18466 partial_die_eq,
18467 NULL,
18468 &cu->comp_unit_obstack,
18469 hashtab_obstack_allocate,
18470 dummy_obstack_deallocate);
18471
18472 while (1)
18473 {
18474 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18475
18476 /* A NULL abbrev means the end of a series of children. */
18477 if (abbrev == NULL)
18478 {
18479 if (--nesting_level == 0)
18480 return first_die;
18481
18482 info_ptr += bytes_read;
18483 last_die = parent_die;
18484 parent_die = parent_die->die_parent;
18485 continue;
18486 }
18487
18488 /* Check for template arguments. We never save these; if
18489 they're seen, we just mark the parent, and go on our way. */
18490 if (parent_die != NULL
18491 && cu->language == language_cplus
18492 && (abbrev->tag == DW_TAG_template_type_param
18493 || abbrev->tag == DW_TAG_template_value_param))
18494 {
18495 parent_die->has_template_arguments = 1;
18496
18497 if (!load_all)
18498 {
18499 /* We don't need a partial DIE for the template argument. */
18500 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18501 continue;
18502 }
18503 }
18504
18505 /* We only recurse into c++ subprograms looking for template arguments.
18506 Skip their other children. */
18507 if (!load_all
18508 && cu->language == language_cplus
18509 && parent_die != NULL
18510 && parent_die->tag == DW_TAG_subprogram)
18511 {
18512 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18513 continue;
18514 }
18515
18516 /* Check whether this DIE is interesting enough to save. Normally
18517 we would not be interested in members here, but there may be
18518 later variables referencing them via DW_AT_specification (for
18519 static members). */
18520 if (!load_all
18521 && !is_type_tag_for_partial (abbrev->tag)
18522 && abbrev->tag != DW_TAG_constant
18523 && abbrev->tag != DW_TAG_enumerator
18524 && abbrev->tag != DW_TAG_subprogram
18525 && abbrev->tag != DW_TAG_inlined_subroutine
18526 && abbrev->tag != DW_TAG_lexical_block
18527 && abbrev->tag != DW_TAG_variable
18528 && abbrev->tag != DW_TAG_namespace
18529 && abbrev->tag != DW_TAG_module
18530 && abbrev->tag != DW_TAG_member
18531 && abbrev->tag != DW_TAG_imported_unit
18532 && abbrev->tag != DW_TAG_imported_declaration)
18533 {
18534 /* Otherwise we skip to the next sibling, if any. */
18535 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18536 continue;
18537 }
18538
18539 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18540 abbrev);
18541
18542 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18543
18544 /* This two-pass algorithm for processing partial symbols has a
18545 high cost in cache pressure. Thus, handle some simple cases
18546 here which cover the majority of C partial symbols. DIEs
18547 which neither have specification tags in them, nor could have
18548 specification tags elsewhere pointing at them, can simply be
18549 processed and discarded.
18550
18551 This segment is also optional; scan_partial_symbols and
18552 add_partial_symbol will handle these DIEs if we chain
18553 them in normally. When compilers which do not emit large
18554 quantities of duplicate debug information are more common,
18555 this code can probably be removed. */
18556
18557 /* Any complete simple types at the top level (pretty much all
18558 of them, for a language without namespaces), can be processed
18559 directly. */
18560 if (parent_die == NULL
18561 && pdi.has_specification == 0
18562 && pdi.is_declaration == 0
18563 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18564 || pdi.tag == DW_TAG_base_type
18565 || pdi.tag == DW_TAG_subrange_type))
18566 {
18567 if (building_psymtab && pdi.name != NULL)
18568 add_psymbol_to_list (pdi.name, false,
18569 VAR_DOMAIN, LOC_TYPEDEF, -1,
18570 psymbol_placement::STATIC,
18571 0, cu->language, objfile);
18572 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18573 continue;
18574 }
18575
18576 /* The exception for DW_TAG_typedef with has_children above is
18577 a workaround of GCC PR debug/47510. In the case of this complaint
18578 type_name_or_error will error on such types later.
18579
18580 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18581 it could not find the child DIEs referenced later, this is checked
18582 above. In correct DWARF DW_TAG_typedef should have no children. */
18583
18584 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18585 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18586 "- DIE at %s [in module %s]"),
18587 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18588
18589 /* If we're at the second level, and we're an enumerator, and
18590 our parent has no specification (meaning possibly lives in a
18591 namespace elsewhere), then we can add the partial symbol now
18592 instead of queueing it. */
18593 if (pdi.tag == DW_TAG_enumerator
18594 && parent_die != NULL
18595 && parent_die->die_parent == NULL
18596 && parent_die->tag == DW_TAG_enumeration_type
18597 && parent_die->has_specification == 0)
18598 {
18599 if (pdi.name == NULL)
18600 complaint (_("malformed enumerator DIE ignored"));
18601 else if (building_psymtab)
18602 add_psymbol_to_list (pdi.name, false,
18603 VAR_DOMAIN, LOC_CONST, -1,
18604 cu->language == language_cplus
18605 ? psymbol_placement::GLOBAL
18606 : psymbol_placement::STATIC,
18607 0, cu->language, objfile);
18608
18609 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18610 continue;
18611 }
18612
18613 struct partial_die_info *part_die
18614 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18615
18616 /* We'll save this DIE so link it in. */
18617 part_die->die_parent = parent_die;
18618 part_die->die_sibling = NULL;
18619 part_die->die_child = NULL;
18620
18621 if (last_die && last_die == parent_die)
18622 last_die->die_child = part_die;
18623 else if (last_die)
18624 last_die->die_sibling = part_die;
18625
18626 last_die = part_die;
18627
18628 if (first_die == NULL)
18629 first_die = part_die;
18630
18631 /* Maybe add the DIE to the hash table. Not all DIEs that we
18632 find interesting need to be in the hash table, because we
18633 also have the parent/sibling/child chains; only those that we
18634 might refer to by offset later during partial symbol reading.
18635
18636 For now this means things that might have be the target of a
18637 DW_AT_specification, DW_AT_abstract_origin, or
18638 DW_AT_extension. DW_AT_extension will refer only to
18639 namespaces; DW_AT_abstract_origin refers to functions (and
18640 many things under the function DIE, but we do not recurse
18641 into function DIEs during partial symbol reading) and
18642 possibly variables as well; DW_AT_specification refers to
18643 declarations. Declarations ought to have the DW_AT_declaration
18644 flag. It happens that GCC forgets to put it in sometimes, but
18645 only for functions, not for types.
18646
18647 Adding more things than necessary to the hash table is harmless
18648 except for the performance cost. Adding too few will result in
18649 wasted time in find_partial_die, when we reread the compilation
18650 unit with load_all_dies set. */
18651
18652 if (load_all
18653 || abbrev->tag == DW_TAG_constant
18654 || abbrev->tag == DW_TAG_subprogram
18655 || abbrev->tag == DW_TAG_variable
18656 || abbrev->tag == DW_TAG_namespace
18657 || part_die->is_declaration)
18658 {
18659 void **slot;
18660
18661 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18662 to_underlying (part_die->sect_off),
18663 INSERT);
18664 *slot = part_die;
18665 }
18666
18667 /* For some DIEs we want to follow their children (if any). For C
18668 we have no reason to follow the children of structures; for other
18669 languages we have to, so that we can get at method physnames
18670 to infer fully qualified class names, for DW_AT_specification,
18671 and for C++ template arguments. For C++, we also look one level
18672 inside functions to find template arguments (if the name of the
18673 function does not already contain the template arguments).
18674
18675 For Ada and Fortran, we need to scan the children of subprograms
18676 and lexical blocks as well because these languages allow the
18677 definition of nested entities that could be interesting for the
18678 debugger, such as nested subprograms for instance. */
18679 if (last_die->has_children
18680 && (load_all
18681 || last_die->tag == DW_TAG_namespace
18682 || last_die->tag == DW_TAG_module
18683 || last_die->tag == DW_TAG_enumeration_type
18684 || (cu->language == language_cplus
18685 && last_die->tag == DW_TAG_subprogram
18686 && (last_die->name == NULL
18687 || strchr (last_die->name, '<') == NULL))
18688 || (cu->language != language_c
18689 && (last_die->tag == DW_TAG_class_type
18690 || last_die->tag == DW_TAG_interface_type
18691 || last_die->tag == DW_TAG_structure_type
18692 || last_die->tag == DW_TAG_union_type))
18693 || ((cu->language == language_ada
18694 || cu->language == language_fortran)
18695 && (last_die->tag == DW_TAG_subprogram
18696 || last_die->tag == DW_TAG_lexical_block))))
18697 {
18698 nesting_level++;
18699 parent_die = last_die;
18700 continue;
18701 }
18702
18703 /* Otherwise we skip to the next sibling, if any. */
18704 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18705
18706 /* Back to the top, do it again. */
18707 }
18708 }
18709
18710 partial_die_info::partial_die_info (sect_offset sect_off_,
18711 struct abbrev_info *abbrev)
18712 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18713 {
18714 }
18715
18716 /* Read a minimal amount of information into the minimal die structure.
18717 INFO_PTR should point just after the initial uleb128 of a DIE. */
18718
18719 const gdb_byte *
18720 partial_die_info::read (const struct die_reader_specs *reader,
18721 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18722 {
18723 struct dwarf2_cu *cu = reader->cu;
18724 struct dwarf2_per_objfile *dwarf2_per_objfile
18725 = cu->per_cu->dwarf2_per_objfile;
18726 unsigned int i;
18727 int has_low_pc_attr = 0;
18728 int has_high_pc_attr = 0;
18729 int high_pc_relative = 0;
18730
18731 for (i = 0; i < abbrev.num_attrs; ++i)
18732 {
18733 struct attribute attr;
18734
18735 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18736
18737 /* Store the data if it is of an attribute we want to keep in a
18738 partial symbol table. */
18739 switch (attr.name)
18740 {
18741 case DW_AT_name:
18742 switch (tag)
18743 {
18744 case DW_TAG_compile_unit:
18745 case DW_TAG_partial_unit:
18746 case DW_TAG_type_unit:
18747 /* Compilation units have a DW_AT_name that is a filename, not
18748 a source language identifier. */
18749 case DW_TAG_enumeration_type:
18750 case DW_TAG_enumerator:
18751 /* These tags always have simple identifiers already; no need
18752 to canonicalize them. */
18753 name = DW_STRING (&attr);
18754 break;
18755 default:
18756 {
18757 struct objfile *objfile = dwarf2_per_objfile->objfile;
18758
18759 name
18760 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18761 &objfile->per_bfd->storage_obstack);
18762 }
18763 break;
18764 }
18765 break;
18766 case DW_AT_linkage_name:
18767 case DW_AT_MIPS_linkage_name:
18768 /* Note that both forms of linkage name might appear. We
18769 assume they will be the same, and we only store the last
18770 one we see. */
18771 linkage_name = DW_STRING (&attr);
18772 break;
18773 case DW_AT_low_pc:
18774 has_low_pc_attr = 1;
18775 lowpc = attr_value_as_address (&attr);
18776 break;
18777 case DW_AT_high_pc:
18778 has_high_pc_attr = 1;
18779 highpc = attr_value_as_address (&attr);
18780 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18781 high_pc_relative = 1;
18782 break;
18783 case DW_AT_location:
18784 /* Support the .debug_loc offsets. */
18785 if (attr_form_is_block (&attr))
18786 {
18787 d.locdesc = DW_BLOCK (&attr);
18788 }
18789 else if (attr_form_is_section_offset (&attr))
18790 {
18791 dwarf2_complex_location_expr_complaint ();
18792 }
18793 else
18794 {
18795 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18796 "partial symbol information");
18797 }
18798 break;
18799 case DW_AT_external:
18800 is_external = DW_UNSND (&attr);
18801 break;
18802 case DW_AT_declaration:
18803 is_declaration = DW_UNSND (&attr);
18804 break;
18805 case DW_AT_type:
18806 has_type = 1;
18807 break;
18808 case DW_AT_abstract_origin:
18809 case DW_AT_specification:
18810 case DW_AT_extension:
18811 has_specification = 1;
18812 spec_offset = dwarf2_get_ref_die_offset (&attr);
18813 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18814 || cu->per_cu->is_dwz);
18815 break;
18816 case DW_AT_sibling:
18817 /* Ignore absolute siblings, they might point outside of
18818 the current compile unit. */
18819 if (attr.form == DW_FORM_ref_addr)
18820 complaint (_("ignoring absolute DW_AT_sibling"));
18821 else
18822 {
18823 const gdb_byte *buffer = reader->buffer;
18824 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18825 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18826
18827 if (sibling_ptr < info_ptr)
18828 complaint (_("DW_AT_sibling points backwards"));
18829 else if (sibling_ptr > reader->buffer_end)
18830 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18831 else
18832 sibling = sibling_ptr;
18833 }
18834 break;
18835 case DW_AT_byte_size:
18836 has_byte_size = 1;
18837 break;
18838 case DW_AT_const_value:
18839 has_const_value = 1;
18840 break;
18841 case DW_AT_calling_convention:
18842 /* DWARF doesn't provide a way to identify a program's source-level
18843 entry point. DW_AT_calling_convention attributes are only meant
18844 to describe functions' calling conventions.
18845
18846 However, because it's a necessary piece of information in
18847 Fortran, and before DWARF 4 DW_CC_program was the only
18848 piece of debugging information whose definition refers to
18849 a 'main program' at all, several compilers marked Fortran
18850 main programs with DW_CC_program --- even when those
18851 functions use the standard calling conventions.
18852
18853 Although DWARF now specifies a way to provide this
18854 information, we support this practice for backward
18855 compatibility. */
18856 if (DW_UNSND (&attr) == DW_CC_program
18857 && cu->language == language_fortran)
18858 main_subprogram = 1;
18859 break;
18860 case DW_AT_inline:
18861 if (DW_UNSND (&attr) == DW_INL_inlined
18862 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18863 may_be_inlined = 1;
18864 break;
18865
18866 case DW_AT_import:
18867 if (tag == DW_TAG_imported_unit)
18868 {
18869 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18870 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18871 || cu->per_cu->is_dwz);
18872 }
18873 break;
18874
18875 case DW_AT_main_subprogram:
18876 main_subprogram = DW_UNSND (&attr);
18877 break;
18878
18879 case DW_AT_ranges:
18880 {
18881 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18882 but that requires a full DIE, so instead we just
18883 reimplement it. */
18884 int need_ranges_base = tag != DW_TAG_compile_unit;
18885 unsigned int ranges_offset = (DW_UNSND (&attr)
18886 + (need_ranges_base
18887 ? cu->ranges_base
18888 : 0));
18889
18890 /* Value of the DW_AT_ranges attribute is the offset in the
18891 .debug_ranges section. */
18892 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18893 nullptr))
18894 has_pc_info = 1;
18895 }
18896 break;
18897
18898 default:
18899 break;
18900 }
18901 }
18902
18903 /* For Ada, if both the name and the linkage name appear, we prefer
18904 the latter. This lets "catch exception" work better, regardless
18905 of the order in which the name and linkage name were emitted.
18906 Really, though, this is just a workaround for the fact that gdb
18907 doesn't store both the name and the linkage name. */
18908 if (cu->language == language_ada && linkage_name != nullptr)
18909 name = linkage_name;
18910
18911 if (high_pc_relative)
18912 highpc += lowpc;
18913
18914 if (has_low_pc_attr && has_high_pc_attr)
18915 {
18916 /* When using the GNU linker, .gnu.linkonce. sections are used to
18917 eliminate duplicate copies of functions and vtables and such.
18918 The linker will arbitrarily choose one and discard the others.
18919 The AT_*_pc values for such functions refer to local labels in
18920 these sections. If the section from that file was discarded, the
18921 labels are not in the output, so the relocs get a value of 0.
18922 If this is a discarded function, mark the pc bounds as invalid,
18923 so that GDB will ignore it. */
18924 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18925 {
18926 struct objfile *objfile = dwarf2_per_objfile->objfile;
18927 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18928
18929 complaint (_("DW_AT_low_pc %s is zero "
18930 "for DIE at %s [in module %s]"),
18931 paddress (gdbarch, lowpc),
18932 sect_offset_str (sect_off),
18933 objfile_name (objfile));
18934 }
18935 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18936 else if (lowpc >= highpc)
18937 {
18938 struct objfile *objfile = dwarf2_per_objfile->objfile;
18939 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18940
18941 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18942 "for DIE at %s [in module %s]"),
18943 paddress (gdbarch, lowpc),
18944 paddress (gdbarch, highpc),
18945 sect_offset_str (sect_off),
18946 objfile_name (objfile));
18947 }
18948 else
18949 has_pc_info = 1;
18950 }
18951
18952 return info_ptr;
18953 }
18954
18955 /* Find a cached partial DIE at OFFSET in CU. */
18956
18957 struct partial_die_info *
18958 dwarf2_cu::find_partial_die (sect_offset sect_off)
18959 {
18960 struct partial_die_info *lookup_die = NULL;
18961 struct partial_die_info part_die (sect_off);
18962
18963 lookup_die = ((struct partial_die_info *)
18964 htab_find_with_hash (partial_dies, &part_die,
18965 to_underlying (sect_off)));
18966
18967 return lookup_die;
18968 }
18969
18970 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18971 except in the case of .debug_types DIEs which do not reference
18972 outside their CU (they do however referencing other types via
18973 DW_FORM_ref_sig8). */
18974
18975 static const struct cu_partial_die_info
18976 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18977 {
18978 struct dwarf2_per_objfile *dwarf2_per_objfile
18979 = cu->per_cu->dwarf2_per_objfile;
18980 struct objfile *objfile = dwarf2_per_objfile->objfile;
18981 struct dwarf2_per_cu_data *per_cu = NULL;
18982 struct partial_die_info *pd = NULL;
18983
18984 if (offset_in_dwz == cu->per_cu->is_dwz
18985 && offset_in_cu_p (&cu->header, sect_off))
18986 {
18987 pd = cu->find_partial_die (sect_off);
18988 if (pd != NULL)
18989 return { cu, pd };
18990 /* We missed recording what we needed.
18991 Load all dies and try again. */
18992 per_cu = cu->per_cu;
18993 }
18994 else
18995 {
18996 /* TUs don't reference other CUs/TUs (except via type signatures). */
18997 if (cu->per_cu->is_debug_types)
18998 {
18999 error (_("Dwarf Error: Type Unit at offset %s contains"
19000 " external reference to offset %s [in module %s].\n"),
19001 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19002 bfd_get_filename (objfile->obfd));
19003 }
19004 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19005 dwarf2_per_objfile);
19006
19007 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19008 load_partial_comp_unit (per_cu);
19009
19010 per_cu->cu->last_used = 0;
19011 pd = per_cu->cu->find_partial_die (sect_off);
19012 }
19013
19014 /* If we didn't find it, and not all dies have been loaded,
19015 load them all and try again. */
19016
19017 if (pd == NULL && per_cu->load_all_dies == 0)
19018 {
19019 per_cu->load_all_dies = 1;
19020
19021 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19022 THIS_CU->cu may already be in use. So we can't just free it and
19023 replace its DIEs with the ones we read in. Instead, we leave those
19024 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19025 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19026 set. */
19027 load_partial_comp_unit (per_cu);
19028
19029 pd = per_cu->cu->find_partial_die (sect_off);
19030 }
19031
19032 if (pd == NULL)
19033 internal_error (__FILE__, __LINE__,
19034 _("could not find partial DIE %s "
19035 "in cache [from module %s]\n"),
19036 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19037 return { per_cu->cu, pd };
19038 }
19039
19040 /* See if we can figure out if the class lives in a namespace. We do
19041 this by looking for a member function; its demangled name will
19042 contain namespace info, if there is any. */
19043
19044 static void
19045 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19046 struct dwarf2_cu *cu)
19047 {
19048 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19049 what template types look like, because the demangler
19050 frequently doesn't give the same name as the debug info. We
19051 could fix this by only using the demangled name to get the
19052 prefix (but see comment in read_structure_type). */
19053
19054 struct partial_die_info *real_pdi;
19055 struct partial_die_info *child_pdi;
19056
19057 /* If this DIE (this DIE's specification, if any) has a parent, then
19058 we should not do this. We'll prepend the parent's fully qualified
19059 name when we create the partial symbol. */
19060
19061 real_pdi = struct_pdi;
19062 while (real_pdi->has_specification)
19063 {
19064 auto res = find_partial_die (real_pdi->spec_offset,
19065 real_pdi->spec_is_dwz, cu);
19066 real_pdi = res.pdi;
19067 cu = res.cu;
19068 }
19069
19070 if (real_pdi->die_parent != NULL)
19071 return;
19072
19073 for (child_pdi = struct_pdi->die_child;
19074 child_pdi != NULL;
19075 child_pdi = child_pdi->die_sibling)
19076 {
19077 if (child_pdi->tag == DW_TAG_subprogram
19078 && child_pdi->linkage_name != NULL)
19079 {
19080 char *actual_class_name
19081 = language_class_name_from_physname (cu->language_defn,
19082 child_pdi->linkage_name);
19083 if (actual_class_name != NULL)
19084 {
19085 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19086 struct_pdi->name
19087 = obstack_strdup (&objfile->per_bfd->storage_obstack,
19088 actual_class_name);
19089 xfree (actual_class_name);
19090 }
19091 break;
19092 }
19093 }
19094 }
19095
19096 void
19097 partial_die_info::fixup (struct dwarf2_cu *cu)
19098 {
19099 /* Once we've fixed up a die, there's no point in doing so again.
19100 This also avoids a memory leak if we were to call
19101 guess_partial_die_structure_name multiple times. */
19102 if (fixup_called)
19103 return;
19104
19105 /* If we found a reference attribute and the DIE has no name, try
19106 to find a name in the referred to DIE. */
19107
19108 if (name == NULL && has_specification)
19109 {
19110 struct partial_die_info *spec_die;
19111
19112 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19113 spec_die = res.pdi;
19114 cu = res.cu;
19115
19116 spec_die->fixup (cu);
19117
19118 if (spec_die->name)
19119 {
19120 name = spec_die->name;
19121
19122 /* Copy DW_AT_external attribute if it is set. */
19123 if (spec_die->is_external)
19124 is_external = spec_die->is_external;
19125 }
19126 }
19127
19128 /* Set default names for some unnamed DIEs. */
19129
19130 if (name == NULL && tag == DW_TAG_namespace)
19131 name = CP_ANONYMOUS_NAMESPACE_STR;
19132
19133 /* If there is no parent die to provide a namespace, and there are
19134 children, see if we can determine the namespace from their linkage
19135 name. */
19136 if (cu->language == language_cplus
19137 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
19138 && die_parent == NULL
19139 && has_children
19140 && (tag == DW_TAG_class_type
19141 || tag == DW_TAG_structure_type
19142 || tag == DW_TAG_union_type))
19143 guess_partial_die_structure_name (this, cu);
19144
19145 /* GCC might emit a nameless struct or union that has a linkage
19146 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19147 if (name == NULL
19148 && (tag == DW_TAG_class_type
19149 || tag == DW_TAG_interface_type
19150 || tag == DW_TAG_structure_type
19151 || tag == DW_TAG_union_type)
19152 && linkage_name != NULL)
19153 {
19154 char *demangled;
19155
19156 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
19157 if (demangled)
19158 {
19159 const char *base;
19160
19161 /* Strip any leading namespaces/classes, keep only the base name.
19162 DW_AT_name for named DIEs does not contain the prefixes. */
19163 base = strrchr (demangled, ':');
19164 if (base && base > demangled && base[-1] == ':')
19165 base++;
19166 else
19167 base = demangled;
19168
19169 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19170 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
19171 xfree (demangled);
19172 }
19173 }
19174
19175 fixup_called = 1;
19176 }
19177
19178 /* Read an attribute value described by an attribute form. */
19179
19180 static const gdb_byte *
19181 read_attribute_value (const struct die_reader_specs *reader,
19182 struct attribute *attr, unsigned form,
19183 LONGEST implicit_const, const gdb_byte *info_ptr)
19184 {
19185 struct dwarf2_cu *cu = reader->cu;
19186 struct dwarf2_per_objfile *dwarf2_per_objfile
19187 = cu->per_cu->dwarf2_per_objfile;
19188 struct objfile *objfile = dwarf2_per_objfile->objfile;
19189 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19190 bfd *abfd = reader->abfd;
19191 struct comp_unit_head *cu_header = &cu->header;
19192 unsigned int bytes_read;
19193 struct dwarf_block *blk;
19194
19195 attr->form = (enum dwarf_form) form;
19196 switch (form)
19197 {
19198 case DW_FORM_ref_addr:
19199 if (cu->header.version == 2)
19200 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19201 else
19202 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19203 &cu->header, &bytes_read);
19204 info_ptr += bytes_read;
19205 break;
19206 case DW_FORM_GNU_ref_alt:
19207 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19208 info_ptr += bytes_read;
19209 break;
19210 case DW_FORM_addr:
19211 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19212 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19213 info_ptr += bytes_read;
19214 break;
19215 case DW_FORM_block2:
19216 blk = dwarf_alloc_block (cu);
19217 blk->size = read_2_bytes (abfd, info_ptr);
19218 info_ptr += 2;
19219 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19220 info_ptr += blk->size;
19221 DW_BLOCK (attr) = blk;
19222 break;
19223 case DW_FORM_block4:
19224 blk = dwarf_alloc_block (cu);
19225 blk->size = read_4_bytes (abfd, info_ptr);
19226 info_ptr += 4;
19227 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19228 info_ptr += blk->size;
19229 DW_BLOCK (attr) = blk;
19230 break;
19231 case DW_FORM_data2:
19232 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19233 info_ptr += 2;
19234 break;
19235 case DW_FORM_data4:
19236 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19237 info_ptr += 4;
19238 break;
19239 case DW_FORM_data8:
19240 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19241 info_ptr += 8;
19242 break;
19243 case DW_FORM_data16:
19244 blk = dwarf_alloc_block (cu);
19245 blk->size = 16;
19246 blk->data = read_n_bytes (abfd, info_ptr, 16);
19247 info_ptr += 16;
19248 DW_BLOCK (attr) = blk;
19249 break;
19250 case DW_FORM_sec_offset:
19251 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19252 info_ptr += bytes_read;
19253 break;
19254 case DW_FORM_string:
19255 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19256 DW_STRING_IS_CANONICAL (attr) = 0;
19257 info_ptr += bytes_read;
19258 break;
19259 case DW_FORM_strp:
19260 if (!cu->per_cu->is_dwz)
19261 {
19262 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19263 abfd, info_ptr, cu_header,
19264 &bytes_read);
19265 DW_STRING_IS_CANONICAL (attr) = 0;
19266 info_ptr += bytes_read;
19267 break;
19268 }
19269 /* FALLTHROUGH */
19270 case DW_FORM_line_strp:
19271 if (!cu->per_cu->is_dwz)
19272 {
19273 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19274 abfd, info_ptr,
19275 cu_header, &bytes_read);
19276 DW_STRING_IS_CANONICAL (attr) = 0;
19277 info_ptr += bytes_read;
19278 break;
19279 }
19280 /* FALLTHROUGH */
19281 case DW_FORM_GNU_strp_alt:
19282 {
19283 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19284 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19285 &bytes_read);
19286
19287 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19288 dwz, str_offset);
19289 DW_STRING_IS_CANONICAL (attr) = 0;
19290 info_ptr += bytes_read;
19291 }
19292 break;
19293 case DW_FORM_exprloc:
19294 case DW_FORM_block:
19295 blk = dwarf_alloc_block (cu);
19296 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19297 info_ptr += bytes_read;
19298 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19299 info_ptr += blk->size;
19300 DW_BLOCK (attr) = blk;
19301 break;
19302 case DW_FORM_block1:
19303 blk = dwarf_alloc_block (cu);
19304 blk->size = read_1_byte (abfd, info_ptr);
19305 info_ptr += 1;
19306 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19307 info_ptr += blk->size;
19308 DW_BLOCK (attr) = blk;
19309 break;
19310 case DW_FORM_data1:
19311 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19312 info_ptr += 1;
19313 break;
19314 case DW_FORM_flag:
19315 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19316 info_ptr += 1;
19317 break;
19318 case DW_FORM_flag_present:
19319 DW_UNSND (attr) = 1;
19320 break;
19321 case DW_FORM_sdata:
19322 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19323 info_ptr += bytes_read;
19324 break;
19325 case DW_FORM_udata:
19326 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19327 info_ptr += bytes_read;
19328 break;
19329 case DW_FORM_ref1:
19330 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19331 + read_1_byte (abfd, info_ptr));
19332 info_ptr += 1;
19333 break;
19334 case DW_FORM_ref2:
19335 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19336 + read_2_bytes (abfd, info_ptr));
19337 info_ptr += 2;
19338 break;
19339 case DW_FORM_ref4:
19340 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19341 + read_4_bytes (abfd, info_ptr));
19342 info_ptr += 4;
19343 break;
19344 case DW_FORM_ref8:
19345 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19346 + read_8_bytes (abfd, info_ptr));
19347 info_ptr += 8;
19348 break;
19349 case DW_FORM_ref_sig8:
19350 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19351 info_ptr += 8;
19352 break;
19353 case DW_FORM_ref_udata:
19354 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19355 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19356 info_ptr += bytes_read;
19357 break;
19358 case DW_FORM_indirect:
19359 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19360 info_ptr += bytes_read;
19361 if (form == DW_FORM_implicit_const)
19362 {
19363 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19364 info_ptr += bytes_read;
19365 }
19366 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19367 info_ptr);
19368 break;
19369 case DW_FORM_implicit_const:
19370 DW_SND (attr) = implicit_const;
19371 break;
19372 case DW_FORM_addrx:
19373 case DW_FORM_GNU_addr_index:
19374 if (reader->dwo_file == NULL)
19375 {
19376 /* For now flag a hard error.
19377 Later we can turn this into a complaint. */
19378 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19379 dwarf_form_name (form),
19380 bfd_get_filename (abfd));
19381 }
19382 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19383 info_ptr += bytes_read;
19384 break;
19385 case DW_FORM_strx:
19386 case DW_FORM_strx1:
19387 case DW_FORM_strx2:
19388 case DW_FORM_strx3:
19389 case DW_FORM_strx4:
19390 case DW_FORM_GNU_str_index:
19391 if (reader->dwo_file == NULL)
19392 {
19393 /* For now flag a hard error.
19394 Later we can turn this into a complaint if warranted. */
19395 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19396 dwarf_form_name (form),
19397 bfd_get_filename (abfd));
19398 }
19399 {
19400 ULONGEST str_index;
19401 if (form == DW_FORM_strx1)
19402 {
19403 str_index = read_1_byte (abfd, info_ptr);
19404 info_ptr += 1;
19405 }
19406 else if (form == DW_FORM_strx2)
19407 {
19408 str_index = read_2_bytes (abfd, info_ptr);
19409 info_ptr += 2;
19410 }
19411 else if (form == DW_FORM_strx3)
19412 {
19413 str_index = read_3_bytes (abfd, info_ptr);
19414 info_ptr += 3;
19415 }
19416 else if (form == DW_FORM_strx4)
19417 {
19418 str_index = read_4_bytes (abfd, info_ptr);
19419 info_ptr += 4;
19420 }
19421 else
19422 {
19423 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19424 info_ptr += bytes_read;
19425 }
19426 DW_STRING (attr) = read_str_index (reader, str_index);
19427 DW_STRING_IS_CANONICAL (attr) = 0;
19428 }
19429 break;
19430 default:
19431 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19432 dwarf_form_name (form),
19433 bfd_get_filename (abfd));
19434 }
19435
19436 /* Super hack. */
19437 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19438 attr->form = DW_FORM_GNU_ref_alt;
19439
19440 /* We have seen instances where the compiler tried to emit a byte
19441 size attribute of -1 which ended up being encoded as an unsigned
19442 0xffffffff. Although 0xffffffff is technically a valid size value,
19443 an object of this size seems pretty unlikely so we can relatively
19444 safely treat these cases as if the size attribute was invalid and
19445 treat them as zero by default. */
19446 if (attr->name == DW_AT_byte_size
19447 && form == DW_FORM_data4
19448 && DW_UNSND (attr) >= 0xffffffff)
19449 {
19450 complaint
19451 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19452 hex_string (DW_UNSND (attr)));
19453 DW_UNSND (attr) = 0;
19454 }
19455
19456 return info_ptr;
19457 }
19458
19459 /* Read an attribute described by an abbreviated attribute. */
19460
19461 static const gdb_byte *
19462 read_attribute (const struct die_reader_specs *reader,
19463 struct attribute *attr, struct attr_abbrev *abbrev,
19464 const gdb_byte *info_ptr)
19465 {
19466 attr->name = abbrev->name;
19467 return read_attribute_value (reader, attr, abbrev->form,
19468 abbrev->implicit_const, info_ptr);
19469 }
19470
19471 /* Read dwarf information from a buffer. */
19472
19473 static unsigned int
19474 read_1_byte (bfd *abfd, const gdb_byte *buf)
19475 {
19476 return bfd_get_8 (abfd, buf);
19477 }
19478
19479 static int
19480 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19481 {
19482 return bfd_get_signed_8 (abfd, buf);
19483 }
19484
19485 static unsigned int
19486 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19487 {
19488 return bfd_get_16 (abfd, buf);
19489 }
19490
19491 static int
19492 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19493 {
19494 return bfd_get_signed_16 (abfd, buf);
19495 }
19496
19497 static unsigned int
19498 read_3_bytes (bfd *abfd, const gdb_byte *buf)
19499 {
19500 unsigned int result = 0;
19501 for (int i = 0; i < 3; ++i)
19502 {
19503 unsigned char byte = bfd_get_8 (abfd, buf);
19504 buf++;
19505 result |= ((unsigned int) byte << (i * 8));
19506 }
19507 return result;
19508 }
19509
19510 static unsigned int
19511 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19512 {
19513 return bfd_get_32 (abfd, buf);
19514 }
19515
19516 static int
19517 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19518 {
19519 return bfd_get_signed_32 (abfd, buf);
19520 }
19521
19522 static ULONGEST
19523 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19524 {
19525 return bfd_get_64 (abfd, buf);
19526 }
19527
19528 static CORE_ADDR
19529 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19530 unsigned int *bytes_read)
19531 {
19532 struct comp_unit_head *cu_header = &cu->header;
19533 CORE_ADDR retval = 0;
19534
19535 if (cu_header->signed_addr_p)
19536 {
19537 switch (cu_header->addr_size)
19538 {
19539 case 2:
19540 retval = bfd_get_signed_16 (abfd, buf);
19541 break;
19542 case 4:
19543 retval = bfd_get_signed_32 (abfd, buf);
19544 break;
19545 case 8:
19546 retval = bfd_get_signed_64 (abfd, buf);
19547 break;
19548 default:
19549 internal_error (__FILE__, __LINE__,
19550 _("read_address: bad switch, signed [in module %s]"),
19551 bfd_get_filename (abfd));
19552 }
19553 }
19554 else
19555 {
19556 switch (cu_header->addr_size)
19557 {
19558 case 2:
19559 retval = bfd_get_16 (abfd, buf);
19560 break;
19561 case 4:
19562 retval = bfd_get_32 (abfd, buf);
19563 break;
19564 case 8:
19565 retval = bfd_get_64 (abfd, buf);
19566 break;
19567 default:
19568 internal_error (__FILE__, __LINE__,
19569 _("read_address: bad switch, "
19570 "unsigned [in module %s]"),
19571 bfd_get_filename (abfd));
19572 }
19573 }
19574
19575 *bytes_read = cu_header->addr_size;
19576 return retval;
19577 }
19578
19579 /* Read the initial length from a section. The (draft) DWARF 3
19580 specification allows the initial length to take up either 4 bytes
19581 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19582 bytes describe the length and all offsets will be 8 bytes in length
19583 instead of 4.
19584
19585 An older, non-standard 64-bit format is also handled by this
19586 function. The older format in question stores the initial length
19587 as an 8-byte quantity without an escape value. Lengths greater
19588 than 2^32 aren't very common which means that the initial 4 bytes
19589 is almost always zero. Since a length value of zero doesn't make
19590 sense for the 32-bit format, this initial zero can be considered to
19591 be an escape value which indicates the presence of the older 64-bit
19592 format. As written, the code can't detect (old format) lengths
19593 greater than 4GB. If it becomes necessary to handle lengths
19594 somewhat larger than 4GB, we could allow other small values (such
19595 as the non-sensical values of 1, 2, and 3) to also be used as
19596 escape values indicating the presence of the old format.
19597
19598 The value returned via bytes_read should be used to increment the
19599 relevant pointer after calling read_initial_length().
19600
19601 [ Note: read_initial_length() and read_offset() are based on the
19602 document entitled "DWARF Debugging Information Format", revision
19603 3, draft 8, dated November 19, 2001. This document was obtained
19604 from:
19605
19606 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19607
19608 This document is only a draft and is subject to change. (So beware.)
19609
19610 Details regarding the older, non-standard 64-bit format were
19611 determined empirically by examining 64-bit ELF files produced by
19612 the SGI toolchain on an IRIX 6.5 machine.
19613
19614 - Kevin, July 16, 2002
19615 ] */
19616
19617 static LONGEST
19618 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19619 {
19620 LONGEST length = bfd_get_32 (abfd, buf);
19621
19622 if (length == 0xffffffff)
19623 {
19624 length = bfd_get_64 (abfd, buf + 4);
19625 *bytes_read = 12;
19626 }
19627 else if (length == 0)
19628 {
19629 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19630 length = bfd_get_64 (abfd, buf);
19631 *bytes_read = 8;
19632 }
19633 else
19634 {
19635 *bytes_read = 4;
19636 }
19637
19638 return length;
19639 }
19640
19641 /* Cover function for read_initial_length.
19642 Returns the length of the object at BUF, and stores the size of the
19643 initial length in *BYTES_READ and stores the size that offsets will be in
19644 *OFFSET_SIZE.
19645 If the initial length size is not equivalent to that specified in
19646 CU_HEADER then issue a complaint.
19647 This is useful when reading non-comp-unit headers. */
19648
19649 static LONGEST
19650 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19651 const struct comp_unit_head *cu_header,
19652 unsigned int *bytes_read,
19653 unsigned int *offset_size)
19654 {
19655 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19656
19657 gdb_assert (cu_header->initial_length_size == 4
19658 || cu_header->initial_length_size == 8
19659 || cu_header->initial_length_size == 12);
19660
19661 if (cu_header->initial_length_size != *bytes_read)
19662 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
19663
19664 *offset_size = (*bytes_read == 4) ? 4 : 8;
19665 return length;
19666 }
19667
19668 /* Read an offset from the data stream. The size of the offset is
19669 given by cu_header->offset_size. */
19670
19671 static LONGEST
19672 read_offset (bfd *abfd, const gdb_byte *buf,
19673 const struct comp_unit_head *cu_header,
19674 unsigned int *bytes_read)
19675 {
19676 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19677
19678 *bytes_read = cu_header->offset_size;
19679 return offset;
19680 }
19681
19682 /* Read an offset from the data stream. */
19683
19684 static LONGEST
19685 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19686 {
19687 LONGEST retval = 0;
19688
19689 switch (offset_size)
19690 {
19691 case 4:
19692 retval = bfd_get_32 (abfd, buf);
19693 break;
19694 case 8:
19695 retval = bfd_get_64 (abfd, buf);
19696 break;
19697 default:
19698 internal_error (__FILE__, __LINE__,
19699 _("read_offset_1: bad switch [in module %s]"),
19700 bfd_get_filename (abfd));
19701 }
19702
19703 return retval;
19704 }
19705
19706 static const gdb_byte *
19707 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19708 {
19709 /* If the size of a host char is 8 bits, we can return a pointer
19710 to the buffer, otherwise we have to copy the data to a buffer
19711 allocated on the temporary obstack. */
19712 gdb_assert (HOST_CHAR_BIT == 8);
19713 return buf;
19714 }
19715
19716 static const char *
19717 read_direct_string (bfd *abfd, const gdb_byte *buf,
19718 unsigned int *bytes_read_ptr)
19719 {
19720 /* If the size of a host char is 8 bits, we can return a pointer
19721 to the string, otherwise we have to copy the string to a buffer
19722 allocated on the temporary obstack. */
19723 gdb_assert (HOST_CHAR_BIT == 8);
19724 if (*buf == '\0')
19725 {
19726 *bytes_read_ptr = 1;
19727 return NULL;
19728 }
19729 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19730 return (const char *) buf;
19731 }
19732
19733 /* Return pointer to string at section SECT offset STR_OFFSET with error
19734 reporting strings FORM_NAME and SECT_NAME. */
19735
19736 static const char *
19737 read_indirect_string_at_offset_from (struct objfile *objfile,
19738 bfd *abfd, LONGEST str_offset,
19739 struct dwarf2_section_info *sect,
19740 const char *form_name,
19741 const char *sect_name)
19742 {
19743 dwarf2_read_section (objfile, sect);
19744 if (sect->buffer == NULL)
19745 error (_("%s used without %s section [in module %s]"),
19746 form_name, sect_name, bfd_get_filename (abfd));
19747 if (str_offset >= sect->size)
19748 error (_("%s pointing outside of %s section [in module %s]"),
19749 form_name, sect_name, bfd_get_filename (abfd));
19750 gdb_assert (HOST_CHAR_BIT == 8);
19751 if (sect->buffer[str_offset] == '\0')
19752 return NULL;
19753 return (const char *) (sect->buffer + str_offset);
19754 }
19755
19756 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19757
19758 static const char *
19759 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19760 bfd *abfd, LONGEST str_offset)
19761 {
19762 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19763 abfd, str_offset,
19764 &dwarf2_per_objfile->str,
19765 "DW_FORM_strp", ".debug_str");
19766 }
19767
19768 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19769
19770 static const char *
19771 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19772 bfd *abfd, LONGEST str_offset)
19773 {
19774 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19775 abfd, str_offset,
19776 &dwarf2_per_objfile->line_str,
19777 "DW_FORM_line_strp",
19778 ".debug_line_str");
19779 }
19780
19781 /* Read a string at offset STR_OFFSET in the .debug_str section from
19782 the .dwz file DWZ. Throw an error if the offset is too large. If
19783 the string consists of a single NUL byte, return NULL; otherwise
19784 return a pointer to the string. */
19785
19786 static const char *
19787 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19788 LONGEST str_offset)
19789 {
19790 dwarf2_read_section (objfile, &dwz->str);
19791
19792 if (dwz->str.buffer == NULL)
19793 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19794 "section [in module %s]"),
19795 bfd_get_filename (dwz->dwz_bfd.get ()));
19796 if (str_offset >= dwz->str.size)
19797 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19798 ".debug_str section [in module %s]"),
19799 bfd_get_filename (dwz->dwz_bfd.get ()));
19800 gdb_assert (HOST_CHAR_BIT == 8);
19801 if (dwz->str.buffer[str_offset] == '\0')
19802 return NULL;
19803 return (const char *) (dwz->str.buffer + str_offset);
19804 }
19805
19806 /* Return pointer to string at .debug_str offset as read from BUF.
19807 BUF is assumed to be in a compilation unit described by CU_HEADER.
19808 Return *BYTES_READ_PTR count of bytes read from BUF. */
19809
19810 static const char *
19811 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19812 const gdb_byte *buf,
19813 const struct comp_unit_head *cu_header,
19814 unsigned int *bytes_read_ptr)
19815 {
19816 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19817
19818 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19819 }
19820
19821 /* Return pointer to string at .debug_line_str offset as read from BUF.
19822 BUF is assumed to be in a compilation unit described by CU_HEADER.
19823 Return *BYTES_READ_PTR count of bytes read from BUF. */
19824
19825 static const char *
19826 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19827 bfd *abfd, const gdb_byte *buf,
19828 const struct comp_unit_head *cu_header,
19829 unsigned int *bytes_read_ptr)
19830 {
19831 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19832
19833 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19834 str_offset);
19835 }
19836
19837 ULONGEST
19838 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19839 unsigned int *bytes_read_ptr)
19840 {
19841 ULONGEST result;
19842 unsigned int num_read;
19843 int shift;
19844 unsigned char byte;
19845
19846 result = 0;
19847 shift = 0;
19848 num_read = 0;
19849 while (1)
19850 {
19851 byte = bfd_get_8 (abfd, buf);
19852 buf++;
19853 num_read++;
19854 result |= ((ULONGEST) (byte & 127) << shift);
19855 if ((byte & 128) == 0)
19856 {
19857 break;
19858 }
19859 shift += 7;
19860 }
19861 *bytes_read_ptr = num_read;
19862 return result;
19863 }
19864
19865 static LONGEST
19866 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19867 unsigned int *bytes_read_ptr)
19868 {
19869 ULONGEST result;
19870 int shift, num_read;
19871 unsigned char byte;
19872
19873 result = 0;
19874 shift = 0;
19875 num_read = 0;
19876 while (1)
19877 {
19878 byte = bfd_get_8 (abfd, buf);
19879 buf++;
19880 num_read++;
19881 result |= ((ULONGEST) (byte & 127) << shift);
19882 shift += 7;
19883 if ((byte & 128) == 0)
19884 {
19885 break;
19886 }
19887 }
19888 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19889 result |= -(((ULONGEST) 1) << shift);
19890 *bytes_read_ptr = num_read;
19891 return result;
19892 }
19893
19894 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19895 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19896 ADDR_SIZE is the size of addresses from the CU header. */
19897
19898 static CORE_ADDR
19899 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19900 unsigned int addr_index, ULONGEST addr_base, int addr_size)
19901 {
19902 struct objfile *objfile = dwarf2_per_objfile->objfile;
19903 bfd *abfd = objfile->obfd;
19904 const gdb_byte *info_ptr;
19905
19906 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19907 if (dwarf2_per_objfile->addr.buffer == NULL)
19908 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19909 objfile_name (objfile));
19910 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19911 error (_("DW_FORM_addr_index pointing outside of "
19912 ".debug_addr section [in module %s]"),
19913 objfile_name (objfile));
19914 info_ptr = (dwarf2_per_objfile->addr.buffer
19915 + addr_base + addr_index * addr_size);
19916 if (addr_size == 4)
19917 return bfd_get_32 (abfd, info_ptr);
19918 else
19919 return bfd_get_64 (abfd, info_ptr);
19920 }
19921
19922 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19923
19924 static CORE_ADDR
19925 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19926 {
19927 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19928 cu->addr_base, cu->header.addr_size);
19929 }
19930
19931 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19932
19933 static CORE_ADDR
19934 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19935 unsigned int *bytes_read)
19936 {
19937 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19938 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19939
19940 return read_addr_index (cu, addr_index);
19941 }
19942
19943 /* Data structure to pass results from dwarf2_read_addr_index_reader
19944 back to dwarf2_read_addr_index. */
19945
19946 struct dwarf2_read_addr_index_data
19947 {
19948 ULONGEST addr_base;
19949 int addr_size;
19950 };
19951
19952 /* die_reader_func for dwarf2_read_addr_index. */
19953
19954 static void
19955 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19956 const gdb_byte *info_ptr,
19957 struct die_info *comp_unit_die,
19958 int has_children,
19959 void *data)
19960 {
19961 struct dwarf2_cu *cu = reader->cu;
19962 struct dwarf2_read_addr_index_data *aidata =
19963 (struct dwarf2_read_addr_index_data *) data;
19964
19965 aidata->addr_base = cu->addr_base;
19966 aidata->addr_size = cu->header.addr_size;
19967 }
19968
19969 /* Given an index in .debug_addr, fetch the value.
19970 NOTE: This can be called during dwarf expression evaluation,
19971 long after the debug information has been read, and thus per_cu->cu
19972 may no longer exist. */
19973
19974 CORE_ADDR
19975 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19976 unsigned int addr_index)
19977 {
19978 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19979 struct dwarf2_cu *cu = per_cu->cu;
19980 ULONGEST addr_base;
19981 int addr_size;
19982
19983 /* We need addr_base and addr_size.
19984 If we don't have PER_CU->cu, we have to get it.
19985 Nasty, but the alternative is storing the needed info in PER_CU,
19986 which at this point doesn't seem justified: it's not clear how frequently
19987 it would get used and it would increase the size of every PER_CU.
19988 Entry points like dwarf2_per_cu_addr_size do a similar thing
19989 so we're not in uncharted territory here.
19990 Alas we need to be a bit more complicated as addr_base is contained
19991 in the DIE.
19992
19993 We don't need to read the entire CU(/TU).
19994 We just need the header and top level die.
19995
19996 IWBN to use the aging mechanism to let us lazily later discard the CU.
19997 For now we skip this optimization. */
19998
19999 if (cu != NULL)
20000 {
20001 addr_base = cu->addr_base;
20002 addr_size = cu->header.addr_size;
20003 }
20004 else
20005 {
20006 struct dwarf2_read_addr_index_data aidata;
20007
20008 /* Note: We can't use init_cutu_and_read_dies_simple here,
20009 we need addr_base. */
20010 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
20011 dwarf2_read_addr_index_reader, &aidata);
20012 addr_base = aidata.addr_base;
20013 addr_size = aidata.addr_size;
20014 }
20015
20016 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20017 addr_size);
20018 }
20019
20020 /* Given a DW_FORM_GNU_str_index or DW_FORM_strx, fetch the string.
20021 This is only used by the Fission support. */
20022
20023 static const char *
20024 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20025 {
20026 struct dwarf2_cu *cu = reader->cu;
20027 struct dwarf2_per_objfile *dwarf2_per_objfile
20028 = cu->per_cu->dwarf2_per_objfile;
20029 struct objfile *objfile = dwarf2_per_objfile->objfile;
20030 const char *objf_name = objfile_name (objfile);
20031 bfd *abfd = objfile->obfd;
20032 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20033 struct dwarf2_section_info *str_offsets_section =
20034 &reader->dwo_file->sections.str_offsets;
20035 const gdb_byte *info_ptr;
20036 ULONGEST str_offset;
20037 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20038
20039 dwarf2_read_section (objfile, str_section);
20040 dwarf2_read_section (objfile, str_offsets_section);
20041 if (str_section->buffer == NULL)
20042 error (_("%s used without .debug_str.dwo section"
20043 " in CU at offset %s [in module %s]"),
20044 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20045 if (str_offsets_section->buffer == NULL)
20046 error (_("%s used without .debug_str_offsets.dwo section"
20047 " in CU at offset %s [in module %s]"),
20048 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20049 if (str_index * cu->header.offset_size >= str_offsets_section->size)
20050 error (_("%s pointing outside of .debug_str_offsets.dwo"
20051 " section in CU at offset %s [in module %s]"),
20052 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20053 info_ptr = (str_offsets_section->buffer
20054 + str_index * cu->header.offset_size);
20055 if (cu->header.offset_size == 4)
20056 str_offset = bfd_get_32 (abfd, info_ptr);
20057 else
20058 str_offset = bfd_get_64 (abfd, info_ptr);
20059 if (str_offset >= str_section->size)
20060 error (_("Offset from %s pointing outside of"
20061 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20062 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20063 return (const char *) (str_section->buffer + str_offset);
20064 }
20065
20066 /* Return the length of an LEB128 number in BUF. */
20067
20068 static int
20069 leb128_size (const gdb_byte *buf)
20070 {
20071 const gdb_byte *begin = buf;
20072 gdb_byte byte;
20073
20074 while (1)
20075 {
20076 byte = *buf++;
20077 if ((byte & 128) == 0)
20078 return buf - begin;
20079 }
20080 }
20081
20082 static void
20083 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20084 {
20085 switch (lang)
20086 {
20087 case DW_LANG_C89:
20088 case DW_LANG_C99:
20089 case DW_LANG_C11:
20090 case DW_LANG_C:
20091 case DW_LANG_UPC:
20092 cu->language = language_c;
20093 break;
20094 case DW_LANG_Java:
20095 case DW_LANG_C_plus_plus:
20096 case DW_LANG_C_plus_plus_11:
20097 case DW_LANG_C_plus_plus_14:
20098 cu->language = language_cplus;
20099 break;
20100 case DW_LANG_D:
20101 cu->language = language_d;
20102 break;
20103 case DW_LANG_Fortran77:
20104 case DW_LANG_Fortran90:
20105 case DW_LANG_Fortran95:
20106 case DW_LANG_Fortran03:
20107 case DW_LANG_Fortran08:
20108 cu->language = language_fortran;
20109 break;
20110 case DW_LANG_Go:
20111 cu->language = language_go;
20112 break;
20113 case DW_LANG_Mips_Assembler:
20114 cu->language = language_asm;
20115 break;
20116 case DW_LANG_Ada83:
20117 case DW_LANG_Ada95:
20118 cu->language = language_ada;
20119 break;
20120 case DW_LANG_Modula2:
20121 cu->language = language_m2;
20122 break;
20123 case DW_LANG_Pascal83:
20124 cu->language = language_pascal;
20125 break;
20126 case DW_LANG_ObjC:
20127 cu->language = language_objc;
20128 break;
20129 case DW_LANG_Rust:
20130 case DW_LANG_Rust_old:
20131 cu->language = language_rust;
20132 break;
20133 case DW_LANG_Cobol74:
20134 case DW_LANG_Cobol85:
20135 default:
20136 cu->language = language_minimal;
20137 break;
20138 }
20139 cu->language_defn = language_def (cu->language);
20140 }
20141
20142 /* Return the named attribute or NULL if not there. */
20143
20144 static struct attribute *
20145 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20146 {
20147 for (;;)
20148 {
20149 unsigned int i;
20150 struct attribute *spec = NULL;
20151
20152 for (i = 0; i < die->num_attrs; ++i)
20153 {
20154 if (die->attrs[i].name == name)
20155 return &die->attrs[i];
20156 if (die->attrs[i].name == DW_AT_specification
20157 || die->attrs[i].name == DW_AT_abstract_origin)
20158 spec = &die->attrs[i];
20159 }
20160
20161 if (!spec)
20162 break;
20163
20164 die = follow_die_ref (die, spec, &cu);
20165 }
20166
20167 return NULL;
20168 }
20169
20170 /* Return the named attribute or NULL if not there,
20171 but do not follow DW_AT_specification, etc.
20172 This is for use in contexts where we're reading .debug_types dies.
20173 Following DW_AT_specification, DW_AT_abstract_origin will take us
20174 back up the chain, and we want to go down. */
20175
20176 static struct attribute *
20177 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
20178 {
20179 unsigned int i;
20180
20181 for (i = 0; i < die->num_attrs; ++i)
20182 if (die->attrs[i].name == name)
20183 return &die->attrs[i];
20184
20185 return NULL;
20186 }
20187
20188 /* Return the string associated with a string-typed attribute, or NULL if it
20189 is either not found or is of an incorrect type. */
20190
20191 static const char *
20192 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20193 {
20194 struct attribute *attr;
20195 const char *str = NULL;
20196
20197 attr = dwarf2_attr (die, name, cu);
20198
20199 if (attr != NULL)
20200 {
20201 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
20202 || attr->form == DW_FORM_string
20203 || attr->form == DW_FORM_strx
20204 || attr->form == DW_FORM_strx1
20205 || attr->form == DW_FORM_strx2
20206 || attr->form == DW_FORM_strx3
20207 || attr->form == DW_FORM_strx4
20208 || attr->form == DW_FORM_GNU_str_index
20209 || attr->form == DW_FORM_GNU_strp_alt)
20210 str = DW_STRING (attr);
20211 else
20212 complaint (_("string type expected for attribute %s for "
20213 "DIE at %s in module %s"),
20214 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20215 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
20216 }
20217
20218 return str;
20219 }
20220
20221 /* Return the dwo name or NULL if not present. If present, it is in either
20222 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20223 static const char *
20224 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20225 {
20226 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20227 if (dwo_name == nullptr)
20228 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20229 return dwo_name;
20230 }
20231
20232 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20233 and holds a non-zero value. This function should only be used for
20234 DW_FORM_flag or DW_FORM_flag_present attributes. */
20235
20236 static int
20237 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20238 {
20239 struct attribute *attr = dwarf2_attr (die, name, cu);
20240
20241 return (attr && DW_UNSND (attr));
20242 }
20243
20244 static int
20245 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20246 {
20247 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20248 which value is non-zero. However, we have to be careful with
20249 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20250 (via dwarf2_flag_true_p) follows this attribute. So we may
20251 end up accidently finding a declaration attribute that belongs
20252 to a different DIE referenced by the specification attribute,
20253 even though the given DIE does not have a declaration attribute. */
20254 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20255 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20256 }
20257
20258 /* Return the die giving the specification for DIE, if there is
20259 one. *SPEC_CU is the CU containing DIE on input, and the CU
20260 containing the return value on output. If there is no
20261 specification, but there is an abstract origin, that is
20262 returned. */
20263
20264 static struct die_info *
20265 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20266 {
20267 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20268 *spec_cu);
20269
20270 if (spec_attr == NULL)
20271 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20272
20273 if (spec_attr == NULL)
20274 return NULL;
20275 else
20276 return follow_die_ref (die, spec_attr, spec_cu);
20277 }
20278
20279 /* Stub for free_line_header to match void * callback types. */
20280
20281 static void
20282 free_line_header_voidp (void *arg)
20283 {
20284 struct line_header *lh = (struct line_header *) arg;
20285
20286 delete lh;
20287 }
20288
20289 void
20290 line_header::add_include_dir (const char *include_dir)
20291 {
20292 if (dwarf_line_debug >= 2)
20293 {
20294 size_t new_size;
20295 if (version >= 5)
20296 new_size = m_include_dirs.size ();
20297 else
20298 new_size = m_include_dirs.size () + 1;
20299 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20300 new_size, include_dir);
20301 }
20302 m_include_dirs.push_back (include_dir);
20303 }
20304
20305 void
20306 line_header::add_file_name (const char *name,
20307 dir_index d_index,
20308 unsigned int mod_time,
20309 unsigned int length)
20310 {
20311 if (dwarf_line_debug >= 2)
20312 {
20313 size_t new_size;
20314 if (version >= 5)
20315 new_size = file_names_size ();
20316 else
20317 new_size = file_names_size () + 1;
20318 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
20319 new_size, name);
20320 }
20321 m_file_names.emplace_back (name, d_index, mod_time, length);
20322 }
20323
20324 /* A convenience function to find the proper .debug_line section for a CU. */
20325
20326 static struct dwarf2_section_info *
20327 get_debug_line_section (struct dwarf2_cu *cu)
20328 {
20329 struct dwarf2_section_info *section;
20330 struct dwarf2_per_objfile *dwarf2_per_objfile
20331 = cu->per_cu->dwarf2_per_objfile;
20332
20333 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20334 DWO file. */
20335 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20336 section = &cu->dwo_unit->dwo_file->sections.line;
20337 else if (cu->per_cu->is_dwz)
20338 {
20339 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20340
20341 section = &dwz->line;
20342 }
20343 else
20344 section = &dwarf2_per_objfile->line;
20345
20346 return section;
20347 }
20348
20349 /* Read directory or file name entry format, starting with byte of
20350 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20351 entries count and the entries themselves in the described entry
20352 format. */
20353
20354 static void
20355 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20356 bfd *abfd, const gdb_byte **bufp,
20357 struct line_header *lh,
20358 const struct comp_unit_head *cu_header,
20359 void (*callback) (struct line_header *lh,
20360 const char *name,
20361 dir_index d_index,
20362 unsigned int mod_time,
20363 unsigned int length))
20364 {
20365 gdb_byte format_count, formati;
20366 ULONGEST data_count, datai;
20367 const gdb_byte *buf = *bufp;
20368 const gdb_byte *format_header_data;
20369 unsigned int bytes_read;
20370
20371 format_count = read_1_byte (abfd, buf);
20372 buf += 1;
20373 format_header_data = buf;
20374 for (formati = 0; formati < format_count; formati++)
20375 {
20376 read_unsigned_leb128 (abfd, buf, &bytes_read);
20377 buf += bytes_read;
20378 read_unsigned_leb128 (abfd, buf, &bytes_read);
20379 buf += bytes_read;
20380 }
20381
20382 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20383 buf += bytes_read;
20384 for (datai = 0; datai < data_count; datai++)
20385 {
20386 const gdb_byte *format = format_header_data;
20387 struct file_entry fe;
20388
20389 for (formati = 0; formati < format_count; formati++)
20390 {
20391 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20392 format += bytes_read;
20393
20394 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20395 format += bytes_read;
20396
20397 gdb::optional<const char *> string;
20398 gdb::optional<unsigned int> uint;
20399
20400 switch (form)
20401 {
20402 case DW_FORM_string:
20403 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20404 buf += bytes_read;
20405 break;
20406
20407 case DW_FORM_line_strp:
20408 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20409 abfd, buf,
20410 cu_header,
20411 &bytes_read));
20412 buf += bytes_read;
20413 break;
20414
20415 case DW_FORM_data1:
20416 uint.emplace (read_1_byte (abfd, buf));
20417 buf += 1;
20418 break;
20419
20420 case DW_FORM_data2:
20421 uint.emplace (read_2_bytes (abfd, buf));
20422 buf += 2;
20423 break;
20424
20425 case DW_FORM_data4:
20426 uint.emplace (read_4_bytes (abfd, buf));
20427 buf += 4;
20428 break;
20429
20430 case DW_FORM_data8:
20431 uint.emplace (read_8_bytes (abfd, buf));
20432 buf += 8;
20433 break;
20434
20435 case DW_FORM_data16:
20436 /* This is used for MD5, but file_entry does not record MD5s. */
20437 buf += 16;
20438 break;
20439
20440 case DW_FORM_udata:
20441 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20442 buf += bytes_read;
20443 break;
20444
20445 case DW_FORM_block:
20446 /* It is valid only for DW_LNCT_timestamp which is ignored by
20447 current GDB. */
20448 break;
20449 }
20450
20451 switch (content_type)
20452 {
20453 case DW_LNCT_path:
20454 if (string.has_value ())
20455 fe.name = *string;
20456 break;
20457 case DW_LNCT_directory_index:
20458 if (uint.has_value ())
20459 fe.d_index = (dir_index) *uint;
20460 break;
20461 case DW_LNCT_timestamp:
20462 if (uint.has_value ())
20463 fe.mod_time = *uint;
20464 break;
20465 case DW_LNCT_size:
20466 if (uint.has_value ())
20467 fe.length = *uint;
20468 break;
20469 case DW_LNCT_MD5:
20470 break;
20471 default:
20472 complaint (_("Unknown format content type %s"),
20473 pulongest (content_type));
20474 }
20475 }
20476
20477 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20478 }
20479
20480 *bufp = buf;
20481 }
20482
20483 /* Read the statement program header starting at OFFSET in
20484 .debug_line, or .debug_line.dwo. Return a pointer
20485 to a struct line_header, allocated using xmalloc.
20486 Returns NULL if there is a problem reading the header, e.g., if it
20487 has a version we don't understand.
20488
20489 NOTE: the strings in the include directory and file name tables of
20490 the returned object point into the dwarf line section buffer,
20491 and must not be freed. */
20492
20493 static line_header_up
20494 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20495 {
20496 const gdb_byte *line_ptr;
20497 unsigned int bytes_read, offset_size;
20498 int i;
20499 const char *cur_dir, *cur_file;
20500 struct dwarf2_section_info *section;
20501 bfd *abfd;
20502 struct dwarf2_per_objfile *dwarf2_per_objfile
20503 = cu->per_cu->dwarf2_per_objfile;
20504
20505 section = get_debug_line_section (cu);
20506 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20507 if (section->buffer == NULL)
20508 {
20509 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20510 complaint (_("missing .debug_line.dwo section"));
20511 else
20512 complaint (_("missing .debug_line section"));
20513 return 0;
20514 }
20515
20516 /* We can't do this until we know the section is non-empty.
20517 Only then do we know we have such a section. */
20518 abfd = get_section_bfd_owner (section);
20519
20520 /* Make sure that at least there's room for the total_length field.
20521 That could be 12 bytes long, but we're just going to fudge that. */
20522 if (to_underlying (sect_off) + 4 >= section->size)
20523 {
20524 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20525 return 0;
20526 }
20527
20528 line_header_up lh (new line_header ());
20529
20530 lh->sect_off = sect_off;
20531 lh->offset_in_dwz = cu->per_cu->is_dwz;
20532
20533 line_ptr = section->buffer + to_underlying (sect_off);
20534
20535 /* Read in the header. */
20536 lh->total_length =
20537 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20538 &bytes_read, &offset_size);
20539 line_ptr += bytes_read;
20540
20541 const gdb_byte *start_here = line_ptr;
20542
20543 if (line_ptr + lh->total_length > (section->buffer + section->size))
20544 {
20545 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20546 return 0;
20547 }
20548 lh->statement_program_end = start_here + lh->total_length;
20549 lh->version = read_2_bytes (abfd, line_ptr);
20550 line_ptr += 2;
20551 if (lh->version > 5)
20552 {
20553 /* This is a version we don't understand. The format could have
20554 changed in ways we don't handle properly so just punt. */
20555 complaint (_("unsupported version in .debug_line section"));
20556 return NULL;
20557 }
20558 if (lh->version >= 5)
20559 {
20560 gdb_byte segment_selector_size;
20561
20562 /* Skip address size. */
20563 read_1_byte (abfd, line_ptr);
20564 line_ptr += 1;
20565
20566 segment_selector_size = read_1_byte (abfd, line_ptr);
20567 line_ptr += 1;
20568 if (segment_selector_size != 0)
20569 {
20570 complaint (_("unsupported segment selector size %u "
20571 "in .debug_line section"),
20572 segment_selector_size);
20573 return NULL;
20574 }
20575 }
20576 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20577 line_ptr += offset_size;
20578 lh->statement_program_start = line_ptr + lh->header_length;
20579 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20580 line_ptr += 1;
20581 if (lh->version >= 4)
20582 {
20583 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20584 line_ptr += 1;
20585 }
20586 else
20587 lh->maximum_ops_per_instruction = 1;
20588
20589 if (lh->maximum_ops_per_instruction == 0)
20590 {
20591 lh->maximum_ops_per_instruction = 1;
20592 complaint (_("invalid maximum_ops_per_instruction "
20593 "in `.debug_line' section"));
20594 }
20595
20596 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20597 line_ptr += 1;
20598 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20599 line_ptr += 1;
20600 lh->line_range = read_1_byte (abfd, line_ptr);
20601 line_ptr += 1;
20602 lh->opcode_base = read_1_byte (abfd, line_ptr);
20603 line_ptr += 1;
20604 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20605
20606 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20607 for (i = 1; i < lh->opcode_base; ++i)
20608 {
20609 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20610 line_ptr += 1;
20611 }
20612
20613 if (lh->version >= 5)
20614 {
20615 /* Read directory table. */
20616 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20617 &cu->header,
20618 [] (struct line_header *header, const char *name,
20619 dir_index d_index, unsigned int mod_time,
20620 unsigned int length)
20621 {
20622 header->add_include_dir (name);
20623 });
20624
20625 /* Read file name table. */
20626 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20627 &cu->header,
20628 [] (struct line_header *header, const char *name,
20629 dir_index d_index, unsigned int mod_time,
20630 unsigned int length)
20631 {
20632 header->add_file_name (name, d_index, mod_time, length);
20633 });
20634 }
20635 else
20636 {
20637 /* Read directory table. */
20638 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20639 {
20640 line_ptr += bytes_read;
20641 lh->add_include_dir (cur_dir);
20642 }
20643 line_ptr += bytes_read;
20644
20645 /* Read file name table. */
20646 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20647 {
20648 unsigned int mod_time, length;
20649 dir_index d_index;
20650
20651 line_ptr += bytes_read;
20652 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20653 line_ptr += bytes_read;
20654 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20655 line_ptr += bytes_read;
20656 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20657 line_ptr += bytes_read;
20658
20659 lh->add_file_name (cur_file, d_index, mod_time, length);
20660 }
20661 line_ptr += bytes_read;
20662 }
20663
20664 if (line_ptr > (section->buffer + section->size))
20665 complaint (_("line number info header doesn't "
20666 "fit in `.debug_line' section"));
20667
20668 return lh;
20669 }
20670
20671 /* Subroutine of dwarf_decode_lines to simplify it.
20672 Return the file name of the psymtab for the given file_entry.
20673 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20674 If space for the result is malloc'd, *NAME_HOLDER will be set.
20675 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20676
20677 static const char *
20678 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20679 const struct partial_symtab *pst,
20680 const char *comp_dir,
20681 gdb::unique_xmalloc_ptr<char> *name_holder)
20682 {
20683 const char *include_name = fe.name;
20684 const char *include_name_to_compare = include_name;
20685 const char *pst_filename;
20686 int file_is_pst;
20687
20688 const char *dir_name = fe.include_dir (lh);
20689
20690 gdb::unique_xmalloc_ptr<char> hold_compare;
20691 if (!IS_ABSOLUTE_PATH (include_name)
20692 && (dir_name != NULL || comp_dir != NULL))
20693 {
20694 /* Avoid creating a duplicate psymtab for PST.
20695 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20696 Before we do the comparison, however, we need to account
20697 for DIR_NAME and COMP_DIR.
20698 First prepend dir_name (if non-NULL). If we still don't
20699 have an absolute path prepend comp_dir (if non-NULL).
20700 However, the directory we record in the include-file's
20701 psymtab does not contain COMP_DIR (to match the
20702 corresponding symtab(s)).
20703
20704 Example:
20705
20706 bash$ cd /tmp
20707 bash$ gcc -g ./hello.c
20708 include_name = "hello.c"
20709 dir_name = "."
20710 DW_AT_comp_dir = comp_dir = "/tmp"
20711 DW_AT_name = "./hello.c"
20712
20713 */
20714
20715 if (dir_name != NULL)
20716 {
20717 name_holder->reset (concat (dir_name, SLASH_STRING,
20718 include_name, (char *) NULL));
20719 include_name = name_holder->get ();
20720 include_name_to_compare = include_name;
20721 }
20722 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20723 {
20724 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20725 include_name, (char *) NULL));
20726 include_name_to_compare = hold_compare.get ();
20727 }
20728 }
20729
20730 pst_filename = pst->filename;
20731 gdb::unique_xmalloc_ptr<char> copied_name;
20732 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20733 {
20734 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20735 pst_filename, (char *) NULL));
20736 pst_filename = copied_name.get ();
20737 }
20738
20739 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20740
20741 if (file_is_pst)
20742 return NULL;
20743 return include_name;
20744 }
20745
20746 /* State machine to track the state of the line number program. */
20747
20748 class lnp_state_machine
20749 {
20750 public:
20751 /* Initialize a machine state for the start of a line number
20752 program. */
20753 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20754 bool record_lines_p);
20755
20756 file_entry *current_file ()
20757 {
20758 /* lh->file_names is 0-based, but the file name numbers in the
20759 statement program are 1-based. */
20760 return m_line_header->file_name_at (m_file);
20761 }
20762
20763 /* Record the line in the state machine. END_SEQUENCE is true if
20764 we're processing the end of a sequence. */
20765 void record_line (bool end_sequence);
20766
20767 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20768 nop-out rest of the lines in this sequence. */
20769 void check_line_address (struct dwarf2_cu *cu,
20770 const gdb_byte *line_ptr,
20771 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20772
20773 void handle_set_discriminator (unsigned int discriminator)
20774 {
20775 m_discriminator = discriminator;
20776 m_line_has_non_zero_discriminator |= discriminator != 0;
20777 }
20778
20779 /* Handle DW_LNE_set_address. */
20780 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20781 {
20782 m_op_index = 0;
20783 address += baseaddr;
20784 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20785 }
20786
20787 /* Handle DW_LNS_advance_pc. */
20788 void handle_advance_pc (CORE_ADDR adjust);
20789
20790 /* Handle a special opcode. */
20791 void handle_special_opcode (unsigned char op_code);
20792
20793 /* Handle DW_LNS_advance_line. */
20794 void handle_advance_line (int line_delta)
20795 {
20796 advance_line (line_delta);
20797 }
20798
20799 /* Handle DW_LNS_set_file. */
20800 void handle_set_file (file_name_index file);
20801
20802 /* Handle DW_LNS_negate_stmt. */
20803 void handle_negate_stmt ()
20804 {
20805 m_is_stmt = !m_is_stmt;
20806 }
20807
20808 /* Handle DW_LNS_const_add_pc. */
20809 void handle_const_add_pc ();
20810
20811 /* Handle DW_LNS_fixed_advance_pc. */
20812 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20813 {
20814 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20815 m_op_index = 0;
20816 }
20817
20818 /* Handle DW_LNS_copy. */
20819 void handle_copy ()
20820 {
20821 record_line (false);
20822 m_discriminator = 0;
20823 }
20824
20825 /* Handle DW_LNE_end_sequence. */
20826 void handle_end_sequence ()
20827 {
20828 m_currently_recording_lines = true;
20829 }
20830
20831 private:
20832 /* Advance the line by LINE_DELTA. */
20833 void advance_line (int line_delta)
20834 {
20835 m_line += line_delta;
20836
20837 if (line_delta != 0)
20838 m_line_has_non_zero_discriminator = m_discriminator != 0;
20839 }
20840
20841 struct dwarf2_cu *m_cu;
20842
20843 gdbarch *m_gdbarch;
20844
20845 /* True if we're recording lines.
20846 Otherwise we're building partial symtabs and are just interested in
20847 finding include files mentioned by the line number program. */
20848 bool m_record_lines_p;
20849
20850 /* The line number header. */
20851 line_header *m_line_header;
20852
20853 /* These are part of the standard DWARF line number state machine,
20854 and initialized according to the DWARF spec. */
20855
20856 unsigned char m_op_index = 0;
20857 /* The line table index of the current file. */
20858 file_name_index m_file = 1;
20859 unsigned int m_line = 1;
20860
20861 /* These are initialized in the constructor. */
20862
20863 CORE_ADDR m_address;
20864 bool m_is_stmt;
20865 unsigned int m_discriminator;
20866
20867 /* Additional bits of state we need to track. */
20868
20869 /* The last file that we called dwarf2_start_subfile for.
20870 This is only used for TLLs. */
20871 unsigned int m_last_file = 0;
20872 /* The last file a line number was recorded for. */
20873 struct subfile *m_last_subfile = NULL;
20874
20875 /* When true, record the lines we decode. */
20876 bool m_currently_recording_lines = false;
20877
20878 /* The last line number that was recorded, used to coalesce
20879 consecutive entries for the same line. This can happen, for
20880 example, when discriminators are present. PR 17276. */
20881 unsigned int m_last_line = 0;
20882 bool m_line_has_non_zero_discriminator = false;
20883 };
20884
20885 void
20886 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20887 {
20888 CORE_ADDR addr_adj = (((m_op_index + adjust)
20889 / m_line_header->maximum_ops_per_instruction)
20890 * m_line_header->minimum_instruction_length);
20891 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20892 m_op_index = ((m_op_index + adjust)
20893 % m_line_header->maximum_ops_per_instruction);
20894 }
20895
20896 void
20897 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20898 {
20899 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20900 CORE_ADDR addr_adj = (((m_op_index
20901 + (adj_opcode / m_line_header->line_range))
20902 / m_line_header->maximum_ops_per_instruction)
20903 * m_line_header->minimum_instruction_length);
20904 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20905 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20906 % m_line_header->maximum_ops_per_instruction);
20907
20908 int line_delta = (m_line_header->line_base
20909 + (adj_opcode % m_line_header->line_range));
20910 advance_line (line_delta);
20911 record_line (false);
20912 m_discriminator = 0;
20913 }
20914
20915 void
20916 lnp_state_machine::handle_set_file (file_name_index file)
20917 {
20918 m_file = file;
20919
20920 const file_entry *fe = current_file ();
20921 if (fe == NULL)
20922 dwarf2_debug_line_missing_file_complaint ();
20923 else if (m_record_lines_p)
20924 {
20925 const char *dir = fe->include_dir (m_line_header);
20926
20927 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20928 m_line_has_non_zero_discriminator = m_discriminator != 0;
20929 dwarf2_start_subfile (m_cu, fe->name, dir);
20930 }
20931 }
20932
20933 void
20934 lnp_state_machine::handle_const_add_pc ()
20935 {
20936 CORE_ADDR adjust
20937 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20938
20939 CORE_ADDR addr_adj
20940 = (((m_op_index + adjust)
20941 / m_line_header->maximum_ops_per_instruction)
20942 * m_line_header->minimum_instruction_length);
20943
20944 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20945 m_op_index = ((m_op_index + adjust)
20946 % m_line_header->maximum_ops_per_instruction);
20947 }
20948
20949 /* Return non-zero if we should add LINE to the line number table.
20950 LINE is the line to add, LAST_LINE is the last line that was added,
20951 LAST_SUBFILE is the subfile for LAST_LINE.
20952 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20953 had a non-zero discriminator.
20954
20955 We have to be careful in the presence of discriminators.
20956 E.g., for this line:
20957
20958 for (i = 0; i < 100000; i++);
20959
20960 clang can emit four line number entries for that one line,
20961 each with a different discriminator.
20962 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20963
20964 However, we want gdb to coalesce all four entries into one.
20965 Otherwise the user could stepi into the middle of the line and
20966 gdb would get confused about whether the pc really was in the
20967 middle of the line.
20968
20969 Things are further complicated by the fact that two consecutive
20970 line number entries for the same line is a heuristic used by gcc
20971 to denote the end of the prologue. So we can't just discard duplicate
20972 entries, we have to be selective about it. The heuristic we use is
20973 that we only collapse consecutive entries for the same line if at least
20974 one of those entries has a non-zero discriminator. PR 17276.
20975
20976 Note: Addresses in the line number state machine can never go backwards
20977 within one sequence, thus this coalescing is ok. */
20978
20979 static int
20980 dwarf_record_line_p (struct dwarf2_cu *cu,
20981 unsigned int line, unsigned int last_line,
20982 int line_has_non_zero_discriminator,
20983 struct subfile *last_subfile)
20984 {
20985 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20986 return 1;
20987 if (line != last_line)
20988 return 1;
20989 /* Same line for the same file that we've seen already.
20990 As a last check, for pr 17276, only record the line if the line
20991 has never had a non-zero discriminator. */
20992 if (!line_has_non_zero_discriminator)
20993 return 1;
20994 return 0;
20995 }
20996
20997 /* Use the CU's builder to record line number LINE beginning at
20998 address ADDRESS in the line table of subfile SUBFILE. */
20999
21000 static void
21001 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21002 unsigned int line, CORE_ADDR address,
21003 struct dwarf2_cu *cu)
21004 {
21005 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21006
21007 if (dwarf_line_debug)
21008 {
21009 fprintf_unfiltered (gdb_stdlog,
21010 "Recording line %u, file %s, address %s\n",
21011 line, lbasename (subfile->name),
21012 paddress (gdbarch, address));
21013 }
21014
21015 if (cu != nullptr)
21016 cu->get_builder ()->record_line (subfile, line, addr);
21017 }
21018
21019 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21020 Mark the end of a set of line number records.
21021 The arguments are the same as for dwarf_record_line_1.
21022 If SUBFILE is NULL the request is ignored. */
21023
21024 static void
21025 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21026 CORE_ADDR address, struct dwarf2_cu *cu)
21027 {
21028 if (subfile == NULL)
21029 return;
21030
21031 if (dwarf_line_debug)
21032 {
21033 fprintf_unfiltered (gdb_stdlog,
21034 "Finishing current line, file %s, address %s\n",
21035 lbasename (subfile->name),
21036 paddress (gdbarch, address));
21037 }
21038
21039 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
21040 }
21041
21042 void
21043 lnp_state_machine::record_line (bool end_sequence)
21044 {
21045 if (dwarf_line_debug)
21046 {
21047 fprintf_unfiltered (gdb_stdlog,
21048 "Processing actual line %u: file %u,"
21049 " address %s, is_stmt %u, discrim %u\n",
21050 m_line, m_file,
21051 paddress (m_gdbarch, m_address),
21052 m_is_stmt, m_discriminator);
21053 }
21054
21055 file_entry *fe = current_file ();
21056
21057 if (fe == NULL)
21058 dwarf2_debug_line_missing_file_complaint ();
21059 /* For now we ignore lines not starting on an instruction boundary.
21060 But not when processing end_sequence for compatibility with the
21061 previous version of the code. */
21062 else if (m_op_index == 0 || end_sequence)
21063 {
21064 fe->included_p = 1;
21065 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
21066 {
21067 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
21068 || end_sequence)
21069 {
21070 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21071 m_currently_recording_lines ? m_cu : nullptr);
21072 }
21073
21074 if (!end_sequence)
21075 {
21076 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21077 m_line_has_non_zero_discriminator,
21078 m_last_subfile))
21079 {
21080 buildsym_compunit *builder = m_cu->get_builder ();
21081 dwarf_record_line_1 (m_gdbarch,
21082 builder->get_current_subfile (),
21083 m_line, m_address,
21084 m_currently_recording_lines ? m_cu : nullptr);
21085 }
21086 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21087 m_last_line = m_line;
21088 }
21089 }
21090 }
21091 }
21092
21093 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21094 line_header *lh, bool record_lines_p)
21095 {
21096 m_cu = cu;
21097 m_gdbarch = arch;
21098 m_record_lines_p = record_lines_p;
21099 m_line_header = lh;
21100
21101 m_currently_recording_lines = true;
21102
21103 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21104 was a line entry for it so that the backend has a chance to adjust it
21105 and also record it in case it needs it. This is currently used by MIPS
21106 code, cf. `mips_adjust_dwarf2_line'. */
21107 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21108 m_is_stmt = lh->default_is_stmt;
21109 m_discriminator = 0;
21110 }
21111
21112 void
21113 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21114 const gdb_byte *line_ptr,
21115 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21116 {
21117 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21118 the pc range of the CU. However, we restrict the test to only ADDRESS
21119 values of zero to preserve GDB's previous behaviour which is to handle
21120 the specific case of a function being GC'd by the linker. */
21121
21122 if (address == 0 && address < unrelocated_lowpc)
21123 {
21124 /* This line table is for a function which has been
21125 GCd by the linker. Ignore it. PR gdb/12528 */
21126
21127 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21128 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21129
21130 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21131 line_offset, objfile_name (objfile));
21132 m_currently_recording_lines = false;
21133 /* Note: m_currently_recording_lines is left as false until we see
21134 DW_LNE_end_sequence. */
21135 }
21136 }
21137
21138 /* Subroutine of dwarf_decode_lines to simplify it.
21139 Process the line number information in LH.
21140 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21141 program in order to set included_p for every referenced header. */
21142
21143 static void
21144 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21145 const int decode_for_pst_p, CORE_ADDR lowpc)
21146 {
21147 const gdb_byte *line_ptr, *extended_end;
21148 const gdb_byte *line_end;
21149 unsigned int bytes_read, extended_len;
21150 unsigned char op_code, extended_op;
21151 CORE_ADDR baseaddr;
21152 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21153 bfd *abfd = objfile->obfd;
21154 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21155 /* True if we're recording line info (as opposed to building partial
21156 symtabs and just interested in finding include files mentioned by
21157 the line number program). */
21158 bool record_lines_p = !decode_for_pst_p;
21159
21160 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21161
21162 line_ptr = lh->statement_program_start;
21163 line_end = lh->statement_program_end;
21164
21165 /* Read the statement sequences until there's nothing left. */
21166 while (line_ptr < line_end)
21167 {
21168 /* The DWARF line number program state machine. Reset the state
21169 machine at the start of each sequence. */
21170 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21171 bool end_sequence = false;
21172
21173 if (record_lines_p)
21174 {
21175 /* Start a subfile for the current file of the state
21176 machine. */
21177 const file_entry *fe = state_machine.current_file ();
21178
21179 if (fe != NULL)
21180 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21181 }
21182
21183 /* Decode the table. */
21184 while (line_ptr < line_end && !end_sequence)
21185 {
21186 op_code = read_1_byte (abfd, line_ptr);
21187 line_ptr += 1;
21188
21189 if (op_code >= lh->opcode_base)
21190 {
21191 /* Special opcode. */
21192 state_machine.handle_special_opcode (op_code);
21193 }
21194 else switch (op_code)
21195 {
21196 case DW_LNS_extended_op:
21197 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21198 &bytes_read);
21199 line_ptr += bytes_read;
21200 extended_end = line_ptr + extended_len;
21201 extended_op = read_1_byte (abfd, line_ptr);
21202 line_ptr += 1;
21203 switch (extended_op)
21204 {
21205 case DW_LNE_end_sequence:
21206 state_machine.handle_end_sequence ();
21207 end_sequence = true;
21208 break;
21209 case DW_LNE_set_address:
21210 {
21211 CORE_ADDR address
21212 = read_address (abfd, line_ptr, cu, &bytes_read);
21213 line_ptr += bytes_read;
21214
21215 state_machine.check_line_address (cu, line_ptr,
21216 lowpc - baseaddr, address);
21217 state_machine.handle_set_address (baseaddr, address);
21218 }
21219 break;
21220 case DW_LNE_define_file:
21221 {
21222 const char *cur_file;
21223 unsigned int mod_time, length;
21224 dir_index dindex;
21225
21226 cur_file = read_direct_string (abfd, line_ptr,
21227 &bytes_read);
21228 line_ptr += bytes_read;
21229 dindex = (dir_index)
21230 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21231 line_ptr += bytes_read;
21232 mod_time =
21233 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21234 line_ptr += bytes_read;
21235 length =
21236 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21237 line_ptr += bytes_read;
21238 lh->add_file_name (cur_file, dindex, mod_time, length);
21239 }
21240 break;
21241 case DW_LNE_set_discriminator:
21242 {
21243 /* The discriminator is not interesting to the
21244 debugger; just ignore it. We still need to
21245 check its value though:
21246 if there are consecutive entries for the same
21247 (non-prologue) line we want to coalesce them.
21248 PR 17276. */
21249 unsigned int discr
21250 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21251 line_ptr += bytes_read;
21252
21253 state_machine.handle_set_discriminator (discr);
21254 }
21255 break;
21256 default:
21257 complaint (_("mangled .debug_line section"));
21258 return;
21259 }
21260 /* Make sure that we parsed the extended op correctly. If e.g.
21261 we expected a different address size than the producer used,
21262 we may have read the wrong number of bytes. */
21263 if (line_ptr != extended_end)
21264 {
21265 complaint (_("mangled .debug_line section"));
21266 return;
21267 }
21268 break;
21269 case DW_LNS_copy:
21270 state_machine.handle_copy ();
21271 break;
21272 case DW_LNS_advance_pc:
21273 {
21274 CORE_ADDR adjust
21275 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21276 line_ptr += bytes_read;
21277
21278 state_machine.handle_advance_pc (adjust);
21279 }
21280 break;
21281 case DW_LNS_advance_line:
21282 {
21283 int line_delta
21284 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21285 line_ptr += bytes_read;
21286
21287 state_machine.handle_advance_line (line_delta);
21288 }
21289 break;
21290 case DW_LNS_set_file:
21291 {
21292 file_name_index file
21293 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21294 &bytes_read);
21295 line_ptr += bytes_read;
21296
21297 state_machine.handle_set_file (file);
21298 }
21299 break;
21300 case DW_LNS_set_column:
21301 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21302 line_ptr += bytes_read;
21303 break;
21304 case DW_LNS_negate_stmt:
21305 state_machine.handle_negate_stmt ();
21306 break;
21307 case DW_LNS_set_basic_block:
21308 break;
21309 /* Add to the address register of the state machine the
21310 address increment value corresponding to special opcode
21311 255. I.e., this value is scaled by the minimum
21312 instruction length since special opcode 255 would have
21313 scaled the increment. */
21314 case DW_LNS_const_add_pc:
21315 state_machine.handle_const_add_pc ();
21316 break;
21317 case DW_LNS_fixed_advance_pc:
21318 {
21319 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21320 line_ptr += 2;
21321
21322 state_machine.handle_fixed_advance_pc (addr_adj);
21323 }
21324 break;
21325 default:
21326 {
21327 /* Unknown standard opcode, ignore it. */
21328 int i;
21329
21330 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21331 {
21332 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21333 line_ptr += bytes_read;
21334 }
21335 }
21336 }
21337 }
21338
21339 if (!end_sequence)
21340 dwarf2_debug_line_missing_end_sequence_complaint ();
21341
21342 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21343 in which case we still finish recording the last line). */
21344 state_machine.record_line (true);
21345 }
21346 }
21347
21348 /* Decode the Line Number Program (LNP) for the given line_header
21349 structure and CU. The actual information extracted and the type
21350 of structures created from the LNP depends on the value of PST.
21351
21352 1. If PST is NULL, then this procedure uses the data from the program
21353 to create all necessary symbol tables, and their linetables.
21354
21355 2. If PST is not NULL, this procedure reads the program to determine
21356 the list of files included by the unit represented by PST, and
21357 builds all the associated partial symbol tables.
21358
21359 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21360 It is used for relative paths in the line table.
21361 NOTE: When processing partial symtabs (pst != NULL),
21362 comp_dir == pst->dirname.
21363
21364 NOTE: It is important that psymtabs have the same file name (via strcmp)
21365 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21366 symtab we don't use it in the name of the psymtabs we create.
21367 E.g. expand_line_sal requires this when finding psymtabs to expand.
21368 A good testcase for this is mb-inline.exp.
21369
21370 LOWPC is the lowest address in CU (or 0 if not known).
21371
21372 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21373 for its PC<->lines mapping information. Otherwise only the filename
21374 table is read in. */
21375
21376 static void
21377 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21378 struct dwarf2_cu *cu, struct partial_symtab *pst,
21379 CORE_ADDR lowpc, int decode_mapping)
21380 {
21381 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21382 const int decode_for_pst_p = (pst != NULL);
21383
21384 if (decode_mapping)
21385 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21386
21387 if (decode_for_pst_p)
21388 {
21389 /* Now that we're done scanning the Line Header Program, we can
21390 create the psymtab of each included file. */
21391 for (auto &file_entry : lh->file_names ())
21392 if (file_entry.included_p == 1)
21393 {
21394 gdb::unique_xmalloc_ptr<char> name_holder;
21395 const char *include_name =
21396 psymtab_include_file_name (lh, file_entry, pst,
21397 comp_dir, &name_holder);
21398 if (include_name != NULL)
21399 dwarf2_create_include_psymtab (include_name, pst, objfile);
21400 }
21401 }
21402 else
21403 {
21404 /* Make sure a symtab is created for every file, even files
21405 which contain only variables (i.e. no code with associated
21406 line numbers). */
21407 buildsym_compunit *builder = cu->get_builder ();
21408 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21409
21410 for (auto &fe : lh->file_names ())
21411 {
21412 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21413 if (builder->get_current_subfile ()->symtab == NULL)
21414 {
21415 builder->get_current_subfile ()->symtab
21416 = allocate_symtab (cust,
21417 builder->get_current_subfile ()->name);
21418 }
21419 fe.symtab = builder->get_current_subfile ()->symtab;
21420 }
21421 }
21422 }
21423
21424 /* Start a subfile for DWARF. FILENAME is the name of the file and
21425 DIRNAME the name of the source directory which contains FILENAME
21426 or NULL if not known.
21427 This routine tries to keep line numbers from identical absolute and
21428 relative file names in a common subfile.
21429
21430 Using the `list' example from the GDB testsuite, which resides in
21431 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21432 of /srcdir/list0.c yields the following debugging information for list0.c:
21433
21434 DW_AT_name: /srcdir/list0.c
21435 DW_AT_comp_dir: /compdir
21436 files.files[0].name: list0.h
21437 files.files[0].dir: /srcdir
21438 files.files[1].name: list0.c
21439 files.files[1].dir: /srcdir
21440
21441 The line number information for list0.c has to end up in a single
21442 subfile, so that `break /srcdir/list0.c:1' works as expected.
21443 start_subfile will ensure that this happens provided that we pass the
21444 concatenation of files.files[1].dir and files.files[1].name as the
21445 subfile's name. */
21446
21447 static void
21448 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21449 const char *dirname)
21450 {
21451 char *copy = NULL;
21452
21453 /* In order not to lose the line information directory,
21454 we concatenate it to the filename when it makes sense.
21455 Note that the Dwarf3 standard says (speaking of filenames in line
21456 information): ``The directory index is ignored for file names
21457 that represent full path names''. Thus ignoring dirname in the
21458 `else' branch below isn't an issue. */
21459
21460 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21461 {
21462 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21463 filename = copy;
21464 }
21465
21466 cu->get_builder ()->start_subfile (filename);
21467
21468 if (copy != NULL)
21469 xfree (copy);
21470 }
21471
21472 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21473 buildsym_compunit constructor. */
21474
21475 struct compunit_symtab *
21476 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21477 CORE_ADDR low_pc)
21478 {
21479 gdb_assert (m_builder == nullptr);
21480
21481 m_builder.reset (new struct buildsym_compunit
21482 (per_cu->dwarf2_per_objfile->objfile,
21483 name, comp_dir, language, low_pc));
21484
21485 list_in_scope = get_builder ()->get_file_symbols ();
21486
21487 get_builder ()->record_debugformat ("DWARF 2");
21488 get_builder ()->record_producer (producer);
21489
21490 processing_has_namespace_info = false;
21491
21492 return get_builder ()->get_compunit_symtab ();
21493 }
21494
21495 static void
21496 var_decode_location (struct attribute *attr, struct symbol *sym,
21497 struct dwarf2_cu *cu)
21498 {
21499 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21500 struct comp_unit_head *cu_header = &cu->header;
21501
21502 /* NOTE drow/2003-01-30: There used to be a comment and some special
21503 code here to turn a symbol with DW_AT_external and a
21504 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21505 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21506 with some versions of binutils) where shared libraries could have
21507 relocations against symbols in their debug information - the
21508 minimal symbol would have the right address, but the debug info
21509 would not. It's no longer necessary, because we will explicitly
21510 apply relocations when we read in the debug information now. */
21511
21512 /* A DW_AT_location attribute with no contents indicates that a
21513 variable has been optimized away. */
21514 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21515 {
21516 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21517 return;
21518 }
21519
21520 /* Handle one degenerate form of location expression specially, to
21521 preserve GDB's previous behavior when section offsets are
21522 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21523 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21524
21525 if (attr_form_is_block (attr)
21526 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21527 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21528 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21529 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
21530 && (DW_BLOCK (attr)->size
21531 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21532 {
21533 unsigned int dummy;
21534
21535 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21536 SET_SYMBOL_VALUE_ADDRESS (sym,
21537 read_address (objfile->obfd,
21538 DW_BLOCK (attr)->data + 1,
21539 cu, &dummy));
21540 else
21541 SET_SYMBOL_VALUE_ADDRESS
21542 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21543 &dummy));
21544 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21545 fixup_symbol_section (sym, objfile);
21546 SET_SYMBOL_VALUE_ADDRESS (sym,
21547 SYMBOL_VALUE_ADDRESS (sym)
21548 + ANOFFSET (objfile->section_offsets,
21549 SYMBOL_SECTION (sym)));
21550 return;
21551 }
21552
21553 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21554 expression evaluator, and use LOC_COMPUTED only when necessary
21555 (i.e. when the value of a register or memory location is
21556 referenced, or a thread-local block, etc.). Then again, it might
21557 not be worthwhile. I'm assuming that it isn't unless performance
21558 or memory numbers show me otherwise. */
21559
21560 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21561
21562 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21563 cu->has_loclist = true;
21564 }
21565
21566 /* Given a pointer to a DWARF information entry, figure out if we need
21567 to make a symbol table entry for it, and if so, create a new entry
21568 and return a pointer to it.
21569 If TYPE is NULL, determine symbol type from the die, otherwise
21570 used the passed type.
21571 If SPACE is not NULL, use it to hold the new symbol. If it is
21572 NULL, allocate a new symbol on the objfile's obstack. */
21573
21574 static struct symbol *
21575 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21576 struct symbol *space)
21577 {
21578 struct dwarf2_per_objfile *dwarf2_per_objfile
21579 = cu->per_cu->dwarf2_per_objfile;
21580 struct objfile *objfile = dwarf2_per_objfile->objfile;
21581 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21582 struct symbol *sym = NULL;
21583 const char *name;
21584 struct attribute *attr = NULL;
21585 struct attribute *attr2 = NULL;
21586 CORE_ADDR baseaddr;
21587 struct pending **list_to_add = NULL;
21588
21589 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21590
21591 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21592
21593 name = dwarf2_name (die, cu);
21594 if (name)
21595 {
21596 const char *linkagename;
21597 int suppress_add = 0;
21598
21599 if (space)
21600 sym = space;
21601 else
21602 sym = allocate_symbol (objfile);
21603 OBJSTAT (objfile, n_syms++);
21604
21605 /* Cache this symbol's name and the name's demangled form (if any). */
21606 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21607 linkagename = dwarf2_physname (name, die, cu);
21608 SYMBOL_SET_NAMES (sym, linkagename, false, objfile);
21609
21610 /* Fortran does not have mangling standard and the mangling does differ
21611 between gfortran, iFort etc. */
21612 if (cu->language == language_fortran
21613 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21614 symbol_set_demangled_name (&(sym->ginfo),
21615 dwarf2_full_name (name, die, cu),
21616 NULL);
21617
21618 /* Default assumptions.
21619 Use the passed type or decode it from the die. */
21620 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21621 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21622 if (type != NULL)
21623 SYMBOL_TYPE (sym) = type;
21624 else
21625 SYMBOL_TYPE (sym) = die_type (die, cu);
21626 attr = dwarf2_attr (die,
21627 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21628 cu);
21629 if (attr)
21630 {
21631 SYMBOL_LINE (sym) = DW_UNSND (attr);
21632 }
21633
21634 attr = dwarf2_attr (die,
21635 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21636 cu);
21637 if (attr)
21638 {
21639 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21640 struct file_entry *fe;
21641
21642 if (cu->line_header != NULL)
21643 fe = cu->line_header->file_name_at (file_index);
21644 else
21645 fe = NULL;
21646
21647 if (fe == NULL)
21648 complaint (_("file index out of range"));
21649 else
21650 symbol_set_symtab (sym, fe->symtab);
21651 }
21652
21653 switch (die->tag)
21654 {
21655 case DW_TAG_label:
21656 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21657 if (attr)
21658 {
21659 CORE_ADDR addr;
21660
21661 addr = attr_value_as_address (attr);
21662 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21663 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21664 }
21665 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21666 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21667 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21668 add_symbol_to_list (sym, cu->list_in_scope);
21669 break;
21670 case DW_TAG_subprogram:
21671 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21672 finish_block. */
21673 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21674 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21675 if ((attr2 && (DW_UNSND (attr2) != 0))
21676 || cu->language == language_ada
21677 || cu->language == language_fortran)
21678 {
21679 /* Subprograms marked external are stored as a global symbol.
21680 Ada and Fortran subprograms, whether marked external or
21681 not, are always stored as a global symbol, because we want
21682 to be able to access them globally. For instance, we want
21683 to be able to break on a nested subprogram without having
21684 to specify the context. */
21685 list_to_add = cu->get_builder ()->get_global_symbols ();
21686 }
21687 else
21688 {
21689 list_to_add = cu->list_in_scope;
21690 }
21691 break;
21692 case DW_TAG_inlined_subroutine:
21693 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21694 finish_block. */
21695 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21696 SYMBOL_INLINED (sym) = 1;
21697 list_to_add = cu->list_in_scope;
21698 break;
21699 case DW_TAG_template_value_param:
21700 suppress_add = 1;
21701 /* Fall through. */
21702 case DW_TAG_constant:
21703 case DW_TAG_variable:
21704 case DW_TAG_member:
21705 /* Compilation with minimal debug info may result in
21706 variables with missing type entries. Change the
21707 misleading `void' type to something sensible. */
21708 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21709 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21710
21711 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21712 /* In the case of DW_TAG_member, we should only be called for
21713 static const members. */
21714 if (die->tag == DW_TAG_member)
21715 {
21716 /* dwarf2_add_field uses die_is_declaration,
21717 so we do the same. */
21718 gdb_assert (die_is_declaration (die, cu));
21719 gdb_assert (attr);
21720 }
21721 if (attr)
21722 {
21723 dwarf2_const_value (attr, sym, cu);
21724 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21725 if (!suppress_add)
21726 {
21727 if (attr2 && (DW_UNSND (attr2) != 0))
21728 list_to_add = cu->get_builder ()->get_global_symbols ();
21729 else
21730 list_to_add = cu->list_in_scope;
21731 }
21732 break;
21733 }
21734 attr = dwarf2_attr (die, DW_AT_location, cu);
21735 if (attr)
21736 {
21737 var_decode_location (attr, sym, cu);
21738 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21739
21740 /* Fortran explicitly imports any global symbols to the local
21741 scope by DW_TAG_common_block. */
21742 if (cu->language == language_fortran && die->parent
21743 && die->parent->tag == DW_TAG_common_block)
21744 attr2 = NULL;
21745
21746 if (SYMBOL_CLASS (sym) == LOC_STATIC
21747 && SYMBOL_VALUE_ADDRESS (sym) == 0
21748 && !dwarf2_per_objfile->has_section_at_zero)
21749 {
21750 /* When a static variable is eliminated by the linker,
21751 the corresponding debug information is not stripped
21752 out, but the variable address is set to null;
21753 do not add such variables into symbol table. */
21754 }
21755 else if (attr2 && (DW_UNSND (attr2) != 0))
21756 {
21757 if (SYMBOL_CLASS (sym) == LOC_STATIC
21758 && (objfile->flags & OBJF_MAINLINE) == 0
21759 && dwarf2_per_objfile->can_copy)
21760 {
21761 /* A global static variable might be subject to
21762 copy relocation. We first check for a local
21763 minsym, though, because maybe the symbol was
21764 marked hidden, in which case this would not
21765 apply. */
21766 bound_minimal_symbol found
21767 = (lookup_minimal_symbol_linkage
21768 (SYMBOL_LINKAGE_NAME (sym), objfile));
21769 if (found.minsym != nullptr)
21770 sym->maybe_copied = 1;
21771 }
21772
21773 /* A variable with DW_AT_external is never static,
21774 but it may be block-scoped. */
21775 list_to_add
21776 = ((cu->list_in_scope
21777 == cu->get_builder ()->get_file_symbols ())
21778 ? cu->get_builder ()->get_global_symbols ()
21779 : cu->list_in_scope);
21780 }
21781 else
21782 list_to_add = cu->list_in_scope;
21783 }
21784 else
21785 {
21786 /* We do not know the address of this symbol.
21787 If it is an external symbol and we have type information
21788 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21789 The address of the variable will then be determined from
21790 the minimal symbol table whenever the variable is
21791 referenced. */
21792 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21793
21794 /* Fortran explicitly imports any global symbols to the local
21795 scope by DW_TAG_common_block. */
21796 if (cu->language == language_fortran && die->parent
21797 && die->parent->tag == DW_TAG_common_block)
21798 {
21799 /* SYMBOL_CLASS doesn't matter here because
21800 read_common_block is going to reset it. */
21801 if (!suppress_add)
21802 list_to_add = cu->list_in_scope;
21803 }
21804 else if (attr2 && (DW_UNSND (attr2) != 0)
21805 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21806 {
21807 /* A variable with DW_AT_external is never static, but it
21808 may be block-scoped. */
21809 list_to_add
21810 = ((cu->list_in_scope
21811 == cu->get_builder ()->get_file_symbols ())
21812 ? cu->get_builder ()->get_global_symbols ()
21813 : cu->list_in_scope);
21814
21815 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21816 }
21817 else if (!die_is_declaration (die, cu))
21818 {
21819 /* Use the default LOC_OPTIMIZED_OUT class. */
21820 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21821 if (!suppress_add)
21822 list_to_add = cu->list_in_scope;
21823 }
21824 }
21825 break;
21826 case DW_TAG_formal_parameter:
21827 {
21828 /* If we are inside a function, mark this as an argument. If
21829 not, we might be looking at an argument to an inlined function
21830 when we do not have enough information to show inlined frames;
21831 pretend it's a local variable in that case so that the user can
21832 still see it. */
21833 struct context_stack *curr
21834 = cu->get_builder ()->get_current_context_stack ();
21835 if (curr != nullptr && curr->name != nullptr)
21836 SYMBOL_IS_ARGUMENT (sym) = 1;
21837 attr = dwarf2_attr (die, DW_AT_location, cu);
21838 if (attr)
21839 {
21840 var_decode_location (attr, sym, cu);
21841 }
21842 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21843 if (attr)
21844 {
21845 dwarf2_const_value (attr, sym, cu);
21846 }
21847
21848 list_to_add = cu->list_in_scope;
21849 }
21850 break;
21851 case DW_TAG_unspecified_parameters:
21852 /* From varargs functions; gdb doesn't seem to have any
21853 interest in this information, so just ignore it for now.
21854 (FIXME?) */
21855 break;
21856 case DW_TAG_template_type_param:
21857 suppress_add = 1;
21858 /* Fall through. */
21859 case DW_TAG_class_type:
21860 case DW_TAG_interface_type:
21861 case DW_TAG_structure_type:
21862 case DW_TAG_union_type:
21863 case DW_TAG_set_type:
21864 case DW_TAG_enumeration_type:
21865 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21866 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21867
21868 {
21869 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21870 really ever be static objects: otherwise, if you try
21871 to, say, break of a class's method and you're in a file
21872 which doesn't mention that class, it won't work unless
21873 the check for all static symbols in lookup_symbol_aux
21874 saves you. See the OtherFileClass tests in
21875 gdb.c++/namespace.exp. */
21876
21877 if (!suppress_add)
21878 {
21879 buildsym_compunit *builder = cu->get_builder ();
21880 list_to_add
21881 = (cu->list_in_scope == builder->get_file_symbols ()
21882 && cu->language == language_cplus
21883 ? builder->get_global_symbols ()
21884 : cu->list_in_scope);
21885
21886 /* The semantics of C++ state that "struct foo {
21887 ... }" also defines a typedef for "foo". */
21888 if (cu->language == language_cplus
21889 || cu->language == language_ada
21890 || cu->language == language_d
21891 || cu->language == language_rust)
21892 {
21893 /* The symbol's name is already allocated along
21894 with this objfile, so we don't need to
21895 duplicate it for the type. */
21896 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21897 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21898 }
21899 }
21900 }
21901 break;
21902 case DW_TAG_typedef:
21903 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21904 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21905 list_to_add = cu->list_in_scope;
21906 break;
21907 case DW_TAG_base_type:
21908 case DW_TAG_subrange_type:
21909 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21910 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21911 list_to_add = cu->list_in_scope;
21912 break;
21913 case DW_TAG_enumerator:
21914 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21915 if (attr)
21916 {
21917 dwarf2_const_value (attr, sym, cu);
21918 }
21919 {
21920 /* NOTE: carlton/2003-11-10: See comment above in the
21921 DW_TAG_class_type, etc. block. */
21922
21923 list_to_add
21924 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21925 && cu->language == language_cplus
21926 ? cu->get_builder ()->get_global_symbols ()
21927 : cu->list_in_scope);
21928 }
21929 break;
21930 case DW_TAG_imported_declaration:
21931 case DW_TAG_namespace:
21932 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21933 list_to_add = cu->get_builder ()->get_global_symbols ();
21934 break;
21935 case DW_TAG_module:
21936 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21937 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21938 list_to_add = cu->get_builder ()->get_global_symbols ();
21939 break;
21940 case DW_TAG_common_block:
21941 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21942 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21943 add_symbol_to_list (sym, cu->list_in_scope);
21944 break;
21945 default:
21946 /* Not a tag we recognize. Hopefully we aren't processing
21947 trash data, but since we must specifically ignore things
21948 we don't recognize, there is nothing else we should do at
21949 this point. */
21950 complaint (_("unsupported tag: '%s'"),
21951 dwarf_tag_name (die->tag));
21952 break;
21953 }
21954
21955 if (suppress_add)
21956 {
21957 sym->hash_next = objfile->template_symbols;
21958 objfile->template_symbols = sym;
21959 list_to_add = NULL;
21960 }
21961
21962 if (list_to_add != NULL)
21963 add_symbol_to_list (sym, list_to_add);
21964
21965 /* For the benefit of old versions of GCC, check for anonymous
21966 namespaces based on the demangled name. */
21967 if (!cu->processing_has_namespace_info
21968 && cu->language == language_cplus)
21969 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21970 }
21971 return (sym);
21972 }
21973
21974 /* Given an attr with a DW_FORM_dataN value in host byte order,
21975 zero-extend it as appropriate for the symbol's type. The DWARF
21976 standard (v4) is not entirely clear about the meaning of using
21977 DW_FORM_dataN for a constant with a signed type, where the type is
21978 wider than the data. The conclusion of a discussion on the DWARF
21979 list was that this is unspecified. We choose to always zero-extend
21980 because that is the interpretation long in use by GCC. */
21981
21982 static gdb_byte *
21983 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21984 struct dwarf2_cu *cu, LONGEST *value, int bits)
21985 {
21986 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21987 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21988 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21989 LONGEST l = DW_UNSND (attr);
21990
21991 if (bits < sizeof (*value) * 8)
21992 {
21993 l &= ((LONGEST) 1 << bits) - 1;
21994 *value = l;
21995 }
21996 else if (bits == sizeof (*value) * 8)
21997 *value = l;
21998 else
21999 {
22000 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22001 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22002 return bytes;
22003 }
22004
22005 return NULL;
22006 }
22007
22008 /* Read a constant value from an attribute. Either set *VALUE, or if
22009 the value does not fit in *VALUE, set *BYTES - either already
22010 allocated on the objfile obstack, or newly allocated on OBSTACK,
22011 or, set *BATON, if we translated the constant to a location
22012 expression. */
22013
22014 static void
22015 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22016 const char *name, struct obstack *obstack,
22017 struct dwarf2_cu *cu,
22018 LONGEST *value, const gdb_byte **bytes,
22019 struct dwarf2_locexpr_baton **baton)
22020 {
22021 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22022 struct comp_unit_head *cu_header = &cu->header;
22023 struct dwarf_block *blk;
22024 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22025 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22026
22027 *value = 0;
22028 *bytes = NULL;
22029 *baton = NULL;
22030
22031 switch (attr->form)
22032 {
22033 case DW_FORM_addr:
22034 case DW_FORM_addrx:
22035 case DW_FORM_GNU_addr_index:
22036 {
22037 gdb_byte *data;
22038
22039 if (TYPE_LENGTH (type) != cu_header->addr_size)
22040 dwarf2_const_value_length_mismatch_complaint (name,
22041 cu_header->addr_size,
22042 TYPE_LENGTH (type));
22043 /* Symbols of this form are reasonably rare, so we just
22044 piggyback on the existing location code rather than writing
22045 a new implementation of symbol_computed_ops. */
22046 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22047 (*baton)->per_cu = cu->per_cu;
22048 gdb_assert ((*baton)->per_cu);
22049
22050 (*baton)->size = 2 + cu_header->addr_size;
22051 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22052 (*baton)->data = data;
22053
22054 data[0] = DW_OP_addr;
22055 store_unsigned_integer (&data[1], cu_header->addr_size,
22056 byte_order, DW_ADDR (attr));
22057 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22058 }
22059 break;
22060 case DW_FORM_string:
22061 case DW_FORM_strp:
22062 case DW_FORM_strx:
22063 case DW_FORM_GNU_str_index:
22064 case DW_FORM_GNU_strp_alt:
22065 /* DW_STRING is already allocated on the objfile obstack, point
22066 directly to it. */
22067 *bytes = (const gdb_byte *) DW_STRING (attr);
22068 break;
22069 case DW_FORM_block1:
22070 case DW_FORM_block2:
22071 case DW_FORM_block4:
22072 case DW_FORM_block:
22073 case DW_FORM_exprloc:
22074 case DW_FORM_data16:
22075 blk = DW_BLOCK (attr);
22076 if (TYPE_LENGTH (type) != blk->size)
22077 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22078 TYPE_LENGTH (type));
22079 *bytes = blk->data;
22080 break;
22081
22082 /* The DW_AT_const_value attributes are supposed to carry the
22083 symbol's value "represented as it would be on the target
22084 architecture." By the time we get here, it's already been
22085 converted to host endianness, so we just need to sign- or
22086 zero-extend it as appropriate. */
22087 case DW_FORM_data1:
22088 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22089 break;
22090 case DW_FORM_data2:
22091 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22092 break;
22093 case DW_FORM_data4:
22094 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22095 break;
22096 case DW_FORM_data8:
22097 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22098 break;
22099
22100 case DW_FORM_sdata:
22101 case DW_FORM_implicit_const:
22102 *value = DW_SND (attr);
22103 break;
22104
22105 case DW_FORM_udata:
22106 *value = DW_UNSND (attr);
22107 break;
22108
22109 default:
22110 complaint (_("unsupported const value attribute form: '%s'"),
22111 dwarf_form_name (attr->form));
22112 *value = 0;
22113 break;
22114 }
22115 }
22116
22117
22118 /* Copy constant value from an attribute to a symbol. */
22119
22120 static void
22121 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22122 struct dwarf2_cu *cu)
22123 {
22124 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22125 LONGEST value;
22126 const gdb_byte *bytes;
22127 struct dwarf2_locexpr_baton *baton;
22128
22129 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22130 SYMBOL_PRINT_NAME (sym),
22131 &objfile->objfile_obstack, cu,
22132 &value, &bytes, &baton);
22133
22134 if (baton != NULL)
22135 {
22136 SYMBOL_LOCATION_BATON (sym) = baton;
22137 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22138 }
22139 else if (bytes != NULL)
22140 {
22141 SYMBOL_VALUE_BYTES (sym) = bytes;
22142 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22143 }
22144 else
22145 {
22146 SYMBOL_VALUE (sym) = value;
22147 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22148 }
22149 }
22150
22151 /* Return the type of the die in question using its DW_AT_type attribute. */
22152
22153 static struct type *
22154 die_type (struct die_info *die, struct dwarf2_cu *cu)
22155 {
22156 struct attribute *type_attr;
22157
22158 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22159 if (!type_attr)
22160 {
22161 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22162 /* A missing DW_AT_type represents a void type. */
22163 return objfile_type (objfile)->builtin_void;
22164 }
22165
22166 return lookup_die_type (die, type_attr, cu);
22167 }
22168
22169 /* True iff CU's producer generates GNAT Ada auxiliary information
22170 that allows to find parallel types through that information instead
22171 of having to do expensive parallel lookups by type name. */
22172
22173 static int
22174 need_gnat_info (struct dwarf2_cu *cu)
22175 {
22176 /* Assume that the Ada compiler was GNAT, which always produces
22177 the auxiliary information. */
22178 return (cu->language == language_ada);
22179 }
22180
22181 /* Return the auxiliary type of the die in question using its
22182 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22183 attribute is not present. */
22184
22185 static struct type *
22186 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22187 {
22188 struct attribute *type_attr;
22189
22190 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22191 if (!type_attr)
22192 return NULL;
22193
22194 return lookup_die_type (die, type_attr, cu);
22195 }
22196
22197 /* If DIE has a descriptive_type attribute, then set the TYPE's
22198 descriptive type accordingly. */
22199
22200 static void
22201 set_descriptive_type (struct type *type, struct die_info *die,
22202 struct dwarf2_cu *cu)
22203 {
22204 struct type *descriptive_type = die_descriptive_type (die, cu);
22205
22206 if (descriptive_type)
22207 {
22208 ALLOCATE_GNAT_AUX_TYPE (type);
22209 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22210 }
22211 }
22212
22213 /* Return the containing type of the die in question using its
22214 DW_AT_containing_type attribute. */
22215
22216 static struct type *
22217 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22218 {
22219 struct attribute *type_attr;
22220 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22221
22222 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22223 if (!type_attr)
22224 error (_("Dwarf Error: Problem turning containing type into gdb type "
22225 "[in module %s]"), objfile_name (objfile));
22226
22227 return lookup_die_type (die, type_attr, cu);
22228 }
22229
22230 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22231
22232 static struct type *
22233 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22234 {
22235 struct dwarf2_per_objfile *dwarf2_per_objfile
22236 = cu->per_cu->dwarf2_per_objfile;
22237 struct objfile *objfile = dwarf2_per_objfile->objfile;
22238 char *saved;
22239
22240 std::string message
22241 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22242 objfile_name (objfile),
22243 sect_offset_str (cu->header.sect_off),
22244 sect_offset_str (die->sect_off));
22245 saved = obstack_strdup (&objfile->objfile_obstack, message);
22246
22247 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22248 }
22249
22250 /* Look up the type of DIE in CU using its type attribute ATTR.
22251 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22252 DW_AT_containing_type.
22253 If there is no type substitute an error marker. */
22254
22255 static struct type *
22256 lookup_die_type (struct die_info *die, const struct attribute *attr,
22257 struct dwarf2_cu *cu)
22258 {
22259 struct dwarf2_per_objfile *dwarf2_per_objfile
22260 = cu->per_cu->dwarf2_per_objfile;
22261 struct objfile *objfile = dwarf2_per_objfile->objfile;
22262 struct type *this_type;
22263
22264 gdb_assert (attr->name == DW_AT_type
22265 || attr->name == DW_AT_GNAT_descriptive_type
22266 || attr->name == DW_AT_containing_type);
22267
22268 /* First see if we have it cached. */
22269
22270 if (attr->form == DW_FORM_GNU_ref_alt)
22271 {
22272 struct dwarf2_per_cu_data *per_cu;
22273 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22274
22275 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22276 dwarf2_per_objfile);
22277 this_type = get_die_type_at_offset (sect_off, per_cu);
22278 }
22279 else if (attr_form_is_ref (attr))
22280 {
22281 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22282
22283 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
22284 }
22285 else if (attr->form == DW_FORM_ref_sig8)
22286 {
22287 ULONGEST signature = DW_SIGNATURE (attr);
22288
22289 return get_signatured_type (die, signature, cu);
22290 }
22291 else
22292 {
22293 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22294 " at %s [in module %s]"),
22295 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22296 objfile_name (objfile));
22297 return build_error_marker_type (cu, die);
22298 }
22299
22300 /* If not cached we need to read it in. */
22301
22302 if (this_type == NULL)
22303 {
22304 struct die_info *type_die = NULL;
22305 struct dwarf2_cu *type_cu = cu;
22306
22307 if (attr_form_is_ref (attr))
22308 type_die = follow_die_ref (die, attr, &type_cu);
22309 if (type_die == NULL)
22310 return build_error_marker_type (cu, die);
22311 /* If we find the type now, it's probably because the type came
22312 from an inter-CU reference and the type's CU got expanded before
22313 ours. */
22314 this_type = read_type_die (type_die, type_cu);
22315 }
22316
22317 /* If we still don't have a type use an error marker. */
22318
22319 if (this_type == NULL)
22320 return build_error_marker_type (cu, die);
22321
22322 return this_type;
22323 }
22324
22325 /* Return the type in DIE, CU.
22326 Returns NULL for invalid types.
22327
22328 This first does a lookup in die_type_hash,
22329 and only reads the die in if necessary.
22330
22331 NOTE: This can be called when reading in partial or full symbols. */
22332
22333 static struct type *
22334 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22335 {
22336 struct type *this_type;
22337
22338 this_type = get_die_type (die, cu);
22339 if (this_type)
22340 return this_type;
22341
22342 return read_type_die_1 (die, cu);
22343 }
22344
22345 /* Read the type in DIE, CU.
22346 Returns NULL for invalid types. */
22347
22348 static struct type *
22349 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22350 {
22351 struct type *this_type = NULL;
22352
22353 switch (die->tag)
22354 {
22355 case DW_TAG_class_type:
22356 case DW_TAG_interface_type:
22357 case DW_TAG_structure_type:
22358 case DW_TAG_union_type:
22359 this_type = read_structure_type (die, cu);
22360 break;
22361 case DW_TAG_enumeration_type:
22362 this_type = read_enumeration_type (die, cu);
22363 break;
22364 case DW_TAG_subprogram:
22365 case DW_TAG_subroutine_type:
22366 case DW_TAG_inlined_subroutine:
22367 this_type = read_subroutine_type (die, cu);
22368 break;
22369 case DW_TAG_array_type:
22370 this_type = read_array_type (die, cu);
22371 break;
22372 case DW_TAG_set_type:
22373 this_type = read_set_type (die, cu);
22374 break;
22375 case DW_TAG_pointer_type:
22376 this_type = read_tag_pointer_type (die, cu);
22377 break;
22378 case DW_TAG_ptr_to_member_type:
22379 this_type = read_tag_ptr_to_member_type (die, cu);
22380 break;
22381 case DW_TAG_reference_type:
22382 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22383 break;
22384 case DW_TAG_rvalue_reference_type:
22385 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22386 break;
22387 case DW_TAG_const_type:
22388 this_type = read_tag_const_type (die, cu);
22389 break;
22390 case DW_TAG_volatile_type:
22391 this_type = read_tag_volatile_type (die, cu);
22392 break;
22393 case DW_TAG_restrict_type:
22394 this_type = read_tag_restrict_type (die, cu);
22395 break;
22396 case DW_TAG_string_type:
22397 this_type = read_tag_string_type (die, cu);
22398 break;
22399 case DW_TAG_typedef:
22400 this_type = read_typedef (die, cu);
22401 break;
22402 case DW_TAG_subrange_type:
22403 this_type = read_subrange_type (die, cu);
22404 break;
22405 case DW_TAG_base_type:
22406 this_type = read_base_type (die, cu);
22407 break;
22408 case DW_TAG_unspecified_type:
22409 this_type = read_unspecified_type (die, cu);
22410 break;
22411 case DW_TAG_namespace:
22412 this_type = read_namespace_type (die, cu);
22413 break;
22414 case DW_TAG_module:
22415 this_type = read_module_type (die, cu);
22416 break;
22417 case DW_TAG_atomic_type:
22418 this_type = read_tag_atomic_type (die, cu);
22419 break;
22420 default:
22421 complaint (_("unexpected tag in read_type_die: '%s'"),
22422 dwarf_tag_name (die->tag));
22423 break;
22424 }
22425
22426 return this_type;
22427 }
22428
22429 /* See if we can figure out if the class lives in a namespace. We do
22430 this by looking for a member function; its demangled name will
22431 contain namespace info, if there is any.
22432 Return the computed name or NULL.
22433 Space for the result is allocated on the objfile's obstack.
22434 This is the full-die version of guess_partial_die_structure_name.
22435 In this case we know DIE has no useful parent. */
22436
22437 static char *
22438 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22439 {
22440 struct die_info *spec_die;
22441 struct dwarf2_cu *spec_cu;
22442 struct die_info *child;
22443 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22444
22445 spec_cu = cu;
22446 spec_die = die_specification (die, &spec_cu);
22447 if (spec_die != NULL)
22448 {
22449 die = spec_die;
22450 cu = spec_cu;
22451 }
22452
22453 for (child = die->child;
22454 child != NULL;
22455 child = child->sibling)
22456 {
22457 if (child->tag == DW_TAG_subprogram)
22458 {
22459 const char *linkage_name = dw2_linkage_name (child, cu);
22460
22461 if (linkage_name != NULL)
22462 {
22463 char *actual_name
22464 = language_class_name_from_physname (cu->language_defn,
22465 linkage_name);
22466 char *name = NULL;
22467
22468 if (actual_name != NULL)
22469 {
22470 const char *die_name = dwarf2_name (die, cu);
22471
22472 if (die_name != NULL
22473 && strcmp (die_name, actual_name) != 0)
22474 {
22475 /* Strip off the class name from the full name.
22476 We want the prefix. */
22477 int die_name_len = strlen (die_name);
22478 int actual_name_len = strlen (actual_name);
22479
22480 /* Test for '::' as a sanity check. */
22481 if (actual_name_len > die_name_len + 2
22482 && actual_name[actual_name_len
22483 - die_name_len - 1] == ':')
22484 name = obstack_strndup (
22485 &objfile->per_bfd->storage_obstack,
22486 actual_name, actual_name_len - die_name_len - 2);
22487 }
22488 }
22489 xfree (actual_name);
22490 return name;
22491 }
22492 }
22493 }
22494
22495 return NULL;
22496 }
22497
22498 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22499 prefix part in such case. See
22500 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22501
22502 static const char *
22503 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22504 {
22505 struct attribute *attr;
22506 const char *base;
22507
22508 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22509 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22510 return NULL;
22511
22512 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22513 return NULL;
22514
22515 attr = dw2_linkage_name_attr (die, cu);
22516 if (attr == NULL || DW_STRING (attr) == NULL)
22517 return NULL;
22518
22519 /* dwarf2_name had to be already called. */
22520 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22521
22522 /* Strip the base name, keep any leading namespaces/classes. */
22523 base = strrchr (DW_STRING (attr), ':');
22524 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22525 return "";
22526
22527 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22528 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22529 DW_STRING (attr),
22530 &base[-1] - DW_STRING (attr));
22531 }
22532
22533 /* Return the name of the namespace/class that DIE is defined within,
22534 or "" if we can't tell. The caller should not xfree the result.
22535
22536 For example, if we're within the method foo() in the following
22537 code:
22538
22539 namespace N {
22540 class C {
22541 void foo () {
22542 }
22543 };
22544 }
22545
22546 then determine_prefix on foo's die will return "N::C". */
22547
22548 static const char *
22549 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22550 {
22551 struct dwarf2_per_objfile *dwarf2_per_objfile
22552 = cu->per_cu->dwarf2_per_objfile;
22553 struct die_info *parent, *spec_die;
22554 struct dwarf2_cu *spec_cu;
22555 struct type *parent_type;
22556 const char *retval;
22557
22558 if (cu->language != language_cplus
22559 && cu->language != language_fortran && cu->language != language_d
22560 && cu->language != language_rust)
22561 return "";
22562
22563 retval = anonymous_struct_prefix (die, cu);
22564 if (retval)
22565 return retval;
22566
22567 /* We have to be careful in the presence of DW_AT_specification.
22568 For example, with GCC 3.4, given the code
22569
22570 namespace N {
22571 void foo() {
22572 // Definition of N::foo.
22573 }
22574 }
22575
22576 then we'll have a tree of DIEs like this:
22577
22578 1: DW_TAG_compile_unit
22579 2: DW_TAG_namespace // N
22580 3: DW_TAG_subprogram // declaration of N::foo
22581 4: DW_TAG_subprogram // definition of N::foo
22582 DW_AT_specification // refers to die #3
22583
22584 Thus, when processing die #4, we have to pretend that we're in
22585 the context of its DW_AT_specification, namely the contex of die
22586 #3. */
22587 spec_cu = cu;
22588 spec_die = die_specification (die, &spec_cu);
22589 if (spec_die == NULL)
22590 parent = die->parent;
22591 else
22592 {
22593 parent = spec_die->parent;
22594 cu = spec_cu;
22595 }
22596
22597 if (parent == NULL)
22598 return "";
22599 else if (parent->building_fullname)
22600 {
22601 const char *name;
22602 const char *parent_name;
22603
22604 /* It has been seen on RealView 2.2 built binaries,
22605 DW_TAG_template_type_param types actually _defined_ as
22606 children of the parent class:
22607
22608 enum E {};
22609 template class <class Enum> Class{};
22610 Class<enum E> class_e;
22611
22612 1: DW_TAG_class_type (Class)
22613 2: DW_TAG_enumeration_type (E)
22614 3: DW_TAG_enumerator (enum1:0)
22615 3: DW_TAG_enumerator (enum2:1)
22616 ...
22617 2: DW_TAG_template_type_param
22618 DW_AT_type DW_FORM_ref_udata (E)
22619
22620 Besides being broken debug info, it can put GDB into an
22621 infinite loop. Consider:
22622
22623 When we're building the full name for Class<E>, we'll start
22624 at Class, and go look over its template type parameters,
22625 finding E. We'll then try to build the full name of E, and
22626 reach here. We're now trying to build the full name of E,
22627 and look over the parent DIE for containing scope. In the
22628 broken case, if we followed the parent DIE of E, we'd again
22629 find Class, and once again go look at its template type
22630 arguments, etc., etc. Simply don't consider such parent die
22631 as source-level parent of this die (it can't be, the language
22632 doesn't allow it), and break the loop here. */
22633 name = dwarf2_name (die, cu);
22634 parent_name = dwarf2_name (parent, cu);
22635 complaint (_("template param type '%s' defined within parent '%s'"),
22636 name ? name : "<unknown>",
22637 parent_name ? parent_name : "<unknown>");
22638 return "";
22639 }
22640 else
22641 switch (parent->tag)
22642 {
22643 case DW_TAG_namespace:
22644 parent_type = read_type_die (parent, cu);
22645 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22646 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22647 Work around this problem here. */
22648 if (cu->language == language_cplus
22649 && strcmp (TYPE_NAME (parent_type), "::") == 0)
22650 return "";
22651 /* We give a name to even anonymous namespaces. */
22652 return TYPE_NAME (parent_type);
22653 case DW_TAG_class_type:
22654 case DW_TAG_interface_type:
22655 case DW_TAG_structure_type:
22656 case DW_TAG_union_type:
22657 case DW_TAG_module:
22658 parent_type = read_type_die (parent, cu);
22659 if (TYPE_NAME (parent_type) != NULL)
22660 return TYPE_NAME (parent_type);
22661 else
22662 /* An anonymous structure is only allowed non-static data
22663 members; no typedefs, no member functions, et cetera.
22664 So it does not need a prefix. */
22665 return "";
22666 case DW_TAG_compile_unit:
22667 case DW_TAG_partial_unit:
22668 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22669 if (cu->language == language_cplus
22670 && !dwarf2_per_objfile->types.empty ()
22671 && die->child != NULL
22672 && (die->tag == DW_TAG_class_type
22673 || die->tag == DW_TAG_structure_type
22674 || die->tag == DW_TAG_union_type))
22675 {
22676 char *name = guess_full_die_structure_name (die, cu);
22677 if (name != NULL)
22678 return name;
22679 }
22680 return "";
22681 case DW_TAG_subprogram:
22682 /* Nested subroutines in Fortran get a prefix with the name
22683 of the parent's subroutine. */
22684 if (cu->language == language_fortran)
22685 {
22686 if ((die->tag == DW_TAG_subprogram)
22687 && (dwarf2_name (parent, cu) != NULL))
22688 return dwarf2_name (parent, cu);
22689 }
22690 return determine_prefix (parent, cu);
22691 case DW_TAG_enumeration_type:
22692 parent_type = read_type_die (parent, cu);
22693 if (TYPE_DECLARED_CLASS (parent_type))
22694 {
22695 if (TYPE_NAME (parent_type) != NULL)
22696 return TYPE_NAME (parent_type);
22697 return "";
22698 }
22699 /* Fall through. */
22700 default:
22701 return determine_prefix (parent, cu);
22702 }
22703 }
22704
22705 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22706 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22707 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22708 an obconcat, otherwise allocate storage for the result. The CU argument is
22709 used to determine the language and hence, the appropriate separator. */
22710
22711 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22712
22713 static char *
22714 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22715 int physname, struct dwarf2_cu *cu)
22716 {
22717 const char *lead = "";
22718 const char *sep;
22719
22720 if (suffix == NULL || suffix[0] == '\0'
22721 || prefix == NULL || prefix[0] == '\0')
22722 sep = "";
22723 else if (cu->language == language_d)
22724 {
22725 /* For D, the 'main' function could be defined in any module, but it
22726 should never be prefixed. */
22727 if (strcmp (suffix, "D main") == 0)
22728 {
22729 prefix = "";
22730 sep = "";
22731 }
22732 else
22733 sep = ".";
22734 }
22735 else if (cu->language == language_fortran && physname)
22736 {
22737 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22738 DW_AT_MIPS_linkage_name is preferred and used instead. */
22739
22740 lead = "__";
22741 sep = "_MOD_";
22742 }
22743 else
22744 sep = "::";
22745
22746 if (prefix == NULL)
22747 prefix = "";
22748 if (suffix == NULL)
22749 suffix = "";
22750
22751 if (obs == NULL)
22752 {
22753 char *retval
22754 = ((char *)
22755 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22756
22757 strcpy (retval, lead);
22758 strcat (retval, prefix);
22759 strcat (retval, sep);
22760 strcat (retval, suffix);
22761 return retval;
22762 }
22763 else
22764 {
22765 /* We have an obstack. */
22766 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22767 }
22768 }
22769
22770 /* Return sibling of die, NULL if no sibling. */
22771
22772 static struct die_info *
22773 sibling_die (struct die_info *die)
22774 {
22775 return die->sibling;
22776 }
22777
22778 /* Get name of a die, return NULL if not found. */
22779
22780 static const char *
22781 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22782 struct obstack *obstack)
22783 {
22784 if (name && cu->language == language_cplus)
22785 {
22786 std::string canon_name = cp_canonicalize_string (name);
22787
22788 if (!canon_name.empty ())
22789 {
22790 if (canon_name != name)
22791 name = obstack_strdup (obstack, canon_name);
22792 }
22793 }
22794
22795 return name;
22796 }
22797
22798 /* Get name of a die, return NULL if not found.
22799 Anonymous namespaces are converted to their magic string. */
22800
22801 static const char *
22802 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22803 {
22804 struct attribute *attr;
22805 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22806
22807 attr = dwarf2_attr (die, DW_AT_name, cu);
22808 if ((!attr || !DW_STRING (attr))
22809 && die->tag != DW_TAG_namespace
22810 && die->tag != DW_TAG_class_type
22811 && die->tag != DW_TAG_interface_type
22812 && die->tag != DW_TAG_structure_type
22813 && die->tag != DW_TAG_union_type)
22814 return NULL;
22815
22816 switch (die->tag)
22817 {
22818 case DW_TAG_compile_unit:
22819 case DW_TAG_partial_unit:
22820 /* Compilation units have a DW_AT_name that is a filename, not
22821 a source language identifier. */
22822 case DW_TAG_enumeration_type:
22823 case DW_TAG_enumerator:
22824 /* These tags always have simple identifiers already; no need
22825 to canonicalize them. */
22826 return DW_STRING (attr);
22827
22828 case DW_TAG_namespace:
22829 if (attr != NULL && DW_STRING (attr) != NULL)
22830 return DW_STRING (attr);
22831 return CP_ANONYMOUS_NAMESPACE_STR;
22832
22833 case DW_TAG_class_type:
22834 case DW_TAG_interface_type:
22835 case DW_TAG_structure_type:
22836 case DW_TAG_union_type:
22837 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22838 structures or unions. These were of the form "._%d" in GCC 4.1,
22839 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22840 and GCC 4.4. We work around this problem by ignoring these. */
22841 if (attr && DW_STRING (attr)
22842 && (startswith (DW_STRING (attr), "._")
22843 || startswith (DW_STRING (attr), "<anonymous")))
22844 return NULL;
22845
22846 /* GCC might emit a nameless typedef that has a linkage name. See
22847 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22848 if (!attr || DW_STRING (attr) == NULL)
22849 {
22850 char *demangled = NULL;
22851
22852 attr = dw2_linkage_name_attr (die, cu);
22853 if (attr == NULL || DW_STRING (attr) == NULL)
22854 return NULL;
22855
22856 /* Avoid demangling DW_STRING (attr) the second time on a second
22857 call for the same DIE. */
22858 if (!DW_STRING_IS_CANONICAL (attr))
22859 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22860
22861 if (demangled)
22862 {
22863 const char *base;
22864
22865 /* FIXME: we already did this for the partial symbol... */
22866 DW_STRING (attr)
22867 = obstack_strdup (&objfile->per_bfd->storage_obstack,
22868 demangled);
22869 DW_STRING_IS_CANONICAL (attr) = 1;
22870 xfree (demangled);
22871
22872 /* Strip any leading namespaces/classes, keep only the base name.
22873 DW_AT_name for named DIEs does not contain the prefixes. */
22874 base = strrchr (DW_STRING (attr), ':');
22875 if (base && base > DW_STRING (attr) && base[-1] == ':')
22876 return &base[1];
22877 else
22878 return DW_STRING (attr);
22879 }
22880 }
22881 break;
22882
22883 default:
22884 break;
22885 }
22886
22887 if (!DW_STRING_IS_CANONICAL (attr))
22888 {
22889 DW_STRING (attr)
22890 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22891 &objfile->per_bfd->storage_obstack);
22892 DW_STRING_IS_CANONICAL (attr) = 1;
22893 }
22894 return DW_STRING (attr);
22895 }
22896
22897 /* Return the die that this die in an extension of, or NULL if there
22898 is none. *EXT_CU is the CU containing DIE on input, and the CU
22899 containing the return value on output. */
22900
22901 static struct die_info *
22902 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22903 {
22904 struct attribute *attr;
22905
22906 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22907 if (attr == NULL)
22908 return NULL;
22909
22910 return follow_die_ref (die, attr, ext_cu);
22911 }
22912
22913 /* A convenience function that returns an "unknown" DWARF name,
22914 including the value of V. STR is the name of the entity being
22915 printed, e.g., "TAG". */
22916
22917 static const char *
22918 dwarf_unknown (const char *str, unsigned v)
22919 {
22920 char *cell = get_print_cell ();
22921 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
22922 return cell;
22923 }
22924
22925 /* Convert a DIE tag into its string name. */
22926
22927 static const char *
22928 dwarf_tag_name (unsigned tag)
22929 {
22930 const char *name = get_DW_TAG_name (tag);
22931
22932 if (name == NULL)
22933 return dwarf_unknown ("TAG", tag);
22934
22935 return name;
22936 }
22937
22938 /* Convert a DWARF attribute code into its string name. */
22939
22940 static const char *
22941 dwarf_attr_name (unsigned attr)
22942 {
22943 const char *name;
22944
22945 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22946 if (attr == DW_AT_MIPS_fde)
22947 return "DW_AT_MIPS_fde";
22948 #else
22949 if (attr == DW_AT_HP_block_index)
22950 return "DW_AT_HP_block_index";
22951 #endif
22952
22953 name = get_DW_AT_name (attr);
22954
22955 if (name == NULL)
22956 return dwarf_unknown ("AT", attr);
22957
22958 return name;
22959 }
22960
22961 /* Convert a unit type to corresponding DW_UT name. */
22962
22963 static const char *
22964 dwarf_unit_type_name (int unit_type) {
22965 switch (unit_type)
22966 {
22967 case 0x01:
22968 return "DW_UT_compile (0x01)";
22969 case 0x02:
22970 return "DW_UT_type (0x02)";
22971 case 0x03:
22972 return "DW_UT_partial (0x03)";
22973 case 0x04:
22974 return "DW_UT_skeleton (0x04)";
22975 case 0x05:
22976 return "DW_UT_split_compile (0x05)";
22977 case 0x06:
22978 return "DW_UT_split_type (0x06)";
22979 case 0x80:
22980 return "DW_UT_lo_user (0x80)";
22981 case 0xff:
22982 return "DW_UT_hi_user (0xff)";
22983 default:
22984 return nullptr;
22985 }
22986 }
22987
22988 /* Convert a DWARF value form code into its string name. */
22989
22990 static const char *
22991 dwarf_form_name (unsigned form)
22992 {
22993 const char *name = get_DW_FORM_name (form);
22994
22995 if (name == NULL)
22996 return dwarf_unknown ("FORM", form);
22997
22998 return name;
22999 }
23000
23001 static const char *
23002 dwarf_bool_name (unsigned mybool)
23003 {
23004 if (mybool)
23005 return "TRUE";
23006 else
23007 return "FALSE";
23008 }
23009
23010 /* Convert a DWARF type code into its string name. */
23011
23012 static const char *
23013 dwarf_type_encoding_name (unsigned enc)
23014 {
23015 const char *name = get_DW_ATE_name (enc);
23016
23017 if (name == NULL)
23018 return dwarf_unknown ("ATE", enc);
23019
23020 return name;
23021 }
23022
23023 static void
23024 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23025 {
23026 unsigned int i;
23027
23028 print_spaces (indent, f);
23029 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23030 dwarf_tag_name (die->tag), die->abbrev,
23031 sect_offset_str (die->sect_off));
23032
23033 if (die->parent != NULL)
23034 {
23035 print_spaces (indent, f);
23036 fprintf_unfiltered (f, " parent at offset: %s\n",
23037 sect_offset_str (die->parent->sect_off));
23038 }
23039
23040 print_spaces (indent, f);
23041 fprintf_unfiltered (f, " has children: %s\n",
23042 dwarf_bool_name (die->child != NULL));
23043
23044 print_spaces (indent, f);
23045 fprintf_unfiltered (f, " attributes:\n");
23046
23047 for (i = 0; i < die->num_attrs; ++i)
23048 {
23049 print_spaces (indent, f);
23050 fprintf_unfiltered (f, " %s (%s) ",
23051 dwarf_attr_name (die->attrs[i].name),
23052 dwarf_form_name (die->attrs[i].form));
23053
23054 switch (die->attrs[i].form)
23055 {
23056 case DW_FORM_addr:
23057 case DW_FORM_addrx:
23058 case DW_FORM_GNU_addr_index:
23059 fprintf_unfiltered (f, "address: ");
23060 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
23061 break;
23062 case DW_FORM_block2:
23063 case DW_FORM_block4:
23064 case DW_FORM_block:
23065 case DW_FORM_block1:
23066 fprintf_unfiltered (f, "block: size %s",
23067 pulongest (DW_BLOCK (&die->attrs[i])->size));
23068 break;
23069 case DW_FORM_exprloc:
23070 fprintf_unfiltered (f, "expression: size %s",
23071 pulongest (DW_BLOCK (&die->attrs[i])->size));
23072 break;
23073 case DW_FORM_data16:
23074 fprintf_unfiltered (f, "constant of 16 bytes");
23075 break;
23076 case DW_FORM_ref_addr:
23077 fprintf_unfiltered (f, "ref address: ");
23078 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23079 break;
23080 case DW_FORM_GNU_ref_alt:
23081 fprintf_unfiltered (f, "alt ref address: ");
23082 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23083 break;
23084 case DW_FORM_ref1:
23085 case DW_FORM_ref2:
23086 case DW_FORM_ref4:
23087 case DW_FORM_ref8:
23088 case DW_FORM_ref_udata:
23089 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23090 (long) (DW_UNSND (&die->attrs[i])));
23091 break;
23092 case DW_FORM_data1:
23093 case DW_FORM_data2:
23094 case DW_FORM_data4:
23095 case DW_FORM_data8:
23096 case DW_FORM_udata:
23097 case DW_FORM_sdata:
23098 fprintf_unfiltered (f, "constant: %s",
23099 pulongest (DW_UNSND (&die->attrs[i])));
23100 break;
23101 case DW_FORM_sec_offset:
23102 fprintf_unfiltered (f, "section offset: %s",
23103 pulongest (DW_UNSND (&die->attrs[i])));
23104 break;
23105 case DW_FORM_ref_sig8:
23106 fprintf_unfiltered (f, "signature: %s",
23107 hex_string (DW_SIGNATURE (&die->attrs[i])));
23108 break;
23109 case DW_FORM_string:
23110 case DW_FORM_strp:
23111 case DW_FORM_line_strp:
23112 case DW_FORM_strx:
23113 case DW_FORM_GNU_str_index:
23114 case DW_FORM_GNU_strp_alt:
23115 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23116 DW_STRING (&die->attrs[i])
23117 ? DW_STRING (&die->attrs[i]) : "",
23118 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
23119 break;
23120 case DW_FORM_flag:
23121 if (DW_UNSND (&die->attrs[i]))
23122 fprintf_unfiltered (f, "flag: TRUE");
23123 else
23124 fprintf_unfiltered (f, "flag: FALSE");
23125 break;
23126 case DW_FORM_flag_present:
23127 fprintf_unfiltered (f, "flag: TRUE");
23128 break;
23129 case DW_FORM_indirect:
23130 /* The reader will have reduced the indirect form to
23131 the "base form" so this form should not occur. */
23132 fprintf_unfiltered (f,
23133 "unexpected attribute form: DW_FORM_indirect");
23134 break;
23135 case DW_FORM_implicit_const:
23136 fprintf_unfiltered (f, "constant: %s",
23137 plongest (DW_SND (&die->attrs[i])));
23138 break;
23139 default:
23140 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23141 die->attrs[i].form);
23142 break;
23143 }
23144 fprintf_unfiltered (f, "\n");
23145 }
23146 }
23147
23148 static void
23149 dump_die_for_error (struct die_info *die)
23150 {
23151 dump_die_shallow (gdb_stderr, 0, die);
23152 }
23153
23154 static void
23155 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23156 {
23157 int indent = level * 4;
23158
23159 gdb_assert (die != NULL);
23160
23161 if (level >= max_level)
23162 return;
23163
23164 dump_die_shallow (f, indent, die);
23165
23166 if (die->child != NULL)
23167 {
23168 print_spaces (indent, f);
23169 fprintf_unfiltered (f, " Children:");
23170 if (level + 1 < max_level)
23171 {
23172 fprintf_unfiltered (f, "\n");
23173 dump_die_1 (f, level + 1, max_level, die->child);
23174 }
23175 else
23176 {
23177 fprintf_unfiltered (f,
23178 " [not printed, max nesting level reached]\n");
23179 }
23180 }
23181
23182 if (die->sibling != NULL && level > 0)
23183 {
23184 dump_die_1 (f, level, max_level, die->sibling);
23185 }
23186 }
23187
23188 /* This is called from the pdie macro in gdbinit.in.
23189 It's not static so gcc will keep a copy callable from gdb. */
23190
23191 void
23192 dump_die (struct die_info *die, int max_level)
23193 {
23194 dump_die_1 (gdb_stdlog, 0, max_level, die);
23195 }
23196
23197 static void
23198 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23199 {
23200 void **slot;
23201
23202 slot = htab_find_slot_with_hash (cu->die_hash, die,
23203 to_underlying (die->sect_off),
23204 INSERT);
23205
23206 *slot = die;
23207 }
23208
23209 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23210 required kind. */
23211
23212 static sect_offset
23213 dwarf2_get_ref_die_offset (const struct attribute *attr)
23214 {
23215 if (attr_form_is_ref (attr))
23216 return (sect_offset) DW_UNSND (attr);
23217
23218 complaint (_("unsupported die ref attribute form: '%s'"),
23219 dwarf_form_name (attr->form));
23220 return {};
23221 }
23222
23223 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23224 * the value held by the attribute is not constant. */
23225
23226 static LONGEST
23227 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
23228 {
23229 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
23230 return DW_SND (attr);
23231 else if (attr->form == DW_FORM_udata
23232 || attr->form == DW_FORM_data1
23233 || attr->form == DW_FORM_data2
23234 || attr->form == DW_FORM_data4
23235 || attr->form == DW_FORM_data8)
23236 return DW_UNSND (attr);
23237 else
23238 {
23239 /* For DW_FORM_data16 see attr_form_is_constant. */
23240 complaint (_("Attribute value is not a constant (%s)"),
23241 dwarf_form_name (attr->form));
23242 return default_value;
23243 }
23244 }
23245
23246 /* Follow reference or signature attribute ATTR of SRC_DIE.
23247 On entry *REF_CU is the CU of SRC_DIE.
23248 On exit *REF_CU is the CU of the result. */
23249
23250 static struct die_info *
23251 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23252 struct dwarf2_cu **ref_cu)
23253 {
23254 struct die_info *die;
23255
23256 if (attr_form_is_ref (attr))
23257 die = follow_die_ref (src_die, attr, ref_cu);
23258 else if (attr->form == DW_FORM_ref_sig8)
23259 die = follow_die_sig (src_die, attr, ref_cu);
23260 else
23261 {
23262 dump_die_for_error (src_die);
23263 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23264 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23265 }
23266
23267 return die;
23268 }
23269
23270 /* Follow reference OFFSET.
23271 On entry *REF_CU is the CU of the source die referencing OFFSET.
23272 On exit *REF_CU is the CU of the result.
23273 Returns NULL if OFFSET is invalid. */
23274
23275 static struct die_info *
23276 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23277 struct dwarf2_cu **ref_cu)
23278 {
23279 struct die_info temp_die;
23280 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23281 struct dwarf2_per_objfile *dwarf2_per_objfile
23282 = cu->per_cu->dwarf2_per_objfile;
23283
23284 gdb_assert (cu->per_cu != NULL);
23285
23286 target_cu = cu;
23287
23288 if (cu->per_cu->is_debug_types)
23289 {
23290 /* .debug_types CUs cannot reference anything outside their CU.
23291 If they need to, they have to reference a signatured type via
23292 DW_FORM_ref_sig8. */
23293 if (!offset_in_cu_p (&cu->header, sect_off))
23294 return NULL;
23295 }
23296 else if (offset_in_dwz != cu->per_cu->is_dwz
23297 || !offset_in_cu_p (&cu->header, sect_off))
23298 {
23299 struct dwarf2_per_cu_data *per_cu;
23300
23301 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23302 dwarf2_per_objfile);
23303
23304 /* If necessary, add it to the queue and load its DIEs. */
23305 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23306 load_full_comp_unit (per_cu, false, cu->language);
23307
23308 target_cu = per_cu->cu;
23309 }
23310 else if (cu->dies == NULL)
23311 {
23312 /* We're loading full DIEs during partial symbol reading. */
23313 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23314 load_full_comp_unit (cu->per_cu, false, language_minimal);
23315 }
23316
23317 *ref_cu = target_cu;
23318 temp_die.sect_off = sect_off;
23319
23320 if (target_cu != cu)
23321 target_cu->ancestor = cu;
23322
23323 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23324 &temp_die,
23325 to_underlying (sect_off));
23326 }
23327
23328 /* Follow reference attribute ATTR of SRC_DIE.
23329 On entry *REF_CU is the CU of SRC_DIE.
23330 On exit *REF_CU is the CU of the result. */
23331
23332 static struct die_info *
23333 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23334 struct dwarf2_cu **ref_cu)
23335 {
23336 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23337 struct dwarf2_cu *cu = *ref_cu;
23338 struct die_info *die;
23339
23340 die = follow_die_offset (sect_off,
23341 (attr->form == DW_FORM_GNU_ref_alt
23342 || cu->per_cu->is_dwz),
23343 ref_cu);
23344 if (!die)
23345 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23346 "at %s [in module %s]"),
23347 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23348 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23349
23350 return die;
23351 }
23352
23353 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23354 Returned value is intended for DW_OP_call*. Returned
23355 dwarf2_locexpr_baton->data has lifetime of
23356 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23357
23358 struct dwarf2_locexpr_baton
23359 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23360 struct dwarf2_per_cu_data *per_cu,
23361 CORE_ADDR (*get_frame_pc) (void *baton),
23362 void *baton, bool resolve_abstract_p)
23363 {
23364 struct dwarf2_cu *cu;
23365 struct die_info *die;
23366 struct attribute *attr;
23367 struct dwarf2_locexpr_baton retval;
23368 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23369 struct objfile *objfile = dwarf2_per_objfile->objfile;
23370
23371 if (per_cu->cu == NULL)
23372 load_cu (per_cu, false);
23373 cu = per_cu->cu;
23374 if (cu == NULL)
23375 {
23376 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23377 Instead just throw an error, not much else we can do. */
23378 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23379 sect_offset_str (sect_off), objfile_name (objfile));
23380 }
23381
23382 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23383 if (!die)
23384 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23385 sect_offset_str (sect_off), objfile_name (objfile));
23386
23387 attr = dwarf2_attr (die, DW_AT_location, cu);
23388 if (!attr && resolve_abstract_p
23389 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
23390 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23391 {
23392 CORE_ADDR pc = (*get_frame_pc) (baton);
23393 CORE_ADDR baseaddr
23394 = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23395 struct gdbarch *gdbarch = get_objfile_arch (objfile);
23396
23397 for (const auto &cand_off
23398 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
23399 {
23400 struct dwarf2_cu *cand_cu = cu;
23401 struct die_info *cand
23402 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23403 if (!cand
23404 || !cand->parent
23405 || cand->parent->tag != DW_TAG_subprogram)
23406 continue;
23407
23408 CORE_ADDR pc_low, pc_high;
23409 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23410 if (pc_low == ((CORE_ADDR) -1))
23411 continue;
23412 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23413 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23414 if (!(pc_low <= pc && pc < pc_high))
23415 continue;
23416
23417 die = cand;
23418 attr = dwarf2_attr (die, DW_AT_location, cu);
23419 break;
23420 }
23421 }
23422
23423 if (!attr)
23424 {
23425 /* DWARF: "If there is no such attribute, then there is no effect.".
23426 DATA is ignored if SIZE is 0. */
23427
23428 retval.data = NULL;
23429 retval.size = 0;
23430 }
23431 else if (attr_form_is_section_offset (attr))
23432 {
23433 struct dwarf2_loclist_baton loclist_baton;
23434 CORE_ADDR pc = (*get_frame_pc) (baton);
23435 size_t size;
23436
23437 fill_in_loclist_baton (cu, &loclist_baton, attr);
23438
23439 retval.data = dwarf2_find_location_expression (&loclist_baton,
23440 &size, pc);
23441 retval.size = size;
23442 }
23443 else
23444 {
23445 if (!attr_form_is_block (attr))
23446 error (_("Dwarf Error: DIE at %s referenced in module %s "
23447 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23448 sect_offset_str (sect_off), objfile_name (objfile));
23449
23450 retval.data = DW_BLOCK (attr)->data;
23451 retval.size = DW_BLOCK (attr)->size;
23452 }
23453 retval.per_cu = cu->per_cu;
23454
23455 age_cached_comp_units (dwarf2_per_objfile);
23456
23457 return retval;
23458 }
23459
23460 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23461 offset. */
23462
23463 struct dwarf2_locexpr_baton
23464 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23465 struct dwarf2_per_cu_data *per_cu,
23466 CORE_ADDR (*get_frame_pc) (void *baton),
23467 void *baton)
23468 {
23469 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23470
23471 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23472 }
23473
23474 /* Write a constant of a given type as target-ordered bytes into
23475 OBSTACK. */
23476
23477 static const gdb_byte *
23478 write_constant_as_bytes (struct obstack *obstack,
23479 enum bfd_endian byte_order,
23480 struct type *type,
23481 ULONGEST value,
23482 LONGEST *len)
23483 {
23484 gdb_byte *result;
23485
23486 *len = TYPE_LENGTH (type);
23487 result = (gdb_byte *) obstack_alloc (obstack, *len);
23488 store_unsigned_integer (result, *len, byte_order, value);
23489
23490 return result;
23491 }
23492
23493 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23494 pointer to the constant bytes and set LEN to the length of the
23495 data. If memory is needed, allocate it on OBSTACK. If the DIE
23496 does not have a DW_AT_const_value, return NULL. */
23497
23498 const gdb_byte *
23499 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23500 struct dwarf2_per_cu_data *per_cu,
23501 struct obstack *obstack,
23502 LONGEST *len)
23503 {
23504 struct dwarf2_cu *cu;
23505 struct die_info *die;
23506 struct attribute *attr;
23507 const gdb_byte *result = NULL;
23508 struct type *type;
23509 LONGEST value;
23510 enum bfd_endian byte_order;
23511 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23512
23513 if (per_cu->cu == NULL)
23514 load_cu (per_cu, false);
23515 cu = per_cu->cu;
23516 if (cu == NULL)
23517 {
23518 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23519 Instead just throw an error, not much else we can do. */
23520 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23521 sect_offset_str (sect_off), objfile_name (objfile));
23522 }
23523
23524 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23525 if (!die)
23526 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23527 sect_offset_str (sect_off), objfile_name (objfile));
23528
23529 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23530 if (attr == NULL)
23531 return NULL;
23532
23533 byte_order = (bfd_big_endian (objfile->obfd)
23534 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23535
23536 switch (attr->form)
23537 {
23538 case DW_FORM_addr:
23539 case DW_FORM_addrx:
23540 case DW_FORM_GNU_addr_index:
23541 {
23542 gdb_byte *tem;
23543
23544 *len = cu->header.addr_size;
23545 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23546 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23547 result = tem;
23548 }
23549 break;
23550 case DW_FORM_string:
23551 case DW_FORM_strp:
23552 case DW_FORM_strx:
23553 case DW_FORM_GNU_str_index:
23554 case DW_FORM_GNU_strp_alt:
23555 /* DW_STRING is already allocated on the objfile obstack, point
23556 directly to it. */
23557 result = (const gdb_byte *) DW_STRING (attr);
23558 *len = strlen (DW_STRING (attr));
23559 break;
23560 case DW_FORM_block1:
23561 case DW_FORM_block2:
23562 case DW_FORM_block4:
23563 case DW_FORM_block:
23564 case DW_FORM_exprloc:
23565 case DW_FORM_data16:
23566 result = DW_BLOCK (attr)->data;
23567 *len = DW_BLOCK (attr)->size;
23568 break;
23569
23570 /* The DW_AT_const_value attributes are supposed to carry the
23571 symbol's value "represented as it would be on the target
23572 architecture." By the time we get here, it's already been
23573 converted to host endianness, so we just need to sign- or
23574 zero-extend it as appropriate. */
23575 case DW_FORM_data1:
23576 type = die_type (die, cu);
23577 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23578 if (result == NULL)
23579 result = write_constant_as_bytes (obstack, byte_order,
23580 type, value, len);
23581 break;
23582 case DW_FORM_data2:
23583 type = die_type (die, cu);
23584 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23585 if (result == NULL)
23586 result = write_constant_as_bytes (obstack, byte_order,
23587 type, value, len);
23588 break;
23589 case DW_FORM_data4:
23590 type = die_type (die, cu);
23591 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23592 if (result == NULL)
23593 result = write_constant_as_bytes (obstack, byte_order,
23594 type, value, len);
23595 break;
23596 case DW_FORM_data8:
23597 type = die_type (die, cu);
23598 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23599 if (result == NULL)
23600 result = write_constant_as_bytes (obstack, byte_order,
23601 type, value, len);
23602 break;
23603
23604 case DW_FORM_sdata:
23605 case DW_FORM_implicit_const:
23606 type = die_type (die, cu);
23607 result = write_constant_as_bytes (obstack, byte_order,
23608 type, DW_SND (attr), len);
23609 break;
23610
23611 case DW_FORM_udata:
23612 type = die_type (die, cu);
23613 result = write_constant_as_bytes (obstack, byte_order,
23614 type, DW_UNSND (attr), len);
23615 break;
23616
23617 default:
23618 complaint (_("unsupported const value attribute form: '%s'"),
23619 dwarf_form_name (attr->form));
23620 break;
23621 }
23622
23623 return result;
23624 }
23625
23626 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23627 valid type for this die is found. */
23628
23629 struct type *
23630 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23631 struct dwarf2_per_cu_data *per_cu)
23632 {
23633 struct dwarf2_cu *cu;
23634 struct die_info *die;
23635
23636 if (per_cu->cu == NULL)
23637 load_cu (per_cu, false);
23638 cu = per_cu->cu;
23639 if (!cu)
23640 return NULL;
23641
23642 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23643 if (!die)
23644 return NULL;
23645
23646 return die_type (die, cu);
23647 }
23648
23649 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23650 PER_CU. */
23651
23652 struct type *
23653 dwarf2_get_die_type (cu_offset die_offset,
23654 struct dwarf2_per_cu_data *per_cu)
23655 {
23656 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23657 return get_die_type_at_offset (die_offset_sect, per_cu);
23658 }
23659
23660 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23661 On entry *REF_CU is the CU of SRC_DIE.
23662 On exit *REF_CU is the CU of the result.
23663 Returns NULL if the referenced DIE isn't found. */
23664
23665 static struct die_info *
23666 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23667 struct dwarf2_cu **ref_cu)
23668 {
23669 struct die_info temp_die;
23670 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23671 struct die_info *die;
23672
23673 /* While it might be nice to assert sig_type->type == NULL here,
23674 we can get here for DW_AT_imported_declaration where we need
23675 the DIE not the type. */
23676
23677 /* If necessary, add it to the queue and load its DIEs. */
23678
23679 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23680 read_signatured_type (sig_type);
23681
23682 sig_cu = sig_type->per_cu.cu;
23683 gdb_assert (sig_cu != NULL);
23684 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23685 temp_die.sect_off = sig_type->type_offset_in_section;
23686 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23687 to_underlying (temp_die.sect_off));
23688 if (die)
23689 {
23690 struct dwarf2_per_objfile *dwarf2_per_objfile
23691 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23692
23693 /* For .gdb_index version 7 keep track of included TUs.
23694 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23695 if (dwarf2_per_objfile->index_table != NULL
23696 && dwarf2_per_objfile->index_table->version <= 7)
23697 {
23698 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23699 }
23700
23701 *ref_cu = sig_cu;
23702 if (sig_cu != cu)
23703 sig_cu->ancestor = cu;
23704
23705 return die;
23706 }
23707
23708 return NULL;
23709 }
23710
23711 /* Follow signatured type referenced by ATTR in SRC_DIE.
23712 On entry *REF_CU is the CU of SRC_DIE.
23713 On exit *REF_CU is the CU of the result.
23714 The result is the DIE of the type.
23715 If the referenced type cannot be found an error is thrown. */
23716
23717 static struct die_info *
23718 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23719 struct dwarf2_cu **ref_cu)
23720 {
23721 ULONGEST signature = DW_SIGNATURE (attr);
23722 struct signatured_type *sig_type;
23723 struct die_info *die;
23724
23725 gdb_assert (attr->form == DW_FORM_ref_sig8);
23726
23727 sig_type = lookup_signatured_type (*ref_cu, signature);
23728 /* sig_type will be NULL if the signatured type is missing from
23729 the debug info. */
23730 if (sig_type == NULL)
23731 {
23732 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23733 " from DIE at %s [in module %s]"),
23734 hex_string (signature), sect_offset_str (src_die->sect_off),
23735 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23736 }
23737
23738 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23739 if (die == NULL)
23740 {
23741 dump_die_for_error (src_die);
23742 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23743 " from DIE at %s [in module %s]"),
23744 hex_string (signature), sect_offset_str (src_die->sect_off),
23745 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23746 }
23747
23748 return die;
23749 }
23750
23751 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23752 reading in and processing the type unit if necessary. */
23753
23754 static struct type *
23755 get_signatured_type (struct die_info *die, ULONGEST signature,
23756 struct dwarf2_cu *cu)
23757 {
23758 struct dwarf2_per_objfile *dwarf2_per_objfile
23759 = cu->per_cu->dwarf2_per_objfile;
23760 struct signatured_type *sig_type;
23761 struct dwarf2_cu *type_cu;
23762 struct die_info *type_die;
23763 struct type *type;
23764
23765 sig_type = lookup_signatured_type (cu, signature);
23766 /* sig_type will be NULL if the signatured type is missing from
23767 the debug info. */
23768 if (sig_type == NULL)
23769 {
23770 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23771 " from DIE at %s [in module %s]"),
23772 hex_string (signature), sect_offset_str (die->sect_off),
23773 objfile_name (dwarf2_per_objfile->objfile));
23774 return build_error_marker_type (cu, die);
23775 }
23776
23777 /* If we already know the type we're done. */
23778 if (sig_type->type != NULL)
23779 return sig_type->type;
23780
23781 type_cu = cu;
23782 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23783 if (type_die != NULL)
23784 {
23785 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23786 is created. This is important, for example, because for c++ classes
23787 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23788 type = read_type_die (type_die, type_cu);
23789 if (type == NULL)
23790 {
23791 complaint (_("Dwarf Error: Cannot build signatured type %s"
23792 " referenced from DIE at %s [in module %s]"),
23793 hex_string (signature), sect_offset_str (die->sect_off),
23794 objfile_name (dwarf2_per_objfile->objfile));
23795 type = build_error_marker_type (cu, die);
23796 }
23797 }
23798 else
23799 {
23800 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23801 " from DIE at %s [in module %s]"),
23802 hex_string (signature), sect_offset_str (die->sect_off),
23803 objfile_name (dwarf2_per_objfile->objfile));
23804 type = build_error_marker_type (cu, die);
23805 }
23806 sig_type->type = type;
23807
23808 return type;
23809 }
23810
23811 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23812 reading in and processing the type unit if necessary. */
23813
23814 static struct type *
23815 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23816 struct dwarf2_cu *cu) /* ARI: editCase function */
23817 {
23818 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23819 if (attr_form_is_ref (attr))
23820 {
23821 struct dwarf2_cu *type_cu = cu;
23822 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23823
23824 return read_type_die (type_die, type_cu);
23825 }
23826 else if (attr->form == DW_FORM_ref_sig8)
23827 {
23828 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23829 }
23830 else
23831 {
23832 struct dwarf2_per_objfile *dwarf2_per_objfile
23833 = cu->per_cu->dwarf2_per_objfile;
23834
23835 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23836 " at %s [in module %s]"),
23837 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23838 objfile_name (dwarf2_per_objfile->objfile));
23839 return build_error_marker_type (cu, die);
23840 }
23841 }
23842
23843 /* Load the DIEs associated with type unit PER_CU into memory. */
23844
23845 static void
23846 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23847 {
23848 struct signatured_type *sig_type;
23849
23850 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23851 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23852
23853 /* We have the per_cu, but we need the signatured_type.
23854 Fortunately this is an easy translation. */
23855 gdb_assert (per_cu->is_debug_types);
23856 sig_type = (struct signatured_type *) per_cu;
23857
23858 gdb_assert (per_cu->cu == NULL);
23859
23860 read_signatured_type (sig_type);
23861
23862 gdb_assert (per_cu->cu != NULL);
23863 }
23864
23865 /* die_reader_func for read_signatured_type.
23866 This is identical to load_full_comp_unit_reader,
23867 but is kept separate for now. */
23868
23869 static void
23870 read_signatured_type_reader (const struct die_reader_specs *reader,
23871 const gdb_byte *info_ptr,
23872 struct die_info *comp_unit_die,
23873 int has_children,
23874 void *data)
23875 {
23876 struct dwarf2_cu *cu = reader->cu;
23877
23878 gdb_assert (cu->die_hash == NULL);
23879 cu->die_hash =
23880 htab_create_alloc_ex (cu->header.length / 12,
23881 die_hash,
23882 die_eq,
23883 NULL,
23884 &cu->comp_unit_obstack,
23885 hashtab_obstack_allocate,
23886 dummy_obstack_deallocate);
23887
23888 if (has_children)
23889 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23890 &info_ptr, comp_unit_die);
23891 cu->dies = comp_unit_die;
23892 /* comp_unit_die is not stored in die_hash, no need. */
23893
23894 /* We try not to read any attributes in this function, because not
23895 all CUs needed for references have been loaded yet, and symbol
23896 table processing isn't initialized. But we have to set the CU language,
23897 or we won't be able to build types correctly.
23898 Similarly, if we do not read the producer, we can not apply
23899 producer-specific interpretation. */
23900 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23901 }
23902
23903 /* Read in a signatured type and build its CU and DIEs.
23904 If the type is a stub for the real type in a DWO file,
23905 read in the real type from the DWO file as well. */
23906
23907 static void
23908 read_signatured_type (struct signatured_type *sig_type)
23909 {
23910 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23911
23912 gdb_assert (per_cu->is_debug_types);
23913 gdb_assert (per_cu->cu == NULL);
23914
23915 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
23916 read_signatured_type_reader, NULL);
23917 sig_type->per_cu.tu_read = 1;
23918 }
23919
23920 /* Decode simple location descriptions.
23921 Given a pointer to a dwarf block that defines a location, compute
23922 the location and return the value.
23923
23924 NOTE drow/2003-11-18: This function is called in two situations
23925 now: for the address of static or global variables (partial symbols
23926 only) and for offsets into structures which are expected to be
23927 (more or less) constant. The partial symbol case should go away,
23928 and only the constant case should remain. That will let this
23929 function complain more accurately. A few special modes are allowed
23930 without complaint for global variables (for instance, global
23931 register values and thread-local values).
23932
23933 A location description containing no operations indicates that the
23934 object is optimized out. The return value is 0 for that case.
23935 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23936 callers will only want a very basic result and this can become a
23937 complaint.
23938
23939 Note that stack[0] is unused except as a default error return. */
23940
23941 static CORE_ADDR
23942 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23943 {
23944 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23945 size_t i;
23946 size_t size = blk->size;
23947 const gdb_byte *data = blk->data;
23948 CORE_ADDR stack[64];
23949 int stacki;
23950 unsigned int bytes_read, unsnd;
23951 gdb_byte op;
23952
23953 i = 0;
23954 stacki = 0;
23955 stack[stacki] = 0;
23956 stack[++stacki] = 0;
23957
23958 while (i < size)
23959 {
23960 op = data[i++];
23961 switch (op)
23962 {
23963 case DW_OP_lit0:
23964 case DW_OP_lit1:
23965 case DW_OP_lit2:
23966 case DW_OP_lit3:
23967 case DW_OP_lit4:
23968 case DW_OP_lit5:
23969 case DW_OP_lit6:
23970 case DW_OP_lit7:
23971 case DW_OP_lit8:
23972 case DW_OP_lit9:
23973 case DW_OP_lit10:
23974 case DW_OP_lit11:
23975 case DW_OP_lit12:
23976 case DW_OP_lit13:
23977 case DW_OP_lit14:
23978 case DW_OP_lit15:
23979 case DW_OP_lit16:
23980 case DW_OP_lit17:
23981 case DW_OP_lit18:
23982 case DW_OP_lit19:
23983 case DW_OP_lit20:
23984 case DW_OP_lit21:
23985 case DW_OP_lit22:
23986 case DW_OP_lit23:
23987 case DW_OP_lit24:
23988 case DW_OP_lit25:
23989 case DW_OP_lit26:
23990 case DW_OP_lit27:
23991 case DW_OP_lit28:
23992 case DW_OP_lit29:
23993 case DW_OP_lit30:
23994 case DW_OP_lit31:
23995 stack[++stacki] = op - DW_OP_lit0;
23996 break;
23997
23998 case DW_OP_reg0:
23999 case DW_OP_reg1:
24000 case DW_OP_reg2:
24001 case DW_OP_reg3:
24002 case DW_OP_reg4:
24003 case DW_OP_reg5:
24004 case DW_OP_reg6:
24005 case DW_OP_reg7:
24006 case DW_OP_reg8:
24007 case DW_OP_reg9:
24008 case DW_OP_reg10:
24009 case DW_OP_reg11:
24010 case DW_OP_reg12:
24011 case DW_OP_reg13:
24012 case DW_OP_reg14:
24013 case DW_OP_reg15:
24014 case DW_OP_reg16:
24015 case DW_OP_reg17:
24016 case DW_OP_reg18:
24017 case DW_OP_reg19:
24018 case DW_OP_reg20:
24019 case DW_OP_reg21:
24020 case DW_OP_reg22:
24021 case DW_OP_reg23:
24022 case DW_OP_reg24:
24023 case DW_OP_reg25:
24024 case DW_OP_reg26:
24025 case DW_OP_reg27:
24026 case DW_OP_reg28:
24027 case DW_OP_reg29:
24028 case DW_OP_reg30:
24029 case DW_OP_reg31:
24030 stack[++stacki] = op - DW_OP_reg0;
24031 if (i < size)
24032 dwarf2_complex_location_expr_complaint ();
24033 break;
24034
24035 case DW_OP_regx:
24036 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24037 i += bytes_read;
24038 stack[++stacki] = unsnd;
24039 if (i < size)
24040 dwarf2_complex_location_expr_complaint ();
24041 break;
24042
24043 case DW_OP_addr:
24044 stack[++stacki] = read_address (objfile->obfd, &data[i],
24045 cu, &bytes_read);
24046 i += bytes_read;
24047 break;
24048
24049 case DW_OP_const1u:
24050 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24051 i += 1;
24052 break;
24053
24054 case DW_OP_const1s:
24055 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24056 i += 1;
24057 break;
24058
24059 case DW_OP_const2u:
24060 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24061 i += 2;
24062 break;
24063
24064 case DW_OP_const2s:
24065 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24066 i += 2;
24067 break;
24068
24069 case DW_OP_const4u:
24070 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24071 i += 4;
24072 break;
24073
24074 case DW_OP_const4s:
24075 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24076 i += 4;
24077 break;
24078
24079 case DW_OP_const8u:
24080 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24081 i += 8;
24082 break;
24083
24084 case DW_OP_constu:
24085 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24086 &bytes_read);
24087 i += bytes_read;
24088 break;
24089
24090 case DW_OP_consts:
24091 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24092 i += bytes_read;
24093 break;
24094
24095 case DW_OP_dup:
24096 stack[stacki + 1] = stack[stacki];
24097 stacki++;
24098 break;
24099
24100 case DW_OP_plus:
24101 stack[stacki - 1] += stack[stacki];
24102 stacki--;
24103 break;
24104
24105 case DW_OP_plus_uconst:
24106 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24107 &bytes_read);
24108 i += bytes_read;
24109 break;
24110
24111 case DW_OP_minus:
24112 stack[stacki - 1] -= stack[stacki];
24113 stacki--;
24114 break;
24115
24116 case DW_OP_deref:
24117 /* If we're not the last op, then we definitely can't encode
24118 this using GDB's address_class enum. This is valid for partial
24119 global symbols, although the variable's address will be bogus
24120 in the psymtab. */
24121 if (i < size)
24122 dwarf2_complex_location_expr_complaint ();
24123 break;
24124
24125 case DW_OP_GNU_push_tls_address:
24126 case DW_OP_form_tls_address:
24127 /* The top of the stack has the offset from the beginning
24128 of the thread control block at which the variable is located. */
24129 /* Nothing should follow this operator, so the top of stack would
24130 be returned. */
24131 /* This is valid for partial global symbols, but the variable's
24132 address will be bogus in the psymtab. Make it always at least
24133 non-zero to not look as a variable garbage collected by linker
24134 which have DW_OP_addr 0. */
24135 if (i < size)
24136 dwarf2_complex_location_expr_complaint ();
24137 stack[stacki]++;
24138 break;
24139
24140 case DW_OP_GNU_uninit:
24141 break;
24142
24143 case DW_OP_addrx:
24144 case DW_OP_GNU_addr_index:
24145 case DW_OP_GNU_const_index:
24146 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24147 &bytes_read);
24148 i += bytes_read;
24149 break;
24150
24151 default:
24152 {
24153 const char *name = get_DW_OP_name (op);
24154
24155 if (name)
24156 complaint (_("unsupported stack op: '%s'"),
24157 name);
24158 else
24159 complaint (_("unsupported stack op: '%02x'"),
24160 op);
24161 }
24162
24163 return (stack[stacki]);
24164 }
24165
24166 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24167 outside of the allocated space. Also enforce minimum>0. */
24168 if (stacki >= ARRAY_SIZE (stack) - 1)
24169 {
24170 complaint (_("location description stack overflow"));
24171 return 0;
24172 }
24173
24174 if (stacki <= 0)
24175 {
24176 complaint (_("location description stack underflow"));
24177 return 0;
24178 }
24179 }
24180 return (stack[stacki]);
24181 }
24182
24183 /* memory allocation interface */
24184
24185 static struct dwarf_block *
24186 dwarf_alloc_block (struct dwarf2_cu *cu)
24187 {
24188 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24189 }
24190
24191 static struct die_info *
24192 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24193 {
24194 struct die_info *die;
24195 size_t size = sizeof (struct die_info);
24196
24197 if (num_attrs > 1)
24198 size += (num_attrs - 1) * sizeof (struct attribute);
24199
24200 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24201 memset (die, 0, sizeof (struct die_info));
24202 return (die);
24203 }
24204
24205 \f
24206 /* Macro support. */
24207
24208 /* Return file name relative to the compilation directory of file number I in
24209 *LH's file name table. The result is allocated using xmalloc; the caller is
24210 responsible for freeing it. */
24211
24212 static char *
24213 file_file_name (int file, struct line_header *lh)
24214 {
24215 /* Is the file number a valid index into the line header's file name
24216 table? Remember that file numbers start with one, not zero. */
24217 if (lh->is_valid_file_index (file))
24218 {
24219 const file_entry *fe = lh->file_name_at (file);
24220
24221 if (!IS_ABSOLUTE_PATH (fe->name))
24222 {
24223 const char *dir = fe->include_dir (lh);
24224 if (dir != NULL)
24225 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
24226 }
24227 return xstrdup (fe->name);
24228 }
24229 else
24230 {
24231 /* The compiler produced a bogus file number. We can at least
24232 record the macro definitions made in the file, even if we
24233 won't be able to find the file by name. */
24234 char fake_name[80];
24235
24236 xsnprintf (fake_name, sizeof (fake_name),
24237 "<bad macro file number %d>", file);
24238
24239 complaint (_("bad file number in macro information (%d)"),
24240 file);
24241
24242 return xstrdup (fake_name);
24243 }
24244 }
24245
24246 /* Return the full name of file number I in *LH's file name table.
24247 Use COMP_DIR as the name of the current directory of the
24248 compilation. The result is allocated using xmalloc; the caller is
24249 responsible for freeing it. */
24250 static char *
24251 file_full_name (int file, struct line_header *lh, const char *comp_dir)
24252 {
24253 /* Is the file number a valid index into the line header's file name
24254 table? Remember that file numbers start with one, not zero. */
24255 if (lh->is_valid_file_index (file))
24256 {
24257 char *relative = file_file_name (file, lh);
24258
24259 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24260 return relative;
24261 return reconcat (relative, comp_dir, SLASH_STRING,
24262 relative, (char *) NULL);
24263 }
24264 else
24265 return file_file_name (file, lh);
24266 }
24267
24268
24269 static struct macro_source_file *
24270 macro_start_file (struct dwarf2_cu *cu,
24271 int file, int line,
24272 struct macro_source_file *current_file,
24273 struct line_header *lh)
24274 {
24275 /* File name relative to the compilation directory of this source file. */
24276 char *file_name = file_file_name (file, lh);
24277
24278 if (! current_file)
24279 {
24280 /* Note: We don't create a macro table for this compilation unit
24281 at all until we actually get a filename. */
24282 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
24283
24284 /* If we have no current file, then this must be the start_file
24285 directive for the compilation unit's main source file. */
24286 current_file = macro_set_main (macro_table, file_name);
24287 macro_define_special (macro_table);
24288 }
24289 else
24290 current_file = macro_include (current_file, line, file_name);
24291
24292 xfree (file_name);
24293
24294 return current_file;
24295 }
24296
24297 static const char *
24298 consume_improper_spaces (const char *p, const char *body)
24299 {
24300 if (*p == ' ')
24301 {
24302 complaint (_("macro definition contains spaces "
24303 "in formal argument list:\n`%s'"),
24304 body);
24305
24306 while (*p == ' ')
24307 p++;
24308 }
24309
24310 return p;
24311 }
24312
24313
24314 static void
24315 parse_macro_definition (struct macro_source_file *file, int line,
24316 const char *body)
24317 {
24318 const char *p;
24319
24320 /* The body string takes one of two forms. For object-like macro
24321 definitions, it should be:
24322
24323 <macro name> " " <definition>
24324
24325 For function-like macro definitions, it should be:
24326
24327 <macro name> "() " <definition>
24328 or
24329 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24330
24331 Spaces may appear only where explicitly indicated, and in the
24332 <definition>.
24333
24334 The Dwarf 2 spec says that an object-like macro's name is always
24335 followed by a space, but versions of GCC around March 2002 omit
24336 the space when the macro's definition is the empty string.
24337
24338 The Dwarf 2 spec says that there should be no spaces between the
24339 formal arguments in a function-like macro's formal argument list,
24340 but versions of GCC around March 2002 include spaces after the
24341 commas. */
24342
24343
24344 /* Find the extent of the macro name. The macro name is terminated
24345 by either a space or null character (for an object-like macro) or
24346 an opening paren (for a function-like macro). */
24347 for (p = body; *p; p++)
24348 if (*p == ' ' || *p == '(')
24349 break;
24350
24351 if (*p == ' ' || *p == '\0')
24352 {
24353 /* It's an object-like macro. */
24354 int name_len = p - body;
24355 char *name = savestring (body, name_len);
24356 const char *replacement;
24357
24358 if (*p == ' ')
24359 replacement = body + name_len + 1;
24360 else
24361 {
24362 dwarf2_macro_malformed_definition_complaint (body);
24363 replacement = body + name_len;
24364 }
24365
24366 macro_define_object (file, line, name, replacement);
24367
24368 xfree (name);
24369 }
24370 else if (*p == '(')
24371 {
24372 /* It's a function-like macro. */
24373 char *name = savestring (body, p - body);
24374 int argc = 0;
24375 int argv_size = 1;
24376 char **argv = XNEWVEC (char *, argv_size);
24377
24378 p++;
24379
24380 p = consume_improper_spaces (p, body);
24381
24382 /* Parse the formal argument list. */
24383 while (*p && *p != ')')
24384 {
24385 /* Find the extent of the current argument name. */
24386 const char *arg_start = p;
24387
24388 while (*p && *p != ',' && *p != ')' && *p != ' ')
24389 p++;
24390
24391 if (! *p || p == arg_start)
24392 dwarf2_macro_malformed_definition_complaint (body);
24393 else
24394 {
24395 /* Make sure argv has room for the new argument. */
24396 if (argc >= argv_size)
24397 {
24398 argv_size *= 2;
24399 argv = XRESIZEVEC (char *, argv, argv_size);
24400 }
24401
24402 argv[argc++] = savestring (arg_start, p - arg_start);
24403 }
24404
24405 p = consume_improper_spaces (p, body);
24406
24407 /* Consume the comma, if present. */
24408 if (*p == ',')
24409 {
24410 p++;
24411
24412 p = consume_improper_spaces (p, body);
24413 }
24414 }
24415
24416 if (*p == ')')
24417 {
24418 p++;
24419
24420 if (*p == ' ')
24421 /* Perfectly formed definition, no complaints. */
24422 macro_define_function (file, line, name,
24423 argc, (const char **) argv,
24424 p + 1);
24425 else if (*p == '\0')
24426 {
24427 /* Complain, but do define it. */
24428 dwarf2_macro_malformed_definition_complaint (body);
24429 macro_define_function (file, line, name,
24430 argc, (const char **) argv,
24431 p);
24432 }
24433 else
24434 /* Just complain. */
24435 dwarf2_macro_malformed_definition_complaint (body);
24436 }
24437 else
24438 /* Just complain. */
24439 dwarf2_macro_malformed_definition_complaint (body);
24440
24441 xfree (name);
24442 {
24443 int i;
24444
24445 for (i = 0; i < argc; i++)
24446 xfree (argv[i]);
24447 }
24448 xfree (argv);
24449 }
24450 else
24451 dwarf2_macro_malformed_definition_complaint (body);
24452 }
24453
24454 /* Skip some bytes from BYTES according to the form given in FORM.
24455 Returns the new pointer. */
24456
24457 static const gdb_byte *
24458 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24459 enum dwarf_form form,
24460 unsigned int offset_size,
24461 struct dwarf2_section_info *section)
24462 {
24463 unsigned int bytes_read;
24464
24465 switch (form)
24466 {
24467 case DW_FORM_data1:
24468 case DW_FORM_flag:
24469 ++bytes;
24470 break;
24471
24472 case DW_FORM_data2:
24473 bytes += 2;
24474 break;
24475
24476 case DW_FORM_data4:
24477 bytes += 4;
24478 break;
24479
24480 case DW_FORM_data8:
24481 bytes += 8;
24482 break;
24483
24484 case DW_FORM_data16:
24485 bytes += 16;
24486 break;
24487
24488 case DW_FORM_string:
24489 read_direct_string (abfd, bytes, &bytes_read);
24490 bytes += bytes_read;
24491 break;
24492
24493 case DW_FORM_sec_offset:
24494 case DW_FORM_strp:
24495 case DW_FORM_GNU_strp_alt:
24496 bytes += offset_size;
24497 break;
24498
24499 case DW_FORM_block:
24500 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24501 bytes += bytes_read;
24502 break;
24503
24504 case DW_FORM_block1:
24505 bytes += 1 + read_1_byte (abfd, bytes);
24506 break;
24507 case DW_FORM_block2:
24508 bytes += 2 + read_2_bytes (abfd, bytes);
24509 break;
24510 case DW_FORM_block4:
24511 bytes += 4 + read_4_bytes (abfd, bytes);
24512 break;
24513
24514 case DW_FORM_addrx:
24515 case DW_FORM_sdata:
24516 case DW_FORM_strx:
24517 case DW_FORM_udata:
24518 case DW_FORM_GNU_addr_index:
24519 case DW_FORM_GNU_str_index:
24520 bytes = gdb_skip_leb128 (bytes, buffer_end);
24521 if (bytes == NULL)
24522 {
24523 dwarf2_section_buffer_overflow_complaint (section);
24524 return NULL;
24525 }
24526 break;
24527
24528 case DW_FORM_implicit_const:
24529 break;
24530
24531 default:
24532 {
24533 complaint (_("invalid form 0x%x in `%s'"),
24534 form, get_section_name (section));
24535 return NULL;
24536 }
24537 }
24538
24539 return bytes;
24540 }
24541
24542 /* A helper for dwarf_decode_macros that handles skipping an unknown
24543 opcode. Returns an updated pointer to the macro data buffer; or,
24544 on error, issues a complaint and returns NULL. */
24545
24546 static const gdb_byte *
24547 skip_unknown_opcode (unsigned int opcode,
24548 const gdb_byte **opcode_definitions,
24549 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24550 bfd *abfd,
24551 unsigned int offset_size,
24552 struct dwarf2_section_info *section)
24553 {
24554 unsigned int bytes_read, i;
24555 unsigned long arg;
24556 const gdb_byte *defn;
24557
24558 if (opcode_definitions[opcode] == NULL)
24559 {
24560 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
24561 opcode);
24562 return NULL;
24563 }
24564
24565 defn = opcode_definitions[opcode];
24566 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24567 defn += bytes_read;
24568
24569 for (i = 0; i < arg; ++i)
24570 {
24571 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24572 (enum dwarf_form) defn[i], offset_size,
24573 section);
24574 if (mac_ptr == NULL)
24575 {
24576 /* skip_form_bytes already issued the complaint. */
24577 return NULL;
24578 }
24579 }
24580
24581 return mac_ptr;
24582 }
24583
24584 /* A helper function which parses the header of a macro section.
24585 If the macro section is the extended (for now called "GNU") type,
24586 then this updates *OFFSET_SIZE. Returns a pointer to just after
24587 the header, or issues a complaint and returns NULL on error. */
24588
24589 static const gdb_byte *
24590 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24591 bfd *abfd,
24592 const gdb_byte *mac_ptr,
24593 unsigned int *offset_size,
24594 int section_is_gnu)
24595 {
24596 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24597
24598 if (section_is_gnu)
24599 {
24600 unsigned int version, flags;
24601
24602 version = read_2_bytes (abfd, mac_ptr);
24603 if (version != 4 && version != 5)
24604 {
24605 complaint (_("unrecognized version `%d' in .debug_macro section"),
24606 version);
24607 return NULL;
24608 }
24609 mac_ptr += 2;
24610
24611 flags = read_1_byte (abfd, mac_ptr);
24612 ++mac_ptr;
24613 *offset_size = (flags & 1) ? 8 : 4;
24614
24615 if ((flags & 2) != 0)
24616 /* We don't need the line table offset. */
24617 mac_ptr += *offset_size;
24618
24619 /* Vendor opcode descriptions. */
24620 if ((flags & 4) != 0)
24621 {
24622 unsigned int i, count;
24623
24624 count = read_1_byte (abfd, mac_ptr);
24625 ++mac_ptr;
24626 for (i = 0; i < count; ++i)
24627 {
24628 unsigned int opcode, bytes_read;
24629 unsigned long arg;
24630
24631 opcode = read_1_byte (abfd, mac_ptr);
24632 ++mac_ptr;
24633 opcode_definitions[opcode] = mac_ptr;
24634 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24635 mac_ptr += bytes_read;
24636 mac_ptr += arg;
24637 }
24638 }
24639 }
24640
24641 return mac_ptr;
24642 }
24643
24644 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24645 including DW_MACRO_import. */
24646
24647 static void
24648 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
24649 bfd *abfd,
24650 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24651 struct macro_source_file *current_file,
24652 struct line_header *lh,
24653 struct dwarf2_section_info *section,
24654 int section_is_gnu, int section_is_dwz,
24655 unsigned int offset_size,
24656 htab_t include_hash)
24657 {
24658 struct dwarf2_per_objfile *dwarf2_per_objfile
24659 = cu->per_cu->dwarf2_per_objfile;
24660 struct objfile *objfile = dwarf2_per_objfile->objfile;
24661 enum dwarf_macro_record_type macinfo_type;
24662 int at_commandline;
24663 const gdb_byte *opcode_definitions[256];
24664
24665 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24666 &offset_size, section_is_gnu);
24667 if (mac_ptr == NULL)
24668 {
24669 /* We already issued a complaint. */
24670 return;
24671 }
24672
24673 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24674 GDB is still reading the definitions from command line. First
24675 DW_MACINFO_start_file will need to be ignored as it was already executed
24676 to create CURRENT_FILE for the main source holding also the command line
24677 definitions. On first met DW_MACINFO_start_file this flag is reset to
24678 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24679
24680 at_commandline = 1;
24681
24682 do
24683 {
24684 /* Do we at least have room for a macinfo type byte? */
24685 if (mac_ptr >= mac_end)
24686 {
24687 dwarf2_section_buffer_overflow_complaint (section);
24688 break;
24689 }
24690
24691 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24692 mac_ptr++;
24693
24694 /* Note that we rely on the fact that the corresponding GNU and
24695 DWARF constants are the same. */
24696 DIAGNOSTIC_PUSH
24697 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24698 switch (macinfo_type)
24699 {
24700 /* A zero macinfo type indicates the end of the macro
24701 information. */
24702 case 0:
24703 break;
24704
24705 case DW_MACRO_define:
24706 case DW_MACRO_undef:
24707 case DW_MACRO_define_strp:
24708 case DW_MACRO_undef_strp:
24709 case DW_MACRO_define_sup:
24710 case DW_MACRO_undef_sup:
24711 {
24712 unsigned int bytes_read;
24713 int line;
24714 const char *body;
24715 int is_define;
24716
24717 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24718 mac_ptr += bytes_read;
24719
24720 if (macinfo_type == DW_MACRO_define
24721 || macinfo_type == DW_MACRO_undef)
24722 {
24723 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24724 mac_ptr += bytes_read;
24725 }
24726 else
24727 {
24728 LONGEST str_offset;
24729
24730 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24731 mac_ptr += offset_size;
24732
24733 if (macinfo_type == DW_MACRO_define_sup
24734 || macinfo_type == DW_MACRO_undef_sup
24735 || section_is_dwz)
24736 {
24737 struct dwz_file *dwz
24738 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24739
24740 body = read_indirect_string_from_dwz (objfile,
24741 dwz, str_offset);
24742 }
24743 else
24744 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24745 abfd, str_offset);
24746 }
24747
24748 is_define = (macinfo_type == DW_MACRO_define
24749 || macinfo_type == DW_MACRO_define_strp
24750 || macinfo_type == DW_MACRO_define_sup);
24751 if (! current_file)
24752 {
24753 /* DWARF violation as no main source is present. */
24754 complaint (_("debug info with no main source gives macro %s "
24755 "on line %d: %s"),
24756 is_define ? _("definition") : _("undefinition"),
24757 line, body);
24758 break;
24759 }
24760 if ((line == 0 && !at_commandline)
24761 || (line != 0 && at_commandline))
24762 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
24763 at_commandline ? _("command-line") : _("in-file"),
24764 is_define ? _("definition") : _("undefinition"),
24765 line == 0 ? _("zero") : _("non-zero"), line, body);
24766
24767 if (body == NULL)
24768 {
24769 /* Fedora's rpm-build's "debugedit" binary
24770 corrupted .debug_macro sections.
24771
24772 For more info, see
24773 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24774 complaint (_("debug info gives %s invalid macro %s "
24775 "without body (corrupted?) at line %d "
24776 "on file %s"),
24777 at_commandline ? _("command-line") : _("in-file"),
24778 is_define ? _("definition") : _("undefinition"),
24779 line, current_file->filename);
24780 }
24781 else if (is_define)
24782 parse_macro_definition (current_file, line, body);
24783 else
24784 {
24785 gdb_assert (macinfo_type == DW_MACRO_undef
24786 || macinfo_type == DW_MACRO_undef_strp
24787 || macinfo_type == DW_MACRO_undef_sup);
24788 macro_undef (current_file, line, body);
24789 }
24790 }
24791 break;
24792
24793 case DW_MACRO_start_file:
24794 {
24795 unsigned int bytes_read;
24796 int line, file;
24797
24798 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24799 mac_ptr += bytes_read;
24800 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24801 mac_ptr += bytes_read;
24802
24803 if ((line == 0 && !at_commandline)
24804 || (line != 0 && at_commandline))
24805 complaint (_("debug info gives source %d included "
24806 "from %s at %s line %d"),
24807 file, at_commandline ? _("command-line") : _("file"),
24808 line == 0 ? _("zero") : _("non-zero"), line);
24809
24810 if (at_commandline)
24811 {
24812 /* This DW_MACRO_start_file was executed in the
24813 pass one. */
24814 at_commandline = 0;
24815 }
24816 else
24817 current_file = macro_start_file (cu, file, line, current_file,
24818 lh);
24819 }
24820 break;
24821
24822 case DW_MACRO_end_file:
24823 if (! current_file)
24824 complaint (_("macro debug info has an unmatched "
24825 "`close_file' directive"));
24826 else
24827 {
24828 current_file = current_file->included_by;
24829 if (! current_file)
24830 {
24831 enum dwarf_macro_record_type next_type;
24832
24833 /* GCC circa March 2002 doesn't produce the zero
24834 type byte marking the end of the compilation
24835 unit. Complain if it's not there, but exit no
24836 matter what. */
24837
24838 /* Do we at least have room for a macinfo type byte? */
24839 if (mac_ptr >= mac_end)
24840 {
24841 dwarf2_section_buffer_overflow_complaint (section);
24842 return;
24843 }
24844
24845 /* We don't increment mac_ptr here, so this is just
24846 a look-ahead. */
24847 next_type
24848 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24849 mac_ptr);
24850 if (next_type != 0)
24851 complaint (_("no terminating 0-type entry for "
24852 "macros in `.debug_macinfo' section"));
24853
24854 return;
24855 }
24856 }
24857 break;
24858
24859 case DW_MACRO_import:
24860 case DW_MACRO_import_sup:
24861 {
24862 LONGEST offset;
24863 void **slot;
24864 bfd *include_bfd = abfd;
24865 struct dwarf2_section_info *include_section = section;
24866 const gdb_byte *include_mac_end = mac_end;
24867 int is_dwz = section_is_dwz;
24868 const gdb_byte *new_mac_ptr;
24869
24870 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24871 mac_ptr += offset_size;
24872
24873 if (macinfo_type == DW_MACRO_import_sup)
24874 {
24875 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24876
24877 dwarf2_read_section (objfile, &dwz->macro);
24878
24879 include_section = &dwz->macro;
24880 include_bfd = get_section_bfd_owner (include_section);
24881 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24882 is_dwz = 1;
24883 }
24884
24885 new_mac_ptr = include_section->buffer + offset;
24886 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24887
24888 if (*slot != NULL)
24889 {
24890 /* This has actually happened; see
24891 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24892 complaint (_("recursive DW_MACRO_import in "
24893 ".debug_macro section"));
24894 }
24895 else
24896 {
24897 *slot = (void *) new_mac_ptr;
24898
24899 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
24900 include_mac_end, current_file, lh,
24901 section, section_is_gnu, is_dwz,
24902 offset_size, include_hash);
24903
24904 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24905 }
24906 }
24907 break;
24908
24909 case DW_MACINFO_vendor_ext:
24910 if (!section_is_gnu)
24911 {
24912 unsigned int bytes_read;
24913
24914 /* This reads the constant, but since we don't recognize
24915 any vendor extensions, we ignore it. */
24916 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24917 mac_ptr += bytes_read;
24918 read_direct_string (abfd, mac_ptr, &bytes_read);
24919 mac_ptr += bytes_read;
24920
24921 /* We don't recognize any vendor extensions. */
24922 break;
24923 }
24924 /* FALLTHROUGH */
24925
24926 default:
24927 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24928 mac_ptr, mac_end, abfd, offset_size,
24929 section);
24930 if (mac_ptr == NULL)
24931 return;
24932 break;
24933 }
24934 DIAGNOSTIC_POP
24935 } while (macinfo_type != 0);
24936 }
24937
24938 static void
24939 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24940 int section_is_gnu)
24941 {
24942 struct dwarf2_per_objfile *dwarf2_per_objfile
24943 = cu->per_cu->dwarf2_per_objfile;
24944 struct objfile *objfile = dwarf2_per_objfile->objfile;
24945 struct line_header *lh = cu->line_header;
24946 bfd *abfd;
24947 const gdb_byte *mac_ptr, *mac_end;
24948 struct macro_source_file *current_file = 0;
24949 enum dwarf_macro_record_type macinfo_type;
24950 unsigned int offset_size = cu->header.offset_size;
24951 const gdb_byte *opcode_definitions[256];
24952 void **slot;
24953 struct dwarf2_section_info *section;
24954 const char *section_name;
24955
24956 if (cu->dwo_unit != NULL)
24957 {
24958 if (section_is_gnu)
24959 {
24960 section = &cu->dwo_unit->dwo_file->sections.macro;
24961 section_name = ".debug_macro.dwo";
24962 }
24963 else
24964 {
24965 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24966 section_name = ".debug_macinfo.dwo";
24967 }
24968 }
24969 else
24970 {
24971 if (section_is_gnu)
24972 {
24973 section = &dwarf2_per_objfile->macro;
24974 section_name = ".debug_macro";
24975 }
24976 else
24977 {
24978 section = &dwarf2_per_objfile->macinfo;
24979 section_name = ".debug_macinfo";
24980 }
24981 }
24982
24983 dwarf2_read_section (objfile, section);
24984 if (section->buffer == NULL)
24985 {
24986 complaint (_("missing %s section"), section_name);
24987 return;
24988 }
24989 abfd = get_section_bfd_owner (section);
24990
24991 /* First pass: Find the name of the base filename.
24992 This filename is needed in order to process all macros whose definition
24993 (or undefinition) comes from the command line. These macros are defined
24994 before the first DW_MACINFO_start_file entry, and yet still need to be
24995 associated to the base file.
24996
24997 To determine the base file name, we scan the macro definitions until we
24998 reach the first DW_MACINFO_start_file entry. We then initialize
24999 CURRENT_FILE accordingly so that any macro definition found before the
25000 first DW_MACINFO_start_file can still be associated to the base file. */
25001
25002 mac_ptr = section->buffer + offset;
25003 mac_end = section->buffer + section->size;
25004
25005 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
25006 &offset_size, section_is_gnu);
25007 if (mac_ptr == NULL)
25008 {
25009 /* We already issued a complaint. */
25010 return;
25011 }
25012
25013 do
25014 {
25015 /* Do we at least have room for a macinfo type byte? */
25016 if (mac_ptr >= mac_end)
25017 {
25018 /* Complaint is printed during the second pass as GDB will probably
25019 stop the first pass earlier upon finding
25020 DW_MACINFO_start_file. */
25021 break;
25022 }
25023
25024 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
25025 mac_ptr++;
25026
25027 /* Note that we rely on the fact that the corresponding GNU and
25028 DWARF constants are the same. */
25029 DIAGNOSTIC_PUSH
25030 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
25031 switch (macinfo_type)
25032 {
25033 /* A zero macinfo type indicates the end of the macro
25034 information. */
25035 case 0:
25036 break;
25037
25038 case DW_MACRO_define:
25039 case DW_MACRO_undef:
25040 /* Only skip the data by MAC_PTR. */
25041 {
25042 unsigned int bytes_read;
25043
25044 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25045 mac_ptr += bytes_read;
25046 read_direct_string (abfd, mac_ptr, &bytes_read);
25047 mac_ptr += bytes_read;
25048 }
25049 break;
25050
25051 case DW_MACRO_start_file:
25052 {
25053 unsigned int bytes_read;
25054 int line, file;
25055
25056 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25057 mac_ptr += bytes_read;
25058 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25059 mac_ptr += bytes_read;
25060
25061 current_file = macro_start_file (cu, file, line, current_file, lh);
25062 }
25063 break;
25064
25065 case DW_MACRO_end_file:
25066 /* No data to skip by MAC_PTR. */
25067 break;
25068
25069 case DW_MACRO_define_strp:
25070 case DW_MACRO_undef_strp:
25071 case DW_MACRO_define_sup:
25072 case DW_MACRO_undef_sup:
25073 {
25074 unsigned int bytes_read;
25075
25076 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25077 mac_ptr += bytes_read;
25078 mac_ptr += offset_size;
25079 }
25080 break;
25081
25082 case DW_MACRO_import:
25083 case DW_MACRO_import_sup:
25084 /* Note that, according to the spec, a transparent include
25085 chain cannot call DW_MACRO_start_file. So, we can just
25086 skip this opcode. */
25087 mac_ptr += offset_size;
25088 break;
25089
25090 case DW_MACINFO_vendor_ext:
25091 /* Only skip the data by MAC_PTR. */
25092 if (!section_is_gnu)
25093 {
25094 unsigned int bytes_read;
25095
25096 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25097 mac_ptr += bytes_read;
25098 read_direct_string (abfd, mac_ptr, &bytes_read);
25099 mac_ptr += bytes_read;
25100 }
25101 /* FALLTHROUGH */
25102
25103 default:
25104 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25105 mac_ptr, mac_end, abfd, offset_size,
25106 section);
25107 if (mac_ptr == NULL)
25108 return;
25109 break;
25110 }
25111 DIAGNOSTIC_POP
25112 } while (macinfo_type != 0 && current_file == NULL);
25113
25114 /* Second pass: Process all entries.
25115
25116 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25117 command-line macro definitions/undefinitions. This flag is unset when we
25118 reach the first DW_MACINFO_start_file entry. */
25119
25120 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25121 htab_eq_pointer,
25122 NULL, xcalloc, xfree));
25123 mac_ptr = section->buffer + offset;
25124 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
25125 *slot = (void *) mac_ptr;
25126 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
25127 current_file, lh, section,
25128 section_is_gnu, 0, offset_size,
25129 include_hash.get ());
25130 }
25131
25132 /* Check if the attribute's form is a DW_FORM_block*
25133 if so return true else false. */
25134
25135 static int
25136 attr_form_is_block (const struct attribute *attr)
25137 {
25138 return (attr == NULL ? 0 :
25139 attr->form == DW_FORM_block1
25140 || attr->form == DW_FORM_block2
25141 || attr->form == DW_FORM_block4
25142 || attr->form == DW_FORM_block
25143 || attr->form == DW_FORM_exprloc);
25144 }
25145
25146 /* Return non-zero if ATTR's value is a section offset --- classes
25147 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25148 You may use DW_UNSND (attr) to retrieve such offsets.
25149
25150 Section 7.5.4, "Attribute Encodings", explains that no attribute
25151 may have a value that belongs to more than one of these classes; it
25152 would be ambiguous if we did, because we use the same forms for all
25153 of them. */
25154
25155 static int
25156 attr_form_is_section_offset (const struct attribute *attr)
25157 {
25158 return (attr->form == DW_FORM_data4
25159 || attr->form == DW_FORM_data8
25160 || attr->form == DW_FORM_sec_offset);
25161 }
25162
25163 /* Return non-zero if ATTR's value falls in the 'constant' class, or
25164 zero otherwise. When this function returns true, you can apply
25165 dwarf2_get_attr_constant_value to it.
25166
25167 However, note that for some attributes you must check
25168 attr_form_is_section_offset before using this test. DW_FORM_data4
25169 and DW_FORM_data8 are members of both the constant class, and of
25170 the classes that contain offsets into other debug sections
25171 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25172 that, if an attribute's can be either a constant or one of the
25173 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
25174 taken as section offsets, not constants.
25175
25176 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25177 cannot handle that. */
25178
25179 static int
25180 attr_form_is_constant (const struct attribute *attr)
25181 {
25182 switch (attr->form)
25183 {
25184 case DW_FORM_sdata:
25185 case DW_FORM_udata:
25186 case DW_FORM_data1:
25187 case DW_FORM_data2:
25188 case DW_FORM_data4:
25189 case DW_FORM_data8:
25190 case DW_FORM_implicit_const:
25191 return 1;
25192 default:
25193 return 0;
25194 }
25195 }
25196
25197
25198 /* DW_ADDR is always stored already as sect_offset; despite for the forms
25199 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25200
25201 static int
25202 attr_form_is_ref (const struct attribute *attr)
25203 {
25204 switch (attr->form)
25205 {
25206 case DW_FORM_ref_addr:
25207 case DW_FORM_ref1:
25208 case DW_FORM_ref2:
25209 case DW_FORM_ref4:
25210 case DW_FORM_ref8:
25211 case DW_FORM_ref_udata:
25212 case DW_FORM_GNU_ref_alt:
25213 return 1;
25214 default:
25215 return 0;
25216 }
25217 }
25218
25219 /* Return the .debug_loc section to use for CU.
25220 For DWO files use .debug_loc.dwo. */
25221
25222 static struct dwarf2_section_info *
25223 cu_debug_loc_section (struct dwarf2_cu *cu)
25224 {
25225 struct dwarf2_per_objfile *dwarf2_per_objfile
25226 = cu->per_cu->dwarf2_per_objfile;
25227
25228 if (cu->dwo_unit)
25229 {
25230 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25231
25232 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25233 }
25234 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25235 : &dwarf2_per_objfile->loc);
25236 }
25237
25238 /* A helper function that fills in a dwarf2_loclist_baton. */
25239
25240 static void
25241 fill_in_loclist_baton (struct dwarf2_cu *cu,
25242 struct dwarf2_loclist_baton *baton,
25243 const struct attribute *attr)
25244 {
25245 struct dwarf2_per_objfile *dwarf2_per_objfile
25246 = cu->per_cu->dwarf2_per_objfile;
25247 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25248
25249 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
25250
25251 baton->per_cu = cu->per_cu;
25252 gdb_assert (baton->per_cu);
25253 /* We don't know how long the location list is, but make sure we
25254 don't run off the edge of the section. */
25255 baton->size = section->size - DW_UNSND (attr);
25256 baton->data = section->buffer + DW_UNSND (attr);
25257 baton->base_address = cu->base_address;
25258 baton->from_dwo = cu->dwo_unit != NULL;
25259 }
25260
25261 static void
25262 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
25263 struct dwarf2_cu *cu, int is_block)
25264 {
25265 struct dwarf2_per_objfile *dwarf2_per_objfile
25266 = cu->per_cu->dwarf2_per_objfile;
25267 struct objfile *objfile = dwarf2_per_objfile->objfile;
25268 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25269
25270 if (attr_form_is_section_offset (attr)
25271 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
25272 the section. If so, fall through to the complaint in the
25273 other branch. */
25274 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
25275 {
25276 struct dwarf2_loclist_baton *baton;
25277
25278 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
25279
25280 fill_in_loclist_baton (cu, baton, attr);
25281
25282 if (cu->base_known == 0)
25283 complaint (_("Location list used without "
25284 "specifying the CU base address."));
25285
25286 SYMBOL_ACLASS_INDEX (sym) = (is_block
25287 ? dwarf2_loclist_block_index
25288 : dwarf2_loclist_index);
25289 SYMBOL_LOCATION_BATON (sym) = baton;
25290 }
25291 else
25292 {
25293 struct dwarf2_locexpr_baton *baton;
25294
25295 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
25296 baton->per_cu = cu->per_cu;
25297 gdb_assert (baton->per_cu);
25298
25299 if (attr_form_is_block (attr))
25300 {
25301 /* Note that we're just copying the block's data pointer
25302 here, not the actual data. We're still pointing into the
25303 info_buffer for SYM's objfile; right now we never release
25304 that buffer, but when we do clean up properly this may
25305 need to change. */
25306 baton->size = DW_BLOCK (attr)->size;
25307 baton->data = DW_BLOCK (attr)->data;
25308 }
25309 else
25310 {
25311 dwarf2_invalid_attrib_class_complaint ("location description",
25312 SYMBOL_NATURAL_NAME (sym));
25313 baton->size = 0;
25314 }
25315
25316 SYMBOL_ACLASS_INDEX (sym) = (is_block
25317 ? dwarf2_locexpr_block_index
25318 : dwarf2_locexpr_index);
25319 SYMBOL_LOCATION_BATON (sym) = baton;
25320 }
25321 }
25322
25323 /* Return the OBJFILE associated with the compilation unit CU. If CU
25324 came from a separate debuginfo file, then the master objfile is
25325 returned. */
25326
25327 struct objfile *
25328 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25329 {
25330 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25331
25332 /* Return the master objfile, so that we can report and look up the
25333 correct file containing this variable. */
25334 if (objfile->separate_debug_objfile_backlink)
25335 objfile = objfile->separate_debug_objfile_backlink;
25336
25337 return objfile;
25338 }
25339
25340 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25341 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25342 CU_HEADERP first. */
25343
25344 static const struct comp_unit_head *
25345 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25346 struct dwarf2_per_cu_data *per_cu)
25347 {
25348 const gdb_byte *info_ptr;
25349
25350 if (per_cu->cu)
25351 return &per_cu->cu->header;
25352
25353 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25354
25355 memset (cu_headerp, 0, sizeof (*cu_headerp));
25356 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25357 rcuh_kind::COMPILE);
25358
25359 return cu_headerp;
25360 }
25361
25362 /* Return the address size given in the compilation unit header for CU. */
25363
25364 int
25365 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25366 {
25367 struct comp_unit_head cu_header_local;
25368 const struct comp_unit_head *cu_headerp;
25369
25370 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25371
25372 return cu_headerp->addr_size;
25373 }
25374
25375 /* Return the offset size given in the compilation unit header for CU. */
25376
25377 int
25378 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25379 {
25380 struct comp_unit_head cu_header_local;
25381 const struct comp_unit_head *cu_headerp;
25382
25383 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25384
25385 return cu_headerp->offset_size;
25386 }
25387
25388 /* See its dwarf2loc.h declaration. */
25389
25390 int
25391 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25392 {
25393 struct comp_unit_head cu_header_local;
25394 const struct comp_unit_head *cu_headerp;
25395
25396 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25397
25398 if (cu_headerp->version == 2)
25399 return cu_headerp->addr_size;
25400 else
25401 return cu_headerp->offset_size;
25402 }
25403
25404 /* Return the text offset of the CU. The returned offset comes from
25405 this CU's objfile. If this objfile came from a separate debuginfo
25406 file, then the offset may be different from the corresponding
25407 offset in the parent objfile. */
25408
25409 CORE_ADDR
25410 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25411 {
25412 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25413
25414 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25415 }
25416
25417 /* Return a type that is a generic pointer type, the size of which matches
25418 the address size given in the compilation unit header for PER_CU. */
25419 static struct type *
25420 dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25421 {
25422 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25423 struct type *void_type = objfile_type (objfile)->builtin_void;
25424 struct type *addr_type = lookup_pointer_type (void_type);
25425 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25426
25427 if (TYPE_LENGTH (addr_type) == addr_size)
25428 return addr_type;
25429
25430 addr_type
25431 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25432 return addr_type;
25433 }
25434
25435 /* Return DWARF version number of PER_CU. */
25436
25437 short
25438 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25439 {
25440 return per_cu->dwarf_version;
25441 }
25442
25443 /* Locate the .debug_info compilation unit from CU's objfile which contains
25444 the DIE at OFFSET. Raises an error on failure. */
25445
25446 static struct dwarf2_per_cu_data *
25447 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25448 unsigned int offset_in_dwz,
25449 struct dwarf2_per_objfile *dwarf2_per_objfile)
25450 {
25451 struct dwarf2_per_cu_data *this_cu;
25452 int low, high;
25453
25454 low = 0;
25455 high = dwarf2_per_objfile->all_comp_units.size () - 1;
25456 while (high > low)
25457 {
25458 struct dwarf2_per_cu_data *mid_cu;
25459 int mid = low + (high - low) / 2;
25460
25461 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25462 if (mid_cu->is_dwz > offset_in_dwz
25463 || (mid_cu->is_dwz == offset_in_dwz
25464 && mid_cu->sect_off + mid_cu->length >= sect_off))
25465 high = mid;
25466 else
25467 low = mid + 1;
25468 }
25469 gdb_assert (low == high);
25470 this_cu = dwarf2_per_objfile->all_comp_units[low];
25471 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
25472 {
25473 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25474 error (_("Dwarf Error: could not find partial DIE containing "
25475 "offset %s [in module %s]"),
25476 sect_offset_str (sect_off),
25477 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25478
25479 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25480 <= sect_off);
25481 return dwarf2_per_objfile->all_comp_units[low-1];
25482 }
25483 else
25484 {
25485 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
25486 && sect_off >= this_cu->sect_off + this_cu->length)
25487 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25488 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25489 return this_cu;
25490 }
25491 }
25492
25493 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25494
25495 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25496 : per_cu (per_cu_),
25497 mark (false),
25498 has_loclist (false),
25499 checked_producer (false),
25500 producer_is_gxx_lt_4_6 (false),
25501 producer_is_gcc_lt_4_3 (false),
25502 producer_is_icc (false),
25503 producer_is_icc_lt_14 (false),
25504 producer_is_codewarrior (false),
25505 processing_has_namespace_info (false)
25506 {
25507 per_cu->cu = this;
25508 }
25509
25510 /* Destroy a dwarf2_cu. */
25511
25512 dwarf2_cu::~dwarf2_cu ()
25513 {
25514 per_cu->cu = NULL;
25515 }
25516
25517 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25518
25519 static void
25520 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25521 enum language pretend_language)
25522 {
25523 struct attribute *attr;
25524
25525 /* Set the language we're debugging. */
25526 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25527 if (attr)
25528 set_cu_language (DW_UNSND (attr), cu);
25529 else
25530 {
25531 cu->language = pretend_language;
25532 cu->language_defn = language_def (cu->language);
25533 }
25534
25535 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25536 }
25537
25538 /* Increase the age counter on each cached compilation unit, and free
25539 any that are too old. */
25540
25541 static void
25542 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25543 {
25544 struct dwarf2_per_cu_data *per_cu, **last_chain;
25545
25546 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25547 per_cu = dwarf2_per_objfile->read_in_chain;
25548 while (per_cu != NULL)
25549 {
25550 per_cu->cu->last_used ++;
25551 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25552 dwarf2_mark (per_cu->cu);
25553 per_cu = per_cu->cu->read_in_chain;
25554 }
25555
25556 per_cu = dwarf2_per_objfile->read_in_chain;
25557 last_chain = &dwarf2_per_objfile->read_in_chain;
25558 while (per_cu != NULL)
25559 {
25560 struct dwarf2_per_cu_data *next_cu;
25561
25562 next_cu = per_cu->cu->read_in_chain;
25563
25564 if (!per_cu->cu->mark)
25565 {
25566 delete per_cu->cu;
25567 *last_chain = next_cu;
25568 }
25569 else
25570 last_chain = &per_cu->cu->read_in_chain;
25571
25572 per_cu = next_cu;
25573 }
25574 }
25575
25576 /* Remove a single compilation unit from the cache. */
25577
25578 static void
25579 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25580 {
25581 struct dwarf2_per_cu_data *per_cu, **last_chain;
25582 struct dwarf2_per_objfile *dwarf2_per_objfile
25583 = target_per_cu->dwarf2_per_objfile;
25584
25585 per_cu = dwarf2_per_objfile->read_in_chain;
25586 last_chain = &dwarf2_per_objfile->read_in_chain;
25587 while (per_cu != NULL)
25588 {
25589 struct dwarf2_per_cu_data *next_cu;
25590
25591 next_cu = per_cu->cu->read_in_chain;
25592
25593 if (per_cu == target_per_cu)
25594 {
25595 delete per_cu->cu;
25596 per_cu->cu = NULL;
25597 *last_chain = next_cu;
25598 break;
25599 }
25600 else
25601 last_chain = &per_cu->cu->read_in_chain;
25602
25603 per_cu = next_cu;
25604 }
25605 }
25606
25607 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25608 We store these in a hash table separate from the DIEs, and preserve them
25609 when the DIEs are flushed out of cache.
25610
25611 The CU "per_cu" pointer is needed because offset alone is not enough to
25612 uniquely identify the type. A file may have multiple .debug_types sections,
25613 or the type may come from a DWO file. Furthermore, while it's more logical
25614 to use per_cu->section+offset, with Fission the section with the data is in
25615 the DWO file but we don't know that section at the point we need it.
25616 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25617 because we can enter the lookup routine, get_die_type_at_offset, from
25618 outside this file, and thus won't necessarily have PER_CU->cu.
25619 Fortunately, PER_CU is stable for the life of the objfile. */
25620
25621 struct dwarf2_per_cu_offset_and_type
25622 {
25623 const struct dwarf2_per_cu_data *per_cu;
25624 sect_offset sect_off;
25625 struct type *type;
25626 };
25627
25628 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25629
25630 static hashval_t
25631 per_cu_offset_and_type_hash (const void *item)
25632 {
25633 const struct dwarf2_per_cu_offset_and_type *ofs
25634 = (const struct dwarf2_per_cu_offset_and_type *) item;
25635
25636 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25637 }
25638
25639 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25640
25641 static int
25642 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25643 {
25644 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25645 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25646 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25647 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25648
25649 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25650 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25651 }
25652
25653 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25654 table if necessary. For convenience, return TYPE.
25655
25656 The DIEs reading must have careful ordering to:
25657 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25658 reading current DIE.
25659 * Not trying to dereference contents of still incompletely read in types
25660 while reading in other DIEs.
25661 * Enable referencing still incompletely read in types just by a pointer to
25662 the type without accessing its fields.
25663
25664 Therefore caller should follow these rules:
25665 * Try to fetch any prerequisite types we may need to build this DIE type
25666 before building the type and calling set_die_type.
25667 * After building type call set_die_type for current DIE as soon as
25668 possible before fetching more types to complete the current type.
25669 * Make the type as complete as possible before fetching more types. */
25670
25671 static struct type *
25672 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25673 {
25674 struct dwarf2_per_objfile *dwarf2_per_objfile
25675 = cu->per_cu->dwarf2_per_objfile;
25676 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25677 struct objfile *objfile = dwarf2_per_objfile->objfile;
25678 struct attribute *attr;
25679 struct dynamic_prop prop;
25680
25681 /* For Ada types, make sure that the gnat-specific data is always
25682 initialized (if not already set). There are a few types where
25683 we should not be doing so, because the type-specific area is
25684 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25685 where the type-specific area is used to store the floatformat).
25686 But this is not a problem, because the gnat-specific information
25687 is actually not needed for these types. */
25688 if (need_gnat_info (cu)
25689 && TYPE_CODE (type) != TYPE_CODE_FUNC
25690 && TYPE_CODE (type) != TYPE_CODE_FLT
25691 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25692 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25693 && TYPE_CODE (type) != TYPE_CODE_METHOD
25694 && !HAVE_GNAT_AUX_INFO (type))
25695 INIT_GNAT_SPECIFIC (type);
25696
25697 /* Read DW_AT_allocated and set in type. */
25698 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25699 if (attr_form_is_block (attr))
25700 {
25701 struct type *prop_type
25702 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25703 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25704 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25705 }
25706 else if (attr != NULL)
25707 {
25708 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25709 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25710 sect_offset_str (die->sect_off));
25711 }
25712
25713 /* Read DW_AT_associated and set in type. */
25714 attr = dwarf2_attr (die, DW_AT_associated, cu);
25715 if (attr_form_is_block (attr))
25716 {
25717 struct type *prop_type
25718 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25719 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25720 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25721 }
25722 else if (attr != NULL)
25723 {
25724 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
25725 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25726 sect_offset_str (die->sect_off));
25727 }
25728
25729 /* Read DW_AT_data_location and set in type. */
25730 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25731 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25732 dwarf2_per_cu_addr_type (cu->per_cu)))
25733 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25734
25735 if (dwarf2_per_objfile->die_type_hash == NULL)
25736 {
25737 dwarf2_per_objfile->die_type_hash =
25738 htab_create_alloc_ex (127,
25739 per_cu_offset_and_type_hash,
25740 per_cu_offset_and_type_eq,
25741 NULL,
25742 &objfile->objfile_obstack,
25743 hashtab_obstack_allocate,
25744 dummy_obstack_deallocate);
25745 }
25746
25747 ofs.per_cu = cu->per_cu;
25748 ofs.sect_off = die->sect_off;
25749 ofs.type = type;
25750 slot = (struct dwarf2_per_cu_offset_and_type **)
25751 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25752 if (*slot)
25753 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25754 sect_offset_str (die->sect_off));
25755 *slot = XOBNEW (&objfile->objfile_obstack,
25756 struct dwarf2_per_cu_offset_and_type);
25757 **slot = ofs;
25758 return type;
25759 }
25760
25761 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25762 or return NULL if the die does not have a saved type. */
25763
25764 static struct type *
25765 get_die_type_at_offset (sect_offset sect_off,
25766 struct dwarf2_per_cu_data *per_cu)
25767 {
25768 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25769 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25770
25771 if (dwarf2_per_objfile->die_type_hash == NULL)
25772 return NULL;
25773
25774 ofs.per_cu = per_cu;
25775 ofs.sect_off = sect_off;
25776 slot = ((struct dwarf2_per_cu_offset_and_type *)
25777 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25778 if (slot)
25779 return slot->type;
25780 else
25781 return NULL;
25782 }
25783
25784 /* Look up the type for DIE in CU in die_type_hash,
25785 or return NULL if DIE does not have a saved type. */
25786
25787 static struct type *
25788 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25789 {
25790 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25791 }
25792
25793 /* Add a dependence relationship from CU to REF_PER_CU. */
25794
25795 static void
25796 dwarf2_add_dependence (struct dwarf2_cu *cu,
25797 struct dwarf2_per_cu_data *ref_per_cu)
25798 {
25799 void **slot;
25800
25801 if (cu->dependencies == NULL)
25802 cu->dependencies
25803 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25804 NULL, &cu->comp_unit_obstack,
25805 hashtab_obstack_allocate,
25806 dummy_obstack_deallocate);
25807
25808 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25809 if (*slot == NULL)
25810 *slot = ref_per_cu;
25811 }
25812
25813 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25814 Set the mark field in every compilation unit in the
25815 cache that we must keep because we are keeping CU. */
25816
25817 static int
25818 dwarf2_mark_helper (void **slot, void *data)
25819 {
25820 struct dwarf2_per_cu_data *per_cu;
25821
25822 per_cu = (struct dwarf2_per_cu_data *) *slot;
25823
25824 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25825 reading of the chain. As such dependencies remain valid it is not much
25826 useful to track and undo them during QUIT cleanups. */
25827 if (per_cu->cu == NULL)
25828 return 1;
25829
25830 if (per_cu->cu->mark)
25831 return 1;
25832 per_cu->cu->mark = true;
25833
25834 if (per_cu->cu->dependencies != NULL)
25835 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25836
25837 return 1;
25838 }
25839
25840 /* Set the mark field in CU and in every other compilation unit in the
25841 cache that we must keep because we are keeping CU. */
25842
25843 static void
25844 dwarf2_mark (struct dwarf2_cu *cu)
25845 {
25846 if (cu->mark)
25847 return;
25848 cu->mark = true;
25849 if (cu->dependencies != NULL)
25850 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25851 }
25852
25853 static void
25854 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25855 {
25856 while (per_cu)
25857 {
25858 per_cu->cu->mark = false;
25859 per_cu = per_cu->cu->read_in_chain;
25860 }
25861 }
25862
25863 /* Trivial hash function for partial_die_info: the hash value of a DIE
25864 is its offset in .debug_info for this objfile. */
25865
25866 static hashval_t
25867 partial_die_hash (const void *item)
25868 {
25869 const struct partial_die_info *part_die
25870 = (const struct partial_die_info *) item;
25871
25872 return to_underlying (part_die->sect_off);
25873 }
25874
25875 /* Trivial comparison function for partial_die_info structures: two DIEs
25876 are equal if they have the same offset. */
25877
25878 static int
25879 partial_die_eq (const void *item_lhs, const void *item_rhs)
25880 {
25881 const struct partial_die_info *part_die_lhs
25882 = (const struct partial_die_info *) item_lhs;
25883 const struct partial_die_info *part_die_rhs
25884 = (const struct partial_die_info *) item_rhs;
25885
25886 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25887 }
25888
25889 struct cmd_list_element *set_dwarf_cmdlist;
25890 struct cmd_list_element *show_dwarf_cmdlist;
25891
25892 static void
25893 set_dwarf_cmd (const char *args, int from_tty)
25894 {
25895 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25896 gdb_stdout);
25897 }
25898
25899 static void
25900 show_dwarf_cmd (const char *args, int from_tty)
25901 {
25902 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25903 }
25904
25905 bool dwarf_always_disassemble;
25906
25907 static void
25908 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25909 struct cmd_list_element *c, const char *value)
25910 {
25911 fprintf_filtered (file,
25912 _("Whether to always disassemble "
25913 "DWARF expressions is %s.\n"),
25914 value);
25915 }
25916
25917 static void
25918 show_check_physname (struct ui_file *file, int from_tty,
25919 struct cmd_list_element *c, const char *value)
25920 {
25921 fprintf_filtered (file,
25922 _("Whether to check \"physname\" is %s.\n"),
25923 value);
25924 }
25925
25926 void
25927 _initialize_dwarf2_read (void)
25928 {
25929 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25930 Set DWARF specific variables.\n\
25931 Configure DWARF variables such as the cache size."),
25932 &set_dwarf_cmdlist, "maintenance set dwarf ",
25933 0/*allow-unknown*/, &maintenance_set_cmdlist);
25934
25935 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25936 Show DWARF specific variables.\n\
25937 Show DWARF variables such as the cache size."),
25938 &show_dwarf_cmdlist, "maintenance show dwarf ",
25939 0/*allow-unknown*/, &maintenance_show_cmdlist);
25940
25941 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25942 &dwarf_max_cache_age, _("\
25943 Set the upper bound on the age of cached DWARF compilation units."), _("\
25944 Show the upper bound on the age of cached DWARF compilation units."), _("\
25945 A higher limit means that cached compilation units will be stored\n\
25946 in memory longer, and more total memory will be used. Zero disables\n\
25947 caching, which can slow down startup."),
25948 NULL,
25949 show_dwarf_max_cache_age,
25950 &set_dwarf_cmdlist,
25951 &show_dwarf_cmdlist);
25952
25953 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25954 &dwarf_always_disassemble, _("\
25955 Set whether `info address' always disassembles DWARF expressions."), _("\
25956 Show whether `info address' always disassembles DWARF expressions."), _("\
25957 When enabled, DWARF expressions are always printed in an assembly-like\n\
25958 syntax. When disabled, expressions will be printed in a more\n\
25959 conversational style, when possible."),
25960 NULL,
25961 show_dwarf_always_disassemble,
25962 &set_dwarf_cmdlist,
25963 &show_dwarf_cmdlist);
25964
25965 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25966 Set debugging of the DWARF reader."), _("\
25967 Show debugging of the DWARF reader."), _("\
25968 When enabled (non-zero), debugging messages are printed during DWARF\n\
25969 reading and symtab expansion. A value of 1 (one) provides basic\n\
25970 information. A value greater than 1 provides more verbose information."),
25971 NULL,
25972 NULL,
25973 &setdebuglist, &showdebuglist);
25974
25975 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25976 Set debugging of the DWARF DIE reader."), _("\
25977 Show debugging of the DWARF DIE reader."), _("\
25978 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25979 The value is the maximum depth to print."),
25980 NULL,
25981 NULL,
25982 &setdebuglist, &showdebuglist);
25983
25984 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25985 Set debugging of the dwarf line reader."), _("\
25986 Show debugging of the dwarf line reader."), _("\
25987 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25988 A value of 1 (one) provides basic information.\n\
25989 A value greater than 1 provides more verbose information."),
25990 NULL,
25991 NULL,
25992 &setdebuglist, &showdebuglist);
25993
25994 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25995 Set cross-checking of \"physname\" code against demangler."), _("\
25996 Show cross-checking of \"physname\" code against demangler."), _("\
25997 When enabled, GDB's internal \"physname\" code is checked against\n\
25998 the demangler."),
25999 NULL, show_check_physname,
26000 &setdebuglist, &showdebuglist);
26001
26002 add_setshow_boolean_cmd ("use-deprecated-index-sections",
26003 no_class, &use_deprecated_index_sections, _("\
26004 Set whether to use deprecated gdb_index sections."), _("\
26005 Show whether to use deprecated gdb_index sections."), _("\
26006 When enabled, deprecated .gdb_index sections are used anyway.\n\
26007 Normally they are ignored either because of a missing feature or\n\
26008 performance issue.\n\
26009 Warning: This option must be enabled before gdb reads the file."),
26010 NULL,
26011 NULL,
26012 &setlist, &showlist);
26013
26014 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
26015 &dwarf2_locexpr_funcs);
26016 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
26017 &dwarf2_loclist_funcs);
26018
26019 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
26020 &dwarf2_block_frame_base_locexpr_funcs);
26021 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
26022 &dwarf2_block_frame_base_loclist_funcs);
26023
26024 #if GDB_SELF_TEST
26025 selftests::register_test ("dw2_expand_symtabs_matching",
26026 selftests::dw2_expand_symtabs_matching::run_test);
26027 #endif
26028 }
This page took 0.618088 seconds and 4 git commands to generate.