[gdb] Fix typos in comments
[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 /* A 1-based directory index. This is a strong typedef to prevent
913 accidentally using a directory index as a 0-based index into an
914 array/vector. */
915 enum class dir_index : unsigned int {};
916
917 /* Likewise, a 1-based file name index. */
918 enum class file_name_index : unsigned int {};
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 (1-based). Returns NULL if INDEX
971 is out of bounds. */
972 const char *include_dir_at (dir_index index) const
973 {
974 /* Convert directory index number (1-based) to vector index
975 (0-based). */
976 size_t vec_index = to_underlying (index) - 1;
977
978 if (vec_index >= include_dirs.size ())
979 return NULL;
980 return include_dirs[vec_index];
981 }
982
983 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
984 is out of bounds. */
985 file_entry *file_name_at (file_name_index index)
986 {
987 /* Convert file name index number (1-based) to vector index
988 (0-based). */
989 size_t vec_index = to_underlying (index) - 1;
990
991 if (vec_index >= file_names.size ())
992 return NULL;
993 return &file_names[vec_index];
994 }
995
996 /* Offset of line number information in .debug_line section. */
997 sect_offset sect_off {};
998
999 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1000 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1001
1002 unsigned int total_length {};
1003 unsigned short version {};
1004 unsigned int header_length {};
1005 unsigned char minimum_instruction_length {};
1006 unsigned char maximum_ops_per_instruction {};
1007 unsigned char default_is_stmt {};
1008 int line_base {};
1009 unsigned char line_range {};
1010 unsigned char opcode_base {};
1011
1012 /* standard_opcode_lengths[i] is the number of operands for the
1013 standard opcode whose value is i. This means that
1014 standard_opcode_lengths[0] is unused, and the last meaningful
1015 element is standard_opcode_lengths[opcode_base - 1]. */
1016 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1017
1018 /* The include_directories table. Note these are observing
1019 pointers. The memory is owned by debug_line_buffer. */
1020 std::vector<const char *> include_dirs;
1021
1022 /* The file_names table. */
1023 std::vector<file_entry> file_names;
1024
1025 /* The start and end of the statement program following this
1026 header. These point into dwarf2_per_objfile->line_buffer. */
1027 const gdb_byte *statement_program_start {}, *statement_program_end {};
1028 };
1029
1030 typedef std::unique_ptr<line_header> line_header_up;
1031
1032 const char *
1033 file_entry::include_dir (const line_header *lh) const
1034 {
1035 return lh->include_dir_at (d_index);
1036 }
1037
1038 /* When we construct a partial symbol table entry we only
1039 need this much information. */
1040 struct partial_die_info : public allocate_on_obstack
1041 {
1042 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1043
1044 /* Disable assign but still keep copy ctor, which is needed
1045 load_partial_dies. */
1046 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1047
1048 /* Adjust the partial die before generating a symbol for it. This
1049 function may set the is_external flag or change the DIE's
1050 name. */
1051 void fixup (struct dwarf2_cu *cu);
1052
1053 /* Read a minimal amount of information into the minimal die
1054 structure. */
1055 const gdb_byte *read (const struct die_reader_specs *reader,
1056 const struct abbrev_info &abbrev,
1057 const gdb_byte *info_ptr);
1058
1059 /* Offset of this DIE. */
1060 const sect_offset sect_off;
1061
1062 /* DWARF-2 tag for this DIE. */
1063 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1064
1065 /* Assorted flags describing the data found in this DIE. */
1066 const unsigned int has_children : 1;
1067
1068 unsigned int is_external : 1;
1069 unsigned int is_declaration : 1;
1070 unsigned int has_type : 1;
1071 unsigned int has_specification : 1;
1072 unsigned int has_pc_info : 1;
1073 unsigned int may_be_inlined : 1;
1074
1075 /* This DIE has been marked DW_AT_main_subprogram. */
1076 unsigned int main_subprogram : 1;
1077
1078 /* Flag set if the SCOPE field of this structure has been
1079 computed. */
1080 unsigned int scope_set : 1;
1081
1082 /* Flag set if the DIE has a byte_size attribute. */
1083 unsigned int has_byte_size : 1;
1084
1085 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1086 unsigned int has_const_value : 1;
1087
1088 /* Flag set if any of the DIE's children are template arguments. */
1089 unsigned int has_template_arguments : 1;
1090
1091 /* Flag set if fixup has been called on this die. */
1092 unsigned int fixup_called : 1;
1093
1094 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1095 unsigned int is_dwz : 1;
1096
1097 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1098 unsigned int spec_is_dwz : 1;
1099
1100 /* The name of this DIE. Normally the value of DW_AT_name, but
1101 sometimes a default name for unnamed DIEs. */
1102 const char *name = nullptr;
1103
1104 /* The linkage name, if present. */
1105 const char *linkage_name = nullptr;
1106
1107 /* The scope to prepend to our children. This is generally
1108 allocated on the comp_unit_obstack, so will disappear
1109 when this compilation unit leaves the cache. */
1110 const char *scope = nullptr;
1111
1112 /* Some data associated with the partial DIE. The tag determines
1113 which field is live. */
1114 union
1115 {
1116 /* The location description associated with this DIE, if any. */
1117 struct dwarf_block *locdesc;
1118 /* The offset of an import, for DW_TAG_imported_unit. */
1119 sect_offset sect_off;
1120 } d {};
1121
1122 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1123 CORE_ADDR lowpc = 0;
1124 CORE_ADDR highpc = 0;
1125
1126 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1127 DW_AT_sibling, if any. */
1128 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1129 could return DW_AT_sibling values to its caller load_partial_dies. */
1130 const gdb_byte *sibling = nullptr;
1131
1132 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1133 DW_AT_specification (or DW_AT_abstract_origin or
1134 DW_AT_extension). */
1135 sect_offset spec_offset {};
1136
1137 /* Pointers to this DIE's parent, first child, and next sibling,
1138 if any. */
1139 struct partial_die_info *die_parent = nullptr;
1140 struct partial_die_info *die_child = nullptr;
1141 struct partial_die_info *die_sibling = nullptr;
1142
1143 friend struct partial_die_info *
1144 dwarf2_cu::find_partial_die (sect_offset sect_off);
1145
1146 private:
1147 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1148 partial_die_info (sect_offset sect_off)
1149 : partial_die_info (sect_off, DW_TAG_padding, 0)
1150 {
1151 }
1152
1153 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1154 int has_children_)
1155 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1156 {
1157 is_external = 0;
1158 is_declaration = 0;
1159 has_type = 0;
1160 has_specification = 0;
1161 has_pc_info = 0;
1162 may_be_inlined = 0;
1163 main_subprogram = 0;
1164 scope_set = 0;
1165 has_byte_size = 0;
1166 has_const_value = 0;
1167 has_template_arguments = 0;
1168 fixup_called = 0;
1169 is_dwz = 0;
1170 spec_is_dwz = 0;
1171 }
1172 };
1173
1174 /* This data structure holds the information of an abbrev. */
1175 struct abbrev_info
1176 {
1177 unsigned int number; /* number identifying abbrev */
1178 enum dwarf_tag tag; /* dwarf tag */
1179 unsigned short has_children; /* boolean */
1180 unsigned short num_attrs; /* number of attributes */
1181 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1182 struct abbrev_info *next; /* next in chain */
1183 };
1184
1185 struct attr_abbrev
1186 {
1187 ENUM_BITFIELD(dwarf_attribute) name : 16;
1188 ENUM_BITFIELD(dwarf_form) form : 16;
1189
1190 /* It is valid only if FORM is DW_FORM_implicit_const. */
1191 LONGEST implicit_const;
1192 };
1193
1194 /* Size of abbrev_table.abbrev_hash_table. */
1195 #define ABBREV_HASH_SIZE 121
1196
1197 /* Top level data structure to contain an abbreviation table. */
1198
1199 struct abbrev_table
1200 {
1201 explicit abbrev_table (sect_offset off)
1202 : sect_off (off)
1203 {
1204 m_abbrevs =
1205 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1206 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1207 }
1208
1209 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1210
1211 /* Allocate space for a struct abbrev_info object in
1212 ABBREV_TABLE. */
1213 struct abbrev_info *alloc_abbrev ();
1214
1215 /* Add an abbreviation to the table. */
1216 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1217
1218 /* Look up an abbrev in the table.
1219 Returns NULL if the abbrev is not found. */
1220
1221 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1222
1223
1224 /* Where the abbrev table came from.
1225 This is used as a sanity check when the table is used. */
1226 const sect_offset sect_off;
1227
1228 /* Storage for the abbrev table. */
1229 auto_obstack abbrev_obstack;
1230
1231 private:
1232
1233 /* Hash table of abbrevs.
1234 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1235 It could be statically allocated, but the previous code didn't so we
1236 don't either. */
1237 struct abbrev_info **m_abbrevs;
1238 };
1239
1240 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1241
1242 /* Attributes have a name and a value. */
1243 struct attribute
1244 {
1245 ENUM_BITFIELD(dwarf_attribute) name : 16;
1246 ENUM_BITFIELD(dwarf_form) form : 15;
1247
1248 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1249 field should be in u.str (existing only for DW_STRING) but it is kept
1250 here for better struct attribute alignment. */
1251 unsigned int string_is_canonical : 1;
1252
1253 union
1254 {
1255 const char *str;
1256 struct dwarf_block *blk;
1257 ULONGEST unsnd;
1258 LONGEST snd;
1259 CORE_ADDR addr;
1260 ULONGEST signature;
1261 }
1262 u;
1263 };
1264
1265 /* This data structure holds a complete die structure. */
1266 struct die_info
1267 {
1268 /* DWARF-2 tag for this DIE. */
1269 ENUM_BITFIELD(dwarf_tag) tag : 16;
1270
1271 /* Number of attributes */
1272 unsigned char num_attrs;
1273
1274 /* True if we're presently building the full type name for the
1275 type derived from this DIE. */
1276 unsigned char building_fullname : 1;
1277
1278 /* True if this die is in process. PR 16581. */
1279 unsigned char in_process : 1;
1280
1281 /* Abbrev number */
1282 unsigned int abbrev;
1283
1284 /* Offset in .debug_info or .debug_types section. */
1285 sect_offset sect_off;
1286
1287 /* The dies in a compilation unit form an n-ary tree. PARENT
1288 points to this die's parent; CHILD points to the first child of
1289 this node; and all the children of a given node are chained
1290 together via their SIBLING fields. */
1291 struct die_info *child; /* Its first child, if any. */
1292 struct die_info *sibling; /* Its next sibling, if any. */
1293 struct die_info *parent; /* Its parent, if any. */
1294
1295 /* An array of attributes, with NUM_ATTRS elements. There may be
1296 zero, but it's not common and zero-sized arrays are not
1297 sufficiently portable C. */
1298 struct attribute attrs[1];
1299 };
1300
1301 /* Get at parts of an attribute structure. */
1302
1303 #define DW_STRING(attr) ((attr)->u.str)
1304 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1305 #define DW_UNSND(attr) ((attr)->u.unsnd)
1306 #define DW_BLOCK(attr) ((attr)->u.blk)
1307 #define DW_SND(attr) ((attr)->u.snd)
1308 #define DW_ADDR(attr) ((attr)->u.addr)
1309 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1310
1311 /* Blocks are a bunch of untyped bytes. */
1312 struct dwarf_block
1313 {
1314 size_t size;
1315
1316 /* Valid only if SIZE is not zero. */
1317 const gdb_byte *data;
1318 };
1319
1320 #ifndef ATTR_ALLOC_CHUNK
1321 #define ATTR_ALLOC_CHUNK 4
1322 #endif
1323
1324 /* Allocate fields for structs, unions and enums in this size. */
1325 #ifndef DW_FIELD_ALLOC_CHUNK
1326 #define DW_FIELD_ALLOC_CHUNK 4
1327 #endif
1328
1329 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1330 but this would require a corresponding change in unpack_field_as_long
1331 and friends. */
1332 static int bits_per_byte = 8;
1333
1334 /* When reading a variant or variant part, we track a bit more
1335 information about the field, and store it in an object of this
1336 type. */
1337
1338 struct variant_field
1339 {
1340 /* If we see a DW_TAG_variant, then this will be the discriminant
1341 value. */
1342 ULONGEST discriminant_value;
1343 /* If we see a DW_TAG_variant, then this will be set if this is the
1344 default branch. */
1345 bool default_branch;
1346 /* While reading a DW_TAG_variant_part, this will be set if this
1347 field is the discriminant. */
1348 bool is_discriminant;
1349 };
1350
1351 struct nextfield
1352 {
1353 int accessibility = 0;
1354 int virtuality = 0;
1355 /* Extra information to describe a variant or variant part. */
1356 struct variant_field variant {};
1357 struct field field {};
1358 };
1359
1360 struct fnfieldlist
1361 {
1362 const char *name = nullptr;
1363 std::vector<struct fn_field> fnfields;
1364 };
1365
1366 /* The routines that read and process dies for a C struct or C++ class
1367 pass lists of data member fields and lists of member function fields
1368 in an instance of a field_info structure, as defined below. */
1369 struct field_info
1370 {
1371 /* List of data member and baseclasses fields. */
1372 std::vector<struct nextfield> fields;
1373 std::vector<struct nextfield> baseclasses;
1374
1375 /* Number of fields (including baseclasses). */
1376 int nfields = 0;
1377
1378 /* Set if the accesibility of one of the fields is not public. */
1379 int non_public_fields = 0;
1380
1381 /* Member function fieldlist array, contains name of possibly overloaded
1382 member function, number of overloaded member functions and a pointer
1383 to the head of the member function field chain. */
1384 std::vector<struct fnfieldlist> fnfieldlists;
1385
1386 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1387 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1388 std::vector<struct decl_field> typedef_field_list;
1389
1390 /* Nested types defined by this class and the number of elements in this
1391 list. */
1392 std::vector<struct decl_field> nested_types_list;
1393 };
1394
1395 /* One item on the queue of compilation units to read in full symbols
1396 for. */
1397 struct dwarf2_queue_item
1398 {
1399 struct dwarf2_per_cu_data *per_cu;
1400 enum language pretend_language;
1401 struct dwarf2_queue_item *next;
1402 };
1403
1404 /* The current queue. */
1405 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1406
1407 /* Loaded secondary compilation units are kept in memory until they
1408 have not been referenced for the processing of this many
1409 compilation units. Set this to zero to disable caching. Cache
1410 sizes of up to at least twenty will improve startup time for
1411 typical inter-CU-reference binaries, at an obvious memory cost. */
1412 static int dwarf_max_cache_age = 5;
1413 static void
1414 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1415 struct cmd_list_element *c, const char *value)
1416 {
1417 fprintf_filtered (file, _("The upper bound on the age of cached "
1418 "DWARF compilation units is %s.\n"),
1419 value);
1420 }
1421 \f
1422 /* local function prototypes */
1423
1424 static const char *get_section_name (const struct dwarf2_section_info *);
1425
1426 static const char *get_section_file_name (const struct dwarf2_section_info *);
1427
1428 static void dwarf2_find_base_address (struct die_info *die,
1429 struct dwarf2_cu *cu);
1430
1431 static struct partial_symtab *create_partial_symtab
1432 (struct dwarf2_per_cu_data *per_cu, const char *name);
1433
1434 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1435 const gdb_byte *info_ptr,
1436 struct die_info *type_unit_die,
1437 int has_children, void *data);
1438
1439 static void dwarf2_build_psymtabs_hard
1440 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1441
1442 static void scan_partial_symbols (struct partial_die_info *,
1443 CORE_ADDR *, CORE_ADDR *,
1444 int, struct dwarf2_cu *);
1445
1446 static void add_partial_symbol (struct partial_die_info *,
1447 struct dwarf2_cu *);
1448
1449 static void add_partial_namespace (struct partial_die_info *pdi,
1450 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1451 int set_addrmap, struct dwarf2_cu *cu);
1452
1453 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1454 CORE_ADDR *highpc, int set_addrmap,
1455 struct dwarf2_cu *cu);
1456
1457 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1458 struct dwarf2_cu *cu);
1459
1460 static void add_partial_subprogram (struct partial_die_info *pdi,
1461 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1462 int need_pc, struct dwarf2_cu *cu);
1463
1464 static void dwarf2_read_symtab (struct partial_symtab *,
1465 struct objfile *);
1466
1467 static void psymtab_to_symtab_1 (struct partial_symtab *);
1468
1469 static abbrev_table_up abbrev_table_read_table
1470 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1471 sect_offset);
1472
1473 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1474
1475 static struct partial_die_info *load_partial_dies
1476 (const struct die_reader_specs *, const gdb_byte *, int);
1477
1478 /* A pair of partial_die_info and compilation unit. */
1479 struct cu_partial_die_info
1480 {
1481 /* The compilation unit of the partial_die_info. */
1482 struct dwarf2_cu *cu;
1483 /* A partial_die_info. */
1484 struct partial_die_info *pdi;
1485
1486 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1487 : cu (cu),
1488 pdi (pdi)
1489 { /* Nothing. */ }
1490
1491 private:
1492 cu_partial_die_info () = delete;
1493 };
1494
1495 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1496 struct dwarf2_cu *);
1497
1498 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1499 struct attribute *, struct attr_abbrev *,
1500 const gdb_byte *);
1501
1502 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1503
1504 static int read_1_signed_byte (bfd *, const gdb_byte *);
1505
1506 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1507
1508 /* Read the next three bytes (little-endian order) as an unsigned integer. */
1509 static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1510
1511 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1512
1513 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1514
1515 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1516 unsigned int *);
1517
1518 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1519
1520 static LONGEST read_checked_initial_length_and_offset
1521 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1522 unsigned int *, unsigned int *);
1523
1524 static LONGEST read_offset (bfd *, const gdb_byte *,
1525 const struct comp_unit_head *,
1526 unsigned int *);
1527
1528 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1529
1530 static sect_offset read_abbrev_offset
1531 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1532 struct dwarf2_section_info *, sect_offset);
1533
1534 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1535
1536 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1537
1538 static const char *read_indirect_string
1539 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1540 const struct comp_unit_head *, unsigned int *);
1541
1542 static const char *read_indirect_line_string
1543 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1544 const struct comp_unit_head *, unsigned int *);
1545
1546 static const char *read_indirect_string_at_offset
1547 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1548 LONGEST str_offset);
1549
1550 static const char *read_indirect_string_from_dwz
1551 (struct objfile *objfile, struct dwz_file *, LONGEST);
1552
1553 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1554
1555 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1556 const gdb_byte *,
1557 unsigned int *);
1558
1559 static const char *read_str_index (const struct die_reader_specs *reader,
1560 ULONGEST str_index);
1561
1562 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1563
1564 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1565 struct dwarf2_cu *);
1566
1567 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1568 unsigned int);
1569
1570 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1571 struct dwarf2_cu *cu);
1572
1573 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1574
1575 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1576 struct dwarf2_cu *cu);
1577
1578 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1579
1580 static struct die_info *die_specification (struct die_info *die,
1581 struct dwarf2_cu **);
1582
1583 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1584 struct dwarf2_cu *cu);
1585
1586 static void dwarf_decode_lines (struct line_header *, const char *,
1587 struct dwarf2_cu *, struct partial_symtab *,
1588 CORE_ADDR, int decode_mapping);
1589
1590 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1591 const char *);
1592
1593 static struct symbol *new_symbol (struct die_info *, struct type *,
1594 struct dwarf2_cu *, struct symbol * = NULL);
1595
1596 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1597 struct dwarf2_cu *);
1598
1599 static void dwarf2_const_value_attr (const struct attribute *attr,
1600 struct type *type,
1601 const char *name,
1602 struct obstack *obstack,
1603 struct dwarf2_cu *cu, LONGEST *value,
1604 const gdb_byte **bytes,
1605 struct dwarf2_locexpr_baton **baton);
1606
1607 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1608
1609 static int need_gnat_info (struct dwarf2_cu *);
1610
1611 static struct type *die_descriptive_type (struct die_info *,
1612 struct dwarf2_cu *);
1613
1614 static void set_descriptive_type (struct type *, struct die_info *,
1615 struct dwarf2_cu *);
1616
1617 static struct type *die_containing_type (struct die_info *,
1618 struct dwarf2_cu *);
1619
1620 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1621 struct dwarf2_cu *);
1622
1623 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1624
1625 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1626
1627 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1628
1629 static char *typename_concat (struct obstack *obs, const char *prefix,
1630 const char *suffix, int physname,
1631 struct dwarf2_cu *cu);
1632
1633 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1634
1635 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1636
1637 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1638
1639 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1640
1641 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1642
1643 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1644
1645 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1646 struct dwarf2_cu *, struct partial_symtab *);
1647
1648 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1649 values. Keep the items ordered with increasing constraints compliance. */
1650 enum pc_bounds_kind
1651 {
1652 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1653 PC_BOUNDS_NOT_PRESENT,
1654
1655 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1656 were present but they do not form a valid range of PC addresses. */
1657 PC_BOUNDS_INVALID,
1658
1659 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1660 PC_BOUNDS_RANGES,
1661
1662 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1663 PC_BOUNDS_HIGH_LOW,
1664 };
1665
1666 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1667 CORE_ADDR *, CORE_ADDR *,
1668 struct dwarf2_cu *,
1669 struct partial_symtab *);
1670
1671 static void get_scope_pc_bounds (struct die_info *,
1672 CORE_ADDR *, CORE_ADDR *,
1673 struct dwarf2_cu *);
1674
1675 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1676 CORE_ADDR, struct dwarf2_cu *);
1677
1678 static void dwarf2_add_field (struct field_info *, struct die_info *,
1679 struct dwarf2_cu *);
1680
1681 static void dwarf2_attach_fields_to_type (struct field_info *,
1682 struct type *, struct dwarf2_cu *);
1683
1684 static void dwarf2_add_member_fn (struct field_info *,
1685 struct die_info *, struct type *,
1686 struct dwarf2_cu *);
1687
1688 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1689 struct type *,
1690 struct dwarf2_cu *);
1691
1692 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1693
1694 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1695
1696 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1697
1698 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1699
1700 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1701
1702 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1703
1704 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1705
1706 static struct type *read_module_type (struct die_info *die,
1707 struct dwarf2_cu *cu);
1708
1709 static const char *namespace_name (struct die_info *die,
1710 int *is_anonymous, struct dwarf2_cu *);
1711
1712 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1713
1714 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1715
1716 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1717 struct dwarf2_cu *);
1718
1719 static struct die_info *read_die_and_siblings_1
1720 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1721 struct die_info *);
1722
1723 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1724 const gdb_byte *info_ptr,
1725 const gdb_byte **new_info_ptr,
1726 struct die_info *parent);
1727
1728 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1729 struct die_info **, const gdb_byte *,
1730 int *, int);
1731
1732 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1733 struct die_info **, const gdb_byte *,
1734 int *);
1735
1736 static void process_die (struct die_info *, struct dwarf2_cu *);
1737
1738 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1739 struct obstack *);
1740
1741 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1742
1743 static const char *dwarf2_full_name (const char *name,
1744 struct die_info *die,
1745 struct dwarf2_cu *cu);
1746
1747 static const char *dwarf2_physname (const char *name, struct die_info *die,
1748 struct dwarf2_cu *cu);
1749
1750 static struct die_info *dwarf2_extension (struct die_info *die,
1751 struct dwarf2_cu **);
1752
1753 static const char *dwarf_tag_name (unsigned int);
1754
1755 static const char *dwarf_attr_name (unsigned int);
1756
1757 static const char *dwarf_unit_type_name (int unit_type);
1758
1759 static const char *dwarf_form_name (unsigned int);
1760
1761 static const char *dwarf_bool_name (unsigned int);
1762
1763 static const char *dwarf_type_encoding_name (unsigned int);
1764
1765 static struct die_info *sibling_die (struct die_info *);
1766
1767 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1768
1769 static void dump_die_for_error (struct die_info *);
1770
1771 static void dump_die_1 (struct ui_file *, int level, int max_level,
1772 struct die_info *);
1773
1774 /*static*/ void dump_die (struct die_info *, int max_level);
1775
1776 static void store_in_ref_table (struct die_info *,
1777 struct dwarf2_cu *);
1778
1779 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1780
1781 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1782
1783 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1784 const struct attribute *,
1785 struct dwarf2_cu **);
1786
1787 static struct die_info *follow_die_ref (struct die_info *,
1788 const struct attribute *,
1789 struct dwarf2_cu **);
1790
1791 static struct die_info *follow_die_sig (struct die_info *,
1792 const struct attribute *,
1793 struct dwarf2_cu **);
1794
1795 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1796 struct dwarf2_cu *);
1797
1798 static struct type *get_DW_AT_signature_type (struct die_info *,
1799 const struct attribute *,
1800 struct dwarf2_cu *);
1801
1802 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1803
1804 static void read_signatured_type (struct signatured_type *);
1805
1806 static int attr_to_dynamic_prop (const struct attribute *attr,
1807 struct die_info *die, struct dwarf2_cu *cu,
1808 struct dynamic_prop *prop, struct type *type);
1809
1810 /* memory allocation interface */
1811
1812 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1813
1814 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1815
1816 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1817
1818 static int attr_form_is_block (const struct attribute *);
1819
1820 static int attr_form_is_section_offset (const struct attribute *);
1821
1822 static int attr_form_is_constant (const struct attribute *);
1823
1824 static int attr_form_is_ref (const struct attribute *);
1825
1826 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1827 struct dwarf2_loclist_baton *baton,
1828 const struct attribute *attr);
1829
1830 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1831 struct symbol *sym,
1832 struct dwarf2_cu *cu,
1833 int is_block);
1834
1835 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1836 const gdb_byte *info_ptr,
1837 struct abbrev_info *abbrev);
1838
1839 static hashval_t partial_die_hash (const void *item);
1840
1841 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1842
1843 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1844 (sect_offset sect_off, unsigned int offset_in_dwz,
1845 struct dwarf2_per_objfile *dwarf2_per_objfile);
1846
1847 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1848 struct die_info *comp_unit_die,
1849 enum language pretend_language);
1850
1851 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1852
1853 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1854
1855 static struct type *set_die_type (struct die_info *, struct type *,
1856 struct dwarf2_cu *);
1857
1858 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1859
1860 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1861
1862 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1863 enum language);
1864
1865 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1866 enum language);
1867
1868 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1869 enum language);
1870
1871 static void dwarf2_add_dependence (struct dwarf2_cu *,
1872 struct dwarf2_per_cu_data *);
1873
1874 static void dwarf2_mark (struct dwarf2_cu *);
1875
1876 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1877
1878 static struct type *get_die_type_at_offset (sect_offset,
1879 struct dwarf2_per_cu_data *);
1880
1881 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1882
1883 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1884 enum language pretend_language);
1885
1886 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1887
1888 static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1889 static struct type *dwarf2_per_cu_addr_sized_int_type
1890 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
1891
1892 /* Class, the destructor of which frees all allocated queue entries. This
1893 will only have work to do if an error was thrown while processing the
1894 dwarf. If no error was thrown then the queue entries should have all
1895 been processed, and freed, as we went along. */
1896
1897 class dwarf2_queue_guard
1898 {
1899 public:
1900 dwarf2_queue_guard () = default;
1901
1902 /* Free any entries remaining on the queue. There should only be
1903 entries left if we hit an error while processing the dwarf. */
1904 ~dwarf2_queue_guard ()
1905 {
1906 struct dwarf2_queue_item *item, *last;
1907
1908 item = dwarf2_queue;
1909 while (item)
1910 {
1911 /* Anything still marked queued is likely to be in an
1912 inconsistent state, so discard it. */
1913 if (item->per_cu->queued)
1914 {
1915 if (item->per_cu->cu != NULL)
1916 free_one_cached_comp_unit (item->per_cu);
1917 item->per_cu->queued = 0;
1918 }
1919
1920 last = item;
1921 item = item->next;
1922 xfree (last);
1923 }
1924
1925 dwarf2_queue = dwarf2_queue_tail = NULL;
1926 }
1927 };
1928
1929 /* The return type of find_file_and_directory. Note, the enclosed
1930 string pointers are only valid while this object is valid. */
1931
1932 struct file_and_directory
1933 {
1934 /* The filename. This is never NULL. */
1935 const char *name;
1936
1937 /* The compilation directory. NULL if not known. If we needed to
1938 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1939 points directly to the DW_AT_comp_dir string attribute owned by
1940 the obstack that owns the DIE. */
1941 const char *comp_dir;
1942
1943 /* If we needed to build a new string for comp_dir, this is what
1944 owns the storage. */
1945 std::string comp_dir_storage;
1946 };
1947
1948 static file_and_directory find_file_and_directory (struct die_info *die,
1949 struct dwarf2_cu *cu);
1950
1951 static char *file_full_name (int file, struct line_header *lh,
1952 const char *comp_dir);
1953
1954 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
1955 enum class rcuh_kind { COMPILE, TYPE };
1956
1957 static const gdb_byte *read_and_check_comp_unit_head
1958 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1959 struct comp_unit_head *header,
1960 struct dwarf2_section_info *section,
1961 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1962 rcuh_kind section_kind);
1963
1964 static void init_cutu_and_read_dies
1965 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1966 int use_existing_cu, int keep, bool skip_partial,
1967 die_reader_func_ftype *die_reader_func, void *data);
1968
1969 static void init_cutu_and_read_dies_simple
1970 (struct dwarf2_per_cu_data *this_cu,
1971 die_reader_func_ftype *die_reader_func, void *data);
1972
1973 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1974
1975 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1976
1977 static struct dwo_unit *lookup_dwo_unit_in_dwp
1978 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1979 struct dwp_file *dwp_file, const char *comp_dir,
1980 ULONGEST signature, int is_debug_types);
1981
1982 static struct dwp_file *get_dwp_file
1983 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1984
1985 static struct dwo_unit *lookup_dwo_comp_unit
1986 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1987
1988 static struct dwo_unit *lookup_dwo_type_unit
1989 (struct signatured_type *, const char *, const char *);
1990
1991 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1992
1993 /* A unique pointer to a dwo_file. */
1994
1995 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1996
1997 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1998
1999 static void check_producer (struct dwarf2_cu *cu);
2000
2001 static void free_line_header_voidp (void *arg);
2002 \f
2003 /* Various complaints about symbol reading that don't abort the process. */
2004
2005 static void
2006 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2007 {
2008 complaint (_("statement list doesn't fit in .debug_line section"));
2009 }
2010
2011 static void
2012 dwarf2_debug_line_missing_file_complaint (void)
2013 {
2014 complaint (_(".debug_line section has line data without a file"));
2015 }
2016
2017 static void
2018 dwarf2_debug_line_missing_end_sequence_complaint (void)
2019 {
2020 complaint (_(".debug_line section has line "
2021 "program sequence without an end"));
2022 }
2023
2024 static void
2025 dwarf2_complex_location_expr_complaint (void)
2026 {
2027 complaint (_("location expression too complex"));
2028 }
2029
2030 static void
2031 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2032 int arg3)
2033 {
2034 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
2035 arg1, arg2, arg3);
2036 }
2037
2038 static void
2039 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2040 {
2041 complaint (_("debug info runs off end of %s section"
2042 " [in module %s]"),
2043 get_section_name (section),
2044 get_section_file_name (section));
2045 }
2046
2047 static void
2048 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2049 {
2050 complaint (_("macro debug info contains a "
2051 "malformed macro definition:\n`%s'"),
2052 arg1);
2053 }
2054
2055 static void
2056 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2057 {
2058 complaint (_("invalid attribute class or form for '%s' in '%s'"),
2059 arg1, arg2);
2060 }
2061
2062 /* Hash function for line_header_hash. */
2063
2064 static hashval_t
2065 line_header_hash (const struct line_header *ofs)
2066 {
2067 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2068 }
2069
2070 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2071
2072 static hashval_t
2073 line_header_hash_voidp (const void *item)
2074 {
2075 const struct line_header *ofs = (const struct line_header *) item;
2076
2077 return line_header_hash (ofs);
2078 }
2079
2080 /* Equality function for line_header_hash. */
2081
2082 static int
2083 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2084 {
2085 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2086 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2087
2088 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2089 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2090 }
2091
2092 \f
2093
2094 /* Read the given attribute value as an address, taking the attribute's
2095 form into account. */
2096
2097 static CORE_ADDR
2098 attr_value_as_address (struct attribute *attr)
2099 {
2100 CORE_ADDR addr;
2101
2102 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2103 && attr->form != DW_FORM_GNU_addr_index)
2104 {
2105 /* Aside from a few clearly defined exceptions, attributes that
2106 contain an address must always be in DW_FORM_addr form.
2107 Unfortunately, some compilers happen to be violating this
2108 requirement by encoding addresses using other forms, such
2109 as DW_FORM_data4 for example. For those broken compilers,
2110 we try to do our best, without any guarantee of success,
2111 to interpret the address correctly. It would also be nice
2112 to generate a complaint, but that would require us to maintain
2113 a list of legitimate cases where a non-address form is allowed,
2114 as well as update callers to pass in at least the CU's DWARF
2115 version. This is more overhead than what we're willing to
2116 expand for a pretty rare case. */
2117 addr = DW_UNSND (attr);
2118 }
2119 else
2120 addr = DW_ADDR (attr);
2121
2122 return addr;
2123 }
2124
2125 /* See declaration. */
2126
2127 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2128 const dwarf2_debug_sections *names,
2129 bool can_copy_)
2130 : objfile (objfile_),
2131 can_copy (can_copy_)
2132 {
2133 if (names == NULL)
2134 names = &dwarf2_elf_names;
2135
2136 bfd *obfd = objfile->obfd;
2137
2138 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2139 locate_sections (obfd, sec, *names);
2140 }
2141
2142 dwarf2_per_objfile::~dwarf2_per_objfile ()
2143 {
2144 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2145 free_cached_comp_units ();
2146
2147 if (quick_file_names_table)
2148 htab_delete (quick_file_names_table);
2149
2150 if (line_header_hash)
2151 htab_delete (line_header_hash);
2152
2153 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2154 per_cu->imported_symtabs_free ();
2155
2156 for (signatured_type *sig_type : all_type_units)
2157 sig_type->per_cu.imported_symtabs_free ();
2158
2159 /* Everything else should be on the objfile obstack. */
2160 }
2161
2162 /* See declaration. */
2163
2164 void
2165 dwarf2_per_objfile::free_cached_comp_units ()
2166 {
2167 dwarf2_per_cu_data *per_cu = read_in_chain;
2168 dwarf2_per_cu_data **last_chain = &read_in_chain;
2169 while (per_cu != NULL)
2170 {
2171 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2172
2173 delete per_cu->cu;
2174 *last_chain = next_cu;
2175 per_cu = next_cu;
2176 }
2177 }
2178
2179 /* A helper class that calls free_cached_comp_units on
2180 destruction. */
2181
2182 class free_cached_comp_units
2183 {
2184 public:
2185
2186 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2187 : m_per_objfile (per_objfile)
2188 {
2189 }
2190
2191 ~free_cached_comp_units ()
2192 {
2193 m_per_objfile->free_cached_comp_units ();
2194 }
2195
2196 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2197
2198 private:
2199
2200 dwarf2_per_objfile *m_per_objfile;
2201 };
2202
2203 /* Try to locate the sections we need for DWARF 2 debugging
2204 information and return true if we have enough to do something.
2205 NAMES points to the dwarf2 section names, or is NULL if the standard
2206 ELF names are used. CAN_COPY is true for formats where symbol
2207 interposition is possible and so symbol values must follow copy
2208 relocation rules. */
2209
2210 int
2211 dwarf2_has_info (struct objfile *objfile,
2212 const struct dwarf2_debug_sections *names,
2213 bool can_copy)
2214 {
2215 if (objfile->flags & OBJF_READNEVER)
2216 return 0;
2217
2218 struct dwarf2_per_objfile *dwarf2_per_objfile
2219 = get_dwarf2_per_objfile (objfile);
2220
2221 if (dwarf2_per_objfile == NULL)
2222 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
2223 names,
2224 can_copy);
2225
2226 return (!dwarf2_per_objfile->info.is_virtual
2227 && dwarf2_per_objfile->info.s.section != NULL
2228 && !dwarf2_per_objfile->abbrev.is_virtual
2229 && dwarf2_per_objfile->abbrev.s.section != NULL);
2230 }
2231
2232 /* Return the containing section of virtual section SECTION. */
2233
2234 static struct dwarf2_section_info *
2235 get_containing_section (const struct dwarf2_section_info *section)
2236 {
2237 gdb_assert (section->is_virtual);
2238 return section->s.containing_section;
2239 }
2240
2241 /* Return the bfd owner of SECTION. */
2242
2243 static struct bfd *
2244 get_section_bfd_owner (const struct dwarf2_section_info *section)
2245 {
2246 if (section->is_virtual)
2247 {
2248 section = get_containing_section (section);
2249 gdb_assert (!section->is_virtual);
2250 }
2251 return section->s.section->owner;
2252 }
2253
2254 /* Return the bfd section of SECTION.
2255 Returns NULL if the section is not present. */
2256
2257 static asection *
2258 get_section_bfd_section (const struct dwarf2_section_info *section)
2259 {
2260 if (section->is_virtual)
2261 {
2262 section = get_containing_section (section);
2263 gdb_assert (!section->is_virtual);
2264 }
2265 return section->s.section;
2266 }
2267
2268 /* Return the name of SECTION. */
2269
2270 static const char *
2271 get_section_name (const struct dwarf2_section_info *section)
2272 {
2273 asection *sectp = get_section_bfd_section (section);
2274
2275 gdb_assert (sectp != NULL);
2276 return bfd_section_name (sectp);
2277 }
2278
2279 /* Return the name of the file SECTION is in. */
2280
2281 static const char *
2282 get_section_file_name (const struct dwarf2_section_info *section)
2283 {
2284 bfd *abfd = get_section_bfd_owner (section);
2285
2286 return bfd_get_filename (abfd);
2287 }
2288
2289 /* Return the id of SECTION.
2290 Returns 0 if SECTION doesn't exist. */
2291
2292 static int
2293 get_section_id (const struct dwarf2_section_info *section)
2294 {
2295 asection *sectp = get_section_bfd_section (section);
2296
2297 if (sectp == NULL)
2298 return 0;
2299 return sectp->id;
2300 }
2301
2302 /* Return the flags of SECTION.
2303 SECTION (or containing section if this is a virtual section) must exist. */
2304
2305 static int
2306 get_section_flags (const struct dwarf2_section_info *section)
2307 {
2308 asection *sectp = get_section_bfd_section (section);
2309
2310 gdb_assert (sectp != NULL);
2311 return bfd_section_flags (sectp);
2312 }
2313
2314 /* When loading sections, we look either for uncompressed section or for
2315 compressed section names. */
2316
2317 static int
2318 section_is_p (const char *section_name,
2319 const struct dwarf2_section_names *names)
2320 {
2321 if (names->normal != NULL
2322 && strcmp (section_name, names->normal) == 0)
2323 return 1;
2324 if (names->compressed != NULL
2325 && strcmp (section_name, names->compressed) == 0)
2326 return 1;
2327 return 0;
2328 }
2329
2330 /* See declaration. */
2331
2332 void
2333 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2334 const dwarf2_debug_sections &names)
2335 {
2336 flagword aflag = bfd_section_flags (sectp);
2337
2338 if ((aflag & SEC_HAS_CONTENTS) == 0)
2339 {
2340 }
2341 else if (elf_section_data (sectp)->this_hdr.sh_size
2342 > bfd_get_file_size (abfd))
2343 {
2344 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2345 warning (_("Discarding section %s which has a section size (%s"
2346 ") larger than the file size [in module %s]"),
2347 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2348 bfd_get_filename (abfd));
2349 }
2350 else if (section_is_p (sectp->name, &names.info))
2351 {
2352 this->info.s.section = sectp;
2353 this->info.size = bfd_section_size (sectp);
2354 }
2355 else if (section_is_p (sectp->name, &names.abbrev))
2356 {
2357 this->abbrev.s.section = sectp;
2358 this->abbrev.size = bfd_section_size (sectp);
2359 }
2360 else if (section_is_p (sectp->name, &names.line))
2361 {
2362 this->line.s.section = sectp;
2363 this->line.size = bfd_section_size (sectp);
2364 }
2365 else if (section_is_p (sectp->name, &names.loc))
2366 {
2367 this->loc.s.section = sectp;
2368 this->loc.size = bfd_section_size (sectp);
2369 }
2370 else if (section_is_p (sectp->name, &names.loclists))
2371 {
2372 this->loclists.s.section = sectp;
2373 this->loclists.size = bfd_section_size (sectp);
2374 }
2375 else if (section_is_p (sectp->name, &names.macinfo))
2376 {
2377 this->macinfo.s.section = sectp;
2378 this->macinfo.size = bfd_section_size (sectp);
2379 }
2380 else if (section_is_p (sectp->name, &names.macro))
2381 {
2382 this->macro.s.section = sectp;
2383 this->macro.size = bfd_section_size (sectp);
2384 }
2385 else if (section_is_p (sectp->name, &names.str))
2386 {
2387 this->str.s.section = sectp;
2388 this->str.size = bfd_section_size (sectp);
2389 }
2390 else if (section_is_p (sectp->name, &names.line_str))
2391 {
2392 this->line_str.s.section = sectp;
2393 this->line_str.size = bfd_section_size (sectp);
2394 }
2395 else if (section_is_p (sectp->name, &names.addr))
2396 {
2397 this->addr.s.section = sectp;
2398 this->addr.size = bfd_section_size (sectp);
2399 }
2400 else if (section_is_p (sectp->name, &names.frame))
2401 {
2402 this->frame.s.section = sectp;
2403 this->frame.size = bfd_section_size (sectp);
2404 }
2405 else if (section_is_p (sectp->name, &names.eh_frame))
2406 {
2407 this->eh_frame.s.section = sectp;
2408 this->eh_frame.size = bfd_section_size (sectp);
2409 }
2410 else if (section_is_p (sectp->name, &names.ranges))
2411 {
2412 this->ranges.s.section = sectp;
2413 this->ranges.size = bfd_section_size (sectp);
2414 }
2415 else if (section_is_p (sectp->name, &names.rnglists))
2416 {
2417 this->rnglists.s.section = sectp;
2418 this->rnglists.size = bfd_section_size (sectp);
2419 }
2420 else if (section_is_p (sectp->name, &names.types))
2421 {
2422 struct dwarf2_section_info type_section;
2423
2424 memset (&type_section, 0, sizeof (type_section));
2425 type_section.s.section = sectp;
2426 type_section.size = bfd_section_size (sectp);
2427
2428 this->types.push_back (type_section);
2429 }
2430 else if (section_is_p (sectp->name, &names.gdb_index))
2431 {
2432 this->gdb_index.s.section = sectp;
2433 this->gdb_index.size = bfd_section_size (sectp);
2434 }
2435 else if (section_is_p (sectp->name, &names.debug_names))
2436 {
2437 this->debug_names.s.section = sectp;
2438 this->debug_names.size = bfd_section_size (sectp);
2439 }
2440 else if (section_is_p (sectp->name, &names.debug_aranges))
2441 {
2442 this->debug_aranges.s.section = sectp;
2443 this->debug_aranges.size = bfd_section_size (sectp);
2444 }
2445
2446 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2447 && bfd_section_vma (sectp) == 0)
2448 this->has_section_at_zero = true;
2449 }
2450
2451 /* A helper function that decides whether a section is empty,
2452 or not present. */
2453
2454 static int
2455 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2456 {
2457 if (section->is_virtual)
2458 return section->size == 0;
2459 return section->s.section == NULL || section->size == 0;
2460 }
2461
2462 /* See dwarf2read.h. */
2463
2464 void
2465 dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
2466 {
2467 asection *sectp;
2468 bfd *abfd;
2469 gdb_byte *buf, *retbuf;
2470
2471 if (info->readin)
2472 return;
2473 info->buffer = NULL;
2474 info->readin = true;
2475
2476 if (dwarf2_section_empty_p (info))
2477 return;
2478
2479 sectp = get_section_bfd_section (info);
2480
2481 /* If this is a virtual section we need to read in the real one first. */
2482 if (info->is_virtual)
2483 {
2484 struct dwarf2_section_info *containing_section =
2485 get_containing_section (info);
2486
2487 gdb_assert (sectp != NULL);
2488 if ((sectp->flags & SEC_RELOC) != 0)
2489 {
2490 error (_("Dwarf Error: DWP format V2 with relocations is not"
2491 " supported in section %s [in module %s]"),
2492 get_section_name (info), get_section_file_name (info));
2493 }
2494 dwarf2_read_section (objfile, containing_section);
2495 /* Other code should have already caught virtual sections that don't
2496 fit. */
2497 gdb_assert (info->virtual_offset + info->size
2498 <= containing_section->size);
2499 /* If the real section is empty or there was a problem reading the
2500 section we shouldn't get here. */
2501 gdb_assert (containing_section->buffer != NULL);
2502 info->buffer = containing_section->buffer + info->virtual_offset;
2503 return;
2504 }
2505
2506 /* If the section has relocations, we must read it ourselves.
2507 Otherwise we attach it to the BFD. */
2508 if ((sectp->flags & SEC_RELOC) == 0)
2509 {
2510 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2511 return;
2512 }
2513
2514 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2515 info->buffer = buf;
2516
2517 /* When debugging .o files, we may need to apply relocations; see
2518 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2519 We never compress sections in .o files, so we only need to
2520 try this when the section is not compressed. */
2521 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2522 if (retbuf != NULL)
2523 {
2524 info->buffer = retbuf;
2525 return;
2526 }
2527
2528 abfd = get_section_bfd_owner (info);
2529 gdb_assert (abfd != NULL);
2530
2531 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2532 || bfd_bread (buf, info->size, abfd) != info->size)
2533 {
2534 error (_("Dwarf Error: Can't read DWARF data"
2535 " in section %s [in module %s]"),
2536 bfd_section_name (sectp), bfd_get_filename (abfd));
2537 }
2538 }
2539
2540 /* A helper function that returns the size of a section in a safe way.
2541 If you are positive that the section has been read before using the
2542 size, then it is safe to refer to the dwarf2_section_info object's
2543 "size" field directly. In other cases, you must call this
2544 function, because for compressed sections the size field is not set
2545 correctly until the section has been read. */
2546
2547 static bfd_size_type
2548 dwarf2_section_size (struct objfile *objfile,
2549 struct dwarf2_section_info *info)
2550 {
2551 if (!info->readin)
2552 dwarf2_read_section (objfile, info);
2553 return info->size;
2554 }
2555
2556 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2557 SECTION_NAME. */
2558
2559 void
2560 dwarf2_get_section_info (struct objfile *objfile,
2561 enum dwarf2_section_enum sect,
2562 asection **sectp, const gdb_byte **bufp,
2563 bfd_size_type *sizep)
2564 {
2565 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2566 struct dwarf2_section_info *info;
2567
2568 /* We may see an objfile without any DWARF, in which case we just
2569 return nothing. */
2570 if (data == NULL)
2571 {
2572 *sectp = NULL;
2573 *bufp = NULL;
2574 *sizep = 0;
2575 return;
2576 }
2577 switch (sect)
2578 {
2579 case DWARF2_DEBUG_FRAME:
2580 info = &data->frame;
2581 break;
2582 case DWARF2_EH_FRAME:
2583 info = &data->eh_frame;
2584 break;
2585 default:
2586 gdb_assert_not_reached ("unexpected section");
2587 }
2588
2589 dwarf2_read_section (objfile, info);
2590
2591 *sectp = get_section_bfd_section (info);
2592 *bufp = info->buffer;
2593 *sizep = info->size;
2594 }
2595
2596 /* A helper function to find the sections for a .dwz file. */
2597
2598 static void
2599 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2600 {
2601 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2602
2603 /* Note that we only support the standard ELF names, because .dwz
2604 is ELF-only (at the time of writing). */
2605 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2606 {
2607 dwz_file->abbrev.s.section = sectp;
2608 dwz_file->abbrev.size = bfd_section_size (sectp);
2609 }
2610 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2611 {
2612 dwz_file->info.s.section = sectp;
2613 dwz_file->info.size = bfd_section_size (sectp);
2614 }
2615 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2616 {
2617 dwz_file->str.s.section = sectp;
2618 dwz_file->str.size = bfd_section_size (sectp);
2619 }
2620 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2621 {
2622 dwz_file->line.s.section = sectp;
2623 dwz_file->line.size = bfd_section_size (sectp);
2624 }
2625 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2626 {
2627 dwz_file->macro.s.section = sectp;
2628 dwz_file->macro.size = bfd_section_size (sectp);
2629 }
2630 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2631 {
2632 dwz_file->gdb_index.s.section = sectp;
2633 dwz_file->gdb_index.size = bfd_section_size (sectp);
2634 }
2635 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2636 {
2637 dwz_file->debug_names.s.section = sectp;
2638 dwz_file->debug_names.size = bfd_section_size (sectp);
2639 }
2640 }
2641
2642 /* See dwarf2read.h. */
2643
2644 struct dwz_file *
2645 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2646 {
2647 const char *filename;
2648 bfd_size_type buildid_len_arg;
2649 size_t buildid_len;
2650 bfd_byte *buildid;
2651
2652 if (dwarf2_per_objfile->dwz_file != NULL)
2653 return dwarf2_per_objfile->dwz_file.get ();
2654
2655 bfd_set_error (bfd_error_no_error);
2656 gdb::unique_xmalloc_ptr<char> data
2657 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2658 &buildid_len_arg, &buildid));
2659 if (data == NULL)
2660 {
2661 if (bfd_get_error () == bfd_error_no_error)
2662 return NULL;
2663 error (_("could not read '.gnu_debugaltlink' section: %s"),
2664 bfd_errmsg (bfd_get_error ()));
2665 }
2666
2667 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2668
2669 buildid_len = (size_t) buildid_len_arg;
2670
2671 filename = data.get ();
2672
2673 std::string abs_storage;
2674 if (!IS_ABSOLUTE_PATH (filename))
2675 {
2676 gdb::unique_xmalloc_ptr<char> abs
2677 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2678
2679 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2680 filename = abs_storage.c_str ();
2681 }
2682
2683 /* First try the file name given in the section. If that doesn't
2684 work, try to use the build-id instead. */
2685 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2686 if (dwz_bfd != NULL)
2687 {
2688 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2689 dwz_bfd.reset (nullptr);
2690 }
2691
2692 if (dwz_bfd == NULL)
2693 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2694
2695 if (dwz_bfd == NULL)
2696 error (_("could not find '.gnu_debugaltlink' file for %s"),
2697 objfile_name (dwarf2_per_objfile->objfile));
2698
2699 std::unique_ptr<struct dwz_file> result
2700 (new struct dwz_file (std::move (dwz_bfd)));
2701
2702 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2703 result.get ());
2704
2705 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2706 result->dwz_bfd.get ());
2707 dwarf2_per_objfile->dwz_file = std::move (result);
2708 return dwarf2_per_objfile->dwz_file.get ();
2709 }
2710 \f
2711 /* DWARF quick_symbols_functions support. */
2712
2713 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2714 unique line tables, so we maintain a separate table of all .debug_line
2715 derived entries to support the sharing.
2716 All the quick functions need is the list of file names. We discard the
2717 line_header when we're done and don't need to record it here. */
2718 struct quick_file_names
2719 {
2720 /* The data used to construct the hash key. */
2721 struct stmt_list_hash hash;
2722
2723 /* The number of entries in file_names, real_names. */
2724 unsigned int num_file_names;
2725
2726 /* The file names from the line table, after being run through
2727 file_full_name. */
2728 const char **file_names;
2729
2730 /* The file names from the line table after being run through
2731 gdb_realpath. These are computed lazily. */
2732 const char **real_names;
2733 };
2734
2735 /* When using the index (and thus not using psymtabs), each CU has an
2736 object of this type. This is used to hold information needed by
2737 the various "quick" methods. */
2738 struct dwarf2_per_cu_quick_data
2739 {
2740 /* The file table. This can be NULL if there was no file table
2741 or it's currently not read in.
2742 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2743 struct quick_file_names *file_names;
2744
2745 /* The corresponding symbol table. This is NULL if symbols for this
2746 CU have not yet been read. */
2747 struct compunit_symtab *compunit_symtab;
2748
2749 /* A temporary mark bit used when iterating over all CUs in
2750 expand_symtabs_matching. */
2751 unsigned int mark : 1;
2752
2753 /* True if we've tried to read the file table and found there isn't one.
2754 There will be no point in trying to read it again next time. */
2755 unsigned int no_file_data : 1;
2756 };
2757
2758 /* Utility hash function for a stmt_list_hash. */
2759
2760 static hashval_t
2761 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2762 {
2763 hashval_t v = 0;
2764
2765 if (stmt_list_hash->dwo_unit != NULL)
2766 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2767 v += to_underlying (stmt_list_hash->line_sect_off);
2768 return v;
2769 }
2770
2771 /* Utility equality function for a stmt_list_hash. */
2772
2773 static int
2774 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2775 const struct stmt_list_hash *rhs)
2776 {
2777 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2778 return 0;
2779 if (lhs->dwo_unit != NULL
2780 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2781 return 0;
2782
2783 return lhs->line_sect_off == rhs->line_sect_off;
2784 }
2785
2786 /* Hash function for a quick_file_names. */
2787
2788 static hashval_t
2789 hash_file_name_entry (const void *e)
2790 {
2791 const struct quick_file_names *file_data
2792 = (const struct quick_file_names *) e;
2793
2794 return hash_stmt_list_entry (&file_data->hash);
2795 }
2796
2797 /* Equality function for a quick_file_names. */
2798
2799 static int
2800 eq_file_name_entry (const void *a, const void *b)
2801 {
2802 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2803 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2804
2805 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2806 }
2807
2808 /* Delete function for a quick_file_names. */
2809
2810 static void
2811 delete_file_name_entry (void *e)
2812 {
2813 struct quick_file_names *file_data = (struct quick_file_names *) e;
2814 int i;
2815
2816 for (i = 0; i < file_data->num_file_names; ++i)
2817 {
2818 xfree ((void*) file_data->file_names[i]);
2819 if (file_data->real_names)
2820 xfree ((void*) file_data->real_names[i]);
2821 }
2822
2823 /* The space for the struct itself lives on objfile_obstack,
2824 so we don't free it here. */
2825 }
2826
2827 /* Create a quick_file_names hash table. */
2828
2829 static htab_t
2830 create_quick_file_names_table (unsigned int nr_initial_entries)
2831 {
2832 return htab_create_alloc (nr_initial_entries,
2833 hash_file_name_entry, eq_file_name_entry,
2834 delete_file_name_entry, xcalloc, xfree);
2835 }
2836
2837 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2838 have to be created afterwards. You should call age_cached_comp_units after
2839 processing PER_CU->CU. dw2_setup must have been already called. */
2840
2841 static void
2842 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2843 {
2844 if (per_cu->is_debug_types)
2845 load_full_type_unit (per_cu);
2846 else
2847 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2848
2849 if (per_cu->cu == NULL)
2850 return; /* Dummy CU. */
2851
2852 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2853 }
2854
2855 /* Read in the symbols for PER_CU. */
2856
2857 static void
2858 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2859 {
2860 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2861
2862 /* Skip type_unit_groups, reading the type units they contain
2863 is handled elsewhere. */
2864 if (IS_TYPE_UNIT_GROUP (per_cu))
2865 return;
2866
2867 /* The destructor of dwarf2_queue_guard frees any entries left on
2868 the queue. After this point we're guaranteed to leave this function
2869 with the dwarf queue empty. */
2870 dwarf2_queue_guard q_guard;
2871
2872 if (dwarf2_per_objfile->using_index
2873 ? per_cu->v.quick->compunit_symtab == NULL
2874 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2875 {
2876 queue_comp_unit (per_cu, language_minimal);
2877 load_cu (per_cu, skip_partial);
2878
2879 /* If we just loaded a CU from a DWO, and we're working with an index
2880 that may badly handle TUs, load all the TUs in that DWO as well.
2881 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2882 if (!per_cu->is_debug_types
2883 && per_cu->cu != NULL
2884 && per_cu->cu->dwo_unit != NULL
2885 && dwarf2_per_objfile->index_table != NULL
2886 && dwarf2_per_objfile->index_table->version <= 7
2887 /* DWP files aren't supported yet. */
2888 && get_dwp_file (dwarf2_per_objfile) == NULL)
2889 queue_and_load_all_dwo_tus (per_cu);
2890 }
2891
2892 process_queue (dwarf2_per_objfile);
2893
2894 /* Age the cache, releasing compilation units that have not
2895 been used recently. */
2896 age_cached_comp_units (dwarf2_per_objfile);
2897 }
2898
2899 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2900 the objfile from which this CU came. Returns the resulting symbol
2901 table. */
2902
2903 static struct compunit_symtab *
2904 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2905 {
2906 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2907
2908 gdb_assert (dwarf2_per_objfile->using_index);
2909 if (!per_cu->v.quick->compunit_symtab)
2910 {
2911 free_cached_comp_units freer (dwarf2_per_objfile);
2912 scoped_restore decrementer = increment_reading_symtab ();
2913 dw2_do_instantiate_symtab (per_cu, skip_partial);
2914 process_cu_includes (dwarf2_per_objfile);
2915 }
2916
2917 return per_cu->v.quick->compunit_symtab;
2918 }
2919
2920 /* See declaration. */
2921
2922 dwarf2_per_cu_data *
2923 dwarf2_per_objfile::get_cutu (int index)
2924 {
2925 if (index >= this->all_comp_units.size ())
2926 {
2927 index -= this->all_comp_units.size ();
2928 gdb_assert (index < this->all_type_units.size ());
2929 return &this->all_type_units[index]->per_cu;
2930 }
2931
2932 return this->all_comp_units[index];
2933 }
2934
2935 /* See declaration. */
2936
2937 dwarf2_per_cu_data *
2938 dwarf2_per_objfile::get_cu (int index)
2939 {
2940 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2941
2942 return this->all_comp_units[index];
2943 }
2944
2945 /* See declaration. */
2946
2947 signatured_type *
2948 dwarf2_per_objfile::get_tu (int index)
2949 {
2950 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2951
2952 return this->all_type_units[index];
2953 }
2954
2955 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2956 objfile_obstack, and constructed with the specified field
2957 values. */
2958
2959 static dwarf2_per_cu_data *
2960 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2961 struct dwarf2_section_info *section,
2962 int is_dwz,
2963 sect_offset sect_off, ULONGEST length)
2964 {
2965 struct objfile *objfile = dwarf2_per_objfile->objfile;
2966 dwarf2_per_cu_data *the_cu
2967 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2968 struct dwarf2_per_cu_data);
2969 the_cu->sect_off = sect_off;
2970 the_cu->length = length;
2971 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2972 the_cu->section = section;
2973 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2974 struct dwarf2_per_cu_quick_data);
2975 the_cu->is_dwz = is_dwz;
2976 return the_cu;
2977 }
2978
2979 /* A helper for create_cus_from_index that handles a given list of
2980 CUs. */
2981
2982 static void
2983 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2984 const gdb_byte *cu_list, offset_type n_elements,
2985 struct dwarf2_section_info *section,
2986 int is_dwz)
2987 {
2988 for (offset_type i = 0; i < n_elements; i += 2)
2989 {
2990 gdb_static_assert (sizeof (ULONGEST) >= 8);
2991
2992 sect_offset sect_off
2993 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2994 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2995 cu_list += 2 * 8;
2996
2997 dwarf2_per_cu_data *per_cu
2998 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2999 sect_off, length);
3000 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
3001 }
3002 }
3003
3004 /* Read the CU list from the mapped index, and use it to create all
3005 the CU objects for this objfile. */
3006
3007 static void
3008 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3009 const gdb_byte *cu_list, offset_type cu_list_elements,
3010 const gdb_byte *dwz_list, offset_type dwz_elements)
3011 {
3012 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3013 dwarf2_per_objfile->all_comp_units.reserve
3014 ((cu_list_elements + dwz_elements) / 2);
3015
3016 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
3017 &dwarf2_per_objfile->info, 0);
3018
3019 if (dwz_elements == 0)
3020 return;
3021
3022 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3023 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
3024 &dwz->info, 1);
3025 }
3026
3027 /* Create the signatured type hash table from the index. */
3028
3029 static void
3030 create_signatured_type_table_from_index
3031 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3032 struct dwarf2_section_info *section,
3033 const gdb_byte *bytes,
3034 offset_type elements)
3035 {
3036 struct objfile *objfile = dwarf2_per_objfile->objfile;
3037
3038 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3039 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
3040
3041 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3042
3043 for (offset_type i = 0; i < elements; i += 3)
3044 {
3045 struct signatured_type *sig_type;
3046 ULONGEST signature;
3047 void **slot;
3048 cu_offset type_offset_in_tu;
3049
3050 gdb_static_assert (sizeof (ULONGEST) >= 8);
3051 sect_offset sect_off
3052 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3053 type_offset_in_tu
3054 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3055 BFD_ENDIAN_LITTLE);
3056 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3057 bytes += 3 * 8;
3058
3059 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3060 struct signatured_type);
3061 sig_type->signature = signature;
3062 sig_type->type_offset_in_tu = type_offset_in_tu;
3063 sig_type->per_cu.is_debug_types = 1;
3064 sig_type->per_cu.section = section;
3065 sig_type->per_cu.sect_off = sect_off;
3066 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3067 sig_type->per_cu.v.quick
3068 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3069 struct dwarf2_per_cu_quick_data);
3070
3071 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3072 *slot = sig_type;
3073
3074 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3075 }
3076
3077 dwarf2_per_objfile->signatured_types = sig_types_hash;
3078 }
3079
3080 /* Create the signatured type hash table from .debug_names. */
3081
3082 static void
3083 create_signatured_type_table_from_debug_names
3084 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3085 const mapped_debug_names &map,
3086 struct dwarf2_section_info *section,
3087 struct dwarf2_section_info *abbrev_section)
3088 {
3089 struct objfile *objfile = dwarf2_per_objfile->objfile;
3090
3091 dwarf2_read_section (objfile, section);
3092 dwarf2_read_section (objfile, abbrev_section);
3093
3094 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3095 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
3096
3097 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3098
3099 for (uint32_t i = 0; i < map.tu_count; ++i)
3100 {
3101 struct signatured_type *sig_type;
3102 void **slot;
3103
3104 sect_offset sect_off
3105 = (sect_offset) (extract_unsigned_integer
3106 (map.tu_table_reordered + i * map.offset_size,
3107 map.offset_size,
3108 map.dwarf5_byte_order));
3109
3110 comp_unit_head cu_header;
3111 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3112 abbrev_section,
3113 section->buffer + to_underlying (sect_off),
3114 rcuh_kind::TYPE);
3115
3116 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3117 struct signatured_type);
3118 sig_type->signature = cu_header.signature;
3119 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3120 sig_type->per_cu.is_debug_types = 1;
3121 sig_type->per_cu.section = section;
3122 sig_type->per_cu.sect_off = sect_off;
3123 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3124 sig_type->per_cu.v.quick
3125 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3126 struct dwarf2_per_cu_quick_data);
3127
3128 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3129 *slot = sig_type;
3130
3131 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3132 }
3133
3134 dwarf2_per_objfile->signatured_types = sig_types_hash;
3135 }
3136
3137 /* Read the address map data from the mapped index, and use it to
3138 populate the objfile's psymtabs_addrmap. */
3139
3140 static void
3141 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3142 struct mapped_index *index)
3143 {
3144 struct objfile *objfile = dwarf2_per_objfile->objfile;
3145 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3146 const gdb_byte *iter, *end;
3147 struct addrmap *mutable_map;
3148 CORE_ADDR baseaddr;
3149
3150 auto_obstack temp_obstack;
3151
3152 mutable_map = addrmap_create_mutable (&temp_obstack);
3153
3154 iter = index->address_table.data ();
3155 end = iter + index->address_table.size ();
3156
3157 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3158
3159 while (iter < end)
3160 {
3161 ULONGEST hi, lo, cu_index;
3162 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3163 iter += 8;
3164 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3165 iter += 8;
3166 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3167 iter += 4;
3168
3169 if (lo > hi)
3170 {
3171 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
3172 hex_string (lo), hex_string (hi));
3173 continue;
3174 }
3175
3176 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
3177 {
3178 complaint (_(".gdb_index address table has invalid CU number %u"),
3179 (unsigned) cu_index);
3180 continue;
3181 }
3182
3183 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3184 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
3185 addrmap_set_empty (mutable_map, lo, hi - 1,
3186 dwarf2_per_objfile->get_cu (cu_index));
3187 }
3188
3189 objfile->partial_symtabs->psymtabs_addrmap
3190 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3191 }
3192
3193 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3194 populate the objfile's psymtabs_addrmap. */
3195
3196 static void
3197 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3198 struct dwarf2_section_info *section)
3199 {
3200 struct objfile *objfile = dwarf2_per_objfile->objfile;
3201 bfd *abfd = objfile->obfd;
3202 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3203 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3204 SECT_OFF_TEXT (objfile));
3205
3206 auto_obstack temp_obstack;
3207 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3208
3209 std::unordered_map<sect_offset,
3210 dwarf2_per_cu_data *,
3211 gdb::hash_enum<sect_offset>>
3212 debug_info_offset_to_per_cu;
3213 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3214 {
3215 const auto insertpair
3216 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3217 if (!insertpair.second)
3218 {
3219 warning (_("Section .debug_aranges in %s has duplicate "
3220 "debug_info_offset %s, ignoring .debug_aranges."),
3221 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3222 return;
3223 }
3224 }
3225
3226 dwarf2_read_section (objfile, section);
3227
3228 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3229
3230 const gdb_byte *addr = section->buffer;
3231
3232 while (addr < section->buffer + section->size)
3233 {
3234 const gdb_byte *const entry_addr = addr;
3235 unsigned int bytes_read;
3236
3237 const LONGEST entry_length = read_initial_length (abfd, addr,
3238 &bytes_read);
3239 addr += bytes_read;
3240
3241 const gdb_byte *const entry_end = addr + entry_length;
3242 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3243 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3244 if (addr + entry_length > section->buffer + section->size)
3245 {
3246 warning (_("Section .debug_aranges in %s entry at offset %s "
3247 "length %s exceeds section length %s, "
3248 "ignoring .debug_aranges."),
3249 objfile_name (objfile),
3250 plongest (entry_addr - section->buffer),
3251 plongest (bytes_read + entry_length),
3252 pulongest (section->size));
3253 return;
3254 }
3255
3256 /* The version number. */
3257 const uint16_t version = read_2_bytes (abfd, addr);
3258 addr += 2;
3259 if (version != 2)
3260 {
3261 warning (_("Section .debug_aranges in %s entry at offset %s "
3262 "has unsupported version %d, ignoring .debug_aranges."),
3263 objfile_name (objfile),
3264 plongest (entry_addr - section->buffer), version);
3265 return;
3266 }
3267
3268 const uint64_t debug_info_offset
3269 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3270 addr += offset_size;
3271 const auto per_cu_it
3272 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3273 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3274 {
3275 warning (_("Section .debug_aranges in %s entry at offset %s "
3276 "debug_info_offset %s does not exists, "
3277 "ignoring .debug_aranges."),
3278 objfile_name (objfile),
3279 plongest (entry_addr - section->buffer),
3280 pulongest (debug_info_offset));
3281 return;
3282 }
3283 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3284
3285 const uint8_t address_size = *addr++;
3286 if (address_size < 1 || address_size > 8)
3287 {
3288 warning (_("Section .debug_aranges in %s entry at offset %s "
3289 "address_size %u is invalid, ignoring .debug_aranges."),
3290 objfile_name (objfile),
3291 plongest (entry_addr - section->buffer), address_size);
3292 return;
3293 }
3294
3295 const uint8_t segment_selector_size = *addr++;
3296 if (segment_selector_size != 0)
3297 {
3298 warning (_("Section .debug_aranges in %s entry at offset %s "
3299 "segment_selector_size %u is not supported, "
3300 "ignoring .debug_aranges."),
3301 objfile_name (objfile),
3302 plongest (entry_addr - section->buffer),
3303 segment_selector_size);
3304 return;
3305 }
3306
3307 /* Must pad to an alignment boundary that is twice the address
3308 size. It is undocumented by the DWARF standard but GCC does
3309 use it. */
3310 for (size_t padding = ((-(addr - section->buffer))
3311 & (2 * address_size - 1));
3312 padding > 0; padding--)
3313 if (*addr++ != 0)
3314 {
3315 warning (_("Section .debug_aranges in %s entry at offset %s "
3316 "padding is not zero, ignoring .debug_aranges."),
3317 objfile_name (objfile),
3318 plongest (entry_addr - section->buffer));
3319 return;
3320 }
3321
3322 for (;;)
3323 {
3324 if (addr + 2 * address_size > entry_end)
3325 {
3326 warning (_("Section .debug_aranges in %s entry at offset %s "
3327 "address list is not properly terminated, "
3328 "ignoring .debug_aranges."),
3329 objfile_name (objfile),
3330 plongest (entry_addr - section->buffer));
3331 return;
3332 }
3333 ULONGEST start = extract_unsigned_integer (addr, address_size,
3334 dwarf5_byte_order);
3335 addr += address_size;
3336 ULONGEST length = extract_unsigned_integer (addr, address_size,
3337 dwarf5_byte_order);
3338 addr += address_size;
3339 if (start == 0 && length == 0)
3340 break;
3341 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3342 {
3343 /* Symbol was eliminated due to a COMDAT group. */
3344 continue;
3345 }
3346 ULONGEST end = start + length;
3347 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3348 - baseaddr);
3349 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3350 - baseaddr);
3351 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3352 }
3353 }
3354
3355 objfile->partial_symtabs->psymtabs_addrmap
3356 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3357 }
3358
3359 /* Find a slot in the mapped index INDEX for the object named NAME.
3360 If NAME is found, set *VEC_OUT to point to the CU vector in the
3361 constant pool and return true. If NAME cannot be found, return
3362 false. */
3363
3364 static bool
3365 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3366 offset_type **vec_out)
3367 {
3368 offset_type hash;
3369 offset_type slot, step;
3370 int (*cmp) (const char *, const char *);
3371
3372 gdb::unique_xmalloc_ptr<char> without_params;
3373 if (current_language->la_language == language_cplus
3374 || current_language->la_language == language_fortran
3375 || current_language->la_language == language_d)
3376 {
3377 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3378 not contain any. */
3379
3380 if (strchr (name, '(') != NULL)
3381 {
3382 without_params = cp_remove_params (name);
3383
3384 if (without_params != NULL)
3385 name = without_params.get ();
3386 }
3387 }
3388
3389 /* Index version 4 did not support case insensitive searches. But the
3390 indices for case insensitive languages are built in lowercase, therefore
3391 simulate our NAME being searched is also lowercased. */
3392 hash = mapped_index_string_hash ((index->version == 4
3393 && case_sensitivity == case_sensitive_off
3394 ? 5 : index->version),
3395 name);
3396
3397 slot = hash & (index->symbol_table.size () - 1);
3398 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3399 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3400
3401 for (;;)
3402 {
3403 const char *str;
3404
3405 const auto &bucket = index->symbol_table[slot];
3406 if (bucket.name == 0 && bucket.vec == 0)
3407 return false;
3408
3409 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3410 if (!cmp (name, str))
3411 {
3412 *vec_out = (offset_type *) (index->constant_pool
3413 + MAYBE_SWAP (bucket.vec));
3414 return true;
3415 }
3416
3417 slot = (slot + step) & (index->symbol_table.size () - 1);
3418 }
3419 }
3420
3421 /* A helper function that reads the .gdb_index from BUFFER and fills
3422 in MAP. FILENAME is the name of the file containing the data;
3423 it is used for error reporting. DEPRECATED_OK is true if it is
3424 ok to use deprecated sections.
3425
3426 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3427 out parameters that are filled in with information about the CU and
3428 TU lists in the section.
3429
3430 Returns true if all went well, false otherwise. */
3431
3432 static bool
3433 read_gdb_index_from_buffer (struct objfile *objfile,
3434 const char *filename,
3435 bool deprecated_ok,
3436 gdb::array_view<const gdb_byte> buffer,
3437 struct mapped_index *map,
3438 const gdb_byte **cu_list,
3439 offset_type *cu_list_elements,
3440 const gdb_byte **types_list,
3441 offset_type *types_list_elements)
3442 {
3443 const gdb_byte *addr = &buffer[0];
3444
3445 /* Version check. */
3446 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3447 /* Versions earlier than 3 emitted every copy of a psymbol. This
3448 causes the index to behave very poorly for certain requests. Version 3
3449 contained incomplete addrmap. So, it seems better to just ignore such
3450 indices. */
3451 if (version < 4)
3452 {
3453 static int warning_printed = 0;
3454 if (!warning_printed)
3455 {
3456 warning (_("Skipping obsolete .gdb_index section in %s."),
3457 filename);
3458 warning_printed = 1;
3459 }
3460 return 0;
3461 }
3462 /* Index version 4 uses a different hash function than index version
3463 5 and later.
3464
3465 Versions earlier than 6 did not emit psymbols for inlined
3466 functions. Using these files will cause GDB not to be able to
3467 set breakpoints on inlined functions by name, so we ignore these
3468 indices unless the user has done
3469 "set use-deprecated-index-sections on". */
3470 if (version < 6 && !deprecated_ok)
3471 {
3472 static int warning_printed = 0;
3473 if (!warning_printed)
3474 {
3475 warning (_("\
3476 Skipping deprecated .gdb_index section in %s.\n\
3477 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3478 to use the section anyway."),
3479 filename);
3480 warning_printed = 1;
3481 }
3482 return 0;
3483 }
3484 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3485 of the TU (for symbols coming from TUs),
3486 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3487 Plus gold-generated indices can have duplicate entries for global symbols,
3488 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3489 These are just performance bugs, and we can't distinguish gdb-generated
3490 indices from gold-generated ones, so issue no warning here. */
3491
3492 /* Indexes with higher version than the one supported by GDB may be no
3493 longer backward compatible. */
3494 if (version > 8)
3495 return 0;
3496
3497 map->version = version;
3498
3499 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3500
3501 int i = 0;
3502 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3503 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3504 / 8);
3505 ++i;
3506
3507 *types_list = addr + MAYBE_SWAP (metadata[i]);
3508 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3509 - MAYBE_SWAP (metadata[i]))
3510 / 8);
3511 ++i;
3512
3513 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3514 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3515 map->address_table
3516 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3517 ++i;
3518
3519 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3520 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3521 map->symbol_table
3522 = gdb::array_view<mapped_index::symbol_table_slot>
3523 ((mapped_index::symbol_table_slot *) symbol_table,
3524 (mapped_index::symbol_table_slot *) symbol_table_end);
3525
3526 ++i;
3527 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3528
3529 return 1;
3530 }
3531
3532 /* Callback types for dwarf2_read_gdb_index. */
3533
3534 typedef gdb::function_view
3535 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3536 get_gdb_index_contents_ftype;
3537 typedef gdb::function_view
3538 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3539 get_gdb_index_contents_dwz_ftype;
3540
3541 /* Read .gdb_index. If everything went ok, initialize the "quick"
3542 elements of all the CUs and return 1. Otherwise, return 0. */
3543
3544 static int
3545 dwarf2_read_gdb_index
3546 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3547 get_gdb_index_contents_ftype get_gdb_index_contents,
3548 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3549 {
3550 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3551 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3552 struct dwz_file *dwz;
3553 struct objfile *objfile = dwarf2_per_objfile->objfile;
3554
3555 gdb::array_view<const gdb_byte> main_index_contents
3556 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3557
3558 if (main_index_contents.empty ())
3559 return 0;
3560
3561 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3562 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3563 use_deprecated_index_sections,
3564 main_index_contents, map.get (), &cu_list,
3565 &cu_list_elements, &types_list,
3566 &types_list_elements))
3567 return 0;
3568
3569 /* Don't use the index if it's empty. */
3570 if (map->symbol_table.empty ())
3571 return 0;
3572
3573 /* If there is a .dwz file, read it so we can get its CU list as
3574 well. */
3575 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3576 if (dwz != NULL)
3577 {
3578 struct mapped_index dwz_map;
3579 const gdb_byte *dwz_types_ignore;
3580 offset_type dwz_types_elements_ignore;
3581
3582 gdb::array_view<const gdb_byte> dwz_index_content
3583 = get_gdb_index_contents_dwz (objfile, dwz);
3584
3585 if (dwz_index_content.empty ())
3586 return 0;
3587
3588 if (!read_gdb_index_from_buffer (objfile,
3589 bfd_get_filename (dwz->dwz_bfd.get ()),
3590 1, dwz_index_content, &dwz_map,
3591 &dwz_list, &dwz_list_elements,
3592 &dwz_types_ignore,
3593 &dwz_types_elements_ignore))
3594 {
3595 warning (_("could not read '.gdb_index' section from %s; skipping"),
3596 bfd_get_filename (dwz->dwz_bfd.get ()));
3597 return 0;
3598 }
3599 }
3600
3601 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3602 dwz_list, dwz_list_elements);
3603
3604 if (types_list_elements)
3605 {
3606 /* We can only handle a single .debug_types when we have an
3607 index. */
3608 if (dwarf2_per_objfile->types.size () != 1)
3609 return 0;
3610
3611 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3612
3613 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3614 types_list, types_list_elements);
3615 }
3616
3617 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3618
3619 dwarf2_per_objfile->index_table = std::move (map);
3620 dwarf2_per_objfile->using_index = 1;
3621 dwarf2_per_objfile->quick_file_names_table =
3622 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3623
3624 return 1;
3625 }
3626
3627 /* die_reader_func for dw2_get_file_names. */
3628
3629 static void
3630 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3631 const gdb_byte *info_ptr,
3632 struct die_info *comp_unit_die,
3633 int has_children,
3634 void *data)
3635 {
3636 struct dwarf2_cu *cu = reader->cu;
3637 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3638 struct dwarf2_per_objfile *dwarf2_per_objfile
3639 = cu->per_cu->dwarf2_per_objfile;
3640 struct objfile *objfile = dwarf2_per_objfile->objfile;
3641 struct dwarf2_per_cu_data *lh_cu;
3642 struct attribute *attr;
3643 int i;
3644 void **slot;
3645 struct quick_file_names *qfn;
3646
3647 gdb_assert (! this_cu->is_debug_types);
3648
3649 /* Our callers never want to match partial units -- instead they
3650 will match the enclosing full CU. */
3651 if (comp_unit_die->tag == DW_TAG_partial_unit)
3652 {
3653 this_cu->v.quick->no_file_data = 1;
3654 return;
3655 }
3656
3657 lh_cu = this_cu;
3658 slot = NULL;
3659
3660 line_header_up lh;
3661 sect_offset line_offset {};
3662
3663 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3664 if (attr)
3665 {
3666 struct quick_file_names find_entry;
3667
3668 line_offset = (sect_offset) DW_UNSND (attr);
3669
3670 /* We may have already read in this line header (TU line header sharing).
3671 If we have we're done. */
3672 find_entry.hash.dwo_unit = cu->dwo_unit;
3673 find_entry.hash.line_sect_off = line_offset;
3674 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3675 &find_entry, INSERT);
3676 if (*slot != NULL)
3677 {
3678 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3679 return;
3680 }
3681
3682 lh = dwarf_decode_line_header (line_offset, cu);
3683 }
3684 if (lh == NULL)
3685 {
3686 lh_cu->v.quick->no_file_data = 1;
3687 return;
3688 }
3689
3690 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3691 qfn->hash.dwo_unit = cu->dwo_unit;
3692 qfn->hash.line_sect_off = line_offset;
3693 gdb_assert (slot != NULL);
3694 *slot = qfn;
3695
3696 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3697
3698 int offset = 0;
3699 if (strcmp (fnd.name, "<unknown>") != 0)
3700 ++offset;
3701
3702 qfn->num_file_names = offset + lh->file_names.size ();
3703 qfn->file_names =
3704 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3705 if (offset != 0)
3706 qfn->file_names[0] = xstrdup (fnd.name);
3707 for (i = 0; i < lh->file_names.size (); ++i)
3708 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3709 qfn->real_names = NULL;
3710
3711 lh_cu->v.quick->file_names = qfn;
3712 }
3713
3714 /* A helper for the "quick" functions which attempts to read the line
3715 table for THIS_CU. */
3716
3717 static struct quick_file_names *
3718 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3719 {
3720 /* This should never be called for TUs. */
3721 gdb_assert (! this_cu->is_debug_types);
3722 /* Nor type unit groups. */
3723 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3724
3725 if (this_cu->v.quick->file_names != NULL)
3726 return this_cu->v.quick->file_names;
3727 /* If we know there is no line data, no point in looking again. */
3728 if (this_cu->v.quick->no_file_data)
3729 return NULL;
3730
3731 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
3732
3733 if (this_cu->v.quick->no_file_data)
3734 return NULL;
3735 return this_cu->v.quick->file_names;
3736 }
3737
3738 /* A helper for the "quick" functions which computes and caches the
3739 real path for a given file name from the line table. */
3740
3741 static const char *
3742 dw2_get_real_path (struct objfile *objfile,
3743 struct quick_file_names *qfn, int index)
3744 {
3745 if (qfn->real_names == NULL)
3746 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3747 qfn->num_file_names, const char *);
3748
3749 if (qfn->real_names[index] == NULL)
3750 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3751
3752 return qfn->real_names[index];
3753 }
3754
3755 static struct symtab *
3756 dw2_find_last_source_symtab (struct objfile *objfile)
3757 {
3758 struct dwarf2_per_objfile *dwarf2_per_objfile
3759 = get_dwarf2_per_objfile (objfile);
3760 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3761 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3762
3763 if (cust == NULL)
3764 return NULL;
3765
3766 return compunit_primary_filetab (cust);
3767 }
3768
3769 /* Traversal function for dw2_forget_cached_source_info. */
3770
3771 static int
3772 dw2_free_cached_file_names (void **slot, void *info)
3773 {
3774 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3775
3776 if (file_data->real_names)
3777 {
3778 int i;
3779
3780 for (i = 0; i < file_data->num_file_names; ++i)
3781 {
3782 xfree ((void*) file_data->real_names[i]);
3783 file_data->real_names[i] = NULL;
3784 }
3785 }
3786
3787 return 1;
3788 }
3789
3790 static void
3791 dw2_forget_cached_source_info (struct objfile *objfile)
3792 {
3793 struct dwarf2_per_objfile *dwarf2_per_objfile
3794 = get_dwarf2_per_objfile (objfile);
3795
3796 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3797 dw2_free_cached_file_names, NULL);
3798 }
3799
3800 /* Helper function for dw2_map_symtabs_matching_filename that expands
3801 the symtabs and calls the iterator. */
3802
3803 static int
3804 dw2_map_expand_apply (struct objfile *objfile,
3805 struct dwarf2_per_cu_data *per_cu,
3806 const char *name, const char *real_path,
3807 gdb::function_view<bool (symtab *)> callback)
3808 {
3809 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3810
3811 /* Don't visit already-expanded CUs. */
3812 if (per_cu->v.quick->compunit_symtab)
3813 return 0;
3814
3815 /* This may expand more than one symtab, and we want to iterate over
3816 all of them. */
3817 dw2_instantiate_symtab (per_cu, false);
3818
3819 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3820 last_made, callback);
3821 }
3822
3823 /* Implementation of the map_symtabs_matching_filename method. */
3824
3825 static bool
3826 dw2_map_symtabs_matching_filename
3827 (struct objfile *objfile, const char *name, const char *real_path,
3828 gdb::function_view<bool (symtab *)> callback)
3829 {
3830 const char *name_basename = lbasename (name);
3831 struct dwarf2_per_objfile *dwarf2_per_objfile
3832 = get_dwarf2_per_objfile (objfile);
3833
3834 /* The rule is CUs specify all the files, including those used by
3835 any TU, so there's no need to scan TUs here. */
3836
3837 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3838 {
3839 /* We only need to look at symtabs not already expanded. */
3840 if (per_cu->v.quick->compunit_symtab)
3841 continue;
3842
3843 quick_file_names *file_data = dw2_get_file_names (per_cu);
3844 if (file_data == NULL)
3845 continue;
3846
3847 for (int j = 0; j < file_data->num_file_names; ++j)
3848 {
3849 const char *this_name = file_data->file_names[j];
3850 const char *this_real_name;
3851
3852 if (compare_filenames_for_search (this_name, name))
3853 {
3854 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3855 callback))
3856 return true;
3857 continue;
3858 }
3859
3860 /* Before we invoke realpath, which can get expensive when many
3861 files are involved, do a quick comparison of the basenames. */
3862 if (! basenames_may_differ
3863 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3864 continue;
3865
3866 this_real_name = dw2_get_real_path (objfile, file_data, j);
3867 if (compare_filenames_for_search (this_real_name, name))
3868 {
3869 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3870 callback))
3871 return true;
3872 continue;
3873 }
3874
3875 if (real_path != NULL)
3876 {
3877 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3878 gdb_assert (IS_ABSOLUTE_PATH (name));
3879 if (this_real_name != NULL
3880 && FILENAME_CMP (real_path, this_real_name) == 0)
3881 {
3882 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3883 callback))
3884 return true;
3885 continue;
3886 }
3887 }
3888 }
3889 }
3890
3891 return false;
3892 }
3893
3894 /* Struct used to manage iterating over all CUs looking for a symbol. */
3895
3896 struct dw2_symtab_iterator
3897 {
3898 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3899 struct dwarf2_per_objfile *dwarf2_per_objfile;
3900 /* If set, only look for symbols that match that block. Valid values are
3901 GLOBAL_BLOCK and STATIC_BLOCK. */
3902 gdb::optional<block_enum> block_index;
3903 /* The kind of symbol we're looking for. */
3904 domain_enum domain;
3905 /* The list of CUs from the index entry of the symbol,
3906 or NULL if not found. */
3907 offset_type *vec;
3908 /* The next element in VEC to look at. */
3909 int next;
3910 /* The number of elements in VEC, or zero if there is no match. */
3911 int length;
3912 /* Have we seen a global version of the symbol?
3913 If so we can ignore all further global instances.
3914 This is to work around gold/15646, inefficient gold-generated
3915 indices. */
3916 int global_seen;
3917 };
3918
3919 /* Initialize the index symtab iterator ITER. */
3920
3921 static void
3922 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3923 struct dwarf2_per_objfile *dwarf2_per_objfile,
3924 gdb::optional<block_enum> block_index,
3925 domain_enum domain,
3926 const char *name)
3927 {
3928 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3929 iter->block_index = block_index;
3930 iter->domain = domain;
3931 iter->next = 0;
3932 iter->global_seen = 0;
3933
3934 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3935
3936 /* index is NULL if OBJF_READNOW. */
3937 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3938 iter->length = MAYBE_SWAP (*iter->vec);
3939 else
3940 {
3941 iter->vec = NULL;
3942 iter->length = 0;
3943 }
3944 }
3945
3946 /* Return the next matching CU or NULL if there are no more. */
3947
3948 static struct dwarf2_per_cu_data *
3949 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3950 {
3951 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3952
3953 for ( ; iter->next < iter->length; ++iter->next)
3954 {
3955 offset_type cu_index_and_attrs =
3956 MAYBE_SWAP (iter->vec[iter->next + 1]);
3957 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3958 gdb_index_symbol_kind symbol_kind =
3959 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3960 /* Only check the symbol attributes if they're present.
3961 Indices prior to version 7 don't record them,
3962 and indices >= 7 may elide them for certain symbols
3963 (gold does this). */
3964 int attrs_valid =
3965 (dwarf2_per_objfile->index_table->version >= 7
3966 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3967
3968 /* Don't crash on bad data. */
3969 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3970 + dwarf2_per_objfile->all_type_units.size ()))
3971 {
3972 complaint (_(".gdb_index entry has bad CU index"
3973 " [in module %s]"),
3974 objfile_name (dwarf2_per_objfile->objfile));
3975 continue;
3976 }
3977
3978 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3979
3980 /* Skip if already read in. */
3981 if (per_cu->v.quick->compunit_symtab)
3982 continue;
3983
3984 /* Check static vs global. */
3985 if (attrs_valid)
3986 {
3987 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3988
3989 if (iter->block_index.has_value ())
3990 {
3991 bool want_static = *iter->block_index == STATIC_BLOCK;
3992
3993 if (is_static != want_static)
3994 continue;
3995 }
3996
3997 /* Work around gold/15646. */
3998 if (!is_static && iter->global_seen)
3999 continue;
4000 if (!is_static)
4001 iter->global_seen = 1;
4002 }
4003
4004 /* Only check the symbol's kind if it has one. */
4005 if (attrs_valid)
4006 {
4007 switch (iter->domain)
4008 {
4009 case VAR_DOMAIN:
4010 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4011 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4012 /* Some types are also in VAR_DOMAIN. */
4013 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4014 continue;
4015 break;
4016 case STRUCT_DOMAIN:
4017 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4018 continue;
4019 break;
4020 case LABEL_DOMAIN:
4021 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4022 continue;
4023 break;
4024 default:
4025 break;
4026 }
4027 }
4028
4029 ++iter->next;
4030 return per_cu;
4031 }
4032
4033 return NULL;
4034 }
4035
4036 static struct compunit_symtab *
4037 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
4038 const char *name, domain_enum domain)
4039 {
4040 struct compunit_symtab *stab_best = NULL;
4041 struct dwarf2_per_objfile *dwarf2_per_objfile
4042 = get_dwarf2_per_objfile (objfile);
4043
4044 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4045
4046 struct dw2_symtab_iterator iter;
4047 struct dwarf2_per_cu_data *per_cu;
4048
4049 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
4050
4051 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4052 {
4053 struct symbol *sym, *with_opaque = NULL;
4054 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
4055 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4056 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4057
4058 sym = block_find_symbol (block, name, domain,
4059 block_find_non_opaque_type_preferred,
4060 &with_opaque);
4061
4062 /* Some caution must be observed with overloaded functions
4063 and methods, since the index will not contain any overload
4064 information (but NAME might contain it). */
4065
4066 if (sym != NULL
4067 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4068 return stab;
4069 if (with_opaque != NULL
4070 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4071 stab_best = stab;
4072
4073 /* Keep looking through other CUs. */
4074 }
4075
4076 return stab_best;
4077 }
4078
4079 static void
4080 dw2_print_stats (struct objfile *objfile)
4081 {
4082 struct dwarf2_per_objfile *dwarf2_per_objfile
4083 = get_dwarf2_per_objfile (objfile);
4084 int total = (dwarf2_per_objfile->all_comp_units.size ()
4085 + dwarf2_per_objfile->all_type_units.size ());
4086 int count = 0;
4087
4088 for (int i = 0; i < total; ++i)
4089 {
4090 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4091
4092 if (!per_cu->v.quick->compunit_symtab)
4093 ++count;
4094 }
4095 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4096 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4097 }
4098
4099 /* This dumps minimal information about the index.
4100 It is called via "mt print objfiles".
4101 One use is to verify .gdb_index has been loaded by the
4102 gdb.dwarf2/gdb-index.exp testcase. */
4103
4104 static void
4105 dw2_dump (struct objfile *objfile)
4106 {
4107 struct dwarf2_per_objfile *dwarf2_per_objfile
4108 = get_dwarf2_per_objfile (objfile);
4109
4110 gdb_assert (dwarf2_per_objfile->using_index);
4111 printf_filtered (".gdb_index:");
4112 if (dwarf2_per_objfile->index_table != NULL)
4113 {
4114 printf_filtered (" version %d\n",
4115 dwarf2_per_objfile->index_table->version);
4116 }
4117 else
4118 printf_filtered (" faked for \"readnow\"\n");
4119 printf_filtered ("\n");
4120 }
4121
4122 static void
4123 dw2_expand_symtabs_for_function (struct objfile *objfile,
4124 const char *func_name)
4125 {
4126 struct dwarf2_per_objfile *dwarf2_per_objfile
4127 = get_dwarf2_per_objfile (objfile);
4128
4129 struct dw2_symtab_iterator iter;
4130 struct dwarf2_per_cu_data *per_cu;
4131
4132 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
4133
4134 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4135 dw2_instantiate_symtab (per_cu, false);
4136
4137 }
4138
4139 static void
4140 dw2_expand_all_symtabs (struct objfile *objfile)
4141 {
4142 struct dwarf2_per_objfile *dwarf2_per_objfile
4143 = get_dwarf2_per_objfile (objfile);
4144 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
4145 + dwarf2_per_objfile->all_type_units.size ());
4146
4147 for (int i = 0; i < total_units; ++i)
4148 {
4149 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4150
4151 /* We don't want to directly expand a partial CU, because if we
4152 read it with the wrong language, then assertion failures can
4153 be triggered later on. See PR symtab/23010. So, tell
4154 dw2_instantiate_symtab to skip partial CUs -- any important
4155 partial CU will be read via DW_TAG_imported_unit anyway. */
4156 dw2_instantiate_symtab (per_cu, true);
4157 }
4158 }
4159
4160 static void
4161 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4162 const char *fullname)
4163 {
4164 struct dwarf2_per_objfile *dwarf2_per_objfile
4165 = get_dwarf2_per_objfile (objfile);
4166
4167 /* We don't need to consider type units here.
4168 This is only called for examining code, e.g. expand_line_sal.
4169 There can be an order of magnitude (or more) more type units
4170 than comp units, and we avoid them if we can. */
4171
4172 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4173 {
4174 /* We only need to look at symtabs not already expanded. */
4175 if (per_cu->v.quick->compunit_symtab)
4176 continue;
4177
4178 quick_file_names *file_data = dw2_get_file_names (per_cu);
4179 if (file_data == NULL)
4180 continue;
4181
4182 for (int j = 0; j < file_data->num_file_names; ++j)
4183 {
4184 const char *this_fullname = file_data->file_names[j];
4185
4186 if (filename_cmp (this_fullname, fullname) == 0)
4187 {
4188 dw2_instantiate_symtab (per_cu, false);
4189 break;
4190 }
4191 }
4192 }
4193 }
4194
4195 static void
4196 dw2_map_matching_symbols
4197 (struct objfile *objfile,
4198 const lookup_name_info &name, domain_enum domain,
4199 int global,
4200 gdb::function_view<symbol_found_callback_ftype> callback,
4201 symbol_compare_ftype *ordered_compare)
4202 {
4203 /* Currently unimplemented; used for Ada. The function can be called if the
4204 current language is Ada for a non-Ada objfile using GNU index. As Ada
4205 does not look for non-Ada symbols this function should just return. */
4206 }
4207
4208 /* Starting from a search name, return the string that finds the upper
4209 bound of all strings that start with SEARCH_NAME in a sorted name
4210 list. Returns the empty string to indicate that the upper bound is
4211 the end of the list. */
4212
4213 static std::string
4214 make_sort_after_prefix_name (const char *search_name)
4215 {
4216 /* When looking to complete "func", we find the upper bound of all
4217 symbols that start with "func" by looking for where we'd insert
4218 the closest string that would follow "func" in lexicographical
4219 order. Usually, that's "func"-with-last-character-incremented,
4220 i.e. "fund". Mind non-ASCII characters, though. Usually those
4221 will be UTF-8 multi-byte sequences, but we can't be certain.
4222 Especially mind the 0xff character, which is a valid character in
4223 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4224 rule out compilers allowing it in identifiers. Note that
4225 conveniently, strcmp/strcasecmp are specified to compare
4226 characters interpreted as unsigned char. So what we do is treat
4227 the whole string as a base 256 number composed of a sequence of
4228 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4229 to 0, and carries 1 to the following more-significant position.
4230 If the very first character in SEARCH_NAME ends up incremented
4231 and carries/overflows, then the upper bound is the end of the
4232 list. The string after the empty string is also the empty
4233 string.
4234
4235 Some examples of this operation:
4236
4237 SEARCH_NAME => "+1" RESULT
4238
4239 "abc" => "abd"
4240 "ab\xff" => "ac"
4241 "\xff" "a" "\xff" => "\xff" "b"
4242 "\xff" => ""
4243 "\xff\xff" => ""
4244 "" => ""
4245
4246 Then, with these symbols for example:
4247
4248 func
4249 func1
4250 fund
4251
4252 completing "func" looks for symbols between "func" and
4253 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4254 which finds "func" and "func1", but not "fund".
4255
4256 And with:
4257
4258 funcÿ (Latin1 'ÿ' [0xff])
4259 funcÿ1
4260 fund
4261
4262 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4263 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4264
4265 And with:
4266
4267 ÿÿ (Latin1 'ÿ' [0xff])
4268 ÿÿ1
4269
4270 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4271 the end of the list.
4272 */
4273 std::string after = search_name;
4274 while (!after.empty () && (unsigned char) after.back () == 0xff)
4275 after.pop_back ();
4276 if (!after.empty ())
4277 after.back () = (unsigned char) after.back () + 1;
4278 return after;
4279 }
4280
4281 /* See declaration. */
4282
4283 std::pair<std::vector<name_component>::const_iterator,
4284 std::vector<name_component>::const_iterator>
4285 mapped_index_base::find_name_components_bounds
4286 (const lookup_name_info &lookup_name_without_params, language lang) const
4287 {
4288 auto *name_cmp
4289 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4290
4291 const char *lang_name
4292 = lookup_name_without_params.language_lookup_name (lang).c_str ();
4293
4294 /* Comparison function object for lower_bound that matches against a
4295 given symbol name. */
4296 auto lookup_compare_lower = [&] (const name_component &elem,
4297 const char *name)
4298 {
4299 const char *elem_qualified = this->symbol_name_at (elem.idx);
4300 const char *elem_name = elem_qualified + elem.name_offset;
4301 return name_cmp (elem_name, name) < 0;
4302 };
4303
4304 /* Comparison function object for upper_bound that matches against a
4305 given symbol name. */
4306 auto lookup_compare_upper = [&] (const char *name,
4307 const name_component &elem)
4308 {
4309 const char *elem_qualified = this->symbol_name_at (elem.idx);
4310 const char *elem_name = elem_qualified + elem.name_offset;
4311 return name_cmp (name, elem_name) < 0;
4312 };
4313
4314 auto begin = this->name_components.begin ();
4315 auto end = this->name_components.end ();
4316
4317 /* Find the lower bound. */
4318 auto lower = [&] ()
4319 {
4320 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4321 return begin;
4322 else
4323 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4324 } ();
4325
4326 /* Find the upper bound. */
4327 auto upper = [&] ()
4328 {
4329 if (lookup_name_without_params.completion_mode ())
4330 {
4331 /* In completion mode, we want UPPER to point past all
4332 symbols names that have the same prefix. I.e., with
4333 these symbols, and completing "func":
4334
4335 function << lower bound
4336 function1
4337 other_function << upper bound
4338
4339 We find the upper bound by looking for the insertion
4340 point of "func"-with-last-character-incremented,
4341 i.e. "fund". */
4342 std::string after = make_sort_after_prefix_name (lang_name);
4343 if (after.empty ())
4344 return end;
4345 return std::lower_bound (lower, end, after.c_str (),
4346 lookup_compare_lower);
4347 }
4348 else
4349 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4350 } ();
4351
4352 return {lower, upper};
4353 }
4354
4355 /* See declaration. */
4356
4357 void
4358 mapped_index_base::build_name_components ()
4359 {
4360 if (!this->name_components.empty ())
4361 return;
4362
4363 this->name_components_casing = case_sensitivity;
4364 auto *name_cmp
4365 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4366
4367 /* The code below only knows how to break apart components of C++
4368 symbol names (and other languages that use '::' as
4369 namespace/module separator) and Ada symbol names. */
4370 auto count = this->symbol_name_count ();
4371 for (offset_type idx = 0; idx < count; idx++)
4372 {
4373 if (this->symbol_name_slot_invalid (idx))
4374 continue;
4375
4376 const char *name = this->symbol_name_at (idx);
4377
4378 /* Add each name component to the name component table. */
4379 unsigned int previous_len = 0;
4380
4381 if (strstr (name, "::") != nullptr)
4382 {
4383 for (unsigned int current_len = cp_find_first_component (name);
4384 name[current_len] != '\0';
4385 current_len += cp_find_first_component (name + current_len))
4386 {
4387 gdb_assert (name[current_len] == ':');
4388 this->name_components.push_back ({previous_len, idx});
4389 /* Skip the '::'. */
4390 current_len += 2;
4391 previous_len = current_len;
4392 }
4393 }
4394 else
4395 {
4396 /* Handle the Ada encoded (aka mangled) form here. */
4397 for (const char *iter = strstr (name, "__");
4398 iter != nullptr;
4399 iter = strstr (iter, "__"))
4400 {
4401 this->name_components.push_back ({previous_len, idx});
4402 iter += 2;
4403 previous_len = iter - name;
4404 }
4405 }
4406
4407 this->name_components.push_back ({previous_len, idx});
4408 }
4409
4410 /* Sort name_components elements by name. */
4411 auto name_comp_compare = [&] (const name_component &left,
4412 const name_component &right)
4413 {
4414 const char *left_qualified = this->symbol_name_at (left.idx);
4415 const char *right_qualified = this->symbol_name_at (right.idx);
4416
4417 const char *left_name = left_qualified + left.name_offset;
4418 const char *right_name = right_qualified + right.name_offset;
4419
4420 return name_cmp (left_name, right_name) < 0;
4421 };
4422
4423 std::sort (this->name_components.begin (),
4424 this->name_components.end (),
4425 name_comp_compare);
4426 }
4427
4428 /* Helper for dw2_expand_symtabs_matching that works with a
4429 mapped_index_base instead of the containing objfile. This is split
4430 to a separate function in order to be able to unit test the
4431 name_components matching using a mock mapped_index_base. For each
4432 symbol name that matches, calls MATCH_CALLBACK, passing it the
4433 symbol's index in the mapped_index_base symbol table. */
4434
4435 static void
4436 dw2_expand_symtabs_matching_symbol
4437 (mapped_index_base &index,
4438 const lookup_name_info &lookup_name_in,
4439 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4440 enum search_domain kind,
4441 gdb::function_view<bool (offset_type)> match_callback)
4442 {
4443 lookup_name_info lookup_name_without_params
4444 = lookup_name_in.make_ignore_params ();
4445
4446 /* Build the symbol name component sorted vector, if we haven't
4447 yet. */
4448 index.build_name_components ();
4449
4450 /* The same symbol may appear more than once in the range though.
4451 E.g., if we're looking for symbols that complete "w", and we have
4452 a symbol named "w1::w2", we'll find the two name components for
4453 that same symbol in the range. To be sure we only call the
4454 callback once per symbol, we first collect the symbol name
4455 indexes that matched in a temporary vector and ignore
4456 duplicates. */
4457 std::vector<offset_type> matches;
4458
4459 struct name_and_matcher
4460 {
4461 symbol_name_matcher_ftype *matcher;
4462 const std::string &name;
4463
4464 bool operator== (const name_and_matcher &other) const
4465 {
4466 return matcher == other.matcher && name == other.name;
4467 }
4468 };
4469
4470 /* A vector holding all the different symbol name matchers, for all
4471 languages. */
4472 std::vector<name_and_matcher> matchers;
4473
4474 for (int i = 0; i < nr_languages; i++)
4475 {
4476 enum language lang_e = (enum language) i;
4477
4478 const language_defn *lang = language_def (lang_e);
4479 symbol_name_matcher_ftype *name_matcher
4480 = get_symbol_name_matcher (lang, lookup_name_without_params);
4481
4482 name_and_matcher key {
4483 name_matcher,
4484 lookup_name_without_params.language_lookup_name (lang_e)
4485 };
4486
4487 /* Don't insert the same comparison routine more than once.
4488 Note that we do this linear walk. This is not a problem in
4489 practice because the number of supported languages is
4490 low. */
4491 if (std::find (matchers.begin (), matchers.end (), key)
4492 != matchers.end ())
4493 continue;
4494 matchers.push_back (std::move (key));
4495
4496 auto bounds
4497 = index.find_name_components_bounds (lookup_name_without_params,
4498 lang_e);
4499
4500 /* Now for each symbol name in range, check to see if we have a name
4501 match, and if so, call the MATCH_CALLBACK callback. */
4502
4503 for (; bounds.first != bounds.second; ++bounds.first)
4504 {
4505 const char *qualified = index.symbol_name_at (bounds.first->idx);
4506
4507 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4508 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4509 continue;
4510
4511 matches.push_back (bounds.first->idx);
4512 }
4513 }
4514
4515 std::sort (matches.begin (), matches.end ());
4516
4517 /* Finally call the callback, once per match. */
4518 ULONGEST prev = -1;
4519 for (offset_type idx : matches)
4520 {
4521 if (prev != idx)
4522 {
4523 if (!match_callback (idx))
4524 break;
4525 prev = idx;
4526 }
4527 }
4528
4529 /* Above we use a type wider than idx's for 'prev', since 0 and
4530 (offset_type)-1 are both possible values. */
4531 static_assert (sizeof (prev) > sizeof (offset_type), "");
4532 }
4533
4534 #if GDB_SELF_TEST
4535
4536 namespace selftests { namespace dw2_expand_symtabs_matching {
4537
4538 /* A mock .gdb_index/.debug_names-like name index table, enough to
4539 exercise dw2_expand_symtabs_matching_symbol, which works with the
4540 mapped_index_base interface. Builds an index from the symbol list
4541 passed as parameter to the constructor. */
4542 class mock_mapped_index : public mapped_index_base
4543 {
4544 public:
4545 mock_mapped_index (gdb::array_view<const char *> symbols)
4546 : m_symbol_table (symbols)
4547 {}
4548
4549 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4550
4551 /* Return the number of names in the symbol table. */
4552 size_t symbol_name_count () const override
4553 {
4554 return m_symbol_table.size ();
4555 }
4556
4557 /* Get the name of the symbol at IDX in the symbol table. */
4558 const char *symbol_name_at (offset_type idx) const override
4559 {
4560 return m_symbol_table[idx];
4561 }
4562
4563 private:
4564 gdb::array_view<const char *> m_symbol_table;
4565 };
4566
4567 /* Convenience function that converts a NULL pointer to a "<null>"
4568 string, to pass to print routines. */
4569
4570 static const char *
4571 string_or_null (const char *str)
4572 {
4573 return str != NULL ? str : "<null>";
4574 }
4575
4576 /* Check if a lookup_name_info built from
4577 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4578 index. EXPECTED_LIST is the list of expected matches, in expected
4579 matching order. If no match expected, then an empty list is
4580 specified. Returns true on success. On failure prints a warning
4581 indicating the file:line that failed, and returns false. */
4582
4583 static bool
4584 check_match (const char *file, int line,
4585 mock_mapped_index &mock_index,
4586 const char *name, symbol_name_match_type match_type,
4587 bool completion_mode,
4588 std::initializer_list<const char *> expected_list)
4589 {
4590 lookup_name_info lookup_name (name, match_type, completion_mode);
4591
4592 bool matched = true;
4593
4594 auto mismatch = [&] (const char *expected_str,
4595 const char *got)
4596 {
4597 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4598 "expected=\"%s\", got=\"%s\"\n"),
4599 file, line,
4600 (match_type == symbol_name_match_type::FULL
4601 ? "FULL" : "WILD"),
4602 name, string_or_null (expected_str), string_or_null (got));
4603 matched = false;
4604 };
4605
4606 auto expected_it = expected_list.begin ();
4607 auto expected_end = expected_list.end ();
4608
4609 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4610 NULL, ALL_DOMAIN,
4611 [&] (offset_type idx)
4612 {
4613 const char *matched_name = mock_index.symbol_name_at (idx);
4614 const char *expected_str
4615 = expected_it == expected_end ? NULL : *expected_it++;
4616
4617 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4618 mismatch (expected_str, matched_name);
4619 return true;
4620 });
4621
4622 const char *expected_str
4623 = expected_it == expected_end ? NULL : *expected_it++;
4624 if (expected_str != NULL)
4625 mismatch (expected_str, NULL);
4626
4627 return matched;
4628 }
4629
4630 /* The symbols added to the mock mapped_index for testing (in
4631 canonical form). */
4632 static const char *test_symbols[] = {
4633 "function",
4634 "std::bar",
4635 "std::zfunction",
4636 "std::zfunction2",
4637 "w1::w2",
4638 "ns::foo<char*>",
4639 "ns::foo<int>",
4640 "ns::foo<long>",
4641 "ns2::tmpl<int>::foo2",
4642 "(anonymous namespace)::A::B::C",
4643
4644 /* These are used to check that the increment-last-char in the
4645 matching algorithm for completion doesn't match "t1_fund" when
4646 completing "t1_func". */
4647 "t1_func",
4648 "t1_func1",
4649 "t1_fund",
4650 "t1_fund1",
4651
4652 /* A UTF-8 name with multi-byte sequences to make sure that
4653 cp-name-parser understands this as a single identifier ("função"
4654 is "function" in PT). */
4655 u8"u8função",
4656
4657 /* \377 (0xff) is Latin1 'ÿ'. */
4658 "yfunc\377",
4659
4660 /* \377 (0xff) is Latin1 'ÿ'. */
4661 "\377",
4662 "\377\377123",
4663
4664 /* A name with all sorts of complications. Starts with "z" to make
4665 it easier for the completion tests below. */
4666 #define Z_SYM_NAME \
4667 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4668 "::tuple<(anonymous namespace)::ui*, " \
4669 "std::default_delete<(anonymous namespace)::ui>, void>"
4670
4671 Z_SYM_NAME
4672 };
4673
4674 /* Returns true if the mapped_index_base::find_name_component_bounds
4675 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4676 in completion mode. */
4677
4678 static bool
4679 check_find_bounds_finds (mapped_index_base &index,
4680 const char *search_name,
4681 gdb::array_view<const char *> expected_syms)
4682 {
4683 lookup_name_info lookup_name (search_name,
4684 symbol_name_match_type::FULL, true);
4685
4686 auto bounds = index.find_name_components_bounds (lookup_name,
4687 language_cplus);
4688
4689 size_t distance = std::distance (bounds.first, bounds.second);
4690 if (distance != expected_syms.size ())
4691 return false;
4692
4693 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4694 {
4695 auto nc_elem = bounds.first + exp_elem;
4696 const char *qualified = index.symbol_name_at (nc_elem->idx);
4697 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4698 return false;
4699 }
4700
4701 return true;
4702 }
4703
4704 /* Test the lower-level mapped_index::find_name_component_bounds
4705 method. */
4706
4707 static void
4708 test_mapped_index_find_name_component_bounds ()
4709 {
4710 mock_mapped_index mock_index (test_symbols);
4711
4712 mock_index.build_name_components ();
4713
4714 /* Test the lower-level mapped_index::find_name_component_bounds
4715 method in completion mode. */
4716 {
4717 static const char *expected_syms[] = {
4718 "t1_func",
4719 "t1_func1",
4720 };
4721
4722 SELF_CHECK (check_find_bounds_finds (mock_index,
4723 "t1_func", expected_syms));
4724 }
4725
4726 /* Check that the increment-last-char in the name matching algorithm
4727 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4728 {
4729 static const char *expected_syms1[] = {
4730 "\377",
4731 "\377\377123",
4732 };
4733 SELF_CHECK (check_find_bounds_finds (mock_index,
4734 "\377", expected_syms1));
4735
4736 static const char *expected_syms2[] = {
4737 "\377\377123",
4738 };
4739 SELF_CHECK (check_find_bounds_finds (mock_index,
4740 "\377\377", expected_syms2));
4741 }
4742 }
4743
4744 /* Test dw2_expand_symtabs_matching_symbol. */
4745
4746 static void
4747 test_dw2_expand_symtabs_matching_symbol ()
4748 {
4749 mock_mapped_index mock_index (test_symbols);
4750
4751 /* We let all tests run until the end even if some fails, for debug
4752 convenience. */
4753 bool any_mismatch = false;
4754
4755 /* Create the expected symbols list (an initializer_list). Needed
4756 because lists have commas, and we need to pass them to CHECK,
4757 which is a macro. */
4758 #define EXPECT(...) { __VA_ARGS__ }
4759
4760 /* Wrapper for check_match that passes down the current
4761 __FILE__/__LINE__. */
4762 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4763 any_mismatch |= !check_match (__FILE__, __LINE__, \
4764 mock_index, \
4765 NAME, MATCH_TYPE, COMPLETION_MODE, \
4766 EXPECTED_LIST)
4767
4768 /* Identity checks. */
4769 for (const char *sym : test_symbols)
4770 {
4771 /* Should be able to match all existing symbols. */
4772 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4773 EXPECT (sym));
4774
4775 /* Should be able to match all existing symbols with
4776 parameters. */
4777 std::string with_params = std::string (sym) + "(int)";
4778 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4779 EXPECT (sym));
4780
4781 /* Should be able to match all existing symbols with
4782 parameters and qualifiers. */
4783 with_params = std::string (sym) + " ( int ) const";
4784 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4785 EXPECT (sym));
4786
4787 /* This should really find sym, but cp-name-parser.y doesn't
4788 know about lvalue/rvalue qualifiers yet. */
4789 with_params = std::string (sym) + " ( int ) &&";
4790 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4791 {});
4792 }
4793
4794 /* Check that the name matching algorithm for completion doesn't get
4795 confused with Latin1 'ÿ' / 0xff. */
4796 {
4797 static const char str[] = "\377";
4798 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4799 EXPECT ("\377", "\377\377123"));
4800 }
4801
4802 /* Check that the increment-last-char in the matching algorithm for
4803 completion doesn't match "t1_fund" when completing "t1_func". */
4804 {
4805 static const char str[] = "t1_func";
4806 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4807 EXPECT ("t1_func", "t1_func1"));
4808 }
4809
4810 /* Check that completion mode works at each prefix of the expected
4811 symbol name. */
4812 {
4813 static const char str[] = "function(int)";
4814 size_t len = strlen (str);
4815 std::string lookup;
4816
4817 for (size_t i = 1; i < len; i++)
4818 {
4819 lookup.assign (str, i);
4820 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4821 EXPECT ("function"));
4822 }
4823 }
4824
4825 /* While "w" is a prefix of both components, the match function
4826 should still only be called once. */
4827 {
4828 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4829 EXPECT ("w1::w2"));
4830 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4831 EXPECT ("w1::w2"));
4832 }
4833
4834 /* Same, with a "complicated" symbol. */
4835 {
4836 static const char str[] = Z_SYM_NAME;
4837 size_t len = strlen (str);
4838 std::string lookup;
4839
4840 for (size_t i = 1; i < len; i++)
4841 {
4842 lookup.assign (str, i);
4843 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4844 EXPECT (Z_SYM_NAME));
4845 }
4846 }
4847
4848 /* In FULL mode, an incomplete symbol doesn't match. */
4849 {
4850 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4851 {});
4852 }
4853
4854 /* A complete symbol with parameters matches any overload, since the
4855 index has no overload info. */
4856 {
4857 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4858 EXPECT ("std::zfunction", "std::zfunction2"));
4859 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4860 EXPECT ("std::zfunction", "std::zfunction2"));
4861 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4862 EXPECT ("std::zfunction", "std::zfunction2"));
4863 }
4864
4865 /* Check that whitespace is ignored appropriately. A symbol with a
4866 template argument list. */
4867 {
4868 static const char expected[] = "ns::foo<int>";
4869 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4870 EXPECT (expected));
4871 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4872 EXPECT (expected));
4873 }
4874
4875 /* Check that whitespace is ignored appropriately. A symbol with a
4876 template argument list that includes a pointer. */
4877 {
4878 static const char expected[] = "ns::foo<char*>";
4879 /* Try both completion and non-completion modes. */
4880 static const bool completion_mode[2] = {false, true};
4881 for (size_t i = 0; i < 2; i++)
4882 {
4883 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4884 completion_mode[i], EXPECT (expected));
4885 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4886 completion_mode[i], EXPECT (expected));
4887
4888 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4889 completion_mode[i], EXPECT (expected));
4890 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4891 completion_mode[i], EXPECT (expected));
4892 }
4893 }
4894
4895 {
4896 /* Check method qualifiers are ignored. */
4897 static const char expected[] = "ns::foo<char*>";
4898 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4899 symbol_name_match_type::FULL, true, EXPECT (expected));
4900 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4901 symbol_name_match_type::FULL, true, EXPECT (expected));
4902 CHECK_MATCH ("foo < char * > ( int ) const",
4903 symbol_name_match_type::WILD, true, EXPECT (expected));
4904 CHECK_MATCH ("foo < char * > ( int ) &&",
4905 symbol_name_match_type::WILD, true, EXPECT (expected));
4906 }
4907
4908 /* Test lookup names that don't match anything. */
4909 {
4910 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4911 {});
4912
4913 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4914 {});
4915 }
4916
4917 /* Some wild matching tests, exercising "(anonymous namespace)",
4918 which should not be confused with a parameter list. */
4919 {
4920 static const char *syms[] = {
4921 "A::B::C",
4922 "B::C",
4923 "C",
4924 "A :: B :: C ( int )",
4925 "B :: C ( int )",
4926 "C ( int )",
4927 };
4928
4929 for (const char *s : syms)
4930 {
4931 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4932 EXPECT ("(anonymous namespace)::A::B::C"));
4933 }
4934 }
4935
4936 {
4937 static const char expected[] = "ns2::tmpl<int>::foo2";
4938 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4939 EXPECT (expected));
4940 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4941 EXPECT (expected));
4942 }
4943
4944 SELF_CHECK (!any_mismatch);
4945
4946 #undef EXPECT
4947 #undef CHECK_MATCH
4948 }
4949
4950 static void
4951 run_test ()
4952 {
4953 test_mapped_index_find_name_component_bounds ();
4954 test_dw2_expand_symtabs_matching_symbol ();
4955 }
4956
4957 }} // namespace selftests::dw2_expand_symtabs_matching
4958
4959 #endif /* GDB_SELF_TEST */
4960
4961 /* If FILE_MATCHER is NULL or if PER_CU has
4962 dwarf2_per_cu_quick_data::MARK set (see
4963 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4964 EXPANSION_NOTIFY on it. */
4965
4966 static void
4967 dw2_expand_symtabs_matching_one
4968 (struct dwarf2_per_cu_data *per_cu,
4969 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4970 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4971 {
4972 if (file_matcher == NULL || per_cu->v.quick->mark)
4973 {
4974 bool symtab_was_null
4975 = (per_cu->v.quick->compunit_symtab == NULL);
4976
4977 dw2_instantiate_symtab (per_cu, false);
4978
4979 if (expansion_notify != NULL
4980 && symtab_was_null
4981 && per_cu->v.quick->compunit_symtab != NULL)
4982 expansion_notify (per_cu->v.quick->compunit_symtab);
4983 }
4984 }
4985
4986 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4987 matched, to expand corresponding CUs that were marked. IDX is the
4988 index of the symbol name that matched. */
4989
4990 static void
4991 dw2_expand_marked_cus
4992 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4993 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4994 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4995 search_domain kind)
4996 {
4997 offset_type *vec, vec_len, vec_idx;
4998 bool global_seen = false;
4999 mapped_index &index = *dwarf2_per_objfile->index_table;
5000
5001 vec = (offset_type *) (index.constant_pool
5002 + MAYBE_SWAP (index.symbol_table[idx].vec));
5003 vec_len = MAYBE_SWAP (vec[0]);
5004 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5005 {
5006 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5007 /* This value is only valid for index versions >= 7. */
5008 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5009 gdb_index_symbol_kind symbol_kind =
5010 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5011 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5012 /* Only check the symbol attributes if they're present.
5013 Indices prior to version 7 don't record them,
5014 and indices >= 7 may elide them for certain symbols
5015 (gold does this). */
5016 int attrs_valid =
5017 (index.version >= 7
5018 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5019
5020 /* Work around gold/15646. */
5021 if (attrs_valid)
5022 {
5023 if (!is_static && global_seen)
5024 continue;
5025 if (!is_static)
5026 global_seen = true;
5027 }
5028
5029 /* Only check the symbol's kind if it has one. */
5030 if (attrs_valid)
5031 {
5032 switch (kind)
5033 {
5034 case VARIABLES_DOMAIN:
5035 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5036 continue;
5037 break;
5038 case FUNCTIONS_DOMAIN:
5039 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5040 continue;
5041 break;
5042 case TYPES_DOMAIN:
5043 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5044 continue;
5045 break;
5046 default:
5047 break;
5048 }
5049 }
5050
5051 /* Don't crash on bad data. */
5052 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
5053 + dwarf2_per_objfile->all_type_units.size ()))
5054 {
5055 complaint (_(".gdb_index entry has bad CU index"
5056 " [in module %s]"),
5057 objfile_name (dwarf2_per_objfile->objfile));
5058 continue;
5059 }
5060
5061 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
5062 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5063 expansion_notify);
5064 }
5065 }
5066
5067 /* If FILE_MATCHER is non-NULL, set all the
5068 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5069 that match FILE_MATCHER. */
5070
5071 static void
5072 dw_expand_symtabs_matching_file_matcher
5073 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5074 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5075 {
5076 if (file_matcher == NULL)
5077 return;
5078
5079 objfile *const objfile = dwarf2_per_objfile->objfile;
5080
5081 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5082 htab_eq_pointer,
5083 NULL, xcalloc, xfree));
5084 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5085 htab_eq_pointer,
5086 NULL, xcalloc, xfree));
5087
5088 /* The rule is CUs specify all the files, including those used by
5089 any TU, so there's no need to scan TUs here. */
5090
5091 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5092 {
5093 QUIT;
5094
5095 per_cu->v.quick->mark = 0;
5096
5097 /* We only need to look at symtabs not already expanded. */
5098 if (per_cu->v.quick->compunit_symtab)
5099 continue;
5100
5101 quick_file_names *file_data = dw2_get_file_names (per_cu);
5102 if (file_data == NULL)
5103 continue;
5104
5105 if (htab_find (visited_not_found.get (), file_data) != NULL)
5106 continue;
5107 else if (htab_find (visited_found.get (), file_data) != NULL)
5108 {
5109 per_cu->v.quick->mark = 1;
5110 continue;
5111 }
5112
5113 for (int j = 0; j < file_data->num_file_names; ++j)
5114 {
5115 const char *this_real_name;
5116
5117 if (file_matcher (file_data->file_names[j], false))
5118 {
5119 per_cu->v.quick->mark = 1;
5120 break;
5121 }
5122
5123 /* Before we invoke realpath, which can get expensive when many
5124 files are involved, do a quick comparison of the basenames. */
5125 if (!basenames_may_differ
5126 && !file_matcher (lbasename (file_data->file_names[j]),
5127 true))
5128 continue;
5129
5130 this_real_name = dw2_get_real_path (objfile, file_data, j);
5131 if (file_matcher (this_real_name, false))
5132 {
5133 per_cu->v.quick->mark = 1;
5134 break;
5135 }
5136 }
5137
5138 void **slot = htab_find_slot (per_cu->v.quick->mark
5139 ? visited_found.get ()
5140 : visited_not_found.get (),
5141 file_data, INSERT);
5142 *slot = file_data;
5143 }
5144 }
5145
5146 static void
5147 dw2_expand_symtabs_matching
5148 (struct objfile *objfile,
5149 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5150 const lookup_name_info &lookup_name,
5151 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5152 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5153 enum search_domain kind)
5154 {
5155 struct dwarf2_per_objfile *dwarf2_per_objfile
5156 = get_dwarf2_per_objfile (objfile);
5157
5158 /* index_table is NULL if OBJF_READNOW. */
5159 if (!dwarf2_per_objfile->index_table)
5160 return;
5161
5162 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5163
5164 mapped_index &index = *dwarf2_per_objfile->index_table;
5165
5166 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5167 symbol_matcher,
5168 kind, [&] (offset_type idx)
5169 {
5170 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5171 expansion_notify, kind);
5172 return true;
5173 });
5174 }
5175
5176 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5177 symtab. */
5178
5179 static struct compunit_symtab *
5180 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5181 CORE_ADDR pc)
5182 {
5183 int i;
5184
5185 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5186 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5187 return cust;
5188
5189 if (cust->includes == NULL)
5190 return NULL;
5191
5192 for (i = 0; cust->includes[i]; ++i)
5193 {
5194 struct compunit_symtab *s = cust->includes[i];
5195
5196 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5197 if (s != NULL)
5198 return s;
5199 }
5200
5201 return NULL;
5202 }
5203
5204 static struct compunit_symtab *
5205 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5206 struct bound_minimal_symbol msymbol,
5207 CORE_ADDR pc,
5208 struct obj_section *section,
5209 int warn_if_readin)
5210 {
5211 struct dwarf2_per_cu_data *data;
5212 struct compunit_symtab *result;
5213
5214 if (!objfile->partial_symtabs->psymtabs_addrmap)
5215 return NULL;
5216
5217 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
5218 SECT_OFF_TEXT (objfile));
5219 data = (struct dwarf2_per_cu_data *) addrmap_find
5220 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
5221 if (!data)
5222 return NULL;
5223
5224 if (warn_if_readin && data->v.quick->compunit_symtab)
5225 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5226 paddress (get_objfile_arch (objfile), pc));
5227
5228 result
5229 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5230 false),
5231 pc);
5232 gdb_assert (result != NULL);
5233 return result;
5234 }
5235
5236 static void
5237 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5238 void *data, int need_fullname)
5239 {
5240 struct dwarf2_per_objfile *dwarf2_per_objfile
5241 = get_dwarf2_per_objfile (objfile);
5242
5243 if (!dwarf2_per_objfile->filenames_cache)
5244 {
5245 dwarf2_per_objfile->filenames_cache.emplace ();
5246
5247 htab_up visited (htab_create_alloc (10,
5248 htab_hash_pointer, htab_eq_pointer,
5249 NULL, xcalloc, xfree));
5250
5251 /* The rule is CUs specify all the files, including those used
5252 by any TU, so there's no need to scan TUs here. We can
5253 ignore file names coming from already-expanded CUs. */
5254
5255 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5256 {
5257 if (per_cu->v.quick->compunit_symtab)
5258 {
5259 void **slot = htab_find_slot (visited.get (),
5260 per_cu->v.quick->file_names,
5261 INSERT);
5262
5263 *slot = per_cu->v.quick->file_names;
5264 }
5265 }
5266
5267 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5268 {
5269 /* We only need to look at symtabs not already expanded. */
5270 if (per_cu->v.quick->compunit_symtab)
5271 continue;
5272
5273 quick_file_names *file_data = dw2_get_file_names (per_cu);
5274 if (file_data == NULL)
5275 continue;
5276
5277 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5278 if (*slot)
5279 {
5280 /* Already visited. */
5281 continue;
5282 }
5283 *slot = file_data;
5284
5285 for (int j = 0; j < file_data->num_file_names; ++j)
5286 {
5287 const char *filename = file_data->file_names[j];
5288 dwarf2_per_objfile->filenames_cache->seen (filename);
5289 }
5290 }
5291 }
5292
5293 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5294 {
5295 gdb::unique_xmalloc_ptr<char> this_real_name;
5296
5297 if (need_fullname)
5298 this_real_name = gdb_realpath (filename);
5299 (*fun) (filename, this_real_name.get (), data);
5300 });
5301 }
5302
5303 static int
5304 dw2_has_symbols (struct objfile *objfile)
5305 {
5306 return 1;
5307 }
5308
5309 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5310 {
5311 dw2_has_symbols,
5312 dw2_find_last_source_symtab,
5313 dw2_forget_cached_source_info,
5314 dw2_map_symtabs_matching_filename,
5315 dw2_lookup_symbol,
5316 dw2_print_stats,
5317 dw2_dump,
5318 dw2_expand_symtabs_for_function,
5319 dw2_expand_all_symtabs,
5320 dw2_expand_symtabs_with_fullname,
5321 dw2_map_matching_symbols,
5322 dw2_expand_symtabs_matching,
5323 dw2_find_pc_sect_compunit_symtab,
5324 NULL,
5325 dw2_map_symbol_filenames
5326 };
5327
5328 /* DWARF-5 debug_names reader. */
5329
5330 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5331 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5332
5333 /* A helper function that reads the .debug_names section in SECTION
5334 and fills in MAP. FILENAME is the name of the file containing the
5335 section; it is used for error reporting.
5336
5337 Returns true if all went well, false otherwise. */
5338
5339 static bool
5340 read_debug_names_from_section (struct objfile *objfile,
5341 const char *filename,
5342 struct dwarf2_section_info *section,
5343 mapped_debug_names &map)
5344 {
5345 if (dwarf2_section_empty_p (section))
5346 return false;
5347
5348 /* Older elfutils strip versions could keep the section in the main
5349 executable while splitting it for the separate debug info file. */
5350 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5351 return false;
5352
5353 dwarf2_read_section (objfile, section);
5354
5355 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5356
5357 const gdb_byte *addr = section->buffer;
5358
5359 bfd *const abfd = get_section_bfd_owner (section);
5360
5361 unsigned int bytes_read;
5362 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5363 addr += bytes_read;
5364
5365 map.dwarf5_is_dwarf64 = bytes_read != 4;
5366 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5367 if (bytes_read + length != section->size)
5368 {
5369 /* There may be multiple per-CU indices. */
5370 warning (_("Section .debug_names in %s length %s does not match "
5371 "section length %s, ignoring .debug_names."),
5372 filename, plongest (bytes_read + length),
5373 pulongest (section->size));
5374 return false;
5375 }
5376
5377 /* The version number. */
5378 uint16_t version = read_2_bytes (abfd, addr);
5379 addr += 2;
5380 if (version != 5)
5381 {
5382 warning (_("Section .debug_names in %s has unsupported version %d, "
5383 "ignoring .debug_names."),
5384 filename, version);
5385 return false;
5386 }
5387
5388 /* Padding. */
5389 uint16_t padding = read_2_bytes (abfd, addr);
5390 addr += 2;
5391 if (padding != 0)
5392 {
5393 warning (_("Section .debug_names in %s has unsupported padding %d, "
5394 "ignoring .debug_names."),
5395 filename, padding);
5396 return false;
5397 }
5398
5399 /* comp_unit_count - The number of CUs in the CU list. */
5400 map.cu_count = read_4_bytes (abfd, addr);
5401 addr += 4;
5402
5403 /* local_type_unit_count - The number of TUs in the local TU
5404 list. */
5405 map.tu_count = read_4_bytes (abfd, addr);
5406 addr += 4;
5407
5408 /* foreign_type_unit_count - The number of TUs in the foreign TU
5409 list. */
5410 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5411 addr += 4;
5412 if (foreign_tu_count != 0)
5413 {
5414 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5415 "ignoring .debug_names."),
5416 filename, static_cast<unsigned long> (foreign_tu_count));
5417 return false;
5418 }
5419
5420 /* bucket_count - The number of hash buckets in the hash lookup
5421 table. */
5422 map.bucket_count = read_4_bytes (abfd, addr);
5423 addr += 4;
5424
5425 /* name_count - The number of unique names in the index. */
5426 map.name_count = read_4_bytes (abfd, addr);
5427 addr += 4;
5428
5429 /* abbrev_table_size - The size in bytes of the abbreviations
5430 table. */
5431 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5432 addr += 4;
5433
5434 /* augmentation_string_size - The size in bytes of the augmentation
5435 string. This value is rounded up to a multiple of 4. */
5436 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5437 addr += 4;
5438 map.augmentation_is_gdb = ((augmentation_string_size
5439 == sizeof (dwarf5_augmentation))
5440 && memcmp (addr, dwarf5_augmentation,
5441 sizeof (dwarf5_augmentation)) == 0);
5442 augmentation_string_size += (-augmentation_string_size) & 3;
5443 addr += augmentation_string_size;
5444
5445 /* List of CUs */
5446 map.cu_table_reordered = addr;
5447 addr += map.cu_count * map.offset_size;
5448
5449 /* List of Local TUs */
5450 map.tu_table_reordered = addr;
5451 addr += map.tu_count * map.offset_size;
5452
5453 /* Hash Lookup Table */
5454 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5455 addr += map.bucket_count * 4;
5456 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5457 addr += map.name_count * 4;
5458
5459 /* Name Table */
5460 map.name_table_string_offs_reordered = addr;
5461 addr += map.name_count * map.offset_size;
5462 map.name_table_entry_offs_reordered = addr;
5463 addr += map.name_count * map.offset_size;
5464
5465 const gdb_byte *abbrev_table_start = addr;
5466 for (;;)
5467 {
5468 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5469 addr += bytes_read;
5470 if (index_num == 0)
5471 break;
5472
5473 const auto insertpair
5474 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5475 if (!insertpair.second)
5476 {
5477 warning (_("Section .debug_names in %s has duplicate index %s, "
5478 "ignoring .debug_names."),
5479 filename, pulongest (index_num));
5480 return false;
5481 }
5482 mapped_debug_names::index_val &indexval = insertpair.first->second;
5483 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5484 addr += bytes_read;
5485
5486 for (;;)
5487 {
5488 mapped_debug_names::index_val::attr attr;
5489 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5490 addr += bytes_read;
5491 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5492 addr += bytes_read;
5493 if (attr.form == DW_FORM_implicit_const)
5494 {
5495 attr.implicit_const = read_signed_leb128 (abfd, addr,
5496 &bytes_read);
5497 addr += bytes_read;
5498 }
5499 if (attr.dw_idx == 0 && attr.form == 0)
5500 break;
5501 indexval.attr_vec.push_back (std::move (attr));
5502 }
5503 }
5504 if (addr != abbrev_table_start + abbrev_table_size)
5505 {
5506 warning (_("Section .debug_names in %s has abbreviation_table "
5507 "of size %s vs. written as %u, ignoring .debug_names."),
5508 filename, plongest (addr - abbrev_table_start),
5509 abbrev_table_size);
5510 return false;
5511 }
5512 map.entry_pool = addr;
5513
5514 return true;
5515 }
5516
5517 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5518 list. */
5519
5520 static void
5521 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5522 const mapped_debug_names &map,
5523 dwarf2_section_info &section,
5524 bool is_dwz)
5525 {
5526 sect_offset sect_off_prev;
5527 for (uint32_t i = 0; i <= map.cu_count; ++i)
5528 {
5529 sect_offset sect_off_next;
5530 if (i < map.cu_count)
5531 {
5532 sect_off_next
5533 = (sect_offset) (extract_unsigned_integer
5534 (map.cu_table_reordered + i * map.offset_size,
5535 map.offset_size,
5536 map.dwarf5_byte_order));
5537 }
5538 else
5539 sect_off_next = (sect_offset) section.size;
5540 if (i >= 1)
5541 {
5542 const ULONGEST length = sect_off_next - sect_off_prev;
5543 dwarf2_per_cu_data *per_cu
5544 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5545 sect_off_prev, length);
5546 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5547 }
5548 sect_off_prev = sect_off_next;
5549 }
5550 }
5551
5552 /* Read the CU list from the mapped index, and use it to create all
5553 the CU objects for this dwarf2_per_objfile. */
5554
5555 static void
5556 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5557 const mapped_debug_names &map,
5558 const mapped_debug_names &dwz_map)
5559 {
5560 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5561 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5562
5563 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5564 dwarf2_per_objfile->info,
5565 false /* is_dwz */);
5566
5567 if (dwz_map.cu_count == 0)
5568 return;
5569
5570 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5571 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5572 true /* is_dwz */);
5573 }
5574
5575 /* Read .debug_names. If everything went ok, initialize the "quick"
5576 elements of all the CUs and return true. Otherwise, return false. */
5577
5578 static bool
5579 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5580 {
5581 std::unique_ptr<mapped_debug_names> map
5582 (new mapped_debug_names (dwarf2_per_objfile));
5583 mapped_debug_names dwz_map (dwarf2_per_objfile);
5584 struct objfile *objfile = dwarf2_per_objfile->objfile;
5585
5586 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5587 &dwarf2_per_objfile->debug_names,
5588 *map))
5589 return false;
5590
5591 /* Don't use the index if it's empty. */
5592 if (map->name_count == 0)
5593 return false;
5594
5595 /* If there is a .dwz file, read it so we can get its CU list as
5596 well. */
5597 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5598 if (dwz != NULL)
5599 {
5600 if (!read_debug_names_from_section (objfile,
5601 bfd_get_filename (dwz->dwz_bfd.get ()),
5602 &dwz->debug_names, dwz_map))
5603 {
5604 warning (_("could not read '.debug_names' section from %s; skipping"),
5605 bfd_get_filename (dwz->dwz_bfd.get ()));
5606 return false;
5607 }
5608 }
5609
5610 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5611
5612 if (map->tu_count != 0)
5613 {
5614 /* We can only handle a single .debug_types when we have an
5615 index. */
5616 if (dwarf2_per_objfile->types.size () != 1)
5617 return false;
5618
5619 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5620
5621 create_signatured_type_table_from_debug_names
5622 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5623 }
5624
5625 create_addrmap_from_aranges (dwarf2_per_objfile,
5626 &dwarf2_per_objfile->debug_aranges);
5627
5628 dwarf2_per_objfile->debug_names_table = std::move (map);
5629 dwarf2_per_objfile->using_index = 1;
5630 dwarf2_per_objfile->quick_file_names_table =
5631 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5632
5633 return true;
5634 }
5635
5636 /* Type used to manage iterating over all CUs looking for a symbol for
5637 .debug_names. */
5638
5639 class dw2_debug_names_iterator
5640 {
5641 public:
5642 dw2_debug_names_iterator (const mapped_debug_names &map,
5643 gdb::optional<block_enum> block_index,
5644 domain_enum domain,
5645 const char *name)
5646 : m_map (map), m_block_index (block_index), m_domain (domain),
5647 m_addr (find_vec_in_debug_names (map, name))
5648 {}
5649
5650 dw2_debug_names_iterator (const mapped_debug_names &map,
5651 search_domain search, uint32_t namei)
5652 : m_map (map),
5653 m_search (search),
5654 m_addr (find_vec_in_debug_names (map, namei))
5655 {}
5656
5657 dw2_debug_names_iterator (const mapped_debug_names &map,
5658 block_enum block_index, domain_enum domain,
5659 uint32_t namei)
5660 : m_map (map), m_block_index (block_index), m_domain (domain),
5661 m_addr (find_vec_in_debug_names (map, namei))
5662 {}
5663
5664 /* Return the next matching CU or NULL if there are no more. */
5665 dwarf2_per_cu_data *next ();
5666
5667 private:
5668 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5669 const char *name);
5670 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5671 uint32_t namei);
5672
5673 /* The internalized form of .debug_names. */
5674 const mapped_debug_names &m_map;
5675
5676 /* If set, only look for symbols that match that block. Valid values are
5677 GLOBAL_BLOCK and STATIC_BLOCK. */
5678 const gdb::optional<block_enum> m_block_index;
5679
5680 /* The kind of symbol we're looking for. */
5681 const domain_enum m_domain = UNDEF_DOMAIN;
5682 const search_domain m_search = ALL_DOMAIN;
5683
5684 /* The list of CUs from the index entry of the symbol, or NULL if
5685 not found. */
5686 const gdb_byte *m_addr;
5687 };
5688
5689 const char *
5690 mapped_debug_names::namei_to_name (uint32_t namei) const
5691 {
5692 const ULONGEST namei_string_offs
5693 = extract_unsigned_integer ((name_table_string_offs_reordered
5694 + namei * offset_size),
5695 offset_size,
5696 dwarf5_byte_order);
5697 return read_indirect_string_at_offset
5698 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5699 }
5700
5701 /* Find a slot in .debug_names for the object named NAME. If NAME is
5702 found, return pointer to its pool data. If NAME cannot be found,
5703 return NULL. */
5704
5705 const gdb_byte *
5706 dw2_debug_names_iterator::find_vec_in_debug_names
5707 (const mapped_debug_names &map, const char *name)
5708 {
5709 int (*cmp) (const char *, const char *);
5710
5711 gdb::unique_xmalloc_ptr<char> without_params;
5712 if (current_language->la_language == language_cplus
5713 || current_language->la_language == language_fortran
5714 || current_language->la_language == language_d)
5715 {
5716 /* NAME is already canonical. Drop any qualifiers as
5717 .debug_names does not contain any. */
5718
5719 if (strchr (name, '(') != NULL)
5720 {
5721 without_params = cp_remove_params (name);
5722 if (without_params != NULL)
5723 name = without_params.get ();
5724 }
5725 }
5726
5727 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5728
5729 const uint32_t full_hash = dwarf5_djb_hash (name);
5730 uint32_t namei
5731 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5732 (map.bucket_table_reordered
5733 + (full_hash % map.bucket_count)), 4,
5734 map.dwarf5_byte_order);
5735 if (namei == 0)
5736 return NULL;
5737 --namei;
5738 if (namei >= map.name_count)
5739 {
5740 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5741 "[in module %s]"),
5742 namei, map.name_count,
5743 objfile_name (map.dwarf2_per_objfile->objfile));
5744 return NULL;
5745 }
5746
5747 for (;;)
5748 {
5749 const uint32_t namei_full_hash
5750 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5751 (map.hash_table_reordered + namei), 4,
5752 map.dwarf5_byte_order);
5753 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5754 return NULL;
5755
5756 if (full_hash == namei_full_hash)
5757 {
5758 const char *const namei_string = map.namei_to_name (namei);
5759
5760 #if 0 /* An expensive sanity check. */
5761 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5762 {
5763 complaint (_("Wrong .debug_names hash for string at index %u "
5764 "[in module %s]"),
5765 namei, objfile_name (dwarf2_per_objfile->objfile));
5766 return NULL;
5767 }
5768 #endif
5769
5770 if (cmp (namei_string, name) == 0)
5771 {
5772 const ULONGEST namei_entry_offs
5773 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5774 + namei * map.offset_size),
5775 map.offset_size, map.dwarf5_byte_order);
5776 return map.entry_pool + namei_entry_offs;
5777 }
5778 }
5779
5780 ++namei;
5781 if (namei >= map.name_count)
5782 return NULL;
5783 }
5784 }
5785
5786 const gdb_byte *
5787 dw2_debug_names_iterator::find_vec_in_debug_names
5788 (const mapped_debug_names &map, uint32_t namei)
5789 {
5790 if (namei >= map.name_count)
5791 {
5792 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5793 "[in module %s]"),
5794 namei, map.name_count,
5795 objfile_name (map.dwarf2_per_objfile->objfile));
5796 return NULL;
5797 }
5798
5799 const ULONGEST namei_entry_offs
5800 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5801 + namei * map.offset_size),
5802 map.offset_size, map.dwarf5_byte_order);
5803 return map.entry_pool + namei_entry_offs;
5804 }
5805
5806 /* See dw2_debug_names_iterator. */
5807
5808 dwarf2_per_cu_data *
5809 dw2_debug_names_iterator::next ()
5810 {
5811 if (m_addr == NULL)
5812 return NULL;
5813
5814 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5815 struct objfile *objfile = dwarf2_per_objfile->objfile;
5816 bfd *const abfd = objfile->obfd;
5817
5818 again:
5819
5820 unsigned int bytes_read;
5821 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5822 m_addr += bytes_read;
5823 if (abbrev == 0)
5824 return NULL;
5825
5826 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5827 if (indexval_it == m_map.abbrev_map.cend ())
5828 {
5829 complaint (_("Wrong .debug_names undefined abbrev code %s "
5830 "[in module %s]"),
5831 pulongest (abbrev), objfile_name (objfile));
5832 return NULL;
5833 }
5834 const mapped_debug_names::index_val &indexval = indexval_it->second;
5835 enum class symbol_linkage {
5836 unknown,
5837 static_,
5838 extern_,
5839 } symbol_linkage_ = symbol_linkage::unknown;
5840 dwarf2_per_cu_data *per_cu = NULL;
5841 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5842 {
5843 ULONGEST ull;
5844 switch (attr.form)
5845 {
5846 case DW_FORM_implicit_const:
5847 ull = attr.implicit_const;
5848 break;
5849 case DW_FORM_flag_present:
5850 ull = 1;
5851 break;
5852 case DW_FORM_udata:
5853 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5854 m_addr += bytes_read;
5855 break;
5856 default:
5857 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5858 dwarf_form_name (attr.form),
5859 objfile_name (objfile));
5860 return NULL;
5861 }
5862 switch (attr.dw_idx)
5863 {
5864 case DW_IDX_compile_unit:
5865 /* Don't crash on bad data. */
5866 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5867 {
5868 complaint (_(".debug_names entry has bad CU index %s"
5869 " [in module %s]"),
5870 pulongest (ull),
5871 objfile_name (dwarf2_per_objfile->objfile));
5872 continue;
5873 }
5874 per_cu = dwarf2_per_objfile->get_cutu (ull);
5875 break;
5876 case DW_IDX_type_unit:
5877 /* Don't crash on bad data. */
5878 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5879 {
5880 complaint (_(".debug_names entry has bad TU index %s"
5881 " [in module %s]"),
5882 pulongest (ull),
5883 objfile_name (dwarf2_per_objfile->objfile));
5884 continue;
5885 }
5886 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5887 break;
5888 case DW_IDX_GNU_internal:
5889 if (!m_map.augmentation_is_gdb)
5890 break;
5891 symbol_linkage_ = symbol_linkage::static_;
5892 break;
5893 case DW_IDX_GNU_external:
5894 if (!m_map.augmentation_is_gdb)
5895 break;
5896 symbol_linkage_ = symbol_linkage::extern_;
5897 break;
5898 }
5899 }
5900
5901 /* Skip if already read in. */
5902 if (per_cu->v.quick->compunit_symtab)
5903 goto again;
5904
5905 /* Check static vs global. */
5906 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5907 {
5908 const bool want_static = *m_block_index == STATIC_BLOCK;
5909 const bool symbol_is_static =
5910 symbol_linkage_ == symbol_linkage::static_;
5911 if (want_static != symbol_is_static)
5912 goto again;
5913 }
5914
5915 /* Match dw2_symtab_iter_next, symbol_kind
5916 and debug_names::psymbol_tag. */
5917 switch (m_domain)
5918 {
5919 case VAR_DOMAIN:
5920 switch (indexval.dwarf_tag)
5921 {
5922 case DW_TAG_variable:
5923 case DW_TAG_subprogram:
5924 /* Some types are also in VAR_DOMAIN. */
5925 case DW_TAG_typedef:
5926 case DW_TAG_structure_type:
5927 break;
5928 default:
5929 goto again;
5930 }
5931 break;
5932 case STRUCT_DOMAIN:
5933 switch (indexval.dwarf_tag)
5934 {
5935 case DW_TAG_typedef:
5936 case DW_TAG_structure_type:
5937 break;
5938 default:
5939 goto again;
5940 }
5941 break;
5942 case LABEL_DOMAIN:
5943 switch (indexval.dwarf_tag)
5944 {
5945 case 0:
5946 case DW_TAG_variable:
5947 break;
5948 default:
5949 goto again;
5950 }
5951 break;
5952 default:
5953 break;
5954 }
5955
5956 /* Match dw2_expand_symtabs_matching, symbol_kind and
5957 debug_names::psymbol_tag. */
5958 switch (m_search)
5959 {
5960 case VARIABLES_DOMAIN:
5961 switch (indexval.dwarf_tag)
5962 {
5963 case DW_TAG_variable:
5964 break;
5965 default:
5966 goto again;
5967 }
5968 break;
5969 case FUNCTIONS_DOMAIN:
5970 switch (indexval.dwarf_tag)
5971 {
5972 case DW_TAG_subprogram:
5973 break;
5974 default:
5975 goto again;
5976 }
5977 break;
5978 case TYPES_DOMAIN:
5979 switch (indexval.dwarf_tag)
5980 {
5981 case DW_TAG_typedef:
5982 case DW_TAG_structure_type:
5983 break;
5984 default:
5985 goto again;
5986 }
5987 break;
5988 default:
5989 break;
5990 }
5991
5992 return per_cu;
5993 }
5994
5995 static struct compunit_symtab *
5996 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5997 const char *name, domain_enum domain)
5998 {
5999 struct dwarf2_per_objfile *dwarf2_per_objfile
6000 = get_dwarf2_per_objfile (objfile);
6001
6002 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6003 if (!mapp)
6004 {
6005 /* index is NULL if OBJF_READNOW. */
6006 return NULL;
6007 }
6008 const auto &map = *mapp;
6009
6010 dw2_debug_names_iterator iter (map, block_index, domain, name);
6011
6012 struct compunit_symtab *stab_best = NULL;
6013 struct dwarf2_per_cu_data *per_cu;
6014 while ((per_cu = iter.next ()) != NULL)
6015 {
6016 struct symbol *sym, *with_opaque = NULL;
6017 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
6018 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6019 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6020
6021 sym = block_find_symbol (block, name, domain,
6022 block_find_non_opaque_type_preferred,
6023 &with_opaque);
6024
6025 /* Some caution must be observed with overloaded functions and
6026 methods, since the index will not contain any overload
6027 information (but NAME might contain it). */
6028
6029 if (sym != NULL
6030 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6031 return stab;
6032 if (with_opaque != NULL
6033 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6034 stab_best = stab;
6035
6036 /* Keep looking through other CUs. */
6037 }
6038
6039 return stab_best;
6040 }
6041
6042 /* This dumps minimal information about .debug_names. It is called
6043 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6044 uses this to verify that .debug_names has been loaded. */
6045
6046 static void
6047 dw2_debug_names_dump (struct objfile *objfile)
6048 {
6049 struct dwarf2_per_objfile *dwarf2_per_objfile
6050 = get_dwarf2_per_objfile (objfile);
6051
6052 gdb_assert (dwarf2_per_objfile->using_index);
6053 printf_filtered (".debug_names:");
6054 if (dwarf2_per_objfile->debug_names_table)
6055 printf_filtered (" exists\n");
6056 else
6057 printf_filtered (" faked for \"readnow\"\n");
6058 printf_filtered ("\n");
6059 }
6060
6061 static void
6062 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6063 const char *func_name)
6064 {
6065 struct dwarf2_per_objfile *dwarf2_per_objfile
6066 = get_dwarf2_per_objfile (objfile);
6067
6068 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6069 if (dwarf2_per_objfile->debug_names_table)
6070 {
6071 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6072
6073 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
6074
6075 struct dwarf2_per_cu_data *per_cu;
6076 while ((per_cu = iter.next ()) != NULL)
6077 dw2_instantiate_symtab (per_cu, false);
6078 }
6079 }
6080
6081 static void
6082 dw2_debug_names_map_matching_symbols
6083 (struct objfile *objfile,
6084 const lookup_name_info &name, domain_enum domain,
6085 int global,
6086 gdb::function_view<symbol_found_callback_ftype> callback,
6087 symbol_compare_ftype *ordered_compare)
6088 {
6089 struct dwarf2_per_objfile *dwarf2_per_objfile
6090 = get_dwarf2_per_objfile (objfile);
6091
6092 /* debug_names_table is NULL if OBJF_READNOW. */
6093 if (!dwarf2_per_objfile->debug_names_table)
6094 return;
6095
6096 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6097 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6098
6099 const char *match_name = name.ada ().lookup_name ().c_str ();
6100 auto matcher = [&] (const char *symname)
6101 {
6102 if (ordered_compare == nullptr)
6103 return true;
6104 return ordered_compare (symname, match_name) == 0;
6105 };
6106
6107 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6108 [&] (offset_type namei)
6109 {
6110 /* The name was matched, now expand corresponding CUs that were
6111 marked. */
6112 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6113
6114 struct dwarf2_per_cu_data *per_cu;
6115 while ((per_cu = iter.next ()) != NULL)
6116 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6117 return true;
6118 });
6119
6120 /* It's a shame we couldn't do this inside the
6121 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6122 that have already been expanded. Instead, this loop matches what
6123 the psymtab code does. */
6124 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6125 {
6126 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6127 if (cust != nullptr)
6128 {
6129 const struct block *block
6130 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6131 if (!iterate_over_symbols_terminated (block, name,
6132 domain, callback))
6133 break;
6134 }
6135 }
6136 }
6137
6138 static void
6139 dw2_debug_names_expand_symtabs_matching
6140 (struct objfile *objfile,
6141 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6142 const lookup_name_info &lookup_name,
6143 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6144 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6145 enum search_domain kind)
6146 {
6147 struct dwarf2_per_objfile *dwarf2_per_objfile
6148 = get_dwarf2_per_objfile (objfile);
6149
6150 /* debug_names_table is NULL if OBJF_READNOW. */
6151 if (!dwarf2_per_objfile->debug_names_table)
6152 return;
6153
6154 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6155
6156 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6157
6158 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6159 symbol_matcher,
6160 kind, [&] (offset_type namei)
6161 {
6162 /* The name was matched, now expand corresponding CUs that were
6163 marked. */
6164 dw2_debug_names_iterator iter (map, kind, namei);
6165
6166 struct dwarf2_per_cu_data *per_cu;
6167 while ((per_cu = iter.next ()) != NULL)
6168 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6169 expansion_notify);
6170 return true;
6171 });
6172 }
6173
6174 const struct quick_symbol_functions dwarf2_debug_names_functions =
6175 {
6176 dw2_has_symbols,
6177 dw2_find_last_source_symtab,
6178 dw2_forget_cached_source_info,
6179 dw2_map_symtabs_matching_filename,
6180 dw2_debug_names_lookup_symbol,
6181 dw2_print_stats,
6182 dw2_debug_names_dump,
6183 dw2_debug_names_expand_symtabs_for_function,
6184 dw2_expand_all_symtabs,
6185 dw2_expand_symtabs_with_fullname,
6186 dw2_debug_names_map_matching_symbols,
6187 dw2_debug_names_expand_symtabs_matching,
6188 dw2_find_pc_sect_compunit_symtab,
6189 NULL,
6190 dw2_map_symbol_filenames
6191 };
6192
6193 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6194 to either a dwarf2_per_objfile or dwz_file object. */
6195
6196 template <typename T>
6197 static gdb::array_view<const gdb_byte>
6198 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6199 {
6200 dwarf2_section_info *section = &section_owner->gdb_index;
6201
6202 if (dwarf2_section_empty_p (section))
6203 return {};
6204
6205 /* Older elfutils strip versions could keep the section in the main
6206 executable while splitting it for the separate debug info file. */
6207 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6208 return {};
6209
6210 dwarf2_read_section (obj, section);
6211
6212 /* dwarf2_section_info::size is a bfd_size_type, while
6213 gdb::array_view works with size_t. On 32-bit hosts, with
6214 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6215 is 32-bit. So we need an explicit narrowing conversion here.
6216 This is fine, because it's impossible to allocate or mmap an
6217 array/buffer larger than what size_t can represent. */
6218 return gdb::make_array_view (section->buffer, section->size);
6219 }
6220
6221 /* Lookup the index cache for the contents of the index associated to
6222 DWARF2_OBJ. */
6223
6224 static gdb::array_view<const gdb_byte>
6225 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6226 {
6227 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6228 if (build_id == nullptr)
6229 return {};
6230
6231 return global_index_cache.lookup_gdb_index (build_id,
6232 &dwarf2_obj->index_cache_res);
6233 }
6234
6235 /* Same as the above, but for DWZ. */
6236
6237 static gdb::array_view<const gdb_byte>
6238 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6239 {
6240 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6241 if (build_id == nullptr)
6242 return {};
6243
6244 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6245 }
6246
6247 /* See symfile.h. */
6248
6249 bool
6250 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6251 {
6252 struct dwarf2_per_objfile *dwarf2_per_objfile
6253 = get_dwarf2_per_objfile (objfile);
6254
6255 /* If we're about to read full symbols, don't bother with the
6256 indices. In this case we also don't care if some other debug
6257 format is making psymtabs, because they are all about to be
6258 expanded anyway. */
6259 if ((objfile->flags & OBJF_READNOW))
6260 {
6261 dwarf2_per_objfile->using_index = 1;
6262 create_all_comp_units (dwarf2_per_objfile);
6263 create_all_type_units (dwarf2_per_objfile);
6264 dwarf2_per_objfile->quick_file_names_table
6265 = create_quick_file_names_table
6266 (dwarf2_per_objfile->all_comp_units.size ());
6267
6268 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6269 + dwarf2_per_objfile->all_type_units.size ()); ++i)
6270 {
6271 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6272
6273 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6274 struct dwarf2_per_cu_quick_data);
6275 }
6276
6277 /* Return 1 so that gdb sees the "quick" functions. However,
6278 these functions will be no-ops because we will have expanded
6279 all symtabs. */
6280 *index_kind = dw_index_kind::GDB_INDEX;
6281 return true;
6282 }
6283
6284 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6285 {
6286 *index_kind = dw_index_kind::DEBUG_NAMES;
6287 return true;
6288 }
6289
6290 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6291 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6292 get_gdb_index_contents_from_section<dwz_file>))
6293 {
6294 *index_kind = dw_index_kind::GDB_INDEX;
6295 return true;
6296 }
6297
6298 /* ... otherwise, try to find the index in the index cache. */
6299 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6300 get_gdb_index_contents_from_cache,
6301 get_gdb_index_contents_from_cache_dwz))
6302 {
6303 global_index_cache.hit ();
6304 *index_kind = dw_index_kind::GDB_INDEX;
6305 return true;
6306 }
6307
6308 global_index_cache.miss ();
6309 return false;
6310 }
6311
6312 \f
6313
6314 /* Build a partial symbol table. */
6315
6316 void
6317 dwarf2_build_psymtabs (struct objfile *objfile)
6318 {
6319 struct dwarf2_per_objfile *dwarf2_per_objfile
6320 = get_dwarf2_per_objfile (objfile);
6321
6322 init_psymbol_list (objfile, 1024);
6323
6324 try
6325 {
6326 /* This isn't really ideal: all the data we allocate on the
6327 objfile's obstack is still uselessly kept around. However,
6328 freeing it seems unsafe. */
6329 psymtab_discarder psymtabs (objfile);
6330 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6331 psymtabs.keep ();
6332
6333 /* (maybe) store an index in the cache. */
6334 global_index_cache.store (dwarf2_per_objfile);
6335 }
6336 catch (const gdb_exception_error &except)
6337 {
6338 exception_print (gdb_stderr, except);
6339 }
6340 }
6341
6342 /* Return the total length of the CU described by HEADER. */
6343
6344 static unsigned int
6345 get_cu_length (const struct comp_unit_head *header)
6346 {
6347 return header->initial_length_size + header->length;
6348 }
6349
6350 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6351
6352 static inline bool
6353 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6354 {
6355 sect_offset bottom = cu_header->sect_off;
6356 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6357
6358 return sect_off >= bottom && sect_off < top;
6359 }
6360
6361 /* Find the base address of the compilation unit for range lists and
6362 location lists. It will normally be specified by DW_AT_low_pc.
6363 In DWARF-3 draft 4, the base address could be overridden by
6364 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6365 compilation units with discontinuous ranges. */
6366
6367 static void
6368 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6369 {
6370 struct attribute *attr;
6371
6372 cu->base_known = 0;
6373 cu->base_address = 0;
6374
6375 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6376 if (attr)
6377 {
6378 cu->base_address = attr_value_as_address (attr);
6379 cu->base_known = 1;
6380 }
6381 else
6382 {
6383 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6384 if (attr)
6385 {
6386 cu->base_address = attr_value_as_address (attr);
6387 cu->base_known = 1;
6388 }
6389 }
6390 }
6391
6392 /* Read in the comp unit header information from the debug_info at info_ptr.
6393 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6394 NOTE: This leaves members offset, first_die_offset to be filled in
6395 by the caller. */
6396
6397 static const gdb_byte *
6398 read_comp_unit_head (struct comp_unit_head *cu_header,
6399 const gdb_byte *info_ptr,
6400 struct dwarf2_section_info *section,
6401 rcuh_kind section_kind)
6402 {
6403 int signed_addr;
6404 unsigned int bytes_read;
6405 const char *filename = get_section_file_name (section);
6406 bfd *abfd = get_section_bfd_owner (section);
6407
6408 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6409 cu_header->initial_length_size = bytes_read;
6410 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6411 info_ptr += bytes_read;
6412 cu_header->version = read_2_bytes (abfd, info_ptr);
6413 if (cu_header->version < 2 || cu_header->version > 5)
6414 error (_("Dwarf Error: wrong version in compilation unit header "
6415 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6416 cu_header->version, filename);
6417 info_ptr += 2;
6418 if (cu_header->version < 5)
6419 switch (section_kind)
6420 {
6421 case rcuh_kind::COMPILE:
6422 cu_header->unit_type = DW_UT_compile;
6423 break;
6424 case rcuh_kind::TYPE:
6425 cu_header->unit_type = DW_UT_type;
6426 break;
6427 default:
6428 internal_error (__FILE__, __LINE__,
6429 _("read_comp_unit_head: invalid section_kind"));
6430 }
6431 else
6432 {
6433 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6434 (read_1_byte (abfd, info_ptr));
6435 info_ptr += 1;
6436 switch (cu_header->unit_type)
6437 {
6438 case DW_UT_compile:
6439 case DW_UT_partial:
6440 case DW_UT_skeleton:
6441 case DW_UT_split_compile:
6442 if (section_kind != rcuh_kind::COMPILE)
6443 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6444 "(is %s, should be %s) [in module %s]"),
6445 dwarf_unit_type_name (cu_header->unit_type),
6446 dwarf_unit_type_name (DW_UT_type), filename);
6447 break;
6448 case DW_UT_type:
6449 case DW_UT_split_type:
6450 section_kind = rcuh_kind::TYPE;
6451 break;
6452 default:
6453 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6454 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6455 "[in module %s]"), cu_header->unit_type,
6456 dwarf_unit_type_name (DW_UT_compile),
6457 dwarf_unit_type_name (DW_UT_skeleton),
6458 dwarf_unit_type_name (DW_UT_split_compile),
6459 dwarf_unit_type_name (DW_UT_type),
6460 dwarf_unit_type_name (DW_UT_split_type), filename);
6461 }
6462
6463 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6464 info_ptr += 1;
6465 }
6466 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6467 cu_header,
6468 &bytes_read);
6469 info_ptr += bytes_read;
6470 if (cu_header->version < 5)
6471 {
6472 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6473 info_ptr += 1;
6474 }
6475 signed_addr = bfd_get_sign_extend_vma (abfd);
6476 if (signed_addr < 0)
6477 internal_error (__FILE__, __LINE__,
6478 _("read_comp_unit_head: dwarf from non elf file"));
6479 cu_header->signed_addr_p = signed_addr;
6480
6481 bool header_has_signature = section_kind == rcuh_kind::TYPE
6482 || cu_header->unit_type == DW_UT_skeleton
6483 || cu_header->unit_type == DW_UT_split_compile;
6484
6485 if (header_has_signature)
6486 {
6487 cu_header->signature = read_8_bytes (abfd, info_ptr);
6488 info_ptr += 8;
6489 }
6490
6491 if (section_kind == rcuh_kind::TYPE)
6492 {
6493 LONGEST type_offset;
6494 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6495 info_ptr += bytes_read;
6496 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6497 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6498 error (_("Dwarf Error: Too big type_offset in compilation unit "
6499 "header (is %s) [in module %s]"), plongest (type_offset),
6500 filename);
6501 }
6502
6503 return info_ptr;
6504 }
6505
6506 /* Helper function that returns the proper abbrev section for
6507 THIS_CU. */
6508
6509 static struct dwarf2_section_info *
6510 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6511 {
6512 struct dwarf2_section_info *abbrev;
6513 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6514
6515 if (this_cu->is_dwz)
6516 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6517 else
6518 abbrev = &dwarf2_per_objfile->abbrev;
6519
6520 return abbrev;
6521 }
6522
6523 /* Subroutine of read_and_check_comp_unit_head and
6524 read_and_check_type_unit_head to simplify them.
6525 Perform various error checking on the header. */
6526
6527 static void
6528 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6529 struct comp_unit_head *header,
6530 struct dwarf2_section_info *section,
6531 struct dwarf2_section_info *abbrev_section)
6532 {
6533 const char *filename = get_section_file_name (section);
6534
6535 if (to_underlying (header->abbrev_sect_off)
6536 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6537 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6538 "(offset %s + 6) [in module %s]"),
6539 sect_offset_str (header->abbrev_sect_off),
6540 sect_offset_str (header->sect_off),
6541 filename);
6542
6543 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6544 avoid potential 32-bit overflow. */
6545 if (((ULONGEST) header->sect_off + get_cu_length (header))
6546 > section->size)
6547 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6548 "(offset %s + 0) [in module %s]"),
6549 header->length, sect_offset_str (header->sect_off),
6550 filename);
6551 }
6552
6553 /* Read in a CU/TU header and perform some basic error checking.
6554 The contents of the header are stored in HEADER.
6555 The result is a pointer to the start of the first DIE. */
6556
6557 static const gdb_byte *
6558 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6559 struct comp_unit_head *header,
6560 struct dwarf2_section_info *section,
6561 struct dwarf2_section_info *abbrev_section,
6562 const gdb_byte *info_ptr,
6563 rcuh_kind section_kind)
6564 {
6565 const gdb_byte *beg_of_comp_unit = info_ptr;
6566
6567 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6568
6569 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6570
6571 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6572
6573 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6574 abbrev_section);
6575
6576 return info_ptr;
6577 }
6578
6579 /* Fetch the abbreviation table offset from a comp or type unit header. */
6580
6581 static sect_offset
6582 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6583 struct dwarf2_section_info *section,
6584 sect_offset sect_off)
6585 {
6586 bfd *abfd = get_section_bfd_owner (section);
6587 const gdb_byte *info_ptr;
6588 unsigned int initial_length_size, offset_size;
6589 uint16_t version;
6590
6591 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6592 info_ptr = section->buffer + to_underlying (sect_off);
6593 read_initial_length (abfd, info_ptr, &initial_length_size);
6594 offset_size = initial_length_size == 4 ? 4 : 8;
6595 info_ptr += initial_length_size;
6596
6597 version = read_2_bytes (abfd, info_ptr);
6598 info_ptr += 2;
6599 if (version >= 5)
6600 {
6601 /* Skip unit type and address size. */
6602 info_ptr += 2;
6603 }
6604
6605 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6606 }
6607
6608 /* Allocate a new partial symtab for file named NAME and mark this new
6609 partial symtab as being an include of PST. */
6610
6611 static void
6612 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6613 struct objfile *objfile)
6614 {
6615 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6616
6617 if (!IS_ABSOLUTE_PATH (subpst->filename))
6618 {
6619 /* It shares objfile->objfile_obstack. */
6620 subpst->dirname = pst->dirname;
6621 }
6622
6623 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6624 subpst->dependencies[0] = pst;
6625 subpst->number_of_dependencies = 1;
6626
6627 subpst->read_symtab = pst->read_symtab;
6628
6629 /* No private part is necessary for include psymtabs. This property
6630 can be used to differentiate between such include psymtabs and
6631 the regular ones. */
6632 subpst->read_symtab_private = NULL;
6633 }
6634
6635 /* Read the Line Number Program data and extract the list of files
6636 included by the source file represented by PST. Build an include
6637 partial symtab for each of these included files. */
6638
6639 static void
6640 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6641 struct die_info *die,
6642 struct partial_symtab *pst)
6643 {
6644 line_header_up lh;
6645 struct attribute *attr;
6646
6647 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6648 if (attr)
6649 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6650 if (lh == NULL)
6651 return; /* No linetable, so no includes. */
6652
6653 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6654 that we pass in the raw text_low here; that is ok because we're
6655 only decoding the line table to make include partial symtabs, and
6656 so the addresses aren't really used. */
6657 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6658 pst->raw_text_low (), 1);
6659 }
6660
6661 static hashval_t
6662 hash_signatured_type (const void *item)
6663 {
6664 const struct signatured_type *sig_type
6665 = (const struct signatured_type *) item;
6666
6667 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6668 return sig_type->signature;
6669 }
6670
6671 static int
6672 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6673 {
6674 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6675 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6676
6677 return lhs->signature == rhs->signature;
6678 }
6679
6680 /* Allocate a hash table for signatured types. */
6681
6682 static htab_t
6683 allocate_signatured_type_table (struct objfile *objfile)
6684 {
6685 return htab_create_alloc_ex (41,
6686 hash_signatured_type,
6687 eq_signatured_type,
6688 NULL,
6689 &objfile->objfile_obstack,
6690 hashtab_obstack_allocate,
6691 dummy_obstack_deallocate);
6692 }
6693
6694 /* A helper function to add a signatured type CU to a table. */
6695
6696 static int
6697 add_signatured_type_cu_to_table (void **slot, void *datum)
6698 {
6699 struct signatured_type *sigt = (struct signatured_type *) *slot;
6700 std::vector<signatured_type *> *all_type_units
6701 = (std::vector<signatured_type *> *) datum;
6702
6703 all_type_units->push_back (sigt);
6704
6705 return 1;
6706 }
6707
6708 /* A helper for create_debug_types_hash_table. Read types from SECTION
6709 and fill them into TYPES_HTAB. It will process only type units,
6710 therefore DW_UT_type. */
6711
6712 static void
6713 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6714 struct dwo_file *dwo_file,
6715 dwarf2_section_info *section, htab_t &types_htab,
6716 rcuh_kind section_kind)
6717 {
6718 struct objfile *objfile = dwarf2_per_objfile->objfile;
6719 struct dwarf2_section_info *abbrev_section;
6720 bfd *abfd;
6721 const gdb_byte *info_ptr, *end_ptr;
6722
6723 abbrev_section = (dwo_file != NULL
6724 ? &dwo_file->sections.abbrev
6725 : &dwarf2_per_objfile->abbrev);
6726
6727 if (dwarf_read_debug)
6728 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6729 get_section_name (section),
6730 get_section_file_name (abbrev_section));
6731
6732 dwarf2_read_section (objfile, section);
6733 info_ptr = section->buffer;
6734
6735 if (info_ptr == NULL)
6736 return;
6737
6738 /* We can't set abfd until now because the section may be empty or
6739 not present, in which case the bfd is unknown. */
6740 abfd = get_section_bfd_owner (section);
6741
6742 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6743 because we don't need to read any dies: the signature is in the
6744 header. */
6745
6746 end_ptr = info_ptr + section->size;
6747 while (info_ptr < end_ptr)
6748 {
6749 struct signatured_type *sig_type;
6750 struct dwo_unit *dwo_tu;
6751 void **slot;
6752 const gdb_byte *ptr = info_ptr;
6753 struct comp_unit_head header;
6754 unsigned int length;
6755
6756 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6757
6758 /* Initialize it due to a false compiler warning. */
6759 header.signature = -1;
6760 header.type_cu_offset_in_tu = (cu_offset) -1;
6761
6762 /* We need to read the type's signature in order to build the hash
6763 table, but we don't need anything else just yet. */
6764
6765 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6766 abbrev_section, ptr, section_kind);
6767
6768 length = get_cu_length (&header);
6769
6770 /* Skip dummy type units. */
6771 if (ptr >= info_ptr + length
6772 || peek_abbrev_code (abfd, ptr) == 0
6773 || header.unit_type != DW_UT_type)
6774 {
6775 info_ptr += length;
6776 continue;
6777 }
6778
6779 if (types_htab == NULL)
6780 {
6781 if (dwo_file)
6782 types_htab = allocate_dwo_unit_table (objfile);
6783 else
6784 types_htab = allocate_signatured_type_table (objfile);
6785 }
6786
6787 if (dwo_file)
6788 {
6789 sig_type = NULL;
6790 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6791 struct dwo_unit);
6792 dwo_tu->dwo_file = dwo_file;
6793 dwo_tu->signature = header.signature;
6794 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6795 dwo_tu->section = section;
6796 dwo_tu->sect_off = sect_off;
6797 dwo_tu->length = length;
6798 }
6799 else
6800 {
6801 /* N.B.: type_offset is not usable if this type uses a DWO file.
6802 The real type_offset is in the DWO file. */
6803 dwo_tu = NULL;
6804 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6805 struct signatured_type);
6806 sig_type->signature = header.signature;
6807 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6808 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6809 sig_type->per_cu.is_debug_types = 1;
6810 sig_type->per_cu.section = section;
6811 sig_type->per_cu.sect_off = sect_off;
6812 sig_type->per_cu.length = length;
6813 }
6814
6815 slot = htab_find_slot (types_htab,
6816 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6817 INSERT);
6818 gdb_assert (slot != NULL);
6819 if (*slot != NULL)
6820 {
6821 sect_offset dup_sect_off;
6822
6823 if (dwo_file)
6824 {
6825 const struct dwo_unit *dup_tu
6826 = (const struct dwo_unit *) *slot;
6827
6828 dup_sect_off = dup_tu->sect_off;
6829 }
6830 else
6831 {
6832 const struct signatured_type *dup_tu
6833 = (const struct signatured_type *) *slot;
6834
6835 dup_sect_off = dup_tu->per_cu.sect_off;
6836 }
6837
6838 complaint (_("debug type entry at offset %s is duplicate to"
6839 " the entry at offset %s, signature %s"),
6840 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6841 hex_string (header.signature));
6842 }
6843 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6844
6845 if (dwarf_read_debug > 1)
6846 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6847 sect_offset_str (sect_off),
6848 hex_string (header.signature));
6849
6850 info_ptr += length;
6851 }
6852 }
6853
6854 /* Create the hash table of all entries in the .debug_types
6855 (or .debug_types.dwo) section(s).
6856 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6857 otherwise it is NULL.
6858
6859 The result is a pointer to the hash table or NULL if there are no types.
6860
6861 Note: This function processes DWO files only, not DWP files. */
6862
6863 static void
6864 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6865 struct dwo_file *dwo_file,
6866 gdb::array_view<dwarf2_section_info> type_sections,
6867 htab_t &types_htab)
6868 {
6869 for (dwarf2_section_info &section : type_sections)
6870 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6871 types_htab, rcuh_kind::TYPE);
6872 }
6873
6874 /* Create the hash table of all entries in the .debug_types section,
6875 and initialize all_type_units.
6876 The result is zero if there is an error (e.g. missing .debug_types section),
6877 otherwise non-zero. */
6878
6879 static int
6880 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6881 {
6882 htab_t types_htab = NULL;
6883
6884 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6885 &dwarf2_per_objfile->info, types_htab,
6886 rcuh_kind::COMPILE);
6887 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6888 dwarf2_per_objfile->types, types_htab);
6889 if (types_htab == NULL)
6890 {
6891 dwarf2_per_objfile->signatured_types = NULL;
6892 return 0;
6893 }
6894
6895 dwarf2_per_objfile->signatured_types = types_htab;
6896
6897 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6898 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6899
6900 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6901 &dwarf2_per_objfile->all_type_units);
6902
6903 return 1;
6904 }
6905
6906 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6907 If SLOT is non-NULL, it is the entry to use in the hash table.
6908 Otherwise we find one. */
6909
6910 static struct signatured_type *
6911 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6912 void **slot)
6913 {
6914 struct objfile *objfile = dwarf2_per_objfile->objfile;
6915
6916 if (dwarf2_per_objfile->all_type_units.size ()
6917 == dwarf2_per_objfile->all_type_units.capacity ())
6918 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6919
6920 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6921 struct signatured_type);
6922
6923 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6924 sig_type->signature = sig;
6925 sig_type->per_cu.is_debug_types = 1;
6926 if (dwarf2_per_objfile->using_index)
6927 {
6928 sig_type->per_cu.v.quick =
6929 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6930 struct dwarf2_per_cu_quick_data);
6931 }
6932
6933 if (slot == NULL)
6934 {
6935 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6936 sig_type, INSERT);
6937 }
6938 gdb_assert (*slot == NULL);
6939 *slot = sig_type;
6940 /* The rest of sig_type must be filled in by the caller. */
6941 return sig_type;
6942 }
6943
6944 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6945 Fill in SIG_ENTRY with DWO_ENTRY. */
6946
6947 static void
6948 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6949 struct signatured_type *sig_entry,
6950 struct dwo_unit *dwo_entry)
6951 {
6952 /* Make sure we're not clobbering something we don't expect to. */
6953 gdb_assert (! sig_entry->per_cu.queued);
6954 gdb_assert (sig_entry->per_cu.cu == NULL);
6955 if (dwarf2_per_objfile->using_index)
6956 {
6957 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6958 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6959 }
6960 else
6961 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6962 gdb_assert (sig_entry->signature == dwo_entry->signature);
6963 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6964 gdb_assert (sig_entry->type_unit_group == NULL);
6965 gdb_assert (sig_entry->dwo_unit == NULL);
6966
6967 sig_entry->per_cu.section = dwo_entry->section;
6968 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6969 sig_entry->per_cu.length = dwo_entry->length;
6970 sig_entry->per_cu.reading_dwo_directly = 1;
6971 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6972 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6973 sig_entry->dwo_unit = dwo_entry;
6974 }
6975
6976 /* Subroutine of lookup_signatured_type.
6977 If we haven't read the TU yet, create the signatured_type data structure
6978 for a TU to be read in directly from a DWO file, bypassing the stub.
6979 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6980 using .gdb_index, then when reading a CU we want to stay in the DWO file
6981 containing that CU. Otherwise we could end up reading several other DWO
6982 files (due to comdat folding) to process the transitive closure of all the
6983 mentioned TUs, and that can be slow. The current DWO file will have every
6984 type signature that it needs.
6985 We only do this for .gdb_index because in the psymtab case we already have
6986 to read all the DWOs to build the type unit groups. */
6987
6988 static struct signatured_type *
6989 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6990 {
6991 struct dwarf2_per_objfile *dwarf2_per_objfile
6992 = cu->per_cu->dwarf2_per_objfile;
6993 struct objfile *objfile = dwarf2_per_objfile->objfile;
6994 struct dwo_file *dwo_file;
6995 struct dwo_unit find_dwo_entry, *dwo_entry;
6996 struct signatured_type find_sig_entry, *sig_entry;
6997 void **slot;
6998
6999 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7000
7001 /* If TU skeletons have been removed then we may not have read in any
7002 TUs yet. */
7003 if (dwarf2_per_objfile->signatured_types == NULL)
7004 {
7005 dwarf2_per_objfile->signatured_types
7006 = allocate_signatured_type_table (objfile);
7007 }
7008
7009 /* We only ever need to read in one copy of a signatured type.
7010 Use the global signatured_types array to do our own comdat-folding
7011 of types. If this is the first time we're reading this TU, and
7012 the TU has an entry in .gdb_index, replace the recorded data from
7013 .gdb_index with this TU. */
7014
7015 find_sig_entry.signature = sig;
7016 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7017 &find_sig_entry, INSERT);
7018 sig_entry = (struct signatured_type *) *slot;
7019
7020 /* We can get here with the TU already read, *or* in the process of being
7021 read. Don't reassign the global entry to point to this DWO if that's
7022 the case. Also note that if the TU is already being read, it may not
7023 have come from a DWO, the program may be a mix of Fission-compiled
7024 code and non-Fission-compiled code. */
7025
7026 /* Have we already tried to read this TU?
7027 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7028 needn't exist in the global table yet). */
7029 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7030 return sig_entry;
7031
7032 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7033 dwo_unit of the TU itself. */
7034 dwo_file = cu->dwo_unit->dwo_file;
7035
7036 /* Ok, this is the first time we're reading this TU. */
7037 if (dwo_file->tus == NULL)
7038 return NULL;
7039 find_dwo_entry.signature = sig;
7040 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7041 if (dwo_entry == NULL)
7042 return NULL;
7043
7044 /* If the global table doesn't have an entry for this TU, add one. */
7045 if (sig_entry == NULL)
7046 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7047
7048 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7049 sig_entry->per_cu.tu_read = 1;
7050 return sig_entry;
7051 }
7052
7053 /* Subroutine of lookup_signatured_type.
7054 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7055 then try the DWP file. If the TU stub (skeleton) has been removed then
7056 it won't be in .gdb_index. */
7057
7058 static struct signatured_type *
7059 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7060 {
7061 struct dwarf2_per_objfile *dwarf2_per_objfile
7062 = cu->per_cu->dwarf2_per_objfile;
7063 struct objfile *objfile = dwarf2_per_objfile->objfile;
7064 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7065 struct dwo_unit *dwo_entry;
7066 struct signatured_type find_sig_entry, *sig_entry;
7067 void **slot;
7068
7069 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7070 gdb_assert (dwp_file != NULL);
7071
7072 /* If TU skeletons have been removed then we may not have read in any
7073 TUs yet. */
7074 if (dwarf2_per_objfile->signatured_types == NULL)
7075 {
7076 dwarf2_per_objfile->signatured_types
7077 = allocate_signatured_type_table (objfile);
7078 }
7079
7080 find_sig_entry.signature = sig;
7081 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7082 &find_sig_entry, INSERT);
7083 sig_entry = (struct signatured_type *) *slot;
7084
7085 /* Have we already tried to read this TU?
7086 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7087 needn't exist in the global table yet). */
7088 if (sig_entry != NULL)
7089 return sig_entry;
7090
7091 if (dwp_file->tus == NULL)
7092 return NULL;
7093 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7094 sig, 1 /* is_debug_types */);
7095 if (dwo_entry == NULL)
7096 return NULL;
7097
7098 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7099 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7100
7101 return sig_entry;
7102 }
7103
7104 /* Lookup a signature based type for DW_FORM_ref_sig8.
7105 Returns NULL if signature SIG is not present in the table.
7106 It is up to the caller to complain about this. */
7107
7108 static struct signatured_type *
7109 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7110 {
7111 struct dwarf2_per_objfile *dwarf2_per_objfile
7112 = cu->per_cu->dwarf2_per_objfile;
7113
7114 if (cu->dwo_unit
7115 && dwarf2_per_objfile->using_index)
7116 {
7117 /* We're in a DWO/DWP file, and we're using .gdb_index.
7118 These cases require special processing. */
7119 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7120 return lookup_dwo_signatured_type (cu, sig);
7121 else
7122 return lookup_dwp_signatured_type (cu, sig);
7123 }
7124 else
7125 {
7126 struct signatured_type find_entry, *entry;
7127
7128 if (dwarf2_per_objfile->signatured_types == NULL)
7129 return NULL;
7130 find_entry.signature = sig;
7131 entry = ((struct signatured_type *)
7132 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7133 return entry;
7134 }
7135 }
7136 \f
7137 /* Low level DIE reading support. */
7138
7139 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7140
7141 static void
7142 init_cu_die_reader (struct die_reader_specs *reader,
7143 struct dwarf2_cu *cu,
7144 struct dwarf2_section_info *section,
7145 struct dwo_file *dwo_file,
7146 struct abbrev_table *abbrev_table)
7147 {
7148 gdb_assert (section->readin && section->buffer != NULL);
7149 reader->abfd = get_section_bfd_owner (section);
7150 reader->cu = cu;
7151 reader->dwo_file = dwo_file;
7152 reader->die_section = section;
7153 reader->buffer = section->buffer;
7154 reader->buffer_end = section->buffer + section->size;
7155 reader->comp_dir = NULL;
7156 reader->abbrev_table = abbrev_table;
7157 }
7158
7159 /* Subroutine of init_cutu_and_read_dies to simplify it.
7160 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7161 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7162 already.
7163
7164 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7165 from it to the DIE in the DWO. If NULL we are skipping the stub.
7166 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7167 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7168 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7169 STUB_COMP_DIR may be non-NULL.
7170 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7171 are filled in with the info of the DIE from the DWO file.
7172 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7173 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7174 kept around for at least as long as *RESULT_READER.
7175
7176 The result is non-zero if a valid (non-dummy) DIE was found. */
7177
7178 static int
7179 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7180 struct dwo_unit *dwo_unit,
7181 struct die_info *stub_comp_unit_die,
7182 const char *stub_comp_dir,
7183 struct die_reader_specs *result_reader,
7184 const gdb_byte **result_info_ptr,
7185 struct die_info **result_comp_unit_die,
7186 int *result_has_children,
7187 abbrev_table_up *result_dwo_abbrev_table)
7188 {
7189 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7190 struct objfile *objfile = dwarf2_per_objfile->objfile;
7191 struct dwarf2_cu *cu = this_cu->cu;
7192 bfd *abfd;
7193 const gdb_byte *begin_info_ptr, *info_ptr;
7194 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7195 int i,num_extra_attrs;
7196 struct dwarf2_section_info *dwo_abbrev_section;
7197 struct attribute *attr;
7198 struct die_info *comp_unit_die;
7199
7200 /* At most one of these may be provided. */
7201 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7202
7203 /* These attributes aren't processed until later:
7204 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7205 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7206 referenced later. However, these attributes are found in the stub
7207 which we won't have later. In order to not impose this complication
7208 on the rest of the code, we read them here and copy them to the
7209 DWO CU/TU die. */
7210
7211 stmt_list = NULL;
7212 low_pc = NULL;
7213 high_pc = NULL;
7214 ranges = NULL;
7215 comp_dir = NULL;
7216
7217 if (stub_comp_unit_die != NULL)
7218 {
7219 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7220 DWO file. */
7221 if (! this_cu->is_debug_types)
7222 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7223 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7224 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7225 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7226 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7227
7228 /* There should be a DW_AT_addr_base attribute here (if needed).
7229 We need the value before we can process DW_FORM_GNU_addr_index
7230 or DW_FORM_addrx. */
7231 cu->addr_base = 0;
7232 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7233 if (attr)
7234 cu->addr_base = DW_UNSND (attr);
7235
7236 /* There should be a DW_AT_ranges_base attribute here (if needed).
7237 We need the value before we can process DW_AT_ranges. */
7238 cu->ranges_base = 0;
7239 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7240 if (attr)
7241 cu->ranges_base = DW_UNSND (attr);
7242 }
7243 else if (stub_comp_dir != NULL)
7244 {
7245 /* Reconstruct the comp_dir attribute to simplify the code below. */
7246 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7247 comp_dir->name = DW_AT_comp_dir;
7248 comp_dir->form = DW_FORM_string;
7249 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7250 DW_STRING (comp_dir) = stub_comp_dir;
7251 }
7252
7253 /* Set up for reading the DWO CU/TU. */
7254 cu->dwo_unit = dwo_unit;
7255 dwarf2_section_info *section = dwo_unit->section;
7256 dwarf2_read_section (objfile, section);
7257 abfd = get_section_bfd_owner (section);
7258 begin_info_ptr = info_ptr = (section->buffer
7259 + to_underlying (dwo_unit->sect_off));
7260 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7261
7262 if (this_cu->is_debug_types)
7263 {
7264 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7265
7266 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7267 &cu->header, section,
7268 dwo_abbrev_section,
7269 info_ptr, rcuh_kind::TYPE);
7270 /* This is not an assert because it can be caused by bad debug info. */
7271 if (sig_type->signature != cu->header.signature)
7272 {
7273 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7274 " TU at offset %s [in module %s]"),
7275 hex_string (sig_type->signature),
7276 hex_string (cu->header.signature),
7277 sect_offset_str (dwo_unit->sect_off),
7278 bfd_get_filename (abfd));
7279 }
7280 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7281 /* For DWOs coming from DWP files, we don't know the CU length
7282 nor the type's offset in the TU until now. */
7283 dwo_unit->length = get_cu_length (&cu->header);
7284 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7285
7286 /* Establish the type offset that can be used to lookup the type.
7287 For DWO files, we don't know it until now. */
7288 sig_type->type_offset_in_section
7289 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7290 }
7291 else
7292 {
7293 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7294 &cu->header, section,
7295 dwo_abbrev_section,
7296 info_ptr, rcuh_kind::COMPILE);
7297 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7298 /* For DWOs coming from DWP files, we don't know the CU length
7299 until now. */
7300 dwo_unit->length = get_cu_length (&cu->header);
7301 }
7302
7303 *result_dwo_abbrev_table
7304 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7305 cu->header.abbrev_sect_off);
7306 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7307 result_dwo_abbrev_table->get ());
7308
7309 /* Read in the die, but leave space to copy over the attributes
7310 from the stub. This has the benefit of simplifying the rest of
7311 the code - all the work to maintain the illusion of a single
7312 DW_TAG_{compile,type}_unit DIE is done here. */
7313 num_extra_attrs = ((stmt_list != NULL)
7314 + (low_pc != NULL)
7315 + (high_pc != NULL)
7316 + (ranges != NULL)
7317 + (comp_dir != NULL));
7318 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7319 result_has_children, num_extra_attrs);
7320
7321 /* Copy over the attributes from the stub to the DIE we just read in. */
7322 comp_unit_die = *result_comp_unit_die;
7323 i = comp_unit_die->num_attrs;
7324 if (stmt_list != NULL)
7325 comp_unit_die->attrs[i++] = *stmt_list;
7326 if (low_pc != NULL)
7327 comp_unit_die->attrs[i++] = *low_pc;
7328 if (high_pc != NULL)
7329 comp_unit_die->attrs[i++] = *high_pc;
7330 if (ranges != NULL)
7331 comp_unit_die->attrs[i++] = *ranges;
7332 if (comp_dir != NULL)
7333 comp_unit_die->attrs[i++] = *comp_dir;
7334 comp_unit_die->num_attrs += num_extra_attrs;
7335
7336 if (dwarf_die_debug)
7337 {
7338 fprintf_unfiltered (gdb_stdlog,
7339 "Read die from %s@0x%x of %s:\n",
7340 get_section_name (section),
7341 (unsigned) (begin_info_ptr - section->buffer),
7342 bfd_get_filename (abfd));
7343 dump_die (comp_unit_die, dwarf_die_debug);
7344 }
7345
7346 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7347 TUs by skipping the stub and going directly to the entry in the DWO file.
7348 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7349 to get it via circuitous means. Blech. */
7350 if (comp_dir != NULL)
7351 result_reader->comp_dir = DW_STRING (comp_dir);
7352
7353 /* Skip dummy compilation units. */
7354 if (info_ptr >= begin_info_ptr + dwo_unit->length
7355 || peek_abbrev_code (abfd, info_ptr) == 0)
7356 return 0;
7357
7358 *result_info_ptr = info_ptr;
7359 return 1;
7360 }
7361
7362 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7363 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7364 signature is part of the header. */
7365 static gdb::optional<ULONGEST>
7366 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7367 {
7368 if (cu->header.version >= 5)
7369 return cu->header.signature;
7370 struct attribute *attr;
7371 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7372 if (attr == nullptr)
7373 return gdb::optional<ULONGEST> ();
7374 return DW_UNSND (attr);
7375 }
7376
7377 /* Subroutine of init_cutu_and_read_dies to simplify it.
7378 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7379 Returns NULL if the specified DWO unit cannot be found. */
7380
7381 static struct dwo_unit *
7382 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7383 struct die_info *comp_unit_die)
7384 {
7385 struct dwarf2_cu *cu = this_cu->cu;
7386 struct dwo_unit *dwo_unit;
7387 const char *comp_dir, *dwo_name;
7388
7389 gdb_assert (cu != NULL);
7390
7391 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7392 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7393 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7394
7395 if (this_cu->is_debug_types)
7396 {
7397 struct signatured_type *sig_type;
7398
7399 /* Since this_cu is the first member of struct signatured_type,
7400 we can go from a pointer to one to a pointer to the other. */
7401 sig_type = (struct signatured_type *) this_cu;
7402 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7403 }
7404 else
7405 {
7406 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7407 if (!signature.has_value ())
7408 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7409 " [in module %s]"),
7410 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7411 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7412 *signature);
7413 }
7414
7415 return dwo_unit;
7416 }
7417
7418 /* Subroutine of init_cutu_and_read_dies to simplify it.
7419 See it for a description of the parameters.
7420 Read a TU directly from a DWO file, bypassing the stub. */
7421
7422 static void
7423 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7424 int use_existing_cu, int keep,
7425 die_reader_func_ftype *die_reader_func,
7426 void *data)
7427 {
7428 std::unique_ptr<dwarf2_cu> new_cu;
7429 struct signatured_type *sig_type;
7430 struct die_reader_specs reader;
7431 const gdb_byte *info_ptr;
7432 struct die_info *comp_unit_die;
7433 int has_children;
7434 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7435
7436 /* Verify we can do the following downcast, and that we have the
7437 data we need. */
7438 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7439 sig_type = (struct signatured_type *) this_cu;
7440 gdb_assert (sig_type->dwo_unit != NULL);
7441
7442 if (use_existing_cu && this_cu->cu != NULL)
7443 {
7444 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7445 /* There's no need to do the rereading_dwo_cu handling that
7446 init_cutu_and_read_dies does since we don't read the stub. */
7447 }
7448 else
7449 {
7450 /* If !use_existing_cu, this_cu->cu must be NULL. */
7451 gdb_assert (this_cu->cu == NULL);
7452 new_cu.reset (new dwarf2_cu (this_cu));
7453 }
7454
7455 /* A future optimization, if needed, would be to use an existing
7456 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7457 could share abbrev tables. */
7458
7459 /* The abbreviation table used by READER, this must live at least as long as
7460 READER. */
7461 abbrev_table_up dwo_abbrev_table;
7462
7463 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7464 NULL /* stub_comp_unit_die */,
7465 sig_type->dwo_unit->dwo_file->comp_dir,
7466 &reader, &info_ptr,
7467 &comp_unit_die, &has_children,
7468 &dwo_abbrev_table) == 0)
7469 {
7470 /* Dummy die. */
7471 return;
7472 }
7473
7474 /* All the "real" work is done here. */
7475 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7476
7477 /* This duplicates the code in init_cutu_and_read_dies,
7478 but the alternative is making the latter more complex.
7479 This function is only for the special case of using DWO files directly:
7480 no point in overly complicating the general case just to handle this. */
7481 if (new_cu != NULL && keep)
7482 {
7483 /* Link this CU into read_in_chain. */
7484 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7485 dwarf2_per_objfile->read_in_chain = this_cu;
7486 /* The chain owns it now. */
7487 new_cu.release ();
7488 }
7489 }
7490
7491 /* Initialize a CU (or TU) and read its DIEs.
7492 If the CU defers to a DWO file, read the DWO file as well.
7493
7494 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7495 Otherwise the table specified in the comp unit header is read in and used.
7496 This is an optimization for when we already have the abbrev table.
7497
7498 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7499 Otherwise, a new CU is allocated with xmalloc.
7500
7501 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7502 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7503
7504 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7505 linker) then DIE_READER_FUNC will not get called. */
7506
7507 static void
7508 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
7509 struct abbrev_table *abbrev_table,
7510 int use_existing_cu, int keep,
7511 bool skip_partial,
7512 die_reader_func_ftype *die_reader_func,
7513 void *data)
7514 {
7515 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7516 struct objfile *objfile = dwarf2_per_objfile->objfile;
7517 struct dwarf2_section_info *section = this_cu->section;
7518 bfd *abfd = get_section_bfd_owner (section);
7519 struct dwarf2_cu *cu;
7520 const gdb_byte *begin_info_ptr, *info_ptr;
7521 struct die_reader_specs reader;
7522 struct die_info *comp_unit_die;
7523 int has_children;
7524 struct signatured_type *sig_type = NULL;
7525 struct dwarf2_section_info *abbrev_section;
7526 /* Non-zero if CU currently points to a DWO file and we need to
7527 reread it. When this happens we need to reread the skeleton die
7528 before we can reread the DWO file (this only applies to CUs, not TUs). */
7529 int rereading_dwo_cu = 0;
7530
7531 if (dwarf_die_debug)
7532 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7533 this_cu->is_debug_types ? "type" : "comp",
7534 sect_offset_str (this_cu->sect_off));
7535
7536 if (use_existing_cu)
7537 gdb_assert (keep);
7538
7539 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7540 file (instead of going through the stub), short-circuit all of this. */
7541 if (this_cu->reading_dwo_directly)
7542 {
7543 /* Narrow down the scope of possibilities to have to understand. */
7544 gdb_assert (this_cu->is_debug_types);
7545 gdb_assert (abbrev_table == NULL);
7546 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7547 die_reader_func, data);
7548 return;
7549 }
7550
7551 /* This is cheap if the section is already read in. */
7552 dwarf2_read_section (objfile, section);
7553
7554 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7555
7556 abbrev_section = get_abbrev_section_for_cu (this_cu);
7557
7558 std::unique_ptr<dwarf2_cu> new_cu;
7559 if (use_existing_cu && this_cu->cu != NULL)
7560 {
7561 cu = this_cu->cu;
7562 /* If this CU is from a DWO file we need to start over, we need to
7563 refetch the attributes from the skeleton CU.
7564 This could be optimized by retrieving those attributes from when we
7565 were here the first time: the previous comp_unit_die was stored in
7566 comp_unit_obstack. But there's no data yet that we need this
7567 optimization. */
7568 if (cu->dwo_unit != NULL)
7569 rereading_dwo_cu = 1;
7570 }
7571 else
7572 {
7573 /* If !use_existing_cu, this_cu->cu must be NULL. */
7574 gdb_assert (this_cu->cu == NULL);
7575 new_cu.reset (new dwarf2_cu (this_cu));
7576 cu = new_cu.get ();
7577 }
7578
7579 /* Get the header. */
7580 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7581 {
7582 /* We already have the header, there's no need to read it in again. */
7583 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7584 }
7585 else
7586 {
7587 if (this_cu->is_debug_types)
7588 {
7589 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7590 &cu->header, section,
7591 abbrev_section, info_ptr,
7592 rcuh_kind::TYPE);
7593
7594 /* Since per_cu is the first member of struct signatured_type,
7595 we can go from a pointer to one to a pointer to the other. */
7596 sig_type = (struct signatured_type *) this_cu;
7597 gdb_assert (sig_type->signature == cu->header.signature);
7598 gdb_assert (sig_type->type_offset_in_tu
7599 == cu->header.type_cu_offset_in_tu);
7600 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7601
7602 /* LENGTH has not been set yet for type units if we're
7603 using .gdb_index. */
7604 this_cu->length = get_cu_length (&cu->header);
7605
7606 /* Establish the type offset that can be used to lookup the type. */
7607 sig_type->type_offset_in_section =
7608 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7609
7610 this_cu->dwarf_version = cu->header.version;
7611 }
7612 else
7613 {
7614 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7615 &cu->header, section,
7616 abbrev_section,
7617 info_ptr,
7618 rcuh_kind::COMPILE);
7619
7620 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7621 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7622 this_cu->dwarf_version = cu->header.version;
7623 }
7624 }
7625
7626 /* Skip dummy compilation units. */
7627 if (info_ptr >= begin_info_ptr + this_cu->length
7628 || peek_abbrev_code (abfd, info_ptr) == 0)
7629 return;
7630
7631 /* If we don't have them yet, read the abbrevs for this compilation unit.
7632 And if we need to read them now, make sure they're freed when we're
7633 done (own the table through ABBREV_TABLE_HOLDER). */
7634 abbrev_table_up abbrev_table_holder;
7635 if (abbrev_table != NULL)
7636 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7637 else
7638 {
7639 abbrev_table_holder
7640 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7641 cu->header.abbrev_sect_off);
7642 abbrev_table = abbrev_table_holder.get ();
7643 }
7644
7645 /* Read the top level CU/TU die. */
7646 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
7647 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7648
7649 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7650 return;
7651
7652 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7653 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7654 table from the DWO file and pass the ownership over to us. It will be
7655 referenced from READER, so we must make sure to free it after we're done
7656 with READER.
7657
7658 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7659 DWO CU, that this test will fail (the attribute will not be present). */
7660 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7661 abbrev_table_up dwo_abbrev_table;
7662 if (dwo_name != nullptr)
7663 {
7664 struct dwo_unit *dwo_unit;
7665 struct die_info *dwo_comp_unit_die;
7666
7667 if (has_children)
7668 {
7669 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7670 " has children (offset %s) [in module %s]"),
7671 sect_offset_str (this_cu->sect_off),
7672 bfd_get_filename (abfd));
7673 }
7674 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
7675 if (dwo_unit != NULL)
7676 {
7677 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7678 comp_unit_die, NULL,
7679 &reader, &info_ptr,
7680 &dwo_comp_unit_die, &has_children,
7681 &dwo_abbrev_table) == 0)
7682 {
7683 /* Dummy die. */
7684 return;
7685 }
7686 comp_unit_die = dwo_comp_unit_die;
7687 }
7688 else
7689 {
7690 /* Yikes, we couldn't find the rest of the DIE, we only have
7691 the stub. A complaint has already been logged. There's
7692 not much more we can do except pass on the stub DIE to
7693 die_reader_func. We don't want to throw an error on bad
7694 debug info. */
7695 }
7696 }
7697
7698 /* All of the above is setup for this call. Yikes. */
7699 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7700
7701 /* Done, clean up. */
7702 if (new_cu != NULL && keep)
7703 {
7704 /* Link this CU into read_in_chain. */
7705 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7706 dwarf2_per_objfile->read_in_chain = this_cu;
7707 /* The chain owns it now. */
7708 new_cu.release ();
7709 }
7710 }
7711
7712 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7713 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7714 to have already done the lookup to find the DWO file).
7715
7716 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7717 THIS_CU->is_debug_types, but nothing else.
7718
7719 We fill in THIS_CU->length.
7720
7721 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7722 linker) then DIE_READER_FUNC will not get called.
7723
7724 THIS_CU->cu is always freed when done.
7725 This is done in order to not leave THIS_CU->cu in a state where we have
7726 to care whether it refers to the "main" CU or the DWO CU. */
7727
7728 static void
7729 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
7730 struct dwo_file *dwo_file,
7731 die_reader_func_ftype *die_reader_func,
7732 void *data)
7733 {
7734 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7735 struct objfile *objfile = dwarf2_per_objfile->objfile;
7736 struct dwarf2_section_info *section = this_cu->section;
7737 bfd *abfd = get_section_bfd_owner (section);
7738 struct dwarf2_section_info *abbrev_section;
7739 const gdb_byte *begin_info_ptr, *info_ptr;
7740 struct die_reader_specs reader;
7741 struct die_info *comp_unit_die;
7742 int has_children;
7743
7744 if (dwarf_die_debug)
7745 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7746 this_cu->is_debug_types ? "type" : "comp",
7747 sect_offset_str (this_cu->sect_off));
7748
7749 gdb_assert (this_cu->cu == NULL);
7750
7751 abbrev_section = (dwo_file != NULL
7752 ? &dwo_file->sections.abbrev
7753 : get_abbrev_section_for_cu (this_cu));
7754
7755 /* This is cheap if the section is already read in. */
7756 dwarf2_read_section (objfile, section);
7757
7758 struct dwarf2_cu cu (this_cu);
7759
7760 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7761 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7762 &cu.header, section,
7763 abbrev_section, info_ptr,
7764 (this_cu->is_debug_types
7765 ? rcuh_kind::TYPE
7766 : rcuh_kind::COMPILE));
7767
7768 this_cu->length = get_cu_length (&cu.header);
7769
7770 /* Skip dummy compilation units. */
7771 if (info_ptr >= begin_info_ptr + this_cu->length
7772 || peek_abbrev_code (abfd, info_ptr) == 0)
7773 return;
7774
7775 abbrev_table_up abbrev_table
7776 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7777 cu.header.abbrev_sect_off);
7778
7779 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
7780 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7781
7782 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7783 }
7784
7785 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7786 does not lookup the specified DWO file.
7787 This cannot be used to read DWO files.
7788
7789 THIS_CU->cu is always freed when done.
7790 This is done in order to not leave THIS_CU->cu in a state where we have
7791 to care whether it refers to the "main" CU or the DWO CU.
7792 We can revisit this if the data shows there's a performance issue. */
7793
7794 static void
7795 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7796 die_reader_func_ftype *die_reader_func,
7797 void *data)
7798 {
7799 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
7800 }
7801 \f
7802 /* Type Unit Groups.
7803
7804 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7805 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7806 so that all types coming from the same compilation (.o file) are grouped
7807 together. A future step could be to put the types in the same symtab as
7808 the CU the types ultimately came from. */
7809
7810 static hashval_t
7811 hash_type_unit_group (const void *item)
7812 {
7813 const struct type_unit_group *tu_group
7814 = (const struct type_unit_group *) item;
7815
7816 return hash_stmt_list_entry (&tu_group->hash);
7817 }
7818
7819 static int
7820 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7821 {
7822 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7823 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7824
7825 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7826 }
7827
7828 /* Allocate a hash table for type unit groups. */
7829
7830 static htab_t
7831 allocate_type_unit_groups_table (struct objfile *objfile)
7832 {
7833 return htab_create_alloc_ex (3,
7834 hash_type_unit_group,
7835 eq_type_unit_group,
7836 NULL,
7837 &objfile->objfile_obstack,
7838 hashtab_obstack_allocate,
7839 dummy_obstack_deallocate);
7840 }
7841
7842 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7843 partial symtabs. We combine several TUs per psymtab to not let the size
7844 of any one psymtab grow too big. */
7845 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7846 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7847
7848 /* Helper routine for get_type_unit_group.
7849 Create the type_unit_group object used to hold one or more TUs. */
7850
7851 static struct type_unit_group *
7852 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7853 {
7854 struct dwarf2_per_objfile *dwarf2_per_objfile
7855 = cu->per_cu->dwarf2_per_objfile;
7856 struct objfile *objfile = dwarf2_per_objfile->objfile;
7857 struct dwarf2_per_cu_data *per_cu;
7858 struct type_unit_group *tu_group;
7859
7860 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7861 struct type_unit_group);
7862 per_cu = &tu_group->per_cu;
7863 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7864
7865 if (dwarf2_per_objfile->using_index)
7866 {
7867 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7868 struct dwarf2_per_cu_quick_data);
7869 }
7870 else
7871 {
7872 unsigned int line_offset = to_underlying (line_offset_struct);
7873 struct partial_symtab *pst;
7874 std::string name;
7875
7876 /* Give the symtab a useful name for debug purposes. */
7877 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7878 name = string_printf ("<type_units_%d>",
7879 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7880 else
7881 name = string_printf ("<type_units_at_0x%x>", line_offset);
7882
7883 pst = create_partial_symtab (per_cu, name.c_str ());
7884 pst->anonymous = 1;
7885 }
7886
7887 tu_group->hash.dwo_unit = cu->dwo_unit;
7888 tu_group->hash.line_sect_off = line_offset_struct;
7889
7890 return tu_group;
7891 }
7892
7893 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7894 STMT_LIST is a DW_AT_stmt_list attribute. */
7895
7896 static struct type_unit_group *
7897 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7898 {
7899 struct dwarf2_per_objfile *dwarf2_per_objfile
7900 = cu->per_cu->dwarf2_per_objfile;
7901 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7902 struct type_unit_group *tu_group;
7903 void **slot;
7904 unsigned int line_offset;
7905 struct type_unit_group type_unit_group_for_lookup;
7906
7907 if (dwarf2_per_objfile->type_unit_groups == NULL)
7908 {
7909 dwarf2_per_objfile->type_unit_groups =
7910 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7911 }
7912
7913 /* Do we need to create a new group, or can we use an existing one? */
7914
7915 if (stmt_list)
7916 {
7917 line_offset = DW_UNSND (stmt_list);
7918 ++tu_stats->nr_symtab_sharers;
7919 }
7920 else
7921 {
7922 /* Ugh, no stmt_list. Rare, but we have to handle it.
7923 We can do various things here like create one group per TU or
7924 spread them over multiple groups to split up the expansion work.
7925 To avoid worst case scenarios (too many groups or too large groups)
7926 we, umm, group them in bunches. */
7927 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7928 | (tu_stats->nr_stmt_less_type_units
7929 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7930 ++tu_stats->nr_stmt_less_type_units;
7931 }
7932
7933 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7934 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7935 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7936 &type_unit_group_for_lookup, INSERT);
7937 if (*slot != NULL)
7938 {
7939 tu_group = (struct type_unit_group *) *slot;
7940 gdb_assert (tu_group != NULL);
7941 }
7942 else
7943 {
7944 sect_offset line_offset_struct = (sect_offset) line_offset;
7945 tu_group = create_type_unit_group (cu, line_offset_struct);
7946 *slot = tu_group;
7947 ++tu_stats->nr_symtabs;
7948 }
7949
7950 return tu_group;
7951 }
7952 \f
7953 /* Partial symbol tables. */
7954
7955 /* Create a psymtab named NAME and assign it to PER_CU.
7956
7957 The caller must fill in the following details:
7958 dirname, textlow, texthigh. */
7959
7960 static struct partial_symtab *
7961 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7962 {
7963 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7964 struct partial_symtab *pst;
7965
7966 pst = start_psymtab_common (objfile, name, 0);
7967
7968 pst->psymtabs_addrmap_supported = 1;
7969
7970 /* This is the glue that links PST into GDB's symbol API. */
7971 pst->read_symtab_private = per_cu;
7972 pst->read_symtab = dwarf2_read_symtab;
7973 per_cu->v.psymtab = pst;
7974
7975 return pst;
7976 }
7977
7978 /* The DATA object passed to process_psymtab_comp_unit_reader has this
7979 type. */
7980
7981 struct process_psymtab_comp_unit_data
7982 {
7983 /* True if we are reading a DW_TAG_partial_unit. */
7984
7985 int want_partial_unit;
7986
7987 /* The "pretend" language that is used if the CU doesn't declare a
7988 language. */
7989
7990 enum language pretend_language;
7991 };
7992
7993 /* die_reader_func for process_psymtab_comp_unit. */
7994
7995 static void
7996 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7997 const gdb_byte *info_ptr,
7998 struct die_info *comp_unit_die,
7999 int has_children,
8000 void *data)
8001 {
8002 struct dwarf2_cu *cu = reader->cu;
8003 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8004 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8005 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8006 CORE_ADDR baseaddr;
8007 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8008 struct partial_symtab *pst;
8009 enum pc_bounds_kind cu_bounds_kind;
8010 const char *filename;
8011 struct process_psymtab_comp_unit_data *info
8012 = (struct process_psymtab_comp_unit_data *) data;
8013
8014 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
8015 return;
8016
8017 gdb_assert (! per_cu->is_debug_types);
8018
8019 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
8020
8021 /* Allocate a new partial symbol table structure. */
8022 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8023 if (filename == NULL)
8024 filename = "";
8025
8026 pst = create_partial_symtab (per_cu, filename);
8027
8028 /* This must be done before calling dwarf2_build_include_psymtabs. */
8029 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8030
8031 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8032
8033 dwarf2_find_base_address (comp_unit_die, cu);
8034
8035 /* Possibly set the default values of LOWPC and HIGHPC from
8036 `DW_AT_ranges'. */
8037 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8038 &best_highpc, cu, pst);
8039 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8040 {
8041 CORE_ADDR low
8042 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8043 - baseaddr);
8044 CORE_ADDR high
8045 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8046 - baseaddr - 1);
8047 /* Store the contiguous range if it is not empty; it can be
8048 empty for CUs with no code. */
8049 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8050 low, high, pst);
8051 }
8052
8053 /* Check if comp unit has_children.
8054 If so, read the rest of the partial symbols from this comp unit.
8055 If not, there's no more debug_info for this comp unit. */
8056 if (has_children)
8057 {
8058 struct partial_die_info *first_die;
8059 CORE_ADDR lowpc, highpc;
8060
8061 lowpc = ((CORE_ADDR) -1);
8062 highpc = ((CORE_ADDR) 0);
8063
8064 first_die = load_partial_dies (reader, info_ptr, 1);
8065
8066 scan_partial_symbols (first_die, &lowpc, &highpc,
8067 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8068
8069 /* If we didn't find a lowpc, set it to highpc to avoid
8070 complaints from `maint check'. */
8071 if (lowpc == ((CORE_ADDR) -1))
8072 lowpc = highpc;
8073
8074 /* If the compilation unit didn't have an explicit address range,
8075 then use the information extracted from its child dies. */
8076 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8077 {
8078 best_lowpc = lowpc;
8079 best_highpc = highpc;
8080 }
8081 }
8082 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
8083 best_lowpc + baseaddr)
8084 - baseaddr);
8085 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
8086 best_highpc + baseaddr)
8087 - baseaddr);
8088
8089 end_psymtab_common (objfile, pst);
8090
8091 if (!cu->per_cu->imported_symtabs_empty ())
8092 {
8093 int i;
8094 int len = cu->per_cu->imported_symtabs_size ();
8095
8096 /* Fill in 'dependencies' here; we fill in 'users' in a
8097 post-pass. */
8098 pst->number_of_dependencies = len;
8099 pst->dependencies
8100 = objfile->partial_symtabs->allocate_dependencies (len);
8101 for (i = 0; i < len; ++i)
8102 {
8103 pst->dependencies[i]
8104 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
8105 }
8106
8107 cu->per_cu->imported_symtabs_free ();
8108 }
8109
8110 /* Get the list of files included in the current compilation unit,
8111 and build a psymtab for each of them. */
8112 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8113
8114 if (dwarf_read_debug)
8115 fprintf_unfiltered (gdb_stdlog,
8116 "Psymtab for %s unit @%s: %s - %s"
8117 ", %d global, %d static syms\n",
8118 per_cu->is_debug_types ? "type" : "comp",
8119 sect_offset_str (per_cu->sect_off),
8120 paddress (gdbarch, pst->text_low (objfile)),
8121 paddress (gdbarch, pst->text_high (objfile)),
8122 pst->n_global_syms, pst->n_static_syms);
8123 }
8124
8125 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8126 Process compilation unit THIS_CU for a psymtab. */
8127
8128 static void
8129 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8130 int want_partial_unit,
8131 enum language pretend_language)
8132 {
8133 /* If this compilation unit was already read in, free the
8134 cached copy in order to read it in again. This is
8135 necessary because we skipped some symbols when we first
8136 read in the compilation unit (see load_partial_dies).
8137 This problem could be avoided, but the benefit is unclear. */
8138 if (this_cu->cu != NULL)
8139 free_one_cached_comp_unit (this_cu);
8140
8141 if (this_cu->is_debug_types)
8142 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8143 build_type_psymtabs_reader, NULL);
8144 else
8145 {
8146 process_psymtab_comp_unit_data info;
8147 info.want_partial_unit = want_partial_unit;
8148 info.pretend_language = pretend_language;
8149 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8150 process_psymtab_comp_unit_reader, &info);
8151 }
8152
8153 /* Age out any secondary CUs. */
8154 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8155 }
8156
8157 /* Reader function for build_type_psymtabs. */
8158
8159 static void
8160 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8161 const gdb_byte *info_ptr,
8162 struct die_info *type_unit_die,
8163 int has_children,
8164 void *data)
8165 {
8166 struct dwarf2_per_objfile *dwarf2_per_objfile
8167 = reader->cu->per_cu->dwarf2_per_objfile;
8168 struct objfile *objfile = dwarf2_per_objfile->objfile;
8169 struct dwarf2_cu *cu = reader->cu;
8170 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8171 struct signatured_type *sig_type;
8172 struct type_unit_group *tu_group;
8173 struct attribute *attr;
8174 struct partial_die_info *first_die;
8175 CORE_ADDR lowpc, highpc;
8176 struct partial_symtab *pst;
8177
8178 gdb_assert (data == NULL);
8179 gdb_assert (per_cu->is_debug_types);
8180 sig_type = (struct signatured_type *) per_cu;
8181
8182 if (! has_children)
8183 return;
8184
8185 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8186 tu_group = get_type_unit_group (cu, attr);
8187
8188 if (tu_group->tus == nullptr)
8189 tu_group->tus = new std::vector<signatured_type *>;
8190 tu_group->tus->push_back (sig_type);
8191
8192 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8193 pst = create_partial_symtab (per_cu, "");
8194 pst->anonymous = 1;
8195
8196 first_die = load_partial_dies (reader, info_ptr, 1);
8197
8198 lowpc = (CORE_ADDR) -1;
8199 highpc = (CORE_ADDR) 0;
8200 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8201
8202 end_psymtab_common (objfile, pst);
8203 }
8204
8205 /* Struct used to sort TUs by their abbreviation table offset. */
8206
8207 struct tu_abbrev_offset
8208 {
8209 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8210 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8211 {}
8212
8213 signatured_type *sig_type;
8214 sect_offset abbrev_offset;
8215 };
8216
8217 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8218
8219 static bool
8220 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8221 const struct tu_abbrev_offset &b)
8222 {
8223 return a.abbrev_offset < b.abbrev_offset;
8224 }
8225
8226 /* Efficiently read all the type units.
8227 This does the bulk of the work for build_type_psymtabs.
8228
8229 The efficiency is because we sort TUs by the abbrev table they use and
8230 only read each abbrev table once. In one program there are 200K TUs
8231 sharing 8K abbrev tables.
8232
8233 The main purpose of this function is to support building the
8234 dwarf2_per_objfile->type_unit_groups table.
8235 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8236 can collapse the search space by grouping them by stmt_list.
8237 The savings can be significant, in the same program from above the 200K TUs
8238 share 8K stmt_list tables.
8239
8240 FUNC is expected to call get_type_unit_group, which will create the
8241 struct type_unit_group if necessary and add it to
8242 dwarf2_per_objfile->type_unit_groups. */
8243
8244 static void
8245 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8246 {
8247 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8248 abbrev_table_up abbrev_table;
8249 sect_offset abbrev_offset;
8250
8251 /* It's up to the caller to not call us multiple times. */
8252 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8253
8254 if (dwarf2_per_objfile->all_type_units.empty ())
8255 return;
8256
8257 /* TUs typically share abbrev tables, and there can be way more TUs than
8258 abbrev tables. Sort by abbrev table to reduce the number of times we
8259 read each abbrev table in.
8260 Alternatives are to punt or to maintain a cache of abbrev tables.
8261 This is simpler and efficient enough for now.
8262
8263 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8264 symtab to use). Typically TUs with the same abbrev offset have the same
8265 stmt_list value too so in practice this should work well.
8266
8267 The basic algorithm here is:
8268
8269 sort TUs by abbrev table
8270 for each TU with same abbrev table:
8271 read abbrev table if first user
8272 read TU top level DIE
8273 [IWBN if DWO skeletons had DW_AT_stmt_list]
8274 call FUNC */
8275
8276 if (dwarf_read_debug)
8277 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8278
8279 /* Sort in a separate table to maintain the order of all_type_units
8280 for .gdb_index: TU indices directly index all_type_units. */
8281 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8282 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8283
8284 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8285 sorted_by_abbrev.emplace_back
8286 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8287 sig_type->per_cu.section,
8288 sig_type->per_cu.sect_off));
8289
8290 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8291 sort_tu_by_abbrev_offset);
8292
8293 abbrev_offset = (sect_offset) ~(unsigned) 0;
8294
8295 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
8296 {
8297 /* Switch to the next abbrev table if necessary. */
8298 if (abbrev_table == NULL
8299 || tu.abbrev_offset != abbrev_offset)
8300 {
8301 abbrev_offset = tu.abbrev_offset;
8302 abbrev_table =
8303 abbrev_table_read_table (dwarf2_per_objfile,
8304 &dwarf2_per_objfile->abbrev,
8305 abbrev_offset);
8306 ++tu_stats->nr_uniq_abbrev_tables;
8307 }
8308
8309 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
8310 0, 0, false, build_type_psymtabs_reader, NULL);
8311 }
8312 }
8313
8314 /* Print collected type unit statistics. */
8315
8316 static void
8317 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8318 {
8319 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8320
8321 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8322 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8323 dwarf2_per_objfile->all_type_units.size ());
8324 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8325 tu_stats->nr_uniq_abbrev_tables);
8326 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8327 tu_stats->nr_symtabs);
8328 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8329 tu_stats->nr_symtab_sharers);
8330 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8331 tu_stats->nr_stmt_less_type_units);
8332 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8333 tu_stats->nr_all_type_units_reallocs);
8334 }
8335
8336 /* Traversal function for build_type_psymtabs. */
8337
8338 static int
8339 build_type_psymtab_dependencies (void **slot, void *info)
8340 {
8341 struct dwarf2_per_objfile *dwarf2_per_objfile
8342 = (struct dwarf2_per_objfile *) info;
8343 struct objfile *objfile = dwarf2_per_objfile->objfile;
8344 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8345 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8346 struct partial_symtab *pst = per_cu->v.psymtab;
8347 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
8348 int i;
8349
8350 gdb_assert (len > 0);
8351 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8352
8353 pst->number_of_dependencies = len;
8354 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8355 for (i = 0; i < len; ++i)
8356 {
8357 struct signatured_type *iter = tu_group->tus->at (i);
8358 gdb_assert (iter->per_cu.is_debug_types);
8359 pst->dependencies[i] = iter->per_cu.v.psymtab;
8360 iter->type_unit_group = tu_group;
8361 }
8362
8363 delete tu_group->tus;
8364 tu_group->tus = nullptr;
8365
8366 return 1;
8367 }
8368
8369 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8370 Build partial symbol tables for the .debug_types comp-units. */
8371
8372 static void
8373 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8374 {
8375 if (! create_all_type_units (dwarf2_per_objfile))
8376 return;
8377
8378 build_type_psymtabs_1 (dwarf2_per_objfile);
8379 }
8380
8381 /* Traversal function for process_skeletonless_type_unit.
8382 Read a TU in a DWO file and build partial symbols for it. */
8383
8384 static int
8385 process_skeletonless_type_unit (void **slot, void *info)
8386 {
8387 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8388 struct dwarf2_per_objfile *dwarf2_per_objfile
8389 = (struct dwarf2_per_objfile *) info;
8390 struct signatured_type find_entry, *entry;
8391
8392 /* If this TU doesn't exist in the global table, add it and read it in. */
8393
8394 if (dwarf2_per_objfile->signatured_types == NULL)
8395 {
8396 dwarf2_per_objfile->signatured_types
8397 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8398 }
8399
8400 find_entry.signature = dwo_unit->signature;
8401 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8402 INSERT);
8403 /* If we've already seen this type there's nothing to do. What's happening
8404 is we're doing our own version of comdat-folding here. */
8405 if (*slot != NULL)
8406 return 1;
8407
8408 /* This does the job that create_all_type_units would have done for
8409 this TU. */
8410 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8411 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8412 *slot = entry;
8413
8414 /* This does the job that build_type_psymtabs_1 would have done. */
8415 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
8416 build_type_psymtabs_reader, NULL);
8417
8418 return 1;
8419 }
8420
8421 /* Traversal function for process_skeletonless_type_units. */
8422
8423 static int
8424 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8425 {
8426 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8427
8428 if (dwo_file->tus != NULL)
8429 {
8430 htab_traverse_noresize (dwo_file->tus,
8431 process_skeletonless_type_unit, info);
8432 }
8433
8434 return 1;
8435 }
8436
8437 /* Scan all TUs of DWO files, verifying we've processed them.
8438 This is needed in case a TU was emitted without its skeleton.
8439 Note: This can't be done until we know what all the DWO files are. */
8440
8441 static void
8442 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8443 {
8444 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8445 if (get_dwp_file (dwarf2_per_objfile) == NULL
8446 && dwarf2_per_objfile->dwo_files != NULL)
8447 {
8448 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
8449 process_dwo_file_for_skeletonless_type_units,
8450 dwarf2_per_objfile);
8451 }
8452 }
8453
8454 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8455
8456 static void
8457 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8458 {
8459 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8460 {
8461 struct partial_symtab *pst = per_cu->v.psymtab;
8462
8463 if (pst == NULL)
8464 continue;
8465
8466 for (int j = 0; j < pst->number_of_dependencies; ++j)
8467 {
8468 /* Set the 'user' field only if it is not already set. */
8469 if (pst->dependencies[j]->user == NULL)
8470 pst->dependencies[j]->user = pst;
8471 }
8472 }
8473 }
8474
8475 /* Build the partial symbol table by doing a quick pass through the
8476 .debug_info and .debug_abbrev sections. */
8477
8478 static void
8479 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8480 {
8481 struct objfile *objfile = dwarf2_per_objfile->objfile;
8482
8483 if (dwarf_read_debug)
8484 {
8485 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8486 objfile_name (objfile));
8487 }
8488
8489 dwarf2_per_objfile->reading_partial_symbols = 1;
8490
8491 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8492
8493 /* Any cached compilation units will be linked by the per-objfile
8494 read_in_chain. Make sure to free them when we're done. */
8495 free_cached_comp_units freer (dwarf2_per_objfile);
8496
8497 build_type_psymtabs (dwarf2_per_objfile);
8498
8499 create_all_comp_units (dwarf2_per_objfile);
8500
8501 /* Create a temporary address map on a temporary obstack. We later
8502 copy this to the final obstack. */
8503 auto_obstack temp_obstack;
8504
8505 scoped_restore save_psymtabs_addrmap
8506 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8507 addrmap_create_mutable (&temp_obstack));
8508
8509 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8510 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8511
8512 /* This has to wait until we read the CUs, we need the list of DWOs. */
8513 process_skeletonless_type_units (dwarf2_per_objfile);
8514
8515 /* Now that all TUs have been processed we can fill in the dependencies. */
8516 if (dwarf2_per_objfile->type_unit_groups != NULL)
8517 {
8518 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8519 build_type_psymtab_dependencies, dwarf2_per_objfile);
8520 }
8521
8522 if (dwarf_read_debug)
8523 print_tu_stats (dwarf2_per_objfile);
8524
8525 set_partial_user (dwarf2_per_objfile);
8526
8527 objfile->partial_symtabs->psymtabs_addrmap
8528 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8529 objfile->partial_symtabs->obstack ());
8530 /* At this point we want to keep the address map. */
8531 save_psymtabs_addrmap.release ();
8532
8533 if (dwarf_read_debug)
8534 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8535 objfile_name (objfile));
8536 }
8537
8538 /* die_reader_func for load_partial_comp_unit. */
8539
8540 static void
8541 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
8542 const gdb_byte *info_ptr,
8543 struct die_info *comp_unit_die,
8544 int has_children,
8545 void *data)
8546 {
8547 struct dwarf2_cu *cu = reader->cu;
8548
8549 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
8550
8551 /* Check if comp unit has_children.
8552 If so, read the rest of the partial symbols from this comp unit.
8553 If not, there's no more debug_info for this comp unit. */
8554 if (has_children)
8555 load_partial_dies (reader, info_ptr, 0);
8556 }
8557
8558 /* Load the partial DIEs for a secondary CU into memory.
8559 This is also used when rereading a primary CU with load_all_dies. */
8560
8561 static void
8562 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8563 {
8564 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
8565 load_partial_comp_unit_reader, NULL);
8566 }
8567
8568 static void
8569 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8570 struct dwarf2_section_info *section,
8571 struct dwarf2_section_info *abbrev_section,
8572 unsigned int is_dwz)
8573 {
8574 const gdb_byte *info_ptr;
8575 struct objfile *objfile = dwarf2_per_objfile->objfile;
8576
8577 if (dwarf_read_debug)
8578 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8579 get_section_name (section),
8580 get_section_file_name (section));
8581
8582 dwarf2_read_section (objfile, section);
8583
8584 info_ptr = section->buffer;
8585
8586 while (info_ptr < section->buffer + section->size)
8587 {
8588 struct dwarf2_per_cu_data *this_cu;
8589
8590 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8591
8592 comp_unit_head cu_header;
8593 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8594 abbrev_section, info_ptr,
8595 rcuh_kind::COMPILE);
8596
8597 /* Save the compilation unit for later lookup. */
8598 if (cu_header.unit_type != DW_UT_type)
8599 {
8600 this_cu = XOBNEW (&objfile->objfile_obstack,
8601 struct dwarf2_per_cu_data);
8602 memset (this_cu, 0, sizeof (*this_cu));
8603 }
8604 else
8605 {
8606 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8607 struct signatured_type);
8608 memset (sig_type, 0, sizeof (*sig_type));
8609 sig_type->signature = cu_header.signature;
8610 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8611 this_cu = &sig_type->per_cu;
8612 }
8613 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8614 this_cu->sect_off = sect_off;
8615 this_cu->length = cu_header.length + cu_header.initial_length_size;
8616 this_cu->is_dwz = is_dwz;
8617 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8618 this_cu->section = section;
8619
8620 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8621
8622 info_ptr = info_ptr + this_cu->length;
8623 }
8624 }
8625
8626 /* Create a list of all compilation units in OBJFILE.
8627 This is only done for -readnow and building partial symtabs. */
8628
8629 static void
8630 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8631 {
8632 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8633 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8634 &dwarf2_per_objfile->abbrev, 0);
8635
8636 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8637 if (dwz != NULL)
8638 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8639 1);
8640 }
8641
8642 /* Process all loaded DIEs for compilation unit CU, starting at
8643 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8644 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8645 DW_AT_ranges). See the comments of add_partial_subprogram on how
8646 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8647
8648 static void
8649 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8650 CORE_ADDR *highpc, int set_addrmap,
8651 struct dwarf2_cu *cu)
8652 {
8653 struct partial_die_info *pdi;
8654
8655 /* Now, march along the PDI's, descending into ones which have
8656 interesting children but skipping the children of the other ones,
8657 until we reach the end of the compilation unit. */
8658
8659 pdi = first_die;
8660
8661 while (pdi != NULL)
8662 {
8663 pdi->fixup (cu);
8664
8665 /* Anonymous namespaces or modules have no name but have interesting
8666 children, so we need to look at them. Ditto for anonymous
8667 enums. */
8668
8669 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8670 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8671 || pdi->tag == DW_TAG_imported_unit
8672 || pdi->tag == DW_TAG_inlined_subroutine)
8673 {
8674 switch (pdi->tag)
8675 {
8676 case DW_TAG_subprogram:
8677 case DW_TAG_inlined_subroutine:
8678 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8679 break;
8680 case DW_TAG_constant:
8681 case DW_TAG_variable:
8682 case DW_TAG_typedef:
8683 case DW_TAG_union_type:
8684 if (!pdi->is_declaration)
8685 {
8686 add_partial_symbol (pdi, cu);
8687 }
8688 break;
8689 case DW_TAG_class_type:
8690 case DW_TAG_interface_type:
8691 case DW_TAG_structure_type:
8692 if (!pdi->is_declaration)
8693 {
8694 add_partial_symbol (pdi, cu);
8695 }
8696 if ((cu->language == language_rust
8697 || cu->language == language_cplus) && pdi->has_children)
8698 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8699 set_addrmap, cu);
8700 break;
8701 case DW_TAG_enumeration_type:
8702 if (!pdi->is_declaration)
8703 add_partial_enumeration (pdi, cu);
8704 break;
8705 case DW_TAG_base_type:
8706 case DW_TAG_subrange_type:
8707 /* File scope base type definitions are added to the partial
8708 symbol table. */
8709 add_partial_symbol (pdi, cu);
8710 break;
8711 case DW_TAG_namespace:
8712 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8713 break;
8714 case DW_TAG_module:
8715 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8716 break;
8717 case DW_TAG_imported_unit:
8718 {
8719 struct dwarf2_per_cu_data *per_cu;
8720
8721 /* For now we don't handle imported units in type units. */
8722 if (cu->per_cu->is_debug_types)
8723 {
8724 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8725 " supported in type units [in module %s]"),
8726 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8727 }
8728
8729 per_cu = dwarf2_find_containing_comp_unit
8730 (pdi->d.sect_off, pdi->is_dwz,
8731 cu->per_cu->dwarf2_per_objfile);
8732
8733 /* Go read the partial unit, if needed. */
8734 if (per_cu->v.psymtab == NULL)
8735 process_psymtab_comp_unit (per_cu, 1, cu->language);
8736
8737 cu->per_cu->imported_symtabs_push (per_cu);
8738 }
8739 break;
8740 case DW_TAG_imported_declaration:
8741 add_partial_symbol (pdi, cu);
8742 break;
8743 default:
8744 break;
8745 }
8746 }
8747
8748 /* If the die has a sibling, skip to the sibling. */
8749
8750 pdi = pdi->die_sibling;
8751 }
8752 }
8753
8754 /* Functions used to compute the fully scoped name of a partial DIE.
8755
8756 Normally, this is simple. For C++, the parent DIE's fully scoped
8757 name is concatenated with "::" and the partial DIE's name.
8758 Enumerators are an exception; they use the scope of their parent
8759 enumeration type, i.e. the name of the enumeration type is not
8760 prepended to the enumerator.
8761
8762 There are two complexities. One is DW_AT_specification; in this
8763 case "parent" means the parent of the target of the specification,
8764 instead of the direct parent of the DIE. The other is compilers
8765 which do not emit DW_TAG_namespace; in this case we try to guess
8766 the fully qualified name of structure types from their members'
8767 linkage names. This must be done using the DIE's children rather
8768 than the children of any DW_AT_specification target. We only need
8769 to do this for structures at the top level, i.e. if the target of
8770 any DW_AT_specification (if any; otherwise the DIE itself) does not
8771 have a parent. */
8772
8773 /* Compute the scope prefix associated with PDI's parent, in
8774 compilation unit CU. The result will be allocated on CU's
8775 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8776 field. NULL is returned if no prefix is necessary. */
8777 static const char *
8778 partial_die_parent_scope (struct partial_die_info *pdi,
8779 struct dwarf2_cu *cu)
8780 {
8781 const char *grandparent_scope;
8782 struct partial_die_info *parent, *real_pdi;
8783
8784 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8785 then this means the parent of the specification DIE. */
8786
8787 real_pdi = pdi;
8788 while (real_pdi->has_specification)
8789 {
8790 auto res = find_partial_die (real_pdi->spec_offset,
8791 real_pdi->spec_is_dwz, cu);
8792 real_pdi = res.pdi;
8793 cu = res.cu;
8794 }
8795
8796 parent = real_pdi->die_parent;
8797 if (parent == NULL)
8798 return NULL;
8799
8800 if (parent->scope_set)
8801 return parent->scope;
8802
8803 parent->fixup (cu);
8804
8805 grandparent_scope = partial_die_parent_scope (parent, cu);
8806
8807 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8808 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8809 Work around this problem here. */
8810 if (cu->language == language_cplus
8811 && parent->tag == DW_TAG_namespace
8812 && strcmp (parent->name, "::") == 0
8813 && grandparent_scope == NULL)
8814 {
8815 parent->scope = NULL;
8816 parent->scope_set = 1;
8817 return NULL;
8818 }
8819
8820 /* Nested subroutines in Fortran get a prefix. */
8821 if (pdi->tag == DW_TAG_enumerator)
8822 /* Enumerators should not get the name of the enumeration as a prefix. */
8823 parent->scope = grandparent_scope;
8824 else if (parent->tag == DW_TAG_namespace
8825 || parent->tag == DW_TAG_module
8826 || parent->tag == DW_TAG_structure_type
8827 || parent->tag == DW_TAG_class_type
8828 || parent->tag == DW_TAG_interface_type
8829 || parent->tag == DW_TAG_union_type
8830 || parent->tag == DW_TAG_enumeration_type
8831 || (cu->language == language_fortran
8832 && parent->tag == DW_TAG_subprogram
8833 && pdi->tag == DW_TAG_subprogram))
8834 {
8835 if (grandparent_scope == NULL)
8836 parent->scope = parent->name;
8837 else
8838 parent->scope = typename_concat (&cu->comp_unit_obstack,
8839 grandparent_scope,
8840 parent->name, 0, cu);
8841 }
8842 else
8843 {
8844 /* FIXME drow/2004-04-01: What should we be doing with
8845 function-local names? For partial symbols, we should probably be
8846 ignoring them. */
8847 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8848 dwarf_tag_name (parent->tag),
8849 sect_offset_str (pdi->sect_off));
8850 parent->scope = grandparent_scope;
8851 }
8852
8853 parent->scope_set = 1;
8854 return parent->scope;
8855 }
8856
8857 /* Return the fully scoped name associated with PDI, from compilation unit
8858 CU. The result will be allocated with malloc. */
8859
8860 static char *
8861 partial_die_full_name (struct partial_die_info *pdi,
8862 struct dwarf2_cu *cu)
8863 {
8864 const char *parent_scope;
8865
8866 /* If this is a template instantiation, we can not work out the
8867 template arguments from partial DIEs. So, unfortunately, we have
8868 to go through the full DIEs. At least any work we do building
8869 types here will be reused if full symbols are loaded later. */
8870 if (pdi->has_template_arguments)
8871 {
8872 pdi->fixup (cu);
8873
8874 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8875 {
8876 struct die_info *die;
8877 struct attribute attr;
8878 struct dwarf2_cu *ref_cu = cu;
8879
8880 /* DW_FORM_ref_addr is using section offset. */
8881 attr.name = (enum dwarf_attribute) 0;
8882 attr.form = DW_FORM_ref_addr;
8883 attr.u.unsnd = to_underlying (pdi->sect_off);
8884 die = follow_die_ref (NULL, &attr, &ref_cu);
8885
8886 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8887 }
8888 }
8889
8890 parent_scope = partial_die_parent_scope (pdi, cu);
8891 if (parent_scope == NULL)
8892 return NULL;
8893 else
8894 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
8895 }
8896
8897 static void
8898 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8899 {
8900 struct dwarf2_per_objfile *dwarf2_per_objfile
8901 = cu->per_cu->dwarf2_per_objfile;
8902 struct objfile *objfile = dwarf2_per_objfile->objfile;
8903 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8904 CORE_ADDR addr = 0;
8905 const char *actual_name = NULL;
8906 CORE_ADDR baseaddr;
8907 char *built_actual_name;
8908
8909 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8910
8911 built_actual_name = partial_die_full_name (pdi, cu);
8912 if (built_actual_name != NULL)
8913 actual_name = built_actual_name;
8914
8915 if (actual_name == NULL)
8916 actual_name = pdi->name;
8917
8918 switch (pdi->tag)
8919 {
8920 case DW_TAG_inlined_subroutine:
8921 case DW_TAG_subprogram:
8922 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8923 - baseaddr);
8924 if (pdi->is_external
8925 || cu->language == language_ada
8926 || (cu->language == language_fortran
8927 && pdi->die_parent != NULL
8928 && pdi->die_parent->tag == DW_TAG_subprogram))
8929 {
8930 /* Normally, only "external" DIEs are part of the global scope.
8931 But in Ada and Fortran, we want to be able to access nested
8932 procedures globally. So all Ada and Fortran subprograms are
8933 stored in the global scope. */
8934 add_psymbol_to_list (actual_name, strlen (actual_name),
8935 built_actual_name != NULL,
8936 VAR_DOMAIN, LOC_BLOCK,
8937 SECT_OFF_TEXT (objfile),
8938 psymbol_placement::GLOBAL,
8939 addr,
8940 cu->language, objfile);
8941 }
8942 else
8943 {
8944 add_psymbol_to_list (actual_name, strlen (actual_name),
8945 built_actual_name != NULL,
8946 VAR_DOMAIN, LOC_BLOCK,
8947 SECT_OFF_TEXT (objfile),
8948 psymbol_placement::STATIC,
8949 addr, cu->language, objfile);
8950 }
8951
8952 if (pdi->main_subprogram && actual_name != NULL)
8953 set_objfile_main_name (objfile, actual_name, cu->language);
8954 break;
8955 case DW_TAG_constant:
8956 add_psymbol_to_list (actual_name, strlen (actual_name),
8957 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8958 -1, (pdi->is_external
8959 ? psymbol_placement::GLOBAL
8960 : psymbol_placement::STATIC),
8961 0, cu->language, objfile);
8962 break;
8963 case DW_TAG_variable:
8964 if (pdi->d.locdesc)
8965 addr = decode_locdesc (pdi->d.locdesc, cu);
8966
8967 if (pdi->d.locdesc
8968 && addr == 0
8969 && !dwarf2_per_objfile->has_section_at_zero)
8970 {
8971 /* A global or static variable may also have been stripped
8972 out by the linker if unused, in which case its address
8973 will be nullified; do not add such variables into partial
8974 symbol table then. */
8975 }
8976 else if (pdi->is_external)
8977 {
8978 /* Global Variable.
8979 Don't enter into the minimal symbol tables as there is
8980 a minimal symbol table entry from the ELF symbols already.
8981 Enter into partial symbol table if it has a location
8982 descriptor or a type.
8983 If the location descriptor is missing, new_symbol will create
8984 a LOC_UNRESOLVED symbol, the address of the variable will then
8985 be determined from the minimal symbol table whenever the variable
8986 is referenced.
8987 The address for the partial symbol table entry is not
8988 used by GDB, but it comes in handy for debugging partial symbol
8989 table building. */
8990
8991 if (pdi->d.locdesc || pdi->has_type)
8992 add_psymbol_to_list (actual_name, strlen (actual_name),
8993 built_actual_name != NULL,
8994 VAR_DOMAIN, LOC_STATIC,
8995 SECT_OFF_TEXT (objfile),
8996 psymbol_placement::GLOBAL,
8997 addr, cu->language, objfile);
8998 }
8999 else
9000 {
9001 int has_loc = pdi->d.locdesc != NULL;
9002
9003 /* Static Variable. Skip symbols whose value we cannot know (those
9004 without location descriptors or constant values). */
9005 if (!has_loc && !pdi->has_const_value)
9006 {
9007 xfree (built_actual_name);
9008 return;
9009 }
9010
9011 add_psymbol_to_list (actual_name, strlen (actual_name),
9012 built_actual_name != NULL,
9013 VAR_DOMAIN, LOC_STATIC,
9014 SECT_OFF_TEXT (objfile),
9015 psymbol_placement::STATIC,
9016 has_loc ? addr : 0,
9017 cu->language, objfile);
9018 }
9019 break;
9020 case DW_TAG_typedef:
9021 case DW_TAG_base_type:
9022 case DW_TAG_subrange_type:
9023 add_psymbol_to_list (actual_name, strlen (actual_name),
9024 built_actual_name != NULL,
9025 VAR_DOMAIN, LOC_TYPEDEF, -1,
9026 psymbol_placement::STATIC,
9027 0, cu->language, objfile);
9028 break;
9029 case DW_TAG_imported_declaration:
9030 case DW_TAG_namespace:
9031 add_psymbol_to_list (actual_name, strlen (actual_name),
9032 built_actual_name != NULL,
9033 VAR_DOMAIN, LOC_TYPEDEF, -1,
9034 psymbol_placement::GLOBAL,
9035 0, cu->language, objfile);
9036 break;
9037 case DW_TAG_module:
9038 /* With Fortran 77 there might be a "BLOCK DATA" module
9039 available without any name. If so, we skip the module as it
9040 doesn't bring any value. */
9041 if (actual_name != nullptr)
9042 add_psymbol_to_list (actual_name, strlen (actual_name),
9043 built_actual_name != NULL,
9044 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9045 psymbol_placement::GLOBAL,
9046 0, cu->language, objfile);
9047 break;
9048 case DW_TAG_class_type:
9049 case DW_TAG_interface_type:
9050 case DW_TAG_structure_type:
9051 case DW_TAG_union_type:
9052 case DW_TAG_enumeration_type:
9053 /* Skip external references. The DWARF standard says in the section
9054 about "Structure, Union, and Class Type Entries": "An incomplete
9055 structure, union or class type is represented by a structure,
9056 union or class entry that does not have a byte size attribute
9057 and that has a DW_AT_declaration attribute." */
9058 if (!pdi->has_byte_size && pdi->is_declaration)
9059 {
9060 xfree (built_actual_name);
9061 return;
9062 }
9063
9064 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9065 static vs. global. */
9066 add_psymbol_to_list (actual_name, strlen (actual_name),
9067 built_actual_name != NULL,
9068 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9069 cu->language == language_cplus
9070 ? psymbol_placement::GLOBAL
9071 : psymbol_placement::STATIC,
9072 0, cu->language, objfile);
9073
9074 break;
9075 case DW_TAG_enumerator:
9076 add_psymbol_to_list (actual_name, strlen (actual_name),
9077 built_actual_name != NULL,
9078 VAR_DOMAIN, LOC_CONST, -1,
9079 cu->language == language_cplus
9080 ? psymbol_placement::GLOBAL
9081 : psymbol_placement::STATIC,
9082 0, cu->language, objfile);
9083 break;
9084 default:
9085 break;
9086 }
9087
9088 xfree (built_actual_name);
9089 }
9090
9091 /* Read a partial die corresponding to a namespace; also, add a symbol
9092 corresponding to that namespace to the symbol table. NAMESPACE is
9093 the name of the enclosing namespace. */
9094
9095 static void
9096 add_partial_namespace (struct partial_die_info *pdi,
9097 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9098 int set_addrmap, struct dwarf2_cu *cu)
9099 {
9100 /* Add a symbol for the namespace. */
9101
9102 add_partial_symbol (pdi, cu);
9103
9104 /* Now scan partial symbols in that namespace. */
9105
9106 if (pdi->has_children)
9107 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9108 }
9109
9110 /* Read a partial die corresponding to a Fortran module. */
9111
9112 static void
9113 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9114 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9115 {
9116 /* Add a symbol for the namespace. */
9117
9118 add_partial_symbol (pdi, cu);
9119
9120 /* Now scan partial symbols in that module. */
9121
9122 if (pdi->has_children)
9123 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9124 }
9125
9126 /* Read a partial die corresponding to a subprogram or an inlined
9127 subprogram and create a partial symbol for that subprogram.
9128 When the CU language allows it, this routine also defines a partial
9129 symbol for each nested subprogram that this subprogram contains.
9130 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9131 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9132
9133 PDI may also be a lexical block, in which case we simply search
9134 recursively for subprograms defined inside that lexical block.
9135 Again, this is only performed when the CU language allows this
9136 type of definitions. */
9137
9138 static void
9139 add_partial_subprogram (struct partial_die_info *pdi,
9140 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9141 int set_addrmap, struct dwarf2_cu *cu)
9142 {
9143 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9144 {
9145 if (pdi->has_pc_info)
9146 {
9147 if (pdi->lowpc < *lowpc)
9148 *lowpc = pdi->lowpc;
9149 if (pdi->highpc > *highpc)
9150 *highpc = pdi->highpc;
9151 if (set_addrmap)
9152 {
9153 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9154 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9155 CORE_ADDR baseaddr;
9156 CORE_ADDR this_highpc;
9157 CORE_ADDR this_lowpc;
9158
9159 baseaddr = ANOFFSET (objfile->section_offsets,
9160 SECT_OFF_TEXT (objfile));
9161 this_lowpc
9162 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9163 pdi->lowpc + baseaddr)
9164 - baseaddr);
9165 this_highpc
9166 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9167 pdi->highpc + baseaddr)
9168 - baseaddr);
9169 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
9170 this_lowpc, this_highpc - 1,
9171 cu->per_cu->v.psymtab);
9172 }
9173 }
9174
9175 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9176 {
9177 if (!pdi->is_declaration)
9178 /* Ignore subprogram DIEs that do not have a name, they are
9179 illegal. Do not emit a complaint at this point, we will
9180 do so when we convert this psymtab into a symtab. */
9181 if (pdi->name)
9182 add_partial_symbol (pdi, cu);
9183 }
9184 }
9185
9186 if (! pdi->has_children)
9187 return;
9188
9189 if (cu->language == language_ada || cu->language == language_fortran)
9190 {
9191 pdi = pdi->die_child;
9192 while (pdi != NULL)
9193 {
9194 pdi->fixup (cu);
9195 if (pdi->tag == DW_TAG_subprogram
9196 || pdi->tag == DW_TAG_inlined_subroutine
9197 || pdi->tag == DW_TAG_lexical_block)
9198 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9199 pdi = pdi->die_sibling;
9200 }
9201 }
9202 }
9203
9204 /* Read a partial die corresponding to an enumeration type. */
9205
9206 static void
9207 add_partial_enumeration (struct partial_die_info *enum_pdi,
9208 struct dwarf2_cu *cu)
9209 {
9210 struct partial_die_info *pdi;
9211
9212 if (enum_pdi->name != NULL)
9213 add_partial_symbol (enum_pdi, cu);
9214
9215 pdi = enum_pdi->die_child;
9216 while (pdi)
9217 {
9218 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9219 complaint (_("malformed enumerator DIE ignored"));
9220 else
9221 add_partial_symbol (pdi, cu);
9222 pdi = pdi->die_sibling;
9223 }
9224 }
9225
9226 /* Return the initial uleb128 in the die at INFO_PTR. */
9227
9228 static unsigned int
9229 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9230 {
9231 unsigned int bytes_read;
9232
9233 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9234 }
9235
9236 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9237 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9238
9239 Return the corresponding abbrev, or NULL if the number is zero (indicating
9240 an empty DIE). In either case *BYTES_READ will be set to the length of
9241 the initial number. */
9242
9243 static struct abbrev_info *
9244 peek_die_abbrev (const die_reader_specs &reader,
9245 const gdb_byte *info_ptr, unsigned int *bytes_read)
9246 {
9247 dwarf2_cu *cu = reader.cu;
9248 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9249 unsigned int abbrev_number
9250 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9251
9252 if (abbrev_number == 0)
9253 return NULL;
9254
9255 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9256 if (!abbrev)
9257 {
9258 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9259 " at offset %s [in module %s]"),
9260 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9261 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9262 }
9263
9264 return abbrev;
9265 }
9266
9267 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9268 Returns a pointer to the end of a series of DIEs, terminated by an empty
9269 DIE. Any children of the skipped DIEs will also be skipped. */
9270
9271 static const gdb_byte *
9272 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9273 {
9274 while (1)
9275 {
9276 unsigned int bytes_read;
9277 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9278
9279 if (abbrev == NULL)
9280 return info_ptr + bytes_read;
9281 else
9282 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9283 }
9284 }
9285
9286 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9287 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9288 abbrev corresponding to that skipped uleb128 should be passed in
9289 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9290 children. */
9291
9292 static const gdb_byte *
9293 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9294 struct abbrev_info *abbrev)
9295 {
9296 unsigned int bytes_read;
9297 struct attribute attr;
9298 bfd *abfd = reader->abfd;
9299 struct dwarf2_cu *cu = reader->cu;
9300 const gdb_byte *buffer = reader->buffer;
9301 const gdb_byte *buffer_end = reader->buffer_end;
9302 unsigned int form, i;
9303
9304 for (i = 0; i < abbrev->num_attrs; i++)
9305 {
9306 /* The only abbrev we care about is DW_AT_sibling. */
9307 if (abbrev->attrs[i].name == DW_AT_sibling)
9308 {
9309 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9310 if (attr.form == DW_FORM_ref_addr)
9311 complaint (_("ignoring absolute DW_AT_sibling"));
9312 else
9313 {
9314 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9315 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9316
9317 if (sibling_ptr < info_ptr)
9318 complaint (_("DW_AT_sibling points backwards"));
9319 else if (sibling_ptr > reader->buffer_end)
9320 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9321 else
9322 return sibling_ptr;
9323 }
9324 }
9325
9326 /* If it isn't DW_AT_sibling, skip this attribute. */
9327 form = abbrev->attrs[i].form;
9328 skip_attribute:
9329 switch (form)
9330 {
9331 case DW_FORM_ref_addr:
9332 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9333 and later it is offset sized. */
9334 if (cu->header.version == 2)
9335 info_ptr += cu->header.addr_size;
9336 else
9337 info_ptr += cu->header.offset_size;
9338 break;
9339 case DW_FORM_GNU_ref_alt:
9340 info_ptr += cu->header.offset_size;
9341 break;
9342 case DW_FORM_addr:
9343 info_ptr += cu->header.addr_size;
9344 break;
9345 case DW_FORM_data1:
9346 case DW_FORM_ref1:
9347 case DW_FORM_flag:
9348 case DW_FORM_strx1:
9349 info_ptr += 1;
9350 break;
9351 case DW_FORM_flag_present:
9352 case DW_FORM_implicit_const:
9353 break;
9354 case DW_FORM_data2:
9355 case DW_FORM_ref2:
9356 case DW_FORM_strx2:
9357 info_ptr += 2;
9358 break;
9359 case DW_FORM_strx3:
9360 info_ptr += 3;
9361 break;
9362 case DW_FORM_data4:
9363 case DW_FORM_ref4:
9364 case DW_FORM_strx4:
9365 info_ptr += 4;
9366 break;
9367 case DW_FORM_data8:
9368 case DW_FORM_ref8:
9369 case DW_FORM_ref_sig8:
9370 info_ptr += 8;
9371 break;
9372 case DW_FORM_data16:
9373 info_ptr += 16;
9374 break;
9375 case DW_FORM_string:
9376 read_direct_string (abfd, info_ptr, &bytes_read);
9377 info_ptr += bytes_read;
9378 break;
9379 case DW_FORM_sec_offset:
9380 case DW_FORM_strp:
9381 case DW_FORM_GNU_strp_alt:
9382 info_ptr += cu->header.offset_size;
9383 break;
9384 case DW_FORM_exprloc:
9385 case DW_FORM_block:
9386 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9387 info_ptr += bytes_read;
9388 break;
9389 case DW_FORM_block1:
9390 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9391 break;
9392 case DW_FORM_block2:
9393 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9394 break;
9395 case DW_FORM_block4:
9396 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9397 break;
9398 case DW_FORM_addrx:
9399 case DW_FORM_strx:
9400 case DW_FORM_sdata:
9401 case DW_FORM_udata:
9402 case DW_FORM_ref_udata:
9403 case DW_FORM_GNU_addr_index:
9404 case DW_FORM_GNU_str_index:
9405 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9406 break;
9407 case DW_FORM_indirect:
9408 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9409 info_ptr += bytes_read;
9410 /* We need to continue parsing from here, so just go back to
9411 the top. */
9412 goto skip_attribute;
9413
9414 default:
9415 error (_("Dwarf Error: Cannot handle %s "
9416 "in DWARF reader [in module %s]"),
9417 dwarf_form_name (form),
9418 bfd_get_filename (abfd));
9419 }
9420 }
9421
9422 if (abbrev->has_children)
9423 return skip_children (reader, info_ptr);
9424 else
9425 return info_ptr;
9426 }
9427
9428 /* Locate ORIG_PDI's sibling.
9429 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9430
9431 static const gdb_byte *
9432 locate_pdi_sibling (const struct die_reader_specs *reader,
9433 struct partial_die_info *orig_pdi,
9434 const gdb_byte *info_ptr)
9435 {
9436 /* Do we know the sibling already? */
9437
9438 if (orig_pdi->sibling)
9439 return orig_pdi->sibling;
9440
9441 /* Are there any children to deal with? */
9442
9443 if (!orig_pdi->has_children)
9444 return info_ptr;
9445
9446 /* Skip the children the long way. */
9447
9448 return skip_children (reader, info_ptr);
9449 }
9450
9451 /* Expand this partial symbol table into a full symbol table. SELF is
9452 not NULL. */
9453
9454 static void
9455 dwarf2_read_symtab (struct partial_symtab *self,
9456 struct objfile *objfile)
9457 {
9458 struct dwarf2_per_objfile *dwarf2_per_objfile
9459 = get_dwarf2_per_objfile (objfile);
9460
9461 if (self->readin)
9462 {
9463 warning (_("bug: psymtab for %s is already read in."),
9464 self->filename);
9465 }
9466 else
9467 {
9468 if (info_verbose)
9469 {
9470 printf_filtered (_("Reading in symbols for %s..."),
9471 self->filename);
9472 gdb_flush (gdb_stdout);
9473 }
9474
9475 /* If this psymtab is constructed from a debug-only objfile, the
9476 has_section_at_zero flag will not necessarily be correct. We
9477 can get the correct value for this flag by looking at the data
9478 associated with the (presumably stripped) associated objfile. */
9479 if (objfile->separate_debug_objfile_backlink)
9480 {
9481 struct dwarf2_per_objfile *dpo_backlink
9482 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9483
9484 dwarf2_per_objfile->has_section_at_zero
9485 = dpo_backlink->has_section_at_zero;
9486 }
9487
9488 dwarf2_per_objfile->reading_partial_symbols = 0;
9489
9490 psymtab_to_symtab_1 (self);
9491
9492 /* Finish up the debug error message. */
9493 if (info_verbose)
9494 printf_filtered (_("done.\n"));
9495 }
9496
9497 process_cu_includes (dwarf2_per_objfile);
9498 }
9499 \f
9500 /* Reading in full CUs. */
9501
9502 /* Add PER_CU to the queue. */
9503
9504 static void
9505 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9506 enum language pretend_language)
9507 {
9508 struct dwarf2_queue_item *item;
9509
9510 per_cu->queued = 1;
9511 item = XNEW (struct dwarf2_queue_item);
9512 item->per_cu = per_cu;
9513 item->pretend_language = pretend_language;
9514 item->next = NULL;
9515
9516 if (dwarf2_queue == NULL)
9517 dwarf2_queue = item;
9518 else
9519 dwarf2_queue_tail->next = item;
9520
9521 dwarf2_queue_tail = item;
9522 }
9523
9524 /* If PER_CU is not yet queued, add it to the queue.
9525 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9526 dependency.
9527 The result is non-zero if PER_CU was queued, otherwise the result is zero
9528 meaning either PER_CU is already queued or it is already loaded.
9529
9530 N.B. There is an invariant here that if a CU is queued then it is loaded.
9531 The caller is required to load PER_CU if we return non-zero. */
9532
9533 static int
9534 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9535 struct dwarf2_per_cu_data *per_cu,
9536 enum language pretend_language)
9537 {
9538 /* We may arrive here during partial symbol reading, if we need full
9539 DIEs to process an unusual case (e.g. template arguments). Do
9540 not queue PER_CU, just tell our caller to load its DIEs. */
9541 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9542 {
9543 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9544 return 1;
9545 return 0;
9546 }
9547
9548 /* Mark the dependence relation so that we don't flush PER_CU
9549 too early. */
9550 if (dependent_cu != NULL)
9551 dwarf2_add_dependence (dependent_cu, per_cu);
9552
9553 /* If it's already on the queue, we have nothing to do. */
9554 if (per_cu->queued)
9555 return 0;
9556
9557 /* If the compilation unit is already loaded, just mark it as
9558 used. */
9559 if (per_cu->cu != NULL)
9560 {
9561 per_cu->cu->last_used = 0;
9562 return 0;
9563 }
9564
9565 /* Add it to the queue. */
9566 queue_comp_unit (per_cu, pretend_language);
9567
9568 return 1;
9569 }
9570
9571 /* Process the queue. */
9572
9573 static void
9574 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9575 {
9576 struct dwarf2_queue_item *item, *next_item;
9577
9578 if (dwarf_read_debug)
9579 {
9580 fprintf_unfiltered (gdb_stdlog,
9581 "Expanding one or more symtabs of objfile %s ...\n",
9582 objfile_name (dwarf2_per_objfile->objfile));
9583 }
9584
9585 /* The queue starts out with one item, but following a DIE reference
9586 may load a new CU, adding it to the end of the queue. */
9587 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9588 {
9589 if ((dwarf2_per_objfile->using_index
9590 ? !item->per_cu->v.quick->compunit_symtab
9591 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9592 /* Skip dummy CUs. */
9593 && item->per_cu->cu != NULL)
9594 {
9595 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9596 unsigned int debug_print_threshold;
9597 char buf[100];
9598
9599 if (per_cu->is_debug_types)
9600 {
9601 struct signatured_type *sig_type =
9602 (struct signatured_type *) per_cu;
9603
9604 sprintf (buf, "TU %s at offset %s",
9605 hex_string (sig_type->signature),
9606 sect_offset_str (per_cu->sect_off));
9607 /* There can be 100s of TUs.
9608 Only print them in verbose mode. */
9609 debug_print_threshold = 2;
9610 }
9611 else
9612 {
9613 sprintf (buf, "CU at offset %s",
9614 sect_offset_str (per_cu->sect_off));
9615 debug_print_threshold = 1;
9616 }
9617
9618 if (dwarf_read_debug >= debug_print_threshold)
9619 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9620
9621 if (per_cu->is_debug_types)
9622 process_full_type_unit (per_cu, item->pretend_language);
9623 else
9624 process_full_comp_unit (per_cu, item->pretend_language);
9625
9626 if (dwarf_read_debug >= debug_print_threshold)
9627 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9628 }
9629
9630 item->per_cu->queued = 0;
9631 next_item = item->next;
9632 xfree (item);
9633 }
9634
9635 dwarf2_queue_tail = NULL;
9636
9637 if (dwarf_read_debug)
9638 {
9639 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9640 objfile_name (dwarf2_per_objfile->objfile));
9641 }
9642 }
9643
9644 /* Read in full symbols for PST, and anything it depends on. */
9645
9646 static void
9647 psymtab_to_symtab_1 (struct partial_symtab *pst)
9648 {
9649 struct dwarf2_per_cu_data *per_cu;
9650 int i;
9651
9652 if (pst->readin)
9653 return;
9654
9655 for (i = 0; i < pst->number_of_dependencies; i++)
9656 if (!pst->dependencies[i]->readin
9657 && pst->dependencies[i]->user == NULL)
9658 {
9659 /* Inform about additional files that need to be read in. */
9660 if (info_verbose)
9661 {
9662 /* FIXME: i18n: Need to make this a single string. */
9663 fputs_filtered (" ", gdb_stdout);
9664 wrap_here ("");
9665 fputs_filtered ("and ", gdb_stdout);
9666 wrap_here ("");
9667 printf_filtered ("%s...", pst->dependencies[i]->filename);
9668 wrap_here (""); /* Flush output. */
9669 gdb_flush (gdb_stdout);
9670 }
9671 psymtab_to_symtab_1 (pst->dependencies[i]);
9672 }
9673
9674 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9675
9676 if (per_cu == NULL)
9677 {
9678 /* It's an include file, no symbols to read for it.
9679 Everything is in the parent symtab. */
9680 pst->readin = 1;
9681 return;
9682 }
9683
9684 dw2_do_instantiate_symtab (per_cu, false);
9685 }
9686
9687 /* Trivial hash function for die_info: the hash value of a DIE
9688 is its offset in .debug_info for this objfile. */
9689
9690 static hashval_t
9691 die_hash (const void *item)
9692 {
9693 const struct die_info *die = (const struct die_info *) item;
9694
9695 return to_underlying (die->sect_off);
9696 }
9697
9698 /* Trivial comparison function for die_info structures: two DIEs
9699 are equal if they have the same offset. */
9700
9701 static int
9702 die_eq (const void *item_lhs, const void *item_rhs)
9703 {
9704 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9705 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9706
9707 return die_lhs->sect_off == die_rhs->sect_off;
9708 }
9709
9710 /* die_reader_func for load_full_comp_unit.
9711 This is identical to read_signatured_type_reader,
9712 but is kept separate for now. */
9713
9714 static void
9715 load_full_comp_unit_reader (const struct die_reader_specs *reader,
9716 const gdb_byte *info_ptr,
9717 struct die_info *comp_unit_die,
9718 int has_children,
9719 void *data)
9720 {
9721 struct dwarf2_cu *cu = reader->cu;
9722 enum language *language_ptr = (enum language *) data;
9723
9724 gdb_assert (cu->die_hash == NULL);
9725 cu->die_hash =
9726 htab_create_alloc_ex (cu->header.length / 12,
9727 die_hash,
9728 die_eq,
9729 NULL,
9730 &cu->comp_unit_obstack,
9731 hashtab_obstack_allocate,
9732 dummy_obstack_deallocate);
9733
9734 if (has_children)
9735 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9736 &info_ptr, comp_unit_die);
9737 cu->dies = comp_unit_die;
9738 /* comp_unit_die is not stored in die_hash, no need. */
9739
9740 /* We try not to read any attributes in this function, because not
9741 all CUs needed for references have been loaded yet, and symbol
9742 table processing isn't initialized. But we have to set the CU language,
9743 or we won't be able to build types correctly.
9744 Similarly, if we do not read the producer, we can not apply
9745 producer-specific interpretation. */
9746 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
9747 }
9748
9749 /* Load the DIEs associated with PER_CU into memory. */
9750
9751 static void
9752 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9753 bool skip_partial,
9754 enum language pretend_language)
9755 {
9756 gdb_assert (! this_cu->is_debug_types);
9757
9758 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
9759 load_full_comp_unit_reader, &pretend_language);
9760 }
9761
9762 /* Add a DIE to the delayed physname list. */
9763
9764 static void
9765 add_to_method_list (struct type *type, int fnfield_index, int index,
9766 const char *name, struct die_info *die,
9767 struct dwarf2_cu *cu)
9768 {
9769 struct delayed_method_info mi;
9770 mi.type = type;
9771 mi.fnfield_index = fnfield_index;
9772 mi.index = index;
9773 mi.name = name;
9774 mi.die = die;
9775 cu->method_list.push_back (mi);
9776 }
9777
9778 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9779 "const" / "volatile". If so, decrements LEN by the length of the
9780 modifier and return true. Otherwise return false. */
9781
9782 template<size_t N>
9783 static bool
9784 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9785 {
9786 size_t mod_len = sizeof (mod) - 1;
9787 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9788 {
9789 len -= mod_len;
9790 return true;
9791 }
9792 return false;
9793 }
9794
9795 /* Compute the physnames of any methods on the CU's method list.
9796
9797 The computation of method physnames is delayed in order to avoid the
9798 (bad) condition that one of the method's formal parameters is of an as yet
9799 incomplete type. */
9800
9801 static void
9802 compute_delayed_physnames (struct dwarf2_cu *cu)
9803 {
9804 /* Only C++ delays computing physnames. */
9805 if (cu->method_list.empty ())
9806 return;
9807 gdb_assert (cu->language == language_cplus);
9808
9809 for (const delayed_method_info &mi : cu->method_list)
9810 {
9811 const char *physname;
9812 struct fn_fieldlist *fn_flp
9813 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9814 physname = dwarf2_physname (mi.name, mi.die, cu);
9815 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9816 = physname ? physname : "";
9817
9818 /* Since there's no tag to indicate whether a method is a
9819 const/volatile overload, extract that information out of the
9820 demangled name. */
9821 if (physname != NULL)
9822 {
9823 size_t len = strlen (physname);
9824
9825 while (1)
9826 {
9827 if (physname[len] == ')') /* shortcut */
9828 break;
9829 else if (check_modifier (physname, len, " const"))
9830 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9831 else if (check_modifier (physname, len, " volatile"))
9832 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9833 else
9834 break;
9835 }
9836 }
9837 }
9838
9839 /* The list is no longer needed. */
9840 cu->method_list.clear ();
9841 }
9842
9843 /* Go objects should be embedded in a DW_TAG_module DIE,
9844 and it's not clear if/how imported objects will appear.
9845 To keep Go support simple until that's worked out,
9846 go back through what we've read and create something usable.
9847 We could do this while processing each DIE, and feels kinda cleaner,
9848 but that way is more invasive.
9849 This is to, for example, allow the user to type "p var" or "b main"
9850 without having to specify the package name, and allow lookups
9851 of module.object to work in contexts that use the expression
9852 parser. */
9853
9854 static void
9855 fixup_go_packaging (struct dwarf2_cu *cu)
9856 {
9857 char *package_name = NULL;
9858 struct pending *list;
9859 int i;
9860
9861 for (list = *cu->get_builder ()->get_global_symbols ();
9862 list != NULL;
9863 list = list->next)
9864 {
9865 for (i = 0; i < list->nsyms; ++i)
9866 {
9867 struct symbol *sym = list->symbol[i];
9868
9869 if (SYMBOL_LANGUAGE (sym) == language_go
9870 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9871 {
9872 char *this_package_name = go_symbol_package_name (sym);
9873
9874 if (this_package_name == NULL)
9875 continue;
9876 if (package_name == NULL)
9877 package_name = this_package_name;
9878 else
9879 {
9880 struct objfile *objfile
9881 = cu->per_cu->dwarf2_per_objfile->objfile;
9882 if (strcmp (package_name, this_package_name) != 0)
9883 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9884 (symbol_symtab (sym) != NULL
9885 ? symtab_to_filename_for_display
9886 (symbol_symtab (sym))
9887 : objfile_name (objfile)),
9888 this_package_name, package_name);
9889 xfree (this_package_name);
9890 }
9891 }
9892 }
9893 }
9894
9895 if (package_name != NULL)
9896 {
9897 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9898 const char *saved_package_name
9899 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name);
9900 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9901 saved_package_name);
9902 struct symbol *sym;
9903
9904 sym = allocate_symbol (objfile);
9905 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
9906 SYMBOL_SET_NAMES (sym, saved_package_name,
9907 strlen (saved_package_name), 0, objfile);
9908 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9909 e.g., "main" finds the "main" module and not C's main(). */
9910 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9911 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9912 SYMBOL_TYPE (sym) = type;
9913
9914 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9915
9916 xfree (package_name);
9917 }
9918 }
9919
9920 /* Allocate a fully-qualified name consisting of the two parts on the
9921 obstack. */
9922
9923 static const char *
9924 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9925 {
9926 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9927 }
9928
9929 /* A helper that allocates a struct discriminant_info to attach to a
9930 union type. */
9931
9932 static struct discriminant_info *
9933 alloc_discriminant_info (struct type *type, int discriminant_index,
9934 int default_index)
9935 {
9936 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9937 gdb_assert (discriminant_index == -1
9938 || (discriminant_index >= 0
9939 && discriminant_index < TYPE_NFIELDS (type)));
9940 gdb_assert (default_index == -1
9941 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9942
9943 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9944
9945 struct discriminant_info *disc
9946 = ((struct discriminant_info *)
9947 TYPE_ZALLOC (type,
9948 offsetof (struct discriminant_info, discriminants)
9949 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9950 disc->default_index = default_index;
9951 disc->discriminant_index = discriminant_index;
9952
9953 struct dynamic_prop prop;
9954 prop.kind = PROP_UNDEFINED;
9955 prop.data.baton = disc;
9956
9957 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9958
9959 return disc;
9960 }
9961
9962 /* Some versions of rustc emitted enums in an unusual way.
9963
9964 Ordinary enums were emitted as unions. The first element of each
9965 structure in the union was named "RUST$ENUM$DISR". This element
9966 held the discriminant.
9967
9968 These versions of Rust also implemented the "non-zero"
9969 optimization. When the enum had two values, and one is empty and
9970 the other holds a pointer that cannot be zero, the pointer is used
9971 as the discriminant, with a zero value meaning the empty variant.
9972 Here, the union's first member is of the form
9973 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9974 where the fieldnos are the indices of the fields that should be
9975 traversed in order to find the field (which may be several fields deep)
9976 and the variantname is the name of the variant of the case when the
9977 field is zero.
9978
9979 This function recognizes whether TYPE is of one of these forms,
9980 and, if so, smashes it to be a variant type. */
9981
9982 static void
9983 quirk_rust_enum (struct type *type, struct objfile *objfile)
9984 {
9985 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9986
9987 /* We don't need to deal with empty enums. */
9988 if (TYPE_NFIELDS (type) == 0)
9989 return;
9990
9991 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9992 if (TYPE_NFIELDS (type) == 1
9993 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9994 {
9995 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9996
9997 /* Decode the field name to find the offset of the
9998 discriminant. */
9999 ULONGEST bit_offset = 0;
10000 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10001 while (name[0] >= '0' && name[0] <= '9')
10002 {
10003 char *tail;
10004 unsigned long index = strtoul (name, &tail, 10);
10005 name = tail;
10006 if (*name != '$'
10007 || index >= TYPE_NFIELDS (field_type)
10008 || (TYPE_FIELD_LOC_KIND (field_type, index)
10009 != FIELD_LOC_KIND_BITPOS))
10010 {
10011 complaint (_("Could not parse Rust enum encoding string \"%s\""
10012 "[in module %s]"),
10013 TYPE_FIELD_NAME (type, 0),
10014 objfile_name (objfile));
10015 return;
10016 }
10017 ++name;
10018
10019 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10020 field_type = TYPE_FIELD_TYPE (field_type, index);
10021 }
10022
10023 /* Make a union to hold the variants. */
10024 struct type *union_type = alloc_type (objfile);
10025 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10026 TYPE_NFIELDS (union_type) = 3;
10027 TYPE_FIELDS (union_type)
10028 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10029 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10030 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10031
10032 /* Put the discriminant must at index 0. */
10033 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10034 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10035 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10036 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10037
10038 /* The order of fields doesn't really matter, so put the real
10039 field at index 1 and the data-less field at index 2. */
10040 struct discriminant_info *disc
10041 = alloc_discriminant_info (union_type, 0, 1);
10042 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10043 TYPE_FIELD_NAME (union_type, 1)
10044 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10045 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10046 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10047 TYPE_FIELD_NAME (union_type, 1));
10048
10049 const char *dataless_name
10050 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10051 name);
10052 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10053 dataless_name);
10054 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10055 /* NAME points into the original discriminant name, which
10056 already has the correct lifetime. */
10057 TYPE_FIELD_NAME (union_type, 2) = name;
10058 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10059 disc->discriminants[2] = 0;
10060
10061 /* Smash this type to be a structure type. We have to do this
10062 because the type has already been recorded. */
10063 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10064 TYPE_NFIELDS (type) = 1;
10065 TYPE_FIELDS (type)
10066 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10067
10068 /* Install the variant part. */
10069 TYPE_FIELD_TYPE (type, 0) = union_type;
10070 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10071 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10072 }
10073 /* A union with a single anonymous field is probably an old-style
10074 univariant enum. */
10075 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
10076 {
10077 /* Smash this type to be a structure type. We have to do this
10078 because the type has already been recorded. */
10079 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10080
10081 /* Make a union to hold the variants. */
10082 struct type *union_type = alloc_type (objfile);
10083 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10084 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10085 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10086 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10087 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10088
10089 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10090 const char *variant_name
10091 = rust_last_path_segment (TYPE_NAME (field_type));
10092 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10093 TYPE_NAME (field_type)
10094 = rust_fully_qualify (&objfile->objfile_obstack,
10095 TYPE_NAME (type), variant_name);
10096
10097 /* Install the union in the outer struct type. */
10098 TYPE_NFIELDS (type) = 1;
10099 TYPE_FIELDS (type)
10100 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10101 TYPE_FIELD_TYPE (type, 0) = union_type;
10102 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10103 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10104
10105 alloc_discriminant_info (union_type, -1, 0);
10106 }
10107 else
10108 {
10109 struct type *disr_type = nullptr;
10110 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10111 {
10112 disr_type = TYPE_FIELD_TYPE (type, i);
10113
10114 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10115 {
10116 /* All fields of a true enum will be structs. */
10117 return;
10118 }
10119 else if (TYPE_NFIELDS (disr_type) == 0)
10120 {
10121 /* Could be data-less variant, so keep going. */
10122 disr_type = nullptr;
10123 }
10124 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10125 "RUST$ENUM$DISR") != 0)
10126 {
10127 /* Not a Rust enum. */
10128 return;
10129 }
10130 else
10131 {
10132 /* Found one. */
10133 break;
10134 }
10135 }
10136
10137 /* If we got here without a discriminant, then it's probably
10138 just a union. */
10139 if (disr_type == nullptr)
10140 return;
10141
10142 /* Smash this type to be a structure type. We have to do this
10143 because the type has already been recorded. */
10144 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10145
10146 /* Make a union to hold the variants. */
10147 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10148 struct type *union_type = alloc_type (objfile);
10149 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10150 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10151 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10152 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10153 TYPE_FIELDS (union_type)
10154 = (struct field *) TYPE_ZALLOC (union_type,
10155 (TYPE_NFIELDS (union_type)
10156 * sizeof (struct field)));
10157
10158 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10159 TYPE_NFIELDS (type) * sizeof (struct field));
10160
10161 /* Install the discriminant at index 0 in the union. */
10162 TYPE_FIELD (union_type, 0) = *disr_field;
10163 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10164 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10165
10166 /* Install the union in the outer struct type. */
10167 TYPE_FIELD_TYPE (type, 0) = union_type;
10168 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10169 TYPE_NFIELDS (type) = 1;
10170
10171 /* Set the size and offset of the union type. */
10172 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10173
10174 /* We need a way to find the correct discriminant given a
10175 variant name. For convenience we build a map here. */
10176 struct type *enum_type = FIELD_TYPE (*disr_field);
10177 std::unordered_map<std::string, ULONGEST> discriminant_map;
10178 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10179 {
10180 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10181 {
10182 const char *name
10183 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10184 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10185 }
10186 }
10187
10188 int n_fields = TYPE_NFIELDS (union_type);
10189 struct discriminant_info *disc
10190 = alloc_discriminant_info (union_type, 0, -1);
10191 /* Skip the discriminant here. */
10192 for (int i = 1; i < n_fields; ++i)
10193 {
10194 /* Find the final word in the name of this variant's type.
10195 That name can be used to look up the correct
10196 discriminant. */
10197 const char *variant_name
10198 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10199 i)));
10200
10201 auto iter = discriminant_map.find (variant_name);
10202 if (iter != discriminant_map.end ())
10203 disc->discriminants[i] = iter->second;
10204
10205 /* Remove the discriminant field, if it exists. */
10206 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10207 if (TYPE_NFIELDS (sub_type) > 0)
10208 {
10209 --TYPE_NFIELDS (sub_type);
10210 ++TYPE_FIELDS (sub_type);
10211 }
10212 TYPE_FIELD_NAME (union_type, i) = variant_name;
10213 TYPE_NAME (sub_type)
10214 = rust_fully_qualify (&objfile->objfile_obstack,
10215 TYPE_NAME (type), variant_name);
10216 }
10217 }
10218 }
10219
10220 /* Rewrite some Rust unions to be structures with variants parts. */
10221
10222 static void
10223 rust_union_quirks (struct dwarf2_cu *cu)
10224 {
10225 gdb_assert (cu->language == language_rust);
10226 for (type *type_ : cu->rust_unions)
10227 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
10228 /* We don't need this any more. */
10229 cu->rust_unions.clear ();
10230 }
10231
10232 /* Return the symtab for PER_CU. This works properly regardless of
10233 whether we're using the index or psymtabs. */
10234
10235 static struct compunit_symtab *
10236 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10237 {
10238 return (per_cu->dwarf2_per_objfile->using_index
10239 ? per_cu->v.quick->compunit_symtab
10240 : per_cu->v.psymtab->compunit_symtab);
10241 }
10242
10243 /* A helper function for computing the list of all symbol tables
10244 included by PER_CU. */
10245
10246 static void
10247 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
10248 htab_t all_children, htab_t all_type_symtabs,
10249 struct dwarf2_per_cu_data *per_cu,
10250 struct compunit_symtab *immediate_parent)
10251 {
10252 void **slot;
10253 struct compunit_symtab *cust;
10254
10255 slot = htab_find_slot (all_children, per_cu, INSERT);
10256 if (*slot != NULL)
10257 {
10258 /* This inclusion and its children have been processed. */
10259 return;
10260 }
10261
10262 *slot = per_cu;
10263 /* Only add a CU if it has a symbol table. */
10264 cust = get_compunit_symtab (per_cu);
10265 if (cust != NULL)
10266 {
10267 /* If this is a type unit only add its symbol table if we haven't
10268 seen it yet (type unit per_cu's can share symtabs). */
10269 if (per_cu->is_debug_types)
10270 {
10271 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10272 if (*slot == NULL)
10273 {
10274 *slot = cust;
10275 result->push_back (cust);
10276 if (cust->user == NULL)
10277 cust->user = immediate_parent;
10278 }
10279 }
10280 else
10281 {
10282 result->push_back (cust);
10283 if (cust->user == NULL)
10284 cust->user = immediate_parent;
10285 }
10286 }
10287
10288 if (!per_cu->imported_symtabs_empty ())
10289 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10290 {
10291 recursively_compute_inclusions (result, all_children,
10292 all_type_symtabs, ptr, cust);
10293 }
10294 }
10295
10296 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10297 PER_CU. */
10298
10299 static void
10300 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10301 {
10302 gdb_assert (! per_cu->is_debug_types);
10303
10304 if (!per_cu->imported_symtabs_empty ())
10305 {
10306 int len;
10307 std::vector<compunit_symtab *> result_symtabs;
10308 htab_t all_children, all_type_symtabs;
10309 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10310
10311 /* If we don't have a symtab, we can just skip this case. */
10312 if (cust == NULL)
10313 return;
10314
10315 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10316 NULL, xcalloc, xfree);
10317 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10318 NULL, xcalloc, xfree);
10319
10320 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10321 {
10322 recursively_compute_inclusions (&result_symtabs, all_children,
10323 all_type_symtabs, ptr, cust);
10324 }
10325
10326 /* Now we have a transitive closure of all the included symtabs. */
10327 len = result_symtabs.size ();
10328 cust->includes
10329 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10330 struct compunit_symtab *, len + 1);
10331 memcpy (cust->includes, result_symtabs.data (),
10332 len * sizeof (compunit_symtab *));
10333 cust->includes[len] = NULL;
10334
10335 htab_delete (all_children);
10336 htab_delete (all_type_symtabs);
10337 }
10338 }
10339
10340 /* Compute the 'includes' field for the symtabs of all the CUs we just
10341 read. */
10342
10343 static void
10344 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10345 {
10346 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
10347 {
10348 if (! iter->is_debug_types)
10349 compute_compunit_symtab_includes (iter);
10350 }
10351
10352 dwarf2_per_objfile->just_read_cus.clear ();
10353 }
10354
10355 /* Generate full symbol information for PER_CU, whose DIEs have
10356 already been loaded into memory. */
10357
10358 static void
10359 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10360 enum language pretend_language)
10361 {
10362 struct dwarf2_cu *cu = per_cu->cu;
10363 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10364 struct objfile *objfile = dwarf2_per_objfile->objfile;
10365 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10366 CORE_ADDR lowpc, highpc;
10367 struct compunit_symtab *cust;
10368 CORE_ADDR baseaddr;
10369 struct block *static_block;
10370 CORE_ADDR addr;
10371
10372 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10373
10374 /* Clear the list here in case something was left over. */
10375 cu->method_list.clear ();
10376
10377 cu->language = pretend_language;
10378 cu->language_defn = language_def (cu->language);
10379
10380 /* Do line number decoding in read_file_scope () */
10381 process_die (cu->dies, cu);
10382
10383 /* For now fudge the Go package. */
10384 if (cu->language == language_go)
10385 fixup_go_packaging (cu);
10386
10387 /* Now that we have processed all the DIEs in the CU, all the types
10388 should be complete, and it should now be safe to compute all of the
10389 physnames. */
10390 compute_delayed_physnames (cu);
10391
10392 if (cu->language == language_rust)
10393 rust_union_quirks (cu);
10394
10395 /* Some compilers don't define a DW_AT_high_pc attribute for the
10396 compilation unit. If the DW_AT_high_pc is missing, synthesize
10397 it, by scanning the DIE's below the compilation unit. */
10398 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10399
10400 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10401 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10402
10403 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10404 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10405 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10406 addrmap to help ensure it has an accurate map of pc values belonging to
10407 this comp unit. */
10408 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10409
10410 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10411 SECT_OFF_TEXT (objfile),
10412 0);
10413
10414 if (cust != NULL)
10415 {
10416 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10417
10418 /* Set symtab language to language from DW_AT_language. If the
10419 compilation is from a C file generated by language preprocessors, do
10420 not set the language if it was already deduced by start_subfile. */
10421 if (!(cu->language == language_c
10422 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10423 COMPUNIT_FILETABS (cust)->language = cu->language;
10424
10425 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10426 produce DW_AT_location with location lists but it can be possibly
10427 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10428 there were bugs in prologue debug info, fixed later in GCC-4.5
10429 by "unwind info for epilogues" patch (which is not directly related).
10430
10431 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10432 needed, it would be wrong due to missing DW_AT_producer there.
10433
10434 Still one can confuse GDB by using non-standard GCC compilation
10435 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10436 */
10437 if (cu->has_loclist && gcc_4_minor >= 5)
10438 cust->locations_valid = 1;
10439
10440 if (gcc_4_minor >= 5)
10441 cust->epilogue_unwind_valid = 1;
10442
10443 cust->call_site_htab = cu->call_site_htab;
10444 }
10445
10446 if (dwarf2_per_objfile->using_index)
10447 per_cu->v.quick->compunit_symtab = cust;
10448 else
10449 {
10450 struct partial_symtab *pst = per_cu->v.psymtab;
10451 pst->compunit_symtab = cust;
10452 pst->readin = 1;
10453 }
10454
10455 /* Push it for inclusion processing later. */
10456 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
10457
10458 /* Not needed any more. */
10459 cu->reset_builder ();
10460 }
10461
10462 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10463 already been loaded into memory. */
10464
10465 static void
10466 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10467 enum language pretend_language)
10468 {
10469 struct dwarf2_cu *cu = per_cu->cu;
10470 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10471 struct objfile *objfile = dwarf2_per_objfile->objfile;
10472 struct compunit_symtab *cust;
10473 struct signatured_type *sig_type;
10474
10475 gdb_assert (per_cu->is_debug_types);
10476 sig_type = (struct signatured_type *) per_cu;
10477
10478 /* Clear the list here in case something was left over. */
10479 cu->method_list.clear ();
10480
10481 cu->language = pretend_language;
10482 cu->language_defn = language_def (cu->language);
10483
10484 /* The symbol tables are set up in read_type_unit_scope. */
10485 process_die (cu->dies, cu);
10486
10487 /* For now fudge the Go package. */
10488 if (cu->language == language_go)
10489 fixup_go_packaging (cu);
10490
10491 /* Now that we have processed all the DIEs in the CU, all the types
10492 should be complete, and it should now be safe to compute all of the
10493 physnames. */
10494 compute_delayed_physnames (cu);
10495
10496 if (cu->language == language_rust)
10497 rust_union_quirks (cu);
10498
10499 /* TUs share symbol tables.
10500 If this is the first TU to use this symtab, complete the construction
10501 of it with end_expandable_symtab. Otherwise, complete the addition of
10502 this TU's symbols to the existing symtab. */
10503 if (sig_type->type_unit_group->compunit_symtab == NULL)
10504 {
10505 buildsym_compunit *builder = cu->get_builder ();
10506 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10507 sig_type->type_unit_group->compunit_symtab = cust;
10508
10509 if (cust != NULL)
10510 {
10511 /* Set symtab language to language from DW_AT_language. If the
10512 compilation is from a C file generated by language preprocessors,
10513 do not set the language if it was already deduced by
10514 start_subfile. */
10515 if (!(cu->language == language_c
10516 && COMPUNIT_FILETABS (cust)->language != language_c))
10517 COMPUNIT_FILETABS (cust)->language = cu->language;
10518 }
10519 }
10520 else
10521 {
10522 cu->get_builder ()->augment_type_symtab ();
10523 cust = sig_type->type_unit_group->compunit_symtab;
10524 }
10525
10526 if (dwarf2_per_objfile->using_index)
10527 per_cu->v.quick->compunit_symtab = cust;
10528 else
10529 {
10530 struct partial_symtab *pst = per_cu->v.psymtab;
10531 pst->compunit_symtab = cust;
10532 pst->readin = 1;
10533 }
10534
10535 /* Not needed any more. */
10536 cu->reset_builder ();
10537 }
10538
10539 /* Process an imported unit DIE. */
10540
10541 static void
10542 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10543 {
10544 struct attribute *attr;
10545
10546 /* For now we don't handle imported units in type units. */
10547 if (cu->per_cu->is_debug_types)
10548 {
10549 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10550 " supported in type units [in module %s]"),
10551 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10552 }
10553
10554 attr = dwarf2_attr (die, DW_AT_import, cu);
10555 if (attr != NULL)
10556 {
10557 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10558 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10559 dwarf2_per_cu_data *per_cu
10560 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10561 cu->per_cu->dwarf2_per_objfile);
10562
10563 /* If necessary, add it to the queue and load its DIEs. */
10564 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10565 load_full_comp_unit (per_cu, false, cu->language);
10566
10567 cu->per_cu->imported_symtabs_push (per_cu);
10568 }
10569 }
10570
10571 /* RAII object that represents a process_die scope: i.e.,
10572 starts/finishes processing a DIE. */
10573 class process_die_scope
10574 {
10575 public:
10576 process_die_scope (die_info *die, dwarf2_cu *cu)
10577 : m_die (die), m_cu (cu)
10578 {
10579 /* We should only be processing DIEs not already in process. */
10580 gdb_assert (!m_die->in_process);
10581 m_die->in_process = true;
10582 }
10583
10584 ~process_die_scope ()
10585 {
10586 m_die->in_process = false;
10587
10588 /* If we're done processing the DIE for the CU that owns the line
10589 header, we don't need the line header anymore. */
10590 if (m_cu->line_header_die_owner == m_die)
10591 {
10592 delete m_cu->line_header;
10593 m_cu->line_header = NULL;
10594 m_cu->line_header_die_owner = NULL;
10595 }
10596 }
10597
10598 private:
10599 die_info *m_die;
10600 dwarf2_cu *m_cu;
10601 };
10602
10603 /* Process a die and its children. */
10604
10605 static void
10606 process_die (struct die_info *die, struct dwarf2_cu *cu)
10607 {
10608 process_die_scope scope (die, cu);
10609
10610 switch (die->tag)
10611 {
10612 case DW_TAG_padding:
10613 break;
10614 case DW_TAG_compile_unit:
10615 case DW_TAG_partial_unit:
10616 read_file_scope (die, cu);
10617 break;
10618 case DW_TAG_type_unit:
10619 read_type_unit_scope (die, cu);
10620 break;
10621 case DW_TAG_subprogram:
10622 /* Nested subprograms in Fortran get a prefix. */
10623 if (cu->language == language_fortran
10624 && die->parent != NULL
10625 && die->parent->tag == DW_TAG_subprogram)
10626 cu->processing_has_namespace_info = true;
10627 /* Fall through. */
10628 case DW_TAG_inlined_subroutine:
10629 read_func_scope (die, cu);
10630 break;
10631 case DW_TAG_lexical_block:
10632 case DW_TAG_try_block:
10633 case DW_TAG_catch_block:
10634 read_lexical_block_scope (die, cu);
10635 break;
10636 case DW_TAG_call_site:
10637 case DW_TAG_GNU_call_site:
10638 read_call_site_scope (die, cu);
10639 break;
10640 case DW_TAG_class_type:
10641 case DW_TAG_interface_type:
10642 case DW_TAG_structure_type:
10643 case DW_TAG_union_type:
10644 process_structure_scope (die, cu);
10645 break;
10646 case DW_TAG_enumeration_type:
10647 process_enumeration_scope (die, cu);
10648 break;
10649
10650 /* These dies have a type, but processing them does not create
10651 a symbol or recurse to process the children. Therefore we can
10652 read them on-demand through read_type_die. */
10653 case DW_TAG_subroutine_type:
10654 case DW_TAG_set_type:
10655 case DW_TAG_array_type:
10656 case DW_TAG_pointer_type:
10657 case DW_TAG_ptr_to_member_type:
10658 case DW_TAG_reference_type:
10659 case DW_TAG_rvalue_reference_type:
10660 case DW_TAG_string_type:
10661 break;
10662
10663 case DW_TAG_base_type:
10664 case DW_TAG_subrange_type:
10665 case DW_TAG_typedef:
10666 /* Add a typedef symbol for the type definition, if it has a
10667 DW_AT_name. */
10668 new_symbol (die, read_type_die (die, cu), cu);
10669 break;
10670 case DW_TAG_common_block:
10671 read_common_block (die, cu);
10672 break;
10673 case DW_TAG_common_inclusion:
10674 break;
10675 case DW_TAG_namespace:
10676 cu->processing_has_namespace_info = true;
10677 read_namespace (die, cu);
10678 break;
10679 case DW_TAG_module:
10680 cu->processing_has_namespace_info = true;
10681 read_module (die, cu);
10682 break;
10683 case DW_TAG_imported_declaration:
10684 cu->processing_has_namespace_info = true;
10685 if (read_namespace_alias (die, cu))
10686 break;
10687 /* The declaration is not a global namespace alias. */
10688 /* Fall through. */
10689 case DW_TAG_imported_module:
10690 cu->processing_has_namespace_info = true;
10691 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10692 || cu->language != language_fortran))
10693 complaint (_("Tag '%s' has unexpected children"),
10694 dwarf_tag_name (die->tag));
10695 read_import_statement (die, cu);
10696 break;
10697
10698 case DW_TAG_imported_unit:
10699 process_imported_unit_die (die, cu);
10700 break;
10701
10702 case DW_TAG_variable:
10703 read_variable (die, cu);
10704 break;
10705
10706 default:
10707 new_symbol (die, NULL, cu);
10708 break;
10709 }
10710 }
10711 \f
10712 /* DWARF name computation. */
10713
10714 /* A helper function for dwarf2_compute_name which determines whether DIE
10715 needs to have the name of the scope prepended to the name listed in the
10716 die. */
10717
10718 static int
10719 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10720 {
10721 struct attribute *attr;
10722
10723 switch (die->tag)
10724 {
10725 case DW_TAG_namespace:
10726 case DW_TAG_typedef:
10727 case DW_TAG_class_type:
10728 case DW_TAG_interface_type:
10729 case DW_TAG_structure_type:
10730 case DW_TAG_union_type:
10731 case DW_TAG_enumeration_type:
10732 case DW_TAG_enumerator:
10733 case DW_TAG_subprogram:
10734 case DW_TAG_inlined_subroutine:
10735 case DW_TAG_member:
10736 case DW_TAG_imported_declaration:
10737 return 1;
10738
10739 case DW_TAG_variable:
10740 case DW_TAG_constant:
10741 /* We only need to prefix "globally" visible variables. These include
10742 any variable marked with DW_AT_external or any variable that
10743 lives in a namespace. [Variables in anonymous namespaces
10744 require prefixing, but they are not DW_AT_external.] */
10745
10746 if (dwarf2_attr (die, DW_AT_specification, cu))
10747 {
10748 struct dwarf2_cu *spec_cu = cu;
10749
10750 return die_needs_namespace (die_specification (die, &spec_cu),
10751 spec_cu);
10752 }
10753
10754 attr = dwarf2_attr (die, DW_AT_external, cu);
10755 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10756 && die->parent->tag != DW_TAG_module)
10757 return 0;
10758 /* A variable in a lexical block of some kind does not need a
10759 namespace, even though in C++ such variables may be external
10760 and have a mangled name. */
10761 if (die->parent->tag == DW_TAG_lexical_block
10762 || die->parent->tag == DW_TAG_try_block
10763 || die->parent->tag == DW_TAG_catch_block
10764 || die->parent->tag == DW_TAG_subprogram)
10765 return 0;
10766 return 1;
10767
10768 default:
10769 return 0;
10770 }
10771 }
10772
10773 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10774 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10775 defined for the given DIE. */
10776
10777 static struct attribute *
10778 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10779 {
10780 struct attribute *attr;
10781
10782 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10783 if (attr == NULL)
10784 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10785
10786 return attr;
10787 }
10788
10789 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10790 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10791 defined for the given DIE. */
10792
10793 static const char *
10794 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10795 {
10796 const char *linkage_name;
10797
10798 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10799 if (linkage_name == NULL)
10800 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10801
10802 return linkage_name;
10803 }
10804
10805 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10806 compute the physname for the object, which include a method's:
10807 - formal parameters (C++),
10808 - receiver type (Go),
10809
10810 The term "physname" is a bit confusing.
10811 For C++, for example, it is the demangled name.
10812 For Go, for example, it's the mangled name.
10813
10814 For Ada, return the DIE's linkage name rather than the fully qualified
10815 name. PHYSNAME is ignored..
10816
10817 The result is allocated on the objfile_obstack and canonicalized. */
10818
10819 static const char *
10820 dwarf2_compute_name (const char *name,
10821 struct die_info *die, struct dwarf2_cu *cu,
10822 int physname)
10823 {
10824 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10825
10826 if (name == NULL)
10827 name = dwarf2_name (die, cu);
10828
10829 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10830 but otherwise compute it by typename_concat inside GDB.
10831 FIXME: Actually this is not really true, or at least not always true.
10832 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
10833 Fortran names because there is no mangling standard. So new_symbol
10834 will set the demangled name to the result of dwarf2_full_name, and it is
10835 the demangled name that GDB uses if it exists. */
10836 if (cu->language == language_ada
10837 || (cu->language == language_fortran && physname))
10838 {
10839 /* For Ada unit, we prefer the linkage name over the name, as
10840 the former contains the exported name, which the user expects
10841 to be able to reference. Ideally, we want the user to be able
10842 to reference this entity using either natural or linkage name,
10843 but we haven't started looking at this enhancement yet. */
10844 const char *linkage_name = dw2_linkage_name (die, cu);
10845
10846 if (linkage_name != NULL)
10847 return linkage_name;
10848 }
10849
10850 /* These are the only languages we know how to qualify names in. */
10851 if (name != NULL
10852 && (cu->language == language_cplus
10853 || cu->language == language_fortran || cu->language == language_d
10854 || cu->language == language_rust))
10855 {
10856 if (die_needs_namespace (die, cu))
10857 {
10858 const char *prefix;
10859 const char *canonical_name = NULL;
10860
10861 string_file buf;
10862
10863 prefix = determine_prefix (die, cu);
10864 if (*prefix != '\0')
10865 {
10866 char *prefixed_name = typename_concat (NULL, prefix, name,
10867 physname, cu);
10868
10869 buf.puts (prefixed_name);
10870 xfree (prefixed_name);
10871 }
10872 else
10873 buf.puts (name);
10874
10875 /* Template parameters may be specified in the DIE's DW_AT_name, or
10876 as children with DW_TAG_template_type_param or
10877 DW_TAG_value_type_param. If the latter, add them to the name
10878 here. If the name already has template parameters, then
10879 skip this step; some versions of GCC emit both, and
10880 it is more efficient to use the pre-computed name.
10881
10882 Something to keep in mind about this process: it is very
10883 unlikely, or in some cases downright impossible, to produce
10884 something that will match the mangled name of a function.
10885 If the definition of the function has the same debug info,
10886 we should be able to match up with it anyway. But fallbacks
10887 using the minimal symbol, for instance to find a method
10888 implemented in a stripped copy of libstdc++, will not work.
10889 If we do not have debug info for the definition, we will have to
10890 match them up some other way.
10891
10892 When we do name matching there is a related problem with function
10893 templates; two instantiated function templates are allowed to
10894 differ only by their return types, which we do not add here. */
10895
10896 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10897 {
10898 struct attribute *attr;
10899 struct die_info *child;
10900 int first = 1;
10901
10902 die->building_fullname = 1;
10903
10904 for (child = die->child; child != NULL; child = child->sibling)
10905 {
10906 struct type *type;
10907 LONGEST value;
10908 const gdb_byte *bytes;
10909 struct dwarf2_locexpr_baton *baton;
10910 struct value *v;
10911
10912 if (child->tag != DW_TAG_template_type_param
10913 && child->tag != DW_TAG_template_value_param)
10914 continue;
10915
10916 if (first)
10917 {
10918 buf.puts ("<");
10919 first = 0;
10920 }
10921 else
10922 buf.puts (", ");
10923
10924 attr = dwarf2_attr (child, DW_AT_type, cu);
10925 if (attr == NULL)
10926 {
10927 complaint (_("template parameter missing DW_AT_type"));
10928 buf.puts ("UNKNOWN_TYPE");
10929 continue;
10930 }
10931 type = die_type (child, cu);
10932
10933 if (child->tag == DW_TAG_template_type_param)
10934 {
10935 c_print_type (type, "", &buf, -1, 0, cu->language,
10936 &type_print_raw_options);
10937 continue;
10938 }
10939
10940 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10941 if (attr == NULL)
10942 {
10943 complaint (_("template parameter missing "
10944 "DW_AT_const_value"));
10945 buf.puts ("UNKNOWN_VALUE");
10946 continue;
10947 }
10948
10949 dwarf2_const_value_attr (attr, type, name,
10950 &cu->comp_unit_obstack, cu,
10951 &value, &bytes, &baton);
10952
10953 if (TYPE_NOSIGN (type))
10954 /* GDB prints characters as NUMBER 'CHAR'. If that's
10955 changed, this can use value_print instead. */
10956 c_printchar (value, type, &buf);
10957 else
10958 {
10959 struct value_print_options opts;
10960
10961 if (baton != NULL)
10962 v = dwarf2_evaluate_loc_desc (type, NULL,
10963 baton->data,
10964 baton->size,
10965 baton->per_cu);
10966 else if (bytes != NULL)
10967 {
10968 v = allocate_value (type);
10969 memcpy (value_contents_writeable (v), bytes,
10970 TYPE_LENGTH (type));
10971 }
10972 else
10973 v = value_from_longest (type, value);
10974
10975 /* Specify decimal so that we do not depend on
10976 the radix. */
10977 get_formatted_print_options (&opts, 'd');
10978 opts.raw = 1;
10979 value_print (v, &buf, &opts);
10980 release_value (v);
10981 }
10982 }
10983
10984 die->building_fullname = 0;
10985
10986 if (!first)
10987 {
10988 /* Close the argument list, with a space if necessary
10989 (nested templates). */
10990 if (!buf.empty () && buf.string ().back () == '>')
10991 buf.puts (" >");
10992 else
10993 buf.puts (">");
10994 }
10995 }
10996
10997 /* For C++ methods, append formal parameter type
10998 information, if PHYSNAME. */
10999
11000 if (physname && die->tag == DW_TAG_subprogram
11001 && cu->language == language_cplus)
11002 {
11003 struct type *type = read_type_die (die, cu);
11004
11005 c_type_print_args (type, &buf, 1, cu->language,
11006 &type_print_raw_options);
11007
11008 if (cu->language == language_cplus)
11009 {
11010 /* Assume that an artificial first parameter is
11011 "this", but do not crash if it is not. RealView
11012 marks unnamed (and thus unused) parameters as
11013 artificial; there is no way to differentiate
11014 the two cases. */
11015 if (TYPE_NFIELDS (type) > 0
11016 && TYPE_FIELD_ARTIFICIAL (type, 0)
11017 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11018 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11019 0))))
11020 buf.puts (" const");
11021 }
11022 }
11023
11024 const std::string &intermediate_name = buf.string ();
11025
11026 if (cu->language == language_cplus)
11027 canonical_name
11028 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11029 &objfile->per_bfd->storage_obstack);
11030
11031 /* If we only computed INTERMEDIATE_NAME, or if
11032 INTERMEDIATE_NAME is already canonical, then we need to
11033 copy it to the appropriate obstack. */
11034 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11035 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11036 intermediate_name);
11037 else
11038 name = canonical_name;
11039 }
11040 }
11041
11042 return name;
11043 }
11044
11045 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11046 If scope qualifiers are appropriate they will be added. The result
11047 will be allocated on the storage_obstack, or NULL if the DIE does
11048 not have a name. NAME may either be from a previous call to
11049 dwarf2_name or NULL.
11050
11051 The output string will be canonicalized (if C++). */
11052
11053 static const char *
11054 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11055 {
11056 return dwarf2_compute_name (name, die, cu, 0);
11057 }
11058
11059 /* Construct a physname for the given DIE in CU. NAME may either be
11060 from a previous call to dwarf2_name or NULL. The result will be
11061 allocated on the objfile_objstack or NULL if the DIE does not have a
11062 name.
11063
11064 The output string will be canonicalized (if C++). */
11065
11066 static const char *
11067 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11068 {
11069 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11070 const char *retval, *mangled = NULL, *canon = NULL;
11071 int need_copy = 1;
11072
11073 /* In this case dwarf2_compute_name is just a shortcut not building anything
11074 on its own. */
11075 if (!die_needs_namespace (die, cu))
11076 return dwarf2_compute_name (name, die, cu, 1);
11077
11078 mangled = dw2_linkage_name (die, cu);
11079
11080 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11081 See https://github.com/rust-lang/rust/issues/32925. */
11082 if (cu->language == language_rust && mangled != NULL
11083 && strchr (mangled, '{') != NULL)
11084 mangled = NULL;
11085
11086 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11087 has computed. */
11088 gdb::unique_xmalloc_ptr<char> demangled;
11089 if (mangled != NULL)
11090 {
11091
11092 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11093 {
11094 /* Do nothing (do not demangle the symbol name). */
11095 }
11096 else if (cu->language == language_go)
11097 {
11098 /* This is a lie, but we already lie to the caller new_symbol.
11099 new_symbol assumes we return the mangled name.
11100 This just undoes that lie until things are cleaned up. */
11101 }
11102 else
11103 {
11104 /* Use DMGL_RET_DROP for C++ template functions to suppress
11105 their return type. It is easier for GDB users to search
11106 for such functions as `name(params)' than `long name(params)'.
11107 In such case the minimal symbol names do not match the full
11108 symbol names but for template functions there is never a need
11109 to look up their definition from their declaration so
11110 the only disadvantage remains the minimal symbol variant
11111 `long name(params)' does not have the proper inferior type. */
11112 demangled.reset (gdb_demangle (mangled,
11113 (DMGL_PARAMS | DMGL_ANSI
11114 | DMGL_RET_DROP)));
11115 }
11116 if (demangled)
11117 canon = demangled.get ();
11118 else
11119 {
11120 canon = mangled;
11121 need_copy = 0;
11122 }
11123 }
11124
11125 if (canon == NULL || check_physname)
11126 {
11127 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11128
11129 if (canon != NULL && strcmp (physname, canon) != 0)
11130 {
11131 /* It may not mean a bug in GDB. The compiler could also
11132 compute DW_AT_linkage_name incorrectly. But in such case
11133 GDB would need to be bug-to-bug compatible. */
11134
11135 complaint (_("Computed physname <%s> does not match demangled <%s> "
11136 "(from linkage <%s>) - DIE at %s [in module %s]"),
11137 physname, canon, mangled, sect_offset_str (die->sect_off),
11138 objfile_name (objfile));
11139
11140 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11141 is available here - over computed PHYSNAME. It is safer
11142 against both buggy GDB and buggy compilers. */
11143
11144 retval = canon;
11145 }
11146 else
11147 {
11148 retval = physname;
11149 need_copy = 0;
11150 }
11151 }
11152 else
11153 retval = canon;
11154
11155 if (need_copy)
11156 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
11157
11158 return retval;
11159 }
11160
11161 /* Inspect DIE in CU for a namespace alias. If one exists, record
11162 a new symbol for it.
11163
11164 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11165
11166 static int
11167 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11168 {
11169 struct attribute *attr;
11170
11171 /* If the die does not have a name, this is not a namespace
11172 alias. */
11173 attr = dwarf2_attr (die, DW_AT_name, cu);
11174 if (attr != NULL)
11175 {
11176 int num;
11177 struct die_info *d = die;
11178 struct dwarf2_cu *imported_cu = cu;
11179
11180 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11181 keep inspecting DIEs until we hit the underlying import. */
11182 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11183 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11184 {
11185 attr = dwarf2_attr (d, DW_AT_import, cu);
11186 if (attr == NULL)
11187 break;
11188
11189 d = follow_die_ref (d, attr, &imported_cu);
11190 if (d->tag != DW_TAG_imported_declaration)
11191 break;
11192 }
11193
11194 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11195 {
11196 complaint (_("DIE at %s has too many recursively imported "
11197 "declarations"), sect_offset_str (d->sect_off));
11198 return 0;
11199 }
11200
11201 if (attr != NULL)
11202 {
11203 struct type *type;
11204 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11205
11206 type = get_die_type_at_offset (sect_off, cu->per_cu);
11207 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11208 {
11209 /* This declaration is a global namespace alias. Add
11210 a symbol for it whose type is the aliased namespace. */
11211 new_symbol (die, type, cu);
11212 return 1;
11213 }
11214 }
11215 }
11216
11217 return 0;
11218 }
11219
11220 /* Return the using directives repository (global or local?) to use in the
11221 current context for CU.
11222
11223 For Ada, imported declarations can materialize renamings, which *may* be
11224 global. However it is impossible (for now?) in DWARF to distinguish
11225 "external" imported declarations and "static" ones. As all imported
11226 declarations seem to be static in all other languages, make them all CU-wide
11227 global only in Ada. */
11228
11229 static struct using_direct **
11230 using_directives (struct dwarf2_cu *cu)
11231 {
11232 if (cu->language == language_ada
11233 && cu->get_builder ()->outermost_context_p ())
11234 return cu->get_builder ()->get_global_using_directives ();
11235 else
11236 return cu->get_builder ()->get_local_using_directives ();
11237 }
11238
11239 /* Read the import statement specified by the given die and record it. */
11240
11241 static void
11242 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11243 {
11244 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11245 struct attribute *import_attr;
11246 struct die_info *imported_die, *child_die;
11247 struct dwarf2_cu *imported_cu;
11248 const char *imported_name;
11249 const char *imported_name_prefix;
11250 const char *canonical_name;
11251 const char *import_alias;
11252 const char *imported_declaration = NULL;
11253 const char *import_prefix;
11254 std::vector<const char *> excludes;
11255
11256 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11257 if (import_attr == NULL)
11258 {
11259 complaint (_("Tag '%s' has no DW_AT_import"),
11260 dwarf_tag_name (die->tag));
11261 return;
11262 }
11263
11264 imported_cu = cu;
11265 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11266 imported_name = dwarf2_name (imported_die, imported_cu);
11267 if (imported_name == NULL)
11268 {
11269 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11270
11271 The import in the following code:
11272 namespace A
11273 {
11274 typedef int B;
11275 }
11276
11277 int main ()
11278 {
11279 using A::B;
11280 B b;
11281 return b;
11282 }
11283
11284 ...
11285 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11286 <52> DW_AT_decl_file : 1
11287 <53> DW_AT_decl_line : 6
11288 <54> DW_AT_import : <0x75>
11289 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11290 <59> DW_AT_name : B
11291 <5b> DW_AT_decl_file : 1
11292 <5c> DW_AT_decl_line : 2
11293 <5d> DW_AT_type : <0x6e>
11294 ...
11295 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11296 <76> DW_AT_byte_size : 4
11297 <77> DW_AT_encoding : 5 (signed)
11298
11299 imports the wrong die ( 0x75 instead of 0x58 ).
11300 This case will be ignored until the gcc bug is fixed. */
11301 return;
11302 }
11303
11304 /* Figure out the local name after import. */
11305 import_alias = dwarf2_name (die, cu);
11306
11307 /* Figure out where the statement is being imported to. */
11308 import_prefix = determine_prefix (die, cu);
11309
11310 /* Figure out what the scope of the imported die is and prepend it
11311 to the name of the imported die. */
11312 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11313
11314 if (imported_die->tag != DW_TAG_namespace
11315 && imported_die->tag != DW_TAG_module)
11316 {
11317 imported_declaration = imported_name;
11318 canonical_name = imported_name_prefix;
11319 }
11320 else if (strlen (imported_name_prefix) > 0)
11321 canonical_name = obconcat (&objfile->objfile_obstack,
11322 imported_name_prefix,
11323 (cu->language == language_d ? "." : "::"),
11324 imported_name, (char *) NULL);
11325 else
11326 canonical_name = imported_name;
11327
11328 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11329 for (child_die = die->child; child_die && child_die->tag;
11330 child_die = sibling_die (child_die))
11331 {
11332 /* DWARF-4: A Fortran use statement with a “rename list” may be
11333 represented by an imported module entry with an import attribute
11334 referring to the module and owned entries corresponding to those
11335 entities that are renamed as part of being imported. */
11336
11337 if (child_die->tag != DW_TAG_imported_declaration)
11338 {
11339 complaint (_("child DW_TAG_imported_declaration expected "
11340 "- DIE at %s [in module %s]"),
11341 sect_offset_str (child_die->sect_off),
11342 objfile_name (objfile));
11343 continue;
11344 }
11345
11346 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11347 if (import_attr == NULL)
11348 {
11349 complaint (_("Tag '%s' has no DW_AT_import"),
11350 dwarf_tag_name (child_die->tag));
11351 continue;
11352 }
11353
11354 imported_cu = cu;
11355 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11356 &imported_cu);
11357 imported_name = dwarf2_name (imported_die, imported_cu);
11358 if (imported_name == NULL)
11359 {
11360 complaint (_("child DW_TAG_imported_declaration has unknown "
11361 "imported name - DIE at %s [in module %s]"),
11362 sect_offset_str (child_die->sect_off),
11363 objfile_name (objfile));
11364 continue;
11365 }
11366
11367 excludes.push_back (imported_name);
11368
11369 process_die (child_die, cu);
11370 }
11371
11372 add_using_directive (using_directives (cu),
11373 import_prefix,
11374 canonical_name,
11375 import_alias,
11376 imported_declaration,
11377 excludes,
11378 0,
11379 &objfile->objfile_obstack);
11380 }
11381
11382 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11383 types, but gives them a size of zero. Starting with version 14,
11384 ICC is compatible with GCC. */
11385
11386 static bool
11387 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11388 {
11389 if (!cu->checked_producer)
11390 check_producer (cu);
11391
11392 return cu->producer_is_icc_lt_14;
11393 }
11394
11395 /* ICC generates a DW_AT_type for C void functions. This was observed on
11396 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11397 which says that void functions should not have a DW_AT_type. */
11398
11399 static bool
11400 producer_is_icc (struct dwarf2_cu *cu)
11401 {
11402 if (!cu->checked_producer)
11403 check_producer (cu);
11404
11405 return cu->producer_is_icc;
11406 }
11407
11408 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11409 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11410 this, it was first present in GCC release 4.3.0. */
11411
11412 static bool
11413 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11414 {
11415 if (!cu->checked_producer)
11416 check_producer (cu);
11417
11418 return cu->producer_is_gcc_lt_4_3;
11419 }
11420
11421 static file_and_directory
11422 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11423 {
11424 file_and_directory res;
11425
11426 /* Find the filename. Do not use dwarf2_name here, since the filename
11427 is not a source language identifier. */
11428 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11429 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11430
11431 if (res.comp_dir == NULL
11432 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11433 && IS_ABSOLUTE_PATH (res.name))
11434 {
11435 res.comp_dir_storage = ldirname (res.name);
11436 if (!res.comp_dir_storage.empty ())
11437 res.comp_dir = res.comp_dir_storage.c_str ();
11438 }
11439 if (res.comp_dir != NULL)
11440 {
11441 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11442 directory, get rid of it. */
11443 const char *cp = strchr (res.comp_dir, ':');
11444
11445 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11446 res.comp_dir = cp + 1;
11447 }
11448
11449 if (res.name == NULL)
11450 res.name = "<unknown>";
11451
11452 return res;
11453 }
11454
11455 /* Handle DW_AT_stmt_list for a compilation unit.
11456 DIE is the DW_TAG_compile_unit die for CU.
11457 COMP_DIR is the compilation directory. LOWPC is passed to
11458 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11459
11460 static void
11461 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11462 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11463 {
11464 struct dwarf2_per_objfile *dwarf2_per_objfile
11465 = cu->per_cu->dwarf2_per_objfile;
11466 struct objfile *objfile = dwarf2_per_objfile->objfile;
11467 struct attribute *attr;
11468 struct line_header line_header_local;
11469 hashval_t line_header_local_hash;
11470 void **slot;
11471 int decode_mapping;
11472
11473 gdb_assert (! cu->per_cu->is_debug_types);
11474
11475 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11476 if (attr == NULL)
11477 return;
11478
11479 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11480
11481 /* The line header hash table is only created if needed (it exists to
11482 prevent redundant reading of the line table for partial_units).
11483 If we're given a partial_unit, we'll need it. If we're given a
11484 compile_unit, then use the line header hash table if it's already
11485 created, but don't create one just yet. */
11486
11487 if (dwarf2_per_objfile->line_header_hash == NULL
11488 && die->tag == DW_TAG_partial_unit)
11489 {
11490 dwarf2_per_objfile->line_header_hash
11491 = htab_create_alloc_ex (127, line_header_hash_voidp,
11492 line_header_eq_voidp,
11493 free_line_header_voidp,
11494 &objfile->objfile_obstack,
11495 hashtab_obstack_allocate,
11496 dummy_obstack_deallocate);
11497 }
11498
11499 line_header_local.sect_off = line_offset;
11500 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11501 line_header_local_hash = line_header_hash (&line_header_local);
11502 if (dwarf2_per_objfile->line_header_hash != NULL)
11503 {
11504 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11505 &line_header_local,
11506 line_header_local_hash, NO_INSERT);
11507
11508 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11509 is not present in *SLOT (since if there is something in *SLOT then
11510 it will be for a partial_unit). */
11511 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11512 {
11513 gdb_assert (*slot != NULL);
11514 cu->line_header = (struct line_header *) *slot;
11515 return;
11516 }
11517 }
11518
11519 /* dwarf_decode_line_header does not yet provide sufficient information.
11520 We always have to call also dwarf_decode_lines for it. */
11521 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11522 if (lh == NULL)
11523 return;
11524
11525 cu->line_header = lh.release ();
11526 cu->line_header_die_owner = die;
11527
11528 if (dwarf2_per_objfile->line_header_hash == NULL)
11529 slot = NULL;
11530 else
11531 {
11532 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11533 &line_header_local,
11534 line_header_local_hash, INSERT);
11535 gdb_assert (slot != NULL);
11536 }
11537 if (slot != NULL && *slot == NULL)
11538 {
11539 /* This newly decoded line number information unit will be owned
11540 by line_header_hash hash table. */
11541 *slot = cu->line_header;
11542 cu->line_header_die_owner = NULL;
11543 }
11544 else
11545 {
11546 /* We cannot free any current entry in (*slot) as that struct line_header
11547 may be already used by multiple CUs. Create only temporary decoded
11548 line_header for this CU - it may happen at most once for each line
11549 number information unit. And if we're not using line_header_hash
11550 then this is what we want as well. */
11551 gdb_assert (die->tag != DW_TAG_partial_unit);
11552 }
11553 decode_mapping = (die->tag != DW_TAG_partial_unit);
11554 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11555 decode_mapping);
11556
11557 }
11558
11559 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11560
11561 static void
11562 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11563 {
11564 struct dwarf2_per_objfile *dwarf2_per_objfile
11565 = cu->per_cu->dwarf2_per_objfile;
11566 struct objfile *objfile = dwarf2_per_objfile->objfile;
11567 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11568 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11569 CORE_ADDR highpc = ((CORE_ADDR) 0);
11570 struct attribute *attr;
11571 struct die_info *child_die;
11572 CORE_ADDR baseaddr;
11573
11574 prepare_one_comp_unit (cu, die, cu->language);
11575 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11576
11577 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11578
11579 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11580 from finish_block. */
11581 if (lowpc == ((CORE_ADDR) -1))
11582 lowpc = highpc;
11583 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11584
11585 file_and_directory fnd = find_file_and_directory (die, cu);
11586
11587 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11588 standardised yet. As a workaround for the language detection we fall
11589 back to the DW_AT_producer string. */
11590 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11591 cu->language = language_opencl;
11592
11593 /* Similar hack for Go. */
11594 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11595 set_cu_language (DW_LANG_Go, cu);
11596
11597 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11598
11599 /* Decode line number information if present. We do this before
11600 processing child DIEs, so that the line header table is available
11601 for DW_AT_decl_file. */
11602 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11603
11604 /* Process all dies in compilation unit. */
11605 if (die->child != NULL)
11606 {
11607 child_die = die->child;
11608 while (child_die && child_die->tag)
11609 {
11610 process_die (child_die, cu);
11611 child_die = sibling_die (child_die);
11612 }
11613 }
11614
11615 /* Decode macro information, if present. Dwarf 2 macro information
11616 refers to information in the line number info statement program
11617 header, so we can only read it if we've read the header
11618 successfully. */
11619 attr = dwarf2_attr (die, DW_AT_macros, cu);
11620 if (attr == NULL)
11621 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11622 if (attr && cu->line_header)
11623 {
11624 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11625 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11626
11627 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11628 }
11629 else
11630 {
11631 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11632 if (attr && cu->line_header)
11633 {
11634 unsigned int macro_offset = DW_UNSND (attr);
11635
11636 dwarf_decode_macros (cu, macro_offset, 0);
11637 }
11638 }
11639 }
11640
11641 void
11642 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11643 {
11644 struct type_unit_group *tu_group;
11645 int first_time;
11646 struct attribute *attr;
11647 unsigned int i;
11648 struct signatured_type *sig_type;
11649
11650 gdb_assert (per_cu->is_debug_types);
11651 sig_type = (struct signatured_type *) per_cu;
11652
11653 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11654
11655 /* If we're using .gdb_index (includes -readnow) then
11656 per_cu->type_unit_group may not have been set up yet. */
11657 if (sig_type->type_unit_group == NULL)
11658 sig_type->type_unit_group = get_type_unit_group (this, attr);
11659 tu_group = sig_type->type_unit_group;
11660
11661 /* If we've already processed this stmt_list there's no real need to
11662 do it again, we could fake it and just recreate the part we need
11663 (file name,index -> symtab mapping). If data shows this optimization
11664 is useful we can do it then. */
11665 first_time = tu_group->compunit_symtab == NULL;
11666
11667 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11668 debug info. */
11669 line_header_up lh;
11670 if (attr != NULL)
11671 {
11672 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11673 lh = dwarf_decode_line_header (line_offset, this);
11674 }
11675 if (lh == NULL)
11676 {
11677 if (first_time)
11678 start_symtab ("", NULL, 0);
11679 else
11680 {
11681 gdb_assert (tu_group->symtabs == NULL);
11682 gdb_assert (m_builder == nullptr);
11683 struct compunit_symtab *cust = tu_group->compunit_symtab;
11684 m_builder.reset (new struct buildsym_compunit
11685 (COMPUNIT_OBJFILE (cust), "",
11686 COMPUNIT_DIRNAME (cust),
11687 compunit_language (cust),
11688 0, cust));
11689 }
11690 return;
11691 }
11692
11693 line_header = lh.release ();
11694 line_header_die_owner = die;
11695
11696 if (first_time)
11697 {
11698 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11699
11700 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11701 still initializing it, and our caller (a few levels up)
11702 process_full_type_unit still needs to know if this is the first
11703 time. */
11704
11705 tu_group->num_symtabs = line_header->file_names.size ();
11706 tu_group->symtabs = XNEWVEC (struct symtab *,
11707 line_header->file_names.size ());
11708
11709 for (i = 0; i < line_header->file_names.size (); ++i)
11710 {
11711 file_entry &fe = line_header->file_names[i];
11712
11713 dwarf2_start_subfile (this, fe.name,
11714 fe.include_dir (line_header));
11715 buildsym_compunit *b = get_builder ();
11716 if (b->get_current_subfile ()->symtab == NULL)
11717 {
11718 /* NOTE: start_subfile will recognize when it's been
11719 passed a file it has already seen. So we can't
11720 assume there's a simple mapping from
11721 cu->line_header->file_names to subfiles, plus
11722 cu->line_header->file_names may contain dups. */
11723 b->get_current_subfile ()->symtab
11724 = allocate_symtab (cust, b->get_current_subfile ()->name);
11725 }
11726
11727 fe.symtab = b->get_current_subfile ()->symtab;
11728 tu_group->symtabs[i] = fe.symtab;
11729 }
11730 }
11731 else
11732 {
11733 gdb_assert (m_builder == nullptr);
11734 struct compunit_symtab *cust = tu_group->compunit_symtab;
11735 m_builder.reset (new struct buildsym_compunit
11736 (COMPUNIT_OBJFILE (cust), "",
11737 COMPUNIT_DIRNAME (cust),
11738 compunit_language (cust),
11739 0, cust));
11740
11741 for (i = 0; i < line_header->file_names.size (); ++i)
11742 {
11743 file_entry &fe = line_header->file_names[i];
11744
11745 fe.symtab = tu_group->symtabs[i];
11746 }
11747 }
11748
11749 /* The main symtab is allocated last. Type units don't have DW_AT_name
11750 so they don't have a "real" (so to speak) symtab anyway.
11751 There is later code that will assign the main symtab to all symbols
11752 that don't have one. We need to handle the case of a symbol with a
11753 missing symtab (DW_AT_decl_file) anyway. */
11754 }
11755
11756 /* Process DW_TAG_type_unit.
11757 For TUs we want to skip the first top level sibling if it's not the
11758 actual type being defined by this TU. In this case the first top
11759 level sibling is there to provide context only. */
11760
11761 static void
11762 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11763 {
11764 struct die_info *child_die;
11765
11766 prepare_one_comp_unit (cu, die, language_minimal);
11767
11768 /* Initialize (or reinitialize) the machinery for building symtabs.
11769 We do this before processing child DIEs, so that the line header table
11770 is available for DW_AT_decl_file. */
11771 cu->setup_type_unit_groups (die);
11772
11773 if (die->child != NULL)
11774 {
11775 child_die = die->child;
11776 while (child_die && child_die->tag)
11777 {
11778 process_die (child_die, cu);
11779 child_die = sibling_die (child_die);
11780 }
11781 }
11782 }
11783 \f
11784 /* DWO/DWP files.
11785
11786 http://gcc.gnu.org/wiki/DebugFission
11787 http://gcc.gnu.org/wiki/DebugFissionDWP
11788
11789 To simplify handling of both DWO files ("object" files with the DWARF info)
11790 and DWP files (a file with the DWOs packaged up into one file), we treat
11791 DWP files as having a collection of virtual DWO files. */
11792
11793 static hashval_t
11794 hash_dwo_file (const void *item)
11795 {
11796 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11797 hashval_t hash;
11798
11799 hash = htab_hash_string (dwo_file->dwo_name);
11800 if (dwo_file->comp_dir != NULL)
11801 hash += htab_hash_string (dwo_file->comp_dir);
11802 return hash;
11803 }
11804
11805 static int
11806 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11807 {
11808 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11809 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11810
11811 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11812 return 0;
11813 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11814 return lhs->comp_dir == rhs->comp_dir;
11815 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11816 }
11817
11818 /* Allocate a hash table for DWO files. */
11819
11820 static htab_up
11821 allocate_dwo_file_hash_table (struct objfile *objfile)
11822 {
11823 auto delete_dwo_file = [] (void *item)
11824 {
11825 struct dwo_file *dwo_file = (struct dwo_file *) item;
11826
11827 delete dwo_file;
11828 };
11829
11830 return htab_up (htab_create_alloc_ex (41,
11831 hash_dwo_file,
11832 eq_dwo_file,
11833 delete_dwo_file,
11834 &objfile->objfile_obstack,
11835 hashtab_obstack_allocate,
11836 dummy_obstack_deallocate));
11837 }
11838
11839 /* Lookup DWO file DWO_NAME. */
11840
11841 static void **
11842 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11843 const char *dwo_name,
11844 const char *comp_dir)
11845 {
11846 struct dwo_file find_entry;
11847 void **slot;
11848
11849 if (dwarf2_per_objfile->dwo_files == NULL)
11850 dwarf2_per_objfile->dwo_files
11851 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11852
11853 find_entry.dwo_name = dwo_name;
11854 find_entry.comp_dir = comp_dir;
11855 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11856 INSERT);
11857
11858 return slot;
11859 }
11860
11861 static hashval_t
11862 hash_dwo_unit (const void *item)
11863 {
11864 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11865
11866 /* This drops the top 32 bits of the id, but is ok for a hash. */
11867 return dwo_unit->signature;
11868 }
11869
11870 static int
11871 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11872 {
11873 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11874 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11875
11876 /* The signature is assumed to be unique within the DWO file.
11877 So while object file CU dwo_id's always have the value zero,
11878 that's OK, assuming each object file DWO file has only one CU,
11879 and that's the rule for now. */
11880 return lhs->signature == rhs->signature;
11881 }
11882
11883 /* Allocate a hash table for DWO CUs,TUs.
11884 There is one of these tables for each of CUs,TUs for each DWO file. */
11885
11886 static htab_t
11887 allocate_dwo_unit_table (struct objfile *objfile)
11888 {
11889 /* Start out with a pretty small number.
11890 Generally DWO files contain only one CU and maybe some TUs. */
11891 return htab_create_alloc_ex (3,
11892 hash_dwo_unit,
11893 eq_dwo_unit,
11894 NULL,
11895 &objfile->objfile_obstack,
11896 hashtab_obstack_allocate,
11897 dummy_obstack_deallocate);
11898 }
11899
11900 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
11901
11902 struct create_dwo_cu_data
11903 {
11904 struct dwo_file *dwo_file;
11905 struct dwo_unit dwo_unit;
11906 };
11907
11908 /* die_reader_func for create_dwo_cu. */
11909
11910 static void
11911 create_dwo_cu_reader (const struct die_reader_specs *reader,
11912 const gdb_byte *info_ptr,
11913 struct die_info *comp_unit_die,
11914 int has_children,
11915 void *datap)
11916 {
11917 struct dwarf2_cu *cu = reader->cu;
11918 sect_offset sect_off = cu->per_cu->sect_off;
11919 struct dwarf2_section_info *section = cu->per_cu->section;
11920 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
11921 struct dwo_file *dwo_file = data->dwo_file;
11922 struct dwo_unit *dwo_unit = &data->dwo_unit;
11923
11924 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11925 if (!signature.has_value ())
11926 {
11927 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11928 " its dwo_id [in module %s]"),
11929 sect_offset_str (sect_off), dwo_file->dwo_name);
11930 return;
11931 }
11932
11933 dwo_unit->dwo_file = dwo_file;
11934 dwo_unit->signature = *signature;
11935 dwo_unit->section = section;
11936 dwo_unit->sect_off = sect_off;
11937 dwo_unit->length = cu->per_cu->length;
11938
11939 if (dwarf_read_debug)
11940 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11941 sect_offset_str (sect_off),
11942 hex_string (dwo_unit->signature));
11943 }
11944
11945 /* Create the dwo_units for the CUs in a DWO_FILE.
11946 Note: This function processes DWO files only, not DWP files. */
11947
11948 static void
11949 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11950 struct dwo_file &dwo_file, dwarf2_section_info &section,
11951 htab_t &cus_htab)
11952 {
11953 struct objfile *objfile = dwarf2_per_objfile->objfile;
11954 const gdb_byte *info_ptr, *end_ptr;
11955
11956 dwarf2_read_section (objfile, &section);
11957 info_ptr = section.buffer;
11958
11959 if (info_ptr == NULL)
11960 return;
11961
11962 if (dwarf_read_debug)
11963 {
11964 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11965 get_section_name (&section),
11966 get_section_file_name (&section));
11967 }
11968
11969 end_ptr = info_ptr + section.size;
11970 while (info_ptr < end_ptr)
11971 {
11972 struct dwarf2_per_cu_data per_cu;
11973 struct create_dwo_cu_data create_dwo_cu_data;
11974 struct dwo_unit *dwo_unit;
11975 void **slot;
11976 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11977
11978 memset (&create_dwo_cu_data.dwo_unit, 0,
11979 sizeof (create_dwo_cu_data.dwo_unit));
11980 memset (&per_cu, 0, sizeof (per_cu));
11981 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11982 per_cu.is_debug_types = 0;
11983 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11984 per_cu.section = &section;
11985 create_dwo_cu_data.dwo_file = &dwo_file;
11986
11987 init_cutu_and_read_dies_no_follow (
11988 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11989 info_ptr += per_cu.length;
11990
11991 // If the unit could not be parsed, skip it.
11992 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11993 continue;
11994
11995 if (cus_htab == NULL)
11996 cus_htab = allocate_dwo_unit_table (objfile);
11997
11998 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11999 *dwo_unit = create_dwo_cu_data.dwo_unit;
12000 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12001 gdb_assert (slot != NULL);
12002 if (*slot != NULL)
12003 {
12004 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12005 sect_offset dup_sect_off = dup_cu->sect_off;
12006
12007 complaint (_("debug cu entry at offset %s is duplicate to"
12008 " the entry at offset %s, signature %s"),
12009 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
12010 hex_string (dwo_unit->signature));
12011 }
12012 *slot = (void *)dwo_unit;
12013 }
12014 }
12015
12016 /* DWP file .debug_{cu,tu}_index section format:
12017 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12018
12019 DWP Version 1:
12020
12021 Both index sections have the same format, and serve to map a 64-bit
12022 signature to a set of section numbers. Each section begins with a header,
12023 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12024 indexes, and a pool of 32-bit section numbers. The index sections will be
12025 aligned at 8-byte boundaries in the file.
12026
12027 The index section header consists of:
12028
12029 V, 32 bit version number
12030 -, 32 bits unused
12031 N, 32 bit number of compilation units or type units in the index
12032 M, 32 bit number of slots in the hash table
12033
12034 Numbers are recorded using the byte order of the application binary.
12035
12036 The hash table begins at offset 16 in the section, and consists of an array
12037 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12038 order of the application binary). Unused slots in the hash table are 0.
12039 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12040
12041 The parallel table begins immediately after the hash table
12042 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12043 array of 32-bit indexes (using the byte order of the application binary),
12044 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12045 table contains a 32-bit index into the pool of section numbers. For unused
12046 hash table slots, the corresponding entry in the parallel table will be 0.
12047
12048 The pool of section numbers begins immediately following the hash table
12049 (at offset 16 + 12 * M from the beginning of the section). The pool of
12050 section numbers consists of an array of 32-bit words (using the byte order
12051 of the application binary). Each item in the array is indexed starting
12052 from 0. The hash table entry provides the index of the first section
12053 number in the set. Additional section numbers in the set follow, and the
12054 set is terminated by a 0 entry (section number 0 is not used in ELF).
12055
12056 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12057 section must be the first entry in the set, and the .debug_abbrev.dwo must
12058 be the second entry. Other members of the set may follow in any order.
12059
12060 ---
12061
12062 DWP Version 2:
12063
12064 DWP Version 2 combines all the .debug_info, etc. sections into one,
12065 and the entries in the index tables are now offsets into these sections.
12066 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12067 section.
12068
12069 Index Section Contents:
12070 Header
12071 Hash Table of Signatures dwp_hash_table.hash_table
12072 Parallel Table of Indices dwp_hash_table.unit_table
12073 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12074 Table of Section Sizes dwp_hash_table.v2.sizes
12075
12076 The index section header consists of:
12077
12078 V, 32 bit version number
12079 L, 32 bit number of columns in the table of section offsets
12080 N, 32 bit number of compilation units or type units in the index
12081 M, 32 bit number of slots in the hash table
12082
12083 Numbers are recorded using the byte order of the application binary.
12084
12085 The hash table has the same format as version 1.
12086 The parallel table of indices has the same format as version 1,
12087 except that the entries are origin-1 indices into the table of sections
12088 offsets and the table of section sizes.
12089
12090 The table of offsets begins immediately following the parallel table
12091 (at offset 16 + 12 * M from the beginning of the section). The table is
12092 a two-dimensional array of 32-bit words (using the byte order of the
12093 application binary), with L columns and N+1 rows, in row-major order.
12094 Each row in the array is indexed starting from 0. The first row provides
12095 a key to the remaining rows: each column in this row provides an identifier
12096 for a debug section, and the offsets in the same column of subsequent rows
12097 refer to that section. The section identifiers are:
12098
12099 DW_SECT_INFO 1 .debug_info.dwo
12100 DW_SECT_TYPES 2 .debug_types.dwo
12101 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12102 DW_SECT_LINE 4 .debug_line.dwo
12103 DW_SECT_LOC 5 .debug_loc.dwo
12104 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12105 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12106 DW_SECT_MACRO 8 .debug_macro.dwo
12107
12108 The offsets provided by the CU and TU index sections are the base offsets
12109 for the contributions made by each CU or TU to the corresponding section
12110 in the package file. Each CU and TU header contains an abbrev_offset
12111 field, used to find the abbreviations table for that CU or TU within the
12112 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12113 be interpreted as relative to the base offset given in the index section.
12114 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12115 should be interpreted as relative to the base offset for .debug_line.dwo,
12116 and offsets into other debug sections obtained from DWARF attributes should
12117 also be interpreted as relative to the corresponding base offset.
12118
12119 The table of sizes begins immediately following the table of offsets.
12120 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12121 with L columns and N rows, in row-major order. Each row in the array is
12122 indexed starting from 1 (row 0 is shared by the two tables).
12123
12124 ---
12125
12126 Hash table lookup is handled the same in version 1 and 2:
12127
12128 We assume that N and M will not exceed 2^32 - 1.
12129 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12130
12131 Given a 64-bit compilation unit signature or a type signature S, an entry
12132 in the hash table is located as follows:
12133
12134 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12135 the low-order k bits all set to 1.
12136
12137 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12138
12139 3) If the hash table entry at index H matches the signature, use that
12140 entry. If the hash table entry at index H is unused (all zeroes),
12141 terminate the search: the signature is not present in the table.
12142
12143 4) Let H = (H + H') modulo M. Repeat at Step 3.
12144
12145 Because M > N and H' and M are relatively prime, the search is guaranteed
12146 to stop at an unused slot or find the match. */
12147
12148 /* Create a hash table to map DWO IDs to their CU/TU entry in
12149 .debug_{info,types}.dwo in DWP_FILE.
12150 Returns NULL if there isn't one.
12151 Note: This function processes DWP files only, not DWO files. */
12152
12153 static struct dwp_hash_table *
12154 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12155 struct dwp_file *dwp_file, int is_debug_types)
12156 {
12157 struct objfile *objfile = dwarf2_per_objfile->objfile;
12158 bfd *dbfd = dwp_file->dbfd.get ();
12159 const gdb_byte *index_ptr, *index_end;
12160 struct dwarf2_section_info *index;
12161 uint32_t version, nr_columns, nr_units, nr_slots;
12162 struct dwp_hash_table *htab;
12163
12164 if (is_debug_types)
12165 index = &dwp_file->sections.tu_index;
12166 else
12167 index = &dwp_file->sections.cu_index;
12168
12169 if (dwarf2_section_empty_p (index))
12170 return NULL;
12171 dwarf2_read_section (objfile, index);
12172
12173 index_ptr = index->buffer;
12174 index_end = index_ptr + index->size;
12175
12176 version = read_4_bytes (dbfd, index_ptr);
12177 index_ptr += 4;
12178 if (version == 2)
12179 nr_columns = read_4_bytes (dbfd, index_ptr);
12180 else
12181 nr_columns = 0;
12182 index_ptr += 4;
12183 nr_units = read_4_bytes (dbfd, index_ptr);
12184 index_ptr += 4;
12185 nr_slots = read_4_bytes (dbfd, index_ptr);
12186 index_ptr += 4;
12187
12188 if (version != 1 && version != 2)
12189 {
12190 error (_("Dwarf Error: unsupported DWP file version (%s)"
12191 " [in module %s]"),
12192 pulongest (version), dwp_file->name);
12193 }
12194 if (nr_slots != (nr_slots & -nr_slots))
12195 {
12196 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12197 " is not power of 2 [in module %s]"),
12198 pulongest (nr_slots), dwp_file->name);
12199 }
12200
12201 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12202 htab->version = version;
12203 htab->nr_columns = nr_columns;
12204 htab->nr_units = nr_units;
12205 htab->nr_slots = nr_slots;
12206 htab->hash_table = index_ptr;
12207 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12208
12209 /* Exit early if the table is empty. */
12210 if (nr_slots == 0 || nr_units == 0
12211 || (version == 2 && nr_columns == 0))
12212 {
12213 /* All must be zero. */
12214 if (nr_slots != 0 || nr_units != 0
12215 || (version == 2 && nr_columns != 0))
12216 {
12217 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
12218 " all zero [in modules %s]"),
12219 dwp_file->name);
12220 }
12221 return htab;
12222 }
12223
12224 if (version == 1)
12225 {
12226 htab->section_pool.v1.indices =
12227 htab->unit_table + sizeof (uint32_t) * nr_slots;
12228 /* It's harder to decide whether the section is too small in v1.
12229 V1 is deprecated anyway so we punt. */
12230 }
12231 else
12232 {
12233 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12234 int *ids = htab->section_pool.v2.section_ids;
12235 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
12236 /* Reverse map for error checking. */
12237 int ids_seen[DW_SECT_MAX + 1];
12238 int i;
12239
12240 if (nr_columns < 2)
12241 {
12242 error (_("Dwarf Error: bad DWP hash table, too few columns"
12243 " in section table [in module %s]"),
12244 dwp_file->name);
12245 }
12246 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12247 {
12248 error (_("Dwarf Error: bad DWP hash table, too many columns"
12249 " in section table [in module %s]"),
12250 dwp_file->name);
12251 }
12252 memset (ids, 255, sizeof_ids);
12253 memset (ids_seen, 255, sizeof (ids_seen));
12254 for (i = 0; i < nr_columns; ++i)
12255 {
12256 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12257
12258 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12259 {
12260 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12261 " in section table [in module %s]"),
12262 id, dwp_file->name);
12263 }
12264 if (ids_seen[id] != -1)
12265 {
12266 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12267 " id %d in section table [in module %s]"),
12268 id, dwp_file->name);
12269 }
12270 ids_seen[id] = i;
12271 ids[i] = id;
12272 }
12273 /* Must have exactly one info or types section. */
12274 if (((ids_seen[DW_SECT_INFO] != -1)
12275 + (ids_seen[DW_SECT_TYPES] != -1))
12276 != 1)
12277 {
12278 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12279 " DWO info/types section [in module %s]"),
12280 dwp_file->name);
12281 }
12282 /* Must have an abbrev section. */
12283 if (ids_seen[DW_SECT_ABBREV] == -1)
12284 {
12285 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12286 " section [in module %s]"),
12287 dwp_file->name);
12288 }
12289 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12290 htab->section_pool.v2.sizes =
12291 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12292 * nr_units * nr_columns);
12293 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12294 * nr_units * nr_columns))
12295 > index_end)
12296 {
12297 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12298 " [in module %s]"),
12299 dwp_file->name);
12300 }
12301 }
12302
12303 return htab;
12304 }
12305
12306 /* Update SECTIONS with the data from SECTP.
12307
12308 This function is like the other "locate" section routines that are
12309 passed to bfd_map_over_sections, but in this context the sections to
12310 read comes from the DWP V1 hash table, not the full ELF section table.
12311
12312 The result is non-zero for success, or zero if an error was found. */
12313
12314 static int
12315 locate_v1_virtual_dwo_sections (asection *sectp,
12316 struct virtual_v1_dwo_sections *sections)
12317 {
12318 const struct dwop_section_names *names = &dwop_section_names;
12319
12320 if (section_is_p (sectp->name, &names->abbrev_dwo))
12321 {
12322 /* There can be only one. */
12323 if (sections->abbrev.s.section != NULL)
12324 return 0;
12325 sections->abbrev.s.section = sectp;
12326 sections->abbrev.size = bfd_section_size (sectp);
12327 }
12328 else if (section_is_p (sectp->name, &names->info_dwo)
12329 || section_is_p (sectp->name, &names->types_dwo))
12330 {
12331 /* There can be only one. */
12332 if (sections->info_or_types.s.section != NULL)
12333 return 0;
12334 sections->info_or_types.s.section = sectp;
12335 sections->info_or_types.size = bfd_section_size (sectp);
12336 }
12337 else if (section_is_p (sectp->name, &names->line_dwo))
12338 {
12339 /* There can be only one. */
12340 if (sections->line.s.section != NULL)
12341 return 0;
12342 sections->line.s.section = sectp;
12343 sections->line.size = bfd_section_size (sectp);
12344 }
12345 else if (section_is_p (sectp->name, &names->loc_dwo))
12346 {
12347 /* There can be only one. */
12348 if (sections->loc.s.section != NULL)
12349 return 0;
12350 sections->loc.s.section = sectp;
12351 sections->loc.size = bfd_section_size (sectp);
12352 }
12353 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12354 {
12355 /* There can be only one. */
12356 if (sections->macinfo.s.section != NULL)
12357 return 0;
12358 sections->macinfo.s.section = sectp;
12359 sections->macinfo.size = bfd_section_size (sectp);
12360 }
12361 else if (section_is_p (sectp->name, &names->macro_dwo))
12362 {
12363 /* There can be only one. */
12364 if (sections->macro.s.section != NULL)
12365 return 0;
12366 sections->macro.s.section = sectp;
12367 sections->macro.size = bfd_section_size (sectp);
12368 }
12369 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12370 {
12371 /* There can be only one. */
12372 if (sections->str_offsets.s.section != NULL)
12373 return 0;
12374 sections->str_offsets.s.section = sectp;
12375 sections->str_offsets.size = bfd_section_size (sectp);
12376 }
12377 else
12378 {
12379 /* No other kind of section is valid. */
12380 return 0;
12381 }
12382
12383 return 1;
12384 }
12385
12386 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12387 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12388 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12389 This is for DWP version 1 files. */
12390
12391 static struct dwo_unit *
12392 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12393 struct dwp_file *dwp_file,
12394 uint32_t unit_index,
12395 const char *comp_dir,
12396 ULONGEST signature, int is_debug_types)
12397 {
12398 struct objfile *objfile = dwarf2_per_objfile->objfile;
12399 const struct dwp_hash_table *dwp_htab =
12400 is_debug_types ? dwp_file->tus : dwp_file->cus;
12401 bfd *dbfd = dwp_file->dbfd.get ();
12402 const char *kind = is_debug_types ? "TU" : "CU";
12403 struct dwo_file *dwo_file;
12404 struct dwo_unit *dwo_unit;
12405 struct virtual_v1_dwo_sections sections;
12406 void **dwo_file_slot;
12407 int i;
12408
12409 gdb_assert (dwp_file->version == 1);
12410
12411 if (dwarf_read_debug)
12412 {
12413 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12414 kind,
12415 pulongest (unit_index), hex_string (signature),
12416 dwp_file->name);
12417 }
12418
12419 /* Fetch the sections of this DWO unit.
12420 Put a limit on the number of sections we look for so that bad data
12421 doesn't cause us to loop forever. */
12422
12423 #define MAX_NR_V1_DWO_SECTIONS \
12424 (1 /* .debug_info or .debug_types */ \
12425 + 1 /* .debug_abbrev */ \
12426 + 1 /* .debug_line */ \
12427 + 1 /* .debug_loc */ \
12428 + 1 /* .debug_str_offsets */ \
12429 + 1 /* .debug_macro or .debug_macinfo */ \
12430 + 1 /* trailing zero */)
12431
12432 memset (&sections, 0, sizeof (sections));
12433
12434 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12435 {
12436 asection *sectp;
12437 uint32_t section_nr =
12438 read_4_bytes (dbfd,
12439 dwp_htab->section_pool.v1.indices
12440 + (unit_index + i) * sizeof (uint32_t));
12441
12442 if (section_nr == 0)
12443 break;
12444 if (section_nr >= dwp_file->num_sections)
12445 {
12446 error (_("Dwarf Error: bad DWP hash table, section number too large"
12447 " [in module %s]"),
12448 dwp_file->name);
12449 }
12450
12451 sectp = dwp_file->elf_sections[section_nr];
12452 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12453 {
12454 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12455 " [in module %s]"),
12456 dwp_file->name);
12457 }
12458 }
12459
12460 if (i < 2
12461 || dwarf2_section_empty_p (&sections.info_or_types)
12462 || dwarf2_section_empty_p (&sections.abbrev))
12463 {
12464 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12465 " [in module %s]"),
12466 dwp_file->name);
12467 }
12468 if (i == MAX_NR_V1_DWO_SECTIONS)
12469 {
12470 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12471 " [in module %s]"),
12472 dwp_file->name);
12473 }
12474
12475 /* It's easier for the rest of the code if we fake a struct dwo_file and
12476 have dwo_unit "live" in that. At least for now.
12477
12478 The DWP file can be made up of a random collection of CUs and TUs.
12479 However, for each CU + set of TUs that came from the same original DWO
12480 file, we can combine them back into a virtual DWO file to save space
12481 (fewer struct dwo_file objects to allocate). Remember that for really
12482 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12483
12484 std::string virtual_dwo_name =
12485 string_printf ("virtual-dwo/%d-%d-%d-%d",
12486 get_section_id (&sections.abbrev),
12487 get_section_id (&sections.line),
12488 get_section_id (&sections.loc),
12489 get_section_id (&sections.str_offsets));
12490 /* Can we use an existing virtual DWO file? */
12491 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12492 virtual_dwo_name.c_str (),
12493 comp_dir);
12494 /* Create one if necessary. */
12495 if (*dwo_file_slot == NULL)
12496 {
12497 if (dwarf_read_debug)
12498 {
12499 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12500 virtual_dwo_name.c_str ());
12501 }
12502 dwo_file = new struct dwo_file;
12503 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12504 virtual_dwo_name);
12505 dwo_file->comp_dir = comp_dir;
12506 dwo_file->sections.abbrev = sections.abbrev;
12507 dwo_file->sections.line = sections.line;
12508 dwo_file->sections.loc = sections.loc;
12509 dwo_file->sections.macinfo = sections.macinfo;
12510 dwo_file->sections.macro = sections.macro;
12511 dwo_file->sections.str_offsets = sections.str_offsets;
12512 /* The "str" section is global to the entire DWP file. */
12513 dwo_file->sections.str = dwp_file->sections.str;
12514 /* The info or types section is assigned below to dwo_unit,
12515 there's no need to record it in dwo_file.
12516 Also, we can't simply record type sections in dwo_file because
12517 we record a pointer into the vector in dwo_unit. As we collect more
12518 types we'll grow the vector and eventually have to reallocate space
12519 for it, invalidating all copies of pointers into the previous
12520 contents. */
12521 *dwo_file_slot = dwo_file;
12522 }
12523 else
12524 {
12525 if (dwarf_read_debug)
12526 {
12527 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12528 virtual_dwo_name.c_str ());
12529 }
12530 dwo_file = (struct dwo_file *) *dwo_file_slot;
12531 }
12532
12533 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12534 dwo_unit->dwo_file = dwo_file;
12535 dwo_unit->signature = signature;
12536 dwo_unit->section =
12537 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12538 *dwo_unit->section = sections.info_or_types;
12539 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12540
12541 return dwo_unit;
12542 }
12543
12544 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12545 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12546 piece within that section used by a TU/CU, return a virtual section
12547 of just that piece. */
12548
12549 static struct dwarf2_section_info
12550 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12551 struct dwarf2_section_info *section,
12552 bfd_size_type offset, bfd_size_type size)
12553 {
12554 struct dwarf2_section_info result;
12555 asection *sectp;
12556
12557 gdb_assert (section != NULL);
12558 gdb_assert (!section->is_virtual);
12559
12560 memset (&result, 0, sizeof (result));
12561 result.s.containing_section = section;
12562 result.is_virtual = true;
12563
12564 if (size == 0)
12565 return result;
12566
12567 sectp = get_section_bfd_section (section);
12568
12569 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12570 bounds of the real section. This is a pretty-rare event, so just
12571 flag an error (easier) instead of a warning and trying to cope. */
12572 if (sectp == NULL
12573 || offset + size > bfd_section_size (sectp))
12574 {
12575 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12576 " in section %s [in module %s]"),
12577 sectp ? bfd_section_name (sectp) : "<unknown>",
12578 objfile_name (dwarf2_per_objfile->objfile));
12579 }
12580
12581 result.virtual_offset = offset;
12582 result.size = size;
12583 return result;
12584 }
12585
12586 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12587 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12588 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12589 This is for DWP version 2 files. */
12590
12591 static struct dwo_unit *
12592 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12593 struct dwp_file *dwp_file,
12594 uint32_t unit_index,
12595 const char *comp_dir,
12596 ULONGEST signature, int is_debug_types)
12597 {
12598 struct objfile *objfile = dwarf2_per_objfile->objfile;
12599 const struct dwp_hash_table *dwp_htab =
12600 is_debug_types ? dwp_file->tus : dwp_file->cus;
12601 bfd *dbfd = dwp_file->dbfd.get ();
12602 const char *kind = is_debug_types ? "TU" : "CU";
12603 struct dwo_file *dwo_file;
12604 struct dwo_unit *dwo_unit;
12605 struct virtual_v2_dwo_sections sections;
12606 void **dwo_file_slot;
12607 int i;
12608
12609 gdb_assert (dwp_file->version == 2);
12610
12611 if (dwarf_read_debug)
12612 {
12613 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12614 kind,
12615 pulongest (unit_index), hex_string (signature),
12616 dwp_file->name);
12617 }
12618
12619 /* Fetch the section offsets of this DWO unit. */
12620
12621 memset (&sections, 0, sizeof (sections));
12622
12623 for (i = 0; i < dwp_htab->nr_columns; ++i)
12624 {
12625 uint32_t offset = read_4_bytes (dbfd,
12626 dwp_htab->section_pool.v2.offsets
12627 + (((unit_index - 1) * dwp_htab->nr_columns
12628 + i)
12629 * sizeof (uint32_t)));
12630 uint32_t size = read_4_bytes (dbfd,
12631 dwp_htab->section_pool.v2.sizes
12632 + (((unit_index - 1) * dwp_htab->nr_columns
12633 + i)
12634 * sizeof (uint32_t)));
12635
12636 switch (dwp_htab->section_pool.v2.section_ids[i])
12637 {
12638 case DW_SECT_INFO:
12639 case DW_SECT_TYPES:
12640 sections.info_or_types_offset = offset;
12641 sections.info_or_types_size = size;
12642 break;
12643 case DW_SECT_ABBREV:
12644 sections.abbrev_offset = offset;
12645 sections.abbrev_size = size;
12646 break;
12647 case DW_SECT_LINE:
12648 sections.line_offset = offset;
12649 sections.line_size = size;
12650 break;
12651 case DW_SECT_LOC:
12652 sections.loc_offset = offset;
12653 sections.loc_size = size;
12654 break;
12655 case DW_SECT_STR_OFFSETS:
12656 sections.str_offsets_offset = offset;
12657 sections.str_offsets_size = size;
12658 break;
12659 case DW_SECT_MACINFO:
12660 sections.macinfo_offset = offset;
12661 sections.macinfo_size = size;
12662 break;
12663 case DW_SECT_MACRO:
12664 sections.macro_offset = offset;
12665 sections.macro_size = size;
12666 break;
12667 }
12668 }
12669
12670 /* It's easier for the rest of the code if we fake a struct dwo_file and
12671 have dwo_unit "live" in that. At least for now.
12672
12673 The DWP file can be made up of a random collection of CUs and TUs.
12674 However, for each CU + set of TUs that came from the same original DWO
12675 file, we can combine them back into a virtual DWO file to save space
12676 (fewer struct dwo_file objects to allocate). Remember that for really
12677 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12678
12679 std::string virtual_dwo_name =
12680 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12681 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12682 (long) (sections.line_size ? sections.line_offset : 0),
12683 (long) (sections.loc_size ? sections.loc_offset : 0),
12684 (long) (sections.str_offsets_size
12685 ? sections.str_offsets_offset : 0));
12686 /* Can we use an existing virtual DWO file? */
12687 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12688 virtual_dwo_name.c_str (),
12689 comp_dir);
12690 /* Create one if necessary. */
12691 if (*dwo_file_slot == NULL)
12692 {
12693 if (dwarf_read_debug)
12694 {
12695 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12696 virtual_dwo_name.c_str ());
12697 }
12698 dwo_file = new struct dwo_file;
12699 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12700 virtual_dwo_name);
12701 dwo_file->comp_dir = comp_dir;
12702 dwo_file->sections.abbrev =
12703 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12704 sections.abbrev_offset, sections.abbrev_size);
12705 dwo_file->sections.line =
12706 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12707 sections.line_offset, sections.line_size);
12708 dwo_file->sections.loc =
12709 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12710 sections.loc_offset, sections.loc_size);
12711 dwo_file->sections.macinfo =
12712 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12713 sections.macinfo_offset, sections.macinfo_size);
12714 dwo_file->sections.macro =
12715 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12716 sections.macro_offset, sections.macro_size);
12717 dwo_file->sections.str_offsets =
12718 create_dwp_v2_section (dwarf2_per_objfile,
12719 &dwp_file->sections.str_offsets,
12720 sections.str_offsets_offset,
12721 sections.str_offsets_size);
12722 /* The "str" section is global to the entire DWP file. */
12723 dwo_file->sections.str = dwp_file->sections.str;
12724 /* The info or types section is assigned below to dwo_unit,
12725 there's no need to record it in dwo_file.
12726 Also, we can't simply record type sections in dwo_file because
12727 we record a pointer into the vector in dwo_unit. As we collect more
12728 types we'll grow the vector and eventually have to reallocate space
12729 for it, invalidating all copies of pointers into the previous
12730 contents. */
12731 *dwo_file_slot = dwo_file;
12732 }
12733 else
12734 {
12735 if (dwarf_read_debug)
12736 {
12737 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12738 virtual_dwo_name.c_str ());
12739 }
12740 dwo_file = (struct dwo_file *) *dwo_file_slot;
12741 }
12742
12743 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12744 dwo_unit->dwo_file = dwo_file;
12745 dwo_unit->signature = signature;
12746 dwo_unit->section =
12747 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12748 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12749 is_debug_types
12750 ? &dwp_file->sections.types
12751 : &dwp_file->sections.info,
12752 sections.info_or_types_offset,
12753 sections.info_or_types_size);
12754 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12755
12756 return dwo_unit;
12757 }
12758
12759 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12760 Returns NULL if the signature isn't found. */
12761
12762 static struct dwo_unit *
12763 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12764 struct dwp_file *dwp_file, const char *comp_dir,
12765 ULONGEST signature, int is_debug_types)
12766 {
12767 const struct dwp_hash_table *dwp_htab =
12768 is_debug_types ? dwp_file->tus : dwp_file->cus;
12769 bfd *dbfd = dwp_file->dbfd.get ();
12770 uint32_t mask = dwp_htab->nr_slots - 1;
12771 uint32_t hash = signature & mask;
12772 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12773 unsigned int i;
12774 void **slot;
12775 struct dwo_unit find_dwo_cu;
12776
12777 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12778 find_dwo_cu.signature = signature;
12779 slot = htab_find_slot (is_debug_types
12780 ? dwp_file->loaded_tus
12781 : dwp_file->loaded_cus,
12782 &find_dwo_cu, INSERT);
12783
12784 if (*slot != NULL)
12785 return (struct dwo_unit *) *slot;
12786
12787 /* Use a for loop so that we don't loop forever on bad debug info. */
12788 for (i = 0; i < dwp_htab->nr_slots; ++i)
12789 {
12790 ULONGEST signature_in_table;
12791
12792 signature_in_table =
12793 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12794 if (signature_in_table == signature)
12795 {
12796 uint32_t unit_index =
12797 read_4_bytes (dbfd,
12798 dwp_htab->unit_table + hash * sizeof (uint32_t));
12799
12800 if (dwp_file->version == 1)
12801 {
12802 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12803 dwp_file, unit_index,
12804 comp_dir, signature,
12805 is_debug_types);
12806 }
12807 else
12808 {
12809 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12810 dwp_file, unit_index,
12811 comp_dir, signature,
12812 is_debug_types);
12813 }
12814 return (struct dwo_unit *) *slot;
12815 }
12816 if (signature_in_table == 0)
12817 return NULL;
12818 hash = (hash + hash2) & mask;
12819 }
12820
12821 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12822 " [in module %s]"),
12823 dwp_file->name);
12824 }
12825
12826 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12827 Open the file specified by FILE_NAME and hand it off to BFD for
12828 preliminary analysis. Return a newly initialized bfd *, which
12829 includes a canonicalized copy of FILE_NAME.
12830 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12831 SEARCH_CWD is true if the current directory is to be searched.
12832 It will be searched before debug-file-directory.
12833 If successful, the file is added to the bfd include table of the
12834 objfile's bfd (see gdb_bfd_record_inclusion).
12835 If unable to find/open the file, return NULL.
12836 NOTE: This function is derived from symfile_bfd_open. */
12837
12838 static gdb_bfd_ref_ptr
12839 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12840 const char *file_name, int is_dwp, int search_cwd)
12841 {
12842 int desc;
12843 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12844 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12845 to debug_file_directory. */
12846 const char *search_path;
12847 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12848
12849 gdb::unique_xmalloc_ptr<char> search_path_holder;
12850 if (search_cwd)
12851 {
12852 if (*debug_file_directory != '\0')
12853 {
12854 search_path_holder.reset (concat (".", dirname_separator_string,
12855 debug_file_directory,
12856 (char *) NULL));
12857 search_path = search_path_holder.get ();
12858 }
12859 else
12860 search_path = ".";
12861 }
12862 else
12863 search_path = debug_file_directory;
12864
12865 openp_flags flags = OPF_RETURN_REALPATH;
12866 if (is_dwp)
12867 flags |= OPF_SEARCH_IN_PATH;
12868
12869 gdb::unique_xmalloc_ptr<char> absolute_name;
12870 desc = openp (search_path, flags, file_name,
12871 O_RDONLY | O_BINARY, &absolute_name);
12872 if (desc < 0)
12873 return NULL;
12874
12875 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12876 gnutarget, desc));
12877 if (sym_bfd == NULL)
12878 return NULL;
12879 bfd_set_cacheable (sym_bfd.get (), 1);
12880
12881 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12882 return NULL;
12883
12884 /* Success. Record the bfd as having been included by the objfile's bfd.
12885 This is important because things like demangled_names_hash lives in the
12886 objfile's per_bfd space and may have references to things like symbol
12887 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12888 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12889
12890 return sym_bfd;
12891 }
12892
12893 /* Try to open DWO file FILE_NAME.
12894 COMP_DIR is the DW_AT_comp_dir attribute.
12895 The result is the bfd handle of the file.
12896 If there is a problem finding or opening the file, return NULL.
12897 Upon success, the canonicalized path of the file is stored in the bfd,
12898 same as symfile_bfd_open. */
12899
12900 static gdb_bfd_ref_ptr
12901 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12902 const char *file_name, const char *comp_dir)
12903 {
12904 if (IS_ABSOLUTE_PATH (file_name))
12905 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12906 0 /*is_dwp*/, 0 /*search_cwd*/);
12907
12908 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12909
12910 if (comp_dir != NULL)
12911 {
12912 char *path_to_try = concat (comp_dir, SLASH_STRING,
12913 file_name, (char *) NULL);
12914
12915 /* NOTE: If comp_dir is a relative path, this will also try the
12916 search path, which seems useful. */
12917 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12918 path_to_try,
12919 0 /*is_dwp*/,
12920 1 /*search_cwd*/));
12921 xfree (path_to_try);
12922 if (abfd != NULL)
12923 return abfd;
12924 }
12925
12926 /* That didn't work, try debug-file-directory, which, despite its name,
12927 is a list of paths. */
12928
12929 if (*debug_file_directory == '\0')
12930 return NULL;
12931
12932 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12933 0 /*is_dwp*/, 1 /*search_cwd*/);
12934 }
12935
12936 /* This function is mapped across the sections and remembers the offset and
12937 size of each of the DWO debugging sections we are interested in. */
12938
12939 static void
12940 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12941 {
12942 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12943 const struct dwop_section_names *names = &dwop_section_names;
12944
12945 if (section_is_p (sectp->name, &names->abbrev_dwo))
12946 {
12947 dwo_sections->abbrev.s.section = sectp;
12948 dwo_sections->abbrev.size = bfd_section_size (sectp);
12949 }
12950 else if (section_is_p (sectp->name, &names->info_dwo))
12951 {
12952 dwo_sections->info.s.section = sectp;
12953 dwo_sections->info.size = bfd_section_size (sectp);
12954 }
12955 else if (section_is_p (sectp->name, &names->line_dwo))
12956 {
12957 dwo_sections->line.s.section = sectp;
12958 dwo_sections->line.size = bfd_section_size (sectp);
12959 }
12960 else if (section_is_p (sectp->name, &names->loc_dwo))
12961 {
12962 dwo_sections->loc.s.section = sectp;
12963 dwo_sections->loc.size = bfd_section_size (sectp);
12964 }
12965 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12966 {
12967 dwo_sections->macinfo.s.section = sectp;
12968 dwo_sections->macinfo.size = bfd_section_size (sectp);
12969 }
12970 else if (section_is_p (sectp->name, &names->macro_dwo))
12971 {
12972 dwo_sections->macro.s.section = sectp;
12973 dwo_sections->macro.size = bfd_section_size (sectp);
12974 }
12975 else if (section_is_p (sectp->name, &names->str_dwo))
12976 {
12977 dwo_sections->str.s.section = sectp;
12978 dwo_sections->str.size = bfd_section_size (sectp);
12979 }
12980 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12981 {
12982 dwo_sections->str_offsets.s.section = sectp;
12983 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12984 }
12985 else if (section_is_p (sectp->name, &names->types_dwo))
12986 {
12987 struct dwarf2_section_info type_section;
12988
12989 memset (&type_section, 0, sizeof (type_section));
12990 type_section.s.section = sectp;
12991 type_section.size = bfd_section_size (sectp);
12992 dwo_sections->types.push_back (type_section);
12993 }
12994 }
12995
12996 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12997 by PER_CU. This is for the non-DWP case.
12998 The result is NULL if DWO_NAME can't be found. */
12999
13000 static struct dwo_file *
13001 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13002 const char *dwo_name, const char *comp_dir)
13003 {
13004 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
13005
13006 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
13007 if (dbfd == NULL)
13008 {
13009 if (dwarf_read_debug)
13010 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13011 return NULL;
13012 }
13013
13014 dwo_file_up dwo_file (new struct dwo_file);
13015 dwo_file->dwo_name = dwo_name;
13016 dwo_file->comp_dir = comp_dir;
13017 dwo_file->dbfd = std::move (dbfd);
13018
13019 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
13020 &dwo_file->sections);
13021
13022 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13023 dwo_file->cus);
13024
13025 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
13026 dwo_file->sections.types, dwo_file->tus);
13027
13028 if (dwarf_read_debug)
13029 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13030
13031 return dwo_file.release ();
13032 }
13033
13034 /* This function is mapped across the sections and remembers the offset and
13035 size of each of the DWP debugging sections common to version 1 and 2 that
13036 we are interested in. */
13037
13038 static void
13039 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13040 void *dwp_file_ptr)
13041 {
13042 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13043 const struct dwop_section_names *names = &dwop_section_names;
13044 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13045
13046 /* Record the ELF section number for later lookup: this is what the
13047 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13048 gdb_assert (elf_section_nr < dwp_file->num_sections);
13049 dwp_file->elf_sections[elf_section_nr] = sectp;
13050
13051 /* Look for specific sections that we need. */
13052 if (section_is_p (sectp->name, &names->str_dwo))
13053 {
13054 dwp_file->sections.str.s.section = sectp;
13055 dwp_file->sections.str.size = bfd_section_size (sectp);
13056 }
13057 else if (section_is_p (sectp->name, &names->cu_index))
13058 {
13059 dwp_file->sections.cu_index.s.section = sectp;
13060 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
13061 }
13062 else if (section_is_p (sectp->name, &names->tu_index))
13063 {
13064 dwp_file->sections.tu_index.s.section = sectp;
13065 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
13066 }
13067 }
13068
13069 /* This function is mapped across the sections and remembers the offset and
13070 size of each of the DWP version 2 debugging sections that we are interested
13071 in. This is split into a separate function because we don't know if we
13072 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13073
13074 static void
13075 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13076 {
13077 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13078 const struct dwop_section_names *names = &dwop_section_names;
13079 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13080
13081 /* Record the ELF section number for later lookup: this is what the
13082 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13083 gdb_assert (elf_section_nr < dwp_file->num_sections);
13084 dwp_file->elf_sections[elf_section_nr] = sectp;
13085
13086 /* Look for specific sections that we need. */
13087 if (section_is_p (sectp->name, &names->abbrev_dwo))
13088 {
13089 dwp_file->sections.abbrev.s.section = sectp;
13090 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13091 }
13092 else if (section_is_p (sectp->name, &names->info_dwo))
13093 {
13094 dwp_file->sections.info.s.section = sectp;
13095 dwp_file->sections.info.size = bfd_section_size (sectp);
13096 }
13097 else if (section_is_p (sectp->name, &names->line_dwo))
13098 {
13099 dwp_file->sections.line.s.section = sectp;
13100 dwp_file->sections.line.size = bfd_section_size (sectp);
13101 }
13102 else if (section_is_p (sectp->name, &names->loc_dwo))
13103 {
13104 dwp_file->sections.loc.s.section = sectp;
13105 dwp_file->sections.loc.size = bfd_section_size (sectp);
13106 }
13107 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13108 {
13109 dwp_file->sections.macinfo.s.section = sectp;
13110 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
13111 }
13112 else if (section_is_p (sectp->name, &names->macro_dwo))
13113 {
13114 dwp_file->sections.macro.s.section = sectp;
13115 dwp_file->sections.macro.size = bfd_section_size (sectp);
13116 }
13117 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13118 {
13119 dwp_file->sections.str_offsets.s.section = sectp;
13120 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13121 }
13122 else if (section_is_p (sectp->name, &names->types_dwo))
13123 {
13124 dwp_file->sections.types.s.section = sectp;
13125 dwp_file->sections.types.size = bfd_section_size (sectp);
13126 }
13127 }
13128
13129 /* Hash function for dwp_file loaded CUs/TUs. */
13130
13131 static hashval_t
13132 hash_dwp_loaded_cutus (const void *item)
13133 {
13134 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13135
13136 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13137 return dwo_unit->signature;
13138 }
13139
13140 /* Equality function for dwp_file loaded CUs/TUs. */
13141
13142 static int
13143 eq_dwp_loaded_cutus (const void *a, const void *b)
13144 {
13145 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13146 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13147
13148 return dua->signature == dub->signature;
13149 }
13150
13151 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13152
13153 static htab_t
13154 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13155 {
13156 return htab_create_alloc_ex (3,
13157 hash_dwp_loaded_cutus,
13158 eq_dwp_loaded_cutus,
13159 NULL,
13160 &objfile->objfile_obstack,
13161 hashtab_obstack_allocate,
13162 dummy_obstack_deallocate);
13163 }
13164
13165 /* Try to open DWP file FILE_NAME.
13166 The result is the bfd handle of the file.
13167 If there is a problem finding or opening the file, return NULL.
13168 Upon success, the canonicalized path of the file is stored in the bfd,
13169 same as symfile_bfd_open. */
13170
13171 static gdb_bfd_ref_ptr
13172 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13173 const char *file_name)
13174 {
13175 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13176 1 /*is_dwp*/,
13177 1 /*search_cwd*/));
13178 if (abfd != NULL)
13179 return abfd;
13180
13181 /* Work around upstream bug 15652.
13182 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13183 [Whether that's a "bug" is debatable, but it is getting in our way.]
13184 We have no real idea where the dwp file is, because gdb's realpath-ing
13185 of the executable's path may have discarded the needed info.
13186 [IWBN if the dwp file name was recorded in the executable, akin to
13187 .gnu_debuglink, but that doesn't exist yet.]
13188 Strip the directory from FILE_NAME and search again. */
13189 if (*debug_file_directory != '\0')
13190 {
13191 /* Don't implicitly search the current directory here.
13192 If the user wants to search "." to handle this case,
13193 it must be added to debug-file-directory. */
13194 return try_open_dwop_file (dwarf2_per_objfile,
13195 lbasename (file_name), 1 /*is_dwp*/,
13196 0 /*search_cwd*/);
13197 }
13198
13199 return NULL;
13200 }
13201
13202 /* Initialize the use of the DWP file for the current objfile.
13203 By convention the name of the DWP file is ${objfile}.dwp.
13204 The result is NULL if it can't be found. */
13205
13206 static std::unique_ptr<struct dwp_file>
13207 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13208 {
13209 struct objfile *objfile = dwarf2_per_objfile->objfile;
13210
13211 /* Try to find first .dwp for the binary file before any symbolic links
13212 resolving. */
13213
13214 /* If the objfile is a debug file, find the name of the real binary
13215 file and get the name of dwp file from there. */
13216 std::string dwp_name;
13217 if (objfile->separate_debug_objfile_backlink != NULL)
13218 {
13219 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13220 const char *backlink_basename = lbasename (backlink->original_name);
13221
13222 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13223 }
13224 else
13225 dwp_name = objfile->original_name;
13226
13227 dwp_name += ".dwp";
13228
13229 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13230 if (dbfd == NULL
13231 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13232 {
13233 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13234 dwp_name = objfile_name (objfile);
13235 dwp_name += ".dwp";
13236 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13237 }
13238
13239 if (dbfd == NULL)
13240 {
13241 if (dwarf_read_debug)
13242 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13243 return std::unique_ptr<dwp_file> ();
13244 }
13245
13246 const char *name = bfd_get_filename (dbfd.get ());
13247 std::unique_ptr<struct dwp_file> dwp_file
13248 (new struct dwp_file (name, std::move (dbfd)));
13249
13250 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13251 dwp_file->elf_sections =
13252 OBSTACK_CALLOC (&objfile->objfile_obstack,
13253 dwp_file->num_sections, asection *);
13254
13255 bfd_map_over_sections (dwp_file->dbfd.get (),
13256 dwarf2_locate_common_dwp_sections,
13257 dwp_file.get ());
13258
13259 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13260 0);
13261
13262 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13263 1);
13264
13265 /* The DWP file version is stored in the hash table. Oh well. */
13266 if (dwp_file->cus && dwp_file->tus
13267 && dwp_file->cus->version != dwp_file->tus->version)
13268 {
13269 /* Technically speaking, we should try to limp along, but this is
13270 pretty bizarre. We use pulongest here because that's the established
13271 portability solution (e.g, we cannot use %u for uint32_t). */
13272 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13273 " TU version %s [in DWP file %s]"),
13274 pulongest (dwp_file->cus->version),
13275 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13276 }
13277
13278 if (dwp_file->cus)
13279 dwp_file->version = dwp_file->cus->version;
13280 else if (dwp_file->tus)
13281 dwp_file->version = dwp_file->tus->version;
13282 else
13283 dwp_file->version = 2;
13284
13285 if (dwp_file->version == 2)
13286 bfd_map_over_sections (dwp_file->dbfd.get (),
13287 dwarf2_locate_v2_dwp_sections,
13288 dwp_file.get ());
13289
13290 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13291 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13292
13293 if (dwarf_read_debug)
13294 {
13295 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13296 fprintf_unfiltered (gdb_stdlog,
13297 " %s CUs, %s TUs\n",
13298 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13299 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13300 }
13301
13302 return dwp_file;
13303 }
13304
13305 /* Wrapper around open_and_init_dwp_file, only open it once. */
13306
13307 static struct dwp_file *
13308 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13309 {
13310 if (! dwarf2_per_objfile->dwp_checked)
13311 {
13312 dwarf2_per_objfile->dwp_file
13313 = open_and_init_dwp_file (dwarf2_per_objfile);
13314 dwarf2_per_objfile->dwp_checked = 1;
13315 }
13316 return dwarf2_per_objfile->dwp_file.get ();
13317 }
13318
13319 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13320 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13321 or in the DWP file for the objfile, referenced by THIS_UNIT.
13322 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13323 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13324
13325 This is called, for example, when wanting to read a variable with a
13326 complex location. Therefore we don't want to do file i/o for every call.
13327 Therefore we don't want to look for a DWO file on every call.
13328 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13329 then we check if we've already seen DWO_NAME, and only THEN do we check
13330 for a DWO file.
13331
13332 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13333 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13334
13335 static struct dwo_unit *
13336 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13337 const char *dwo_name, const char *comp_dir,
13338 ULONGEST signature, int is_debug_types)
13339 {
13340 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13341 struct objfile *objfile = dwarf2_per_objfile->objfile;
13342 const char *kind = is_debug_types ? "TU" : "CU";
13343 void **dwo_file_slot;
13344 struct dwo_file *dwo_file;
13345 struct dwp_file *dwp_file;
13346
13347 /* First see if there's a DWP file.
13348 If we have a DWP file but didn't find the DWO inside it, don't
13349 look for the original DWO file. It makes gdb behave differently
13350 depending on whether one is debugging in the build tree. */
13351
13352 dwp_file = get_dwp_file (dwarf2_per_objfile);
13353 if (dwp_file != NULL)
13354 {
13355 const struct dwp_hash_table *dwp_htab =
13356 is_debug_types ? dwp_file->tus : dwp_file->cus;
13357
13358 if (dwp_htab != NULL)
13359 {
13360 struct dwo_unit *dwo_cutu =
13361 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13362 signature, is_debug_types);
13363
13364 if (dwo_cutu != NULL)
13365 {
13366 if (dwarf_read_debug)
13367 {
13368 fprintf_unfiltered (gdb_stdlog,
13369 "Virtual DWO %s %s found: @%s\n",
13370 kind, hex_string (signature),
13371 host_address_to_string (dwo_cutu));
13372 }
13373 return dwo_cutu;
13374 }
13375 }
13376 }
13377 else
13378 {
13379 /* No DWP file, look for the DWO file. */
13380
13381 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13382 dwo_name, comp_dir);
13383 if (*dwo_file_slot == NULL)
13384 {
13385 /* Read in the file and build a table of the CUs/TUs it contains. */
13386 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13387 }
13388 /* NOTE: This will be NULL if unable to open the file. */
13389 dwo_file = (struct dwo_file *) *dwo_file_slot;
13390
13391 if (dwo_file != NULL)
13392 {
13393 struct dwo_unit *dwo_cutu = NULL;
13394
13395 if (is_debug_types && dwo_file->tus)
13396 {
13397 struct dwo_unit find_dwo_cutu;
13398
13399 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13400 find_dwo_cutu.signature = signature;
13401 dwo_cutu
13402 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13403 }
13404 else if (!is_debug_types && dwo_file->cus)
13405 {
13406 struct dwo_unit find_dwo_cutu;
13407
13408 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13409 find_dwo_cutu.signature = signature;
13410 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13411 &find_dwo_cutu);
13412 }
13413
13414 if (dwo_cutu != NULL)
13415 {
13416 if (dwarf_read_debug)
13417 {
13418 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13419 kind, dwo_name, hex_string (signature),
13420 host_address_to_string (dwo_cutu));
13421 }
13422 return dwo_cutu;
13423 }
13424 }
13425 }
13426
13427 /* We didn't find it. This could mean a dwo_id mismatch, or
13428 someone deleted the DWO/DWP file, or the search path isn't set up
13429 correctly to find the file. */
13430
13431 if (dwarf_read_debug)
13432 {
13433 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13434 kind, dwo_name, hex_string (signature));
13435 }
13436
13437 /* This is a warning and not a complaint because it can be caused by
13438 pilot error (e.g., user accidentally deleting the DWO). */
13439 {
13440 /* Print the name of the DWP file if we looked there, helps the user
13441 better diagnose the problem. */
13442 std::string dwp_text;
13443
13444 if (dwp_file != NULL)
13445 dwp_text = string_printf (" [in DWP file %s]",
13446 lbasename (dwp_file->name));
13447
13448 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13449 " [in module %s]"),
13450 kind, dwo_name, hex_string (signature),
13451 dwp_text.c_str (),
13452 this_unit->is_debug_types ? "TU" : "CU",
13453 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13454 }
13455 return NULL;
13456 }
13457
13458 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13459 See lookup_dwo_cutu_unit for details. */
13460
13461 static struct dwo_unit *
13462 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13463 const char *dwo_name, const char *comp_dir,
13464 ULONGEST signature)
13465 {
13466 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13467 }
13468
13469 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13470 See lookup_dwo_cutu_unit for details. */
13471
13472 static struct dwo_unit *
13473 lookup_dwo_type_unit (struct signatured_type *this_tu,
13474 const char *dwo_name, const char *comp_dir)
13475 {
13476 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13477 }
13478
13479 /* Traversal function for queue_and_load_all_dwo_tus. */
13480
13481 static int
13482 queue_and_load_dwo_tu (void **slot, void *info)
13483 {
13484 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13485 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13486 ULONGEST signature = dwo_unit->signature;
13487 struct signatured_type *sig_type =
13488 lookup_dwo_signatured_type (per_cu->cu, signature);
13489
13490 if (sig_type != NULL)
13491 {
13492 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13493
13494 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13495 a real dependency of PER_CU on SIG_TYPE. That is detected later
13496 while processing PER_CU. */
13497 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13498 load_full_type_unit (sig_cu);
13499 per_cu->imported_symtabs_push (sig_cu);
13500 }
13501
13502 return 1;
13503 }
13504
13505 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13506 The DWO may have the only definition of the type, though it may not be
13507 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13508 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13509
13510 static void
13511 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13512 {
13513 struct dwo_unit *dwo_unit;
13514 struct dwo_file *dwo_file;
13515
13516 gdb_assert (!per_cu->is_debug_types);
13517 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13518 gdb_assert (per_cu->cu != NULL);
13519
13520 dwo_unit = per_cu->cu->dwo_unit;
13521 gdb_assert (dwo_unit != NULL);
13522
13523 dwo_file = dwo_unit->dwo_file;
13524 if (dwo_file->tus != NULL)
13525 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13526 }
13527
13528 /* Read in various DIEs. */
13529
13530 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13531 Inherit only the children of the DW_AT_abstract_origin DIE not being
13532 already referenced by DW_AT_abstract_origin from the children of the
13533 current DIE. */
13534
13535 static void
13536 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13537 {
13538 struct die_info *child_die;
13539 sect_offset *offsetp;
13540 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13541 struct die_info *origin_die;
13542 /* Iterator of the ORIGIN_DIE children. */
13543 struct die_info *origin_child_die;
13544 struct attribute *attr;
13545 struct dwarf2_cu *origin_cu;
13546 struct pending **origin_previous_list_in_scope;
13547
13548 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13549 if (!attr)
13550 return;
13551
13552 /* Note that following die references may follow to a die in a
13553 different cu. */
13554
13555 origin_cu = cu;
13556 origin_die = follow_die_ref (die, attr, &origin_cu);
13557
13558 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13559 symbols in. */
13560 origin_previous_list_in_scope = origin_cu->list_in_scope;
13561 origin_cu->list_in_scope = cu->list_in_scope;
13562
13563 if (die->tag != origin_die->tag
13564 && !(die->tag == DW_TAG_inlined_subroutine
13565 && origin_die->tag == DW_TAG_subprogram))
13566 complaint (_("DIE %s and its abstract origin %s have different tags"),
13567 sect_offset_str (die->sect_off),
13568 sect_offset_str (origin_die->sect_off));
13569
13570 std::vector<sect_offset> offsets;
13571
13572 for (child_die = die->child;
13573 child_die && child_die->tag;
13574 child_die = sibling_die (child_die))
13575 {
13576 struct die_info *child_origin_die;
13577 struct dwarf2_cu *child_origin_cu;
13578
13579 /* We are trying to process concrete instance entries:
13580 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13581 it's not relevant to our analysis here. i.e. detecting DIEs that are
13582 present in the abstract instance but not referenced in the concrete
13583 one. */
13584 if (child_die->tag == DW_TAG_call_site
13585 || child_die->tag == DW_TAG_GNU_call_site)
13586 continue;
13587
13588 /* For each CHILD_DIE, find the corresponding child of
13589 ORIGIN_DIE. If there is more than one layer of
13590 DW_AT_abstract_origin, follow them all; there shouldn't be,
13591 but GCC versions at least through 4.4 generate this (GCC PR
13592 40573). */
13593 child_origin_die = child_die;
13594 child_origin_cu = cu;
13595 while (1)
13596 {
13597 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13598 child_origin_cu);
13599 if (attr == NULL)
13600 break;
13601 child_origin_die = follow_die_ref (child_origin_die, attr,
13602 &child_origin_cu);
13603 }
13604
13605 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13606 counterpart may exist. */
13607 if (child_origin_die != child_die)
13608 {
13609 if (child_die->tag != child_origin_die->tag
13610 && !(child_die->tag == DW_TAG_inlined_subroutine
13611 && child_origin_die->tag == DW_TAG_subprogram))
13612 complaint (_("Child DIE %s and its abstract origin %s have "
13613 "different tags"),
13614 sect_offset_str (child_die->sect_off),
13615 sect_offset_str (child_origin_die->sect_off));
13616 if (child_origin_die->parent != origin_die)
13617 complaint (_("Child DIE %s and its abstract origin %s have "
13618 "different parents"),
13619 sect_offset_str (child_die->sect_off),
13620 sect_offset_str (child_origin_die->sect_off));
13621 else
13622 offsets.push_back (child_origin_die->sect_off);
13623 }
13624 }
13625 std::sort (offsets.begin (), offsets.end ());
13626 sect_offset *offsets_end = offsets.data () + offsets.size ();
13627 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13628 if (offsetp[-1] == *offsetp)
13629 complaint (_("Multiple children of DIE %s refer "
13630 "to DIE %s as their abstract origin"),
13631 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13632
13633 offsetp = offsets.data ();
13634 origin_child_die = origin_die->child;
13635 while (origin_child_die && origin_child_die->tag)
13636 {
13637 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13638 while (offsetp < offsets_end
13639 && *offsetp < origin_child_die->sect_off)
13640 offsetp++;
13641 if (offsetp >= offsets_end
13642 || *offsetp > origin_child_die->sect_off)
13643 {
13644 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13645 Check whether we're already processing ORIGIN_CHILD_DIE.
13646 This can happen with mutually referenced abstract_origins.
13647 PR 16581. */
13648 if (!origin_child_die->in_process)
13649 process_die (origin_child_die, origin_cu);
13650 }
13651 origin_child_die = sibling_die (origin_child_die);
13652 }
13653 origin_cu->list_in_scope = origin_previous_list_in_scope;
13654 }
13655
13656 static void
13657 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13658 {
13659 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13660 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13661 struct context_stack *newobj;
13662 CORE_ADDR lowpc;
13663 CORE_ADDR highpc;
13664 struct die_info *child_die;
13665 struct attribute *attr, *call_line, *call_file;
13666 const char *name;
13667 CORE_ADDR baseaddr;
13668 struct block *block;
13669 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13670 std::vector<struct symbol *> template_args;
13671 struct template_symbol *templ_func = NULL;
13672
13673 if (inlined_func)
13674 {
13675 /* If we do not have call site information, we can't show the
13676 caller of this inlined function. That's too confusing, so
13677 only use the scope for local variables. */
13678 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13679 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13680 if (call_line == NULL || call_file == NULL)
13681 {
13682 read_lexical_block_scope (die, cu);
13683 return;
13684 }
13685 }
13686
13687 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13688
13689 name = dwarf2_name (die, cu);
13690
13691 /* Ignore functions with missing or empty names. These are actually
13692 illegal according to the DWARF standard. */
13693 if (name == NULL)
13694 {
13695 complaint (_("missing name for subprogram DIE at %s"),
13696 sect_offset_str (die->sect_off));
13697 return;
13698 }
13699
13700 /* Ignore functions with missing or invalid low and high pc attributes. */
13701 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13702 <= PC_BOUNDS_INVALID)
13703 {
13704 attr = dwarf2_attr (die, DW_AT_external, cu);
13705 if (!attr || !DW_UNSND (attr))
13706 complaint (_("cannot get low and high bounds "
13707 "for subprogram DIE at %s"),
13708 sect_offset_str (die->sect_off));
13709 return;
13710 }
13711
13712 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13713 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13714
13715 /* If we have any template arguments, then we must allocate a
13716 different sort of symbol. */
13717 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13718 {
13719 if (child_die->tag == DW_TAG_template_type_param
13720 || child_die->tag == DW_TAG_template_value_param)
13721 {
13722 templ_func = allocate_template_symbol (objfile);
13723 templ_func->subclass = SYMBOL_TEMPLATE;
13724 break;
13725 }
13726 }
13727
13728 newobj = cu->get_builder ()->push_context (0, lowpc);
13729 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13730 (struct symbol *) templ_func);
13731
13732 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13733 set_objfile_main_name (objfile, SYMBOL_LINKAGE_NAME (newobj->name),
13734 cu->language);
13735
13736 /* If there is a location expression for DW_AT_frame_base, record
13737 it. */
13738 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13739 if (attr)
13740 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13741
13742 /* If there is a location for the static link, record it. */
13743 newobj->static_link = NULL;
13744 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13745 if (attr)
13746 {
13747 newobj->static_link
13748 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13749 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13750 dwarf2_per_cu_addr_type (cu->per_cu));
13751 }
13752
13753 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13754
13755 if (die->child != NULL)
13756 {
13757 child_die = die->child;
13758 while (child_die && child_die->tag)
13759 {
13760 if (child_die->tag == DW_TAG_template_type_param
13761 || child_die->tag == DW_TAG_template_value_param)
13762 {
13763 struct symbol *arg = new_symbol (child_die, NULL, cu);
13764
13765 if (arg != NULL)
13766 template_args.push_back (arg);
13767 }
13768 else
13769 process_die (child_die, cu);
13770 child_die = sibling_die (child_die);
13771 }
13772 }
13773
13774 inherit_abstract_dies (die, cu);
13775
13776 /* If we have a DW_AT_specification, we might need to import using
13777 directives from the context of the specification DIE. See the
13778 comment in determine_prefix. */
13779 if (cu->language == language_cplus
13780 && dwarf2_attr (die, DW_AT_specification, cu))
13781 {
13782 struct dwarf2_cu *spec_cu = cu;
13783 struct die_info *spec_die = die_specification (die, &spec_cu);
13784
13785 while (spec_die)
13786 {
13787 child_die = spec_die->child;
13788 while (child_die && child_die->tag)
13789 {
13790 if (child_die->tag == DW_TAG_imported_module)
13791 process_die (child_die, spec_cu);
13792 child_die = sibling_die (child_die);
13793 }
13794
13795 /* In some cases, GCC generates specification DIEs that
13796 themselves contain DW_AT_specification attributes. */
13797 spec_die = die_specification (spec_die, &spec_cu);
13798 }
13799 }
13800
13801 struct context_stack cstk = cu->get_builder ()->pop_context ();
13802 /* Make a block for the local symbols within. */
13803 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13804 cstk.static_link, lowpc, highpc);
13805
13806 /* For C++, set the block's scope. */
13807 if ((cu->language == language_cplus
13808 || cu->language == language_fortran
13809 || cu->language == language_d
13810 || cu->language == language_rust)
13811 && cu->processing_has_namespace_info)
13812 block_set_scope (block, determine_prefix (die, cu),
13813 &objfile->objfile_obstack);
13814
13815 /* If we have address ranges, record them. */
13816 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13817
13818 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13819
13820 /* Attach template arguments to function. */
13821 if (!template_args.empty ())
13822 {
13823 gdb_assert (templ_func != NULL);
13824
13825 templ_func->n_template_arguments = template_args.size ();
13826 templ_func->template_arguments
13827 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13828 templ_func->n_template_arguments);
13829 memcpy (templ_func->template_arguments,
13830 template_args.data (),
13831 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13832
13833 /* Make sure that the symtab is set on the new symbols. Even
13834 though they don't appear in this symtab directly, other parts
13835 of gdb assume that symbols do, and this is reasonably
13836 true. */
13837 for (symbol *sym : template_args)
13838 symbol_set_symtab (sym, symbol_symtab (templ_func));
13839 }
13840
13841 /* In C++, we can have functions nested inside functions (e.g., when
13842 a function declares a class that has methods). This means that
13843 when we finish processing a function scope, we may need to go
13844 back to building a containing block's symbol lists. */
13845 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13846 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13847
13848 /* If we've finished processing a top-level function, subsequent
13849 symbols go in the file symbol list. */
13850 if (cu->get_builder ()->outermost_context_p ())
13851 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13852 }
13853
13854 /* Process all the DIES contained within a lexical block scope. Start
13855 a new scope, process the dies, and then close the scope. */
13856
13857 static void
13858 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13859 {
13860 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13861 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13862 CORE_ADDR lowpc, highpc;
13863 struct die_info *child_die;
13864 CORE_ADDR baseaddr;
13865
13866 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13867
13868 /* Ignore blocks with missing or invalid low and high pc attributes. */
13869 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13870 as multiple lexical blocks? Handling children in a sane way would
13871 be nasty. Might be easier to properly extend generic blocks to
13872 describe ranges. */
13873 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13874 {
13875 case PC_BOUNDS_NOT_PRESENT:
13876 /* DW_TAG_lexical_block has no attributes, process its children as if
13877 there was no wrapping by that DW_TAG_lexical_block.
13878 GCC does no longer produces such DWARF since GCC r224161. */
13879 for (child_die = die->child;
13880 child_die != NULL && child_die->tag;
13881 child_die = sibling_die (child_die))
13882 process_die (child_die, cu);
13883 return;
13884 case PC_BOUNDS_INVALID:
13885 return;
13886 }
13887 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13888 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13889
13890 cu->get_builder ()->push_context (0, lowpc);
13891 if (die->child != NULL)
13892 {
13893 child_die = die->child;
13894 while (child_die && child_die->tag)
13895 {
13896 process_die (child_die, cu);
13897 child_die = sibling_die (child_die);
13898 }
13899 }
13900 inherit_abstract_dies (die, cu);
13901 struct context_stack cstk = cu->get_builder ()->pop_context ();
13902
13903 if (*cu->get_builder ()->get_local_symbols () != NULL
13904 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13905 {
13906 struct block *block
13907 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13908 cstk.start_addr, highpc);
13909
13910 /* Note that recording ranges after traversing children, as we
13911 do here, means that recording a parent's ranges entails
13912 walking across all its children's ranges as they appear in
13913 the address map, which is quadratic behavior.
13914
13915 It would be nicer to record the parent's ranges before
13916 traversing its children, simply overriding whatever you find
13917 there. But since we don't even decide whether to create a
13918 block until after we've traversed its children, that's hard
13919 to do. */
13920 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13921 }
13922 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13923 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13924 }
13925
13926 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13927
13928 static void
13929 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13930 {
13931 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13932 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13933 CORE_ADDR pc, baseaddr;
13934 struct attribute *attr;
13935 struct call_site *call_site, call_site_local;
13936 void **slot;
13937 int nparams;
13938 struct die_info *child_die;
13939
13940 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13941
13942 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13943 if (attr == NULL)
13944 {
13945 /* This was a pre-DWARF-5 GNU extension alias
13946 for DW_AT_call_return_pc. */
13947 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13948 }
13949 if (!attr)
13950 {
13951 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13952 "DIE %s [in module %s]"),
13953 sect_offset_str (die->sect_off), objfile_name (objfile));
13954 return;
13955 }
13956 pc = attr_value_as_address (attr) + baseaddr;
13957 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13958
13959 if (cu->call_site_htab == NULL)
13960 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13961 NULL, &objfile->objfile_obstack,
13962 hashtab_obstack_allocate, NULL);
13963 call_site_local.pc = pc;
13964 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13965 if (*slot != NULL)
13966 {
13967 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13968 "DIE %s [in module %s]"),
13969 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13970 objfile_name (objfile));
13971 return;
13972 }
13973
13974 /* Count parameters at the caller. */
13975
13976 nparams = 0;
13977 for (child_die = die->child; child_die && child_die->tag;
13978 child_die = sibling_die (child_die))
13979 {
13980 if (child_die->tag != DW_TAG_call_site_parameter
13981 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13982 {
13983 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13984 "DW_TAG_call_site child DIE %s [in module %s]"),
13985 child_die->tag, sect_offset_str (child_die->sect_off),
13986 objfile_name (objfile));
13987 continue;
13988 }
13989
13990 nparams++;
13991 }
13992
13993 call_site
13994 = ((struct call_site *)
13995 obstack_alloc (&objfile->objfile_obstack,
13996 sizeof (*call_site)
13997 + (sizeof (*call_site->parameter) * (nparams - 1))));
13998 *slot = call_site;
13999 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14000 call_site->pc = pc;
14001
14002 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14003 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14004 {
14005 struct die_info *func_die;
14006
14007 /* Skip also over DW_TAG_inlined_subroutine. */
14008 for (func_die = die->parent;
14009 func_die && func_die->tag != DW_TAG_subprogram
14010 && func_die->tag != DW_TAG_subroutine_type;
14011 func_die = func_die->parent);
14012
14013 /* DW_AT_call_all_calls is a superset
14014 of DW_AT_call_all_tail_calls. */
14015 if (func_die
14016 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14017 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14018 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14019 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14020 {
14021 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14022 not complete. But keep CALL_SITE for look ups via call_site_htab,
14023 both the initial caller containing the real return address PC and
14024 the final callee containing the current PC of a chain of tail
14025 calls do not need to have the tail call list complete. But any
14026 function candidate for a virtual tail call frame searched via
14027 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14028 determined unambiguously. */
14029 }
14030 else
14031 {
14032 struct type *func_type = NULL;
14033
14034 if (func_die)
14035 func_type = get_die_type (func_die, cu);
14036 if (func_type != NULL)
14037 {
14038 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14039
14040 /* Enlist this call site to the function. */
14041 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14042 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14043 }
14044 else
14045 complaint (_("Cannot find function owning DW_TAG_call_site "
14046 "DIE %s [in module %s]"),
14047 sect_offset_str (die->sect_off), objfile_name (objfile));
14048 }
14049 }
14050
14051 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14052 if (attr == NULL)
14053 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14054 if (attr == NULL)
14055 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14056 if (attr == NULL)
14057 {
14058 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14059 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14060 }
14061 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14062 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14063 /* Keep NULL DWARF_BLOCK. */;
14064 else if (attr_form_is_block (attr))
14065 {
14066 struct dwarf2_locexpr_baton *dlbaton;
14067
14068 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14069 dlbaton->data = DW_BLOCK (attr)->data;
14070 dlbaton->size = DW_BLOCK (attr)->size;
14071 dlbaton->per_cu = cu->per_cu;
14072
14073 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14074 }
14075 else if (attr_form_is_ref (attr))
14076 {
14077 struct dwarf2_cu *target_cu = cu;
14078 struct die_info *target_die;
14079
14080 target_die = follow_die_ref (die, attr, &target_cu);
14081 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14082 if (die_is_declaration (target_die, target_cu))
14083 {
14084 const char *target_physname;
14085
14086 /* Prefer the mangled name; otherwise compute the demangled one. */
14087 target_physname = dw2_linkage_name (target_die, target_cu);
14088 if (target_physname == NULL)
14089 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14090 if (target_physname == NULL)
14091 complaint (_("DW_AT_call_target target DIE has invalid "
14092 "physname, for referencing DIE %s [in module %s]"),
14093 sect_offset_str (die->sect_off), objfile_name (objfile));
14094 else
14095 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14096 }
14097 else
14098 {
14099 CORE_ADDR lowpc;
14100
14101 /* DW_AT_entry_pc should be preferred. */
14102 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14103 <= PC_BOUNDS_INVALID)
14104 complaint (_("DW_AT_call_target target DIE has invalid "
14105 "low pc, for referencing DIE %s [in module %s]"),
14106 sect_offset_str (die->sect_off), objfile_name (objfile));
14107 else
14108 {
14109 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14110 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14111 }
14112 }
14113 }
14114 else
14115 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14116 "block nor reference, for DIE %s [in module %s]"),
14117 sect_offset_str (die->sect_off), objfile_name (objfile));
14118
14119 call_site->per_cu = cu->per_cu;
14120
14121 for (child_die = die->child;
14122 child_die && child_die->tag;
14123 child_die = sibling_die (child_die))
14124 {
14125 struct call_site_parameter *parameter;
14126 struct attribute *loc, *origin;
14127
14128 if (child_die->tag != DW_TAG_call_site_parameter
14129 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14130 {
14131 /* Already printed the complaint above. */
14132 continue;
14133 }
14134
14135 gdb_assert (call_site->parameter_count < nparams);
14136 parameter = &call_site->parameter[call_site->parameter_count];
14137
14138 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14139 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14140 register is contained in DW_AT_call_value. */
14141
14142 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14143 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14144 if (origin == NULL)
14145 {
14146 /* This was a pre-DWARF-5 GNU extension alias
14147 for DW_AT_call_parameter. */
14148 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14149 }
14150 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14151 {
14152 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14153
14154 sect_offset sect_off
14155 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14156 if (!offset_in_cu_p (&cu->header, sect_off))
14157 {
14158 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14159 binding can be done only inside one CU. Such referenced DIE
14160 therefore cannot be even moved to DW_TAG_partial_unit. */
14161 complaint (_("DW_AT_call_parameter offset is not in CU for "
14162 "DW_TAG_call_site child DIE %s [in module %s]"),
14163 sect_offset_str (child_die->sect_off),
14164 objfile_name (objfile));
14165 continue;
14166 }
14167 parameter->u.param_cu_off
14168 = (cu_offset) (sect_off - cu->header.sect_off);
14169 }
14170 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14171 {
14172 complaint (_("No DW_FORM_block* DW_AT_location for "
14173 "DW_TAG_call_site child DIE %s [in module %s]"),
14174 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14175 continue;
14176 }
14177 else
14178 {
14179 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14180 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14181 if (parameter->u.dwarf_reg != -1)
14182 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14183 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14184 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14185 &parameter->u.fb_offset))
14186 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14187 else
14188 {
14189 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14190 "for DW_FORM_block* DW_AT_location is supported for "
14191 "DW_TAG_call_site child DIE %s "
14192 "[in module %s]"),
14193 sect_offset_str (child_die->sect_off),
14194 objfile_name (objfile));
14195 continue;
14196 }
14197 }
14198
14199 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14200 if (attr == NULL)
14201 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14202 if (!attr_form_is_block (attr))
14203 {
14204 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14205 "DW_TAG_call_site child DIE %s [in module %s]"),
14206 sect_offset_str (child_die->sect_off),
14207 objfile_name (objfile));
14208 continue;
14209 }
14210 parameter->value = DW_BLOCK (attr)->data;
14211 parameter->value_size = DW_BLOCK (attr)->size;
14212
14213 /* Parameters are not pre-cleared by memset above. */
14214 parameter->data_value = NULL;
14215 parameter->data_value_size = 0;
14216 call_site->parameter_count++;
14217
14218 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14219 if (attr == NULL)
14220 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14221 if (attr)
14222 {
14223 if (!attr_form_is_block (attr))
14224 complaint (_("No DW_FORM_block* DW_AT_call_data_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 else
14229 {
14230 parameter->data_value = DW_BLOCK (attr)->data;
14231 parameter->data_value_size = DW_BLOCK (attr)->size;
14232 }
14233 }
14234 }
14235 }
14236
14237 /* Helper function for read_variable. If DIE represents a virtual
14238 table, then return the type of the concrete object that is
14239 associated with the virtual table. Otherwise, return NULL. */
14240
14241 static struct type *
14242 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14243 {
14244 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14245 if (attr == NULL)
14246 return NULL;
14247
14248 /* Find the type DIE. */
14249 struct die_info *type_die = NULL;
14250 struct dwarf2_cu *type_cu = cu;
14251
14252 if (attr_form_is_ref (attr))
14253 type_die = follow_die_ref (die, attr, &type_cu);
14254 if (type_die == NULL)
14255 return NULL;
14256
14257 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14258 return NULL;
14259 return die_containing_type (type_die, type_cu);
14260 }
14261
14262 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14263
14264 static void
14265 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14266 {
14267 struct rust_vtable_symbol *storage = NULL;
14268
14269 if (cu->language == language_rust)
14270 {
14271 struct type *containing_type = rust_containing_type (die, cu);
14272
14273 if (containing_type != NULL)
14274 {
14275 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14276
14277 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14278 struct rust_vtable_symbol);
14279 initialize_objfile_symbol (storage);
14280 storage->concrete_type = containing_type;
14281 storage->subclass = SYMBOL_RUST_VTABLE;
14282 }
14283 }
14284
14285 struct symbol *res = new_symbol (die, NULL, cu, storage);
14286 struct attribute *abstract_origin
14287 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14288 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14289 if (res == NULL && loc && abstract_origin)
14290 {
14291 /* We have a variable without a name, but with a location and an abstract
14292 origin. This may be a concrete instance of an abstract variable
14293 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14294 later. */
14295 struct dwarf2_cu *origin_cu = cu;
14296 struct die_info *origin_die
14297 = follow_die_ref (die, abstract_origin, &origin_cu);
14298 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
14299 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
14300 }
14301 }
14302
14303 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14304 reading .debug_rnglists.
14305 Callback's type should be:
14306 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14307 Return true if the attributes are present and valid, otherwise,
14308 return false. */
14309
14310 template <typename Callback>
14311 static bool
14312 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14313 Callback &&callback)
14314 {
14315 struct dwarf2_per_objfile *dwarf2_per_objfile
14316 = cu->per_cu->dwarf2_per_objfile;
14317 struct objfile *objfile = dwarf2_per_objfile->objfile;
14318 bfd *obfd = objfile->obfd;
14319 /* Base address selection entry. */
14320 CORE_ADDR base;
14321 int found_base;
14322 const gdb_byte *buffer;
14323 CORE_ADDR baseaddr;
14324 bool overflow = false;
14325
14326 found_base = cu->base_known;
14327 base = cu->base_address;
14328
14329 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14330 if (offset >= dwarf2_per_objfile->rnglists.size)
14331 {
14332 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14333 offset);
14334 return false;
14335 }
14336 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14337
14338 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14339
14340 while (1)
14341 {
14342 /* Initialize it due to a false compiler warning. */
14343 CORE_ADDR range_beginning = 0, range_end = 0;
14344 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14345 + dwarf2_per_objfile->rnglists.size);
14346 unsigned int bytes_read;
14347
14348 if (buffer == buf_end)
14349 {
14350 overflow = true;
14351 break;
14352 }
14353 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14354 switch (rlet)
14355 {
14356 case DW_RLE_end_of_list:
14357 break;
14358 case DW_RLE_base_address:
14359 if (buffer + cu->header.addr_size > buf_end)
14360 {
14361 overflow = true;
14362 break;
14363 }
14364 base = read_address (obfd, buffer, cu, &bytes_read);
14365 found_base = 1;
14366 buffer += bytes_read;
14367 break;
14368 case DW_RLE_start_length:
14369 if (buffer + cu->header.addr_size > buf_end)
14370 {
14371 overflow = true;
14372 break;
14373 }
14374 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14375 buffer += bytes_read;
14376 range_end = (range_beginning
14377 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14378 buffer += bytes_read;
14379 if (buffer > buf_end)
14380 {
14381 overflow = true;
14382 break;
14383 }
14384 break;
14385 case DW_RLE_offset_pair:
14386 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14387 buffer += bytes_read;
14388 if (buffer > buf_end)
14389 {
14390 overflow = true;
14391 break;
14392 }
14393 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14394 buffer += bytes_read;
14395 if (buffer > buf_end)
14396 {
14397 overflow = true;
14398 break;
14399 }
14400 break;
14401 case DW_RLE_start_end:
14402 if (buffer + 2 * cu->header.addr_size > buf_end)
14403 {
14404 overflow = true;
14405 break;
14406 }
14407 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14408 buffer += bytes_read;
14409 range_end = read_address (obfd, buffer, cu, &bytes_read);
14410 buffer += bytes_read;
14411 break;
14412 default:
14413 complaint (_("Invalid .debug_rnglists data (no base address)"));
14414 return false;
14415 }
14416 if (rlet == DW_RLE_end_of_list || overflow)
14417 break;
14418 if (rlet == DW_RLE_base_address)
14419 continue;
14420
14421 if (!found_base)
14422 {
14423 /* We have no valid base address for the ranges
14424 data. */
14425 complaint (_("Invalid .debug_rnglists data (no base address)"));
14426 return false;
14427 }
14428
14429 if (range_beginning > range_end)
14430 {
14431 /* Inverted range entries are invalid. */
14432 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14433 return false;
14434 }
14435
14436 /* Empty range entries have no effect. */
14437 if (range_beginning == range_end)
14438 continue;
14439
14440 range_beginning += base;
14441 range_end += base;
14442
14443 /* A not-uncommon case of bad debug info.
14444 Don't pollute the addrmap with bad data. */
14445 if (range_beginning + baseaddr == 0
14446 && !dwarf2_per_objfile->has_section_at_zero)
14447 {
14448 complaint (_(".debug_rnglists entry has start address of zero"
14449 " [in module %s]"), objfile_name (objfile));
14450 continue;
14451 }
14452
14453 callback (range_beginning, range_end);
14454 }
14455
14456 if (overflow)
14457 {
14458 complaint (_("Offset %d is not terminated "
14459 "for DW_AT_ranges attribute"),
14460 offset);
14461 return false;
14462 }
14463
14464 return true;
14465 }
14466
14467 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14468 Callback's type should be:
14469 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14470 Return 1 if the attributes are present and valid, otherwise, return 0. */
14471
14472 template <typename Callback>
14473 static int
14474 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14475 Callback &&callback)
14476 {
14477 struct dwarf2_per_objfile *dwarf2_per_objfile
14478 = cu->per_cu->dwarf2_per_objfile;
14479 struct objfile *objfile = dwarf2_per_objfile->objfile;
14480 struct comp_unit_head *cu_header = &cu->header;
14481 bfd *obfd = objfile->obfd;
14482 unsigned int addr_size = cu_header->addr_size;
14483 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14484 /* Base address selection entry. */
14485 CORE_ADDR base;
14486 int found_base;
14487 unsigned int dummy;
14488 const gdb_byte *buffer;
14489 CORE_ADDR baseaddr;
14490
14491 if (cu_header->version >= 5)
14492 return dwarf2_rnglists_process (offset, cu, callback);
14493
14494 found_base = cu->base_known;
14495 base = cu->base_address;
14496
14497 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14498 if (offset >= dwarf2_per_objfile->ranges.size)
14499 {
14500 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14501 offset);
14502 return 0;
14503 }
14504 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14505
14506 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14507
14508 while (1)
14509 {
14510 CORE_ADDR range_beginning, range_end;
14511
14512 range_beginning = read_address (obfd, buffer, cu, &dummy);
14513 buffer += addr_size;
14514 range_end = read_address (obfd, buffer, cu, &dummy);
14515 buffer += addr_size;
14516 offset += 2 * addr_size;
14517
14518 /* An end of list marker is a pair of zero addresses. */
14519 if (range_beginning == 0 && range_end == 0)
14520 /* Found the end of list entry. */
14521 break;
14522
14523 /* Each base address selection entry is a pair of 2 values.
14524 The first is the largest possible address, the second is
14525 the base address. Check for a base address here. */
14526 if ((range_beginning & mask) == mask)
14527 {
14528 /* If we found the largest possible address, then we already
14529 have the base address in range_end. */
14530 base = range_end;
14531 found_base = 1;
14532 continue;
14533 }
14534
14535 if (!found_base)
14536 {
14537 /* We have no valid base address for the ranges
14538 data. */
14539 complaint (_("Invalid .debug_ranges data (no base address)"));
14540 return 0;
14541 }
14542
14543 if (range_beginning > range_end)
14544 {
14545 /* Inverted range entries are invalid. */
14546 complaint (_("Invalid .debug_ranges data (inverted range)"));
14547 return 0;
14548 }
14549
14550 /* Empty range entries have no effect. */
14551 if (range_beginning == range_end)
14552 continue;
14553
14554 range_beginning += base;
14555 range_end += base;
14556
14557 /* A not-uncommon case of bad debug info.
14558 Don't pollute the addrmap with bad data. */
14559 if (range_beginning + baseaddr == 0
14560 && !dwarf2_per_objfile->has_section_at_zero)
14561 {
14562 complaint (_(".debug_ranges entry has start address of zero"
14563 " [in module %s]"), objfile_name (objfile));
14564 continue;
14565 }
14566
14567 callback (range_beginning, range_end);
14568 }
14569
14570 return 1;
14571 }
14572
14573 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14574 Return 1 if the attributes are present and valid, otherwise, return 0.
14575 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14576
14577 static int
14578 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14579 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14580 struct partial_symtab *ranges_pst)
14581 {
14582 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14583 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14584 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14585 SECT_OFF_TEXT (objfile));
14586 int low_set = 0;
14587 CORE_ADDR low = 0;
14588 CORE_ADDR high = 0;
14589 int retval;
14590
14591 retval = dwarf2_ranges_process (offset, cu,
14592 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14593 {
14594 if (ranges_pst != NULL)
14595 {
14596 CORE_ADDR lowpc;
14597 CORE_ADDR highpc;
14598
14599 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14600 range_beginning + baseaddr)
14601 - baseaddr);
14602 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14603 range_end + baseaddr)
14604 - baseaddr);
14605 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14606 lowpc, highpc - 1, ranges_pst);
14607 }
14608
14609 /* FIXME: This is recording everything as a low-high
14610 segment of consecutive addresses. We should have a
14611 data structure for discontiguous block ranges
14612 instead. */
14613 if (! low_set)
14614 {
14615 low = range_beginning;
14616 high = range_end;
14617 low_set = 1;
14618 }
14619 else
14620 {
14621 if (range_beginning < low)
14622 low = range_beginning;
14623 if (range_end > high)
14624 high = range_end;
14625 }
14626 });
14627 if (!retval)
14628 return 0;
14629
14630 if (! low_set)
14631 /* If the first entry is an end-of-list marker, the range
14632 describes an empty scope, i.e. no instructions. */
14633 return 0;
14634
14635 if (low_return)
14636 *low_return = low;
14637 if (high_return)
14638 *high_return = high;
14639 return 1;
14640 }
14641
14642 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14643 definition for the return value. *LOWPC and *HIGHPC are set iff
14644 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14645
14646 static enum pc_bounds_kind
14647 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14648 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14649 struct partial_symtab *pst)
14650 {
14651 struct dwarf2_per_objfile *dwarf2_per_objfile
14652 = cu->per_cu->dwarf2_per_objfile;
14653 struct attribute *attr;
14654 struct attribute *attr_high;
14655 CORE_ADDR low = 0;
14656 CORE_ADDR high = 0;
14657 enum pc_bounds_kind ret;
14658
14659 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14660 if (attr_high)
14661 {
14662 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14663 if (attr)
14664 {
14665 low = attr_value_as_address (attr);
14666 high = attr_value_as_address (attr_high);
14667 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14668 high += low;
14669 }
14670 else
14671 /* Found high w/o low attribute. */
14672 return PC_BOUNDS_INVALID;
14673
14674 /* Found consecutive range of addresses. */
14675 ret = PC_BOUNDS_HIGH_LOW;
14676 }
14677 else
14678 {
14679 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14680 if (attr != NULL)
14681 {
14682 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14683 We take advantage of the fact that DW_AT_ranges does not appear
14684 in DW_TAG_compile_unit of DWO files. */
14685 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14686 unsigned int ranges_offset = (DW_UNSND (attr)
14687 + (need_ranges_base
14688 ? cu->ranges_base
14689 : 0));
14690
14691 /* Value of the DW_AT_ranges attribute is the offset in the
14692 .debug_ranges section. */
14693 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14694 return PC_BOUNDS_INVALID;
14695 /* Found discontinuous range of addresses. */
14696 ret = PC_BOUNDS_RANGES;
14697 }
14698 else
14699 return PC_BOUNDS_NOT_PRESENT;
14700 }
14701
14702 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14703 if (high <= low)
14704 return PC_BOUNDS_INVALID;
14705
14706 /* When using the GNU linker, .gnu.linkonce. sections are used to
14707 eliminate duplicate copies of functions and vtables and such.
14708 The linker will arbitrarily choose one and discard the others.
14709 The AT_*_pc values for such functions refer to local labels in
14710 these sections. If the section from that file was discarded, the
14711 labels are not in the output, so the relocs get a value of 0.
14712 If this is a discarded function, mark the pc bounds as invalid,
14713 so that GDB will ignore it. */
14714 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14715 return PC_BOUNDS_INVALID;
14716
14717 *lowpc = low;
14718 if (highpc)
14719 *highpc = high;
14720 return ret;
14721 }
14722
14723 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14724 its low and high PC addresses. Do nothing if these addresses could not
14725 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14726 and HIGHPC to the high address if greater than HIGHPC. */
14727
14728 static void
14729 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14730 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14731 struct dwarf2_cu *cu)
14732 {
14733 CORE_ADDR low, high;
14734 struct die_info *child = die->child;
14735
14736 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14737 {
14738 *lowpc = std::min (*lowpc, low);
14739 *highpc = std::max (*highpc, high);
14740 }
14741
14742 /* If the language does not allow nested subprograms (either inside
14743 subprograms or lexical blocks), we're done. */
14744 if (cu->language != language_ada)
14745 return;
14746
14747 /* Check all the children of the given DIE. If it contains nested
14748 subprograms, then check their pc bounds. Likewise, we need to
14749 check lexical blocks as well, as they may also contain subprogram
14750 definitions. */
14751 while (child && child->tag)
14752 {
14753 if (child->tag == DW_TAG_subprogram
14754 || child->tag == DW_TAG_lexical_block)
14755 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14756 child = sibling_die (child);
14757 }
14758 }
14759
14760 /* Get the low and high pc's represented by the scope DIE, and store
14761 them in *LOWPC and *HIGHPC. If the correct values can't be
14762 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14763
14764 static void
14765 get_scope_pc_bounds (struct die_info *die,
14766 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14767 struct dwarf2_cu *cu)
14768 {
14769 CORE_ADDR best_low = (CORE_ADDR) -1;
14770 CORE_ADDR best_high = (CORE_ADDR) 0;
14771 CORE_ADDR current_low, current_high;
14772
14773 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14774 >= PC_BOUNDS_RANGES)
14775 {
14776 best_low = current_low;
14777 best_high = current_high;
14778 }
14779 else
14780 {
14781 struct die_info *child = die->child;
14782
14783 while (child && child->tag)
14784 {
14785 switch (child->tag) {
14786 case DW_TAG_subprogram:
14787 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14788 break;
14789 case DW_TAG_namespace:
14790 case DW_TAG_module:
14791 /* FIXME: carlton/2004-01-16: Should we do this for
14792 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14793 that current GCC's always emit the DIEs corresponding
14794 to definitions of methods of classes as children of a
14795 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14796 the DIEs giving the declarations, which could be
14797 anywhere). But I don't see any reason why the
14798 standards says that they have to be there. */
14799 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14800
14801 if (current_low != ((CORE_ADDR) -1))
14802 {
14803 best_low = std::min (best_low, current_low);
14804 best_high = std::max (best_high, current_high);
14805 }
14806 break;
14807 default:
14808 /* Ignore. */
14809 break;
14810 }
14811
14812 child = sibling_die (child);
14813 }
14814 }
14815
14816 *lowpc = best_low;
14817 *highpc = best_high;
14818 }
14819
14820 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14821 in DIE. */
14822
14823 static void
14824 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14825 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14826 {
14827 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14828 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14829 struct attribute *attr;
14830 struct attribute *attr_high;
14831
14832 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14833 if (attr_high)
14834 {
14835 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14836 if (attr)
14837 {
14838 CORE_ADDR low = attr_value_as_address (attr);
14839 CORE_ADDR high = attr_value_as_address (attr_high);
14840
14841 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14842 high += low;
14843
14844 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14845 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14846 cu->get_builder ()->record_block_range (block, low, high - 1);
14847 }
14848 }
14849
14850 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14851 if (attr)
14852 {
14853 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14854 We take advantage of the fact that DW_AT_ranges does not appear
14855 in DW_TAG_compile_unit of DWO files. */
14856 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14857
14858 /* The value of the DW_AT_ranges attribute is the offset of the
14859 address range list in the .debug_ranges section. */
14860 unsigned long offset = (DW_UNSND (attr)
14861 + (need_ranges_base ? cu->ranges_base : 0));
14862
14863 std::vector<blockrange> blockvec;
14864 dwarf2_ranges_process (offset, cu,
14865 [&] (CORE_ADDR start, CORE_ADDR end)
14866 {
14867 start += baseaddr;
14868 end += baseaddr;
14869 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14870 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14871 cu->get_builder ()->record_block_range (block, start, end - 1);
14872 blockvec.emplace_back (start, end);
14873 });
14874
14875 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14876 }
14877 }
14878
14879 /* Check whether the producer field indicates either of GCC < 4.6, or the
14880 Intel C/C++ compiler, and cache the result in CU. */
14881
14882 static void
14883 check_producer (struct dwarf2_cu *cu)
14884 {
14885 int major, minor;
14886
14887 if (cu->producer == NULL)
14888 {
14889 /* For unknown compilers expect their behavior is DWARF version
14890 compliant.
14891
14892 GCC started to support .debug_types sections by -gdwarf-4 since
14893 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14894 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14895 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14896 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14897 }
14898 else if (producer_is_gcc (cu->producer, &major, &minor))
14899 {
14900 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14901 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14902 }
14903 else if (producer_is_icc (cu->producer, &major, &minor))
14904 {
14905 cu->producer_is_icc = true;
14906 cu->producer_is_icc_lt_14 = major < 14;
14907 }
14908 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14909 cu->producer_is_codewarrior = true;
14910 else
14911 {
14912 /* For other non-GCC compilers, expect their behavior is DWARF version
14913 compliant. */
14914 }
14915
14916 cu->checked_producer = true;
14917 }
14918
14919 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14920 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14921 during 4.6.0 experimental. */
14922
14923 static bool
14924 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14925 {
14926 if (!cu->checked_producer)
14927 check_producer (cu);
14928
14929 return cu->producer_is_gxx_lt_4_6;
14930 }
14931
14932
14933 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14934 with incorrect is_stmt attributes. */
14935
14936 static bool
14937 producer_is_codewarrior (struct dwarf2_cu *cu)
14938 {
14939 if (!cu->checked_producer)
14940 check_producer (cu);
14941
14942 return cu->producer_is_codewarrior;
14943 }
14944
14945 /* Return the default accessibility type if it is not overridden by
14946 DW_AT_accessibility. */
14947
14948 static enum dwarf_access_attribute
14949 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14950 {
14951 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14952 {
14953 /* The default DWARF 2 accessibility for members is public, the default
14954 accessibility for inheritance is private. */
14955
14956 if (die->tag != DW_TAG_inheritance)
14957 return DW_ACCESS_public;
14958 else
14959 return DW_ACCESS_private;
14960 }
14961 else
14962 {
14963 /* DWARF 3+ defines the default accessibility a different way. The same
14964 rules apply now for DW_TAG_inheritance as for the members and it only
14965 depends on the container kind. */
14966
14967 if (die->parent->tag == DW_TAG_class_type)
14968 return DW_ACCESS_private;
14969 else
14970 return DW_ACCESS_public;
14971 }
14972 }
14973
14974 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14975 offset. If the attribute was not found return 0, otherwise return
14976 1. If it was found but could not properly be handled, set *OFFSET
14977 to 0. */
14978
14979 static int
14980 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14981 LONGEST *offset)
14982 {
14983 struct attribute *attr;
14984
14985 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14986 if (attr != NULL)
14987 {
14988 *offset = 0;
14989
14990 /* Note that we do not check for a section offset first here.
14991 This is because DW_AT_data_member_location is new in DWARF 4,
14992 so if we see it, we can assume that a constant form is really
14993 a constant and not a section offset. */
14994 if (attr_form_is_constant (attr))
14995 *offset = dwarf2_get_attr_constant_value (attr, 0);
14996 else if (attr_form_is_section_offset (attr))
14997 dwarf2_complex_location_expr_complaint ();
14998 else if (attr_form_is_block (attr))
14999 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15000 else
15001 dwarf2_complex_location_expr_complaint ();
15002
15003 return 1;
15004 }
15005
15006 return 0;
15007 }
15008
15009 /* Add an aggregate field to the field list. */
15010
15011 static void
15012 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15013 struct dwarf2_cu *cu)
15014 {
15015 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15016 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15017 struct nextfield *new_field;
15018 struct attribute *attr;
15019 struct field *fp;
15020 const char *fieldname = "";
15021
15022 if (die->tag == DW_TAG_inheritance)
15023 {
15024 fip->baseclasses.emplace_back ();
15025 new_field = &fip->baseclasses.back ();
15026 }
15027 else
15028 {
15029 fip->fields.emplace_back ();
15030 new_field = &fip->fields.back ();
15031 }
15032
15033 fip->nfields++;
15034
15035 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15036 if (attr)
15037 new_field->accessibility = DW_UNSND (attr);
15038 else
15039 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15040 if (new_field->accessibility != DW_ACCESS_public)
15041 fip->non_public_fields = 1;
15042
15043 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15044 if (attr)
15045 new_field->virtuality = DW_UNSND (attr);
15046 else
15047 new_field->virtuality = DW_VIRTUALITY_none;
15048
15049 fp = &new_field->field;
15050
15051 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15052 {
15053 LONGEST offset;
15054
15055 /* Data member other than a C++ static data member. */
15056
15057 /* Get type of field. */
15058 fp->type = die_type (die, cu);
15059
15060 SET_FIELD_BITPOS (*fp, 0);
15061
15062 /* Get bit size of field (zero if none). */
15063 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15064 if (attr)
15065 {
15066 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15067 }
15068 else
15069 {
15070 FIELD_BITSIZE (*fp) = 0;
15071 }
15072
15073 /* Get bit offset of field. */
15074 if (handle_data_member_location (die, cu, &offset))
15075 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15076 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15077 if (attr)
15078 {
15079 if (gdbarch_bits_big_endian (gdbarch))
15080 {
15081 /* For big endian bits, the DW_AT_bit_offset gives the
15082 additional bit offset from the MSB of the containing
15083 anonymous object to the MSB of the field. We don't
15084 have to do anything special since we don't need to
15085 know the size of the anonymous object. */
15086 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15087 }
15088 else
15089 {
15090 /* For little endian bits, compute the bit offset to the
15091 MSB of the anonymous object, subtract off the number of
15092 bits from the MSB of the field to the MSB of the
15093 object, and then subtract off the number of bits of
15094 the field itself. The result is the bit offset of
15095 the LSB of the field. */
15096 int anonymous_size;
15097 int bit_offset = DW_UNSND (attr);
15098
15099 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15100 if (attr)
15101 {
15102 /* The size of the anonymous object containing
15103 the bit field is explicit, so use the
15104 indicated size (in bytes). */
15105 anonymous_size = DW_UNSND (attr);
15106 }
15107 else
15108 {
15109 /* The size of the anonymous object containing
15110 the bit field must be inferred from the type
15111 attribute of the data member containing the
15112 bit field. */
15113 anonymous_size = TYPE_LENGTH (fp->type);
15114 }
15115 SET_FIELD_BITPOS (*fp,
15116 (FIELD_BITPOS (*fp)
15117 + anonymous_size * bits_per_byte
15118 - bit_offset - FIELD_BITSIZE (*fp)));
15119 }
15120 }
15121 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15122 if (attr != NULL)
15123 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15124 + dwarf2_get_attr_constant_value (attr, 0)));
15125
15126 /* Get name of field. */
15127 fieldname = dwarf2_name (die, cu);
15128 if (fieldname == NULL)
15129 fieldname = "";
15130
15131 /* The name is already allocated along with this objfile, so we don't
15132 need to duplicate it for the type. */
15133 fp->name = fieldname;
15134
15135 /* Change accessibility for artificial fields (e.g. virtual table
15136 pointer or virtual base class pointer) to private. */
15137 if (dwarf2_attr (die, DW_AT_artificial, cu))
15138 {
15139 FIELD_ARTIFICIAL (*fp) = 1;
15140 new_field->accessibility = DW_ACCESS_private;
15141 fip->non_public_fields = 1;
15142 }
15143 }
15144 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15145 {
15146 /* C++ static member. */
15147
15148 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15149 is a declaration, but all versions of G++ as of this writing
15150 (so through at least 3.2.1) incorrectly generate
15151 DW_TAG_variable tags. */
15152
15153 const char *physname;
15154
15155 /* Get name of field. */
15156 fieldname = dwarf2_name (die, cu);
15157 if (fieldname == NULL)
15158 return;
15159
15160 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15161 if (attr
15162 /* Only create a symbol if this is an external value.
15163 new_symbol checks this and puts the value in the global symbol
15164 table, which we want. If it is not external, new_symbol
15165 will try to put the value in cu->list_in_scope which is wrong. */
15166 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15167 {
15168 /* A static const member, not much different than an enum as far as
15169 we're concerned, except that we can support more types. */
15170 new_symbol (die, NULL, cu);
15171 }
15172
15173 /* Get physical name. */
15174 physname = dwarf2_physname (fieldname, die, cu);
15175
15176 /* The name is already allocated along with this objfile, so we don't
15177 need to duplicate it for the type. */
15178 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15179 FIELD_TYPE (*fp) = die_type (die, cu);
15180 FIELD_NAME (*fp) = fieldname;
15181 }
15182 else if (die->tag == DW_TAG_inheritance)
15183 {
15184 LONGEST offset;
15185
15186 /* C++ base class field. */
15187 if (handle_data_member_location (die, cu, &offset))
15188 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15189 FIELD_BITSIZE (*fp) = 0;
15190 FIELD_TYPE (*fp) = die_type (die, cu);
15191 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
15192 }
15193 else if (die->tag == DW_TAG_variant_part)
15194 {
15195 /* process_structure_scope will treat this DIE as a union. */
15196 process_structure_scope (die, cu);
15197
15198 /* The variant part is relative to the start of the enclosing
15199 structure. */
15200 SET_FIELD_BITPOS (*fp, 0);
15201 fp->type = get_die_type (die, cu);
15202 fp->artificial = 1;
15203 fp->name = "<<variant>>";
15204
15205 /* Normally a DW_TAG_variant_part won't have a size, but our
15206 representation requires one, so set it to the maximum of the
15207 child sizes. */
15208 if (TYPE_LENGTH (fp->type) == 0)
15209 {
15210 unsigned max = 0;
15211 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
15212 if (TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)) > max)
15213 max = TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i));
15214 TYPE_LENGTH (fp->type) = max;
15215 }
15216 }
15217 else
15218 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15219 }
15220
15221 /* Can the type given by DIE define another type? */
15222
15223 static bool
15224 type_can_define_types (const struct die_info *die)
15225 {
15226 switch (die->tag)
15227 {
15228 case DW_TAG_typedef:
15229 case DW_TAG_class_type:
15230 case DW_TAG_structure_type:
15231 case DW_TAG_union_type:
15232 case DW_TAG_enumeration_type:
15233 return true;
15234
15235 default:
15236 return false;
15237 }
15238 }
15239
15240 /* Add a type definition defined in the scope of the FIP's class. */
15241
15242 static void
15243 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15244 struct dwarf2_cu *cu)
15245 {
15246 struct decl_field fp;
15247 memset (&fp, 0, sizeof (fp));
15248
15249 gdb_assert (type_can_define_types (die));
15250
15251 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15252 fp.name = dwarf2_name (die, cu);
15253 fp.type = read_type_die (die, cu);
15254
15255 /* Save accessibility. */
15256 enum dwarf_access_attribute accessibility;
15257 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15258 if (attr != NULL)
15259 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15260 else
15261 accessibility = dwarf2_default_access_attribute (die, cu);
15262 switch (accessibility)
15263 {
15264 case DW_ACCESS_public:
15265 /* The assumed value if neither private nor protected. */
15266 break;
15267 case DW_ACCESS_private:
15268 fp.is_private = 1;
15269 break;
15270 case DW_ACCESS_protected:
15271 fp.is_protected = 1;
15272 break;
15273 default:
15274 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15275 }
15276
15277 if (die->tag == DW_TAG_typedef)
15278 fip->typedef_field_list.push_back (fp);
15279 else
15280 fip->nested_types_list.push_back (fp);
15281 }
15282
15283 /* Create the vector of fields, and attach it to the type. */
15284
15285 static void
15286 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15287 struct dwarf2_cu *cu)
15288 {
15289 int nfields = fip->nfields;
15290
15291 /* Record the field count, allocate space for the array of fields,
15292 and create blank accessibility bitfields if necessary. */
15293 TYPE_NFIELDS (type) = nfields;
15294 TYPE_FIELDS (type) = (struct field *)
15295 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15296
15297 if (fip->non_public_fields && cu->language != language_ada)
15298 {
15299 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15300
15301 TYPE_FIELD_PRIVATE_BITS (type) =
15302 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15303 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15304
15305 TYPE_FIELD_PROTECTED_BITS (type) =
15306 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15307 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15308
15309 TYPE_FIELD_IGNORE_BITS (type) =
15310 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15311 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15312 }
15313
15314 /* If the type has baseclasses, allocate and clear a bit vector for
15315 TYPE_FIELD_VIRTUAL_BITS. */
15316 if (!fip->baseclasses.empty () && cu->language != language_ada)
15317 {
15318 int num_bytes = B_BYTES (fip->baseclasses.size ());
15319 unsigned char *pointer;
15320
15321 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15322 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15323 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15324 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15325 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15326 }
15327
15328 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15329 {
15330 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15331
15332 for (int index = 0; index < nfields; ++index)
15333 {
15334 struct nextfield &field = fip->fields[index];
15335
15336 if (field.variant.is_discriminant)
15337 di->discriminant_index = index;
15338 else if (field.variant.default_branch)
15339 di->default_index = index;
15340 else
15341 di->discriminants[index] = field.variant.discriminant_value;
15342 }
15343 }
15344
15345 /* Copy the saved-up fields into the field vector. */
15346 for (int i = 0; i < nfields; ++i)
15347 {
15348 struct nextfield &field
15349 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15350 : fip->fields[i - fip->baseclasses.size ()]);
15351
15352 TYPE_FIELD (type, i) = field.field;
15353 switch (field.accessibility)
15354 {
15355 case DW_ACCESS_private:
15356 if (cu->language != language_ada)
15357 SET_TYPE_FIELD_PRIVATE (type, i);
15358 break;
15359
15360 case DW_ACCESS_protected:
15361 if (cu->language != language_ada)
15362 SET_TYPE_FIELD_PROTECTED (type, i);
15363 break;
15364
15365 case DW_ACCESS_public:
15366 break;
15367
15368 default:
15369 /* Unknown accessibility. Complain and treat it as public. */
15370 {
15371 complaint (_("unsupported accessibility %d"),
15372 field.accessibility);
15373 }
15374 break;
15375 }
15376 if (i < fip->baseclasses.size ())
15377 {
15378 switch (field.virtuality)
15379 {
15380 case DW_VIRTUALITY_virtual:
15381 case DW_VIRTUALITY_pure_virtual:
15382 if (cu->language == language_ada)
15383 error (_("unexpected virtuality in component of Ada type"));
15384 SET_TYPE_FIELD_VIRTUAL (type, i);
15385 break;
15386 }
15387 }
15388 }
15389 }
15390
15391 /* Return true if this member function is a constructor, false
15392 otherwise. */
15393
15394 static int
15395 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15396 {
15397 const char *fieldname;
15398 const char *type_name;
15399 int len;
15400
15401 if (die->parent == NULL)
15402 return 0;
15403
15404 if (die->parent->tag != DW_TAG_structure_type
15405 && die->parent->tag != DW_TAG_union_type
15406 && die->parent->tag != DW_TAG_class_type)
15407 return 0;
15408
15409 fieldname = dwarf2_name (die, cu);
15410 type_name = dwarf2_name (die->parent, cu);
15411 if (fieldname == NULL || type_name == NULL)
15412 return 0;
15413
15414 len = strlen (fieldname);
15415 return (strncmp (fieldname, type_name, len) == 0
15416 && (type_name[len] == '\0' || type_name[len] == '<'));
15417 }
15418
15419 /* Add a member function to the proper fieldlist. */
15420
15421 static void
15422 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15423 struct type *type, struct dwarf2_cu *cu)
15424 {
15425 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15426 struct attribute *attr;
15427 int i;
15428 struct fnfieldlist *flp = nullptr;
15429 struct fn_field *fnp;
15430 const char *fieldname;
15431 struct type *this_type;
15432 enum dwarf_access_attribute accessibility;
15433
15434 if (cu->language == language_ada)
15435 error (_("unexpected member function in Ada type"));
15436
15437 /* Get name of member function. */
15438 fieldname = dwarf2_name (die, cu);
15439 if (fieldname == NULL)
15440 return;
15441
15442 /* Look up member function name in fieldlist. */
15443 for (i = 0; i < fip->fnfieldlists.size (); i++)
15444 {
15445 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15446 {
15447 flp = &fip->fnfieldlists[i];
15448 break;
15449 }
15450 }
15451
15452 /* Create a new fnfieldlist if necessary. */
15453 if (flp == nullptr)
15454 {
15455 fip->fnfieldlists.emplace_back ();
15456 flp = &fip->fnfieldlists.back ();
15457 flp->name = fieldname;
15458 i = fip->fnfieldlists.size () - 1;
15459 }
15460
15461 /* Create a new member function field and add it to the vector of
15462 fnfieldlists. */
15463 flp->fnfields.emplace_back ();
15464 fnp = &flp->fnfields.back ();
15465
15466 /* Delay processing of the physname until later. */
15467 if (cu->language == language_cplus)
15468 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15469 die, cu);
15470 else
15471 {
15472 const char *physname = dwarf2_physname (fieldname, die, cu);
15473 fnp->physname = physname ? physname : "";
15474 }
15475
15476 fnp->type = alloc_type (objfile);
15477 this_type = read_type_die (die, cu);
15478 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15479 {
15480 int nparams = TYPE_NFIELDS (this_type);
15481
15482 /* TYPE is the domain of this method, and THIS_TYPE is the type
15483 of the method itself (TYPE_CODE_METHOD). */
15484 smash_to_method_type (fnp->type, type,
15485 TYPE_TARGET_TYPE (this_type),
15486 TYPE_FIELDS (this_type),
15487 TYPE_NFIELDS (this_type),
15488 TYPE_VARARGS (this_type));
15489
15490 /* Handle static member functions.
15491 Dwarf2 has no clean way to discern C++ static and non-static
15492 member functions. G++ helps GDB by marking the first
15493 parameter for non-static member functions (which is the this
15494 pointer) as artificial. We obtain this information from
15495 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15496 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15497 fnp->voffset = VOFFSET_STATIC;
15498 }
15499 else
15500 complaint (_("member function type missing for '%s'"),
15501 dwarf2_full_name (fieldname, die, cu));
15502
15503 /* Get fcontext from DW_AT_containing_type if present. */
15504 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15505 fnp->fcontext = die_containing_type (die, cu);
15506
15507 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15508 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15509
15510 /* Get accessibility. */
15511 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15512 if (attr)
15513 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15514 else
15515 accessibility = dwarf2_default_access_attribute (die, cu);
15516 switch (accessibility)
15517 {
15518 case DW_ACCESS_private:
15519 fnp->is_private = 1;
15520 break;
15521 case DW_ACCESS_protected:
15522 fnp->is_protected = 1;
15523 break;
15524 }
15525
15526 /* Check for artificial methods. */
15527 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15528 if (attr && DW_UNSND (attr) != 0)
15529 fnp->is_artificial = 1;
15530
15531 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15532
15533 /* Get index in virtual function table if it is a virtual member
15534 function. For older versions of GCC, this is an offset in the
15535 appropriate virtual table, as specified by DW_AT_containing_type.
15536 For everyone else, it is an expression to be evaluated relative
15537 to the object address. */
15538
15539 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15540 if (attr)
15541 {
15542 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15543 {
15544 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15545 {
15546 /* Old-style GCC. */
15547 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15548 }
15549 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15550 || (DW_BLOCK (attr)->size > 1
15551 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15552 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15553 {
15554 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15555 if ((fnp->voffset % cu->header.addr_size) != 0)
15556 dwarf2_complex_location_expr_complaint ();
15557 else
15558 fnp->voffset /= cu->header.addr_size;
15559 fnp->voffset += 2;
15560 }
15561 else
15562 dwarf2_complex_location_expr_complaint ();
15563
15564 if (!fnp->fcontext)
15565 {
15566 /* If there is no `this' field and no DW_AT_containing_type,
15567 we cannot actually find a base class context for the
15568 vtable! */
15569 if (TYPE_NFIELDS (this_type) == 0
15570 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15571 {
15572 complaint (_("cannot determine context for virtual member "
15573 "function \"%s\" (offset %s)"),
15574 fieldname, sect_offset_str (die->sect_off));
15575 }
15576 else
15577 {
15578 fnp->fcontext
15579 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15580 }
15581 }
15582 }
15583 else if (attr_form_is_section_offset (attr))
15584 {
15585 dwarf2_complex_location_expr_complaint ();
15586 }
15587 else
15588 {
15589 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15590 fieldname);
15591 }
15592 }
15593 else
15594 {
15595 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15596 if (attr && DW_UNSND (attr))
15597 {
15598 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15599 complaint (_("Member function \"%s\" (offset %s) is virtual "
15600 "but the vtable offset is not specified"),
15601 fieldname, sect_offset_str (die->sect_off));
15602 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15603 TYPE_CPLUS_DYNAMIC (type) = 1;
15604 }
15605 }
15606 }
15607
15608 /* Create the vector of member function fields, and attach it to the type. */
15609
15610 static void
15611 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15612 struct dwarf2_cu *cu)
15613 {
15614 if (cu->language == language_ada)
15615 error (_("unexpected member functions in Ada type"));
15616
15617 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15618 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15619 TYPE_ALLOC (type,
15620 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15621
15622 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15623 {
15624 struct fnfieldlist &nf = fip->fnfieldlists[i];
15625 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15626
15627 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15628 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15629 fn_flp->fn_fields = (struct fn_field *)
15630 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15631
15632 for (int k = 0; k < nf.fnfields.size (); ++k)
15633 fn_flp->fn_fields[k] = nf.fnfields[k];
15634 }
15635
15636 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15637 }
15638
15639 /* Returns non-zero if NAME is the name of a vtable member in CU's
15640 language, zero otherwise. */
15641 static int
15642 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15643 {
15644 static const char vptr[] = "_vptr";
15645
15646 /* Look for the C++ form of the vtable. */
15647 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15648 return 1;
15649
15650 return 0;
15651 }
15652
15653 /* GCC outputs unnamed structures that are really pointers to member
15654 functions, with the ABI-specified layout. If TYPE describes
15655 such a structure, smash it into a member function type.
15656
15657 GCC shouldn't do this; it should just output pointer to member DIEs.
15658 This is GCC PR debug/28767. */
15659
15660 static void
15661 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15662 {
15663 struct type *pfn_type, *self_type, *new_type;
15664
15665 /* Check for a structure with no name and two children. */
15666 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15667 return;
15668
15669 /* Check for __pfn and __delta members. */
15670 if (TYPE_FIELD_NAME (type, 0) == NULL
15671 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15672 || TYPE_FIELD_NAME (type, 1) == NULL
15673 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15674 return;
15675
15676 /* Find the type of the method. */
15677 pfn_type = TYPE_FIELD_TYPE (type, 0);
15678 if (pfn_type == NULL
15679 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15680 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15681 return;
15682
15683 /* Look for the "this" argument. */
15684 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15685 if (TYPE_NFIELDS (pfn_type) == 0
15686 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15687 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15688 return;
15689
15690 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15691 new_type = alloc_type (objfile);
15692 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15693 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15694 TYPE_VARARGS (pfn_type));
15695 smash_to_methodptr_type (type, new_type);
15696 }
15697
15698 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15699 appropriate error checking and issuing complaints if there is a
15700 problem. */
15701
15702 static ULONGEST
15703 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15704 {
15705 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15706
15707 if (attr == nullptr)
15708 return 0;
15709
15710 if (!attr_form_is_constant (attr))
15711 {
15712 complaint (_("DW_AT_alignment must have constant form"
15713 " - DIE at %s [in module %s]"),
15714 sect_offset_str (die->sect_off),
15715 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15716 return 0;
15717 }
15718
15719 ULONGEST align;
15720 if (attr->form == DW_FORM_sdata)
15721 {
15722 LONGEST val = DW_SND (attr);
15723 if (val < 0)
15724 {
15725 complaint (_("DW_AT_alignment value must not be negative"
15726 " - DIE at %s [in module %s]"),
15727 sect_offset_str (die->sect_off),
15728 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15729 return 0;
15730 }
15731 align = val;
15732 }
15733 else
15734 align = DW_UNSND (attr);
15735
15736 if (align == 0)
15737 {
15738 complaint (_("DW_AT_alignment value must not be zero"
15739 " - DIE at %s [in module %s]"),
15740 sect_offset_str (die->sect_off),
15741 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15742 return 0;
15743 }
15744 if ((align & (align - 1)) != 0)
15745 {
15746 complaint (_("DW_AT_alignment value must be a power of 2"
15747 " - DIE at %s [in module %s]"),
15748 sect_offset_str (die->sect_off),
15749 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15750 return 0;
15751 }
15752
15753 return align;
15754 }
15755
15756 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15757 the alignment for TYPE. */
15758
15759 static void
15760 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15761 struct type *type)
15762 {
15763 if (!set_type_align (type, get_alignment (cu, die)))
15764 complaint (_("DW_AT_alignment value too large"
15765 " - DIE at %s [in module %s]"),
15766 sect_offset_str (die->sect_off),
15767 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15768 }
15769
15770 /* Called when we find the DIE that starts a structure or union scope
15771 (definition) to create a type for the structure or union. Fill in
15772 the type's name and general properties; the members will not be
15773 processed until process_structure_scope. A symbol table entry for
15774 the type will also not be done until process_structure_scope (assuming
15775 the type has a name).
15776
15777 NOTE: we need to call these functions regardless of whether or not the
15778 DIE has a DW_AT_name attribute, since it might be an anonymous
15779 structure or union. This gets the type entered into our set of
15780 user defined types. */
15781
15782 static struct type *
15783 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15784 {
15785 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15786 struct type *type;
15787 struct attribute *attr;
15788 const char *name;
15789
15790 /* If the definition of this type lives in .debug_types, read that type.
15791 Don't follow DW_AT_specification though, that will take us back up
15792 the chain and we want to go down. */
15793 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15794 if (attr)
15795 {
15796 type = get_DW_AT_signature_type (die, attr, cu);
15797
15798 /* The type's CU may not be the same as CU.
15799 Ensure TYPE is recorded with CU in die_type_hash. */
15800 return set_die_type (die, type, cu);
15801 }
15802
15803 type = alloc_type (objfile);
15804 INIT_CPLUS_SPECIFIC (type);
15805
15806 name = dwarf2_name (die, cu);
15807 if (name != NULL)
15808 {
15809 if (cu->language == language_cplus
15810 || cu->language == language_d
15811 || cu->language == language_rust)
15812 {
15813 const char *full_name = dwarf2_full_name (name, die, cu);
15814
15815 /* dwarf2_full_name might have already finished building the DIE's
15816 type. If so, there is no need to continue. */
15817 if (get_die_type (die, cu) != NULL)
15818 return get_die_type (die, cu);
15819
15820 TYPE_NAME (type) = full_name;
15821 }
15822 else
15823 {
15824 /* The name is already allocated along with this objfile, so
15825 we don't need to duplicate it for the type. */
15826 TYPE_NAME (type) = name;
15827 }
15828 }
15829
15830 if (die->tag == DW_TAG_structure_type)
15831 {
15832 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15833 }
15834 else if (die->tag == DW_TAG_union_type)
15835 {
15836 TYPE_CODE (type) = TYPE_CODE_UNION;
15837 }
15838 else if (die->tag == DW_TAG_variant_part)
15839 {
15840 TYPE_CODE (type) = TYPE_CODE_UNION;
15841 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15842 }
15843 else
15844 {
15845 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15846 }
15847
15848 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15849 TYPE_DECLARED_CLASS (type) = 1;
15850
15851 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15852 if (attr)
15853 {
15854 if (attr_form_is_constant (attr))
15855 TYPE_LENGTH (type) = DW_UNSND (attr);
15856 else
15857 {
15858 /* For the moment, dynamic type sizes are not supported
15859 by GDB's struct type. The actual size is determined
15860 on-demand when resolving the type of a given object,
15861 so set the type's length to zero for now. Otherwise,
15862 we record an expression as the length, and that expression
15863 could lead to a very large value, which could eventually
15864 lead to us trying to allocate that much memory when creating
15865 a value of that type. */
15866 TYPE_LENGTH (type) = 0;
15867 }
15868 }
15869 else
15870 {
15871 TYPE_LENGTH (type) = 0;
15872 }
15873
15874 maybe_set_alignment (cu, die, type);
15875
15876 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15877 {
15878 /* ICC<14 does not output the required DW_AT_declaration on
15879 incomplete types, but gives them a size of zero. */
15880 TYPE_STUB (type) = 1;
15881 }
15882 else
15883 TYPE_STUB_SUPPORTED (type) = 1;
15884
15885 if (die_is_declaration (die, cu))
15886 TYPE_STUB (type) = 1;
15887 else if (attr == NULL && die->child == NULL
15888 && producer_is_realview (cu->producer))
15889 /* RealView does not output the required DW_AT_declaration
15890 on incomplete types. */
15891 TYPE_STUB (type) = 1;
15892
15893 /* We need to add the type field to the die immediately so we don't
15894 infinitely recurse when dealing with pointers to the structure
15895 type within the structure itself. */
15896 set_die_type (die, type, cu);
15897
15898 /* set_die_type should be already done. */
15899 set_descriptive_type (type, die, cu);
15900
15901 return type;
15902 }
15903
15904 /* A helper for process_structure_scope that handles a single member
15905 DIE. */
15906
15907 static void
15908 handle_struct_member_die (struct die_info *child_die, struct type *type,
15909 struct field_info *fi,
15910 std::vector<struct symbol *> *template_args,
15911 struct dwarf2_cu *cu)
15912 {
15913 if (child_die->tag == DW_TAG_member
15914 || child_die->tag == DW_TAG_variable
15915 || child_die->tag == DW_TAG_variant_part)
15916 {
15917 /* NOTE: carlton/2002-11-05: A C++ static data member
15918 should be a DW_TAG_member that is a declaration, but
15919 all versions of G++ as of this writing (so through at
15920 least 3.2.1) incorrectly generate DW_TAG_variable
15921 tags for them instead. */
15922 dwarf2_add_field (fi, child_die, cu);
15923 }
15924 else if (child_die->tag == DW_TAG_subprogram)
15925 {
15926 /* Rust doesn't have member functions in the C++ sense.
15927 However, it does emit ordinary functions as children
15928 of a struct DIE. */
15929 if (cu->language == language_rust)
15930 read_func_scope (child_die, cu);
15931 else
15932 {
15933 /* C++ member function. */
15934 dwarf2_add_member_fn (fi, child_die, type, cu);
15935 }
15936 }
15937 else if (child_die->tag == DW_TAG_inheritance)
15938 {
15939 /* C++ base class field. */
15940 dwarf2_add_field (fi, child_die, cu);
15941 }
15942 else if (type_can_define_types (child_die))
15943 dwarf2_add_type_defn (fi, child_die, cu);
15944 else if (child_die->tag == DW_TAG_template_type_param
15945 || child_die->tag == DW_TAG_template_value_param)
15946 {
15947 struct symbol *arg = new_symbol (child_die, NULL, cu);
15948
15949 if (arg != NULL)
15950 template_args->push_back (arg);
15951 }
15952 else if (child_die->tag == DW_TAG_variant)
15953 {
15954 /* In a variant we want to get the discriminant and also add a
15955 field for our sole member child. */
15956 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15957
15958 for (die_info *variant_child = child_die->child;
15959 variant_child != NULL;
15960 variant_child = sibling_die (variant_child))
15961 {
15962 if (variant_child->tag == DW_TAG_member)
15963 {
15964 handle_struct_member_die (variant_child, type, fi,
15965 template_args, cu);
15966 /* Only handle the one. */
15967 break;
15968 }
15969 }
15970
15971 /* We don't handle this but we might as well report it if we see
15972 it. */
15973 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15974 complaint (_("DW_AT_discr_list is not supported yet"
15975 " - DIE at %s [in module %s]"),
15976 sect_offset_str (child_die->sect_off),
15977 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15978
15979 /* The first field was just added, so we can stash the
15980 discriminant there. */
15981 gdb_assert (!fi->fields.empty ());
15982 if (discr == NULL)
15983 fi->fields.back ().variant.default_branch = true;
15984 else
15985 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15986 }
15987 }
15988
15989 /* Finish creating a structure or union type, including filling in
15990 its members and creating a symbol for it. */
15991
15992 static void
15993 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15994 {
15995 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15996 struct die_info *child_die;
15997 struct type *type;
15998
15999 type = get_die_type (die, cu);
16000 if (type == NULL)
16001 type = read_structure_type (die, cu);
16002
16003 /* When reading a DW_TAG_variant_part, we need to notice when we
16004 read the discriminant member, so we can record it later in the
16005 discriminant_info. */
16006 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16007 sect_offset discr_offset;
16008 bool has_template_parameters = false;
16009
16010 if (is_variant_part)
16011 {
16012 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16013 if (discr == NULL)
16014 {
16015 /* Maybe it's a univariant form, an extension we support.
16016 In this case arrange not to check the offset. */
16017 is_variant_part = false;
16018 }
16019 else if (attr_form_is_ref (discr))
16020 {
16021 struct dwarf2_cu *target_cu = cu;
16022 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16023
16024 discr_offset = target_die->sect_off;
16025 }
16026 else
16027 {
16028 complaint (_("DW_AT_discr does not have DIE reference form"
16029 " - DIE at %s [in module %s]"),
16030 sect_offset_str (die->sect_off),
16031 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16032 is_variant_part = false;
16033 }
16034 }
16035
16036 if (die->child != NULL && ! die_is_declaration (die, cu))
16037 {
16038 struct field_info fi;
16039 std::vector<struct symbol *> template_args;
16040
16041 child_die = die->child;
16042
16043 while (child_die && child_die->tag)
16044 {
16045 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16046
16047 if (is_variant_part && discr_offset == child_die->sect_off)
16048 fi.fields.back ().variant.is_discriminant = true;
16049
16050 child_die = sibling_die (child_die);
16051 }
16052
16053 /* Attach template arguments to type. */
16054 if (!template_args.empty ())
16055 {
16056 has_template_parameters = true;
16057 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16058 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16059 TYPE_TEMPLATE_ARGUMENTS (type)
16060 = XOBNEWVEC (&objfile->objfile_obstack,
16061 struct symbol *,
16062 TYPE_N_TEMPLATE_ARGUMENTS (type));
16063 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16064 template_args.data (),
16065 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16066 * sizeof (struct symbol *)));
16067 }
16068
16069 /* Attach fields and member functions to the type. */
16070 if (fi.nfields)
16071 dwarf2_attach_fields_to_type (&fi, type, cu);
16072 if (!fi.fnfieldlists.empty ())
16073 {
16074 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16075
16076 /* Get the type which refers to the base class (possibly this
16077 class itself) which contains the vtable pointer for the current
16078 class from the DW_AT_containing_type attribute. This use of
16079 DW_AT_containing_type is a GNU extension. */
16080
16081 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16082 {
16083 struct type *t = die_containing_type (die, cu);
16084
16085 set_type_vptr_basetype (type, t);
16086 if (type == t)
16087 {
16088 int i;
16089
16090 /* Our own class provides vtbl ptr. */
16091 for (i = TYPE_NFIELDS (t) - 1;
16092 i >= TYPE_N_BASECLASSES (t);
16093 --i)
16094 {
16095 const char *fieldname = TYPE_FIELD_NAME (t, i);
16096
16097 if (is_vtable_name (fieldname, cu))
16098 {
16099 set_type_vptr_fieldno (type, i);
16100 break;
16101 }
16102 }
16103
16104 /* Complain if virtual function table field not found. */
16105 if (i < TYPE_N_BASECLASSES (t))
16106 complaint (_("virtual function table pointer "
16107 "not found when defining class '%s'"),
16108 TYPE_NAME (type) ? TYPE_NAME (type) : "");
16109 }
16110 else
16111 {
16112 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16113 }
16114 }
16115 else if (cu->producer
16116 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16117 {
16118 /* The IBM XLC compiler does not provide direct indication
16119 of the containing type, but the vtable pointer is
16120 always named __vfp. */
16121
16122 int i;
16123
16124 for (i = TYPE_NFIELDS (type) - 1;
16125 i >= TYPE_N_BASECLASSES (type);
16126 --i)
16127 {
16128 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16129 {
16130 set_type_vptr_fieldno (type, i);
16131 set_type_vptr_basetype (type, type);
16132 break;
16133 }
16134 }
16135 }
16136 }
16137
16138 /* Copy fi.typedef_field_list linked list elements content into the
16139 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16140 if (!fi.typedef_field_list.empty ())
16141 {
16142 int count = fi.typedef_field_list.size ();
16143
16144 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16145 TYPE_TYPEDEF_FIELD_ARRAY (type)
16146 = ((struct decl_field *)
16147 TYPE_ALLOC (type,
16148 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16149 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16150
16151 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16152 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16153 }
16154
16155 /* Copy fi.nested_types_list linked list elements content into the
16156 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16157 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16158 {
16159 int count = fi.nested_types_list.size ();
16160
16161 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16162 TYPE_NESTED_TYPES_ARRAY (type)
16163 = ((struct decl_field *)
16164 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16165 TYPE_NESTED_TYPES_COUNT (type) = count;
16166
16167 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16168 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16169 }
16170 }
16171
16172 quirk_gcc_member_function_pointer (type, objfile);
16173 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16174 cu->rust_unions.push_back (type);
16175
16176 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16177 snapshots) has been known to create a die giving a declaration
16178 for a class that has, as a child, a die giving a definition for a
16179 nested class. So we have to process our children even if the
16180 current die is a declaration. Normally, of course, a declaration
16181 won't have any children at all. */
16182
16183 child_die = die->child;
16184
16185 while (child_die != NULL && child_die->tag)
16186 {
16187 if (child_die->tag == DW_TAG_member
16188 || child_die->tag == DW_TAG_variable
16189 || child_die->tag == DW_TAG_inheritance
16190 || child_die->tag == DW_TAG_template_value_param
16191 || child_die->tag == DW_TAG_template_type_param)
16192 {
16193 /* Do nothing. */
16194 }
16195 else
16196 process_die (child_die, cu);
16197
16198 child_die = sibling_die (child_die);
16199 }
16200
16201 /* Do not consider external references. According to the DWARF standard,
16202 these DIEs are identified by the fact that they have no byte_size
16203 attribute, and a declaration attribute. */
16204 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16205 || !die_is_declaration (die, cu))
16206 {
16207 struct symbol *sym = new_symbol (die, type, cu);
16208
16209 if (has_template_parameters)
16210 {
16211 struct symtab *symtab;
16212 if (sym != nullptr)
16213 symtab = symbol_symtab (sym);
16214 else if (cu->line_header != nullptr)
16215 {
16216 /* Any related symtab will do. */
16217 symtab
16218 = cu->line_header->file_name_at (file_name_index (1))->symtab;
16219 }
16220 else
16221 {
16222 symtab = nullptr;
16223 complaint (_("could not find suitable "
16224 "symtab for template parameter"
16225 " - DIE at %s [in module %s]"),
16226 sect_offset_str (die->sect_off),
16227 objfile_name (objfile));
16228 }
16229
16230 if (symtab != nullptr)
16231 {
16232 /* Make sure that the symtab is set on the new symbols.
16233 Even though they don't appear in this symtab directly,
16234 other parts of gdb assume that symbols do, and this is
16235 reasonably true. */
16236 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16237 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16238 }
16239 }
16240 }
16241 }
16242
16243 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16244 update TYPE using some information only available in DIE's children. */
16245
16246 static void
16247 update_enumeration_type_from_children (struct die_info *die,
16248 struct type *type,
16249 struct dwarf2_cu *cu)
16250 {
16251 struct die_info *child_die;
16252 int unsigned_enum = 1;
16253 int flag_enum = 1;
16254 ULONGEST mask = 0;
16255
16256 auto_obstack obstack;
16257
16258 for (child_die = die->child;
16259 child_die != NULL && child_die->tag;
16260 child_die = sibling_die (child_die))
16261 {
16262 struct attribute *attr;
16263 LONGEST value;
16264 const gdb_byte *bytes;
16265 struct dwarf2_locexpr_baton *baton;
16266 const char *name;
16267
16268 if (child_die->tag != DW_TAG_enumerator)
16269 continue;
16270
16271 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16272 if (attr == NULL)
16273 continue;
16274
16275 name = dwarf2_name (child_die, cu);
16276 if (name == NULL)
16277 name = "<anonymous enumerator>";
16278
16279 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16280 &value, &bytes, &baton);
16281 if (value < 0)
16282 {
16283 unsigned_enum = 0;
16284 flag_enum = 0;
16285 }
16286 else if ((mask & value) != 0)
16287 flag_enum = 0;
16288 else
16289 mask |= value;
16290
16291 /* If we already know that the enum type is neither unsigned, nor
16292 a flag type, no need to look at the rest of the enumerates. */
16293 if (!unsigned_enum && !flag_enum)
16294 break;
16295 }
16296
16297 if (unsigned_enum)
16298 TYPE_UNSIGNED (type) = 1;
16299 if (flag_enum)
16300 TYPE_FLAG_ENUM (type) = 1;
16301 }
16302
16303 /* Given a DW_AT_enumeration_type die, set its type. We do not
16304 complete the type's fields yet, or create any symbols. */
16305
16306 static struct type *
16307 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16308 {
16309 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16310 struct type *type;
16311 struct attribute *attr;
16312 const char *name;
16313
16314 /* If the definition of this type lives in .debug_types, read that type.
16315 Don't follow DW_AT_specification though, that will take us back up
16316 the chain and we want to go down. */
16317 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16318 if (attr)
16319 {
16320 type = get_DW_AT_signature_type (die, attr, cu);
16321
16322 /* The type's CU may not be the same as CU.
16323 Ensure TYPE is recorded with CU in die_type_hash. */
16324 return set_die_type (die, type, cu);
16325 }
16326
16327 type = alloc_type (objfile);
16328
16329 TYPE_CODE (type) = TYPE_CODE_ENUM;
16330 name = dwarf2_full_name (NULL, die, cu);
16331 if (name != NULL)
16332 TYPE_NAME (type) = name;
16333
16334 attr = dwarf2_attr (die, DW_AT_type, cu);
16335 if (attr != NULL)
16336 {
16337 struct type *underlying_type = die_type (die, cu);
16338
16339 TYPE_TARGET_TYPE (type) = underlying_type;
16340 }
16341
16342 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16343 if (attr)
16344 {
16345 TYPE_LENGTH (type) = DW_UNSND (attr);
16346 }
16347 else
16348 {
16349 TYPE_LENGTH (type) = 0;
16350 }
16351
16352 maybe_set_alignment (cu, die, type);
16353
16354 /* The enumeration DIE can be incomplete. In Ada, any type can be
16355 declared as private in the package spec, and then defined only
16356 inside the package body. Such types are known as Taft Amendment
16357 Types. When another package uses such a type, an incomplete DIE
16358 may be generated by the compiler. */
16359 if (die_is_declaration (die, cu))
16360 TYPE_STUB (type) = 1;
16361
16362 /* Finish the creation of this type by using the enum's children.
16363 We must call this even when the underlying type has been provided
16364 so that we can determine if we're looking at a "flag" enum. */
16365 update_enumeration_type_from_children (die, type, cu);
16366
16367 /* If this type has an underlying type that is not a stub, then we
16368 may use its attributes. We always use the "unsigned" attribute
16369 in this situation, because ordinarily we guess whether the type
16370 is unsigned -- but the guess can be wrong and the underlying type
16371 can tell us the reality. However, we defer to a local size
16372 attribute if one exists, because this lets the compiler override
16373 the underlying type if needed. */
16374 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16375 {
16376 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16377 if (TYPE_LENGTH (type) == 0)
16378 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16379 if (TYPE_RAW_ALIGN (type) == 0
16380 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16381 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16382 }
16383
16384 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16385
16386 return set_die_type (die, type, cu);
16387 }
16388
16389 /* Given a pointer to a die which begins an enumeration, process all
16390 the dies that define the members of the enumeration, and create the
16391 symbol for the enumeration type.
16392
16393 NOTE: We reverse the order of the element list. */
16394
16395 static void
16396 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16397 {
16398 struct type *this_type;
16399
16400 this_type = get_die_type (die, cu);
16401 if (this_type == NULL)
16402 this_type = read_enumeration_type (die, cu);
16403
16404 if (die->child != NULL)
16405 {
16406 struct die_info *child_die;
16407 struct symbol *sym;
16408 struct field *fields = NULL;
16409 int num_fields = 0;
16410 const char *name;
16411
16412 child_die = die->child;
16413 while (child_die && child_die->tag)
16414 {
16415 if (child_die->tag != DW_TAG_enumerator)
16416 {
16417 process_die (child_die, cu);
16418 }
16419 else
16420 {
16421 name = dwarf2_name (child_die, cu);
16422 if (name)
16423 {
16424 sym = new_symbol (child_die, this_type, cu);
16425
16426 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16427 {
16428 fields = (struct field *)
16429 xrealloc (fields,
16430 (num_fields + DW_FIELD_ALLOC_CHUNK)
16431 * sizeof (struct field));
16432 }
16433
16434 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
16435 FIELD_TYPE (fields[num_fields]) = NULL;
16436 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
16437 FIELD_BITSIZE (fields[num_fields]) = 0;
16438
16439 num_fields++;
16440 }
16441 }
16442
16443 child_die = sibling_die (child_die);
16444 }
16445
16446 if (num_fields)
16447 {
16448 TYPE_NFIELDS (this_type) = num_fields;
16449 TYPE_FIELDS (this_type) = (struct field *)
16450 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16451 memcpy (TYPE_FIELDS (this_type), fields,
16452 sizeof (struct field) * num_fields);
16453 xfree (fields);
16454 }
16455 }
16456
16457 /* If we are reading an enum from a .debug_types unit, and the enum
16458 is a declaration, and the enum is not the signatured type in the
16459 unit, then we do not want to add a symbol for it. Adding a
16460 symbol would in some cases obscure the true definition of the
16461 enum, giving users an incomplete type when the definition is
16462 actually available. Note that we do not want to do this for all
16463 enums which are just declarations, because C++0x allows forward
16464 enum declarations. */
16465 if (cu->per_cu->is_debug_types
16466 && die_is_declaration (die, cu))
16467 {
16468 struct signatured_type *sig_type;
16469
16470 sig_type = (struct signatured_type *) cu->per_cu;
16471 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16472 if (sig_type->type_offset_in_section != die->sect_off)
16473 return;
16474 }
16475
16476 new_symbol (die, this_type, cu);
16477 }
16478
16479 /* Extract all information from a DW_TAG_array_type DIE and put it in
16480 the DIE's type field. For now, this only handles one dimensional
16481 arrays. */
16482
16483 static struct type *
16484 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16485 {
16486 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16487 struct die_info *child_die;
16488 struct type *type;
16489 struct type *element_type, *range_type, *index_type;
16490 struct attribute *attr;
16491 const char *name;
16492 struct dynamic_prop *byte_stride_prop = NULL;
16493 unsigned int bit_stride = 0;
16494
16495 element_type = die_type (die, cu);
16496
16497 /* The die_type call above may have already set the type for this DIE. */
16498 type = get_die_type (die, cu);
16499 if (type)
16500 return type;
16501
16502 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16503 if (attr != NULL)
16504 {
16505 int stride_ok;
16506 struct type *prop_type
16507 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
16508
16509 byte_stride_prop
16510 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16511 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16512 prop_type);
16513 if (!stride_ok)
16514 {
16515 complaint (_("unable to read array DW_AT_byte_stride "
16516 " - DIE at %s [in module %s]"),
16517 sect_offset_str (die->sect_off),
16518 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16519 /* Ignore this attribute. We will likely not be able to print
16520 arrays of this type correctly, but there is little we can do
16521 to help if we cannot read the attribute's value. */
16522 byte_stride_prop = NULL;
16523 }
16524 }
16525
16526 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16527 if (attr != NULL)
16528 bit_stride = DW_UNSND (attr);
16529
16530 /* Irix 6.2 native cc creates array types without children for
16531 arrays with unspecified length. */
16532 if (die->child == NULL)
16533 {
16534 index_type = objfile_type (objfile)->builtin_int;
16535 range_type = create_static_range_type (NULL, index_type, 0, -1);
16536 type = create_array_type_with_stride (NULL, element_type, range_type,
16537 byte_stride_prop, bit_stride);
16538 return set_die_type (die, type, cu);
16539 }
16540
16541 std::vector<struct type *> range_types;
16542 child_die = die->child;
16543 while (child_die && child_die->tag)
16544 {
16545 if (child_die->tag == DW_TAG_subrange_type)
16546 {
16547 struct type *child_type = read_type_die (child_die, cu);
16548
16549 if (child_type != NULL)
16550 {
16551 /* The range type was succesfully read. Save it for the
16552 array type creation. */
16553 range_types.push_back (child_type);
16554 }
16555 }
16556 child_die = sibling_die (child_die);
16557 }
16558
16559 /* Dwarf2 dimensions are output from left to right, create the
16560 necessary array types in backwards order. */
16561
16562 type = element_type;
16563
16564 if (read_array_order (die, cu) == DW_ORD_col_major)
16565 {
16566 int i = 0;
16567
16568 while (i < range_types.size ())
16569 type = create_array_type_with_stride (NULL, type, range_types[i++],
16570 byte_stride_prop, bit_stride);
16571 }
16572 else
16573 {
16574 size_t ndim = range_types.size ();
16575 while (ndim-- > 0)
16576 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16577 byte_stride_prop, bit_stride);
16578 }
16579
16580 /* Understand Dwarf2 support for vector types (like they occur on
16581 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16582 array type. This is not part of the Dwarf2/3 standard yet, but a
16583 custom vendor extension. The main difference between a regular
16584 array and the vector variant is that vectors are passed by value
16585 to functions. */
16586 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16587 if (attr)
16588 make_vector_type (type);
16589
16590 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16591 implementation may choose to implement triple vectors using this
16592 attribute. */
16593 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16594 if (attr)
16595 {
16596 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16597 TYPE_LENGTH (type) = DW_UNSND (attr);
16598 else
16599 complaint (_("DW_AT_byte_size for array type smaller "
16600 "than the total size of elements"));
16601 }
16602
16603 name = dwarf2_name (die, cu);
16604 if (name)
16605 TYPE_NAME (type) = name;
16606
16607 maybe_set_alignment (cu, die, type);
16608
16609 /* Install the type in the die. */
16610 set_die_type (die, type, cu);
16611
16612 /* set_die_type should be already done. */
16613 set_descriptive_type (type, die, cu);
16614
16615 return type;
16616 }
16617
16618 static enum dwarf_array_dim_ordering
16619 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16620 {
16621 struct attribute *attr;
16622
16623 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16624
16625 if (attr)
16626 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16627
16628 /* GNU F77 is a special case, as at 08/2004 array type info is the
16629 opposite order to the dwarf2 specification, but data is still
16630 laid out as per normal fortran.
16631
16632 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16633 version checking. */
16634
16635 if (cu->language == language_fortran
16636 && cu->producer && strstr (cu->producer, "GNU F77"))
16637 {
16638 return DW_ORD_row_major;
16639 }
16640
16641 switch (cu->language_defn->la_array_ordering)
16642 {
16643 case array_column_major:
16644 return DW_ORD_col_major;
16645 case array_row_major:
16646 default:
16647 return DW_ORD_row_major;
16648 };
16649 }
16650
16651 /* Extract all information from a DW_TAG_set_type DIE and put it in
16652 the DIE's type field. */
16653
16654 static struct type *
16655 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16656 {
16657 struct type *domain_type, *set_type;
16658 struct attribute *attr;
16659
16660 domain_type = die_type (die, cu);
16661
16662 /* The die_type call above may have already set the type for this DIE. */
16663 set_type = get_die_type (die, cu);
16664 if (set_type)
16665 return set_type;
16666
16667 set_type = create_set_type (NULL, domain_type);
16668
16669 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16670 if (attr)
16671 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16672
16673 maybe_set_alignment (cu, die, set_type);
16674
16675 return set_die_type (die, set_type, cu);
16676 }
16677
16678 /* A helper for read_common_block that creates a locexpr baton.
16679 SYM is the symbol which we are marking as computed.
16680 COMMON_DIE is the DIE for the common block.
16681 COMMON_LOC is the location expression attribute for the common
16682 block itself.
16683 MEMBER_LOC is the location expression attribute for the particular
16684 member of the common block that we are processing.
16685 CU is the CU from which the above come. */
16686
16687 static void
16688 mark_common_block_symbol_computed (struct symbol *sym,
16689 struct die_info *common_die,
16690 struct attribute *common_loc,
16691 struct attribute *member_loc,
16692 struct dwarf2_cu *cu)
16693 {
16694 struct dwarf2_per_objfile *dwarf2_per_objfile
16695 = cu->per_cu->dwarf2_per_objfile;
16696 struct objfile *objfile = dwarf2_per_objfile->objfile;
16697 struct dwarf2_locexpr_baton *baton;
16698 gdb_byte *ptr;
16699 unsigned int cu_off;
16700 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16701 LONGEST offset = 0;
16702
16703 gdb_assert (common_loc && member_loc);
16704 gdb_assert (attr_form_is_block (common_loc));
16705 gdb_assert (attr_form_is_block (member_loc)
16706 || attr_form_is_constant (member_loc));
16707
16708 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16709 baton->per_cu = cu->per_cu;
16710 gdb_assert (baton->per_cu);
16711
16712 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16713
16714 if (attr_form_is_constant (member_loc))
16715 {
16716 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16717 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16718 }
16719 else
16720 baton->size += DW_BLOCK (member_loc)->size;
16721
16722 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16723 baton->data = ptr;
16724
16725 *ptr++ = DW_OP_call4;
16726 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16727 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16728 ptr += 4;
16729
16730 if (attr_form_is_constant (member_loc))
16731 {
16732 *ptr++ = DW_OP_addr;
16733 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16734 ptr += cu->header.addr_size;
16735 }
16736 else
16737 {
16738 /* We have to copy the data here, because DW_OP_call4 will only
16739 use a DW_AT_location attribute. */
16740 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16741 ptr += DW_BLOCK (member_loc)->size;
16742 }
16743
16744 *ptr++ = DW_OP_plus;
16745 gdb_assert (ptr - baton->data == baton->size);
16746
16747 SYMBOL_LOCATION_BATON (sym) = baton;
16748 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16749 }
16750
16751 /* Create appropriate locally-scoped variables for all the
16752 DW_TAG_common_block entries. Also create a struct common_block
16753 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16754 is used to sepate the common blocks name namespace from regular
16755 variable names. */
16756
16757 static void
16758 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16759 {
16760 struct attribute *attr;
16761
16762 attr = dwarf2_attr (die, DW_AT_location, cu);
16763 if (attr)
16764 {
16765 /* Support the .debug_loc offsets. */
16766 if (attr_form_is_block (attr))
16767 {
16768 /* Ok. */
16769 }
16770 else if (attr_form_is_section_offset (attr))
16771 {
16772 dwarf2_complex_location_expr_complaint ();
16773 attr = NULL;
16774 }
16775 else
16776 {
16777 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16778 "common block member");
16779 attr = NULL;
16780 }
16781 }
16782
16783 if (die->child != NULL)
16784 {
16785 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16786 struct die_info *child_die;
16787 size_t n_entries = 0, size;
16788 struct common_block *common_block;
16789 struct symbol *sym;
16790
16791 for (child_die = die->child;
16792 child_die && child_die->tag;
16793 child_die = sibling_die (child_die))
16794 ++n_entries;
16795
16796 size = (sizeof (struct common_block)
16797 + (n_entries - 1) * sizeof (struct symbol *));
16798 common_block
16799 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16800 size);
16801 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16802 common_block->n_entries = 0;
16803
16804 for (child_die = die->child;
16805 child_die && child_die->tag;
16806 child_die = sibling_die (child_die))
16807 {
16808 /* Create the symbol in the DW_TAG_common_block block in the current
16809 symbol scope. */
16810 sym = new_symbol (child_die, NULL, cu);
16811 if (sym != NULL)
16812 {
16813 struct attribute *member_loc;
16814
16815 common_block->contents[common_block->n_entries++] = sym;
16816
16817 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16818 cu);
16819 if (member_loc)
16820 {
16821 /* GDB has handled this for a long time, but it is
16822 not specified by DWARF. It seems to have been
16823 emitted by gfortran at least as recently as:
16824 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16825 complaint (_("Variable in common block has "
16826 "DW_AT_data_member_location "
16827 "- DIE at %s [in module %s]"),
16828 sect_offset_str (child_die->sect_off),
16829 objfile_name (objfile));
16830
16831 if (attr_form_is_section_offset (member_loc))
16832 dwarf2_complex_location_expr_complaint ();
16833 else if (attr_form_is_constant (member_loc)
16834 || attr_form_is_block (member_loc))
16835 {
16836 if (attr)
16837 mark_common_block_symbol_computed (sym, die, attr,
16838 member_loc, cu);
16839 }
16840 else
16841 dwarf2_complex_location_expr_complaint ();
16842 }
16843 }
16844 }
16845
16846 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16847 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16848 }
16849 }
16850
16851 /* Create a type for a C++ namespace. */
16852
16853 static struct type *
16854 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16855 {
16856 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16857 const char *previous_prefix, *name;
16858 int is_anonymous;
16859 struct type *type;
16860
16861 /* For extensions, reuse the type of the original namespace. */
16862 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16863 {
16864 struct die_info *ext_die;
16865 struct dwarf2_cu *ext_cu = cu;
16866
16867 ext_die = dwarf2_extension (die, &ext_cu);
16868 type = read_type_die (ext_die, ext_cu);
16869
16870 /* EXT_CU may not be the same as CU.
16871 Ensure TYPE is recorded with CU in die_type_hash. */
16872 return set_die_type (die, type, cu);
16873 }
16874
16875 name = namespace_name (die, &is_anonymous, cu);
16876
16877 /* Now build the name of the current namespace. */
16878
16879 previous_prefix = determine_prefix (die, cu);
16880 if (previous_prefix[0] != '\0')
16881 name = typename_concat (&objfile->objfile_obstack,
16882 previous_prefix, name, 0, cu);
16883
16884 /* Create the type. */
16885 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16886
16887 return set_die_type (die, type, cu);
16888 }
16889
16890 /* Read a namespace scope. */
16891
16892 static void
16893 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16894 {
16895 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16896 int is_anonymous;
16897
16898 /* Add a symbol associated to this if we haven't seen the namespace
16899 before. Also, add a using directive if it's an anonymous
16900 namespace. */
16901
16902 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16903 {
16904 struct type *type;
16905
16906 type = read_type_die (die, cu);
16907 new_symbol (die, type, cu);
16908
16909 namespace_name (die, &is_anonymous, cu);
16910 if (is_anonymous)
16911 {
16912 const char *previous_prefix = determine_prefix (die, cu);
16913
16914 std::vector<const char *> excludes;
16915 add_using_directive (using_directives (cu),
16916 previous_prefix, TYPE_NAME (type), NULL,
16917 NULL, excludes, 0, &objfile->objfile_obstack);
16918 }
16919 }
16920
16921 if (die->child != NULL)
16922 {
16923 struct die_info *child_die = die->child;
16924
16925 while (child_die && child_die->tag)
16926 {
16927 process_die (child_die, cu);
16928 child_die = sibling_die (child_die);
16929 }
16930 }
16931 }
16932
16933 /* Read a Fortran module as type. This DIE can be only a declaration used for
16934 imported module. Still we need that type as local Fortran "use ... only"
16935 declaration imports depend on the created type in determine_prefix. */
16936
16937 static struct type *
16938 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16939 {
16940 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16941 const char *module_name;
16942 struct type *type;
16943
16944 module_name = dwarf2_name (die, cu);
16945 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16946
16947 return set_die_type (die, type, cu);
16948 }
16949
16950 /* Read a Fortran module. */
16951
16952 static void
16953 read_module (struct die_info *die, struct dwarf2_cu *cu)
16954 {
16955 struct die_info *child_die = die->child;
16956 struct type *type;
16957
16958 type = read_type_die (die, cu);
16959 new_symbol (die, type, cu);
16960
16961 while (child_die && child_die->tag)
16962 {
16963 process_die (child_die, cu);
16964 child_die = sibling_die (child_die);
16965 }
16966 }
16967
16968 /* Return the name of the namespace represented by DIE. Set
16969 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16970 namespace. */
16971
16972 static const char *
16973 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16974 {
16975 struct die_info *current_die;
16976 const char *name = NULL;
16977
16978 /* Loop through the extensions until we find a name. */
16979
16980 for (current_die = die;
16981 current_die != NULL;
16982 current_die = dwarf2_extension (die, &cu))
16983 {
16984 /* We don't use dwarf2_name here so that we can detect the absence
16985 of a name -> anonymous namespace. */
16986 name = dwarf2_string_attr (die, DW_AT_name, cu);
16987
16988 if (name != NULL)
16989 break;
16990 }
16991
16992 /* Is it an anonymous namespace? */
16993
16994 *is_anonymous = (name == NULL);
16995 if (*is_anonymous)
16996 name = CP_ANONYMOUS_NAMESPACE_STR;
16997
16998 return name;
16999 }
17000
17001 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17002 the user defined type vector. */
17003
17004 static struct type *
17005 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17006 {
17007 struct gdbarch *gdbarch
17008 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
17009 struct comp_unit_head *cu_header = &cu->header;
17010 struct type *type;
17011 struct attribute *attr_byte_size;
17012 struct attribute *attr_address_class;
17013 int byte_size, addr_class;
17014 struct type *target_type;
17015
17016 target_type = die_type (die, cu);
17017
17018 /* The die_type call above may have already set the type for this DIE. */
17019 type = get_die_type (die, cu);
17020 if (type)
17021 return type;
17022
17023 type = lookup_pointer_type (target_type);
17024
17025 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17026 if (attr_byte_size)
17027 byte_size = DW_UNSND (attr_byte_size);
17028 else
17029 byte_size = cu_header->addr_size;
17030
17031 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17032 if (attr_address_class)
17033 addr_class = DW_UNSND (attr_address_class);
17034 else
17035 addr_class = DW_ADDR_none;
17036
17037 ULONGEST alignment = get_alignment (cu, die);
17038
17039 /* If the pointer size, alignment, or address class is different
17040 than the default, create a type variant marked as such and set
17041 the length accordingly. */
17042 if (TYPE_LENGTH (type) != byte_size
17043 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17044 && alignment != TYPE_RAW_ALIGN (type))
17045 || addr_class != DW_ADDR_none)
17046 {
17047 if (gdbarch_address_class_type_flags_p (gdbarch))
17048 {
17049 int type_flags;
17050
17051 type_flags = gdbarch_address_class_type_flags
17052 (gdbarch, byte_size, addr_class);
17053 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17054 == 0);
17055 type = make_type_with_address_space (type, type_flags);
17056 }
17057 else if (TYPE_LENGTH (type) != byte_size)
17058 {
17059 complaint (_("invalid pointer size %d"), byte_size);
17060 }
17061 else if (TYPE_RAW_ALIGN (type) != alignment)
17062 {
17063 complaint (_("Invalid DW_AT_alignment"
17064 " - DIE at %s [in module %s]"),
17065 sect_offset_str (die->sect_off),
17066 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17067 }
17068 else
17069 {
17070 /* Should we also complain about unhandled address classes? */
17071 }
17072 }
17073
17074 TYPE_LENGTH (type) = byte_size;
17075 set_type_align (type, alignment);
17076 return set_die_type (die, type, cu);
17077 }
17078
17079 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17080 the user defined type vector. */
17081
17082 static struct type *
17083 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17084 {
17085 struct type *type;
17086 struct type *to_type;
17087 struct type *domain;
17088
17089 to_type = die_type (die, cu);
17090 domain = die_containing_type (die, cu);
17091
17092 /* The calls above may have already set the type for this DIE. */
17093 type = get_die_type (die, cu);
17094 if (type)
17095 return type;
17096
17097 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17098 type = lookup_methodptr_type (to_type);
17099 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17100 {
17101 struct type *new_type
17102 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17103
17104 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17105 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17106 TYPE_VARARGS (to_type));
17107 type = lookup_methodptr_type (new_type);
17108 }
17109 else
17110 type = lookup_memberptr_type (to_type, domain);
17111
17112 return set_die_type (die, type, cu);
17113 }
17114
17115 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17116 the user defined type vector. */
17117
17118 static struct type *
17119 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17120 enum type_code refcode)
17121 {
17122 struct comp_unit_head *cu_header = &cu->header;
17123 struct type *type, *target_type;
17124 struct attribute *attr;
17125
17126 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17127
17128 target_type = die_type (die, cu);
17129
17130 /* The die_type call above may have already set the type for this DIE. */
17131 type = get_die_type (die, cu);
17132 if (type)
17133 return type;
17134
17135 type = lookup_reference_type (target_type, refcode);
17136 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17137 if (attr)
17138 {
17139 TYPE_LENGTH (type) = DW_UNSND (attr);
17140 }
17141 else
17142 {
17143 TYPE_LENGTH (type) = cu_header->addr_size;
17144 }
17145 maybe_set_alignment (cu, die, type);
17146 return set_die_type (die, type, cu);
17147 }
17148
17149 /* Add the given cv-qualifiers to the element type of the array. GCC
17150 outputs DWARF type qualifiers that apply to an array, not the
17151 element type. But GDB relies on the array element type to carry
17152 the cv-qualifiers. This mimics section 6.7.3 of the C99
17153 specification. */
17154
17155 static struct type *
17156 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17157 struct type *base_type, int cnst, int voltl)
17158 {
17159 struct type *el_type, *inner_array;
17160
17161 base_type = copy_type (base_type);
17162 inner_array = base_type;
17163
17164 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17165 {
17166 TYPE_TARGET_TYPE (inner_array) =
17167 copy_type (TYPE_TARGET_TYPE (inner_array));
17168 inner_array = TYPE_TARGET_TYPE (inner_array);
17169 }
17170
17171 el_type = TYPE_TARGET_TYPE (inner_array);
17172 cnst |= TYPE_CONST (el_type);
17173 voltl |= TYPE_VOLATILE (el_type);
17174 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17175
17176 return set_die_type (die, base_type, cu);
17177 }
17178
17179 static struct type *
17180 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17181 {
17182 struct type *base_type, *cv_type;
17183
17184 base_type = die_type (die, cu);
17185
17186 /* The die_type call above may have already set the type for this DIE. */
17187 cv_type = get_die_type (die, cu);
17188 if (cv_type)
17189 return cv_type;
17190
17191 /* In case the const qualifier is applied to an array type, the element type
17192 is so qualified, not the array type (section 6.7.3 of C99). */
17193 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17194 return add_array_cv_type (die, cu, base_type, 1, 0);
17195
17196 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17197 return set_die_type (die, cv_type, cu);
17198 }
17199
17200 static struct type *
17201 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17202 {
17203 struct type *base_type, *cv_type;
17204
17205 base_type = die_type (die, cu);
17206
17207 /* The die_type call above may have already set the type for this DIE. */
17208 cv_type = get_die_type (die, cu);
17209 if (cv_type)
17210 return cv_type;
17211
17212 /* In case the volatile qualifier is applied to an array type, the
17213 element type is so qualified, not the array type (section 6.7.3
17214 of C99). */
17215 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17216 return add_array_cv_type (die, cu, base_type, 0, 1);
17217
17218 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17219 return set_die_type (die, cv_type, cu);
17220 }
17221
17222 /* Handle DW_TAG_restrict_type. */
17223
17224 static struct type *
17225 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17226 {
17227 struct type *base_type, *cv_type;
17228
17229 base_type = die_type (die, cu);
17230
17231 /* The die_type call above may have already set the type for this DIE. */
17232 cv_type = get_die_type (die, cu);
17233 if (cv_type)
17234 return cv_type;
17235
17236 cv_type = make_restrict_type (base_type);
17237 return set_die_type (die, cv_type, cu);
17238 }
17239
17240 /* Handle DW_TAG_atomic_type. */
17241
17242 static struct type *
17243 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17244 {
17245 struct type *base_type, *cv_type;
17246
17247 base_type = die_type (die, cu);
17248
17249 /* The die_type call above may have already set the type for this DIE. */
17250 cv_type = get_die_type (die, cu);
17251 if (cv_type)
17252 return cv_type;
17253
17254 cv_type = make_atomic_type (base_type);
17255 return set_die_type (die, cv_type, cu);
17256 }
17257
17258 /* Extract all information from a DW_TAG_string_type DIE and add to
17259 the user defined type vector. It isn't really a user defined type,
17260 but it behaves like one, with other DIE's using an AT_user_def_type
17261 attribute to reference it. */
17262
17263 static struct type *
17264 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17265 {
17266 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17267 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17268 struct type *type, *range_type, *index_type, *char_type;
17269 struct attribute *attr;
17270 unsigned int length;
17271
17272 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17273 if (attr)
17274 {
17275 length = DW_UNSND (attr);
17276 }
17277 else
17278 {
17279 /* Check for the DW_AT_byte_size attribute. */
17280 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17281 if (attr)
17282 {
17283 length = DW_UNSND (attr);
17284 }
17285 else
17286 {
17287 length = 1;
17288 }
17289 }
17290
17291 index_type = objfile_type (objfile)->builtin_int;
17292 range_type = create_static_range_type (NULL, index_type, 1, length);
17293 char_type = language_string_char_type (cu->language_defn, gdbarch);
17294 type = create_string_type (NULL, char_type, range_type);
17295
17296 return set_die_type (die, type, cu);
17297 }
17298
17299 /* Assuming that DIE corresponds to a function, returns nonzero
17300 if the function is prototyped. */
17301
17302 static int
17303 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17304 {
17305 struct attribute *attr;
17306
17307 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17308 if (attr && (DW_UNSND (attr) != 0))
17309 return 1;
17310
17311 /* The DWARF standard implies that the DW_AT_prototyped attribute
17312 is only meaninful for C, but the concept also extends to other
17313 languages that allow unprototyped functions (Eg: Objective C).
17314 For all other languages, assume that functions are always
17315 prototyped. */
17316 if (cu->language != language_c
17317 && cu->language != language_objc
17318 && cu->language != language_opencl)
17319 return 1;
17320
17321 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17322 prototyped and unprototyped functions; default to prototyped,
17323 since that is more common in modern code (and RealView warns
17324 about unprototyped functions). */
17325 if (producer_is_realview (cu->producer))
17326 return 1;
17327
17328 return 0;
17329 }
17330
17331 /* Handle DIES due to C code like:
17332
17333 struct foo
17334 {
17335 int (*funcp)(int a, long l);
17336 int b;
17337 };
17338
17339 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17340
17341 static struct type *
17342 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17343 {
17344 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17345 struct type *type; /* Type that this function returns. */
17346 struct type *ftype; /* Function that returns above type. */
17347 struct attribute *attr;
17348
17349 type = die_type (die, cu);
17350
17351 /* The die_type call above may have already set the type for this DIE. */
17352 ftype = get_die_type (die, cu);
17353 if (ftype)
17354 return ftype;
17355
17356 ftype = lookup_function_type (type);
17357
17358 if (prototyped_function_p (die, cu))
17359 TYPE_PROTOTYPED (ftype) = 1;
17360
17361 /* Store the calling convention in the type if it's available in
17362 the subroutine die. Otherwise set the calling convention to
17363 the default value DW_CC_normal. */
17364 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17365 if (attr)
17366 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17367 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17368 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17369 else
17370 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17371
17372 /* Record whether the function returns normally to its caller or not
17373 if the DWARF producer set that information. */
17374 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17375 if (attr && (DW_UNSND (attr) != 0))
17376 TYPE_NO_RETURN (ftype) = 1;
17377
17378 /* We need to add the subroutine type to the die immediately so
17379 we don't infinitely recurse when dealing with parameters
17380 declared as the same subroutine type. */
17381 set_die_type (die, ftype, cu);
17382
17383 if (die->child != NULL)
17384 {
17385 struct type *void_type = objfile_type (objfile)->builtin_void;
17386 struct die_info *child_die;
17387 int nparams, iparams;
17388
17389 /* Count the number of parameters.
17390 FIXME: GDB currently ignores vararg functions, but knows about
17391 vararg member functions. */
17392 nparams = 0;
17393 child_die = die->child;
17394 while (child_die && child_die->tag)
17395 {
17396 if (child_die->tag == DW_TAG_formal_parameter)
17397 nparams++;
17398 else if (child_die->tag == DW_TAG_unspecified_parameters)
17399 TYPE_VARARGS (ftype) = 1;
17400 child_die = sibling_die (child_die);
17401 }
17402
17403 /* Allocate storage for parameters and fill them in. */
17404 TYPE_NFIELDS (ftype) = nparams;
17405 TYPE_FIELDS (ftype) = (struct field *)
17406 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17407
17408 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17409 even if we error out during the parameters reading below. */
17410 for (iparams = 0; iparams < nparams; iparams++)
17411 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17412
17413 iparams = 0;
17414 child_die = die->child;
17415 while (child_die && child_die->tag)
17416 {
17417 if (child_die->tag == DW_TAG_formal_parameter)
17418 {
17419 struct type *arg_type;
17420
17421 /* DWARF version 2 has no clean way to discern C++
17422 static and non-static member functions. G++ helps
17423 GDB by marking the first parameter for non-static
17424 member functions (which is the this pointer) as
17425 artificial. We pass this information to
17426 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17427
17428 DWARF version 3 added DW_AT_object_pointer, which GCC
17429 4.5 does not yet generate. */
17430 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17431 if (attr)
17432 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17433 else
17434 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17435 arg_type = die_type (child_die, cu);
17436
17437 /* RealView does not mark THIS as const, which the testsuite
17438 expects. GCC marks THIS as const in method definitions,
17439 but not in the class specifications (GCC PR 43053). */
17440 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17441 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17442 {
17443 int is_this = 0;
17444 struct dwarf2_cu *arg_cu = cu;
17445 const char *name = dwarf2_name (child_die, cu);
17446
17447 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17448 if (attr)
17449 {
17450 /* If the compiler emits this, use it. */
17451 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17452 is_this = 1;
17453 }
17454 else if (name && strcmp (name, "this") == 0)
17455 /* Function definitions will have the argument names. */
17456 is_this = 1;
17457 else if (name == NULL && iparams == 0)
17458 /* Declarations may not have the names, so like
17459 elsewhere in GDB, assume an artificial first
17460 argument is "this". */
17461 is_this = 1;
17462
17463 if (is_this)
17464 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17465 arg_type, 0);
17466 }
17467
17468 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17469 iparams++;
17470 }
17471 child_die = sibling_die (child_die);
17472 }
17473 }
17474
17475 return ftype;
17476 }
17477
17478 static struct type *
17479 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17480 {
17481 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17482 const char *name = NULL;
17483 struct type *this_type, *target_type;
17484
17485 name = dwarf2_full_name (NULL, die, cu);
17486 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17487 TYPE_TARGET_STUB (this_type) = 1;
17488 set_die_type (die, this_type, cu);
17489 target_type = die_type (die, cu);
17490 if (target_type != this_type)
17491 TYPE_TARGET_TYPE (this_type) = target_type;
17492 else
17493 {
17494 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17495 spec and cause infinite loops in GDB. */
17496 complaint (_("Self-referential DW_TAG_typedef "
17497 "- DIE at %s [in module %s]"),
17498 sect_offset_str (die->sect_off), objfile_name (objfile));
17499 TYPE_TARGET_TYPE (this_type) = NULL;
17500 }
17501 return this_type;
17502 }
17503
17504 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17505 (which may be different from NAME) to the architecture back-end to allow
17506 it to guess the correct format if necessary. */
17507
17508 static struct type *
17509 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17510 const char *name_hint)
17511 {
17512 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17513 const struct floatformat **format;
17514 struct type *type;
17515
17516 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17517 if (format)
17518 type = init_float_type (objfile, bits, name, format);
17519 else
17520 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17521
17522 return type;
17523 }
17524
17525 /* Allocate an integer type of size BITS and name NAME. */
17526
17527 static struct type *
17528 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17529 int bits, int unsigned_p, const char *name)
17530 {
17531 struct type *type;
17532
17533 /* Versions of Intel's C Compiler generate an integer type called "void"
17534 instead of using DW_TAG_unspecified_type. This has been seen on
17535 at least versions 14, 17, and 18. */
17536 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17537 && strcmp (name, "void") == 0)
17538 type = objfile_type (objfile)->builtin_void;
17539 else
17540 type = init_integer_type (objfile, bits, unsigned_p, name);
17541
17542 return type;
17543 }
17544
17545 /* Initialise and return a floating point type of size BITS suitable for
17546 use as a component of a complex number. The NAME_HINT is passed through
17547 when initialising the floating point type and is the name of the complex
17548 type.
17549
17550 As DWARF doesn't currently provide an explicit name for the components
17551 of a complex number, but it can be helpful to have these components
17552 named, we try to select a suitable name based on the size of the
17553 component. */
17554 static struct type *
17555 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17556 struct objfile *objfile,
17557 int bits, const char *name_hint)
17558 {
17559 gdbarch *gdbarch = get_objfile_arch (objfile);
17560 struct type *tt = nullptr;
17561
17562 /* Try to find a suitable floating point builtin type of size BITS.
17563 We're going to use the name of this type as the name for the complex
17564 target type that we are about to create. */
17565 switch (cu->language)
17566 {
17567 case language_fortran:
17568 switch (bits)
17569 {
17570 case 32:
17571 tt = builtin_f_type (gdbarch)->builtin_real;
17572 break;
17573 case 64:
17574 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17575 break;
17576 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17577 case 128:
17578 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17579 break;
17580 }
17581 break;
17582 default:
17583 switch (bits)
17584 {
17585 case 32:
17586 tt = builtin_type (gdbarch)->builtin_float;
17587 break;
17588 case 64:
17589 tt = builtin_type (gdbarch)->builtin_double;
17590 break;
17591 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17592 case 128:
17593 tt = builtin_type (gdbarch)->builtin_long_double;
17594 break;
17595 }
17596 break;
17597 }
17598
17599 /* If the type we found doesn't match the size we were looking for, then
17600 pretend we didn't find a type at all, the complex target type we
17601 create will then be nameless. */
17602 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17603 tt = nullptr;
17604
17605 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17606 return dwarf2_init_float_type (objfile, bits, name, name_hint);
17607 }
17608
17609 /* Find a representation of a given base type and install
17610 it in the TYPE field of the die. */
17611
17612 static struct type *
17613 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17614 {
17615 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17616 struct type *type;
17617 struct attribute *attr;
17618 int encoding = 0, bits = 0;
17619 const char *name;
17620
17621 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17622 if (attr)
17623 {
17624 encoding = DW_UNSND (attr);
17625 }
17626 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17627 if (attr)
17628 {
17629 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17630 }
17631 name = dwarf2_name (die, cu);
17632 if (!name)
17633 {
17634 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17635 }
17636
17637 switch (encoding)
17638 {
17639 case DW_ATE_address:
17640 /* Turn DW_ATE_address into a void * pointer. */
17641 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17642 type = init_pointer_type (objfile, bits, name, type);
17643 break;
17644 case DW_ATE_boolean:
17645 type = init_boolean_type (objfile, bits, 1, name);
17646 break;
17647 case DW_ATE_complex_float:
17648 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name);
17649 type = init_complex_type (objfile, name, type);
17650 break;
17651 case DW_ATE_decimal_float:
17652 type = init_decfloat_type (objfile, bits, name);
17653 break;
17654 case DW_ATE_float:
17655 type = dwarf2_init_float_type (objfile, bits, name, name);
17656 break;
17657 case DW_ATE_signed:
17658 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17659 break;
17660 case DW_ATE_unsigned:
17661 if (cu->language == language_fortran
17662 && name
17663 && startswith (name, "character("))
17664 type = init_character_type (objfile, bits, 1, name);
17665 else
17666 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17667 break;
17668 case DW_ATE_signed_char:
17669 if (cu->language == language_ada || cu->language == language_m2
17670 || cu->language == language_pascal
17671 || cu->language == language_fortran)
17672 type = init_character_type (objfile, bits, 0, name);
17673 else
17674 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17675 break;
17676 case DW_ATE_unsigned_char:
17677 if (cu->language == language_ada || cu->language == language_m2
17678 || cu->language == language_pascal
17679 || cu->language == language_fortran
17680 || cu->language == language_rust)
17681 type = init_character_type (objfile, bits, 1, name);
17682 else
17683 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17684 break;
17685 case DW_ATE_UTF:
17686 {
17687 gdbarch *arch = get_objfile_arch (objfile);
17688
17689 if (bits == 16)
17690 type = builtin_type (arch)->builtin_char16;
17691 else if (bits == 32)
17692 type = builtin_type (arch)->builtin_char32;
17693 else
17694 {
17695 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17696 bits);
17697 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17698 }
17699 return set_die_type (die, type, cu);
17700 }
17701 break;
17702
17703 default:
17704 complaint (_("unsupported DW_AT_encoding: '%s'"),
17705 dwarf_type_encoding_name (encoding));
17706 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17707 break;
17708 }
17709
17710 if (name && strcmp (name, "char") == 0)
17711 TYPE_NOSIGN (type) = 1;
17712
17713 maybe_set_alignment (cu, die, type);
17714
17715 return set_die_type (die, type, cu);
17716 }
17717
17718 /* Parse dwarf attribute if it's a block, reference or constant and put the
17719 resulting value of the attribute into struct bound_prop.
17720 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17721
17722 static int
17723 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17724 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17725 struct type *default_type)
17726 {
17727 struct dwarf2_property_baton *baton;
17728 struct obstack *obstack
17729 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17730
17731 gdb_assert (default_type != NULL);
17732
17733 if (attr == NULL || prop == NULL)
17734 return 0;
17735
17736 if (attr_form_is_block (attr))
17737 {
17738 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17739 baton->property_type = default_type;
17740 baton->locexpr.per_cu = cu->per_cu;
17741 baton->locexpr.size = DW_BLOCK (attr)->size;
17742 baton->locexpr.data = DW_BLOCK (attr)->data;
17743 baton->locexpr.is_reference = false;
17744 prop->data.baton = baton;
17745 prop->kind = PROP_LOCEXPR;
17746 gdb_assert (prop->data.baton != NULL);
17747 }
17748 else if (attr_form_is_ref (attr))
17749 {
17750 struct dwarf2_cu *target_cu = cu;
17751 struct die_info *target_die;
17752 struct attribute *target_attr;
17753
17754 target_die = follow_die_ref (die, attr, &target_cu);
17755 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17756 if (target_attr == NULL)
17757 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17758 target_cu);
17759 if (target_attr == NULL)
17760 return 0;
17761
17762 switch (target_attr->name)
17763 {
17764 case DW_AT_location:
17765 if (attr_form_is_section_offset (target_attr))
17766 {
17767 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17768 baton->property_type = die_type (target_die, target_cu);
17769 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17770 prop->data.baton = baton;
17771 prop->kind = PROP_LOCLIST;
17772 gdb_assert (prop->data.baton != NULL);
17773 }
17774 else if (attr_form_is_block (target_attr))
17775 {
17776 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17777 baton->property_type = die_type (target_die, target_cu);
17778 baton->locexpr.per_cu = cu->per_cu;
17779 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17780 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17781 baton->locexpr.is_reference = true;
17782 prop->data.baton = baton;
17783 prop->kind = PROP_LOCEXPR;
17784 gdb_assert (prop->data.baton != NULL);
17785 }
17786 else
17787 {
17788 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17789 "dynamic property");
17790 return 0;
17791 }
17792 break;
17793 case DW_AT_data_member_location:
17794 {
17795 LONGEST offset;
17796
17797 if (!handle_data_member_location (target_die, target_cu,
17798 &offset))
17799 return 0;
17800
17801 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17802 baton->property_type = read_type_die (target_die->parent,
17803 target_cu);
17804 baton->offset_info.offset = offset;
17805 baton->offset_info.type = die_type (target_die, target_cu);
17806 prop->data.baton = baton;
17807 prop->kind = PROP_ADDR_OFFSET;
17808 break;
17809 }
17810 }
17811 }
17812 else if (attr_form_is_constant (attr))
17813 {
17814 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17815 prop->kind = PROP_CONST;
17816 }
17817 else
17818 {
17819 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17820 dwarf2_name (die, cu));
17821 return 0;
17822 }
17823
17824 return 1;
17825 }
17826
17827 /* Find an integer type the same size as the address size given in the
17828 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
17829 is unsigned or not. */
17830
17831 static struct type *
17832 dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
17833 bool unsigned_p)
17834 {
17835 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
17836 int addr_size = dwarf2_per_cu_addr_size (per_cu);
17837 struct type *int_type;
17838
17839 /* Helper macro to examine the various builtin types. */
17840 #define TRY_TYPE(F) \
17841 int_type = (unsigned_p \
17842 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17843 : objfile_type (objfile)->builtin_ ## F); \
17844 if (int_type != NULL && TYPE_LENGTH (int_type) == addr_size) \
17845 return int_type
17846
17847 TRY_TYPE (char);
17848 TRY_TYPE (short);
17849 TRY_TYPE (int);
17850 TRY_TYPE (long);
17851 TRY_TYPE (long_long);
17852
17853 #undef TRY_TYPE
17854
17855 gdb_assert_not_reached ("unable to find suitable integer type");
17856 }
17857
17858 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17859 present (which is valid) then compute the default type based on the
17860 compilation units address size. */
17861
17862 static struct type *
17863 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17864 {
17865 struct type *index_type = die_type (die, cu);
17866
17867 /* Dwarf-2 specifications explicitly allows to create subrange types
17868 without specifying a base type.
17869 In that case, the base type must be set to the type of
17870 the lower bound, upper bound or count, in that order, if any of these
17871 three attributes references an object that has a type.
17872 If no base type is found, the Dwarf-2 specifications say that
17873 a signed integer type of size equal to the size of an address should
17874 be used.
17875 For the following C code: `extern char gdb_int [];'
17876 GCC produces an empty range DIE.
17877 FIXME: muller/2010-05-28: Possible references to object for low bound,
17878 high bound or count are not yet handled by this code. */
17879 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17880 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17881
17882 return index_type;
17883 }
17884
17885 /* Read the given DW_AT_subrange DIE. */
17886
17887 static struct type *
17888 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17889 {
17890 struct type *base_type, *orig_base_type;
17891 struct type *range_type;
17892 struct attribute *attr;
17893 struct dynamic_prop low, high;
17894 int low_default_is_valid;
17895 int high_bound_is_count = 0;
17896 const char *name;
17897 ULONGEST negative_mask;
17898
17899 orig_base_type = read_subrange_index_type (die, cu);
17900
17901 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17902 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17903 creating the range type, but we use the result of check_typedef
17904 when examining properties of the type. */
17905 base_type = check_typedef (orig_base_type);
17906
17907 /* The die_type call above may have already set the type for this DIE. */
17908 range_type = get_die_type (die, cu);
17909 if (range_type)
17910 return range_type;
17911
17912 low.kind = PROP_CONST;
17913 high.kind = PROP_CONST;
17914 high.data.const_val = 0;
17915
17916 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17917 omitting DW_AT_lower_bound. */
17918 switch (cu->language)
17919 {
17920 case language_c:
17921 case language_cplus:
17922 low.data.const_val = 0;
17923 low_default_is_valid = 1;
17924 break;
17925 case language_fortran:
17926 low.data.const_val = 1;
17927 low_default_is_valid = 1;
17928 break;
17929 case language_d:
17930 case language_objc:
17931 case language_rust:
17932 low.data.const_val = 0;
17933 low_default_is_valid = (cu->header.version >= 4);
17934 break;
17935 case language_ada:
17936 case language_m2:
17937 case language_pascal:
17938 low.data.const_val = 1;
17939 low_default_is_valid = (cu->header.version >= 4);
17940 break;
17941 default:
17942 low.data.const_val = 0;
17943 low_default_is_valid = 0;
17944 break;
17945 }
17946
17947 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17948 if (attr)
17949 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17950 else if (!low_default_is_valid)
17951 complaint (_("Missing DW_AT_lower_bound "
17952 "- DIE at %s [in module %s]"),
17953 sect_offset_str (die->sect_off),
17954 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17955
17956 struct attribute *attr_ub, *attr_count;
17957 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17958 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17959 {
17960 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17961 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17962 {
17963 /* If bounds are constant do the final calculation here. */
17964 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17965 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17966 else
17967 high_bound_is_count = 1;
17968 }
17969 else
17970 {
17971 if (attr_ub != NULL)
17972 complaint (_("Unresolved DW_AT_upper_bound "
17973 "- DIE at %s [in module %s]"),
17974 sect_offset_str (die->sect_off),
17975 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17976 if (attr_count != NULL)
17977 complaint (_("Unresolved DW_AT_count "
17978 "- DIE at %s [in module %s]"),
17979 sect_offset_str (die->sect_off),
17980 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17981 }
17982 }
17983
17984 LONGEST bias = 0;
17985 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17986 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
17987 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17988
17989 /* Normally, the DWARF producers are expected to use a signed
17990 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17991 But this is unfortunately not always the case, as witnessed
17992 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17993 is used instead. To work around that ambiguity, we treat
17994 the bounds as signed, and thus sign-extend their values, when
17995 the base type is signed. */
17996 negative_mask =
17997 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17998 if (low.kind == PROP_CONST
17999 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18000 low.data.const_val |= negative_mask;
18001 if (high.kind == PROP_CONST
18002 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18003 high.data.const_val |= negative_mask;
18004
18005 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18006
18007 if (high_bound_is_count)
18008 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18009
18010 /* Ada expects an empty array on no boundary attributes. */
18011 if (attr == NULL && cu->language != language_ada)
18012 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
18013
18014 name = dwarf2_name (die, cu);
18015 if (name)
18016 TYPE_NAME (range_type) = name;
18017
18018 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18019 if (attr)
18020 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18021
18022 maybe_set_alignment (cu, die, range_type);
18023
18024 set_die_type (die, range_type, cu);
18025
18026 /* set_die_type should be already done. */
18027 set_descriptive_type (range_type, die, cu);
18028
18029 return range_type;
18030 }
18031
18032 static struct type *
18033 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18034 {
18035 struct type *type;
18036
18037 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18038 NULL);
18039 TYPE_NAME (type) = dwarf2_name (die, cu);
18040
18041 /* In Ada, an unspecified type is typically used when the description
18042 of the type is defered to a different unit. When encountering
18043 such a type, we treat it as a stub, and try to resolve it later on,
18044 when needed. */
18045 if (cu->language == language_ada)
18046 TYPE_STUB (type) = 1;
18047
18048 return set_die_type (die, type, cu);
18049 }
18050
18051 /* Read a single die and all its descendents. Set the die's sibling
18052 field to NULL; set other fields in the die correctly, and set all
18053 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18054 location of the info_ptr after reading all of those dies. PARENT
18055 is the parent of the die in question. */
18056
18057 static struct die_info *
18058 read_die_and_children (const struct die_reader_specs *reader,
18059 const gdb_byte *info_ptr,
18060 const gdb_byte **new_info_ptr,
18061 struct die_info *parent)
18062 {
18063 struct die_info *die;
18064 const gdb_byte *cur_ptr;
18065 int has_children;
18066
18067 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
18068 if (die == NULL)
18069 {
18070 *new_info_ptr = cur_ptr;
18071 return NULL;
18072 }
18073 store_in_ref_table (die, reader->cu);
18074
18075 if (has_children)
18076 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18077 else
18078 {
18079 die->child = NULL;
18080 *new_info_ptr = cur_ptr;
18081 }
18082
18083 die->sibling = NULL;
18084 die->parent = parent;
18085 return die;
18086 }
18087
18088 /* Read a die, all of its descendents, and all of its siblings; set
18089 all of the fields of all of the dies correctly. Arguments are as
18090 in read_die_and_children. */
18091
18092 static struct die_info *
18093 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18094 const gdb_byte *info_ptr,
18095 const gdb_byte **new_info_ptr,
18096 struct die_info *parent)
18097 {
18098 struct die_info *first_die, *last_sibling;
18099 const gdb_byte *cur_ptr;
18100
18101 cur_ptr = info_ptr;
18102 first_die = last_sibling = NULL;
18103
18104 while (1)
18105 {
18106 struct die_info *die
18107 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18108
18109 if (die == NULL)
18110 {
18111 *new_info_ptr = cur_ptr;
18112 return first_die;
18113 }
18114
18115 if (!first_die)
18116 first_die = die;
18117 else
18118 last_sibling->sibling = die;
18119
18120 last_sibling = die;
18121 }
18122 }
18123
18124 /* Read a die, all of its descendents, and all of its siblings; set
18125 all of the fields of all of the dies correctly. Arguments are as
18126 in read_die_and_children.
18127 This the main entry point for reading a DIE and all its children. */
18128
18129 static struct die_info *
18130 read_die_and_siblings (const struct die_reader_specs *reader,
18131 const gdb_byte *info_ptr,
18132 const gdb_byte **new_info_ptr,
18133 struct die_info *parent)
18134 {
18135 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18136 new_info_ptr, parent);
18137
18138 if (dwarf_die_debug)
18139 {
18140 fprintf_unfiltered (gdb_stdlog,
18141 "Read die from %s@0x%x of %s:\n",
18142 get_section_name (reader->die_section),
18143 (unsigned) (info_ptr - reader->die_section->buffer),
18144 bfd_get_filename (reader->abfd));
18145 dump_die (die, dwarf_die_debug);
18146 }
18147
18148 return die;
18149 }
18150
18151 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18152 attributes.
18153 The caller is responsible for filling in the extra attributes
18154 and updating (*DIEP)->num_attrs.
18155 Set DIEP to point to a newly allocated die with its information,
18156 except for its child, sibling, and parent fields.
18157 Set HAS_CHILDREN to tell whether the die has children or not. */
18158
18159 static const gdb_byte *
18160 read_full_die_1 (const struct die_reader_specs *reader,
18161 struct die_info **diep, const gdb_byte *info_ptr,
18162 int *has_children, int num_extra_attrs)
18163 {
18164 unsigned int abbrev_number, bytes_read, i;
18165 struct abbrev_info *abbrev;
18166 struct die_info *die;
18167 struct dwarf2_cu *cu = reader->cu;
18168 bfd *abfd = reader->abfd;
18169
18170 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18171 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18172 info_ptr += bytes_read;
18173 if (!abbrev_number)
18174 {
18175 *diep = NULL;
18176 *has_children = 0;
18177 return info_ptr;
18178 }
18179
18180 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18181 if (!abbrev)
18182 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18183 abbrev_number,
18184 bfd_get_filename (abfd));
18185
18186 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18187 die->sect_off = sect_off;
18188 die->tag = abbrev->tag;
18189 die->abbrev = abbrev_number;
18190
18191 /* Make the result usable.
18192 The caller needs to update num_attrs after adding the extra
18193 attributes. */
18194 die->num_attrs = abbrev->num_attrs;
18195
18196 for (i = 0; i < abbrev->num_attrs; ++i)
18197 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18198 info_ptr);
18199
18200 *diep = die;
18201 *has_children = abbrev->has_children;
18202 return info_ptr;
18203 }
18204
18205 /* Read a die and all its attributes.
18206 Set DIEP to point to a newly allocated die with its information,
18207 except for its child, sibling, and parent fields.
18208 Set HAS_CHILDREN to tell whether the die has children or not. */
18209
18210 static const gdb_byte *
18211 read_full_die (const struct die_reader_specs *reader,
18212 struct die_info **diep, const gdb_byte *info_ptr,
18213 int *has_children)
18214 {
18215 const gdb_byte *result;
18216
18217 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18218
18219 if (dwarf_die_debug)
18220 {
18221 fprintf_unfiltered (gdb_stdlog,
18222 "Read die from %s@0x%x of %s:\n",
18223 get_section_name (reader->die_section),
18224 (unsigned) (info_ptr - reader->die_section->buffer),
18225 bfd_get_filename (reader->abfd));
18226 dump_die (*diep, dwarf_die_debug);
18227 }
18228
18229 return result;
18230 }
18231 \f
18232 /* Abbreviation tables.
18233
18234 In DWARF version 2, the description of the debugging information is
18235 stored in a separate .debug_abbrev section. Before we read any
18236 dies from a section we read in all abbreviations and install them
18237 in a hash table. */
18238
18239 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18240
18241 struct abbrev_info *
18242 abbrev_table::alloc_abbrev ()
18243 {
18244 struct abbrev_info *abbrev;
18245
18246 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18247 memset (abbrev, 0, sizeof (struct abbrev_info));
18248
18249 return abbrev;
18250 }
18251
18252 /* Add an abbreviation to the table. */
18253
18254 void
18255 abbrev_table::add_abbrev (unsigned int abbrev_number,
18256 struct abbrev_info *abbrev)
18257 {
18258 unsigned int hash_number;
18259
18260 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18261 abbrev->next = m_abbrevs[hash_number];
18262 m_abbrevs[hash_number] = abbrev;
18263 }
18264
18265 /* Look up an abbrev in the table.
18266 Returns NULL if the abbrev is not found. */
18267
18268 struct abbrev_info *
18269 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18270 {
18271 unsigned int hash_number;
18272 struct abbrev_info *abbrev;
18273
18274 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18275 abbrev = m_abbrevs[hash_number];
18276
18277 while (abbrev)
18278 {
18279 if (abbrev->number == abbrev_number)
18280 return abbrev;
18281 abbrev = abbrev->next;
18282 }
18283 return NULL;
18284 }
18285
18286 /* Read in an abbrev table. */
18287
18288 static abbrev_table_up
18289 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18290 struct dwarf2_section_info *section,
18291 sect_offset sect_off)
18292 {
18293 struct objfile *objfile = dwarf2_per_objfile->objfile;
18294 bfd *abfd = get_section_bfd_owner (section);
18295 const gdb_byte *abbrev_ptr;
18296 struct abbrev_info *cur_abbrev;
18297 unsigned int abbrev_number, bytes_read, abbrev_name;
18298 unsigned int abbrev_form;
18299 struct attr_abbrev *cur_attrs;
18300 unsigned int allocated_attrs;
18301
18302 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18303
18304 dwarf2_read_section (objfile, section);
18305 abbrev_ptr = section->buffer + to_underlying (sect_off);
18306 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18307 abbrev_ptr += bytes_read;
18308
18309 allocated_attrs = ATTR_ALLOC_CHUNK;
18310 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
18311
18312 /* Loop until we reach an abbrev number of 0. */
18313 while (abbrev_number)
18314 {
18315 cur_abbrev = abbrev_table->alloc_abbrev ();
18316
18317 /* read in abbrev header */
18318 cur_abbrev->number = abbrev_number;
18319 cur_abbrev->tag
18320 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18321 abbrev_ptr += bytes_read;
18322 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18323 abbrev_ptr += 1;
18324
18325 /* now read in declarations */
18326 for (;;)
18327 {
18328 LONGEST implicit_const;
18329
18330 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18331 abbrev_ptr += bytes_read;
18332 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18333 abbrev_ptr += bytes_read;
18334 if (abbrev_form == DW_FORM_implicit_const)
18335 {
18336 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18337 &bytes_read);
18338 abbrev_ptr += bytes_read;
18339 }
18340 else
18341 {
18342 /* Initialize it due to a false compiler warning. */
18343 implicit_const = -1;
18344 }
18345
18346 if (abbrev_name == 0)
18347 break;
18348
18349 if (cur_abbrev->num_attrs == allocated_attrs)
18350 {
18351 allocated_attrs += ATTR_ALLOC_CHUNK;
18352 cur_attrs
18353 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
18354 }
18355
18356 cur_attrs[cur_abbrev->num_attrs].name
18357 = (enum dwarf_attribute) abbrev_name;
18358 cur_attrs[cur_abbrev->num_attrs].form
18359 = (enum dwarf_form) abbrev_form;
18360 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
18361 ++cur_abbrev->num_attrs;
18362 }
18363
18364 cur_abbrev->attrs =
18365 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18366 cur_abbrev->num_attrs);
18367 memcpy (cur_abbrev->attrs, cur_attrs,
18368 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18369
18370 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18371
18372 /* Get next abbreviation.
18373 Under Irix6 the abbreviations for a compilation unit are not
18374 always properly terminated with an abbrev number of 0.
18375 Exit loop if we encounter an abbreviation which we have
18376 already read (which means we are about to read the abbreviations
18377 for the next compile unit) or if the end of the abbreviation
18378 table is reached. */
18379 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18380 break;
18381 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18382 abbrev_ptr += bytes_read;
18383 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18384 break;
18385 }
18386
18387 xfree (cur_attrs);
18388 return abbrev_table;
18389 }
18390
18391 /* Returns nonzero if TAG represents a type that we might generate a partial
18392 symbol for. */
18393
18394 static int
18395 is_type_tag_for_partial (int tag)
18396 {
18397 switch (tag)
18398 {
18399 #if 0
18400 /* Some types that would be reasonable to generate partial symbols for,
18401 that we don't at present. */
18402 case DW_TAG_array_type:
18403 case DW_TAG_file_type:
18404 case DW_TAG_ptr_to_member_type:
18405 case DW_TAG_set_type:
18406 case DW_TAG_string_type:
18407 case DW_TAG_subroutine_type:
18408 #endif
18409 case DW_TAG_base_type:
18410 case DW_TAG_class_type:
18411 case DW_TAG_interface_type:
18412 case DW_TAG_enumeration_type:
18413 case DW_TAG_structure_type:
18414 case DW_TAG_subrange_type:
18415 case DW_TAG_typedef:
18416 case DW_TAG_union_type:
18417 return 1;
18418 default:
18419 return 0;
18420 }
18421 }
18422
18423 /* Load all DIEs that are interesting for partial symbols into memory. */
18424
18425 static struct partial_die_info *
18426 load_partial_dies (const struct die_reader_specs *reader,
18427 const gdb_byte *info_ptr, int building_psymtab)
18428 {
18429 struct dwarf2_cu *cu = reader->cu;
18430 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18431 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18432 unsigned int bytes_read;
18433 unsigned int load_all = 0;
18434 int nesting_level = 1;
18435
18436 parent_die = NULL;
18437 last_die = NULL;
18438
18439 gdb_assert (cu->per_cu != NULL);
18440 if (cu->per_cu->load_all_dies)
18441 load_all = 1;
18442
18443 cu->partial_dies
18444 = htab_create_alloc_ex (cu->header.length / 12,
18445 partial_die_hash,
18446 partial_die_eq,
18447 NULL,
18448 &cu->comp_unit_obstack,
18449 hashtab_obstack_allocate,
18450 dummy_obstack_deallocate);
18451
18452 while (1)
18453 {
18454 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18455
18456 /* A NULL abbrev means the end of a series of children. */
18457 if (abbrev == NULL)
18458 {
18459 if (--nesting_level == 0)
18460 return first_die;
18461
18462 info_ptr += bytes_read;
18463 last_die = parent_die;
18464 parent_die = parent_die->die_parent;
18465 continue;
18466 }
18467
18468 /* Check for template arguments. We never save these; if
18469 they're seen, we just mark the parent, and go on our way. */
18470 if (parent_die != NULL
18471 && cu->language == language_cplus
18472 && (abbrev->tag == DW_TAG_template_type_param
18473 || abbrev->tag == DW_TAG_template_value_param))
18474 {
18475 parent_die->has_template_arguments = 1;
18476
18477 if (!load_all)
18478 {
18479 /* We don't need a partial DIE for the template argument. */
18480 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18481 continue;
18482 }
18483 }
18484
18485 /* We only recurse into c++ subprograms looking for template arguments.
18486 Skip their other children. */
18487 if (!load_all
18488 && cu->language == language_cplus
18489 && parent_die != NULL
18490 && parent_die->tag == DW_TAG_subprogram)
18491 {
18492 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18493 continue;
18494 }
18495
18496 /* Check whether this DIE is interesting enough to save. Normally
18497 we would not be interested in members here, but there may be
18498 later variables referencing them via DW_AT_specification (for
18499 static members). */
18500 if (!load_all
18501 && !is_type_tag_for_partial (abbrev->tag)
18502 && abbrev->tag != DW_TAG_constant
18503 && abbrev->tag != DW_TAG_enumerator
18504 && abbrev->tag != DW_TAG_subprogram
18505 && abbrev->tag != DW_TAG_inlined_subroutine
18506 && abbrev->tag != DW_TAG_lexical_block
18507 && abbrev->tag != DW_TAG_variable
18508 && abbrev->tag != DW_TAG_namespace
18509 && abbrev->tag != DW_TAG_module
18510 && abbrev->tag != DW_TAG_member
18511 && abbrev->tag != DW_TAG_imported_unit
18512 && abbrev->tag != DW_TAG_imported_declaration)
18513 {
18514 /* Otherwise we skip to the next sibling, if any. */
18515 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18516 continue;
18517 }
18518
18519 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18520 abbrev);
18521
18522 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18523
18524 /* This two-pass algorithm for processing partial symbols has a
18525 high cost in cache pressure. Thus, handle some simple cases
18526 here which cover the majority of C partial symbols. DIEs
18527 which neither have specification tags in them, nor could have
18528 specification tags elsewhere pointing at them, can simply be
18529 processed and discarded.
18530
18531 This segment is also optional; scan_partial_symbols and
18532 add_partial_symbol will handle these DIEs if we chain
18533 them in normally. When compilers which do not emit large
18534 quantities of duplicate debug information are more common,
18535 this code can probably be removed. */
18536
18537 /* Any complete simple types at the top level (pretty much all
18538 of them, for a language without namespaces), can be processed
18539 directly. */
18540 if (parent_die == NULL
18541 && pdi.has_specification == 0
18542 && pdi.is_declaration == 0
18543 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18544 || pdi.tag == DW_TAG_base_type
18545 || pdi.tag == DW_TAG_subrange_type))
18546 {
18547 if (building_psymtab && pdi.name != NULL)
18548 add_psymbol_to_list (pdi.name, strlen (pdi.name), false,
18549 VAR_DOMAIN, LOC_TYPEDEF, -1,
18550 psymbol_placement::STATIC,
18551 0, cu->language, objfile);
18552 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18553 continue;
18554 }
18555
18556 /* The exception for DW_TAG_typedef with has_children above is
18557 a workaround of GCC PR debug/47510. In the case of this complaint
18558 type_name_or_error will error on such types later.
18559
18560 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18561 it could not find the child DIEs referenced later, this is checked
18562 above. In correct DWARF DW_TAG_typedef should have no children. */
18563
18564 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18565 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18566 "- DIE at %s [in module %s]"),
18567 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18568
18569 /* If we're at the second level, and we're an enumerator, and
18570 our parent has no specification (meaning possibly lives in a
18571 namespace elsewhere), then we can add the partial symbol now
18572 instead of queueing it. */
18573 if (pdi.tag == DW_TAG_enumerator
18574 && parent_die != NULL
18575 && parent_die->die_parent == NULL
18576 && parent_die->tag == DW_TAG_enumeration_type
18577 && parent_die->has_specification == 0)
18578 {
18579 if (pdi.name == NULL)
18580 complaint (_("malformed enumerator DIE ignored"));
18581 else if (building_psymtab)
18582 add_psymbol_to_list (pdi.name, strlen (pdi.name), false,
18583 VAR_DOMAIN, LOC_CONST, -1,
18584 cu->language == language_cplus
18585 ? psymbol_placement::GLOBAL
18586 : psymbol_placement::STATIC,
18587 0, cu->language, objfile);
18588
18589 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18590 continue;
18591 }
18592
18593 struct partial_die_info *part_die
18594 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18595
18596 /* We'll save this DIE so link it in. */
18597 part_die->die_parent = parent_die;
18598 part_die->die_sibling = NULL;
18599 part_die->die_child = NULL;
18600
18601 if (last_die && last_die == parent_die)
18602 last_die->die_child = part_die;
18603 else if (last_die)
18604 last_die->die_sibling = part_die;
18605
18606 last_die = part_die;
18607
18608 if (first_die == NULL)
18609 first_die = part_die;
18610
18611 /* Maybe add the DIE to the hash table. Not all DIEs that we
18612 find interesting need to be in the hash table, because we
18613 also have the parent/sibling/child chains; only those that we
18614 might refer to by offset later during partial symbol reading.
18615
18616 For now this means things that might have be the target of a
18617 DW_AT_specification, DW_AT_abstract_origin, or
18618 DW_AT_extension. DW_AT_extension will refer only to
18619 namespaces; DW_AT_abstract_origin refers to functions (and
18620 many things under the function DIE, but we do not recurse
18621 into function DIEs during partial symbol reading) and
18622 possibly variables as well; DW_AT_specification refers to
18623 declarations. Declarations ought to have the DW_AT_declaration
18624 flag. It happens that GCC forgets to put it in sometimes, but
18625 only for functions, not for types.
18626
18627 Adding more things than necessary to the hash table is harmless
18628 except for the performance cost. Adding too few will result in
18629 wasted time in find_partial_die, when we reread the compilation
18630 unit with load_all_dies set. */
18631
18632 if (load_all
18633 || abbrev->tag == DW_TAG_constant
18634 || abbrev->tag == DW_TAG_subprogram
18635 || abbrev->tag == DW_TAG_variable
18636 || abbrev->tag == DW_TAG_namespace
18637 || part_die->is_declaration)
18638 {
18639 void **slot;
18640
18641 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18642 to_underlying (part_die->sect_off),
18643 INSERT);
18644 *slot = part_die;
18645 }
18646
18647 /* For some DIEs we want to follow their children (if any). For C
18648 we have no reason to follow the children of structures; for other
18649 languages we have to, so that we can get at method physnames
18650 to infer fully qualified class names, for DW_AT_specification,
18651 and for C++ template arguments. For C++, we also look one level
18652 inside functions to find template arguments (if the name of the
18653 function does not already contain the template arguments).
18654
18655 For Ada and Fortran, we need to scan the children of subprograms
18656 and lexical blocks as well because these languages allow the
18657 definition of nested entities that could be interesting for the
18658 debugger, such as nested subprograms for instance. */
18659 if (last_die->has_children
18660 && (load_all
18661 || last_die->tag == DW_TAG_namespace
18662 || last_die->tag == DW_TAG_module
18663 || last_die->tag == DW_TAG_enumeration_type
18664 || (cu->language == language_cplus
18665 && last_die->tag == DW_TAG_subprogram
18666 && (last_die->name == NULL
18667 || strchr (last_die->name, '<') == NULL))
18668 || (cu->language != language_c
18669 && (last_die->tag == DW_TAG_class_type
18670 || last_die->tag == DW_TAG_interface_type
18671 || last_die->tag == DW_TAG_structure_type
18672 || last_die->tag == DW_TAG_union_type))
18673 || ((cu->language == language_ada
18674 || cu->language == language_fortran)
18675 && (last_die->tag == DW_TAG_subprogram
18676 || last_die->tag == DW_TAG_lexical_block))))
18677 {
18678 nesting_level++;
18679 parent_die = last_die;
18680 continue;
18681 }
18682
18683 /* Otherwise we skip to the next sibling, if any. */
18684 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18685
18686 /* Back to the top, do it again. */
18687 }
18688 }
18689
18690 partial_die_info::partial_die_info (sect_offset sect_off_,
18691 struct abbrev_info *abbrev)
18692 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18693 {
18694 }
18695
18696 /* Read a minimal amount of information into the minimal die structure.
18697 INFO_PTR should point just after the initial uleb128 of a DIE. */
18698
18699 const gdb_byte *
18700 partial_die_info::read (const struct die_reader_specs *reader,
18701 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18702 {
18703 struct dwarf2_cu *cu = reader->cu;
18704 struct dwarf2_per_objfile *dwarf2_per_objfile
18705 = cu->per_cu->dwarf2_per_objfile;
18706 unsigned int i;
18707 int has_low_pc_attr = 0;
18708 int has_high_pc_attr = 0;
18709 int high_pc_relative = 0;
18710
18711 for (i = 0; i < abbrev.num_attrs; ++i)
18712 {
18713 struct attribute attr;
18714
18715 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18716
18717 /* Store the data if it is of an attribute we want to keep in a
18718 partial symbol table. */
18719 switch (attr.name)
18720 {
18721 case DW_AT_name:
18722 switch (tag)
18723 {
18724 case DW_TAG_compile_unit:
18725 case DW_TAG_partial_unit:
18726 case DW_TAG_type_unit:
18727 /* Compilation units have a DW_AT_name that is a filename, not
18728 a source language identifier. */
18729 case DW_TAG_enumeration_type:
18730 case DW_TAG_enumerator:
18731 /* These tags always have simple identifiers already; no need
18732 to canonicalize them. */
18733 name = DW_STRING (&attr);
18734 break;
18735 default:
18736 {
18737 struct objfile *objfile = dwarf2_per_objfile->objfile;
18738
18739 name
18740 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18741 &objfile->per_bfd->storage_obstack);
18742 }
18743 break;
18744 }
18745 break;
18746 case DW_AT_linkage_name:
18747 case DW_AT_MIPS_linkage_name:
18748 /* Note that both forms of linkage name might appear. We
18749 assume they will be the same, and we only store the last
18750 one we see. */
18751 linkage_name = DW_STRING (&attr);
18752 break;
18753 case DW_AT_low_pc:
18754 has_low_pc_attr = 1;
18755 lowpc = attr_value_as_address (&attr);
18756 break;
18757 case DW_AT_high_pc:
18758 has_high_pc_attr = 1;
18759 highpc = attr_value_as_address (&attr);
18760 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18761 high_pc_relative = 1;
18762 break;
18763 case DW_AT_location:
18764 /* Support the .debug_loc offsets. */
18765 if (attr_form_is_block (&attr))
18766 {
18767 d.locdesc = DW_BLOCK (&attr);
18768 }
18769 else if (attr_form_is_section_offset (&attr))
18770 {
18771 dwarf2_complex_location_expr_complaint ();
18772 }
18773 else
18774 {
18775 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18776 "partial symbol information");
18777 }
18778 break;
18779 case DW_AT_external:
18780 is_external = DW_UNSND (&attr);
18781 break;
18782 case DW_AT_declaration:
18783 is_declaration = DW_UNSND (&attr);
18784 break;
18785 case DW_AT_type:
18786 has_type = 1;
18787 break;
18788 case DW_AT_abstract_origin:
18789 case DW_AT_specification:
18790 case DW_AT_extension:
18791 has_specification = 1;
18792 spec_offset = dwarf2_get_ref_die_offset (&attr);
18793 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18794 || cu->per_cu->is_dwz);
18795 break;
18796 case DW_AT_sibling:
18797 /* Ignore absolute siblings, they might point outside of
18798 the current compile unit. */
18799 if (attr.form == DW_FORM_ref_addr)
18800 complaint (_("ignoring absolute DW_AT_sibling"));
18801 else
18802 {
18803 const gdb_byte *buffer = reader->buffer;
18804 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18805 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18806
18807 if (sibling_ptr < info_ptr)
18808 complaint (_("DW_AT_sibling points backwards"));
18809 else if (sibling_ptr > reader->buffer_end)
18810 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18811 else
18812 sibling = sibling_ptr;
18813 }
18814 break;
18815 case DW_AT_byte_size:
18816 has_byte_size = 1;
18817 break;
18818 case DW_AT_const_value:
18819 has_const_value = 1;
18820 break;
18821 case DW_AT_calling_convention:
18822 /* DWARF doesn't provide a way to identify a program's source-level
18823 entry point. DW_AT_calling_convention attributes are only meant
18824 to describe functions' calling conventions.
18825
18826 However, because it's a necessary piece of information in
18827 Fortran, and before DWARF 4 DW_CC_program was the only
18828 piece of debugging information whose definition refers to
18829 a 'main program' at all, several compilers marked Fortran
18830 main programs with DW_CC_program --- even when those
18831 functions use the standard calling conventions.
18832
18833 Although DWARF now specifies a way to provide this
18834 information, we support this practice for backward
18835 compatibility. */
18836 if (DW_UNSND (&attr) == DW_CC_program
18837 && cu->language == language_fortran)
18838 main_subprogram = 1;
18839 break;
18840 case DW_AT_inline:
18841 if (DW_UNSND (&attr) == DW_INL_inlined
18842 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18843 may_be_inlined = 1;
18844 break;
18845
18846 case DW_AT_import:
18847 if (tag == DW_TAG_imported_unit)
18848 {
18849 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18850 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18851 || cu->per_cu->is_dwz);
18852 }
18853 break;
18854
18855 case DW_AT_main_subprogram:
18856 main_subprogram = DW_UNSND (&attr);
18857 break;
18858
18859 case DW_AT_ranges:
18860 {
18861 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18862 but that requires a full DIE, so instead we just
18863 reimplement it. */
18864 int need_ranges_base = tag != DW_TAG_compile_unit;
18865 unsigned int ranges_offset = (DW_UNSND (&attr)
18866 + (need_ranges_base
18867 ? cu->ranges_base
18868 : 0));
18869
18870 /* Value of the DW_AT_ranges attribute is the offset in the
18871 .debug_ranges section. */
18872 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18873 nullptr))
18874 has_pc_info = 1;
18875 }
18876 break;
18877
18878 default:
18879 break;
18880 }
18881 }
18882
18883 /* For Ada, if both the name and the linkage name appear, we prefer
18884 the latter. This lets "catch exception" work better, regardless
18885 of the order in which the name and linkage name were emitted.
18886 Really, though, this is just a workaround for the fact that gdb
18887 doesn't store both the name and the linkage name. */
18888 if (cu->language == language_ada && linkage_name != nullptr)
18889 name = linkage_name;
18890
18891 if (high_pc_relative)
18892 highpc += lowpc;
18893
18894 if (has_low_pc_attr && has_high_pc_attr)
18895 {
18896 /* When using the GNU linker, .gnu.linkonce. sections are used to
18897 eliminate duplicate copies of functions and vtables and such.
18898 The linker will arbitrarily choose one and discard the others.
18899 The AT_*_pc values for such functions refer to local labels in
18900 these sections. If the section from that file was discarded, the
18901 labels are not in the output, so the relocs get a value of 0.
18902 If this is a discarded function, mark the pc bounds as invalid,
18903 so that GDB will ignore it. */
18904 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18905 {
18906 struct objfile *objfile = dwarf2_per_objfile->objfile;
18907 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18908
18909 complaint (_("DW_AT_low_pc %s is zero "
18910 "for DIE at %s [in module %s]"),
18911 paddress (gdbarch, lowpc),
18912 sect_offset_str (sect_off),
18913 objfile_name (objfile));
18914 }
18915 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18916 else if (lowpc >= highpc)
18917 {
18918 struct objfile *objfile = dwarf2_per_objfile->objfile;
18919 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18920
18921 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18922 "for DIE at %s [in module %s]"),
18923 paddress (gdbarch, lowpc),
18924 paddress (gdbarch, highpc),
18925 sect_offset_str (sect_off),
18926 objfile_name (objfile));
18927 }
18928 else
18929 has_pc_info = 1;
18930 }
18931
18932 return info_ptr;
18933 }
18934
18935 /* Find a cached partial DIE at OFFSET in CU. */
18936
18937 struct partial_die_info *
18938 dwarf2_cu::find_partial_die (sect_offset sect_off)
18939 {
18940 struct partial_die_info *lookup_die = NULL;
18941 struct partial_die_info part_die (sect_off);
18942
18943 lookup_die = ((struct partial_die_info *)
18944 htab_find_with_hash (partial_dies, &part_die,
18945 to_underlying (sect_off)));
18946
18947 return lookup_die;
18948 }
18949
18950 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18951 except in the case of .debug_types DIEs which do not reference
18952 outside their CU (they do however referencing other types via
18953 DW_FORM_ref_sig8). */
18954
18955 static const struct cu_partial_die_info
18956 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18957 {
18958 struct dwarf2_per_objfile *dwarf2_per_objfile
18959 = cu->per_cu->dwarf2_per_objfile;
18960 struct objfile *objfile = dwarf2_per_objfile->objfile;
18961 struct dwarf2_per_cu_data *per_cu = NULL;
18962 struct partial_die_info *pd = NULL;
18963
18964 if (offset_in_dwz == cu->per_cu->is_dwz
18965 && offset_in_cu_p (&cu->header, sect_off))
18966 {
18967 pd = cu->find_partial_die (sect_off);
18968 if (pd != NULL)
18969 return { cu, pd };
18970 /* We missed recording what we needed.
18971 Load all dies and try again. */
18972 per_cu = cu->per_cu;
18973 }
18974 else
18975 {
18976 /* TUs don't reference other CUs/TUs (except via type signatures). */
18977 if (cu->per_cu->is_debug_types)
18978 {
18979 error (_("Dwarf Error: Type Unit at offset %s contains"
18980 " external reference to offset %s [in module %s].\n"),
18981 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18982 bfd_get_filename (objfile->obfd));
18983 }
18984 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18985 dwarf2_per_objfile);
18986
18987 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18988 load_partial_comp_unit (per_cu);
18989
18990 per_cu->cu->last_used = 0;
18991 pd = per_cu->cu->find_partial_die (sect_off);
18992 }
18993
18994 /* If we didn't find it, and not all dies have been loaded,
18995 load them all and try again. */
18996
18997 if (pd == NULL && per_cu->load_all_dies == 0)
18998 {
18999 per_cu->load_all_dies = 1;
19000
19001 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19002 THIS_CU->cu may already be in use. So we can't just free it and
19003 replace its DIEs with the ones we read in. Instead, we leave those
19004 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19005 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19006 set. */
19007 load_partial_comp_unit (per_cu);
19008
19009 pd = per_cu->cu->find_partial_die (sect_off);
19010 }
19011
19012 if (pd == NULL)
19013 internal_error (__FILE__, __LINE__,
19014 _("could not find partial DIE %s "
19015 "in cache [from module %s]\n"),
19016 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19017 return { per_cu->cu, pd };
19018 }
19019
19020 /* See if we can figure out if the class lives in a namespace. We do
19021 this by looking for a member function; its demangled name will
19022 contain namespace info, if there is any. */
19023
19024 static void
19025 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19026 struct dwarf2_cu *cu)
19027 {
19028 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19029 what template types look like, because the demangler
19030 frequently doesn't give the same name as the debug info. We
19031 could fix this by only using the demangled name to get the
19032 prefix (but see comment in read_structure_type). */
19033
19034 struct partial_die_info *real_pdi;
19035 struct partial_die_info *child_pdi;
19036
19037 /* If this DIE (this DIE's specification, if any) has a parent, then
19038 we should not do this. We'll prepend the parent's fully qualified
19039 name when we create the partial symbol. */
19040
19041 real_pdi = struct_pdi;
19042 while (real_pdi->has_specification)
19043 {
19044 auto res = find_partial_die (real_pdi->spec_offset,
19045 real_pdi->spec_is_dwz, cu);
19046 real_pdi = res.pdi;
19047 cu = res.cu;
19048 }
19049
19050 if (real_pdi->die_parent != NULL)
19051 return;
19052
19053 for (child_pdi = struct_pdi->die_child;
19054 child_pdi != NULL;
19055 child_pdi = child_pdi->die_sibling)
19056 {
19057 if (child_pdi->tag == DW_TAG_subprogram
19058 && child_pdi->linkage_name != NULL)
19059 {
19060 char *actual_class_name
19061 = language_class_name_from_physname (cu->language_defn,
19062 child_pdi->linkage_name);
19063 if (actual_class_name != NULL)
19064 {
19065 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19066 struct_pdi->name
19067 = obstack_strdup (&objfile->per_bfd->storage_obstack,
19068 actual_class_name);
19069 xfree (actual_class_name);
19070 }
19071 break;
19072 }
19073 }
19074 }
19075
19076 void
19077 partial_die_info::fixup (struct dwarf2_cu *cu)
19078 {
19079 /* Once we've fixed up a die, there's no point in doing so again.
19080 This also avoids a memory leak if we were to call
19081 guess_partial_die_structure_name multiple times. */
19082 if (fixup_called)
19083 return;
19084
19085 /* If we found a reference attribute and the DIE has no name, try
19086 to find a name in the referred to DIE. */
19087
19088 if (name == NULL && has_specification)
19089 {
19090 struct partial_die_info *spec_die;
19091
19092 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19093 spec_die = res.pdi;
19094 cu = res.cu;
19095
19096 spec_die->fixup (cu);
19097
19098 if (spec_die->name)
19099 {
19100 name = spec_die->name;
19101
19102 /* Copy DW_AT_external attribute if it is set. */
19103 if (spec_die->is_external)
19104 is_external = spec_die->is_external;
19105 }
19106 }
19107
19108 /* Set default names for some unnamed DIEs. */
19109
19110 if (name == NULL && tag == DW_TAG_namespace)
19111 name = CP_ANONYMOUS_NAMESPACE_STR;
19112
19113 /* If there is no parent die to provide a namespace, and there are
19114 children, see if we can determine the namespace from their linkage
19115 name. */
19116 if (cu->language == language_cplus
19117 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
19118 && die_parent == NULL
19119 && has_children
19120 && (tag == DW_TAG_class_type
19121 || tag == DW_TAG_structure_type
19122 || tag == DW_TAG_union_type))
19123 guess_partial_die_structure_name (this, cu);
19124
19125 /* GCC might emit a nameless struct or union that has a linkage
19126 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19127 if (name == NULL
19128 && (tag == DW_TAG_class_type
19129 || tag == DW_TAG_interface_type
19130 || tag == DW_TAG_structure_type
19131 || tag == DW_TAG_union_type)
19132 && linkage_name != NULL)
19133 {
19134 char *demangled;
19135
19136 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
19137 if (demangled)
19138 {
19139 const char *base;
19140
19141 /* Strip any leading namespaces/classes, keep only the base name.
19142 DW_AT_name for named DIEs does not contain the prefixes. */
19143 base = strrchr (demangled, ':');
19144 if (base && base > demangled && base[-1] == ':')
19145 base++;
19146 else
19147 base = demangled;
19148
19149 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19150 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
19151 xfree (demangled);
19152 }
19153 }
19154
19155 fixup_called = 1;
19156 }
19157
19158 /* Read an attribute value described by an attribute form. */
19159
19160 static const gdb_byte *
19161 read_attribute_value (const struct die_reader_specs *reader,
19162 struct attribute *attr, unsigned form,
19163 LONGEST implicit_const, const gdb_byte *info_ptr)
19164 {
19165 struct dwarf2_cu *cu = reader->cu;
19166 struct dwarf2_per_objfile *dwarf2_per_objfile
19167 = cu->per_cu->dwarf2_per_objfile;
19168 struct objfile *objfile = dwarf2_per_objfile->objfile;
19169 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19170 bfd *abfd = reader->abfd;
19171 struct comp_unit_head *cu_header = &cu->header;
19172 unsigned int bytes_read;
19173 struct dwarf_block *blk;
19174
19175 attr->form = (enum dwarf_form) form;
19176 switch (form)
19177 {
19178 case DW_FORM_ref_addr:
19179 if (cu->header.version == 2)
19180 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19181 else
19182 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19183 &cu->header, &bytes_read);
19184 info_ptr += bytes_read;
19185 break;
19186 case DW_FORM_GNU_ref_alt:
19187 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19188 info_ptr += bytes_read;
19189 break;
19190 case DW_FORM_addr:
19191 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19192 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19193 info_ptr += bytes_read;
19194 break;
19195 case DW_FORM_block2:
19196 blk = dwarf_alloc_block (cu);
19197 blk->size = read_2_bytes (abfd, info_ptr);
19198 info_ptr += 2;
19199 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19200 info_ptr += blk->size;
19201 DW_BLOCK (attr) = blk;
19202 break;
19203 case DW_FORM_block4:
19204 blk = dwarf_alloc_block (cu);
19205 blk->size = read_4_bytes (abfd, info_ptr);
19206 info_ptr += 4;
19207 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19208 info_ptr += blk->size;
19209 DW_BLOCK (attr) = blk;
19210 break;
19211 case DW_FORM_data2:
19212 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19213 info_ptr += 2;
19214 break;
19215 case DW_FORM_data4:
19216 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19217 info_ptr += 4;
19218 break;
19219 case DW_FORM_data8:
19220 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19221 info_ptr += 8;
19222 break;
19223 case DW_FORM_data16:
19224 blk = dwarf_alloc_block (cu);
19225 blk->size = 16;
19226 blk->data = read_n_bytes (abfd, info_ptr, 16);
19227 info_ptr += 16;
19228 DW_BLOCK (attr) = blk;
19229 break;
19230 case DW_FORM_sec_offset:
19231 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19232 info_ptr += bytes_read;
19233 break;
19234 case DW_FORM_string:
19235 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19236 DW_STRING_IS_CANONICAL (attr) = 0;
19237 info_ptr += bytes_read;
19238 break;
19239 case DW_FORM_strp:
19240 if (!cu->per_cu->is_dwz)
19241 {
19242 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19243 abfd, info_ptr, cu_header,
19244 &bytes_read);
19245 DW_STRING_IS_CANONICAL (attr) = 0;
19246 info_ptr += bytes_read;
19247 break;
19248 }
19249 /* FALLTHROUGH */
19250 case DW_FORM_line_strp:
19251 if (!cu->per_cu->is_dwz)
19252 {
19253 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19254 abfd, info_ptr,
19255 cu_header, &bytes_read);
19256 DW_STRING_IS_CANONICAL (attr) = 0;
19257 info_ptr += bytes_read;
19258 break;
19259 }
19260 /* FALLTHROUGH */
19261 case DW_FORM_GNU_strp_alt:
19262 {
19263 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19264 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19265 &bytes_read);
19266
19267 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19268 dwz, str_offset);
19269 DW_STRING_IS_CANONICAL (attr) = 0;
19270 info_ptr += bytes_read;
19271 }
19272 break;
19273 case DW_FORM_exprloc:
19274 case DW_FORM_block:
19275 blk = dwarf_alloc_block (cu);
19276 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19277 info_ptr += bytes_read;
19278 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19279 info_ptr += blk->size;
19280 DW_BLOCK (attr) = blk;
19281 break;
19282 case DW_FORM_block1:
19283 blk = dwarf_alloc_block (cu);
19284 blk->size = read_1_byte (abfd, info_ptr);
19285 info_ptr += 1;
19286 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19287 info_ptr += blk->size;
19288 DW_BLOCK (attr) = blk;
19289 break;
19290 case DW_FORM_data1:
19291 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19292 info_ptr += 1;
19293 break;
19294 case DW_FORM_flag:
19295 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19296 info_ptr += 1;
19297 break;
19298 case DW_FORM_flag_present:
19299 DW_UNSND (attr) = 1;
19300 break;
19301 case DW_FORM_sdata:
19302 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19303 info_ptr += bytes_read;
19304 break;
19305 case DW_FORM_udata:
19306 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19307 info_ptr += bytes_read;
19308 break;
19309 case DW_FORM_ref1:
19310 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19311 + read_1_byte (abfd, info_ptr));
19312 info_ptr += 1;
19313 break;
19314 case DW_FORM_ref2:
19315 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19316 + read_2_bytes (abfd, info_ptr));
19317 info_ptr += 2;
19318 break;
19319 case DW_FORM_ref4:
19320 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19321 + read_4_bytes (abfd, info_ptr));
19322 info_ptr += 4;
19323 break;
19324 case DW_FORM_ref8:
19325 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19326 + read_8_bytes (abfd, info_ptr));
19327 info_ptr += 8;
19328 break;
19329 case DW_FORM_ref_sig8:
19330 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19331 info_ptr += 8;
19332 break;
19333 case DW_FORM_ref_udata:
19334 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19335 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19336 info_ptr += bytes_read;
19337 break;
19338 case DW_FORM_indirect:
19339 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19340 info_ptr += bytes_read;
19341 if (form == DW_FORM_implicit_const)
19342 {
19343 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19344 info_ptr += bytes_read;
19345 }
19346 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19347 info_ptr);
19348 break;
19349 case DW_FORM_implicit_const:
19350 DW_SND (attr) = implicit_const;
19351 break;
19352 case DW_FORM_addrx:
19353 case DW_FORM_GNU_addr_index:
19354 if (reader->dwo_file == NULL)
19355 {
19356 /* For now flag a hard error.
19357 Later we can turn this into a complaint. */
19358 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19359 dwarf_form_name (form),
19360 bfd_get_filename (abfd));
19361 }
19362 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19363 info_ptr += bytes_read;
19364 break;
19365 case DW_FORM_strx:
19366 case DW_FORM_strx1:
19367 case DW_FORM_strx2:
19368 case DW_FORM_strx3:
19369 case DW_FORM_strx4:
19370 case DW_FORM_GNU_str_index:
19371 if (reader->dwo_file == NULL)
19372 {
19373 /* For now flag a hard error.
19374 Later we can turn this into a complaint if warranted. */
19375 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19376 dwarf_form_name (form),
19377 bfd_get_filename (abfd));
19378 }
19379 {
19380 ULONGEST str_index;
19381 if (form == DW_FORM_strx1)
19382 {
19383 str_index = read_1_byte (abfd, info_ptr);
19384 info_ptr += 1;
19385 }
19386 else if (form == DW_FORM_strx2)
19387 {
19388 str_index = read_2_bytes (abfd, info_ptr);
19389 info_ptr += 2;
19390 }
19391 else if (form == DW_FORM_strx3)
19392 {
19393 str_index = read_3_bytes (abfd, info_ptr);
19394 info_ptr += 3;
19395 }
19396 else if (form == DW_FORM_strx4)
19397 {
19398 str_index = read_4_bytes (abfd, info_ptr);
19399 info_ptr += 4;
19400 }
19401 else
19402 {
19403 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19404 info_ptr += bytes_read;
19405 }
19406 DW_STRING (attr) = read_str_index (reader, str_index);
19407 DW_STRING_IS_CANONICAL (attr) = 0;
19408 }
19409 break;
19410 default:
19411 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19412 dwarf_form_name (form),
19413 bfd_get_filename (abfd));
19414 }
19415
19416 /* Super hack. */
19417 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19418 attr->form = DW_FORM_GNU_ref_alt;
19419
19420 /* We have seen instances where the compiler tried to emit a byte
19421 size attribute of -1 which ended up being encoded as an unsigned
19422 0xffffffff. Although 0xffffffff is technically a valid size value,
19423 an object of this size seems pretty unlikely so we can relatively
19424 safely treat these cases as if the size attribute was invalid and
19425 treat them as zero by default. */
19426 if (attr->name == DW_AT_byte_size
19427 && form == DW_FORM_data4
19428 && DW_UNSND (attr) >= 0xffffffff)
19429 {
19430 complaint
19431 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19432 hex_string (DW_UNSND (attr)));
19433 DW_UNSND (attr) = 0;
19434 }
19435
19436 return info_ptr;
19437 }
19438
19439 /* Read an attribute described by an abbreviated attribute. */
19440
19441 static const gdb_byte *
19442 read_attribute (const struct die_reader_specs *reader,
19443 struct attribute *attr, struct attr_abbrev *abbrev,
19444 const gdb_byte *info_ptr)
19445 {
19446 attr->name = abbrev->name;
19447 return read_attribute_value (reader, attr, abbrev->form,
19448 abbrev->implicit_const, info_ptr);
19449 }
19450
19451 /* Read dwarf information from a buffer. */
19452
19453 static unsigned int
19454 read_1_byte (bfd *abfd, const gdb_byte *buf)
19455 {
19456 return bfd_get_8 (abfd, buf);
19457 }
19458
19459 static int
19460 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19461 {
19462 return bfd_get_signed_8 (abfd, buf);
19463 }
19464
19465 static unsigned int
19466 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19467 {
19468 return bfd_get_16 (abfd, buf);
19469 }
19470
19471 static int
19472 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19473 {
19474 return bfd_get_signed_16 (abfd, buf);
19475 }
19476
19477 static unsigned int
19478 read_3_bytes (bfd *abfd, const gdb_byte *buf)
19479 {
19480 unsigned int result = 0;
19481 for (int i = 0; i < 3; ++i)
19482 {
19483 unsigned char byte = bfd_get_8 (abfd, buf);
19484 buf++;
19485 result |= ((unsigned int) byte << (i * 8));
19486 }
19487 return result;
19488 }
19489
19490 static unsigned int
19491 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19492 {
19493 return bfd_get_32 (abfd, buf);
19494 }
19495
19496 static int
19497 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19498 {
19499 return bfd_get_signed_32 (abfd, buf);
19500 }
19501
19502 static ULONGEST
19503 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19504 {
19505 return bfd_get_64 (abfd, buf);
19506 }
19507
19508 static CORE_ADDR
19509 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19510 unsigned int *bytes_read)
19511 {
19512 struct comp_unit_head *cu_header = &cu->header;
19513 CORE_ADDR retval = 0;
19514
19515 if (cu_header->signed_addr_p)
19516 {
19517 switch (cu_header->addr_size)
19518 {
19519 case 2:
19520 retval = bfd_get_signed_16 (abfd, buf);
19521 break;
19522 case 4:
19523 retval = bfd_get_signed_32 (abfd, buf);
19524 break;
19525 case 8:
19526 retval = bfd_get_signed_64 (abfd, buf);
19527 break;
19528 default:
19529 internal_error (__FILE__, __LINE__,
19530 _("read_address: bad switch, signed [in module %s]"),
19531 bfd_get_filename (abfd));
19532 }
19533 }
19534 else
19535 {
19536 switch (cu_header->addr_size)
19537 {
19538 case 2:
19539 retval = bfd_get_16 (abfd, buf);
19540 break;
19541 case 4:
19542 retval = bfd_get_32 (abfd, buf);
19543 break;
19544 case 8:
19545 retval = bfd_get_64 (abfd, buf);
19546 break;
19547 default:
19548 internal_error (__FILE__, __LINE__,
19549 _("read_address: bad switch, "
19550 "unsigned [in module %s]"),
19551 bfd_get_filename (abfd));
19552 }
19553 }
19554
19555 *bytes_read = cu_header->addr_size;
19556 return retval;
19557 }
19558
19559 /* Read the initial length from a section. The (draft) DWARF 3
19560 specification allows the initial length to take up either 4 bytes
19561 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19562 bytes describe the length and all offsets will be 8 bytes in length
19563 instead of 4.
19564
19565 An older, non-standard 64-bit format is also handled by this
19566 function. The older format in question stores the initial length
19567 as an 8-byte quantity without an escape value. Lengths greater
19568 than 2^32 aren't very common which means that the initial 4 bytes
19569 is almost always zero. Since a length value of zero doesn't make
19570 sense for the 32-bit format, this initial zero can be considered to
19571 be an escape value which indicates the presence of the older 64-bit
19572 format. As written, the code can't detect (old format) lengths
19573 greater than 4GB. If it becomes necessary to handle lengths
19574 somewhat larger than 4GB, we could allow other small values (such
19575 as the non-sensical values of 1, 2, and 3) to also be used as
19576 escape values indicating the presence of the old format.
19577
19578 The value returned via bytes_read should be used to increment the
19579 relevant pointer after calling read_initial_length().
19580
19581 [ Note: read_initial_length() and read_offset() are based on the
19582 document entitled "DWARF Debugging Information Format", revision
19583 3, draft 8, dated November 19, 2001. This document was obtained
19584 from:
19585
19586 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19587
19588 This document is only a draft and is subject to change. (So beware.)
19589
19590 Details regarding the older, non-standard 64-bit format were
19591 determined empirically by examining 64-bit ELF files produced by
19592 the SGI toolchain on an IRIX 6.5 machine.
19593
19594 - Kevin, July 16, 2002
19595 ] */
19596
19597 static LONGEST
19598 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19599 {
19600 LONGEST length = bfd_get_32 (abfd, buf);
19601
19602 if (length == 0xffffffff)
19603 {
19604 length = bfd_get_64 (abfd, buf + 4);
19605 *bytes_read = 12;
19606 }
19607 else if (length == 0)
19608 {
19609 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19610 length = bfd_get_64 (abfd, buf);
19611 *bytes_read = 8;
19612 }
19613 else
19614 {
19615 *bytes_read = 4;
19616 }
19617
19618 return length;
19619 }
19620
19621 /* Cover function for read_initial_length.
19622 Returns the length of the object at BUF, and stores the size of the
19623 initial length in *BYTES_READ and stores the size that offsets will be in
19624 *OFFSET_SIZE.
19625 If the initial length size is not equivalent to that specified in
19626 CU_HEADER then issue a complaint.
19627 This is useful when reading non-comp-unit headers. */
19628
19629 static LONGEST
19630 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19631 const struct comp_unit_head *cu_header,
19632 unsigned int *bytes_read,
19633 unsigned int *offset_size)
19634 {
19635 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19636
19637 gdb_assert (cu_header->initial_length_size == 4
19638 || cu_header->initial_length_size == 8
19639 || cu_header->initial_length_size == 12);
19640
19641 if (cu_header->initial_length_size != *bytes_read)
19642 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
19643
19644 *offset_size = (*bytes_read == 4) ? 4 : 8;
19645 return length;
19646 }
19647
19648 /* Read an offset from the data stream. The size of the offset is
19649 given by cu_header->offset_size. */
19650
19651 static LONGEST
19652 read_offset (bfd *abfd, const gdb_byte *buf,
19653 const struct comp_unit_head *cu_header,
19654 unsigned int *bytes_read)
19655 {
19656 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19657
19658 *bytes_read = cu_header->offset_size;
19659 return offset;
19660 }
19661
19662 /* Read an offset from the data stream. */
19663
19664 static LONGEST
19665 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19666 {
19667 LONGEST retval = 0;
19668
19669 switch (offset_size)
19670 {
19671 case 4:
19672 retval = bfd_get_32 (abfd, buf);
19673 break;
19674 case 8:
19675 retval = bfd_get_64 (abfd, buf);
19676 break;
19677 default:
19678 internal_error (__FILE__, __LINE__,
19679 _("read_offset_1: bad switch [in module %s]"),
19680 bfd_get_filename (abfd));
19681 }
19682
19683 return retval;
19684 }
19685
19686 static const gdb_byte *
19687 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19688 {
19689 /* If the size of a host char is 8 bits, we can return a pointer
19690 to the buffer, otherwise we have to copy the data to a buffer
19691 allocated on the temporary obstack. */
19692 gdb_assert (HOST_CHAR_BIT == 8);
19693 return buf;
19694 }
19695
19696 static const char *
19697 read_direct_string (bfd *abfd, const gdb_byte *buf,
19698 unsigned int *bytes_read_ptr)
19699 {
19700 /* If the size of a host char is 8 bits, we can return a pointer
19701 to the string, otherwise we have to copy the string to a buffer
19702 allocated on the temporary obstack. */
19703 gdb_assert (HOST_CHAR_BIT == 8);
19704 if (*buf == '\0')
19705 {
19706 *bytes_read_ptr = 1;
19707 return NULL;
19708 }
19709 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19710 return (const char *) buf;
19711 }
19712
19713 /* Return pointer to string at section SECT offset STR_OFFSET with error
19714 reporting strings FORM_NAME and SECT_NAME. */
19715
19716 static const char *
19717 read_indirect_string_at_offset_from (struct objfile *objfile,
19718 bfd *abfd, LONGEST str_offset,
19719 struct dwarf2_section_info *sect,
19720 const char *form_name,
19721 const char *sect_name)
19722 {
19723 dwarf2_read_section (objfile, sect);
19724 if (sect->buffer == NULL)
19725 error (_("%s used without %s section [in module %s]"),
19726 form_name, sect_name, bfd_get_filename (abfd));
19727 if (str_offset >= sect->size)
19728 error (_("%s pointing outside of %s section [in module %s]"),
19729 form_name, sect_name, bfd_get_filename (abfd));
19730 gdb_assert (HOST_CHAR_BIT == 8);
19731 if (sect->buffer[str_offset] == '\0')
19732 return NULL;
19733 return (const char *) (sect->buffer + str_offset);
19734 }
19735
19736 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19737
19738 static const char *
19739 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19740 bfd *abfd, LONGEST str_offset)
19741 {
19742 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19743 abfd, str_offset,
19744 &dwarf2_per_objfile->str,
19745 "DW_FORM_strp", ".debug_str");
19746 }
19747
19748 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19749
19750 static const char *
19751 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19752 bfd *abfd, LONGEST str_offset)
19753 {
19754 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19755 abfd, str_offset,
19756 &dwarf2_per_objfile->line_str,
19757 "DW_FORM_line_strp",
19758 ".debug_line_str");
19759 }
19760
19761 /* Read a string at offset STR_OFFSET in the .debug_str section from
19762 the .dwz file DWZ. Throw an error if the offset is too large. If
19763 the string consists of a single NUL byte, return NULL; otherwise
19764 return a pointer to the string. */
19765
19766 static const char *
19767 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19768 LONGEST str_offset)
19769 {
19770 dwarf2_read_section (objfile, &dwz->str);
19771
19772 if (dwz->str.buffer == NULL)
19773 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19774 "section [in module %s]"),
19775 bfd_get_filename (dwz->dwz_bfd.get ()));
19776 if (str_offset >= dwz->str.size)
19777 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19778 ".debug_str section [in module %s]"),
19779 bfd_get_filename (dwz->dwz_bfd.get ()));
19780 gdb_assert (HOST_CHAR_BIT == 8);
19781 if (dwz->str.buffer[str_offset] == '\0')
19782 return NULL;
19783 return (const char *) (dwz->str.buffer + str_offset);
19784 }
19785
19786 /* Return pointer to string at .debug_str offset as read from BUF.
19787 BUF is assumed to be in a compilation unit described by CU_HEADER.
19788 Return *BYTES_READ_PTR count of bytes read from BUF. */
19789
19790 static const char *
19791 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19792 const gdb_byte *buf,
19793 const struct comp_unit_head *cu_header,
19794 unsigned int *bytes_read_ptr)
19795 {
19796 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19797
19798 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
19799 }
19800
19801 /* Return pointer to string at .debug_line_str offset as read from BUF.
19802 BUF is assumed to be in a compilation unit described by CU_HEADER.
19803 Return *BYTES_READ_PTR count of bytes read from BUF. */
19804
19805 static const char *
19806 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19807 bfd *abfd, const gdb_byte *buf,
19808 const struct comp_unit_head *cu_header,
19809 unsigned int *bytes_read_ptr)
19810 {
19811 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19812
19813 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19814 str_offset);
19815 }
19816
19817 ULONGEST
19818 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
19819 unsigned int *bytes_read_ptr)
19820 {
19821 ULONGEST result;
19822 unsigned int num_read;
19823 int shift;
19824 unsigned char byte;
19825
19826 result = 0;
19827 shift = 0;
19828 num_read = 0;
19829 while (1)
19830 {
19831 byte = bfd_get_8 (abfd, buf);
19832 buf++;
19833 num_read++;
19834 result |= ((ULONGEST) (byte & 127) << shift);
19835 if ((byte & 128) == 0)
19836 {
19837 break;
19838 }
19839 shift += 7;
19840 }
19841 *bytes_read_ptr = num_read;
19842 return result;
19843 }
19844
19845 static LONGEST
19846 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19847 unsigned int *bytes_read_ptr)
19848 {
19849 ULONGEST result;
19850 int shift, num_read;
19851 unsigned char byte;
19852
19853 result = 0;
19854 shift = 0;
19855 num_read = 0;
19856 while (1)
19857 {
19858 byte = bfd_get_8 (abfd, buf);
19859 buf++;
19860 num_read++;
19861 result |= ((ULONGEST) (byte & 127) << shift);
19862 shift += 7;
19863 if ((byte & 128) == 0)
19864 {
19865 break;
19866 }
19867 }
19868 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
19869 result |= -(((ULONGEST) 1) << shift);
19870 *bytes_read_ptr = num_read;
19871 return result;
19872 }
19873
19874 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19875 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19876 ADDR_SIZE is the size of addresses from the CU header. */
19877
19878 static CORE_ADDR
19879 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19880 unsigned int addr_index, ULONGEST addr_base, int addr_size)
19881 {
19882 struct objfile *objfile = dwarf2_per_objfile->objfile;
19883 bfd *abfd = objfile->obfd;
19884 const gdb_byte *info_ptr;
19885
19886 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19887 if (dwarf2_per_objfile->addr.buffer == NULL)
19888 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19889 objfile_name (objfile));
19890 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19891 error (_("DW_FORM_addr_index pointing outside of "
19892 ".debug_addr section [in module %s]"),
19893 objfile_name (objfile));
19894 info_ptr = (dwarf2_per_objfile->addr.buffer
19895 + addr_base + addr_index * addr_size);
19896 if (addr_size == 4)
19897 return bfd_get_32 (abfd, info_ptr);
19898 else
19899 return bfd_get_64 (abfd, info_ptr);
19900 }
19901
19902 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19903
19904 static CORE_ADDR
19905 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19906 {
19907 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19908 cu->addr_base, cu->header.addr_size);
19909 }
19910
19911 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19912
19913 static CORE_ADDR
19914 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19915 unsigned int *bytes_read)
19916 {
19917 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
19918 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19919
19920 return read_addr_index (cu, addr_index);
19921 }
19922
19923 /* Data structure to pass results from dwarf2_read_addr_index_reader
19924 back to dwarf2_read_addr_index. */
19925
19926 struct dwarf2_read_addr_index_data
19927 {
19928 ULONGEST addr_base;
19929 int addr_size;
19930 };
19931
19932 /* die_reader_func for dwarf2_read_addr_index. */
19933
19934 static void
19935 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
19936 const gdb_byte *info_ptr,
19937 struct die_info *comp_unit_die,
19938 int has_children,
19939 void *data)
19940 {
19941 struct dwarf2_cu *cu = reader->cu;
19942 struct dwarf2_read_addr_index_data *aidata =
19943 (struct dwarf2_read_addr_index_data *) data;
19944
19945 aidata->addr_base = cu->addr_base;
19946 aidata->addr_size = cu->header.addr_size;
19947 }
19948
19949 /* Given an index in .debug_addr, fetch the value.
19950 NOTE: This can be called during dwarf expression evaluation,
19951 long after the debug information has been read, and thus per_cu->cu
19952 may no longer exist. */
19953
19954 CORE_ADDR
19955 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19956 unsigned int addr_index)
19957 {
19958 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19959 struct dwarf2_cu *cu = per_cu->cu;
19960 ULONGEST addr_base;
19961 int addr_size;
19962
19963 /* We need addr_base and addr_size.
19964 If we don't have PER_CU->cu, we have to get it.
19965 Nasty, but the alternative is storing the needed info in PER_CU,
19966 which at this point doesn't seem justified: it's not clear how frequently
19967 it would get used and it would increase the size of every PER_CU.
19968 Entry points like dwarf2_per_cu_addr_size do a similar thing
19969 so we're not in uncharted territory here.
19970 Alas we need to be a bit more complicated as addr_base is contained
19971 in the DIE.
19972
19973 We don't need to read the entire CU(/TU).
19974 We just need the header and top level die.
19975
19976 IWBN to use the aging mechanism to let us lazily later discard the CU.
19977 For now we skip this optimization. */
19978
19979 if (cu != NULL)
19980 {
19981 addr_base = cu->addr_base;
19982 addr_size = cu->header.addr_size;
19983 }
19984 else
19985 {
19986 struct dwarf2_read_addr_index_data aidata;
19987
19988 /* Note: We can't use init_cutu_and_read_dies_simple here,
19989 we need addr_base. */
19990 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
19991 dwarf2_read_addr_index_reader, &aidata);
19992 addr_base = aidata.addr_base;
19993 addr_size = aidata.addr_size;
19994 }
19995
19996 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19997 addr_size);
19998 }
19999
20000 /* Given a DW_FORM_GNU_str_index or DW_FORM_strx, fetch the string.
20001 This is only used by the Fission support. */
20002
20003 static const char *
20004 read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20005 {
20006 struct dwarf2_cu *cu = reader->cu;
20007 struct dwarf2_per_objfile *dwarf2_per_objfile
20008 = cu->per_cu->dwarf2_per_objfile;
20009 struct objfile *objfile = dwarf2_per_objfile->objfile;
20010 const char *objf_name = objfile_name (objfile);
20011 bfd *abfd = objfile->obfd;
20012 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20013 struct dwarf2_section_info *str_offsets_section =
20014 &reader->dwo_file->sections.str_offsets;
20015 const gdb_byte *info_ptr;
20016 ULONGEST str_offset;
20017 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20018
20019 dwarf2_read_section (objfile, str_section);
20020 dwarf2_read_section (objfile, str_offsets_section);
20021 if (str_section->buffer == NULL)
20022 error (_("%s used without .debug_str.dwo section"
20023 " in CU at offset %s [in module %s]"),
20024 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20025 if (str_offsets_section->buffer == NULL)
20026 error (_("%s used without .debug_str_offsets.dwo section"
20027 " in CU at offset %s [in module %s]"),
20028 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20029 if (str_index * cu->header.offset_size >= str_offsets_section->size)
20030 error (_("%s pointing outside of .debug_str_offsets.dwo"
20031 " section in CU at offset %s [in module %s]"),
20032 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20033 info_ptr = (str_offsets_section->buffer
20034 + str_index * cu->header.offset_size);
20035 if (cu->header.offset_size == 4)
20036 str_offset = bfd_get_32 (abfd, info_ptr);
20037 else
20038 str_offset = bfd_get_64 (abfd, info_ptr);
20039 if (str_offset >= str_section->size)
20040 error (_("Offset from %s pointing outside of"
20041 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20042 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20043 return (const char *) (str_section->buffer + str_offset);
20044 }
20045
20046 /* Return the length of an LEB128 number in BUF. */
20047
20048 static int
20049 leb128_size (const gdb_byte *buf)
20050 {
20051 const gdb_byte *begin = buf;
20052 gdb_byte byte;
20053
20054 while (1)
20055 {
20056 byte = *buf++;
20057 if ((byte & 128) == 0)
20058 return buf - begin;
20059 }
20060 }
20061
20062 static void
20063 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20064 {
20065 switch (lang)
20066 {
20067 case DW_LANG_C89:
20068 case DW_LANG_C99:
20069 case DW_LANG_C11:
20070 case DW_LANG_C:
20071 case DW_LANG_UPC:
20072 cu->language = language_c;
20073 break;
20074 case DW_LANG_Java:
20075 case DW_LANG_C_plus_plus:
20076 case DW_LANG_C_plus_plus_11:
20077 case DW_LANG_C_plus_plus_14:
20078 cu->language = language_cplus;
20079 break;
20080 case DW_LANG_D:
20081 cu->language = language_d;
20082 break;
20083 case DW_LANG_Fortran77:
20084 case DW_LANG_Fortran90:
20085 case DW_LANG_Fortran95:
20086 case DW_LANG_Fortran03:
20087 case DW_LANG_Fortran08:
20088 cu->language = language_fortran;
20089 break;
20090 case DW_LANG_Go:
20091 cu->language = language_go;
20092 break;
20093 case DW_LANG_Mips_Assembler:
20094 cu->language = language_asm;
20095 break;
20096 case DW_LANG_Ada83:
20097 case DW_LANG_Ada95:
20098 cu->language = language_ada;
20099 break;
20100 case DW_LANG_Modula2:
20101 cu->language = language_m2;
20102 break;
20103 case DW_LANG_Pascal83:
20104 cu->language = language_pascal;
20105 break;
20106 case DW_LANG_ObjC:
20107 cu->language = language_objc;
20108 break;
20109 case DW_LANG_Rust:
20110 case DW_LANG_Rust_old:
20111 cu->language = language_rust;
20112 break;
20113 case DW_LANG_Cobol74:
20114 case DW_LANG_Cobol85:
20115 default:
20116 cu->language = language_minimal;
20117 break;
20118 }
20119 cu->language_defn = language_def (cu->language);
20120 }
20121
20122 /* Return the named attribute or NULL if not there. */
20123
20124 static struct attribute *
20125 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20126 {
20127 for (;;)
20128 {
20129 unsigned int i;
20130 struct attribute *spec = NULL;
20131
20132 for (i = 0; i < die->num_attrs; ++i)
20133 {
20134 if (die->attrs[i].name == name)
20135 return &die->attrs[i];
20136 if (die->attrs[i].name == DW_AT_specification
20137 || die->attrs[i].name == DW_AT_abstract_origin)
20138 spec = &die->attrs[i];
20139 }
20140
20141 if (!spec)
20142 break;
20143
20144 die = follow_die_ref (die, spec, &cu);
20145 }
20146
20147 return NULL;
20148 }
20149
20150 /* Return the named attribute or NULL if not there,
20151 but do not follow DW_AT_specification, etc.
20152 This is for use in contexts where we're reading .debug_types dies.
20153 Following DW_AT_specification, DW_AT_abstract_origin will take us
20154 back up the chain, and we want to go down. */
20155
20156 static struct attribute *
20157 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
20158 {
20159 unsigned int i;
20160
20161 for (i = 0; i < die->num_attrs; ++i)
20162 if (die->attrs[i].name == name)
20163 return &die->attrs[i];
20164
20165 return NULL;
20166 }
20167
20168 /* Return the string associated with a string-typed attribute, or NULL if it
20169 is either not found or is of an incorrect type. */
20170
20171 static const char *
20172 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20173 {
20174 struct attribute *attr;
20175 const char *str = NULL;
20176
20177 attr = dwarf2_attr (die, name, cu);
20178
20179 if (attr != NULL)
20180 {
20181 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
20182 || attr->form == DW_FORM_string
20183 || attr->form == DW_FORM_strx
20184 || attr->form == DW_FORM_strx1
20185 || attr->form == DW_FORM_strx2
20186 || attr->form == DW_FORM_strx3
20187 || attr->form == DW_FORM_strx4
20188 || attr->form == DW_FORM_GNU_str_index
20189 || attr->form == DW_FORM_GNU_strp_alt)
20190 str = DW_STRING (attr);
20191 else
20192 complaint (_("string type expected for attribute %s for "
20193 "DIE at %s in module %s"),
20194 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20195 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
20196 }
20197
20198 return str;
20199 }
20200
20201 /* Return the dwo name or NULL if not present. If present, it is in either
20202 DW_AT_GNU_dwo_name or DW_AT_dwo_name atrribute. */
20203 static const char *
20204 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20205 {
20206 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20207 if (dwo_name == nullptr)
20208 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20209 return dwo_name;
20210 }
20211
20212 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20213 and holds a non-zero value. This function should only be used for
20214 DW_FORM_flag or DW_FORM_flag_present attributes. */
20215
20216 static int
20217 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20218 {
20219 struct attribute *attr = dwarf2_attr (die, name, cu);
20220
20221 return (attr && DW_UNSND (attr));
20222 }
20223
20224 static int
20225 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20226 {
20227 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20228 which value is non-zero. However, we have to be careful with
20229 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20230 (via dwarf2_flag_true_p) follows this attribute. So we may
20231 end up accidently finding a declaration attribute that belongs
20232 to a different DIE referenced by the specification attribute,
20233 even though the given DIE does not have a declaration attribute. */
20234 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20235 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20236 }
20237
20238 /* Return the die giving the specification for DIE, if there is
20239 one. *SPEC_CU is the CU containing DIE on input, and the CU
20240 containing the return value on output. If there is no
20241 specification, but there is an abstract origin, that is
20242 returned. */
20243
20244 static struct die_info *
20245 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20246 {
20247 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20248 *spec_cu);
20249
20250 if (spec_attr == NULL)
20251 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20252
20253 if (spec_attr == NULL)
20254 return NULL;
20255 else
20256 return follow_die_ref (die, spec_attr, spec_cu);
20257 }
20258
20259 /* Stub for free_line_header to match void * callback types. */
20260
20261 static void
20262 free_line_header_voidp (void *arg)
20263 {
20264 struct line_header *lh = (struct line_header *) arg;
20265
20266 delete lh;
20267 }
20268
20269 void
20270 line_header::add_include_dir (const char *include_dir)
20271 {
20272 if (dwarf_line_debug >= 2)
20273 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20274 include_dirs.size () + 1, include_dir);
20275
20276 include_dirs.push_back (include_dir);
20277 }
20278
20279 void
20280 line_header::add_file_name (const char *name,
20281 dir_index d_index,
20282 unsigned int mod_time,
20283 unsigned int length)
20284 {
20285 if (dwarf_line_debug >= 2)
20286 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
20287 (unsigned) file_names.size () + 1, name);
20288
20289 file_names.emplace_back (name, d_index, mod_time, length);
20290 }
20291
20292 /* A convenience function to find the proper .debug_line section for a CU. */
20293
20294 static struct dwarf2_section_info *
20295 get_debug_line_section (struct dwarf2_cu *cu)
20296 {
20297 struct dwarf2_section_info *section;
20298 struct dwarf2_per_objfile *dwarf2_per_objfile
20299 = cu->per_cu->dwarf2_per_objfile;
20300
20301 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20302 DWO file. */
20303 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20304 section = &cu->dwo_unit->dwo_file->sections.line;
20305 else if (cu->per_cu->is_dwz)
20306 {
20307 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20308
20309 section = &dwz->line;
20310 }
20311 else
20312 section = &dwarf2_per_objfile->line;
20313
20314 return section;
20315 }
20316
20317 /* Read directory or file name entry format, starting with byte of
20318 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20319 entries count and the entries themselves in the described entry
20320 format. */
20321
20322 static void
20323 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20324 bfd *abfd, const gdb_byte **bufp,
20325 struct line_header *lh,
20326 const struct comp_unit_head *cu_header,
20327 void (*callback) (struct line_header *lh,
20328 const char *name,
20329 dir_index d_index,
20330 unsigned int mod_time,
20331 unsigned int length))
20332 {
20333 gdb_byte format_count, formati;
20334 ULONGEST data_count, datai;
20335 const gdb_byte *buf = *bufp;
20336 const gdb_byte *format_header_data;
20337 unsigned int bytes_read;
20338
20339 format_count = read_1_byte (abfd, buf);
20340 buf += 1;
20341 format_header_data = buf;
20342 for (formati = 0; formati < format_count; formati++)
20343 {
20344 read_unsigned_leb128 (abfd, buf, &bytes_read);
20345 buf += bytes_read;
20346 read_unsigned_leb128 (abfd, buf, &bytes_read);
20347 buf += bytes_read;
20348 }
20349
20350 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20351 buf += bytes_read;
20352 for (datai = 0; datai < data_count; datai++)
20353 {
20354 const gdb_byte *format = format_header_data;
20355 struct file_entry fe;
20356
20357 for (formati = 0; formati < format_count; formati++)
20358 {
20359 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20360 format += bytes_read;
20361
20362 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20363 format += bytes_read;
20364
20365 gdb::optional<const char *> string;
20366 gdb::optional<unsigned int> uint;
20367
20368 switch (form)
20369 {
20370 case DW_FORM_string:
20371 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20372 buf += bytes_read;
20373 break;
20374
20375 case DW_FORM_line_strp:
20376 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20377 abfd, buf,
20378 cu_header,
20379 &bytes_read));
20380 buf += bytes_read;
20381 break;
20382
20383 case DW_FORM_data1:
20384 uint.emplace (read_1_byte (abfd, buf));
20385 buf += 1;
20386 break;
20387
20388 case DW_FORM_data2:
20389 uint.emplace (read_2_bytes (abfd, buf));
20390 buf += 2;
20391 break;
20392
20393 case DW_FORM_data4:
20394 uint.emplace (read_4_bytes (abfd, buf));
20395 buf += 4;
20396 break;
20397
20398 case DW_FORM_data8:
20399 uint.emplace (read_8_bytes (abfd, buf));
20400 buf += 8;
20401 break;
20402
20403 case DW_FORM_udata:
20404 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20405 buf += bytes_read;
20406 break;
20407
20408 case DW_FORM_block:
20409 /* It is valid only for DW_LNCT_timestamp which is ignored by
20410 current GDB. */
20411 break;
20412 }
20413
20414 switch (content_type)
20415 {
20416 case DW_LNCT_path:
20417 if (string.has_value ())
20418 fe.name = *string;
20419 break;
20420 case DW_LNCT_directory_index:
20421 if (uint.has_value ())
20422 fe.d_index = (dir_index) *uint;
20423 break;
20424 case DW_LNCT_timestamp:
20425 if (uint.has_value ())
20426 fe.mod_time = *uint;
20427 break;
20428 case DW_LNCT_size:
20429 if (uint.has_value ())
20430 fe.length = *uint;
20431 break;
20432 case DW_LNCT_MD5:
20433 break;
20434 default:
20435 complaint (_("Unknown format content type %s"),
20436 pulongest (content_type));
20437 }
20438 }
20439
20440 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20441 }
20442
20443 *bufp = buf;
20444 }
20445
20446 /* Read the statement program header starting at OFFSET in
20447 .debug_line, or .debug_line.dwo. Return a pointer
20448 to a struct line_header, allocated using xmalloc.
20449 Returns NULL if there is a problem reading the header, e.g., if it
20450 has a version we don't understand.
20451
20452 NOTE: the strings in the include directory and file name tables of
20453 the returned object point into the dwarf line section buffer,
20454 and must not be freed. */
20455
20456 static line_header_up
20457 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20458 {
20459 const gdb_byte *line_ptr;
20460 unsigned int bytes_read, offset_size;
20461 int i;
20462 const char *cur_dir, *cur_file;
20463 struct dwarf2_section_info *section;
20464 bfd *abfd;
20465 struct dwarf2_per_objfile *dwarf2_per_objfile
20466 = cu->per_cu->dwarf2_per_objfile;
20467
20468 section = get_debug_line_section (cu);
20469 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20470 if (section->buffer == NULL)
20471 {
20472 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20473 complaint (_("missing .debug_line.dwo section"));
20474 else
20475 complaint (_("missing .debug_line section"));
20476 return 0;
20477 }
20478
20479 /* We can't do this until we know the section is non-empty.
20480 Only then do we know we have such a section. */
20481 abfd = get_section_bfd_owner (section);
20482
20483 /* Make sure that at least there's room for the total_length field.
20484 That could be 12 bytes long, but we're just going to fudge that. */
20485 if (to_underlying (sect_off) + 4 >= section->size)
20486 {
20487 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20488 return 0;
20489 }
20490
20491 line_header_up lh (new line_header ());
20492
20493 lh->sect_off = sect_off;
20494 lh->offset_in_dwz = cu->per_cu->is_dwz;
20495
20496 line_ptr = section->buffer + to_underlying (sect_off);
20497
20498 /* Read in the header. */
20499 lh->total_length =
20500 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20501 &bytes_read, &offset_size);
20502 line_ptr += bytes_read;
20503 if (line_ptr + lh->total_length > (section->buffer + section->size))
20504 {
20505 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20506 return 0;
20507 }
20508 lh->statement_program_end = line_ptr + lh->total_length;
20509 lh->version = read_2_bytes (abfd, line_ptr);
20510 line_ptr += 2;
20511 if (lh->version > 5)
20512 {
20513 /* This is a version we don't understand. The format could have
20514 changed in ways we don't handle properly so just punt. */
20515 complaint (_("unsupported version in .debug_line section"));
20516 return NULL;
20517 }
20518 if (lh->version >= 5)
20519 {
20520 gdb_byte segment_selector_size;
20521
20522 /* Skip address size. */
20523 read_1_byte (abfd, line_ptr);
20524 line_ptr += 1;
20525
20526 segment_selector_size = read_1_byte (abfd, line_ptr);
20527 line_ptr += 1;
20528 if (segment_selector_size != 0)
20529 {
20530 complaint (_("unsupported segment selector size %u "
20531 "in .debug_line section"),
20532 segment_selector_size);
20533 return NULL;
20534 }
20535 }
20536 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20537 line_ptr += offset_size;
20538 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20539 line_ptr += 1;
20540 if (lh->version >= 4)
20541 {
20542 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20543 line_ptr += 1;
20544 }
20545 else
20546 lh->maximum_ops_per_instruction = 1;
20547
20548 if (lh->maximum_ops_per_instruction == 0)
20549 {
20550 lh->maximum_ops_per_instruction = 1;
20551 complaint (_("invalid maximum_ops_per_instruction "
20552 "in `.debug_line' section"));
20553 }
20554
20555 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20556 line_ptr += 1;
20557 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20558 line_ptr += 1;
20559 lh->line_range = read_1_byte (abfd, line_ptr);
20560 line_ptr += 1;
20561 lh->opcode_base = read_1_byte (abfd, line_ptr);
20562 line_ptr += 1;
20563 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20564
20565 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20566 for (i = 1; i < lh->opcode_base; ++i)
20567 {
20568 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20569 line_ptr += 1;
20570 }
20571
20572 if (lh->version >= 5)
20573 {
20574 /* Read directory table. */
20575 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20576 &cu->header,
20577 [] (struct line_header *header, const char *name,
20578 dir_index d_index, unsigned int mod_time,
20579 unsigned int length)
20580 {
20581 header->add_include_dir (name);
20582 });
20583
20584 /* Read file name table. */
20585 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20586 &cu->header,
20587 [] (struct line_header *header, const char *name,
20588 dir_index d_index, unsigned int mod_time,
20589 unsigned int length)
20590 {
20591 header->add_file_name (name, d_index, mod_time, length);
20592 });
20593 }
20594 else
20595 {
20596 /* Read directory table. */
20597 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20598 {
20599 line_ptr += bytes_read;
20600 lh->add_include_dir (cur_dir);
20601 }
20602 line_ptr += bytes_read;
20603
20604 /* Read file name table. */
20605 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20606 {
20607 unsigned int mod_time, length;
20608 dir_index d_index;
20609
20610 line_ptr += bytes_read;
20611 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20612 line_ptr += bytes_read;
20613 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20614 line_ptr += bytes_read;
20615 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20616 line_ptr += bytes_read;
20617
20618 lh->add_file_name (cur_file, d_index, mod_time, length);
20619 }
20620 line_ptr += bytes_read;
20621 }
20622 lh->statement_program_start = line_ptr;
20623
20624 if (line_ptr > (section->buffer + section->size))
20625 complaint (_("line number info header doesn't "
20626 "fit in `.debug_line' section"));
20627
20628 return lh;
20629 }
20630
20631 /* Subroutine of dwarf_decode_lines to simplify it.
20632 Return the file name of the psymtab for included file FILE_INDEX
20633 in line header LH of PST.
20634 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20635 If space for the result is malloc'd, *NAME_HOLDER will be set.
20636 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20637
20638 static const char *
20639 psymtab_include_file_name (const struct line_header *lh, int file_index,
20640 const struct partial_symtab *pst,
20641 const char *comp_dir,
20642 gdb::unique_xmalloc_ptr<char> *name_holder)
20643 {
20644 const file_entry &fe = lh->file_names[file_index];
20645 const char *include_name = fe.name;
20646 const char *include_name_to_compare = include_name;
20647 const char *pst_filename;
20648 int file_is_pst;
20649
20650 const char *dir_name = fe.include_dir (lh);
20651
20652 gdb::unique_xmalloc_ptr<char> hold_compare;
20653 if (!IS_ABSOLUTE_PATH (include_name)
20654 && (dir_name != NULL || comp_dir != NULL))
20655 {
20656 /* Avoid creating a duplicate psymtab for PST.
20657 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20658 Before we do the comparison, however, we need to account
20659 for DIR_NAME and COMP_DIR.
20660 First prepend dir_name (if non-NULL). If we still don't
20661 have an absolute path prepend comp_dir (if non-NULL).
20662 However, the directory we record in the include-file's
20663 psymtab does not contain COMP_DIR (to match the
20664 corresponding symtab(s)).
20665
20666 Example:
20667
20668 bash$ cd /tmp
20669 bash$ gcc -g ./hello.c
20670 include_name = "hello.c"
20671 dir_name = "."
20672 DW_AT_comp_dir = comp_dir = "/tmp"
20673 DW_AT_name = "./hello.c"
20674
20675 */
20676
20677 if (dir_name != NULL)
20678 {
20679 name_holder->reset (concat (dir_name, SLASH_STRING,
20680 include_name, (char *) NULL));
20681 include_name = name_holder->get ();
20682 include_name_to_compare = include_name;
20683 }
20684 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20685 {
20686 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20687 include_name, (char *) NULL));
20688 include_name_to_compare = hold_compare.get ();
20689 }
20690 }
20691
20692 pst_filename = pst->filename;
20693 gdb::unique_xmalloc_ptr<char> copied_name;
20694 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20695 {
20696 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20697 pst_filename, (char *) NULL));
20698 pst_filename = copied_name.get ();
20699 }
20700
20701 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20702
20703 if (file_is_pst)
20704 return NULL;
20705 return include_name;
20706 }
20707
20708 /* State machine to track the state of the line number program. */
20709
20710 class lnp_state_machine
20711 {
20712 public:
20713 /* Initialize a machine state for the start of a line number
20714 program. */
20715 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20716 bool record_lines_p);
20717
20718 file_entry *current_file ()
20719 {
20720 /* lh->file_names is 0-based, but the file name numbers in the
20721 statement program are 1-based. */
20722 return m_line_header->file_name_at (m_file);
20723 }
20724
20725 /* Record the line in the state machine. END_SEQUENCE is true if
20726 we're processing the end of a sequence. */
20727 void record_line (bool end_sequence);
20728
20729 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20730 nop-out rest of the lines in this sequence. */
20731 void check_line_address (struct dwarf2_cu *cu,
20732 const gdb_byte *line_ptr,
20733 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20734
20735 void handle_set_discriminator (unsigned int discriminator)
20736 {
20737 m_discriminator = discriminator;
20738 m_line_has_non_zero_discriminator |= discriminator != 0;
20739 }
20740
20741 /* Handle DW_LNE_set_address. */
20742 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20743 {
20744 m_op_index = 0;
20745 address += baseaddr;
20746 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20747 }
20748
20749 /* Handle DW_LNS_advance_pc. */
20750 void handle_advance_pc (CORE_ADDR adjust);
20751
20752 /* Handle a special opcode. */
20753 void handle_special_opcode (unsigned char op_code);
20754
20755 /* Handle DW_LNS_advance_line. */
20756 void handle_advance_line (int line_delta)
20757 {
20758 advance_line (line_delta);
20759 }
20760
20761 /* Handle DW_LNS_set_file. */
20762 void handle_set_file (file_name_index file);
20763
20764 /* Handle DW_LNS_negate_stmt. */
20765 void handle_negate_stmt ()
20766 {
20767 m_is_stmt = !m_is_stmt;
20768 }
20769
20770 /* Handle DW_LNS_const_add_pc. */
20771 void handle_const_add_pc ();
20772
20773 /* Handle DW_LNS_fixed_advance_pc. */
20774 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20775 {
20776 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20777 m_op_index = 0;
20778 }
20779
20780 /* Handle DW_LNS_copy. */
20781 void handle_copy ()
20782 {
20783 record_line (false);
20784 m_discriminator = 0;
20785 }
20786
20787 /* Handle DW_LNE_end_sequence. */
20788 void handle_end_sequence ()
20789 {
20790 m_currently_recording_lines = true;
20791 }
20792
20793 private:
20794 /* Advance the line by LINE_DELTA. */
20795 void advance_line (int line_delta)
20796 {
20797 m_line += line_delta;
20798
20799 if (line_delta != 0)
20800 m_line_has_non_zero_discriminator = m_discriminator != 0;
20801 }
20802
20803 struct dwarf2_cu *m_cu;
20804
20805 gdbarch *m_gdbarch;
20806
20807 /* True if we're recording lines.
20808 Otherwise we're building partial symtabs and are just interested in
20809 finding include files mentioned by the line number program. */
20810 bool m_record_lines_p;
20811
20812 /* The line number header. */
20813 line_header *m_line_header;
20814
20815 /* These are part of the standard DWARF line number state machine,
20816 and initialized according to the DWARF spec. */
20817
20818 unsigned char m_op_index = 0;
20819 /* The line table index (1-based) of the current file. */
20820 file_name_index m_file = (file_name_index) 1;
20821 unsigned int m_line = 1;
20822
20823 /* These are initialized in the constructor. */
20824
20825 CORE_ADDR m_address;
20826 bool m_is_stmt;
20827 unsigned int m_discriminator;
20828
20829 /* Additional bits of state we need to track. */
20830
20831 /* The last file that we called dwarf2_start_subfile for.
20832 This is only used for TLLs. */
20833 unsigned int m_last_file = 0;
20834 /* The last file a line number was recorded for. */
20835 struct subfile *m_last_subfile = NULL;
20836
20837 /* When true, record the lines we decode. */
20838 bool m_currently_recording_lines = false;
20839
20840 /* The last line number that was recorded, used to coalesce
20841 consecutive entries for the same line. This can happen, for
20842 example, when discriminators are present. PR 17276. */
20843 unsigned int m_last_line = 0;
20844 bool m_line_has_non_zero_discriminator = false;
20845 };
20846
20847 void
20848 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20849 {
20850 CORE_ADDR addr_adj = (((m_op_index + adjust)
20851 / m_line_header->maximum_ops_per_instruction)
20852 * m_line_header->minimum_instruction_length);
20853 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20854 m_op_index = ((m_op_index + adjust)
20855 % m_line_header->maximum_ops_per_instruction);
20856 }
20857
20858 void
20859 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20860 {
20861 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20862 CORE_ADDR addr_adj = (((m_op_index
20863 + (adj_opcode / m_line_header->line_range))
20864 / m_line_header->maximum_ops_per_instruction)
20865 * m_line_header->minimum_instruction_length);
20866 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20867 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20868 % m_line_header->maximum_ops_per_instruction);
20869
20870 int line_delta = (m_line_header->line_base
20871 + (adj_opcode % m_line_header->line_range));
20872 advance_line (line_delta);
20873 record_line (false);
20874 m_discriminator = 0;
20875 }
20876
20877 void
20878 lnp_state_machine::handle_set_file (file_name_index file)
20879 {
20880 m_file = file;
20881
20882 const file_entry *fe = current_file ();
20883 if (fe == NULL)
20884 dwarf2_debug_line_missing_file_complaint ();
20885 else if (m_record_lines_p)
20886 {
20887 const char *dir = fe->include_dir (m_line_header);
20888
20889 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20890 m_line_has_non_zero_discriminator = m_discriminator != 0;
20891 dwarf2_start_subfile (m_cu, fe->name, dir);
20892 }
20893 }
20894
20895 void
20896 lnp_state_machine::handle_const_add_pc ()
20897 {
20898 CORE_ADDR adjust
20899 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20900
20901 CORE_ADDR addr_adj
20902 = (((m_op_index + adjust)
20903 / m_line_header->maximum_ops_per_instruction)
20904 * m_line_header->minimum_instruction_length);
20905
20906 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20907 m_op_index = ((m_op_index + adjust)
20908 % m_line_header->maximum_ops_per_instruction);
20909 }
20910
20911 /* Return non-zero if we should add LINE to the line number table.
20912 LINE is the line to add, LAST_LINE is the last line that was added,
20913 LAST_SUBFILE is the subfile for LAST_LINE.
20914 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20915 had a non-zero discriminator.
20916
20917 We have to be careful in the presence of discriminators.
20918 E.g., for this line:
20919
20920 for (i = 0; i < 100000; i++);
20921
20922 clang can emit four line number entries for that one line,
20923 each with a different discriminator.
20924 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20925
20926 However, we want gdb to coalesce all four entries into one.
20927 Otherwise the user could stepi into the middle of the line and
20928 gdb would get confused about whether the pc really was in the
20929 middle of the line.
20930
20931 Things are further complicated by the fact that two consecutive
20932 line number entries for the same line is a heuristic used by gcc
20933 to denote the end of the prologue. So we can't just discard duplicate
20934 entries, we have to be selective about it. The heuristic we use is
20935 that we only collapse consecutive entries for the same line if at least
20936 one of those entries has a non-zero discriminator. PR 17276.
20937
20938 Note: Addresses in the line number state machine can never go backwards
20939 within one sequence, thus this coalescing is ok. */
20940
20941 static int
20942 dwarf_record_line_p (struct dwarf2_cu *cu,
20943 unsigned int line, unsigned int last_line,
20944 int line_has_non_zero_discriminator,
20945 struct subfile *last_subfile)
20946 {
20947 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20948 return 1;
20949 if (line != last_line)
20950 return 1;
20951 /* Same line for the same file that we've seen already.
20952 As a last check, for pr 17276, only record the line if the line
20953 has never had a non-zero discriminator. */
20954 if (!line_has_non_zero_discriminator)
20955 return 1;
20956 return 0;
20957 }
20958
20959 /* Use the CU's builder to record line number LINE beginning at
20960 address ADDRESS in the line table of subfile SUBFILE. */
20961
20962 static void
20963 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20964 unsigned int line, CORE_ADDR address,
20965 struct dwarf2_cu *cu)
20966 {
20967 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20968
20969 if (dwarf_line_debug)
20970 {
20971 fprintf_unfiltered (gdb_stdlog,
20972 "Recording line %u, file %s, address %s\n",
20973 line, lbasename (subfile->name),
20974 paddress (gdbarch, address));
20975 }
20976
20977 if (cu != nullptr)
20978 cu->get_builder ()->record_line (subfile, line, addr);
20979 }
20980
20981 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20982 Mark the end of a set of line number records.
20983 The arguments are the same as for dwarf_record_line_1.
20984 If SUBFILE is NULL the request is ignored. */
20985
20986 static void
20987 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20988 CORE_ADDR address, struct dwarf2_cu *cu)
20989 {
20990 if (subfile == NULL)
20991 return;
20992
20993 if (dwarf_line_debug)
20994 {
20995 fprintf_unfiltered (gdb_stdlog,
20996 "Finishing current line, file %s, address %s\n",
20997 lbasename (subfile->name),
20998 paddress (gdbarch, address));
20999 }
21000
21001 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
21002 }
21003
21004 void
21005 lnp_state_machine::record_line (bool end_sequence)
21006 {
21007 if (dwarf_line_debug)
21008 {
21009 fprintf_unfiltered (gdb_stdlog,
21010 "Processing actual line %u: file %u,"
21011 " address %s, is_stmt %u, discrim %u\n",
21012 m_line, to_underlying (m_file),
21013 paddress (m_gdbarch, m_address),
21014 m_is_stmt, m_discriminator);
21015 }
21016
21017 file_entry *fe = current_file ();
21018
21019 if (fe == NULL)
21020 dwarf2_debug_line_missing_file_complaint ();
21021 /* For now we ignore lines not starting on an instruction boundary.
21022 But not when processing end_sequence for compatibility with the
21023 previous version of the code. */
21024 else if (m_op_index == 0 || end_sequence)
21025 {
21026 fe->included_p = 1;
21027 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
21028 {
21029 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
21030 || end_sequence)
21031 {
21032 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21033 m_currently_recording_lines ? m_cu : nullptr);
21034 }
21035
21036 if (!end_sequence)
21037 {
21038 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21039 m_line_has_non_zero_discriminator,
21040 m_last_subfile))
21041 {
21042 buildsym_compunit *builder = m_cu->get_builder ();
21043 dwarf_record_line_1 (m_gdbarch,
21044 builder->get_current_subfile (),
21045 m_line, m_address,
21046 m_currently_recording_lines ? m_cu : nullptr);
21047 }
21048 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21049 m_last_line = m_line;
21050 }
21051 }
21052 }
21053 }
21054
21055 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21056 line_header *lh, bool record_lines_p)
21057 {
21058 m_cu = cu;
21059 m_gdbarch = arch;
21060 m_record_lines_p = record_lines_p;
21061 m_line_header = lh;
21062
21063 m_currently_recording_lines = true;
21064
21065 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21066 was a line entry for it so that the backend has a chance to adjust it
21067 and also record it in case it needs it. This is currently used by MIPS
21068 code, cf. `mips_adjust_dwarf2_line'. */
21069 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21070 m_is_stmt = lh->default_is_stmt;
21071 m_discriminator = 0;
21072 }
21073
21074 void
21075 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21076 const gdb_byte *line_ptr,
21077 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21078 {
21079 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21080 the pc range of the CU. However, we restrict the test to only ADDRESS
21081 values of zero to preserve GDB's previous behaviour which is to handle
21082 the specific case of a function being GC'd by the linker. */
21083
21084 if (address == 0 && address < unrelocated_lowpc)
21085 {
21086 /* This line table is for a function which has been
21087 GCd by the linker. Ignore it. PR gdb/12528 */
21088
21089 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21090 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21091
21092 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21093 line_offset, objfile_name (objfile));
21094 m_currently_recording_lines = false;
21095 /* Note: m_currently_recording_lines is left as false until we see
21096 DW_LNE_end_sequence. */
21097 }
21098 }
21099
21100 /* Subroutine of dwarf_decode_lines to simplify it.
21101 Process the line number information in LH.
21102 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21103 program in order to set included_p for every referenced header. */
21104
21105 static void
21106 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21107 const int decode_for_pst_p, CORE_ADDR lowpc)
21108 {
21109 const gdb_byte *line_ptr, *extended_end;
21110 const gdb_byte *line_end;
21111 unsigned int bytes_read, extended_len;
21112 unsigned char op_code, extended_op;
21113 CORE_ADDR baseaddr;
21114 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21115 bfd *abfd = objfile->obfd;
21116 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21117 /* True if we're recording line info (as opposed to building partial
21118 symtabs and just interested in finding include files mentioned by
21119 the line number program). */
21120 bool record_lines_p = !decode_for_pst_p;
21121
21122 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21123
21124 line_ptr = lh->statement_program_start;
21125 line_end = lh->statement_program_end;
21126
21127 /* Read the statement sequences until there's nothing left. */
21128 while (line_ptr < line_end)
21129 {
21130 /* The DWARF line number program state machine. Reset the state
21131 machine at the start of each sequence. */
21132 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21133 bool end_sequence = false;
21134
21135 if (record_lines_p)
21136 {
21137 /* Start a subfile for the current file of the state
21138 machine. */
21139 const file_entry *fe = state_machine.current_file ();
21140
21141 if (fe != NULL)
21142 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21143 }
21144
21145 /* Decode the table. */
21146 while (line_ptr < line_end && !end_sequence)
21147 {
21148 op_code = read_1_byte (abfd, line_ptr);
21149 line_ptr += 1;
21150
21151 if (op_code >= lh->opcode_base)
21152 {
21153 /* Special opcode. */
21154 state_machine.handle_special_opcode (op_code);
21155 }
21156 else switch (op_code)
21157 {
21158 case DW_LNS_extended_op:
21159 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21160 &bytes_read);
21161 line_ptr += bytes_read;
21162 extended_end = line_ptr + extended_len;
21163 extended_op = read_1_byte (abfd, line_ptr);
21164 line_ptr += 1;
21165 switch (extended_op)
21166 {
21167 case DW_LNE_end_sequence:
21168 state_machine.handle_end_sequence ();
21169 end_sequence = true;
21170 break;
21171 case DW_LNE_set_address:
21172 {
21173 CORE_ADDR address
21174 = read_address (abfd, line_ptr, cu, &bytes_read);
21175 line_ptr += bytes_read;
21176
21177 state_machine.check_line_address (cu, line_ptr,
21178 lowpc - baseaddr, address);
21179 state_machine.handle_set_address (baseaddr, address);
21180 }
21181 break;
21182 case DW_LNE_define_file:
21183 {
21184 const char *cur_file;
21185 unsigned int mod_time, length;
21186 dir_index dindex;
21187
21188 cur_file = read_direct_string (abfd, line_ptr,
21189 &bytes_read);
21190 line_ptr += bytes_read;
21191 dindex = (dir_index)
21192 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21193 line_ptr += bytes_read;
21194 mod_time =
21195 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21196 line_ptr += bytes_read;
21197 length =
21198 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21199 line_ptr += bytes_read;
21200 lh->add_file_name (cur_file, dindex, mod_time, length);
21201 }
21202 break;
21203 case DW_LNE_set_discriminator:
21204 {
21205 /* The discriminator is not interesting to the
21206 debugger; just ignore it. We still need to
21207 check its value though:
21208 if there are consecutive entries for the same
21209 (non-prologue) line we want to coalesce them.
21210 PR 17276. */
21211 unsigned int discr
21212 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21213 line_ptr += bytes_read;
21214
21215 state_machine.handle_set_discriminator (discr);
21216 }
21217 break;
21218 default:
21219 complaint (_("mangled .debug_line section"));
21220 return;
21221 }
21222 /* Make sure that we parsed the extended op correctly. If e.g.
21223 we expected a different address size than the producer used,
21224 we may have read the wrong number of bytes. */
21225 if (line_ptr != extended_end)
21226 {
21227 complaint (_("mangled .debug_line section"));
21228 return;
21229 }
21230 break;
21231 case DW_LNS_copy:
21232 state_machine.handle_copy ();
21233 break;
21234 case DW_LNS_advance_pc:
21235 {
21236 CORE_ADDR adjust
21237 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21238 line_ptr += bytes_read;
21239
21240 state_machine.handle_advance_pc (adjust);
21241 }
21242 break;
21243 case DW_LNS_advance_line:
21244 {
21245 int line_delta
21246 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21247 line_ptr += bytes_read;
21248
21249 state_machine.handle_advance_line (line_delta);
21250 }
21251 break;
21252 case DW_LNS_set_file:
21253 {
21254 file_name_index file
21255 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21256 &bytes_read);
21257 line_ptr += bytes_read;
21258
21259 state_machine.handle_set_file (file);
21260 }
21261 break;
21262 case DW_LNS_set_column:
21263 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21264 line_ptr += bytes_read;
21265 break;
21266 case DW_LNS_negate_stmt:
21267 state_machine.handle_negate_stmt ();
21268 break;
21269 case DW_LNS_set_basic_block:
21270 break;
21271 /* Add to the address register of the state machine the
21272 address increment value corresponding to special opcode
21273 255. I.e., this value is scaled by the minimum
21274 instruction length since special opcode 255 would have
21275 scaled the increment. */
21276 case DW_LNS_const_add_pc:
21277 state_machine.handle_const_add_pc ();
21278 break;
21279 case DW_LNS_fixed_advance_pc:
21280 {
21281 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21282 line_ptr += 2;
21283
21284 state_machine.handle_fixed_advance_pc (addr_adj);
21285 }
21286 break;
21287 default:
21288 {
21289 /* Unknown standard opcode, ignore it. */
21290 int i;
21291
21292 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21293 {
21294 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21295 line_ptr += bytes_read;
21296 }
21297 }
21298 }
21299 }
21300
21301 if (!end_sequence)
21302 dwarf2_debug_line_missing_end_sequence_complaint ();
21303
21304 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21305 in which case we still finish recording the last line). */
21306 state_machine.record_line (true);
21307 }
21308 }
21309
21310 /* Decode the Line Number Program (LNP) for the given line_header
21311 structure and CU. The actual information extracted and the type
21312 of structures created from the LNP depends on the value of PST.
21313
21314 1. If PST is NULL, then this procedure uses the data from the program
21315 to create all necessary symbol tables, and their linetables.
21316
21317 2. If PST is not NULL, this procedure reads the program to determine
21318 the list of files included by the unit represented by PST, and
21319 builds all the associated partial symbol tables.
21320
21321 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21322 It is used for relative paths in the line table.
21323 NOTE: When processing partial symtabs (pst != NULL),
21324 comp_dir == pst->dirname.
21325
21326 NOTE: It is important that psymtabs have the same file name (via strcmp)
21327 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21328 symtab we don't use it in the name of the psymtabs we create.
21329 E.g. expand_line_sal requires this when finding psymtabs to expand.
21330 A good testcase for this is mb-inline.exp.
21331
21332 LOWPC is the lowest address in CU (or 0 if not known).
21333
21334 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21335 for its PC<->lines mapping information. Otherwise only the filename
21336 table is read in. */
21337
21338 static void
21339 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21340 struct dwarf2_cu *cu, struct partial_symtab *pst,
21341 CORE_ADDR lowpc, int decode_mapping)
21342 {
21343 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21344 const int decode_for_pst_p = (pst != NULL);
21345
21346 if (decode_mapping)
21347 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21348
21349 if (decode_for_pst_p)
21350 {
21351 int file_index;
21352
21353 /* Now that we're done scanning the Line Header Program, we can
21354 create the psymtab of each included file. */
21355 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
21356 if (lh->file_names[file_index].included_p == 1)
21357 {
21358 gdb::unique_xmalloc_ptr<char> name_holder;
21359 const char *include_name =
21360 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21361 &name_holder);
21362 if (include_name != NULL)
21363 dwarf2_create_include_psymtab (include_name, pst, objfile);
21364 }
21365 }
21366 else
21367 {
21368 /* Make sure a symtab is created for every file, even files
21369 which contain only variables (i.e. no code with associated
21370 line numbers). */
21371 buildsym_compunit *builder = cu->get_builder ();
21372 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21373 int i;
21374
21375 for (i = 0; i < lh->file_names.size (); i++)
21376 {
21377 file_entry &fe = lh->file_names[i];
21378
21379 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21380
21381 if (builder->get_current_subfile ()->symtab == NULL)
21382 {
21383 builder->get_current_subfile ()->symtab
21384 = allocate_symtab (cust,
21385 builder->get_current_subfile ()->name);
21386 }
21387 fe.symtab = builder->get_current_subfile ()->symtab;
21388 }
21389 }
21390 }
21391
21392 /* Start a subfile for DWARF. FILENAME is the name of the file and
21393 DIRNAME the name of the source directory which contains FILENAME
21394 or NULL if not known.
21395 This routine tries to keep line numbers from identical absolute and
21396 relative file names in a common subfile.
21397
21398 Using the `list' example from the GDB testsuite, which resides in
21399 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21400 of /srcdir/list0.c yields the following debugging information for list0.c:
21401
21402 DW_AT_name: /srcdir/list0.c
21403 DW_AT_comp_dir: /compdir
21404 files.files[0].name: list0.h
21405 files.files[0].dir: /srcdir
21406 files.files[1].name: list0.c
21407 files.files[1].dir: /srcdir
21408
21409 The line number information for list0.c has to end up in a single
21410 subfile, so that `break /srcdir/list0.c:1' works as expected.
21411 start_subfile will ensure that this happens provided that we pass the
21412 concatenation of files.files[1].dir and files.files[1].name as the
21413 subfile's name. */
21414
21415 static void
21416 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21417 const char *dirname)
21418 {
21419 char *copy = NULL;
21420
21421 /* In order not to lose the line information directory,
21422 we concatenate it to the filename when it makes sense.
21423 Note that the Dwarf3 standard says (speaking of filenames in line
21424 information): ``The directory index is ignored for file names
21425 that represent full path names''. Thus ignoring dirname in the
21426 `else' branch below isn't an issue. */
21427
21428 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21429 {
21430 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21431 filename = copy;
21432 }
21433
21434 cu->get_builder ()->start_subfile (filename);
21435
21436 if (copy != NULL)
21437 xfree (copy);
21438 }
21439
21440 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21441 buildsym_compunit constructor. */
21442
21443 struct compunit_symtab *
21444 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21445 CORE_ADDR low_pc)
21446 {
21447 gdb_assert (m_builder == nullptr);
21448
21449 m_builder.reset (new struct buildsym_compunit
21450 (per_cu->dwarf2_per_objfile->objfile,
21451 name, comp_dir, language, low_pc));
21452
21453 list_in_scope = get_builder ()->get_file_symbols ();
21454
21455 get_builder ()->record_debugformat ("DWARF 2");
21456 get_builder ()->record_producer (producer);
21457
21458 processing_has_namespace_info = false;
21459
21460 return get_builder ()->get_compunit_symtab ();
21461 }
21462
21463 static void
21464 var_decode_location (struct attribute *attr, struct symbol *sym,
21465 struct dwarf2_cu *cu)
21466 {
21467 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21468 struct comp_unit_head *cu_header = &cu->header;
21469
21470 /* NOTE drow/2003-01-30: There used to be a comment and some special
21471 code here to turn a symbol with DW_AT_external and a
21472 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21473 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21474 with some versions of binutils) where shared libraries could have
21475 relocations against symbols in their debug information - the
21476 minimal symbol would have the right address, but the debug info
21477 would not. It's no longer necessary, because we will explicitly
21478 apply relocations when we read in the debug information now. */
21479
21480 /* A DW_AT_location attribute with no contents indicates that a
21481 variable has been optimized away. */
21482 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21483 {
21484 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21485 return;
21486 }
21487
21488 /* Handle one degenerate form of location expression specially, to
21489 preserve GDB's previous behavior when section offsets are
21490 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21491 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21492
21493 if (attr_form_is_block (attr)
21494 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21495 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21496 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21497 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
21498 && (DW_BLOCK (attr)->size
21499 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21500 {
21501 unsigned int dummy;
21502
21503 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21504 SET_SYMBOL_VALUE_ADDRESS (sym,
21505 read_address (objfile->obfd,
21506 DW_BLOCK (attr)->data + 1,
21507 cu, &dummy));
21508 else
21509 SET_SYMBOL_VALUE_ADDRESS
21510 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21511 &dummy));
21512 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21513 fixup_symbol_section (sym, objfile);
21514 SET_SYMBOL_VALUE_ADDRESS (sym,
21515 SYMBOL_VALUE_ADDRESS (sym)
21516 + ANOFFSET (objfile->section_offsets,
21517 SYMBOL_SECTION (sym)));
21518 return;
21519 }
21520
21521 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21522 expression evaluator, and use LOC_COMPUTED only when necessary
21523 (i.e. when the value of a register or memory location is
21524 referenced, or a thread-local block, etc.). Then again, it might
21525 not be worthwhile. I'm assuming that it isn't unless performance
21526 or memory numbers show me otherwise. */
21527
21528 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21529
21530 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21531 cu->has_loclist = true;
21532 }
21533
21534 /* Given a pointer to a DWARF information entry, figure out if we need
21535 to make a symbol table entry for it, and if so, create a new entry
21536 and return a pointer to it.
21537 If TYPE is NULL, determine symbol type from the die, otherwise
21538 used the passed type.
21539 If SPACE is not NULL, use it to hold the new symbol. If it is
21540 NULL, allocate a new symbol on the objfile's obstack. */
21541
21542 static struct symbol *
21543 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21544 struct symbol *space)
21545 {
21546 struct dwarf2_per_objfile *dwarf2_per_objfile
21547 = cu->per_cu->dwarf2_per_objfile;
21548 struct objfile *objfile = dwarf2_per_objfile->objfile;
21549 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21550 struct symbol *sym = NULL;
21551 const char *name;
21552 struct attribute *attr = NULL;
21553 struct attribute *attr2 = NULL;
21554 CORE_ADDR baseaddr;
21555 struct pending **list_to_add = NULL;
21556
21557 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21558
21559 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21560
21561 name = dwarf2_name (die, cu);
21562 if (name)
21563 {
21564 const char *linkagename;
21565 int suppress_add = 0;
21566
21567 if (space)
21568 sym = space;
21569 else
21570 sym = allocate_symbol (objfile);
21571 OBJSTAT (objfile, n_syms++);
21572
21573 /* Cache this symbol's name and the name's demangled form (if any). */
21574 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
21575 linkagename = dwarf2_physname (name, die, cu);
21576 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
21577
21578 /* Fortran does not have mangling standard and the mangling does differ
21579 between gfortran, iFort etc. */
21580 if (cu->language == language_fortran
21581 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
21582 symbol_set_demangled_name (&(sym->ginfo),
21583 dwarf2_full_name (name, die, cu),
21584 NULL);
21585
21586 /* Default assumptions.
21587 Use the passed type or decode it from the die. */
21588 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21589 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21590 if (type != NULL)
21591 SYMBOL_TYPE (sym) = type;
21592 else
21593 SYMBOL_TYPE (sym) = die_type (die, cu);
21594 attr = dwarf2_attr (die,
21595 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21596 cu);
21597 if (attr)
21598 {
21599 SYMBOL_LINE (sym) = DW_UNSND (attr);
21600 }
21601
21602 attr = dwarf2_attr (die,
21603 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21604 cu);
21605 if (attr)
21606 {
21607 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21608 struct file_entry *fe;
21609
21610 if (cu->line_header != NULL)
21611 fe = cu->line_header->file_name_at (file_index);
21612 else
21613 fe = NULL;
21614
21615 if (fe == NULL)
21616 complaint (_("file index out of range"));
21617 else
21618 symbol_set_symtab (sym, fe->symtab);
21619 }
21620
21621 switch (die->tag)
21622 {
21623 case DW_TAG_label:
21624 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21625 if (attr)
21626 {
21627 CORE_ADDR addr;
21628
21629 addr = attr_value_as_address (attr);
21630 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21631 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21632 }
21633 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21634 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21635 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21636 add_symbol_to_list (sym, cu->list_in_scope);
21637 break;
21638 case DW_TAG_subprogram:
21639 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21640 finish_block. */
21641 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21642 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21643 if ((attr2 && (DW_UNSND (attr2) != 0))
21644 || cu->language == language_ada
21645 || cu->language == language_fortran)
21646 {
21647 /* Subprograms marked external are stored as a global symbol.
21648 Ada and Fortran subprograms, whether marked external or
21649 not, are always stored as a global symbol, because we want
21650 to be able to access them globally. For instance, we want
21651 to be able to break on a nested subprogram without having
21652 to specify the context. */
21653 list_to_add = cu->get_builder ()->get_global_symbols ();
21654 }
21655 else
21656 {
21657 list_to_add = cu->list_in_scope;
21658 }
21659 break;
21660 case DW_TAG_inlined_subroutine:
21661 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21662 finish_block. */
21663 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21664 SYMBOL_INLINED (sym) = 1;
21665 list_to_add = cu->list_in_scope;
21666 break;
21667 case DW_TAG_template_value_param:
21668 suppress_add = 1;
21669 /* Fall through. */
21670 case DW_TAG_constant:
21671 case DW_TAG_variable:
21672 case DW_TAG_member:
21673 /* Compilation with minimal debug info may result in
21674 variables with missing type entries. Change the
21675 misleading `void' type to something sensible. */
21676 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21677 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21678
21679 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21680 /* In the case of DW_TAG_member, we should only be called for
21681 static const members. */
21682 if (die->tag == DW_TAG_member)
21683 {
21684 /* dwarf2_add_field uses die_is_declaration,
21685 so we do the same. */
21686 gdb_assert (die_is_declaration (die, cu));
21687 gdb_assert (attr);
21688 }
21689 if (attr)
21690 {
21691 dwarf2_const_value (attr, sym, cu);
21692 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21693 if (!suppress_add)
21694 {
21695 if (attr2 && (DW_UNSND (attr2) != 0))
21696 list_to_add = cu->get_builder ()->get_global_symbols ();
21697 else
21698 list_to_add = cu->list_in_scope;
21699 }
21700 break;
21701 }
21702 attr = dwarf2_attr (die, DW_AT_location, cu);
21703 if (attr)
21704 {
21705 var_decode_location (attr, sym, cu);
21706 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21707
21708 /* Fortran explicitly imports any global symbols to the local
21709 scope by DW_TAG_common_block. */
21710 if (cu->language == language_fortran && die->parent
21711 && die->parent->tag == DW_TAG_common_block)
21712 attr2 = NULL;
21713
21714 if (SYMBOL_CLASS (sym) == LOC_STATIC
21715 && SYMBOL_VALUE_ADDRESS (sym) == 0
21716 && !dwarf2_per_objfile->has_section_at_zero)
21717 {
21718 /* When a static variable is eliminated by the linker,
21719 the corresponding debug information is not stripped
21720 out, but the variable address is set to null;
21721 do not add such variables into symbol table. */
21722 }
21723 else if (attr2 && (DW_UNSND (attr2) != 0))
21724 {
21725 if (SYMBOL_CLASS (sym) == LOC_STATIC
21726 && (objfile->flags & OBJF_MAINLINE) == 0
21727 && dwarf2_per_objfile->can_copy)
21728 {
21729 /* A global static variable might be subject to
21730 copy relocation. We first check for a local
21731 minsym, though, because maybe the symbol was
21732 marked hidden, in which case this would not
21733 apply. */
21734 bound_minimal_symbol found
21735 = (lookup_minimal_symbol_linkage
21736 (SYMBOL_LINKAGE_NAME (sym), objfile));
21737 if (found.minsym != nullptr)
21738 sym->maybe_copied = 1;
21739 }
21740
21741 /* A variable with DW_AT_external is never static,
21742 but it may be block-scoped. */
21743 list_to_add
21744 = ((cu->list_in_scope
21745 == cu->get_builder ()->get_file_symbols ())
21746 ? cu->get_builder ()->get_global_symbols ()
21747 : cu->list_in_scope);
21748 }
21749 else
21750 list_to_add = cu->list_in_scope;
21751 }
21752 else
21753 {
21754 /* We do not know the address of this symbol.
21755 If it is an external symbol and we have type information
21756 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21757 The address of the variable will then be determined from
21758 the minimal symbol table whenever the variable is
21759 referenced. */
21760 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21761
21762 /* Fortran explicitly imports any global symbols to the local
21763 scope by DW_TAG_common_block. */
21764 if (cu->language == language_fortran && die->parent
21765 && die->parent->tag == DW_TAG_common_block)
21766 {
21767 /* SYMBOL_CLASS doesn't matter here because
21768 read_common_block is going to reset it. */
21769 if (!suppress_add)
21770 list_to_add = cu->list_in_scope;
21771 }
21772 else if (attr2 && (DW_UNSND (attr2) != 0)
21773 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
21774 {
21775 /* A variable with DW_AT_external is never static, but it
21776 may be block-scoped. */
21777 list_to_add
21778 = ((cu->list_in_scope
21779 == cu->get_builder ()->get_file_symbols ())
21780 ? cu->get_builder ()->get_global_symbols ()
21781 : cu->list_in_scope);
21782
21783 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
21784 }
21785 else if (!die_is_declaration (die, cu))
21786 {
21787 /* Use the default LOC_OPTIMIZED_OUT class. */
21788 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
21789 if (!suppress_add)
21790 list_to_add = cu->list_in_scope;
21791 }
21792 }
21793 break;
21794 case DW_TAG_formal_parameter:
21795 {
21796 /* If we are inside a function, mark this as an argument. If
21797 not, we might be looking at an argument to an inlined function
21798 when we do not have enough information to show inlined frames;
21799 pretend it's a local variable in that case so that the user can
21800 still see it. */
21801 struct context_stack *curr
21802 = cu->get_builder ()->get_current_context_stack ();
21803 if (curr != nullptr && curr->name != nullptr)
21804 SYMBOL_IS_ARGUMENT (sym) = 1;
21805 attr = dwarf2_attr (die, DW_AT_location, cu);
21806 if (attr)
21807 {
21808 var_decode_location (attr, sym, cu);
21809 }
21810 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21811 if (attr)
21812 {
21813 dwarf2_const_value (attr, sym, cu);
21814 }
21815
21816 list_to_add = cu->list_in_scope;
21817 }
21818 break;
21819 case DW_TAG_unspecified_parameters:
21820 /* From varargs functions; gdb doesn't seem to have any
21821 interest in this information, so just ignore it for now.
21822 (FIXME?) */
21823 break;
21824 case DW_TAG_template_type_param:
21825 suppress_add = 1;
21826 /* Fall through. */
21827 case DW_TAG_class_type:
21828 case DW_TAG_interface_type:
21829 case DW_TAG_structure_type:
21830 case DW_TAG_union_type:
21831 case DW_TAG_set_type:
21832 case DW_TAG_enumeration_type:
21833 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21834 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21835
21836 {
21837 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21838 really ever be static objects: otherwise, if you try
21839 to, say, break of a class's method and you're in a file
21840 which doesn't mention that class, it won't work unless
21841 the check for all static symbols in lookup_symbol_aux
21842 saves you. See the OtherFileClass tests in
21843 gdb.c++/namespace.exp. */
21844
21845 if (!suppress_add)
21846 {
21847 buildsym_compunit *builder = cu->get_builder ();
21848 list_to_add
21849 = (cu->list_in_scope == builder->get_file_symbols ()
21850 && cu->language == language_cplus
21851 ? builder->get_global_symbols ()
21852 : cu->list_in_scope);
21853
21854 /* The semantics of C++ state that "struct foo {
21855 ... }" also defines a typedef for "foo". */
21856 if (cu->language == language_cplus
21857 || cu->language == language_ada
21858 || cu->language == language_d
21859 || cu->language == language_rust)
21860 {
21861 /* The symbol's name is already allocated along
21862 with this objfile, so we don't need to
21863 duplicate it for the type. */
21864 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21865 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21866 }
21867 }
21868 }
21869 break;
21870 case DW_TAG_typedef:
21871 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21872 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21873 list_to_add = cu->list_in_scope;
21874 break;
21875 case DW_TAG_base_type:
21876 case DW_TAG_subrange_type:
21877 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21878 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21879 list_to_add = cu->list_in_scope;
21880 break;
21881 case DW_TAG_enumerator:
21882 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21883 if (attr)
21884 {
21885 dwarf2_const_value (attr, sym, cu);
21886 }
21887 {
21888 /* NOTE: carlton/2003-11-10: See comment above in the
21889 DW_TAG_class_type, etc. block. */
21890
21891 list_to_add
21892 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21893 && cu->language == language_cplus
21894 ? cu->get_builder ()->get_global_symbols ()
21895 : cu->list_in_scope);
21896 }
21897 break;
21898 case DW_TAG_imported_declaration:
21899 case DW_TAG_namespace:
21900 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21901 list_to_add = cu->get_builder ()->get_global_symbols ();
21902 break;
21903 case DW_TAG_module:
21904 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21905 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21906 list_to_add = cu->get_builder ()->get_global_symbols ();
21907 break;
21908 case DW_TAG_common_block:
21909 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21910 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21911 add_symbol_to_list (sym, cu->list_in_scope);
21912 break;
21913 default:
21914 /* Not a tag we recognize. Hopefully we aren't processing
21915 trash data, but since we must specifically ignore things
21916 we don't recognize, there is nothing else we should do at
21917 this point. */
21918 complaint (_("unsupported tag: '%s'"),
21919 dwarf_tag_name (die->tag));
21920 break;
21921 }
21922
21923 if (suppress_add)
21924 {
21925 sym->hash_next = objfile->template_symbols;
21926 objfile->template_symbols = sym;
21927 list_to_add = NULL;
21928 }
21929
21930 if (list_to_add != NULL)
21931 add_symbol_to_list (sym, list_to_add);
21932
21933 /* For the benefit of old versions of GCC, check for anonymous
21934 namespaces based on the demangled name. */
21935 if (!cu->processing_has_namespace_info
21936 && cu->language == language_cplus)
21937 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21938 }
21939 return (sym);
21940 }
21941
21942 /* Given an attr with a DW_FORM_dataN value in host byte order,
21943 zero-extend it as appropriate for the symbol's type. The DWARF
21944 standard (v4) is not entirely clear about the meaning of using
21945 DW_FORM_dataN for a constant with a signed type, where the type is
21946 wider than the data. The conclusion of a discussion on the DWARF
21947 list was that this is unspecified. We choose to always zero-extend
21948 because that is the interpretation long in use by GCC. */
21949
21950 static gdb_byte *
21951 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21952 struct dwarf2_cu *cu, LONGEST *value, int bits)
21953 {
21954 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21955 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21956 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21957 LONGEST l = DW_UNSND (attr);
21958
21959 if (bits < sizeof (*value) * 8)
21960 {
21961 l &= ((LONGEST) 1 << bits) - 1;
21962 *value = l;
21963 }
21964 else if (bits == sizeof (*value) * 8)
21965 *value = l;
21966 else
21967 {
21968 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21969 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21970 return bytes;
21971 }
21972
21973 return NULL;
21974 }
21975
21976 /* Read a constant value from an attribute. Either set *VALUE, or if
21977 the value does not fit in *VALUE, set *BYTES - either already
21978 allocated on the objfile obstack, or newly allocated on OBSTACK,
21979 or, set *BATON, if we translated the constant to a location
21980 expression. */
21981
21982 static void
21983 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21984 const char *name, struct obstack *obstack,
21985 struct dwarf2_cu *cu,
21986 LONGEST *value, const gdb_byte **bytes,
21987 struct dwarf2_locexpr_baton **baton)
21988 {
21989 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21990 struct comp_unit_head *cu_header = &cu->header;
21991 struct dwarf_block *blk;
21992 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21993 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21994
21995 *value = 0;
21996 *bytes = NULL;
21997 *baton = NULL;
21998
21999 switch (attr->form)
22000 {
22001 case DW_FORM_addr:
22002 case DW_FORM_addrx:
22003 case DW_FORM_GNU_addr_index:
22004 {
22005 gdb_byte *data;
22006
22007 if (TYPE_LENGTH (type) != cu_header->addr_size)
22008 dwarf2_const_value_length_mismatch_complaint (name,
22009 cu_header->addr_size,
22010 TYPE_LENGTH (type));
22011 /* Symbols of this form are reasonably rare, so we just
22012 piggyback on the existing location code rather than writing
22013 a new implementation of symbol_computed_ops. */
22014 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22015 (*baton)->per_cu = cu->per_cu;
22016 gdb_assert ((*baton)->per_cu);
22017
22018 (*baton)->size = 2 + cu_header->addr_size;
22019 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22020 (*baton)->data = data;
22021
22022 data[0] = DW_OP_addr;
22023 store_unsigned_integer (&data[1], cu_header->addr_size,
22024 byte_order, DW_ADDR (attr));
22025 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22026 }
22027 break;
22028 case DW_FORM_string:
22029 case DW_FORM_strp:
22030 case DW_FORM_strx:
22031 case DW_FORM_GNU_str_index:
22032 case DW_FORM_GNU_strp_alt:
22033 /* DW_STRING is already allocated on the objfile obstack, point
22034 directly to it. */
22035 *bytes = (const gdb_byte *) DW_STRING (attr);
22036 break;
22037 case DW_FORM_block1:
22038 case DW_FORM_block2:
22039 case DW_FORM_block4:
22040 case DW_FORM_block:
22041 case DW_FORM_exprloc:
22042 case DW_FORM_data16:
22043 blk = DW_BLOCK (attr);
22044 if (TYPE_LENGTH (type) != blk->size)
22045 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22046 TYPE_LENGTH (type));
22047 *bytes = blk->data;
22048 break;
22049
22050 /* The DW_AT_const_value attributes are supposed to carry the
22051 symbol's value "represented as it would be on the target
22052 architecture." By the time we get here, it's already been
22053 converted to host endianness, so we just need to sign- or
22054 zero-extend it as appropriate. */
22055 case DW_FORM_data1:
22056 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22057 break;
22058 case DW_FORM_data2:
22059 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22060 break;
22061 case DW_FORM_data4:
22062 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22063 break;
22064 case DW_FORM_data8:
22065 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22066 break;
22067
22068 case DW_FORM_sdata:
22069 case DW_FORM_implicit_const:
22070 *value = DW_SND (attr);
22071 break;
22072
22073 case DW_FORM_udata:
22074 *value = DW_UNSND (attr);
22075 break;
22076
22077 default:
22078 complaint (_("unsupported const value attribute form: '%s'"),
22079 dwarf_form_name (attr->form));
22080 *value = 0;
22081 break;
22082 }
22083 }
22084
22085
22086 /* Copy constant value from an attribute to a symbol. */
22087
22088 static void
22089 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22090 struct dwarf2_cu *cu)
22091 {
22092 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22093 LONGEST value;
22094 const gdb_byte *bytes;
22095 struct dwarf2_locexpr_baton *baton;
22096
22097 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22098 SYMBOL_PRINT_NAME (sym),
22099 &objfile->objfile_obstack, cu,
22100 &value, &bytes, &baton);
22101
22102 if (baton != NULL)
22103 {
22104 SYMBOL_LOCATION_BATON (sym) = baton;
22105 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22106 }
22107 else if (bytes != NULL)
22108 {
22109 SYMBOL_VALUE_BYTES (sym) = bytes;
22110 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22111 }
22112 else
22113 {
22114 SYMBOL_VALUE (sym) = value;
22115 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22116 }
22117 }
22118
22119 /* Return the type of the die in question using its DW_AT_type attribute. */
22120
22121 static struct type *
22122 die_type (struct die_info *die, struct dwarf2_cu *cu)
22123 {
22124 struct attribute *type_attr;
22125
22126 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22127 if (!type_attr)
22128 {
22129 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22130 /* A missing DW_AT_type represents a void type. */
22131 return objfile_type (objfile)->builtin_void;
22132 }
22133
22134 return lookup_die_type (die, type_attr, cu);
22135 }
22136
22137 /* True iff CU's producer generates GNAT Ada auxiliary information
22138 that allows to find parallel types through that information instead
22139 of having to do expensive parallel lookups by type name. */
22140
22141 static int
22142 need_gnat_info (struct dwarf2_cu *cu)
22143 {
22144 /* Assume that the Ada compiler was GNAT, which always produces
22145 the auxiliary information. */
22146 return (cu->language == language_ada);
22147 }
22148
22149 /* Return the auxiliary type of the die in question using its
22150 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22151 attribute is not present. */
22152
22153 static struct type *
22154 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22155 {
22156 struct attribute *type_attr;
22157
22158 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22159 if (!type_attr)
22160 return NULL;
22161
22162 return lookup_die_type (die, type_attr, cu);
22163 }
22164
22165 /* If DIE has a descriptive_type attribute, then set the TYPE's
22166 descriptive type accordingly. */
22167
22168 static void
22169 set_descriptive_type (struct type *type, struct die_info *die,
22170 struct dwarf2_cu *cu)
22171 {
22172 struct type *descriptive_type = die_descriptive_type (die, cu);
22173
22174 if (descriptive_type)
22175 {
22176 ALLOCATE_GNAT_AUX_TYPE (type);
22177 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22178 }
22179 }
22180
22181 /* Return the containing type of the die in question using its
22182 DW_AT_containing_type attribute. */
22183
22184 static struct type *
22185 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22186 {
22187 struct attribute *type_attr;
22188 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22189
22190 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22191 if (!type_attr)
22192 error (_("Dwarf Error: Problem turning containing type into gdb type "
22193 "[in module %s]"), objfile_name (objfile));
22194
22195 return lookup_die_type (die, type_attr, cu);
22196 }
22197
22198 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22199
22200 static struct type *
22201 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22202 {
22203 struct dwarf2_per_objfile *dwarf2_per_objfile
22204 = cu->per_cu->dwarf2_per_objfile;
22205 struct objfile *objfile = dwarf2_per_objfile->objfile;
22206 char *saved;
22207
22208 std::string message
22209 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22210 objfile_name (objfile),
22211 sect_offset_str (cu->header.sect_off),
22212 sect_offset_str (die->sect_off));
22213 saved = obstack_strdup (&objfile->objfile_obstack, message);
22214
22215 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22216 }
22217
22218 /* Look up the type of DIE in CU using its type attribute ATTR.
22219 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22220 DW_AT_containing_type.
22221 If there is no type substitute an error marker. */
22222
22223 static struct type *
22224 lookup_die_type (struct die_info *die, const struct attribute *attr,
22225 struct dwarf2_cu *cu)
22226 {
22227 struct dwarf2_per_objfile *dwarf2_per_objfile
22228 = cu->per_cu->dwarf2_per_objfile;
22229 struct objfile *objfile = dwarf2_per_objfile->objfile;
22230 struct type *this_type;
22231
22232 gdb_assert (attr->name == DW_AT_type
22233 || attr->name == DW_AT_GNAT_descriptive_type
22234 || attr->name == DW_AT_containing_type);
22235
22236 /* First see if we have it cached. */
22237
22238 if (attr->form == DW_FORM_GNU_ref_alt)
22239 {
22240 struct dwarf2_per_cu_data *per_cu;
22241 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22242
22243 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22244 dwarf2_per_objfile);
22245 this_type = get_die_type_at_offset (sect_off, per_cu);
22246 }
22247 else if (attr_form_is_ref (attr))
22248 {
22249 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22250
22251 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
22252 }
22253 else if (attr->form == DW_FORM_ref_sig8)
22254 {
22255 ULONGEST signature = DW_SIGNATURE (attr);
22256
22257 return get_signatured_type (die, signature, cu);
22258 }
22259 else
22260 {
22261 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22262 " at %s [in module %s]"),
22263 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22264 objfile_name (objfile));
22265 return build_error_marker_type (cu, die);
22266 }
22267
22268 /* If not cached we need to read it in. */
22269
22270 if (this_type == NULL)
22271 {
22272 struct die_info *type_die = NULL;
22273 struct dwarf2_cu *type_cu = cu;
22274
22275 if (attr_form_is_ref (attr))
22276 type_die = follow_die_ref (die, attr, &type_cu);
22277 if (type_die == NULL)
22278 return build_error_marker_type (cu, die);
22279 /* If we find the type now, it's probably because the type came
22280 from an inter-CU reference and the type's CU got expanded before
22281 ours. */
22282 this_type = read_type_die (type_die, type_cu);
22283 }
22284
22285 /* If we still don't have a type use an error marker. */
22286
22287 if (this_type == NULL)
22288 return build_error_marker_type (cu, die);
22289
22290 return this_type;
22291 }
22292
22293 /* Return the type in DIE, CU.
22294 Returns NULL for invalid types.
22295
22296 This first does a lookup in die_type_hash,
22297 and only reads the die in if necessary.
22298
22299 NOTE: This can be called when reading in partial or full symbols. */
22300
22301 static struct type *
22302 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22303 {
22304 struct type *this_type;
22305
22306 this_type = get_die_type (die, cu);
22307 if (this_type)
22308 return this_type;
22309
22310 return read_type_die_1 (die, cu);
22311 }
22312
22313 /* Read the type in DIE, CU.
22314 Returns NULL for invalid types. */
22315
22316 static struct type *
22317 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22318 {
22319 struct type *this_type = NULL;
22320
22321 switch (die->tag)
22322 {
22323 case DW_TAG_class_type:
22324 case DW_TAG_interface_type:
22325 case DW_TAG_structure_type:
22326 case DW_TAG_union_type:
22327 this_type = read_structure_type (die, cu);
22328 break;
22329 case DW_TAG_enumeration_type:
22330 this_type = read_enumeration_type (die, cu);
22331 break;
22332 case DW_TAG_subprogram:
22333 case DW_TAG_subroutine_type:
22334 case DW_TAG_inlined_subroutine:
22335 this_type = read_subroutine_type (die, cu);
22336 break;
22337 case DW_TAG_array_type:
22338 this_type = read_array_type (die, cu);
22339 break;
22340 case DW_TAG_set_type:
22341 this_type = read_set_type (die, cu);
22342 break;
22343 case DW_TAG_pointer_type:
22344 this_type = read_tag_pointer_type (die, cu);
22345 break;
22346 case DW_TAG_ptr_to_member_type:
22347 this_type = read_tag_ptr_to_member_type (die, cu);
22348 break;
22349 case DW_TAG_reference_type:
22350 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22351 break;
22352 case DW_TAG_rvalue_reference_type:
22353 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22354 break;
22355 case DW_TAG_const_type:
22356 this_type = read_tag_const_type (die, cu);
22357 break;
22358 case DW_TAG_volatile_type:
22359 this_type = read_tag_volatile_type (die, cu);
22360 break;
22361 case DW_TAG_restrict_type:
22362 this_type = read_tag_restrict_type (die, cu);
22363 break;
22364 case DW_TAG_string_type:
22365 this_type = read_tag_string_type (die, cu);
22366 break;
22367 case DW_TAG_typedef:
22368 this_type = read_typedef (die, cu);
22369 break;
22370 case DW_TAG_subrange_type:
22371 this_type = read_subrange_type (die, cu);
22372 break;
22373 case DW_TAG_base_type:
22374 this_type = read_base_type (die, cu);
22375 break;
22376 case DW_TAG_unspecified_type:
22377 this_type = read_unspecified_type (die, cu);
22378 break;
22379 case DW_TAG_namespace:
22380 this_type = read_namespace_type (die, cu);
22381 break;
22382 case DW_TAG_module:
22383 this_type = read_module_type (die, cu);
22384 break;
22385 case DW_TAG_atomic_type:
22386 this_type = read_tag_atomic_type (die, cu);
22387 break;
22388 default:
22389 complaint (_("unexpected tag in read_type_die: '%s'"),
22390 dwarf_tag_name (die->tag));
22391 break;
22392 }
22393
22394 return this_type;
22395 }
22396
22397 /* See if we can figure out if the class lives in a namespace. We do
22398 this by looking for a member function; its demangled name will
22399 contain namespace info, if there is any.
22400 Return the computed name or NULL.
22401 Space for the result is allocated on the objfile's obstack.
22402 This is the full-die version of guess_partial_die_structure_name.
22403 In this case we know DIE has no useful parent. */
22404
22405 static char *
22406 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22407 {
22408 struct die_info *spec_die;
22409 struct dwarf2_cu *spec_cu;
22410 struct die_info *child;
22411 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22412
22413 spec_cu = cu;
22414 spec_die = die_specification (die, &spec_cu);
22415 if (spec_die != NULL)
22416 {
22417 die = spec_die;
22418 cu = spec_cu;
22419 }
22420
22421 for (child = die->child;
22422 child != NULL;
22423 child = child->sibling)
22424 {
22425 if (child->tag == DW_TAG_subprogram)
22426 {
22427 const char *linkage_name = dw2_linkage_name (child, cu);
22428
22429 if (linkage_name != NULL)
22430 {
22431 char *actual_name
22432 = language_class_name_from_physname (cu->language_defn,
22433 linkage_name);
22434 char *name = NULL;
22435
22436 if (actual_name != NULL)
22437 {
22438 const char *die_name = dwarf2_name (die, cu);
22439
22440 if (die_name != NULL
22441 && strcmp (die_name, actual_name) != 0)
22442 {
22443 /* Strip off the class name from the full name.
22444 We want the prefix. */
22445 int die_name_len = strlen (die_name);
22446 int actual_name_len = strlen (actual_name);
22447
22448 /* Test for '::' as a sanity check. */
22449 if (actual_name_len > die_name_len + 2
22450 && actual_name[actual_name_len
22451 - die_name_len - 1] == ':')
22452 name = obstack_strndup (
22453 &objfile->per_bfd->storage_obstack,
22454 actual_name, actual_name_len - die_name_len - 2);
22455 }
22456 }
22457 xfree (actual_name);
22458 return name;
22459 }
22460 }
22461 }
22462
22463 return NULL;
22464 }
22465
22466 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22467 prefix part in such case. See
22468 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22469
22470 static const char *
22471 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22472 {
22473 struct attribute *attr;
22474 const char *base;
22475
22476 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22477 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22478 return NULL;
22479
22480 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22481 return NULL;
22482
22483 attr = dw2_linkage_name_attr (die, cu);
22484 if (attr == NULL || DW_STRING (attr) == NULL)
22485 return NULL;
22486
22487 /* dwarf2_name had to be already called. */
22488 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22489
22490 /* Strip the base name, keep any leading namespaces/classes. */
22491 base = strrchr (DW_STRING (attr), ':');
22492 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22493 return "";
22494
22495 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22496 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22497 DW_STRING (attr),
22498 &base[-1] - DW_STRING (attr));
22499 }
22500
22501 /* Return the name of the namespace/class that DIE is defined within,
22502 or "" if we can't tell. The caller should not xfree the result.
22503
22504 For example, if we're within the method foo() in the following
22505 code:
22506
22507 namespace N {
22508 class C {
22509 void foo () {
22510 }
22511 };
22512 }
22513
22514 then determine_prefix on foo's die will return "N::C". */
22515
22516 static const char *
22517 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22518 {
22519 struct dwarf2_per_objfile *dwarf2_per_objfile
22520 = cu->per_cu->dwarf2_per_objfile;
22521 struct die_info *parent, *spec_die;
22522 struct dwarf2_cu *spec_cu;
22523 struct type *parent_type;
22524 const char *retval;
22525
22526 if (cu->language != language_cplus
22527 && cu->language != language_fortran && cu->language != language_d
22528 && cu->language != language_rust)
22529 return "";
22530
22531 retval = anonymous_struct_prefix (die, cu);
22532 if (retval)
22533 return retval;
22534
22535 /* We have to be careful in the presence of DW_AT_specification.
22536 For example, with GCC 3.4, given the code
22537
22538 namespace N {
22539 void foo() {
22540 // Definition of N::foo.
22541 }
22542 }
22543
22544 then we'll have a tree of DIEs like this:
22545
22546 1: DW_TAG_compile_unit
22547 2: DW_TAG_namespace // N
22548 3: DW_TAG_subprogram // declaration of N::foo
22549 4: DW_TAG_subprogram // definition of N::foo
22550 DW_AT_specification // refers to die #3
22551
22552 Thus, when processing die #4, we have to pretend that we're in
22553 the context of its DW_AT_specification, namely the contex of die
22554 #3. */
22555 spec_cu = cu;
22556 spec_die = die_specification (die, &spec_cu);
22557 if (spec_die == NULL)
22558 parent = die->parent;
22559 else
22560 {
22561 parent = spec_die->parent;
22562 cu = spec_cu;
22563 }
22564
22565 if (parent == NULL)
22566 return "";
22567 else if (parent->building_fullname)
22568 {
22569 const char *name;
22570 const char *parent_name;
22571
22572 /* It has been seen on RealView 2.2 built binaries,
22573 DW_TAG_template_type_param types actually _defined_ as
22574 children of the parent class:
22575
22576 enum E {};
22577 template class <class Enum> Class{};
22578 Class<enum E> class_e;
22579
22580 1: DW_TAG_class_type (Class)
22581 2: DW_TAG_enumeration_type (E)
22582 3: DW_TAG_enumerator (enum1:0)
22583 3: DW_TAG_enumerator (enum2:1)
22584 ...
22585 2: DW_TAG_template_type_param
22586 DW_AT_type DW_FORM_ref_udata (E)
22587
22588 Besides being broken debug info, it can put GDB into an
22589 infinite loop. Consider:
22590
22591 When we're building the full name for Class<E>, we'll start
22592 at Class, and go look over its template type parameters,
22593 finding E. We'll then try to build the full name of E, and
22594 reach here. We're now trying to build the full name of E,
22595 and look over the parent DIE for containing scope. In the
22596 broken case, if we followed the parent DIE of E, we'd again
22597 find Class, and once again go look at its template type
22598 arguments, etc., etc. Simply don't consider such parent die
22599 as source-level parent of this die (it can't be, the language
22600 doesn't allow it), and break the loop here. */
22601 name = dwarf2_name (die, cu);
22602 parent_name = dwarf2_name (parent, cu);
22603 complaint (_("template param type '%s' defined within parent '%s'"),
22604 name ? name : "<unknown>",
22605 parent_name ? parent_name : "<unknown>");
22606 return "";
22607 }
22608 else
22609 switch (parent->tag)
22610 {
22611 case DW_TAG_namespace:
22612 parent_type = read_type_die (parent, cu);
22613 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22614 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22615 Work around this problem here. */
22616 if (cu->language == language_cplus
22617 && strcmp (TYPE_NAME (parent_type), "::") == 0)
22618 return "";
22619 /* We give a name to even anonymous namespaces. */
22620 return TYPE_NAME (parent_type);
22621 case DW_TAG_class_type:
22622 case DW_TAG_interface_type:
22623 case DW_TAG_structure_type:
22624 case DW_TAG_union_type:
22625 case DW_TAG_module:
22626 parent_type = read_type_die (parent, cu);
22627 if (TYPE_NAME (parent_type) != NULL)
22628 return TYPE_NAME (parent_type);
22629 else
22630 /* An anonymous structure is only allowed non-static data
22631 members; no typedefs, no member functions, et cetera.
22632 So it does not need a prefix. */
22633 return "";
22634 case DW_TAG_compile_unit:
22635 case DW_TAG_partial_unit:
22636 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22637 if (cu->language == language_cplus
22638 && !dwarf2_per_objfile->types.empty ()
22639 && die->child != NULL
22640 && (die->tag == DW_TAG_class_type
22641 || die->tag == DW_TAG_structure_type
22642 || die->tag == DW_TAG_union_type))
22643 {
22644 char *name = guess_full_die_structure_name (die, cu);
22645 if (name != NULL)
22646 return name;
22647 }
22648 return "";
22649 case DW_TAG_subprogram:
22650 /* Nested subroutines in Fortran get a prefix with the name
22651 of the parent's subroutine. */
22652 if (cu->language == language_fortran)
22653 {
22654 if ((die->tag == DW_TAG_subprogram)
22655 && (dwarf2_name (parent, cu) != NULL))
22656 return dwarf2_name (parent, cu);
22657 }
22658 return determine_prefix (parent, cu);
22659 case DW_TAG_enumeration_type:
22660 parent_type = read_type_die (parent, cu);
22661 if (TYPE_DECLARED_CLASS (parent_type))
22662 {
22663 if (TYPE_NAME (parent_type) != NULL)
22664 return TYPE_NAME (parent_type);
22665 return "";
22666 }
22667 /* Fall through. */
22668 default:
22669 return determine_prefix (parent, cu);
22670 }
22671 }
22672
22673 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22674 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22675 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22676 an obconcat, otherwise allocate storage for the result. The CU argument is
22677 used to determine the language and hence, the appropriate separator. */
22678
22679 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22680
22681 static char *
22682 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22683 int physname, struct dwarf2_cu *cu)
22684 {
22685 const char *lead = "";
22686 const char *sep;
22687
22688 if (suffix == NULL || suffix[0] == '\0'
22689 || prefix == NULL || prefix[0] == '\0')
22690 sep = "";
22691 else if (cu->language == language_d)
22692 {
22693 /* For D, the 'main' function could be defined in any module, but it
22694 should never be prefixed. */
22695 if (strcmp (suffix, "D main") == 0)
22696 {
22697 prefix = "";
22698 sep = "";
22699 }
22700 else
22701 sep = ".";
22702 }
22703 else if (cu->language == language_fortran && physname)
22704 {
22705 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22706 DW_AT_MIPS_linkage_name is preferred and used instead. */
22707
22708 lead = "__";
22709 sep = "_MOD_";
22710 }
22711 else
22712 sep = "::";
22713
22714 if (prefix == NULL)
22715 prefix = "";
22716 if (suffix == NULL)
22717 suffix = "";
22718
22719 if (obs == NULL)
22720 {
22721 char *retval
22722 = ((char *)
22723 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22724
22725 strcpy (retval, lead);
22726 strcat (retval, prefix);
22727 strcat (retval, sep);
22728 strcat (retval, suffix);
22729 return retval;
22730 }
22731 else
22732 {
22733 /* We have an obstack. */
22734 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22735 }
22736 }
22737
22738 /* Return sibling of die, NULL if no sibling. */
22739
22740 static struct die_info *
22741 sibling_die (struct die_info *die)
22742 {
22743 return die->sibling;
22744 }
22745
22746 /* Get name of a die, return NULL if not found. */
22747
22748 static const char *
22749 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22750 struct obstack *obstack)
22751 {
22752 if (name && cu->language == language_cplus)
22753 {
22754 std::string canon_name = cp_canonicalize_string (name);
22755
22756 if (!canon_name.empty ())
22757 {
22758 if (canon_name != name)
22759 name = obstack_strdup (obstack, canon_name);
22760 }
22761 }
22762
22763 return name;
22764 }
22765
22766 /* Get name of a die, return NULL if not found.
22767 Anonymous namespaces are converted to their magic string. */
22768
22769 static const char *
22770 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22771 {
22772 struct attribute *attr;
22773 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22774
22775 attr = dwarf2_attr (die, DW_AT_name, cu);
22776 if ((!attr || !DW_STRING (attr))
22777 && die->tag != DW_TAG_namespace
22778 && die->tag != DW_TAG_class_type
22779 && die->tag != DW_TAG_interface_type
22780 && die->tag != DW_TAG_structure_type
22781 && die->tag != DW_TAG_union_type)
22782 return NULL;
22783
22784 switch (die->tag)
22785 {
22786 case DW_TAG_compile_unit:
22787 case DW_TAG_partial_unit:
22788 /* Compilation units have a DW_AT_name that is a filename, not
22789 a source language identifier. */
22790 case DW_TAG_enumeration_type:
22791 case DW_TAG_enumerator:
22792 /* These tags always have simple identifiers already; no need
22793 to canonicalize them. */
22794 return DW_STRING (attr);
22795
22796 case DW_TAG_namespace:
22797 if (attr != NULL && DW_STRING (attr) != NULL)
22798 return DW_STRING (attr);
22799 return CP_ANONYMOUS_NAMESPACE_STR;
22800
22801 case DW_TAG_class_type:
22802 case DW_TAG_interface_type:
22803 case DW_TAG_structure_type:
22804 case DW_TAG_union_type:
22805 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22806 structures or unions. These were of the form "._%d" in GCC 4.1,
22807 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22808 and GCC 4.4. We work around this problem by ignoring these. */
22809 if (attr && DW_STRING (attr)
22810 && (startswith (DW_STRING (attr), "._")
22811 || startswith (DW_STRING (attr), "<anonymous")))
22812 return NULL;
22813
22814 /* GCC might emit a nameless typedef that has a linkage name. See
22815 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22816 if (!attr || DW_STRING (attr) == NULL)
22817 {
22818 char *demangled = NULL;
22819
22820 attr = dw2_linkage_name_attr (die, cu);
22821 if (attr == NULL || DW_STRING (attr) == NULL)
22822 return NULL;
22823
22824 /* Avoid demangling DW_STRING (attr) the second time on a second
22825 call for the same DIE. */
22826 if (!DW_STRING_IS_CANONICAL (attr))
22827 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
22828
22829 if (demangled)
22830 {
22831 const char *base;
22832
22833 /* FIXME: we already did this for the partial symbol... */
22834 DW_STRING (attr)
22835 = obstack_strdup (&objfile->per_bfd->storage_obstack,
22836 demangled);
22837 DW_STRING_IS_CANONICAL (attr) = 1;
22838 xfree (demangled);
22839
22840 /* Strip any leading namespaces/classes, keep only the base name.
22841 DW_AT_name for named DIEs does not contain the prefixes. */
22842 base = strrchr (DW_STRING (attr), ':');
22843 if (base && base > DW_STRING (attr) && base[-1] == ':')
22844 return &base[1];
22845 else
22846 return DW_STRING (attr);
22847 }
22848 }
22849 break;
22850
22851 default:
22852 break;
22853 }
22854
22855 if (!DW_STRING_IS_CANONICAL (attr))
22856 {
22857 DW_STRING (attr)
22858 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22859 &objfile->per_bfd->storage_obstack);
22860 DW_STRING_IS_CANONICAL (attr) = 1;
22861 }
22862 return DW_STRING (attr);
22863 }
22864
22865 /* Return the die that this die in an extension of, or NULL if there
22866 is none. *EXT_CU is the CU containing DIE on input, and the CU
22867 containing the return value on output. */
22868
22869 static struct die_info *
22870 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22871 {
22872 struct attribute *attr;
22873
22874 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22875 if (attr == NULL)
22876 return NULL;
22877
22878 return follow_die_ref (die, attr, ext_cu);
22879 }
22880
22881 /* A convenience function that returns an "unknown" DWARF name,
22882 including the value of V. STR is the name of the entity being
22883 printed, e.g., "TAG". */
22884
22885 static const char *
22886 dwarf_unknown (const char *str, unsigned v)
22887 {
22888 char *cell = get_print_cell ();
22889 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
22890 return cell;
22891 }
22892
22893 /* Convert a DIE tag into its string name. */
22894
22895 static const char *
22896 dwarf_tag_name (unsigned tag)
22897 {
22898 const char *name = get_DW_TAG_name (tag);
22899
22900 if (name == NULL)
22901 return dwarf_unknown ("TAG", tag);
22902
22903 return name;
22904 }
22905
22906 /* Convert a DWARF attribute code into its string name. */
22907
22908 static const char *
22909 dwarf_attr_name (unsigned attr)
22910 {
22911 const char *name;
22912
22913 #ifdef MIPS /* collides with DW_AT_HP_block_index */
22914 if (attr == DW_AT_MIPS_fde)
22915 return "DW_AT_MIPS_fde";
22916 #else
22917 if (attr == DW_AT_HP_block_index)
22918 return "DW_AT_HP_block_index";
22919 #endif
22920
22921 name = get_DW_AT_name (attr);
22922
22923 if (name == NULL)
22924 return dwarf_unknown ("AT", attr);
22925
22926 return name;
22927 }
22928
22929 /* Convert a unit type to corresponding DW_UT name. */
22930
22931 static const char *
22932 dwarf_unit_type_name (int unit_type) {
22933 switch (unit_type)
22934 {
22935 case 0x01:
22936 return "DW_UT_compile (0x01)";
22937 case 0x02:
22938 return "DW_UT_type (0x02)";
22939 case 0x03:
22940 return "DW_UT_partial (0x03)";
22941 case 0x04:
22942 return "DW_UT_skeleton (0x04)";
22943 case 0x05:
22944 return "DW_UT_split_compile (0x05)";
22945 case 0x06:
22946 return "DW_UT_split_type (0x06)";
22947 case 0x80:
22948 return "DW_UT_lo_user (0x80)";
22949 case 0xff:
22950 return "DW_UT_hi_user (0xff)";
22951 default:
22952 return nullptr;
22953 }
22954 }
22955
22956 /* Convert a DWARF value form code into its string name. */
22957
22958 static const char *
22959 dwarf_form_name (unsigned form)
22960 {
22961 const char *name = get_DW_FORM_name (form);
22962
22963 if (name == NULL)
22964 return dwarf_unknown ("FORM", form);
22965
22966 return name;
22967 }
22968
22969 static const char *
22970 dwarf_bool_name (unsigned mybool)
22971 {
22972 if (mybool)
22973 return "TRUE";
22974 else
22975 return "FALSE";
22976 }
22977
22978 /* Convert a DWARF type code into its string name. */
22979
22980 static const char *
22981 dwarf_type_encoding_name (unsigned enc)
22982 {
22983 const char *name = get_DW_ATE_name (enc);
22984
22985 if (name == NULL)
22986 return dwarf_unknown ("ATE", enc);
22987
22988 return name;
22989 }
22990
22991 static void
22992 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22993 {
22994 unsigned int i;
22995
22996 print_spaces (indent, f);
22997 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22998 dwarf_tag_name (die->tag), die->abbrev,
22999 sect_offset_str (die->sect_off));
23000
23001 if (die->parent != NULL)
23002 {
23003 print_spaces (indent, f);
23004 fprintf_unfiltered (f, " parent at offset: %s\n",
23005 sect_offset_str (die->parent->sect_off));
23006 }
23007
23008 print_spaces (indent, f);
23009 fprintf_unfiltered (f, " has children: %s\n",
23010 dwarf_bool_name (die->child != NULL));
23011
23012 print_spaces (indent, f);
23013 fprintf_unfiltered (f, " attributes:\n");
23014
23015 for (i = 0; i < die->num_attrs; ++i)
23016 {
23017 print_spaces (indent, f);
23018 fprintf_unfiltered (f, " %s (%s) ",
23019 dwarf_attr_name (die->attrs[i].name),
23020 dwarf_form_name (die->attrs[i].form));
23021
23022 switch (die->attrs[i].form)
23023 {
23024 case DW_FORM_addr:
23025 case DW_FORM_addrx:
23026 case DW_FORM_GNU_addr_index:
23027 fprintf_unfiltered (f, "address: ");
23028 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
23029 break;
23030 case DW_FORM_block2:
23031 case DW_FORM_block4:
23032 case DW_FORM_block:
23033 case DW_FORM_block1:
23034 fprintf_unfiltered (f, "block: size %s",
23035 pulongest (DW_BLOCK (&die->attrs[i])->size));
23036 break;
23037 case DW_FORM_exprloc:
23038 fprintf_unfiltered (f, "expression: size %s",
23039 pulongest (DW_BLOCK (&die->attrs[i])->size));
23040 break;
23041 case DW_FORM_data16:
23042 fprintf_unfiltered (f, "constant of 16 bytes");
23043 break;
23044 case DW_FORM_ref_addr:
23045 fprintf_unfiltered (f, "ref address: ");
23046 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23047 break;
23048 case DW_FORM_GNU_ref_alt:
23049 fprintf_unfiltered (f, "alt ref address: ");
23050 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23051 break;
23052 case DW_FORM_ref1:
23053 case DW_FORM_ref2:
23054 case DW_FORM_ref4:
23055 case DW_FORM_ref8:
23056 case DW_FORM_ref_udata:
23057 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23058 (long) (DW_UNSND (&die->attrs[i])));
23059 break;
23060 case DW_FORM_data1:
23061 case DW_FORM_data2:
23062 case DW_FORM_data4:
23063 case DW_FORM_data8:
23064 case DW_FORM_udata:
23065 case DW_FORM_sdata:
23066 fprintf_unfiltered (f, "constant: %s",
23067 pulongest (DW_UNSND (&die->attrs[i])));
23068 break;
23069 case DW_FORM_sec_offset:
23070 fprintf_unfiltered (f, "section offset: %s",
23071 pulongest (DW_UNSND (&die->attrs[i])));
23072 break;
23073 case DW_FORM_ref_sig8:
23074 fprintf_unfiltered (f, "signature: %s",
23075 hex_string (DW_SIGNATURE (&die->attrs[i])));
23076 break;
23077 case DW_FORM_string:
23078 case DW_FORM_strp:
23079 case DW_FORM_line_strp:
23080 case DW_FORM_strx:
23081 case DW_FORM_GNU_str_index:
23082 case DW_FORM_GNU_strp_alt:
23083 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23084 DW_STRING (&die->attrs[i])
23085 ? DW_STRING (&die->attrs[i]) : "",
23086 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
23087 break;
23088 case DW_FORM_flag:
23089 if (DW_UNSND (&die->attrs[i]))
23090 fprintf_unfiltered (f, "flag: TRUE");
23091 else
23092 fprintf_unfiltered (f, "flag: FALSE");
23093 break;
23094 case DW_FORM_flag_present:
23095 fprintf_unfiltered (f, "flag: TRUE");
23096 break;
23097 case DW_FORM_indirect:
23098 /* The reader will have reduced the indirect form to
23099 the "base form" so this form should not occur. */
23100 fprintf_unfiltered (f,
23101 "unexpected attribute form: DW_FORM_indirect");
23102 break;
23103 case DW_FORM_implicit_const:
23104 fprintf_unfiltered (f, "constant: %s",
23105 plongest (DW_SND (&die->attrs[i])));
23106 break;
23107 default:
23108 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23109 die->attrs[i].form);
23110 break;
23111 }
23112 fprintf_unfiltered (f, "\n");
23113 }
23114 }
23115
23116 static void
23117 dump_die_for_error (struct die_info *die)
23118 {
23119 dump_die_shallow (gdb_stderr, 0, die);
23120 }
23121
23122 static void
23123 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23124 {
23125 int indent = level * 4;
23126
23127 gdb_assert (die != NULL);
23128
23129 if (level >= max_level)
23130 return;
23131
23132 dump_die_shallow (f, indent, die);
23133
23134 if (die->child != NULL)
23135 {
23136 print_spaces (indent, f);
23137 fprintf_unfiltered (f, " Children:");
23138 if (level + 1 < max_level)
23139 {
23140 fprintf_unfiltered (f, "\n");
23141 dump_die_1 (f, level + 1, max_level, die->child);
23142 }
23143 else
23144 {
23145 fprintf_unfiltered (f,
23146 " [not printed, max nesting level reached]\n");
23147 }
23148 }
23149
23150 if (die->sibling != NULL && level > 0)
23151 {
23152 dump_die_1 (f, level, max_level, die->sibling);
23153 }
23154 }
23155
23156 /* This is called from the pdie macro in gdbinit.in.
23157 It's not static so gcc will keep a copy callable from gdb. */
23158
23159 void
23160 dump_die (struct die_info *die, int max_level)
23161 {
23162 dump_die_1 (gdb_stdlog, 0, max_level, die);
23163 }
23164
23165 static void
23166 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23167 {
23168 void **slot;
23169
23170 slot = htab_find_slot_with_hash (cu->die_hash, die,
23171 to_underlying (die->sect_off),
23172 INSERT);
23173
23174 *slot = die;
23175 }
23176
23177 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23178 required kind. */
23179
23180 static sect_offset
23181 dwarf2_get_ref_die_offset (const struct attribute *attr)
23182 {
23183 if (attr_form_is_ref (attr))
23184 return (sect_offset) DW_UNSND (attr);
23185
23186 complaint (_("unsupported die ref attribute form: '%s'"),
23187 dwarf_form_name (attr->form));
23188 return {};
23189 }
23190
23191 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23192 * the value held by the attribute is not constant. */
23193
23194 static LONGEST
23195 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
23196 {
23197 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
23198 return DW_SND (attr);
23199 else if (attr->form == DW_FORM_udata
23200 || attr->form == DW_FORM_data1
23201 || attr->form == DW_FORM_data2
23202 || attr->form == DW_FORM_data4
23203 || attr->form == DW_FORM_data8)
23204 return DW_UNSND (attr);
23205 else
23206 {
23207 /* For DW_FORM_data16 see attr_form_is_constant. */
23208 complaint (_("Attribute value is not a constant (%s)"),
23209 dwarf_form_name (attr->form));
23210 return default_value;
23211 }
23212 }
23213
23214 /* Follow reference or signature attribute ATTR of SRC_DIE.
23215 On entry *REF_CU is the CU of SRC_DIE.
23216 On exit *REF_CU is the CU of the result. */
23217
23218 static struct die_info *
23219 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23220 struct dwarf2_cu **ref_cu)
23221 {
23222 struct die_info *die;
23223
23224 if (attr_form_is_ref (attr))
23225 die = follow_die_ref (src_die, attr, ref_cu);
23226 else if (attr->form == DW_FORM_ref_sig8)
23227 die = follow_die_sig (src_die, attr, ref_cu);
23228 else
23229 {
23230 dump_die_for_error (src_die);
23231 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23232 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23233 }
23234
23235 return die;
23236 }
23237
23238 /* Follow reference OFFSET.
23239 On entry *REF_CU is the CU of the source die referencing OFFSET.
23240 On exit *REF_CU is the CU of the result.
23241 Returns NULL if OFFSET is invalid. */
23242
23243 static struct die_info *
23244 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23245 struct dwarf2_cu **ref_cu)
23246 {
23247 struct die_info temp_die;
23248 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23249 struct dwarf2_per_objfile *dwarf2_per_objfile
23250 = cu->per_cu->dwarf2_per_objfile;
23251
23252 gdb_assert (cu->per_cu != NULL);
23253
23254 target_cu = cu;
23255
23256 if (cu->per_cu->is_debug_types)
23257 {
23258 /* .debug_types CUs cannot reference anything outside their CU.
23259 If they need to, they have to reference a signatured type via
23260 DW_FORM_ref_sig8. */
23261 if (!offset_in_cu_p (&cu->header, sect_off))
23262 return NULL;
23263 }
23264 else if (offset_in_dwz != cu->per_cu->is_dwz
23265 || !offset_in_cu_p (&cu->header, sect_off))
23266 {
23267 struct dwarf2_per_cu_data *per_cu;
23268
23269 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23270 dwarf2_per_objfile);
23271
23272 /* If necessary, add it to the queue and load its DIEs. */
23273 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23274 load_full_comp_unit (per_cu, false, cu->language);
23275
23276 target_cu = per_cu->cu;
23277 }
23278 else if (cu->dies == NULL)
23279 {
23280 /* We're loading full DIEs during partial symbol reading. */
23281 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23282 load_full_comp_unit (cu->per_cu, false, language_minimal);
23283 }
23284
23285 *ref_cu = target_cu;
23286 temp_die.sect_off = sect_off;
23287
23288 if (target_cu != cu)
23289 target_cu->ancestor = cu;
23290
23291 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23292 &temp_die,
23293 to_underlying (sect_off));
23294 }
23295
23296 /* Follow reference attribute ATTR of SRC_DIE.
23297 On entry *REF_CU is the CU of SRC_DIE.
23298 On exit *REF_CU is the CU of the result. */
23299
23300 static struct die_info *
23301 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23302 struct dwarf2_cu **ref_cu)
23303 {
23304 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23305 struct dwarf2_cu *cu = *ref_cu;
23306 struct die_info *die;
23307
23308 die = follow_die_offset (sect_off,
23309 (attr->form == DW_FORM_GNU_ref_alt
23310 || cu->per_cu->is_dwz),
23311 ref_cu);
23312 if (!die)
23313 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23314 "at %s [in module %s]"),
23315 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23316 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23317
23318 return die;
23319 }
23320
23321 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23322 Returned value is intended for DW_OP_call*. Returned
23323 dwarf2_locexpr_baton->data has lifetime of
23324 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23325
23326 struct dwarf2_locexpr_baton
23327 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23328 struct dwarf2_per_cu_data *per_cu,
23329 CORE_ADDR (*get_frame_pc) (void *baton),
23330 void *baton, bool resolve_abstract_p)
23331 {
23332 struct dwarf2_cu *cu;
23333 struct die_info *die;
23334 struct attribute *attr;
23335 struct dwarf2_locexpr_baton retval;
23336 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23337 struct objfile *objfile = dwarf2_per_objfile->objfile;
23338
23339 if (per_cu->cu == NULL)
23340 load_cu (per_cu, false);
23341 cu = per_cu->cu;
23342 if (cu == NULL)
23343 {
23344 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23345 Instead just throw an error, not much else we can do. */
23346 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23347 sect_offset_str (sect_off), objfile_name (objfile));
23348 }
23349
23350 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23351 if (!die)
23352 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23353 sect_offset_str (sect_off), objfile_name (objfile));
23354
23355 attr = dwarf2_attr (die, DW_AT_location, cu);
23356 if (!attr && resolve_abstract_p
23357 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
23358 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23359 {
23360 CORE_ADDR pc = (*get_frame_pc) (baton);
23361 CORE_ADDR baseaddr
23362 = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23363 struct gdbarch *gdbarch = get_objfile_arch (objfile);
23364
23365 for (const auto &cand_off
23366 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
23367 {
23368 struct dwarf2_cu *cand_cu = cu;
23369 struct die_info *cand
23370 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23371 if (!cand
23372 || !cand->parent
23373 || cand->parent->tag != DW_TAG_subprogram)
23374 continue;
23375
23376 CORE_ADDR pc_low, pc_high;
23377 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23378 if (pc_low == ((CORE_ADDR) -1))
23379 continue;
23380 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23381 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23382 if (!(pc_low <= pc && pc < pc_high))
23383 continue;
23384
23385 die = cand;
23386 attr = dwarf2_attr (die, DW_AT_location, cu);
23387 break;
23388 }
23389 }
23390
23391 if (!attr)
23392 {
23393 /* DWARF: "If there is no such attribute, then there is no effect.".
23394 DATA is ignored if SIZE is 0. */
23395
23396 retval.data = NULL;
23397 retval.size = 0;
23398 }
23399 else if (attr_form_is_section_offset (attr))
23400 {
23401 struct dwarf2_loclist_baton loclist_baton;
23402 CORE_ADDR pc = (*get_frame_pc) (baton);
23403 size_t size;
23404
23405 fill_in_loclist_baton (cu, &loclist_baton, attr);
23406
23407 retval.data = dwarf2_find_location_expression (&loclist_baton,
23408 &size, pc);
23409 retval.size = size;
23410 }
23411 else
23412 {
23413 if (!attr_form_is_block (attr))
23414 error (_("Dwarf Error: DIE at %s referenced in module %s "
23415 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23416 sect_offset_str (sect_off), objfile_name (objfile));
23417
23418 retval.data = DW_BLOCK (attr)->data;
23419 retval.size = DW_BLOCK (attr)->size;
23420 }
23421 retval.per_cu = cu->per_cu;
23422
23423 age_cached_comp_units (dwarf2_per_objfile);
23424
23425 return retval;
23426 }
23427
23428 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23429 offset. */
23430
23431 struct dwarf2_locexpr_baton
23432 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23433 struct dwarf2_per_cu_data *per_cu,
23434 CORE_ADDR (*get_frame_pc) (void *baton),
23435 void *baton)
23436 {
23437 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23438
23439 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23440 }
23441
23442 /* Write a constant of a given type as target-ordered bytes into
23443 OBSTACK. */
23444
23445 static const gdb_byte *
23446 write_constant_as_bytes (struct obstack *obstack,
23447 enum bfd_endian byte_order,
23448 struct type *type,
23449 ULONGEST value,
23450 LONGEST *len)
23451 {
23452 gdb_byte *result;
23453
23454 *len = TYPE_LENGTH (type);
23455 result = (gdb_byte *) obstack_alloc (obstack, *len);
23456 store_unsigned_integer (result, *len, byte_order, value);
23457
23458 return result;
23459 }
23460
23461 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23462 pointer to the constant bytes and set LEN to the length of the
23463 data. If memory is needed, allocate it on OBSTACK. If the DIE
23464 does not have a DW_AT_const_value, return NULL. */
23465
23466 const gdb_byte *
23467 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23468 struct dwarf2_per_cu_data *per_cu,
23469 struct obstack *obstack,
23470 LONGEST *len)
23471 {
23472 struct dwarf2_cu *cu;
23473 struct die_info *die;
23474 struct attribute *attr;
23475 const gdb_byte *result = NULL;
23476 struct type *type;
23477 LONGEST value;
23478 enum bfd_endian byte_order;
23479 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23480
23481 if (per_cu->cu == NULL)
23482 load_cu (per_cu, false);
23483 cu = per_cu->cu;
23484 if (cu == NULL)
23485 {
23486 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23487 Instead just throw an error, not much else we can do. */
23488 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23489 sect_offset_str (sect_off), objfile_name (objfile));
23490 }
23491
23492 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23493 if (!die)
23494 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23495 sect_offset_str (sect_off), objfile_name (objfile));
23496
23497 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23498 if (attr == NULL)
23499 return NULL;
23500
23501 byte_order = (bfd_big_endian (objfile->obfd)
23502 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23503
23504 switch (attr->form)
23505 {
23506 case DW_FORM_addr:
23507 case DW_FORM_addrx:
23508 case DW_FORM_GNU_addr_index:
23509 {
23510 gdb_byte *tem;
23511
23512 *len = cu->header.addr_size;
23513 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23514 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23515 result = tem;
23516 }
23517 break;
23518 case DW_FORM_string:
23519 case DW_FORM_strp:
23520 case DW_FORM_strx:
23521 case DW_FORM_GNU_str_index:
23522 case DW_FORM_GNU_strp_alt:
23523 /* DW_STRING is already allocated on the objfile obstack, point
23524 directly to it. */
23525 result = (const gdb_byte *) DW_STRING (attr);
23526 *len = strlen (DW_STRING (attr));
23527 break;
23528 case DW_FORM_block1:
23529 case DW_FORM_block2:
23530 case DW_FORM_block4:
23531 case DW_FORM_block:
23532 case DW_FORM_exprloc:
23533 case DW_FORM_data16:
23534 result = DW_BLOCK (attr)->data;
23535 *len = DW_BLOCK (attr)->size;
23536 break;
23537
23538 /* The DW_AT_const_value attributes are supposed to carry the
23539 symbol's value "represented as it would be on the target
23540 architecture." By the time we get here, it's already been
23541 converted to host endianness, so we just need to sign- or
23542 zero-extend it as appropriate. */
23543 case DW_FORM_data1:
23544 type = die_type (die, cu);
23545 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23546 if (result == NULL)
23547 result = write_constant_as_bytes (obstack, byte_order,
23548 type, value, len);
23549 break;
23550 case DW_FORM_data2:
23551 type = die_type (die, cu);
23552 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23553 if (result == NULL)
23554 result = write_constant_as_bytes (obstack, byte_order,
23555 type, value, len);
23556 break;
23557 case DW_FORM_data4:
23558 type = die_type (die, cu);
23559 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23560 if (result == NULL)
23561 result = write_constant_as_bytes (obstack, byte_order,
23562 type, value, len);
23563 break;
23564 case DW_FORM_data8:
23565 type = die_type (die, cu);
23566 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23567 if (result == NULL)
23568 result = write_constant_as_bytes (obstack, byte_order,
23569 type, value, len);
23570 break;
23571
23572 case DW_FORM_sdata:
23573 case DW_FORM_implicit_const:
23574 type = die_type (die, cu);
23575 result = write_constant_as_bytes (obstack, byte_order,
23576 type, DW_SND (attr), len);
23577 break;
23578
23579 case DW_FORM_udata:
23580 type = die_type (die, cu);
23581 result = write_constant_as_bytes (obstack, byte_order,
23582 type, DW_UNSND (attr), len);
23583 break;
23584
23585 default:
23586 complaint (_("unsupported const value attribute form: '%s'"),
23587 dwarf_form_name (attr->form));
23588 break;
23589 }
23590
23591 return result;
23592 }
23593
23594 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23595 valid type for this die is found. */
23596
23597 struct type *
23598 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23599 struct dwarf2_per_cu_data *per_cu)
23600 {
23601 struct dwarf2_cu *cu;
23602 struct die_info *die;
23603
23604 if (per_cu->cu == NULL)
23605 load_cu (per_cu, false);
23606 cu = per_cu->cu;
23607 if (!cu)
23608 return NULL;
23609
23610 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23611 if (!die)
23612 return NULL;
23613
23614 return die_type (die, cu);
23615 }
23616
23617 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23618 PER_CU. */
23619
23620 struct type *
23621 dwarf2_get_die_type (cu_offset die_offset,
23622 struct dwarf2_per_cu_data *per_cu)
23623 {
23624 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23625 return get_die_type_at_offset (die_offset_sect, per_cu);
23626 }
23627
23628 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23629 On entry *REF_CU is the CU of SRC_DIE.
23630 On exit *REF_CU is the CU of the result.
23631 Returns NULL if the referenced DIE isn't found. */
23632
23633 static struct die_info *
23634 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23635 struct dwarf2_cu **ref_cu)
23636 {
23637 struct die_info temp_die;
23638 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23639 struct die_info *die;
23640
23641 /* While it might be nice to assert sig_type->type == NULL here,
23642 we can get here for DW_AT_imported_declaration where we need
23643 the DIE not the type. */
23644
23645 /* If necessary, add it to the queue and load its DIEs. */
23646
23647 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23648 read_signatured_type (sig_type);
23649
23650 sig_cu = sig_type->per_cu.cu;
23651 gdb_assert (sig_cu != NULL);
23652 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23653 temp_die.sect_off = sig_type->type_offset_in_section;
23654 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23655 to_underlying (temp_die.sect_off));
23656 if (die)
23657 {
23658 struct dwarf2_per_objfile *dwarf2_per_objfile
23659 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23660
23661 /* For .gdb_index version 7 keep track of included TUs.
23662 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23663 if (dwarf2_per_objfile->index_table != NULL
23664 && dwarf2_per_objfile->index_table->version <= 7)
23665 {
23666 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23667 }
23668
23669 *ref_cu = sig_cu;
23670 if (sig_cu != cu)
23671 sig_cu->ancestor = cu;
23672
23673 return die;
23674 }
23675
23676 return NULL;
23677 }
23678
23679 /* Follow signatured type referenced by ATTR in SRC_DIE.
23680 On entry *REF_CU is the CU of SRC_DIE.
23681 On exit *REF_CU is the CU of the result.
23682 The result is the DIE of the type.
23683 If the referenced type cannot be found an error is thrown. */
23684
23685 static struct die_info *
23686 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23687 struct dwarf2_cu **ref_cu)
23688 {
23689 ULONGEST signature = DW_SIGNATURE (attr);
23690 struct signatured_type *sig_type;
23691 struct die_info *die;
23692
23693 gdb_assert (attr->form == DW_FORM_ref_sig8);
23694
23695 sig_type = lookup_signatured_type (*ref_cu, signature);
23696 /* sig_type will be NULL if the signatured type is missing from
23697 the debug info. */
23698 if (sig_type == NULL)
23699 {
23700 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23701 " from DIE at %s [in module %s]"),
23702 hex_string (signature), sect_offset_str (src_die->sect_off),
23703 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23704 }
23705
23706 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23707 if (die == NULL)
23708 {
23709 dump_die_for_error (src_die);
23710 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23711 " from DIE at %s [in module %s]"),
23712 hex_string (signature), sect_offset_str (src_die->sect_off),
23713 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23714 }
23715
23716 return die;
23717 }
23718
23719 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23720 reading in and processing the type unit if necessary. */
23721
23722 static struct type *
23723 get_signatured_type (struct die_info *die, ULONGEST signature,
23724 struct dwarf2_cu *cu)
23725 {
23726 struct dwarf2_per_objfile *dwarf2_per_objfile
23727 = cu->per_cu->dwarf2_per_objfile;
23728 struct signatured_type *sig_type;
23729 struct dwarf2_cu *type_cu;
23730 struct die_info *type_die;
23731 struct type *type;
23732
23733 sig_type = lookup_signatured_type (cu, signature);
23734 /* sig_type will be NULL if the signatured type is missing from
23735 the debug info. */
23736 if (sig_type == NULL)
23737 {
23738 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23739 " from DIE at %s [in module %s]"),
23740 hex_string (signature), sect_offset_str (die->sect_off),
23741 objfile_name (dwarf2_per_objfile->objfile));
23742 return build_error_marker_type (cu, die);
23743 }
23744
23745 /* If we already know the type we're done. */
23746 if (sig_type->type != NULL)
23747 return sig_type->type;
23748
23749 type_cu = cu;
23750 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23751 if (type_die != NULL)
23752 {
23753 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23754 is created. This is important, for example, because for c++ classes
23755 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23756 type = read_type_die (type_die, type_cu);
23757 if (type == NULL)
23758 {
23759 complaint (_("Dwarf Error: Cannot build signatured type %s"
23760 " referenced from DIE at %s [in module %s]"),
23761 hex_string (signature), sect_offset_str (die->sect_off),
23762 objfile_name (dwarf2_per_objfile->objfile));
23763 type = build_error_marker_type (cu, die);
23764 }
23765 }
23766 else
23767 {
23768 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23769 " from DIE at %s [in module %s]"),
23770 hex_string (signature), sect_offset_str (die->sect_off),
23771 objfile_name (dwarf2_per_objfile->objfile));
23772 type = build_error_marker_type (cu, die);
23773 }
23774 sig_type->type = type;
23775
23776 return type;
23777 }
23778
23779 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23780 reading in and processing the type unit if necessary. */
23781
23782 static struct type *
23783 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23784 struct dwarf2_cu *cu) /* ARI: editCase function */
23785 {
23786 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23787 if (attr_form_is_ref (attr))
23788 {
23789 struct dwarf2_cu *type_cu = cu;
23790 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23791
23792 return read_type_die (type_die, type_cu);
23793 }
23794 else if (attr->form == DW_FORM_ref_sig8)
23795 {
23796 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23797 }
23798 else
23799 {
23800 struct dwarf2_per_objfile *dwarf2_per_objfile
23801 = cu->per_cu->dwarf2_per_objfile;
23802
23803 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23804 " at %s [in module %s]"),
23805 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23806 objfile_name (dwarf2_per_objfile->objfile));
23807 return build_error_marker_type (cu, die);
23808 }
23809 }
23810
23811 /* Load the DIEs associated with type unit PER_CU into memory. */
23812
23813 static void
23814 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
23815 {
23816 struct signatured_type *sig_type;
23817
23818 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23819 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23820
23821 /* We have the per_cu, but we need the signatured_type.
23822 Fortunately this is an easy translation. */
23823 gdb_assert (per_cu->is_debug_types);
23824 sig_type = (struct signatured_type *) per_cu;
23825
23826 gdb_assert (per_cu->cu == NULL);
23827
23828 read_signatured_type (sig_type);
23829
23830 gdb_assert (per_cu->cu != NULL);
23831 }
23832
23833 /* die_reader_func for read_signatured_type.
23834 This is identical to load_full_comp_unit_reader,
23835 but is kept separate for now. */
23836
23837 static void
23838 read_signatured_type_reader (const struct die_reader_specs *reader,
23839 const gdb_byte *info_ptr,
23840 struct die_info *comp_unit_die,
23841 int has_children,
23842 void *data)
23843 {
23844 struct dwarf2_cu *cu = reader->cu;
23845
23846 gdb_assert (cu->die_hash == NULL);
23847 cu->die_hash =
23848 htab_create_alloc_ex (cu->header.length / 12,
23849 die_hash,
23850 die_eq,
23851 NULL,
23852 &cu->comp_unit_obstack,
23853 hashtab_obstack_allocate,
23854 dummy_obstack_deallocate);
23855
23856 if (has_children)
23857 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23858 &info_ptr, comp_unit_die);
23859 cu->dies = comp_unit_die;
23860 /* comp_unit_die is not stored in die_hash, no need. */
23861
23862 /* We try not to read any attributes in this function, because not
23863 all CUs needed for references have been loaded yet, and symbol
23864 table processing isn't initialized. But we have to set the CU language,
23865 or we won't be able to build types correctly.
23866 Similarly, if we do not read the producer, we can not apply
23867 producer-specific interpretation. */
23868 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23869 }
23870
23871 /* Read in a signatured type and build its CU and DIEs.
23872 If the type is a stub for the real type in a DWO file,
23873 read in the real type from the DWO file as well. */
23874
23875 static void
23876 read_signatured_type (struct signatured_type *sig_type)
23877 {
23878 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
23879
23880 gdb_assert (per_cu->is_debug_types);
23881 gdb_assert (per_cu->cu == NULL);
23882
23883 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
23884 read_signatured_type_reader, NULL);
23885 sig_type->per_cu.tu_read = 1;
23886 }
23887
23888 /* Decode simple location descriptions.
23889 Given a pointer to a dwarf block that defines a location, compute
23890 the location and return the value.
23891
23892 NOTE drow/2003-11-18: This function is called in two situations
23893 now: for the address of static or global variables (partial symbols
23894 only) and for offsets into structures which are expected to be
23895 (more or less) constant. The partial symbol case should go away,
23896 and only the constant case should remain. That will let this
23897 function complain more accurately. A few special modes are allowed
23898 without complaint for global variables (for instance, global
23899 register values and thread-local values).
23900
23901 A location description containing no operations indicates that the
23902 object is optimized out. The return value is 0 for that case.
23903 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23904 callers will only want a very basic result and this can become a
23905 complaint.
23906
23907 Note that stack[0] is unused except as a default error return. */
23908
23909 static CORE_ADDR
23910 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
23911 {
23912 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23913 size_t i;
23914 size_t size = blk->size;
23915 const gdb_byte *data = blk->data;
23916 CORE_ADDR stack[64];
23917 int stacki;
23918 unsigned int bytes_read, unsnd;
23919 gdb_byte op;
23920
23921 i = 0;
23922 stacki = 0;
23923 stack[stacki] = 0;
23924 stack[++stacki] = 0;
23925
23926 while (i < size)
23927 {
23928 op = data[i++];
23929 switch (op)
23930 {
23931 case DW_OP_lit0:
23932 case DW_OP_lit1:
23933 case DW_OP_lit2:
23934 case DW_OP_lit3:
23935 case DW_OP_lit4:
23936 case DW_OP_lit5:
23937 case DW_OP_lit6:
23938 case DW_OP_lit7:
23939 case DW_OP_lit8:
23940 case DW_OP_lit9:
23941 case DW_OP_lit10:
23942 case DW_OP_lit11:
23943 case DW_OP_lit12:
23944 case DW_OP_lit13:
23945 case DW_OP_lit14:
23946 case DW_OP_lit15:
23947 case DW_OP_lit16:
23948 case DW_OP_lit17:
23949 case DW_OP_lit18:
23950 case DW_OP_lit19:
23951 case DW_OP_lit20:
23952 case DW_OP_lit21:
23953 case DW_OP_lit22:
23954 case DW_OP_lit23:
23955 case DW_OP_lit24:
23956 case DW_OP_lit25:
23957 case DW_OP_lit26:
23958 case DW_OP_lit27:
23959 case DW_OP_lit28:
23960 case DW_OP_lit29:
23961 case DW_OP_lit30:
23962 case DW_OP_lit31:
23963 stack[++stacki] = op - DW_OP_lit0;
23964 break;
23965
23966 case DW_OP_reg0:
23967 case DW_OP_reg1:
23968 case DW_OP_reg2:
23969 case DW_OP_reg3:
23970 case DW_OP_reg4:
23971 case DW_OP_reg5:
23972 case DW_OP_reg6:
23973 case DW_OP_reg7:
23974 case DW_OP_reg8:
23975 case DW_OP_reg9:
23976 case DW_OP_reg10:
23977 case DW_OP_reg11:
23978 case DW_OP_reg12:
23979 case DW_OP_reg13:
23980 case DW_OP_reg14:
23981 case DW_OP_reg15:
23982 case DW_OP_reg16:
23983 case DW_OP_reg17:
23984 case DW_OP_reg18:
23985 case DW_OP_reg19:
23986 case DW_OP_reg20:
23987 case DW_OP_reg21:
23988 case DW_OP_reg22:
23989 case DW_OP_reg23:
23990 case DW_OP_reg24:
23991 case DW_OP_reg25:
23992 case DW_OP_reg26:
23993 case DW_OP_reg27:
23994 case DW_OP_reg28:
23995 case DW_OP_reg29:
23996 case DW_OP_reg30:
23997 case DW_OP_reg31:
23998 stack[++stacki] = op - DW_OP_reg0;
23999 if (i < size)
24000 dwarf2_complex_location_expr_complaint ();
24001 break;
24002
24003 case DW_OP_regx:
24004 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24005 i += bytes_read;
24006 stack[++stacki] = unsnd;
24007 if (i < size)
24008 dwarf2_complex_location_expr_complaint ();
24009 break;
24010
24011 case DW_OP_addr:
24012 stack[++stacki] = read_address (objfile->obfd, &data[i],
24013 cu, &bytes_read);
24014 i += bytes_read;
24015 break;
24016
24017 case DW_OP_const1u:
24018 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24019 i += 1;
24020 break;
24021
24022 case DW_OP_const1s:
24023 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24024 i += 1;
24025 break;
24026
24027 case DW_OP_const2u:
24028 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24029 i += 2;
24030 break;
24031
24032 case DW_OP_const2s:
24033 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24034 i += 2;
24035 break;
24036
24037 case DW_OP_const4u:
24038 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24039 i += 4;
24040 break;
24041
24042 case DW_OP_const4s:
24043 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24044 i += 4;
24045 break;
24046
24047 case DW_OP_const8u:
24048 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24049 i += 8;
24050 break;
24051
24052 case DW_OP_constu:
24053 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24054 &bytes_read);
24055 i += bytes_read;
24056 break;
24057
24058 case DW_OP_consts:
24059 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24060 i += bytes_read;
24061 break;
24062
24063 case DW_OP_dup:
24064 stack[stacki + 1] = stack[stacki];
24065 stacki++;
24066 break;
24067
24068 case DW_OP_plus:
24069 stack[stacki - 1] += stack[stacki];
24070 stacki--;
24071 break;
24072
24073 case DW_OP_plus_uconst:
24074 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24075 &bytes_read);
24076 i += bytes_read;
24077 break;
24078
24079 case DW_OP_minus:
24080 stack[stacki - 1] -= stack[stacki];
24081 stacki--;
24082 break;
24083
24084 case DW_OP_deref:
24085 /* If we're not the last op, then we definitely can't encode
24086 this using GDB's address_class enum. This is valid for partial
24087 global symbols, although the variable's address will be bogus
24088 in the psymtab. */
24089 if (i < size)
24090 dwarf2_complex_location_expr_complaint ();
24091 break;
24092
24093 case DW_OP_GNU_push_tls_address:
24094 case DW_OP_form_tls_address:
24095 /* The top of the stack has the offset from the beginning
24096 of the thread control block at which the variable is located. */
24097 /* Nothing should follow this operator, so the top of stack would
24098 be returned. */
24099 /* This is valid for partial global symbols, but the variable's
24100 address will be bogus in the psymtab. Make it always at least
24101 non-zero to not look as a variable garbage collected by linker
24102 which have DW_OP_addr 0. */
24103 if (i < size)
24104 dwarf2_complex_location_expr_complaint ();
24105 stack[stacki]++;
24106 break;
24107
24108 case DW_OP_GNU_uninit:
24109 break;
24110
24111 case DW_OP_addrx:
24112 case DW_OP_GNU_addr_index:
24113 case DW_OP_GNU_const_index:
24114 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24115 &bytes_read);
24116 i += bytes_read;
24117 break;
24118
24119 default:
24120 {
24121 const char *name = get_DW_OP_name (op);
24122
24123 if (name)
24124 complaint (_("unsupported stack op: '%s'"),
24125 name);
24126 else
24127 complaint (_("unsupported stack op: '%02x'"),
24128 op);
24129 }
24130
24131 return (stack[stacki]);
24132 }
24133
24134 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24135 outside of the allocated space. Also enforce minimum>0. */
24136 if (stacki >= ARRAY_SIZE (stack) - 1)
24137 {
24138 complaint (_("location description stack overflow"));
24139 return 0;
24140 }
24141
24142 if (stacki <= 0)
24143 {
24144 complaint (_("location description stack underflow"));
24145 return 0;
24146 }
24147 }
24148 return (stack[stacki]);
24149 }
24150
24151 /* memory allocation interface */
24152
24153 static struct dwarf_block *
24154 dwarf_alloc_block (struct dwarf2_cu *cu)
24155 {
24156 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24157 }
24158
24159 static struct die_info *
24160 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24161 {
24162 struct die_info *die;
24163 size_t size = sizeof (struct die_info);
24164
24165 if (num_attrs > 1)
24166 size += (num_attrs - 1) * sizeof (struct attribute);
24167
24168 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24169 memset (die, 0, sizeof (struct die_info));
24170 return (die);
24171 }
24172
24173 \f
24174 /* Macro support. */
24175
24176 /* Return file name relative to the compilation directory of file number I in
24177 *LH's file name table. The result is allocated using xmalloc; the caller is
24178 responsible for freeing it. */
24179
24180 static char *
24181 file_file_name (int file, struct line_header *lh)
24182 {
24183 /* Is the file number a valid index into the line header's file name
24184 table? Remember that file numbers start with one, not zero. */
24185 if (1 <= file && file <= lh->file_names.size ())
24186 {
24187 const file_entry &fe = lh->file_names[file - 1];
24188
24189 if (!IS_ABSOLUTE_PATH (fe.name))
24190 {
24191 const char *dir = fe.include_dir (lh);
24192 if (dir != NULL)
24193 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
24194 }
24195 return xstrdup (fe.name);
24196 }
24197 else
24198 {
24199 /* The compiler produced a bogus file number. We can at least
24200 record the macro definitions made in the file, even if we
24201 won't be able to find the file by name. */
24202 char fake_name[80];
24203
24204 xsnprintf (fake_name, sizeof (fake_name),
24205 "<bad macro file number %d>", file);
24206
24207 complaint (_("bad file number in macro information (%d)"),
24208 file);
24209
24210 return xstrdup (fake_name);
24211 }
24212 }
24213
24214 /* Return the full name of file number I in *LH's file name table.
24215 Use COMP_DIR as the name of the current directory of the
24216 compilation. The result is allocated using xmalloc; the caller is
24217 responsible for freeing it. */
24218 static char *
24219 file_full_name (int file, struct line_header *lh, const char *comp_dir)
24220 {
24221 /* Is the file number a valid index into the line header's file name
24222 table? Remember that file numbers start with one, not zero. */
24223 if (1 <= file && file <= lh->file_names.size ())
24224 {
24225 char *relative = file_file_name (file, lh);
24226
24227 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24228 return relative;
24229 return reconcat (relative, comp_dir, SLASH_STRING,
24230 relative, (char *) NULL);
24231 }
24232 else
24233 return file_file_name (file, lh);
24234 }
24235
24236
24237 static struct macro_source_file *
24238 macro_start_file (struct dwarf2_cu *cu,
24239 int file, int line,
24240 struct macro_source_file *current_file,
24241 struct line_header *lh)
24242 {
24243 /* File name relative to the compilation directory of this source file. */
24244 char *file_name = file_file_name (file, lh);
24245
24246 if (! current_file)
24247 {
24248 /* Note: We don't create a macro table for this compilation unit
24249 at all until we actually get a filename. */
24250 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
24251
24252 /* If we have no current file, then this must be the start_file
24253 directive for the compilation unit's main source file. */
24254 current_file = macro_set_main (macro_table, file_name);
24255 macro_define_special (macro_table);
24256 }
24257 else
24258 current_file = macro_include (current_file, line, file_name);
24259
24260 xfree (file_name);
24261
24262 return current_file;
24263 }
24264
24265 static const char *
24266 consume_improper_spaces (const char *p, const char *body)
24267 {
24268 if (*p == ' ')
24269 {
24270 complaint (_("macro definition contains spaces "
24271 "in formal argument list:\n`%s'"),
24272 body);
24273
24274 while (*p == ' ')
24275 p++;
24276 }
24277
24278 return p;
24279 }
24280
24281
24282 static void
24283 parse_macro_definition (struct macro_source_file *file, int line,
24284 const char *body)
24285 {
24286 const char *p;
24287
24288 /* The body string takes one of two forms. For object-like macro
24289 definitions, it should be:
24290
24291 <macro name> " " <definition>
24292
24293 For function-like macro definitions, it should be:
24294
24295 <macro name> "() " <definition>
24296 or
24297 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24298
24299 Spaces may appear only where explicitly indicated, and in the
24300 <definition>.
24301
24302 The Dwarf 2 spec says that an object-like macro's name is always
24303 followed by a space, but versions of GCC around March 2002 omit
24304 the space when the macro's definition is the empty string.
24305
24306 The Dwarf 2 spec says that there should be no spaces between the
24307 formal arguments in a function-like macro's formal argument list,
24308 but versions of GCC around March 2002 include spaces after the
24309 commas. */
24310
24311
24312 /* Find the extent of the macro name. The macro name is terminated
24313 by either a space or null character (for an object-like macro) or
24314 an opening paren (for a function-like macro). */
24315 for (p = body; *p; p++)
24316 if (*p == ' ' || *p == '(')
24317 break;
24318
24319 if (*p == ' ' || *p == '\0')
24320 {
24321 /* It's an object-like macro. */
24322 int name_len = p - body;
24323 char *name = savestring (body, name_len);
24324 const char *replacement;
24325
24326 if (*p == ' ')
24327 replacement = body + name_len + 1;
24328 else
24329 {
24330 dwarf2_macro_malformed_definition_complaint (body);
24331 replacement = body + name_len;
24332 }
24333
24334 macro_define_object (file, line, name, replacement);
24335
24336 xfree (name);
24337 }
24338 else if (*p == '(')
24339 {
24340 /* It's a function-like macro. */
24341 char *name = savestring (body, p - body);
24342 int argc = 0;
24343 int argv_size = 1;
24344 char **argv = XNEWVEC (char *, argv_size);
24345
24346 p++;
24347
24348 p = consume_improper_spaces (p, body);
24349
24350 /* Parse the formal argument list. */
24351 while (*p && *p != ')')
24352 {
24353 /* Find the extent of the current argument name. */
24354 const char *arg_start = p;
24355
24356 while (*p && *p != ',' && *p != ')' && *p != ' ')
24357 p++;
24358
24359 if (! *p || p == arg_start)
24360 dwarf2_macro_malformed_definition_complaint (body);
24361 else
24362 {
24363 /* Make sure argv has room for the new argument. */
24364 if (argc >= argv_size)
24365 {
24366 argv_size *= 2;
24367 argv = XRESIZEVEC (char *, argv, argv_size);
24368 }
24369
24370 argv[argc++] = savestring (arg_start, p - arg_start);
24371 }
24372
24373 p = consume_improper_spaces (p, body);
24374
24375 /* Consume the comma, if present. */
24376 if (*p == ',')
24377 {
24378 p++;
24379
24380 p = consume_improper_spaces (p, body);
24381 }
24382 }
24383
24384 if (*p == ')')
24385 {
24386 p++;
24387
24388 if (*p == ' ')
24389 /* Perfectly formed definition, no complaints. */
24390 macro_define_function (file, line, name,
24391 argc, (const char **) argv,
24392 p + 1);
24393 else if (*p == '\0')
24394 {
24395 /* Complain, but do define it. */
24396 dwarf2_macro_malformed_definition_complaint (body);
24397 macro_define_function (file, line, name,
24398 argc, (const char **) argv,
24399 p);
24400 }
24401 else
24402 /* Just complain. */
24403 dwarf2_macro_malformed_definition_complaint (body);
24404 }
24405 else
24406 /* Just complain. */
24407 dwarf2_macro_malformed_definition_complaint (body);
24408
24409 xfree (name);
24410 {
24411 int i;
24412
24413 for (i = 0; i < argc; i++)
24414 xfree (argv[i]);
24415 }
24416 xfree (argv);
24417 }
24418 else
24419 dwarf2_macro_malformed_definition_complaint (body);
24420 }
24421
24422 /* Skip some bytes from BYTES according to the form given in FORM.
24423 Returns the new pointer. */
24424
24425 static const gdb_byte *
24426 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24427 enum dwarf_form form,
24428 unsigned int offset_size,
24429 struct dwarf2_section_info *section)
24430 {
24431 unsigned int bytes_read;
24432
24433 switch (form)
24434 {
24435 case DW_FORM_data1:
24436 case DW_FORM_flag:
24437 ++bytes;
24438 break;
24439
24440 case DW_FORM_data2:
24441 bytes += 2;
24442 break;
24443
24444 case DW_FORM_data4:
24445 bytes += 4;
24446 break;
24447
24448 case DW_FORM_data8:
24449 bytes += 8;
24450 break;
24451
24452 case DW_FORM_data16:
24453 bytes += 16;
24454 break;
24455
24456 case DW_FORM_string:
24457 read_direct_string (abfd, bytes, &bytes_read);
24458 bytes += bytes_read;
24459 break;
24460
24461 case DW_FORM_sec_offset:
24462 case DW_FORM_strp:
24463 case DW_FORM_GNU_strp_alt:
24464 bytes += offset_size;
24465 break;
24466
24467 case DW_FORM_block:
24468 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24469 bytes += bytes_read;
24470 break;
24471
24472 case DW_FORM_block1:
24473 bytes += 1 + read_1_byte (abfd, bytes);
24474 break;
24475 case DW_FORM_block2:
24476 bytes += 2 + read_2_bytes (abfd, bytes);
24477 break;
24478 case DW_FORM_block4:
24479 bytes += 4 + read_4_bytes (abfd, bytes);
24480 break;
24481
24482 case DW_FORM_addrx:
24483 case DW_FORM_sdata:
24484 case DW_FORM_strx:
24485 case DW_FORM_udata:
24486 case DW_FORM_GNU_addr_index:
24487 case DW_FORM_GNU_str_index:
24488 bytes = gdb_skip_leb128 (bytes, buffer_end);
24489 if (bytes == NULL)
24490 {
24491 dwarf2_section_buffer_overflow_complaint (section);
24492 return NULL;
24493 }
24494 break;
24495
24496 case DW_FORM_implicit_const:
24497 break;
24498
24499 default:
24500 {
24501 complaint (_("invalid form 0x%x in `%s'"),
24502 form, get_section_name (section));
24503 return NULL;
24504 }
24505 }
24506
24507 return bytes;
24508 }
24509
24510 /* A helper for dwarf_decode_macros that handles skipping an unknown
24511 opcode. Returns an updated pointer to the macro data buffer; or,
24512 on error, issues a complaint and returns NULL. */
24513
24514 static const gdb_byte *
24515 skip_unknown_opcode (unsigned int opcode,
24516 const gdb_byte **opcode_definitions,
24517 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24518 bfd *abfd,
24519 unsigned int offset_size,
24520 struct dwarf2_section_info *section)
24521 {
24522 unsigned int bytes_read, i;
24523 unsigned long arg;
24524 const gdb_byte *defn;
24525
24526 if (opcode_definitions[opcode] == NULL)
24527 {
24528 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
24529 opcode);
24530 return NULL;
24531 }
24532
24533 defn = opcode_definitions[opcode];
24534 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24535 defn += bytes_read;
24536
24537 for (i = 0; i < arg; ++i)
24538 {
24539 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24540 (enum dwarf_form) defn[i], offset_size,
24541 section);
24542 if (mac_ptr == NULL)
24543 {
24544 /* skip_form_bytes already issued the complaint. */
24545 return NULL;
24546 }
24547 }
24548
24549 return mac_ptr;
24550 }
24551
24552 /* A helper function which parses the header of a macro section.
24553 If the macro section is the extended (for now called "GNU") type,
24554 then this updates *OFFSET_SIZE. Returns a pointer to just after
24555 the header, or issues a complaint and returns NULL on error. */
24556
24557 static const gdb_byte *
24558 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24559 bfd *abfd,
24560 const gdb_byte *mac_ptr,
24561 unsigned int *offset_size,
24562 int section_is_gnu)
24563 {
24564 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24565
24566 if (section_is_gnu)
24567 {
24568 unsigned int version, flags;
24569
24570 version = read_2_bytes (abfd, mac_ptr);
24571 if (version != 4 && version != 5)
24572 {
24573 complaint (_("unrecognized version `%d' in .debug_macro section"),
24574 version);
24575 return NULL;
24576 }
24577 mac_ptr += 2;
24578
24579 flags = read_1_byte (abfd, mac_ptr);
24580 ++mac_ptr;
24581 *offset_size = (flags & 1) ? 8 : 4;
24582
24583 if ((flags & 2) != 0)
24584 /* We don't need the line table offset. */
24585 mac_ptr += *offset_size;
24586
24587 /* Vendor opcode descriptions. */
24588 if ((flags & 4) != 0)
24589 {
24590 unsigned int i, count;
24591
24592 count = read_1_byte (abfd, mac_ptr);
24593 ++mac_ptr;
24594 for (i = 0; i < count; ++i)
24595 {
24596 unsigned int opcode, bytes_read;
24597 unsigned long arg;
24598
24599 opcode = read_1_byte (abfd, mac_ptr);
24600 ++mac_ptr;
24601 opcode_definitions[opcode] = mac_ptr;
24602 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24603 mac_ptr += bytes_read;
24604 mac_ptr += arg;
24605 }
24606 }
24607 }
24608
24609 return mac_ptr;
24610 }
24611
24612 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24613 including DW_MACRO_import. */
24614
24615 static void
24616 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
24617 bfd *abfd,
24618 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24619 struct macro_source_file *current_file,
24620 struct line_header *lh,
24621 struct dwarf2_section_info *section,
24622 int section_is_gnu, int section_is_dwz,
24623 unsigned int offset_size,
24624 htab_t include_hash)
24625 {
24626 struct dwarf2_per_objfile *dwarf2_per_objfile
24627 = cu->per_cu->dwarf2_per_objfile;
24628 struct objfile *objfile = dwarf2_per_objfile->objfile;
24629 enum dwarf_macro_record_type macinfo_type;
24630 int at_commandline;
24631 const gdb_byte *opcode_definitions[256];
24632
24633 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24634 &offset_size, section_is_gnu);
24635 if (mac_ptr == NULL)
24636 {
24637 /* We already issued a complaint. */
24638 return;
24639 }
24640
24641 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24642 GDB is still reading the definitions from command line. First
24643 DW_MACINFO_start_file will need to be ignored as it was already executed
24644 to create CURRENT_FILE for the main source holding also the command line
24645 definitions. On first met DW_MACINFO_start_file this flag is reset to
24646 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24647
24648 at_commandline = 1;
24649
24650 do
24651 {
24652 /* Do we at least have room for a macinfo type byte? */
24653 if (mac_ptr >= mac_end)
24654 {
24655 dwarf2_section_buffer_overflow_complaint (section);
24656 break;
24657 }
24658
24659 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24660 mac_ptr++;
24661
24662 /* Note that we rely on the fact that the corresponding GNU and
24663 DWARF constants are the same. */
24664 DIAGNOSTIC_PUSH
24665 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24666 switch (macinfo_type)
24667 {
24668 /* A zero macinfo type indicates the end of the macro
24669 information. */
24670 case 0:
24671 break;
24672
24673 case DW_MACRO_define:
24674 case DW_MACRO_undef:
24675 case DW_MACRO_define_strp:
24676 case DW_MACRO_undef_strp:
24677 case DW_MACRO_define_sup:
24678 case DW_MACRO_undef_sup:
24679 {
24680 unsigned int bytes_read;
24681 int line;
24682 const char *body;
24683 int is_define;
24684
24685 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24686 mac_ptr += bytes_read;
24687
24688 if (macinfo_type == DW_MACRO_define
24689 || macinfo_type == DW_MACRO_undef)
24690 {
24691 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24692 mac_ptr += bytes_read;
24693 }
24694 else
24695 {
24696 LONGEST str_offset;
24697
24698 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24699 mac_ptr += offset_size;
24700
24701 if (macinfo_type == DW_MACRO_define_sup
24702 || macinfo_type == DW_MACRO_undef_sup
24703 || section_is_dwz)
24704 {
24705 struct dwz_file *dwz
24706 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24707
24708 body = read_indirect_string_from_dwz (objfile,
24709 dwz, str_offset);
24710 }
24711 else
24712 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24713 abfd, str_offset);
24714 }
24715
24716 is_define = (macinfo_type == DW_MACRO_define
24717 || macinfo_type == DW_MACRO_define_strp
24718 || macinfo_type == DW_MACRO_define_sup);
24719 if (! current_file)
24720 {
24721 /* DWARF violation as no main source is present. */
24722 complaint (_("debug info with no main source gives macro %s "
24723 "on line %d: %s"),
24724 is_define ? _("definition") : _("undefinition"),
24725 line, body);
24726 break;
24727 }
24728 if ((line == 0 && !at_commandline)
24729 || (line != 0 && at_commandline))
24730 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
24731 at_commandline ? _("command-line") : _("in-file"),
24732 is_define ? _("definition") : _("undefinition"),
24733 line == 0 ? _("zero") : _("non-zero"), line, body);
24734
24735 if (body == NULL)
24736 {
24737 /* Fedora's rpm-build's "debugedit" binary
24738 corrupted .debug_macro sections.
24739
24740 For more info, see
24741 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24742 complaint (_("debug info gives %s invalid macro %s "
24743 "without body (corrupted?) at line %d "
24744 "on file %s"),
24745 at_commandline ? _("command-line") : _("in-file"),
24746 is_define ? _("definition") : _("undefinition"),
24747 line, current_file->filename);
24748 }
24749 else if (is_define)
24750 parse_macro_definition (current_file, line, body);
24751 else
24752 {
24753 gdb_assert (macinfo_type == DW_MACRO_undef
24754 || macinfo_type == DW_MACRO_undef_strp
24755 || macinfo_type == DW_MACRO_undef_sup);
24756 macro_undef (current_file, line, body);
24757 }
24758 }
24759 break;
24760
24761 case DW_MACRO_start_file:
24762 {
24763 unsigned int bytes_read;
24764 int line, file;
24765
24766 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24767 mac_ptr += bytes_read;
24768 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24769 mac_ptr += bytes_read;
24770
24771 if ((line == 0 && !at_commandline)
24772 || (line != 0 && at_commandline))
24773 complaint (_("debug info gives source %d included "
24774 "from %s at %s line %d"),
24775 file, at_commandline ? _("command-line") : _("file"),
24776 line == 0 ? _("zero") : _("non-zero"), line);
24777
24778 if (at_commandline)
24779 {
24780 /* This DW_MACRO_start_file was executed in the
24781 pass one. */
24782 at_commandline = 0;
24783 }
24784 else
24785 current_file = macro_start_file (cu, file, line, current_file,
24786 lh);
24787 }
24788 break;
24789
24790 case DW_MACRO_end_file:
24791 if (! current_file)
24792 complaint (_("macro debug info has an unmatched "
24793 "`close_file' directive"));
24794 else
24795 {
24796 current_file = current_file->included_by;
24797 if (! current_file)
24798 {
24799 enum dwarf_macro_record_type next_type;
24800
24801 /* GCC circa March 2002 doesn't produce the zero
24802 type byte marking the end of the compilation
24803 unit. Complain if it's not there, but exit no
24804 matter what. */
24805
24806 /* Do we at least have room for a macinfo type byte? */
24807 if (mac_ptr >= mac_end)
24808 {
24809 dwarf2_section_buffer_overflow_complaint (section);
24810 return;
24811 }
24812
24813 /* We don't increment mac_ptr here, so this is just
24814 a look-ahead. */
24815 next_type
24816 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24817 mac_ptr);
24818 if (next_type != 0)
24819 complaint (_("no terminating 0-type entry for "
24820 "macros in `.debug_macinfo' section"));
24821
24822 return;
24823 }
24824 }
24825 break;
24826
24827 case DW_MACRO_import:
24828 case DW_MACRO_import_sup:
24829 {
24830 LONGEST offset;
24831 void **slot;
24832 bfd *include_bfd = abfd;
24833 struct dwarf2_section_info *include_section = section;
24834 const gdb_byte *include_mac_end = mac_end;
24835 int is_dwz = section_is_dwz;
24836 const gdb_byte *new_mac_ptr;
24837
24838 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24839 mac_ptr += offset_size;
24840
24841 if (macinfo_type == DW_MACRO_import_sup)
24842 {
24843 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
24844
24845 dwarf2_read_section (objfile, &dwz->macro);
24846
24847 include_section = &dwz->macro;
24848 include_bfd = get_section_bfd_owner (include_section);
24849 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24850 is_dwz = 1;
24851 }
24852
24853 new_mac_ptr = include_section->buffer + offset;
24854 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24855
24856 if (*slot != NULL)
24857 {
24858 /* This has actually happened; see
24859 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
24860 complaint (_("recursive DW_MACRO_import in "
24861 ".debug_macro section"));
24862 }
24863 else
24864 {
24865 *slot = (void *) new_mac_ptr;
24866
24867 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
24868 include_mac_end, current_file, lh,
24869 section, section_is_gnu, is_dwz,
24870 offset_size, include_hash);
24871
24872 htab_remove_elt (include_hash, (void *) new_mac_ptr);
24873 }
24874 }
24875 break;
24876
24877 case DW_MACINFO_vendor_ext:
24878 if (!section_is_gnu)
24879 {
24880 unsigned int bytes_read;
24881
24882 /* This reads the constant, but since we don't recognize
24883 any vendor extensions, we ignore it. */
24884 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24885 mac_ptr += bytes_read;
24886 read_direct_string (abfd, mac_ptr, &bytes_read);
24887 mac_ptr += bytes_read;
24888
24889 /* We don't recognize any vendor extensions. */
24890 break;
24891 }
24892 /* FALLTHROUGH */
24893
24894 default:
24895 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
24896 mac_ptr, mac_end, abfd, offset_size,
24897 section);
24898 if (mac_ptr == NULL)
24899 return;
24900 break;
24901 }
24902 DIAGNOSTIC_POP
24903 } while (macinfo_type != 0);
24904 }
24905
24906 static void
24907 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24908 int section_is_gnu)
24909 {
24910 struct dwarf2_per_objfile *dwarf2_per_objfile
24911 = cu->per_cu->dwarf2_per_objfile;
24912 struct objfile *objfile = dwarf2_per_objfile->objfile;
24913 struct line_header *lh = cu->line_header;
24914 bfd *abfd;
24915 const gdb_byte *mac_ptr, *mac_end;
24916 struct macro_source_file *current_file = 0;
24917 enum dwarf_macro_record_type macinfo_type;
24918 unsigned int offset_size = cu->header.offset_size;
24919 const gdb_byte *opcode_definitions[256];
24920 void **slot;
24921 struct dwarf2_section_info *section;
24922 const char *section_name;
24923
24924 if (cu->dwo_unit != NULL)
24925 {
24926 if (section_is_gnu)
24927 {
24928 section = &cu->dwo_unit->dwo_file->sections.macro;
24929 section_name = ".debug_macro.dwo";
24930 }
24931 else
24932 {
24933 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24934 section_name = ".debug_macinfo.dwo";
24935 }
24936 }
24937 else
24938 {
24939 if (section_is_gnu)
24940 {
24941 section = &dwarf2_per_objfile->macro;
24942 section_name = ".debug_macro";
24943 }
24944 else
24945 {
24946 section = &dwarf2_per_objfile->macinfo;
24947 section_name = ".debug_macinfo";
24948 }
24949 }
24950
24951 dwarf2_read_section (objfile, section);
24952 if (section->buffer == NULL)
24953 {
24954 complaint (_("missing %s section"), section_name);
24955 return;
24956 }
24957 abfd = get_section_bfd_owner (section);
24958
24959 /* First pass: Find the name of the base filename.
24960 This filename is needed in order to process all macros whose definition
24961 (or undefinition) comes from the command line. These macros are defined
24962 before the first DW_MACINFO_start_file entry, and yet still need to be
24963 associated to the base file.
24964
24965 To determine the base file name, we scan the macro definitions until we
24966 reach the first DW_MACINFO_start_file entry. We then initialize
24967 CURRENT_FILE accordingly so that any macro definition found before the
24968 first DW_MACINFO_start_file can still be associated to the base file. */
24969
24970 mac_ptr = section->buffer + offset;
24971 mac_end = section->buffer + section->size;
24972
24973 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24974 &offset_size, section_is_gnu);
24975 if (mac_ptr == NULL)
24976 {
24977 /* We already issued a complaint. */
24978 return;
24979 }
24980
24981 do
24982 {
24983 /* Do we at least have room for a macinfo type byte? */
24984 if (mac_ptr >= mac_end)
24985 {
24986 /* Complaint is printed during the second pass as GDB will probably
24987 stop the first pass earlier upon finding
24988 DW_MACINFO_start_file. */
24989 break;
24990 }
24991
24992 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24993 mac_ptr++;
24994
24995 /* Note that we rely on the fact that the corresponding GNU and
24996 DWARF constants are the same. */
24997 DIAGNOSTIC_PUSH
24998 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24999 switch (macinfo_type)
25000 {
25001 /* A zero macinfo type indicates the end of the macro
25002 information. */
25003 case 0:
25004 break;
25005
25006 case DW_MACRO_define:
25007 case DW_MACRO_undef:
25008 /* Only skip the data by MAC_PTR. */
25009 {
25010 unsigned int bytes_read;
25011
25012 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25013 mac_ptr += bytes_read;
25014 read_direct_string (abfd, mac_ptr, &bytes_read);
25015 mac_ptr += bytes_read;
25016 }
25017 break;
25018
25019 case DW_MACRO_start_file:
25020 {
25021 unsigned int bytes_read;
25022 int line, file;
25023
25024 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25025 mac_ptr += bytes_read;
25026 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25027 mac_ptr += bytes_read;
25028
25029 current_file = macro_start_file (cu, file, line, current_file, lh);
25030 }
25031 break;
25032
25033 case DW_MACRO_end_file:
25034 /* No data to skip by MAC_PTR. */
25035 break;
25036
25037 case DW_MACRO_define_strp:
25038 case DW_MACRO_undef_strp:
25039 case DW_MACRO_define_sup:
25040 case DW_MACRO_undef_sup:
25041 {
25042 unsigned int bytes_read;
25043
25044 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25045 mac_ptr += bytes_read;
25046 mac_ptr += offset_size;
25047 }
25048 break;
25049
25050 case DW_MACRO_import:
25051 case DW_MACRO_import_sup:
25052 /* Note that, according to the spec, a transparent include
25053 chain cannot call DW_MACRO_start_file. So, we can just
25054 skip this opcode. */
25055 mac_ptr += offset_size;
25056 break;
25057
25058 case DW_MACINFO_vendor_ext:
25059 /* Only skip the data by MAC_PTR. */
25060 if (!section_is_gnu)
25061 {
25062 unsigned int bytes_read;
25063
25064 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25065 mac_ptr += bytes_read;
25066 read_direct_string (abfd, mac_ptr, &bytes_read);
25067 mac_ptr += bytes_read;
25068 }
25069 /* FALLTHROUGH */
25070
25071 default:
25072 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25073 mac_ptr, mac_end, abfd, offset_size,
25074 section);
25075 if (mac_ptr == NULL)
25076 return;
25077 break;
25078 }
25079 DIAGNOSTIC_POP
25080 } while (macinfo_type != 0 && current_file == NULL);
25081
25082 /* Second pass: Process all entries.
25083
25084 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25085 command-line macro definitions/undefinitions. This flag is unset when we
25086 reach the first DW_MACINFO_start_file entry. */
25087
25088 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25089 htab_eq_pointer,
25090 NULL, xcalloc, xfree));
25091 mac_ptr = section->buffer + offset;
25092 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
25093 *slot = (void *) mac_ptr;
25094 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
25095 current_file, lh, section,
25096 section_is_gnu, 0, offset_size,
25097 include_hash.get ());
25098 }
25099
25100 /* Check if the attribute's form is a DW_FORM_block*
25101 if so return true else false. */
25102
25103 static int
25104 attr_form_is_block (const struct attribute *attr)
25105 {
25106 return (attr == NULL ? 0 :
25107 attr->form == DW_FORM_block1
25108 || attr->form == DW_FORM_block2
25109 || attr->form == DW_FORM_block4
25110 || attr->form == DW_FORM_block
25111 || attr->form == DW_FORM_exprloc);
25112 }
25113
25114 /* Return non-zero if ATTR's value is a section offset --- classes
25115 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25116 You may use DW_UNSND (attr) to retrieve such offsets.
25117
25118 Section 7.5.4, "Attribute Encodings", explains that no attribute
25119 may have a value that belongs to more than one of these classes; it
25120 would be ambiguous if we did, because we use the same forms for all
25121 of them. */
25122
25123 static int
25124 attr_form_is_section_offset (const struct attribute *attr)
25125 {
25126 return (attr->form == DW_FORM_data4
25127 || attr->form == DW_FORM_data8
25128 || attr->form == DW_FORM_sec_offset);
25129 }
25130
25131 /* Return non-zero if ATTR's value falls in the 'constant' class, or
25132 zero otherwise. When this function returns true, you can apply
25133 dwarf2_get_attr_constant_value to it.
25134
25135 However, note that for some attributes you must check
25136 attr_form_is_section_offset before using this test. DW_FORM_data4
25137 and DW_FORM_data8 are members of both the constant class, and of
25138 the classes that contain offsets into other debug sections
25139 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25140 that, if an attribute's can be either a constant or one of the
25141 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
25142 taken as section offsets, not constants.
25143
25144 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25145 cannot handle that. */
25146
25147 static int
25148 attr_form_is_constant (const struct attribute *attr)
25149 {
25150 switch (attr->form)
25151 {
25152 case DW_FORM_sdata:
25153 case DW_FORM_udata:
25154 case DW_FORM_data1:
25155 case DW_FORM_data2:
25156 case DW_FORM_data4:
25157 case DW_FORM_data8:
25158 case DW_FORM_implicit_const:
25159 return 1;
25160 default:
25161 return 0;
25162 }
25163 }
25164
25165
25166 /* DW_ADDR is always stored already as sect_offset; despite for the forms
25167 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25168
25169 static int
25170 attr_form_is_ref (const struct attribute *attr)
25171 {
25172 switch (attr->form)
25173 {
25174 case DW_FORM_ref_addr:
25175 case DW_FORM_ref1:
25176 case DW_FORM_ref2:
25177 case DW_FORM_ref4:
25178 case DW_FORM_ref8:
25179 case DW_FORM_ref_udata:
25180 case DW_FORM_GNU_ref_alt:
25181 return 1;
25182 default:
25183 return 0;
25184 }
25185 }
25186
25187 /* Return the .debug_loc section to use for CU.
25188 For DWO files use .debug_loc.dwo. */
25189
25190 static struct dwarf2_section_info *
25191 cu_debug_loc_section (struct dwarf2_cu *cu)
25192 {
25193 struct dwarf2_per_objfile *dwarf2_per_objfile
25194 = cu->per_cu->dwarf2_per_objfile;
25195
25196 if (cu->dwo_unit)
25197 {
25198 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25199
25200 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25201 }
25202 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25203 : &dwarf2_per_objfile->loc);
25204 }
25205
25206 /* A helper function that fills in a dwarf2_loclist_baton. */
25207
25208 static void
25209 fill_in_loclist_baton (struct dwarf2_cu *cu,
25210 struct dwarf2_loclist_baton *baton,
25211 const struct attribute *attr)
25212 {
25213 struct dwarf2_per_objfile *dwarf2_per_objfile
25214 = cu->per_cu->dwarf2_per_objfile;
25215 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25216
25217 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
25218
25219 baton->per_cu = cu->per_cu;
25220 gdb_assert (baton->per_cu);
25221 /* We don't know how long the location list is, but make sure we
25222 don't run off the edge of the section. */
25223 baton->size = section->size - DW_UNSND (attr);
25224 baton->data = section->buffer + DW_UNSND (attr);
25225 baton->base_address = cu->base_address;
25226 baton->from_dwo = cu->dwo_unit != NULL;
25227 }
25228
25229 static void
25230 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
25231 struct dwarf2_cu *cu, int is_block)
25232 {
25233 struct dwarf2_per_objfile *dwarf2_per_objfile
25234 = cu->per_cu->dwarf2_per_objfile;
25235 struct objfile *objfile = dwarf2_per_objfile->objfile;
25236 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25237
25238 if (attr_form_is_section_offset (attr)
25239 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
25240 the section. If so, fall through to the complaint in the
25241 other branch. */
25242 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
25243 {
25244 struct dwarf2_loclist_baton *baton;
25245
25246 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
25247
25248 fill_in_loclist_baton (cu, baton, attr);
25249
25250 if (cu->base_known == 0)
25251 complaint (_("Location list used without "
25252 "specifying the CU base address."));
25253
25254 SYMBOL_ACLASS_INDEX (sym) = (is_block
25255 ? dwarf2_loclist_block_index
25256 : dwarf2_loclist_index);
25257 SYMBOL_LOCATION_BATON (sym) = baton;
25258 }
25259 else
25260 {
25261 struct dwarf2_locexpr_baton *baton;
25262
25263 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
25264 baton->per_cu = cu->per_cu;
25265 gdb_assert (baton->per_cu);
25266
25267 if (attr_form_is_block (attr))
25268 {
25269 /* Note that we're just copying the block's data pointer
25270 here, not the actual data. We're still pointing into the
25271 info_buffer for SYM's objfile; right now we never release
25272 that buffer, but when we do clean up properly this may
25273 need to change. */
25274 baton->size = DW_BLOCK (attr)->size;
25275 baton->data = DW_BLOCK (attr)->data;
25276 }
25277 else
25278 {
25279 dwarf2_invalid_attrib_class_complaint ("location description",
25280 SYMBOL_NATURAL_NAME (sym));
25281 baton->size = 0;
25282 }
25283
25284 SYMBOL_ACLASS_INDEX (sym) = (is_block
25285 ? dwarf2_locexpr_block_index
25286 : dwarf2_locexpr_index);
25287 SYMBOL_LOCATION_BATON (sym) = baton;
25288 }
25289 }
25290
25291 /* Return the OBJFILE associated with the compilation unit CU. If CU
25292 came from a separate debuginfo file, then the master objfile is
25293 returned. */
25294
25295 struct objfile *
25296 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25297 {
25298 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25299
25300 /* Return the master objfile, so that we can report and look up the
25301 correct file containing this variable. */
25302 if (objfile->separate_debug_objfile_backlink)
25303 objfile = objfile->separate_debug_objfile_backlink;
25304
25305 return objfile;
25306 }
25307
25308 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25309 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25310 CU_HEADERP first. */
25311
25312 static const struct comp_unit_head *
25313 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25314 struct dwarf2_per_cu_data *per_cu)
25315 {
25316 const gdb_byte *info_ptr;
25317
25318 if (per_cu->cu)
25319 return &per_cu->cu->header;
25320
25321 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25322
25323 memset (cu_headerp, 0, sizeof (*cu_headerp));
25324 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25325 rcuh_kind::COMPILE);
25326
25327 return cu_headerp;
25328 }
25329
25330 /* Return the address size given in the compilation unit header for CU. */
25331
25332 int
25333 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25334 {
25335 struct comp_unit_head cu_header_local;
25336 const struct comp_unit_head *cu_headerp;
25337
25338 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25339
25340 return cu_headerp->addr_size;
25341 }
25342
25343 /* Return the offset size given in the compilation unit header for CU. */
25344
25345 int
25346 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25347 {
25348 struct comp_unit_head cu_header_local;
25349 const struct comp_unit_head *cu_headerp;
25350
25351 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25352
25353 return cu_headerp->offset_size;
25354 }
25355
25356 /* See its dwarf2loc.h declaration. */
25357
25358 int
25359 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25360 {
25361 struct comp_unit_head cu_header_local;
25362 const struct comp_unit_head *cu_headerp;
25363
25364 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25365
25366 if (cu_headerp->version == 2)
25367 return cu_headerp->addr_size;
25368 else
25369 return cu_headerp->offset_size;
25370 }
25371
25372 /* Return the text offset of the CU. The returned offset comes from
25373 this CU's objfile. If this objfile came from a separate debuginfo
25374 file, then the offset may be different from the corresponding
25375 offset in the parent objfile. */
25376
25377 CORE_ADDR
25378 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25379 {
25380 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25381
25382 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25383 }
25384
25385 /* Return a type that is a generic pointer type, the size of which matches
25386 the address size given in the compilation unit header for PER_CU. */
25387 static struct type *
25388 dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25389 {
25390 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25391 struct type *void_type = objfile_type (objfile)->builtin_void;
25392 struct type *addr_type = lookup_pointer_type (void_type);
25393 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25394
25395 if (TYPE_LENGTH (addr_type) == addr_size)
25396 return addr_type;
25397
25398 addr_type
25399 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25400 return addr_type;
25401 }
25402
25403 /* Return DWARF version number of PER_CU. */
25404
25405 short
25406 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25407 {
25408 return per_cu->dwarf_version;
25409 }
25410
25411 /* Locate the .debug_info compilation unit from CU's objfile which contains
25412 the DIE at OFFSET. Raises an error on failure. */
25413
25414 static struct dwarf2_per_cu_data *
25415 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25416 unsigned int offset_in_dwz,
25417 struct dwarf2_per_objfile *dwarf2_per_objfile)
25418 {
25419 struct dwarf2_per_cu_data *this_cu;
25420 int low, high;
25421
25422 low = 0;
25423 high = dwarf2_per_objfile->all_comp_units.size () - 1;
25424 while (high > low)
25425 {
25426 struct dwarf2_per_cu_data *mid_cu;
25427 int mid = low + (high - low) / 2;
25428
25429 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25430 if (mid_cu->is_dwz > offset_in_dwz
25431 || (mid_cu->is_dwz == offset_in_dwz
25432 && mid_cu->sect_off + mid_cu->length >= sect_off))
25433 high = mid;
25434 else
25435 low = mid + 1;
25436 }
25437 gdb_assert (low == high);
25438 this_cu = dwarf2_per_objfile->all_comp_units[low];
25439 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
25440 {
25441 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25442 error (_("Dwarf Error: could not find partial DIE containing "
25443 "offset %s [in module %s]"),
25444 sect_offset_str (sect_off),
25445 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25446
25447 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25448 <= sect_off);
25449 return dwarf2_per_objfile->all_comp_units[low-1];
25450 }
25451 else
25452 {
25453 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
25454 && sect_off >= this_cu->sect_off + this_cu->length)
25455 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25456 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25457 return this_cu;
25458 }
25459 }
25460
25461 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25462
25463 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25464 : per_cu (per_cu_),
25465 mark (false),
25466 has_loclist (false),
25467 checked_producer (false),
25468 producer_is_gxx_lt_4_6 (false),
25469 producer_is_gcc_lt_4_3 (false),
25470 producer_is_icc (false),
25471 producer_is_icc_lt_14 (false),
25472 producer_is_codewarrior (false),
25473 processing_has_namespace_info (false)
25474 {
25475 per_cu->cu = this;
25476 }
25477
25478 /* Destroy a dwarf2_cu. */
25479
25480 dwarf2_cu::~dwarf2_cu ()
25481 {
25482 per_cu->cu = NULL;
25483 }
25484
25485 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25486
25487 static void
25488 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25489 enum language pretend_language)
25490 {
25491 struct attribute *attr;
25492
25493 /* Set the language we're debugging. */
25494 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25495 if (attr)
25496 set_cu_language (DW_UNSND (attr), cu);
25497 else
25498 {
25499 cu->language = pretend_language;
25500 cu->language_defn = language_def (cu->language);
25501 }
25502
25503 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25504 }
25505
25506 /* Increase the age counter on each cached compilation unit, and free
25507 any that are too old. */
25508
25509 static void
25510 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25511 {
25512 struct dwarf2_per_cu_data *per_cu, **last_chain;
25513
25514 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25515 per_cu = dwarf2_per_objfile->read_in_chain;
25516 while (per_cu != NULL)
25517 {
25518 per_cu->cu->last_used ++;
25519 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25520 dwarf2_mark (per_cu->cu);
25521 per_cu = per_cu->cu->read_in_chain;
25522 }
25523
25524 per_cu = dwarf2_per_objfile->read_in_chain;
25525 last_chain = &dwarf2_per_objfile->read_in_chain;
25526 while (per_cu != NULL)
25527 {
25528 struct dwarf2_per_cu_data *next_cu;
25529
25530 next_cu = per_cu->cu->read_in_chain;
25531
25532 if (!per_cu->cu->mark)
25533 {
25534 delete per_cu->cu;
25535 *last_chain = next_cu;
25536 }
25537 else
25538 last_chain = &per_cu->cu->read_in_chain;
25539
25540 per_cu = next_cu;
25541 }
25542 }
25543
25544 /* Remove a single compilation unit from the cache. */
25545
25546 static void
25547 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25548 {
25549 struct dwarf2_per_cu_data *per_cu, **last_chain;
25550 struct dwarf2_per_objfile *dwarf2_per_objfile
25551 = target_per_cu->dwarf2_per_objfile;
25552
25553 per_cu = dwarf2_per_objfile->read_in_chain;
25554 last_chain = &dwarf2_per_objfile->read_in_chain;
25555 while (per_cu != NULL)
25556 {
25557 struct dwarf2_per_cu_data *next_cu;
25558
25559 next_cu = per_cu->cu->read_in_chain;
25560
25561 if (per_cu == target_per_cu)
25562 {
25563 delete per_cu->cu;
25564 per_cu->cu = NULL;
25565 *last_chain = next_cu;
25566 break;
25567 }
25568 else
25569 last_chain = &per_cu->cu->read_in_chain;
25570
25571 per_cu = next_cu;
25572 }
25573 }
25574
25575 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25576 We store these in a hash table separate from the DIEs, and preserve them
25577 when the DIEs are flushed out of cache.
25578
25579 The CU "per_cu" pointer is needed because offset alone is not enough to
25580 uniquely identify the type. A file may have multiple .debug_types sections,
25581 or the type may come from a DWO file. Furthermore, while it's more logical
25582 to use per_cu->section+offset, with Fission the section with the data is in
25583 the DWO file but we don't know that section at the point we need it.
25584 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25585 because we can enter the lookup routine, get_die_type_at_offset, from
25586 outside this file, and thus won't necessarily have PER_CU->cu.
25587 Fortunately, PER_CU is stable for the life of the objfile. */
25588
25589 struct dwarf2_per_cu_offset_and_type
25590 {
25591 const struct dwarf2_per_cu_data *per_cu;
25592 sect_offset sect_off;
25593 struct type *type;
25594 };
25595
25596 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25597
25598 static hashval_t
25599 per_cu_offset_and_type_hash (const void *item)
25600 {
25601 const struct dwarf2_per_cu_offset_and_type *ofs
25602 = (const struct dwarf2_per_cu_offset_and_type *) item;
25603
25604 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25605 }
25606
25607 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25608
25609 static int
25610 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25611 {
25612 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25613 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25614 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25615 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25616
25617 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25618 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25619 }
25620
25621 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25622 table if necessary. For convenience, return TYPE.
25623
25624 The DIEs reading must have careful ordering to:
25625 * Not cause infite loops trying to read in DIEs as a prerequisite for
25626 reading current DIE.
25627 * Not trying to dereference contents of still incompletely read in types
25628 while reading in other DIEs.
25629 * Enable referencing still incompletely read in types just by a pointer to
25630 the type without accessing its fields.
25631
25632 Therefore caller should follow these rules:
25633 * Try to fetch any prerequisite types we may need to build this DIE type
25634 before building the type and calling set_die_type.
25635 * After building type call set_die_type for current DIE as soon as
25636 possible before fetching more types to complete the current type.
25637 * Make the type as complete as possible before fetching more types. */
25638
25639 static struct type *
25640 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25641 {
25642 struct dwarf2_per_objfile *dwarf2_per_objfile
25643 = cu->per_cu->dwarf2_per_objfile;
25644 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25645 struct objfile *objfile = dwarf2_per_objfile->objfile;
25646 struct attribute *attr;
25647 struct dynamic_prop prop;
25648
25649 /* For Ada types, make sure that the gnat-specific data is always
25650 initialized (if not already set). There are a few types where
25651 we should not be doing so, because the type-specific area is
25652 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25653 where the type-specific area is used to store the floatformat).
25654 But this is not a problem, because the gnat-specific information
25655 is actually not needed for these types. */
25656 if (need_gnat_info (cu)
25657 && TYPE_CODE (type) != TYPE_CODE_FUNC
25658 && TYPE_CODE (type) != TYPE_CODE_FLT
25659 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25660 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25661 && TYPE_CODE (type) != TYPE_CODE_METHOD
25662 && !HAVE_GNAT_AUX_INFO (type))
25663 INIT_GNAT_SPECIFIC (type);
25664
25665 /* Read DW_AT_allocated and set in type. */
25666 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25667 if (attr_form_is_block (attr))
25668 {
25669 struct type *prop_type
25670 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25671 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25672 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25673 }
25674 else if (attr != NULL)
25675 {
25676 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25677 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25678 sect_offset_str (die->sect_off));
25679 }
25680
25681 /* Read DW_AT_associated and set in type. */
25682 attr = dwarf2_attr (die, DW_AT_associated, cu);
25683 if (attr_form_is_block (attr))
25684 {
25685 struct type *prop_type
25686 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25687 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25688 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25689 }
25690 else if (attr != NULL)
25691 {
25692 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
25693 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25694 sect_offset_str (die->sect_off));
25695 }
25696
25697 /* Read DW_AT_data_location and set in type. */
25698 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25699 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25700 dwarf2_per_cu_addr_type (cu->per_cu)))
25701 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25702
25703 if (dwarf2_per_objfile->die_type_hash == NULL)
25704 {
25705 dwarf2_per_objfile->die_type_hash =
25706 htab_create_alloc_ex (127,
25707 per_cu_offset_and_type_hash,
25708 per_cu_offset_and_type_eq,
25709 NULL,
25710 &objfile->objfile_obstack,
25711 hashtab_obstack_allocate,
25712 dummy_obstack_deallocate);
25713 }
25714
25715 ofs.per_cu = cu->per_cu;
25716 ofs.sect_off = die->sect_off;
25717 ofs.type = type;
25718 slot = (struct dwarf2_per_cu_offset_and_type **)
25719 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25720 if (*slot)
25721 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25722 sect_offset_str (die->sect_off));
25723 *slot = XOBNEW (&objfile->objfile_obstack,
25724 struct dwarf2_per_cu_offset_and_type);
25725 **slot = ofs;
25726 return type;
25727 }
25728
25729 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25730 or return NULL if the die does not have a saved type. */
25731
25732 static struct type *
25733 get_die_type_at_offset (sect_offset sect_off,
25734 struct dwarf2_per_cu_data *per_cu)
25735 {
25736 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25737 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
25738
25739 if (dwarf2_per_objfile->die_type_hash == NULL)
25740 return NULL;
25741
25742 ofs.per_cu = per_cu;
25743 ofs.sect_off = sect_off;
25744 slot = ((struct dwarf2_per_cu_offset_and_type *)
25745 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
25746 if (slot)
25747 return slot->type;
25748 else
25749 return NULL;
25750 }
25751
25752 /* Look up the type for DIE in CU in die_type_hash,
25753 or return NULL if DIE does not have a saved type. */
25754
25755 static struct type *
25756 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25757 {
25758 return get_die_type_at_offset (die->sect_off, cu->per_cu);
25759 }
25760
25761 /* Add a dependence relationship from CU to REF_PER_CU. */
25762
25763 static void
25764 dwarf2_add_dependence (struct dwarf2_cu *cu,
25765 struct dwarf2_per_cu_data *ref_per_cu)
25766 {
25767 void **slot;
25768
25769 if (cu->dependencies == NULL)
25770 cu->dependencies
25771 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25772 NULL, &cu->comp_unit_obstack,
25773 hashtab_obstack_allocate,
25774 dummy_obstack_deallocate);
25775
25776 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25777 if (*slot == NULL)
25778 *slot = ref_per_cu;
25779 }
25780
25781 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25782 Set the mark field in every compilation unit in the
25783 cache that we must keep because we are keeping CU. */
25784
25785 static int
25786 dwarf2_mark_helper (void **slot, void *data)
25787 {
25788 struct dwarf2_per_cu_data *per_cu;
25789
25790 per_cu = (struct dwarf2_per_cu_data *) *slot;
25791
25792 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25793 reading of the chain. As such dependencies remain valid it is not much
25794 useful to track and undo them during QUIT cleanups. */
25795 if (per_cu->cu == NULL)
25796 return 1;
25797
25798 if (per_cu->cu->mark)
25799 return 1;
25800 per_cu->cu->mark = true;
25801
25802 if (per_cu->cu->dependencies != NULL)
25803 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25804
25805 return 1;
25806 }
25807
25808 /* Set the mark field in CU and in every other compilation unit in the
25809 cache that we must keep because we are keeping CU. */
25810
25811 static void
25812 dwarf2_mark (struct dwarf2_cu *cu)
25813 {
25814 if (cu->mark)
25815 return;
25816 cu->mark = true;
25817 if (cu->dependencies != NULL)
25818 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
25819 }
25820
25821 static void
25822 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25823 {
25824 while (per_cu)
25825 {
25826 per_cu->cu->mark = false;
25827 per_cu = per_cu->cu->read_in_chain;
25828 }
25829 }
25830
25831 /* Trivial hash function for partial_die_info: the hash value of a DIE
25832 is its offset in .debug_info for this objfile. */
25833
25834 static hashval_t
25835 partial_die_hash (const void *item)
25836 {
25837 const struct partial_die_info *part_die
25838 = (const struct partial_die_info *) item;
25839
25840 return to_underlying (part_die->sect_off);
25841 }
25842
25843 /* Trivial comparison function for partial_die_info structures: two DIEs
25844 are equal if they have the same offset. */
25845
25846 static int
25847 partial_die_eq (const void *item_lhs, const void *item_rhs)
25848 {
25849 const struct partial_die_info *part_die_lhs
25850 = (const struct partial_die_info *) item_lhs;
25851 const struct partial_die_info *part_die_rhs
25852 = (const struct partial_die_info *) item_rhs;
25853
25854 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25855 }
25856
25857 struct cmd_list_element *set_dwarf_cmdlist;
25858 struct cmd_list_element *show_dwarf_cmdlist;
25859
25860 static void
25861 set_dwarf_cmd (const char *args, int from_tty)
25862 {
25863 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
25864 gdb_stdout);
25865 }
25866
25867 static void
25868 show_dwarf_cmd (const char *args, int from_tty)
25869 {
25870 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
25871 }
25872
25873 bool dwarf_always_disassemble;
25874
25875 static void
25876 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25877 struct cmd_list_element *c, const char *value)
25878 {
25879 fprintf_filtered (file,
25880 _("Whether to always disassemble "
25881 "DWARF expressions is %s.\n"),
25882 value);
25883 }
25884
25885 static void
25886 show_check_physname (struct ui_file *file, int from_tty,
25887 struct cmd_list_element *c, const char *value)
25888 {
25889 fprintf_filtered (file,
25890 _("Whether to check \"physname\" is %s.\n"),
25891 value);
25892 }
25893
25894 void
25895 _initialize_dwarf2_read (void)
25896 {
25897 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25898 Set DWARF specific variables.\n\
25899 Configure DWARF variables such as the cache size."),
25900 &set_dwarf_cmdlist, "maintenance set dwarf ",
25901 0/*allow-unknown*/, &maintenance_set_cmdlist);
25902
25903 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25904 Show DWARF specific variables.\n\
25905 Show DWARF variables such as the cache size."),
25906 &show_dwarf_cmdlist, "maintenance show dwarf ",
25907 0/*allow-unknown*/, &maintenance_show_cmdlist);
25908
25909 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25910 &dwarf_max_cache_age, _("\
25911 Set the upper bound on the age of cached DWARF compilation units."), _("\
25912 Show the upper bound on the age of cached DWARF compilation units."), _("\
25913 A higher limit means that cached compilation units will be stored\n\
25914 in memory longer, and more total memory will be used. Zero disables\n\
25915 caching, which can slow down startup."),
25916 NULL,
25917 show_dwarf_max_cache_age,
25918 &set_dwarf_cmdlist,
25919 &show_dwarf_cmdlist);
25920
25921 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25922 &dwarf_always_disassemble, _("\
25923 Set whether `info address' always disassembles DWARF expressions."), _("\
25924 Show whether `info address' always disassembles DWARF expressions."), _("\
25925 When enabled, DWARF expressions are always printed in an assembly-like\n\
25926 syntax. When disabled, expressions will be printed in a more\n\
25927 conversational style, when possible."),
25928 NULL,
25929 show_dwarf_always_disassemble,
25930 &set_dwarf_cmdlist,
25931 &show_dwarf_cmdlist);
25932
25933 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25934 Set debugging of the DWARF reader."), _("\
25935 Show debugging of the DWARF reader."), _("\
25936 When enabled (non-zero), debugging messages are printed during DWARF\n\
25937 reading and symtab expansion. A value of 1 (one) provides basic\n\
25938 information. A value greater than 1 provides more verbose information."),
25939 NULL,
25940 NULL,
25941 &setdebuglist, &showdebuglist);
25942
25943 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25944 Set debugging of the DWARF DIE reader."), _("\
25945 Show debugging of the DWARF DIE reader."), _("\
25946 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25947 The value is the maximum depth to print."),
25948 NULL,
25949 NULL,
25950 &setdebuglist, &showdebuglist);
25951
25952 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25953 Set debugging of the dwarf line reader."), _("\
25954 Show debugging of the dwarf line reader."), _("\
25955 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25956 A value of 1 (one) provides basic information.\n\
25957 A value greater than 1 provides more verbose information."),
25958 NULL,
25959 NULL,
25960 &setdebuglist, &showdebuglist);
25961
25962 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25963 Set cross-checking of \"physname\" code against demangler."), _("\
25964 Show cross-checking of \"physname\" code against demangler."), _("\
25965 When enabled, GDB's internal \"physname\" code is checked against\n\
25966 the demangler."),
25967 NULL, show_check_physname,
25968 &setdebuglist, &showdebuglist);
25969
25970 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25971 no_class, &use_deprecated_index_sections, _("\
25972 Set whether to use deprecated gdb_index sections."), _("\
25973 Show whether to use deprecated gdb_index sections."), _("\
25974 When enabled, deprecated .gdb_index sections are used anyway.\n\
25975 Normally they are ignored either because of a missing feature or\n\
25976 performance issue.\n\
25977 Warning: This option must be enabled before gdb reads the file."),
25978 NULL,
25979 NULL,
25980 &setlist, &showlist);
25981
25982 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25983 &dwarf2_locexpr_funcs);
25984 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25985 &dwarf2_loclist_funcs);
25986
25987 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25988 &dwarf2_block_frame_base_locexpr_funcs);
25989 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25990 &dwarf2_block_frame_base_loclist_funcs);
25991
25992 #if GDB_SELF_TEST
25993 selftests::register_test ("dw2_expand_symtabs_matching",
25994 selftests::dw2_expand_symtabs_matching::run_test);
25995 #endif
25996 }
This page took 0.54 seconds and 5 git commands to generate.