Change section_offsets to a std::vector
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
b811d2c2 3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
c906108c
SS
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
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
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
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
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.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
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
c906108c 31#include "defs.h"
4de283e4 32#include "dwarf2read.h"
d55e5aa6
TT
33#include "dwarf-index-cache.h"
34#include "dwarf-index-common.h"
4de283e4
TT
35#include "bfd.h"
36#include "elf-bfd.h"
37#include "symtab.h"
38#include "gdbtypes.h"
39#include "objfiles.h"
d55e5aa6 40#include "dwarf2.h"
4de283e4
TT
41#include "buildsym.h"
42#include "demangle.h"
43#include "gdb-demangle.h"
4de283e4
TT
44#include "filenames.h" /* for DOSish file names */
45#include "macrotab.h"
46#include "language.h"
47#include "complaints.h"
d55e5aa6
TT
48#include "dwarf2expr.h"
49#include "dwarf2loc.h"
4de283e4
TT
50#include "cp-support.h"
51#include "hashtab.h"
52#include "command.h"
d55e5aa6 53#include "gdbcmd.h"
4de283e4
TT
54#include "block.h"
55#include "addrmap.h"
56#include "typeprint.h"
57#include "psympriv.h"
4de283e4 58#include "c-lang.h"
d55e5aa6 59#include "go-lang.h"
4de283e4
TT
60#include "valprint.h"
61#include "gdbcore.h" /* for gnutarget */
62#include "gdb/gdb-index.h"
4de283e4
TT
63#include "gdb_bfd.h"
64#include "f-lang.h"
65#include "source.h"
4de283e4 66#include "build-id.h"
d55e5aa6 67#include "namespace.h"
268a13a5
TT
68#include "gdbsupport/function-view.h"
69#include "gdbsupport/gdb_optional.h"
70#include "gdbsupport/underlying.h"
268a13a5 71#include "gdbsupport/hash_enum.h"
4de283e4 72#include "filename-seen-cache.h"
b32b108a 73#include "producer.h"
4de283e4 74#include <fcntl.h>
4de283e4 75#include <algorithm>
4de283e4 76#include <unordered_map>
268a13a5 77#include "gdbsupport/selftest.h"
c9317f21 78#include "rust-lang.h"
268a13a5 79#include "gdbsupport/pathstuff.h"
437afbb8 80
73be47f5
DE
81/* When == 1, print basic high level tracing messages.
82 When > 1, be more verbose.
b4f54984
DE
83 This is in contrast to the low level DIE reading of dwarf_die_debug. */
84static unsigned int dwarf_read_debug = 0;
45cfd468 85
d97bc12b 86/* When non-zero, dump DIEs after they are read in. */
b4f54984 87static unsigned int dwarf_die_debug = 0;
d97bc12b 88
27e0867f
DE
89/* When non-zero, dump line number entries as they are read in. */
90static unsigned int dwarf_line_debug = 0;
91
491144b5
CB
92/* When true, cross-check physname against demangler. */
93static bool check_physname = false;
900e11f9 94
491144b5
CB
95/* When true, do not reject deprecated .gdb_index sections. */
96static bool use_deprecated_index_sections = false;
481860b3 97
5bfd760d 98static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
6502dd73 99
f1e6e072
TT
100/* The "aclass" indices for various kinds of computed DWARF symbols. */
101
102static int dwarf2_locexpr_index;
103static int dwarf2_loclist_index;
104static int dwarf2_locexpr_block_index;
105static int dwarf2_loclist_block_index;
106
3f563c84
PA
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
120struct 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
44ed8f3e
PA
132/* Base class containing bits shared by both .gdb_index and
133 .debug_name indexes. */
134
135struct mapped_index_base
136{
22ca247e
TT
137 mapped_index_base () = default;
138 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
139
44ed8f3e
PA
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>
3b00ef10
TT
169 find_name_components_bounds (const lookup_name_info &ln_no_params,
170 enum language lang) const;
44ed8f3e
PA
171
172 /* Prevent deleting/destroying via a base class pointer. */
173protected:
174 ~mapped_index_base() = default;
175};
176
9291a0cd
TT
177/* A description of the mapped index. The file format is described in
178 a comment by the code that writes the index. */
fc898b42 179struct mapped_index final : public mapped_index_base
9291a0cd 180{
f00a2de2
PA
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
559a7a62 188 /* Index data format version. */
3063847f 189 int version = 0;
559a7a62 190
f00a2de2
PA
191 /* The address table data. */
192 gdb::array_view<const gdb_byte> address_table;
b11b1f88 193
3876f04e 194 /* The symbol table, implemented as a hash table. */
f00a2de2 195 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 196
9291a0cd 197 /* A pointer to the constant pool. */
3063847f 198 const char *constant_pool = nullptr;
3f563c84 199
44ed8f3e
PA
200 bool symbol_name_slot_invalid (offset_type idx) const override
201 {
202 const auto &bucket = this->symbol_table[idx];
9ab08412 203 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 204 }
5c58de74 205
3f563c84
PA
206 /* Convenience method to get at the name of the symbol at IDX in the
207 symbol table. */
44ed8f3e 208 const char *symbol_name_at (offset_type idx) const override
f00a2de2 209 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 210
44ed8f3e
PA
211 size_t symbol_name_count () const override
212 { return this->symbol_table.size (); }
9291a0cd
TT
213};
214
927aa2e7
JK
215/* A description of the mapped .debug_names.
216 Uninitialized map has CU_COUNT 0. */
fc898b42 217struct mapped_debug_names final : public mapped_index_base
927aa2e7 218{
ed2dc618
SM
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;
927aa2e7
JK
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;
44ed8f3e
PA
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; }
927aa2e7
JK
265};
266
cd4fb1b2 267/* See dwarf2read.h. */
ed2dc618 268
cd4fb1b2 269dwarf2_per_objfile *
ed2dc618
SM
270get_dwarf2_per_objfile (struct objfile *objfile)
271{
5bfd760d 272 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 273}
c906108c 274
251d32d9 275/* Default names of the debugging sections. */
c906108c 276
233a11ab
CS
277/* Note that if the debugging section has been compressed, it might
278 have a name like .zdebug_info. */
279
9cdd5dbd
DE
280static const struct dwarf2_debug_sections dwarf2_elf_names =
281{
251d32d9
TG
282 { ".debug_info", ".zdebug_info" },
283 { ".debug_abbrev", ".zdebug_abbrev" },
284 { ".debug_line", ".zdebug_line" },
285 { ".debug_loc", ".zdebug_loc" },
43988095 286 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 287 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 288 { ".debug_macro", ".zdebug_macro" },
251d32d9 289 { ".debug_str", ".zdebug_str" },
43988095 290 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 291 { ".debug_ranges", ".zdebug_ranges" },
43988095 292 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 293 { ".debug_types", ".zdebug_types" },
3019eac3 294 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
295 { ".debug_frame", ".zdebug_frame" },
296 { ".eh_frame", NULL },
24d3216f 297 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
298 { ".debug_names", ".zdebug_names" },
299 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 300 23
251d32d9 301};
c906108c 302
80626a55 303/* List of DWO/DWP sections. */
3019eac3 304
80626a55 305static const struct dwop_section_names
3019eac3
DE
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;
43988095 311 struct dwarf2_section_names loclists_dwo;
09262596
DE
312 struct dwarf2_section_names macinfo_dwo;
313 struct dwarf2_section_names macro_dwo;
3019eac3
DE
314 struct dwarf2_section_names str_dwo;
315 struct dwarf2_section_names str_offsets_dwo;
316 struct dwarf2_section_names types_dwo;
80626a55
DE
317 struct dwarf2_section_names cu_index;
318 struct dwarf2_section_names tu_index;
3019eac3 319}
80626a55 320dwop_section_names =
3019eac3
DE
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" },
43988095 326 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
327 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
328 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
329 { ".debug_str.dwo", ".zdebug_str.dwo" },
330 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
331 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
332 { ".debug_cu_index", ".zdebug_cu_index" },
333 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
334};
335
c906108c
SS
336/* local data types */
337
107d2387
AC
338/* The data in a compilation unit header, after target2host
339 translation, looks like this. */
c906108c 340struct comp_unit_head
a738430d 341{
c764a876 342 unsigned int length;
a738430d 343 short version;
a738430d
MK
344 unsigned char addr_size;
345 unsigned char signed_addr_p;
9c541725 346 sect_offset abbrev_sect_off;
57349743 347
a738430d
MK
348 /* Size of file offsets; either 4 or 8. */
349 unsigned int offset_size;
57349743 350
a738430d
MK
351 /* Size of the length field; either 4 or 12. */
352 unsigned int initial_length_size;
57349743 353
43988095
JK
354 enum dwarf_unit_type unit_type;
355
a738430d
MK
356 /* Offset to the first byte of this compilation unit header in the
357 .debug_info section, for resolving relative reference dies. */
9c541725 358 sect_offset sect_off;
57349743 359
d00adf39
DE
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. */
9c541725 362 cu_offset first_die_cu_offset;
43988095 363
a084a2a6
AT
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. */
43988095
JK
369 ULONGEST signature;
370
371 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 372 cu_offset type_cu_offset_in_tu;
a738430d 373};
c906108c 374
3da10d80
KS
375/* Type used for delaying computation of method physnames.
376 See comments for compute_delayed_physnames. */
377struct 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
e7c27a73
DJ
395/* Internal state when decoding a particular compilation unit. */
396struct dwarf2_cu
397{
fcd3b13d
SM
398 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
399 ~dwarf2_cu ();
400
401 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
402
c24bdb02
KS
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
d00adf39 419 /* The header of the compilation unit. */
fcd3b13d 420 struct comp_unit_head header {};
e142c38c 421
d00adf39 422 /* Base address of this compilation unit. */
fcd3b13d 423 CORE_ADDR base_address = 0;
d00adf39
DE
424
425 /* Non-zero if base_address has been set. */
fcd3b13d 426 int base_known = 0;
d00adf39 427
e142c38c 428 /* The language we are debugging. */
fcd3b13d
SM
429 enum language language = language_unknown;
430 const struct language_defn *language_defn = nullptr;
e142c38c 431
fcd3b13d 432 const char *producer = nullptr;
b0f35d58 433
c24bdb02 434private:
804d2729
TT
435 /* The symtab builder for this CU. This is only non-NULL when full
436 symbols are being read. */
c24bdb02 437 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 438
c24bdb02 439public:
e142c38c
DJ
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. */
fcd3b13d 449 struct pending **list_in_scope = nullptr;
e142c38c 450
b64f50a1
JK
451 /* Hash table holding all the loaded partial DIEs
452 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 453 htab_t partial_dies = nullptr;
72bf9492
DJ
454
455 /* Storage for things with the same lifetime as this read-in compilation
456 unit, including partial DIEs. */
fcd3b13d 457 auto_obstack comp_unit_obstack;
72bf9492 458
ae038cb0
DJ
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... */
fcd3b13d 463 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 464
69d751e3 465 /* Backlink to our per_cu entry. */
ae038cb0
DJ
466 struct dwarf2_per_cu_data *per_cu;
467
468 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 469 int last_used = 0;
ae038cb0 470
b64f50a1
JK
471 /* A hash table of DIE cu_offset for following references with
472 die_info->offset.sect_off as hash. */
fcd3b13d 473 htab_t die_hash = nullptr;
10b3939b
DJ
474
475 /* Full DIEs if read in. */
fcd3b13d 476 struct die_info *dies = nullptr;
10b3939b
DJ
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. */
fcd3b13d 481 htab_t dependencies = nullptr;
10b3939b 482
cb1df416 483 /* Header data from the line table, during full symbol processing. */
fcd3b13d 484 struct line_header *line_header = nullptr;
4c8aa72d
PA
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. */
fcd3b13d 490 die_info *line_header_die_owner = nullptr;
cb1df416 491
3da10d80
KS
492 /* A list of methods which need to have physnames computed
493 after all type information has been read. */
c89b44cd 494 std::vector<delayed_method_info> method_list;
3da10d80 495
96408a79 496 /* To be copied to symtab->call_site_htab. */
fcd3b13d 497 htab_t call_site_htab = nullptr;
96408a79 498
034e5797
DE
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). */
fcd3b13d 508 struct dwo_unit *dwo_unit = nullptr;
3019eac3
DE
509
510 /* The DW_AT_addr_base attribute if present, zero otherwise
511 (zero is a valid value though).
1dbab08b 512 Note this value comes from the Fission stub CU/TU's DIE. */
fcd3b13d 513 ULONGEST addr_base = 0;
3019eac3 514
2e3cf129
DE
515 /* The DW_AT_ranges_base attribute if present, zero otherwise
516 (zero is a valid value though).
1dbab08b 517 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 518 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
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. */
fcd3b13d 525 ULONGEST ranges_base = 0;
2e3cf129 526
c9317f21
TT
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
ae038cb0 535 /* Mark used when releasing cached dies. */
9068261f 536 bool mark : 1;
ae038cb0 537
8be455d7
JK
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. */
9068261f 542 bool has_loclist : 1;
ba919b58 543
9068261f 544 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
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. */
9068261f
AB
548 bool checked_producer : 1;
549 bool producer_is_gxx_lt_4_6 : 1;
550 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 551 bool producer_is_icc : 1;
9068261f 552 bool producer_is_icc_lt_14 : 1;
c258c396 553 bool producer_is_codewarrior : 1;
4d4ec4e5 554
9068261f 555 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
556 debugging info for C++ namespaces. GCC 3.3.x did not produce
557 this information, but later versions do. */
558
9068261f 559 bool processing_has_namespace_info : 1;
d590ff25
YQ
560
561 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
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 }
e7c27a73
DJ
580};
581
094b34ac
DE
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
585struct 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. */
9c541725 591 sect_offset line_sect_off;
094b34ac
DE
592};
593
f4dc4d17
DE
594/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
595 an object of this type. */
596
597struct type_unit_group
598{
0186c6a7 599 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
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. */
8a0459fd 604#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
605 struct dwarf2_per_cu_data per_cu;
606
0186c6a7
DE
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. */
a8b3b8e9 610 std::vector<signatured_type *> *tus;
f4dc4d17 611
43f3e411 612 /* The compunit symtab.
094b34ac 613 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
614 so we create an essentially anonymous symtab as the compunit symtab. */
615 struct compunit_symtab *compunit_symtab;
f4dc4d17 616
094b34ac
DE
617 /* The data used to construct the hash key. */
618 struct stmt_list_hash hash;
f4dc4d17
DE
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
73869dc2 635/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
636
637struct dwo_sections
638{
639 struct dwarf2_section_info abbrev;
3019eac3
DE
640 struct dwarf2_section_info line;
641 struct dwarf2_section_info loc;
43988095 642 struct dwarf2_section_info loclists;
09262596
DE
643 struct dwarf2_section_info macinfo;
644 struct dwarf2_section_info macro;
3019eac3
DE
645 struct dwarf2_section_info str;
646 struct dwarf2_section_info str_offsets;
80626a55
DE
647 /* In the case of a virtual DWO file, these two are unused. */
648 struct dwarf2_section_info info;
fd5866f6 649 std::vector<dwarf2_section_info> types;
3019eac3
DE
650};
651
c88ee1f0 652/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
653
654struct 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. */
8a0459fd 665 struct dwarf2_section_info *section;
3019eac3 666
9c541725
PA
667 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
668 sect_offset sect_off;
3019eac3
DE
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
73869dc2
DE
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
679enum dwp_v2_section_ids
680{
681 DW_SECT_MIN = 1
682};
683
80626a55 684/* Data for one DWO file.
57d63ce2
DE
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). */
3019eac3
DE
694
695struct dwo_file
696{
51ac9db5
SM
697 dwo_file () = default;
698 DISABLE_COPY_AND_ASSIGN (dwo_file);
699
0ac5b59e 700 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
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. */
51ac9db5 704 const char *dwo_name = nullptr;
0ac5b59e
DE
705
706 /* The DW_AT_comp_dir attribute. */
51ac9db5 707 const char *comp_dir = nullptr;
3019eac3 708
80626a55
DE
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. */
fb1eb2f9 711 gdb_bfd_ref_ptr dbfd;
3019eac3 712
73869dc2
DE
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). */
51ac9db5 716 struct dwo_sections sections {};
3019eac3 717
33c5cd75
DB
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). */
51ac9db5 722 htab_t cus {};
3019eac3
DE
723
724 /* Table of TUs in the file.
725 Each element is a struct dwo_unit. */
51ac9db5 726 htab_t tus {};
3019eac3
DE
727};
728
80626a55
DE
729/* These sections are what may appear in a DWP file. */
730
731struct dwp_sections
732{
73869dc2 733 /* These are used by both DWP version 1 and 2. */
80626a55
DE
734 struct dwarf2_section_info str;
735 struct dwarf2_section_info cu_index;
736 struct dwarf2_section_info tu_index;
73869dc2
DE
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;
80626a55
DE
754};
755
73869dc2
DE
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. */
80626a55 758
73869dc2 759struct virtual_v1_dwo_sections
80626a55
DE
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.
8a0459fd 768 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
769 struct dwarf2_section_info info_or_types;
770};
771
73869dc2
DE
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
777struct 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
80626a55
DE
803/* Contents of DWP hash tables. */
804
805struct dwp_hash_table
806{
73869dc2 807 uint32_t version, nr_columns;
80626a55 808 uint32_t nr_units, nr_slots;
73869dc2
DE
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;
80626a55
DE
832};
833
834/* Data for one DWP file. */
835
836struct dwp_file
837{
400174b1
TT
838 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
839 : name (name_),
840 dbfd (std::move (abfd))
841 {
842 }
843
80626a55
DE
844 /* Name of the file. */
845 const char *name;
846
73869dc2 847 /* File format version. */
400174b1 848 int version = 0;
73869dc2 849
93417882 850 /* The bfd. */
400174b1 851 gdb_bfd_ref_ptr dbfd;
80626a55
DE
852
853 /* Section info for this file. */
400174b1 854 struct dwp_sections sections {};
80626a55 855
57d63ce2 856 /* Table of CUs in the file. */
400174b1 857 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
858
859 /* Table of TUs in the file. */
400174b1 860 const struct dwp_hash_table *tus = nullptr;
80626a55 861
19ac8c2e 862 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
400174b1
TT
863 htab_t loaded_cus {};
864 htab_t loaded_tus {};
80626a55 865
73869dc2
DE
866 /* Table to map ELF section numbers to their sections.
867 This is only needed for the DWP V1 file format. */
400174b1
TT
868 unsigned int num_sections = 0;
869 asection **elf_sections = nullptr;
80626a55
DE
870};
871
0963b4bd
MS
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
dee91e82 875 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
876
877struct die_reader_specs
878{
a32a8923 879 /* The bfd of die_section. */
93311388
DE
880 bfd* abfd;
881
882 /* The CU of the DIE we are parsing. */
883 struct dwarf2_cu *cu;
884
80626a55 885 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
886 struct dwo_file *dwo_file;
887
dee91e82 888 /* The section the die comes from.
3019eac3 889 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
890 struct dwarf2_section_info *die_section;
891
892 /* die_section->buffer. */
d521ce57 893 const gdb_byte *buffer;
f664829e
DE
894
895 /* The end of the buffer. */
896 const gdb_byte *buffer_end;
a2ce51a0
DE
897
898 /* The value of the DW_AT_comp_dir attribute. */
899 const char *comp_dir;
685af9cd
TT
900
901 /* The abbreviation table to use when reading the DIEs. */
902 struct abbrev_table *abbrev_table;
93311388
DE
903};
904
fd820528 905/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 906typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 907 const gdb_byte *info_ptr,
dee91e82
DE
908 struct die_info *comp_unit_die,
909 int has_children,
910 void *data);
911
7ba99d21
AT
912/* dir_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5 and
913 later. */
914typedef int dir_index;
ecfb656c 915
7ba99d21
AT
916/* file_name_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5
917 and later. */
918typedef int file_name_index;
ecfb656c 919
52059ffd
TT
920struct file_entry
921{
fff8551c
PA
922 file_entry () = default;
923
ecfb656c 924 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
925 unsigned int mod_time_, unsigned int length_)
926 : name (name_),
ecfb656c 927 d_index (d_index_),
fff8551c
PA
928 mod_time (mod_time_),
929 length (length_)
930 {}
931
ecfb656c
PA
932 /* Return the include directory at D_INDEX stored in LH. Returns
933 NULL if D_INDEX is out of bounds. */
8c43009f
PA
934 const char *include_dir (const line_header *lh) const;
935
fff8551c
PA
936 /* The file name. Note this is an observing pointer. The memory is
937 owned by debug_line_buffer. */
938 const char *name {};
939
8c43009f 940 /* The directory index (1-based). */
ecfb656c 941 dir_index d_index {};
fff8551c
PA
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
83769d0b 950 /* The associated symbol table, if any. */
fff8551c 951 struct symtab *symtab {};
52059ffd
TT
952};
953
debd256d
JB
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. */
957struct line_header
958{
fff8551c
PA
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. */
ecfb656c 967 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
968 unsigned int mod_time, unsigned int length);
969
7ba99d21
AT
970 /* Return the include dir at INDEX (0-based in DWARF 5 and 1-based before).
971 Returns NULL if INDEX is out of bounds. */
ecfb656c 972 const char *include_dir_at (dir_index index) const
8c43009f 973 {
7ba99d21
AT
974 int vec_index;
975 if (version >= 5)
976 vec_index = index;
977 else
978 vec_index = index - 1;
979 if (vec_index < 0 || vec_index >= m_include_dirs.size ())
8c43009f 980 return NULL;
7ba99d21 981 return m_include_dirs[vec_index];
8c43009f
PA
982 }
983
7ba99d21 984 bool is_valid_file_index (int file_index)
8c43009f 985 {
7ba99d21
AT
986 if (version >= 5)
987 return 0 <= file_index && file_index < file_names_size ();
988 return 1 <= file_index && file_index <= file_names_size ();
989 }
ecfb656c 990
7ba99d21
AT
991 /* Return the file name at INDEX (0-based in DWARF 5 and 1-based before).
992 Returns NULL if INDEX is out of bounds. */
993 file_entry *file_name_at (file_name_index index)
994 {
995 int vec_index;
996 if (version >= 5)
997 vec_index = index;
998 else
999 vec_index = index - 1;
1000 if (vec_index < 0 || vec_index >= m_file_names.size ())
fff8551c 1001 return NULL;
7ba99d21 1002 return &m_file_names[vec_index];
fff8551c
PA
1003 }
1004
7ba99d21
AT
1005 /* The indexes are 0-based in DWARF 5 and 1-based in DWARF 4. Therefore,
1006 this method should only be used to iterate through all file entries in an
1007 index-agnostic manner. */
1008 std::vector<file_entry> &file_names ()
1009 { return m_file_names; }
1010
527f3840 1011 /* Offset of line number information in .debug_line section. */
9c541725 1012 sect_offset sect_off {};
527f3840
JK
1013
1014 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1015 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1016
1017 unsigned int total_length {};
1018 unsigned short version {};
1019 unsigned int header_length {};
1020 unsigned char minimum_instruction_length {};
1021 unsigned char maximum_ops_per_instruction {};
1022 unsigned char default_is_stmt {};
1023 int line_base {};
1024 unsigned char line_range {};
1025 unsigned char opcode_base {};
debd256d
JB
1026
1027 /* standard_opcode_lengths[i] is the number of operands for the
1028 standard opcode whose value is i. This means that
1029 standard_opcode_lengths[0] is unused, and the last meaningful
1030 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1031 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1032
7ba99d21
AT
1033 int file_names_size ()
1034 { return m_file_names.size(); }
debd256d
JB
1035
1036 /* The start and end of the statement program following this
6502dd73 1037 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1038 const gdb_byte *statement_program_start {}, *statement_program_end {};
7ba99d21
AT
1039
1040 private:
1041 /* The include_directories table. Note these are observing
1042 pointers. The memory is owned by debug_line_buffer. */
1043 std::vector<const char *> m_include_dirs;
1044
1045 /* The file_names table. This is private because the meaning of indexes
1046 differs among DWARF versions (The first valid index is 1 in DWARF 4 and
1047 before, and is 0 in DWARF 5 and later). So the client should use
1048 file_name_at method for access. */
1049 std::vector<file_entry> m_file_names;
debd256d 1050};
c906108c 1051
fff8551c
PA
1052typedef std::unique_ptr<line_header> line_header_up;
1053
8c43009f
PA
1054const char *
1055file_entry::include_dir (const line_header *lh) const
1056{
ecfb656c 1057 return lh->include_dir_at (d_index);
8c43009f
PA
1058}
1059
c906108c 1060/* When we construct a partial symbol table entry we only
0963b4bd 1061 need this much information. */
6f06d47b 1062struct partial_die_info : public allocate_on_obstack
c906108c 1063 {
6f06d47b
YQ
1064 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1065
1066 /* Disable assign but still keep copy ctor, which is needed
1067 load_partial_dies. */
1068 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1069
52356b79
YQ
1070 /* Adjust the partial die before generating a symbol for it. This
1071 function may set the is_external flag or change the DIE's
1072 name. */
1073 void fixup (struct dwarf2_cu *cu);
1074
48fbe735
YQ
1075 /* Read a minimal amount of information into the minimal die
1076 structure. */
1077 const gdb_byte *read (const struct die_reader_specs *reader,
1078 const struct abbrev_info &abbrev,
1079 const gdb_byte *info_ptr);
1080
72bf9492 1081 /* Offset of this DIE. */
6f06d47b 1082 const sect_offset sect_off;
72bf9492
DJ
1083
1084 /* DWARF-2 tag for this DIE. */
6f06d47b 1085 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1086
72bf9492 1087 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1088 const unsigned int has_children : 1;
1089
72bf9492
DJ
1090 unsigned int is_external : 1;
1091 unsigned int is_declaration : 1;
1092 unsigned int has_type : 1;
1093 unsigned int has_specification : 1;
1094 unsigned int has_pc_info : 1;
481860b3 1095 unsigned int may_be_inlined : 1;
72bf9492 1096
0c1b455e
TT
1097 /* This DIE has been marked DW_AT_main_subprogram. */
1098 unsigned int main_subprogram : 1;
1099
72bf9492
DJ
1100 /* Flag set if the SCOPE field of this structure has been
1101 computed. */
1102 unsigned int scope_set : 1;
1103
fa4028e9
JB
1104 /* Flag set if the DIE has a byte_size attribute. */
1105 unsigned int has_byte_size : 1;
1106
ff908ebf
AW
1107 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1108 unsigned int has_const_value : 1;
1109
98bfdba5
PA
1110 /* Flag set if any of the DIE's children are template arguments. */
1111 unsigned int has_template_arguments : 1;
1112
52356b79 1113 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1114 unsigned int fixup_called : 1;
1115
36586728
TT
1116 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1117 unsigned int is_dwz : 1;
1118
1119 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1120 unsigned int spec_is_dwz : 1;
1121
72bf9492 1122 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1123 sometimes a default name for unnamed DIEs. */
6f06d47b 1124 const char *name = nullptr;
72bf9492 1125
abc72ce4 1126 /* The linkage name, if present. */
6f06d47b 1127 const char *linkage_name = nullptr;
abc72ce4 1128
72bf9492
DJ
1129 /* The scope to prepend to our children. This is generally
1130 allocated on the comp_unit_obstack, so will disappear
1131 when this compilation unit leaves the cache. */
6f06d47b 1132 const char *scope = nullptr;
72bf9492 1133
95554aad
TT
1134 /* Some data associated with the partial DIE. The tag determines
1135 which field is live. */
1136 union
1137 {
1138 /* The location description associated with this DIE, if any. */
1139 struct dwarf_block *locdesc;
1140 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1141 sect_offset sect_off;
6f06d47b 1142 } d {};
72bf9492
DJ
1143
1144 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1145 CORE_ADDR lowpc = 0;
1146 CORE_ADDR highpc = 0;
72bf9492 1147
93311388 1148 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1149 DW_AT_sibling, if any. */
48fbe735
YQ
1150 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1151 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1152 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1153
1154 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1155 DW_AT_specification (or DW_AT_abstract_origin or
1156 DW_AT_extension). */
6f06d47b 1157 sect_offset spec_offset {};
72bf9492
DJ
1158
1159 /* Pointers to this DIE's parent, first child, and next sibling,
1160 if any. */
6f06d47b
YQ
1161 struct partial_die_info *die_parent = nullptr;
1162 struct partial_die_info *die_child = nullptr;
1163 struct partial_die_info *die_sibling = nullptr;
1164
1165 friend struct partial_die_info *
1166 dwarf2_cu::find_partial_die (sect_offset sect_off);
1167
1168 private:
1169 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1170 partial_die_info (sect_offset sect_off)
1171 : partial_die_info (sect_off, DW_TAG_padding, 0)
1172 {
1173 }
1174
1175 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1176 int has_children_)
1177 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1178 {
1179 is_external = 0;
1180 is_declaration = 0;
1181 has_type = 0;
1182 has_specification = 0;
1183 has_pc_info = 0;
1184 may_be_inlined = 0;
1185 main_subprogram = 0;
1186 scope_set = 0;
1187 has_byte_size = 0;
1188 has_const_value = 0;
1189 has_template_arguments = 0;
1190 fixup_called = 0;
1191 is_dwz = 0;
1192 spec_is_dwz = 0;
1193 }
c906108c
SS
1194 };
1195
0963b4bd 1196/* This data structure holds the information of an abbrev. */
c906108c
SS
1197struct abbrev_info
1198 {
1199 unsigned int number; /* number identifying abbrev */
1200 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1201 unsigned short has_children; /* boolean */
1202 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1203 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1204 struct abbrev_info *next; /* next in chain */
1205 };
1206
1207struct attr_abbrev
1208 {
9d25dd43
DE
1209 ENUM_BITFIELD(dwarf_attribute) name : 16;
1210 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1211
1212 /* It is valid only if FORM is DW_FORM_implicit_const. */
1213 LONGEST implicit_const;
c906108c
SS
1214 };
1215
433df2d4
DE
1216/* Size of abbrev_table.abbrev_hash_table. */
1217#define ABBREV_HASH_SIZE 121
1218
1219/* Top level data structure to contain an abbreviation table. */
1220
1221struct abbrev_table
1222{
685af9cd
TT
1223 explicit abbrev_table (sect_offset off)
1224 : sect_off (off)
1225 {
4a17f768 1226 m_abbrevs =
685af9cd 1227 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
4a17f768 1228 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
685af9cd
TT
1229 }
1230
1231 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1232
1233 /* Allocate space for a struct abbrev_info object in
1234 ABBREV_TABLE. */
1235 struct abbrev_info *alloc_abbrev ();
1236
1237 /* Add an abbreviation to the table. */
1238 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1239
1240 /* Look up an abbrev in the table.
1241 Returns NULL if the abbrev is not found. */
1242
1243 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1244
1245
f4dc4d17
DE
1246 /* Where the abbrev table came from.
1247 This is used as a sanity check when the table is used. */
685af9cd 1248 const sect_offset sect_off;
433df2d4
DE
1249
1250 /* Storage for the abbrev table. */
685af9cd 1251 auto_obstack abbrev_obstack;
433df2d4 1252
4a17f768
YQ
1253private:
1254
433df2d4
DE
1255 /* Hash table of abbrevs.
1256 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1257 It could be statically allocated, but the previous code didn't so we
1258 don't either. */
4a17f768 1259 struct abbrev_info **m_abbrevs;
433df2d4
DE
1260};
1261
685af9cd
TT
1262typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1263
0963b4bd 1264/* Attributes have a name and a value. */
b60c80d6
DJ
1265struct attribute
1266 {
9d25dd43 1267 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1268 ENUM_BITFIELD(dwarf_form) form : 15;
1269
1270 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1271 field should be in u.str (existing only for DW_STRING) but it is kept
1272 here for better struct attribute alignment. */
1273 unsigned int string_is_canonical : 1;
1274
b60c80d6
DJ
1275 union
1276 {
15d034d0 1277 const char *str;
b60c80d6 1278 struct dwarf_block *blk;
43bbcdc2
PH
1279 ULONGEST unsnd;
1280 LONGEST snd;
b60c80d6 1281 CORE_ADDR addr;
ac9ec31b 1282 ULONGEST signature;
b60c80d6
DJ
1283 }
1284 u;
1285 };
1286
0963b4bd 1287/* This data structure holds a complete die structure. */
c906108c
SS
1288struct die_info
1289 {
76815b17
DE
1290 /* DWARF-2 tag for this DIE. */
1291 ENUM_BITFIELD(dwarf_tag) tag : 16;
1292
1293 /* Number of attributes */
98bfdba5
PA
1294 unsigned char num_attrs;
1295
1296 /* True if we're presently building the full type name for the
1297 type derived from this DIE. */
1298 unsigned char building_fullname : 1;
76815b17 1299
adde2bff
DE
1300 /* True if this die is in process. PR 16581. */
1301 unsigned char in_process : 1;
1302
76815b17
DE
1303 /* Abbrev number */
1304 unsigned int abbrev;
1305
93311388 1306 /* Offset in .debug_info or .debug_types section. */
9c541725 1307 sect_offset sect_off;
78ba4af6
JB
1308
1309 /* The dies in a compilation unit form an n-ary tree. PARENT
1310 points to this die's parent; CHILD points to the first child of
1311 this node; and all the children of a given node are chained
4950bc1c 1312 together via their SIBLING fields. */
639d11d3
DC
1313 struct die_info *child; /* Its first child, if any. */
1314 struct die_info *sibling; /* Its next sibling, if any. */
1315 struct die_info *parent; /* Its parent, if any. */
c906108c 1316
b60c80d6
DJ
1317 /* An array of attributes, with NUM_ATTRS elements. There may be
1318 zero, but it's not common and zero-sized arrays are not
1319 sufficiently portable C. */
1320 struct attribute attrs[1];
c906108c
SS
1321 };
1322
0963b4bd 1323/* Get at parts of an attribute structure. */
c906108c
SS
1324
1325#define DW_STRING(attr) ((attr)->u.str)
8285870a 1326#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1327#define DW_UNSND(attr) ((attr)->u.unsnd)
1328#define DW_BLOCK(attr) ((attr)->u.blk)
1329#define DW_SND(attr) ((attr)->u.snd)
1330#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1331#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1332
0963b4bd 1333/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1334struct dwarf_block
1335 {
56eb65bd 1336 size_t size;
1d6edc3c
JK
1337
1338 /* Valid only if SIZE is not zero. */
d521ce57 1339 const gdb_byte *data;
c906108c
SS
1340 };
1341
c906108c
SS
1342/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1343 but this would require a corresponding change in unpack_field_as_long
1344 and friends. */
1345static int bits_per_byte = 8;
1346
2ddeaf8a
TT
1347/* When reading a variant or variant part, we track a bit more
1348 information about the field, and store it in an object of this
1349 type. */
1350
1351struct variant_field
1352{
1353 /* If we see a DW_TAG_variant, then this will be the discriminant
1354 value. */
1355 ULONGEST discriminant_value;
1356 /* If we see a DW_TAG_variant, then this will be set if this is the
1357 default branch. */
1358 bool default_branch;
1359 /* While reading a DW_TAG_variant_part, this will be set if this
1360 field is the discriminant. */
1361 bool is_discriminant;
1362};
1363
52059ffd
TT
1364struct nextfield
1365{
be2daae6
TT
1366 int accessibility = 0;
1367 int virtuality = 0;
2ddeaf8a 1368 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1369 struct variant_field variant {};
1370 struct field field {};
52059ffd
TT
1371};
1372
1373struct fnfieldlist
1374{
be2daae6
TT
1375 const char *name = nullptr;
1376 std::vector<struct fn_field> fnfields;
52059ffd
TT
1377};
1378
c906108c
SS
1379/* The routines that read and process dies for a C struct or C++ class
1380 pass lists of data member fields and lists of member function fields
1381 in an instance of a field_info structure, as defined below. */
1382struct field_info
c5aa993b 1383 {
0963b4bd 1384 /* List of data member and baseclasses fields. */
be2daae6
TT
1385 std::vector<struct nextfield> fields;
1386 std::vector<struct nextfield> baseclasses;
c906108c 1387
7d0ccb61 1388 /* Number of fields (including baseclasses). */
be2daae6 1389 int nfields = 0;
c906108c 1390
85102364 1391 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1392 int non_public_fields = 0;
c906108c 1393
c5aa993b
JM
1394 /* Member function fieldlist array, contains name of possibly overloaded
1395 member function, number of overloaded member functions and a pointer
1396 to the head of the member function field chain. */
be2daae6 1397 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1398
1399 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1400 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1401 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1402
1403 /* Nested types defined by this class and the number of elements in this
1404 list. */
be2daae6 1405 std::vector<struct decl_field> nested_types_list;
c5aa993b 1406 };
c906108c 1407
10b3939b
DJ
1408/* One item on the queue of compilation units to read in full symbols
1409 for. */
1410struct dwarf2_queue_item
1411{
1412 struct dwarf2_per_cu_data *per_cu;
95554aad 1413 enum language pretend_language;
10b3939b
DJ
1414 struct dwarf2_queue_item *next;
1415};
1416
1417/* The current queue. */
1418static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1419
ae038cb0
DJ
1420/* Loaded secondary compilation units are kept in memory until they
1421 have not been referenced for the processing of this many
1422 compilation units. Set this to zero to disable caching. Cache
1423 sizes of up to at least twenty will improve startup time for
1424 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1425static int dwarf_max_cache_age = 5;
920d2a44 1426static void
b4f54984
DE
1427show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1428 struct cmd_list_element *c, const char *value)
920d2a44 1429{
3e43a32a 1430 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1431 "DWARF compilation units is %s.\n"),
920d2a44
AC
1432 value);
1433}
4390d890 1434\f
c906108c
SS
1435/* local function prototypes */
1436
a32a8923
DE
1437static const char *get_section_name (const struct dwarf2_section_info *);
1438
1439static const char *get_section_file_name (const struct dwarf2_section_info *);
1440
918dd910
JK
1441static void dwarf2_find_base_address (struct die_info *die,
1442 struct dwarf2_cu *cu);
1443
0018ea6f
DE
1444static struct partial_symtab *create_partial_symtab
1445 (struct dwarf2_per_cu_data *per_cu, const char *name);
1446
f1902523
JK
1447static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1448 const gdb_byte *info_ptr,
1449 struct die_info *type_unit_die,
1450 int has_children, void *data);
1451
ed2dc618
SM
1452static void dwarf2_build_psymtabs_hard
1453 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1454
72bf9492
DJ
1455static void scan_partial_symbols (struct partial_die_info *,
1456 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1457 int, struct dwarf2_cu *);
c906108c 1458
72bf9492
DJ
1459static void add_partial_symbol (struct partial_die_info *,
1460 struct dwarf2_cu *);
63d06c5c 1461
72bf9492
DJ
1462static void add_partial_namespace (struct partial_die_info *pdi,
1463 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1464 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1465
5d7cb8df 1466static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1467 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1468 struct dwarf2_cu *cu);
1469
72bf9492
DJ
1470static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1471 struct dwarf2_cu *cu);
91c24f0a 1472
bc30ff58
JB
1473static void add_partial_subprogram (struct partial_die_info *pdi,
1474 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1475 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1476
257e7a09
YQ
1477static void dwarf2_read_symtab (struct partial_symtab *,
1478 struct objfile *);
c906108c 1479
a14ed312 1480static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1481
685af9cd 1482static abbrev_table_up abbrev_table_read_table
ed2dc618
SM
1483 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1484 sect_offset);
433df2d4 1485
d521ce57 1486static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1487
dee91e82 1488static struct partial_die_info *load_partial_dies
d521ce57 1489 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1490
fb816e8b
TV
1491/* A pair of partial_die_info and compilation unit. */
1492struct cu_partial_die_info
1493{
1494 /* The compilation unit of the partial_die_info. */
1495 struct dwarf2_cu *cu;
1496 /* A partial_die_info. */
1497 struct partial_die_info *pdi;
122cf0f2
AB
1498
1499 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1500 : cu (cu),
1501 pdi (pdi)
405feb71 1502 { /* Nothing. */ }
122cf0f2
AB
1503
1504private:
1505 cu_partial_die_info () = delete;
fb816e8b
TV
1506};
1507
122cf0f2
AB
1508static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1509 struct dwarf2_cu *);
72bf9492 1510
d521ce57
TT
1511static const gdb_byte *read_attribute (const struct die_reader_specs *,
1512 struct attribute *, struct attr_abbrev *,
1513 const gdb_byte *);
a8329558 1514
a1855c1d 1515static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1516
a1855c1d 1517static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1518
a1855c1d 1519static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1520
15f18d14
AT
1521/* Read the next three bytes (little-endian order) as an unsigned integer. */
1522static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1523
a1855c1d 1524static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1525
a1855c1d 1526static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1527
d521ce57 1528static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1529 unsigned int *);
c906108c 1530
d521ce57 1531static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1532
1533static LONGEST read_checked_initial_length_and_offset
d521ce57 1534 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1535 unsigned int *, unsigned int *);
613e1657 1536
d521ce57
TT
1537static LONGEST read_offset (bfd *, const gdb_byte *,
1538 const struct comp_unit_head *,
c764a876
DE
1539 unsigned int *);
1540
d521ce57 1541static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1542
ed2dc618
SM
1543static sect_offset read_abbrev_offset
1544 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1545 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1546
d521ce57 1547static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1548
d521ce57 1549static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1550
ed2dc618
SM
1551static const char *read_indirect_string
1552 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1553 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1554
ed2dc618
SM
1555static const char *read_indirect_line_string
1556 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1557 const struct comp_unit_head *, unsigned int *);
36586728 1558
ed2dc618
SM
1559static const char *read_indirect_string_at_offset
1560 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1561 LONGEST str_offset);
927aa2e7 1562
ed2dc618
SM
1563static const char *read_indirect_string_from_dwz
1564 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1565
d521ce57 1566static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1567
d521ce57
TT
1568static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1569 const gdb_byte *,
3019eac3
DE
1570 unsigned int *);
1571
d521ce57 1572static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1573 ULONGEST str_index);
3019eac3 1574
e142c38c 1575static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1576
e142c38c
DJ
1577static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1578 struct dwarf2_cu *);
c906108c 1579
348e048f 1580static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1581 unsigned int);
348e048f 1582
7d45c7c3
KB
1583static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1584 struct dwarf2_cu *cu);
1585
a084a2a6
AT
1586static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1587
05cf31d1
JB
1588static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1589 struct dwarf2_cu *cu);
1590
e142c38c 1591static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1592
e142c38c 1593static struct die_info *die_specification (struct die_info *die,
f2f0e013 1594 struct dwarf2_cu **);
63d06c5c 1595
9c541725 1596static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1597 struct dwarf2_cu *cu);
debd256d 1598
f3f5162e 1599static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1600 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1601 CORE_ADDR, int decode_mapping);
c906108c 1602
804d2729
TT
1603static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1604 const char *);
c906108c 1605
a14ed312 1606static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1607 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1608
ff39bb5e 1609static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1610 struct dwarf2_cu *);
c906108c 1611
ff39bb5e 1612static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1613 struct type *type,
1614 const char *name,
1615 struct obstack *obstack,
12df843f 1616 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1617 const gdb_byte **bytes,
98bfdba5 1618 struct dwarf2_locexpr_baton **baton);
2df3850c 1619
e7c27a73 1620static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1621
b4ba55a1
JB
1622static int need_gnat_info (struct dwarf2_cu *);
1623
3e43a32a
MS
1624static struct type *die_descriptive_type (struct die_info *,
1625 struct dwarf2_cu *);
b4ba55a1
JB
1626
1627static void set_descriptive_type (struct type *, struct die_info *,
1628 struct dwarf2_cu *);
1629
e7c27a73
DJ
1630static struct type *die_containing_type (struct die_info *,
1631 struct dwarf2_cu *);
c906108c 1632
ff39bb5e 1633static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1634 struct dwarf2_cu *);
c906108c 1635
f792889a 1636static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1637
673bfd45
DE
1638static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1639
0d5cff50 1640static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1641
6e70227d 1642static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1643 const char *suffix, int physname,
1644 struct dwarf2_cu *cu);
63d06c5c 1645
e7c27a73 1646static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1647
348e048f
DE
1648static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1649
e7c27a73 1650static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1651
e7c27a73 1652static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1653
96408a79
SA
1654static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1655
71a3c369
TT
1656static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1657
ff013f42
JK
1658static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1659 struct dwarf2_cu *, struct partial_symtab *);
1660
3a2b436a 1661/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1662 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1663enum pc_bounds_kind
1664{
e385593e 1665 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1666 PC_BOUNDS_NOT_PRESENT,
1667
e385593e
JK
1668 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1669 were present but they do not form a valid range of PC addresses. */
1670 PC_BOUNDS_INVALID,
1671
3a2b436a
JK
1672 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1673 PC_BOUNDS_RANGES,
1674
1675 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1676 PC_BOUNDS_HIGH_LOW,
1677};
1678
1679static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1680 CORE_ADDR *, CORE_ADDR *,
1681 struct dwarf2_cu *,
1682 struct partial_symtab *);
c906108c 1683
fae299cd
DC
1684static void get_scope_pc_bounds (struct die_info *,
1685 CORE_ADDR *, CORE_ADDR *,
1686 struct dwarf2_cu *);
1687
801e3a5b
JB
1688static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1689 CORE_ADDR, struct dwarf2_cu *);
1690
a14ed312 1691static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1692 struct dwarf2_cu *);
c906108c 1693
a14ed312 1694static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1695 struct type *, struct dwarf2_cu *);
c906108c 1696
a14ed312 1697static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1698 struct die_info *, struct type *,
e7c27a73 1699 struct dwarf2_cu *);
c906108c 1700
a14ed312 1701static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1702 struct type *,
1703 struct dwarf2_cu *);
c906108c 1704
134d01f1 1705static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1706
e7c27a73 1707static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1708
e7c27a73 1709static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1710
5d7cb8df
JK
1711static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1712
804d2729 1713static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1714
27aa8d6a
SW
1715static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1716
74921315
KS
1717static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1718
f55ee35c
JK
1719static struct type *read_module_type (struct die_info *die,
1720 struct dwarf2_cu *cu);
1721
38d518c9 1722static const char *namespace_name (struct die_info *die,
e142c38c 1723 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1724
134d01f1 1725static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1726
e7c27a73 1727static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1728
6e70227d 1729static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1730 struct dwarf2_cu *);
1731
bf6af496 1732static struct die_info *read_die_and_siblings_1
d521ce57 1733 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1734 struct die_info *);
639d11d3 1735
dee91e82 1736static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1737 const gdb_byte *info_ptr,
1738 const gdb_byte **new_info_ptr,
639d11d3
DC
1739 struct die_info *parent);
1740
d521ce57
TT
1741static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1742 struct die_info **, const gdb_byte *,
1743 int *, int);
3019eac3 1744
d521ce57
TT
1745static const gdb_byte *read_full_die (const struct die_reader_specs *,
1746 struct die_info **, const gdb_byte *,
1747 int *);
93311388 1748
e7c27a73 1749static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1750
15d034d0
TT
1751static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1752 struct obstack *);
71c25dea 1753
15d034d0 1754static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1755
15d034d0 1756static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1757 struct die_info *die,
1758 struct dwarf2_cu *cu);
1759
ca69b9e6
DE
1760static const char *dwarf2_physname (const char *name, struct die_info *die,
1761 struct dwarf2_cu *cu);
1762
e142c38c 1763static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1764 struct dwarf2_cu **);
9219021c 1765
f39c6ffd 1766static const char *dwarf_tag_name (unsigned int);
c906108c 1767
f39c6ffd 1768static const char *dwarf_attr_name (unsigned int);
c906108c 1769
a084a2a6
AT
1770static const char *dwarf_unit_type_name (int unit_type);
1771
f39c6ffd 1772static const char *dwarf_form_name (unsigned int);
c906108c 1773
a121b7c1 1774static const char *dwarf_bool_name (unsigned int);
c906108c 1775
f39c6ffd 1776static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1777
f9aca02d 1778static struct die_info *sibling_die (struct die_info *);
c906108c 1779
d97bc12b
DE
1780static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1781
1782static void dump_die_for_error (struct die_info *);
1783
1784static void dump_die_1 (struct ui_file *, int level, int max_level,
1785 struct die_info *);
c906108c 1786
d97bc12b 1787/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1788
51545339 1789static void store_in_ref_table (struct die_info *,
10b3939b 1790 struct dwarf2_cu *);
c906108c 1791
ff39bb5e 1792static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1793
ff39bb5e 1794static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1795
348e048f 1796static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1797 const struct attribute *,
348e048f
DE
1798 struct dwarf2_cu **);
1799
10b3939b 1800static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1801 const struct attribute *,
f2f0e013 1802 struct dwarf2_cu **);
c906108c 1803
348e048f 1804static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1805 const struct attribute *,
348e048f
DE
1806 struct dwarf2_cu **);
1807
ac9ec31b
DE
1808static struct type *get_signatured_type (struct die_info *, ULONGEST,
1809 struct dwarf2_cu *);
1810
1811static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1812 const struct attribute *,
ac9ec31b
DE
1813 struct dwarf2_cu *);
1814
e5fe5e75 1815static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1816
52dc124a 1817static void read_signatured_type (struct signatured_type *);
348e048f 1818
63e43d3a
PMR
1819static int attr_to_dynamic_prop (const struct attribute *attr,
1820 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1821 struct dynamic_prop *prop, struct type *type);
63e43d3a 1822
c906108c
SS
1823/* memory allocation interface */
1824
7b5a2f43 1825static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1826
b60c80d6 1827static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1828
43f3e411 1829static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1830
6e5a29e1 1831static int attr_form_is_block (const struct attribute *);
8e19ed76 1832
6e5a29e1 1833static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1834
6e5a29e1 1835static int attr_form_is_constant (const struct attribute *);
3690dd37 1836
6e5a29e1 1837static int attr_form_is_ref (const struct attribute *);
7771576e 1838
8cf6f0b1
TT
1839static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1840 struct dwarf2_loclist_baton *baton,
ff39bb5e 1841 const struct attribute *attr);
8cf6f0b1 1842
ff39bb5e 1843static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1844 struct symbol *sym,
f1e6e072
TT
1845 struct dwarf2_cu *cu,
1846 int is_block);
4c2df51b 1847
d521ce57
TT
1848static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1849 const gdb_byte *info_ptr,
1850 struct abbrev_info *abbrev);
4bb7a0a7 1851
72bf9492
DJ
1852static hashval_t partial_die_hash (const void *item);
1853
1854static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1855
ae038cb0 1856static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1857 (sect_offset sect_off, unsigned int offset_in_dwz,
1858 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1859
9816fde3 1860static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1861 struct die_info *comp_unit_die,
1862 enum language pretend_language);
93311388 1863
ed2dc618 1864static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1865
dee91e82 1866static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1867
f792889a
DJ
1868static struct type *set_die_type (struct die_info *, struct type *,
1869 struct dwarf2_cu *);
1c379e20 1870
ed2dc618 1871static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1872
ed2dc618 1873static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1874
58f0c718 1875static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1876 enum language);
10b3939b 1877
95554aad
TT
1878static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1879 enum language);
10b3939b 1880
f4dc4d17
DE
1881static void process_full_type_unit (struct dwarf2_per_cu_data *,
1882 enum language);
1883
10b3939b
DJ
1884static void dwarf2_add_dependence (struct dwarf2_cu *,
1885 struct dwarf2_per_cu_data *);
1886
ae038cb0
DJ
1887static void dwarf2_mark (struct dwarf2_cu *);
1888
1889static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1890
b64f50a1 1891static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1892 struct dwarf2_per_cu_data *);
673bfd45 1893
f792889a 1894static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1895
95554aad
TT
1896static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1897 enum language pretend_language);
1898
ed2dc618 1899static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1900
9a49df9d
AB
1901static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1902static struct type *dwarf2_per_cu_addr_sized_int_type
1903 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
11a8b164
AB
1904static struct type *dwarf2_per_cu_int_type
1905 (struct dwarf2_per_cu_data *per_cu, int size_in_bytes,
1906 bool unsigned_p);
9a49df9d 1907
b303c6f6
AB
1908/* Class, the destructor of which frees all allocated queue entries. This
1909 will only have work to do if an error was thrown while processing the
1910 dwarf. If no error was thrown then the queue entries should have all
1911 been processed, and freed, as we went along. */
1912
1913class dwarf2_queue_guard
1914{
1915public:
1916 dwarf2_queue_guard () = default;
1917
1918 /* Free any entries remaining on the queue. There should only be
1919 entries left if we hit an error while processing the dwarf. */
1920 ~dwarf2_queue_guard ()
1921 {
1922 struct dwarf2_queue_item *item, *last;
1923
1924 item = dwarf2_queue;
1925 while (item)
1926 {
1927 /* Anything still marked queued is likely to be in an
1928 inconsistent state, so discard it. */
1929 if (item->per_cu->queued)
1930 {
1931 if (item->per_cu->cu != NULL)
1932 free_one_cached_comp_unit (item->per_cu);
1933 item->per_cu->queued = 0;
1934 }
1935
1936 last = item;
1937 item = item->next;
1938 xfree (last);
1939 }
1940
1941 dwarf2_queue = dwarf2_queue_tail = NULL;
1942 }
1943};
1944
d721ba37
PA
1945/* The return type of find_file_and_directory. Note, the enclosed
1946 string pointers are only valid while this object is valid. */
1947
1948struct file_and_directory
1949{
1950 /* The filename. This is never NULL. */
1951 const char *name;
1952
1953 /* The compilation directory. NULL if not known. If we needed to
1954 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1955 points directly to the DW_AT_comp_dir string attribute owned by
1956 the obstack that owns the DIE. */
1957 const char *comp_dir;
1958
1959 /* If we needed to build a new string for comp_dir, this is what
1960 owns the storage. */
1961 std::string comp_dir_storage;
1962};
1963
1964static file_and_directory find_file_and_directory (struct die_info *die,
1965 struct dwarf2_cu *cu);
9291a0cd
TT
1966
1967static char *file_full_name (int file, struct line_header *lh,
1968 const char *comp_dir);
1969
43988095
JK
1970/* Expected enum dwarf_unit_type for read_comp_unit_head. */
1971enum class rcuh_kind { COMPILE, TYPE };
1972
d521ce57 1973static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
1974 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1975 struct comp_unit_head *header,
36586728 1976 struct dwarf2_section_info *section,
d521ce57 1977 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 1978 rcuh_kind section_kind);
36586728 1979
fd820528 1980static void init_cutu_and_read_dies
f4dc4d17 1981 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
58f0c718 1982 int use_existing_cu, int keep, bool skip_partial,
3019eac3
DE
1983 die_reader_func_ftype *die_reader_func, void *data);
1984
dee91e82
DE
1985static void init_cutu_and_read_dies_simple
1986 (struct dwarf2_per_cu_data *this_cu,
1987 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1988
673bfd45 1989static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1990
3019eac3
DE
1991static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1992
57d63ce2 1993static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1994 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1995 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1996 ULONGEST signature, int is_debug_types);
a2ce51a0 1997
ed2dc618
SM
1998static struct dwp_file *get_dwp_file
1999 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 2000
3019eac3 2001static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 2002 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2003
2004static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2005 (struct signatured_type *, const char *, const char *);
3019eac3 2006
89e63ee4
DE
2007static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2008
263db9a1
TT
2009/* A unique pointer to a dwo_file. */
2010
51ac9db5 2011typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 2012
ed2dc618 2013static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 2014
1b80a9fa 2015static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2016
2017static void free_line_header_voidp (void *arg);
4390d890
DE
2018\f
2019/* Various complaints about symbol reading that don't abort the process. */
2020
2021static void
2022dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2023{
b98664d3 2024 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
2025}
2026
2027static void
2028dwarf2_debug_line_missing_file_complaint (void)
2029{
b98664d3 2030 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
2031}
2032
2033static void
2034dwarf2_debug_line_missing_end_sequence_complaint (void)
2035{
b98664d3 2036 complaint (_(".debug_line section has line "
4390d890
DE
2037 "program sequence without an end"));
2038}
2039
2040static void
2041dwarf2_complex_location_expr_complaint (void)
2042{
b98664d3 2043 complaint (_("location expression too complex"));
4390d890
DE
2044}
2045
2046static void
2047dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2048 int arg3)
2049{
b98664d3 2050 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
2051 arg1, arg2, arg3);
2052}
2053
2054static void
2055dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2056{
b98664d3 2057 complaint (_("debug info runs off end of %s section"
4390d890 2058 " [in module %s]"),
a32a8923
DE
2059 get_section_name (section),
2060 get_section_file_name (section));
4390d890 2061}
1b80a9fa 2062
4390d890
DE
2063static void
2064dwarf2_macro_malformed_definition_complaint (const char *arg1)
2065{
b98664d3 2066 complaint (_("macro debug info contains a "
4390d890
DE
2067 "malformed macro definition:\n`%s'"),
2068 arg1);
2069}
2070
2071static void
2072dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2073{
b98664d3 2074 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
2075 arg1, arg2);
2076}
527f3840
JK
2077
2078/* Hash function for line_header_hash. */
2079
2080static hashval_t
2081line_header_hash (const struct line_header *ofs)
2082{
9c541725 2083 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2084}
2085
2086/* Hash function for htab_create_alloc_ex for line_header_hash. */
2087
2088static hashval_t
2089line_header_hash_voidp (const void *item)
2090{
9a3c8263 2091 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2092
2093 return line_header_hash (ofs);
2094}
2095
2096/* Equality function for line_header_hash. */
2097
2098static int
2099line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2100{
9a3c8263
SM
2101 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2102 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2103
9c541725 2104 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2105 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2106}
2107
4390d890 2108\f
9291a0cd 2109
31aa7e4e
JB
2110/* Read the given attribute value as an address, taking the attribute's
2111 form into account. */
2112
2113static CORE_ADDR
2114attr_value_as_address (struct attribute *attr)
2115{
2116 CORE_ADDR addr;
2117
336d760d
AT
2118 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2119 && attr->form != DW_FORM_GNU_addr_index)
31aa7e4e
JB
2120 {
2121 /* Aside from a few clearly defined exceptions, attributes that
2122 contain an address must always be in DW_FORM_addr form.
2123 Unfortunately, some compilers happen to be violating this
2124 requirement by encoding addresses using other forms, such
2125 as DW_FORM_data4 for example. For those broken compilers,
2126 we try to do our best, without any guarantee of success,
2127 to interpret the address correctly. It would also be nice
2128 to generate a complaint, but that would require us to maintain
2129 a list of legitimate cases where a non-address form is allowed,
2130 as well as update callers to pass in at least the CU's DWARF
2131 version. This is more overhead than what we're willing to
2132 expand for a pretty rare case. */
2133 addr = DW_UNSND (attr);
2134 }
2135 else
2136 addr = DW_ADDR (attr);
2137
2138 return addr;
2139}
2140
330cdd98
PA
2141/* See declaration. */
2142
2143dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
2144 const dwarf2_debug_sections *names,
2145 bool can_copy_)
2146 : objfile (objfile_),
2147 can_copy (can_copy_)
330cdd98
PA
2148{
2149 if (names == NULL)
2150 names = &dwarf2_elf_names;
2151
2152 bfd *obfd = objfile->obfd;
2153
2154 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2155 locate_sections (obfd, sec, *names);
2156}
2157
2158dwarf2_per_objfile::~dwarf2_per_objfile ()
2159{
2160 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2161 free_cached_comp_units ();
2162
2163 if (quick_file_names_table)
2164 htab_delete (quick_file_names_table);
2165
2166 if (line_header_hash)
2167 htab_delete (line_header_hash);
2168
b76e467d 2169 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 2170 per_cu->imported_symtabs_free ();
fc8e7e75 2171
b2bdb8cf 2172 for (signatured_type *sig_type : all_type_units)
ae640021 2173 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 2174
330cdd98
PA
2175 /* Everything else should be on the objfile obstack. */
2176}
2177
2178/* See declaration. */
2179
2180void
2181dwarf2_per_objfile::free_cached_comp_units ()
2182{
2183 dwarf2_per_cu_data *per_cu = read_in_chain;
2184 dwarf2_per_cu_data **last_chain = &read_in_chain;
2185 while (per_cu != NULL)
2186 {
2187 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2188
fcd3b13d 2189 delete per_cu->cu;
330cdd98
PA
2190 *last_chain = next_cu;
2191 per_cu = next_cu;
2192 }
2193}
2194
11ed8cad
TT
2195/* A helper class that calls free_cached_comp_units on
2196 destruction. */
2197
2198class free_cached_comp_units
2199{
2200public:
2201
2202 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2203 : m_per_objfile (per_objfile)
2204 {
2205 }
2206
2207 ~free_cached_comp_units ()
2208 {
2209 m_per_objfile->free_cached_comp_units ();
2210 }
2211
2212 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2213
2214private:
2215
2216 dwarf2_per_objfile *m_per_objfile;
2217};
2218
c906108c 2219/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2220 information and return true if we have enough to do something.
2221 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
2222 ELF names are used. CAN_COPY is true for formats where symbol
2223 interposition is possible and so symbol values must follow copy
2224 relocation rules. */
c906108c
SS
2225
2226int
251d32d9 2227dwarf2_has_info (struct objfile *objfile,
4b610737
TT
2228 const struct dwarf2_debug_sections *names,
2229 bool can_copy)
c906108c 2230{
97cbe998
SDJ
2231 if (objfile->flags & OBJF_READNEVER)
2232 return 0;
2233
ed2dc618
SM
2234 struct dwarf2_per_objfile *dwarf2_per_objfile
2235 = get_dwarf2_per_objfile (objfile);
2236
2237 if (dwarf2_per_objfile == NULL)
5bfd760d 2238 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
2239 names,
2240 can_copy);
5bfd760d 2241
73869dc2 2242 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2243 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2244 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2245 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2246}
2247
2248/* Return the containing section of virtual section SECTION. */
2249
2250static struct dwarf2_section_info *
2251get_containing_section (const struct dwarf2_section_info *section)
2252{
2253 gdb_assert (section->is_virtual);
2254 return section->s.containing_section;
c906108c
SS
2255}
2256
a32a8923
DE
2257/* Return the bfd owner of SECTION. */
2258
2259static struct bfd *
2260get_section_bfd_owner (const struct dwarf2_section_info *section)
2261{
73869dc2
DE
2262 if (section->is_virtual)
2263 {
2264 section = get_containing_section (section);
2265 gdb_assert (!section->is_virtual);
2266 }
049412e3 2267 return section->s.section->owner;
a32a8923
DE
2268}
2269
2270/* Return the bfd section of SECTION.
2271 Returns NULL if the section is not present. */
2272
2273static asection *
2274get_section_bfd_section (const struct dwarf2_section_info *section)
2275{
73869dc2
DE
2276 if (section->is_virtual)
2277 {
2278 section = get_containing_section (section);
2279 gdb_assert (!section->is_virtual);
2280 }
049412e3 2281 return section->s.section;
a32a8923
DE
2282}
2283
2284/* Return the name of SECTION. */
2285
2286static const char *
2287get_section_name (const struct dwarf2_section_info *section)
2288{
2289 asection *sectp = get_section_bfd_section (section);
2290
2291 gdb_assert (sectp != NULL);
fd361982 2292 return bfd_section_name (sectp);
a32a8923
DE
2293}
2294
2295/* Return the name of the file SECTION is in. */
2296
2297static const char *
2298get_section_file_name (const struct dwarf2_section_info *section)
2299{
2300 bfd *abfd = get_section_bfd_owner (section);
2301
2302 return bfd_get_filename (abfd);
2303}
2304
2305/* Return the id of SECTION.
2306 Returns 0 if SECTION doesn't exist. */
2307
2308static int
2309get_section_id (const struct dwarf2_section_info *section)
2310{
2311 asection *sectp = get_section_bfd_section (section);
2312
2313 if (sectp == NULL)
2314 return 0;
2315 return sectp->id;
2316}
2317
2318/* Return the flags of SECTION.
73869dc2 2319 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2320
2321static int
2322get_section_flags (const struct dwarf2_section_info *section)
2323{
2324 asection *sectp = get_section_bfd_section (section);
2325
2326 gdb_assert (sectp != NULL);
fd361982 2327 return bfd_section_flags (sectp);
a32a8923
DE
2328}
2329
251d32d9
TG
2330/* When loading sections, we look either for uncompressed section or for
2331 compressed section names. */
233a11ab
CS
2332
2333static int
251d32d9
TG
2334section_is_p (const char *section_name,
2335 const struct dwarf2_section_names *names)
233a11ab 2336{
251d32d9
TG
2337 if (names->normal != NULL
2338 && strcmp (section_name, names->normal) == 0)
2339 return 1;
2340 if (names->compressed != NULL
2341 && strcmp (section_name, names->compressed) == 0)
2342 return 1;
2343 return 0;
233a11ab
CS
2344}
2345
330cdd98 2346/* See declaration. */
c906108c 2347
330cdd98
PA
2348void
2349dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2350 const dwarf2_debug_sections &names)
c906108c 2351{
fd361982 2352 flagword aflag = bfd_section_flags (sectp);
251d32d9 2353
dc7650b8
JK
2354 if ((aflag & SEC_HAS_CONTENTS) == 0)
2355 {
2356 }
950b7495
KS
2357 else if (elf_section_data (sectp)->this_hdr.sh_size
2358 > bfd_get_file_size (abfd))
2359 {
2360 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2361 warning (_("Discarding section %s which has a section size (%s"
2362 ") larger than the file size [in module %s]"),
2363 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2364 bfd_get_filename (abfd));
2365 }
330cdd98 2366 else if (section_is_p (sectp->name, &names.info))
c906108c 2367 {
330cdd98 2368 this->info.s.section = sectp;
fd361982 2369 this->info.size = bfd_section_size (sectp);
c906108c 2370 }
330cdd98 2371 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2372 {
330cdd98 2373 this->abbrev.s.section = sectp;
fd361982 2374 this->abbrev.size = bfd_section_size (sectp);
c906108c 2375 }
330cdd98 2376 else if (section_is_p (sectp->name, &names.line))
c906108c 2377 {
330cdd98 2378 this->line.s.section = sectp;
fd361982 2379 this->line.size = bfd_section_size (sectp);
c906108c 2380 }
330cdd98 2381 else if (section_is_p (sectp->name, &names.loc))
c906108c 2382 {
330cdd98 2383 this->loc.s.section = sectp;
fd361982 2384 this->loc.size = bfd_section_size (sectp);
c906108c 2385 }
330cdd98 2386 else if (section_is_p (sectp->name, &names.loclists))
43988095 2387 {
330cdd98 2388 this->loclists.s.section = sectp;
fd361982 2389 this->loclists.size = bfd_section_size (sectp);
43988095 2390 }
330cdd98 2391 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2392 {
330cdd98 2393 this->macinfo.s.section = sectp;
fd361982 2394 this->macinfo.size = bfd_section_size (sectp);
c906108c 2395 }
330cdd98 2396 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2397 {
330cdd98 2398 this->macro.s.section = sectp;
fd361982 2399 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2400 }
330cdd98 2401 else if (section_is_p (sectp->name, &names.str))
c906108c 2402 {
330cdd98 2403 this->str.s.section = sectp;
fd361982 2404 this->str.size = bfd_section_size (sectp);
c906108c 2405 }
330cdd98 2406 else if (section_is_p (sectp->name, &names.line_str))
43988095 2407 {
330cdd98 2408 this->line_str.s.section = sectp;
fd361982 2409 this->line_str.size = bfd_section_size (sectp);
43988095 2410 }
330cdd98 2411 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2412 {
330cdd98 2413 this->addr.s.section = sectp;
fd361982 2414 this->addr.size = bfd_section_size (sectp);
3019eac3 2415 }
330cdd98 2416 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2417 {
330cdd98 2418 this->frame.s.section = sectp;
fd361982 2419 this->frame.size = bfd_section_size (sectp);
b6af0555 2420 }
330cdd98 2421 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2422 {
330cdd98 2423 this->eh_frame.s.section = sectp;
fd361982 2424 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2425 }
330cdd98 2426 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2427 {
330cdd98 2428 this->ranges.s.section = sectp;
fd361982 2429 this->ranges.size = bfd_section_size (sectp);
af34e669 2430 }
330cdd98 2431 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2432 {
330cdd98 2433 this->rnglists.s.section = sectp;
fd361982 2434 this->rnglists.size = bfd_section_size (sectp);
43988095 2435 }
330cdd98 2436 else if (section_is_p (sectp->name, &names.types))
348e048f 2437 {
8b70b953
TT
2438 struct dwarf2_section_info type_section;
2439
2440 memset (&type_section, 0, sizeof (type_section));
049412e3 2441 type_section.s.section = sectp;
fd361982 2442 type_section.size = bfd_section_size (sectp);
8b70b953 2443
fd5866f6 2444 this->types.push_back (type_section);
348e048f 2445 }
330cdd98 2446 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2447 {
330cdd98 2448 this->gdb_index.s.section = sectp;
fd361982 2449 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2450 }
927aa2e7
JK
2451 else if (section_is_p (sectp->name, &names.debug_names))
2452 {
2453 this->debug_names.s.section = sectp;
fd361982 2454 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2455 }
2456 else if (section_is_p (sectp->name, &names.debug_aranges))
2457 {
2458 this->debug_aranges.s.section = sectp;
fd361982 2459 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2460 }
dce234bc 2461
fd361982
AM
2462 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2463 && bfd_section_vma (sectp) == 0)
330cdd98 2464 this->has_section_at_zero = true;
c906108c
SS
2465}
2466
fceca515
DE
2467/* A helper function that decides whether a section is empty,
2468 or not present. */
9e0ac564
TT
2469
2470static int
19ac8c2e 2471dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2472{
73869dc2
DE
2473 if (section->is_virtual)
2474 return section->size == 0;
049412e3 2475 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2476}
2477
cd4fb1b2 2478/* See dwarf2read.h. */
c906108c 2479
cd4fb1b2
SM
2480void
2481dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
c906108c 2482{
a32a8923 2483 asection *sectp;
3019eac3 2484 bfd *abfd;
dce234bc 2485 gdb_byte *buf, *retbuf;
c906108c 2486
be391dca
TT
2487 if (info->readin)
2488 return;
dce234bc 2489 info->buffer = NULL;
dc4ccb6f 2490 info->readin = true;
188dd5d6 2491
9e0ac564 2492 if (dwarf2_section_empty_p (info))
dce234bc 2493 return;
c906108c 2494
a32a8923 2495 sectp = get_section_bfd_section (info);
3019eac3 2496
73869dc2
DE
2497 /* If this is a virtual section we need to read in the real one first. */
2498 if (info->is_virtual)
2499 {
2500 struct dwarf2_section_info *containing_section =
2501 get_containing_section (info);
2502
2503 gdb_assert (sectp != NULL);
2504 if ((sectp->flags & SEC_RELOC) != 0)
2505 {
2506 error (_("Dwarf Error: DWP format V2 with relocations is not"
2507 " supported in section %s [in module %s]"),
2508 get_section_name (info), get_section_file_name (info));
2509 }
2510 dwarf2_read_section (objfile, containing_section);
2511 /* Other code should have already caught virtual sections that don't
2512 fit. */
2513 gdb_assert (info->virtual_offset + info->size
2514 <= containing_section->size);
2515 /* If the real section is empty or there was a problem reading the
2516 section we shouldn't get here. */
2517 gdb_assert (containing_section->buffer != NULL);
2518 info->buffer = containing_section->buffer + info->virtual_offset;
2519 return;
2520 }
2521
4bf44c1c
TT
2522 /* If the section has relocations, we must read it ourselves.
2523 Otherwise we attach it to the BFD. */
2524 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2525 {
d521ce57 2526 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2527 return;
dce234bc 2528 }
dce234bc 2529
224c3ddb 2530 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2531 info->buffer = buf;
dce234bc
PP
2532
2533 /* When debugging .o files, we may need to apply relocations; see
2534 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2535 We never compress sections in .o files, so we only need to
2536 try this when the section is not compressed. */
ac8035ab 2537 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2538 if (retbuf != NULL)
2539 {
2540 info->buffer = retbuf;
2541 return;
2542 }
2543
a32a8923
DE
2544 abfd = get_section_bfd_owner (info);
2545 gdb_assert (abfd != NULL);
2546
dce234bc
PP
2547 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2548 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2549 {
2550 error (_("Dwarf Error: Can't read DWARF data"
2551 " in section %s [in module %s]"),
fd361982 2552 bfd_section_name (sectp), bfd_get_filename (abfd));
19ac8c2e 2553 }
dce234bc
PP
2554}
2555
9e0ac564
TT
2556/* A helper function that returns the size of a section in a safe way.
2557 If you are positive that the section has been read before using the
2558 size, then it is safe to refer to the dwarf2_section_info object's
2559 "size" field directly. In other cases, you must call this
2560 function, because for compressed sections the size field is not set
2561 correctly until the section has been read. */
2562
2563static bfd_size_type
2564dwarf2_section_size (struct objfile *objfile,
2565 struct dwarf2_section_info *info)
2566{
2567 if (!info->readin)
2568 dwarf2_read_section (objfile, info);
2569 return info->size;
2570}
2571
dce234bc 2572/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2573 SECTION_NAME. */
af34e669 2574
dce234bc 2575void
3017a003
TG
2576dwarf2_get_section_info (struct objfile *objfile,
2577 enum dwarf2_section_enum sect,
d521ce57 2578 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2579 bfd_size_type *sizep)
2580{
5bfd760d 2581 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2582 struct dwarf2_section_info *info;
a3b2a86b
TT
2583
2584 /* We may see an objfile without any DWARF, in which case we just
2585 return nothing. */
2586 if (data == NULL)
2587 {
2588 *sectp = NULL;
2589 *bufp = NULL;
2590 *sizep = 0;
2591 return;
2592 }
3017a003
TG
2593 switch (sect)
2594 {
2595 case DWARF2_DEBUG_FRAME:
2596 info = &data->frame;
2597 break;
2598 case DWARF2_EH_FRAME:
2599 info = &data->eh_frame;
2600 break;
2601 default:
2602 gdb_assert_not_reached ("unexpected section");
2603 }
dce234bc 2604
9e0ac564 2605 dwarf2_read_section (objfile, info);
dce234bc 2606
a32a8923 2607 *sectp = get_section_bfd_section (info);
dce234bc
PP
2608 *bufp = info->buffer;
2609 *sizep = info->size;
2610}
2611
36586728
TT
2612/* A helper function to find the sections for a .dwz file. */
2613
2614static void
2615locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2616{
9a3c8263 2617 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2618
2619 /* Note that we only support the standard ELF names, because .dwz
2620 is ELF-only (at the time of writing). */
2621 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2622 {
049412e3 2623 dwz_file->abbrev.s.section = sectp;
fd361982 2624 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2625 }
2626 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2627 {
049412e3 2628 dwz_file->info.s.section = sectp;
fd361982 2629 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2630 }
2631 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2632 {
049412e3 2633 dwz_file->str.s.section = sectp;
fd361982 2634 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2635 }
2636 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2637 {
049412e3 2638 dwz_file->line.s.section = sectp;
fd361982 2639 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2640 }
2641 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2642 {
049412e3 2643 dwz_file->macro.s.section = sectp;
fd361982 2644 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2645 }
2ec9a5e0
TT
2646 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2647 {
049412e3 2648 dwz_file->gdb_index.s.section = sectp;
fd361982 2649 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2650 }
927aa2e7
JK
2651 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2652 {
2653 dwz_file->debug_names.s.section = sectp;
fd361982 2654 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2655 }
36586728
TT
2656}
2657
c4973306 2658/* See dwarf2read.h. */
36586728 2659
c4973306 2660struct dwz_file *
ed2dc618 2661dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2662{
36586728 2663 const char *filename;
acd13123 2664 bfd_size_type buildid_len_arg;
dc294be5
TT
2665 size_t buildid_len;
2666 bfd_byte *buildid;
36586728
TT
2667
2668 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2669 return dwarf2_per_objfile->dwz_file.get ();
36586728 2670
4db1a1dc 2671 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2672 gdb::unique_xmalloc_ptr<char> data
2673 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2674 &buildid_len_arg, &buildid));
4db1a1dc
TT
2675 if (data == NULL)
2676 {
2677 if (bfd_get_error () == bfd_error_no_error)
2678 return NULL;
2679 error (_("could not read '.gnu_debugaltlink' section: %s"),
2680 bfd_errmsg (bfd_get_error ()));
2681 }
791afaa2
TT
2682
2683 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2684
acd13123
TT
2685 buildid_len = (size_t) buildid_len_arg;
2686
791afaa2 2687 filename = data.get ();
d721ba37
PA
2688
2689 std::string abs_storage;
36586728
TT
2690 if (!IS_ABSOLUTE_PATH (filename))
2691 {
14278e1f
TT
2692 gdb::unique_xmalloc_ptr<char> abs
2693 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2694
14278e1f 2695 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2696 filename = abs_storage.c_str ();
36586728
TT
2697 }
2698
dc294be5
TT
2699 /* First try the file name given in the section. If that doesn't
2700 work, try to use the build-id instead. */
192b62ce 2701 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2702 if (dwz_bfd != NULL)
36586728 2703 {
192b62ce 2704 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2705 dwz_bfd.reset (nullptr);
36586728
TT
2706 }
2707
dc294be5
TT
2708 if (dwz_bfd == NULL)
2709 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2710
2711 if (dwz_bfd == NULL)
2712 error (_("could not find '.gnu_debugaltlink' file for %s"),
2713 objfile_name (dwarf2_per_objfile->objfile));
2714
7ff8cb8c
TT
2715 std::unique_ptr<struct dwz_file> result
2716 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2717
7ff8cb8c
TT
2718 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2719 result.get ());
36586728 2720
7ff8cb8c
TT
2721 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2722 result->dwz_bfd.get ());
2723 dwarf2_per_objfile->dwz_file = std::move (result);
2724 return dwarf2_per_objfile->dwz_file.get ();
36586728 2725}
9291a0cd 2726\f
7b9f3c50
DE
2727/* DWARF quick_symbols_functions support. */
2728
2729/* TUs can share .debug_line entries, and there can be a lot more TUs than
2730 unique line tables, so we maintain a separate table of all .debug_line
2731 derived entries to support the sharing.
2732 All the quick functions need is the list of file names. We discard the
2733 line_header when we're done and don't need to record it here. */
2734struct quick_file_names
2735{
094b34ac
DE
2736 /* The data used to construct the hash key. */
2737 struct stmt_list_hash hash;
7b9f3c50
DE
2738
2739 /* The number of entries in file_names, real_names. */
2740 unsigned int num_file_names;
2741
2742 /* The file names from the line table, after being run through
2743 file_full_name. */
2744 const char **file_names;
2745
2746 /* The file names from the line table after being run through
2747 gdb_realpath. These are computed lazily. */
2748 const char **real_names;
2749};
2750
2751/* When using the index (and thus not using psymtabs), each CU has an
2752 object of this type. This is used to hold information needed by
2753 the various "quick" methods. */
2754struct dwarf2_per_cu_quick_data
2755{
2756 /* The file table. This can be NULL if there was no file table
2757 or it's currently not read in.
2758 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2759 struct quick_file_names *file_names;
2760
2761 /* The corresponding symbol table. This is NULL if symbols for this
2762 CU have not yet been read. */
43f3e411 2763 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2764
2765 /* A temporary mark bit used when iterating over all CUs in
2766 expand_symtabs_matching. */
2767 unsigned int mark : 1;
2768
2769 /* True if we've tried to read the file table and found there isn't one.
2770 There will be no point in trying to read it again next time. */
2771 unsigned int no_file_data : 1;
2772};
2773
094b34ac
DE
2774/* Utility hash function for a stmt_list_hash. */
2775
2776static hashval_t
2777hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2778{
2779 hashval_t v = 0;
2780
2781 if (stmt_list_hash->dwo_unit != NULL)
2782 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2783 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2784 return v;
2785}
2786
2787/* Utility equality function for a stmt_list_hash. */
2788
2789static int
2790eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2791 const struct stmt_list_hash *rhs)
2792{
2793 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2794 return 0;
2795 if (lhs->dwo_unit != NULL
2796 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2797 return 0;
2798
9c541725 2799 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2800}
2801
7b9f3c50
DE
2802/* Hash function for a quick_file_names. */
2803
2804static hashval_t
2805hash_file_name_entry (const void *e)
2806{
9a3c8263
SM
2807 const struct quick_file_names *file_data
2808 = (const struct quick_file_names *) e;
7b9f3c50 2809
094b34ac 2810 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2811}
2812
2813/* Equality function for a quick_file_names. */
2814
2815static int
2816eq_file_name_entry (const void *a, const void *b)
2817{
9a3c8263
SM
2818 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2819 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2820
094b34ac 2821 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2822}
2823
2824/* Delete function for a quick_file_names. */
2825
2826static void
2827delete_file_name_entry (void *e)
2828{
9a3c8263 2829 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2830 int i;
2831
2832 for (i = 0; i < file_data->num_file_names; ++i)
2833 {
2834 xfree ((void*) file_data->file_names[i]);
2835 if (file_data->real_names)
2836 xfree ((void*) file_data->real_names[i]);
2837 }
2838
2839 /* The space for the struct itself lives on objfile_obstack,
2840 so we don't free it here. */
2841}
2842
2843/* Create a quick_file_names hash table. */
2844
2845static htab_t
2846create_quick_file_names_table (unsigned int nr_initial_entries)
2847{
2848 return htab_create_alloc (nr_initial_entries,
2849 hash_file_name_entry, eq_file_name_entry,
2850 delete_file_name_entry, xcalloc, xfree);
2851}
9291a0cd 2852
918dd910
JK
2853/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2854 have to be created afterwards. You should call age_cached_comp_units after
2855 processing PER_CU->CU. dw2_setup must have been already called. */
2856
2857static void
58f0c718 2858load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2859{
3019eac3 2860 if (per_cu->is_debug_types)
e5fe5e75 2861 load_full_type_unit (per_cu);
918dd910 2862 else
58f0c718 2863 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2864
cc12ce38
DE
2865 if (per_cu->cu == NULL)
2866 return; /* Dummy CU. */
2dc860c0
DE
2867
2868 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2869}
2870
a0f42c21 2871/* Read in the symbols for PER_CU. */
2fdf6df6 2872
9291a0cd 2873static void
58f0c718 2874dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2875{
ed2dc618 2876 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2877
f4dc4d17
DE
2878 /* Skip type_unit_groups, reading the type units they contain
2879 is handled elsewhere. */
2880 if (IS_TYPE_UNIT_GROUP (per_cu))
2881 return;
2882
b303c6f6
AB
2883 /* The destructor of dwarf2_queue_guard frees any entries left on
2884 the queue. After this point we're guaranteed to leave this function
2885 with the dwarf queue empty. */
2886 dwarf2_queue_guard q_guard;
9291a0cd 2887
95554aad 2888 if (dwarf2_per_objfile->using_index
43f3e411 2889 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2890 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2891 {
2892 queue_comp_unit (per_cu, language_minimal);
58f0c718 2893 load_cu (per_cu, skip_partial);
89e63ee4
DE
2894
2895 /* If we just loaded a CU from a DWO, and we're working with an index
2896 that may badly handle TUs, load all the TUs in that DWO as well.
2897 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2898 if (!per_cu->is_debug_types
cc12ce38 2899 && per_cu->cu != NULL
89e63ee4
DE
2900 && per_cu->cu->dwo_unit != NULL
2901 && dwarf2_per_objfile->index_table != NULL
2902 && dwarf2_per_objfile->index_table->version <= 7
2903 /* DWP files aren't supported yet. */
ed2dc618 2904 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2905 queue_and_load_all_dwo_tus (per_cu);
95554aad 2906 }
9291a0cd 2907
ed2dc618 2908 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2909
2910 /* Age the cache, releasing compilation units that have not
2911 been used recently. */
ed2dc618 2912 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2913}
2914
2915/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2916 the objfile from which this CU came. Returns the resulting symbol
2917 table. */
2fdf6df6 2918
43f3e411 2919static struct compunit_symtab *
58f0c718 2920dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2921{
ed2dc618
SM
2922 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2923
95554aad 2924 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2925 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2926 {
11ed8cad 2927 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2928 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2929 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2930 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2931 }
f194fefb 2932
43f3e411 2933 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2934}
2935
ff4c9fec 2936/* See declaration. */
f4dc4d17 2937
ff4c9fec
SM
2938dwarf2_per_cu_data *
2939dwarf2_per_objfile::get_cutu (int index)
2940{
b76e467d 2941 if (index >= this->all_comp_units.size ())
ff4c9fec 2942 {
b76e467d 2943 index -= this->all_comp_units.size ();
b2bdb8cf 2944 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2945 return &this->all_type_units[index]->per_cu;
2946 }
f4dc4d17 2947
ff4c9fec
SM
2948 return this->all_comp_units[index];
2949}
f4dc4d17 2950
ff4c9fec 2951/* See declaration. */
2fdf6df6 2952
ff4c9fec
SM
2953dwarf2_per_cu_data *
2954dwarf2_per_objfile::get_cu (int index)
1fd400ff 2955{
b76e467d 2956 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2957
ff4c9fec 2958 return this->all_comp_units[index];
f4dc4d17
DE
2959}
2960
ff4c9fec 2961/* See declaration. */
f4dc4d17 2962
ff4c9fec
SM
2963signatured_type *
2964dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2965{
b2bdb8cf 2966 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2967
ff4c9fec 2968 return this->all_type_units[index];
1fd400ff
TT
2969}
2970
4b514bc8
JK
2971/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2972 objfile_obstack, and constructed with the specified field
2973 values. */
2974
2975static dwarf2_per_cu_data *
ed2dc618 2976create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2977 struct dwarf2_section_info *section,
2978 int is_dwz,
2979 sect_offset sect_off, ULONGEST length)
2980{
ed2dc618 2981 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2982 dwarf2_per_cu_data *the_cu
2983 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2984 struct dwarf2_per_cu_data);
2985 the_cu->sect_off = sect_off;
2986 the_cu->length = length;
e3b94546 2987 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2988 the_cu->section = section;
2989 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2990 struct dwarf2_per_cu_quick_data);
2991 the_cu->is_dwz = is_dwz;
2992 return the_cu;
2993}
2994
2ec9a5e0
TT
2995/* A helper for create_cus_from_index that handles a given list of
2996 CUs. */
2fdf6df6 2997
74a0d9f6 2998static void
12359b5e 2999create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3000 const gdb_byte *cu_list, offset_type n_elements,
3001 struct dwarf2_section_info *section,
b76e467d 3002 int is_dwz)
9291a0cd 3003{
12359b5e 3004 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 3005 {
74a0d9f6 3006 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3007
3008 sect_offset sect_off
3009 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3010 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3011 cu_list += 2 * 8;
3012
b76e467d 3013 dwarf2_per_cu_data *per_cu
ed2dc618
SM
3014 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3015 sect_off, length);
b76e467d 3016 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 3017 }
9291a0cd
TT
3018}
3019
2ec9a5e0 3020/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3021 the CU objects for this objfile. */
2ec9a5e0 3022
74a0d9f6 3023static void
12359b5e 3024create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3025 const gdb_byte *cu_list, offset_type cu_list_elements,
3026 const gdb_byte *dwz_list, offset_type dwz_elements)
3027{
b76e467d
SM
3028 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3029 dwarf2_per_objfile->all_comp_units.reserve
3030 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 3031
12359b5e 3032 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 3033 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
3034
3035 if (dwz_elements == 0)
74a0d9f6 3036 return;
2ec9a5e0 3037
12359b5e
SM
3038 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3039 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 3040 &dwz->info, 1);
2ec9a5e0
TT
3041}
3042
1fd400ff 3043/* Create the signatured type hash table from the index. */
673bfd45 3044
74a0d9f6 3045static void
12359b5e
SM
3046create_signatured_type_table_from_index
3047 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3048 struct dwarf2_section_info *section,
3049 const gdb_byte *bytes,
3050 offset_type elements)
1fd400ff 3051{
12359b5e 3052 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 3053
b2bdb8cf
SM
3054 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3055 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 3056
12359b5e 3057 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff 3058
12359b5e 3059 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 3060 {
52dc124a 3061 struct signatured_type *sig_type;
9c541725 3062 ULONGEST signature;
1fd400ff 3063 void **slot;
9c541725 3064 cu_offset type_offset_in_tu;
1fd400ff 3065
74a0d9f6 3066 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3067 sect_offset sect_off
3068 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3069 type_offset_in_tu
3070 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3071 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3072 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3073 bytes += 3 * 8;
3074
52dc124a 3075 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3076 struct signatured_type);
52dc124a 3077 sig_type->signature = signature;
9c541725 3078 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3079 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3080 sig_type->per_cu.section = section;
9c541725 3081 sig_type->per_cu.sect_off = sect_off;
e3b94546 3082 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3083 sig_type->per_cu.v.quick
1fd400ff
TT
3084 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3085 struct dwarf2_per_cu_quick_data);
3086
52dc124a
DE
3087 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3088 *slot = sig_type;
1fd400ff 3089
b2bdb8cf 3090 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
3091 }
3092
673bfd45 3093 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3094}
3095
927aa2e7
JK
3096/* Create the signatured type hash table from .debug_names. */
3097
3098static void
3099create_signatured_type_table_from_debug_names
ed2dc618 3100 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3101 const mapped_debug_names &map,
3102 struct dwarf2_section_info *section,
3103 struct dwarf2_section_info *abbrev_section)
3104{
ed2dc618
SM
3105 struct objfile *objfile = dwarf2_per_objfile->objfile;
3106
927aa2e7
JK
3107 dwarf2_read_section (objfile, section);
3108 dwarf2_read_section (objfile, abbrev_section);
3109
b2bdb8cf
SM
3110 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3111 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7
JK
3112
3113 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3114
3115 for (uint32_t i = 0; i < map.tu_count; ++i)
3116 {
3117 struct signatured_type *sig_type;
927aa2e7 3118 void **slot;
927aa2e7
JK
3119
3120 sect_offset sect_off
3121 = (sect_offset) (extract_unsigned_integer
3122 (map.tu_table_reordered + i * map.offset_size,
3123 map.offset_size,
3124 map.dwarf5_byte_order));
3125
3126 comp_unit_head cu_header;
ed2dc618
SM
3127 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3128 abbrev_section,
927aa2e7
JK
3129 section->buffer + to_underlying (sect_off),
3130 rcuh_kind::TYPE);
3131
3132 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3133 struct signatured_type);
3134 sig_type->signature = cu_header.signature;
3135 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3136 sig_type->per_cu.is_debug_types = 1;
3137 sig_type->per_cu.section = section;
3138 sig_type->per_cu.sect_off = sect_off;
e3b94546 3139 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3140 sig_type->per_cu.v.quick
3141 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3142 struct dwarf2_per_cu_quick_data);
3143
3144 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3145 *slot = sig_type;
3146
b2bdb8cf 3147 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
3148 }
3149
3150 dwarf2_per_objfile->signatured_types = sig_types_hash;
3151}
3152
9291a0cd
TT
3153/* Read the address map data from the mapped index, and use it to
3154 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3155
9291a0cd 3156static void
ed2dc618
SM
3157create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3158 struct mapped_index *index)
9291a0cd 3159{
ed2dc618 3160 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3161 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3162 const gdb_byte *iter, *end;
9291a0cd 3163 struct addrmap *mutable_map;
9291a0cd
TT
3164 CORE_ADDR baseaddr;
3165
8268c778
PA
3166 auto_obstack temp_obstack;
3167
9291a0cd
TT
3168 mutable_map = addrmap_create_mutable (&temp_obstack);
3169
f00a2de2
PA
3170 iter = index->address_table.data ();
3171 end = iter + index->address_table.size ();
9291a0cd 3172
6a053cb1 3173 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
9291a0cd
TT
3174
3175 while (iter < end)
3176 {
3177 ULONGEST hi, lo, cu_index;
3178 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3179 iter += 8;
3180 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3181 iter += 8;
3182 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3183 iter += 4;
f652bce2 3184
24a55014 3185 if (lo > hi)
f652bce2 3186 {
b98664d3 3187 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3188 hex_string (lo), hex_string (hi));
24a55014 3189 continue;
f652bce2 3190 }
24a55014 3191
b76e467d 3192 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 3193 {
b98664d3 3194 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 3195 (unsigned) cu_index);
24a55014 3196 continue;
f652bce2 3197 }
24a55014 3198
79748972
TT
3199 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3200 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 3201 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 3202 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
3203 }
3204
d320c2b5 3205 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3206 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
3207}
3208
927aa2e7
JK
3209/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3210 populate the objfile's psymtabs_addrmap. */
3211
3212static void
ed2dc618 3213create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3214 struct dwarf2_section_info *section)
3215{
ed2dc618 3216 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3217 bfd *abfd = objfile->obfd;
3218 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6a053cb1 3219 const CORE_ADDR baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
927aa2e7
JK
3220
3221 auto_obstack temp_obstack;
3222 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3223
3224 std::unordered_map<sect_offset,
3225 dwarf2_per_cu_data *,
3226 gdb::hash_enum<sect_offset>>
3227 debug_info_offset_to_per_cu;
b76e467d 3228 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 3229 {
927aa2e7
JK
3230 const auto insertpair
3231 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3232 if (!insertpair.second)
3233 {
3234 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
3235 "debug_info_offset %s, ignoring .debug_aranges."),
3236 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
3237 return;
3238 }
3239 }
3240
3241 dwarf2_read_section (objfile, section);
3242
3243 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3244
3245 const gdb_byte *addr = section->buffer;
3246
3247 while (addr < section->buffer + section->size)
3248 {
3249 const gdb_byte *const entry_addr = addr;
3250 unsigned int bytes_read;
3251
3252 const LONGEST entry_length = read_initial_length (abfd, addr,
3253 &bytes_read);
3254 addr += bytes_read;
3255
3256 const gdb_byte *const entry_end = addr + entry_length;
3257 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3258 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3259 if (addr + entry_length > section->buffer + section->size)
3260 {
47e3f474 3261 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3262 "length %s exceeds section length %s, "
3263 "ignoring .debug_aranges."),
47e3f474
TV
3264 objfile_name (objfile),
3265 plongest (entry_addr - section->buffer),
927aa2e7
JK
3266 plongest (bytes_read + entry_length),
3267 pulongest (section->size));
3268 return;
3269 }
3270
3271 /* The version number. */
3272 const uint16_t version = read_2_bytes (abfd, addr);
3273 addr += 2;
3274 if (version != 2)
3275 {
47e3f474 3276 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3277 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
3278 objfile_name (objfile),
3279 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
3280 return;
3281 }
3282
3283 const uint64_t debug_info_offset
3284 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3285 addr += offset_size;
3286 const auto per_cu_it
3287 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3288 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3289 {
47e3f474 3290 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3291 "debug_info_offset %s does not exists, "
3292 "ignoring .debug_aranges."),
47e3f474
TV
3293 objfile_name (objfile),
3294 plongest (entry_addr - section->buffer),
927aa2e7
JK
3295 pulongest (debug_info_offset));
3296 return;
3297 }
3298 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3299
3300 const uint8_t address_size = *addr++;
3301 if (address_size < 1 || address_size > 8)
3302 {
47e3f474 3303 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3304 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
3305 objfile_name (objfile),
3306 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
3307 return;
3308 }
3309
3310 const uint8_t segment_selector_size = *addr++;
3311 if (segment_selector_size != 0)
3312 {
47e3f474 3313 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3314 "segment_selector_size %u is not supported, "
3315 "ignoring .debug_aranges."),
47e3f474
TV
3316 objfile_name (objfile),
3317 plongest (entry_addr - section->buffer),
927aa2e7
JK
3318 segment_selector_size);
3319 return;
3320 }
3321
3322 /* Must pad to an alignment boundary that is twice the address
3323 size. It is undocumented by the DWARF standard but GCC does
3324 use it. */
3325 for (size_t padding = ((-(addr - section->buffer))
3326 & (2 * address_size - 1));
3327 padding > 0; padding--)
3328 if (*addr++ != 0)
3329 {
47e3f474 3330 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3331 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
3332 objfile_name (objfile),
3333 plongest (entry_addr - section->buffer));
927aa2e7
JK
3334 return;
3335 }
3336
3337 for (;;)
3338 {
3339 if (addr + 2 * address_size > entry_end)
3340 {
47e3f474 3341 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3342 "address list is not properly terminated, "
3343 "ignoring .debug_aranges."),
47e3f474
TV
3344 objfile_name (objfile),
3345 plongest (entry_addr - section->buffer));
927aa2e7
JK
3346 return;
3347 }
3348 ULONGEST start = extract_unsigned_integer (addr, address_size,
3349 dwarf5_byte_order);
3350 addr += address_size;
3351 ULONGEST length = extract_unsigned_integer (addr, address_size,
3352 dwarf5_byte_order);
3353 addr += address_size;
3354 if (start == 0 && length == 0)
3355 break;
3356 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3357 {
3358 /* Symbol was eliminated due to a COMDAT group. */
3359 continue;
3360 }
3361 ULONGEST end = start + length;
79748972
TT
3362 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3363 - baseaddr);
3364 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3365 - baseaddr);
927aa2e7
JK
3366 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3367 }
3368 }
3369
d320c2b5 3370 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3371 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
3372}
3373
9291a0cd
TT
3374/* Find a slot in the mapped index INDEX for the object named NAME.
3375 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3376 constant pool and return true. If NAME cannot be found, return
3377 false. */
2fdf6df6 3378
109483d9 3379static bool
9291a0cd
TT
3380find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3381 offset_type **vec_out)
3382{
0cf03b49 3383 offset_type hash;
9291a0cd 3384 offset_type slot, step;
559a7a62 3385 int (*cmp) (const char *, const char *);
9291a0cd 3386
791afaa2 3387 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3388 if (current_language->la_language == language_cplus
45280282
IB
3389 || current_language->la_language == language_fortran
3390 || current_language->la_language == language_d)
0cf03b49
JK
3391 {
3392 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3393 not contain any. */
a8719064 3394
72998fb3 3395 if (strchr (name, '(') != NULL)
0cf03b49 3396 {
109483d9 3397 without_params = cp_remove_params (name);
0cf03b49 3398
72998fb3 3399 if (without_params != NULL)
791afaa2 3400 name = without_params.get ();
0cf03b49
JK
3401 }
3402 }
3403
559a7a62 3404 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3405 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3406 simulate our NAME being searched is also lowercased. */
3407 hash = mapped_index_string_hash ((index->version == 4
3408 && case_sensitivity == case_sensitive_off
3409 ? 5 : index->version),
3410 name);
3411
f00a2de2
PA
3412 slot = hash & (index->symbol_table.size () - 1);
3413 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3414 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3415
3416 for (;;)
3417 {
9291a0cd 3418 const char *str;
f00a2de2
PA
3419
3420 const auto &bucket = index->symbol_table[slot];
3421 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3422 return false;
9291a0cd 3423
f00a2de2 3424 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3425 if (!cmp (name, str))
9291a0cd
TT
3426 {
3427 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3428 + MAYBE_SWAP (bucket.vec));
109483d9 3429 return true;
9291a0cd
TT
3430 }
3431
f00a2de2 3432 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3433 }
3434}
3435
4485a1c1
SM
3436/* A helper function that reads the .gdb_index from BUFFER and fills
3437 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 3438 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3439 ok to use deprecated sections.
3440
3441 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3442 out parameters that are filled in with information about the CU and
3443 TU lists in the section.
3444
4485a1c1 3445 Returns true if all went well, false otherwise. */
2fdf6df6 3446
d33bc52e 3447static bool
4485a1c1
SM
3448read_gdb_index_from_buffer (struct objfile *objfile,
3449 const char *filename,
3450 bool deprecated_ok,
3451 gdb::array_view<const gdb_byte> buffer,
3452 struct mapped_index *map,
3453 const gdb_byte **cu_list,
3454 offset_type *cu_list_elements,
3455 const gdb_byte **types_list,
3456 offset_type *types_list_elements)
3457{
3458 const gdb_byte *addr = &buffer[0];
82430852 3459
9291a0cd 3460 /* Version check. */
4485a1c1 3461 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3462 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3463 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3464 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3465 indices. */
831adc1f 3466 if (version < 4)
481860b3
GB
3467 {
3468 static int warning_printed = 0;
3469 if (!warning_printed)
3470 {
3471 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3472 filename);
481860b3
GB
3473 warning_printed = 1;
3474 }
3475 return 0;
3476 }
3477 /* Index version 4 uses a different hash function than index version
3478 5 and later.
3479
3480 Versions earlier than 6 did not emit psymbols for inlined
3481 functions. Using these files will cause GDB not to be able to
3482 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3483 indices unless the user has done
3484 "set use-deprecated-index-sections on". */
2ec9a5e0 3485 if (version < 6 && !deprecated_ok)
481860b3
GB
3486 {
3487 static int warning_printed = 0;
3488 if (!warning_printed)
3489 {
e615022a
DE
3490 warning (_("\
3491Skipping deprecated .gdb_index section in %s.\n\
3492Do \"set use-deprecated-index-sections on\" before the file is read\n\
3493to use the section anyway."),
2ec9a5e0 3494 filename);
481860b3
GB
3495 warning_printed = 1;
3496 }
3497 return 0;
3498 }
796a7ff8 3499 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3500 of the TU (for symbols coming from TUs),
3501 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3502 Plus gold-generated indices can have duplicate entries for global symbols,
3503 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3504 These are just performance bugs, and we can't distinguish gdb-generated
3505 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3506
481860b3 3507 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3508 longer backward compatible. */
796a7ff8 3509 if (version > 8)
594e8718 3510 return 0;
9291a0cd 3511
559a7a62 3512 map->version = version;
9291a0cd 3513
4485a1c1 3514 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3515
4485a1c1 3516 int i = 0;
2ec9a5e0
TT
3517 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3518 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3519 / 8);
1fd400ff
TT
3520 ++i;
3521
2ec9a5e0
TT
3522 *types_list = addr + MAYBE_SWAP (metadata[i]);
3523 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3524 - MAYBE_SWAP (metadata[i]))
3525 / 8);
987d643c 3526 ++i;
1fd400ff 3527
f00a2de2
PA
3528 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3529 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3530 map->address_table
3531 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3532 ++i;
3533
f00a2de2
PA
3534 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3535 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3536 map->symbol_table
3537 = gdb::array_view<mapped_index::symbol_table_slot>
3538 ((mapped_index::symbol_table_slot *) symbol_table,
3539 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3540
f00a2de2 3541 ++i;
f9d83a0b 3542 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3543
2ec9a5e0
TT
3544 return 1;
3545}
3546
4485a1c1
SM
3547/* Callback types for dwarf2_read_gdb_index. */
3548
3549typedef gdb::function_view
3550 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3551 get_gdb_index_contents_ftype;
3552typedef gdb::function_view
3553 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3554 get_gdb_index_contents_dwz_ftype;
3555
927aa2e7 3556/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3557 elements of all the CUs and return 1. Otherwise, return 0. */
3558
3559static int
4485a1c1
SM
3560dwarf2_read_gdb_index
3561 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3562 get_gdb_index_contents_ftype get_gdb_index_contents,
3563 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3564{
2ec9a5e0
TT
3565 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3566 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3567 struct dwz_file *dwz;
12359b5e 3568 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3569
4485a1c1
SM
3570 gdb::array_view<const gdb_byte> main_index_contents
3571 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3572
3573 if (main_index_contents.empty ())
3574 return 0;
3575
3063847f 3576 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
3577 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3578 use_deprecated_index_sections,
3579 main_index_contents, map.get (), &cu_list,
3580 &cu_list_elements, &types_list,
3581 &types_list_elements))
2ec9a5e0
TT
3582 return 0;
3583
0fefef59 3584 /* Don't use the index if it's empty. */
3063847f 3585 if (map->symbol_table.empty ())
0fefef59
DE
3586 return 0;
3587
2ec9a5e0
TT
3588 /* If there is a .dwz file, read it so we can get its CU list as
3589 well. */
ed2dc618 3590 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3591 if (dwz != NULL)
2ec9a5e0 3592 {
2ec9a5e0
TT
3593 struct mapped_index dwz_map;
3594 const gdb_byte *dwz_types_ignore;
3595 offset_type dwz_types_elements_ignore;
3596
4485a1c1
SM
3597 gdb::array_view<const gdb_byte> dwz_index_content
3598 = get_gdb_index_contents_dwz (objfile, dwz);
3599
3600 if (dwz_index_content.empty ())
3601 return 0;
3602
3603 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
3604 bfd_get_filename (dwz->dwz_bfd.get ()),
3605 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3606 &dwz_list, &dwz_list_elements,
3607 &dwz_types_ignore,
3608 &dwz_types_elements_ignore))
2ec9a5e0
TT
3609 {
3610 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3611 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3612 return 0;
3613 }
3614 }
3615
12359b5e
SM
3616 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3617 dwz_list, dwz_list_elements);
1fd400ff 3618
8b70b953
TT
3619 if (types_list_elements)
3620 {
8b70b953
TT
3621 /* We can only handle a single .debug_types when we have an
3622 index. */
fd5866f6 3623 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3624 return 0;
3625
fd5866f6 3626 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3627
12359b5e
SM
3628 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3629 types_list, types_list_elements);
8b70b953 3630 }
9291a0cd 3631
3063847f 3632 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3633
3063847f 3634 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3635 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3636 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3637 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3638
3639 return 1;
3640}
3641
dee91e82 3642/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3643
dee91e82
DE
3644static void
3645dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3646 const gdb_byte *info_ptr,
dee91e82
DE
3647 struct die_info *comp_unit_die,
3648 int has_children,
3649 void *data)
9291a0cd 3650{
dee91e82 3651 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3652 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3653 struct dwarf2_per_objfile *dwarf2_per_objfile
3654 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3655 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3656 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3657 struct attribute *attr;
7b9f3c50
DE
3658 void **slot;
3659 struct quick_file_names *qfn;
9291a0cd 3660
0186c6a7
DE
3661 gdb_assert (! this_cu->is_debug_types);
3662
07261596
TT
3663 /* Our callers never want to match partial units -- instead they
3664 will match the enclosing full CU. */
3665 if (comp_unit_die->tag == DW_TAG_partial_unit)
3666 {
3667 this_cu->v.quick->no_file_data = 1;
3668 return;
3669 }
3670
0186c6a7 3671 lh_cu = this_cu;
7b9f3c50 3672 slot = NULL;
dee91e82 3673
fff8551c 3674 line_header_up lh;
9c541725 3675 sect_offset line_offset {};
fff8551c 3676
dee91e82 3677 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3678 if (attr != nullptr)
9291a0cd 3679 {
7b9f3c50
DE
3680 struct quick_file_names find_entry;
3681
9c541725 3682 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3683
3684 /* We may have already read in this line header (TU line header sharing).
3685 If we have we're done. */
094b34ac 3686 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3687 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3688 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3689 &find_entry, INSERT);
3690 if (*slot != NULL)
3691 {
9a3c8263 3692 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3693 return;
7b9f3c50
DE
3694 }
3695
3019eac3 3696 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3697 }
3698 if (lh == NULL)
3699 {
094b34ac 3700 lh_cu->v.quick->no_file_data = 1;
dee91e82 3701 return;
9291a0cd
TT
3702 }
3703
8d749320 3704 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3705 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3706 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3707 gdb_assert (slot != NULL);
3708 *slot = qfn;
9291a0cd 3709
d721ba37 3710 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3711
aa391654
TT
3712 int offset = 0;
3713 if (strcmp (fnd.name, "<unknown>") != 0)
3714 ++offset;
3715
7ba99d21 3716 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3717 qfn->file_names =
aa391654
TT
3718 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3719 if (offset != 0)
3720 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3721 for (int i = 0; i < lh->file_names_size (); ++i)
aa391654 3722 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3723 qfn->real_names = NULL;
9291a0cd 3724
094b34ac 3725 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3726}
3727
3728/* A helper for the "quick" functions which attempts to read the line
3729 table for THIS_CU. */
3730
3731static struct quick_file_names *
e4a48d9d 3732dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3733{
0186c6a7
DE
3734 /* This should never be called for TUs. */
3735 gdb_assert (! this_cu->is_debug_types);
3736 /* Nor type unit groups. */
3737 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3738
dee91e82
DE
3739 if (this_cu->v.quick->file_names != NULL)
3740 return this_cu->v.quick->file_names;
3741 /* If we know there is no line data, no point in looking again. */
3742 if (this_cu->v.quick->no_file_data)
3743 return NULL;
3744
0186c6a7 3745 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3746
3747 if (this_cu->v.quick->no_file_data)
3748 return NULL;
3749 return this_cu->v.quick->file_names;
9291a0cd
TT
3750}
3751
3752/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3753 real path for a given file name from the line table. */
2fdf6df6 3754
9291a0cd 3755static const char *
7b9f3c50
DE
3756dw2_get_real_path (struct objfile *objfile,
3757 struct quick_file_names *qfn, int index)
9291a0cd 3758{
7b9f3c50
DE
3759 if (qfn->real_names == NULL)
3760 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3761 qfn->num_file_names, const char *);
9291a0cd 3762
7b9f3c50 3763 if (qfn->real_names[index] == NULL)
14278e1f 3764 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3765
7b9f3c50 3766 return qfn->real_names[index];
9291a0cd
TT
3767}
3768
3769static struct symtab *
3770dw2_find_last_source_symtab (struct objfile *objfile)
3771{
ed2dc618
SM
3772 struct dwarf2_per_objfile *dwarf2_per_objfile
3773 = get_dwarf2_per_objfile (objfile);
b76e467d 3774 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3775 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3776
43f3e411
DE
3777 if (cust == NULL)
3778 return NULL;
ed2dc618 3779
43f3e411 3780 return compunit_primary_filetab (cust);
9291a0cd
TT
3781}
3782
7b9f3c50
DE
3783/* Traversal function for dw2_forget_cached_source_info. */
3784
3785static int
3786dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3787{
7b9f3c50 3788 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3789
7b9f3c50 3790 if (file_data->real_names)
9291a0cd 3791 {
7b9f3c50 3792 int i;
9291a0cd 3793
7b9f3c50 3794 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3795 {
7b9f3c50
DE
3796 xfree ((void*) file_data->real_names[i]);
3797 file_data->real_names[i] = NULL;
9291a0cd
TT
3798 }
3799 }
7b9f3c50
DE
3800
3801 return 1;
3802}
3803
3804static void
3805dw2_forget_cached_source_info (struct objfile *objfile)
3806{
ed2dc618
SM
3807 struct dwarf2_per_objfile *dwarf2_per_objfile
3808 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
3809
3810 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3811 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3812}
3813
f8eba3c6
TT
3814/* Helper function for dw2_map_symtabs_matching_filename that expands
3815 the symtabs and calls the iterator. */
3816
3817static int
3818dw2_map_expand_apply (struct objfile *objfile,
3819 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3820 const char *name, const char *real_path,
14bc53a8 3821 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3822{
43f3e411 3823 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3824
3825 /* Don't visit already-expanded CUs. */
43f3e411 3826 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3827 return 0;
3828
3829 /* This may expand more than one symtab, and we want to iterate over
3830 all of them. */
58f0c718 3831 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3832
14bc53a8
PA
3833 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3834 last_made, callback);
f8eba3c6
TT
3835}
3836
3837/* Implementation of the map_symtabs_matching_filename method. */
3838
14bc53a8
PA
3839static bool
3840dw2_map_symtabs_matching_filename
3841 (struct objfile *objfile, const char *name, const char *real_path,
3842 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3843{
c011a4f4 3844 const char *name_basename = lbasename (name);
ed2dc618
SM
3845 struct dwarf2_per_objfile *dwarf2_per_objfile
3846 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3847
848e3e78
DE
3848 /* The rule is CUs specify all the files, including those used by
3849 any TU, so there's no need to scan TUs here. */
f4dc4d17 3850
b76e467d 3851 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3852 {
3d7bb9d9 3853 /* We only need to look at symtabs not already expanded. */
43f3e411 3854 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3855 continue;
3856
b76e467d 3857 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3858 if (file_data == NULL)
9291a0cd
TT
3859 continue;
3860
b76e467d 3861 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3862 {
7b9f3c50 3863 const char *this_name = file_data->file_names[j];
da235a7c 3864 const char *this_real_name;
9291a0cd 3865
af529f8f 3866 if (compare_filenames_for_search (this_name, name))
9291a0cd 3867 {
f5b95b50 3868 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3869 callback))
3870 return true;
288e77a7 3871 continue;
4aac40c8 3872 }
9291a0cd 3873
c011a4f4
DE
3874 /* Before we invoke realpath, which can get expensive when many
3875 files are involved, do a quick comparison of the basenames. */
3876 if (! basenames_may_differ
3877 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3878 continue;
3879
da235a7c
JK
3880 this_real_name = dw2_get_real_path (objfile, file_data, j);
3881 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3882 {
da235a7c 3883 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3884 callback))
3885 return true;
288e77a7 3886 continue;
da235a7c 3887 }
9291a0cd 3888
da235a7c
JK
3889 if (real_path != NULL)
3890 {
af529f8f
JK
3891 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3892 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3893 if (this_real_name != NULL
af529f8f 3894 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3895 {
f5b95b50 3896 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3897 callback))
3898 return true;
288e77a7 3899 continue;
9291a0cd
TT
3900 }
3901 }
3902 }
3903 }
3904
14bc53a8 3905 return false;
9291a0cd
TT
3906}
3907
da51c347
DE
3908/* Struct used to manage iterating over all CUs looking for a symbol. */
3909
3910struct dw2_symtab_iterator
9291a0cd 3911{
ed2dc618
SM
3912 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3913 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3914 /* If set, only look for symbols that match that block. Valid values are
3915 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3916 gdb::optional<block_enum> block_index;
da51c347
DE
3917 /* The kind of symbol we're looking for. */
3918 domain_enum domain;
3919 /* The list of CUs from the index entry of the symbol,
3920 or NULL if not found. */
3921 offset_type *vec;
3922 /* The next element in VEC to look at. */
3923 int next;
3924 /* The number of elements in VEC, or zero if there is no match. */
3925 int length;
8943b874
DE
3926 /* Have we seen a global version of the symbol?
3927 If so we can ignore all further global instances.
3928 This is to work around gold/15646, inefficient gold-generated
3929 indices. */
3930 int global_seen;
da51c347 3931};
9291a0cd 3932
2b79f376 3933/* Initialize the index symtab iterator ITER. */
2fdf6df6 3934
9291a0cd 3935static void
da51c347 3936dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3937 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3938 gdb::optional<block_enum> block_index,
da51c347
DE
3939 domain_enum domain,
3940 const char *name)
3941{
ed2dc618 3942 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3943 iter->block_index = block_index;
3944 iter->domain = domain;
3945 iter->next = 0;
8943b874 3946 iter->global_seen = 0;
da51c347 3947
3063847f 3948 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3949
3950 /* index is NULL if OBJF_READNOW. */
3951 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3952 iter->length = MAYBE_SWAP (*iter->vec);
3953 else
3954 {
3955 iter->vec = NULL;
3956 iter->length = 0;
3957 }
3958}
3959
3960/* Return the next matching CU or NULL if there are no more. */
3961
3962static struct dwarf2_per_cu_data *
3963dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3964{
ed2dc618
SM
3965 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3966
da51c347
DE
3967 for ( ; iter->next < iter->length; ++iter->next)
3968 {
3969 offset_type cu_index_and_attrs =
3970 MAYBE_SWAP (iter->vec[iter->next + 1]);
3971 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3972 gdb_index_symbol_kind symbol_kind =
3973 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3974 /* Only check the symbol attributes if they're present.
3975 Indices prior to version 7 don't record them,
3976 and indices >= 7 may elide them for certain symbols
3977 (gold does this). */
3978 int attrs_valid =
ed2dc618 3979 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3980 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3981
3190f0c6 3982 /* Don't crash on bad data. */
b76e467d 3983 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3984 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3985 {
b98664d3 3986 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3987 " [in module %s]"),
3988 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3989 continue;
3990 }
3991
ff4c9fec 3992 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3993
da51c347 3994 /* Skip if already read in. */
43f3e411 3995 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3996 continue;
3997
8943b874
DE
3998 /* Check static vs global. */
3999 if (attrs_valid)
4000 {
2b79f376
SM
4001 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4002
4003 if (iter->block_index.has_value ())
4004 {
4005 bool want_static = *iter->block_index == STATIC_BLOCK;
4006
4007 if (is_static != want_static)
4008 continue;
4009 }
4010
8943b874
DE
4011 /* Work around gold/15646. */
4012 if (!is_static && iter->global_seen)
4013 continue;
4014 if (!is_static)
4015 iter->global_seen = 1;
4016 }
da51c347
DE
4017
4018 /* Only check the symbol's kind if it has one. */
4019 if (attrs_valid)
4020 {
4021 switch (iter->domain)
4022 {
4023 case VAR_DOMAIN:
4024 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4025 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4026 /* Some types are also in VAR_DOMAIN. */
4027 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4028 continue;
4029 break;
4030 case STRUCT_DOMAIN:
4031 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4032 continue;
4033 break;
4034 case LABEL_DOMAIN:
4035 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4036 continue;
4037 break;
59c35742
AB
4038 case MODULE_DOMAIN:
4039 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4040 continue;
4041 break;
da51c347
DE
4042 default:
4043 break;
4044 }
4045 }
4046
4047 ++iter->next;
4048 return per_cu;
4049 }
4050
4051 return NULL;
4052}
4053
43f3e411 4054static struct compunit_symtab *
c7f839cb 4055dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 4056 const char *name, domain_enum domain)
9291a0cd 4057{
43f3e411 4058 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4059 struct dwarf2_per_objfile *dwarf2_per_objfile
4060 = get_dwarf2_per_objfile (objfile);
9291a0cd 4061
b5ec771e
PA
4062 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4063
ed2dc618
SM
4064 struct dw2_symtab_iterator iter;
4065 struct dwarf2_per_cu_data *per_cu;
da51c347 4066
2b79f376 4067 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 4068
ed2dc618
SM
4069 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4070 {
4071 struct symbol *sym, *with_opaque = NULL;
58f0c718 4072 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 4073 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 4074 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4075
ed2dc618
SM
4076 sym = block_find_symbol (block, name, domain,
4077 block_find_non_opaque_type_preferred,
4078 &with_opaque);
b2e2f908 4079
ed2dc618
SM
4080 /* Some caution must be observed with overloaded functions
4081 and methods, since the index will not contain any overload
4082 information (but NAME might contain it). */
da51c347 4083
ed2dc618
SM
4084 if (sym != NULL
4085 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4086 return stab;
4087 if (with_opaque != NULL
4088 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4089 stab_best = stab;
da51c347 4090
ed2dc618 4091 /* Keep looking through other CUs. */
9291a0cd 4092 }
9291a0cd 4093
da51c347 4094 return stab_best;
9291a0cd
TT
4095}
4096
4097static void
4098dw2_print_stats (struct objfile *objfile)
4099{
ed2dc618
SM
4100 struct dwarf2_per_objfile *dwarf2_per_objfile
4101 = get_dwarf2_per_objfile (objfile);
b76e467d 4102 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4103 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 4104 int count = 0;
9291a0cd 4105
ed2dc618 4106 for (int i = 0; i < total; ++i)
9291a0cd 4107 {
ff4c9fec 4108 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4109
43f3e411 4110 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4111 ++count;
4112 }
e4a48d9d 4113 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4114 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4115}
4116
779bd270
DE
4117/* This dumps minimal information about the index.
4118 It is called via "mt print objfiles".
4119 One use is to verify .gdb_index has been loaded by the
4120 gdb.dwarf2/gdb-index.exp testcase. */
4121
9291a0cd
TT
4122static void
4123dw2_dump (struct objfile *objfile)
4124{
ed2dc618
SM
4125 struct dwarf2_per_objfile *dwarf2_per_objfile
4126 = get_dwarf2_per_objfile (objfile);
4127
779bd270
DE
4128 gdb_assert (dwarf2_per_objfile->using_index);
4129 printf_filtered (".gdb_index:");
4130 if (dwarf2_per_objfile->index_table != NULL)
4131 {
4132 printf_filtered (" version %d\n",
4133 dwarf2_per_objfile->index_table->version);
4134 }
4135 else
4136 printf_filtered (" faked for \"readnow\"\n");
4137 printf_filtered ("\n");
9291a0cd
TT
4138}
4139
9291a0cd
TT
4140static void
4141dw2_expand_symtabs_for_function (struct objfile *objfile,
4142 const char *func_name)
4143{
ed2dc618
SM
4144 struct dwarf2_per_objfile *dwarf2_per_objfile
4145 = get_dwarf2_per_objfile (objfile);
da51c347 4146
ed2dc618
SM
4147 struct dw2_symtab_iterator iter;
4148 struct dwarf2_per_cu_data *per_cu;
da51c347 4149
2b79f376 4150 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 4151
ed2dc618 4152 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 4153 dw2_instantiate_symtab (per_cu, false);
da51c347 4154
9291a0cd
TT
4155}
4156
4157static void
4158dw2_expand_all_symtabs (struct objfile *objfile)
4159{
ed2dc618
SM
4160 struct dwarf2_per_objfile *dwarf2_per_objfile
4161 = get_dwarf2_per_objfile (objfile);
b76e467d 4162 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4163 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 4164
ed2dc618 4165 for (int i = 0; i < total_units; ++i)
9291a0cd 4166 {
ff4c9fec 4167 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4168
58f0c718
TT
4169 /* We don't want to directly expand a partial CU, because if we
4170 read it with the wrong language, then assertion failures can
4171 be triggered later on. See PR symtab/23010. So, tell
4172 dw2_instantiate_symtab to skip partial CUs -- any important
4173 partial CU will be read via DW_TAG_imported_unit anyway. */
4174 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
4175 }
4176}
4177
4178static void
652a8996
JK
4179dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4180 const char *fullname)
9291a0cd 4181{
ed2dc618
SM
4182 struct dwarf2_per_objfile *dwarf2_per_objfile
4183 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4184
4185 /* We don't need to consider type units here.
4186 This is only called for examining code, e.g. expand_line_sal.
4187 There can be an order of magnitude (or more) more type units
4188 than comp units, and we avoid them if we can. */
4189
b76e467d 4190 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 4191 {
3d7bb9d9 4192 /* We only need to look at symtabs not already expanded. */
43f3e411 4193 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4194 continue;
4195
b76e467d 4196 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 4197 if (file_data == NULL)
9291a0cd
TT
4198 continue;
4199
b76e467d 4200 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4201 {
652a8996
JK
4202 const char *this_fullname = file_data->file_names[j];
4203
4204 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4205 {
58f0c718 4206 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
4207 break;
4208 }
4209 }
4210 }
4211}
4212
9291a0cd 4213static void
199b4314
TT
4214dw2_map_matching_symbols
4215 (struct objfile *objfile,
b054970d 4216 const lookup_name_info &name, domain_enum domain,
199b4314
TT
4217 int global,
4218 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 4219 symbol_compare_ftype *ordered_compare)
9291a0cd 4220{
40658b94 4221 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4222 current language is Ada for a non-Ada objfile using GNU index. As Ada
4223 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4224}
4225
e1ef7d7a
PA
4226/* Starting from a search name, return the string that finds the upper
4227 bound of all strings that start with SEARCH_NAME in a sorted name
4228 list. Returns the empty string to indicate that the upper bound is
4229 the end of the list. */
4230
4231static std::string
4232make_sort_after_prefix_name (const char *search_name)
4233{
4234 /* When looking to complete "func", we find the upper bound of all
4235 symbols that start with "func" by looking for where we'd insert
4236 the closest string that would follow "func" in lexicographical
4237 order. Usually, that's "func"-with-last-character-incremented,
4238 i.e. "fund". Mind non-ASCII characters, though. Usually those
4239 will be UTF-8 multi-byte sequences, but we can't be certain.
4240 Especially mind the 0xff character, which is a valid character in
4241 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4242 rule out compilers allowing it in identifiers. Note that
4243 conveniently, strcmp/strcasecmp are specified to compare
4244 characters interpreted as unsigned char. So what we do is treat
4245 the whole string as a base 256 number composed of a sequence of
4246 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4247 to 0, and carries 1 to the following more-significant position.
4248 If the very first character in SEARCH_NAME ends up incremented
4249 and carries/overflows, then the upper bound is the end of the
4250 list. The string after the empty string is also the empty
4251 string.
4252
4253 Some examples of this operation:
4254
4255 SEARCH_NAME => "+1" RESULT
4256
4257 "abc" => "abd"
4258 "ab\xff" => "ac"
4259 "\xff" "a" "\xff" => "\xff" "b"
4260 "\xff" => ""
4261 "\xff\xff" => ""
4262 "" => ""
4263
4264 Then, with these symbols for example:
4265
4266 func
4267 func1
4268 fund
4269
4270 completing "func" looks for symbols between "func" and
4271 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4272 which finds "func" and "func1", but not "fund".
4273
4274 And with:
4275
4276 funcÿ (Latin1 'ÿ' [0xff])
4277 funcÿ1
4278 fund
4279
4280 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4281 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4282
4283 And with:
4284
4285 ÿÿ (Latin1 'ÿ' [0xff])
4286 ÿÿ1
4287
4288 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4289 the end of the list.
4290 */
4291 std::string after = search_name;
4292 while (!after.empty () && (unsigned char) after.back () == 0xff)
4293 after.pop_back ();
4294 if (!after.empty ())
4295 after.back () = (unsigned char) after.back () + 1;
4296 return after;
4297}
4298
5c58de74 4299/* See declaration. */
61d96d7e 4300
5c58de74
PA
4301std::pair<std::vector<name_component>::const_iterator,
4302 std::vector<name_component>::const_iterator>
44ed8f3e 4303mapped_index_base::find_name_components_bounds
3b00ef10 4304 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 4305{
5c58de74
PA
4306 auto *name_cmp
4307 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 4308
3b00ef10
TT
4309 const char *lang_name
4310 = lookup_name_without_params.language_lookup_name (lang).c_str ();
9291a0cd 4311
3f563c84
PA
4312 /* Comparison function object for lower_bound that matches against a
4313 given symbol name. */
4314 auto lookup_compare_lower = [&] (const name_component &elem,
4315 const char *name)
4316 {
5c58de74 4317 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4318 const char *elem_name = elem_qualified + elem.name_offset;
4319 return name_cmp (elem_name, name) < 0;
4320 };
4321
4322 /* Comparison function object for upper_bound that matches against a
4323 given symbol name. */
4324 auto lookup_compare_upper = [&] (const char *name,
4325 const name_component &elem)
4326 {
5c58de74 4327 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4328 const char *elem_name = elem_qualified + elem.name_offset;
4329 return name_cmp (name, elem_name) < 0;
4330 };
4331
5c58de74
PA
4332 auto begin = this->name_components.begin ();
4333 auto end = this->name_components.end ();
3f563c84
PA
4334
4335 /* Find the lower bound. */
4336 auto lower = [&] ()
4337 {
3b00ef10 4338 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
4339 return begin;
4340 else
3b00ef10 4341 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
4342 } ();
4343
4344 /* Find the upper bound. */
4345 auto upper = [&] ()
4346 {
5c58de74 4347 if (lookup_name_without_params.completion_mode ())
3f563c84 4348 {
e1ef7d7a
PA
4349 /* In completion mode, we want UPPER to point past all
4350 symbols names that have the same prefix. I.e., with
4351 these symbols, and completing "func":
4352
4353 function << lower bound
4354 function1
4355 other_function << upper bound
4356
4357 We find the upper bound by looking for the insertion
4358 point of "func"-with-last-character-incremented,
4359 i.e. "fund". */
3b00ef10 4360 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 4361 if (after.empty ())
3f563c84 4362 return end;
e6b2f5ef
PA
4363 return std::lower_bound (lower, end, after.c_str (),
4364 lookup_compare_lower);
3f563c84
PA
4365 }
4366 else
3b00ef10 4367 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
4368 } ();
4369
5c58de74
PA
4370 return {lower, upper};
4371}
4372
4373/* See declaration. */
4374
4375void
44ed8f3e 4376mapped_index_base::build_name_components ()
5c58de74
PA
4377{
4378 if (!this->name_components.empty ())
4379 return;
4380
4381 this->name_components_casing = case_sensitivity;
4382 auto *name_cmp
4383 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4384
4385 /* The code below only knows how to break apart components of C++
4386 symbol names (and other languages that use '::' as
3b00ef10 4387 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
4388 auto count = this->symbol_name_count ();
4389 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4390 {
44ed8f3e 4391 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4392 continue;
4393
4394 const char *name = this->symbol_name_at (idx);
4395
4396 /* Add each name component to the name component table. */
4397 unsigned int previous_len = 0;
3b00ef10
TT
4398
4399 if (strstr (name, "::") != nullptr)
4400 {
4401 for (unsigned int current_len = cp_find_first_component (name);
4402 name[current_len] != '\0';
4403 current_len += cp_find_first_component (name + current_len))
4404 {
4405 gdb_assert (name[current_len] == ':');
4406 this->name_components.push_back ({previous_len, idx});
4407 /* Skip the '::'. */
4408 current_len += 2;
4409 previous_len = current_len;
4410 }
4411 }
4412 else
5c58de74 4413 {
3b00ef10
TT
4414 /* Handle the Ada encoded (aka mangled) form here. */
4415 for (const char *iter = strstr (name, "__");
4416 iter != nullptr;
4417 iter = strstr (iter, "__"))
4418 {
4419 this->name_components.push_back ({previous_len, idx});
4420 iter += 2;
4421 previous_len = iter - name;
4422 }
5c58de74 4423 }
3b00ef10 4424
5c58de74
PA
4425 this->name_components.push_back ({previous_len, idx});
4426 }
4427
4428 /* Sort name_components elements by name. */
4429 auto name_comp_compare = [&] (const name_component &left,
4430 const name_component &right)
4431 {
4432 const char *left_qualified = this->symbol_name_at (left.idx);
4433 const char *right_qualified = this->symbol_name_at (right.idx);
4434
4435 const char *left_name = left_qualified + left.name_offset;
4436 const char *right_name = right_qualified + right.name_offset;
4437
4438 return name_cmp (left_name, right_name) < 0;
4439 };
4440
4441 std::sort (this->name_components.begin (),
4442 this->name_components.end (),
4443 name_comp_compare);
4444}
4445
4446/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4447 mapped_index_base instead of the containing objfile. This is split
4448 to a separate function in order to be able to unit test the
4449 name_components matching using a mock mapped_index_base. For each
5c58de74 4450 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4451 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4452
4453static void
4454dw2_expand_symtabs_matching_symbol
44ed8f3e 4455 (mapped_index_base &index,
5c58de74
PA
4456 const lookup_name_info &lookup_name_in,
4457 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4458 enum search_domain kind,
3b00ef10 4459 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
4460{
4461 lookup_name_info lookup_name_without_params
4462 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4463
4464 /* Build the symbol name component sorted vector, if we haven't
4465 yet. */
4466 index.build_name_components ();
4467
3f563c84
PA
4468 /* The same symbol may appear more than once in the range though.
4469 E.g., if we're looking for symbols that complete "w", and we have
4470 a symbol named "w1::w2", we'll find the two name components for
4471 that same symbol in the range. To be sure we only call the
4472 callback once per symbol, we first collect the symbol name
4473 indexes that matched in a temporary vector and ignore
4474 duplicates. */
4475 std::vector<offset_type> matches;
3f563c84 4476
3b00ef10
TT
4477 struct name_and_matcher
4478 {
4479 symbol_name_matcher_ftype *matcher;
4480 const std::string &name;
4481
4482 bool operator== (const name_and_matcher &other) const
3f563c84 4483 {
3b00ef10
TT
4484 return matcher == other.matcher && name == other.name;
4485 }
4486 };
4487
4488 /* A vector holding all the different symbol name matchers, for all
4489 languages. */
4490 std::vector<name_and_matcher> matchers;
4491
4492 for (int i = 0; i < nr_languages; i++)
4493 {
4494 enum language lang_e = (enum language) i;
4495
4496 const language_defn *lang = language_def (lang_e);
4497 symbol_name_matcher_ftype *name_matcher
4498 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4499
3b00ef10
TT
4500 name_and_matcher key {
4501 name_matcher,
4502 lookup_name_without_params.language_lookup_name (lang_e)
4503 };
4504
4505 /* Don't insert the same comparison routine more than once.
4506 Note that we do this linear walk. This is not a problem in
4507 practice because the number of supported languages is
4508 low. */
4509 if (std::find (matchers.begin (), matchers.end (), key)
4510 != matchers.end ())
9291a0cd 4511 continue;
3b00ef10
TT
4512 matchers.push_back (std::move (key));
4513
4514 auto bounds
4515 = index.find_name_components_bounds (lookup_name_without_params,
4516 lang_e);
4517
4518 /* Now for each symbol name in range, check to see if we have a name
4519 match, and if so, call the MATCH_CALLBACK callback. */
4520
4521 for (; bounds.first != bounds.second; ++bounds.first)
4522 {
4523 const char *qualified = index.symbol_name_at (bounds.first->idx);
4524
4525 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4526 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4527 continue;
9291a0cd 4528
3b00ef10
TT
4529 matches.push_back (bounds.first->idx);
4530 }
3f563c84
PA
4531 }
4532
4533 std::sort (matches.begin (), matches.end ());
4534
4535 /* Finally call the callback, once per match. */
4536 ULONGEST prev = -1;
4537 for (offset_type idx : matches)
4538 {
4539 if (prev != idx)
4540 {
3b00ef10
TT
4541 if (!match_callback (idx))
4542 break;
3f563c84
PA
4543 prev = idx;
4544 }
4545 }
4546
4547 /* Above we use a type wider than idx's for 'prev', since 0 and
4548 (offset_type)-1 are both possible values. */
4549 static_assert (sizeof (prev) > sizeof (offset_type), "");
4550}
4551
c62446b1
PA
4552#if GDB_SELF_TEST
4553
4554namespace selftests { namespace dw2_expand_symtabs_matching {
4555
a3c5fafd
PA
4556/* A mock .gdb_index/.debug_names-like name index table, enough to
4557 exercise dw2_expand_symtabs_matching_symbol, which works with the
4558 mapped_index_base interface. Builds an index from the symbol list
4559 passed as parameter to the constructor. */
4560class mock_mapped_index : public mapped_index_base
c62446b1
PA
4561{
4562public:
a3c5fafd
PA
4563 mock_mapped_index (gdb::array_view<const char *> symbols)
4564 : m_symbol_table (symbols)
c62446b1
PA
4565 {}
4566
a3c5fafd 4567 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4568
a3c5fafd 4569 /* Return the number of names in the symbol table. */
632e107b 4570 size_t symbol_name_count () const override
c62446b1 4571 {
a3c5fafd 4572 return m_symbol_table.size ();
c62446b1
PA
4573 }
4574
a3c5fafd 4575 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4576 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4577 {
4578 return m_symbol_table[idx];
4579 }
c62446b1 4580
a3c5fafd
PA
4581private:
4582 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4583};
4584
4585/* Convenience function that converts a NULL pointer to a "<null>"
4586 string, to pass to print routines. */
4587
4588static const char *
4589string_or_null (const char *str)
4590{
4591 return str != NULL ? str : "<null>";
4592}
4593
4594/* Check if a lookup_name_info built from
4595 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4596 index. EXPECTED_LIST is the list of expected matches, in expected
4597 matching order. If no match expected, then an empty list is
4598 specified. Returns true on success. On failure prints a warning
4599 indicating the file:line that failed, and returns false. */
4600
4601static bool
4602check_match (const char *file, int line,
4603 mock_mapped_index &mock_index,
4604 const char *name, symbol_name_match_type match_type,
4605 bool completion_mode,
4606 std::initializer_list<const char *> expected_list)
4607{
4608 lookup_name_info lookup_name (name, match_type, completion_mode);
4609
4610 bool matched = true;
4611
4612 auto mismatch = [&] (const char *expected_str,
4613 const char *got)
4614 {
4615 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4616 "expected=\"%s\", got=\"%s\"\n"),
4617 file, line,
4618 (match_type == symbol_name_match_type::FULL
4619 ? "FULL" : "WILD"),
4620 name, string_or_null (expected_str), string_or_null (got));
4621 matched = false;
4622 };
4623
4624 auto expected_it = expected_list.begin ();
4625 auto expected_end = expected_list.end ();
4626
a3c5fafd 4627 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4628 NULL, ALL_DOMAIN,
4629 [&] (offset_type idx)
4630 {
a3c5fafd 4631 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4632 const char *expected_str
4633 = expected_it == expected_end ? NULL : *expected_it++;
4634
4635 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4636 mismatch (expected_str, matched_name);
3b00ef10 4637 return true;
c62446b1
PA
4638 });
4639
4640 const char *expected_str
4641 = expected_it == expected_end ? NULL : *expected_it++;
4642 if (expected_str != NULL)
4643 mismatch (expected_str, NULL);
4644
4645 return matched;
4646}
4647
4648/* The symbols added to the mock mapped_index for testing (in
4649 canonical form). */
4650static const char *test_symbols[] = {
4651 "function",
4652 "std::bar",
4653 "std::zfunction",
4654 "std::zfunction2",
4655 "w1::w2",
4656 "ns::foo<char*>",
4657 "ns::foo<int>",
4658 "ns::foo<long>",
a20714ff
PA
4659 "ns2::tmpl<int>::foo2",
4660 "(anonymous namespace)::A::B::C",
c62446b1 4661
e1ef7d7a
PA
4662 /* These are used to check that the increment-last-char in the
4663 matching algorithm for completion doesn't match "t1_fund" when
4664 completing "t1_func". */
4665 "t1_func",
4666 "t1_func1",
4667 "t1_fund",
4668 "t1_fund1",
4669
4670 /* A UTF-8 name with multi-byte sequences to make sure that
4671 cp-name-parser understands this as a single identifier ("função"
4672 is "function" in PT). */
4673 u8"u8função",
4674
4675 /* \377 (0xff) is Latin1 'ÿ'. */
4676 "yfunc\377",
4677
4678 /* \377 (0xff) is Latin1 'ÿ'. */
4679 "\377",
4680 "\377\377123",
4681
c62446b1
PA
4682 /* A name with all sorts of complications. Starts with "z" to make
4683 it easier for the completion tests below. */
4684#define Z_SYM_NAME \
4685 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4686 "::tuple<(anonymous namespace)::ui*, " \
4687 "std::default_delete<(anonymous namespace)::ui>, void>"
4688
4689 Z_SYM_NAME
4690};
4691
a3c5fafd
PA
4692/* Returns true if the mapped_index_base::find_name_component_bounds
4693 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4694 in completion mode. */
5c58de74
PA
4695
4696static bool
a3c5fafd 4697check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4698 const char *search_name,
4699 gdb::array_view<const char *> expected_syms)
4700{
4701 lookup_name_info lookup_name (search_name,
4702 symbol_name_match_type::FULL, true);
4703
3b00ef10
TT
4704 auto bounds = index.find_name_components_bounds (lookup_name,
4705 language_cplus);
5c58de74
PA
4706
4707 size_t distance = std::distance (bounds.first, bounds.second);
4708 if (distance != expected_syms.size ())
4709 return false;
4710
4711 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4712 {
4713 auto nc_elem = bounds.first + exp_elem;
4714 const char *qualified = index.symbol_name_at (nc_elem->idx);
4715 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4716 return false;
4717 }
4718
4719 return true;
4720}
4721
4722/* Test the lower-level mapped_index::find_name_component_bounds
4723 method. */
4724
c62446b1 4725static void
5c58de74
PA
4726test_mapped_index_find_name_component_bounds ()
4727{
4728 mock_mapped_index mock_index (test_symbols);
4729
a3c5fafd 4730 mock_index.build_name_components ();
5c58de74
PA
4731
4732 /* Test the lower-level mapped_index::find_name_component_bounds
4733 method in completion mode. */
4734 {
4735 static const char *expected_syms[] = {
4736 "t1_func",
4737 "t1_func1",
5c58de74
PA
4738 };
4739
a3c5fafd 4740 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4741 "t1_func", expected_syms));
4742 }
4743
4744 /* Check that the increment-last-char in the name matching algorithm
4745 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4746 {
4747 static const char *expected_syms1[] = {
4748 "\377",
4749 "\377\377123",
4750 };
a3c5fafd 4751 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4752 "\377", expected_syms1));
4753
4754 static const char *expected_syms2[] = {
4755 "\377\377123",
4756 };
a3c5fafd 4757 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4758 "\377\377", expected_syms2));
4759 }
4760}
4761
4762/* Test dw2_expand_symtabs_matching_symbol. */
4763
4764static void
4765test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4766{
4767 mock_mapped_index mock_index (test_symbols);
4768
4769 /* We let all tests run until the end even if some fails, for debug
4770 convenience. */
4771 bool any_mismatch = false;
4772
4773 /* Create the expected symbols list (an initializer_list). Needed
4774 because lists have commas, and we need to pass them to CHECK,
4775 which is a macro. */
4776#define EXPECT(...) { __VA_ARGS__ }
4777
4778 /* Wrapper for check_match that passes down the current
4779 __FILE__/__LINE__. */
4780#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4781 any_mismatch |= !check_match (__FILE__, __LINE__, \
4782 mock_index, \
4783 NAME, MATCH_TYPE, COMPLETION_MODE, \
4784 EXPECTED_LIST)
4785
4786 /* Identity checks. */
4787 for (const char *sym : test_symbols)
4788 {
4789 /* Should be able to match all existing symbols. */
4790 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4791 EXPECT (sym));
4792
4793 /* Should be able to match all existing symbols with
4794 parameters. */
4795 std::string with_params = std::string (sym) + "(int)";
4796 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4797 EXPECT (sym));
4798
4799 /* Should be able to match all existing symbols with
4800 parameters and qualifiers. */
4801 with_params = std::string (sym) + " ( int ) const";
4802 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4803 EXPECT (sym));
4804
4805 /* This should really find sym, but cp-name-parser.y doesn't
4806 know about lvalue/rvalue qualifiers yet. */
4807 with_params = std::string (sym) + " ( int ) &&";
4808 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4809 {});
4810 }
4811
e1ef7d7a
PA
4812 /* Check that the name matching algorithm for completion doesn't get
4813 confused with Latin1 'ÿ' / 0xff. */
4814 {
4815 static const char str[] = "\377";
4816 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4817 EXPECT ("\377", "\377\377123"));
4818 }
4819
4820 /* Check that the increment-last-char in the matching algorithm for
4821 completion doesn't match "t1_fund" when completing "t1_func". */
4822 {
4823 static const char str[] = "t1_func";
4824 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4825 EXPECT ("t1_func", "t1_func1"));
4826 }
4827
c62446b1
PA
4828 /* Check that completion mode works at each prefix of the expected
4829 symbol name. */
4830 {
4831 static const char str[] = "function(int)";
4832 size_t len = strlen (str);
4833 std::string lookup;
4834
4835 for (size_t i = 1; i < len; i++)
4836 {
4837 lookup.assign (str, i);
4838 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4839 EXPECT ("function"));
4840 }
4841 }
4842
4843 /* While "w" is a prefix of both components, the match function
4844 should still only be called once. */
4845 {
4846 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4847 EXPECT ("w1::w2"));
a20714ff
PA
4848 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4849 EXPECT ("w1::w2"));
c62446b1
PA
4850 }
4851
4852 /* Same, with a "complicated" symbol. */
4853 {
4854 static const char str[] = Z_SYM_NAME;
4855 size_t len = strlen (str);
4856 std::string lookup;
4857
4858 for (size_t i = 1; i < len; i++)
4859 {
4860 lookup.assign (str, i);
4861 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4862 EXPECT (Z_SYM_NAME));
4863 }
4864 }
4865
4866 /* In FULL mode, an incomplete symbol doesn't match. */
4867 {
4868 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4869 {});
4870 }
4871
4872 /* A complete symbol with parameters matches any overload, since the
4873 index has no overload info. */
4874 {
4875 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4876 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4877 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4878 EXPECT ("std::zfunction", "std::zfunction2"));
4879 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4880 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4881 }
4882
4883 /* Check that whitespace is ignored appropriately. A symbol with a
4884 template argument list. */
4885 {
4886 static const char expected[] = "ns::foo<int>";
4887 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4888 EXPECT (expected));
a20714ff
PA
4889 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4890 EXPECT (expected));
c62446b1
PA
4891 }
4892
4893 /* Check that whitespace is ignored appropriately. A symbol with a
4894 template argument list that includes a pointer. */
4895 {
4896 static const char expected[] = "ns::foo<char*>";
4897 /* Try both completion and non-completion modes. */
4898 static const bool completion_mode[2] = {false, true};
4899 for (size_t i = 0; i < 2; i++)
4900 {
4901 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4902 completion_mode[i], EXPECT (expected));
a20714ff
PA
4903 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4904 completion_mode[i], EXPECT (expected));
c62446b1
PA
4905
4906 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4907 completion_mode[i], EXPECT (expected));
a20714ff
PA
4908 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4909 completion_mode[i], EXPECT (expected));
c62446b1
PA
4910 }
4911 }
4912
4913 {
4914 /* Check method qualifiers are ignored. */
4915 static const char expected[] = "ns::foo<char*>";
4916 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4917 symbol_name_match_type::FULL, true, EXPECT (expected));
4918 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4919 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4920 CHECK_MATCH ("foo < char * > ( int ) const",
4921 symbol_name_match_type::WILD, true, EXPECT (expected));
4922 CHECK_MATCH ("foo < char * > ( int ) &&",
4923 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4924 }
4925
4926 /* Test lookup names that don't match anything. */
4927 {
a20714ff
PA
4928 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4929 {});
4930
c62446b1
PA
4931 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4932 {});
4933 }
4934
a20714ff
PA
4935 /* Some wild matching tests, exercising "(anonymous namespace)",
4936 which should not be confused with a parameter list. */
4937 {
4938 static const char *syms[] = {
4939 "A::B::C",
4940 "B::C",
4941 "C",
4942 "A :: B :: C ( int )",
4943 "B :: C ( int )",
4944 "C ( int )",
4945 };
4946
4947 for (const char *s : syms)
4948 {
4949 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4950 EXPECT ("(anonymous namespace)::A::B::C"));
4951 }
4952 }
4953
4954 {
4955 static const char expected[] = "ns2::tmpl<int>::foo2";
4956 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4957 EXPECT (expected));
4958 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4959 EXPECT (expected));
4960 }
4961
c62446b1
PA
4962 SELF_CHECK (!any_mismatch);
4963
4964#undef EXPECT
4965#undef CHECK_MATCH
4966}
4967
5c58de74
PA
4968static void
4969run_test ()
4970{
4971 test_mapped_index_find_name_component_bounds ();
4972 test_dw2_expand_symtabs_matching_symbol ();
4973}
4974
c62446b1
PA
4975}} // namespace selftests::dw2_expand_symtabs_matching
4976
4977#endif /* GDB_SELF_TEST */
4978
4b514bc8
JK
4979/* If FILE_MATCHER is NULL or if PER_CU has
4980 dwarf2_per_cu_quick_data::MARK set (see
4981 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4982 EXPANSION_NOTIFY on it. */
4983
4984static void
4985dw2_expand_symtabs_matching_one
4986 (struct dwarf2_per_cu_data *per_cu,
4987 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4988 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4989{
4990 if (file_matcher == NULL || per_cu->v.quick->mark)
4991 {
4992 bool symtab_was_null
4993 = (per_cu->v.quick->compunit_symtab == NULL);
4994
58f0c718 4995 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4996
4997 if (expansion_notify != NULL
4998 && symtab_was_null
4999 && per_cu->v.quick->compunit_symtab != NULL)
5000 expansion_notify (per_cu->v.quick->compunit_symtab);
5001 }
5002}
5003
3f563c84
PA
5004/* Helper for dw2_expand_matching symtabs. Called on each symbol
5005 matched, to expand corresponding CUs that were marked. IDX is the
5006 index of the symbol name that matched. */
5007
5008static void
5009dw2_expand_marked_cus
ed2dc618 5010 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
5011 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5012 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5013 search_domain kind)
5014{
3f563c84
PA
5015 offset_type *vec, vec_len, vec_idx;
5016 bool global_seen = false;
ed2dc618 5017 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5018
61920122 5019 vec = (offset_type *) (index.constant_pool
f00a2de2 5020 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5021 vec_len = MAYBE_SWAP (vec[0]);
5022 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5023 {
61920122
PA
5024 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5025 /* This value is only valid for index versions >= 7. */
5026 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5027 gdb_index_symbol_kind symbol_kind =
5028 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5029 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5030 /* Only check the symbol attributes if they're present.
5031 Indices prior to version 7 don't record them,
5032 and indices >= 7 may elide them for certain symbols
5033 (gold does this). */
5034 int attrs_valid =
5035 (index.version >= 7
5036 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5037
5038 /* Work around gold/15646. */
5039 if (attrs_valid)
9291a0cd 5040 {
61920122
PA
5041 if (!is_static && global_seen)
5042 continue;
5043 if (!is_static)
5044 global_seen = true;
5045 }
3190f0c6 5046
61920122
PA
5047 /* Only check the symbol's kind if it has one. */
5048 if (attrs_valid)
5049 {
5050 switch (kind)
8943b874 5051 {
61920122
PA
5052 case VARIABLES_DOMAIN:
5053 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5054 continue;
5055 break;
5056 case FUNCTIONS_DOMAIN:
5057 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5058 continue;
61920122
PA
5059 break;
5060 case TYPES_DOMAIN:
5061 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5062 continue;
5063 break;
59c35742
AB
5064 case MODULES_DOMAIN:
5065 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
5066 continue;
5067 break;
61920122
PA
5068 default:
5069 break;
8943b874 5070 }
61920122 5071 }
8943b874 5072
61920122 5073 /* Don't crash on bad data. */
b76e467d 5074 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5075 + dwarf2_per_objfile->all_type_units.size ()))
61920122 5076 {
b98664d3 5077 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
5078 " [in module %s]"),
5079 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5080 continue;
5081 }
5082
ff4c9fec 5083 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
5084 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5085 expansion_notify);
61920122
PA
5086 }
5087}
5088
4b514bc8
JK
5089/* If FILE_MATCHER is non-NULL, set all the
5090 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5091 that match FILE_MATCHER. */
5092
61920122 5093static void
4b514bc8 5094dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5095 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5096 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5097{
4b514bc8 5098 if (file_matcher == NULL)
61920122
PA
5099 return;
5100
4b514bc8
JK
5101 objfile *const objfile = dwarf2_per_objfile->objfile;
5102
5103 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5104 htab_eq_pointer,
5105 NULL, xcalloc, xfree));
5106 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5107 htab_eq_pointer,
5108 NULL, xcalloc, xfree));
61920122 5109
4b514bc8
JK
5110 /* The rule is CUs specify all the files, including those used by
5111 any TU, so there's no need to scan TUs here. */
61920122 5112
b76e467d 5113 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5114 {
927aa2e7
JK
5115 QUIT;
5116
5117 per_cu->v.quick->mark = 0;
5118
5119 /* We only need to look at symtabs not already expanded. */
5120 if (per_cu->v.quick->compunit_symtab)
5121 continue;
5122
b76e467d 5123 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5124 if (file_data == NULL)
5125 continue;
5126
5127 if (htab_find (visited_not_found.get (), file_data) != NULL)
5128 continue;
5129 else if (htab_find (visited_found.get (), file_data) != NULL)
5130 {
5131 per_cu->v.quick->mark = 1;
5132 continue;
5133 }
5134
b76e467d 5135 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
5136 {
5137 const char *this_real_name;
5138
5139 if (file_matcher (file_data->file_names[j], false))
5140 {
5141 per_cu->v.quick->mark = 1;
5142 break;
5143 }
5144
5145 /* Before we invoke realpath, which can get expensive when many
5146 files are involved, do a quick comparison of the basenames. */
5147 if (!basenames_may_differ
5148 && !file_matcher (lbasename (file_data->file_names[j]),
5149 true))
5150 continue;
5151
5152 this_real_name = dw2_get_real_path (objfile, file_data, j);
5153 if (file_matcher (this_real_name, false))
5154 {
5155 per_cu->v.quick->mark = 1;
5156 break;
5157 }
5158 }
5159
b76e467d
SM
5160 void **slot = htab_find_slot (per_cu->v.quick->mark
5161 ? visited_found.get ()
5162 : visited_not_found.get (),
5163 file_data, INSERT);
927aa2e7
JK
5164 *slot = file_data;
5165 }
5166}
5167
5168static void
5169dw2_expand_symtabs_matching
5170 (struct objfile *objfile,
5171 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5172 const lookup_name_info &lookup_name,
5173 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5174 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5175 enum search_domain kind)
5176{
ed2dc618
SM
5177 struct dwarf2_per_objfile *dwarf2_per_objfile
5178 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5179
5180 /* index_table is NULL if OBJF_READNOW. */
5181 if (!dwarf2_per_objfile->index_table)
5182 return;
5183
ed2dc618 5184 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5185
5186 mapped_index &index = *dwarf2_per_objfile->index_table;
5187
5188 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5189 symbol_matcher,
5190 kind, [&] (offset_type idx)
5191 {
ed2dc618 5192 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 5193 expansion_notify, kind);
3b00ef10 5194 return true;
927aa2e7
JK
5195 });
5196}
5197
5198/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5199 symtab. */
5200
5201static struct compunit_symtab *
5202recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5203 CORE_ADDR pc)
5204{
5205 int i;
5206
5207 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5208 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5209 return cust;
5210
5211 if (cust->includes == NULL)
5212 return NULL;
5213
5214 for (i = 0; cust->includes[i]; ++i)
5215 {
5216 struct compunit_symtab *s = cust->includes[i];
5217
5218 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5219 if (s != NULL)
5220 return s;
5221 }
5222
5223 return NULL;
5224}
5225
5226static struct compunit_symtab *
5227dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5228 struct bound_minimal_symbol msymbol,
5229 CORE_ADDR pc,
5230 struct obj_section *section,
5231 int warn_if_readin)
5232{
5233 struct dwarf2_per_cu_data *data;
5234 struct compunit_symtab *result;
5235
d320c2b5 5236 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
5237 return NULL;
5238
6a053cb1 5239 CORE_ADDR baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
d320c2b5
TT
5240 data = (struct dwarf2_per_cu_data *) addrmap_find
5241 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
5242 if (!data)
5243 return NULL;
5244
5245 if (warn_if_readin && data->v.quick->compunit_symtab)
5246 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5247 paddress (get_objfile_arch (objfile), pc));
5248
5249 result
58f0c718
TT
5250 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5251 false),
927aa2e7
JK
5252 pc);
5253 gdb_assert (result != NULL);
5254 return result;
5255}
5256
5257static void
5258dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5259 void *data, int need_fullname)
5260{
ed2dc618
SM
5261 struct dwarf2_per_objfile *dwarf2_per_objfile
5262 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5263
5264 if (!dwarf2_per_objfile->filenames_cache)
5265 {
5266 dwarf2_per_objfile->filenames_cache.emplace ();
5267
5268 htab_up visited (htab_create_alloc (10,
5269 htab_hash_pointer, htab_eq_pointer,
5270 NULL, xcalloc, xfree));
5271
5272 /* The rule is CUs specify all the files, including those used
5273 by any TU, so there's no need to scan TUs here. We can
5274 ignore file names coming from already-expanded CUs. */
5275
b76e467d 5276 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5277 {
927aa2e7
JK
5278 if (per_cu->v.quick->compunit_symtab)
5279 {
5280 void **slot = htab_find_slot (visited.get (),
5281 per_cu->v.quick->file_names,
5282 INSERT);
5283
5284 *slot = per_cu->v.quick->file_names;
5285 }
5286 }
5287
b76e467d 5288 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5289 {
927aa2e7
JK
5290 /* We only need to look at symtabs not already expanded. */
5291 if (per_cu->v.quick->compunit_symtab)
5292 continue;
5293
b76e467d 5294 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5295 if (file_data == NULL)
5296 continue;
5297
b76e467d 5298 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
5299 if (*slot)
5300 {
5301 /* Already visited. */
5302 continue;
5303 }
5304 *slot = file_data;
5305
5306 for (int j = 0; j < file_data->num_file_names; ++j)
5307 {
5308 const char *filename = file_data->file_names[j];
5309 dwarf2_per_objfile->filenames_cache->seen (filename);
5310 }
5311 }
5312 }
5313
5314 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5315 {
5316 gdb::unique_xmalloc_ptr<char> this_real_name;
5317
5318 if (need_fullname)
5319 this_real_name = gdb_realpath (filename);
5320 (*fun) (filename, this_real_name.get (), data);
5321 });
5322}
5323
5324static int
5325dw2_has_symbols (struct objfile *objfile)
5326{
5327 return 1;
5328}
5329
5330const struct quick_symbol_functions dwarf2_gdb_index_functions =
5331{
5332 dw2_has_symbols,
5333 dw2_find_last_source_symtab,
5334 dw2_forget_cached_source_info,
5335 dw2_map_symtabs_matching_filename,
5336 dw2_lookup_symbol,
5337 dw2_print_stats,
5338 dw2_dump,
927aa2e7
JK
5339 dw2_expand_symtabs_for_function,
5340 dw2_expand_all_symtabs,
5341 dw2_expand_symtabs_with_fullname,
5342 dw2_map_matching_symbols,
5343 dw2_expand_symtabs_matching,
5344 dw2_find_pc_sect_compunit_symtab,
5345 NULL,
5346 dw2_map_symbol_filenames
5347};
5348
5349/* DWARF-5 debug_names reader. */
5350
5351/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5352static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5353
5354/* A helper function that reads the .debug_names section in SECTION
5355 and fills in MAP. FILENAME is the name of the file containing the
5356 section; it is used for error reporting.
5357
5358 Returns true if all went well, false otherwise. */
5359
5360static bool
5361read_debug_names_from_section (struct objfile *objfile,
5362 const char *filename,
5363 struct dwarf2_section_info *section,
5364 mapped_debug_names &map)
5365{
5366 if (dwarf2_section_empty_p (section))
5367 return false;
5368
5369 /* Older elfutils strip versions could keep the section in the main
5370 executable while splitting it for the separate debug info file. */
5371 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5372 return false;
5373
5374 dwarf2_read_section (objfile, section);
5375
5376 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5377
5378 const gdb_byte *addr = section->buffer;
5379
5380 bfd *const abfd = get_section_bfd_owner (section);
5381
5382 unsigned int bytes_read;
5383 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5384 addr += bytes_read;
5385
5386 map.dwarf5_is_dwarf64 = bytes_read != 4;
5387 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5388 if (bytes_read + length != section->size)
5389 {
5390 /* There may be multiple per-CU indices. */
5391 warning (_("Section .debug_names in %s length %s does not match "
5392 "section length %s, ignoring .debug_names."),
5393 filename, plongest (bytes_read + length),
5394 pulongest (section->size));
5395 return false;
5396 }
5397
5398 /* The version number. */
5399 uint16_t version = read_2_bytes (abfd, addr);
5400 addr += 2;
5401 if (version != 5)
5402 {
5403 warning (_("Section .debug_names in %s has unsupported version %d, "
5404 "ignoring .debug_names."),
5405 filename, version);
5406 return false;
5407 }
5408
5409 /* Padding. */
5410 uint16_t padding = read_2_bytes (abfd, addr);
5411 addr += 2;
5412 if (padding != 0)
5413 {
5414 warning (_("Section .debug_names in %s has unsupported padding %d, "
5415 "ignoring .debug_names."),
5416 filename, padding);
5417 return false;
5418 }
5419
5420 /* comp_unit_count - The number of CUs in the CU list. */
5421 map.cu_count = read_4_bytes (abfd, addr);
5422 addr += 4;
5423
5424 /* local_type_unit_count - The number of TUs in the local TU
5425 list. */
5426 map.tu_count = read_4_bytes (abfd, addr);
5427 addr += 4;
5428
5429 /* foreign_type_unit_count - The number of TUs in the foreign TU
5430 list. */
5431 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5432 addr += 4;
5433 if (foreign_tu_count != 0)
5434 {
5435 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5436 "ignoring .debug_names."),
5437 filename, static_cast<unsigned long> (foreign_tu_count));
5438 return false;
5439 }
5440
5441 /* bucket_count - The number of hash buckets in the hash lookup
5442 table. */
5443 map.bucket_count = read_4_bytes (abfd, addr);
5444 addr += 4;
5445
5446 /* name_count - The number of unique names in the index. */
5447 map.name_count = read_4_bytes (abfd, addr);
5448 addr += 4;
5449
5450 /* abbrev_table_size - The size in bytes of the abbreviations
5451 table. */
5452 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5453 addr += 4;
5454
5455 /* augmentation_string_size - The size in bytes of the augmentation
5456 string. This value is rounded up to a multiple of 4. */
5457 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5458 addr += 4;
5459 map.augmentation_is_gdb = ((augmentation_string_size
5460 == sizeof (dwarf5_augmentation))
5461 && memcmp (addr, dwarf5_augmentation,
5462 sizeof (dwarf5_augmentation)) == 0);
5463 augmentation_string_size += (-augmentation_string_size) & 3;
5464 addr += augmentation_string_size;
5465
5466 /* List of CUs */
5467 map.cu_table_reordered = addr;
5468 addr += map.cu_count * map.offset_size;
5469
5470 /* List of Local TUs */
5471 map.tu_table_reordered = addr;
5472 addr += map.tu_count * map.offset_size;
5473
5474 /* Hash Lookup Table */
5475 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5476 addr += map.bucket_count * 4;
5477 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5478 addr += map.name_count * 4;
5479
5480 /* Name Table */
5481 map.name_table_string_offs_reordered = addr;
5482 addr += map.name_count * map.offset_size;
5483 map.name_table_entry_offs_reordered = addr;
5484 addr += map.name_count * map.offset_size;
5485
5486 const gdb_byte *abbrev_table_start = addr;
5487 for (;;)
5488 {
927aa2e7
JK
5489 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5490 addr += bytes_read;
5491 if (index_num == 0)
5492 break;
5493
5494 const auto insertpair
5495 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5496 if (!insertpair.second)
5497 {
5498 warning (_("Section .debug_names in %s has duplicate index %s, "
5499 "ignoring .debug_names."),
5500 filename, pulongest (index_num));
5501 return false;
5502 }
5503 mapped_debug_names::index_val &indexval = insertpair.first->second;
5504 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5505 addr += bytes_read;
5506
5507 for (;;)
5508 {
5509 mapped_debug_names::index_val::attr attr;
5510 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5511 addr += bytes_read;
5512 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5513 addr += bytes_read;
5514 if (attr.form == DW_FORM_implicit_const)
5515 {
5516 attr.implicit_const = read_signed_leb128 (abfd, addr,
5517 &bytes_read);
5518 addr += bytes_read;
5519 }
5520 if (attr.dw_idx == 0 && attr.form == 0)
5521 break;
5522 indexval.attr_vec.push_back (std::move (attr));
5523 }
5524 }
5525 if (addr != abbrev_table_start + abbrev_table_size)
5526 {
5527 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5528 "of size %s vs. written as %u, ignoring .debug_names."),
5529 filename, plongest (addr - abbrev_table_start),
5530 abbrev_table_size);
927aa2e7
JK
5531 return false;
5532 }
5533 map.entry_pool = addr;
5534
5535 return true;
5536}
5537
5538/* A helper for create_cus_from_debug_names that handles the MAP's CU
5539 list. */
5540
5541static void
ed2dc618 5542create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5543 const mapped_debug_names &map,
5544 dwarf2_section_info &section,
b76e467d 5545 bool is_dwz)
927aa2e7
JK
5546{
5547 sect_offset sect_off_prev;
5548 for (uint32_t i = 0; i <= map.cu_count; ++i)
5549 {
5550 sect_offset sect_off_next;
5551 if (i < map.cu_count)
5552 {
5553 sect_off_next
5554 = (sect_offset) (extract_unsigned_integer
5555 (map.cu_table_reordered + i * map.offset_size,
5556 map.offset_size,
5557 map.dwarf5_byte_order));
5558 }
5559 else
5560 sect_off_next = (sect_offset) section.size;
5561 if (i >= 1)
5562 {
5563 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5564 dwarf2_per_cu_data *per_cu
ed2dc618 5565 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5566 sect_off_prev, length);
b76e467d 5567 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5568 }
5569 sect_off_prev = sect_off_next;
5570 }
5571}
5572
5573/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5574 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5575
5576static void
ed2dc618 5577create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5578 const mapped_debug_names &map,
5579 const mapped_debug_names &dwz_map)
5580{
b76e467d
SM
5581 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5582 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5583
ed2dc618
SM
5584 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5585 dwarf2_per_objfile->info,
b76e467d 5586 false /* is_dwz */);
927aa2e7
JK
5587
5588 if (dwz_map.cu_count == 0)
5589 return;
5590
ed2dc618
SM
5591 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5592 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5593 true /* is_dwz */);
927aa2e7
JK
5594}
5595
5596/* Read .debug_names. If everything went ok, initialize the "quick"
5597 elements of all the CUs and return true. Otherwise, return false. */
5598
5599static bool
ed2dc618 5600dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5601{
22ca247e
TT
5602 std::unique_ptr<mapped_debug_names> map
5603 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5604 mapped_debug_names dwz_map (dwarf2_per_objfile);
5605 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5606
5607 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5608 &dwarf2_per_objfile->debug_names,
22ca247e 5609 *map))
927aa2e7
JK
5610 return false;
5611
5612 /* Don't use the index if it's empty. */
22ca247e 5613 if (map->name_count == 0)
927aa2e7
JK
5614 return false;
5615
5616 /* If there is a .dwz file, read it so we can get its CU list as
5617 well. */
ed2dc618 5618 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5619 if (dwz != NULL)
5620 {
5621 if (!read_debug_names_from_section (objfile,
00f93c44 5622 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5623 &dwz->debug_names, dwz_map))
5624 {
5625 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5626 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5627 return false;
5628 }
5629 }
5630
22ca247e 5631 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5632
22ca247e 5633 if (map->tu_count != 0)
927aa2e7
JK
5634 {
5635 /* We can only handle a single .debug_types when we have an
5636 index. */
fd5866f6 5637 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5638 return false;
5639
fd5866f6 5640 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5641
5642 create_signatured_type_table_from_debug_names
22ca247e 5643 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5644 }
5645
ed2dc618
SM
5646 create_addrmap_from_aranges (dwarf2_per_objfile,
5647 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5648
22ca247e 5649 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5650 dwarf2_per_objfile->using_index = 1;
5651 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5652 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5653
5654 return true;
5655}
5656
927aa2e7
JK
5657/* Type used to manage iterating over all CUs looking for a symbol for
5658 .debug_names. */
5659
5660class dw2_debug_names_iterator
5661{
5662public:
927aa2e7 5663 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5664 gdb::optional<block_enum> block_index,
5665 domain_enum domain,
927aa2e7 5666 const char *name)
2b79f376 5667 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5668 m_addr (find_vec_in_debug_names (map, name))
5669 {}
5670
5671 dw2_debug_names_iterator (const mapped_debug_names &map,
5672 search_domain search, uint32_t namei)
5673 : m_map (map),
5674 m_search (search),
5675 m_addr (find_vec_in_debug_names (map, namei))
5676 {}
5677
3b00ef10
TT
5678 dw2_debug_names_iterator (const mapped_debug_names &map,
5679 block_enum block_index, domain_enum domain,
5680 uint32_t namei)
5681 : m_map (map), m_block_index (block_index), m_domain (domain),
5682 m_addr (find_vec_in_debug_names (map, namei))
5683 {}
5684
927aa2e7
JK
5685 /* Return the next matching CU or NULL if there are no more. */
5686 dwarf2_per_cu_data *next ();
5687
5688private:
5689 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5690 const char *name);
5691 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5692 uint32_t namei);
5693
5694 /* The internalized form of .debug_names. */
5695 const mapped_debug_names &m_map;
5696
2b79f376
SM
5697 /* If set, only look for symbols that match that block. Valid values are
5698 GLOBAL_BLOCK and STATIC_BLOCK. */
5699 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5700
5701 /* The kind of symbol we're looking for. */
5702 const domain_enum m_domain = UNDEF_DOMAIN;
5703 const search_domain m_search = ALL_DOMAIN;
5704
5705 /* The list of CUs from the index entry of the symbol, or NULL if
5706 not found. */
5707 const gdb_byte *m_addr;
5708};
5709
5710const char *
5711mapped_debug_names::namei_to_name (uint32_t namei) const
5712{
5713 const ULONGEST namei_string_offs
5714 = extract_unsigned_integer ((name_table_string_offs_reordered
5715 + namei * offset_size),
5716 offset_size,
5717 dwarf5_byte_order);
5718 return read_indirect_string_at_offset
ed2dc618 5719 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5720}
5721
5722/* Find a slot in .debug_names for the object named NAME. If NAME is
5723 found, return pointer to its pool data. If NAME cannot be found,
5724 return NULL. */
5725
5726const gdb_byte *
5727dw2_debug_names_iterator::find_vec_in_debug_names
5728 (const mapped_debug_names &map, const char *name)
5729{
5730 int (*cmp) (const char *, const char *);
5731
54ee4252 5732 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5733 if (current_language->la_language == language_cplus
5734 || current_language->la_language == language_fortran
5735 || current_language->la_language == language_d)
5736 {
5737 /* NAME is already canonical. Drop any qualifiers as
5738 .debug_names does not contain any. */
5739
5740 if (strchr (name, '(') != NULL)
5741 {
54ee4252 5742 without_params = cp_remove_params (name);
927aa2e7 5743 if (without_params != NULL)
54ee4252 5744 name = without_params.get ();
927aa2e7
JK
5745 }
5746 }
5747
5748 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5749
5750 const uint32_t full_hash = dwarf5_djb_hash (name);
5751 uint32_t namei
5752 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5753 (map.bucket_table_reordered
5754 + (full_hash % map.bucket_count)), 4,
5755 map.dwarf5_byte_order);
5756 if (namei == 0)
5757 return NULL;
5758 --namei;
5759 if (namei >= map.name_count)
5760 {
b98664d3 5761 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5762 "[in module %s]"),
5763 namei, map.name_count,
ed2dc618 5764 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5765 return NULL;
5766 }
5767
5768 for (;;)
5769 {
5770 const uint32_t namei_full_hash
5771 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5772 (map.hash_table_reordered + namei), 4,
5773 map.dwarf5_byte_order);
5774 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5775 return NULL;
5776
5777 if (full_hash == namei_full_hash)
5778 {
5779 const char *const namei_string = map.namei_to_name (namei);
5780
5781#if 0 /* An expensive sanity check. */
5782 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5783 {
b98664d3 5784 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5785 "[in module %s]"),
5786 namei, objfile_name (dwarf2_per_objfile->objfile));
5787 return NULL;
5788 }
5789#endif
5790
5791 if (cmp (namei_string, name) == 0)
5792 {
5793 const ULONGEST namei_entry_offs
5794 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5795 + namei * map.offset_size),
5796 map.offset_size, map.dwarf5_byte_order);
5797 return map.entry_pool + namei_entry_offs;
5798 }
5799 }
5800
5801 ++namei;
5802 if (namei >= map.name_count)
5803 return NULL;
5804 }
5805}
5806
5807const gdb_byte *
5808dw2_debug_names_iterator::find_vec_in_debug_names
5809 (const mapped_debug_names &map, uint32_t namei)
5810{
5811 if (namei >= map.name_count)
5812 {
b98664d3 5813 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5814 "[in module %s]"),
5815 namei, map.name_count,
ed2dc618 5816 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5817 return NULL;
5818 }
5819
5820 const ULONGEST namei_entry_offs
5821 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5822 + namei * map.offset_size),
5823 map.offset_size, map.dwarf5_byte_order);
5824 return map.entry_pool + namei_entry_offs;
5825}
5826
5827/* See dw2_debug_names_iterator. */
5828
5829dwarf2_per_cu_data *
5830dw2_debug_names_iterator::next ()
5831{
5832 if (m_addr == NULL)
5833 return NULL;
5834
ed2dc618
SM
5835 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5836 struct objfile *objfile = dwarf2_per_objfile->objfile;
5837 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5838
5839 again:
5840
5841 unsigned int bytes_read;
5842 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5843 m_addr += bytes_read;
5844 if (abbrev == 0)
5845 return NULL;
5846
5847 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5848 if (indexval_it == m_map.abbrev_map.cend ())
5849 {
b98664d3 5850 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5851 "[in module %s]"),
ed2dc618 5852 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5853 return NULL;
5854 }
5855 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5856 enum class symbol_linkage {
5857 unknown,
5858 static_,
5859 extern_,
23c13d42 5860 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5861 dwarf2_per_cu_data *per_cu = NULL;
5862 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5863 {
5864 ULONGEST ull;
5865 switch (attr.form)
5866 {
5867 case DW_FORM_implicit_const:
5868 ull = attr.implicit_const;
5869 break;
5870 case DW_FORM_flag_present:
5871 ull = 1;
5872 break;
5873 case DW_FORM_udata:
5874 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5875 m_addr += bytes_read;
5876 break;
5877 default:
b98664d3 5878 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5879 dwarf_form_name (attr.form),
ed2dc618 5880 objfile_name (objfile));
927aa2e7
JK
5881 return NULL;
5882 }
5883 switch (attr.dw_idx)
5884 {
5885 case DW_IDX_compile_unit:
5886 /* Don't crash on bad data. */
b76e467d 5887 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5888 {
b98664d3 5889 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5890 " [in module %s]"),
5891 pulongest (ull),
5892 objfile_name (dwarf2_per_objfile->objfile));
5893 continue;
5894 }
ff4c9fec 5895 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5896 break;
8af5c486
JK
5897 case DW_IDX_type_unit:
5898 /* Don't crash on bad data. */
b2bdb8cf 5899 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5900 {
b98664d3 5901 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5902 " [in module %s]"),
5903 pulongest (ull),
5904 objfile_name (dwarf2_per_objfile->objfile));
5905 continue;
5906 }
ff4c9fec 5907 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5908 break;
927aa2e7
JK
5909 case DW_IDX_GNU_internal:
5910 if (!m_map.augmentation_is_gdb)
5911 break;
23c13d42 5912 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5913 break;
5914 case DW_IDX_GNU_external:
5915 if (!m_map.augmentation_is_gdb)
5916 break;
23c13d42 5917 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5918 break;
5919 }
5920 }
5921
5922 /* Skip if already read in. */
5923 if (per_cu->v.quick->compunit_symtab)
5924 goto again;
5925
5926 /* Check static vs global. */
23c13d42 5927 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5928 {
2b79f376 5929 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5930 const bool symbol_is_static =
5931 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5932 if (want_static != symbol_is_static)
2b79f376 5933 goto again;
927aa2e7
JK
5934 }
5935
5936 /* Match dw2_symtab_iter_next, symbol_kind
5937 and debug_names::psymbol_tag. */
5938 switch (m_domain)
5939 {
5940 case VAR_DOMAIN:
5941 switch (indexval.dwarf_tag)
5942 {
5943 case DW_TAG_variable:
5944 case DW_TAG_subprogram:
5945 /* Some types are also in VAR_DOMAIN. */
5946 case DW_TAG_typedef:
5947 case DW_TAG_structure_type:
5948 break;
5949 default:
5950 goto again;
5951 }
5952 break;
5953 case STRUCT_DOMAIN:
5954 switch (indexval.dwarf_tag)
5955 {
5956 case DW_TAG_typedef:
5957 case DW_TAG_structure_type:
5958 break;
5959 default:
5960 goto again;
5961 }
5962 break;
5963 case LABEL_DOMAIN:
5964 switch (indexval.dwarf_tag)
5965 {
5966 case 0:
5967 case DW_TAG_variable:
5968 break;
5969 default:
5970 goto again;
5971 }
5972 break;
59c35742
AB
5973 case MODULE_DOMAIN:
5974 switch (indexval.dwarf_tag)
5975 {
5976 case DW_TAG_module:
5977 break;
5978 default:
5979 goto again;
5980 }
5981 break;
927aa2e7
JK
5982 default:
5983 break;
5984 }
5985
5986 /* Match dw2_expand_symtabs_matching, symbol_kind and
5987 debug_names::psymbol_tag. */
5988 switch (m_search)
4b514bc8 5989 {
927aa2e7
JK
5990 case VARIABLES_DOMAIN:
5991 switch (indexval.dwarf_tag)
4b514bc8 5992 {
927aa2e7
JK
5993 case DW_TAG_variable:
5994 break;
5995 default:
5996 goto again;
4b514bc8 5997 }
927aa2e7
JK
5998 break;
5999 case FUNCTIONS_DOMAIN:
6000 switch (indexval.dwarf_tag)
4b514bc8 6001 {
927aa2e7
JK
6002 case DW_TAG_subprogram:
6003 break;
6004 default:
6005 goto again;
4b514bc8 6006 }
927aa2e7
JK
6007 break;
6008 case TYPES_DOMAIN:
6009 switch (indexval.dwarf_tag)
6010 {
6011 case DW_TAG_typedef:
6012 case DW_TAG_structure_type:
6013 break;
6014 default:
6015 goto again;
6016 }
6017 break;
59c35742
AB
6018 case MODULES_DOMAIN:
6019 switch (indexval.dwarf_tag)
6020 {
6021 case DW_TAG_module:
6022 break;
6023 default:
6024 goto again;
6025 }
927aa2e7
JK
6026 default:
6027 break;
4b514bc8 6028 }
927aa2e7
JK
6029
6030 return per_cu;
4b514bc8 6031}
61920122 6032
927aa2e7 6033static struct compunit_symtab *
c7f839cb 6034dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 6035 const char *name, domain_enum domain)
4b514bc8 6036{
ed2dc618
SM
6037 struct dwarf2_per_objfile *dwarf2_per_objfile
6038 = get_dwarf2_per_objfile (objfile);
61920122 6039
927aa2e7
JK
6040 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6041 if (!mapp)
61920122 6042 {
927aa2e7
JK
6043 /* index is NULL if OBJF_READNOW. */
6044 return NULL;
6045 }
6046 const auto &map = *mapp;
9291a0cd 6047
2b79f376 6048 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 6049
927aa2e7
JK
6050 struct compunit_symtab *stab_best = NULL;
6051 struct dwarf2_per_cu_data *per_cu;
6052 while ((per_cu = iter.next ()) != NULL)
6053 {
6054 struct symbol *sym, *with_opaque = NULL;
58f0c718 6055 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 6056 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 6057 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6058
927aa2e7
JK
6059 sym = block_find_symbol (block, name, domain,
6060 block_find_non_opaque_type_preferred,
6061 &with_opaque);
9703b513 6062
927aa2e7
JK
6063 /* Some caution must be observed with overloaded functions and
6064 methods, since the index will not contain any overload
6065 information (but NAME might contain it). */
a3ec0bb1 6066
927aa2e7 6067 if (sym != NULL
987012b8 6068 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
6069 return stab;
6070 if (with_opaque != NULL
987012b8 6071 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 6072 stab_best = stab;
9703b513 6073
927aa2e7 6074 /* Keep looking through other CUs. */
9703b513
TT
6075 }
6076
927aa2e7 6077 return stab_best;
9703b513
TT
6078}
6079
927aa2e7
JK
6080/* This dumps minimal information about .debug_names. It is called
6081 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6082 uses this to verify that .debug_names has been loaded. */
9291a0cd 6083
927aa2e7
JK
6084static void
6085dw2_debug_names_dump (struct objfile *objfile)
6086{
ed2dc618
SM
6087 struct dwarf2_per_objfile *dwarf2_per_objfile
6088 = get_dwarf2_per_objfile (objfile);
6089
927aa2e7
JK
6090 gdb_assert (dwarf2_per_objfile->using_index);
6091 printf_filtered (".debug_names:");
6092 if (dwarf2_per_objfile->debug_names_table)
6093 printf_filtered (" exists\n");
6094 else
6095 printf_filtered (" faked for \"readnow\"\n");
6096 printf_filtered ("\n");
9291a0cd
TT
6097}
6098
9291a0cd 6099static void
927aa2e7
JK
6100dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6101 const char *func_name)
9291a0cd 6102{
ed2dc618
SM
6103 struct dwarf2_per_objfile *dwarf2_per_objfile
6104 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6105
927aa2e7
JK
6106 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6107 if (dwarf2_per_objfile->debug_names_table)
24c79950 6108 {
927aa2e7 6109 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6110
2b79f376 6111 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 6112
927aa2e7
JK
6113 struct dwarf2_per_cu_data *per_cu;
6114 while ((per_cu = iter.next ()) != NULL)
58f0c718 6115 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
6116 }
6117}
24c79950 6118
3b00ef10
TT
6119static void
6120dw2_debug_names_map_matching_symbols
6121 (struct objfile *objfile,
6122 const lookup_name_info &name, domain_enum domain,
6123 int global,
6124 gdb::function_view<symbol_found_callback_ftype> callback,
6125 symbol_compare_ftype *ordered_compare)
6126{
6127 struct dwarf2_per_objfile *dwarf2_per_objfile
6128 = get_dwarf2_per_objfile (objfile);
6129
6130 /* debug_names_table is NULL if OBJF_READNOW. */
6131 if (!dwarf2_per_objfile->debug_names_table)
6132 return;
6133
6134 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6135 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6136
6137 const char *match_name = name.ada ().lookup_name ().c_str ();
6138 auto matcher = [&] (const char *symname)
6139 {
6140 if (ordered_compare == nullptr)
6141 return true;
6142 return ordered_compare (symname, match_name) == 0;
6143 };
6144
6145 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6146 [&] (offset_type namei)
6147 {
6148 /* The name was matched, now expand corresponding CUs that were
6149 marked. */
6150 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6151
6152 struct dwarf2_per_cu_data *per_cu;
6153 while ((per_cu = iter.next ()) != NULL)
6154 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6155 return true;
6156 });
6157
6158 /* It's a shame we couldn't do this inside the
6159 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6160 that have already been expanded. Instead, this loop matches what
6161 the psymtab code does. */
6162 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6163 {
6164 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6165 if (cust != nullptr)
6166 {
6167 const struct block *block
6168 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6169 if (!iterate_over_symbols_terminated (block, name,
6170 domain, callback))
6171 break;
6172 }
6173 }
6174}
6175
927aa2e7
JK
6176static void
6177dw2_debug_names_expand_symtabs_matching
6178 (struct objfile *objfile,
6179 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6180 const lookup_name_info &lookup_name,
6181 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6182 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6183 enum search_domain kind)
6184{
ed2dc618
SM
6185 struct dwarf2_per_objfile *dwarf2_per_objfile
6186 = get_dwarf2_per_objfile (objfile);
9291a0cd 6187
927aa2e7
JK
6188 /* debug_names_table is NULL if OBJF_READNOW. */
6189 if (!dwarf2_per_objfile->debug_names_table)
6190 return;
9291a0cd 6191
ed2dc618 6192 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6193
44ed8f3e 6194 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6195
44ed8f3e
PA
6196 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6197 symbol_matcher,
6198 kind, [&] (offset_type namei)
927aa2e7 6199 {
927aa2e7
JK
6200 /* The name was matched, now expand corresponding CUs that were
6201 marked. */
6202 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6203
927aa2e7
JK
6204 struct dwarf2_per_cu_data *per_cu;
6205 while ((per_cu = iter.next ()) != NULL)
6206 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6207 expansion_notify);
3b00ef10 6208 return true;
44ed8f3e 6209 });
9291a0cd
TT
6210}
6211
927aa2e7 6212const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6213{
6214 dw2_has_symbols,
6215 dw2_find_last_source_symtab,
6216 dw2_forget_cached_source_info,
f8eba3c6 6217 dw2_map_symtabs_matching_filename,
927aa2e7 6218 dw2_debug_names_lookup_symbol,
9291a0cd 6219 dw2_print_stats,
927aa2e7 6220 dw2_debug_names_dump,
927aa2e7 6221 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6222 dw2_expand_all_symtabs,
652a8996 6223 dw2_expand_symtabs_with_fullname,
3b00ef10 6224 dw2_debug_names_map_matching_symbols,
927aa2e7 6225 dw2_debug_names_expand_symtabs_matching,
43f3e411 6226 dw2_find_pc_sect_compunit_symtab,
71a3c369 6227 NULL,
9291a0cd
TT
6228 dw2_map_symbol_filenames
6229};
6230
4485a1c1
SM
6231/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6232 to either a dwarf2_per_objfile or dwz_file object. */
6233
6234template <typename T>
6235static gdb::array_view<const gdb_byte>
6236get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6237{
6238 dwarf2_section_info *section = &section_owner->gdb_index;
6239
6240 if (dwarf2_section_empty_p (section))
6241 return {};
6242
6243 /* Older elfutils strip versions could keep the section in the main
6244 executable while splitting it for the separate debug info file. */
6245 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6246 return {};
6247
6248 dwarf2_read_section (obj, section);
6249
8bebfcda
PA
6250 /* dwarf2_section_info::size is a bfd_size_type, while
6251 gdb::array_view works with size_t. On 32-bit hosts, with
6252 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6253 is 32-bit. So we need an explicit narrowing conversion here.
6254 This is fine, because it's impossible to allocate or mmap an
6255 array/buffer larger than what size_t can represent. */
6256 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
6257}
6258
87d6a7aa
SM
6259/* Lookup the index cache for the contents of the index associated to
6260 DWARF2_OBJ. */
6261
6262static gdb::array_view<const gdb_byte>
6263get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6264{
6265 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6266 if (build_id == nullptr)
6267 return {};
6268
6269 return global_index_cache.lookup_gdb_index (build_id,
6270 &dwarf2_obj->index_cache_res);
6271}
6272
6273/* Same as the above, but for DWZ. */
6274
6275static gdb::array_view<const gdb_byte>
6276get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6277{
6278 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6279 if (build_id == nullptr)
6280 return {};
6281
6282 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6283}
6284
3c0aa29a 6285/* See symfile.h. */
9291a0cd 6286
3c0aa29a
PA
6287bool
6288dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6289{
ed2dc618
SM
6290 struct dwarf2_per_objfile *dwarf2_per_objfile
6291 = get_dwarf2_per_objfile (objfile);
6292
9291a0cd
TT
6293 /* If we're about to read full symbols, don't bother with the
6294 indices. In this case we also don't care if some other debug
6295 format is making psymtabs, because they are all about to be
6296 expanded anyway. */
6297 if ((objfile->flags & OBJF_READNOW))
6298 {
9291a0cd 6299 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6300 create_all_comp_units (dwarf2_per_objfile);
6301 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
6302 dwarf2_per_objfile->quick_file_names_table
6303 = create_quick_file_names_table
6304 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 6305
b76e467d 6306 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 6307 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 6308 {
ff4c9fec 6309 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 6310
e254ef6a
DE
6311 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6312 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6313 }
6314
6315 /* Return 1 so that gdb sees the "quick" functions. However,
6316 these functions will be no-ops because we will have expanded
6317 all symtabs. */
3c0aa29a
PA
6318 *index_kind = dw_index_kind::GDB_INDEX;
6319 return true;
9291a0cd
TT
6320 }
6321
ed2dc618 6322 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6323 {
6324 *index_kind = dw_index_kind::DEBUG_NAMES;
6325 return true;
6326 }
927aa2e7 6327
4485a1c1
SM
6328 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6329 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6330 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6331 {
6332 *index_kind = dw_index_kind::GDB_INDEX;
6333 return true;
6334 }
9291a0cd 6335
87d6a7aa
SM
6336 /* ... otherwise, try to find the index in the index cache. */
6337 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6338 get_gdb_index_contents_from_cache,
6339 get_gdb_index_contents_from_cache_dwz))
6340 {
6341 global_index_cache.hit ();
6342 *index_kind = dw_index_kind::GDB_INDEX;
6343 return true;
6344 }
6345
6346 global_index_cache.miss ();
3c0aa29a 6347 return false;
9291a0cd
TT
6348}
6349
6350\f
6351
dce234bc
PP
6352/* Build a partial symbol table. */
6353
6354void
f29dff0a 6355dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6356{
ed2dc618
SM
6357 struct dwarf2_per_objfile *dwarf2_per_objfile
6358 = get_dwarf2_per_objfile (objfile);
c9bf0622 6359
6eee24ce 6360 init_psymbol_list (objfile, 1024);
c906108c 6361
a70b8144 6362 try
c9bf0622
TT
6363 {
6364 /* This isn't really ideal: all the data we allocate on the
6365 objfile's obstack is still uselessly kept around. However,
6366 freeing it seems unsafe. */
906768f9 6367 psymtab_discarder psymtabs (objfile);
ed2dc618 6368 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6369 psymtabs.keep ();
87d6a7aa
SM
6370
6371 /* (maybe) store an index in the cache. */
6372 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 6373 }
230d2906 6374 catch (const gdb_exception_error &except)
492d29ea
PA
6375 {
6376 exception_print (gdb_stderr, except);
6377 }
c906108c 6378}
c906108c 6379
1ce1cefd
DE
6380/* Return the total length of the CU described by HEADER. */
6381
6382static unsigned int
6383get_cu_length (const struct comp_unit_head *header)
6384{
6385 return header->initial_length_size + header->length;
6386}
6387
9c541725 6388/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6389
9c541725
PA
6390static inline bool
6391offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6392{
9c541725
PA
6393 sect_offset bottom = cu_header->sect_off;
6394 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6395
9c541725 6396 return sect_off >= bottom && sect_off < top;
45452591
DE
6397}
6398
3b80fe9b
DE
6399/* Find the base address of the compilation unit for range lists and
6400 location lists. It will normally be specified by DW_AT_low_pc.
6401 In DWARF-3 draft 4, the base address could be overridden by
6402 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6403 compilation units with discontinuous ranges. */
6404
6405static void
6406dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6407{
6408 struct attribute *attr;
6409
6410 cu->base_known = 0;
6411 cu->base_address = 0;
6412
6413 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6414 if (attr != nullptr)
3b80fe9b 6415 {
31aa7e4e 6416 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6417 cu->base_known = 1;
6418 }
6419 else
6420 {
6421 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6422 if (attr != nullptr)
3b80fe9b 6423 {
31aa7e4e 6424 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6425 cu->base_known = 1;
6426 }
6427 }
6428}
6429
93311388 6430/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6431 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6432 NOTE: This leaves members offset, first_die_offset to be filled in
6433 by the caller. */
107d2387 6434
d521ce57 6435static const gdb_byte *
107d2387 6436read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6437 const gdb_byte *info_ptr,
6438 struct dwarf2_section_info *section,
6439 rcuh_kind section_kind)
107d2387
AC
6440{
6441 int signed_addr;
891d2f0b 6442 unsigned int bytes_read;
43988095
JK
6443 const char *filename = get_section_file_name (section);
6444 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6445
6446 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6447 cu_header->initial_length_size = bytes_read;
6448 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6449 info_ptr += bytes_read;
107d2387 6450 cu_header->version = read_2_bytes (abfd, info_ptr);
1ea5da02
TV
6451 if (cu_header->version < 2 || cu_header->version > 5)
6452 error (_("Dwarf Error: wrong version in compilation unit header "
6453 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6454 cu_header->version, filename);
107d2387 6455 info_ptr += 2;
43988095
JK
6456 if (cu_header->version < 5)
6457 switch (section_kind)
6458 {
6459 case rcuh_kind::COMPILE:
6460 cu_header->unit_type = DW_UT_compile;
6461 break;
6462 case rcuh_kind::TYPE:
6463 cu_header->unit_type = DW_UT_type;
6464 break;
6465 default:
6466 internal_error (__FILE__, __LINE__,
6467 _("read_comp_unit_head: invalid section_kind"));
6468 }
6469 else
6470 {
6471 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6472 (read_1_byte (abfd, info_ptr));
6473 info_ptr += 1;
6474 switch (cu_header->unit_type)
6475 {
6476 case DW_UT_compile:
a084a2a6
AT
6477 case DW_UT_partial:
6478 case DW_UT_skeleton:
6479 case DW_UT_split_compile:
43988095
JK
6480 if (section_kind != rcuh_kind::COMPILE)
6481 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6482 "(is %s, should be %s) [in module %s]"),
6483 dwarf_unit_type_name (cu_header->unit_type),
6484 dwarf_unit_type_name (DW_UT_type), filename);
43988095
JK
6485 break;
6486 case DW_UT_type:
a084a2a6 6487 case DW_UT_split_type:
43988095
JK
6488 section_kind = rcuh_kind::TYPE;
6489 break;
6490 default:
6491 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6492 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6493 "[in module %s]"), cu_header->unit_type,
6494 dwarf_unit_type_name (DW_UT_compile),
6495 dwarf_unit_type_name (DW_UT_skeleton),
6496 dwarf_unit_type_name (DW_UT_split_compile),
6497 dwarf_unit_type_name (DW_UT_type),
6498 dwarf_unit_type_name (DW_UT_split_type), filename);
43988095
JK
6499 }
6500
6501 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6502 info_ptr += 1;
6503 }
9c541725
PA
6504 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6505 cu_header,
6506 &bytes_read);
613e1657 6507 info_ptr += bytes_read;
43988095
JK
6508 if (cu_header->version < 5)
6509 {
6510 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6511 info_ptr += 1;
6512 }
107d2387
AC
6513 signed_addr = bfd_get_sign_extend_vma (abfd);
6514 if (signed_addr < 0)
8e65ff28 6515 internal_error (__FILE__, __LINE__,
e2e0b3e5 6516 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6517 cu_header->signed_addr_p = signed_addr;
c764a876 6518
a084a2a6
AT
6519 bool header_has_signature = section_kind == rcuh_kind::TYPE
6520 || cu_header->unit_type == DW_UT_skeleton
6521 || cu_header->unit_type == DW_UT_split_compile;
43988095 6522
a084a2a6
AT
6523 if (header_has_signature)
6524 {
43988095
JK
6525 cu_header->signature = read_8_bytes (abfd, info_ptr);
6526 info_ptr += 8;
a084a2a6 6527 }
43988095 6528
a084a2a6
AT
6529 if (section_kind == rcuh_kind::TYPE)
6530 {
6531 LONGEST type_offset;
43988095
JK
6532 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6533 info_ptr += bytes_read;
9c541725
PA
6534 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6535 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6536 error (_("Dwarf Error: Too big type_offset in compilation unit "
6537 "header (is %s) [in module %s]"), plongest (type_offset),
6538 filename);
6539 }
6540
107d2387
AC
6541 return info_ptr;
6542}
6543
36586728
TT
6544/* Helper function that returns the proper abbrev section for
6545 THIS_CU. */
6546
6547static struct dwarf2_section_info *
6548get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6549{
6550 struct dwarf2_section_info *abbrev;
ed2dc618 6551 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6552
6553 if (this_cu->is_dwz)
ed2dc618 6554 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6555 else
6556 abbrev = &dwarf2_per_objfile->abbrev;
6557
6558 return abbrev;
6559}
6560
9ff913ba
DE
6561/* Subroutine of read_and_check_comp_unit_head and
6562 read_and_check_type_unit_head to simplify them.
6563 Perform various error checking on the header. */
6564
6565static void
ed2dc618
SM
6566error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6567 struct comp_unit_head *header,
4bdcc0c1
DE
6568 struct dwarf2_section_info *section,
6569 struct dwarf2_section_info *abbrev_section)
9ff913ba 6570{
a32a8923 6571 const char *filename = get_section_file_name (section);
9ff913ba 6572
9c541725 6573 if (to_underlying (header->abbrev_sect_off)
36586728 6574 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6575 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6576 "(offset %s + 6) [in module %s]"),
6577 sect_offset_str (header->abbrev_sect_off),
6578 sect_offset_str (header->sect_off),
9ff913ba
DE
6579 filename);
6580
9c541725 6581 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6582 avoid potential 32-bit overflow. */
9c541725 6583 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6584 > section->size)
9c541725 6585 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6586 "(offset %s + 0) [in module %s]"),
6587 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6588 filename);
6589}
6590
6591/* Read in a CU/TU header and perform some basic error checking.
6592 The contents of the header are stored in HEADER.
6593 The result is a pointer to the start of the first DIE. */
adabb602 6594
d521ce57 6595static const gdb_byte *
ed2dc618
SM
6596read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6597 struct comp_unit_head *header,
9ff913ba 6598 struct dwarf2_section_info *section,
4bdcc0c1 6599 struct dwarf2_section_info *abbrev_section,
d521ce57 6600 const gdb_byte *info_ptr,
43988095 6601 rcuh_kind section_kind)
72bf9492 6602{
d521ce57 6603 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6604
9c541725 6605 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6606
43988095 6607 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6608
9c541725 6609 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6610
ed2dc618
SM
6611 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6612 abbrev_section);
9ff913ba
DE
6613
6614 return info_ptr;
348e048f
DE
6615}
6616
f4dc4d17
DE
6617/* Fetch the abbreviation table offset from a comp or type unit header. */
6618
6619static sect_offset
ed2dc618
SM
6620read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6621 struct dwarf2_section_info *section,
9c541725 6622 sect_offset sect_off)
f4dc4d17 6623{
a32a8923 6624 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6625 const gdb_byte *info_ptr;
ac298888 6626 unsigned int initial_length_size, offset_size;
43988095 6627 uint16_t version;
f4dc4d17
DE
6628
6629 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6630 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6631 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6632 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6633 info_ptr += initial_length_size;
6634
6635 version = read_2_bytes (abfd, info_ptr);
6636 info_ptr += 2;
6637 if (version >= 5)
6638 {
6639 /* Skip unit type and address size. */
6640 info_ptr += 2;
6641 }
6642
9c541725 6643 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6644}
6645
aaa75496
JB
6646/* Allocate a new partial symtab for file named NAME and mark this new
6647 partial symtab as being an include of PST. */
6648
6649static void
d521ce57 6650dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6651 struct objfile *objfile)
6652{
6653 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6654
fbd9ab74
JK
6655 if (!IS_ABSOLUTE_PATH (subpst->filename))
6656 {
6657 /* It shares objfile->objfile_obstack. */
6658 subpst->dirname = pst->dirname;
6659 }
6660
a9342b62 6661 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6662 subpst->dependencies[0] = pst;
6663 subpst->number_of_dependencies = 1;
6664
aaa75496 6665 subpst->read_symtab = pst->read_symtab;
aaa75496
JB
6666
6667 /* No private part is necessary for include psymtabs. This property
6668 can be used to differentiate between such include psymtabs and
10b3939b 6669 the regular ones. */
58a9656e 6670 subpst->read_symtab_private = NULL;
aaa75496
JB
6671}
6672
6673/* Read the Line Number Program data and extract the list of files
6674 included by the source file represented by PST. Build an include
d85a05f0 6675 partial symtab for each of these included files. */
aaa75496
JB
6676
6677static void
6678dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
6679 struct die_info *die,
6680 struct partial_symtab *pst)
aaa75496 6681{
fff8551c 6682 line_header_up lh;
d85a05f0 6683 struct attribute *attr;
aaa75496 6684
d85a05f0 6685 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6686 if (attr != nullptr)
9c541725 6687 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6688 if (lh == NULL)
6689 return; /* No linetable, so no includes. */
6690
79748972
TT
6691 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6692 that we pass in the raw text_low here; that is ok because we're
6693 only decoding the line table to make include partial symtabs, and
6694 so the addresses aren't really used. */
4ae976d1 6695 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6696 pst->raw_text_low (), 1);
aaa75496
JB
6697}
6698
348e048f 6699static hashval_t
52dc124a 6700hash_signatured_type (const void *item)
348e048f 6701{
9a3c8263
SM
6702 const struct signatured_type *sig_type
6703 = (const struct signatured_type *) item;
9a619af0 6704
348e048f 6705 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6706 return sig_type->signature;
348e048f
DE
6707}
6708
6709static int
52dc124a 6710eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6711{
9a3c8263
SM
6712 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6713 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6714
348e048f
DE
6715 return lhs->signature == rhs->signature;
6716}
6717
1fd400ff
TT
6718/* Allocate a hash table for signatured types. */
6719
6720static htab_t
673bfd45 6721allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6722{
6723 return htab_create_alloc_ex (41,
52dc124a
DE
6724 hash_signatured_type,
6725 eq_signatured_type,
1fd400ff
TT
6726 NULL,
6727 &objfile->objfile_obstack,
6728 hashtab_obstack_allocate,
6729 dummy_obstack_deallocate);
6730}
6731
d467dd73 6732/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6733
6734static int
d467dd73 6735add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6736{
9a3c8263 6737 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6738 std::vector<signatured_type *> *all_type_units
6739 = (std::vector<signatured_type *> *) datum;
1fd400ff 6740
b2bdb8cf 6741 all_type_units->push_back (sigt);
1fd400ff
TT
6742
6743 return 1;
6744}
6745
78d4d2c5 6746/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6747 and fill them into TYPES_HTAB. It will process only type units,
6748 therefore DW_UT_type. */
c88ee1f0 6749
78d4d2c5 6750static void
ed2dc618
SM
6751create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6752 struct dwo_file *dwo_file,
43988095
JK
6753 dwarf2_section_info *section, htab_t &types_htab,
6754 rcuh_kind section_kind)
348e048f 6755{
3019eac3 6756 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6757 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6758 bfd *abfd;
6759 const gdb_byte *info_ptr, *end_ptr;
348e048f 6760
4bdcc0c1
DE
6761 abbrev_section = (dwo_file != NULL
6762 ? &dwo_file->sections.abbrev
6763 : &dwarf2_per_objfile->abbrev);
6764
b4f54984 6765 if (dwarf_read_debug)
43988095
JK
6766 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6767 get_section_name (section),
a32a8923 6768 get_section_file_name (abbrev_section));
09406207 6769
78d4d2c5
JK
6770 dwarf2_read_section (objfile, section);
6771 info_ptr = section->buffer;
348e048f 6772
78d4d2c5
JK
6773 if (info_ptr == NULL)
6774 return;
348e048f 6775
78d4d2c5
JK
6776 /* We can't set abfd until now because the section may be empty or
6777 not present, in which case the bfd is unknown. */
6778 abfd = get_section_bfd_owner (section);
348e048f 6779
78d4d2c5
JK
6780 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6781 because we don't need to read any dies: the signature is in the
6782 header. */
3019eac3 6783
78d4d2c5
JK
6784 end_ptr = info_ptr + section->size;
6785 while (info_ptr < end_ptr)
6786 {
78d4d2c5
JK
6787 struct signatured_type *sig_type;
6788 struct dwo_unit *dwo_tu;
6789 void **slot;
6790 const gdb_byte *ptr = info_ptr;
6791 struct comp_unit_head header;
6792 unsigned int length;
8b70b953 6793
9c541725 6794 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6795
a49dd8dd
JK
6796 /* Initialize it due to a false compiler warning. */
6797 header.signature = -1;
9c541725 6798 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6799
78d4d2c5
JK
6800 /* We need to read the type's signature in order to build the hash
6801 table, but we don't need anything else just yet. */
348e048f 6802
ed2dc618 6803 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6804 abbrev_section, ptr, section_kind);
348e048f 6805
78d4d2c5 6806 length = get_cu_length (&header);
6caca83c 6807
78d4d2c5
JK
6808 /* Skip dummy type units. */
6809 if (ptr >= info_ptr + length
43988095
JK
6810 || peek_abbrev_code (abfd, ptr) == 0
6811 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6812 {
6813 info_ptr += length;
6814 continue;
6815 }
dee91e82 6816
78d4d2c5
JK
6817 if (types_htab == NULL)
6818 {
6819 if (dwo_file)
6820 types_htab = allocate_dwo_unit_table (objfile);
6821 else
6822 types_htab = allocate_signatured_type_table (objfile);
6823 }
8b70b953 6824
78d4d2c5
JK
6825 if (dwo_file)
6826 {
6827 sig_type = NULL;
6828 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6829 struct dwo_unit);
6830 dwo_tu->dwo_file = dwo_file;
43988095 6831 dwo_tu->signature = header.signature;
9c541725 6832 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6833 dwo_tu->section = section;
9c541725 6834 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6835 dwo_tu->length = length;
6836 }
6837 else
6838 {
6839 /* N.B.: type_offset is not usable if this type uses a DWO file.
6840 The real type_offset is in the DWO file. */
6841 dwo_tu = NULL;
6842 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6843 struct signatured_type);
43988095 6844 sig_type->signature = header.signature;
9c541725 6845 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6846 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6847 sig_type->per_cu.is_debug_types = 1;
6848 sig_type->per_cu.section = section;
9c541725 6849 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6850 sig_type->per_cu.length = length;
6851 }
6852
6853 slot = htab_find_slot (types_htab,
6854 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6855 INSERT);
6856 gdb_assert (slot != NULL);
6857 if (*slot != NULL)
6858 {
9c541725 6859 sect_offset dup_sect_off;
0349ea22 6860
3019eac3
DE
6861 if (dwo_file)
6862 {
78d4d2c5
JK
6863 const struct dwo_unit *dup_tu
6864 = (const struct dwo_unit *) *slot;
6865
9c541725 6866 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6867 }
6868 else
6869 {
78d4d2c5
JK
6870 const struct signatured_type *dup_tu
6871 = (const struct signatured_type *) *slot;
6872
9c541725 6873 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6874 }
8b70b953 6875
b98664d3 6876 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6877 " the entry at offset %s, signature %s"),
6878 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6879 hex_string (header.signature));
78d4d2c5
JK
6880 }
6881 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6882
78d4d2c5 6883 if (dwarf_read_debug > 1)
9d8780f0
SM
6884 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6885 sect_offset_str (sect_off),
43988095 6886 hex_string (header.signature));
3019eac3 6887
78d4d2c5
JK
6888 info_ptr += length;
6889 }
6890}
3019eac3 6891
78d4d2c5
JK
6892/* Create the hash table of all entries in the .debug_types
6893 (or .debug_types.dwo) section(s).
6894 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6895 otherwise it is NULL.
b3c8eb43 6896
78d4d2c5 6897 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6898
78d4d2c5 6899 Note: This function processes DWO files only, not DWP files. */
348e048f 6900
78d4d2c5 6901static void
ed2dc618
SM
6902create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6903 struct dwo_file *dwo_file,
fd5866f6 6904 gdb::array_view<dwarf2_section_info> type_sections,
78d4d2c5
JK
6905 htab_t &types_htab)
6906{
fd5866f6
SM
6907 for (dwarf2_section_info &section : type_sections)
6908 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6909 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6910}
6911
6912/* Create the hash table of all entries in the .debug_types section,
6913 and initialize all_type_units.
6914 The result is zero if there is an error (e.g. missing .debug_types section),
6915 otherwise non-zero. */
6916
6917static int
ed2dc618 6918create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6919{
78d4d2c5 6920 htab_t types_htab = NULL;
3019eac3 6921
ed2dc618
SM
6922 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6923 &dwarf2_per_objfile->info, types_htab,
43988095 6924 rcuh_kind::COMPILE);
ed2dc618
SM
6925 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6926 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6927 if (types_htab == NULL)
6928 {
6929 dwarf2_per_objfile->signatured_types = NULL;
6930 return 0;
6931 }
6932
348e048f
DE
6933 dwarf2_per_objfile->signatured_types = types_htab;
6934
b2bdb8cf
SM
6935 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6936 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6937
6938 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6939 &dwarf2_per_objfile->all_type_units);
1fd400ff 6940
348e048f
DE
6941 return 1;
6942}
6943
6aa5f3a6
DE
6944/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6945 If SLOT is non-NULL, it is the entry to use in the hash table.
6946 Otherwise we find one. */
6947
6948static struct signatured_type *
ed2dc618
SM
6949add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6950 void **slot)
6aa5f3a6
DE
6951{
6952 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6953
b2bdb8cf
SM
6954 if (dwarf2_per_objfile->all_type_units.size ()
6955 == dwarf2_per_objfile->all_type_units.capacity ())
6956 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6957
b2bdb8cf
SM
6958 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6959 struct signatured_type);
6960
6961 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6962 sig_type->signature = sig;
6963 sig_type->per_cu.is_debug_types = 1;
6964 if (dwarf2_per_objfile->using_index)
6965 {
6966 sig_type->per_cu.v.quick =
6967 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6968 struct dwarf2_per_cu_quick_data);
6969 }
6970
6971 if (slot == NULL)
6972 {
6973 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6974 sig_type, INSERT);
6975 }
6976 gdb_assert (*slot == NULL);
6977 *slot = sig_type;
6978 /* The rest of sig_type must be filled in by the caller. */
6979 return sig_type;
6980}
6981
a2ce51a0
DE
6982/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6983 Fill in SIG_ENTRY with DWO_ENTRY. */
6984
6985static void
ed2dc618 6986fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6987 struct signatured_type *sig_entry,
6988 struct dwo_unit *dwo_entry)
6989{
7ee85ab1 6990 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6991 gdb_assert (! sig_entry->per_cu.queued);
6992 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6993 if (dwarf2_per_objfile->using_index)
6994 {
6995 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6996 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6997 }
6998 else
6999 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 7000 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 7001 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 7002 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
7003 gdb_assert (sig_entry->dwo_unit == NULL);
7004
7005 sig_entry->per_cu.section = dwo_entry->section;
9c541725 7006 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
7007 sig_entry->per_cu.length = dwo_entry->length;
7008 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 7009 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
7010 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7011 sig_entry->dwo_unit = dwo_entry;
7012}
7013
7014/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
7015 If we haven't read the TU yet, create the signatured_type data structure
7016 for a TU to be read in directly from a DWO file, bypassing the stub.
7017 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7018 using .gdb_index, then when reading a CU we want to stay in the DWO file
7019 containing that CU. Otherwise we could end up reading several other DWO
7020 files (due to comdat folding) to process the transitive closure of all the
7021 mentioned TUs, and that can be slow. The current DWO file will have every
7022 type signature that it needs.
a2ce51a0
DE
7023 We only do this for .gdb_index because in the psymtab case we already have
7024 to read all the DWOs to build the type unit groups. */
7025
7026static struct signatured_type *
7027lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7028{
518817b3
SM
7029 struct dwarf2_per_objfile *dwarf2_per_objfile
7030 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
7031 struct objfile *objfile = dwarf2_per_objfile->objfile;
7032 struct dwo_file *dwo_file;
7033 struct dwo_unit find_dwo_entry, *dwo_entry;
7034 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7035 void **slot;
a2ce51a0
DE
7036
7037 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7038
6aa5f3a6
DE
7039 /* If TU skeletons have been removed then we may not have read in any
7040 TUs yet. */
7041 if (dwarf2_per_objfile->signatured_types == NULL)
7042 {
7043 dwarf2_per_objfile->signatured_types
7044 = allocate_signatured_type_table (objfile);
7045 }
a2ce51a0
DE
7046
7047 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
7048 Use the global signatured_types array to do our own comdat-folding
7049 of types. If this is the first time we're reading this TU, and
7050 the TU has an entry in .gdb_index, replace the recorded data from
7051 .gdb_index with this TU. */
a2ce51a0 7052
a2ce51a0 7053 find_sig_entry.signature = sig;
6aa5f3a6
DE
7054 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7055 &find_sig_entry, INSERT);
9a3c8263 7056 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
7057
7058 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
7059 read. Don't reassign the global entry to point to this DWO if that's
7060 the case. Also note that if the TU is already being read, it may not
7061 have come from a DWO, the program may be a mix of Fission-compiled
7062 code and non-Fission-compiled code. */
7063
7064 /* Have we already tried to read this TU?
7065 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7066 needn't exist in the global table yet). */
7067 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
7068 return sig_entry;
7069
6aa5f3a6
DE
7070 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7071 dwo_unit of the TU itself. */
7072 dwo_file = cu->dwo_unit->dwo_file;
7073
a2ce51a0
DE
7074 /* Ok, this is the first time we're reading this TU. */
7075 if (dwo_file->tus == NULL)
7076 return NULL;
7077 find_dwo_entry.signature = sig;
9a3c8263 7078 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
7079 if (dwo_entry == NULL)
7080 return NULL;
7081
6aa5f3a6
DE
7082 /* If the global table doesn't have an entry for this TU, add one. */
7083 if (sig_entry == NULL)
ed2dc618 7084 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 7085
ed2dc618 7086 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 7087 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
7088 return sig_entry;
7089}
7090
a2ce51a0
DE
7091/* Subroutine of lookup_signatured_type.
7092 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
7093 then try the DWP file. If the TU stub (skeleton) has been removed then
7094 it won't be in .gdb_index. */
a2ce51a0
DE
7095
7096static struct signatured_type *
7097lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7098{
518817b3
SM
7099 struct dwarf2_per_objfile *dwarf2_per_objfile
7100 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 7101 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 7102 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
7103 struct dwo_unit *dwo_entry;
7104 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7105 void **slot;
a2ce51a0
DE
7106
7107 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7108 gdb_assert (dwp_file != NULL);
7109
6aa5f3a6
DE
7110 /* If TU skeletons have been removed then we may not have read in any
7111 TUs yet. */
7112 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 7113 {
6aa5f3a6
DE
7114 dwarf2_per_objfile->signatured_types
7115 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
7116 }
7117
6aa5f3a6
DE
7118 find_sig_entry.signature = sig;
7119 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7120 &find_sig_entry, INSERT);
9a3c8263 7121 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
7122
7123 /* Have we already tried to read this TU?
7124 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7125 needn't exist in the global table yet). */
7126 if (sig_entry != NULL)
7127 return sig_entry;
7128
a2ce51a0
DE
7129 if (dwp_file->tus == NULL)
7130 return NULL;
ed2dc618 7131 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 7132 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7133 if (dwo_entry == NULL)
7134 return NULL;
7135
ed2dc618
SM
7136 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7137 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 7138
a2ce51a0
DE
7139 return sig_entry;
7140}
7141
380bca97 7142/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7143 Returns NULL if signature SIG is not present in the table.
7144 It is up to the caller to complain about this. */
348e048f
DE
7145
7146static struct signatured_type *
a2ce51a0 7147lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7148{
518817b3
SM
7149 struct dwarf2_per_objfile *dwarf2_per_objfile
7150 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7151
a2ce51a0
DE
7152 if (cu->dwo_unit
7153 && dwarf2_per_objfile->using_index)
7154 {
7155 /* We're in a DWO/DWP file, and we're using .gdb_index.
7156 These cases require special processing. */
ed2dc618 7157 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7158 return lookup_dwo_signatured_type (cu, sig);
7159 else
7160 return lookup_dwp_signatured_type (cu, sig);
7161 }
7162 else
7163 {
7164 struct signatured_type find_entry, *entry;
348e048f 7165
a2ce51a0
DE
7166 if (dwarf2_per_objfile->signatured_types == NULL)
7167 return NULL;
7168 find_entry.signature = sig;
9a3c8263
SM
7169 entry = ((struct signatured_type *)
7170 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7171 return entry;
7172 }
348e048f 7173}
42e7ad6c
DE
7174\f
7175/* Low level DIE reading support. */
348e048f 7176
d85a05f0
DJ
7177/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7178
7179static void
7180init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7181 struct dwarf2_cu *cu,
3019eac3 7182 struct dwarf2_section_info *section,
685af9cd
TT
7183 struct dwo_file *dwo_file,
7184 struct abbrev_table *abbrev_table)
d85a05f0 7185{
fceca515 7186 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7187 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7188 reader->cu = cu;
3019eac3 7189 reader->dwo_file = dwo_file;
dee91e82
DE
7190 reader->die_section = section;
7191 reader->buffer = section->buffer;
f664829e 7192 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7193 reader->comp_dir = NULL;
685af9cd 7194 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7195}
7196
b0c7bfa9
DE
7197/* Subroutine of init_cutu_and_read_dies to simplify it.
7198 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7199 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7200 already.
7201
7202 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7203 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7204 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7205 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7206 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7207 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7208 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7209 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7210 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7211 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7212 kept around for at least as long as *RESULT_READER.
7213
b0c7bfa9
DE
7214 The result is non-zero if a valid (non-dummy) DIE was found. */
7215
7216static int
7217read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7218 struct dwo_unit *dwo_unit,
b0c7bfa9 7219 struct die_info *stub_comp_unit_die,
a2ce51a0 7220 const char *stub_comp_dir,
b0c7bfa9 7221 struct die_reader_specs *result_reader,
d521ce57 7222 const gdb_byte **result_info_ptr,
b0c7bfa9 7223 struct die_info **result_comp_unit_die,
685af9cd
TT
7224 int *result_has_children,
7225 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7226{
ed2dc618 7227 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7228 struct objfile *objfile = dwarf2_per_objfile->objfile;
7229 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7230 bfd *abfd;
d521ce57 7231 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7232 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7233 int i,num_extra_attrs;
7234 struct dwarf2_section_info *dwo_abbrev_section;
7235 struct attribute *attr;
7236 struct die_info *comp_unit_die;
7237
b0aeadb3
DE
7238 /* At most one of these may be provided. */
7239 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7240
b0c7bfa9
DE
7241 /* These attributes aren't processed until later:
7242 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7243 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7244 referenced later. However, these attributes are found in the stub
7245 which we won't have later. In order to not impose this complication
7246 on the rest of the code, we read them here and copy them to the
7247 DWO CU/TU die. */
b0c7bfa9
DE
7248
7249 stmt_list = NULL;
7250 low_pc = NULL;
7251 high_pc = NULL;
7252 ranges = NULL;
7253 comp_dir = NULL;
7254
7255 if (stub_comp_unit_die != NULL)
7256 {
7257 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7258 DWO file. */
7259 if (! this_cu->is_debug_types)
7260 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7261 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7262 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7263 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7264 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7265
7266 /* There should be a DW_AT_addr_base attribute here (if needed).
336d760d
AT
7267 We need the value before we can process DW_FORM_GNU_addr_index
7268 or DW_FORM_addrx. */
b0c7bfa9
DE
7269 cu->addr_base = 0;
7270 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
435d3d88 7271 if (attr != nullptr)
b0c7bfa9
DE
7272 cu->addr_base = DW_UNSND (attr);
7273
7274 /* There should be a DW_AT_ranges_base attribute here (if needed).
7275 We need the value before we can process DW_AT_ranges. */
7276 cu->ranges_base = 0;
7277 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
435d3d88 7278 if (attr != nullptr)
b0c7bfa9
DE
7279 cu->ranges_base = DW_UNSND (attr);
7280 }
a2ce51a0
DE
7281 else if (stub_comp_dir != NULL)
7282 {
7283 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7284 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7285 comp_dir->name = DW_AT_comp_dir;
7286 comp_dir->form = DW_FORM_string;
7287 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7288 DW_STRING (comp_dir) = stub_comp_dir;
7289 }
b0c7bfa9
DE
7290
7291 /* Set up for reading the DWO CU/TU. */
7292 cu->dwo_unit = dwo_unit;
685af9cd 7293 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7294 dwarf2_read_section (objfile, section);
a32a8923 7295 abfd = get_section_bfd_owner (section);
9c541725
PA
7296 begin_info_ptr = info_ptr = (section->buffer
7297 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7298 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7299
7300 if (this_cu->is_debug_types)
7301 {
b0c7bfa9
DE
7302 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7303
ed2dc618
SM
7304 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7305 &cu->header, section,
b0c7bfa9 7306 dwo_abbrev_section,
43988095 7307 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7308 /* This is not an assert because it can be caused by bad debug info. */
43988095 7309 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7310 {
7311 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7312 " TU at offset %s [in module %s]"),
a2ce51a0 7313 hex_string (sig_type->signature),
43988095 7314 hex_string (cu->header.signature),
9d8780f0 7315 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7316 bfd_get_filename (abfd));
7317 }
9c541725 7318 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7319 /* For DWOs coming from DWP files, we don't know the CU length
7320 nor the type's offset in the TU until now. */
7321 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7322 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7323
7324 /* Establish the type offset that can be used to lookup the type.
7325 For DWO files, we don't know it until now. */
9c541725
PA
7326 sig_type->type_offset_in_section
7327 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7328 }
7329 else
7330 {
ed2dc618
SM
7331 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7332 &cu->header, section,
b0c7bfa9 7333 dwo_abbrev_section,
43988095 7334 info_ptr, rcuh_kind::COMPILE);
9c541725 7335 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7336 /* For DWOs coming from DWP files, we don't know the CU length
7337 until now. */
7338 dwo_unit->length = get_cu_length (&cu->header);
7339 }
7340
685af9cd
TT
7341 *result_dwo_abbrev_table
7342 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7343 cu->header.abbrev_sect_off);
7344 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7345 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7346
7347 /* Read in the die, but leave space to copy over the attributes
7348 from the stub. This has the benefit of simplifying the rest of
7349 the code - all the work to maintain the illusion of a single
7350 DW_TAG_{compile,type}_unit DIE is done here. */
7351 num_extra_attrs = ((stmt_list != NULL)
7352 + (low_pc != NULL)
7353 + (high_pc != NULL)
7354 + (ranges != NULL)
7355 + (comp_dir != NULL));
7356 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7357 result_has_children, num_extra_attrs);
7358
7359 /* Copy over the attributes from the stub to the DIE we just read in. */
7360 comp_unit_die = *result_comp_unit_die;
7361 i = comp_unit_die->num_attrs;
7362 if (stmt_list != NULL)
7363 comp_unit_die->attrs[i++] = *stmt_list;
7364 if (low_pc != NULL)
7365 comp_unit_die->attrs[i++] = *low_pc;
7366 if (high_pc != NULL)
7367 comp_unit_die->attrs[i++] = *high_pc;
7368 if (ranges != NULL)
7369 comp_unit_die->attrs[i++] = *ranges;
7370 if (comp_dir != NULL)
7371 comp_unit_die->attrs[i++] = *comp_dir;
7372 comp_unit_die->num_attrs += num_extra_attrs;
7373
b4f54984 7374 if (dwarf_die_debug)
bf6af496
DE
7375 {
7376 fprintf_unfiltered (gdb_stdlog,
7377 "Read die from %s@0x%x of %s:\n",
a32a8923 7378 get_section_name (section),
bf6af496
DE
7379 (unsigned) (begin_info_ptr - section->buffer),
7380 bfd_get_filename (abfd));
b4f54984 7381 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7382 }
7383
a2ce51a0
DE
7384 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7385 TUs by skipping the stub and going directly to the entry in the DWO file.
7386 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7387 to get it via circuitous means. Blech. */
7388 if (comp_dir != NULL)
7389 result_reader->comp_dir = DW_STRING (comp_dir);
7390
b0c7bfa9
DE
7391 /* Skip dummy compilation units. */
7392 if (info_ptr >= begin_info_ptr + dwo_unit->length
7393 || peek_abbrev_code (abfd, info_ptr) == 0)
7394 return 0;
7395
7396 *result_info_ptr = info_ptr;
7397 return 1;
7398}
7399
a084a2a6
AT
7400/* Return the signature of the compile unit, if found. In DWARF 4 and before,
7401 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7402 signature is part of the header. */
7403static gdb::optional<ULONGEST>
7404lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7405{
7406 if (cu->header.version >= 5)
7407 return cu->header.signature;
7408 struct attribute *attr;
7409 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7410 if (attr == nullptr)
7411 return gdb::optional<ULONGEST> ();
7412 return DW_UNSND (attr);
7413}
7414
b0c7bfa9
DE
7415/* Subroutine of init_cutu_and_read_dies to simplify it.
7416 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7417 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7418
7419static struct dwo_unit *
7420lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7421 struct die_info *comp_unit_die)
7422{
7423 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7424 struct dwo_unit *dwo_unit;
7425 const char *comp_dir, *dwo_name;
7426
a2ce51a0
DE
7427 gdb_assert (cu != NULL);
7428
b0c7bfa9 7429 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 7430 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 7431 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7432
7433 if (this_cu->is_debug_types)
7434 {
7435 struct signatured_type *sig_type;
7436
7437 /* Since this_cu is the first member of struct signatured_type,
7438 we can go from a pointer to one to a pointer to the other. */
7439 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
7440 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7441 }
7442 else
7443 {
a084a2a6
AT
7444 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7445 if (!signature.has_value ())
b0c7bfa9
DE
7446 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7447 " [in module %s]"),
e3b94546 7448 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 7449 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 7450 *signature);
b0c7bfa9
DE
7451 }
7452
b0c7bfa9
DE
7453 return dwo_unit;
7454}
7455
a2ce51a0 7456/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6 7457 See it for a description of the parameters.
fcd3b13d 7458 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0
DE
7459
7460static void
6aa5f3a6
DE
7461init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7462 int use_existing_cu, int keep,
a2ce51a0
DE
7463 die_reader_func_ftype *die_reader_func,
7464 void *data)
7465{
fcd3b13d 7466 std::unique_ptr<dwarf2_cu> new_cu;
a2ce51a0 7467 struct signatured_type *sig_type;
a2ce51a0
DE
7468 struct die_reader_specs reader;
7469 const gdb_byte *info_ptr;
7470 struct die_info *comp_unit_die;
7471 int has_children;
ed2dc618 7472 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
a2ce51a0
DE
7473
7474 /* Verify we can do the following downcast, and that we have the
7475 data we need. */
7476 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7477 sig_type = (struct signatured_type *) this_cu;
7478 gdb_assert (sig_type->dwo_unit != NULL);
7479
6aa5f3a6
DE
7480 if (use_existing_cu && this_cu->cu != NULL)
7481 {
7482 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6
DE
7483 /* There's no need to do the rereading_dwo_cu handling that
7484 init_cutu_and_read_dies does since we don't read the stub. */
7485 }
7486 else
7487 {
7488 /* If !use_existing_cu, this_cu->cu must be NULL. */
7489 gdb_assert (this_cu->cu == NULL);
fcd3b13d 7490 new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7491 }
7492
7493 /* A future optimization, if needed, would be to use an existing
7494 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7495 could share abbrev tables. */
a2ce51a0 7496
685af9cd
TT
7497 /* The abbreviation table used by READER, this must live at least as long as
7498 READER. */
7499 abbrev_table_up dwo_abbrev_table;
7500
a2ce51a0 7501 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7502 NULL /* stub_comp_unit_die */,
7503 sig_type->dwo_unit->dwo_file->comp_dir,
7504 &reader, &info_ptr,
685af9cd
TT
7505 &comp_unit_die, &has_children,
7506 &dwo_abbrev_table) == 0)
a2ce51a0
DE
7507 {
7508 /* Dummy die. */
a2ce51a0
DE
7509 return;
7510 }
7511
7512 /* All the "real" work is done here. */
7513 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7514
6aa5f3a6 7515 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7516 but the alternative is making the latter more complex.
7517 This function is only for the special case of using DWO files directly:
7518 no point in overly complicating the general case just to handle this. */
fcd3b13d 7519 if (new_cu != NULL && keep)
a2ce51a0 7520 {
fcd3b13d
SM
7521 /* Link this CU into read_in_chain. */
7522 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7523 dwarf2_per_objfile->read_in_chain = this_cu;
7524 /* The chain owns it now. */
7525 new_cu.release ();
a2ce51a0 7526 }
a2ce51a0
DE
7527}
7528
fd820528 7529/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7530 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7531
f4dc4d17
DE
7532 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7533 Otherwise the table specified in the comp unit header is read in and used.
7534 This is an optimization for when we already have the abbrev table.
7535
dee91e82
DE
7536 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7537 Otherwise, a new CU is allocated with xmalloc.
7538
7539 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7540 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7541
7542 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7543 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7544
70221824 7545static void
fd820528 7546init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7547 struct abbrev_table *abbrev_table,
fd820528 7548 int use_existing_cu, int keep,
58f0c718 7549 bool skip_partial,
fd820528
DE
7550 die_reader_func_ftype *die_reader_func,
7551 void *data)
c906108c 7552{
ed2dc618 7553 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7554 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7555 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7556 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7557 struct dwarf2_cu *cu;
d521ce57 7558 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7559 struct die_reader_specs reader;
d85a05f0 7560 struct die_info *comp_unit_die;
dee91e82 7561 int has_children;
dee91e82 7562 struct signatured_type *sig_type = NULL;
4bdcc0c1 7563 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7564 /* Non-zero if CU currently points to a DWO file and we need to
7565 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7566 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7567 int rereading_dwo_cu = 0;
c906108c 7568
b4f54984 7569 if (dwarf_die_debug)
9d8780f0 7570 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7571 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7572 sect_offset_str (this_cu->sect_off));
09406207 7573
dee91e82
DE
7574 if (use_existing_cu)
7575 gdb_assert (keep);
23745b47 7576
a2ce51a0
DE
7577 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7578 file (instead of going through the stub), short-circuit all of this. */
7579 if (this_cu->reading_dwo_directly)
7580 {
7581 /* Narrow down the scope of possibilities to have to understand. */
7582 gdb_assert (this_cu->is_debug_types);
7583 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7584 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7585 die_reader_func, data);
a2ce51a0
DE
7586 return;
7587 }
7588
dee91e82
DE
7589 /* This is cheap if the section is already read in. */
7590 dwarf2_read_section (objfile, section);
7591
9c541725 7592 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7593
7594 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7595
fcd3b13d 7596 std::unique_ptr<dwarf2_cu> new_cu;
dee91e82
DE
7597 if (use_existing_cu && this_cu->cu != NULL)
7598 {
7599 cu = this_cu->cu;
42e7ad6c
DE
7600 /* If this CU is from a DWO file we need to start over, we need to
7601 refetch the attributes from the skeleton CU.
7602 This could be optimized by retrieving those attributes from when we
7603 were here the first time: the previous comp_unit_die was stored in
7604 comp_unit_obstack. But there's no data yet that we need this
7605 optimization. */
7606 if (cu->dwo_unit != NULL)
7607 rereading_dwo_cu = 1;
dee91e82
DE
7608 }
7609 else
7610 {
7611 /* If !use_existing_cu, this_cu->cu must be NULL. */
7612 gdb_assert (this_cu->cu == NULL);
fcd3b13d
SM
7613 new_cu.reset (new dwarf2_cu (this_cu));
7614 cu = new_cu.get ();
42e7ad6c 7615 }
dee91e82 7616
b0c7bfa9 7617 /* Get the header. */
9c541725 7618 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7619 {
7620 /* We already have the header, there's no need to read it in again. */
9c541725 7621 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7622 }
7623 else
7624 {
3019eac3 7625 if (this_cu->is_debug_types)
dee91e82 7626 {
ed2dc618
SM
7627 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7628 &cu->header, section,
4bdcc0c1 7629 abbrev_section, info_ptr,
43988095 7630 rcuh_kind::TYPE);
dee91e82 7631
42e7ad6c
DE
7632 /* Since per_cu is the first member of struct signatured_type,
7633 we can go from a pointer to one to a pointer to the other. */
7634 sig_type = (struct signatured_type *) this_cu;
43988095 7635 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7636 gdb_assert (sig_type->type_offset_in_tu
7637 == cu->header.type_cu_offset_in_tu);
7638 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7639
42e7ad6c
DE
7640 /* LENGTH has not been set yet for type units if we're
7641 using .gdb_index. */
1ce1cefd 7642 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7643
7644 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7645 sig_type->type_offset_in_section =
7646 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7647
7648 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7649 }
7650 else
7651 {
ed2dc618
SM
7652 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7653 &cu->header, section,
4bdcc0c1 7654 abbrev_section,
43988095
JK
7655 info_ptr,
7656 rcuh_kind::COMPILE);
dee91e82 7657
9c541725 7658 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7659 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7660 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7661 }
7662 }
10b3939b 7663
6caca83c 7664 /* Skip dummy compilation units. */
dee91e82 7665 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7666 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7667 return;
6caca83c 7668
433df2d4
DE
7669 /* If we don't have them yet, read the abbrevs for this compilation unit.
7670 And if we need to read them now, make sure they're freed when we're
685af9cd
TT
7671 done (own the table through ABBREV_TABLE_HOLDER). */
7672 abbrev_table_up abbrev_table_holder;
f4dc4d17 7673 if (abbrev_table != NULL)
685af9cd
TT
7674 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7675 else
f4dc4d17 7676 {
685af9cd
TT
7677 abbrev_table_holder
7678 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7679 cu->header.abbrev_sect_off);
7680 abbrev_table = abbrev_table_holder.get ();
42e7ad6c 7681 }
af703f96 7682
dee91e82 7683 /* Read the top level CU/TU die. */
685af9cd 7684 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
dee91e82 7685 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 7686
58f0c718
TT
7687 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7688 return;
7689
b0c7bfa9 7690 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7691 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7692 table from the DWO file and pass the ownership over to us. It will be
7693 referenced from READER, so we must make sure to free it after we're done
7694 with READER.
7695
b0c7bfa9
DE
7696 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7697 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7698 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
685af9cd 7699 abbrev_table_up dwo_abbrev_table;
a084a2a6 7700 if (dwo_name != nullptr)
3019eac3 7701 {
3019eac3 7702 struct dwo_unit *dwo_unit;
b0c7bfa9 7703 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7704
7705 if (has_children)
6a506a2d 7706 {
b98664d3 7707 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7708 " has children (offset %s) [in module %s]"),
7709 sect_offset_str (this_cu->sect_off),
7710 bfd_get_filename (abfd));
6a506a2d 7711 }
b0c7bfa9 7712 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 7713 if (dwo_unit != NULL)
3019eac3 7714 {
6a506a2d 7715 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7716 comp_unit_die, NULL,
6a506a2d 7717 &reader, &info_ptr,
685af9cd
TT
7718 &dwo_comp_unit_die, &has_children,
7719 &dwo_abbrev_table) == 0)
6a506a2d
DE
7720 {
7721 /* Dummy die. */
6a506a2d
DE
7722 return;
7723 }
7724 comp_unit_die = dwo_comp_unit_die;
7725 }
7726 else
7727 {
7728 /* Yikes, we couldn't find the rest of the DIE, we only have
7729 the stub. A complaint has already been logged. There's
7730 not much more we can do except pass on the stub DIE to
7731 die_reader_func. We don't want to throw an error on bad
7732 debug info. */
3019eac3
DE
7733 }
7734 }
7735
b0c7bfa9 7736 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
7737 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7738
b0c7bfa9 7739 /* Done, clean up. */
fcd3b13d 7740 if (new_cu != NULL && keep)
348e048f 7741 {
fcd3b13d
SM
7742 /* Link this CU into read_in_chain. */
7743 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7744 dwarf2_per_objfile->read_in_chain = this_cu;
7745 /* The chain owns it now. */
7746 new_cu.release ();
348e048f 7747 }
dee91e82
DE
7748}
7749
33e80786
DE
7750/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7751 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7752 to have already done the lookup to find the DWO file).
dee91e82
DE
7753
7754 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7755 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7756
7757 We fill in THIS_CU->length.
7758
7759 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7760 linker) then DIE_READER_FUNC will not get called.
7761
7762 THIS_CU->cu is always freed when done.
3019eac3
DE
7763 This is done in order to not leave THIS_CU->cu in a state where we have
7764 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
7765
7766static void
7767init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 7768 struct dwo_file *dwo_file,
dee91e82
DE
7769 die_reader_func_ftype *die_reader_func,
7770 void *data)
7771{
ed2dc618 7772 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7773 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7774 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7775 bfd *abfd = get_section_bfd_owner (section);
33e80786 7776 struct dwarf2_section_info *abbrev_section;
d521ce57 7777 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7778 struct die_reader_specs reader;
dee91e82
DE
7779 struct die_info *comp_unit_die;
7780 int has_children;
7781
b4f54984 7782 if (dwarf_die_debug)
9d8780f0 7783 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7784 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7785 sect_offset_str (this_cu->sect_off));
09406207 7786
dee91e82
DE
7787 gdb_assert (this_cu->cu == NULL);
7788
33e80786
DE
7789 abbrev_section = (dwo_file != NULL
7790 ? &dwo_file->sections.abbrev
7791 : get_abbrev_section_for_cu (this_cu));
7792
dee91e82
DE
7793 /* This is cheap if the section is already read in. */
7794 dwarf2_read_section (objfile, section);
7795
fcd3b13d 7796 struct dwarf2_cu cu (this_cu);
dee91e82 7797
9c541725 7798 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618
SM
7799 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7800 &cu.header, section,
4bdcc0c1 7801 abbrev_section, info_ptr,
43988095
JK
7802 (this_cu->is_debug_types
7803 ? rcuh_kind::TYPE
7804 : rcuh_kind::COMPILE));
dee91e82 7805
1ce1cefd 7806 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
7807
7808 /* Skip dummy compilation units. */
7809 if (info_ptr >= begin_info_ptr + this_cu->length
7810 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7811 return;
72bf9492 7812
685af9cd
TT
7813 abbrev_table_up abbrev_table
7814 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7815 cu.header.abbrev_sect_off);
dee91e82 7816
685af9cd 7817 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
dee91e82
DE
7818 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7819
7820 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
dee91e82
DE
7821}
7822
3019eac3
DE
7823/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7824 does not lookup the specified DWO file.
7825 This cannot be used to read DWO files.
dee91e82
DE
7826
7827 THIS_CU->cu is always freed when done.
3019eac3
DE
7828 This is done in order to not leave THIS_CU->cu in a state where we have
7829 to care whether it refers to the "main" CU or the DWO CU.
7830 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
7831
7832static void
7833init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7834 die_reader_func_ftype *die_reader_func,
7835 void *data)
7836{
33e80786 7837 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 7838}
0018ea6f
DE
7839\f
7840/* Type Unit Groups.
dee91e82 7841
0018ea6f
DE
7842 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7843 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7844 so that all types coming from the same compilation (.o file) are grouped
7845 together. A future step could be to put the types in the same symtab as
7846 the CU the types ultimately came from. */
ff013f42 7847
f4dc4d17
DE
7848static hashval_t
7849hash_type_unit_group (const void *item)
7850{
9a3c8263
SM
7851 const struct type_unit_group *tu_group
7852 = (const struct type_unit_group *) item;
f4dc4d17 7853
094b34ac 7854 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7855}
348e048f
DE
7856
7857static int
f4dc4d17 7858eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7859{
9a3c8263
SM
7860 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7861 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7862
094b34ac 7863 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7864}
348e048f 7865
f4dc4d17
DE
7866/* Allocate a hash table for type unit groups. */
7867
7868static htab_t
ed2dc618 7869allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
7870{
7871 return htab_create_alloc_ex (3,
7872 hash_type_unit_group,
7873 eq_type_unit_group,
7874 NULL,
ed2dc618 7875 &objfile->objfile_obstack,
f4dc4d17
DE
7876 hashtab_obstack_allocate,
7877 dummy_obstack_deallocate);
7878}
dee91e82 7879
f4dc4d17
DE
7880/* Type units that don't have DW_AT_stmt_list are grouped into their own
7881 partial symtabs. We combine several TUs per psymtab to not let the size
7882 of any one psymtab grow too big. */
7883#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7884#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7885
094b34ac 7886/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7887 Create the type_unit_group object used to hold one or more TUs. */
7888
7889static struct type_unit_group *
094b34ac 7890create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7891{
518817b3
SM
7892 struct dwarf2_per_objfile *dwarf2_per_objfile
7893 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7894 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7895 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7896 struct type_unit_group *tu_group;
f4dc4d17
DE
7897
7898 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7899 struct type_unit_group);
094b34ac 7900 per_cu = &tu_group->per_cu;
518817b3 7901 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7902
094b34ac
DE
7903 if (dwarf2_per_objfile->using_index)
7904 {
7905 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7906 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7907 }
7908 else
7909 {
9c541725 7910 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac 7911 struct partial_symtab *pst;
528e1572 7912 std::string name;
094b34ac
DE
7913
7914 /* Give the symtab a useful name for debug purposes. */
7915 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7916 name = string_printf ("<type_units_%d>",
7917 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7918 else
528e1572 7919 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7920
528e1572 7921 pst = create_partial_symtab (per_cu, name.c_str ());
094b34ac 7922 pst->anonymous = 1;
094b34ac 7923 }
f4dc4d17 7924
094b34ac 7925 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7926 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7927
7928 return tu_group;
7929}
7930
094b34ac
DE
7931/* Look up the type_unit_group for type unit CU, and create it if necessary.
7932 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7933
7934static struct type_unit_group *
ff39bb5e 7935get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7936{
518817b3
SM
7937 struct dwarf2_per_objfile *dwarf2_per_objfile
7938 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7939 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7940 struct type_unit_group *tu_group;
7941 void **slot;
7942 unsigned int line_offset;
7943 struct type_unit_group type_unit_group_for_lookup;
7944
7945 if (dwarf2_per_objfile->type_unit_groups == NULL)
7946 {
7947 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7948 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7949 }
7950
7951 /* Do we need to create a new group, or can we use an existing one? */
7952
7953 if (stmt_list)
7954 {
7955 line_offset = DW_UNSND (stmt_list);
7956 ++tu_stats->nr_symtab_sharers;
7957 }
7958 else
7959 {
7960 /* Ugh, no stmt_list. Rare, but we have to handle it.
7961 We can do various things here like create one group per TU or
7962 spread them over multiple groups to split up the expansion work.
7963 To avoid worst case scenarios (too many groups or too large groups)
7964 we, umm, group them in bunches. */
7965 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7966 | (tu_stats->nr_stmt_less_type_units
7967 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7968 ++tu_stats->nr_stmt_less_type_units;
7969 }
7970
094b34ac 7971 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7972 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
7973 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7974 &type_unit_group_for_lookup, INSERT);
7975 if (*slot != NULL)
7976 {
9a3c8263 7977 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7978 gdb_assert (tu_group != NULL);
7979 }
7980 else
7981 {
9c541725 7982 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7983 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7984 *slot = tu_group;
7985 ++tu_stats->nr_symtabs;
7986 }
7987
7988 return tu_group;
7989}
0018ea6f
DE
7990\f
7991/* Partial symbol tables. */
7992
7993/* Create a psymtab named NAME and assign it to PER_CU.
7994
7995 The caller must fill in the following details:
7996 dirname, textlow, texthigh. */
7997
7998static struct partial_symtab *
7999create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8000{
e3b94546 8001 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
0018ea6f
DE
8002 struct partial_symtab *pst;
8003
939652a5 8004 pst = start_psymtab_common (objfile, name, 0);
0018ea6f
DE
8005
8006 pst->psymtabs_addrmap_supported = 1;
8007
8008 /* This is the glue that links PST into GDB's symbol API. */
8009 pst->read_symtab_private = per_cu;
8010 pst->read_symtab = dwarf2_read_symtab;
8011 per_cu->v.psymtab = pst;
8012
8013 return pst;
8014}
8015
b93601f3
TT
8016/* The DATA object passed to process_psymtab_comp_unit_reader has this
8017 type. */
8018
8019struct process_psymtab_comp_unit_data
8020{
8021 /* True if we are reading a DW_TAG_partial_unit. */
8022
8023 int want_partial_unit;
8024
8025 /* The "pretend" language that is used if the CU doesn't declare a
8026 language. */
8027
8028 enum language pretend_language;
8029};
8030
0018ea6f
DE
8031/* die_reader_func for process_psymtab_comp_unit. */
8032
8033static void
8034process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8035 const gdb_byte *info_ptr,
0018ea6f
DE
8036 struct die_info *comp_unit_die,
8037 int has_children,
8038 void *data)
8039{
8040 struct dwarf2_cu *cu = reader->cu;
518817b3 8041 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 8042 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 8043 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
8044 CORE_ADDR baseaddr;
8045 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8046 struct partial_symtab *pst;
3a2b436a 8047 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 8048 const char *filename;
9a3c8263
SM
8049 struct process_psymtab_comp_unit_data *info
8050 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 8051
b93601f3 8052 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
8053 return;
8054
8055 gdb_assert (! per_cu->is_debug_types);
8056
b93601f3 8057 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f 8058
0018ea6f 8059 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
8060 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8061 if (filename == NULL)
0018ea6f 8062 filename = "";
0018ea6f
DE
8063
8064 pst = create_partial_symtab (per_cu, filename);
8065
8066 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 8067 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 8068
6a053cb1 8069 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
0018ea6f
DE
8070
8071 dwarf2_find_base_address (comp_unit_die, cu);
8072
8073 /* Possibly set the default values of LOWPC and HIGHPC from
8074 `DW_AT_ranges'. */
3a2b436a
JK
8075 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8076 &best_highpc, cu, pst);
8077 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
8078 {
8079 CORE_ADDR low
8080 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8081 - baseaddr);
8082 CORE_ADDR high
8083 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8084 - baseaddr - 1);
8085 /* Store the contiguous range if it is not empty; it can be
8086 empty for CUs with no code. */
d320c2b5
TT
8087 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8088 low, high, pst);
79748972 8089 }
0018ea6f
DE
8090
8091 /* Check if comp unit has_children.
8092 If so, read the rest of the partial symbols from this comp unit.
8093 If not, there's no more debug_info for this comp unit. */
8094 if (has_children)
8095 {
8096 struct partial_die_info *first_die;
8097 CORE_ADDR lowpc, highpc;
8098
8099 lowpc = ((CORE_ADDR) -1);
8100 highpc = ((CORE_ADDR) 0);
8101
8102 first_die = load_partial_dies (reader, info_ptr, 1);
8103
8104 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 8105 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
8106
8107 /* If we didn't find a lowpc, set it to highpc to avoid
8108 complaints from `maint check'. */
8109 if (lowpc == ((CORE_ADDR) -1))
8110 lowpc = highpc;
8111
8112 /* If the compilation unit didn't have an explicit address range,
8113 then use the information extracted from its child dies. */
e385593e 8114 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
8115 {
8116 best_lowpc = lowpc;
8117 best_highpc = highpc;
8118 }
8119 }
4ae976d1 8120 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8121 best_lowpc + baseaddr)
8122 - baseaddr);
4ae976d1 8123 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8124 best_highpc + baseaddr)
8125 - baseaddr);
0018ea6f 8126
8763cede 8127 end_psymtab_common (objfile, pst);
0018ea6f 8128
ae640021 8129 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
8130 {
8131 int i;
ae640021 8132 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
8133
8134 /* Fill in 'dependencies' here; we fill in 'users' in a
8135 post-pass. */
8136 pst->number_of_dependencies = len;
a9342b62
TT
8137 pst->dependencies
8138 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
8139 for (i = 0; i < len; ++i)
8140 {
8141 pst->dependencies[i]
8142 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
8143 }
0018ea6f 8144
ae640021 8145 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
8146 }
8147
8148 /* Get the list of files included in the current compilation unit,
8149 and build a psymtab for each of them. */
8150 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8151
b4f54984 8152 if (dwarf_read_debug)
b926417a
TT
8153 fprintf_unfiltered (gdb_stdlog,
8154 "Psymtab for %s unit @%s: %s - %s"
8155 ", %d global, %d static syms\n",
8156 per_cu->is_debug_types ? "type" : "comp",
8157 sect_offset_str (per_cu->sect_off),
8158 paddress (gdbarch, pst->text_low (objfile)),
8159 paddress (gdbarch, pst->text_high (objfile)),
8160 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
8161}
8162
8163/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8164 Process compilation unit THIS_CU for a psymtab. */
8165
8166static void
8167process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8168 int want_partial_unit,
8169 enum language pretend_language)
0018ea6f
DE
8170{
8171 /* If this compilation unit was already read in, free the
8172 cached copy in order to read it in again. This is
8173 necessary because we skipped some symbols when we first
8174 read in the compilation unit (see load_partial_dies).
8175 This problem could be avoided, but the benefit is unclear. */
8176 if (this_cu->cu != NULL)
8177 free_one_cached_comp_unit (this_cu);
8178
f1902523 8179 if (this_cu->is_debug_types)
58f0c718
TT
8180 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8181 build_type_psymtabs_reader, NULL);
f1902523
JK
8182 else
8183 {
8184 process_psymtab_comp_unit_data info;
8185 info.want_partial_unit = want_partial_unit;
8186 info.pretend_language = pretend_language;
58f0c718 8187 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
f1902523
JK
8188 process_psymtab_comp_unit_reader, &info);
8189 }
0018ea6f
DE
8190
8191 /* Age out any secondary CUs. */
ed2dc618 8192 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8193}
f4dc4d17
DE
8194
8195/* Reader function for build_type_psymtabs. */
8196
8197static void
8198build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8199 const gdb_byte *info_ptr,
f4dc4d17
DE
8200 struct die_info *type_unit_die,
8201 int has_children,
8202 void *data)
8203{
ed2dc618 8204 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8205 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8206 struct objfile *objfile = dwarf2_per_objfile->objfile;
8207 struct dwarf2_cu *cu = reader->cu;
8208 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8209 struct signatured_type *sig_type;
f4dc4d17
DE
8210 struct type_unit_group *tu_group;
8211 struct attribute *attr;
8212 struct partial_die_info *first_die;
8213 CORE_ADDR lowpc, highpc;
8214 struct partial_symtab *pst;
8215
8216 gdb_assert (data == NULL);
0186c6a7
DE
8217 gdb_assert (per_cu->is_debug_types);
8218 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8219
8220 if (! has_children)
8221 return;
8222
8223 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8224 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8225
df07e2c7 8226 if (tu_group->tus == nullptr)
a8b3b8e9 8227 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 8228 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
8229
8230 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17
DE
8231 pst = create_partial_symtab (per_cu, "");
8232 pst->anonymous = 1;
8233
8234 first_die = load_partial_dies (reader, info_ptr, 1);
8235
8236 lowpc = (CORE_ADDR) -1;
8237 highpc = (CORE_ADDR) 0;
8238 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8239
8763cede 8240 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8241}
8242
73051182
DE
8243/* Struct used to sort TUs by their abbreviation table offset. */
8244
8245struct tu_abbrev_offset
8246{
b2bdb8cf
SM
8247 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8248 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8249 {}
8250
8251 signatured_type *sig_type;
73051182
DE
8252 sect_offset abbrev_offset;
8253};
8254
484cf504 8255/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 8256
484cf504
TT
8257static bool
8258sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8259 const struct tu_abbrev_offset &b)
73051182 8260{
484cf504 8261 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
8262}
8263
8264/* Efficiently read all the type units.
8265 This does the bulk of the work for build_type_psymtabs.
8266
8267 The efficiency is because we sort TUs by the abbrev table they use and
8268 only read each abbrev table once. In one program there are 200K TUs
8269 sharing 8K abbrev tables.
8270
8271 The main purpose of this function is to support building the
8272 dwarf2_per_objfile->type_unit_groups table.
8273 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8274 can collapse the search space by grouping them by stmt_list.
8275 The savings can be significant, in the same program from above the 200K TUs
8276 share 8K stmt_list tables.
8277
8278 FUNC is expected to call get_type_unit_group, which will create the
8279 struct type_unit_group if necessary and add it to
8280 dwarf2_per_objfile->type_unit_groups. */
8281
8282static void
ed2dc618 8283build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8284{
73051182 8285 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 8286 abbrev_table_up abbrev_table;
73051182 8287 sect_offset abbrev_offset;
73051182
DE
8288
8289 /* It's up to the caller to not call us multiple times. */
8290 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8291
b2bdb8cf 8292 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
8293 return;
8294
8295 /* TUs typically share abbrev tables, and there can be way more TUs than
8296 abbrev tables. Sort by abbrev table to reduce the number of times we
8297 read each abbrev table in.
8298 Alternatives are to punt or to maintain a cache of abbrev tables.
8299 This is simpler and efficient enough for now.
8300
8301 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8302 symtab to use). Typically TUs with the same abbrev offset have the same
8303 stmt_list value too so in practice this should work well.
8304
8305 The basic algorithm here is:
8306
8307 sort TUs by abbrev table
8308 for each TU with same abbrev table:
8309 read abbrev table if first user
8310 read TU top level DIE
8311 [IWBN if DWO skeletons had DW_AT_stmt_list]
8312 call FUNC */
8313
b4f54984 8314 if (dwarf_read_debug)
73051182
DE
8315 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8316
8317 /* Sort in a separate table to maintain the order of all_type_units
8318 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
8319 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8320 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8321
8322 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8323 sorted_by_abbrev.emplace_back
8324 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8325 sig_type->per_cu.section,
8326 sig_type->per_cu.sect_off));
73051182 8327
484cf504
TT
8328 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8329 sort_tu_by_abbrev_offset);
73051182 8330
9c541725 8331 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 8332
b2bdb8cf 8333 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 8334 {
73051182
DE
8335 /* Switch to the next abbrev table if necessary. */
8336 if (abbrev_table == NULL
b2bdb8cf 8337 || tu.abbrev_offset != abbrev_offset)
73051182 8338 {
b2bdb8cf 8339 abbrev_offset = tu.abbrev_offset;
73051182 8340 abbrev_table =
ed2dc618
SM
8341 abbrev_table_read_table (dwarf2_per_objfile,
8342 &dwarf2_per_objfile->abbrev,
73051182
DE
8343 abbrev_offset);
8344 ++tu_stats->nr_uniq_abbrev_tables;
8345 }
8346
b2bdb8cf 8347 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
58f0c718 8348 0, 0, false, build_type_psymtabs_reader, NULL);
73051182 8349 }
6aa5f3a6 8350}
73051182 8351
6aa5f3a6
DE
8352/* Print collected type unit statistics. */
8353
8354static void
ed2dc618 8355print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8356{
8357 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8358
8359 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
8360 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8361 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
8362 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8363 tu_stats->nr_uniq_abbrev_tables);
8364 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8365 tu_stats->nr_symtabs);
8366 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8367 tu_stats->nr_symtab_sharers);
8368 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8369 tu_stats->nr_stmt_less_type_units);
8370 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8371 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8372}
8373
f4dc4d17
DE
8374/* Traversal function for build_type_psymtabs. */
8375
8376static int
8377build_type_psymtab_dependencies (void **slot, void *info)
8378{
ed2dc618
SM
8379 struct dwarf2_per_objfile *dwarf2_per_objfile
8380 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8381 struct objfile *objfile = dwarf2_per_objfile->objfile;
8382 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8383 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8384 struct partial_symtab *pst = per_cu->v.psymtab;
df07e2c7 8385 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
8386 int i;
8387
8388 gdb_assert (len > 0);
0186c6a7 8389 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8390
8391 pst->number_of_dependencies = len;
a9342b62 8392 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 8393 for (i = 0; i < len; ++i)
f4dc4d17 8394 {
df07e2c7 8395 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
8396 gdb_assert (iter->per_cu.is_debug_types);
8397 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8398 iter->type_unit_group = tu_group;
f4dc4d17
DE
8399 }
8400
df07e2c7
AB
8401 delete tu_group->tus;
8402 tu_group->tus = nullptr;
348e048f
DE
8403
8404 return 1;
8405}
8406
8407/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8408 Build partial symbol tables for the .debug_types comp-units. */
8409
8410static void
ed2dc618 8411build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8412{
ed2dc618 8413 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8414 return;
8415
ed2dc618 8416 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8417}
f4dc4d17 8418
6aa5f3a6
DE
8419/* Traversal function for process_skeletonless_type_unit.
8420 Read a TU in a DWO file and build partial symbols for it. */
8421
8422static int
8423process_skeletonless_type_unit (void **slot, void *info)
8424{
8425 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8426 struct dwarf2_per_objfile *dwarf2_per_objfile
8427 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8428 struct signatured_type find_entry, *entry;
8429
8430 /* If this TU doesn't exist in the global table, add it and read it in. */
8431
8432 if (dwarf2_per_objfile->signatured_types == NULL)
8433 {
8434 dwarf2_per_objfile->signatured_types
ed2dc618 8435 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8436 }
8437
8438 find_entry.signature = dwo_unit->signature;
8439 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8440 INSERT);
8441 /* If we've already seen this type there's nothing to do. What's happening
8442 is we're doing our own version of comdat-folding here. */
8443 if (*slot != NULL)
8444 return 1;
8445
8446 /* This does the job that create_all_type_units would have done for
8447 this TU. */
ed2dc618
SM
8448 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8449 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8450 *slot = entry;
8451
8452 /* This does the job that build_type_psymtabs_1 would have done. */
58f0c718 8453 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
6aa5f3a6
DE
8454 build_type_psymtabs_reader, NULL);
8455
8456 return 1;
8457}
8458
8459/* Traversal function for process_skeletonless_type_units. */
8460
8461static int
8462process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8463{
8464 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8465
8466 if (dwo_file->tus != NULL)
8467 {
8468 htab_traverse_noresize (dwo_file->tus,
8469 process_skeletonless_type_unit, info);
8470 }
8471
8472 return 1;
8473}
8474
8475/* Scan all TUs of DWO files, verifying we've processed them.
8476 This is needed in case a TU was emitted without its skeleton.
8477 Note: This can't be done until we know what all the DWO files are. */
8478
8479static void
ed2dc618 8480process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8481{
8482 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8483 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8484 && dwarf2_per_objfile->dwo_files != NULL)
8485 {
51ac9db5 8486 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 8487 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8488 dwarf2_per_objfile);
6aa5f3a6 8489 }
348e048f
DE
8490}
8491
ed2dc618 8492/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8493
8494static void
ed2dc618 8495set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 8496{
b76e467d 8497 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 8498 {
95554aad 8499 struct partial_symtab *pst = per_cu->v.psymtab;
95554aad 8500
36586728
TT
8501 if (pst == NULL)
8502 continue;
8503
b76e467d 8504 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8505 {
8506 /* Set the 'user' field only if it is not already set. */
8507 if (pst->dependencies[j]->user == NULL)
8508 pst->dependencies[j]->user = pst;
8509 }
8510 }
8511}
8512
93311388
DE
8513/* Build the partial symbol table by doing a quick pass through the
8514 .debug_info and .debug_abbrev sections. */
72bf9492 8515
93311388 8516static void
ed2dc618 8517dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8518{
ed2dc618 8519 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8520
b4f54984 8521 if (dwarf_read_debug)
45cfd468
DE
8522 {
8523 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8524 objfile_name (objfile));
45cfd468
DE
8525 }
8526
98bfdba5
PA
8527 dwarf2_per_objfile->reading_partial_symbols = 1;
8528
be391dca 8529 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8530
93311388
DE
8531 /* Any cached compilation units will be linked by the per-objfile
8532 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8533 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8534
ed2dc618 8535 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8536
ed2dc618 8537 create_all_comp_units (dwarf2_per_objfile);
c906108c 8538
60606b2c
TT
8539 /* Create a temporary address map on a temporary obstack. We later
8540 copy this to the final obstack. */
8268c778 8541 auto_obstack temp_obstack;
791afaa2
TT
8542
8543 scoped_restore save_psymtabs_addrmap
d320c2b5 8544 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8545 addrmap_create_mutable (&temp_obstack));
72bf9492 8546
b76e467d
SM
8547 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8548 process_psymtab_comp_unit (per_cu, 0, language_minimal);
ff013f42 8549
6aa5f3a6 8550 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8551 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8552
8553 /* Now that all TUs have been processed we can fill in the dependencies. */
8554 if (dwarf2_per_objfile->type_unit_groups != NULL)
8555 {
8556 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8557 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8558 }
8559
b4f54984 8560 if (dwarf_read_debug)
ed2dc618 8561 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8562
ed2dc618 8563 set_partial_user (dwarf2_per_objfile);
95554aad 8564
d320c2b5
TT
8565 objfile->partial_symtabs->psymtabs_addrmap
8566 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8567 objfile->partial_symtabs->obstack ());
791afaa2
TT
8568 /* At this point we want to keep the address map. */
8569 save_psymtabs_addrmap.release ();
ff013f42 8570
b4f54984 8571 if (dwarf_read_debug)
45cfd468 8572 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8573 objfile_name (objfile));
ae038cb0
DJ
8574}
8575
3019eac3 8576/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8577
8578static void
dee91e82 8579load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8580 const gdb_byte *info_ptr,
dee91e82
DE
8581 struct die_info *comp_unit_die,
8582 int has_children,
8583 void *data)
ae038cb0 8584{
dee91e82 8585 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8586
95554aad 8587 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8588
ae038cb0
DJ
8589 /* Check if comp unit has_children.
8590 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8591 If not, there's no more debug_info for this comp unit. */
d85a05f0 8592 if (has_children)
dee91e82
DE
8593 load_partial_dies (reader, info_ptr, 0);
8594}
98bfdba5 8595
dee91e82
DE
8596/* Load the partial DIEs for a secondary CU into memory.
8597 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8598
dee91e82
DE
8599static void
8600load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8601{
58f0c718 8602 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
f4dc4d17 8603 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8604}
8605
ae038cb0 8606static void
ed2dc618 8607read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8608 struct dwarf2_section_info *section,
f1902523 8609 struct dwarf2_section_info *abbrev_section,
b76e467d 8610 unsigned int is_dwz)
ae038cb0 8611{
d521ce57 8612 const gdb_byte *info_ptr;
ed2dc618 8613 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8614
b4f54984 8615 if (dwarf_read_debug)
bf6af496 8616 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8617 get_section_name (section),
8618 get_section_file_name (section));
bf6af496 8619
36586728 8620 dwarf2_read_section (objfile, section);
ae038cb0 8621
36586728 8622 info_ptr = section->buffer;
6e70227d 8623
36586728 8624 while (info_ptr < section->buffer + section->size)
ae038cb0 8625 {
ae038cb0 8626 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8627
9c541725 8628 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8629
f1902523 8630 comp_unit_head cu_header;
ed2dc618
SM
8631 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8632 abbrev_section, info_ptr,
8633 rcuh_kind::COMPILE);
ae038cb0
DJ
8634
8635 /* Save the compilation unit for later lookup. */
f1902523
JK
8636 if (cu_header.unit_type != DW_UT_type)
8637 {
8638 this_cu = XOBNEW (&objfile->objfile_obstack,
8639 struct dwarf2_per_cu_data);
8640 memset (this_cu, 0, sizeof (*this_cu));
8641 }
8642 else
8643 {
8644 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8645 struct signatured_type);
8646 memset (sig_type, 0, sizeof (*sig_type));
8647 sig_type->signature = cu_header.signature;
8648 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8649 this_cu = &sig_type->per_cu;
8650 }
8651 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8652 this_cu->sect_off = sect_off;
f1902523 8653 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8654 this_cu->is_dwz = is_dwz;
e3b94546 8655 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8656 this_cu->section = section;
ae038cb0 8657
b76e467d 8658 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8659
8660 info_ptr = info_ptr + this_cu->length;
8661 }
36586728
TT
8662}
8663
8664/* Create a list of all compilation units in OBJFILE.
8665 This is only done for -readnow and building partial symtabs. */
8666
8667static void
ed2dc618 8668create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8669{
b76e467d 8670 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 8671 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 8672 &dwarf2_per_objfile->abbrev, 0);
36586728 8673
b76e467d 8674 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8675 if (dwz != NULL)
ed2dc618 8676 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8677 1);
c906108c
SS
8678}
8679
5734ee8b 8680/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8681 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8682 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8683 DW_AT_ranges). See the comments of add_partial_subprogram on how
8684 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8685
72bf9492
DJ
8686static void
8687scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8688 CORE_ADDR *highpc, int set_addrmap,
8689 struct dwarf2_cu *cu)
c906108c 8690{
72bf9492 8691 struct partial_die_info *pdi;
c906108c 8692
91c24f0a
DC
8693 /* Now, march along the PDI's, descending into ones which have
8694 interesting children but skipping the children of the other ones,
8695 until we reach the end of the compilation unit. */
c906108c 8696
72bf9492 8697 pdi = first_die;
91c24f0a 8698
72bf9492
DJ
8699 while (pdi != NULL)
8700 {
52356b79 8701 pdi->fixup (cu);
c906108c 8702
f55ee35c 8703 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8704 children, so we need to look at them. Ditto for anonymous
8705 enums. */
933c6fe4 8706
72bf9492 8707 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8708 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8709 || pdi->tag == DW_TAG_imported_unit
8710 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8711 {
72bf9492 8712 switch (pdi->tag)
c906108c
SS
8713 {
8714 case DW_TAG_subprogram:
b1dc1806 8715 case DW_TAG_inlined_subroutine:
cdc07690 8716 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8717 break;
72929c62 8718 case DW_TAG_constant:
c906108c
SS
8719 case DW_TAG_variable:
8720 case DW_TAG_typedef:
91c24f0a 8721 case DW_TAG_union_type:
72bf9492 8722 if (!pdi->is_declaration)
63d06c5c 8723 {
72bf9492 8724 add_partial_symbol (pdi, cu);
63d06c5c
DC
8725 }
8726 break;
c906108c 8727 case DW_TAG_class_type:
680b30c7 8728 case DW_TAG_interface_type:
c906108c 8729 case DW_TAG_structure_type:
72bf9492 8730 if (!pdi->is_declaration)
c906108c 8731 {
72bf9492 8732 add_partial_symbol (pdi, cu);
c906108c 8733 }
b7fee5a3
KS
8734 if ((cu->language == language_rust
8735 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8736 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8737 set_addrmap, cu);
c906108c 8738 break;
91c24f0a 8739 case DW_TAG_enumeration_type:
72bf9492
DJ
8740 if (!pdi->is_declaration)
8741 add_partial_enumeration (pdi, cu);
c906108c
SS
8742 break;
8743 case DW_TAG_base_type:
a02abb62 8744 case DW_TAG_subrange_type:
c906108c 8745 /* File scope base type definitions are added to the partial
c5aa993b 8746 symbol table. */
72bf9492 8747 add_partial_symbol (pdi, cu);
c906108c 8748 break;
d9fa45fe 8749 case DW_TAG_namespace:
cdc07690 8750 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8751 break;
5d7cb8df 8752 case DW_TAG_module:
59c35742
AB
8753 if (!pdi->is_declaration)
8754 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8755 break;
95554aad
TT
8756 case DW_TAG_imported_unit:
8757 {
8758 struct dwarf2_per_cu_data *per_cu;
8759
f4dc4d17
DE
8760 /* For now we don't handle imported units in type units. */
8761 if (cu->per_cu->is_debug_types)
8762 {
8763 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8764 " supported in type units [in module %s]"),
518817b3 8765 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8766 }
8767
e3b94546
SM
8768 per_cu = dwarf2_find_containing_comp_unit
8769 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8770 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8771
8772 /* Go read the partial unit, if needed. */
8773 if (per_cu->v.psymtab == NULL)
b93601f3 8774 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8775
ae640021 8776 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8777 }
8778 break;
74921315
KS
8779 case DW_TAG_imported_declaration:
8780 add_partial_symbol (pdi, cu);
8781 break;
c906108c
SS
8782 default:
8783 break;
8784 }
8785 }
8786
72bf9492
DJ
8787 /* If the die has a sibling, skip to the sibling. */
8788
8789 pdi = pdi->die_sibling;
8790 }
8791}
8792
8793/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8794
72bf9492 8795 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8796 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8797 Enumerators are an exception; they use the scope of their parent
8798 enumeration type, i.e. the name of the enumeration type is not
8799 prepended to the enumerator.
91c24f0a 8800
72bf9492
DJ
8801 There are two complexities. One is DW_AT_specification; in this
8802 case "parent" means the parent of the target of the specification,
8803 instead of the direct parent of the DIE. The other is compilers
8804 which do not emit DW_TAG_namespace; in this case we try to guess
8805 the fully qualified name of structure types from their members'
8806 linkage names. This must be done using the DIE's children rather
8807 than the children of any DW_AT_specification target. We only need
8808 to do this for structures at the top level, i.e. if the target of
8809 any DW_AT_specification (if any; otherwise the DIE itself) does not
8810 have a parent. */
8811
8812/* Compute the scope prefix associated with PDI's parent, in
8813 compilation unit CU. The result will be allocated on CU's
8814 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8815 field. NULL is returned if no prefix is necessary. */
15d034d0 8816static const char *
72bf9492
DJ
8817partial_die_parent_scope (struct partial_die_info *pdi,
8818 struct dwarf2_cu *cu)
8819{
15d034d0 8820 const char *grandparent_scope;
72bf9492 8821 struct partial_die_info *parent, *real_pdi;
91c24f0a 8822
72bf9492
DJ
8823 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8824 then this means the parent of the specification DIE. */
8825
8826 real_pdi = pdi;
72bf9492 8827 while (real_pdi->has_specification)
fb816e8b 8828 {
122cf0f2
AB
8829 auto res = find_partial_die (real_pdi->spec_offset,
8830 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8831 real_pdi = res.pdi;
8832 cu = res.cu;
8833 }
72bf9492
DJ
8834
8835 parent = real_pdi->die_parent;
8836 if (parent == NULL)
8837 return NULL;
8838
8839 if (parent->scope_set)
8840 return parent->scope;
8841
52356b79 8842 parent->fixup (cu);
72bf9492 8843
10b3939b 8844 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8845
acebe513
UW
8846 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8847 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8848 Work around this problem here. */
8849 if (cu->language == language_cplus
6e70227d 8850 && parent->tag == DW_TAG_namespace
acebe513
UW
8851 && strcmp (parent->name, "::") == 0
8852 && grandparent_scope == NULL)
8853 {
8854 parent->scope = NULL;
8855 parent->scope_set = 1;
8856 return NULL;
8857 }
8858
0a4b0913 8859 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8860 if (pdi->tag == DW_TAG_enumerator)
8861 /* Enumerators should not get the name of the enumeration as a prefix. */
8862 parent->scope = grandparent_scope;
8863 else if (parent->tag == DW_TAG_namespace
f55ee35c 8864 || parent->tag == DW_TAG_module
72bf9492
DJ
8865 || parent->tag == DW_TAG_structure_type
8866 || parent->tag == DW_TAG_class_type
680b30c7 8867 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8868 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8869 || parent->tag == DW_TAG_enumeration_type
8870 || (cu->language == language_fortran
8871 && parent->tag == DW_TAG_subprogram
8872 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8873 {
8874 if (grandparent_scope == NULL)
8875 parent->scope = parent->name;
8876 else
3e43a32a
MS
8877 parent->scope = typename_concat (&cu->comp_unit_obstack,
8878 grandparent_scope,
f55ee35c 8879 parent->name, 0, cu);
72bf9492 8880 }
72bf9492
DJ
8881 else
8882 {
8883 /* FIXME drow/2004-04-01: What should we be doing with
8884 function-local names? For partial symbols, we should probably be
8885 ignoring them. */
fa9c3fa0
TT
8886 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8887 dwarf_tag_name (parent->tag),
8888 sect_offset_str (pdi->sect_off));
72bf9492 8889 parent->scope = grandparent_scope;
c906108c
SS
8890 }
8891
72bf9492
DJ
8892 parent->scope_set = 1;
8893 return parent->scope;
8894}
8895
8896/* Return the fully scoped name associated with PDI, from compilation unit
8897 CU. The result will be allocated with malloc. */
4568ecf9 8898
43816ebc 8899static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8900partial_die_full_name (struct partial_die_info *pdi,
8901 struct dwarf2_cu *cu)
8902{
15d034d0 8903 const char *parent_scope;
72bf9492 8904
98bfdba5
PA
8905 /* If this is a template instantiation, we can not work out the
8906 template arguments from partial DIEs. So, unfortunately, we have
8907 to go through the full DIEs. At least any work we do building
8908 types here will be reused if full symbols are loaded later. */
8909 if (pdi->has_template_arguments)
8910 {
52356b79 8911 pdi->fixup (cu);
98bfdba5
PA
8912
8913 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8914 {
8915 struct die_info *die;
8916 struct attribute attr;
8917 struct dwarf2_cu *ref_cu = cu;
8918
b64f50a1 8919 /* DW_FORM_ref_addr is using section offset. */
b4069958 8920 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8921 attr.form = DW_FORM_ref_addr;
9c541725 8922 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8923 die = follow_die_ref (NULL, &attr, &ref_cu);
8924
43816ebc 8925 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8926 }
8927 }
8928
72bf9492
DJ
8929 parent_scope = partial_die_parent_scope (pdi, cu);
8930 if (parent_scope == NULL)
8931 return NULL;
8932 else
43816ebc
TT
8933 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8934 pdi->name, 0, cu));
c906108c
SS
8935}
8936
8937static void
72bf9492 8938add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8939{
518817b3
SM
8940 struct dwarf2_per_objfile *dwarf2_per_objfile
8941 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8942 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8943 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8944 CORE_ADDR addr = 0;
15d034d0 8945 const char *actual_name = NULL;
e142c38c
DJ
8946 CORE_ADDR baseaddr;
8947
6a053cb1 8948 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
c906108c 8949
43816ebc
TT
8950 gdb::unique_xmalloc_ptr<char> built_actual_name
8951 = partial_die_full_name (pdi, cu);
15d034d0 8952 if (built_actual_name != NULL)
43816ebc 8953 actual_name = built_actual_name.get ();
63d06c5c 8954
72bf9492
DJ
8955 if (actual_name == NULL)
8956 actual_name = pdi->name;
8957
c906108c
SS
8958 switch (pdi->tag)
8959 {
b1dc1806 8960 case DW_TAG_inlined_subroutine:
c906108c 8961 case DW_TAG_subprogram:
79748972
TT
8962 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8963 - baseaddr);
0a4b0913
AB
8964 if (pdi->is_external
8965 || cu->language == language_ada
8966 || (cu->language == language_fortran
8967 && pdi->die_parent != NULL
8968 && pdi->die_parent->tag == DW_TAG_subprogram))
8969 {
8970 /* Normally, only "external" DIEs are part of the global scope.
8971 But in Ada and Fortran, we want to be able to access nested
8972 procedures globally. So all Ada and Fortran subprograms are
8973 stored in the global scope. */
31edb802 8974 add_psymbol_to_list (actual_name,
15d034d0 8975 built_actual_name != NULL,
f47fb265 8976 VAR_DOMAIN, LOC_BLOCK,
79748972 8977 SECT_OFF_TEXT (objfile),
75aedd27 8978 psymbol_placement::GLOBAL,
79748972
TT
8979 addr,
8980 cu->language, objfile);
c906108c
SS
8981 }
8982 else
8983 {
31edb802 8984 add_psymbol_to_list (actual_name,
15d034d0 8985 built_actual_name != NULL,
f47fb265 8986 VAR_DOMAIN, LOC_BLOCK,
79748972 8987 SECT_OFF_TEXT (objfile),
75aedd27 8988 psymbol_placement::STATIC,
1762568f 8989 addr, cu->language, objfile);
c906108c 8990 }
0c1b455e
TT
8991
8992 if (pdi->main_subprogram && actual_name != NULL)
8993 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8994 break;
72929c62 8995 case DW_TAG_constant:
31edb802 8996 add_psymbol_to_list (actual_name,
75aedd27
TT
8997 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8998 -1, (pdi->is_external
8999 ? psymbol_placement::GLOBAL
9000 : psymbol_placement::STATIC),
9001 0, cu->language, objfile);
72929c62 9002 break;
c906108c 9003 case DW_TAG_variable:
95554aad
TT
9004 if (pdi->d.locdesc)
9005 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 9006
95554aad 9007 if (pdi->d.locdesc
caac4577
JG
9008 && addr == 0
9009 && !dwarf2_per_objfile->has_section_at_zero)
9010 {
9011 /* A global or static variable may also have been stripped
9012 out by the linker if unused, in which case its address
9013 will be nullified; do not add such variables into partial
9014 symbol table then. */
9015 }
9016 else if (pdi->is_external)
c906108c
SS
9017 {
9018 /* Global Variable.
9019 Don't enter into the minimal symbol tables as there is
9020 a minimal symbol table entry from the ELF symbols already.
9021 Enter into partial symbol table if it has a location
9022 descriptor or a type.
9023 If the location descriptor is missing, new_symbol will create
9024 a LOC_UNRESOLVED symbol, the address of the variable will then
9025 be determined from the minimal symbol table whenever the variable
9026 is referenced.
9027 The address for the partial symbol table entry is not
9028 used by GDB, but it comes in handy for debugging partial symbol
9029 table building. */
9030
95554aad 9031 if (pdi->d.locdesc || pdi->has_type)
31edb802 9032 add_psymbol_to_list (actual_name,
15d034d0 9033 built_actual_name != NULL,
f47fb265 9034 VAR_DOMAIN, LOC_STATIC,
79748972 9035 SECT_OFF_TEXT (objfile),
75aedd27 9036 psymbol_placement::GLOBAL,
79748972 9037 addr, cu->language, objfile);
c906108c
SS
9038 }
9039 else
9040 {
ff908ebf
AW
9041 int has_loc = pdi->d.locdesc != NULL;
9042
9043 /* Static Variable. Skip symbols whose value we cannot know (those
9044 without location descriptors or constant values). */
9045 if (!has_loc && !pdi->has_const_value)
43816ebc 9046 return;
ff908ebf 9047
31edb802 9048 add_psymbol_to_list (actual_name,
15d034d0 9049 built_actual_name != NULL,
f47fb265 9050 VAR_DOMAIN, LOC_STATIC,
79748972 9051 SECT_OFF_TEXT (objfile),
75aedd27 9052 psymbol_placement::STATIC,
79748972 9053 has_loc ? addr : 0,
f47fb265 9054 cu->language, objfile);
c906108c
SS
9055 }
9056 break;
9057 case DW_TAG_typedef:
9058 case DW_TAG_base_type:
a02abb62 9059 case DW_TAG_subrange_type:
31edb802 9060 add_psymbol_to_list (actual_name,
15d034d0 9061 built_actual_name != NULL,
79748972 9062 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9063 psymbol_placement::STATIC,
1762568f 9064 0, cu->language, objfile);
c906108c 9065 break;
74921315 9066 case DW_TAG_imported_declaration:
72bf9492 9067 case DW_TAG_namespace:
31edb802 9068 add_psymbol_to_list (actual_name,
15d034d0 9069 built_actual_name != NULL,
79748972 9070 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9071 psymbol_placement::GLOBAL,
1762568f 9072 0, cu->language, objfile);
72bf9492 9073 break;
530e8392 9074 case DW_TAG_module:
a5fd13a9
BH
9075 /* With Fortran 77 there might be a "BLOCK DATA" module
9076 available without any name. If so, we skip the module as it
9077 doesn't bring any value. */
9078 if (actual_name != nullptr)
31edb802 9079 add_psymbol_to_list (actual_name,
a5fd13a9
BH
9080 built_actual_name != NULL,
9081 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9082 psymbol_placement::GLOBAL,
9083 0, cu->language, objfile);
530e8392 9084 break;
c906108c 9085 case DW_TAG_class_type:
680b30c7 9086 case DW_TAG_interface_type:
c906108c
SS
9087 case DW_TAG_structure_type:
9088 case DW_TAG_union_type:
9089 case DW_TAG_enumeration_type:
fa4028e9
JB
9090 /* Skip external references. The DWARF standard says in the section
9091 about "Structure, Union, and Class Type Entries": "An incomplete
9092 structure, union or class type is represented by a structure,
9093 union or class entry that does not have a byte size attribute
9094 and that has a DW_AT_declaration attribute." */
9095 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 9096 return;
fa4028e9 9097
63d06c5c
DC
9098 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9099 static vs. global. */
31edb802 9100 add_psymbol_to_list (actual_name,
15d034d0 9101 built_actual_name != NULL,
79748972 9102 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 9103 cu->language == language_cplus
75aedd27
TT
9104 ? psymbol_placement::GLOBAL
9105 : psymbol_placement::STATIC,
1762568f 9106 0, cu->language, objfile);
c906108c 9107
c906108c
SS
9108 break;
9109 case DW_TAG_enumerator:
31edb802 9110 add_psymbol_to_list (actual_name,
15d034d0 9111 built_actual_name != NULL,
79748972 9112 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 9113 cu->language == language_cplus
75aedd27
TT
9114 ? psymbol_placement::GLOBAL
9115 : psymbol_placement::STATIC,
1762568f 9116 0, cu->language, objfile);
c906108c
SS
9117 break;
9118 default:
9119 break;
9120 }
9121}
9122
5c4e30ca
DC
9123/* Read a partial die corresponding to a namespace; also, add a symbol
9124 corresponding to that namespace to the symbol table. NAMESPACE is
9125 the name of the enclosing namespace. */
91c24f0a 9126
72bf9492
DJ
9127static void
9128add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 9129 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9130 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 9131{
72bf9492 9132 /* Add a symbol for the namespace. */
e7c27a73 9133
72bf9492 9134 add_partial_symbol (pdi, cu);
5c4e30ca
DC
9135
9136 /* Now scan partial symbols in that namespace. */
9137
91c24f0a 9138 if (pdi->has_children)
cdc07690 9139 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
9140}
9141
5d7cb8df
JK
9142/* Read a partial die corresponding to a Fortran module. */
9143
9144static void
9145add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9146 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9147{
530e8392
KB
9148 /* Add a symbol for the namespace. */
9149
9150 add_partial_symbol (pdi, cu);
9151
f55ee35c 9152 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9153
9154 if (pdi->has_children)
cdc07690 9155 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9156}
9157
b1dc1806
XR
9158/* Read a partial die corresponding to a subprogram or an inlined
9159 subprogram and create a partial symbol for that subprogram.
9160 When the CU language allows it, this routine also defines a partial
9161 symbol for each nested subprogram that this subprogram contains.
9162 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9163 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9164
cdc07690
YQ
9165 PDI may also be a lexical block, in which case we simply search
9166 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9167 Again, this is only performed when the CU language allows this
9168 type of definitions. */
9169
9170static void
9171add_partial_subprogram (struct partial_die_info *pdi,
9172 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9173 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9174{
b1dc1806 9175 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9176 {
9177 if (pdi->has_pc_info)
9178 {
9179 if (pdi->lowpc < *lowpc)
9180 *lowpc = pdi->lowpc;
9181 if (pdi->highpc > *highpc)
9182 *highpc = pdi->highpc;
cdc07690 9183 if (set_addrmap)
5734ee8b 9184 {
518817b3 9185 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9186 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9187 CORE_ADDR baseaddr;
b926417a
TT
9188 CORE_ADDR this_highpc;
9189 CORE_ADDR this_lowpc;
5734ee8b 9190
6a053cb1 9191 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
b926417a
TT
9192 this_lowpc
9193 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9194 pdi->lowpc + baseaddr)
9195 - baseaddr);
9196 this_highpc
9197 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9198 pdi->highpc + baseaddr)
9199 - baseaddr);
d320c2b5 9200 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 9201 this_lowpc, this_highpc - 1,
9291a0cd 9202 cu->per_cu->v.psymtab);
5734ee8b 9203 }
481860b3
GB
9204 }
9205
9206 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9207 {
bc30ff58 9208 if (!pdi->is_declaration)
e8d05480
JB
9209 /* Ignore subprogram DIEs that do not have a name, they are
9210 illegal. Do not emit a complaint at this point, we will
9211 do so when we convert this psymtab into a symtab. */
9212 if (pdi->name)
9213 add_partial_symbol (pdi, cu);
bc30ff58
JB
9214 }
9215 }
6e70227d 9216
bc30ff58
JB
9217 if (! pdi->has_children)
9218 return;
9219
0a4b0913 9220 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
9221 {
9222 pdi = pdi->die_child;
9223 while (pdi != NULL)
9224 {
52356b79 9225 pdi->fixup (cu);
bc30ff58 9226 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9227 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9228 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9229 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9230 pdi = pdi->die_sibling;
9231 }
9232 }
9233}
9234
91c24f0a
DC
9235/* Read a partial die corresponding to an enumeration type. */
9236
72bf9492
DJ
9237static void
9238add_partial_enumeration (struct partial_die_info *enum_pdi,
9239 struct dwarf2_cu *cu)
91c24f0a 9240{
72bf9492 9241 struct partial_die_info *pdi;
91c24f0a
DC
9242
9243 if (enum_pdi->name != NULL)
72bf9492
DJ
9244 add_partial_symbol (enum_pdi, cu);
9245
9246 pdi = enum_pdi->die_child;
9247 while (pdi)
91c24f0a 9248 {
72bf9492 9249 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 9250 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 9251 else
72bf9492
DJ
9252 add_partial_symbol (pdi, cu);
9253 pdi = pdi->die_sibling;
91c24f0a 9254 }
91c24f0a
DC
9255}
9256
6caca83c
CC
9257/* Return the initial uleb128 in the die at INFO_PTR. */
9258
9259static unsigned int
d521ce57 9260peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9261{
9262 unsigned int bytes_read;
9263
9264 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9265}
9266
685af9cd
TT
9267/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9268 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9269
4bb7a0a7
DJ
9270 Return the corresponding abbrev, or NULL if the number is zero (indicating
9271 an empty DIE). In either case *BYTES_READ will be set to the length of
9272 the initial number. */
9273
9274static struct abbrev_info *
685af9cd
TT
9275peek_die_abbrev (const die_reader_specs &reader,
9276 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9277{
685af9cd 9278 dwarf2_cu *cu = reader.cu;
518817b3 9279 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9280 unsigned int abbrev_number
9281 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9282
9283 if (abbrev_number == 0)
9284 return NULL;
9285
685af9cd 9286 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9287 if (!abbrev)
9288 {
422b9917 9289 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 9290 " at offset %s [in module %s]"),
422b9917 9291 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 9292 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9293 }
9294
9295 return abbrev;
9296}
9297
93311388
DE
9298/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9299 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9300 DIE. Any children of the skipped DIEs will also be skipped. */
9301
d521ce57
TT
9302static const gdb_byte *
9303skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9304{
4bb7a0a7
DJ
9305 while (1)
9306 {
685af9cd
TT
9307 unsigned int bytes_read;
9308 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9309
4bb7a0a7
DJ
9310 if (abbrev == NULL)
9311 return info_ptr + bytes_read;
9312 else
dee91e82 9313 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9314 }
9315}
9316
93311388
DE
9317/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9318 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9319 abbrev corresponding to that skipped uleb128 should be passed in
9320 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9321 children. */
9322
d521ce57
TT
9323static const gdb_byte *
9324skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9325 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9326{
9327 unsigned int bytes_read;
9328 struct attribute attr;
dee91e82
DE
9329 bfd *abfd = reader->abfd;
9330 struct dwarf2_cu *cu = reader->cu;
d521ce57 9331 const gdb_byte *buffer = reader->buffer;
f664829e 9332 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9333 unsigned int form, i;
9334
9335 for (i = 0; i < abbrev->num_attrs; i++)
9336 {
9337 /* The only abbrev we care about is DW_AT_sibling. */
9338 if (abbrev->attrs[i].name == DW_AT_sibling)
9339 {
dee91e82 9340 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9341 if (attr.form == DW_FORM_ref_addr)
b98664d3 9342 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9343 else
b9502d3f 9344 {
9c541725
PA
9345 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9346 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9347
9348 if (sibling_ptr < info_ptr)
b98664d3 9349 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
9350 else if (sibling_ptr > reader->buffer_end)
9351 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9352 else
9353 return sibling_ptr;
9354 }
4bb7a0a7
DJ
9355 }
9356
9357 /* If it isn't DW_AT_sibling, skip this attribute. */
9358 form = abbrev->attrs[i].form;
9359 skip_attribute:
9360 switch (form)
9361 {
4bb7a0a7 9362 case DW_FORM_ref_addr:
ae411497
TT
9363 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9364 and later it is offset sized. */
9365 if (cu->header.version == 2)
9366 info_ptr += cu->header.addr_size;
9367 else
9368 info_ptr += cu->header.offset_size;
9369 break;
36586728
TT
9370 case DW_FORM_GNU_ref_alt:
9371 info_ptr += cu->header.offset_size;
9372 break;
ae411497 9373 case DW_FORM_addr:
4bb7a0a7
DJ
9374 info_ptr += cu->header.addr_size;
9375 break;
9376 case DW_FORM_data1:
9377 case DW_FORM_ref1:
9378 case DW_FORM_flag:
8fe0f950 9379 case DW_FORM_strx1:
4bb7a0a7
DJ
9380 info_ptr += 1;
9381 break;
2dc7f7b3 9382 case DW_FORM_flag_present:
43988095 9383 case DW_FORM_implicit_const:
2dc7f7b3 9384 break;
4bb7a0a7
DJ
9385 case DW_FORM_data2:
9386 case DW_FORM_ref2:
8fe0f950 9387 case DW_FORM_strx2:
4bb7a0a7
DJ
9388 info_ptr += 2;
9389 break;
8fe0f950
AT
9390 case DW_FORM_strx3:
9391 info_ptr += 3;
9392 break;
4bb7a0a7
DJ
9393 case DW_FORM_data4:
9394 case DW_FORM_ref4:
8fe0f950 9395 case DW_FORM_strx4:
4bb7a0a7
DJ
9396 info_ptr += 4;
9397 break;
9398 case DW_FORM_data8:
9399 case DW_FORM_ref8:
55f1336d 9400 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9401 info_ptr += 8;
9402 break;
0224619f
JK
9403 case DW_FORM_data16:
9404 info_ptr += 16;
9405 break;
4bb7a0a7 9406 case DW_FORM_string:
9b1c24c8 9407 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9408 info_ptr += bytes_read;
9409 break;
2dc7f7b3 9410 case DW_FORM_sec_offset:
4bb7a0a7 9411 case DW_FORM_strp:
36586728 9412 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9413 info_ptr += cu->header.offset_size;
9414 break;
2dc7f7b3 9415 case DW_FORM_exprloc:
4bb7a0a7
DJ
9416 case DW_FORM_block:
9417 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9418 info_ptr += bytes_read;
9419 break;
9420 case DW_FORM_block1:
9421 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9422 break;
9423 case DW_FORM_block2:
9424 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9425 break;
9426 case DW_FORM_block4:
9427 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9428 break;
336d760d 9429 case DW_FORM_addrx:
cf532bd1 9430 case DW_FORM_strx:
4bb7a0a7
DJ
9431 case DW_FORM_sdata:
9432 case DW_FORM_udata:
9433 case DW_FORM_ref_udata:
3019eac3
DE
9434 case DW_FORM_GNU_addr_index:
9435 case DW_FORM_GNU_str_index:
d521ce57 9436 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9437 break;
9438 case DW_FORM_indirect:
9439 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9440 info_ptr += bytes_read;
9441 /* We need to continue parsing from here, so just go back to
9442 the top. */
9443 goto skip_attribute;
9444
9445 default:
3e43a32a
MS
9446 error (_("Dwarf Error: Cannot handle %s "
9447 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9448 dwarf_form_name (form),
9449 bfd_get_filename (abfd));
9450 }
9451 }
9452
9453 if (abbrev->has_children)
dee91e82 9454 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9455 else
9456 return info_ptr;
9457}
9458
93311388 9459/* Locate ORIG_PDI's sibling.
dee91e82 9460 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9461
d521ce57 9462static const gdb_byte *
dee91e82
DE
9463locate_pdi_sibling (const struct die_reader_specs *reader,
9464 struct partial_die_info *orig_pdi,
d521ce57 9465 const gdb_byte *info_ptr)
91c24f0a
DC
9466{
9467 /* Do we know the sibling already? */
72bf9492 9468
91c24f0a
DC
9469 if (orig_pdi->sibling)
9470 return orig_pdi->sibling;
9471
9472 /* Are there any children to deal with? */
9473
9474 if (!orig_pdi->has_children)
9475 return info_ptr;
9476
4bb7a0a7 9477 /* Skip the children the long way. */
91c24f0a 9478
dee91e82 9479 return skip_children (reader, info_ptr);
91c24f0a
DC
9480}
9481
257e7a09 9482/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9483 not NULL. */
c906108c
SS
9484
9485static void
257e7a09
YQ
9486dwarf2_read_symtab (struct partial_symtab *self,
9487 struct objfile *objfile)
c906108c 9488{
ed2dc618
SM
9489 struct dwarf2_per_objfile *dwarf2_per_objfile
9490 = get_dwarf2_per_objfile (objfile);
9491
257e7a09 9492 if (self->readin)
c906108c 9493 {
442e4d9c 9494 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9495 self->filename);
442e4d9c
YQ
9496 }
9497 else
9498 {
9499 if (info_verbose)
c906108c 9500 {
442e4d9c 9501 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9502 self->filename);
442e4d9c 9503 gdb_flush (gdb_stdout);
c906108c 9504 }
c906108c 9505
442e4d9c
YQ
9506 /* If this psymtab is constructed from a debug-only objfile, the
9507 has_section_at_zero flag will not necessarily be correct. We
9508 can get the correct value for this flag by looking at the data
9509 associated with the (presumably stripped) associated objfile. */
9510 if (objfile->separate_debug_objfile_backlink)
9511 {
9512 struct dwarf2_per_objfile *dpo_backlink
ed2dc618 9513 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9a619af0 9514
442e4d9c
YQ
9515 dwarf2_per_objfile->has_section_at_zero
9516 = dpo_backlink->has_section_at_zero;
9517 }
b2ab525c 9518
442e4d9c 9519 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9520
257e7a09 9521 psymtab_to_symtab_1 (self);
c906108c 9522
442e4d9c
YQ
9523 /* Finish up the debug error message. */
9524 if (info_verbose)
9525 printf_filtered (_("done.\n"));
c906108c 9526 }
95554aad 9527
ed2dc618 9528 process_cu_includes (dwarf2_per_objfile);
c906108c 9529}
9cdd5dbd
DE
9530\f
9531/* Reading in full CUs. */
c906108c 9532
10b3939b
DJ
9533/* Add PER_CU to the queue. */
9534
9535static void
95554aad
TT
9536queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9537 enum language pretend_language)
10b3939b
DJ
9538{
9539 struct dwarf2_queue_item *item;
9540
9541 per_cu->queued = 1;
8d749320 9542 item = XNEW (struct dwarf2_queue_item);
10b3939b 9543 item->per_cu = per_cu;
95554aad 9544 item->pretend_language = pretend_language;
10b3939b
DJ
9545 item->next = NULL;
9546
9547 if (dwarf2_queue == NULL)
9548 dwarf2_queue = item;
9549 else
9550 dwarf2_queue_tail->next = item;
9551
9552 dwarf2_queue_tail = item;
9553}
9554
89e63ee4
DE
9555/* If PER_CU is not yet queued, add it to the queue.
9556 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9557 dependency.
0907af0c 9558 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9559 meaning either PER_CU is already queued or it is already loaded.
9560
9561 N.B. There is an invariant here that if a CU is queued then it is loaded.
9562 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9563
9564static int
89e63ee4 9565maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9566 struct dwarf2_per_cu_data *per_cu,
9567 enum language pretend_language)
9568{
9569 /* We may arrive here during partial symbol reading, if we need full
9570 DIEs to process an unusual case (e.g. template arguments). Do
9571 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9572 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9573 {
9574 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9575 return 1;
9576 return 0;
9577 }
9578
9579 /* Mark the dependence relation so that we don't flush PER_CU
9580 too early. */
89e63ee4
DE
9581 if (dependent_cu != NULL)
9582 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9583
9584 /* If it's already on the queue, we have nothing to do. */
9585 if (per_cu->queued)
9586 return 0;
9587
9588 /* If the compilation unit is already loaded, just mark it as
9589 used. */
9590 if (per_cu->cu != NULL)
9591 {
9592 per_cu->cu->last_used = 0;
9593 return 0;
9594 }
9595
9596 /* Add it to the queue. */
9597 queue_comp_unit (per_cu, pretend_language);
9598
9599 return 1;
9600}
9601
10b3939b
DJ
9602/* Process the queue. */
9603
9604static void
ed2dc618 9605process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9606{
9607 struct dwarf2_queue_item *item, *next_item;
9608
b4f54984 9609 if (dwarf_read_debug)
45cfd468
DE
9610 {
9611 fprintf_unfiltered (gdb_stdlog,
9612 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9613 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9614 }
9615
03dd20cc
DJ
9616 /* The queue starts out with one item, but following a DIE reference
9617 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9618 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9619 {
cc12ce38
DE
9620 if ((dwarf2_per_objfile->using_index
9621 ? !item->per_cu->v.quick->compunit_symtab
9622 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9623 /* Skip dummy CUs. */
9624 && item->per_cu->cu != NULL)
f4dc4d17
DE
9625 {
9626 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9627 unsigned int debug_print_threshold;
247f5c4f 9628 char buf[100];
f4dc4d17 9629
247f5c4f 9630 if (per_cu->is_debug_types)
f4dc4d17 9631 {
247f5c4f
DE
9632 struct signatured_type *sig_type =
9633 (struct signatured_type *) per_cu;
9634
9d8780f0 9635 sprintf (buf, "TU %s at offset %s",
73be47f5 9636 hex_string (sig_type->signature),
9d8780f0 9637 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9638 /* There can be 100s of TUs.
9639 Only print them in verbose mode. */
9640 debug_print_threshold = 2;
f4dc4d17 9641 }
247f5c4f 9642 else
73be47f5 9643 {
9d8780f0
SM
9644 sprintf (buf, "CU at offset %s",
9645 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9646 debug_print_threshold = 1;
9647 }
247f5c4f 9648
b4f54984 9649 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9650 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9651
9652 if (per_cu->is_debug_types)
9653 process_full_type_unit (per_cu, item->pretend_language);
9654 else
9655 process_full_comp_unit (per_cu, item->pretend_language);
9656
b4f54984 9657 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9658 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9659 }
10b3939b
DJ
9660
9661 item->per_cu->queued = 0;
9662 next_item = item->next;
9663 xfree (item);
9664 }
9665
9666 dwarf2_queue_tail = NULL;
45cfd468 9667
b4f54984 9668 if (dwarf_read_debug)
45cfd468
DE
9669 {
9670 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9671 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9672 }
10b3939b
DJ
9673}
9674
10b3939b
DJ
9675/* Read in full symbols for PST, and anything it depends on. */
9676
c906108c 9677static void
fba45db2 9678psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 9679{
10b3939b 9680 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
9681 int i;
9682
95554aad
TT
9683 if (pst->readin)
9684 return;
9685
aaa75496 9686 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
9687 if (!pst->dependencies[i]->readin
9688 && pst->dependencies[i]->user == NULL)
aaa75496
JB
9689 {
9690 /* Inform about additional files that need to be read in. */
9691 if (info_verbose)
9692 {
a3f17187 9693 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
9694 fputs_filtered (" ", gdb_stdout);
9695 wrap_here ("");
9696 fputs_filtered ("and ", gdb_stdout);
9697 wrap_here ("");
9698 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 9699 wrap_here (""); /* Flush output. */
aaa75496
JB
9700 gdb_flush (gdb_stdout);
9701 }
9702 psymtab_to_symtab_1 (pst->dependencies[i]);
9703 }
9704
9a3c8263 9705 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
9706
9707 if (per_cu == NULL)
aaa75496
JB
9708 {
9709 /* It's an include file, no symbols to read for it.
9710 Everything is in the parent symtab. */
9711 pst->readin = 1;
9712 return;
9713 }
c906108c 9714
58f0c718 9715 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
9716}
9717
dee91e82
DE
9718/* Trivial hash function for die_info: the hash value of a DIE
9719 is its offset in .debug_info for this objfile. */
10b3939b 9720
dee91e82
DE
9721static hashval_t
9722die_hash (const void *item)
10b3939b 9723{
9a3c8263 9724 const struct die_info *die = (const struct die_info *) item;
6502dd73 9725
9c541725 9726 return to_underlying (die->sect_off);
dee91e82 9727}
63d06c5c 9728
dee91e82
DE
9729/* Trivial comparison function for die_info structures: two DIEs
9730 are equal if they have the same offset. */
98bfdba5 9731
dee91e82
DE
9732static int
9733die_eq (const void *item_lhs, const void *item_rhs)
9734{
9a3c8263
SM
9735 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9736 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9737
9c541725 9738 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9739}
c906108c 9740
dee91e82
DE
9741/* die_reader_func for load_full_comp_unit.
9742 This is identical to read_signatured_type_reader,
9743 but is kept separate for now. */
c906108c 9744
dee91e82
DE
9745static void
9746load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 9747 const gdb_byte *info_ptr,
dee91e82
DE
9748 struct die_info *comp_unit_die,
9749 int has_children,
9750 void *data)
9751{
9752 struct dwarf2_cu *cu = reader->cu;
9a3c8263 9753 enum language *language_ptr = (enum language *) data;
6caca83c 9754
dee91e82
DE
9755 gdb_assert (cu->die_hash == NULL);
9756 cu->die_hash =
9757 htab_create_alloc_ex (cu->header.length / 12,
9758 die_hash,
9759 die_eq,
9760 NULL,
9761 &cu->comp_unit_obstack,
9762 hashtab_obstack_allocate,
9763 dummy_obstack_deallocate);
e142c38c 9764
dee91e82
DE
9765 if (has_children)
9766 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9767 &info_ptr, comp_unit_die);
9768 cu->dies = comp_unit_die;
9769 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9770
9771 /* We try not to read any attributes in this function, because not
9cdd5dbd 9772 all CUs needed for references have been loaded yet, and symbol
10b3939b 9773 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9774 or we won't be able to build types correctly.
9775 Similarly, if we do not read the producer, we can not apply
9776 producer-specific interpretation. */
95554aad 9777 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 9778}
10b3939b 9779
dee91e82 9780/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 9781
dee91e82 9782static void
95554aad 9783load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
58f0c718 9784 bool skip_partial,
95554aad 9785 enum language pretend_language)
dee91e82 9786{
3019eac3 9787 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 9788
58f0c718 9789 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
f4dc4d17 9790 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
9791}
9792
3da10d80
KS
9793/* Add a DIE to the delayed physname list. */
9794
9795static void
9796add_to_method_list (struct type *type, int fnfield_index, int index,
9797 const char *name, struct die_info *die,
9798 struct dwarf2_cu *cu)
9799{
9800 struct delayed_method_info mi;
9801 mi.type = type;
9802 mi.fnfield_index = fnfield_index;
9803 mi.index = index;
9804 mi.name = name;
9805 mi.die = die;
c89b44cd 9806 cu->method_list.push_back (mi);
3da10d80
KS
9807}
9808
3693fdb3
PA
9809/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9810 "const" / "volatile". If so, decrements LEN by the length of the
9811 modifier and return true. Otherwise return false. */
9812
9813template<size_t N>
9814static bool
9815check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9816{
9817 size_t mod_len = sizeof (mod) - 1;
9818 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9819 {
9820 len -= mod_len;
9821 return true;
9822 }
9823 return false;
9824}
9825
3da10d80
KS
9826/* Compute the physnames of any methods on the CU's method list.
9827
9828 The computation of method physnames is delayed in order to avoid the
9829 (bad) condition that one of the method's formal parameters is of an as yet
9830 incomplete type. */
9831
9832static void
9833compute_delayed_physnames (struct dwarf2_cu *cu)
9834{
3693fdb3 9835 /* Only C++ delays computing physnames. */
c89b44cd 9836 if (cu->method_list.empty ())
3693fdb3
PA
9837 return;
9838 gdb_assert (cu->language == language_cplus);
9839
52941706 9840 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9841 {
1d06ead6 9842 const char *physname;
3da10d80 9843 struct fn_fieldlist *fn_flp
c89b44cd
TT
9844 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9845 physname = dwarf2_physname (mi.name, mi.die, cu);
9846 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9847 = physname ? physname : "";
3693fdb3
PA
9848
9849 /* Since there's no tag to indicate whether a method is a
9850 const/volatile overload, extract that information out of the
9851 demangled name. */
9852 if (physname != NULL)
9853 {
9854 size_t len = strlen (physname);
9855
9856 while (1)
9857 {
9858 if (physname[len] == ')') /* shortcut */
9859 break;
9860 else if (check_modifier (physname, len, " const"))
c89b44cd 9861 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9862 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9863 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9864 else
9865 break;
9866 }
9867 }
3da10d80 9868 }
c89b44cd
TT
9869
9870 /* The list is no longer needed. */
9871 cu->method_list.clear ();
3da10d80
KS
9872}
9873
a766d390
DE
9874/* Go objects should be embedded in a DW_TAG_module DIE,
9875 and it's not clear if/how imported objects will appear.
9876 To keep Go support simple until that's worked out,
9877 go back through what we've read and create something usable.
9878 We could do this while processing each DIE, and feels kinda cleaner,
9879 but that way is more invasive.
9880 This is to, for example, allow the user to type "p var" or "b main"
9881 without having to specify the package name, and allow lookups
9882 of module.object to work in contexts that use the expression
9883 parser. */
9884
9885static void
9886fixup_go_packaging (struct dwarf2_cu *cu)
9887{
421d1616 9888 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9889 struct pending *list;
9890 int i;
9891
c24bdb02 9892 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9893 list != NULL;
9894 list = list->next)
a766d390
DE
9895 {
9896 for (i = 0; i < list->nsyms; ++i)
9897 {
9898 struct symbol *sym = list->symbol[i];
9899
c1b5c1eb 9900 if (sym->language () == language_go
a766d390
DE
9901 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9902 {
421d1616
TT
9903 gdb::unique_xmalloc_ptr<char> this_package_name
9904 (go_symbol_package_name (sym));
a766d390
DE
9905
9906 if (this_package_name == NULL)
9907 continue;
9908 if (package_name == NULL)
421d1616 9909 package_name = std::move (this_package_name);
a766d390
DE
9910 else
9911 {
518817b3
SM
9912 struct objfile *objfile
9913 = cu->per_cu->dwarf2_per_objfile->objfile;
421d1616 9914 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9915 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9916 (symbol_symtab (sym) != NULL
9917 ? symtab_to_filename_for_display
9918 (symbol_symtab (sym))
e3b94546 9919 : objfile_name (objfile)),
421d1616 9920 this_package_name.get (), package_name.get ());
a766d390
DE
9921 }
9922 }
9923 }
9924 }
9925
9926 if (package_name != NULL)
9927 {
518817b3 9928 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9929 const char *saved_package_name
421d1616 9930 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
19f392bc
UW
9931 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9932 saved_package_name);
a766d390
DE
9933 struct symbol *sym;
9934
e623cf5d 9935 sym = allocate_symbol (objfile);
d3ecddab 9936 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9937 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9938 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9939 e.g., "main" finds the "main" module and not C's main(). */
9940 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9941 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9942 SYMBOL_TYPE (sym) = type;
9943
c24bdb02 9944 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9945 }
9946}
9947
c9317f21
TT
9948/* Allocate a fully-qualified name consisting of the two parts on the
9949 obstack. */
9950
9951static const char *
9952rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9953{
9954 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9955}
9956
9957/* A helper that allocates a struct discriminant_info to attach to a
9958 union type. */
9959
9960static struct discriminant_info *
9961alloc_discriminant_info (struct type *type, int discriminant_index,
9962 int default_index)
9963{
9964 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9965 gdb_assert (discriminant_index == -1
9966 || (discriminant_index >= 0
9967 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9968 gdb_assert (default_index == -1
c7b15a66 9969 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9970
9971 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9972
9973 struct discriminant_info *disc
9974 = ((struct discriminant_info *)
9975 TYPE_ZALLOC (type,
9976 offsetof (struct discriminant_info, discriminants)
9977 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9978 disc->default_index = default_index;
9979 disc->discriminant_index = discriminant_index;
9980
9981 struct dynamic_prop prop;
9982 prop.kind = PROP_UNDEFINED;
9983 prop.data.baton = disc;
9984
9985 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9986
9987 return disc;
9988}
9989
9990/* Some versions of rustc emitted enums in an unusual way.
9991
9992 Ordinary enums were emitted as unions. The first element of each
9993 structure in the union was named "RUST$ENUM$DISR". This element
9994 held the discriminant.
9995
9996 These versions of Rust also implemented the "non-zero"
9997 optimization. When the enum had two values, and one is empty and
9998 the other holds a pointer that cannot be zero, the pointer is used
9999 as the discriminant, with a zero value meaning the empty variant.
10000 Here, the union's first member is of the form
10001 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
10002 where the fieldnos are the indices of the fields that should be
10003 traversed in order to find the field (which may be several fields deep)
10004 and the variantname is the name of the variant of the case when the
10005 field is zero.
10006
10007 This function recognizes whether TYPE is of one of these forms,
10008 and, if so, smashes it to be a variant type. */
10009
10010static void
10011quirk_rust_enum (struct type *type, struct objfile *objfile)
10012{
10013 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10014
10015 /* We don't need to deal with empty enums. */
10016 if (TYPE_NFIELDS (type) == 0)
10017 return;
10018
10019#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
10020 if (TYPE_NFIELDS (type) == 1
10021 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
10022 {
10023 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10024
10025 /* Decode the field name to find the offset of the
10026 discriminant. */
10027 ULONGEST bit_offset = 0;
10028 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10029 while (name[0] >= '0' && name[0] <= '9')
10030 {
10031 char *tail;
10032 unsigned long index = strtoul (name, &tail, 10);
10033 name = tail;
10034 if (*name != '$'
10035 || index >= TYPE_NFIELDS (field_type)
10036 || (TYPE_FIELD_LOC_KIND (field_type, index)
10037 != FIELD_LOC_KIND_BITPOS))
10038 {
b98664d3 10039 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
10040 "[in module %s]"),
10041 TYPE_FIELD_NAME (type, 0),
10042 objfile_name (objfile));
10043 return;
10044 }
10045 ++name;
10046
10047 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10048 field_type = TYPE_FIELD_TYPE (field_type, index);
10049 }
10050
10051 /* Make a union to hold the variants. */
10052 struct type *union_type = alloc_type (objfile);
10053 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10054 TYPE_NFIELDS (union_type) = 3;
10055 TYPE_FIELDS (union_type)
10056 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10057 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10058 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10059
10060 /* Put the discriminant must at index 0. */
10061 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10062 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10063 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10064 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10065
10066 /* The order of fields doesn't really matter, so put the real
10067 field at index 1 and the data-less field at index 2. */
10068 struct discriminant_info *disc
10069 = alloc_discriminant_info (union_type, 0, 1);
10070 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10071 TYPE_FIELD_NAME (union_type, 1)
10072 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10073 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10074 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10075 TYPE_FIELD_NAME (union_type, 1));
10076
10077 const char *dataless_name
10078 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10079 name);
10080 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10081 dataless_name);
10082 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10083 /* NAME points into the original discriminant name, which
10084 already has the correct lifetime. */
10085 TYPE_FIELD_NAME (union_type, 2) = name;
10086 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10087 disc->discriminants[2] = 0;
10088
10089 /* Smash this type to be a structure type. We have to do this
10090 because the type has already been recorded. */
10091 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10092 TYPE_NFIELDS (type) = 1;
10093 TYPE_FIELDS (type)
10094 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10095
10096 /* Install the variant part. */
10097 TYPE_FIELD_TYPE (type, 0) = union_type;
10098 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10099 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10100 }
77c2dba3
TT
10101 /* A union with a single anonymous field is probably an old-style
10102 univariant enum. */
10103 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 10104 {
c9317f21
TT
10105 /* Smash this type to be a structure type. We have to do this
10106 because the type has already been recorded. */
10107 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10108
10109 /* Make a union to hold the variants. */
10110 struct type *union_type = alloc_type (objfile);
10111 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10112 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10113 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10114 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10115 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10116
10117 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10118 const char *variant_name
10119 = rust_last_path_segment (TYPE_NAME (field_type));
10120 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10121 TYPE_NAME (field_type)
10122 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 10123 TYPE_NAME (type), variant_name);
c9317f21
TT
10124
10125 /* Install the union in the outer struct type. */
10126 TYPE_NFIELDS (type) = 1;
10127 TYPE_FIELDS (type)
10128 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10129 TYPE_FIELD_TYPE (type, 0) = union_type;
10130 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10131 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10132
10133 alloc_discriminant_info (union_type, -1, 0);
10134 }
10135 else
10136 {
10137 struct type *disr_type = nullptr;
10138 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10139 {
10140 disr_type = TYPE_FIELD_TYPE (type, i);
10141
a037790e
TT
10142 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10143 {
10144 /* All fields of a true enum will be structs. */
10145 return;
10146 }
10147 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
10148 {
10149 /* Could be data-less variant, so keep going. */
a037790e 10150 disr_type = nullptr;
c9317f21
TT
10151 }
10152 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10153 "RUST$ENUM$DISR") != 0)
10154 {
10155 /* Not a Rust enum. */
10156 return;
10157 }
10158 else
10159 {
10160 /* Found one. */
10161 break;
10162 }
10163 }
10164
10165 /* If we got here without a discriminant, then it's probably
10166 just a union. */
10167 if (disr_type == nullptr)
10168 return;
10169
10170 /* Smash this type to be a structure type. We have to do this
10171 because the type has already been recorded. */
10172 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10173
10174 /* Make a union to hold the variants. */
10175 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10176 struct type *union_type = alloc_type (objfile);
10177 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10178 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10179 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10180 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10181 TYPE_FIELDS (union_type)
10182 = (struct field *) TYPE_ZALLOC (union_type,
10183 (TYPE_NFIELDS (union_type)
10184 * sizeof (struct field)));
10185
10186 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10187 TYPE_NFIELDS (type) * sizeof (struct field));
10188
10189 /* Install the discriminant at index 0 in the union. */
10190 TYPE_FIELD (union_type, 0) = *disr_field;
10191 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10192 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10193
10194 /* Install the union in the outer struct type. */
10195 TYPE_FIELD_TYPE (type, 0) = union_type;
10196 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10197 TYPE_NFIELDS (type) = 1;
10198
10199 /* Set the size and offset of the union type. */
10200 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10201
10202 /* We need a way to find the correct discriminant given a
10203 variant name. For convenience we build a map here. */
10204 struct type *enum_type = FIELD_TYPE (*disr_field);
10205 std::unordered_map<std::string, ULONGEST> discriminant_map;
10206 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10207 {
10208 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10209 {
10210 const char *name
10211 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10212 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10213 }
10214 }
10215
10216 int n_fields = TYPE_NFIELDS (union_type);
10217 struct discriminant_info *disc
10218 = alloc_discriminant_info (union_type, 0, -1);
10219 /* Skip the discriminant here. */
10220 for (int i = 1; i < n_fields; ++i)
10221 {
10222 /* Find the final word in the name of this variant's type.
10223 That name can be used to look up the correct
10224 discriminant. */
10225 const char *variant_name
10226 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10227 i)));
10228
10229 auto iter = discriminant_map.find (variant_name);
10230 if (iter != discriminant_map.end ())
10231 disc->discriminants[i] = iter->second;
10232
bedda9ac 10233 /* Remove the discriminant field, if it exists. */
c9317f21 10234 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
10235 if (TYPE_NFIELDS (sub_type) > 0)
10236 {
10237 --TYPE_NFIELDS (sub_type);
10238 ++TYPE_FIELDS (sub_type);
10239 }
c9317f21
TT
10240 TYPE_FIELD_NAME (union_type, i) = variant_name;
10241 TYPE_NAME (sub_type)
10242 = rust_fully_qualify (&objfile->objfile_obstack,
10243 TYPE_NAME (type), variant_name);
10244 }
10245 }
10246}
10247
10248/* Rewrite some Rust unions to be structures with variants parts. */
10249
10250static void
10251rust_union_quirks (struct dwarf2_cu *cu)
10252{
10253 gdb_assert (cu->language == language_rust);
52941706
SM
10254 for (type *type_ : cu->rust_unions)
10255 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
10256 /* We don't need this any more. */
10257 cu->rust_unions.clear ();
c9317f21
TT
10258}
10259
95554aad
TT
10260/* Return the symtab for PER_CU. This works properly regardless of
10261 whether we're using the index or psymtabs. */
10262
43f3e411
DE
10263static struct compunit_symtab *
10264get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10265{
ed2dc618 10266 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10267 ? per_cu->v.quick->compunit_symtab
10268 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10269}
10270
10271/* A helper function for computing the list of all symbol tables
10272 included by PER_CU. */
10273
10274static void
4c39bc03 10275recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 10276 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10277 struct dwarf2_per_cu_data *per_cu,
43f3e411 10278 struct compunit_symtab *immediate_parent)
95554aad
TT
10279{
10280 void **slot;
43f3e411 10281 struct compunit_symtab *cust;
95554aad
TT
10282
10283 slot = htab_find_slot (all_children, per_cu, INSERT);
10284 if (*slot != NULL)
10285 {
10286 /* This inclusion and its children have been processed. */
10287 return;
10288 }
10289
10290 *slot = per_cu;
10291 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10292 cust = get_compunit_symtab (per_cu);
10293 if (cust != NULL)
ec94af83
DE
10294 {
10295 /* If this is a type unit only add its symbol table if we haven't
10296 seen it yet (type unit per_cu's can share symtabs). */
10297 if (per_cu->is_debug_types)
10298 {
43f3e411 10299 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10300 if (*slot == NULL)
10301 {
43f3e411 10302 *slot = cust;
4c39bc03 10303 result->push_back (cust);
43f3e411
DE
10304 if (cust->user == NULL)
10305 cust->user = immediate_parent;
ec94af83
DE
10306 }
10307 }
10308 else
f9125b6c 10309 {
4c39bc03 10310 result->push_back (cust);
43f3e411
DE
10311 if (cust->user == NULL)
10312 cust->user = immediate_parent;
f9125b6c 10313 }
ec94af83 10314 }
95554aad 10315
ae640021
AB
10316 if (!per_cu->imported_symtabs_empty ())
10317 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10318 {
10319 recursively_compute_inclusions (result, all_children,
10320 all_type_symtabs, ptr, cust);
10321 }
95554aad
TT
10322}
10323
43f3e411 10324/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10325 PER_CU. */
10326
10327static void
43f3e411 10328compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10329{
f4dc4d17
DE
10330 gdb_assert (! per_cu->is_debug_types);
10331
ae640021 10332 if (!per_cu->imported_symtabs_empty ())
95554aad 10333 {
ae640021 10334 int len;
4c39bc03 10335 std::vector<compunit_symtab *> result_symtabs;
ec94af83 10336 htab_t all_children, all_type_symtabs;
43f3e411 10337 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10338
10339 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10340 if (cust == NULL)
95554aad
TT
10341 return;
10342
10343 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10344 NULL, xcalloc, xfree);
ec94af83
DE
10345 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10346 NULL, xcalloc, xfree);
95554aad 10347
ae640021 10348 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
10349 {
10350 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 10351 all_type_symtabs, ptr, cust);
ec94af83 10352 }
95554aad 10353
ec94af83 10354 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 10355 len = result_symtabs.size ();
43f3e411 10356 cust->includes
ed2dc618 10357 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10358 struct compunit_symtab *, len + 1);
4c39bc03
TT
10359 memcpy (cust->includes, result_symtabs.data (),
10360 len * sizeof (compunit_symtab *));
43f3e411 10361 cust->includes[len] = NULL;
95554aad 10362
95554aad 10363 htab_delete (all_children);
ec94af83 10364 htab_delete (all_type_symtabs);
95554aad
TT
10365 }
10366}
10367
10368/* Compute the 'includes' field for the symtabs of all the CUs we just
10369 read. */
10370
10371static void
ed2dc618 10372process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 10373{
71b73764 10374 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
10375 {
10376 if (! iter->is_debug_types)
43f3e411 10377 compute_compunit_symtab_includes (iter);
f4dc4d17 10378 }
95554aad 10379
c5d0225d 10380 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
10381}
10382
9cdd5dbd 10383/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10384 already been loaded into memory. */
10385
10386static void
95554aad
TT
10387process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10388 enum language pretend_language)
10b3939b 10389{
10b3939b 10390 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10391 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10392 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10393 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10394 CORE_ADDR lowpc, highpc;
43f3e411 10395 struct compunit_symtab *cust;
10b3939b 10396 CORE_ADDR baseaddr;
4359dff1 10397 struct block *static_block;
3e29f34a 10398 CORE_ADDR addr;
10b3939b 10399
6a053cb1 10400 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
10b3939b 10401
c89b44cd
TT
10402 /* Clear the list here in case something was left over. */
10403 cu->method_list.clear ();
10b3939b 10404
95554aad
TT
10405 cu->language = pretend_language;
10406 cu->language_defn = language_def (cu->language);
10407
c906108c 10408 /* Do line number decoding in read_file_scope () */
10b3939b 10409 process_die (cu->dies, cu);
c906108c 10410
a766d390
DE
10411 /* For now fudge the Go package. */
10412 if (cu->language == language_go)
10413 fixup_go_packaging (cu);
10414
5f48f8f3 10415 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
10416 should be complete, and it should now be safe to compute all of the
10417 physnames. */
10418 compute_delayed_physnames (cu);
3da10d80 10419
c9317f21
TT
10420 if (cu->language == language_rust)
10421 rust_union_quirks (cu);
10422
fae299cd
DC
10423 /* Some compilers don't define a DW_AT_high_pc attribute for the
10424 compilation unit. If the DW_AT_high_pc is missing, synthesize
10425 it, by scanning the DIE's below the compilation unit. */
10b3939b 10426 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10427
3e29f34a 10428 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 10429 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10430
10431 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10432 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10433 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10434 addrmap to help ensure it has an accurate map of pc values belonging to
10435 this comp unit. */
10436 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10437
c24bdb02 10438 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
10439 SECT_OFF_TEXT (objfile),
10440 0);
c906108c 10441
43f3e411 10442 if (cust != NULL)
c906108c 10443 {
df15bd07 10444 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10445
8be455d7
JK
10446 /* Set symtab language to language from DW_AT_language. If the
10447 compilation is from a C file generated by language preprocessors, do
10448 not set the language if it was already deduced by start_subfile. */
43f3e411 10449 if (!(cu->language == language_c
40e3ad0e 10450 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10451 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10452
10453 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10454 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10455 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10456 there were bugs in prologue debug info, fixed later in GCC-4.5
10457 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10458
10459 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10460 needed, it would be wrong due to missing DW_AT_producer there.
10461
10462 Still one can confuse GDB by using non-standard GCC compilation
10463 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 10464 */
ab260dad 10465 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10466 cust->locations_valid = 1;
e0d00bc7
JK
10467
10468 if (gcc_4_minor >= 5)
43f3e411 10469 cust->epilogue_unwind_valid = 1;
96408a79 10470
43f3e411 10471 cust->call_site_htab = cu->call_site_htab;
c906108c 10472 }
9291a0cd
TT
10473
10474 if (dwarf2_per_objfile->using_index)
43f3e411 10475 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10476 else
10477 {
10478 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10479 pst->compunit_symtab = cust;
9291a0cd
TT
10480 pst->readin = 1;
10481 }
c906108c 10482
95554aad 10483 /* Push it for inclusion processing later. */
c5d0225d 10484 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
10485
10486 /* Not needed any more. */
c24bdb02 10487 cu->reset_builder ();
f4dc4d17 10488}
45cfd468 10489
f4dc4d17
DE
10490/* Generate full symbol information for type unit PER_CU, whose DIEs have
10491 already been loaded into memory. */
10492
10493static void
10494process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10495 enum language pretend_language)
10496{
10497 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10498 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10499 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10500 struct compunit_symtab *cust;
0186c6a7
DE
10501 struct signatured_type *sig_type;
10502
10503 gdb_assert (per_cu->is_debug_types);
10504 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 10505
c89b44cd
TT
10506 /* Clear the list here in case something was left over. */
10507 cu->method_list.clear ();
f4dc4d17 10508
f4dc4d17
DE
10509 cu->language = pretend_language;
10510 cu->language_defn = language_def (cu->language);
10511
10512 /* The symbol tables are set up in read_type_unit_scope. */
10513 process_die (cu->dies, cu);
10514
10515 /* For now fudge the Go package. */
10516 if (cu->language == language_go)
10517 fixup_go_packaging (cu);
10518
5f48f8f3 10519 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
10520 should be complete, and it should now be safe to compute all of the
10521 physnames. */
10522 compute_delayed_physnames (cu);
f4dc4d17 10523
c9317f21
TT
10524 if (cu->language == language_rust)
10525 rust_union_quirks (cu);
10526
f4dc4d17
DE
10527 /* TUs share symbol tables.
10528 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10529 of it with end_expandable_symtab. Otherwise, complete the addition of
10530 this TU's symbols to the existing symtab. */
43f3e411 10531 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10532 {
c24bdb02
KS
10533 buildsym_compunit *builder = cu->get_builder ();
10534 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 10535 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10536
43f3e411 10537 if (cust != NULL)
f4dc4d17
DE
10538 {
10539 /* Set symtab language to language from DW_AT_language. If the
10540 compilation is from a C file generated by language preprocessors,
10541 do not set the language if it was already deduced by
10542 start_subfile. */
43f3e411
DE
10543 if (!(cu->language == language_c
10544 && COMPUNIT_FILETABS (cust)->language != language_c))
10545 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10546 }
10547 }
10548 else
10549 {
c24bdb02 10550 cu->get_builder ()->augment_type_symtab ();
43f3e411 10551 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10552 }
10553
10554 if (dwarf2_per_objfile->using_index)
43f3e411 10555 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10556 else
10557 {
10558 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10559 pst->compunit_symtab = cust;
f4dc4d17 10560 pst->readin = 1;
45cfd468 10561 }
804d2729
TT
10562
10563 /* Not needed any more. */
c24bdb02 10564 cu->reset_builder ();
c906108c
SS
10565}
10566
95554aad
TT
10567/* Process an imported unit DIE. */
10568
10569static void
10570process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10571{
10572 struct attribute *attr;
10573
f4dc4d17
DE
10574 /* For now we don't handle imported units in type units. */
10575 if (cu->per_cu->is_debug_types)
10576 {
10577 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10578 " supported in type units [in module %s]"),
518817b3 10579 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10580 }
10581
95554aad
TT
10582 attr = dwarf2_attr (die, DW_AT_import, cu);
10583 if (attr != NULL)
10584 {
9c541725
PA
10585 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10586 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10587 dwarf2_per_cu_data *per_cu
e3b94546 10588 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10589 cu->per_cu->dwarf2_per_objfile);
95554aad 10590
69d751e3 10591 /* If necessary, add it to the queue and load its DIEs. */
95554aad 10592 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 10593 load_full_comp_unit (per_cu, false, cu->language);
95554aad 10594
ae640021 10595 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10596 }
10597}
10598
4c8aa72d
PA
10599/* RAII object that represents a process_die scope: i.e.,
10600 starts/finishes processing a DIE. */
10601class process_die_scope
adde2bff 10602{
4c8aa72d
PA
10603public:
10604 process_die_scope (die_info *die, dwarf2_cu *cu)
10605 : m_die (die), m_cu (cu)
10606 {
10607 /* We should only be processing DIEs not already in process. */
10608 gdb_assert (!m_die->in_process);
10609 m_die->in_process = true;
10610 }
8c3cb9fa 10611
4c8aa72d
PA
10612 ~process_die_scope ()
10613 {
10614 m_die->in_process = false;
10615
10616 /* If we're done processing the DIE for the CU that owns the line
10617 header, we don't need the line header anymore. */
10618 if (m_cu->line_header_die_owner == m_die)
10619 {
10620 delete m_cu->line_header;
10621 m_cu->line_header = NULL;
10622 m_cu->line_header_die_owner = NULL;
10623 }
10624 }
10625
10626private:
10627 die_info *m_die;
10628 dwarf2_cu *m_cu;
10629};
adde2bff 10630
c906108c
SS
10631/* Process a die and its children. */
10632
10633static void
e7c27a73 10634process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10635{
4c8aa72d 10636 process_die_scope scope (die, cu);
adde2bff 10637
c906108c
SS
10638 switch (die->tag)
10639 {
10640 case DW_TAG_padding:
10641 break;
10642 case DW_TAG_compile_unit:
95554aad 10643 case DW_TAG_partial_unit:
e7c27a73 10644 read_file_scope (die, cu);
c906108c 10645 break;
348e048f
DE
10646 case DW_TAG_type_unit:
10647 read_type_unit_scope (die, cu);
10648 break;
c906108c 10649 case DW_TAG_subprogram:
0a4b0913
AB
10650 /* Nested subprograms in Fortran get a prefix. */
10651 if (cu->language == language_fortran
10652 && die->parent != NULL
10653 && die->parent->tag == DW_TAG_subprogram)
10654 cu->processing_has_namespace_info = true;
10655 /* Fall through. */
c906108c 10656 case DW_TAG_inlined_subroutine:
edb3359d 10657 read_func_scope (die, cu);
c906108c
SS
10658 break;
10659 case DW_TAG_lexical_block:
14898363
L
10660 case DW_TAG_try_block:
10661 case DW_TAG_catch_block:
e7c27a73 10662 read_lexical_block_scope (die, cu);
c906108c 10663 break;
216f72a1 10664 case DW_TAG_call_site:
96408a79
SA
10665 case DW_TAG_GNU_call_site:
10666 read_call_site_scope (die, cu);
10667 break;
c906108c 10668 case DW_TAG_class_type:
680b30c7 10669 case DW_TAG_interface_type:
c906108c
SS
10670 case DW_TAG_structure_type:
10671 case DW_TAG_union_type:
134d01f1 10672 process_structure_scope (die, cu);
c906108c
SS
10673 break;
10674 case DW_TAG_enumeration_type:
134d01f1 10675 process_enumeration_scope (die, cu);
c906108c 10676 break;
134d01f1 10677
f792889a
DJ
10678 /* These dies have a type, but processing them does not create
10679 a symbol or recurse to process the children. Therefore we can
10680 read them on-demand through read_type_die. */
c906108c 10681 case DW_TAG_subroutine_type:
72019c9c 10682 case DW_TAG_set_type:
c906108c 10683 case DW_TAG_array_type:
c906108c 10684 case DW_TAG_pointer_type:
c906108c 10685 case DW_TAG_ptr_to_member_type:
c906108c 10686 case DW_TAG_reference_type:
4297a3f0 10687 case DW_TAG_rvalue_reference_type:
c906108c 10688 case DW_TAG_string_type:
c906108c 10689 break;
134d01f1 10690
c906108c 10691 case DW_TAG_base_type:
a02abb62 10692 case DW_TAG_subrange_type:
cb249c71 10693 case DW_TAG_typedef:
134d01f1
DJ
10694 /* Add a typedef symbol for the type definition, if it has a
10695 DW_AT_name. */
f792889a 10696 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10697 break;
c906108c 10698 case DW_TAG_common_block:
e7c27a73 10699 read_common_block (die, cu);
c906108c
SS
10700 break;
10701 case DW_TAG_common_inclusion:
10702 break;
d9fa45fe 10703 case DW_TAG_namespace:
9068261f 10704 cu->processing_has_namespace_info = true;
e7c27a73 10705 read_namespace (die, cu);
d9fa45fe 10706 break;
5d7cb8df 10707 case DW_TAG_module:
9068261f 10708 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10709 read_module (die, cu);
10710 break;
d9fa45fe 10711 case DW_TAG_imported_declaration:
9068261f 10712 cu->processing_has_namespace_info = true;
74921315
KS
10713 if (read_namespace_alias (die, cu))
10714 break;
86a73007
TT
10715 /* The declaration is not a global namespace alias. */
10716 /* Fall through. */
d9fa45fe 10717 case DW_TAG_imported_module:
9068261f 10718 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10719 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10720 || cu->language != language_fortran))
b98664d3 10721 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10722 dwarf_tag_name (die->tag));
10723 read_import_statement (die, cu);
d9fa45fe 10724 break;
95554aad
TT
10725
10726 case DW_TAG_imported_unit:
10727 process_imported_unit_die (die, cu);
10728 break;
10729
71a3c369
TT
10730 case DW_TAG_variable:
10731 read_variable (die, cu);
10732 break;
10733
c906108c 10734 default:
e7c27a73 10735 new_symbol (die, NULL, cu);
c906108c
SS
10736 break;
10737 }
10738}
ca69b9e6
DE
10739\f
10740/* DWARF name computation. */
c906108c 10741
94af9270
KS
10742/* A helper function for dwarf2_compute_name which determines whether DIE
10743 needs to have the name of the scope prepended to the name listed in the
10744 die. */
10745
10746static int
10747die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10748{
1c809c68
TT
10749 struct attribute *attr;
10750
94af9270
KS
10751 switch (die->tag)
10752 {
10753 case DW_TAG_namespace:
10754 case DW_TAG_typedef:
10755 case DW_TAG_class_type:
10756 case DW_TAG_interface_type:
10757 case DW_TAG_structure_type:
10758 case DW_TAG_union_type:
10759 case DW_TAG_enumeration_type:
10760 case DW_TAG_enumerator:
10761 case DW_TAG_subprogram:
08a76f8a 10762 case DW_TAG_inlined_subroutine:
94af9270 10763 case DW_TAG_member:
74921315 10764 case DW_TAG_imported_declaration:
94af9270
KS
10765 return 1;
10766
10767 case DW_TAG_variable:
c2b0a229 10768 case DW_TAG_constant:
94af9270
KS
10769 /* We only need to prefix "globally" visible variables. These include
10770 any variable marked with DW_AT_external or any variable that
10771 lives in a namespace. [Variables in anonymous namespaces
10772 require prefixing, but they are not DW_AT_external.] */
10773
10774 if (dwarf2_attr (die, DW_AT_specification, cu))
10775 {
10776 struct dwarf2_cu *spec_cu = cu;
9a619af0 10777
94af9270
KS
10778 return die_needs_namespace (die_specification (die, &spec_cu),
10779 spec_cu);
10780 }
10781
1c809c68 10782 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10783 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10784 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10785 return 0;
10786 /* A variable in a lexical block of some kind does not need a
10787 namespace, even though in C++ such variables may be external
10788 and have a mangled name. */
10789 if (die->parent->tag == DW_TAG_lexical_block
10790 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10791 || die->parent->tag == DW_TAG_catch_block
10792 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10793 return 0;
10794 return 1;
94af9270
KS
10795
10796 default:
10797 return 0;
10798 }
10799}
10800
73b9be8b
KS
10801/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10802 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10803 defined for the given DIE. */
10804
10805static struct attribute *
10806dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10807{
10808 struct attribute *attr;
10809
10810 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10811 if (attr == NULL)
10812 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10813
10814 return attr;
10815}
10816
10817/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10818 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10819 defined for the given DIE. */
10820
10821static const char *
10822dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10823{
10824 const char *linkage_name;
10825
10826 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10827 if (linkage_name == NULL)
10828 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10829
10830 return linkage_name;
10831}
10832
94af9270 10833/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10834 compute the physname for the object, which include a method's:
9c37b5ae 10835 - formal parameters (C++),
a766d390 10836 - receiver type (Go),
a766d390
DE
10837
10838 The term "physname" is a bit confusing.
10839 For C++, for example, it is the demangled name.
10840 For Go, for example, it's the mangled name.
94af9270 10841
af6b7be1
JB
10842 For Ada, return the DIE's linkage name rather than the fully qualified
10843 name. PHYSNAME is ignored..
10844
94af9270
KS
10845 The result is allocated on the objfile_obstack and canonicalized. */
10846
10847static const char *
15d034d0
TT
10848dwarf2_compute_name (const char *name,
10849 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10850 int physname)
10851{
518817b3 10852 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10853
94af9270
KS
10854 if (name == NULL)
10855 name = dwarf2_name (die, cu);
10856
2ee7123e
DE
10857 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10858 but otherwise compute it by typename_concat inside GDB.
10859 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10860 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10861 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10862 will set the demangled name to the result of dwarf2_full_name, and it is
10863 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10864 if (cu->language == language_ada
10865 || (cu->language == language_fortran && physname))
10866 {
10867 /* For Ada unit, we prefer the linkage name over the name, as
10868 the former contains the exported name, which the user expects
10869 to be able to reference. Ideally, we want the user to be able
10870 to reference this entity using either natural or linkage name,
10871 but we haven't started looking at this enhancement yet. */
73b9be8b 10872 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10873
2ee7123e
DE
10874 if (linkage_name != NULL)
10875 return linkage_name;
f55ee35c
JK
10876 }
10877
94af9270
KS
10878 /* These are the only languages we know how to qualify names in. */
10879 if (name != NULL
9c37b5ae 10880 && (cu->language == language_cplus
c44af4eb
TT
10881 || cu->language == language_fortran || cu->language == language_d
10882 || cu->language == language_rust))
94af9270
KS
10883 {
10884 if (die_needs_namespace (die, cu))
10885 {
0d5cff50 10886 const char *prefix;
34a68019 10887 const char *canonical_name = NULL;
94af9270 10888
d7e74731
PA
10889 string_file buf;
10890
94af9270 10891 prefix = determine_prefix (die, cu);
94af9270
KS
10892 if (*prefix != '\0')
10893 {
43816ebc
TT
10894 gdb::unique_xmalloc_ptr<char> prefixed_name
10895 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10896
43816ebc 10897 buf.puts (prefixed_name.get ());
94af9270
KS
10898 }
10899 else
d7e74731 10900 buf.puts (name);
94af9270 10901
98bfdba5
PA
10902 /* Template parameters may be specified in the DIE's DW_AT_name, or
10903 as children with DW_TAG_template_type_param or
10904 DW_TAG_value_type_param. If the latter, add them to the name
10905 here. If the name already has template parameters, then
10906 skip this step; some versions of GCC emit both, and
10907 it is more efficient to use the pre-computed name.
10908
10909 Something to keep in mind about this process: it is very
10910 unlikely, or in some cases downright impossible, to produce
10911 something that will match the mangled name of a function.
10912 If the definition of the function has the same debug info,
10913 we should be able to match up with it anyway. But fallbacks
10914 using the minimal symbol, for instance to find a method
10915 implemented in a stripped copy of libstdc++, will not work.
10916 If we do not have debug info for the definition, we will have to
10917 match them up some other way.
10918
10919 When we do name matching there is a related problem with function
10920 templates; two instantiated function templates are allowed to
10921 differ only by their return types, which we do not add here. */
10922
10923 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10924 {
10925 struct attribute *attr;
10926 struct die_info *child;
10927 int first = 1;
10928
10929 die->building_fullname = 1;
10930
10931 for (child = die->child; child != NULL; child = child->sibling)
10932 {
10933 struct type *type;
12df843f 10934 LONGEST value;
d521ce57 10935 const gdb_byte *bytes;
98bfdba5
PA
10936 struct dwarf2_locexpr_baton *baton;
10937 struct value *v;
10938
10939 if (child->tag != DW_TAG_template_type_param
10940 && child->tag != DW_TAG_template_value_param)
10941 continue;
10942
10943 if (first)
10944 {
d7e74731 10945 buf.puts ("<");
98bfdba5
PA
10946 first = 0;
10947 }
10948 else
d7e74731 10949 buf.puts (", ");
98bfdba5
PA
10950
10951 attr = dwarf2_attr (child, DW_AT_type, cu);
10952 if (attr == NULL)
10953 {
b98664d3 10954 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10955 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10956 continue;
10957 }
10958 type = die_type (child, cu);
10959
10960 if (child->tag == DW_TAG_template_type_param)
10961 {
c1ec8cea
TT
10962 c_print_type (type, "", &buf, -1, 0, cu->language,
10963 &type_print_raw_options);
98bfdba5
PA
10964 continue;
10965 }
10966
10967 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10968 if (attr == NULL)
10969 {
b98664d3 10970 complaint (_("template parameter missing "
3e43a32a 10971 "DW_AT_const_value"));
d7e74731 10972 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10973 continue;
10974 }
10975
10976 dwarf2_const_value_attr (attr, type, name,
10977 &cu->comp_unit_obstack, cu,
10978 &value, &bytes, &baton);
10979
10980 if (TYPE_NOSIGN (type))
10981 /* GDB prints characters as NUMBER 'CHAR'. If that's
10982 changed, this can use value_print instead. */
d7e74731 10983 c_printchar (value, type, &buf);
98bfdba5
PA
10984 else
10985 {
10986 struct value_print_options opts;
10987
10988 if (baton != NULL)
10989 v = dwarf2_evaluate_loc_desc (type, NULL,
10990 baton->data,
10991 baton->size,
10992 baton->per_cu);
10993 else if (bytes != NULL)
10994 {
10995 v = allocate_value (type);
10996 memcpy (value_contents_writeable (v), bytes,
10997 TYPE_LENGTH (type));
10998 }
10999 else
11000 v = value_from_longest (type, value);
11001
3e43a32a
MS
11002 /* Specify decimal so that we do not depend on
11003 the radix. */
98bfdba5
PA
11004 get_formatted_print_options (&opts, 'd');
11005 opts.raw = 1;
d7e74731 11006 value_print (v, &buf, &opts);
98bfdba5 11007 release_value (v);
98bfdba5
PA
11008 }
11009 }
11010
11011 die->building_fullname = 0;
11012
11013 if (!first)
11014 {
11015 /* Close the argument list, with a space if necessary
11016 (nested templates). */
d7e74731
PA
11017 if (!buf.empty () && buf.string ().back () == '>')
11018 buf.puts (" >");
98bfdba5 11019 else
d7e74731 11020 buf.puts (">");
98bfdba5
PA
11021 }
11022 }
11023
9c37b5ae 11024 /* For C++ methods, append formal parameter type
94af9270 11025 information, if PHYSNAME. */
6e70227d 11026
94af9270 11027 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 11028 && cu->language == language_cplus)
94af9270
KS
11029 {
11030 struct type *type = read_type_die (die, cu);
11031
d7e74731 11032 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 11033 &type_print_raw_options);
94af9270 11034
9c37b5ae 11035 if (cu->language == language_cplus)
94af9270 11036 {
60430eff
DJ
11037 /* Assume that an artificial first parameter is
11038 "this", but do not crash if it is not. RealView
11039 marks unnamed (and thus unused) parameters as
11040 artificial; there is no way to differentiate
11041 the two cases. */
94af9270
KS
11042 if (TYPE_NFIELDS (type) > 0
11043 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 11044 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
11045 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11046 0))))
d7e74731 11047 buf.puts (" const");
94af9270
KS
11048 }
11049 }
11050
d7e74731 11051 const std::string &intermediate_name = buf.string ();
94af9270
KS
11052
11053 if (cu->language == language_cplus)
34a68019 11054 canonical_name
322a8516 11055 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
11056 &objfile->per_bfd->storage_obstack);
11057
11058 /* If we only computed INTERMEDIATE_NAME, or if
11059 INTERMEDIATE_NAME is already canonical, then we need to
11060 copy it to the appropriate obstack. */
322a8516 11061 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
efba19b0
TT
11062 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11063 intermediate_name);
34a68019
TT
11064 else
11065 name = canonical_name;
94af9270
KS
11066 }
11067 }
11068
11069 return name;
11070}
11071
0114d602
DJ
11072/* Return the fully qualified name of DIE, based on its DW_AT_name.
11073 If scope qualifiers are appropriate they will be added. The result
34a68019 11074 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
11075 not have a name. NAME may either be from a previous call to
11076 dwarf2_name or NULL.
11077
9c37b5ae 11078 The output string will be canonicalized (if C++). */
0114d602
DJ
11079
11080static const char *
15d034d0 11081dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 11082{
94af9270
KS
11083 return dwarf2_compute_name (name, die, cu, 0);
11084}
0114d602 11085
94af9270
KS
11086/* Construct a physname for the given DIE in CU. NAME may either be
11087 from a previous call to dwarf2_name or NULL. The result will be
11088 allocated on the objfile_objstack or NULL if the DIE does not have a
11089 name.
0114d602 11090
9c37b5ae 11091 The output string will be canonicalized (if C++). */
0114d602 11092
94af9270 11093static const char *
15d034d0 11094dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 11095{
518817b3 11096 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 11097 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
11098 int need_copy = 1;
11099
11100 /* In this case dwarf2_compute_name is just a shortcut not building anything
11101 on its own. */
11102 if (!die_needs_namespace (die, cu))
11103 return dwarf2_compute_name (name, die, cu, 1);
11104
73b9be8b 11105 mangled = dw2_linkage_name (die, cu);
900e11f9 11106
e98c9e7c
TT
11107 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11108 See https://github.com/rust-lang/rust/issues/32925. */
11109 if (cu->language == language_rust && mangled != NULL
11110 && strchr (mangled, '{') != NULL)
11111 mangled = NULL;
11112
900e11f9
JK
11113 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11114 has computed. */
791afaa2 11115 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 11116 if (mangled != NULL)
900e11f9 11117 {
900e11f9 11118
59cc4834
JB
11119 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11120 {
11121 /* Do nothing (do not demangle the symbol name). */
11122 }
11123 else if (cu->language == language_go)
a766d390 11124 {
5e2db402
TT
11125 /* This is a lie, but we already lie to the caller new_symbol.
11126 new_symbol assumes we return the mangled name.
a766d390 11127 This just undoes that lie until things are cleaned up. */
a766d390
DE
11128 }
11129 else
11130 {
0eb876f5
JB
11131 /* Use DMGL_RET_DROP for C++ template functions to suppress
11132 their return type. It is easier for GDB users to search
11133 for such functions as `name(params)' than `long name(params)'.
11134 In such case the minimal symbol names do not match the full
11135 symbol names but for template functions there is never a need
11136 to look up their definition from their declaration so
11137 the only disadvantage remains the minimal symbol variant
11138 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
11139 demangled.reset (gdb_demangle (mangled,
11140 (DMGL_PARAMS | DMGL_ANSI
11141 | DMGL_RET_DROP)));
a766d390 11142 }
900e11f9 11143 if (demangled)
791afaa2 11144 canon = demangled.get ();
900e11f9
JK
11145 else
11146 {
11147 canon = mangled;
11148 need_copy = 0;
11149 }
11150 }
11151
11152 if (canon == NULL || check_physname)
11153 {
11154 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11155
11156 if (canon != NULL && strcmp (physname, canon) != 0)
11157 {
11158 /* It may not mean a bug in GDB. The compiler could also
11159 compute DW_AT_linkage_name incorrectly. But in such case
11160 GDB would need to be bug-to-bug compatible. */
11161
b98664d3 11162 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
11163 "(from linkage <%s>) - DIE at %s [in module %s]"),
11164 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 11165 objfile_name (objfile));
900e11f9
JK
11166
11167 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11168 is available here - over computed PHYSNAME. It is safer
11169 against both buggy GDB and buggy compilers. */
11170
11171 retval = canon;
11172 }
11173 else
11174 {
11175 retval = physname;
11176 need_copy = 0;
11177 }
11178 }
11179 else
11180 retval = canon;
11181
11182 if (need_copy)
021887d8 11183 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
900e11f9 11184
900e11f9 11185 return retval;
0114d602
DJ
11186}
11187
74921315
KS
11188/* Inspect DIE in CU for a namespace alias. If one exists, record
11189 a new symbol for it.
11190
11191 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11192
11193static int
11194read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11195{
11196 struct attribute *attr;
11197
11198 /* If the die does not have a name, this is not a namespace
11199 alias. */
11200 attr = dwarf2_attr (die, DW_AT_name, cu);
11201 if (attr != NULL)
11202 {
11203 int num;
11204 struct die_info *d = die;
11205 struct dwarf2_cu *imported_cu = cu;
11206
11207 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11208 keep inspecting DIEs until we hit the underlying import. */
11209#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11210 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11211 {
11212 attr = dwarf2_attr (d, DW_AT_import, cu);
11213 if (attr == NULL)
11214 break;
11215
11216 d = follow_die_ref (d, attr, &imported_cu);
11217 if (d->tag != DW_TAG_imported_declaration)
11218 break;
11219 }
11220
11221 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11222 {
b98664d3 11223 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 11224 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
11225 return 0;
11226 }
11227
11228 if (attr != NULL)
11229 {
11230 struct type *type;
9c541725 11231 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11232
9c541725 11233 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11234 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11235 {
11236 /* This declaration is a global namespace alias. Add
11237 a symbol for it whose type is the aliased namespace. */
11238 new_symbol (die, type, cu);
11239 return 1;
11240 }
11241 }
11242 }
11243
11244 return 0;
11245}
11246
22cee43f 11247/* Return the using directives repository (global or local?) to use in the
804d2729 11248 current context for CU.
22cee43f
PMR
11249
11250 For Ada, imported declarations can materialize renamings, which *may* be
11251 global. However it is impossible (for now?) in DWARF to distinguish
11252 "external" imported declarations and "static" ones. As all imported
11253 declarations seem to be static in all other languages, make them all CU-wide
11254 global only in Ada. */
11255
11256static struct using_direct **
804d2729 11257using_directives (struct dwarf2_cu *cu)
22cee43f 11258{
c24bdb02
KS
11259 if (cu->language == language_ada
11260 && cu->get_builder ()->outermost_context_p ())
11261 return cu->get_builder ()->get_global_using_directives ();
22cee43f 11262 else
c24bdb02 11263 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
11264}
11265
27aa8d6a
SW
11266/* Read the import statement specified by the given die and record it. */
11267
11268static void
11269read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11270{
518817b3 11271 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11272 struct attribute *import_attr;
32019081 11273 struct die_info *imported_die, *child_die;
de4affc9 11274 struct dwarf2_cu *imported_cu;
27aa8d6a 11275 const char *imported_name;
794684b6 11276 const char *imported_name_prefix;
13387711
SW
11277 const char *canonical_name;
11278 const char *import_alias;
11279 const char *imported_declaration = NULL;
794684b6 11280 const char *import_prefix;
eb1e02fd 11281 std::vector<const char *> excludes;
13387711 11282
27aa8d6a
SW
11283 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11284 if (import_attr == NULL)
11285 {
b98664d3 11286 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
11287 dwarf_tag_name (die->tag));
11288 return;
11289 }
11290
de4affc9
CC
11291 imported_cu = cu;
11292 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11293 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11294 if (imported_name == NULL)
11295 {
11296 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11297
11298 The import in the following code:
11299 namespace A
11300 {
11301 typedef int B;
11302 }
11303
11304 int main ()
11305 {
11306 using A::B;
11307 B b;
11308 return b;
11309 }
11310
11311 ...
11312 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11313 <52> DW_AT_decl_file : 1
11314 <53> DW_AT_decl_line : 6
11315 <54> DW_AT_import : <0x75>
11316 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11317 <59> DW_AT_name : B
11318 <5b> DW_AT_decl_file : 1
11319 <5c> DW_AT_decl_line : 2
11320 <5d> DW_AT_type : <0x6e>
11321 ...
11322 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11323 <76> DW_AT_byte_size : 4
11324 <77> DW_AT_encoding : 5 (signed)
11325
11326 imports the wrong die ( 0x75 instead of 0x58 ).
11327 This case will be ignored until the gcc bug is fixed. */
11328 return;
11329 }
11330
82856980
SW
11331 /* Figure out the local name after import. */
11332 import_alias = dwarf2_name (die, cu);
27aa8d6a 11333
794684b6
SW
11334 /* Figure out where the statement is being imported to. */
11335 import_prefix = determine_prefix (die, cu);
11336
11337 /* Figure out what the scope of the imported die is and prepend it
11338 to the name of the imported die. */
de4affc9 11339 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11340
f55ee35c
JK
11341 if (imported_die->tag != DW_TAG_namespace
11342 && imported_die->tag != DW_TAG_module)
794684b6 11343 {
13387711
SW
11344 imported_declaration = imported_name;
11345 canonical_name = imported_name_prefix;
794684b6 11346 }
13387711 11347 else if (strlen (imported_name_prefix) > 0)
12aaed36 11348 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11349 imported_name_prefix,
11350 (cu->language == language_d ? "." : "::"),
11351 imported_name, (char *) NULL);
13387711
SW
11352 else
11353 canonical_name = imported_name;
794684b6 11354
32019081
JK
11355 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11356 for (child_die = die->child; child_die && child_die->tag;
11357 child_die = sibling_die (child_die))
11358 {
11359 /* DWARF-4: A Fortran use statement with a “rename list” may be
11360 represented by an imported module entry with an import attribute
11361 referring to the module and owned entries corresponding to those
11362 entities that are renamed as part of being imported. */
11363
11364 if (child_die->tag != DW_TAG_imported_declaration)
11365 {
b98664d3 11366 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11367 "- DIE at %s [in module %s]"),
11368 sect_offset_str (child_die->sect_off),
11369 objfile_name (objfile));
32019081
JK
11370 continue;
11371 }
11372
11373 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11374 if (import_attr == NULL)
11375 {
b98664d3 11376 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
11377 dwarf_tag_name (child_die->tag));
11378 continue;
11379 }
11380
11381 imported_cu = cu;
11382 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11383 &imported_cu);
11384 imported_name = dwarf2_name (imported_die, imported_cu);
11385 if (imported_name == NULL)
11386 {
b98664d3 11387 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11388 "imported name - DIE at %s [in module %s]"),
11389 sect_offset_str (child_die->sect_off),
11390 objfile_name (objfile));
32019081
JK
11391 continue;
11392 }
11393
eb1e02fd 11394 excludes.push_back (imported_name);
32019081
JK
11395
11396 process_die (child_die, cu);
11397 }
11398
804d2729 11399 add_using_directive (using_directives (cu),
22cee43f
PMR
11400 import_prefix,
11401 canonical_name,
11402 import_alias,
11403 imported_declaration,
11404 excludes,
11405 0,
11406 &objfile->objfile_obstack);
27aa8d6a
SW
11407}
11408
5230b05a
WT
11409/* ICC<14 does not output the required DW_AT_declaration on incomplete
11410 types, but gives them a size of zero. Starting with version 14,
11411 ICC is compatible with GCC. */
11412
9068261f 11413static bool
5230b05a
WT
11414producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11415{
11416 if (!cu->checked_producer)
11417 check_producer (cu);
11418
11419 return cu->producer_is_icc_lt_14;
11420}
11421
eb77c9df
AB
11422/* ICC generates a DW_AT_type for C void functions. This was observed on
11423 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11424 which says that void functions should not have a DW_AT_type. */
11425
11426static bool
11427producer_is_icc (struct dwarf2_cu *cu)
11428{
11429 if (!cu->checked_producer)
11430 check_producer (cu);
11431
11432 return cu->producer_is_icc;
11433}
11434
1b80a9fa
JK
11435/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11436 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11437 this, it was first present in GCC release 4.3.0. */
11438
9068261f 11439static bool
1b80a9fa
JK
11440producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11441{
11442 if (!cu->checked_producer)
11443 check_producer (cu);
11444
11445 return cu->producer_is_gcc_lt_4_3;
11446}
11447
d721ba37
PA
11448static file_and_directory
11449find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11450{
d721ba37
PA
11451 file_and_directory res;
11452
9291a0cd
TT
11453 /* Find the filename. Do not use dwarf2_name here, since the filename
11454 is not a source language identifier. */
d721ba37
PA
11455 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11456 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11457
d721ba37
PA
11458 if (res.comp_dir == NULL
11459 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11460 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11461 {
d721ba37
PA
11462 res.comp_dir_storage = ldirname (res.name);
11463 if (!res.comp_dir_storage.empty ())
11464 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11465 }
d721ba37 11466 if (res.comp_dir != NULL)
9291a0cd
TT
11467 {
11468 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11469 directory, get rid of it. */
d721ba37 11470 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11471
d721ba37
PA
11472 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11473 res.comp_dir = cp + 1;
9291a0cd
TT
11474 }
11475
d721ba37
PA
11476 if (res.name == NULL)
11477 res.name = "<unknown>";
11478
11479 return res;
9291a0cd
TT
11480}
11481
f4dc4d17
DE
11482/* Handle DW_AT_stmt_list for a compilation unit.
11483 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11484 COMP_DIR is the compilation directory. LOWPC is passed to
11485 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11486
11487static void
11488handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11489 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11490{
518817b3
SM
11491 struct dwarf2_per_objfile *dwarf2_per_objfile
11492 = cu->per_cu->dwarf2_per_objfile;
527f3840 11493 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11494 struct attribute *attr;
527f3840
JK
11495 struct line_header line_header_local;
11496 hashval_t line_header_local_hash;
527f3840
JK
11497 void **slot;
11498 int decode_mapping;
2ab95328 11499
f4dc4d17
DE
11500 gdb_assert (! cu->per_cu->is_debug_types);
11501
2ab95328 11502 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11503 if (attr == NULL)
11504 return;
11505
9c541725 11506 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11507
11508 /* The line header hash table is only created if needed (it exists to
11509 prevent redundant reading of the line table for partial_units).
11510 If we're given a partial_unit, we'll need it. If we're given a
11511 compile_unit, then use the line header hash table if it's already
11512 created, but don't create one just yet. */
11513
11514 if (dwarf2_per_objfile->line_header_hash == NULL
11515 && die->tag == DW_TAG_partial_unit)
2ab95328 11516 {
527f3840
JK
11517 dwarf2_per_objfile->line_header_hash
11518 = htab_create_alloc_ex (127, line_header_hash_voidp,
11519 line_header_eq_voidp,
11520 free_line_header_voidp,
11521 &objfile->objfile_obstack,
11522 hashtab_obstack_allocate,
11523 dummy_obstack_deallocate);
11524 }
2ab95328 11525
9c541725 11526 line_header_local.sect_off = line_offset;
527f3840
JK
11527 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11528 line_header_local_hash = line_header_hash (&line_header_local);
11529 if (dwarf2_per_objfile->line_header_hash != NULL)
11530 {
11531 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11532 &line_header_local,
11533 line_header_local_hash, NO_INSERT);
11534
11535 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11536 is not present in *SLOT (since if there is something in *SLOT then
11537 it will be for a partial_unit). */
11538 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11539 {
527f3840 11540 gdb_assert (*slot != NULL);
9a3c8263 11541 cu->line_header = (struct line_header *) *slot;
527f3840 11542 return;
dee91e82 11543 }
2ab95328 11544 }
527f3840
JK
11545
11546 /* dwarf_decode_line_header does not yet provide sufficient information.
11547 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11548 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11549 if (lh == NULL)
527f3840 11550 return;
4c8aa72d
PA
11551
11552 cu->line_header = lh.release ();
11553 cu->line_header_die_owner = die;
527f3840
JK
11554
11555 if (dwarf2_per_objfile->line_header_hash == NULL)
11556 slot = NULL;
11557 else
11558 {
11559 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11560 &line_header_local,
11561 line_header_local_hash, INSERT);
11562 gdb_assert (slot != NULL);
11563 }
11564 if (slot != NULL && *slot == NULL)
11565 {
11566 /* This newly decoded line number information unit will be owned
11567 by line_header_hash hash table. */
11568 *slot = cu->line_header;
4c8aa72d 11569 cu->line_header_die_owner = NULL;
527f3840
JK
11570 }
11571 else
11572 {
11573 /* We cannot free any current entry in (*slot) as that struct line_header
11574 may be already used by multiple CUs. Create only temporary decoded
11575 line_header for this CU - it may happen at most once for each line
11576 number information unit. And if we're not using line_header_hash
11577 then this is what we want as well. */
11578 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11579 }
11580 decode_mapping = (die->tag != DW_TAG_partial_unit);
11581 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11582 decode_mapping);
fff8551c 11583
2ab95328
TT
11584}
11585
95554aad 11586/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11587
c906108c 11588static void
e7c27a73 11589read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11590{
518817b3
SM
11591 struct dwarf2_per_objfile *dwarf2_per_objfile
11592 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11593 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11594 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11595 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11596 CORE_ADDR highpc = ((CORE_ADDR) 0);
11597 struct attribute *attr;
c906108c 11598 struct die_info *child_die;
e142c38c 11599 CORE_ADDR baseaddr;
6e70227d 11600
380618d6 11601 prepare_one_comp_unit (cu, die, cu->language);
6a053cb1 11602 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
c906108c 11603
fae299cd 11604 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11605
11606 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11607 from finish_block. */
2acceee2 11608 if (lowpc == ((CORE_ADDR) -1))
c906108c 11609 lowpc = highpc;
3e29f34a 11610 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11611
d721ba37 11612 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11613
f4b8a18d
KW
11614 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11615 standardised yet. As a workaround for the language detection we fall
11616 back to the DW_AT_producer string. */
11617 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11618 cu->language = language_opencl;
11619
3019eac3
DE
11620 /* Similar hack for Go. */
11621 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11622 set_cu_language (DW_LANG_Go, cu);
11623
c24bdb02 11624 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11625
11626 /* Decode line number information if present. We do this before
11627 processing child DIEs, so that the line header table is available
11628 for DW_AT_decl_file. */
d721ba37 11629 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11630
11631 /* Process all dies in compilation unit. */
11632 if (die->child != NULL)
11633 {
11634 child_die = die->child;
11635 while (child_die && child_die->tag)
11636 {
11637 process_die (child_die, cu);
11638 child_die = sibling_die (child_die);
11639 }
11640 }
11641
11642 /* Decode macro information, if present. Dwarf 2 macro information
11643 refers to information in the line number info statement program
11644 header, so we can only read it if we've read the header
11645 successfully. */
0af92d60
JK
11646 attr = dwarf2_attr (die, DW_AT_macros, cu);
11647 if (attr == NULL)
11648 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11649 if (attr && cu->line_header)
11650 {
11651 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11652 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11653
43f3e411 11654 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11655 }
11656 else
11657 {
11658 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11659 if (attr && cu->line_header)
11660 {
11661 unsigned int macro_offset = DW_UNSND (attr);
11662
43f3e411 11663 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11664 }
11665 }
3019eac3
DE
11666}
11667
c24bdb02
KS
11668void
11669dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11670{
f4dc4d17
DE
11671 struct type_unit_group *tu_group;
11672 int first_time;
3019eac3 11673 struct attribute *attr;
9c541725 11674 unsigned int i;
0186c6a7 11675 struct signatured_type *sig_type;
3019eac3 11676
f4dc4d17 11677 gdb_assert (per_cu->is_debug_types);
0186c6a7 11678 sig_type = (struct signatured_type *) per_cu;
3019eac3 11679
c24bdb02 11680 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11681
f4dc4d17 11682 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11683 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11684 if (sig_type->type_unit_group == NULL)
c24bdb02 11685 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11686 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11687
11688 /* If we've already processed this stmt_list there's no real need to
11689 do it again, we could fake it and just recreate the part we need
11690 (file name,index -> symtab mapping). If data shows this optimization
11691 is useful we can do it then. */
43f3e411 11692 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11693
11694 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11695 debug info. */
fff8551c 11696 line_header_up lh;
f4dc4d17 11697 if (attr != NULL)
3019eac3 11698 {
9c541725 11699 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11700 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11701 }
11702 if (lh == NULL)
11703 {
11704 if (first_time)
c24bdb02 11705 start_symtab ("", NULL, 0);
f4dc4d17
DE
11706 else
11707 {
11708 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 11709 gdb_assert (m_builder == nullptr);
804d2729 11710 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11711 m_builder.reset (new struct buildsym_compunit
11712 (COMPUNIT_OBJFILE (cust), "",
11713 COMPUNIT_DIRNAME (cust),
11714 compunit_language (cust),
11715 0, cust));
f4dc4d17 11716 }
f4dc4d17 11717 return;
3019eac3
DE
11718 }
11719
c24bdb02
KS
11720 line_header = lh.release ();
11721 line_header_die_owner = die;
3019eac3 11722
f4dc4d17
DE
11723 if (first_time)
11724 {
c24bdb02 11725 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11726
1fd60fc0
DE
11727 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11728 still initializing it, and our caller (a few levels up)
11729 process_full_type_unit still needs to know if this is the first
11730 time. */
11731
7ba99d21 11732 tu_group->num_symtabs = line_header->file_names_size ();
4c8aa72d 11733 tu_group->symtabs = XNEWVEC (struct symtab *,
7ba99d21 11734 line_header->file_names_size ());
3019eac3 11735
7ba99d21
AT
11736 auto &file_names = line_header->file_names ();
11737 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11738 {
7ba99d21 11739 file_entry &fe = file_names[i];
c24bdb02
KS
11740 dwarf2_start_subfile (this, fe.name,
11741 fe.include_dir (line_header));
11742 buildsym_compunit *b = get_builder ();
11743 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11744 {
4c8aa72d
PA
11745 /* NOTE: start_subfile will recognize when it's been
11746 passed a file it has already seen. So we can't
11747 assume there's a simple mapping from
11748 cu->line_header->file_names to subfiles, plus
11749 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11750 b->get_current_subfile ()->symtab
11751 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11752 }
11753
c24bdb02 11754 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11755 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11756 }
11757 }
11758 else
3019eac3 11759 {
c24bdb02 11760 gdb_assert (m_builder == nullptr);
804d2729 11761 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11762 m_builder.reset (new struct buildsym_compunit
11763 (COMPUNIT_OBJFILE (cust), "",
11764 COMPUNIT_DIRNAME (cust),
11765 compunit_language (cust),
11766 0, cust));
f4dc4d17 11767
7ba99d21
AT
11768 auto &file_names = line_header->file_names ();
11769 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11770 {
7ba99d21 11771 file_entry &fe = file_names[i];
4c8aa72d 11772 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11773 }
3019eac3
DE
11774 }
11775
f4dc4d17
DE
11776 /* The main symtab is allocated last. Type units don't have DW_AT_name
11777 so they don't have a "real" (so to speak) symtab anyway.
11778 There is later code that will assign the main symtab to all symbols
11779 that don't have one. We need to handle the case of a symbol with a
11780 missing symtab (DW_AT_decl_file) anyway. */
11781}
3019eac3 11782
f4dc4d17
DE
11783/* Process DW_TAG_type_unit.
11784 For TUs we want to skip the first top level sibling if it's not the
11785 actual type being defined by this TU. In this case the first top
11786 level sibling is there to provide context only. */
3019eac3 11787
f4dc4d17
DE
11788static void
11789read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11790{
11791 struct die_info *child_die;
3019eac3 11792
f4dc4d17
DE
11793 prepare_one_comp_unit (cu, die, language_minimal);
11794
11795 /* Initialize (or reinitialize) the machinery for building symtabs.
11796 We do this before processing child DIEs, so that the line header table
11797 is available for DW_AT_decl_file. */
c24bdb02 11798 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11799
11800 if (die->child != NULL)
11801 {
11802 child_die = die->child;
11803 while (child_die && child_die->tag)
11804 {
11805 process_die (child_die, cu);
11806 child_die = sibling_die (child_die);
11807 }
11808 }
3019eac3
DE
11809}
11810\f
80626a55
DE
11811/* DWO/DWP files.
11812
11813 http://gcc.gnu.org/wiki/DebugFission
11814 http://gcc.gnu.org/wiki/DebugFissionDWP
11815
11816 To simplify handling of both DWO files ("object" files with the DWARF info)
11817 and DWP files (a file with the DWOs packaged up into one file), we treat
11818 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11819
11820static hashval_t
11821hash_dwo_file (const void *item)
11822{
9a3c8263 11823 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11824 hashval_t hash;
3019eac3 11825
a2ce51a0
DE
11826 hash = htab_hash_string (dwo_file->dwo_name);
11827 if (dwo_file->comp_dir != NULL)
11828 hash += htab_hash_string (dwo_file->comp_dir);
11829 return hash;
3019eac3
DE
11830}
11831
11832static int
11833eq_dwo_file (const void *item_lhs, const void *item_rhs)
11834{
9a3c8263
SM
11835 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11836 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11837
a2ce51a0
DE
11838 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11839 return 0;
11840 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11841 return lhs->comp_dir == rhs->comp_dir;
11842 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11843}
11844
11845/* Allocate a hash table for DWO files. */
11846
51ac9db5 11847static htab_up
ed2dc618 11848allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11849{
51ac9db5
SM
11850 auto delete_dwo_file = [] (void *item)
11851 {
11852 struct dwo_file *dwo_file = (struct dwo_file *) item;
11853
11854 delete dwo_file;
11855 };
11856
11857 return htab_up (htab_create_alloc_ex (41,
11858 hash_dwo_file,
11859 eq_dwo_file,
11860 delete_dwo_file,
11861 &objfile->objfile_obstack,
11862 hashtab_obstack_allocate,
11863 dummy_obstack_deallocate));
3019eac3
DE
11864}
11865
80626a55
DE
11866/* Lookup DWO file DWO_NAME. */
11867
11868static void **
ed2dc618
SM
11869lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11870 const char *dwo_name,
11871 const char *comp_dir)
80626a55
DE
11872{
11873 struct dwo_file find_entry;
11874 void **slot;
11875
11876 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11877 dwarf2_per_objfile->dwo_files
11878 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55 11879
0ac5b59e
DE
11880 find_entry.dwo_name = dwo_name;
11881 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11882 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11883 INSERT);
80626a55
DE
11884
11885 return slot;
11886}
11887
3019eac3
DE
11888static hashval_t
11889hash_dwo_unit (const void *item)
11890{
9a3c8263 11891 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11892
11893 /* This drops the top 32 bits of the id, but is ok for a hash. */
11894 return dwo_unit->signature;
11895}
11896
11897static int
11898eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11899{
9a3c8263
SM
11900 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11901 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11902
11903 /* The signature is assumed to be unique within the DWO file.
11904 So while object file CU dwo_id's always have the value zero,
11905 that's OK, assuming each object file DWO file has only one CU,
11906 and that's the rule for now. */
11907 return lhs->signature == rhs->signature;
11908}
11909
11910/* Allocate a hash table for DWO CUs,TUs.
11911 There is one of these tables for each of CUs,TUs for each DWO file. */
11912
11913static htab_t
11914allocate_dwo_unit_table (struct objfile *objfile)
11915{
11916 /* Start out with a pretty small number.
11917 Generally DWO files contain only one CU and maybe some TUs. */
11918 return htab_create_alloc_ex (3,
11919 hash_dwo_unit,
11920 eq_dwo_unit,
11921 NULL,
11922 &objfile->objfile_obstack,
11923 hashtab_obstack_allocate,
11924 dummy_obstack_deallocate);
11925}
11926
80626a55 11927/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 11928
19c3d4c9 11929struct create_dwo_cu_data
3019eac3
DE
11930{
11931 struct dwo_file *dwo_file;
19c3d4c9 11932 struct dwo_unit dwo_unit;
3019eac3
DE
11933};
11934
19c3d4c9 11935/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11936
11937static void
19c3d4c9
DE
11938create_dwo_cu_reader (const struct die_reader_specs *reader,
11939 const gdb_byte *info_ptr,
11940 struct die_info *comp_unit_die,
11941 int has_children,
11942 void *datap)
3019eac3
DE
11943{
11944 struct dwarf2_cu *cu = reader->cu;
9c541725 11945 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11946 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 11947 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 11948 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 11949 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 11950
a084a2a6
AT
11951 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11952 if (!signature.has_value ())
3019eac3 11953 {
b98664d3 11954 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11955 " its dwo_id [in module %s]"),
9d8780f0 11956 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11957 return;
11958 }
11959
3019eac3 11960 dwo_unit->dwo_file = dwo_file;
a084a2a6 11961 dwo_unit->signature = *signature;
8a0459fd 11962 dwo_unit->section = section;
9c541725 11963 dwo_unit->sect_off = sect_off;
3019eac3
DE
11964 dwo_unit->length = cu->per_cu->length;
11965
b4f54984 11966 if (dwarf_read_debug)
9d8780f0
SM
11967 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11968 sect_offset_str (sect_off),
9c541725 11969 hex_string (dwo_unit->signature));
3019eac3
DE
11970}
11971
33c5cd75 11972/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11973 Note: This function processes DWO files only, not DWP files. */
3019eac3 11974
33c5cd75 11975static void
ed2dc618
SM
11976create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11977 struct dwo_file &dwo_file, dwarf2_section_info &section,
33c5cd75 11978 htab_t &cus_htab)
3019eac3
DE
11979{
11980 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11981 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11982
33c5cd75
DB
11983 dwarf2_read_section (objfile, &section);
11984 info_ptr = section.buffer;
3019eac3
DE
11985
11986 if (info_ptr == NULL)
33c5cd75 11987 return;
3019eac3 11988
b4f54984 11989 if (dwarf_read_debug)
19c3d4c9
DE
11990 {
11991 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
11992 get_section_name (&section),
11993 get_section_file_name (&section));
19c3d4c9 11994 }
3019eac3 11995
33c5cd75 11996 end_ptr = info_ptr + section.size;
3019eac3
DE
11997 while (info_ptr < end_ptr)
11998 {
11999 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
12000 struct create_dwo_cu_data create_dwo_cu_data;
12001 struct dwo_unit *dwo_unit;
12002 void **slot;
12003 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 12004
19c3d4c9
DE
12005 memset (&create_dwo_cu_data.dwo_unit, 0,
12006 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3 12007 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 12008 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 12009 per_cu.is_debug_types = 0;
33c5cd75
DB
12010 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12011 per_cu.section = &section;
c5ed0576 12012 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
12013
12014 init_cutu_and_read_dies_no_follow (
12015 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12016 info_ptr += per_cu.length;
12017
12018 // If the unit could not be parsed, skip it.
12019 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12020 continue;
3019eac3 12021
33c5cd75
DB
12022 if (cus_htab == NULL)
12023 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 12024
33c5cd75
DB
12025 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12026 *dwo_unit = create_dwo_cu_data.dwo_unit;
12027 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12028 gdb_assert (slot != NULL);
12029 if (*slot != NULL)
19c3d4c9 12030 {
33c5cd75
DB
12031 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12032 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 12033
b98664d3 12034 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
12035 " the entry at offset %s, signature %s"),
12036 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 12037 hex_string (dwo_unit->signature));
19c3d4c9 12038 }
33c5cd75 12039 *slot = (void *)dwo_unit;
3019eac3 12040 }
3019eac3
DE
12041}
12042
80626a55
DE
12043/* DWP file .debug_{cu,tu}_index section format:
12044 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12045
d2415c6c
DE
12046 DWP Version 1:
12047
80626a55
DE
12048 Both index sections have the same format, and serve to map a 64-bit
12049 signature to a set of section numbers. Each section begins with a header,
12050 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12051 indexes, and a pool of 32-bit section numbers. The index sections will be
12052 aligned at 8-byte boundaries in the file.
12053
d2415c6c
DE
12054 The index section header consists of:
12055
12056 V, 32 bit version number
12057 -, 32 bits unused
12058 N, 32 bit number of compilation units or type units in the index
12059 M, 32 bit number of slots in the hash table
80626a55 12060
d2415c6c 12061 Numbers are recorded using the byte order of the application binary.
80626a55 12062
d2415c6c
DE
12063 The hash table begins at offset 16 in the section, and consists of an array
12064 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12065 order of the application binary). Unused slots in the hash table are 0.
12066 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 12067
d2415c6c
DE
12068 The parallel table begins immediately after the hash table
12069 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12070 array of 32-bit indexes (using the byte order of the application binary),
12071 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12072 table contains a 32-bit index into the pool of section numbers. For unused
12073 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 12074
73869dc2
DE
12075 The pool of section numbers begins immediately following the hash table
12076 (at offset 16 + 12 * M from the beginning of the section). The pool of
12077 section numbers consists of an array of 32-bit words (using the byte order
12078 of the application binary). Each item in the array is indexed starting
12079 from 0. The hash table entry provides the index of the first section
12080 number in the set. Additional section numbers in the set follow, and the
12081 set is terminated by a 0 entry (section number 0 is not used in ELF).
12082
12083 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12084 section must be the first entry in the set, and the .debug_abbrev.dwo must
12085 be the second entry. Other members of the set may follow in any order.
12086
12087 ---
12088
12089 DWP Version 2:
12090
12091 DWP Version 2 combines all the .debug_info, etc. sections into one,
12092 and the entries in the index tables are now offsets into these sections.
12093 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12094 section.
12095
12096 Index Section Contents:
12097 Header
12098 Hash Table of Signatures dwp_hash_table.hash_table
12099 Parallel Table of Indices dwp_hash_table.unit_table
12100 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12101 Table of Section Sizes dwp_hash_table.v2.sizes
12102
12103 The index section header consists of:
12104
12105 V, 32 bit version number
12106 L, 32 bit number of columns in the table of section offsets
12107 N, 32 bit number of compilation units or type units in the index
12108 M, 32 bit number of slots in the hash table
12109
12110 Numbers are recorded using the byte order of the application binary.
12111
12112 The hash table has the same format as version 1.
12113 The parallel table of indices has the same format as version 1,
12114 except that the entries are origin-1 indices into the table of sections
12115 offsets and the table of section sizes.
12116
12117 The table of offsets begins immediately following the parallel table
12118 (at offset 16 + 12 * M from the beginning of the section). The table is
12119 a two-dimensional array of 32-bit words (using the byte order of the
12120 application binary), with L columns and N+1 rows, in row-major order.
12121 Each row in the array is indexed starting from 0. The first row provides
12122 a key to the remaining rows: each column in this row provides an identifier
12123 for a debug section, and the offsets in the same column of subsequent rows
12124 refer to that section. The section identifiers are:
12125
12126 DW_SECT_INFO 1 .debug_info.dwo
12127 DW_SECT_TYPES 2 .debug_types.dwo
12128 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12129 DW_SECT_LINE 4 .debug_line.dwo
12130 DW_SECT_LOC 5 .debug_loc.dwo
12131 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12132 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12133 DW_SECT_MACRO 8 .debug_macro.dwo
12134
12135 The offsets provided by the CU and TU index sections are the base offsets
12136 for the contributions made by each CU or TU to the corresponding section
12137 in the package file. Each CU and TU header contains an abbrev_offset
12138 field, used to find the abbreviations table for that CU or TU within the
12139 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12140 be interpreted as relative to the base offset given in the index section.
12141 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12142 should be interpreted as relative to the base offset for .debug_line.dwo,
12143 and offsets into other debug sections obtained from DWARF attributes should
12144 also be interpreted as relative to the corresponding base offset.
12145
12146 The table of sizes begins immediately following the table of offsets.
12147 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12148 with L columns and N rows, in row-major order. Each row in the array is
12149 indexed starting from 1 (row 0 is shared by the two tables).
12150
12151 ---
12152
12153 Hash table lookup is handled the same in version 1 and 2:
12154
12155 We assume that N and M will not exceed 2^32 - 1.
12156 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12157
d2415c6c
DE
12158 Given a 64-bit compilation unit signature or a type signature S, an entry
12159 in the hash table is located as follows:
80626a55 12160
d2415c6c
DE
12161 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12162 the low-order k bits all set to 1.
80626a55 12163
d2415c6c 12164 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12165
d2415c6c
DE
12166 3) If the hash table entry at index H matches the signature, use that
12167 entry. If the hash table entry at index H is unused (all zeroes),
12168 terminate the search: the signature is not present in the table.
80626a55 12169
d2415c6c 12170 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12171
d2415c6c 12172 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12173 to stop at an unused slot or find the match. */
80626a55
DE
12174
12175/* Create a hash table to map DWO IDs to their CU/TU entry in
12176 .debug_{info,types}.dwo in DWP_FILE.
12177 Returns NULL if there isn't one.
12178 Note: This function processes DWP files only, not DWO files. */
12179
12180static struct dwp_hash_table *
ed2dc618
SM
12181create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12182 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12183{
12184 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 12185 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 12186 const gdb_byte *index_ptr, *index_end;
80626a55 12187 struct dwarf2_section_info *index;
73869dc2 12188 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12189 struct dwp_hash_table *htab;
12190
12191 if (is_debug_types)
12192 index = &dwp_file->sections.tu_index;
12193 else
12194 index = &dwp_file->sections.cu_index;
12195
12196 if (dwarf2_section_empty_p (index))
12197 return NULL;
12198 dwarf2_read_section (objfile, index);
12199
12200 index_ptr = index->buffer;
12201 index_end = index_ptr + index->size;
12202
12203 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12204 index_ptr += 4;
12205 if (version == 2)
12206 nr_columns = read_4_bytes (dbfd, index_ptr);
12207 else
12208 nr_columns = 0;
12209 index_ptr += 4;
80626a55
DE
12210 nr_units = read_4_bytes (dbfd, index_ptr);
12211 index_ptr += 4;
12212 nr_slots = read_4_bytes (dbfd, index_ptr);
12213 index_ptr += 4;
12214
73869dc2 12215 if (version != 1 && version != 2)
80626a55 12216 {
21aa081e 12217 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12218 " [in module %s]"),
21aa081e 12219 pulongest (version), dwp_file->name);
80626a55
DE
12220 }
12221 if (nr_slots != (nr_slots & -nr_slots))
12222 {
21aa081e 12223 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12224 " is not power of 2 [in module %s]"),
21aa081e 12225 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12226 }
12227
12228 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12229 htab->version = version;
12230 htab->nr_columns = nr_columns;
80626a55
DE
12231 htab->nr_units = nr_units;
12232 htab->nr_slots = nr_slots;
12233 htab->hash_table = index_ptr;
12234 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12235
12236 /* Exit early if the table is empty. */
12237 if (nr_slots == 0 || nr_units == 0
12238 || (version == 2 && nr_columns == 0))
12239 {
12240 /* All must be zero. */
12241 if (nr_slots != 0 || nr_units != 0
12242 || (version == 2 && nr_columns != 0))
12243 {
b98664d3 12244 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
12245 " all zero [in modules %s]"),
12246 dwp_file->name);
12247 }
12248 return htab;
12249 }
12250
12251 if (version == 1)
12252 {
12253 htab->section_pool.v1.indices =
12254 htab->unit_table + sizeof (uint32_t) * nr_slots;
12255 /* It's harder to decide whether the section is too small in v1.
12256 V1 is deprecated anyway so we punt. */
12257 }
12258 else
12259 {
12260 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12261 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 12262 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
12263 /* Reverse map for error checking. */
12264 int ids_seen[DW_SECT_MAX + 1];
12265 int i;
12266
12267 if (nr_columns < 2)
12268 {
12269 error (_("Dwarf Error: bad DWP hash table, too few columns"
12270 " in section table [in module %s]"),
12271 dwp_file->name);
12272 }
12273 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12274 {
12275 error (_("Dwarf Error: bad DWP hash table, too many columns"
12276 " in section table [in module %s]"),
12277 dwp_file->name);
12278 }
04fd5eed
GB
12279 memset (ids, 255, sizeof_ids);
12280 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
12281 for (i = 0; i < nr_columns; ++i)
12282 {
12283 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12284
12285 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12286 {
12287 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12288 " in section table [in module %s]"),
12289 id, dwp_file->name);
12290 }
12291 if (ids_seen[id] != -1)
12292 {
12293 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12294 " id %d in section table [in module %s]"),
12295 id, dwp_file->name);
12296 }
12297 ids_seen[id] = i;
12298 ids[i] = id;
12299 }
12300 /* Must have exactly one info or types section. */
12301 if (((ids_seen[DW_SECT_INFO] != -1)
12302 + (ids_seen[DW_SECT_TYPES] != -1))
12303 != 1)
12304 {
12305 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12306 " DWO info/types section [in module %s]"),
12307 dwp_file->name);
12308 }
12309 /* Must have an abbrev section. */
12310 if (ids_seen[DW_SECT_ABBREV] == -1)
12311 {
12312 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12313 " section [in module %s]"),
12314 dwp_file->name);
12315 }
12316 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12317 htab->section_pool.v2.sizes =
12318 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12319 * nr_units * nr_columns);
12320 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12321 * nr_units * nr_columns))
12322 > index_end)
12323 {
12324 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12325 " [in module %s]"),
12326 dwp_file->name);
12327 }
12328 }
80626a55
DE
12329
12330 return htab;
12331}
12332
12333/* Update SECTIONS with the data from SECTP.
12334
12335 This function is like the other "locate" section routines that are
12336 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12337 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12338
12339 The result is non-zero for success, or zero if an error was found. */
12340
12341static int
73869dc2
DE
12342locate_v1_virtual_dwo_sections (asection *sectp,
12343 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12344{
12345 const struct dwop_section_names *names = &dwop_section_names;
12346
12347 if (section_is_p (sectp->name, &names->abbrev_dwo))
12348 {
12349 /* There can be only one. */
049412e3 12350 if (sections->abbrev.s.section != NULL)
80626a55 12351 return 0;
049412e3 12352 sections->abbrev.s.section = sectp;
fd361982 12353 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12354 }
12355 else if (section_is_p (sectp->name, &names->info_dwo)
12356 || section_is_p (sectp->name, &names->types_dwo))
12357 {
12358 /* There can be only one. */
049412e3 12359 if (sections->info_or_types.s.section != NULL)
80626a55 12360 return 0;
049412e3 12361 sections->info_or_types.s.section = sectp;
fd361982 12362 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
12363 }
12364 else if (section_is_p (sectp->name, &names->line_dwo))
12365 {
12366 /* There can be only one. */
049412e3 12367 if (sections->line.s.section != NULL)
80626a55 12368 return 0;
049412e3 12369 sections->line.s.section = sectp;
fd361982 12370 sections->line.size = bfd_section_size (sectp);
80626a55
DE
12371 }
12372 else if (section_is_p (sectp->name, &names->loc_dwo))
12373 {
12374 /* There can be only one. */
049412e3 12375 if (sections->loc.s.section != NULL)
80626a55 12376 return 0;
049412e3 12377 sections->loc.s.section = sectp;
fd361982 12378 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12379 }
12380 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12381 {
12382 /* There can be only one. */
049412e3 12383 if (sections->macinfo.s.section != NULL)
80626a55 12384 return 0;
049412e3 12385 sections->macinfo.s.section = sectp;
fd361982 12386 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12387 }
12388 else if (section_is_p (sectp->name, &names->macro_dwo))
12389 {
12390 /* There can be only one. */
049412e3 12391 if (sections->macro.s.section != NULL)
80626a55 12392 return 0;
049412e3 12393 sections->macro.s.section = sectp;
fd361982 12394 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12395 }
12396 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12397 {
12398 /* There can be only one. */
049412e3 12399 if (sections->str_offsets.s.section != NULL)
80626a55 12400 return 0;
049412e3 12401 sections->str_offsets.s.section = sectp;
fd361982 12402 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12403 }
12404 else
12405 {
12406 /* No other kind of section is valid. */
12407 return 0;
12408 }
12409
12410 return 1;
12411}
12412
73869dc2
DE
12413/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12414 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12415 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12416 This is for DWP version 1 files. */
80626a55
DE
12417
12418static struct dwo_unit *
ed2dc618
SM
12419create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12420 struct dwp_file *dwp_file,
73869dc2
DE
12421 uint32_t unit_index,
12422 const char *comp_dir,
12423 ULONGEST signature, int is_debug_types)
80626a55
DE
12424{
12425 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12426 const struct dwp_hash_table *dwp_htab =
12427 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12428 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12429 const char *kind = is_debug_types ? "TU" : "CU";
12430 struct dwo_file *dwo_file;
12431 struct dwo_unit *dwo_unit;
73869dc2 12432 struct virtual_v1_dwo_sections sections;
80626a55 12433 void **dwo_file_slot;
80626a55
DE
12434 int i;
12435
73869dc2
DE
12436 gdb_assert (dwp_file->version == 1);
12437
b4f54984 12438 if (dwarf_read_debug)
80626a55 12439 {
73869dc2 12440 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12441 kind,
73869dc2 12442 pulongest (unit_index), hex_string (signature),
80626a55
DE
12443 dwp_file->name);
12444 }
12445
19ac8c2e 12446 /* Fetch the sections of this DWO unit.
80626a55
DE
12447 Put a limit on the number of sections we look for so that bad data
12448 doesn't cause us to loop forever. */
12449
73869dc2 12450#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12451 (1 /* .debug_info or .debug_types */ \
12452 + 1 /* .debug_abbrev */ \
12453 + 1 /* .debug_line */ \
12454 + 1 /* .debug_loc */ \
12455 + 1 /* .debug_str_offsets */ \
19ac8c2e 12456 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12457 + 1 /* trailing zero */)
12458
12459 memset (&sections, 0, sizeof (sections));
80626a55 12460
73869dc2 12461 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12462 {
12463 asection *sectp;
12464 uint32_t section_nr =
12465 read_4_bytes (dbfd,
73869dc2
DE
12466 dwp_htab->section_pool.v1.indices
12467 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12468
12469 if (section_nr == 0)
12470 break;
12471 if (section_nr >= dwp_file->num_sections)
12472 {
12473 error (_("Dwarf Error: bad DWP hash table, section number too large"
12474 " [in module %s]"),
12475 dwp_file->name);
12476 }
12477
12478 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12479 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12480 {
12481 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12482 " [in module %s]"),
12483 dwp_file->name);
12484 }
12485 }
12486
12487 if (i < 2
a32a8923
DE
12488 || dwarf2_section_empty_p (&sections.info_or_types)
12489 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12490 {
12491 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12492 " [in module %s]"),
12493 dwp_file->name);
12494 }
73869dc2 12495 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12496 {
12497 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12498 " [in module %s]"),
12499 dwp_file->name);
12500 }
12501
12502 /* It's easier for the rest of the code if we fake a struct dwo_file and
12503 have dwo_unit "live" in that. At least for now.
12504
12505 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12506 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12507 file, we can combine them back into a virtual DWO file to save space
12508 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12509 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12510
791afaa2
TT
12511 std::string virtual_dwo_name =
12512 string_printf ("virtual-dwo/%d-%d-%d-%d",
12513 get_section_id (&sections.abbrev),
12514 get_section_id (&sections.line),
12515 get_section_id (&sections.loc),
12516 get_section_id (&sections.str_offsets));
80626a55 12517 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12518 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12519 virtual_dwo_name.c_str (),
12520 comp_dir);
80626a55
DE
12521 /* Create one if necessary. */
12522 if (*dwo_file_slot == NULL)
12523 {
b4f54984 12524 if (dwarf_read_debug)
80626a55
DE
12525 {
12526 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12527 virtual_dwo_name.c_str ());
80626a55 12528 }
51ac9db5 12529 dwo_file = new struct dwo_file;
efba19b0
TT
12530 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12531 virtual_dwo_name);
0ac5b59e 12532 dwo_file->comp_dir = comp_dir;
80626a55
DE
12533 dwo_file->sections.abbrev = sections.abbrev;
12534 dwo_file->sections.line = sections.line;
12535 dwo_file->sections.loc = sections.loc;
12536 dwo_file->sections.macinfo = sections.macinfo;
12537 dwo_file->sections.macro = sections.macro;
12538 dwo_file->sections.str_offsets = sections.str_offsets;
12539 /* The "str" section is global to the entire DWP file. */
12540 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12541 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12542 there's no need to record it in dwo_file.
12543 Also, we can't simply record type sections in dwo_file because
12544 we record a pointer into the vector in dwo_unit. As we collect more
12545 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12546 for it, invalidating all copies of pointers into the previous
12547 contents. */
80626a55
DE
12548 *dwo_file_slot = dwo_file;
12549 }
12550 else
12551 {
b4f54984 12552 if (dwarf_read_debug)
80626a55
DE
12553 {
12554 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12555 virtual_dwo_name.c_str ());
80626a55 12556 }
9a3c8263 12557 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12558 }
80626a55
DE
12559
12560 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12561 dwo_unit->dwo_file = dwo_file;
12562 dwo_unit->signature = signature;
8d749320
SM
12563 dwo_unit->section =
12564 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12565 *dwo_unit->section = sections.info_or_types;
57d63ce2 12566 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12567
12568 return dwo_unit;
12569}
12570
73869dc2
DE
12571/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12572 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12573 piece within that section used by a TU/CU, return a virtual section
12574 of just that piece. */
12575
12576static struct dwarf2_section_info
ed2dc618
SM
12577create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12578 struct dwarf2_section_info *section,
73869dc2
DE
12579 bfd_size_type offset, bfd_size_type size)
12580{
12581 struct dwarf2_section_info result;
12582 asection *sectp;
12583
12584 gdb_assert (section != NULL);
12585 gdb_assert (!section->is_virtual);
12586
12587 memset (&result, 0, sizeof (result));
12588 result.s.containing_section = section;
dc4ccb6f 12589 result.is_virtual = true;
73869dc2
DE
12590
12591 if (size == 0)
12592 return result;
12593
12594 sectp = get_section_bfd_section (section);
12595
12596 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12597 bounds of the real section. This is a pretty-rare event, so just
12598 flag an error (easier) instead of a warning and trying to cope. */
12599 if (sectp == NULL
fd361982 12600 || offset + size > bfd_section_size (sectp))
73869dc2 12601 {
73869dc2
DE
12602 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12603 " in section %s [in module %s]"),
fd361982 12604 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
12605 objfile_name (dwarf2_per_objfile->objfile));
12606 }
12607
12608 result.virtual_offset = offset;
12609 result.size = size;
12610 return result;
12611}
12612
12613/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12614 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12615 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12616 This is for DWP version 2 files. */
12617
12618static struct dwo_unit *
ed2dc618
SM
12619create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12620 struct dwp_file *dwp_file,
73869dc2
DE
12621 uint32_t unit_index,
12622 const char *comp_dir,
12623 ULONGEST signature, int is_debug_types)
12624{
12625 struct objfile *objfile = dwarf2_per_objfile->objfile;
12626 const struct dwp_hash_table *dwp_htab =
12627 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12628 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12629 const char *kind = is_debug_types ? "TU" : "CU";
12630 struct dwo_file *dwo_file;
12631 struct dwo_unit *dwo_unit;
12632 struct virtual_v2_dwo_sections sections;
12633 void **dwo_file_slot;
73869dc2
DE
12634 int i;
12635
12636 gdb_assert (dwp_file->version == 2);
12637
b4f54984 12638 if (dwarf_read_debug)
73869dc2
DE
12639 {
12640 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12641 kind,
12642 pulongest (unit_index), hex_string (signature),
12643 dwp_file->name);
12644 }
12645
12646 /* Fetch the section offsets of this DWO unit. */
12647
12648 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12649
12650 for (i = 0; i < dwp_htab->nr_columns; ++i)
12651 {
12652 uint32_t offset = read_4_bytes (dbfd,
12653 dwp_htab->section_pool.v2.offsets
12654 + (((unit_index - 1) * dwp_htab->nr_columns
12655 + i)
12656 * sizeof (uint32_t)));
12657 uint32_t size = read_4_bytes (dbfd,
12658 dwp_htab->section_pool.v2.sizes
12659 + (((unit_index - 1) * dwp_htab->nr_columns
12660 + i)
12661 * sizeof (uint32_t)));
12662
12663 switch (dwp_htab->section_pool.v2.section_ids[i])
12664 {
12665 case DW_SECT_INFO:
12666 case DW_SECT_TYPES:
12667 sections.info_or_types_offset = offset;
12668 sections.info_or_types_size = size;
12669 break;
12670 case DW_SECT_ABBREV:
12671 sections.abbrev_offset = offset;
12672 sections.abbrev_size = size;
12673 break;
12674 case DW_SECT_LINE:
12675 sections.line_offset = offset;
12676 sections.line_size = size;
12677 break;
12678 case DW_SECT_LOC:
12679 sections.loc_offset = offset;
12680 sections.loc_size = size;
12681 break;
12682 case DW_SECT_STR_OFFSETS:
12683 sections.str_offsets_offset = offset;
12684 sections.str_offsets_size = size;
12685 break;
12686 case DW_SECT_MACINFO:
12687 sections.macinfo_offset = offset;
12688 sections.macinfo_size = size;
12689 break;
12690 case DW_SECT_MACRO:
12691 sections.macro_offset = offset;
12692 sections.macro_size = size;
12693 break;
12694 }
12695 }
12696
12697 /* It's easier for the rest of the code if we fake a struct dwo_file and
12698 have dwo_unit "live" in that. At least for now.
12699
12700 The DWP file can be made up of a random collection of CUs and TUs.
12701 However, for each CU + set of TUs that came from the same original DWO
12702 file, we can combine them back into a virtual DWO file to save space
12703 (fewer struct dwo_file objects to allocate). Remember that for really
12704 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12705
791afaa2
TT
12706 std::string virtual_dwo_name =
12707 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12708 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12709 (long) (sections.line_size ? sections.line_offset : 0),
12710 (long) (sections.loc_size ? sections.loc_offset : 0),
12711 (long) (sections.str_offsets_size
12712 ? sections.str_offsets_offset : 0));
73869dc2 12713 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12714 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12715 virtual_dwo_name.c_str (),
12716 comp_dir);
73869dc2
DE
12717 /* Create one if necessary. */
12718 if (*dwo_file_slot == NULL)
12719 {
b4f54984 12720 if (dwarf_read_debug)
73869dc2
DE
12721 {
12722 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12723 virtual_dwo_name.c_str ());
73869dc2 12724 }
51ac9db5 12725 dwo_file = new struct dwo_file;
efba19b0
TT
12726 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12727 virtual_dwo_name);
73869dc2
DE
12728 dwo_file->comp_dir = comp_dir;
12729 dwo_file->sections.abbrev =
ed2dc618 12730 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12731 sections.abbrev_offset, sections.abbrev_size);
12732 dwo_file->sections.line =
ed2dc618 12733 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12734 sections.line_offset, sections.line_size);
12735 dwo_file->sections.loc =
ed2dc618 12736 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12737 sections.loc_offset, sections.loc_size);
12738 dwo_file->sections.macinfo =
ed2dc618 12739 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12740 sections.macinfo_offset, sections.macinfo_size);
12741 dwo_file->sections.macro =
ed2dc618 12742 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12743 sections.macro_offset, sections.macro_size);
12744 dwo_file->sections.str_offsets =
ed2dc618
SM
12745 create_dwp_v2_section (dwarf2_per_objfile,
12746 &dwp_file->sections.str_offsets,
73869dc2
DE
12747 sections.str_offsets_offset,
12748 sections.str_offsets_size);
12749 /* The "str" section is global to the entire DWP file. */
12750 dwo_file->sections.str = dwp_file->sections.str;
12751 /* The info or types section is assigned below to dwo_unit,
12752 there's no need to record it in dwo_file.
12753 Also, we can't simply record type sections in dwo_file because
12754 we record a pointer into the vector in dwo_unit. As we collect more
12755 types we'll grow the vector and eventually have to reallocate space
12756 for it, invalidating all copies of pointers into the previous
12757 contents. */
12758 *dwo_file_slot = dwo_file;
12759 }
12760 else
12761 {
b4f54984 12762 if (dwarf_read_debug)
73869dc2
DE
12763 {
12764 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12765 virtual_dwo_name.c_str ());
73869dc2 12766 }
9a3c8263 12767 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12768 }
73869dc2
DE
12769
12770 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12771 dwo_unit->dwo_file = dwo_file;
12772 dwo_unit->signature = signature;
8d749320
SM
12773 dwo_unit->section =
12774 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12775 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12776 is_debug_types
73869dc2
DE
12777 ? &dwp_file->sections.types
12778 : &dwp_file->sections.info,
12779 sections.info_or_types_offset,
12780 sections.info_or_types_size);
12781 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12782
12783 return dwo_unit;
12784}
12785
57d63ce2
DE
12786/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12787 Returns NULL if the signature isn't found. */
80626a55
DE
12788
12789static struct dwo_unit *
ed2dc618
SM
12790lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12791 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12792 ULONGEST signature, int is_debug_types)
80626a55 12793{
57d63ce2
DE
12794 const struct dwp_hash_table *dwp_htab =
12795 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12796 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12797 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12798 uint32_t hash = signature & mask;
12799 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12800 unsigned int i;
12801 void **slot;
870f88f7 12802 struct dwo_unit find_dwo_cu;
80626a55
DE
12803
12804 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12805 find_dwo_cu.signature = signature;
19ac8c2e
DE
12806 slot = htab_find_slot (is_debug_types
12807 ? dwp_file->loaded_tus
12808 : dwp_file->loaded_cus,
12809 &find_dwo_cu, INSERT);
80626a55
DE
12810
12811 if (*slot != NULL)
9a3c8263 12812 return (struct dwo_unit *) *slot;
80626a55
DE
12813
12814 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12815 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12816 {
12817 ULONGEST signature_in_table;
12818
12819 signature_in_table =
57d63ce2 12820 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12821 if (signature_in_table == signature)
12822 {
57d63ce2
DE
12823 uint32_t unit_index =
12824 read_4_bytes (dbfd,
12825 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12826
73869dc2
DE
12827 if (dwp_file->version == 1)
12828 {
ed2dc618
SM
12829 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12830 dwp_file, unit_index,
73869dc2
DE
12831 comp_dir, signature,
12832 is_debug_types);
12833 }
12834 else
12835 {
ed2dc618
SM
12836 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12837 dwp_file, unit_index,
73869dc2
DE
12838 comp_dir, signature,
12839 is_debug_types);
12840 }
9a3c8263 12841 return (struct dwo_unit *) *slot;
80626a55
DE
12842 }
12843 if (signature_in_table == 0)
12844 return NULL;
12845 hash = (hash + hash2) & mask;
12846 }
12847
12848 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12849 " [in module %s]"),
12850 dwp_file->name);
12851}
12852
ab5088bf 12853/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12854 Open the file specified by FILE_NAME and hand it off to BFD for
12855 preliminary analysis. Return a newly initialized bfd *, which
12856 includes a canonicalized copy of FILE_NAME.
80626a55 12857 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12858 SEARCH_CWD is true if the current directory is to be searched.
12859 It will be searched before debug-file-directory.
13aaf454
DE
12860 If successful, the file is added to the bfd include table of the
12861 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12862 If unable to find/open the file, return NULL.
3019eac3
DE
12863 NOTE: This function is derived from symfile_bfd_open. */
12864
192b62ce 12865static gdb_bfd_ref_ptr
ed2dc618
SM
12866try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12867 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12868{
24b9144d 12869 int desc;
9c02c129
DE
12870 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12871 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12872 to debug_file_directory. */
e0cc99a6 12873 const char *search_path;
9c02c129
DE
12874 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12875
e0cc99a6 12876 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12877 if (search_cwd)
12878 {
12879 if (*debug_file_directory != '\0')
e0cc99a6
TT
12880 {
12881 search_path_holder.reset (concat (".", dirname_separator_string,
12882 debug_file_directory,
12883 (char *) NULL));
12884 search_path = search_path_holder.get ();
12885 }
6ac97d4c 12886 else
e0cc99a6 12887 search_path = ".";
6ac97d4c 12888 }
9c02c129 12889 else
e0cc99a6 12890 search_path = debug_file_directory;
3019eac3 12891
24b9144d 12892 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12893 if (is_dwp)
12894 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12895
12896 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12897 desc = openp (search_path, flags, file_name,
3019eac3
DE
12898 O_RDONLY | O_BINARY, &absolute_name);
12899 if (desc < 0)
12900 return NULL;
12901
e0cc99a6
TT
12902 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12903 gnutarget, desc));
9c02c129
DE
12904 if (sym_bfd == NULL)
12905 return NULL;
192b62ce 12906 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12907
192b62ce
TT
12908 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12909 return NULL;
3019eac3 12910
13aaf454
DE
12911 /* Success. Record the bfd as having been included by the objfile's bfd.
12912 This is important because things like demangled_names_hash lives in the
12913 objfile's per_bfd space and may have references to things like symbol
12914 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12915 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12916
3019eac3
DE
12917 return sym_bfd;
12918}
12919
ab5088bf 12920/* Try to open DWO file FILE_NAME.
3019eac3
DE
12921 COMP_DIR is the DW_AT_comp_dir attribute.
12922 The result is the bfd handle of the file.
12923 If there is a problem finding or opening the file, return NULL.
12924 Upon success, the canonicalized path of the file is stored in the bfd,
12925 same as symfile_bfd_open. */
12926
192b62ce 12927static gdb_bfd_ref_ptr
ed2dc618
SM
12928open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12929 const char *file_name, const char *comp_dir)
3019eac3 12930{
80626a55 12931 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12932 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12933 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12934
12935 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12936
12937 if (comp_dir != NULL)
12938 {
43816ebc
TT
12939 gdb::unique_xmalloc_ptr<char> path_to_try
12940 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12941
12942 /* NOTE: If comp_dir is a relative path, this will also try the
12943 search path, which seems useful. */
ed2dc618 12944 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12945 path_to_try.get (),
ed2dc618 12946 0 /*is_dwp*/,
192b62ce 12947 1 /*search_cwd*/));
3019eac3
DE
12948 if (abfd != NULL)
12949 return abfd;
12950 }
12951
12952 /* That didn't work, try debug-file-directory, which, despite its name,
12953 is a list of paths. */
12954
12955 if (*debug_file_directory == '\0')
12956 return NULL;
12957
ed2dc618
SM
12958 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12959 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12960}
12961
80626a55
DE
12962/* This function is mapped across the sections and remembers the offset and
12963 size of each of the DWO debugging sections we are interested in. */
12964
12965static void
12966dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12967{
9a3c8263 12968 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12969 const struct dwop_section_names *names = &dwop_section_names;
12970
12971 if (section_is_p (sectp->name, &names->abbrev_dwo))
12972 {
049412e3 12973 dwo_sections->abbrev.s.section = sectp;
fd361982 12974 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12975 }
12976 else if (section_is_p (sectp->name, &names->info_dwo))
12977 {
049412e3 12978 dwo_sections->info.s.section = sectp;
fd361982 12979 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12980 }
12981 else if (section_is_p (sectp->name, &names->line_dwo))
12982 {
049412e3 12983 dwo_sections->line.s.section = sectp;
fd361982 12984 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12985 }
12986 else if (section_is_p (sectp->name, &names->loc_dwo))
12987 {
049412e3 12988 dwo_sections->loc.s.section = sectp;
fd361982 12989 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12990 }
12991 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12992 {
049412e3 12993 dwo_sections->macinfo.s.section = sectp;
fd361982 12994 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12995 }
12996 else if (section_is_p (sectp->name, &names->macro_dwo))
12997 {
049412e3 12998 dwo_sections->macro.s.section = sectp;
fd361982 12999 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
13000 }
13001 else if (section_is_p (sectp->name, &names->str_dwo))
13002 {
049412e3 13003 dwo_sections->str.s.section = sectp;
fd361982 13004 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
13005 }
13006 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13007 {
049412e3 13008 dwo_sections->str_offsets.s.section = sectp;
fd361982 13009 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
13010 }
13011 else if (section_is_p (sectp->name, &names->types_dwo))
13012 {
13013 struct dwarf2_section_info type_section;
13014
13015 memset (&type_section, 0, sizeof (type_section));
049412e3 13016 type_section.s.section = sectp;
fd361982 13017 type_section.size = bfd_section_size (sectp);
fd5866f6 13018 dwo_sections->types.push_back (type_section);
80626a55
DE
13019 }
13020}
13021
ab5088bf 13022/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 13023 by PER_CU. This is for the non-DWP case.
80626a55 13024 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
13025
13026static struct dwo_file *
0ac5b59e
DE
13027open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13028 const char *dwo_name, const char *comp_dir)
3019eac3 13029{
ed2dc618 13030 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 13031
fb1eb2f9 13032 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
13033 if (dbfd == NULL)
13034 {
b4f54984 13035 if (dwarf_read_debug)
80626a55
DE
13036 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13037 return NULL;
13038 }
263db9a1 13039
51ac9db5 13040 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
13041 dwo_file->dwo_name = dwo_name;
13042 dwo_file->comp_dir = comp_dir;
fb1eb2f9 13043 dwo_file->dbfd = std::move (dbfd);
3019eac3 13044
fb1eb2f9 13045 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 13046 &dwo_file->sections);
3019eac3 13047
ed2dc618
SM
13048 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13049 dwo_file->cus);
3019eac3 13050
263db9a1 13051 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 13052 dwo_file->sections.types, dwo_file->tus);
3019eac3 13053
b4f54984 13054 if (dwarf_read_debug)
80626a55
DE
13055 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13056
263db9a1 13057 return dwo_file.release ();
3019eac3
DE
13058}
13059
80626a55 13060/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
13061 size of each of the DWP debugging sections common to version 1 and 2 that
13062 we are interested in. */
3019eac3 13063
80626a55 13064static void
73869dc2
DE
13065dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13066 void *dwp_file_ptr)
3019eac3 13067{
9a3c8263 13068 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
13069 const struct dwop_section_names *names = &dwop_section_names;
13070 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 13071
80626a55 13072 /* Record the ELF section number for later lookup: this is what the
73869dc2 13073 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
13074 gdb_assert (elf_section_nr < dwp_file->num_sections);
13075 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 13076
80626a55
DE
13077 /* Look for specific sections that we need. */
13078 if (section_is_p (sectp->name, &names->str_dwo))
13079 {
049412e3 13080 dwp_file->sections.str.s.section = sectp;
fd361982 13081 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
13082 }
13083 else if (section_is_p (sectp->name, &names->cu_index))
13084 {
049412e3 13085 dwp_file->sections.cu_index.s.section = sectp;
fd361982 13086 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
13087 }
13088 else if (section_is_p (sectp->name, &names->tu_index))
13089 {
049412e3 13090 dwp_file->sections.tu_index.s.section = sectp;
fd361982 13091 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
13092 }
13093}
3019eac3 13094
73869dc2
DE
13095/* This function is mapped across the sections and remembers the offset and
13096 size of each of the DWP version 2 debugging sections that we are interested
13097 in. This is split into a separate function because we don't know if we
13098 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13099
13100static void
13101dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13102{
9a3c8263 13103 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13104 const struct dwop_section_names *names = &dwop_section_names;
13105 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13106
13107 /* Record the ELF section number for later lookup: this is what the
13108 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13109 gdb_assert (elf_section_nr < dwp_file->num_sections);
13110 dwp_file->elf_sections[elf_section_nr] = sectp;
13111
13112 /* Look for specific sections that we need. */
13113 if (section_is_p (sectp->name, &names->abbrev_dwo))
13114 {
049412e3 13115 dwp_file->sections.abbrev.s.section = sectp;
fd361982 13116 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
13117 }
13118 else if (section_is_p (sectp->name, &names->info_dwo))
13119 {
049412e3 13120 dwp_file->sections.info.s.section = sectp;
fd361982 13121 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
13122 }
13123 else if (section_is_p (sectp->name, &names->line_dwo))
13124 {
049412e3 13125 dwp_file->sections.line.s.section = sectp;
fd361982 13126 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
13127 }
13128 else if (section_is_p (sectp->name, &names->loc_dwo))
13129 {
049412e3 13130 dwp_file->sections.loc.s.section = sectp;
fd361982 13131 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
13132 }
13133 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13134 {
049412e3 13135 dwp_file->sections.macinfo.s.section = sectp;
fd361982 13136 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
13137 }
13138 else if (section_is_p (sectp->name, &names->macro_dwo))
13139 {
049412e3 13140 dwp_file->sections.macro.s.section = sectp;
fd361982 13141 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
13142 }
13143 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13144 {
049412e3 13145 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 13146 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
13147 }
13148 else if (section_is_p (sectp->name, &names->types_dwo))
13149 {
049412e3 13150 dwp_file->sections.types.s.section = sectp;
fd361982 13151 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
13152 }
13153}
13154
80626a55 13155/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13156
80626a55
DE
13157static hashval_t
13158hash_dwp_loaded_cutus (const void *item)
13159{
9a3c8263 13160 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13161
80626a55
DE
13162 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13163 return dwo_unit->signature;
3019eac3
DE
13164}
13165
80626a55 13166/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13167
80626a55
DE
13168static int
13169eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13170{
9a3c8263
SM
13171 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13172 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13173
80626a55
DE
13174 return dua->signature == dub->signature;
13175}
3019eac3 13176
80626a55 13177/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13178
80626a55
DE
13179static htab_t
13180allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13181{
13182 return htab_create_alloc_ex (3,
13183 hash_dwp_loaded_cutus,
13184 eq_dwp_loaded_cutus,
13185 NULL,
13186 &objfile->objfile_obstack,
13187 hashtab_obstack_allocate,
13188 dummy_obstack_deallocate);
13189}
3019eac3 13190
ab5088bf
DE
13191/* Try to open DWP file FILE_NAME.
13192 The result is the bfd handle of the file.
13193 If there is a problem finding or opening the file, return NULL.
13194 Upon success, the canonicalized path of the file is stored in the bfd,
13195 same as symfile_bfd_open. */
13196
192b62ce 13197static gdb_bfd_ref_ptr
ed2dc618
SM
13198open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13199 const char *file_name)
ab5088bf 13200{
ed2dc618
SM
13201 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13202 1 /*is_dwp*/,
192b62ce 13203 1 /*search_cwd*/));
6ac97d4c
DE
13204 if (abfd != NULL)
13205 return abfd;
13206
13207 /* Work around upstream bug 15652.
13208 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13209 [Whether that's a "bug" is debatable, but it is getting in our way.]
13210 We have no real idea where the dwp file is, because gdb's realpath-ing
13211 of the executable's path may have discarded the needed info.
13212 [IWBN if the dwp file name was recorded in the executable, akin to
13213 .gnu_debuglink, but that doesn't exist yet.]
13214 Strip the directory from FILE_NAME and search again. */
13215 if (*debug_file_directory != '\0')
13216 {
13217 /* Don't implicitly search the current directory here.
13218 If the user wants to search "." to handle this case,
13219 it must be added to debug-file-directory. */
ed2dc618
SM
13220 return try_open_dwop_file (dwarf2_per_objfile,
13221 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13222 0 /*search_cwd*/);
13223 }
13224
13225 return NULL;
ab5088bf
DE
13226}
13227
80626a55
DE
13228/* Initialize the use of the DWP file for the current objfile.
13229 By convention the name of the DWP file is ${objfile}.dwp.
13230 The result is NULL if it can't be found. */
a766d390 13231
400174b1 13232static std::unique_ptr<struct dwp_file>
ed2dc618 13233open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13234{
13235 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 13236
82bf32bc
JK
13237 /* Try to find first .dwp for the binary file before any symbolic links
13238 resolving. */
6c447423
DE
13239
13240 /* If the objfile is a debug file, find the name of the real binary
13241 file and get the name of dwp file from there. */
d721ba37 13242 std::string dwp_name;
6c447423
DE
13243 if (objfile->separate_debug_objfile_backlink != NULL)
13244 {
13245 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13246 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13247
d721ba37 13248 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13249 }
13250 else
d721ba37
PA
13251 dwp_name = objfile->original_name;
13252
13253 dwp_name += ".dwp";
80626a55 13254
ed2dc618 13255 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13256 if (dbfd == NULL
13257 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13258 {
13259 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13260 dwp_name = objfile_name (objfile);
13261 dwp_name += ".dwp";
ed2dc618 13262 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13263 }
13264
80626a55
DE
13265 if (dbfd == NULL)
13266 {
b4f54984 13267 if (dwarf_read_debug)
d721ba37 13268 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 13269 return std::unique_ptr<dwp_file> ();
3019eac3 13270 }
400174b1
TT
13271
13272 const char *name = bfd_get_filename (dbfd.get ());
13273 std::unique_ptr<struct dwp_file> dwp_file
13274 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13275
0a0f4c01 13276 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
13277 dwp_file->elf_sections =
13278 OBSTACK_CALLOC (&objfile->objfile_obstack,
13279 dwp_file->num_sections, asection *);
13280
400174b1
TT
13281 bfd_map_over_sections (dwp_file->dbfd.get (),
13282 dwarf2_locate_common_dwp_sections,
13283 dwp_file.get ());
80626a55 13284
400174b1
TT
13285 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13286 0);
80626a55 13287
400174b1
TT
13288 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13289 1);
80626a55 13290
73869dc2 13291 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13292 if (dwp_file->cus && dwp_file->tus
13293 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13294 {
13295 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13296 pretty bizarre. We use pulongest here because that's the established
4d65956b 13297 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13298 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13299 " TU version %s [in DWP file %s]"),
13300 pulongest (dwp_file->cus->version),
d721ba37 13301 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13302 }
08302ed2
DE
13303
13304 if (dwp_file->cus)
13305 dwp_file->version = dwp_file->cus->version;
13306 else if (dwp_file->tus)
13307 dwp_file->version = dwp_file->tus->version;
13308 else
13309 dwp_file->version = 2;
73869dc2
DE
13310
13311 if (dwp_file->version == 2)
400174b1
TT
13312 bfd_map_over_sections (dwp_file->dbfd.get (),
13313 dwarf2_locate_v2_dwp_sections,
13314 dwp_file.get ());
73869dc2 13315
19ac8c2e
DE
13316 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13317 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13318
b4f54984 13319 if (dwarf_read_debug)
80626a55
DE
13320 {
13321 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13322 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13323 " %s CUs, %s TUs\n",
13324 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13325 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13326 }
13327
13328 return dwp_file;
3019eac3 13329}
c906108c 13330
ab5088bf
DE
13331/* Wrapper around open_and_init_dwp_file, only open it once. */
13332
13333static struct dwp_file *
ed2dc618 13334get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13335{
13336 if (! dwarf2_per_objfile->dwp_checked)
13337 {
ed2dc618
SM
13338 dwarf2_per_objfile->dwp_file
13339 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13340 dwarf2_per_objfile->dwp_checked = 1;
13341 }
400174b1 13342 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
13343}
13344
80626a55
DE
13345/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13346 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13347 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13348 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13349 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13350
13351 This is called, for example, when wanting to read a variable with a
13352 complex location. Therefore we don't want to do file i/o for every call.
13353 Therefore we don't want to look for a DWO file on every call.
13354 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13355 then we check if we've already seen DWO_NAME, and only THEN do we check
13356 for a DWO file.
13357
1c658ad5 13358 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13359 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13360
3019eac3 13361static struct dwo_unit *
80626a55
DE
13362lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13363 const char *dwo_name, const char *comp_dir,
13364 ULONGEST signature, int is_debug_types)
3019eac3 13365{
ed2dc618 13366 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13367 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13368 const char *kind = is_debug_types ? "TU" : "CU";
13369 void **dwo_file_slot;
3019eac3 13370 struct dwo_file *dwo_file;
80626a55 13371 struct dwp_file *dwp_file;
cb1df416 13372
6a506a2d
DE
13373 /* First see if there's a DWP file.
13374 If we have a DWP file but didn't find the DWO inside it, don't
13375 look for the original DWO file. It makes gdb behave differently
13376 depending on whether one is debugging in the build tree. */
cf2c3c16 13377
ed2dc618 13378 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13379 if (dwp_file != NULL)
cf2c3c16 13380 {
80626a55
DE
13381 const struct dwp_hash_table *dwp_htab =
13382 is_debug_types ? dwp_file->tus : dwp_file->cus;
13383
13384 if (dwp_htab != NULL)
13385 {
13386 struct dwo_unit *dwo_cutu =
ed2dc618 13387 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13388 signature, is_debug_types);
80626a55
DE
13389
13390 if (dwo_cutu != NULL)
13391 {
b4f54984 13392 if (dwarf_read_debug)
80626a55
DE
13393 {
13394 fprintf_unfiltered (gdb_stdlog,
13395 "Virtual DWO %s %s found: @%s\n",
13396 kind, hex_string (signature),
13397 host_address_to_string (dwo_cutu));
13398 }
13399 return dwo_cutu;
13400 }
13401 }
13402 }
6a506a2d 13403 else
80626a55 13404 {
6a506a2d 13405 /* No DWP file, look for the DWO file. */
80626a55 13406
ed2dc618
SM
13407 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13408 dwo_name, comp_dir);
6a506a2d 13409 if (*dwo_file_slot == NULL)
80626a55 13410 {
6a506a2d
DE
13411 /* Read in the file and build a table of the CUs/TUs it contains. */
13412 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13413 }
6a506a2d 13414 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13415 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13416
6a506a2d 13417 if (dwo_file != NULL)
19c3d4c9 13418 {
6a506a2d
DE
13419 struct dwo_unit *dwo_cutu = NULL;
13420
13421 if (is_debug_types && dwo_file->tus)
13422 {
13423 struct dwo_unit find_dwo_cutu;
13424
13425 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13426 find_dwo_cutu.signature = signature;
9a3c8263
SM
13427 dwo_cutu
13428 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13429 }
33c5cd75 13430 else if (!is_debug_types && dwo_file->cus)
80626a55 13431 {
33c5cd75
DB
13432 struct dwo_unit find_dwo_cutu;
13433
13434 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13435 find_dwo_cutu.signature = signature;
13436 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13437 &find_dwo_cutu);
6a506a2d
DE
13438 }
13439
13440 if (dwo_cutu != NULL)
13441 {
b4f54984 13442 if (dwarf_read_debug)
6a506a2d
DE
13443 {
13444 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13445 kind, dwo_name, hex_string (signature),
13446 host_address_to_string (dwo_cutu));
13447 }
13448 return dwo_cutu;
80626a55
DE
13449 }
13450 }
2e276125 13451 }
9cdd5dbd 13452
80626a55
DE
13453 /* We didn't find it. This could mean a dwo_id mismatch, or
13454 someone deleted the DWO/DWP file, or the search path isn't set up
13455 correctly to find the file. */
13456
b4f54984 13457 if (dwarf_read_debug)
80626a55
DE
13458 {
13459 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13460 kind, dwo_name, hex_string (signature));
13461 }
3019eac3 13462
6656a72d
DE
13463 /* This is a warning and not a complaint because it can be caused by
13464 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13465 {
13466 /* Print the name of the DWP file if we looked there, helps the user
13467 better diagnose the problem. */
791afaa2 13468 std::string dwp_text;
43942612
DE
13469
13470 if (dwp_file != NULL)
791afaa2
TT
13471 dwp_text = string_printf (" [in DWP file %s]",
13472 lbasename (dwp_file->name));
43942612 13473
9d8780f0 13474 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13475 " [in module %s]"),
13476 kind, dwo_name, hex_string (signature),
791afaa2 13477 dwp_text.c_str (),
43942612 13478 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13479 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13480 }
3019eac3 13481 return NULL;
5fb290d7
DJ
13482}
13483
80626a55
DE
13484/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13485 See lookup_dwo_cutu_unit for details. */
13486
13487static struct dwo_unit *
13488lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13489 const char *dwo_name, const char *comp_dir,
13490 ULONGEST signature)
13491{
13492 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13493}
13494
13495/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13496 See lookup_dwo_cutu_unit for details. */
13497
13498static struct dwo_unit *
13499lookup_dwo_type_unit (struct signatured_type *this_tu,
13500 const char *dwo_name, const char *comp_dir)
13501{
13502 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13503}
13504
89e63ee4
DE
13505/* Traversal function for queue_and_load_all_dwo_tus. */
13506
13507static int
13508queue_and_load_dwo_tu (void **slot, void *info)
13509{
13510 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13511 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13512 ULONGEST signature = dwo_unit->signature;
13513 struct signatured_type *sig_type =
13514 lookup_dwo_signatured_type (per_cu->cu, signature);
13515
13516 if (sig_type != NULL)
13517 {
13518 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13519
13520 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13521 a real dependency of PER_CU on SIG_TYPE. That is detected later
13522 while processing PER_CU. */
13523 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13524 load_full_type_unit (sig_cu);
ae640021 13525 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13526 }
13527
13528 return 1;
13529}
13530
13531/* Queue all TUs contained in the DWO of PER_CU to be read in.
13532 The DWO may have the only definition of the type, though it may not be
13533 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13534 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13535
13536static void
13537queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13538{
13539 struct dwo_unit *dwo_unit;
13540 struct dwo_file *dwo_file;
13541
13542 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13543 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13544 gdb_assert (per_cu->cu != NULL);
13545
13546 dwo_unit = per_cu->cu->dwo_unit;
13547 gdb_assert (dwo_unit != NULL);
13548
13549 dwo_file = dwo_unit->dwo_file;
13550 if (dwo_file->tus != NULL)
13551 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13552}
13553
3019eac3 13554/* Read in various DIEs. */
348e048f 13555
d389af10 13556/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13557 Inherit only the children of the DW_AT_abstract_origin DIE not being
13558 already referenced by DW_AT_abstract_origin from the children of the
13559 current DIE. */
d389af10
JK
13560
13561static void
13562inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13563{
13564 struct die_info *child_die;
791afaa2 13565 sect_offset *offsetp;
d389af10
JK
13566 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13567 struct die_info *origin_die;
13568 /* Iterator of the ORIGIN_DIE children. */
13569 struct die_info *origin_child_die;
d389af10 13570 struct attribute *attr;
cd02d79d
PA
13571 struct dwarf2_cu *origin_cu;
13572 struct pending **origin_previous_list_in_scope;
d389af10
JK
13573
13574 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13575 if (!attr)
13576 return;
13577
cd02d79d
PA
13578 /* Note that following die references may follow to a die in a
13579 different cu. */
13580
13581 origin_cu = cu;
13582 origin_die = follow_die_ref (die, attr, &origin_cu);
13583
13584 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13585 symbols in. */
13586 origin_previous_list_in_scope = origin_cu->list_in_scope;
13587 origin_cu->list_in_scope = cu->list_in_scope;
13588
edb3359d
DJ
13589 if (die->tag != origin_die->tag
13590 && !(die->tag == DW_TAG_inlined_subroutine
13591 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13592 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13593 sect_offset_str (die->sect_off),
13594 sect_offset_str (origin_die->sect_off));
d389af10 13595
791afaa2 13596 std::vector<sect_offset> offsets;
d389af10 13597
3ea89b92
PMR
13598 for (child_die = die->child;
13599 child_die && child_die->tag;
13600 child_die = sibling_die (child_die))
13601 {
13602 struct die_info *child_origin_die;
13603 struct dwarf2_cu *child_origin_cu;
13604
13605 /* We are trying to process concrete instance entries:
216f72a1 13606 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13607 it's not relevant to our analysis here. i.e. detecting DIEs that are
13608 present in the abstract instance but not referenced in the concrete
13609 one. */
216f72a1
JK
13610 if (child_die->tag == DW_TAG_call_site
13611 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13612 continue;
13613
c38f313d
DJ
13614 /* For each CHILD_DIE, find the corresponding child of
13615 ORIGIN_DIE. If there is more than one layer of
13616 DW_AT_abstract_origin, follow them all; there shouldn't be,
13617 but GCC versions at least through 4.4 generate this (GCC PR
13618 40573). */
3ea89b92
PMR
13619 child_origin_die = child_die;
13620 child_origin_cu = cu;
c38f313d
DJ
13621 while (1)
13622 {
cd02d79d
PA
13623 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13624 child_origin_cu);
c38f313d
DJ
13625 if (attr == NULL)
13626 break;
cd02d79d
PA
13627 child_origin_die = follow_die_ref (child_origin_die, attr,
13628 &child_origin_cu);
c38f313d
DJ
13629 }
13630
d389af10
JK
13631 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13632 counterpart may exist. */
c38f313d 13633 if (child_origin_die != child_die)
d389af10 13634 {
edb3359d
DJ
13635 if (child_die->tag != child_origin_die->tag
13636 && !(child_die->tag == DW_TAG_inlined_subroutine
13637 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13638 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13639 "different tags"),
9d8780f0
SM
13640 sect_offset_str (child_die->sect_off),
13641 sect_offset_str (child_origin_die->sect_off));
c38f313d 13642 if (child_origin_die->parent != origin_die)
b98664d3 13643 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13644 "different parents"),
9d8780f0
SM
13645 sect_offset_str (child_die->sect_off),
13646 sect_offset_str (child_origin_die->sect_off));
c38f313d 13647 else
791afaa2 13648 offsets.push_back (child_origin_die->sect_off);
d389af10 13649 }
d389af10 13650 }
791afaa2
TT
13651 std::sort (offsets.begin (), offsets.end ());
13652 sect_offset *offsets_end = offsets.data () + offsets.size ();
13653 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13654 if (offsetp[-1] == *offsetp)
b98664d3 13655 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13656 "to DIE %s as their abstract origin"),
13657 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13658
791afaa2 13659 offsetp = offsets.data ();
d389af10
JK
13660 origin_child_die = origin_die->child;
13661 while (origin_child_die && origin_child_die->tag)
13662 {
13663 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13664 while (offsetp < offsets_end
9c541725 13665 && *offsetp < origin_child_die->sect_off)
d389af10 13666 offsetp++;
b64f50a1 13667 if (offsetp >= offsets_end
9c541725 13668 || *offsetp > origin_child_die->sect_off)
d389af10 13669 {
adde2bff
DE
13670 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13671 Check whether we're already processing ORIGIN_CHILD_DIE.
13672 This can happen with mutually referenced abstract_origins.
13673 PR 16581. */
13674 if (!origin_child_die->in_process)
13675 process_die (origin_child_die, origin_cu);
d389af10
JK
13676 }
13677 origin_child_die = sibling_die (origin_child_die);
13678 }
cd02d79d 13679 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13680
13681 if (cu != origin_cu)
13682 compute_delayed_physnames (origin_cu);
d389af10
JK
13683}
13684
c906108c 13685static void
e7c27a73 13686read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13687{
518817b3 13688 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13689 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13690 struct context_stack *newobj;
c906108c
SS
13691 CORE_ADDR lowpc;
13692 CORE_ADDR highpc;
13693 struct die_info *child_die;
edb3359d 13694 struct attribute *attr, *call_line, *call_file;
15d034d0 13695 const char *name;
e142c38c 13696 CORE_ADDR baseaddr;
801e3a5b 13697 struct block *block;
edb3359d 13698 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13699 std::vector<struct symbol *> template_args;
34eaf542 13700 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13701
13702 if (inlined_func)
13703 {
13704 /* If we do not have call site information, we can't show the
13705 caller of this inlined function. That's too confusing, so
13706 only use the scope for local variables. */
13707 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13708 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13709 if (call_line == NULL || call_file == NULL)
13710 {
13711 read_lexical_block_scope (die, cu);
13712 return;
13713 }
13714 }
c906108c 13715
6a053cb1 13716 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
e142c38c 13717
94af9270 13718 name = dwarf2_name (die, cu);
c906108c 13719
e8d05480
JB
13720 /* Ignore functions with missing or empty names. These are actually
13721 illegal according to the DWARF standard. */
13722 if (name == NULL)
13723 {
b98664d3 13724 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13725 sect_offset_str (die->sect_off));
e8d05480
JB
13726 return;
13727 }
13728
13729 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13730 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13731 <= PC_BOUNDS_INVALID)
e8d05480 13732 {
ae4d0c03
PM
13733 attr = dwarf2_attr (die, DW_AT_external, cu);
13734 if (!attr || !DW_UNSND (attr))
b98664d3 13735 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13736 "for subprogram DIE at %s"),
13737 sect_offset_str (die->sect_off));
e8d05480
JB
13738 return;
13739 }
c906108c 13740
3e29f34a
MR
13741 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13742 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13743
34eaf542
TT
13744 /* If we have any template arguments, then we must allocate a
13745 different sort of symbol. */
13746 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13747 {
13748 if (child_die->tag == DW_TAG_template_type_param
13749 || child_die->tag == DW_TAG_template_value_param)
13750 {
e623cf5d 13751 templ_func = allocate_template_symbol (objfile);
cf724bc9 13752 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13753 break;
13754 }
13755 }
13756
c24bdb02 13757 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13758 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13759 (struct symbol *) templ_func);
4c2df51b 13760
81873cc8 13761 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13762 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13763 cu->language);
13764
4cecd739
DJ
13765 /* If there is a location expression for DW_AT_frame_base, record
13766 it. */
e142c38c 13767 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13768 if (attr != nullptr)
fe978cb0 13769 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13770
63e43d3a
PMR
13771 /* If there is a location for the static link, record it. */
13772 newobj->static_link = NULL;
13773 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13774 if (attr != nullptr)
63e43d3a 13775 {
224c3ddb
SM
13776 newobj->static_link
13777 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d
AB
13778 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13779 dwarf2_per_cu_addr_type (cu->per_cu));
63e43d3a
PMR
13780 }
13781
c24bdb02 13782 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13783
639d11d3 13784 if (die->child != NULL)
c906108c 13785 {
639d11d3 13786 child_die = die->child;
c906108c
SS
13787 while (child_die && child_die->tag)
13788 {
34eaf542
TT
13789 if (child_die->tag == DW_TAG_template_type_param
13790 || child_die->tag == DW_TAG_template_value_param)
13791 {
13792 struct symbol *arg = new_symbol (child_die, NULL, cu);
13793
f1078f66 13794 if (arg != NULL)
2f4732b0 13795 template_args.push_back (arg);
34eaf542
TT
13796 }
13797 else
13798 process_die (child_die, cu);
c906108c
SS
13799 child_die = sibling_die (child_die);
13800 }
13801 }
13802
d389af10
JK
13803 inherit_abstract_dies (die, cu);
13804
4a811a97
UW
13805 /* If we have a DW_AT_specification, we might need to import using
13806 directives from the context of the specification DIE. See the
13807 comment in determine_prefix. */
13808 if (cu->language == language_cplus
13809 && dwarf2_attr (die, DW_AT_specification, cu))
13810 {
13811 struct dwarf2_cu *spec_cu = cu;
13812 struct die_info *spec_die = die_specification (die, &spec_cu);
13813
13814 while (spec_die)
13815 {
13816 child_die = spec_die->child;
13817 while (child_die && child_die->tag)
13818 {
13819 if (child_die->tag == DW_TAG_imported_module)
13820 process_die (child_die, spec_cu);
13821 child_die = sibling_die (child_die);
13822 }
13823
13824 /* In some cases, GCC generates specification DIEs that
13825 themselves contain DW_AT_specification attributes. */
13826 spec_die = die_specification (spec_die, &spec_cu);
13827 }
13828 }
13829
c24bdb02 13830 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13831 /* Make a block for the local symbols within. */
c24bdb02 13832 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13833 cstk.static_link, lowpc, highpc);
801e3a5b 13834
df8a16a1 13835 /* For C++, set the block's scope. */
45280282
IB
13836 if ((cu->language == language_cplus
13837 || cu->language == language_fortran
c44af4eb
TT
13838 || cu->language == language_d
13839 || cu->language == language_rust)
4d4ec4e5 13840 && cu->processing_has_namespace_info)
195a3f6c
TT
13841 block_set_scope (block, determine_prefix (die, cu),
13842 &objfile->objfile_obstack);
df8a16a1 13843
801e3a5b
JB
13844 /* If we have address ranges, record them. */
13845 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13846
a60f3166 13847 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13848
34eaf542 13849 /* Attach template arguments to function. */
2f4732b0 13850 if (!template_args.empty ())
34eaf542
TT
13851 {
13852 gdb_assert (templ_func != NULL);
13853
2f4732b0 13854 templ_func->n_template_arguments = template_args.size ();
34eaf542 13855 templ_func->template_arguments
8d749320
SM
13856 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13857 templ_func->n_template_arguments);
34eaf542 13858 memcpy (templ_func->template_arguments,
2f4732b0 13859 template_args.data (),
34eaf542 13860 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13861
13862 /* Make sure that the symtab is set on the new symbols. Even
13863 though they don't appear in this symtab directly, other parts
13864 of gdb assume that symbols do, and this is reasonably
13865 true. */
8634679f 13866 for (symbol *sym : template_args)
3e1d3d8c 13867 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13868 }
13869
208d8187
JB
13870 /* In C++, we can have functions nested inside functions (e.g., when
13871 a function declares a class that has methods). This means that
13872 when we finish processing a function scope, we may need to go
13873 back to building a containing block's symbol lists. */
c24bdb02
KS
13874 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13875 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13876
921e78cf
JB
13877 /* If we've finished processing a top-level function, subsequent
13878 symbols go in the file symbol list. */
c24bdb02
KS
13879 if (cu->get_builder ()->outermost_context_p ())
13880 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13881}
13882
13883/* Process all the DIES contained within a lexical block scope. Start
13884 a new scope, process the dies, and then close the scope. */
13885
13886static void
e7c27a73 13887read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13888{
518817b3 13889 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13890 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13891 CORE_ADDR lowpc, highpc;
13892 struct die_info *child_die;
e142c38c
DJ
13893 CORE_ADDR baseaddr;
13894
6a053cb1 13895 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
c906108c
SS
13896
13897 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13898 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13899 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13900 be nasty. Might be easier to properly extend generic blocks to
af34e669 13901 describe ranges. */
e385593e
JK
13902 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13903 {
13904 case PC_BOUNDS_NOT_PRESENT:
13905 /* DW_TAG_lexical_block has no attributes, process its children as if
13906 there was no wrapping by that DW_TAG_lexical_block.
13907 GCC does no longer produces such DWARF since GCC r224161. */
13908 for (child_die = die->child;
13909 child_die != NULL && child_die->tag;
13910 child_die = sibling_die (child_die))
13911 process_die (child_die, cu);
13912 return;
13913 case PC_BOUNDS_INVALID:
13914 return;
13915 }
3e29f34a
MR
13916 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13917 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13918
c24bdb02 13919 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13920 if (die->child != NULL)
c906108c 13921 {
639d11d3 13922 child_die = die->child;
c906108c
SS
13923 while (child_die && child_die->tag)
13924 {
e7c27a73 13925 process_die (child_die, cu);
c906108c
SS
13926 child_die = sibling_die (child_die);
13927 }
13928 }
3ea89b92 13929 inherit_abstract_dies (die, cu);
c24bdb02 13930 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13931
c24bdb02
KS
13932 if (*cu->get_builder ()->get_local_symbols () != NULL
13933 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13934 {
801e3a5b 13935 struct block *block
c24bdb02 13936 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13937 cstk.start_addr, highpc);
801e3a5b
JB
13938
13939 /* Note that recording ranges after traversing children, as we
13940 do here, means that recording a parent's ranges entails
13941 walking across all its children's ranges as they appear in
13942 the address map, which is quadratic behavior.
13943
13944 It would be nicer to record the parent's ranges before
13945 traversing its children, simply overriding whatever you find
13946 there. But since we don't even decide whether to create a
13947 block until after we've traversed its children, that's hard
13948 to do. */
13949 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13950 }
c24bdb02
KS
13951 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13952 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13953}
13954
216f72a1 13955/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13956
13957static void
13958read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13959{
518817b3 13960 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13961 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13962 CORE_ADDR pc, baseaddr;
13963 struct attribute *attr;
13964 struct call_site *call_site, call_site_local;
13965 void **slot;
13966 int nparams;
13967 struct die_info *child_die;
13968
6a053cb1 13969 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
96408a79 13970
216f72a1
JK
13971 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13972 if (attr == NULL)
13973 {
13974 /* This was a pre-DWARF-5 GNU extension alias
13975 for DW_AT_call_return_pc. */
13976 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13977 }
96408a79
SA
13978 if (!attr)
13979 {
b98664d3 13980 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13981 "DIE %s [in module %s]"),
13982 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13983 return;
13984 }
31aa7e4e 13985 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 13986 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13987
13988 if (cu->call_site_htab == NULL)
13989 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13990 NULL, &objfile->objfile_obstack,
13991 hashtab_obstack_allocate, NULL);
13992 call_site_local.pc = pc;
13993 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13994 if (*slot != NULL)
13995 {
b98664d3 13996 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13997 "DIE %s [in module %s]"),
13998 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13999 objfile_name (objfile));
96408a79
SA
14000 return;
14001 }
14002
14003 /* Count parameters at the caller. */
14004
14005 nparams = 0;
14006 for (child_die = die->child; child_die && child_die->tag;
14007 child_die = sibling_die (child_die))
14008 {
216f72a1
JK
14009 if (child_die->tag != DW_TAG_call_site_parameter
14010 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 14011 {
b98664d3 14012 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
14013 "DW_TAG_call_site child DIE %s [in module %s]"),
14014 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 14015 objfile_name (objfile));
96408a79
SA
14016 continue;
14017 }
14018
14019 nparams++;
14020 }
14021
224c3ddb
SM
14022 call_site
14023 = ((struct call_site *)
14024 obstack_alloc (&objfile->objfile_obstack,
14025 sizeof (*call_site)
14026 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
14027 *slot = call_site;
14028 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14029 call_site->pc = pc;
14030
216f72a1
JK
14031 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14032 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
14033 {
14034 struct die_info *func_die;
14035
14036 /* Skip also over DW_TAG_inlined_subroutine. */
14037 for (func_die = die->parent;
14038 func_die && func_die->tag != DW_TAG_subprogram
14039 && func_die->tag != DW_TAG_subroutine_type;
14040 func_die = func_die->parent);
14041
216f72a1
JK
14042 /* DW_AT_call_all_calls is a superset
14043 of DW_AT_call_all_tail_calls. */
96408a79 14044 if (func_die
216f72a1 14045 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 14046 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 14047 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
14048 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14049 {
14050 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14051 not complete. But keep CALL_SITE for look ups via call_site_htab,
14052 both the initial caller containing the real return address PC and
14053 the final callee containing the current PC of a chain of tail
14054 calls do not need to have the tail call list complete. But any
14055 function candidate for a virtual tail call frame searched via
14056 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14057 determined unambiguously. */
14058 }
14059 else
14060 {
14061 struct type *func_type = NULL;
14062
14063 if (func_die)
14064 func_type = get_die_type (func_die, cu);
14065 if (func_type != NULL)
14066 {
14067 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14068
14069 /* Enlist this call site to the function. */
14070 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14071 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14072 }
14073 else
b98664d3 14074 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
14075 "DIE %s [in module %s]"),
14076 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14077 }
14078 }
14079
216f72a1
JK
14080 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14081 if (attr == NULL)
14082 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14083 if (attr == NULL)
14084 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14085 if (attr == NULL)
216f72a1
JK
14086 {
14087 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14088 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14089 }
96408a79
SA
14090 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14091 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14092 /* Keep NULL DWARF_BLOCK. */;
14093 else if (attr_form_is_block (attr))
14094 {
14095 struct dwarf2_locexpr_baton *dlbaton;
14096
8d749320 14097 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
14098 dlbaton->data = DW_BLOCK (attr)->data;
14099 dlbaton->size = DW_BLOCK (attr)->size;
14100 dlbaton->per_cu = cu->per_cu;
14101
14102 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14103 }
7771576e 14104 else if (attr_form_is_ref (attr))
96408a79 14105 {
96408a79
SA
14106 struct dwarf2_cu *target_cu = cu;
14107 struct die_info *target_die;
14108
ac9ec31b 14109 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 14110 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
14111 if (die_is_declaration (target_die, target_cu))
14112 {
7d45c7c3 14113 const char *target_physname;
9112db09
JK
14114
14115 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14116 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14117 if (target_physname == NULL)
9112db09 14118 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 14119 if (target_physname == NULL)
b98664d3 14120 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14121 "physname, for referencing DIE %s [in module %s]"),
14122 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14123 else
7d455152 14124 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14125 }
14126 else
14127 {
14128 CORE_ADDR lowpc;
14129
14130 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14131 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14132 <= PC_BOUNDS_INVALID)
b98664d3 14133 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14134 "low pc, for referencing DIE %s [in module %s]"),
14135 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14136 else
3e29f34a
MR
14137 {
14138 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14139 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14140 }
96408a79
SA
14141 }
14142 }
14143 else
b98664d3 14144 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14145 "block nor reference, for DIE %s [in module %s]"),
14146 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14147
14148 call_site->per_cu = cu->per_cu;
14149
14150 for (child_die = die->child;
14151 child_die && child_die->tag;
14152 child_die = sibling_die (child_die))
14153 {
96408a79 14154 struct call_site_parameter *parameter;
1788b2d3 14155 struct attribute *loc, *origin;
96408a79 14156
216f72a1
JK
14157 if (child_die->tag != DW_TAG_call_site_parameter
14158 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14159 {
14160 /* Already printed the complaint above. */
14161 continue;
14162 }
14163
14164 gdb_assert (call_site->parameter_count < nparams);
14165 parameter = &call_site->parameter[call_site->parameter_count];
14166
1788b2d3
JK
14167 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14168 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14169 register is contained in DW_AT_call_value. */
96408a79 14170
24c5c679 14171 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14172 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14173 if (origin == NULL)
14174 {
14175 /* This was a pre-DWARF-5 GNU extension alias
14176 for DW_AT_call_parameter. */
14177 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14178 }
7771576e 14179 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14180 {
1788b2d3 14181 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14182
14183 sect_offset sect_off
14184 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14185 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14186 {
14187 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14188 binding can be done only inside one CU. Such referenced DIE
14189 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14190 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14191 "DW_TAG_call_site child DIE %s [in module %s]"),
14192 sect_offset_str (child_die->sect_off),
9c541725 14193 objfile_name (objfile));
d76b7dbc
JK
14194 continue;
14195 }
9c541725
PA
14196 parameter->u.param_cu_off
14197 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14198 }
14199 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79 14200 {
b98664d3 14201 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14202 "DW_TAG_call_site child DIE %s [in module %s]"),
14203 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14204 continue;
14205 }
24c5c679 14206 else
96408a79 14207 {
24c5c679
JK
14208 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14209 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14210 if (parameter->u.dwarf_reg != -1)
14211 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14212 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14213 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14214 &parameter->u.fb_offset))
14215 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14216 else
14217 {
b98664d3 14218 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14219 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14220 "DW_TAG_call_site child DIE %s "
24c5c679 14221 "[in module %s]"),
9d8780f0 14222 sect_offset_str (child_die->sect_off),
9c541725 14223 objfile_name (objfile));
24c5c679
JK
14224 continue;
14225 }
96408a79
SA
14226 }
14227
216f72a1
JK
14228 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14229 if (attr == NULL)
14230 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14231 if (!attr_form_is_block (attr))
14232 {
b98664d3 14233 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14234 "DW_TAG_call_site child DIE %s [in module %s]"),
14235 sect_offset_str (child_die->sect_off),
9c541725 14236 objfile_name (objfile));
96408a79
SA
14237 continue;
14238 }
14239 parameter->value = DW_BLOCK (attr)->data;
14240 parameter->value_size = DW_BLOCK (attr)->size;
14241
14242 /* Parameters are not pre-cleared by memset above. */
14243 parameter->data_value = NULL;
14244 parameter->data_value_size = 0;
14245 call_site->parameter_count++;
14246
216f72a1
JK
14247 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14248 if (attr == NULL)
14249 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 14250 if (attr != nullptr)
96408a79
SA
14251 {
14252 if (!attr_form_is_block (attr))
b98664d3 14253 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14254 "DW_TAG_call_site child DIE %s [in module %s]"),
14255 sect_offset_str (child_die->sect_off),
9c541725 14256 objfile_name (objfile));
96408a79
SA
14257 else
14258 {
14259 parameter->data_value = DW_BLOCK (attr)->data;
14260 parameter->data_value_size = DW_BLOCK (attr)->size;
14261 }
14262 }
14263 }
14264}
14265
71a3c369
TT
14266/* Helper function for read_variable. If DIE represents a virtual
14267 table, then return the type of the concrete object that is
14268 associated with the virtual table. Otherwise, return NULL. */
14269
14270static struct type *
14271rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14272{
14273 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14274 if (attr == NULL)
14275 return NULL;
14276
14277 /* Find the type DIE. */
14278 struct die_info *type_die = NULL;
14279 struct dwarf2_cu *type_cu = cu;
14280
14281 if (attr_form_is_ref (attr))
14282 type_die = follow_die_ref (die, attr, &type_cu);
14283 if (type_die == NULL)
14284 return NULL;
14285
14286 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14287 return NULL;
14288 return die_containing_type (type_die, type_cu);
14289}
14290
14291/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14292
14293static void
14294read_variable (struct die_info *die, struct dwarf2_cu *cu)
14295{
14296 struct rust_vtable_symbol *storage = NULL;
14297
14298 if (cu->language == language_rust)
14299 {
14300 struct type *containing_type = rust_containing_type (die, cu);
14301
14302 if (containing_type != NULL)
14303 {
518817b3 14304 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 14305
468c0cbb 14306 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
71a3c369
TT
14307 initialize_objfile_symbol (storage);
14308 storage->concrete_type = containing_type;
cf724bc9 14309 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14310 }
14311 }
14312
e4a62c65
TV
14313 struct symbol *res = new_symbol (die, NULL, cu, storage);
14314 struct attribute *abstract_origin
14315 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14316 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14317 if (res == NULL && loc && abstract_origin)
14318 {
14319 /* We have a variable without a name, but with a location and an abstract
14320 origin. This may be a concrete instance of an abstract variable
14321 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14322 later. */
14323 struct dwarf2_cu *origin_cu = cu;
14324 struct die_info *origin_die
14325 = follow_die_ref (die, abstract_origin, &origin_cu);
14326 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 14327 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 14328 }
71a3c369
TT
14329}
14330
43988095
JK
14331/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14332 reading .debug_rnglists.
14333 Callback's type should be:
14334 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14335 Return true if the attributes are present and valid, otherwise,
14336 return false. */
14337
14338template <typename Callback>
14339static bool
14340dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14341 Callback &&callback)
14342{
ed2dc618 14343 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14344 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14345 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14346 bfd *obfd = objfile->obfd;
43988095
JK
14347 /* Base address selection entry. */
14348 CORE_ADDR base;
14349 int found_base;
43988095 14350 const gdb_byte *buffer;
43988095
JK
14351 CORE_ADDR baseaddr;
14352 bool overflow = false;
14353
14354 found_base = cu->base_known;
14355 base = cu->base_address;
14356
14357 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14358 if (offset >= dwarf2_per_objfile->rnglists.size)
14359 {
b98664d3 14360 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14361 offset);
14362 return false;
14363 }
14364 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14365
6a053cb1 14366 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
43988095
JK
14367
14368 while (1)
14369 {
7814882a
JK
14370 /* Initialize it due to a false compiler warning. */
14371 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14372 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14373 + dwarf2_per_objfile->rnglists.size);
14374 unsigned int bytes_read;
14375
14376 if (buffer == buf_end)
14377 {
14378 overflow = true;
14379 break;
14380 }
14381 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14382 switch (rlet)
14383 {
14384 case DW_RLE_end_of_list:
14385 break;
14386 case DW_RLE_base_address:
14387 if (buffer + cu->header.addr_size > buf_end)
14388 {
14389 overflow = true;
14390 break;
14391 }
14392 base = read_address (obfd, buffer, cu, &bytes_read);
14393 found_base = 1;
14394 buffer += bytes_read;
14395 break;
14396 case DW_RLE_start_length:
14397 if (buffer + cu->header.addr_size > buf_end)
14398 {
14399 overflow = true;
14400 break;
14401 }
14402 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14403 buffer += bytes_read;
14404 range_end = (range_beginning
14405 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14406 buffer += bytes_read;
14407 if (buffer > buf_end)
14408 {
14409 overflow = true;
14410 break;
14411 }
14412 break;
14413 case DW_RLE_offset_pair:
14414 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14415 buffer += bytes_read;
14416 if (buffer > buf_end)
14417 {
14418 overflow = true;
14419 break;
14420 }
14421 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14422 buffer += bytes_read;
14423 if (buffer > buf_end)
14424 {
14425 overflow = true;
14426 break;
14427 }
14428 break;
14429 case DW_RLE_start_end:
14430 if (buffer + 2 * cu->header.addr_size > buf_end)
14431 {
14432 overflow = true;
14433 break;
14434 }
14435 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14436 buffer += bytes_read;
14437 range_end = read_address (obfd, buffer, cu, &bytes_read);
14438 buffer += bytes_read;
14439 break;
14440 default:
b98664d3 14441 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14442 return false;
14443 }
14444 if (rlet == DW_RLE_end_of_list || overflow)
14445 break;
14446 if (rlet == DW_RLE_base_address)
14447 continue;
14448
14449 if (!found_base)
14450 {
14451 /* We have no valid base address for the ranges
14452 data. */
b98664d3 14453 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14454 return false;
14455 }
14456
14457 if (range_beginning > range_end)
14458 {
14459 /* Inverted range entries are invalid. */
b98664d3 14460 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14461 return false;
14462 }
14463
14464 /* Empty range entries have no effect. */
14465 if (range_beginning == range_end)
14466 continue;
14467
14468 range_beginning += base;
14469 range_end += base;
14470
14471 /* A not-uncommon case of bad debug info.
14472 Don't pollute the addrmap with bad data. */
14473 if (range_beginning + baseaddr == 0
14474 && !dwarf2_per_objfile->has_section_at_zero)
14475 {
b98664d3 14476 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14477 " [in module %s]"), objfile_name (objfile));
14478 continue;
14479 }
14480
14481 callback (range_beginning, range_end);
14482 }
14483
14484 if (overflow)
14485 {
b98664d3 14486 complaint (_("Offset %d is not terminated "
43988095
JK
14487 "for DW_AT_ranges attribute"),
14488 offset);
14489 return false;
14490 }
14491
14492 return true;
14493}
14494
14495/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14496 Callback's type should be:
14497 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14498 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14499
43988095 14500template <typename Callback>
43039443 14501static int
5f46c5a5 14502dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14503 Callback &&callback)
43039443 14504{
ed2dc618 14505 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14506 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14507 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14508 struct comp_unit_head *cu_header = &cu->header;
14509 bfd *obfd = objfile->obfd;
14510 unsigned int addr_size = cu_header->addr_size;
14511 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14512 /* Base address selection entry. */
14513 CORE_ADDR base;
14514 int found_base;
14515 unsigned int dummy;
d521ce57 14516 const gdb_byte *buffer;
ff013f42 14517 CORE_ADDR baseaddr;
43039443 14518
43988095
JK
14519 if (cu_header->version >= 5)
14520 return dwarf2_rnglists_process (offset, cu, callback);
14521
d00adf39
DE
14522 found_base = cu->base_known;
14523 base = cu->base_address;
43039443 14524
be391dca 14525 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14526 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 14527 {
b98664d3 14528 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14529 offset);
14530 return 0;
14531 }
dce234bc 14532 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14533
6a053cb1 14534 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
ff013f42 14535
43039443
JK
14536 while (1)
14537 {
14538 CORE_ADDR range_beginning, range_end;
14539
14540 range_beginning = read_address (obfd, buffer, cu, &dummy);
14541 buffer += addr_size;
14542 range_end = read_address (obfd, buffer, cu, &dummy);
14543 buffer += addr_size;
14544 offset += 2 * addr_size;
14545
14546 /* An end of list marker is a pair of zero addresses. */
14547 if (range_beginning == 0 && range_end == 0)
14548 /* Found the end of list entry. */
14549 break;
14550
14551 /* Each base address selection entry is a pair of 2 values.
14552 The first is the largest possible address, the second is
14553 the base address. Check for a base address here. */
14554 if ((range_beginning & mask) == mask)
14555 {
28d2bfb9
AB
14556 /* If we found the largest possible address, then we already
14557 have the base address in range_end. */
14558 base = range_end;
43039443
JK
14559 found_base = 1;
14560 continue;
14561 }
14562
14563 if (!found_base)
14564 {
14565 /* We have no valid base address for the ranges
14566 data. */
b98664d3 14567 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14568 return 0;
14569 }
14570
9277c30c
UW
14571 if (range_beginning > range_end)
14572 {
14573 /* Inverted range entries are invalid. */
b98664d3 14574 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14575 return 0;
14576 }
14577
14578 /* Empty range entries have no effect. */
14579 if (range_beginning == range_end)
14580 continue;
14581
43039443
JK
14582 range_beginning += base;
14583 range_end += base;
14584
01093045
DE
14585 /* A not-uncommon case of bad debug info.
14586 Don't pollute the addrmap with bad data. */
14587 if (range_beginning + baseaddr == 0
14588 && !dwarf2_per_objfile->has_section_at_zero)
14589 {
b98664d3 14590 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14591 " [in module %s]"), objfile_name (objfile));
01093045
DE
14592 continue;
14593 }
14594
5f46c5a5
JK
14595 callback (range_beginning, range_end);
14596 }
14597
14598 return 1;
14599}
14600
14601/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14602 Return 1 if the attributes are present and valid, otherwise, return 0.
14603 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14604
14605static int
14606dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14607 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14608 struct partial_symtab *ranges_pst)
14609{
518817b3 14610 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5 14611 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6a053cb1 14612 const CORE_ADDR baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
5f46c5a5
JK
14613 int low_set = 0;
14614 CORE_ADDR low = 0;
14615 CORE_ADDR high = 0;
14616 int retval;
14617
14618 retval = dwarf2_ranges_process (offset, cu,
14619 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14620 {
9277c30c 14621 if (ranges_pst != NULL)
3e29f34a
MR
14622 {
14623 CORE_ADDR lowpc;
14624 CORE_ADDR highpc;
14625
79748972
TT
14626 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14627 range_beginning + baseaddr)
14628 - baseaddr);
14629 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14630 range_end + baseaddr)
14631 - baseaddr);
d320c2b5
TT
14632 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14633 lowpc, highpc - 1, ranges_pst);
3e29f34a 14634 }
ff013f42 14635
43039443
JK
14636 /* FIXME: This is recording everything as a low-high
14637 segment of consecutive addresses. We should have a
14638 data structure for discontiguous block ranges
14639 instead. */
14640 if (! low_set)
14641 {
14642 low = range_beginning;
14643 high = range_end;
14644 low_set = 1;
14645 }
14646 else
14647 {
14648 if (range_beginning < low)
14649 low = range_beginning;
14650 if (range_end > high)
14651 high = range_end;
14652 }
5f46c5a5
JK
14653 });
14654 if (!retval)
14655 return 0;
43039443
JK
14656
14657 if (! low_set)
14658 /* If the first entry is an end-of-list marker, the range
14659 describes an empty scope, i.e. no instructions. */
14660 return 0;
14661
14662 if (low_return)
14663 *low_return = low;
14664 if (high_return)
14665 *high_return = high;
14666 return 1;
14667}
14668
3a2b436a
JK
14669/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14670 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14671 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14672
3a2b436a 14673static enum pc_bounds_kind
af34e669 14674dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
14675 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14676 struct partial_symtab *pst)
c906108c 14677{
518817b3
SM
14678 struct dwarf2_per_objfile *dwarf2_per_objfile
14679 = cu->per_cu->dwarf2_per_objfile;
c906108c 14680 struct attribute *attr;
91da1414 14681 struct attribute *attr_high;
af34e669
DJ
14682 CORE_ADDR low = 0;
14683 CORE_ADDR high = 0;
e385593e 14684 enum pc_bounds_kind ret;
c906108c 14685
91da1414
MW
14686 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14687 if (attr_high)
af34e669 14688 {
e142c38c 14689 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14690 if (attr != nullptr)
91da1414 14691 {
31aa7e4e
JB
14692 low = attr_value_as_address (attr);
14693 high = attr_value_as_address (attr_high);
14694 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14695 high += low;
91da1414 14696 }
af34e669
DJ
14697 else
14698 /* Found high w/o low attribute. */
e385593e 14699 return PC_BOUNDS_INVALID;
af34e669
DJ
14700
14701 /* Found consecutive range of addresses. */
3a2b436a 14702 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14703 }
c906108c 14704 else
af34e669 14705 {
e142c38c 14706 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14707 if (attr != NULL)
14708 {
ab435259
DE
14709 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14710 We take advantage of the fact that DW_AT_ranges does not appear
14711 in DW_TAG_compile_unit of DWO files. */
14712 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14713 unsigned int ranges_offset = (DW_UNSND (attr)
14714 + (need_ranges_base
14715 ? cu->ranges_base
14716 : 0));
2e3cf129 14717
af34e669 14718 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14719 .debug_ranges section. */
2e3cf129 14720 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14721 return PC_BOUNDS_INVALID;
43039443 14722 /* Found discontinuous range of addresses. */
3a2b436a 14723 ret = PC_BOUNDS_RANGES;
af34e669 14724 }
e385593e
JK
14725 else
14726 return PC_BOUNDS_NOT_PRESENT;
af34e669 14727 }
c906108c 14728
48fbe735 14729 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14730 if (high <= low)
e385593e 14731 return PC_BOUNDS_INVALID;
c906108c
SS
14732
14733 /* When using the GNU linker, .gnu.linkonce. sections are used to
14734 eliminate duplicate copies of functions and vtables and such.
14735 The linker will arbitrarily choose one and discard the others.
14736 The AT_*_pc values for such functions refer to local labels in
14737 these sections. If the section from that file was discarded, the
14738 labels are not in the output, so the relocs get a value of 0.
14739 If this is a discarded function, mark the pc bounds as invalid,
14740 so that GDB will ignore it. */
72dca2f5 14741 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14742 return PC_BOUNDS_INVALID;
c906108c
SS
14743
14744 *lowpc = low;
96408a79
SA
14745 if (highpc)
14746 *highpc = high;
af34e669 14747 return ret;
c906108c
SS
14748}
14749
b084d499
JB
14750/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14751 its low and high PC addresses. Do nothing if these addresses could not
14752 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14753 and HIGHPC to the high address if greater than HIGHPC. */
14754
14755static void
14756dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14757 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14758 struct dwarf2_cu *cu)
14759{
14760 CORE_ADDR low, high;
14761 struct die_info *child = die->child;
14762
e385593e 14763 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14764 {
325fac50
PA
14765 *lowpc = std::min (*lowpc, low);
14766 *highpc = std::max (*highpc, high);
b084d499
JB
14767 }
14768
14769 /* If the language does not allow nested subprograms (either inside
14770 subprograms or lexical blocks), we're done. */
14771 if (cu->language != language_ada)
14772 return;
6e70227d 14773
b084d499
JB
14774 /* Check all the children of the given DIE. If it contains nested
14775 subprograms, then check their pc bounds. Likewise, we need to
14776 check lexical blocks as well, as they may also contain subprogram
14777 definitions. */
14778 while (child && child->tag)
14779 {
14780 if (child->tag == DW_TAG_subprogram
14781 || child->tag == DW_TAG_lexical_block)
14782 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14783 child = sibling_die (child);
14784 }
14785}
14786
fae299cd
DC
14787/* Get the low and high pc's represented by the scope DIE, and store
14788 them in *LOWPC and *HIGHPC. If the correct values can't be
14789 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14790
14791static void
14792get_scope_pc_bounds (struct die_info *die,
14793 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14794 struct dwarf2_cu *cu)
14795{
14796 CORE_ADDR best_low = (CORE_ADDR) -1;
14797 CORE_ADDR best_high = (CORE_ADDR) 0;
14798 CORE_ADDR current_low, current_high;
14799
3a2b436a 14800 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14801 >= PC_BOUNDS_RANGES)
fae299cd
DC
14802 {
14803 best_low = current_low;
14804 best_high = current_high;
14805 }
14806 else
14807 {
14808 struct die_info *child = die->child;
14809
14810 while (child && child->tag)
14811 {
14812 switch (child->tag) {
14813 case DW_TAG_subprogram:
b084d499 14814 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14815 break;
14816 case DW_TAG_namespace:
f55ee35c 14817 case DW_TAG_module:
fae299cd
DC
14818 /* FIXME: carlton/2004-01-16: Should we do this for
14819 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14820 that current GCC's always emit the DIEs corresponding
14821 to definitions of methods of classes as children of a
14822 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14823 the DIEs giving the declarations, which could be
14824 anywhere). But I don't see any reason why the
14825 standards says that they have to be there. */
14826 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14827
14828 if (current_low != ((CORE_ADDR) -1))
14829 {
325fac50
PA
14830 best_low = std::min (best_low, current_low);
14831 best_high = std::max (best_high, current_high);
fae299cd
DC
14832 }
14833 break;
14834 default:
0963b4bd 14835 /* Ignore. */
fae299cd
DC
14836 break;
14837 }
14838
14839 child = sibling_die (child);
14840 }
14841 }
14842
14843 *lowpc = best_low;
14844 *highpc = best_high;
14845}
14846
801e3a5b
JB
14847/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14848 in DIE. */
380bca97 14849
801e3a5b
JB
14850static void
14851dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14852 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14853{
518817b3 14854 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14855 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14856 struct attribute *attr;
91da1414 14857 struct attribute *attr_high;
801e3a5b 14858
91da1414
MW
14859 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14860 if (attr_high)
801e3a5b 14861 {
801e3a5b 14862 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14863 if (attr != nullptr)
801e3a5b 14864 {
31aa7e4e
JB
14865 CORE_ADDR low = attr_value_as_address (attr);
14866 CORE_ADDR high = attr_value_as_address (attr_high);
14867
14868 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14869 high += low;
9a619af0 14870
3e29f34a
MR
14871 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14872 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14873 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14874 }
14875 }
14876
14877 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14878 if (attr != nullptr)
801e3a5b 14879 {
ab435259
DE
14880 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14881 We take advantage of the fact that DW_AT_ranges does not appear
14882 in DW_TAG_compile_unit of DWO files. */
14883 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14884
14885 /* The value of the DW_AT_ranges attribute is the offset of the
14886 address range list in the .debug_ranges section. */
ab435259
DE
14887 unsigned long offset = (DW_UNSND (attr)
14888 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14889
2d5f09ec 14890 std::vector<blockrange> blockvec;
5f46c5a5
JK
14891 dwarf2_ranges_process (offset, cu,
14892 [&] (CORE_ADDR start, CORE_ADDR end)
14893 {
58fdfd2c
JK
14894 start += baseaddr;
14895 end += baseaddr;
5f46c5a5
JK
14896 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14897 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14898 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14899 blockvec.emplace_back (start, end);
5f46c5a5 14900 });
2d5f09ec
KB
14901
14902 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14903 }
14904}
14905
685b1105
JK
14906/* Check whether the producer field indicates either of GCC < 4.6, or the
14907 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14908
685b1105
JK
14909static void
14910check_producer (struct dwarf2_cu *cu)
60d5a603 14911{
38360086 14912 int major, minor;
60d5a603
JK
14913
14914 if (cu->producer == NULL)
14915 {
14916 /* For unknown compilers expect their behavior is DWARF version
14917 compliant.
14918
14919 GCC started to support .debug_types sections by -gdwarf-4 since
14920 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14921 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14922 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14923 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14924 }
b1ffba5a 14925 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14926 {
38360086
MW
14927 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14928 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14929 }
5230b05a 14930 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14931 {
14932 cu->producer_is_icc = true;
14933 cu->producer_is_icc_lt_14 = major < 14;
14934 }
c258c396
JD
14935 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14936 cu->producer_is_codewarrior = true;
685b1105
JK
14937 else
14938 {
14939 /* For other non-GCC compilers, expect their behavior is DWARF version
14940 compliant. */
60d5a603
JK
14941 }
14942
9068261f 14943 cu->checked_producer = true;
685b1105 14944}
ba919b58 14945
685b1105
JK
14946/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14947 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14948 during 4.6.0 experimental. */
14949
9068261f 14950static bool
685b1105
JK
14951producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14952{
14953 if (!cu->checked_producer)
14954 check_producer (cu);
14955
14956 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14957}
14958
c258c396
JD
14959
14960/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14961 with incorrect is_stmt attributes. */
14962
14963static bool
14964producer_is_codewarrior (struct dwarf2_cu *cu)
14965{
14966 if (!cu->checked_producer)
14967 check_producer (cu);
14968
14969 return cu->producer_is_codewarrior;
14970}
14971
405feb71 14972/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14973 DW_AT_accessibility. */
14974
14975static enum dwarf_access_attribute
14976dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14977{
14978 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14979 {
14980 /* The default DWARF 2 accessibility for members is public, the default
14981 accessibility for inheritance is private. */
14982
14983 if (die->tag != DW_TAG_inheritance)
14984 return DW_ACCESS_public;
14985 else
14986 return DW_ACCESS_private;
14987 }
14988 else
14989 {
14990 /* DWARF 3+ defines the default accessibility a different way. The same
14991 rules apply now for DW_TAG_inheritance as for the members and it only
14992 depends on the container kind. */
14993
14994 if (die->parent->tag == DW_TAG_class_type)
14995 return DW_ACCESS_private;
14996 else
14997 return DW_ACCESS_public;
14998 }
14999}
15000
74ac6d43
TT
15001/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15002 offset. If the attribute was not found return 0, otherwise return
15003 1. If it was found but could not properly be handled, set *OFFSET
15004 to 0. */
15005
15006static int
15007handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15008 LONGEST *offset)
15009{
15010 struct attribute *attr;
15011
15012 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15013 if (attr != NULL)
15014 {
15015 *offset = 0;
15016
15017 /* Note that we do not check for a section offset first here.
15018 This is because DW_AT_data_member_location is new in DWARF 4,
15019 so if we see it, we can assume that a constant form is really
15020 a constant and not a section offset. */
15021 if (attr_form_is_constant (attr))
15022 *offset = dwarf2_get_attr_constant_value (attr, 0);
15023 else if (attr_form_is_section_offset (attr))
15024 dwarf2_complex_location_expr_complaint ();
15025 else if (attr_form_is_block (attr))
15026 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15027 else
15028 dwarf2_complex_location_expr_complaint ();
15029
15030 return 1;
15031 }
15032
15033 return 0;
15034}
15035
c906108c
SS
15036/* Add an aggregate field to the field list. */
15037
15038static void
107d2387 15039dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15040 struct dwarf2_cu *cu)
6e70227d 15041{
518817b3 15042 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 15043 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15044 struct nextfield *new_field;
15045 struct attribute *attr;
15046 struct field *fp;
15d034d0 15047 const char *fieldname = "";
c906108c 15048
7d0ccb61
DJ
15049 if (die->tag == DW_TAG_inheritance)
15050 {
be2daae6
TT
15051 fip->baseclasses.emplace_back ();
15052 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
15053 }
15054 else
15055 {
be2daae6
TT
15056 fip->fields.emplace_back ();
15057 new_field = &fip->fields.back ();
7d0ccb61 15058 }
be2daae6 15059
c906108c
SS
15060 fip->nfields++;
15061
e142c38c 15062 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15063 if (attr != nullptr)
c906108c 15064 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
15065 else
15066 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
15067 if (new_field->accessibility != DW_ACCESS_public)
15068 fip->non_public_fields = 1;
60d5a603 15069
e142c38c 15070 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 15071 if (attr != nullptr)
c906108c 15072 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
15073 else
15074 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15075
15076 fp = &new_field->field;
a9a9bd0f 15077
e142c38c 15078 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15079 {
74ac6d43
TT
15080 LONGEST offset;
15081
a9a9bd0f 15082 /* Data member other than a C++ static data member. */
6e70227d 15083
c906108c 15084 /* Get type of field. */
e7c27a73 15085 fp->type = die_type (die, cu);
c906108c 15086
d6a843b5 15087 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15088
c906108c 15089 /* Get bit size of field (zero if none). */
e142c38c 15090 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 15091 if (attr != nullptr)
c906108c
SS
15092 {
15093 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15094 }
15095 else
15096 {
15097 FIELD_BITSIZE (*fp) = 0;
15098 }
15099
15100 /* Get bit offset of field. */
74ac6d43
TT
15101 if (handle_data_member_location (die, cu, &offset))
15102 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 15103 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 15104 if (attr != nullptr)
c906108c 15105 {
d5a22e77 15106 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
15107 {
15108 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15109 additional bit offset from the MSB of the containing
15110 anonymous object to the MSB of the field. We don't
15111 have to do anything special since we don't need to
15112 know the size of the anonymous object. */
f41f5e61 15113 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
15114 }
15115 else
15116 {
15117 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15118 MSB of the anonymous object, subtract off the number of
15119 bits from the MSB of the field to the MSB of the
15120 object, and then subtract off the number of bits of
15121 the field itself. The result is the bit offset of
15122 the LSB of the field. */
c906108c
SS
15123 int anonymous_size;
15124 int bit_offset = DW_UNSND (attr);
15125
e142c38c 15126 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15127 if (attr != nullptr)
c906108c
SS
15128 {
15129 /* The size of the anonymous object containing
15130 the bit field is explicit, so use the
15131 indicated size (in bytes). */
15132 anonymous_size = DW_UNSND (attr);
15133 }
15134 else
15135 {
15136 /* The size of the anonymous object containing
15137 the bit field must be inferred from the type
15138 attribute of the data member containing the
15139 bit field. */
15140 anonymous_size = TYPE_LENGTH (fp->type);
15141 }
f41f5e61
PA
15142 SET_FIELD_BITPOS (*fp,
15143 (FIELD_BITPOS (*fp)
15144 + anonymous_size * bits_per_byte
15145 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15146 }
15147 }
da5b30da
AA
15148 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15149 if (attr != NULL)
15150 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15151 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15152
15153 /* Get name of field. */
39cbfefa
DJ
15154 fieldname = dwarf2_name (die, cu);
15155 if (fieldname == NULL)
15156 fieldname = "";
d8151005
DJ
15157
15158 /* The name is already allocated along with this objfile, so we don't
15159 need to duplicate it for the type. */
15160 fp->name = fieldname;
c906108c
SS
15161
15162 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15163 pointer or virtual base class pointer) to private. */
e142c38c 15164 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15165 {
d48cc9dd 15166 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15167 new_field->accessibility = DW_ACCESS_private;
15168 fip->non_public_fields = 1;
15169 }
15170 }
a9a9bd0f 15171 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15172 {
a9a9bd0f
DC
15173 /* C++ static member. */
15174
15175 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15176 is a declaration, but all versions of G++ as of this writing
15177 (so through at least 3.2.1) incorrectly generate
15178 DW_TAG_variable tags. */
6e70227d 15179
ff355380 15180 const char *physname;
c906108c 15181
a9a9bd0f 15182 /* Get name of field. */
39cbfefa
DJ
15183 fieldname = dwarf2_name (die, cu);
15184 if (fieldname == NULL)
c906108c
SS
15185 return;
15186
254e6b9e 15187 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15188 if (attr
15189 /* Only create a symbol if this is an external value.
15190 new_symbol checks this and puts the value in the global symbol
15191 table, which we want. If it is not external, new_symbol
15192 will try to put the value in cu->list_in_scope which is wrong. */
15193 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15194 {
15195 /* A static const member, not much different than an enum as far as
15196 we're concerned, except that we can support more types. */
15197 new_symbol (die, NULL, cu);
15198 }
15199
2df3850c 15200 /* Get physical name. */
ff355380 15201 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15202
d8151005
DJ
15203 /* The name is already allocated along with this objfile, so we don't
15204 need to duplicate it for the type. */
15205 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15206 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15207 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15208 }
15209 else if (die->tag == DW_TAG_inheritance)
15210 {
74ac6d43 15211 LONGEST offset;
d4b96c9a 15212
74ac6d43
TT
15213 /* C++ base class field. */
15214 if (handle_data_member_location (die, cu, &offset))
15215 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15216 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15217 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 15218 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 15219 }
2ddeaf8a
TT
15220 else if (die->tag == DW_TAG_variant_part)
15221 {
15222 /* process_structure_scope will treat this DIE as a union. */
15223 process_structure_scope (die, cu);
15224
15225 /* The variant part is relative to the start of the enclosing
15226 structure. */
15227 SET_FIELD_BITPOS (*fp, 0);
15228 fp->type = get_die_type (die, cu);
15229 fp->artificial = 1;
15230 fp->name = "<<variant>>";
c8c81635
TT
15231
15232 /* Normally a DW_TAG_variant_part won't have a size, but our
15233 representation requires one, so set it to the maximum of the
489dbda6
TT
15234 child sizes, being sure to account for the offset at which
15235 each child is seen. */
c8c81635
TT
15236 if (TYPE_LENGTH (fp->type) == 0)
15237 {
15238 unsigned max = 0;
15239 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
489dbda6
TT
15240 {
15241 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
15242 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
15243 if (len > max)
15244 max = len;
15245 }
c8c81635
TT
15246 TYPE_LENGTH (fp->type) = max;
15247 }
2ddeaf8a
TT
15248 }
15249 else
15250 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15251}
15252
883fd55a
KS
15253/* Can the type given by DIE define another type? */
15254
15255static bool
15256type_can_define_types (const struct die_info *die)
15257{
15258 switch (die->tag)
15259 {
15260 case DW_TAG_typedef:
15261 case DW_TAG_class_type:
15262 case DW_TAG_structure_type:
15263 case DW_TAG_union_type:
15264 case DW_TAG_enumeration_type:
15265 return true;
15266
15267 default:
15268 return false;
15269 }
15270}
15271
15272/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15273
15274static void
883fd55a
KS
15275dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15276 struct dwarf2_cu *cu)
6e70227d 15277{
be2daae6
TT
15278 struct decl_field fp;
15279 memset (&fp, 0, sizeof (fp));
98751a41 15280
883fd55a 15281 gdb_assert (type_can_define_types (die));
98751a41 15282
883fd55a 15283 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15284 fp.name = dwarf2_name (die, cu);
15285 fp.type = read_type_die (die, cu);
98751a41 15286
c191a687
KS
15287 /* Save accessibility. */
15288 enum dwarf_access_attribute accessibility;
15289 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15290 if (attr != NULL)
15291 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15292 else
15293 accessibility = dwarf2_default_access_attribute (die, cu);
15294 switch (accessibility)
15295 {
15296 case DW_ACCESS_public:
15297 /* The assumed value if neither private nor protected. */
15298 break;
15299 case DW_ACCESS_private:
be2daae6 15300 fp.is_private = 1;
c191a687
KS
15301 break;
15302 case DW_ACCESS_protected:
be2daae6 15303 fp.is_protected = 1;
c191a687
KS
15304 break;
15305 default:
b98664d3 15306 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15307 }
15308
883fd55a 15309 if (die->tag == DW_TAG_typedef)
be2daae6 15310 fip->typedef_field_list.push_back (fp);
883fd55a 15311 else
be2daae6 15312 fip->nested_types_list.push_back (fp);
98751a41
JK
15313}
15314
c906108c
SS
15315/* Create the vector of fields, and attach it to the type. */
15316
15317static void
fba45db2 15318dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15319 struct dwarf2_cu *cu)
c906108c
SS
15320{
15321 int nfields = fip->nfields;
15322
15323 /* Record the field count, allocate space for the array of fields,
15324 and create blank accessibility bitfields if necessary. */
15325 TYPE_NFIELDS (type) = nfields;
15326 TYPE_FIELDS (type) = (struct field *)
be2daae6 15327 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 15328
b4ba55a1 15329 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15330 {
15331 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15332
15333 TYPE_FIELD_PRIVATE_BITS (type) =
15334 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15335 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15336
15337 TYPE_FIELD_PROTECTED_BITS (type) =
15338 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15339 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15340
774b6a14
TT
15341 TYPE_FIELD_IGNORE_BITS (type) =
15342 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15343 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15344 }
15345
15346 /* If the type has baseclasses, allocate and clear a bit vector for
15347 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15348 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15349 {
be2daae6 15350 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15351 unsigned char *pointer;
c906108c
SS
15352
15353 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15354 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15355 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15356 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15357 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15358 }
15359
2ddeaf8a
TT
15360 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15361 {
15362 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15363
be2daae6 15364 for (int index = 0; index < nfields; ++index)
2ddeaf8a 15365 {
be2daae6
TT
15366 struct nextfield &field = fip->fields[index];
15367
15368 if (field.variant.is_discriminant)
2ddeaf8a 15369 di->discriminant_index = index;
be2daae6 15370 else if (field.variant.default_branch)
2ddeaf8a
TT
15371 di->default_index = index;
15372 else
be2daae6 15373 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
15374 }
15375 }
15376
be2daae6
TT
15377 /* Copy the saved-up fields into the field vector. */
15378 for (int i = 0; i < nfields; ++i)
c906108c 15379 {
be2daae6
TT
15380 struct nextfield &field
15381 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15382 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15383
be2daae6
TT
15384 TYPE_FIELD (type, i) = field.field;
15385 switch (field.accessibility)
c906108c 15386 {
c5aa993b 15387 case DW_ACCESS_private:
b4ba55a1 15388 if (cu->language != language_ada)
be2daae6 15389 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15390 break;
c906108c 15391
c5aa993b 15392 case DW_ACCESS_protected:
b4ba55a1 15393 if (cu->language != language_ada)
be2daae6 15394 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15395 break;
c906108c 15396
c5aa993b
JM
15397 case DW_ACCESS_public:
15398 break;
c906108c 15399
c5aa993b
JM
15400 default:
15401 /* Unknown accessibility. Complain and treat it as public. */
15402 {
b98664d3 15403 complaint (_("unsupported accessibility %d"),
be2daae6 15404 field.accessibility);
c5aa993b
JM
15405 }
15406 break;
c906108c 15407 }
be2daae6 15408 if (i < fip->baseclasses.size ())
c906108c 15409 {
be2daae6 15410 switch (field.virtuality)
c906108c 15411 {
c5aa993b
JM
15412 case DW_VIRTUALITY_virtual:
15413 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15414 if (cu->language == language_ada)
a73c6dcd 15415 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15416 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15417 break;
c906108c
SS
15418 }
15419 }
c906108c
SS
15420 }
15421}
15422
7d27a96d
TT
15423/* Return true if this member function is a constructor, false
15424 otherwise. */
15425
15426static int
15427dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15428{
15429 const char *fieldname;
fe978cb0 15430 const char *type_name;
7d27a96d
TT
15431 int len;
15432
15433 if (die->parent == NULL)
15434 return 0;
15435
15436 if (die->parent->tag != DW_TAG_structure_type
15437 && die->parent->tag != DW_TAG_union_type
15438 && die->parent->tag != DW_TAG_class_type)
15439 return 0;
15440
15441 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15442 type_name = dwarf2_name (die->parent, cu);
15443 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15444 return 0;
15445
15446 len = strlen (fieldname);
fe978cb0
PA
15447 return (strncmp (fieldname, type_name, len) == 0
15448 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15449}
15450
e35000a7
TBA
15451/* Check if the given VALUE is a recognized enum
15452 dwarf_defaulted_attribute constant according to DWARF5 spec,
15453 Table 7.24. */
15454
15455static bool
15456is_valid_DW_AT_defaulted (ULONGEST value)
15457{
15458 switch (value)
15459 {
15460 case DW_DEFAULTED_no:
15461 case DW_DEFAULTED_in_class:
15462 case DW_DEFAULTED_out_of_class:
15463 return true;
15464 }
15465
3142e908 15466 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
15467 return false;
15468}
15469
c906108c
SS
15470/* Add a member function to the proper fieldlist. */
15471
15472static void
107d2387 15473dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15474 struct type *type, struct dwarf2_cu *cu)
c906108c 15475{
518817b3 15476 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15477 struct attribute *attr;
c906108c 15478 int i;
be2daae6 15479 struct fnfieldlist *flp = nullptr;
c906108c 15480 struct fn_field *fnp;
15d034d0 15481 const char *fieldname;
f792889a 15482 struct type *this_type;
60d5a603 15483 enum dwarf_access_attribute accessibility;
c906108c 15484
b4ba55a1 15485 if (cu->language == language_ada)
a73c6dcd 15486 error (_("unexpected member function in Ada type"));
b4ba55a1 15487
2df3850c 15488 /* Get name of member function. */
39cbfefa
DJ
15489 fieldname = dwarf2_name (die, cu);
15490 if (fieldname == NULL)
2df3850c 15491 return;
c906108c 15492
c906108c 15493 /* Look up member function name in fieldlist. */
be2daae6 15494 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15495 {
27bfe10e 15496 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15497 {
15498 flp = &fip->fnfieldlists[i];
15499 break;
15500 }
c906108c
SS
15501 }
15502
be2daae6
TT
15503 /* Create a new fnfieldlist if necessary. */
15504 if (flp == nullptr)
c906108c 15505 {
be2daae6
TT
15506 fip->fnfieldlists.emplace_back ();
15507 flp = &fip->fnfieldlists.back ();
c906108c 15508 flp->name = fieldname;
be2daae6 15509 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15510 }
15511
be2daae6
TT
15512 /* Create a new member function field and add it to the vector of
15513 fnfieldlists. */
15514 flp->fnfields.emplace_back ();
15515 fnp = &flp->fnfields.back ();
3da10d80
KS
15516
15517 /* Delay processing of the physname until later. */
9c37b5ae 15518 if (cu->language == language_cplus)
be2daae6
TT
15519 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15520 die, cu);
3da10d80
KS
15521 else
15522 {
1d06ead6 15523 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15524 fnp->physname = physname ? physname : "";
15525 }
15526
c906108c 15527 fnp->type = alloc_type (objfile);
f792889a
DJ
15528 this_type = read_type_die (die, cu);
15529 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15530 {
f792889a 15531 int nparams = TYPE_NFIELDS (this_type);
c906108c 15532
f792889a 15533 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15534 of the method itself (TYPE_CODE_METHOD). */
15535 smash_to_method_type (fnp->type, type,
f792889a
DJ
15536 TYPE_TARGET_TYPE (this_type),
15537 TYPE_FIELDS (this_type),
15538 TYPE_NFIELDS (this_type),
15539 TYPE_VARARGS (this_type));
c906108c
SS
15540
15541 /* Handle static member functions.
c5aa993b 15542 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15543 member functions. G++ helps GDB by marking the first
15544 parameter for non-static member functions (which is the this
15545 pointer) as artificial. We obtain this information from
15546 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15547 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15548 fnp->voffset = VOFFSET_STATIC;
15549 }
15550 else
b98664d3 15551 complaint (_("member function type missing for '%s'"),
3da10d80 15552 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15553
15554 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15555 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15556 fnp->fcontext = die_containing_type (die, cu);
c906108c 15557
3e43a32a
MS
15558 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15559 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15560
15561 /* Get accessibility. */
e142c38c 15562 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15563 if (attr != nullptr)
aead7601 15564 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15565 else
15566 accessibility = dwarf2_default_access_attribute (die, cu);
15567 switch (accessibility)
c906108c 15568 {
60d5a603
JK
15569 case DW_ACCESS_private:
15570 fnp->is_private = 1;
15571 break;
15572 case DW_ACCESS_protected:
15573 fnp->is_protected = 1;
15574 break;
c906108c
SS
15575 }
15576
b02dede2 15577 /* Check for artificial methods. */
e142c38c 15578 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15579 if (attr && DW_UNSND (attr) != 0)
15580 fnp->is_artificial = 1;
15581
e35000a7
TBA
15582 /* Check for defaulted methods. */
15583 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15584 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15585 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15586
15587 /* Check for deleted methods. */
15588 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15589 if (attr != nullptr && DW_UNSND (attr) != 0)
15590 fnp->is_deleted = 1;
15591
7d27a96d
TT
15592 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15593
0d564a31 15594 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15595 function. For older versions of GCC, this is an offset in the
15596 appropriate virtual table, as specified by DW_AT_containing_type.
15597 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15598 to the object address. */
15599
e142c38c 15600 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15601 if (attr != nullptr)
8e19ed76 15602 {
aec5aa8b 15603 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15604 {
aec5aa8b
TT
15605 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15606 {
15607 /* Old-style GCC. */
15608 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15609 }
15610 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15611 || (DW_BLOCK (attr)->size > 1
15612 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15613 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15614 {
aec5aa8b
TT
15615 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15616 if ((fnp->voffset % cu->header.addr_size) != 0)
15617 dwarf2_complex_location_expr_complaint ();
15618 else
15619 fnp->voffset /= cu->header.addr_size;
15620 fnp->voffset += 2;
15621 }
15622 else
15623 dwarf2_complex_location_expr_complaint ();
15624
15625 if (!fnp->fcontext)
7e993ebf
KS
15626 {
15627 /* If there is no `this' field and no DW_AT_containing_type,
15628 we cannot actually find a base class context for the
15629 vtable! */
15630 if (TYPE_NFIELDS (this_type) == 0
15631 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15632 {
b98664d3 15633 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15634 "function \"%s\" (offset %s)"),
15635 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15636 }
15637 else
15638 {
15639 fnp->fcontext
15640 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15641 }
15642 }
aec5aa8b 15643 }
3690dd37 15644 else if (attr_form_is_section_offset (attr))
8e19ed76 15645 {
4d3c2250 15646 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15647 }
15648 else
15649 {
4d3c2250
KB
15650 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15651 fieldname);
8e19ed76 15652 }
0d564a31 15653 }
d48cc9dd
DJ
15654 else
15655 {
15656 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15657 if (attr && DW_UNSND (attr))
15658 {
15659 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15660 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15661 "but the vtable offset is not specified"),
9d8780f0 15662 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15663 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15664 TYPE_CPLUS_DYNAMIC (type) = 1;
15665 }
15666 }
c906108c
SS
15667}
15668
15669/* Create the vector of member function fields, and attach it to the type. */
15670
15671static void
fba45db2 15672dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15673 struct dwarf2_cu *cu)
c906108c 15674{
b4ba55a1 15675 if (cu->language == language_ada)
a73c6dcd 15676 error (_("unexpected member functions in Ada type"));
b4ba55a1 15677
c906108c
SS
15678 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15679 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15680 TYPE_ALLOC (type,
15681 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15682
be2daae6 15683 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15684 {
be2daae6 15685 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15686 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15687
be2daae6
TT
15688 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15689 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15690 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15691 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15692
15693 for (int k = 0; k < nf.fnfields.size (); ++k)
15694 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15695 }
15696
be2daae6 15697 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15698}
15699
1168df01
JB
15700/* Returns non-zero if NAME is the name of a vtable member in CU's
15701 language, zero otherwise. */
15702static int
15703is_vtable_name (const char *name, struct dwarf2_cu *cu)
15704{
15705 static const char vptr[] = "_vptr";
15706
9c37b5ae
TT
15707 /* Look for the C++ form of the vtable. */
15708 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15709 return 1;
15710
15711 return 0;
15712}
15713
c0dd20ea 15714/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15715 functions, with the ABI-specified layout. If TYPE describes
15716 such a structure, smash it into a member function type.
61049d3b
DJ
15717
15718 GCC shouldn't do this; it should just output pointer to member DIEs.
15719 This is GCC PR debug/28767. */
c0dd20ea 15720
0b92b5bb
TT
15721static void
15722quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15723{
09e2d7c7 15724 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15725
15726 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15727 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15728 return;
c0dd20ea
DJ
15729
15730 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15731 if (TYPE_FIELD_NAME (type, 0) == NULL
15732 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15733 || TYPE_FIELD_NAME (type, 1) == NULL
15734 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15735 return;
c0dd20ea
DJ
15736
15737 /* Find the type of the method. */
0b92b5bb 15738 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15739 if (pfn_type == NULL
15740 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15741 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15742 return;
c0dd20ea
DJ
15743
15744 /* Look for the "this" argument. */
15745 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15746 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15747 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15748 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15749 return;
c0dd20ea 15750
09e2d7c7 15751 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15752 new_type = alloc_type (objfile);
09e2d7c7 15753 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15754 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15755 TYPE_VARARGS (pfn_type));
0b92b5bb 15756 smash_to_methodptr_type (type, new_type);
c0dd20ea 15757}
1168df01 15758
2b4424c3
TT
15759/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15760 appropriate error checking and issuing complaints if there is a
15761 problem. */
15762
15763static ULONGEST
15764get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15765{
15766 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15767
15768 if (attr == nullptr)
15769 return 0;
15770
15771 if (!attr_form_is_constant (attr))
15772 {
b98664d3 15773 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15774 " - DIE at %s [in module %s]"),
15775 sect_offset_str (die->sect_off),
15776 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15777 return 0;
15778 }
15779
15780 ULONGEST align;
15781 if (attr->form == DW_FORM_sdata)
15782 {
15783 LONGEST val = DW_SND (attr);
15784 if (val < 0)
15785 {
b98664d3 15786 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15787 " - DIE at %s [in module %s]"),
15788 sect_offset_str (die->sect_off),
15789 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15790 return 0;
15791 }
15792 align = val;
15793 }
15794 else
15795 align = DW_UNSND (attr);
15796
15797 if (align == 0)
15798 {
b98664d3 15799 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15800 " - DIE at %s [in module %s]"),
15801 sect_offset_str (die->sect_off),
15802 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15803 return 0;
15804 }
15805 if ((align & (align - 1)) != 0)
15806 {
b98664d3 15807 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15808 " - DIE at %s [in module %s]"),
15809 sect_offset_str (die->sect_off),
15810 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15811 return 0;
15812 }
15813
15814 return align;
15815}
15816
15817/* If the DIE has a DW_AT_alignment attribute, use its value to set
15818 the alignment for TYPE. */
15819
15820static void
15821maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15822 struct type *type)
15823{
15824 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15825 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15826 " - DIE at %s [in module %s]"),
15827 sect_offset_str (die->sect_off),
15828 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15829}
685b1105 15830
e35000a7
TBA
15831/* Check if the given VALUE is a valid enum dwarf_calling_convention
15832 constant for a type, according to DWARF5 spec, Table 5.5. */
15833
15834static bool
15835is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15836{
15837 switch (value)
15838 {
15839 case DW_CC_normal:
15840 case DW_CC_pass_by_reference:
15841 case DW_CC_pass_by_value:
15842 return true;
15843
15844 default:
15845 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15846 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15847 return false;
15848 }
15849}
15850
d0922fcf
TBA
15851/* Check if the given VALUE is a valid enum dwarf_calling_convention
15852 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15853 also according to GNU-specific values (see include/dwarf2.h). */
15854
15855static bool
15856is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15857{
15858 switch (value)
15859 {
15860 case DW_CC_normal:
15861 case DW_CC_program:
15862 case DW_CC_nocall:
15863 return true;
15864
15865 case DW_CC_GNU_renesas_sh:
15866 case DW_CC_GNU_borland_fastcall_i386:
15867 case DW_CC_GDB_IBM_OpenCL:
15868 return true;
15869
15870 default:
15871 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15872 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15873 return false;
15874 }
15875}
15876
c906108c 15877/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15878 (definition) to create a type for the structure or union. Fill in
15879 the type's name and general properties; the members will not be
83655187
DE
15880 processed until process_structure_scope. A symbol table entry for
15881 the type will also not be done until process_structure_scope (assuming
15882 the type has a name).
c906108c 15883
c767944b
DJ
15884 NOTE: we need to call these functions regardless of whether or not the
15885 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15886 structure or union. This gets the type entered into our set of
83655187 15887 user defined types. */
c906108c 15888
f792889a 15889static struct type *
134d01f1 15890read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15891{
518817b3 15892 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15893 struct type *type;
15894 struct attribute *attr;
15d034d0 15895 const char *name;
c906108c 15896
348e048f
DE
15897 /* If the definition of this type lives in .debug_types, read that type.
15898 Don't follow DW_AT_specification though, that will take us back up
15899 the chain and we want to go down. */
45e58e77 15900 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15901 if (attr != nullptr)
348e048f 15902 {
ac9ec31b 15903 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15904
ac9ec31b 15905 /* The type's CU may not be the same as CU.
02142a6c 15906 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15907 return set_die_type (die, type, cu);
15908 }
15909
c0dd20ea 15910 type = alloc_type (objfile);
c906108c 15911 INIT_CPLUS_SPECIFIC (type);
93311388 15912
39cbfefa
DJ
15913 name = dwarf2_name (die, cu);
15914 if (name != NULL)
c906108c 15915 {
987504bb 15916 if (cu->language == language_cplus
c44af4eb
TT
15917 || cu->language == language_d
15918 || cu->language == language_rust)
63d06c5c 15919 {
15d034d0 15920 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15921
15922 /* dwarf2_full_name might have already finished building the DIE's
15923 type. If so, there is no need to continue. */
15924 if (get_die_type (die, cu) != NULL)
15925 return get_die_type (die, cu);
15926
e86ca25f 15927 TYPE_NAME (type) = full_name;
63d06c5c
DC
15928 }
15929 else
15930 {
d8151005
DJ
15931 /* The name is already allocated along with this objfile, so
15932 we don't need to duplicate it for the type. */
e86ca25f 15933 TYPE_NAME (type) = name;
63d06c5c 15934 }
c906108c
SS
15935 }
15936
15937 if (die->tag == DW_TAG_structure_type)
15938 {
15939 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15940 }
15941 else if (die->tag == DW_TAG_union_type)
15942 {
15943 TYPE_CODE (type) = TYPE_CODE_UNION;
15944 }
2ddeaf8a
TT
15945 else if (die->tag == DW_TAG_variant_part)
15946 {
15947 TYPE_CODE (type) = TYPE_CODE_UNION;
15948 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15949 }
c906108c
SS
15950 else
15951 {
4753d33b 15952 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15953 }
15954
0cc2414c
TT
15955 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15956 TYPE_DECLARED_CLASS (type) = 1;
15957
e35000a7
TBA
15958 /* Store the calling convention in the type if it's available in
15959 the die. Otherwise the calling convention remains set to
15960 the default value DW_CC_normal. */
15961 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15962 if (attr != nullptr
15963 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15964 {
15965 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15966 TYPE_CPLUS_CALLING_CONVENTION (type)
15967 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15968 }
15969
e142c38c 15970 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15971 if (attr != nullptr)
c906108c 15972 {
155bfbd3
JB
15973 if (attr_form_is_constant (attr))
15974 TYPE_LENGTH (type) = DW_UNSND (attr);
15975 else
15976 {
15977 /* For the moment, dynamic type sizes are not supported
15978 by GDB's struct type. The actual size is determined
15979 on-demand when resolving the type of a given object,
15980 so set the type's length to zero for now. Otherwise,
15981 we record an expression as the length, and that expression
15982 could lead to a very large value, which could eventually
15983 lead to us trying to allocate that much memory when creating
15984 a value of that type. */
15985 TYPE_LENGTH (type) = 0;
15986 }
c906108c
SS
15987 }
15988 else
15989 {
15990 TYPE_LENGTH (type) = 0;
15991 }
15992
2b4424c3
TT
15993 maybe_set_alignment (cu, die, type);
15994
5230b05a 15995 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15996 {
5230b05a
WT
15997 /* ICC<14 does not output the required DW_AT_declaration on
15998 incomplete types, but gives them a size of zero. */
422b1cb0 15999 TYPE_STUB (type) = 1;
685b1105
JK
16000 }
16001 else
16002 TYPE_STUB_SUPPORTED (type) = 1;
16003
dc718098 16004 if (die_is_declaration (die, cu))
876cecd0 16005 TYPE_STUB (type) = 1;
a6c727b2
DJ
16006 else if (attr == NULL && die->child == NULL
16007 && producer_is_realview (cu->producer))
16008 /* RealView does not output the required DW_AT_declaration
16009 on incomplete types. */
16010 TYPE_STUB (type) = 1;
dc718098 16011
c906108c
SS
16012 /* We need to add the type field to the die immediately so we don't
16013 infinitely recurse when dealing with pointers to the structure
0963b4bd 16014 type within the structure itself. */
1c379e20 16015 set_die_type (die, type, cu);
c906108c 16016
7e314c57
JK
16017 /* set_die_type should be already done. */
16018 set_descriptive_type (type, die, cu);
16019
c767944b
DJ
16020 return type;
16021}
16022
2ddeaf8a
TT
16023/* A helper for process_structure_scope that handles a single member
16024 DIE. */
16025
16026static void
16027handle_struct_member_die (struct die_info *child_die, struct type *type,
16028 struct field_info *fi,
16029 std::vector<struct symbol *> *template_args,
16030 struct dwarf2_cu *cu)
16031{
16032 if (child_die->tag == DW_TAG_member
16033 || child_die->tag == DW_TAG_variable
16034 || child_die->tag == DW_TAG_variant_part)
16035 {
16036 /* NOTE: carlton/2002-11-05: A C++ static data member
16037 should be a DW_TAG_member that is a declaration, but
16038 all versions of G++ as of this writing (so through at
16039 least 3.2.1) incorrectly generate DW_TAG_variable
16040 tags for them instead. */
16041 dwarf2_add_field (fi, child_die, cu);
16042 }
16043 else if (child_die->tag == DW_TAG_subprogram)
16044 {
16045 /* Rust doesn't have member functions in the C++ sense.
16046 However, it does emit ordinary functions as children
16047 of a struct DIE. */
16048 if (cu->language == language_rust)
16049 read_func_scope (child_die, cu);
16050 else
16051 {
16052 /* C++ member function. */
16053 dwarf2_add_member_fn (fi, child_die, type, cu);
16054 }
16055 }
16056 else if (child_die->tag == DW_TAG_inheritance)
16057 {
16058 /* C++ base class field. */
16059 dwarf2_add_field (fi, child_die, cu);
16060 }
16061 else if (type_can_define_types (child_die))
16062 dwarf2_add_type_defn (fi, child_die, cu);
16063 else if (child_die->tag == DW_TAG_template_type_param
16064 || child_die->tag == DW_TAG_template_value_param)
16065 {
16066 struct symbol *arg = new_symbol (child_die, NULL, cu);
16067
16068 if (arg != NULL)
16069 template_args->push_back (arg);
16070 }
16071 else if (child_die->tag == DW_TAG_variant)
16072 {
16073 /* In a variant we want to get the discriminant and also add a
16074 field for our sole member child. */
16075 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16076
bde09ab7 16077 for (die_info *variant_child = child_die->child;
2ddeaf8a
TT
16078 variant_child != NULL;
16079 variant_child = sibling_die (variant_child))
16080 {
16081 if (variant_child->tag == DW_TAG_member)
16082 {
16083 handle_struct_member_die (variant_child, type, fi,
16084 template_args, cu);
16085 /* Only handle the one. */
16086 break;
16087 }
16088 }
16089
16090 /* We don't handle this but we might as well report it if we see
16091 it. */
16092 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 16093 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
16094 " - DIE at %s [in module %s]"),
16095 sect_offset_str (child_die->sect_off),
16096 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16097
16098 /* The first field was just added, so we can stash the
16099 discriminant there. */
be2daae6 16100 gdb_assert (!fi->fields.empty ());
2ddeaf8a 16101 if (discr == NULL)
be2daae6 16102 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 16103 else
be2daae6 16104 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
16105 }
16106}
16107
c767944b
DJ
16108/* Finish creating a structure or union type, including filling in
16109 its members and creating a symbol for it. */
16110
16111static void
16112process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16113{
518817b3 16114 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 16115 struct die_info *child_die;
c767944b
DJ
16116 struct type *type;
16117
16118 type = get_die_type (die, cu);
16119 if (type == NULL)
16120 type = read_structure_type (die, cu);
16121
2ddeaf8a
TT
16122 /* When reading a DW_TAG_variant_part, we need to notice when we
16123 read the discriminant member, so we can record it later in the
16124 discriminant_info. */
16125 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
feee869b 16126 sect_offset discr_offset {};
3e1d3d8c 16127 bool has_template_parameters = false;
2ddeaf8a
TT
16128
16129 if (is_variant_part)
16130 {
16131 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16132 if (discr == NULL)
16133 {
16134 /* Maybe it's a univariant form, an extension we support.
16135 In this case arrange not to check the offset. */
16136 is_variant_part = false;
16137 }
16138 else if (attr_form_is_ref (discr))
16139 {
16140 struct dwarf2_cu *target_cu = cu;
16141 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16142
16143 discr_offset = target_die->sect_off;
16144 }
16145 else
16146 {
b98664d3 16147 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
16148 " - DIE at %s [in module %s]"),
16149 sect_offset_str (die->sect_off),
16150 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16151 is_variant_part = false;
16152 }
16153 }
16154
e142c38c 16155 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16156 {
16157 struct field_info fi;
2f4732b0 16158 std::vector<struct symbol *> template_args;
c906108c 16159
639d11d3 16160 child_die = die->child;
c906108c
SS
16161
16162 while (child_die && child_die->tag)
16163 {
2ddeaf8a 16164 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 16165
2ddeaf8a 16166 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 16167 fi.fields.back ().variant.is_discriminant = true;
34eaf542 16168
c906108c
SS
16169 child_die = sibling_die (child_die);
16170 }
16171
34eaf542 16172 /* Attach template arguments to type. */
2f4732b0 16173 if (!template_args.empty ())
34eaf542 16174 {
3e1d3d8c 16175 has_template_parameters = true;
34eaf542 16176 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16177 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16178 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16179 = XOBNEWVEC (&objfile->objfile_obstack,
16180 struct symbol *,
16181 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16182 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16183 template_args.data (),
34eaf542
TT
16184 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16185 * sizeof (struct symbol *)));
34eaf542
TT
16186 }
16187
c906108c
SS
16188 /* Attach fields and member functions to the type. */
16189 if (fi.nfields)
e7c27a73 16190 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16191 if (!fi.fnfieldlists.empty ())
c906108c 16192 {
e7c27a73 16193 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16194
c5aa993b 16195 /* Get the type which refers to the base class (possibly this
c906108c 16196 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16197 class from the DW_AT_containing_type attribute. This use of
16198 DW_AT_containing_type is a GNU extension. */
c906108c 16199
e142c38c 16200 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16201 {
e7c27a73 16202 struct type *t = die_containing_type (die, cu);
c906108c 16203
ae6ae975 16204 set_type_vptr_basetype (type, t);
c906108c
SS
16205 if (type == t)
16206 {
c906108c
SS
16207 int i;
16208
16209 /* Our own class provides vtbl ptr. */
16210 for (i = TYPE_NFIELDS (t) - 1;
16211 i >= TYPE_N_BASECLASSES (t);
16212 --i)
16213 {
0d5cff50 16214 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16215
1168df01 16216 if (is_vtable_name (fieldname, cu))
c906108c 16217 {
ae6ae975 16218 set_type_vptr_fieldno (type, i);
c906108c
SS
16219 break;
16220 }
16221 }
16222
16223 /* Complain if virtual function table field not found. */
16224 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16225 complaint (_("virtual function table pointer "
3e43a32a 16226 "not found when defining class '%s'"),
e86ca25f 16227 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
16228 }
16229 else
16230 {
ae6ae975 16231 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16232 }
16233 }
f6235d4c 16234 else if (cu->producer
61012eef 16235 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16236 {
16237 /* The IBM XLC compiler does not provide direct indication
16238 of the containing type, but the vtable pointer is
16239 always named __vfp. */
16240
16241 int i;
16242
16243 for (i = TYPE_NFIELDS (type) - 1;
16244 i >= TYPE_N_BASECLASSES (type);
16245 --i)
16246 {
16247 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16248 {
ae6ae975
DE
16249 set_type_vptr_fieldno (type, i);
16250 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16251 break;
16252 }
16253 }
16254 }
c906108c 16255 }
98751a41
JK
16256
16257 /* Copy fi.typedef_field_list linked list elements content into the
16258 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16259 if (!fi.typedef_field_list.empty ())
98751a41 16260 {
be2daae6 16261 int count = fi.typedef_field_list.size ();
98751a41 16262
a0d7a4ff 16263 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16264 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16265 = ((struct decl_field *)
be2daae6
TT
16266 TYPE_ALLOC (type,
16267 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16268 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16269
be2daae6
TT
16270 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16271 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16272 }
c767944b 16273
883fd55a
KS
16274 /* Copy fi.nested_types_list linked list elements content into the
16275 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16276 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16277 {
be2daae6 16278 int count = fi.nested_types_list.size ();
883fd55a
KS
16279
16280 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16281 TYPE_NESTED_TYPES_ARRAY (type)
16282 = ((struct decl_field *)
be2daae6
TT
16283 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16284 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16285
be2daae6
TT
16286 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16287 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16288 }
c906108c 16289 }
63d06c5c 16290
bb5ed363 16291 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16292 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16293 cu->rust_unions.push_back (type);
0b92b5bb 16294
90aeadfc
DC
16295 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16296 snapshots) has been known to create a die giving a declaration
16297 for a class that has, as a child, a die giving a definition for a
16298 nested class. So we have to process our children even if the
16299 current die is a declaration. Normally, of course, a declaration
16300 won't have any children at all. */
134d01f1 16301
ca040673
DE
16302 child_die = die->child;
16303
90aeadfc
DC
16304 while (child_die != NULL && child_die->tag)
16305 {
16306 if (child_die->tag == DW_TAG_member
16307 || child_die->tag == DW_TAG_variable
34eaf542
TT
16308 || child_die->tag == DW_TAG_inheritance
16309 || child_die->tag == DW_TAG_template_value_param
16310 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16311 {
90aeadfc 16312 /* Do nothing. */
134d01f1 16313 }
90aeadfc
DC
16314 else
16315 process_die (child_die, cu);
134d01f1 16316
90aeadfc 16317 child_die = sibling_die (child_die);
134d01f1
DJ
16318 }
16319
fa4028e9
JB
16320 /* Do not consider external references. According to the DWARF standard,
16321 these DIEs are identified by the fact that they have no byte_size
16322 attribute, and a declaration attribute. */
16323 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16324 || !die_is_declaration (die, cu))
3e1d3d8c
TT
16325 {
16326 struct symbol *sym = new_symbol (die, type, cu);
16327
16328 if (has_template_parameters)
16329 {
a776957c
TT
16330 struct symtab *symtab;
16331 if (sym != nullptr)
16332 symtab = symbol_symtab (sym);
16333 else if (cu->line_header != nullptr)
16334 {
16335 /* Any related symtab will do. */
16336 symtab
7ba99d21 16337 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16338 }
16339 else
16340 {
16341 symtab = nullptr;
16342 complaint (_("could not find suitable "
16343 "symtab for template parameter"
16344 " - DIE at %s [in module %s]"),
16345 sect_offset_str (die->sect_off),
16346 objfile_name (objfile));
16347 }
16348
16349 if (symtab != nullptr)
16350 {
16351 /* Make sure that the symtab is set on the new symbols.
16352 Even though they don't appear in this symtab directly,
16353 other parts of gdb assume that symbols do, and this is
16354 reasonably true. */
16355 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16356 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16357 }
3e1d3d8c
TT
16358 }
16359 }
134d01f1
DJ
16360}
16361
55426c9d
JB
16362/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16363 update TYPE using some information only available in DIE's children. */
16364
16365static void
16366update_enumeration_type_from_children (struct die_info *die,
16367 struct type *type,
16368 struct dwarf2_cu *cu)
16369{
60f7655a 16370 struct die_info *child_die;
55426c9d
JB
16371 int unsigned_enum = 1;
16372 int flag_enum = 1;
16373 ULONGEST mask = 0;
55426c9d 16374
8268c778 16375 auto_obstack obstack;
55426c9d 16376
60f7655a
DE
16377 for (child_die = die->child;
16378 child_die != NULL && child_die->tag;
16379 child_die = sibling_die (child_die))
55426c9d
JB
16380 {
16381 struct attribute *attr;
16382 LONGEST value;
16383 const gdb_byte *bytes;
16384 struct dwarf2_locexpr_baton *baton;
16385 const char *name;
60f7655a 16386
55426c9d
JB
16387 if (child_die->tag != DW_TAG_enumerator)
16388 continue;
16389
16390 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16391 if (attr == NULL)
16392 continue;
16393
16394 name = dwarf2_name (child_die, cu);
16395 if (name == NULL)
16396 name = "<anonymous enumerator>";
16397
16398 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16399 &value, &bytes, &baton);
16400 if (value < 0)
16401 {
16402 unsigned_enum = 0;
16403 flag_enum = 0;
16404 }
16405 else if ((mask & value) != 0)
16406 flag_enum = 0;
16407 else
16408 mask |= value;
16409
16410 /* If we already know that the enum type is neither unsigned, nor
16411 a flag type, no need to look at the rest of the enumerates. */
16412 if (!unsigned_enum && !flag_enum)
16413 break;
55426c9d
JB
16414 }
16415
16416 if (unsigned_enum)
16417 TYPE_UNSIGNED (type) = 1;
16418 if (flag_enum)
16419 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16420}
16421
134d01f1
DJ
16422/* Given a DW_AT_enumeration_type die, set its type. We do not
16423 complete the type's fields yet, or create any symbols. */
c906108c 16424
f792889a 16425static struct type *
134d01f1 16426read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16427{
518817b3 16428 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16429 struct type *type;
c906108c 16430 struct attribute *attr;
0114d602 16431 const char *name;
134d01f1 16432
348e048f
DE
16433 /* If the definition of this type lives in .debug_types, read that type.
16434 Don't follow DW_AT_specification though, that will take us back up
16435 the chain and we want to go down. */
45e58e77 16436 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 16437 if (attr != nullptr)
348e048f 16438 {
ac9ec31b 16439 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16440
ac9ec31b 16441 /* The type's CU may not be the same as CU.
02142a6c 16442 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16443 return set_die_type (die, type, cu);
16444 }
16445
c906108c
SS
16446 type = alloc_type (objfile);
16447
16448 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16449 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16450 if (name != NULL)
e86ca25f 16451 TYPE_NAME (type) = name;
c906108c 16452
0626fc76
TT
16453 attr = dwarf2_attr (die, DW_AT_type, cu);
16454 if (attr != NULL)
16455 {
16456 struct type *underlying_type = die_type (die, cu);
16457
16458 TYPE_TARGET_TYPE (type) = underlying_type;
16459 }
16460
e142c38c 16461 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16462 if (attr != nullptr)
c906108c
SS
16463 {
16464 TYPE_LENGTH (type) = DW_UNSND (attr);
16465 }
16466 else
16467 {
16468 TYPE_LENGTH (type) = 0;
16469 }
16470
2b4424c3
TT
16471 maybe_set_alignment (cu, die, type);
16472
137033e9
JB
16473 /* The enumeration DIE can be incomplete. In Ada, any type can be
16474 declared as private in the package spec, and then defined only
16475 inside the package body. Such types are known as Taft Amendment
16476 Types. When another package uses such a type, an incomplete DIE
16477 may be generated by the compiler. */
02eb380e 16478 if (die_is_declaration (die, cu))
876cecd0 16479 TYPE_STUB (type) = 1;
02eb380e 16480
0626fc76
TT
16481 /* Finish the creation of this type by using the enum's children.
16482 We must call this even when the underlying type has been provided
16483 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16484 update_enumeration_type_from_children (die, type, cu);
16485
0626fc76
TT
16486 /* If this type has an underlying type that is not a stub, then we
16487 may use its attributes. We always use the "unsigned" attribute
16488 in this situation, because ordinarily we guess whether the type
16489 is unsigned -- but the guess can be wrong and the underlying type
16490 can tell us the reality. However, we defer to a local size
16491 attribute if one exists, because this lets the compiler override
16492 the underlying type if needed. */
16493 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16494 {
16495 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16496 if (TYPE_LENGTH (type) == 0)
16497 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
16498 if (TYPE_RAW_ALIGN (type) == 0
16499 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16500 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
16501 }
16502
3d567982
TT
16503 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16504
f792889a 16505 return set_die_type (die, type, cu);
134d01f1
DJ
16506}
16507
16508/* Given a pointer to a die which begins an enumeration, process all
16509 the dies that define the members of the enumeration, and create the
16510 symbol for the enumeration type.
16511
16512 NOTE: We reverse the order of the element list. */
16513
16514static void
16515process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16516{
f792889a 16517 struct type *this_type;
134d01f1 16518
f792889a
DJ
16519 this_type = get_die_type (die, cu);
16520 if (this_type == NULL)
16521 this_type = read_enumeration_type (die, cu);
9dc481d3 16522
639d11d3 16523 if (die->child != NULL)
c906108c 16524 {
9dc481d3
DE
16525 struct die_info *child_die;
16526 struct symbol *sym;
43816ebc 16527 std::vector<struct field> fields;
15d034d0 16528 const char *name;
9dc481d3 16529
639d11d3 16530 child_die = die->child;
c906108c
SS
16531 while (child_die && child_die->tag)
16532 {
16533 if (child_die->tag != DW_TAG_enumerator)
16534 {
e7c27a73 16535 process_die (child_die, cu);
c906108c
SS
16536 }
16537 else
16538 {
39cbfefa
DJ
16539 name = dwarf2_name (child_die, cu);
16540 if (name)
c906108c 16541 {
f792889a 16542 sym = new_symbol (child_die, this_type, cu);
c906108c 16543
43816ebc
TT
16544 fields.emplace_back ();
16545 struct field &field = fields.back ();
c906108c 16546
43816ebc
TT
16547 FIELD_NAME (field) = sym->linkage_name ();
16548 FIELD_TYPE (field) = NULL;
16549 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
16550 FIELD_BITSIZE (field) = 0;
c906108c
SS
16551 }
16552 }
16553
16554 child_die = sibling_die (child_die);
16555 }
16556
43816ebc 16557 if (!fields.empty ())
c906108c 16558 {
43816ebc 16559 TYPE_NFIELDS (this_type) = fields.size ();
f792889a 16560 TYPE_FIELDS (this_type) = (struct field *)
43816ebc
TT
16561 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
16562 memcpy (TYPE_FIELDS (this_type), fields.data (),
16563 sizeof (struct field) * fields.size ());
c906108c 16564 }
c906108c 16565 }
134d01f1 16566
6c83ed52
TT
16567 /* If we are reading an enum from a .debug_types unit, and the enum
16568 is a declaration, and the enum is not the signatured type in the
16569 unit, then we do not want to add a symbol for it. Adding a
16570 symbol would in some cases obscure the true definition of the
16571 enum, giving users an incomplete type when the definition is
16572 actually available. Note that we do not want to do this for all
16573 enums which are just declarations, because C++0x allows forward
16574 enum declarations. */
3019eac3 16575 if (cu->per_cu->is_debug_types
6c83ed52
TT
16576 && die_is_declaration (die, cu))
16577 {
52dc124a 16578 struct signatured_type *sig_type;
6c83ed52 16579
c0f78cd4 16580 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16581 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16582 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16583 return;
16584 }
16585
f792889a 16586 new_symbol (die, this_type, cu);
c906108c
SS
16587}
16588
16589/* Extract all information from a DW_TAG_array_type DIE and put it in
16590 the DIE's type field. For now, this only handles one dimensional
16591 arrays. */
16592
f792889a 16593static struct type *
e7c27a73 16594read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16595{
518817b3 16596 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16597 struct die_info *child_die;
7e314c57 16598 struct type *type;
c906108c 16599 struct type *element_type, *range_type, *index_type;
c906108c 16600 struct attribute *attr;
15d034d0 16601 const char *name;
a405673c 16602 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16603 unsigned int bit_stride = 0;
c906108c 16604
e7c27a73 16605 element_type = die_type (die, cu);
c906108c 16606
7e314c57
JK
16607 /* The die_type call above may have already set the type for this DIE. */
16608 type = get_die_type (die, cu);
16609 if (type)
16610 return type;
16611
dc53a7ad
JB
16612 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16613 if (attr != NULL)
a405673c
JB
16614 {
16615 int stride_ok;
9a49df9d
AB
16616 struct type *prop_type
16617 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
a405673c
JB
16618
16619 byte_stride_prop
16620 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16621 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16622 prop_type);
a405673c
JB
16623 if (!stride_ok)
16624 {
b98664d3 16625 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16626 " - DIE at %s [in module %s]"),
16627 sect_offset_str (die->sect_off),
518817b3 16628 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16629 /* Ignore this attribute. We will likely not be able to print
16630 arrays of this type correctly, but there is little we can do
16631 to help if we cannot read the attribute's value. */
16632 byte_stride_prop = NULL;
16633 }
16634 }
dc53a7ad
JB
16635
16636 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16637 if (attr != NULL)
16638 bit_stride = DW_UNSND (attr);
16639
c906108c
SS
16640 /* Irix 6.2 native cc creates array types without children for
16641 arrays with unspecified length. */
639d11d3 16642 if (die->child == NULL)
c906108c 16643 {
46bf5051 16644 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16645 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16646 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16647 byte_stride_prop, bit_stride);
f792889a 16648 return set_die_type (die, type, cu);
c906108c
SS
16649 }
16650
791afaa2 16651 std::vector<struct type *> range_types;
639d11d3 16652 child_die = die->child;
c906108c
SS
16653 while (child_die && child_die->tag)
16654 {
16655 if (child_die->tag == DW_TAG_subrange_type)
16656 {
f792889a 16657 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16658
f792889a 16659 if (child_type != NULL)
a02abb62 16660 {
0963b4bd
MS
16661 /* The range type was succesfully read. Save it for the
16662 array type creation. */
791afaa2 16663 range_types.push_back (child_type);
a02abb62 16664 }
c906108c
SS
16665 }
16666 child_die = sibling_die (child_die);
16667 }
16668
16669 /* Dwarf2 dimensions are output from left to right, create the
16670 necessary array types in backwards order. */
7ca2d3a3 16671
c906108c 16672 type = element_type;
7ca2d3a3
DL
16673
16674 if (read_array_order (die, cu) == DW_ORD_col_major)
16675 {
16676 int i = 0;
9a619af0 16677
791afaa2 16678 while (i < range_types.size ())
dc53a7ad 16679 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16680 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16681 }
16682 else
16683 {
791afaa2 16684 size_t ndim = range_types.size ();
7ca2d3a3 16685 while (ndim-- > 0)
dc53a7ad 16686 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16687 byte_stride_prop, bit_stride);
7ca2d3a3 16688 }
c906108c 16689
f5f8a009
EZ
16690 /* Understand Dwarf2 support for vector types (like they occur on
16691 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16692 array type. This is not part of the Dwarf2/3 standard yet, but a
16693 custom vendor extension. The main difference between a regular
16694 array and the vector variant is that vectors are passed by value
16695 to functions. */
e142c38c 16696 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16697 if (attr != nullptr)
ea37ba09 16698 make_vector_type (type);
f5f8a009 16699
dbc98a8b
KW
16700 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16701 implementation may choose to implement triple vectors using this
16702 attribute. */
16703 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16704 if (attr != nullptr)
dbc98a8b
KW
16705 {
16706 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16707 TYPE_LENGTH (type) = DW_UNSND (attr);
16708 else
b98664d3 16709 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16710 "than the total size of elements"));
dbc98a8b
KW
16711 }
16712
39cbfefa
DJ
16713 name = dwarf2_name (die, cu);
16714 if (name)
16715 TYPE_NAME (type) = name;
6e70227d 16716
2b4424c3
TT
16717 maybe_set_alignment (cu, die, type);
16718
0963b4bd 16719 /* Install the type in the die. */
7e314c57
JK
16720 set_die_type (die, type, cu);
16721
16722 /* set_die_type should be already done. */
b4ba55a1
JB
16723 set_descriptive_type (type, die, cu);
16724
7e314c57 16725 return type;
c906108c
SS
16726}
16727
7ca2d3a3 16728static enum dwarf_array_dim_ordering
6e70227d 16729read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16730{
16731 struct attribute *attr;
16732
16733 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16734
435d3d88 16735 if (attr != nullptr)
aead7601 16736 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16737
0963b4bd
MS
16738 /* GNU F77 is a special case, as at 08/2004 array type info is the
16739 opposite order to the dwarf2 specification, but data is still
16740 laid out as per normal fortran.
7ca2d3a3 16741
0963b4bd
MS
16742 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16743 version checking. */
7ca2d3a3 16744
905e0470
PM
16745 if (cu->language == language_fortran
16746 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16747 {
16748 return DW_ORD_row_major;
16749 }
16750
6e70227d 16751 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16752 {
16753 case array_column_major:
16754 return DW_ORD_col_major;
16755 case array_row_major:
16756 default:
16757 return DW_ORD_row_major;
16758 };
16759}
16760
72019c9c 16761/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16762 the DIE's type field. */
72019c9c 16763
f792889a 16764static struct type *
72019c9c
GM
16765read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16766{
7e314c57
JK
16767 struct type *domain_type, *set_type;
16768 struct attribute *attr;
f792889a 16769
7e314c57
JK
16770 domain_type = die_type (die, cu);
16771
16772 /* The die_type call above may have already set the type for this DIE. */
16773 set_type = get_die_type (die, cu);
16774 if (set_type)
16775 return set_type;
16776
16777 set_type = create_set_type (NULL, domain_type);
16778
16779 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16780 if (attr != nullptr)
d09039dd 16781 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16782
2b4424c3
TT
16783 maybe_set_alignment (cu, die, set_type);
16784
f792889a 16785 return set_die_type (die, set_type, cu);
72019c9c 16786}
7ca2d3a3 16787
0971de02
TT
16788/* A helper for read_common_block that creates a locexpr baton.
16789 SYM is the symbol which we are marking as computed.
16790 COMMON_DIE is the DIE for the common block.
16791 COMMON_LOC is the location expression attribute for the common
16792 block itself.
16793 MEMBER_LOC is the location expression attribute for the particular
16794 member of the common block that we are processing.
16795 CU is the CU from which the above come. */
16796
16797static void
16798mark_common_block_symbol_computed (struct symbol *sym,
16799 struct die_info *common_die,
16800 struct attribute *common_loc,
16801 struct attribute *member_loc,
16802 struct dwarf2_cu *cu)
16803{
518817b3
SM
16804 struct dwarf2_per_objfile *dwarf2_per_objfile
16805 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16806 struct objfile *objfile = dwarf2_per_objfile->objfile;
16807 struct dwarf2_locexpr_baton *baton;
16808 gdb_byte *ptr;
16809 unsigned int cu_off;
16810 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16811 LONGEST offset = 0;
16812
16813 gdb_assert (common_loc && member_loc);
16814 gdb_assert (attr_form_is_block (common_loc));
16815 gdb_assert (attr_form_is_block (member_loc)
16816 || attr_form_is_constant (member_loc));
16817
8d749320 16818 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16819 baton->per_cu = cu->per_cu;
16820 gdb_assert (baton->per_cu);
16821
16822 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16823
16824 if (attr_form_is_constant (member_loc))
16825 {
16826 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16827 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16828 }
16829 else
16830 baton->size += DW_BLOCK (member_loc)->size;
16831
224c3ddb 16832 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16833 baton->data = ptr;
16834
16835 *ptr++ = DW_OP_call4;
9c541725 16836 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16837 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16838 ptr += 4;
16839
16840 if (attr_form_is_constant (member_loc))
16841 {
16842 *ptr++ = DW_OP_addr;
16843 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16844 ptr += cu->header.addr_size;
16845 }
16846 else
16847 {
16848 /* We have to copy the data here, because DW_OP_call4 will only
16849 use a DW_AT_location attribute. */
16850 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16851 ptr += DW_BLOCK (member_loc)->size;
16852 }
16853
16854 *ptr++ = DW_OP_plus;
16855 gdb_assert (ptr - baton->data == baton->size);
16856
0971de02 16857 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16858 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16859}
16860
4357ac6c
TT
16861/* Create appropriate locally-scoped variables for all the
16862 DW_TAG_common_block entries. Also create a struct common_block
16863 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16864 is used to separate the common blocks name namespace from regular
4357ac6c 16865 variable names. */
c906108c
SS
16866
16867static void
e7c27a73 16868read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16869{
0971de02
TT
16870 struct attribute *attr;
16871
16872 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16873 if (attr != nullptr)
0971de02
TT
16874 {
16875 /* Support the .debug_loc offsets. */
16876 if (attr_form_is_block (attr))
16877 {
16878 /* Ok. */
16879 }
16880 else if (attr_form_is_section_offset (attr))
16881 {
16882 dwarf2_complex_location_expr_complaint ();
16883 attr = NULL;
16884 }
16885 else
16886 {
16887 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16888 "common block member");
16889 attr = NULL;
16890 }
16891 }
16892
639d11d3 16893 if (die->child != NULL)
c906108c 16894 {
518817b3 16895 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16896 struct die_info *child_die;
16897 size_t n_entries = 0, size;
16898 struct common_block *common_block;
16899 struct symbol *sym;
74ac6d43 16900
4357ac6c
TT
16901 for (child_die = die->child;
16902 child_die && child_die->tag;
16903 child_die = sibling_die (child_die))
16904 ++n_entries;
16905
16906 size = (sizeof (struct common_block)
16907 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16908 common_block
16909 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16910 size);
4357ac6c
TT
16911 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16912 common_block->n_entries = 0;
16913
16914 for (child_die = die->child;
16915 child_die && child_die->tag;
16916 child_die = sibling_die (child_die))
16917 {
16918 /* Create the symbol in the DW_TAG_common_block block in the current
16919 symbol scope. */
e7c27a73 16920 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16921 if (sym != NULL)
16922 {
16923 struct attribute *member_loc;
16924
16925 common_block->contents[common_block->n_entries++] = sym;
16926
16927 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16928 cu);
16929 if (member_loc)
16930 {
16931 /* GDB has handled this for a long time, but it is
16932 not specified by DWARF. It seems to have been
16933 emitted by gfortran at least as recently as:
16934 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16935 complaint (_("Variable in common block has "
0971de02 16936 "DW_AT_data_member_location "
9d8780f0
SM
16937 "- DIE at %s [in module %s]"),
16938 sect_offset_str (child_die->sect_off),
518817b3 16939 objfile_name (objfile));
0971de02
TT
16940
16941 if (attr_form_is_section_offset (member_loc))
16942 dwarf2_complex_location_expr_complaint ();
16943 else if (attr_form_is_constant (member_loc)
16944 || attr_form_is_block (member_loc))
16945 {
435d3d88 16946 if (attr != nullptr)
0971de02
TT
16947 mark_common_block_symbol_computed (sym, die, attr,
16948 member_loc, cu);
16949 }
16950 else
16951 dwarf2_complex_location_expr_complaint ();
16952 }
16953 }
c906108c 16954 }
4357ac6c
TT
16955
16956 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16957 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16958 }
16959}
16960
0114d602 16961/* Create a type for a C++ namespace. */
d9fa45fe 16962
0114d602
DJ
16963static struct type *
16964read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16965{
518817b3 16966 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16967 const char *previous_prefix, *name;
9219021c 16968 int is_anonymous;
0114d602
DJ
16969 struct type *type;
16970
16971 /* For extensions, reuse the type of the original namespace. */
16972 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16973 {
16974 struct die_info *ext_die;
16975 struct dwarf2_cu *ext_cu = cu;
9a619af0 16976
0114d602
DJ
16977 ext_die = dwarf2_extension (die, &ext_cu);
16978 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16979
16980 /* EXT_CU may not be the same as CU.
02142a6c 16981 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16982 return set_die_type (die, type, cu);
16983 }
9219021c 16984
e142c38c 16985 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16986
16987 /* Now build the name of the current namespace. */
16988
0114d602
DJ
16989 previous_prefix = determine_prefix (die, cu);
16990 if (previous_prefix[0] != '\0')
16991 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16992 previous_prefix, name, 0, cu);
0114d602
DJ
16993
16994 /* Create the type. */
19f392bc 16995 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16996
60531b24 16997 return set_die_type (die, type, cu);
0114d602
DJ
16998}
16999
22cee43f 17000/* Read a namespace scope. */
0114d602
DJ
17001
17002static void
17003read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17004{
518817b3 17005 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17006 int is_anonymous;
9219021c 17007
5c4e30ca
DC
17008 /* Add a symbol associated to this if we haven't seen the namespace
17009 before. Also, add a using directive if it's an anonymous
17010 namespace. */
9219021c 17011
f2f0e013 17012 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
17013 {
17014 struct type *type;
17015
0114d602 17016 type = read_type_die (die, cu);
e7c27a73 17017 new_symbol (die, type, cu);
5c4e30ca 17018
e8e80198 17019 namespace_name (die, &is_anonymous, cu);
5c4e30ca 17020 if (is_anonymous)
0114d602
DJ
17021 {
17022 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 17023
eb1e02fd 17024 std::vector<const char *> excludes;
804d2729 17025 add_using_directive (using_directives (cu),
22cee43f 17026 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 17027 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 17028 }
5c4e30ca 17029 }
9219021c 17030
639d11d3 17031 if (die->child != NULL)
d9fa45fe 17032 {
639d11d3 17033 struct die_info *child_die = die->child;
6e70227d 17034
d9fa45fe
DC
17035 while (child_die && child_die->tag)
17036 {
e7c27a73 17037 process_die (child_die, cu);
d9fa45fe
DC
17038 child_die = sibling_die (child_die);
17039 }
17040 }
38d518c9
EZ
17041}
17042
f55ee35c
JK
17043/* Read a Fortran module as type. This DIE can be only a declaration used for
17044 imported module. Still we need that type as local Fortran "use ... only"
17045 declaration imports depend on the created type in determine_prefix. */
17046
17047static struct type *
17048read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17049{
518817b3 17050 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 17051 const char *module_name;
f55ee35c
JK
17052 struct type *type;
17053
17054 module_name = dwarf2_name (die, cu);
19f392bc 17055 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 17056
f55ee35c
JK
17057 return set_die_type (die, type, cu);
17058}
17059
5d7cb8df
JK
17060/* Read a Fortran module. */
17061
17062static void
17063read_module (struct die_info *die, struct dwarf2_cu *cu)
17064{
17065 struct die_info *child_die = die->child;
530e8392
KB
17066 struct type *type;
17067
17068 type = read_type_die (die, cu);
17069 new_symbol (die, type, cu);
5d7cb8df 17070
5d7cb8df
JK
17071 while (child_die && child_die->tag)
17072 {
17073 process_die (child_die, cu);
17074 child_die = sibling_die (child_die);
17075 }
17076}
17077
38d518c9
EZ
17078/* Return the name of the namespace represented by DIE. Set
17079 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17080 namespace. */
17081
17082static const char *
e142c38c 17083namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17084{
17085 struct die_info *current_die;
17086 const char *name = NULL;
17087
17088 /* Loop through the extensions until we find a name. */
17089
17090 for (current_die = die;
17091 current_die != NULL;
f2f0e013 17092 current_die = dwarf2_extension (die, &cu))
38d518c9 17093 {
96553a0c
DE
17094 /* We don't use dwarf2_name here so that we can detect the absence
17095 of a name -> anonymous namespace. */
7d45c7c3 17096 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17097
38d518c9
EZ
17098 if (name != NULL)
17099 break;
17100 }
17101
17102 /* Is it an anonymous namespace? */
17103
17104 *is_anonymous = (name == NULL);
17105 if (*is_anonymous)
2b1dbab0 17106 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17107
17108 return name;
d9fa45fe
DC
17109}
17110
c906108c
SS
17111/* Extract all information from a DW_TAG_pointer_type DIE and add to
17112 the user defined type vector. */
17113
f792889a 17114static struct type *
e7c27a73 17115read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17116{
518817b3
SM
17117 struct gdbarch *gdbarch
17118 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 17119 struct comp_unit_head *cu_header = &cu->header;
c906108c 17120 struct type *type;
8b2dbe47
KB
17121 struct attribute *attr_byte_size;
17122 struct attribute *attr_address_class;
17123 int byte_size, addr_class;
7e314c57
JK
17124 struct type *target_type;
17125
17126 target_type = die_type (die, cu);
c906108c 17127
7e314c57
JK
17128 /* The die_type call above may have already set the type for this DIE. */
17129 type = get_die_type (die, cu);
17130 if (type)
17131 return type;
17132
17133 type = lookup_pointer_type (target_type);
8b2dbe47 17134
e142c38c 17135 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
17136 if (attr_byte_size)
17137 byte_size = DW_UNSND (attr_byte_size);
c906108c 17138 else
8b2dbe47
KB
17139 byte_size = cu_header->addr_size;
17140
e142c38c 17141 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
17142 if (attr_address_class)
17143 addr_class = DW_UNSND (attr_address_class);
17144 else
17145 addr_class = DW_ADDR_none;
17146
2b4424c3
TT
17147 ULONGEST alignment = get_alignment (cu, die);
17148
17149 /* If the pointer size, alignment, or address class is different
17150 than the default, create a type variant marked as such and set
17151 the length accordingly. */
17152 if (TYPE_LENGTH (type) != byte_size
17153 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17154 && alignment != TYPE_RAW_ALIGN (type))
17155 || addr_class != DW_ADDR_none)
c906108c 17156 {
5e2b427d 17157 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
17158 {
17159 int type_flags;
17160
849957d9 17161 type_flags = gdbarch_address_class_type_flags
5e2b427d 17162 (gdbarch, byte_size, addr_class);
876cecd0
TT
17163 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17164 == 0);
8b2dbe47
KB
17165 type = make_type_with_address_space (type, type_flags);
17166 }
17167 else if (TYPE_LENGTH (type) != byte_size)
17168 {
b98664d3 17169 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17170 }
2b4424c3
TT
17171 else if (TYPE_RAW_ALIGN (type) != alignment)
17172 {
b98664d3 17173 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17174 " - DIE at %s [in module %s]"),
17175 sect_offset_str (die->sect_off),
17176 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17177 }
6e70227d 17178 else
9a619af0
MS
17179 {
17180 /* Should we also complain about unhandled address classes? */
17181 }
c906108c 17182 }
8b2dbe47
KB
17183
17184 TYPE_LENGTH (type) = byte_size;
2b4424c3 17185 set_type_align (type, alignment);
f792889a 17186 return set_die_type (die, type, cu);
c906108c
SS
17187}
17188
17189/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17190 the user defined type vector. */
17191
f792889a 17192static struct type *
e7c27a73 17193read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17194{
17195 struct type *type;
17196 struct type *to_type;
17197 struct type *domain;
17198
e7c27a73
DJ
17199 to_type = die_type (die, cu);
17200 domain = die_containing_type (die, cu);
0d5de010 17201
7e314c57
JK
17202 /* The calls above may have already set the type for this DIE. */
17203 type = get_die_type (die, cu);
17204 if (type)
17205 return type;
17206
0d5de010
DJ
17207 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17208 type = lookup_methodptr_type (to_type);
7078baeb
TT
17209 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17210 {
518817b3
SM
17211 struct type *new_type
17212 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
17213
17214 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17215 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17216 TYPE_VARARGS (to_type));
17217 type = lookup_methodptr_type (new_type);
17218 }
0d5de010
DJ
17219 else
17220 type = lookup_memberptr_type (to_type, domain);
c906108c 17221
f792889a 17222 return set_die_type (die, type, cu);
c906108c
SS
17223}
17224
4297a3f0 17225/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17226 the user defined type vector. */
17227
f792889a 17228static struct type *
4297a3f0
AV
17229read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17230 enum type_code refcode)
c906108c 17231{
e7c27a73 17232 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17233 struct type *type, *target_type;
c906108c
SS
17234 struct attribute *attr;
17235
4297a3f0
AV
17236 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17237
7e314c57
JK
17238 target_type = die_type (die, cu);
17239
17240 /* The die_type call above may have already set the type for this DIE. */
17241 type = get_die_type (die, cu);
17242 if (type)
17243 return type;
17244
4297a3f0 17245 type = lookup_reference_type (target_type, refcode);
e142c38c 17246 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17247 if (attr != nullptr)
c906108c
SS
17248 {
17249 TYPE_LENGTH (type) = DW_UNSND (attr);
17250 }
17251 else
17252 {
107d2387 17253 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17254 }
2b4424c3 17255 maybe_set_alignment (cu, die, type);
f792889a 17256 return set_die_type (die, type, cu);
c906108c
SS
17257}
17258
cf363f18
MW
17259/* Add the given cv-qualifiers to the element type of the array. GCC
17260 outputs DWARF type qualifiers that apply to an array, not the
17261 element type. But GDB relies on the array element type to carry
17262 the cv-qualifiers. This mimics section 6.7.3 of the C99
17263 specification. */
17264
17265static struct type *
17266add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17267 struct type *base_type, int cnst, int voltl)
17268{
17269 struct type *el_type, *inner_array;
17270
17271 base_type = copy_type (base_type);
17272 inner_array = base_type;
17273
17274 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17275 {
17276 TYPE_TARGET_TYPE (inner_array) =
17277 copy_type (TYPE_TARGET_TYPE (inner_array));
17278 inner_array = TYPE_TARGET_TYPE (inner_array);
17279 }
17280
17281 el_type = TYPE_TARGET_TYPE (inner_array);
17282 cnst |= TYPE_CONST (el_type);
17283 voltl |= TYPE_VOLATILE (el_type);
17284 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17285
17286 return set_die_type (die, base_type, cu);
17287}
17288
f792889a 17289static struct type *
e7c27a73 17290read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17291{
f792889a 17292 struct type *base_type, *cv_type;
c906108c 17293
e7c27a73 17294 base_type = die_type (die, cu);
7e314c57
JK
17295
17296 /* The die_type call above may have already set the type for this DIE. */
17297 cv_type = get_die_type (die, cu);
17298 if (cv_type)
17299 return cv_type;
17300
2f608a3a
KW
17301 /* In case the const qualifier is applied to an array type, the element type
17302 is so qualified, not the array type (section 6.7.3 of C99). */
17303 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 17304 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17305
f792889a
DJ
17306 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17307 return set_die_type (die, cv_type, cu);
c906108c
SS
17308}
17309
f792889a 17310static struct type *
e7c27a73 17311read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17312{
f792889a 17313 struct type *base_type, *cv_type;
c906108c 17314
e7c27a73 17315 base_type = die_type (die, cu);
7e314c57
JK
17316
17317 /* The die_type call above may have already set the type for this DIE. */
17318 cv_type = get_die_type (die, cu);
17319 if (cv_type)
17320 return cv_type;
17321
cf363f18
MW
17322 /* In case the volatile qualifier is applied to an array type, the
17323 element type is so qualified, not the array type (section 6.7.3
17324 of C99). */
17325 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17326 return add_array_cv_type (die, cu, base_type, 0, 1);
17327
f792889a
DJ
17328 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17329 return set_die_type (die, cv_type, cu);
c906108c
SS
17330}
17331
06d66ee9
TT
17332/* Handle DW_TAG_restrict_type. */
17333
17334static struct type *
17335read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17336{
17337 struct type *base_type, *cv_type;
17338
17339 base_type = die_type (die, cu);
17340
17341 /* The die_type call above may have already set the type for this DIE. */
17342 cv_type = get_die_type (die, cu);
17343 if (cv_type)
17344 return cv_type;
17345
17346 cv_type = make_restrict_type (base_type);
17347 return set_die_type (die, cv_type, cu);
17348}
17349
a2c2acaf
MW
17350/* Handle DW_TAG_atomic_type. */
17351
17352static struct type *
17353read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17354{
17355 struct type *base_type, *cv_type;
17356
17357 base_type = die_type (die, cu);
17358
17359 /* The die_type call above may have already set the type for this DIE. */
17360 cv_type = get_die_type (die, cu);
17361 if (cv_type)
17362 return cv_type;
17363
17364 cv_type = make_atomic_type (base_type);
17365 return set_die_type (die, cv_type, cu);
17366}
17367
c906108c
SS
17368/* Extract all information from a DW_TAG_string_type DIE and add to
17369 the user defined type vector. It isn't really a user defined type,
17370 but it behaves like one, with other DIE's using an AT_user_def_type
17371 attribute to reference it. */
17372
f792889a 17373static struct type *
e7c27a73 17374read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17375{
518817b3 17376 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17377 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17378 struct type *type, *range_type, *index_type, *char_type;
17379 struct attribute *attr;
216a7e6b
AB
17380 struct dynamic_prop prop;
17381 bool length_is_constant = true;
17382 LONGEST length;
17383
17384 /* There are a couple of places where bit sizes might be made use of
17385 when parsing a DW_TAG_string_type, however, no producer that we know
17386 of make use of these. Handling bit sizes that are a multiple of the
17387 byte size is easy enough, but what about other bit sizes? Lets deal
17388 with that problem when we have to. Warn about these attributes being
17389 unsupported, then parse the type and ignore them like we always
17390 have. */
17391 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17392 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17393 {
17394 static bool warning_printed = false;
17395 if (!warning_printed)
17396 {
17397 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17398 "currently supported on DW_TAG_string_type."));
17399 warning_printed = true;
17400 }
17401 }
c906108c 17402
e142c38c 17403 attr = dwarf2_attr (die, DW_AT_string_length, cu);
216a7e6b
AB
17404 if (attr != nullptr && !attr_form_is_constant (attr))
17405 {
17406 /* The string length describes the location at which the length of
17407 the string can be found. The size of the length field can be
17408 specified with one of the attributes below. */
17409 struct type *prop_type;
17410 struct attribute *len
17411 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17412 if (len == nullptr)
17413 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17414 if (len != nullptr && attr_form_is_constant (len))
17415 {
17416 /* Pass 0 as the default as we know this attribute is constant
17417 and the default value will not be returned. */
17418 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
17419 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
17420 }
17421 else
17422 {
17423 /* If the size is not specified then we assume it is the size of
17424 an address on this target. */
17425 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
17426 }
17427
17428 /* Convert the attribute into a dynamic property. */
17429 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17430 length = 1;
17431 else
17432 length_is_constant = false;
17433 }
17434 else if (attr != nullptr)
17435 {
17436 /* This DW_AT_string_length just contains the length with no
17437 indirection. There's no need to create a dynamic property in this
17438 case. Pass 0 for the default value as we know it will not be
17439 returned in this case. */
17440 length = dwarf2_get_attr_constant_value (attr, 0);
17441 }
17442 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17443 {
216a7e6b
AB
17444 /* We don't currently support non-constant byte sizes for strings. */
17445 length = dwarf2_get_attr_constant_value (attr, 1);
c906108c
SS
17446 }
17447 else
17448 {
216a7e6b
AB
17449 /* Use 1 as a fallback length if we have nothing else. */
17450 length = 1;
c906108c 17451 }
6ccb9162 17452
46bf5051 17453 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17454 if (length_is_constant)
17455 range_type = create_static_range_type (NULL, index_type, 1, length);
17456 else
17457 {
17458 struct dynamic_prop low_bound;
17459
17460 low_bound.kind = PROP_CONST;
17461 low_bound.data.const_val = 1;
17462 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17463 }
3b7538c0
UW
17464 char_type = language_string_char_type (cu->language_defn, gdbarch);
17465 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17466
f792889a 17467 return set_die_type (die, type, cu);
c906108c
SS
17468}
17469
4d804846
JB
17470/* Assuming that DIE corresponds to a function, returns nonzero
17471 if the function is prototyped. */
17472
17473static int
17474prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17475{
17476 struct attribute *attr;
17477
17478 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17479 if (attr && (DW_UNSND (attr) != 0))
17480 return 1;
17481
17482 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17483 is only meaningful for C, but the concept also extends to other
4d804846
JB
17484 languages that allow unprototyped functions (Eg: Objective C).
17485 For all other languages, assume that functions are always
17486 prototyped. */
17487 if (cu->language != language_c
17488 && cu->language != language_objc
17489 && cu->language != language_opencl)
17490 return 1;
17491
17492 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17493 prototyped and unprototyped functions; default to prototyped,
17494 since that is more common in modern code (and RealView warns
17495 about unprototyped functions). */
17496 if (producer_is_realview (cu->producer))
17497 return 1;
17498
17499 return 0;
17500}
17501
c906108c
SS
17502/* Handle DIES due to C code like:
17503
17504 struct foo
c5aa993b
JM
17505 {
17506 int (*funcp)(int a, long l);
17507 int b;
17508 };
c906108c 17509
0963b4bd 17510 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17511
f792889a 17512static struct type *
e7c27a73 17513read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17514{
518817b3 17515 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17516 struct type *type; /* Type that this function returns. */
17517 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17518 struct attribute *attr;
17519
e7c27a73 17520 type = die_type (die, cu);
7e314c57
JK
17521
17522 /* The die_type call above may have already set the type for this DIE. */
17523 ftype = get_die_type (die, cu);
17524 if (ftype)
17525 return ftype;
17526
0c8b41f1 17527 ftype = lookup_function_type (type);
c906108c 17528
4d804846 17529 if (prototyped_function_p (die, cu))
a6c727b2 17530 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17531
c055b101
CV
17532 /* Store the calling convention in the type if it's available in
17533 the subroutine die. Otherwise set the calling convention to
17534 the default value DW_CC_normal. */
17535 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17536 if (attr != nullptr
17537 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17538 TYPE_CALLING_CONVENTION (ftype)
17539 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17540 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17541 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17542 else
17543 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17544
743649fd
MW
17545 /* Record whether the function returns normally to its caller or not
17546 if the DWARF producer set that information. */
17547 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17548 if (attr && (DW_UNSND (attr) != 0))
17549 TYPE_NO_RETURN (ftype) = 1;
17550
76c10ea2
GM
17551 /* We need to add the subroutine type to the die immediately so
17552 we don't infinitely recurse when dealing with parameters
0963b4bd 17553 declared as the same subroutine type. */
76c10ea2 17554 set_die_type (die, ftype, cu);
6e70227d 17555
639d11d3 17556 if (die->child != NULL)
c906108c 17557 {
bb5ed363 17558 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17559 struct die_info *child_die;
8072405b 17560 int nparams, iparams;
c906108c
SS
17561
17562 /* Count the number of parameters.
17563 FIXME: GDB currently ignores vararg functions, but knows about
17564 vararg member functions. */
8072405b 17565 nparams = 0;
639d11d3 17566 child_die = die->child;
c906108c
SS
17567 while (child_die && child_die->tag)
17568 {
17569 if (child_die->tag == DW_TAG_formal_parameter)
17570 nparams++;
17571 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17572 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17573 child_die = sibling_die (child_die);
17574 }
17575
17576 /* Allocate storage for parameters and fill them in. */
17577 TYPE_NFIELDS (ftype) = nparams;
17578 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17579 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17580
8072405b
JK
17581 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17582 even if we error out during the parameters reading below. */
17583 for (iparams = 0; iparams < nparams; iparams++)
17584 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17585
17586 iparams = 0;
639d11d3 17587 child_die = die->child;
c906108c
SS
17588 while (child_die && child_die->tag)
17589 {
17590 if (child_die->tag == DW_TAG_formal_parameter)
17591 {
3ce3b1ba
PA
17592 struct type *arg_type;
17593
17594 /* DWARF version 2 has no clean way to discern C++
17595 static and non-static member functions. G++ helps
17596 GDB by marking the first parameter for non-static
17597 member functions (which is the this pointer) as
17598 artificial. We pass this information to
17599 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17600
17601 DWARF version 3 added DW_AT_object_pointer, which GCC
17602 4.5 does not yet generate. */
e142c38c 17603 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17604 if (attr != nullptr)
c906108c
SS
17605 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17606 else
9c37b5ae 17607 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17608 arg_type = die_type (child_die, cu);
17609
17610 /* RealView does not mark THIS as const, which the testsuite
17611 expects. GCC marks THIS as const in method definitions,
17612 but not in the class specifications (GCC PR 43053). */
17613 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17614 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17615 {
17616 int is_this = 0;
17617 struct dwarf2_cu *arg_cu = cu;
17618 const char *name = dwarf2_name (child_die, cu);
17619
17620 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17621 if (attr != nullptr)
3ce3b1ba
PA
17622 {
17623 /* If the compiler emits this, use it. */
17624 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17625 is_this = 1;
17626 }
17627 else if (name && strcmp (name, "this") == 0)
17628 /* Function definitions will have the argument names. */
17629 is_this = 1;
17630 else if (name == NULL && iparams == 0)
17631 /* Declarations may not have the names, so like
17632 elsewhere in GDB, assume an artificial first
17633 argument is "this". */
17634 is_this = 1;
17635
17636 if (is_this)
17637 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17638 arg_type, 0);
17639 }
17640
17641 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17642 iparams++;
17643 }
17644 child_die = sibling_die (child_die);
17645 }
17646 }
17647
76c10ea2 17648 return ftype;
c906108c
SS
17649}
17650
f792889a 17651static struct type *
e7c27a73 17652read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17653{
518817b3 17654 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17655 const char *name = NULL;
3c8e0968 17656 struct type *this_type, *target_type;
c906108c 17657
94af9270 17658 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17659 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17660 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17661 set_die_type (die, this_type, cu);
3c8e0968
DE
17662 target_type = die_type (die, cu);
17663 if (target_type != this_type)
17664 TYPE_TARGET_TYPE (this_type) = target_type;
17665 else
17666 {
17667 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17668 spec and cause infinite loops in GDB. */
b98664d3 17669 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17670 "- DIE at %s [in module %s]"),
17671 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17672 TYPE_TARGET_TYPE (this_type) = NULL;
17673 }
f792889a 17674 return this_type;
c906108c
SS
17675}
17676
9b790ce7
UW
17677/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17678 (which may be different from NAME) to the architecture back-end to allow
17679 it to guess the correct format if necessary. */
17680
17681static struct type *
17682dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17683 const char *name_hint, enum bfd_endian byte_order)
9b790ce7
UW
17684{
17685 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17686 const struct floatformat **format;
17687 struct type *type;
17688
17689 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17690 if (format)
103a685e 17691 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17692 else
77b7c781 17693 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17694
17695 return type;
17696}
17697
eb77c9df
AB
17698/* Allocate an integer type of size BITS and name NAME. */
17699
17700static struct type *
17701dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17702 int bits, int unsigned_p, const char *name)
17703{
17704 struct type *type;
17705
17706 /* Versions of Intel's C Compiler generate an integer type called "void"
17707 instead of using DW_TAG_unspecified_type. This has been seen on
17708 at least versions 14, 17, and 18. */
35ee2dc2
AB
17709 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17710 && strcmp (name, "void") == 0)
eb77c9df
AB
17711 type = objfile_type (objfile)->builtin_void;
17712 else
17713 type = init_integer_type (objfile, bits, unsigned_p, name);
17714
17715 return type;
17716}
17717
8bdc1658
AB
17718/* Initialise and return a floating point type of size BITS suitable for
17719 use as a component of a complex number. The NAME_HINT is passed through
17720 when initialising the floating point type and is the name of the complex
17721 type.
17722
17723 As DWARF doesn't currently provide an explicit name for the components
17724 of a complex number, but it can be helpful to have these components
17725 named, we try to select a suitable name based on the size of the
17726 component. */
17727static struct type *
17728dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17729 struct objfile *objfile,
103a685e
TT
17730 int bits, const char *name_hint,
17731 enum bfd_endian byte_order)
8bdc1658
AB
17732{
17733 gdbarch *gdbarch = get_objfile_arch (objfile);
17734 struct type *tt = nullptr;
17735
35add35e
AB
17736 /* Try to find a suitable floating point builtin type of size BITS.
17737 We're going to use the name of this type as the name for the complex
17738 target type that we are about to create. */
1db455a7 17739 switch (cu->language)
8bdc1658 17740 {
1db455a7
AB
17741 case language_fortran:
17742 switch (bits)
17743 {
17744 case 32:
17745 tt = builtin_f_type (gdbarch)->builtin_real;
17746 break;
17747 case 64:
17748 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17749 break;
17750 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17751 case 128:
17752 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17753 break;
17754 }
8bdc1658 17755 break;
1db455a7
AB
17756 default:
17757 switch (bits)
17758 {
17759 case 32:
17760 tt = builtin_type (gdbarch)->builtin_float;
17761 break;
17762 case 64:
17763 tt = builtin_type (gdbarch)->builtin_double;
17764 break;
17765 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17766 case 128:
17767 tt = builtin_type (gdbarch)->builtin_long_double;
17768 break;
17769 }
8bdc1658
AB
17770 break;
17771 }
17772
35add35e
AB
17773 /* If the type we found doesn't match the size we were looking for, then
17774 pretend we didn't find a type at all, the complex target type we
17775 create will then be nameless. */
a12e5744 17776 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17777 tt = nullptr;
17778
8bdc1658 17779 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
103a685e 17780 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17781}
17782
c906108c
SS
17783/* Find a representation of a given base type and install
17784 it in the TYPE field of the die. */
17785
f792889a 17786static struct type *
e7c27a73 17787read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17788{
518817b3 17789 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17790 struct type *type;
17791 struct attribute *attr;
19f392bc 17792 int encoding = 0, bits = 0;
15d034d0 17793 const char *name;
34877895 17794 gdbarch *arch;
c906108c 17795
e142c38c 17796 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17797 if (attr != nullptr)
34877895 17798 encoding = DW_UNSND (attr);
e142c38c 17799 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17800 if (attr != nullptr)
34877895 17801 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17802 name = dwarf2_name (die, cu);
6ccb9162 17803 if (!name)
34877895 17804 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e
TT
17805
17806 arch = get_objfile_arch (objfile);
17807 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17808
34877895
PJ
17809 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17810 if (attr)
103a685e
TT
17811 {
17812 int endianity = DW_UNSND (attr);
17813
17814 switch (endianity)
17815 {
17816 case DW_END_big:
17817 byte_order = BFD_ENDIAN_BIG;
17818 break;
17819 case DW_END_little:
17820 byte_order = BFD_ENDIAN_LITTLE;
17821 break;
17822 default:
17823 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17824 break;
17825 }
17826 }
6ccb9162
UW
17827
17828 switch (encoding)
c906108c 17829 {
6ccb9162
UW
17830 case DW_ATE_address:
17831 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17832 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17833 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17834 break;
17835 case DW_ATE_boolean:
19f392bc 17836 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17837 break;
17838 case DW_ATE_complex_float:
103a685e
TT
17839 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17840 byte_order);
19f392bc 17841 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17842 break;
17843 case DW_ATE_decimal_float:
19f392bc 17844 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17845 break;
17846 case DW_ATE_float:
103a685e 17847 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17848 break;
17849 case DW_ATE_signed:
eb77c9df 17850 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17851 break;
17852 case DW_ATE_unsigned:
3b2b8fea
TT
17853 if (cu->language == language_fortran
17854 && name
61012eef 17855 && startswith (name, "character("))
19f392bc
UW
17856 type = init_character_type (objfile, bits, 1, name);
17857 else
eb77c9df 17858 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17859 break;
17860 case DW_ATE_signed_char:
6e70227d 17861 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17862 || cu->language == language_pascal
17863 || cu->language == language_fortran)
19f392bc
UW
17864 type = init_character_type (objfile, bits, 0, name);
17865 else
eb77c9df 17866 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17867 break;
17868 case DW_ATE_unsigned_char:
868a0084 17869 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17870 || cu->language == language_pascal
c44af4eb
TT
17871 || cu->language == language_fortran
17872 || cu->language == language_rust)
19f392bc
UW
17873 type = init_character_type (objfile, bits, 1, name);
17874 else
eb77c9df 17875 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17876 break;
75079b2b 17877 case DW_ATE_UTF:
53e710ac 17878 {
53e710ac
PA
17879 if (bits == 16)
17880 type = builtin_type (arch)->builtin_char16;
17881 else if (bits == 32)
17882 type = builtin_type (arch)->builtin_char32;
17883 else
17884 {
b98664d3 17885 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17886 bits);
eb77c9df 17887 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17888 }
17889 return set_die_type (die, type, cu);
17890 }
75079b2b
TT
17891 break;
17892
6ccb9162 17893 default:
b98664d3 17894 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17895 dwarf_type_encoding_name (encoding));
77b7c781 17896 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17897 break;
c906108c 17898 }
6ccb9162 17899
0114d602 17900 if (name && strcmp (name, "char") == 0)
876cecd0 17901 TYPE_NOSIGN (type) = 1;
0114d602 17902
2b4424c3
TT
17903 maybe_set_alignment (cu, die, type);
17904
103a685e 17905 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17906
f792889a 17907 return set_die_type (die, type, cu);
c906108c
SS
17908}
17909
80180f79
SA
17910/* Parse dwarf attribute if it's a block, reference or constant and put the
17911 resulting value of the attribute into struct bound_prop.
17912 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17913
17914static int
17915attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17916 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17917 struct type *default_type)
80180f79
SA
17918{
17919 struct dwarf2_property_baton *baton;
518817b3
SM
17920 struct obstack *obstack
17921 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17922
9a49df9d
AB
17923 gdb_assert (default_type != NULL);
17924
80180f79
SA
17925 if (attr == NULL || prop == NULL)
17926 return 0;
17927
17928 if (attr_form_is_block (attr))
17929 {
8d749320 17930 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17931 baton->property_type = default_type;
80180f79
SA
17932 baton->locexpr.per_cu = cu->per_cu;
17933 baton->locexpr.size = DW_BLOCK (attr)->size;
17934 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17935 switch (attr->name)
17936 {
17937 case DW_AT_string_length:
17938 baton->locexpr.is_reference = true;
17939 break;
17940 default:
17941 baton->locexpr.is_reference = false;
17942 break;
17943 }
80180f79
SA
17944 prop->data.baton = baton;
17945 prop->kind = PROP_LOCEXPR;
17946 gdb_assert (prop->data.baton != NULL);
17947 }
17948 else if (attr_form_is_ref (attr))
17949 {
17950 struct dwarf2_cu *target_cu = cu;
17951 struct die_info *target_die;
17952 struct attribute *target_attr;
17953
17954 target_die = follow_die_ref (die, attr, &target_cu);
17955 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17956 if (target_attr == NULL)
17957 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17958 target_cu);
80180f79
SA
17959 if (target_attr == NULL)
17960 return 0;
17961
df25ebbd 17962 switch (target_attr->name)
80180f79 17963 {
df25ebbd
JB
17964 case DW_AT_location:
17965 if (attr_form_is_section_offset (target_attr))
17966 {
8d749320 17967 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17968 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17969 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17970 prop->data.baton = baton;
17971 prop->kind = PROP_LOCLIST;
17972 gdb_assert (prop->data.baton != NULL);
17973 }
17974 else if (attr_form_is_block (target_attr))
17975 {
8d749320 17976 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17977 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17978 baton->locexpr.per_cu = cu->per_cu;
17979 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17980 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17981 baton->locexpr.is_reference = true;
df25ebbd
JB
17982 prop->data.baton = baton;
17983 prop->kind = PROP_LOCEXPR;
17984 gdb_assert (prop->data.baton != NULL);
17985 }
17986 else
17987 {
17988 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17989 "dynamic property");
17990 return 0;
17991 }
17992 break;
17993 case DW_AT_data_member_location:
17994 {
17995 LONGEST offset;
17996
17997 if (!handle_data_member_location (target_die, target_cu,
17998 &offset))
17999 return 0;
18000
8d749320 18001 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18002 baton->property_type = read_type_die (target_die->parent,
6ad395a7 18003 target_cu);
df25ebbd
JB
18004 baton->offset_info.offset = offset;
18005 baton->offset_info.type = die_type (target_die, target_cu);
18006 prop->data.baton = baton;
18007 prop->kind = PROP_ADDR_OFFSET;
18008 break;
18009 }
80180f79
SA
18010 }
18011 }
18012 else if (attr_form_is_constant (attr))
18013 {
18014 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
18015 prop->kind = PROP_CONST;
18016 }
18017 else
18018 {
18019 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18020 dwarf2_name (die, cu));
18021 return 0;
18022 }
18023
18024 return 1;
18025}
18026
11a8b164
AB
18027/* Find an integer type SIZE_IN_BYTES bytes in size and return it.
18028 UNSIGNED_P controls if the integer is unsigned or not. */
9a49df9d
AB
18029
18030static struct type *
11a8b164
AB
18031dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
18032 int size_in_bytes, bool unsigned_p)
9a49df9d
AB
18033{
18034 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9a49df9d
AB
18035 struct type *int_type;
18036
18037 /* Helper macro to examine the various builtin types. */
11a8b164
AB
18038#define TRY_TYPE(F) \
18039 int_type = (unsigned_p \
18040 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18041 : objfile_type (objfile)->builtin_ ## F); \
18042 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
18043 return int_type
18044
18045 TRY_TYPE (char);
18046 TRY_TYPE (short);
18047 TRY_TYPE (int);
18048 TRY_TYPE (long);
18049 TRY_TYPE (long_long);
18050
18051#undef TRY_TYPE
18052
18053 gdb_assert_not_reached ("unable to find suitable integer type");
18054}
18055
11a8b164
AB
18056/* Find an integer type the same size as the address size given in the
18057 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
18058 is unsigned or not. */
18059
18060static struct type *
18061dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
18062 bool unsigned_p)
18063{
18064 int addr_size = dwarf2_per_cu_addr_size (per_cu);
18065 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
18066}
18067
b86352cf
AB
18068/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18069 present (which is valid) then compute the default type based on the
18070 compilation units address size. */
18071
18072static struct type *
18073read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18074{
18075 struct type *index_type = die_type (die, cu);
18076
18077 /* Dwarf-2 specifications explicitly allows to create subrange types
18078 without specifying a base type.
18079 In that case, the base type must be set to the type of
18080 the lower bound, upper bound or count, in that order, if any of these
18081 three attributes references an object that has a type.
18082 If no base type is found, the Dwarf-2 specifications say that
18083 a signed integer type of size equal to the size of an address should
18084 be used.
18085 For the following C code: `extern char gdb_int [];'
18086 GCC produces an empty range DIE.
18087 FIXME: muller/2010-05-28: Possible references to object for low bound,
18088 high bound or count are not yet handled by this code. */
18089 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
9a49df9d 18090 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
b86352cf
AB
18091
18092 return index_type;
18093}
18094
a02abb62
JB
18095/* Read the given DW_AT_subrange DIE. */
18096
f792889a 18097static struct type *
a02abb62
JB
18098read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18099{
4c9ad8c2 18100 struct type *base_type, *orig_base_type;
a02abb62
JB
18101 struct type *range_type;
18102 struct attribute *attr;
729efb13 18103 struct dynamic_prop low, high;
4fae6e18 18104 int low_default_is_valid;
c451ebe5 18105 int high_bound_is_count = 0;
15d034d0 18106 const char *name;
d359392f 18107 ULONGEST negative_mask;
e77813c8 18108
b86352cf
AB
18109 orig_base_type = read_subrange_index_type (die, cu);
18110
4c9ad8c2
TT
18111 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18112 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18113 creating the range type, but we use the result of check_typedef
18114 when examining properties of the type. */
18115 base_type = check_typedef (orig_base_type);
a02abb62 18116
7e314c57
JK
18117 /* The die_type call above may have already set the type for this DIE. */
18118 range_type = get_die_type (die, cu);
18119 if (range_type)
18120 return range_type;
18121
729efb13
SA
18122 low.kind = PROP_CONST;
18123 high.kind = PROP_CONST;
18124 high.data.const_val = 0;
18125
4fae6e18
JK
18126 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18127 omitting DW_AT_lower_bound. */
18128 switch (cu->language)
6e70227d 18129 {
4fae6e18
JK
18130 case language_c:
18131 case language_cplus:
729efb13 18132 low.data.const_val = 0;
4fae6e18
JK
18133 low_default_is_valid = 1;
18134 break;
18135 case language_fortran:
729efb13 18136 low.data.const_val = 1;
4fae6e18
JK
18137 low_default_is_valid = 1;
18138 break;
18139 case language_d:
4fae6e18 18140 case language_objc:
c44af4eb 18141 case language_rust:
729efb13 18142 low.data.const_val = 0;
4fae6e18
JK
18143 low_default_is_valid = (cu->header.version >= 4);
18144 break;
18145 case language_ada:
18146 case language_m2:
18147 case language_pascal:
729efb13 18148 low.data.const_val = 1;
4fae6e18
JK
18149 low_default_is_valid = (cu->header.version >= 4);
18150 break;
18151 default:
729efb13 18152 low.data.const_val = 0;
4fae6e18
JK
18153 low_default_is_valid = 0;
18154 break;
a02abb62
JB
18155 }
18156
e142c38c 18157 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 18158 if (attr != nullptr)
9a49df9d 18159 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 18160 else if (!low_default_is_valid)
b98664d3 18161 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
18162 "- DIE at %s [in module %s]"),
18163 sect_offset_str (die->sect_off),
518817b3 18164 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 18165
506f5c41
TV
18166 struct attribute *attr_ub, *attr_count;
18167 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 18168 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 18169 {
506f5c41 18170 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 18171 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 18172 {
c451ebe5
SA
18173 /* If bounds are constant do the final calculation here. */
18174 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18175 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18176 else
18177 high_bound_is_count = 1;
c2ff108b 18178 }
506f5c41
TV
18179 else
18180 {
18181 if (attr_ub != NULL)
18182 complaint (_("Unresolved DW_AT_upper_bound "
18183 "- DIE at %s [in module %s]"),
18184 sect_offset_str (die->sect_off),
18185 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18186 if (attr_count != NULL)
18187 complaint (_("Unresolved DW_AT_count "
18188 "- DIE at %s [in module %s]"),
18189 sect_offset_str (die->sect_off),
18190 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18191 }
e77813c8 18192 }
a02abb62 18193
4e962e74
TT
18194 LONGEST bias = 0;
18195 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18196 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
18197 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
18198
dbb9c2b1
JB
18199 /* Normally, the DWARF producers are expected to use a signed
18200 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18201 But this is unfortunately not always the case, as witnessed
18202 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18203 is used instead. To work around that ambiguity, we treat
18204 the bounds as signed, and thus sign-extend their values, when
18205 the base type is signed. */
6e70227d 18206 negative_mask =
d359392f 18207 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
18208 if (low.kind == PROP_CONST
18209 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18210 low.data.const_val |= negative_mask;
18211 if (high.kind == PROP_CONST
18212 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18213 high.data.const_val |= negative_mask;
43bbcdc2 18214
5bbd8269
AB
18215 /* Check for bit and byte strides. */
18216 struct dynamic_prop byte_stride_prop;
18217 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18218 if (attr_byte_stride != nullptr)
18219 {
18220 struct type *prop_type
18221 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18222 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18223 prop_type);
18224 }
18225
18226 struct dynamic_prop bit_stride_prop;
18227 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18228 if (attr_bit_stride != nullptr)
18229 {
18230 /* It only makes sense to have either a bit or byte stride. */
18231 if (attr_byte_stride != nullptr)
18232 {
18233 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18234 "- DIE at %s [in module %s]"),
18235 sect_offset_str (die->sect_off),
18236 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18237 attr_bit_stride = nullptr;
18238 }
18239 else
18240 {
18241 struct type *prop_type
18242 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18243 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18244 prop_type);
18245 }
18246 }
18247
18248 if (attr_byte_stride != nullptr
18249 || attr_bit_stride != nullptr)
18250 {
18251 bool byte_stride_p = (attr_byte_stride != nullptr);
18252 struct dynamic_prop *stride
18253 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18254
18255 range_type
18256 = create_range_type_with_stride (NULL, orig_base_type, &low,
18257 &high, bias, stride, byte_stride_p);
18258 }
18259 else
18260 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 18261
c451ebe5
SA
18262 if (high_bound_is_count)
18263 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18264
c2ff108b
JK
18265 /* Ada expects an empty array on no boundary attributes. */
18266 if (attr == NULL && cu->language != language_ada)
729efb13 18267 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 18268
39cbfefa
DJ
18269 name = dwarf2_name (die, cu);
18270 if (name)
18271 TYPE_NAME (range_type) = name;
6e70227d 18272
e142c38c 18273 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18274 if (attr != nullptr)
a02abb62
JB
18275 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18276
2b4424c3
TT
18277 maybe_set_alignment (cu, die, range_type);
18278
7e314c57
JK
18279 set_die_type (die, range_type, cu);
18280
18281 /* set_die_type should be already done. */
b4ba55a1
JB
18282 set_descriptive_type (range_type, die, cu);
18283
7e314c57 18284 return range_type;
a02abb62 18285}
6e70227d 18286
f792889a 18287static struct type *
81a17f79
JB
18288read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18289{
18290 struct type *type;
81a17f79 18291
518817b3
SM
18292 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18293 NULL);
0114d602 18294 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 18295
74a2f8ff 18296 /* In Ada, an unspecified type is typically used when the description
85102364 18297 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
18298 such a type, we treat it as a stub, and try to resolve it later on,
18299 when needed. */
18300 if (cu->language == language_ada)
18301 TYPE_STUB (type) = 1;
18302
f792889a 18303 return set_die_type (die, type, cu);
81a17f79 18304}
a02abb62 18305
639d11d3
DC
18306/* Read a single die and all its descendents. Set the die's sibling
18307 field to NULL; set other fields in the die correctly, and set all
18308 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18309 location of the info_ptr after reading all of those dies. PARENT
18310 is the parent of the die in question. */
18311
18312static struct die_info *
dee91e82 18313read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18314 const gdb_byte *info_ptr,
18315 const gdb_byte **new_info_ptr,
dee91e82 18316 struct die_info *parent)
639d11d3
DC
18317{
18318 struct die_info *die;
d521ce57 18319 const gdb_byte *cur_ptr;
639d11d3
DC
18320 int has_children;
18321
bf6af496 18322 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
18323 if (die == NULL)
18324 {
18325 *new_info_ptr = cur_ptr;
18326 return NULL;
18327 }
93311388 18328 store_in_ref_table (die, reader->cu);
639d11d3
DC
18329
18330 if (has_children)
bf6af496 18331 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18332 else
18333 {
18334 die->child = NULL;
18335 *new_info_ptr = cur_ptr;
18336 }
18337
18338 die->sibling = NULL;
18339 die->parent = parent;
18340 return die;
18341}
18342
18343/* Read a die, all of its descendents, and all of its siblings; set
18344 all of the fields of all of the dies correctly. Arguments are as
18345 in read_die_and_children. */
18346
18347static struct die_info *
bf6af496 18348read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18349 const gdb_byte *info_ptr,
18350 const gdb_byte **new_info_ptr,
bf6af496 18351 struct die_info *parent)
639d11d3
DC
18352{
18353 struct die_info *first_die, *last_sibling;
d521ce57 18354 const gdb_byte *cur_ptr;
639d11d3 18355
c906108c 18356 cur_ptr = info_ptr;
639d11d3
DC
18357 first_die = last_sibling = NULL;
18358
18359 while (1)
c906108c 18360 {
639d11d3 18361 struct die_info *die
dee91e82 18362 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18363
1d325ec1 18364 if (die == NULL)
c906108c 18365 {
639d11d3
DC
18366 *new_info_ptr = cur_ptr;
18367 return first_die;
c906108c 18368 }
1d325ec1
DJ
18369
18370 if (!first_die)
18371 first_die = die;
c906108c 18372 else
1d325ec1
DJ
18373 last_sibling->sibling = die;
18374
18375 last_sibling = die;
c906108c 18376 }
c906108c
SS
18377}
18378
bf6af496
DE
18379/* Read a die, all of its descendents, and all of its siblings; set
18380 all of the fields of all of the dies correctly. Arguments are as
18381 in read_die_and_children.
18382 This the main entry point for reading a DIE and all its children. */
18383
18384static struct die_info *
18385read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18386 const gdb_byte *info_ptr,
18387 const gdb_byte **new_info_ptr,
bf6af496
DE
18388 struct die_info *parent)
18389{
18390 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18391 new_info_ptr, parent);
18392
b4f54984 18393 if (dwarf_die_debug)
bf6af496
DE
18394 {
18395 fprintf_unfiltered (gdb_stdlog,
18396 "Read die from %s@0x%x of %s:\n",
a32a8923 18397 get_section_name (reader->die_section),
bf6af496
DE
18398 (unsigned) (info_ptr - reader->die_section->buffer),
18399 bfd_get_filename (reader->abfd));
b4f54984 18400 dump_die (die, dwarf_die_debug);
bf6af496
DE
18401 }
18402
18403 return die;
18404}
18405
3019eac3
DE
18406/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18407 attributes.
18408 The caller is responsible for filling in the extra attributes
18409 and updating (*DIEP)->num_attrs.
18410 Set DIEP to point to a newly allocated die with its information,
18411 except for its child, sibling, and parent fields.
18412 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 18413
d521ce57 18414static const gdb_byte *
3019eac3 18415read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18416 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 18417 int *has_children, int num_extra_attrs)
93311388 18418{
b64f50a1 18419 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18420 struct abbrev_info *abbrev;
18421 struct die_info *die;
18422 struct dwarf2_cu *cu = reader->cu;
18423 bfd *abfd = reader->abfd;
18424
9c541725 18425 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18426 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18427 info_ptr += bytes_read;
18428 if (!abbrev_number)
18429 {
18430 *diep = NULL;
18431 *has_children = 0;
18432 return info_ptr;
18433 }
18434
685af9cd 18435 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18436 if (!abbrev)
348e048f
DE
18437 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18438 abbrev_number,
18439 bfd_get_filename (abfd));
18440
3019eac3 18441 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18442 die->sect_off = sect_off;
93311388
DE
18443 die->tag = abbrev->tag;
18444 die->abbrev = abbrev_number;
18445
3019eac3
DE
18446 /* Make the result usable.
18447 The caller needs to update num_attrs after adding the extra
18448 attributes. */
93311388
DE
18449 die->num_attrs = abbrev->num_attrs;
18450
18451 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
18452 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18453 info_ptr);
93311388
DE
18454
18455 *diep = die;
18456 *has_children = abbrev->has_children;
18457 return info_ptr;
18458}
18459
3019eac3
DE
18460/* Read a die and all its attributes.
18461 Set DIEP to point to a newly allocated die with its information,
18462 except for its child, sibling, and parent fields.
18463 Set HAS_CHILDREN to tell whether the die has children or not. */
18464
d521ce57 18465static const gdb_byte *
3019eac3 18466read_full_die (const struct die_reader_specs *reader,
d521ce57 18467 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
18468 int *has_children)
18469{
d521ce57 18470 const gdb_byte *result;
bf6af496
DE
18471
18472 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18473
b4f54984 18474 if (dwarf_die_debug)
bf6af496
DE
18475 {
18476 fprintf_unfiltered (gdb_stdlog,
18477 "Read die from %s@0x%x of %s:\n",
a32a8923 18478 get_section_name (reader->die_section),
bf6af496
DE
18479 (unsigned) (info_ptr - reader->die_section->buffer),
18480 bfd_get_filename (reader->abfd));
b4f54984 18481 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18482 }
18483
18484 return result;
3019eac3 18485}
433df2d4
DE
18486\f
18487/* Abbreviation tables.
3019eac3 18488
433df2d4 18489 In DWARF version 2, the description of the debugging information is
c906108c
SS
18490 stored in a separate .debug_abbrev section. Before we read any
18491 dies from a section we read in all abbreviations and install them
433df2d4
DE
18492 in a hash table. */
18493
18494/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18495
685af9cd
TT
18496struct abbrev_info *
18497abbrev_table::alloc_abbrev ()
433df2d4
DE
18498{
18499 struct abbrev_info *abbrev;
18500
685af9cd 18501 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 18502 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 18503
433df2d4
DE
18504 return abbrev;
18505}
18506
18507/* Add an abbreviation to the table. */
c906108c 18508
685af9cd
TT
18509void
18510abbrev_table::add_abbrev (unsigned int abbrev_number,
18511 struct abbrev_info *abbrev)
433df2d4
DE
18512{
18513 unsigned int hash_number;
18514
18515 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
18516 abbrev->next = m_abbrevs[hash_number];
18517 m_abbrevs[hash_number] = abbrev;
433df2d4 18518}
dee91e82 18519
433df2d4
DE
18520/* Look up an abbrev in the table.
18521 Returns NULL if the abbrev is not found. */
18522
685af9cd
TT
18523struct abbrev_info *
18524abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 18525{
433df2d4
DE
18526 unsigned int hash_number;
18527 struct abbrev_info *abbrev;
18528
18529 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 18530 abbrev = m_abbrevs[hash_number];
433df2d4
DE
18531
18532 while (abbrev)
18533 {
18534 if (abbrev->number == abbrev_number)
18535 return abbrev;
18536 abbrev = abbrev->next;
18537 }
18538 return NULL;
18539}
18540
18541/* Read in an abbrev table. */
18542
685af9cd 18543static abbrev_table_up
ed2dc618
SM
18544abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18545 struct dwarf2_section_info *section,
9c541725 18546 sect_offset sect_off)
433df2d4
DE
18547{
18548 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 18549 bfd *abfd = get_section_bfd_owner (section);
d521ce57 18550 const gdb_byte *abbrev_ptr;
c906108c
SS
18551 struct abbrev_info *cur_abbrev;
18552 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 18553 unsigned int abbrev_form;
6dfa2fc2 18554 std::vector<struct attr_abbrev> cur_attrs;
c906108c 18555
685af9cd 18556 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 18557
433df2d4 18558 dwarf2_read_section (objfile, section);
9c541725 18559 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
18560 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18561 abbrev_ptr += bytes_read;
18562
0963b4bd 18563 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
18564 while (abbrev_number)
18565 {
6dfa2fc2 18566 cur_attrs.clear ();
685af9cd 18567 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
18568
18569 /* read in abbrev header */
18570 cur_abbrev->number = abbrev_number;
aead7601
SM
18571 cur_abbrev->tag
18572 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
18573 abbrev_ptr += bytes_read;
18574 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18575 abbrev_ptr += 1;
18576
18577 /* now read in declarations */
22d2f3ab 18578 for (;;)
c906108c 18579 {
43988095
JK
18580 LONGEST implicit_const;
18581
22d2f3ab
JK
18582 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18583 abbrev_ptr += bytes_read;
18584 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18585 abbrev_ptr += bytes_read;
43988095
JK
18586 if (abbrev_form == DW_FORM_implicit_const)
18587 {
18588 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18589 &bytes_read);
18590 abbrev_ptr += bytes_read;
18591 }
18592 else
18593 {
18594 /* Initialize it due to a false compiler warning. */
18595 implicit_const = -1;
18596 }
22d2f3ab
JK
18597
18598 if (abbrev_name == 0)
18599 break;
18600
6dfa2fc2
TT
18601 cur_attrs.emplace_back ();
18602 struct attr_abbrev &cur_attr = cur_attrs.back ();
18603 cur_attr.name = (enum dwarf_attribute) abbrev_name;
18604 cur_attr.form = (enum dwarf_form) abbrev_form;
18605 cur_attr.implicit_const = implicit_const;
22d2f3ab 18606 ++cur_abbrev->num_attrs;
c906108c
SS
18607 }
18608
8d749320
SM
18609 cur_abbrev->attrs =
18610 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18611 cur_abbrev->num_attrs);
6dfa2fc2 18612 memcpy (cur_abbrev->attrs, cur_attrs.data (),
f3dd6933
DJ
18613 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18614
685af9cd 18615 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18616
18617 /* Get next abbreviation.
18618 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18619 always properly terminated with an abbrev number of 0.
18620 Exit loop if we encounter an abbreviation which we have
18621 already read (which means we are about to read the abbreviations
18622 for the next compile unit) or if the end of the abbreviation
18623 table is reached. */
433df2d4 18624 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18625 break;
18626 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18627 abbrev_ptr += bytes_read;
685af9cd 18628 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18629 break;
18630 }
f3dd6933 18631
433df2d4 18632 return abbrev_table;
c906108c
SS
18633}
18634
72bf9492
DJ
18635/* Returns nonzero if TAG represents a type that we might generate a partial
18636 symbol for. */
18637
18638static int
18639is_type_tag_for_partial (int tag)
18640{
18641 switch (tag)
18642 {
18643#if 0
18644 /* Some types that would be reasonable to generate partial symbols for,
18645 that we don't at present. */
18646 case DW_TAG_array_type:
18647 case DW_TAG_file_type:
18648 case DW_TAG_ptr_to_member_type:
18649 case DW_TAG_set_type:
18650 case DW_TAG_string_type:
18651 case DW_TAG_subroutine_type:
18652#endif
18653 case DW_TAG_base_type:
18654 case DW_TAG_class_type:
680b30c7 18655 case DW_TAG_interface_type:
72bf9492
DJ
18656 case DW_TAG_enumeration_type:
18657 case DW_TAG_structure_type:
18658 case DW_TAG_subrange_type:
18659 case DW_TAG_typedef:
18660 case DW_TAG_union_type:
18661 return 1;
18662 default:
18663 return 0;
18664 }
18665}
18666
18667/* Load all DIEs that are interesting for partial symbols into memory. */
18668
18669static struct partial_die_info *
dee91e82 18670load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18671 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18672{
dee91e82 18673 struct dwarf2_cu *cu = reader->cu;
518817b3 18674 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18675 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18676 unsigned int bytes_read;
5afb4e99 18677 unsigned int load_all = 0;
72bf9492
DJ
18678 int nesting_level = 1;
18679
18680 parent_die = NULL;
18681 last_die = NULL;
18682
7adf1e79
DE
18683 gdb_assert (cu->per_cu != NULL);
18684 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18685 load_all = 1;
18686
72bf9492
DJ
18687 cu->partial_dies
18688 = htab_create_alloc_ex (cu->header.length / 12,
18689 partial_die_hash,
18690 partial_die_eq,
18691 NULL,
18692 &cu->comp_unit_obstack,
18693 hashtab_obstack_allocate,
18694 dummy_obstack_deallocate);
18695
72bf9492
DJ
18696 while (1)
18697 {
685af9cd 18698 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18699
18700 /* A NULL abbrev means the end of a series of children. */
18701 if (abbrev == NULL)
18702 {
18703 if (--nesting_level == 0)
cd9983dd
YQ
18704 return first_die;
18705
72bf9492
DJ
18706 info_ptr += bytes_read;
18707 last_die = parent_die;
18708 parent_die = parent_die->die_parent;
18709 continue;
18710 }
18711
98bfdba5
PA
18712 /* Check for template arguments. We never save these; if
18713 they're seen, we just mark the parent, and go on our way. */
18714 if (parent_die != NULL
18715 && cu->language == language_cplus
18716 && (abbrev->tag == DW_TAG_template_type_param
18717 || abbrev->tag == DW_TAG_template_value_param))
18718 {
18719 parent_die->has_template_arguments = 1;
18720
18721 if (!load_all)
18722 {
18723 /* We don't need a partial DIE for the template argument. */
dee91e82 18724 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18725 continue;
18726 }
18727 }
18728
0d99eb77 18729 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18730 Skip their other children. */
18731 if (!load_all
18732 && cu->language == language_cplus
18733 && parent_die != NULL
18734 && parent_die->tag == DW_TAG_subprogram)
18735 {
dee91e82 18736 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18737 continue;
18738 }
18739
5afb4e99
DJ
18740 /* Check whether this DIE is interesting enough to save. Normally
18741 we would not be interested in members here, but there may be
18742 later variables referencing them via DW_AT_specification (for
18743 static members). */
18744 if (!load_all
18745 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18746 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18747 && abbrev->tag != DW_TAG_enumerator
18748 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18749 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18750 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18751 && abbrev->tag != DW_TAG_variable
5afb4e99 18752 && abbrev->tag != DW_TAG_namespace
f55ee35c 18753 && abbrev->tag != DW_TAG_module
95554aad 18754 && abbrev->tag != DW_TAG_member
74921315
KS
18755 && abbrev->tag != DW_TAG_imported_unit
18756 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18757 {
18758 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18759 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18760 continue;
18761 }
18762
6f06d47b
YQ
18763 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18764 abbrev);
cd9983dd 18765
48fbe735 18766 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18767
18768 /* This two-pass algorithm for processing partial symbols has a
18769 high cost in cache pressure. Thus, handle some simple cases
18770 here which cover the majority of C partial symbols. DIEs
18771 which neither have specification tags in them, nor could have
18772 specification tags elsewhere pointing at them, can simply be
18773 processed and discarded.
18774
18775 This segment is also optional; scan_partial_symbols and
18776 add_partial_symbol will handle these DIEs if we chain
18777 them in normally. When compilers which do not emit large
18778 quantities of duplicate debug information are more common,
18779 this code can probably be removed. */
18780
18781 /* Any complete simple types at the top level (pretty much all
18782 of them, for a language without namespaces), can be processed
18783 directly. */
18784 if (parent_die == NULL
cd9983dd
YQ
18785 && pdi.has_specification == 0
18786 && pdi.is_declaration == 0
18787 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18788 || pdi.tag == DW_TAG_base_type
18789 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18790 {
cd9983dd 18791 if (building_psymtab && pdi.name != NULL)
31edb802 18792 add_psymbol_to_list (pdi.name, false,
79748972 18793 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18794 psymbol_placement::STATIC,
1762568f 18795 0, cu->language, objfile);
cd9983dd 18796 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18797 continue;
18798 }
18799
d8228535
JK
18800 /* The exception for DW_TAG_typedef with has_children above is
18801 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18802 type_name_or_error will error on such types later.
d8228535
JK
18803
18804 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18805 it could not find the child DIEs referenced later, this is checked
18806 above. In correct DWARF DW_TAG_typedef should have no children. */
18807
cd9983dd 18808 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18809 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18810 "- DIE at %s [in module %s]"),
cd9983dd 18811 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18812
72bf9492
DJ
18813 /* If we're at the second level, and we're an enumerator, and
18814 our parent has no specification (meaning possibly lives in a
18815 namespace elsewhere), then we can add the partial symbol now
18816 instead of queueing it. */
cd9983dd 18817 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18818 && parent_die != NULL
18819 && parent_die->die_parent == NULL
18820 && parent_die->tag == DW_TAG_enumeration_type
18821 && parent_die->has_specification == 0)
18822 {
cd9983dd 18823 if (pdi.name == NULL)
b98664d3 18824 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18825 else if (building_psymtab)
31edb802 18826 add_psymbol_to_list (pdi.name, false,
79748972 18827 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18828 cu->language == language_cplus
75aedd27
TT
18829 ? psymbol_placement::GLOBAL
18830 : psymbol_placement::STATIC,
1762568f 18831 0, cu->language, objfile);
72bf9492 18832
cd9983dd 18833 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18834 continue;
18835 }
18836
cd9983dd 18837 struct partial_die_info *part_die
6f06d47b 18838 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18839
72bf9492
DJ
18840 /* We'll save this DIE so link it in. */
18841 part_die->die_parent = parent_die;
18842 part_die->die_sibling = NULL;
18843 part_die->die_child = NULL;
18844
18845 if (last_die && last_die == parent_die)
18846 last_die->die_child = part_die;
18847 else if (last_die)
18848 last_die->die_sibling = part_die;
18849
18850 last_die = part_die;
18851
18852 if (first_die == NULL)
18853 first_die = part_die;
18854
18855 /* Maybe add the DIE to the hash table. Not all DIEs that we
18856 find interesting need to be in the hash table, because we
18857 also have the parent/sibling/child chains; only those that we
18858 might refer to by offset later during partial symbol reading.
18859
18860 For now this means things that might have be the target of a
18861 DW_AT_specification, DW_AT_abstract_origin, or
18862 DW_AT_extension. DW_AT_extension will refer only to
18863 namespaces; DW_AT_abstract_origin refers to functions (and
18864 many things under the function DIE, but we do not recurse
18865 into function DIEs during partial symbol reading) and
18866 possibly variables as well; DW_AT_specification refers to
18867 declarations. Declarations ought to have the DW_AT_declaration
18868 flag. It happens that GCC forgets to put it in sometimes, but
18869 only for functions, not for types.
18870
18871 Adding more things than necessary to the hash table is harmless
18872 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18873 wasted time in find_partial_die, when we reread the compilation
18874 unit with load_all_dies set. */
72bf9492 18875
5afb4e99 18876 if (load_all
72929c62 18877 || abbrev->tag == DW_TAG_constant
5afb4e99 18878 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18879 || abbrev->tag == DW_TAG_variable
18880 || abbrev->tag == DW_TAG_namespace
18881 || part_die->is_declaration)
18882 {
18883 void **slot;
18884
18885 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18886 to_underlying (part_die->sect_off),
18887 INSERT);
72bf9492
DJ
18888 *slot = part_die;
18889 }
18890
72bf9492 18891 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18892 we have no reason to follow the children of structures; for other
98bfdba5
PA
18893 languages we have to, so that we can get at method physnames
18894 to infer fully qualified class names, for DW_AT_specification,
18895 and for C++ template arguments. For C++, we also look one level
18896 inside functions to find template arguments (if the name of the
18897 function does not already contain the template arguments).
bc30ff58 18898
0a4b0913
AB
18899 For Ada and Fortran, we need to scan the children of subprograms
18900 and lexical blocks as well because these languages allow the
18901 definition of nested entities that could be interesting for the
18902 debugger, such as nested subprograms for instance. */
72bf9492 18903 if (last_die->has_children
5afb4e99
DJ
18904 && (load_all
18905 || last_die->tag == DW_TAG_namespace
f55ee35c 18906 || last_die->tag == DW_TAG_module
72bf9492 18907 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18908 || (cu->language == language_cplus
18909 && last_die->tag == DW_TAG_subprogram
18910 && (last_die->name == NULL
18911 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18912 || (cu->language != language_c
18913 && (last_die->tag == DW_TAG_class_type
680b30c7 18914 || last_die->tag == DW_TAG_interface_type
72bf9492 18915 || last_die->tag == DW_TAG_structure_type
bc30ff58 18916 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18917 || ((cu->language == language_ada
18918 || cu->language == language_fortran)
bc30ff58
JB
18919 && (last_die->tag == DW_TAG_subprogram
18920 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18921 {
18922 nesting_level++;
18923 parent_die = last_die;
18924 continue;
18925 }
18926
18927 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18928 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18929
18930 /* Back to the top, do it again. */
18931 }
18932}
18933
6f06d47b
YQ
18934partial_die_info::partial_die_info (sect_offset sect_off_,
18935 struct abbrev_info *abbrev)
18936 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18937{
18938}
18939
35cc7ed7
YQ
18940/* Read a minimal amount of information into the minimal die structure.
18941 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18942
48fbe735
YQ
18943const gdb_byte *
18944partial_die_info::read (const struct die_reader_specs *reader,
18945 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18946{
dee91e82 18947 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18948 struct dwarf2_per_objfile *dwarf2_per_objfile
18949 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18950 unsigned int i;
c5aa993b 18951 int has_low_pc_attr = 0;
c906108c 18952 int has_high_pc_attr = 0;
91da1414 18953 int high_pc_relative = 0;
c906108c 18954
fd0a254f 18955 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18956 {
48fbe735
YQ
18957 struct attribute attr;
18958
fd0a254f 18959 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
c906108c
SS
18960
18961 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18962 partial symbol table. */
c906108c
SS
18963 switch (attr.name)
18964 {
18965 case DW_AT_name:
48fbe735 18966 switch (tag)
71c25dea
TT
18967 {
18968 case DW_TAG_compile_unit:
95554aad 18969 case DW_TAG_partial_unit:
348e048f 18970 case DW_TAG_type_unit:
71c25dea
TT
18971 /* Compilation units have a DW_AT_name that is a filename, not
18972 a source language identifier. */
18973 case DW_TAG_enumeration_type:
18974 case DW_TAG_enumerator:
18975 /* These tags always have simple identifiers already; no need
18976 to canonicalize them. */
48fbe735 18977 name = DW_STRING (&attr);
71c25dea
TT
18978 break;
18979 default:
48fbe735
YQ
18980 {
18981 struct objfile *objfile = dwarf2_per_objfile->objfile;
18982
18983 name
18984 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18985 &objfile->per_bfd->storage_obstack);
18986 }
71c25dea
TT
18987 break;
18988 }
c906108c 18989 break;
31ef98ae 18990 case DW_AT_linkage_name:
c906108c 18991 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18992 /* Note that both forms of linkage name might appear. We
18993 assume they will be the same, and we only store the last
18994 one we see. */
48fbe735 18995 linkage_name = DW_STRING (&attr);
c906108c
SS
18996 break;
18997 case DW_AT_low_pc:
18998 has_low_pc_attr = 1;
48fbe735 18999 lowpc = attr_value_as_address (&attr);
c906108c
SS
19000 break;
19001 case DW_AT_high_pc:
19002 has_high_pc_attr = 1;
48fbe735 19003 highpc = attr_value_as_address (&attr);
31aa7e4e
JB
19004 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
19005 high_pc_relative = 1;
c906108c
SS
19006 break;
19007 case DW_AT_location:
0963b4bd 19008 /* Support the .debug_loc offsets. */
8e19ed76
PS
19009 if (attr_form_is_block (&attr))
19010 {
48fbe735 19011 d.locdesc = DW_BLOCK (&attr);
8e19ed76 19012 }
3690dd37 19013 else if (attr_form_is_section_offset (&attr))
8e19ed76 19014 {
4d3c2250 19015 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
19016 }
19017 else
19018 {
4d3c2250
KB
19019 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19020 "partial symbol information");
8e19ed76 19021 }
c906108c 19022 break;
c906108c 19023 case DW_AT_external:
48fbe735 19024 is_external = DW_UNSND (&attr);
c906108c
SS
19025 break;
19026 case DW_AT_declaration:
48fbe735 19027 is_declaration = DW_UNSND (&attr);
c906108c
SS
19028 break;
19029 case DW_AT_type:
48fbe735 19030 has_type = 1;
c906108c
SS
19031 break;
19032 case DW_AT_abstract_origin:
19033 case DW_AT_specification:
72bf9492 19034 case DW_AT_extension:
48fbe735
YQ
19035 has_specification = 1;
19036 spec_offset = dwarf2_get_ref_die_offset (&attr);
19037 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 19038 || cu->per_cu->is_dwz);
c906108c
SS
19039 break;
19040 case DW_AT_sibling:
19041 /* Ignore absolute siblings, they might point outside of
19042 the current compile unit. */
19043 if (attr.form == DW_FORM_ref_addr)
b98664d3 19044 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19045 else
b9502d3f 19046 {
48fbe735 19047 const gdb_byte *buffer = reader->buffer;
9c541725
PA
19048 sect_offset off = dwarf2_get_ref_die_offset (&attr);
19049 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19050
19051 if (sibling_ptr < info_ptr)
b98664d3 19052 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
19053 else if (sibling_ptr > reader->buffer_end)
19054 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 19055 else
48fbe735 19056 sibling = sibling_ptr;
b9502d3f 19057 }
c906108c 19058 break;
fa4028e9 19059 case DW_AT_byte_size:
48fbe735 19060 has_byte_size = 1;
fa4028e9 19061 break;
ff908ebf 19062 case DW_AT_const_value:
48fbe735 19063 has_const_value = 1;
ff908ebf 19064 break;
68511cec
CES
19065 case DW_AT_calling_convention:
19066 /* DWARF doesn't provide a way to identify a program's source-level
19067 entry point. DW_AT_calling_convention attributes are only meant
19068 to describe functions' calling conventions.
19069
19070 However, because it's a necessary piece of information in
0c1b455e
TT
19071 Fortran, and before DWARF 4 DW_CC_program was the only
19072 piece of debugging information whose definition refers to
19073 a 'main program' at all, several compilers marked Fortran
19074 main programs with DW_CC_program --- even when those
19075 functions use the standard calling conventions.
19076
19077 Although DWARF now specifies a way to provide this
19078 information, we support this practice for backward
19079 compatibility. */
68511cec 19080 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 19081 && cu->language == language_fortran)
48fbe735 19082 main_subprogram = 1;
68511cec 19083 break;
481860b3
GB
19084 case DW_AT_inline:
19085 if (DW_UNSND (&attr) == DW_INL_inlined
19086 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 19087 may_be_inlined = 1;
481860b3 19088 break;
95554aad
TT
19089
19090 case DW_AT_import:
48fbe735 19091 if (tag == DW_TAG_imported_unit)
36586728 19092 {
48fbe735
YQ
19093 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19094 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19095 || cu->per_cu->is_dwz);
19096 }
95554aad
TT
19097 break;
19098
0c1b455e 19099 case DW_AT_main_subprogram:
48fbe735 19100 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
19101 break;
19102
05caa1d2
TT
19103 case DW_AT_ranges:
19104 {
19105 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19106 but that requires a full DIE, so instead we just
19107 reimplement it. */
19108 int need_ranges_base = tag != DW_TAG_compile_unit;
19109 unsigned int ranges_offset = (DW_UNSND (&attr)
19110 + (need_ranges_base
19111 ? cu->ranges_base
19112 : 0));
19113
19114 /* Value of the DW_AT_ranges attribute is the offset in the
19115 .debug_ranges section. */
19116 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19117 nullptr))
19118 has_pc_info = 1;
19119 }
19120 break;
19121
c906108c
SS
19122 default:
19123 break;
19124 }
19125 }
19126
10d06d82
TT
19127 /* For Ada, if both the name and the linkage name appear, we prefer
19128 the latter. This lets "catch exception" work better, regardless
19129 of the order in which the name and linkage name were emitted.
19130 Really, though, this is just a workaround for the fact that gdb
19131 doesn't store both the name and the linkage name. */
19132 if (cu->language == language_ada && linkage_name != nullptr)
19133 name = linkage_name;
19134
91da1414 19135 if (high_pc_relative)
48fbe735 19136 highpc += lowpc;
91da1414 19137
9373cf26
JK
19138 if (has_low_pc_attr && has_high_pc_attr)
19139 {
19140 /* When using the GNU linker, .gnu.linkonce. sections are used to
19141 eliminate duplicate copies of functions and vtables and such.
19142 The linker will arbitrarily choose one and discard the others.
19143 The AT_*_pc values for such functions refer to local labels in
19144 these sections. If the section from that file was discarded, the
19145 labels are not in the output, so the relocs get a value of 0.
19146 If this is a discarded function, mark the pc bounds as invalid,
19147 so that GDB will ignore it. */
48fbe735 19148 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 19149 {
48fbe735 19150 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 19151 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 19152
b98664d3 19153 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19154 "for DIE at %s [in module %s]"),
48fbe735
YQ
19155 paddress (gdbarch, lowpc),
19156 sect_offset_str (sect_off),
9d8780f0 19157 objfile_name (objfile));
9373cf26
JK
19158 }
19159 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19160 else if (lowpc >= highpc)
9373cf26 19161 {
48fbe735 19162 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 19163 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 19164
b98664d3 19165 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19166 "for DIE at %s [in module %s]"),
48fbe735
YQ
19167 paddress (gdbarch, lowpc),
19168 paddress (gdbarch, highpc),
19169 sect_offset_str (sect_off),
9c541725 19170 objfile_name (objfile));
9373cf26
JK
19171 }
19172 else
48fbe735 19173 has_pc_info = 1;
9373cf26 19174 }
85cbf3d3 19175
c906108c
SS
19176 return info_ptr;
19177}
19178
72bf9492
DJ
19179/* Find a cached partial DIE at OFFSET in CU. */
19180
d590ff25
YQ
19181struct partial_die_info *
19182dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19183{
19184 struct partial_die_info *lookup_die = NULL;
6f06d47b 19185 struct partial_die_info part_die (sect_off);
72bf9492 19186
9a3c8263 19187 lookup_die = ((struct partial_die_info *)
d590ff25 19188 htab_find_with_hash (partial_dies, &part_die,
9c541725 19189 to_underlying (sect_off)));
72bf9492 19190
72bf9492
DJ
19191 return lookup_die;
19192}
19193
348e048f
DE
19194/* Find a partial DIE at OFFSET, which may or may not be in CU,
19195 except in the case of .debug_types DIEs which do not reference
19196 outside their CU (they do however referencing other types via
55f1336d 19197 DW_FORM_ref_sig8). */
72bf9492 19198
122cf0f2 19199static const struct cu_partial_die_info
9c541725 19200find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19201{
518817b3
SM
19202 struct dwarf2_per_objfile *dwarf2_per_objfile
19203 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19204 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
19205 struct dwarf2_per_cu_data *per_cu = NULL;
19206 struct partial_die_info *pd = NULL;
72bf9492 19207
36586728 19208 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 19209 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 19210 {
d590ff25 19211 pd = cu->find_partial_die (sect_off);
5afb4e99 19212 if (pd != NULL)
fb816e8b 19213 return { cu, pd };
0d99eb77
DE
19214 /* We missed recording what we needed.
19215 Load all dies and try again. */
19216 per_cu = cu->per_cu;
5afb4e99 19217 }
0d99eb77
DE
19218 else
19219 {
19220 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19221 if (cu->per_cu->is_debug_types)
0d99eb77 19222 {
9d8780f0
SM
19223 error (_("Dwarf Error: Type Unit at offset %s contains"
19224 " external reference to offset %s [in module %s].\n"),
19225 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19226 bfd_get_filename (objfile->obfd));
19227 }
9c541725 19228 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 19229 dwarf2_per_objfile);
72bf9492 19230
0d99eb77
DE
19231 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19232 load_partial_comp_unit (per_cu);
ae038cb0 19233
0d99eb77 19234 per_cu->cu->last_used = 0;
d590ff25 19235 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 19236 }
5afb4e99 19237
dee91e82
DE
19238 /* If we didn't find it, and not all dies have been loaded,
19239 load them all and try again. */
19240
5afb4e99
DJ
19241 if (pd == NULL && per_cu->load_all_dies == 0)
19242 {
5afb4e99 19243 per_cu->load_all_dies = 1;
fd820528
DE
19244
19245 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19246 THIS_CU->cu may already be in use. So we can't just free it and
19247 replace its DIEs with the ones we read in. Instead, we leave those
19248 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19249 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19250 set. */
dee91e82 19251 load_partial_comp_unit (per_cu);
5afb4e99 19252
d590ff25 19253 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
19254 }
19255
19256 if (pd == NULL)
19257 internal_error (__FILE__, __LINE__,
9d8780f0 19258 _("could not find partial DIE %s "
3e43a32a 19259 "in cache [from module %s]\n"),
9d8780f0 19260 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 19261 return { per_cu->cu, pd };
72bf9492
DJ
19262}
19263
abc72ce4
DE
19264/* See if we can figure out if the class lives in a namespace. We do
19265 this by looking for a member function; its demangled name will
19266 contain namespace info, if there is any. */
19267
19268static void
19269guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19270 struct dwarf2_cu *cu)
19271{
19272 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19273 what template types look like, because the demangler
19274 frequently doesn't give the same name as the debug info. We
19275 could fix this by only using the demangled name to get the
19276 prefix (but see comment in read_structure_type). */
19277
19278 struct partial_die_info *real_pdi;
19279 struct partial_die_info *child_pdi;
19280
19281 /* If this DIE (this DIE's specification, if any) has a parent, then
19282 we should not do this. We'll prepend the parent's fully qualified
19283 name when we create the partial symbol. */
19284
19285 real_pdi = struct_pdi;
19286 while (real_pdi->has_specification)
fb816e8b 19287 {
122cf0f2
AB
19288 auto res = find_partial_die (real_pdi->spec_offset,
19289 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19290 real_pdi = res.pdi;
19291 cu = res.cu;
19292 }
abc72ce4
DE
19293
19294 if (real_pdi->die_parent != NULL)
19295 return;
19296
19297 for (child_pdi = struct_pdi->die_child;
19298 child_pdi != NULL;
19299 child_pdi = child_pdi->die_sibling)
19300 {
19301 if (child_pdi->tag == DW_TAG_subprogram
19302 && child_pdi->linkage_name != NULL)
19303 {
43816ebc
TT
19304 gdb::unique_xmalloc_ptr<char> actual_class_name
19305 (language_class_name_from_physname (cu->language_defn,
19306 child_pdi->linkage_name));
abc72ce4
DE
19307 if (actual_class_name != NULL)
19308 {
518817b3 19309 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 19310 struct_pdi->name
021887d8 19311 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 19312 actual_class_name.get ());
abc72ce4
DE
19313 }
19314 break;
19315 }
19316 }
19317}
19318
52356b79
YQ
19319void
19320partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19321{
abc72ce4
DE
19322 /* Once we've fixed up a die, there's no point in doing so again.
19323 This also avoids a memory leak if we were to call
19324 guess_partial_die_structure_name multiple times. */
52356b79 19325 if (fixup_called)
abc72ce4
DE
19326 return;
19327
72bf9492
DJ
19328 /* If we found a reference attribute and the DIE has no name, try
19329 to find a name in the referred to DIE. */
19330
52356b79 19331 if (name == NULL && has_specification)
72bf9492
DJ
19332 {
19333 struct partial_die_info *spec_die;
72bf9492 19334
122cf0f2 19335 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19336 spec_die = res.pdi;
19337 cu = res.cu;
72bf9492 19338
52356b79 19339 spec_die->fixup (cu);
72bf9492
DJ
19340
19341 if (spec_die->name)
19342 {
52356b79 19343 name = spec_die->name;
72bf9492
DJ
19344
19345 /* Copy DW_AT_external attribute if it is set. */
19346 if (spec_die->is_external)
52356b79 19347 is_external = spec_die->is_external;
72bf9492
DJ
19348 }
19349 }
19350
19351 /* Set default names for some unnamed DIEs. */
72bf9492 19352
52356b79
YQ
19353 if (name == NULL && tag == DW_TAG_namespace)
19354 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 19355
abc72ce4
DE
19356 /* If there is no parent die to provide a namespace, and there are
19357 children, see if we can determine the namespace from their linkage
122d1940 19358 name. */
abc72ce4 19359 if (cu->language == language_cplus
fd5866f6 19360 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
19361 && die_parent == NULL
19362 && has_children
19363 && (tag == DW_TAG_class_type
19364 || tag == DW_TAG_structure_type
19365 || tag == DW_TAG_union_type))
19366 guess_partial_die_structure_name (this, cu);
abc72ce4 19367
53832f31
TT
19368 /* GCC might emit a nameless struct or union that has a linkage
19369 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
19370 if (name == NULL
19371 && (tag == DW_TAG_class_type
19372 || tag == DW_TAG_interface_type
19373 || tag == DW_TAG_structure_type
19374 || tag == DW_TAG_union_type)
19375 && linkage_name != NULL)
53832f31 19376 {
43816ebc
TT
19377 gdb::unique_xmalloc_ptr<char> demangled
19378 (gdb_demangle (linkage_name, DMGL_TYPES));
19379 if (demangled != nullptr)
53832f31 19380 {
96408a79
SA
19381 const char *base;
19382
19383 /* Strip any leading namespaces/classes, keep only the base name.
19384 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
19385 base = strrchr (demangled.get (), ':');
19386 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
19387 base++;
19388 else
43816ebc 19389 base = demangled.get ();
96408a79 19390
518817b3 19391 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
021887d8 19392 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
53832f31
TT
19393 }
19394 }
19395
52356b79 19396 fixup_called = 1;
72bf9492
DJ
19397}
19398
a8329558 19399/* Read an attribute value described by an attribute form. */
c906108c 19400
d521ce57 19401static const gdb_byte *
dee91e82
DE
19402read_attribute_value (const struct die_reader_specs *reader,
19403 struct attribute *attr, unsigned form,
43988095 19404 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 19405{
dee91e82 19406 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19407 struct dwarf2_per_objfile *dwarf2_per_objfile
19408 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19409 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 19410 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 19411 bfd *abfd = reader->abfd;
e7c27a73 19412 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19413 unsigned int bytes_read;
19414 struct dwarf_block *blk;
19415
aead7601 19416 attr->form = (enum dwarf_form) form;
a8329558 19417 switch (form)
c906108c 19418 {
c906108c 19419 case DW_FORM_ref_addr:
ae411497 19420 if (cu->header.version == 2)
4568ecf9 19421 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 19422 else
4568ecf9
DE
19423 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19424 &cu->header, &bytes_read);
ae411497
TT
19425 info_ptr += bytes_read;
19426 break;
36586728
TT
19427 case DW_FORM_GNU_ref_alt:
19428 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19429 info_ptr += bytes_read;
19430 break;
ae411497 19431 case DW_FORM_addr:
e7c27a73 19432 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 19433 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 19434 info_ptr += bytes_read;
c906108c
SS
19435 break;
19436 case DW_FORM_block2:
7b5a2f43 19437 blk = dwarf_alloc_block (cu);
c906108c
SS
19438 blk->size = read_2_bytes (abfd, info_ptr);
19439 info_ptr += 2;
19440 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19441 info_ptr += blk->size;
19442 DW_BLOCK (attr) = blk;
19443 break;
19444 case DW_FORM_block4:
7b5a2f43 19445 blk = dwarf_alloc_block (cu);
c906108c
SS
19446 blk->size = read_4_bytes (abfd, info_ptr);
19447 info_ptr += 4;
19448 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19449 info_ptr += blk->size;
19450 DW_BLOCK (attr) = blk;
19451 break;
19452 case DW_FORM_data2:
19453 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19454 info_ptr += 2;
19455 break;
19456 case DW_FORM_data4:
19457 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19458 info_ptr += 4;
19459 break;
19460 case DW_FORM_data8:
19461 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19462 info_ptr += 8;
19463 break;
0224619f
JK
19464 case DW_FORM_data16:
19465 blk = dwarf_alloc_block (cu);
19466 blk->size = 16;
19467 blk->data = read_n_bytes (abfd, info_ptr, 16);
19468 info_ptr += 16;
19469 DW_BLOCK (attr) = blk;
19470 break;
2dc7f7b3
TT
19471 case DW_FORM_sec_offset:
19472 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19473 info_ptr += bytes_read;
19474 break;
c906108c 19475 case DW_FORM_string:
9b1c24c8 19476 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19477 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19478 info_ptr += bytes_read;
19479 break;
4bdf3d34 19480 case DW_FORM_strp:
36586728
TT
19481 if (!cu->per_cu->is_dwz)
19482 {
ed2dc618
SM
19483 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19484 abfd, info_ptr, cu_header,
36586728
TT
19485 &bytes_read);
19486 DW_STRING_IS_CANONICAL (attr) = 0;
19487 info_ptr += bytes_read;
19488 break;
19489 }
19490 /* FALLTHROUGH */
43988095
JK
19491 case DW_FORM_line_strp:
19492 if (!cu->per_cu->is_dwz)
19493 {
ed2dc618
SM
19494 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19495 abfd, info_ptr,
43988095
JK
19496 cu_header, &bytes_read);
19497 DW_STRING_IS_CANONICAL (attr) = 0;
19498 info_ptr += bytes_read;
19499 break;
19500 }
19501 /* FALLTHROUGH */
36586728
TT
19502 case DW_FORM_GNU_strp_alt:
19503 {
ed2dc618 19504 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19505 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19506 &bytes_read);
19507
ed2dc618
SM
19508 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19509 dwz, str_offset);
36586728
TT
19510 DW_STRING_IS_CANONICAL (attr) = 0;
19511 info_ptr += bytes_read;
19512 }
4bdf3d34 19513 break;
2dc7f7b3 19514 case DW_FORM_exprloc:
c906108c 19515 case DW_FORM_block:
7b5a2f43 19516 blk = dwarf_alloc_block (cu);
c906108c
SS
19517 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19518 info_ptr += bytes_read;
19519 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19520 info_ptr += blk->size;
19521 DW_BLOCK (attr) = blk;
19522 break;
19523 case DW_FORM_block1:
7b5a2f43 19524 blk = dwarf_alloc_block (cu);
c906108c
SS
19525 blk->size = read_1_byte (abfd, info_ptr);
19526 info_ptr += 1;
19527 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19528 info_ptr += blk->size;
19529 DW_BLOCK (attr) = blk;
19530 break;
19531 case DW_FORM_data1:
19532 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19533 info_ptr += 1;
19534 break;
19535 case DW_FORM_flag:
19536 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19537 info_ptr += 1;
19538 break;
2dc7f7b3
TT
19539 case DW_FORM_flag_present:
19540 DW_UNSND (attr) = 1;
19541 break;
c906108c
SS
19542 case DW_FORM_sdata:
19543 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19544 info_ptr += bytes_read;
19545 break;
19546 case DW_FORM_udata:
19547 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19548 info_ptr += bytes_read;
19549 break;
19550 case DW_FORM_ref1:
9c541725 19551 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19552 + read_1_byte (abfd, info_ptr));
c906108c
SS
19553 info_ptr += 1;
19554 break;
19555 case DW_FORM_ref2:
9c541725 19556 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19557 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19558 info_ptr += 2;
19559 break;
19560 case DW_FORM_ref4:
9c541725 19561 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19562 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19563 info_ptr += 4;
19564 break;
613e1657 19565 case DW_FORM_ref8:
9c541725 19566 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19567 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19568 info_ptr += 8;
19569 break;
55f1336d 19570 case DW_FORM_ref_sig8:
ac9ec31b 19571 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19572 info_ptr += 8;
19573 break;
c906108c 19574 case DW_FORM_ref_udata:
9c541725 19575 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19576 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19577 info_ptr += bytes_read;
19578 break;
c906108c 19579 case DW_FORM_indirect:
a8329558
KW
19580 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19581 info_ptr += bytes_read;
43988095
JK
19582 if (form == DW_FORM_implicit_const)
19583 {
19584 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19585 info_ptr += bytes_read;
19586 }
19587 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19588 info_ptr);
19589 break;
19590 case DW_FORM_implicit_const:
19591 DW_SND (attr) = implicit_const;
a8329558 19592 break;
336d760d 19593 case DW_FORM_addrx:
3019eac3
DE
19594 case DW_FORM_GNU_addr_index:
19595 if (reader->dwo_file == NULL)
19596 {
19597 /* For now flag a hard error.
19598 Later we can turn this into a complaint. */
19599 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19600 dwarf_form_name (form),
19601 bfd_get_filename (abfd));
19602 }
19603 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19604 info_ptr += bytes_read;
19605 break;
cf532bd1 19606 case DW_FORM_strx:
15f18d14
AT
19607 case DW_FORM_strx1:
19608 case DW_FORM_strx2:
19609 case DW_FORM_strx3:
19610 case DW_FORM_strx4:
3019eac3
DE
19611 case DW_FORM_GNU_str_index:
19612 if (reader->dwo_file == NULL)
19613 {
19614 /* For now flag a hard error.
19615 Later we can turn this into a complaint if warranted. */
19616 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19617 dwarf_form_name (form),
19618 bfd_get_filename (abfd));
19619 }
19620 {
15f18d14
AT
19621 ULONGEST str_index;
19622 if (form == DW_FORM_strx1)
19623 {
19624 str_index = read_1_byte (abfd, info_ptr);
19625 info_ptr += 1;
19626 }
19627 else if (form == DW_FORM_strx2)
19628 {
19629 str_index = read_2_bytes (abfd, info_ptr);
19630 info_ptr += 2;
19631 }
19632 else if (form == DW_FORM_strx3)
19633 {
19634 str_index = read_3_bytes (abfd, info_ptr);
19635 info_ptr += 3;
19636 }
19637 else if (form == DW_FORM_strx4)
19638 {
19639 str_index = read_4_bytes (abfd, info_ptr);
19640 info_ptr += 4;
19641 }
19642 else
19643 {
19644 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19645 info_ptr += bytes_read;
19646 }
342587c4 19647 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3 19648 DW_STRING_IS_CANONICAL (attr) = 0;
3019eac3
DE
19649 }
19650 break;
c906108c 19651 default:
8a3fe4f8 19652 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19653 dwarf_form_name (form),
19654 bfd_get_filename (abfd));
c906108c 19655 }
28e94949 19656
36586728 19657 /* Super hack. */
7771576e 19658 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19659 attr->form = DW_FORM_GNU_ref_alt;
19660
28e94949
JB
19661 /* We have seen instances where the compiler tried to emit a byte
19662 size attribute of -1 which ended up being encoded as an unsigned
19663 0xffffffff. Although 0xffffffff is technically a valid size value,
19664 an object of this size seems pretty unlikely so we can relatively
19665 safely treat these cases as if the size attribute was invalid and
19666 treat them as zero by default. */
19667 if (attr->name == DW_AT_byte_size
19668 && form == DW_FORM_data4
19669 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19670 {
19671 complaint
b98664d3 19672 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19673 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19674 DW_UNSND (attr) = 0;
19675 }
28e94949 19676
c906108c
SS
19677 return info_ptr;
19678}
19679
a8329558
KW
19680/* Read an attribute described by an abbreviated attribute. */
19681
d521ce57 19682static const gdb_byte *
dee91e82
DE
19683read_attribute (const struct die_reader_specs *reader,
19684 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19685 const gdb_byte *info_ptr)
a8329558
KW
19686{
19687 attr->name = abbrev->name;
43988095
JK
19688 return read_attribute_value (reader, attr, abbrev->form,
19689 abbrev->implicit_const, info_ptr);
a8329558
KW
19690}
19691
0963b4bd 19692/* Read dwarf information from a buffer. */
c906108c
SS
19693
19694static unsigned int
a1855c1d 19695read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19696{
fe1b8b76 19697 return bfd_get_8 (abfd, buf);
c906108c
SS
19698}
19699
19700static int
a1855c1d 19701read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19702{
fe1b8b76 19703 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19704}
19705
19706static unsigned int
a1855c1d 19707read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19708{
fe1b8b76 19709 return bfd_get_16 (abfd, buf);
c906108c
SS
19710}
19711
21ae7a4d 19712static int
a1855c1d 19713read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19714{
19715 return bfd_get_signed_16 (abfd, buf);
19716}
19717
15f18d14
AT
19718static unsigned int
19719read_3_bytes (bfd *abfd, const gdb_byte *buf)
19720{
19721 unsigned int result = 0;
19722 for (int i = 0; i < 3; ++i)
19723 {
19724 unsigned char byte = bfd_get_8 (abfd, buf);
19725 buf++;
19726 result |= ((unsigned int) byte << (i * 8));
19727 }
19728 return result;
19729}
19730
c906108c 19731static unsigned int
a1855c1d 19732read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19733{
fe1b8b76 19734 return bfd_get_32 (abfd, buf);
c906108c
SS
19735}
19736
21ae7a4d 19737static int
a1855c1d 19738read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19739{
19740 return bfd_get_signed_32 (abfd, buf);
19741}
19742
93311388 19743static ULONGEST
a1855c1d 19744read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19745{
fe1b8b76 19746 return bfd_get_64 (abfd, buf);
c906108c
SS
19747}
19748
19749static CORE_ADDR
d521ce57 19750read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19751 unsigned int *bytes_read)
c906108c 19752{
e7c27a73 19753 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19754 CORE_ADDR retval = 0;
19755
107d2387 19756 if (cu_header->signed_addr_p)
c906108c 19757 {
107d2387
AC
19758 switch (cu_header->addr_size)
19759 {
19760 case 2:
fe1b8b76 19761 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19762 break;
19763 case 4:
fe1b8b76 19764 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19765 break;
19766 case 8:
fe1b8b76 19767 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19768 break;
19769 default:
8e65ff28 19770 internal_error (__FILE__, __LINE__,
e2e0b3e5 19771 _("read_address: bad switch, signed [in module %s]"),
659b0389 19772 bfd_get_filename (abfd));
107d2387
AC
19773 }
19774 }
19775 else
19776 {
19777 switch (cu_header->addr_size)
19778 {
19779 case 2:
fe1b8b76 19780 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19781 break;
19782 case 4:
fe1b8b76 19783 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19784 break;
19785 case 8:
fe1b8b76 19786 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19787 break;
19788 default:
8e65ff28 19789 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19790 _("read_address: bad switch, "
19791 "unsigned [in module %s]"),
659b0389 19792 bfd_get_filename (abfd));
107d2387 19793 }
c906108c 19794 }
64367e0a 19795
107d2387
AC
19796 *bytes_read = cu_header->addr_size;
19797 return retval;
c906108c
SS
19798}
19799
f7ef9339 19800/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19801 specification allows the initial length to take up either 4 bytes
19802 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19803 bytes describe the length and all offsets will be 8 bytes in length
19804 instead of 4.
19805
f7ef9339
KB
19806 An older, non-standard 64-bit format is also handled by this
19807 function. The older format in question stores the initial length
19808 as an 8-byte quantity without an escape value. Lengths greater
19809 than 2^32 aren't very common which means that the initial 4 bytes
19810 is almost always zero. Since a length value of zero doesn't make
19811 sense for the 32-bit format, this initial zero can be considered to
19812 be an escape value which indicates the presence of the older 64-bit
19813 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19814 greater than 4GB. If it becomes necessary to handle lengths
19815 somewhat larger than 4GB, we could allow other small values (such
19816 as the non-sensical values of 1, 2, and 3) to also be used as
19817 escape values indicating the presence of the old format.
f7ef9339 19818
917c78fc
MK
19819 The value returned via bytes_read should be used to increment the
19820 relevant pointer after calling read_initial_length().
c764a876 19821
613e1657
KB
19822 [ Note: read_initial_length() and read_offset() are based on the
19823 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19824 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19825 from:
19826
f7ef9339 19827 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19828
613e1657
KB
19829 This document is only a draft and is subject to change. (So beware.)
19830
f7ef9339 19831 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19832 determined empirically by examining 64-bit ELF files produced by
19833 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19834
19835 - Kevin, July 16, 2002
613e1657
KB
19836 ] */
19837
19838static LONGEST
d521ce57 19839read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19840{
fe1b8b76 19841 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19842
dd373385 19843 if (length == 0xffffffff)
613e1657 19844 {
fe1b8b76 19845 length = bfd_get_64 (abfd, buf + 4);
613e1657 19846 *bytes_read = 12;
613e1657 19847 }
dd373385 19848 else if (length == 0)
f7ef9339 19849 {
dd373385 19850 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19851 length = bfd_get_64 (abfd, buf);
f7ef9339 19852 *bytes_read = 8;
f7ef9339 19853 }
613e1657
KB
19854 else
19855 {
19856 *bytes_read = 4;
613e1657
KB
19857 }
19858
c764a876
DE
19859 return length;
19860}
dd373385 19861
c764a876
DE
19862/* Cover function for read_initial_length.
19863 Returns the length of the object at BUF, and stores the size of the
19864 initial length in *BYTES_READ and stores the size that offsets will be in
19865 *OFFSET_SIZE.
19866 If the initial length size is not equivalent to that specified in
19867 CU_HEADER then issue a complaint.
19868 This is useful when reading non-comp-unit headers. */
dd373385 19869
c764a876 19870static LONGEST
d521ce57 19871read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19872 const struct comp_unit_head *cu_header,
19873 unsigned int *bytes_read,
19874 unsigned int *offset_size)
19875{
19876 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19877
19878 gdb_assert (cu_header->initial_length_size == 4
19879 || cu_header->initial_length_size == 8
19880 || cu_header->initial_length_size == 12);
19881
19882 if (cu_header->initial_length_size != *bytes_read)
b98664d3 19883 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19884
c764a876 19885 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19886 return length;
613e1657
KB
19887}
19888
19889/* Read an offset from the data stream. The size of the offset is
917c78fc 19890 given by cu_header->offset_size. */
613e1657
KB
19891
19892static LONGEST
d521ce57
TT
19893read_offset (bfd *abfd, const gdb_byte *buf,
19894 const struct comp_unit_head *cu_header,
891d2f0b 19895 unsigned int *bytes_read)
c764a876
DE
19896{
19897 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19898
c764a876
DE
19899 *bytes_read = cu_header->offset_size;
19900 return offset;
19901}
19902
19903/* Read an offset from the data stream. */
19904
19905static LONGEST
d521ce57 19906read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19907{
19908 LONGEST retval = 0;
19909
c764a876 19910 switch (offset_size)
613e1657
KB
19911 {
19912 case 4:
fe1b8b76 19913 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19914 break;
19915 case 8:
fe1b8b76 19916 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19917 break;
19918 default:
8e65ff28 19919 internal_error (__FILE__, __LINE__,
c764a876 19920 _("read_offset_1: bad switch [in module %s]"),
659b0389 19921 bfd_get_filename (abfd));
613e1657
KB
19922 }
19923
917c78fc 19924 return retval;
613e1657
KB
19925}
19926
d521ce57
TT
19927static const gdb_byte *
19928read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19929{
19930 /* If the size of a host char is 8 bits, we can return a pointer
19931 to the buffer, otherwise we have to copy the data to a buffer
19932 allocated on the temporary obstack. */
4bdf3d34 19933 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19934 return buf;
c906108c
SS
19935}
19936
d521ce57
TT
19937static const char *
19938read_direct_string (bfd *abfd, const gdb_byte *buf,
19939 unsigned int *bytes_read_ptr)
c906108c
SS
19940{
19941 /* If the size of a host char is 8 bits, we can return a pointer
19942 to the string, otherwise we have to copy the string to a buffer
19943 allocated on the temporary obstack. */
4bdf3d34 19944 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19945 if (*buf == '\0')
19946 {
19947 *bytes_read_ptr = 1;
19948 return NULL;
19949 }
d521ce57
TT
19950 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19951 return (const char *) buf;
4bdf3d34
JJ
19952}
19953
43988095
JK
19954/* Return pointer to string at section SECT offset STR_OFFSET with error
19955 reporting strings FORM_NAME and SECT_NAME. */
19956
d521ce57 19957static const char *
ed2dc618
SM
19958read_indirect_string_at_offset_from (struct objfile *objfile,
19959 bfd *abfd, LONGEST str_offset,
43988095
JK
19960 struct dwarf2_section_info *sect,
19961 const char *form_name,
19962 const char *sect_name)
19963{
ed2dc618 19964 dwarf2_read_section (objfile, sect);
43988095
JK
19965 if (sect->buffer == NULL)
19966 error (_("%s used without %s section [in module %s]"),
19967 form_name, sect_name, bfd_get_filename (abfd));
19968 if (str_offset >= sect->size)
19969 error (_("%s pointing outside of %s section [in module %s]"),
19970 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19971 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19972 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19973 return NULL;
43988095
JK
19974 return (const char *) (sect->buffer + str_offset);
19975}
19976
19977/* Return pointer to string at .debug_str offset STR_OFFSET. */
19978
19979static const char *
ed2dc618
SM
19980read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19981 bfd *abfd, LONGEST str_offset)
43988095 19982{
ed2dc618
SM
19983 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19984 abfd, str_offset,
43988095
JK
19985 &dwarf2_per_objfile->str,
19986 "DW_FORM_strp", ".debug_str");
19987}
19988
19989/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19990
19991static const char *
ed2dc618
SM
19992read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19993 bfd *abfd, LONGEST str_offset)
43988095 19994{
ed2dc618
SM
19995 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19996 abfd, str_offset,
43988095
JK
19997 &dwarf2_per_objfile->line_str,
19998 "DW_FORM_line_strp",
19999 ".debug_line_str");
c906108c
SS
20000}
20001
36586728
TT
20002/* Read a string at offset STR_OFFSET in the .debug_str section from
20003 the .dwz file DWZ. Throw an error if the offset is too large. If
20004 the string consists of a single NUL byte, return NULL; otherwise
20005 return a pointer to the string. */
20006
d521ce57 20007static const char *
ed2dc618
SM
20008read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
20009 LONGEST str_offset)
36586728 20010{
ed2dc618 20011 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
20012
20013 if (dwz->str.buffer == NULL)
20014 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
20015 "section [in module %s]"),
00f93c44 20016 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
20017 if (str_offset >= dwz->str.size)
20018 error (_("DW_FORM_GNU_strp_alt pointing outside of "
20019 ".debug_str section [in module %s]"),
00f93c44 20020 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
20021 gdb_assert (HOST_CHAR_BIT == 8);
20022 if (dwz->str.buffer[str_offset] == '\0')
20023 return NULL;
d521ce57 20024 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
20025}
20026
43988095
JK
20027/* Return pointer to string at .debug_str offset as read from BUF.
20028 BUF is assumed to be in a compilation unit described by CU_HEADER.
20029 Return *BYTES_READ_PTR count of bytes read from BUF. */
20030
d521ce57 20031static const char *
ed2dc618
SM
20032read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
20033 const gdb_byte *buf,
cf2c3c16
TT
20034 const struct comp_unit_head *cu_header,
20035 unsigned int *bytes_read_ptr)
20036{
20037 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20038
ed2dc618 20039 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
20040}
20041
43988095
JK
20042/* Return pointer to string at .debug_line_str offset as read from BUF.
20043 BUF is assumed to be in a compilation unit described by CU_HEADER.
20044 Return *BYTES_READ_PTR count of bytes read from BUF. */
20045
20046static const char *
ed2dc618
SM
20047read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
20048 bfd *abfd, const gdb_byte *buf,
43988095
JK
20049 const struct comp_unit_head *cu_header,
20050 unsigned int *bytes_read_ptr)
20051{
20052 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20053
ed2dc618
SM
20054 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
20055 str_offset);
43988095
JK
20056}
20057
20058ULONGEST
d521ce57 20059read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 20060 unsigned int *bytes_read_ptr)
c906108c 20061{
12df843f 20062 ULONGEST result;
ce5d95e1 20063 unsigned int num_read;
870f88f7 20064 int shift;
c906108c
SS
20065 unsigned char byte;
20066
20067 result = 0;
20068 shift = 0;
20069 num_read = 0;
c906108c
SS
20070 while (1)
20071 {
fe1b8b76 20072 byte = bfd_get_8 (abfd, buf);
c906108c
SS
20073 buf++;
20074 num_read++;
12df843f 20075 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
20076 if ((byte & 128) == 0)
20077 {
20078 break;
20079 }
20080 shift += 7;
20081 }
20082 *bytes_read_ptr = num_read;
20083 return result;
20084}
20085
12df843f 20086static LONGEST
d521ce57
TT
20087read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
20088 unsigned int *bytes_read_ptr)
c906108c 20089{
4dd1b460 20090 ULONGEST result;
870f88f7 20091 int shift, num_read;
c906108c
SS
20092 unsigned char byte;
20093
20094 result = 0;
20095 shift = 0;
c906108c 20096 num_read = 0;
c906108c
SS
20097 while (1)
20098 {
fe1b8b76 20099 byte = bfd_get_8 (abfd, buf);
c906108c
SS
20100 buf++;
20101 num_read++;
4dd1b460 20102 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
20103 shift += 7;
20104 if ((byte & 128) == 0)
20105 {
20106 break;
20107 }
20108 }
77e0b926 20109 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
4dd1b460 20110 result |= -(((ULONGEST) 1) << shift);
c906108c
SS
20111 *bytes_read_ptr = num_read;
20112 return result;
20113}
20114
3019eac3
DE
20115/* Given index ADDR_INDEX in .debug_addr, fetch the value.
20116 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
20117 ADDR_SIZE is the size of addresses from the CU header. */
20118
20119static CORE_ADDR
ed2dc618
SM
20120read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
20121 unsigned int addr_index, ULONGEST addr_base, int addr_size)
3019eac3
DE
20122{
20123 struct objfile *objfile = dwarf2_per_objfile->objfile;
20124 bfd *abfd = objfile->obfd;
20125 const gdb_byte *info_ptr;
20126
20127 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
20128 if (dwarf2_per_objfile->addr.buffer == NULL)
20129 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20130 objfile_name (objfile));
3019eac3
DE
20131 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
20132 error (_("DW_FORM_addr_index pointing outside of "
20133 ".debug_addr section [in module %s]"),
4262abfb 20134 objfile_name (objfile));
3019eac3
DE
20135 info_ptr = (dwarf2_per_objfile->addr.buffer
20136 + addr_base + addr_index * addr_size);
20137 if (addr_size == 4)
20138 return bfd_get_32 (abfd, info_ptr);
20139 else
20140 return bfd_get_64 (abfd, info_ptr);
20141}
20142
20143/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20144
20145static CORE_ADDR
20146read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20147{
518817b3
SM
20148 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20149 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20150}
20151
20152/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20153
20154static CORE_ADDR
d521ce57 20155read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20156 unsigned int *bytes_read)
20157{
518817b3 20158 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
20159 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20160
20161 return read_addr_index (cu, addr_index);
20162}
20163
20164/* Data structure to pass results from dwarf2_read_addr_index_reader
20165 back to dwarf2_read_addr_index. */
20166
20167struct dwarf2_read_addr_index_data
20168{
20169 ULONGEST addr_base;
20170 int addr_size;
20171};
20172
20173/* die_reader_func for dwarf2_read_addr_index. */
20174
20175static void
20176dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 20177 const gdb_byte *info_ptr,
3019eac3
DE
20178 struct die_info *comp_unit_die,
20179 int has_children,
20180 void *data)
20181{
20182 struct dwarf2_cu *cu = reader->cu;
20183 struct dwarf2_read_addr_index_data *aidata =
20184 (struct dwarf2_read_addr_index_data *) data;
20185
20186 aidata->addr_base = cu->addr_base;
20187 aidata->addr_size = cu->header.addr_size;
20188}
20189
20190/* Given an index in .debug_addr, fetch the value.
20191 NOTE: This can be called during dwarf expression evaluation,
20192 long after the debug information has been read, and thus per_cu->cu
20193 may no longer exist. */
20194
20195CORE_ADDR
20196dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20197 unsigned int addr_index)
20198{
ed2dc618 20199 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3
DE
20200 struct dwarf2_cu *cu = per_cu->cu;
20201 ULONGEST addr_base;
20202 int addr_size;
20203
3019eac3
DE
20204 /* We need addr_base and addr_size.
20205 If we don't have PER_CU->cu, we have to get it.
20206 Nasty, but the alternative is storing the needed info in PER_CU,
20207 which at this point doesn't seem justified: it's not clear how frequently
20208 it would get used and it would increase the size of every PER_CU.
20209 Entry points like dwarf2_per_cu_addr_size do a similar thing
20210 so we're not in uncharted territory here.
20211 Alas we need to be a bit more complicated as addr_base is contained
20212 in the DIE.
20213
20214 We don't need to read the entire CU(/TU).
20215 We just need the header and top level die.
a1b64ce1 20216
3019eac3 20217 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20218 For now we skip this optimization. */
3019eac3
DE
20219
20220 if (cu != NULL)
20221 {
20222 addr_base = cu->addr_base;
20223 addr_size = cu->header.addr_size;
20224 }
20225 else
20226 {
20227 struct dwarf2_read_addr_index_data aidata;
20228
a1b64ce1
DE
20229 /* Note: We can't use init_cutu_and_read_dies_simple here,
20230 we need addr_base. */
58f0c718 20231 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
a1b64ce1 20232 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
20233 addr_base = aidata.addr_base;
20234 addr_size = aidata.addr_size;
20235 }
20236
ed2dc618
SM
20237 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20238 addr_size);
3019eac3
DE
20239}
20240
cf532bd1 20241/* Given a DW_FORM_GNU_str_index or DW_FORM_strx, fetch the string.
57d63ce2 20242 This is only used by the Fission support. */
3019eac3 20243
d521ce57 20244static const char *
342587c4 20245read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3 20246{
ed2dc618 20247 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
20248 struct dwarf2_per_objfile *dwarf2_per_objfile
20249 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20250 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 20251 const char *objf_name = objfile_name (objfile);
3019eac3 20252 bfd *abfd = objfile->obfd;
73869dc2
DE
20253 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20254 struct dwarf2_section_info *str_offsets_section =
20255 &reader->dwo_file->sections.str_offsets;
d521ce57 20256 const gdb_byte *info_ptr;
3019eac3 20257 ULONGEST str_offset;
cf532bd1 20258 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20259
73869dc2
DE
20260 dwarf2_read_section (objfile, str_section);
20261 dwarf2_read_section (objfile, str_offsets_section);
20262 if (str_section->buffer == NULL)
57d63ce2 20263 error (_("%s used without .debug_str.dwo section"
9d8780f0
SM
20264 " in CU at offset %s [in module %s]"),
20265 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20266 if (str_offsets_section->buffer == NULL)
57d63ce2 20267 error (_("%s used without .debug_str_offsets.dwo section"
9d8780f0
SM
20268 " in CU at offset %s [in module %s]"),
20269 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20270 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 20271 error (_("%s pointing outside of .debug_str_offsets.dwo"
9d8780f0
SM
20272 " section in CU at offset %s [in module %s]"),
20273 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20274 info_ptr = (str_offsets_section->buffer
3019eac3
DE
20275 + str_index * cu->header.offset_size);
20276 if (cu->header.offset_size == 4)
20277 str_offset = bfd_get_32 (abfd, info_ptr);
20278 else
20279 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20280 if (str_offset >= str_section->size)
57d63ce2 20281 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20282 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20283 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20284 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20285}
20286
3019eac3
DE
20287/* Return the length of an LEB128 number in BUF. */
20288
20289static int
20290leb128_size (const gdb_byte *buf)
20291{
20292 const gdb_byte *begin = buf;
20293 gdb_byte byte;
20294
20295 while (1)
20296 {
20297 byte = *buf++;
20298 if ((byte & 128) == 0)
20299 return buf - begin;
20300 }
20301}
20302
c906108c 20303static void
e142c38c 20304set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20305{
20306 switch (lang)
20307 {
20308 case DW_LANG_C89:
76bee0cc 20309 case DW_LANG_C99:
0cfd832f 20310 case DW_LANG_C11:
c906108c 20311 case DW_LANG_C:
d1be3247 20312 case DW_LANG_UPC:
e142c38c 20313 cu->language = language_c;
c906108c 20314 break;
9c37b5ae 20315 case DW_LANG_Java:
c906108c 20316 case DW_LANG_C_plus_plus:
0cfd832f
MW
20317 case DW_LANG_C_plus_plus_11:
20318 case DW_LANG_C_plus_plus_14:
e142c38c 20319 cu->language = language_cplus;
c906108c 20320 break;
6aecb9c2
JB
20321 case DW_LANG_D:
20322 cu->language = language_d;
20323 break;
c906108c
SS
20324 case DW_LANG_Fortran77:
20325 case DW_LANG_Fortran90:
b21b22e0 20326 case DW_LANG_Fortran95:
f7de9aab
MW
20327 case DW_LANG_Fortran03:
20328 case DW_LANG_Fortran08:
e142c38c 20329 cu->language = language_fortran;
c906108c 20330 break;
a766d390
DE
20331 case DW_LANG_Go:
20332 cu->language = language_go;
20333 break;
c906108c 20334 case DW_LANG_Mips_Assembler:
e142c38c 20335 cu->language = language_asm;
c906108c
SS
20336 break;
20337 case DW_LANG_Ada83:
8aaf0b47 20338 case DW_LANG_Ada95:
bc5f45f8
JB
20339 cu->language = language_ada;
20340 break;
72019c9c
GM
20341 case DW_LANG_Modula2:
20342 cu->language = language_m2;
20343 break;
fe8e67fd
PM
20344 case DW_LANG_Pascal83:
20345 cu->language = language_pascal;
20346 break;
22566fbd
DJ
20347 case DW_LANG_ObjC:
20348 cu->language = language_objc;
20349 break;
c44af4eb
TT
20350 case DW_LANG_Rust:
20351 case DW_LANG_Rust_old:
20352 cu->language = language_rust;
20353 break;
c906108c
SS
20354 case DW_LANG_Cobol74:
20355 case DW_LANG_Cobol85:
c906108c 20356 default:
e142c38c 20357 cu->language = language_minimal;
c906108c
SS
20358 break;
20359 }
e142c38c 20360 cu->language_defn = language_def (cu->language);
c906108c
SS
20361}
20362
20363/* Return the named attribute or NULL if not there. */
20364
20365static struct attribute *
e142c38c 20366dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20367{
a48e046c 20368 for (;;)
c906108c 20369 {
a48e046c
TT
20370 unsigned int i;
20371 struct attribute *spec = NULL;
20372
20373 for (i = 0; i < die->num_attrs; ++i)
20374 {
20375 if (die->attrs[i].name == name)
20376 return &die->attrs[i];
20377 if (die->attrs[i].name == DW_AT_specification
20378 || die->attrs[i].name == DW_AT_abstract_origin)
20379 spec = &die->attrs[i];
20380 }
20381
20382 if (!spec)
20383 break;
c906108c 20384
f2f0e013 20385 die = follow_die_ref (die, spec, &cu);
f2f0e013 20386 }
c5aa993b 20387
c906108c
SS
20388 return NULL;
20389}
20390
348e048f
DE
20391/* Return the named attribute or NULL if not there,
20392 but do not follow DW_AT_specification, etc.
20393 This is for use in contexts where we're reading .debug_types dies.
20394 Following DW_AT_specification, DW_AT_abstract_origin will take us
20395 back up the chain, and we want to go down. */
20396
20397static struct attribute *
45e58e77 20398dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
20399{
20400 unsigned int i;
20401
20402 for (i = 0; i < die->num_attrs; ++i)
20403 if (die->attrs[i].name == name)
20404 return &die->attrs[i];
20405
20406 return NULL;
20407}
20408
7d45c7c3
KB
20409/* Return the string associated with a string-typed attribute, or NULL if it
20410 is either not found or is of an incorrect type. */
20411
20412static const char *
20413dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20414{
20415 struct attribute *attr;
20416 const char *str = NULL;
20417
20418 attr = dwarf2_attr (die, name, cu);
20419
20420 if (attr != NULL)
20421 {
43988095 20422 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 20423 || attr->form == DW_FORM_string
cf532bd1 20424 || attr->form == DW_FORM_strx
8fe0f950
AT
20425 || attr->form == DW_FORM_strx1
20426 || attr->form == DW_FORM_strx2
20427 || attr->form == DW_FORM_strx3
20428 || attr->form == DW_FORM_strx4
b3340438 20429 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 20430 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
20431 str = DW_STRING (attr);
20432 else
b98664d3 20433 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20434 "DIE at %s in module %s"),
20435 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 20436 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
20437 }
20438
20439 return str;
20440}
20441
a084a2a6 20442/* Return the dwo name or NULL if not present. If present, it is in either
85102364 20443 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
20444static const char *
20445dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20446{
20447 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20448 if (dwo_name == nullptr)
20449 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20450 return dwo_name;
20451}
20452
05cf31d1
JB
20453/* Return non-zero iff the attribute NAME is defined for the given DIE,
20454 and holds a non-zero value. This function should only be used for
2dc7f7b3 20455 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20456
20457static int
20458dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20459{
20460 struct attribute *attr = dwarf2_attr (die, name, cu);
20461
20462 return (attr && DW_UNSND (attr));
20463}
20464
3ca72b44 20465static int
e142c38c 20466die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20467{
05cf31d1
JB
20468 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20469 which value is non-zero. However, we have to be careful with
20470 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20471 (via dwarf2_flag_true_p) follows this attribute. So we may
20472 end up accidently finding a declaration attribute that belongs
20473 to a different DIE referenced by the specification attribute,
20474 even though the given DIE does not have a declaration attribute. */
20475 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20476 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20477}
20478
63d06c5c 20479/* Return the die giving the specification for DIE, if there is
f2f0e013 20480 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20481 containing the return value on output. If there is no
20482 specification, but there is an abstract origin, that is
20483 returned. */
63d06c5c
DC
20484
20485static struct die_info *
f2f0e013 20486die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20487{
f2f0e013
DJ
20488 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20489 *spec_cu);
63d06c5c 20490
edb3359d
DJ
20491 if (spec_attr == NULL)
20492 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20493
63d06c5c
DC
20494 if (spec_attr == NULL)
20495 return NULL;
20496 else
f2f0e013 20497 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20498}
c906108c 20499
527f3840
JK
20500/* Stub for free_line_header to match void * callback types. */
20501
20502static void
20503free_line_header_voidp (void *arg)
20504{
9a3c8263 20505 struct line_header *lh = (struct line_header *) arg;
527f3840 20506
fff8551c 20507 delete lh;
527f3840
JK
20508}
20509
fff8551c
PA
20510void
20511line_header::add_include_dir (const char *include_dir)
c906108c 20512{
27e0867f 20513 if (dwarf_line_debug >= 2)
7ba99d21
AT
20514 {
20515 size_t new_size;
20516 if (version >= 5)
20517 new_size = m_include_dirs.size ();
20518 else
20519 new_size = m_include_dirs.size () + 1;
20520 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20521 new_size, include_dir);
20522 }
20523 m_include_dirs.push_back (include_dir);
debd256d 20524}
6e70227d 20525
fff8551c
PA
20526void
20527line_header::add_file_name (const char *name,
ecfb656c 20528 dir_index d_index,
fff8551c
PA
20529 unsigned int mod_time,
20530 unsigned int length)
debd256d 20531{
27e0867f 20532 if (dwarf_line_debug >= 2)
7ba99d21
AT
20533 {
20534 size_t new_size;
20535 if (version >= 5)
20536 new_size = file_names_size ();
20537 else
20538 new_size = file_names_size () + 1;
20539 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
20540 new_size, name);
20541 }
20542 m_file_names.emplace_back (name, d_index, mod_time, length);
debd256d 20543}
6e70227d 20544
83769d0b 20545/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20546
20547static struct dwarf2_section_info *
20548get_debug_line_section (struct dwarf2_cu *cu)
20549{
20550 struct dwarf2_section_info *section;
518817b3
SM
20551 struct dwarf2_per_objfile *dwarf2_per_objfile
20552 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
20553
20554 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20555 DWO file. */
20556 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20557 section = &cu->dwo_unit->dwo_file->sections.line;
20558 else if (cu->per_cu->is_dwz)
20559 {
ed2dc618 20560 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
20561
20562 section = &dwz->line;
20563 }
20564 else
20565 section = &dwarf2_per_objfile->line;
20566
20567 return section;
20568}
20569
43988095
JK
20570/* Read directory or file name entry format, starting with byte of
20571 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20572 entries count and the entries themselves in the described entry
20573 format. */
20574
20575static void
ed2dc618
SM
20576read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20577 bfd *abfd, const gdb_byte **bufp,
43988095
JK
20578 struct line_header *lh,
20579 const struct comp_unit_head *cu_header,
20580 void (*callback) (struct line_header *lh,
20581 const char *name,
ecfb656c 20582 dir_index d_index,
43988095
JK
20583 unsigned int mod_time,
20584 unsigned int length))
20585{
20586 gdb_byte format_count, formati;
20587 ULONGEST data_count, datai;
20588 const gdb_byte *buf = *bufp;
20589 const gdb_byte *format_header_data;
43988095
JK
20590 unsigned int bytes_read;
20591
20592 format_count = read_1_byte (abfd, buf);
20593 buf += 1;
20594 format_header_data = buf;
20595 for (formati = 0; formati < format_count; formati++)
20596 {
20597 read_unsigned_leb128 (abfd, buf, &bytes_read);
20598 buf += bytes_read;
20599 read_unsigned_leb128 (abfd, buf, &bytes_read);
20600 buf += bytes_read;
20601 }
20602
20603 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20604 buf += bytes_read;
20605 for (datai = 0; datai < data_count; datai++)
20606 {
20607 const gdb_byte *format = format_header_data;
20608 struct file_entry fe;
20609
43988095
JK
20610 for (formati = 0; formati < format_count; formati++)
20611 {
ecfb656c 20612 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20613 format += bytes_read;
43988095 20614
ecfb656c 20615 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20616 format += bytes_read;
ecfb656c
PA
20617
20618 gdb::optional<const char *> string;
20619 gdb::optional<unsigned int> uint;
20620
43988095
JK
20621 switch (form)
20622 {
20623 case DW_FORM_string:
ecfb656c 20624 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20625 buf += bytes_read;
20626 break;
20627
20628 case DW_FORM_line_strp:
ed2dc618
SM
20629 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20630 abfd, buf,
ecfb656c
PA
20631 cu_header,
20632 &bytes_read));
43988095
JK
20633 buf += bytes_read;
20634 break;
20635
20636 case DW_FORM_data1:
ecfb656c 20637 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20638 buf += 1;
20639 break;
20640
20641 case DW_FORM_data2:
ecfb656c 20642 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20643 buf += 2;
20644 break;
20645
20646 case DW_FORM_data4:
ecfb656c 20647 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20648 buf += 4;
20649 break;
20650
20651 case DW_FORM_data8:
ecfb656c 20652 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20653 buf += 8;
20654 break;
20655
7ba99d21
AT
20656 case DW_FORM_data16:
20657 /* This is used for MD5, but file_entry does not record MD5s. */
20658 buf += 16;
20659 break;
20660
43988095 20661 case DW_FORM_udata:
ecfb656c 20662 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20663 buf += bytes_read;
20664 break;
20665
20666 case DW_FORM_block:
20667 /* It is valid only for DW_LNCT_timestamp which is ignored by
20668 current GDB. */
20669 break;
20670 }
ecfb656c
PA
20671
20672 switch (content_type)
20673 {
20674 case DW_LNCT_path:
20675 if (string.has_value ())
20676 fe.name = *string;
20677 break;
20678 case DW_LNCT_directory_index:
20679 if (uint.has_value ())
20680 fe.d_index = (dir_index) *uint;
20681 break;
20682 case DW_LNCT_timestamp:
20683 if (uint.has_value ())
20684 fe.mod_time = *uint;
20685 break;
20686 case DW_LNCT_size:
20687 if (uint.has_value ())
20688 fe.length = *uint;
20689 break;
20690 case DW_LNCT_MD5:
20691 break;
20692 default:
b98664d3 20693 complaint (_("Unknown format content type %s"),
ecfb656c
PA
20694 pulongest (content_type));
20695 }
43988095
JK
20696 }
20697
ecfb656c 20698 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20699 }
20700
20701 *bufp = buf;
20702}
20703
debd256d 20704/* Read the statement program header starting at OFFSET in
3019eac3 20705 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20706 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20707 Returns NULL if there is a problem reading the header, e.g., if it
20708 has a version we don't understand.
debd256d
JB
20709
20710 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20711 the returned object point into the dwarf line section buffer,
20712 and must not be freed. */
ae2de4f8 20713
fff8551c 20714static line_header_up
9c541725 20715dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20716{
d521ce57 20717 const gdb_byte *line_ptr;
c764a876 20718 unsigned int bytes_read, offset_size;
debd256d 20719 int i;
d521ce57 20720 const char *cur_dir, *cur_file;
3019eac3
DE
20721 struct dwarf2_section_info *section;
20722 bfd *abfd;
518817b3
SM
20723 struct dwarf2_per_objfile *dwarf2_per_objfile
20724 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20725
36586728 20726 section = get_debug_line_section (cu);
3019eac3
DE
20727 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20728 if (section->buffer == NULL)
debd256d 20729 {
3019eac3 20730 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20731 complaint (_("missing .debug_line.dwo section"));
3019eac3 20732 else
b98664d3 20733 complaint (_("missing .debug_line section"));
debd256d
JB
20734 return 0;
20735 }
20736
fceca515
DE
20737 /* We can't do this until we know the section is non-empty.
20738 Only then do we know we have such a section. */
a32a8923 20739 abfd = get_section_bfd_owner (section);
fceca515 20740
a738430d
MK
20741 /* Make sure that at least there's room for the total_length field.
20742 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20743 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20744 {
4d3c2250 20745 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20746 return 0;
20747 }
20748
fff8551c 20749 line_header_up lh (new line_header ());
debd256d 20750
9c541725 20751 lh->sect_off = sect_off;
527f3840
JK
20752 lh->offset_in_dwz = cu->per_cu->is_dwz;
20753
9c541725 20754 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20755
a738430d 20756 /* Read in the header. */
6e70227d 20757 lh->total_length =
c764a876
DE
20758 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20759 &bytes_read, &offset_size);
debd256d 20760 line_ptr += bytes_read;
7ba99d21
AT
20761
20762 const gdb_byte *start_here = line_ptr;
20763
3019eac3 20764 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20765 {
4d3c2250 20766 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20767 return 0;
20768 }
7ba99d21 20769 lh->statement_program_end = start_here + lh->total_length;
debd256d
JB
20770 lh->version = read_2_bytes (abfd, line_ptr);
20771 line_ptr += 2;
43988095 20772 if (lh->version > 5)
cd366ee8
DE
20773 {
20774 /* This is a version we don't understand. The format could have
20775 changed in ways we don't handle properly so just punt. */
b98664d3 20776 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
20777 return NULL;
20778 }
43988095
JK
20779 if (lh->version >= 5)
20780 {
20781 gdb_byte segment_selector_size;
20782
20783 /* Skip address size. */
20784 read_1_byte (abfd, line_ptr);
20785 line_ptr += 1;
20786
20787 segment_selector_size = read_1_byte (abfd, line_ptr);
20788 line_ptr += 1;
20789 if (segment_selector_size != 0)
20790 {
b98664d3 20791 complaint (_("unsupported segment selector size %u "
43988095
JK
20792 "in .debug_line section"),
20793 segment_selector_size);
20794 return NULL;
20795 }
20796 }
c764a876
DE
20797 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20798 line_ptr += offset_size;
7ba99d21 20799 lh->statement_program_start = line_ptr + lh->header_length;
debd256d
JB
20800 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20801 line_ptr += 1;
2dc7f7b3
TT
20802 if (lh->version >= 4)
20803 {
20804 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20805 line_ptr += 1;
20806 }
20807 else
20808 lh->maximum_ops_per_instruction = 1;
20809
20810 if (lh->maximum_ops_per_instruction == 0)
20811 {
20812 lh->maximum_ops_per_instruction = 1;
b98664d3 20813 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 20814 "in `.debug_line' section"));
2dc7f7b3
TT
20815 }
20816
debd256d
JB
20817 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20818 line_ptr += 1;
20819 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20820 line_ptr += 1;
20821 lh->line_range = read_1_byte (abfd, line_ptr);
20822 line_ptr += 1;
20823 lh->opcode_base = read_1_byte (abfd, line_ptr);
20824 line_ptr += 1;
fff8551c 20825 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20826
20827 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20828 for (i = 1; i < lh->opcode_base; ++i)
20829 {
20830 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20831 line_ptr += 1;
20832 }
20833
43988095 20834 if (lh->version >= 5)
debd256d 20835 {
43988095 20836 /* Read directory table. */
ed2dc618
SM
20837 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20838 &cu->header,
b926417a 20839 [] (struct line_header *header, const char *name,
ecfb656c 20840 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20841 unsigned int length)
20842 {
b926417a 20843 header->add_include_dir (name);
fff8551c 20844 });
debd256d 20845
43988095 20846 /* Read file name table. */
ed2dc618
SM
20847 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20848 &cu->header,
b926417a 20849 [] (struct line_header *header, const char *name,
ecfb656c 20850 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20851 unsigned int length)
20852 {
b926417a 20853 header->add_file_name (name, d_index, mod_time, length);
fff8551c 20854 });
43988095
JK
20855 }
20856 else
debd256d 20857 {
43988095
JK
20858 /* Read directory table. */
20859 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20860 {
20861 line_ptr += bytes_read;
fff8551c 20862 lh->add_include_dir (cur_dir);
43988095 20863 }
debd256d
JB
20864 line_ptr += bytes_read;
20865
43988095
JK
20866 /* Read file name table. */
20867 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20868 {
ecfb656c
PA
20869 unsigned int mod_time, length;
20870 dir_index d_index;
43988095
JK
20871
20872 line_ptr += bytes_read;
ecfb656c 20873 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20874 line_ptr += bytes_read;
20875 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20876 line_ptr += bytes_read;
20877 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20878 line_ptr += bytes_read;
20879
ecfb656c 20880 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20881 }
20882 line_ptr += bytes_read;
debd256d 20883 }
debd256d 20884
3019eac3 20885 if (line_ptr > (section->buffer + section->size))
b98664d3 20886 complaint (_("line number info header doesn't "
3e43a32a 20887 "fit in `.debug_line' section"));
debd256d 20888
debd256d
JB
20889 return lh;
20890}
c906108c 20891
c6da4cef 20892/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 20893 Return the file name of the psymtab for the given file_entry.
c6da4cef 20894 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20895 If space for the result is malloc'd, *NAME_HOLDER will be set.
20896 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20897
d521ce57 20898static const char *
7ba99d21 20899psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
c6da4cef 20900 const struct partial_symtab *pst,
c89b44cd
TT
20901 const char *comp_dir,
20902 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20903{
d521ce57
TT
20904 const char *include_name = fe.name;
20905 const char *include_name_to_compare = include_name;
72b9f47f 20906 const char *pst_filename;
c6da4cef
DE
20907 int file_is_pst;
20908
8c43009f 20909 const char *dir_name = fe.include_dir (lh);
c6da4cef 20910
c89b44cd 20911 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20912 if (!IS_ABSOLUTE_PATH (include_name)
20913 && (dir_name != NULL || comp_dir != NULL))
20914 {
20915 /* Avoid creating a duplicate psymtab for PST.
20916 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20917 Before we do the comparison, however, we need to account
20918 for DIR_NAME and COMP_DIR.
20919 First prepend dir_name (if non-NULL). If we still don't
20920 have an absolute path prepend comp_dir (if non-NULL).
20921 However, the directory we record in the include-file's
20922 psymtab does not contain COMP_DIR (to match the
20923 corresponding symtab(s)).
20924
20925 Example:
20926
20927 bash$ cd /tmp
20928 bash$ gcc -g ./hello.c
20929 include_name = "hello.c"
20930 dir_name = "."
20931 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20932 DW_AT_name = "./hello.c"
20933
20934 */
c6da4cef
DE
20935
20936 if (dir_name != NULL)
20937 {
c89b44cd
TT
20938 name_holder->reset (concat (dir_name, SLASH_STRING,
20939 include_name, (char *) NULL));
20940 include_name = name_holder->get ();
c6da4cef 20941 include_name_to_compare = include_name;
c6da4cef
DE
20942 }
20943 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20944 {
c89b44cd
TT
20945 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20946 include_name, (char *) NULL));
20947 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20948 }
20949 }
20950
20951 pst_filename = pst->filename;
c89b44cd 20952 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20953 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20954 {
c89b44cd
TT
20955 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20956 pst_filename, (char *) NULL));
20957 pst_filename = copied_name.get ();
c6da4cef
DE
20958 }
20959
1e3fad37 20960 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20961
c6da4cef
DE
20962 if (file_is_pst)
20963 return NULL;
20964 return include_name;
20965}
20966
d9b3de22
DE
20967/* State machine to track the state of the line number program. */
20968
6f77053d 20969class lnp_state_machine
d9b3de22 20970{
6f77053d
PA
20971public:
20972 /* Initialize a machine state for the start of a line number
20973 program. */
804d2729
TT
20974 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20975 bool record_lines_p);
6f77053d 20976
8c43009f
PA
20977 file_entry *current_file ()
20978 {
20979 /* lh->file_names is 0-based, but the file name numbers in the
20980 statement program are 1-based. */
6f77053d
PA
20981 return m_line_header->file_name_at (m_file);
20982 }
20983
20984 /* Record the line in the state machine. END_SEQUENCE is true if
20985 we're processing the end of a sequence. */
20986 void record_line (bool end_sequence);
20987
7ab6656f
OJ
20988 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20989 nop-out rest of the lines in this sequence. */
6f77053d
PA
20990 void check_line_address (struct dwarf2_cu *cu,
20991 const gdb_byte *line_ptr,
7ab6656f 20992 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20993
20994 void handle_set_discriminator (unsigned int discriminator)
20995 {
20996 m_discriminator = discriminator;
20997 m_line_has_non_zero_discriminator |= discriminator != 0;
20998 }
20999
21000 /* Handle DW_LNE_set_address. */
21001 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21002 {
21003 m_op_index = 0;
21004 address += baseaddr;
21005 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21006 }
21007
21008 /* Handle DW_LNS_advance_pc. */
21009 void handle_advance_pc (CORE_ADDR adjust);
21010
21011 /* Handle a special opcode. */
21012 void handle_special_opcode (unsigned char op_code);
21013
21014 /* Handle DW_LNS_advance_line. */
21015 void handle_advance_line (int line_delta)
21016 {
21017 advance_line (line_delta);
21018 }
21019
21020 /* Handle DW_LNS_set_file. */
21021 void handle_set_file (file_name_index file);
21022
21023 /* Handle DW_LNS_negate_stmt. */
21024 void handle_negate_stmt ()
21025 {
21026 m_is_stmt = !m_is_stmt;
21027 }
21028
21029 /* Handle DW_LNS_const_add_pc. */
21030 void handle_const_add_pc ();
21031
21032 /* Handle DW_LNS_fixed_advance_pc. */
21033 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21034 {
21035 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21036 m_op_index = 0;
21037 }
21038
21039 /* Handle DW_LNS_copy. */
21040 void handle_copy ()
21041 {
21042 record_line (false);
21043 m_discriminator = 0;
21044 }
21045
21046 /* Handle DW_LNE_end_sequence. */
21047 void handle_end_sequence ()
21048 {
804d2729 21049 m_currently_recording_lines = true;
6f77053d
PA
21050 }
21051
21052private:
21053 /* Advance the line by LINE_DELTA. */
21054 void advance_line (int line_delta)
21055 {
21056 m_line += line_delta;
21057
21058 if (line_delta != 0)
21059 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
21060 }
21061
804d2729
TT
21062 struct dwarf2_cu *m_cu;
21063
6f77053d
PA
21064 gdbarch *m_gdbarch;
21065
21066 /* True if we're recording lines.
21067 Otherwise we're building partial symtabs and are just interested in
21068 finding include files mentioned by the line number program. */
21069 bool m_record_lines_p;
21070
8c43009f 21071 /* The line number header. */
6f77053d 21072 line_header *m_line_header;
8c43009f 21073
6f77053d
PA
21074 /* These are part of the standard DWARF line number state machine,
21075 and initialized according to the DWARF spec. */
d9b3de22 21076
6f77053d 21077 unsigned char m_op_index = 0;
7ba99d21
AT
21078 /* The line table index of the current file. */
21079 file_name_index m_file = 1;
6f77053d
PA
21080 unsigned int m_line = 1;
21081
21082 /* These are initialized in the constructor. */
21083
21084 CORE_ADDR m_address;
21085 bool m_is_stmt;
21086 unsigned int m_discriminator;
d9b3de22
DE
21087
21088 /* Additional bits of state we need to track. */
21089
21090 /* The last file that we called dwarf2_start_subfile for.
21091 This is only used for TLLs. */
6f77053d 21092 unsigned int m_last_file = 0;
d9b3de22 21093 /* The last file a line number was recorded for. */
6f77053d 21094 struct subfile *m_last_subfile = NULL;
d9b3de22 21095
804d2729
TT
21096 /* When true, record the lines we decode. */
21097 bool m_currently_recording_lines = false;
d9b3de22
DE
21098
21099 /* The last line number that was recorded, used to coalesce
21100 consecutive entries for the same line. This can happen, for
21101 example, when discriminators are present. PR 17276. */
6f77053d
PA
21102 unsigned int m_last_line = 0;
21103 bool m_line_has_non_zero_discriminator = false;
8c43009f 21104};
d9b3de22 21105
6f77053d
PA
21106void
21107lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21108{
21109 CORE_ADDR addr_adj = (((m_op_index + adjust)
21110 / m_line_header->maximum_ops_per_instruction)
21111 * m_line_header->minimum_instruction_length);
21112 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21113 m_op_index = ((m_op_index + adjust)
21114 % m_line_header->maximum_ops_per_instruction);
21115}
d9b3de22 21116
6f77053d
PA
21117void
21118lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 21119{
6f77053d
PA
21120 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21121 CORE_ADDR addr_adj = (((m_op_index
21122 + (adj_opcode / m_line_header->line_range))
21123 / m_line_header->maximum_ops_per_instruction)
21124 * m_line_header->minimum_instruction_length);
21125 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21126 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
21127 % m_line_header->maximum_ops_per_instruction);
d9b3de22 21128
6f77053d
PA
21129 int line_delta = (m_line_header->line_base
21130 + (adj_opcode % m_line_header->line_range));
21131 advance_line (line_delta);
21132 record_line (false);
21133 m_discriminator = 0;
21134}
d9b3de22 21135
6f77053d
PA
21136void
21137lnp_state_machine::handle_set_file (file_name_index file)
21138{
21139 m_file = file;
21140
21141 const file_entry *fe = current_file ();
21142 if (fe == NULL)
21143 dwarf2_debug_line_missing_file_complaint ();
21144 else if (m_record_lines_p)
21145 {
21146 const char *dir = fe->include_dir (m_line_header);
21147
c24bdb02 21148 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21149 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 21150 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
21151 }
21152}
21153
21154void
21155lnp_state_machine::handle_const_add_pc ()
21156{
21157 CORE_ADDR adjust
21158 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21159
21160 CORE_ADDR addr_adj
21161 = (((m_op_index + adjust)
21162 / m_line_header->maximum_ops_per_instruction)
21163 * m_line_header->minimum_instruction_length);
21164
21165 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21166 m_op_index = ((m_op_index + adjust)
21167 % m_line_header->maximum_ops_per_instruction);
21168}
d9b3de22 21169
a05a36a5
DE
21170/* Return non-zero if we should add LINE to the line number table.
21171 LINE is the line to add, LAST_LINE is the last line that was added,
21172 LAST_SUBFILE is the subfile for LAST_LINE.
21173 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21174 had a non-zero discriminator.
21175
21176 We have to be careful in the presence of discriminators.
21177 E.g., for this line:
21178
21179 for (i = 0; i < 100000; i++);
21180
21181 clang can emit four line number entries for that one line,
21182 each with a different discriminator.
21183 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21184
21185 However, we want gdb to coalesce all four entries into one.
21186 Otherwise the user could stepi into the middle of the line and
21187 gdb would get confused about whether the pc really was in the
21188 middle of the line.
21189
21190 Things are further complicated by the fact that two consecutive
21191 line number entries for the same line is a heuristic used by gcc
21192 to denote the end of the prologue. So we can't just discard duplicate
21193 entries, we have to be selective about it. The heuristic we use is
21194 that we only collapse consecutive entries for the same line if at least
21195 one of those entries has a non-zero discriminator. PR 17276.
21196
21197 Note: Addresses in the line number state machine can never go backwards
21198 within one sequence, thus this coalescing is ok. */
21199
21200static int
804d2729
TT
21201dwarf_record_line_p (struct dwarf2_cu *cu,
21202 unsigned int line, unsigned int last_line,
a05a36a5
DE
21203 int line_has_non_zero_discriminator,
21204 struct subfile *last_subfile)
21205{
c24bdb02 21206 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
21207 return 1;
21208 if (line != last_line)
21209 return 1;
21210 /* Same line for the same file that we've seen already.
21211 As a last check, for pr 17276, only record the line if the line
21212 has never had a non-zero discriminator. */
21213 if (!line_has_non_zero_discriminator)
21214 return 1;
21215 return 0;
21216}
21217
804d2729
TT
21218/* Use the CU's builder to record line number LINE beginning at
21219 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
21220
21221static void
d9b3de22
DE
21222dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21223 unsigned int line, CORE_ADDR address,
804d2729 21224 struct dwarf2_cu *cu)
252a6764
DE
21225{
21226 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21227
27e0867f
DE
21228 if (dwarf_line_debug)
21229 {
21230 fprintf_unfiltered (gdb_stdlog,
21231 "Recording line %u, file %s, address %s\n",
21232 line, lbasename (subfile->name),
21233 paddress (gdbarch, address));
21234 }
21235
804d2729 21236 if (cu != nullptr)
c24bdb02 21237 cu->get_builder ()->record_line (subfile, line, addr);
252a6764
DE
21238}
21239
21240/* Subroutine of dwarf_decode_lines_1 to simplify it.
21241 Mark the end of a set of line number records.
d9b3de22 21242 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21243 If SUBFILE is NULL the request is ignored. */
21244
21245static void
21246dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 21247 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 21248{
27e0867f
DE
21249 if (subfile == NULL)
21250 return;
21251
21252 if (dwarf_line_debug)
21253 {
21254 fprintf_unfiltered (gdb_stdlog,
21255 "Finishing current line, file %s, address %s\n",
21256 lbasename (subfile->name),
21257 paddress (gdbarch, address));
21258 }
21259
804d2729 21260 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
d9b3de22
DE
21261}
21262
6f77053d
PA
21263void
21264lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21265{
d9b3de22
DE
21266 if (dwarf_line_debug)
21267 {
21268 fprintf_unfiltered (gdb_stdlog,
21269 "Processing actual line %u: file %u,"
21270 " address %s, is_stmt %u, discrim %u\n",
7ba99d21 21271 m_line, m_file,
6f77053d
PA
21272 paddress (m_gdbarch, m_address),
21273 m_is_stmt, m_discriminator);
d9b3de22
DE
21274 }
21275
6f77053d 21276 file_entry *fe = current_file ();
8c43009f
PA
21277
21278 if (fe == NULL)
d9b3de22
DE
21279 dwarf2_debug_line_missing_file_complaint ();
21280 /* For now we ignore lines not starting on an instruction boundary.
21281 But not when processing end_sequence for compatibility with the
21282 previous version of the code. */
6f77053d 21283 else if (m_op_index == 0 || end_sequence)
d9b3de22 21284 {
8c43009f 21285 fe->included_p = 1;
c258c396 21286 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
d9b3de22 21287 {
c24bdb02 21288 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 21289 || end_sequence)
d9b3de22 21290 {
804d2729
TT
21291 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21292 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21293 }
21294
21295 if (!end_sequence)
21296 {
804d2729 21297 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21298 m_line_has_non_zero_discriminator,
21299 m_last_subfile))
d9b3de22 21300 {
c24bdb02 21301 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21302 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21303 builder->get_current_subfile (),
6f77053d 21304 m_line, m_address,
804d2729 21305 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21306 }
c24bdb02 21307 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21308 m_last_line = m_line;
d9b3de22
DE
21309 }
21310 }
21311 }
21312}
21313
804d2729
TT
21314lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21315 line_header *lh, bool record_lines_p)
d9b3de22 21316{
804d2729 21317 m_cu = cu;
6f77053d
PA
21318 m_gdbarch = arch;
21319 m_record_lines_p = record_lines_p;
21320 m_line_header = lh;
d9b3de22 21321
804d2729 21322 m_currently_recording_lines = true;
d9b3de22 21323
d9b3de22
DE
21324 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21325 was a line entry for it so that the backend has a chance to adjust it
21326 and also record it in case it needs it. This is currently used by MIPS
21327 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21328 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21329 m_is_stmt = lh->default_is_stmt;
21330 m_discriminator = 0;
252a6764
DE
21331}
21332
6f77053d
PA
21333void
21334lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21335 const gdb_byte *line_ptr,
7ab6656f 21336 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21337{
7ab6656f
OJ
21338 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21339 the pc range of the CU. However, we restrict the test to only ADDRESS
21340 values of zero to preserve GDB's previous behaviour which is to handle
21341 the specific case of a function being GC'd by the linker. */
924c2928 21342
7ab6656f 21343 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
21344 {
21345 /* This line table is for a function which has been
21346 GCd by the linker. Ignore it. PR gdb/12528 */
21347
518817b3 21348 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
21349 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21350
b98664d3 21351 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21352 line_offset, objfile_name (objfile));
804d2729
TT
21353 m_currently_recording_lines = false;
21354 /* Note: m_currently_recording_lines is left as false until we see
21355 DW_LNE_end_sequence. */
924c2928
DE
21356 }
21357}
21358
f3f5162e 21359/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21360 Process the line number information in LH.
21361 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21362 program in order to set included_p for every referenced header. */
debd256d 21363
c906108c 21364static void
43f3e411
DE
21365dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21366 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21367{
d521ce57
TT
21368 const gdb_byte *line_ptr, *extended_end;
21369 const gdb_byte *line_end;
a8c50c1f 21370 unsigned int bytes_read, extended_len;
699ca60a 21371 unsigned char op_code, extended_op;
e142c38c 21372 CORE_ADDR baseaddr;
518817b3 21373 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21374 bfd *abfd = objfile->obfd;
fbf65064 21375 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
21376 /* True if we're recording line info (as opposed to building partial
21377 symtabs and just interested in finding include files mentioned by
21378 the line number program). */
21379 bool record_lines_p = !decode_for_pst_p;
e142c38c 21380
6a053cb1 21381 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
c906108c 21382
debd256d
JB
21383 line_ptr = lh->statement_program_start;
21384 line_end = lh->statement_program_end;
c906108c
SS
21385
21386 /* Read the statement sequences until there's nothing left. */
21387 while (line_ptr < line_end)
21388 {
6f77053d
PA
21389 /* The DWARF line number program state machine. Reset the state
21390 machine at the start of each sequence. */
804d2729 21391 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21392 bool end_sequence = false;
d9b3de22 21393
8c43009f 21394 if (record_lines_p)
c906108c 21395 {
8c43009f
PA
21396 /* Start a subfile for the current file of the state
21397 machine. */
21398 const file_entry *fe = state_machine.current_file ();
21399
21400 if (fe != NULL)
804d2729 21401 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21402 }
21403
a738430d 21404 /* Decode the table. */
d9b3de22 21405 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21406 {
21407 op_code = read_1_byte (abfd, line_ptr);
21408 line_ptr += 1;
9aa1fe7e 21409
debd256d 21410 if (op_code >= lh->opcode_base)
6e70227d 21411 {
8e07a239 21412 /* Special opcode. */
6f77053d 21413 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21414 }
21415 else switch (op_code)
c906108c
SS
21416 {
21417 case DW_LNS_extended_op:
3e43a32a
MS
21418 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21419 &bytes_read);
473b7be6 21420 line_ptr += bytes_read;
a8c50c1f 21421 extended_end = line_ptr + extended_len;
c906108c
SS
21422 extended_op = read_1_byte (abfd, line_ptr);
21423 line_ptr += 1;
21424 switch (extended_op)
21425 {
21426 case DW_LNE_end_sequence:
6f77053d
PA
21427 state_machine.handle_end_sequence ();
21428 end_sequence = true;
c906108c
SS
21429 break;
21430 case DW_LNE_set_address:
d9b3de22
DE
21431 {
21432 CORE_ADDR address
21433 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 21434 line_ptr += bytes_read;
6f77053d
PA
21435
21436 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21437 lowpc - baseaddr, address);
6f77053d 21438 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21439 }
c906108c
SS
21440 break;
21441 case DW_LNE_define_file:
debd256d 21442 {
d521ce57 21443 const char *cur_file;
ecfb656c
PA
21444 unsigned int mod_time, length;
21445 dir_index dindex;
6e70227d 21446
3e43a32a
MS
21447 cur_file = read_direct_string (abfd, line_ptr,
21448 &bytes_read);
debd256d 21449 line_ptr += bytes_read;
ecfb656c 21450 dindex = (dir_index)
debd256d
JB
21451 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21452 line_ptr += bytes_read;
21453 mod_time =
21454 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21455 line_ptr += bytes_read;
21456 length =
21457 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21458 line_ptr += bytes_read;
ecfb656c 21459 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 21460 }
c906108c 21461 break;
d0c6ba3d 21462 case DW_LNE_set_discriminator:
6f77053d
PA
21463 {
21464 /* The discriminator is not interesting to the
21465 debugger; just ignore it. We still need to
21466 check its value though:
21467 if there are consecutive entries for the same
21468 (non-prologue) line we want to coalesce them.
21469 PR 17276. */
21470 unsigned int discr
21471 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21472 line_ptr += bytes_read;
21473
21474 state_machine.handle_set_discriminator (discr);
21475 }
d0c6ba3d 21476 break;
c906108c 21477 default:
b98664d3 21478 complaint (_("mangled .debug_line section"));
debd256d 21479 return;
c906108c 21480 }
a8c50c1f
DJ
21481 /* Make sure that we parsed the extended op correctly. If e.g.
21482 we expected a different address size than the producer used,
21483 we may have read the wrong number of bytes. */
21484 if (line_ptr != extended_end)
21485 {
b98664d3 21486 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21487 return;
21488 }
c906108c
SS
21489 break;
21490 case DW_LNS_copy:
6f77053d 21491 state_machine.handle_copy ();
c906108c
SS
21492 break;
21493 case DW_LNS_advance_pc:
2dc7f7b3
TT
21494 {
21495 CORE_ADDR adjust
21496 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21497 line_ptr += bytes_read;
6f77053d
PA
21498
21499 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21500 }
c906108c
SS
21501 break;
21502 case DW_LNS_advance_line:
a05a36a5
DE
21503 {
21504 int line_delta
21505 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21506 line_ptr += bytes_read;
6f77053d
PA
21507
21508 state_machine.handle_advance_line (line_delta);
a05a36a5 21509 }
c906108c
SS
21510 break;
21511 case DW_LNS_set_file:
d9b3de22 21512 {
6f77053d 21513 file_name_index file
ecfb656c
PA
21514 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21515 &bytes_read);
d9b3de22 21516 line_ptr += bytes_read;
8c43009f 21517
6f77053d 21518 state_machine.handle_set_file (file);
d9b3de22 21519 }
c906108c
SS
21520 break;
21521 case DW_LNS_set_column:
0ad93d4f 21522 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21523 line_ptr += bytes_read;
21524 break;
21525 case DW_LNS_negate_stmt:
6f77053d 21526 state_machine.handle_negate_stmt ();
c906108c
SS
21527 break;
21528 case DW_LNS_set_basic_block:
c906108c 21529 break;
c2c6d25f
JM
21530 /* Add to the address register of the state machine the
21531 address increment value corresponding to special opcode
a738430d
MK
21532 255. I.e., this value is scaled by the minimum
21533 instruction length since special opcode 255 would have
b021a221 21534 scaled the increment. */
c906108c 21535 case DW_LNS_const_add_pc:
6f77053d 21536 state_machine.handle_const_add_pc ();
c906108c
SS
21537 break;
21538 case DW_LNS_fixed_advance_pc:
3e29f34a 21539 {
6f77053d 21540 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21541 line_ptr += 2;
6f77053d
PA
21542
21543 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21544 }
c906108c 21545 break;
9aa1fe7e 21546 default:
a738430d
MK
21547 {
21548 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21549 int i;
a738430d 21550
debd256d 21551 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21552 {
21553 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21554 line_ptr += bytes_read;
21555 }
21556 }
c906108c
SS
21557 }
21558 }
d9b3de22
DE
21559
21560 if (!end_sequence)
21561 dwarf2_debug_line_missing_end_sequence_complaint ();
21562
21563 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21564 in which case we still finish recording the last line). */
6f77053d 21565 state_machine.record_line (true);
c906108c 21566 }
f3f5162e
DE
21567}
21568
21569/* Decode the Line Number Program (LNP) for the given line_header
21570 structure and CU. The actual information extracted and the type
21571 of structures created from the LNP depends on the value of PST.
21572
21573 1. If PST is NULL, then this procedure uses the data from the program
21574 to create all necessary symbol tables, and their linetables.
21575
21576 2. If PST is not NULL, this procedure reads the program to determine
21577 the list of files included by the unit represented by PST, and
21578 builds all the associated partial symbol tables.
21579
21580 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21581 It is used for relative paths in the line table.
21582 NOTE: When processing partial symtabs (pst != NULL),
21583 comp_dir == pst->dirname.
21584
21585 NOTE: It is important that psymtabs have the same file name (via strcmp)
21586 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21587 symtab we don't use it in the name of the psymtabs we create.
21588 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21589 A good testcase for this is mb-inline.exp.
21590
527f3840
JK
21591 LOWPC is the lowest address in CU (or 0 if not known).
21592
21593 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21594 for its PC<->lines mapping information. Otherwise only the filename
21595 table is read in. */
f3f5162e
DE
21596
21597static void
21598dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 21599 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 21600 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21601{
518817b3 21602 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21603 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21604
527f3840
JK
21605 if (decode_mapping)
21606 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21607
21608 if (decode_for_pst_p)
21609 {
aaa75496
JB
21610 /* Now that we're done scanning the Line Header Program, we can
21611 create the psymtab of each included file. */
7ba99d21
AT
21612 for (auto &file_entry : lh->file_names ())
21613 if (file_entry.included_p == 1)
aaa75496 21614 {
c89b44cd 21615 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21616 const char *include_name =
7ba99d21
AT
21617 psymtab_include_file_name (lh, file_entry, pst,
21618 comp_dir, &name_holder);
c6da4cef 21619 if (include_name != NULL)
aaa75496
JB
21620 dwarf2_create_include_psymtab (include_name, pst, objfile);
21621 }
21622 }
cb1df416
DJ
21623 else
21624 {
21625 /* Make sure a symtab is created for every file, even files
21626 which contain only variables (i.e. no code with associated
21627 line numbers). */
c24bdb02
KS
21628 buildsym_compunit *builder = cu->get_builder ();
21629 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21630
7ba99d21 21631 for (auto &fe : lh->file_names ())
cb1df416 21632 {
804d2729 21633 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21634 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21635 {
c24bdb02 21636 builder->get_current_subfile ()->symtab
804d2729 21637 = allocate_symtab (cust,
c24bdb02 21638 builder->get_current_subfile ()->name);
43f3e411 21639 }
c24bdb02 21640 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21641 }
21642 }
c906108c
SS
21643}
21644
21645/* Start a subfile for DWARF. FILENAME is the name of the file and
21646 DIRNAME the name of the source directory which contains FILENAME
4d663531 21647 or NULL if not known.
c906108c
SS
21648 This routine tries to keep line numbers from identical absolute and
21649 relative file names in a common subfile.
21650
21651 Using the `list' example from the GDB testsuite, which resides in
21652 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21653 of /srcdir/list0.c yields the following debugging information for list0.c:
21654
c5aa993b 21655 DW_AT_name: /srcdir/list0.c
4d663531 21656 DW_AT_comp_dir: /compdir
357e46e7 21657 files.files[0].name: list0.h
c5aa993b 21658 files.files[0].dir: /srcdir
357e46e7 21659 files.files[1].name: list0.c
c5aa993b 21660 files.files[1].dir: /srcdir
c906108c
SS
21661
21662 The line number information for list0.c has to end up in a single
4f1520fb
FR
21663 subfile, so that `break /srcdir/list0.c:1' works as expected.
21664 start_subfile will ensure that this happens provided that we pass the
21665 concatenation of files.files[1].dir and files.files[1].name as the
21666 subfile's name. */
c906108c
SS
21667
21668static void
804d2729
TT
21669dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21670 const char *dirname)
c906108c 21671{
43816ebc 21672 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 21673
4d663531 21674 /* In order not to lose the line information directory,
4f1520fb
FR
21675 we concatenate it to the filename when it makes sense.
21676 Note that the Dwarf3 standard says (speaking of filenames in line
21677 information): ``The directory index is ignored for file names
21678 that represent full path names''. Thus ignoring dirname in the
21679 `else' branch below isn't an issue. */
c906108c 21680
d5166ae1 21681 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 21682 {
43816ebc
TT
21683 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21684 filename = copy.get ();
d521ce57 21685 }
c906108c 21686
c24bdb02 21687 cu->get_builder ()->start_subfile (filename);
c906108c
SS
21688}
21689
804d2729
TT
21690/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21691 buildsym_compunit constructor. */
f4dc4d17 21692
c24bdb02
KS
21693struct compunit_symtab *
21694dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21695 CORE_ADDR low_pc)
f4dc4d17 21696{
c24bdb02 21697 gdb_assert (m_builder == nullptr);
43f3e411 21698
c24bdb02
KS
21699 m_builder.reset (new struct buildsym_compunit
21700 (per_cu->dwarf2_per_objfile->objfile,
21701 name, comp_dir, language, low_pc));
93b8bea4 21702
c24bdb02 21703 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21704
c24bdb02
KS
21705 get_builder ()->record_debugformat ("DWARF 2");
21706 get_builder ()->record_producer (producer);
f4dc4d17 21707
c24bdb02 21708 processing_has_namespace_info = false;
43f3e411 21709
c24bdb02 21710 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21711}
21712
4c2df51b
DJ
21713static void
21714var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21715 struct dwarf2_cu *cu)
4c2df51b 21716{
518817b3 21717 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21718 struct comp_unit_head *cu_header = &cu->header;
21719
4c2df51b
DJ
21720 /* NOTE drow/2003-01-30: There used to be a comment and some special
21721 code here to turn a symbol with DW_AT_external and a
21722 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21723 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21724 with some versions of binutils) where shared libraries could have
21725 relocations against symbols in their debug information - the
21726 minimal symbol would have the right address, but the debug info
21727 would not. It's no longer necessary, because we will explicitly
21728 apply relocations when we read in the debug information now. */
21729
21730 /* A DW_AT_location attribute with no contents indicates that a
21731 variable has been optimized away. */
21732 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21733 {
f1e6e072 21734 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21735 return;
21736 }
21737
21738 /* Handle one degenerate form of location expression specially, to
21739 preserve GDB's previous behavior when section offsets are
336d760d
AT
21740 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21741 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21742
21743 if (attr_form_is_block (attr)
3019eac3
DE
21744 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21745 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
21746 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21747 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
21748 && (DW_BLOCK (attr)->size
21749 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21750 {
891d2f0b 21751 unsigned int dummy;
4c2df51b 21752
3019eac3 21753 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
38583298
TT
21754 SET_SYMBOL_VALUE_ADDRESS (sym,
21755 read_address (objfile->obfd,
21756 DW_BLOCK (attr)->data + 1,
21757 cu, &dummy));
3019eac3 21758 else
38583298
TT
21759 SET_SYMBOL_VALUE_ADDRESS
21760 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21761 &dummy));
f1e6e072 21762 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 21763 fixup_symbol_section (sym, objfile);
6a053cb1
TT
21764 SET_SYMBOL_VALUE_ADDRESS
21765 (sym,
21766 SYMBOL_VALUE_ADDRESS (sym)
21767 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
21768 return;
21769 }
21770
21771 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21772 expression evaluator, and use LOC_COMPUTED only when necessary
21773 (i.e. when the value of a register or memory location is
21774 referenced, or a thread-local block, etc.). Then again, it might
21775 not be worthwhile. I'm assuming that it isn't unless performance
21776 or memory numbers show me otherwise. */
21777
f1e6e072 21778 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21779
f1e6e072 21780 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21781 cu->has_loclist = true;
4c2df51b
DJ
21782}
21783
c906108c
SS
21784/* Given a pointer to a DWARF information entry, figure out if we need
21785 to make a symbol table entry for it, and if so, create a new entry
21786 and return a pointer to it.
21787 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21788 used the passed type.
21789 If SPACE is not NULL, use it to hold the new symbol. If it is
21790 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21791
21792static struct symbol *
5e2db402
TT
21793new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21794 struct symbol *space)
c906108c 21795{
518817b3
SM
21796 struct dwarf2_per_objfile *dwarf2_per_objfile
21797 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21798 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21799 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21800 struct symbol *sym = NULL;
15d034d0 21801 const char *name;
c906108c
SS
21802 struct attribute *attr = NULL;
21803 struct attribute *attr2 = NULL;
e142c38c 21804 CORE_ADDR baseaddr;
e37fd15a
SW
21805 struct pending **list_to_add = NULL;
21806
edb3359d 21807 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 21808
6a053cb1 21809 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
c906108c 21810
94af9270 21811 name = dwarf2_name (die, cu);
c906108c
SS
21812 if (name)
21813 {
94af9270 21814 const char *linkagename;
34eaf542 21815 int suppress_add = 0;
94af9270 21816
34eaf542
TT
21817 if (space)
21818 sym = space;
21819 else
e623cf5d 21820 sym = allocate_symbol (objfile);
c906108c 21821 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21822
21823 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 21824 sym->set_language (cu->language, &objfile->objfile_obstack);
94af9270 21825 linkagename = dwarf2_physname (name, die, cu);
4d4eaa30 21826 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
c906108c 21827
f55ee35c
JK
21828 /* Fortran does not have mangling standard and the mangling does differ
21829 between gfortran, iFort etc. */
21830 if (cu->language == language_fortran
468c0cbb
CB
21831 && symbol_get_demangled_name (sym) == NULL)
21832 symbol_set_demangled_name (sym,
cfc594ee 21833 dwarf2_full_name (name, die, cu),
29df156d 21834 NULL);
f55ee35c 21835
c906108c 21836 /* Default assumptions.
c5aa993b 21837 Use the passed type or decode it from the die. */
176620f1 21838 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21839 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21840 if (type != NULL)
21841 SYMBOL_TYPE (sym) = type;
21842 else
e7c27a73 21843 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21844 attr = dwarf2_attr (die,
21845 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21846 cu);
435d3d88 21847 if (attr != nullptr)
c906108c
SS
21848 {
21849 SYMBOL_LINE (sym) = DW_UNSND (attr);
21850 }
cb1df416 21851
edb3359d
DJ
21852 attr = dwarf2_attr (die,
21853 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21854 cu);
435d3d88 21855 if (attr != nullptr)
cb1df416 21856 {
ecfb656c 21857 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21858 struct file_entry *fe;
9a619af0 21859
ecfb656c
PA
21860 if (cu->line_header != NULL)
21861 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21862 else
21863 fe = NULL;
21864
21865 if (fe == NULL)
b98664d3 21866 complaint (_("file index out of range"));
8c43009f
PA
21867 else
21868 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21869 }
21870
c906108c
SS
21871 switch (die->tag)
21872 {
21873 case DW_TAG_label:
e142c38c 21874 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 21875 if (attr != nullptr)
3e29f34a
MR
21876 {
21877 CORE_ADDR addr;
21878
21879 addr = attr_value_as_address (attr);
21880 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 21881 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 21882 }
0f5238ed
TT
21883 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21884 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21885 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 21886 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21887 break;
21888 case DW_TAG_subprogram:
21889 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21890 finish_block. */
f1e6e072 21891 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21892 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 21893 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
21894 || cu->language == language_ada
21895 || cu->language == language_fortran)
c906108c 21896 {
2cfa0c8d 21897 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
21898 Ada and Fortran subprograms, whether marked external or
21899 not, are always stored as a global symbol, because we want
21900 to be able to access them globally. For instance, we want
21901 to be able to break on a nested subprogram without having
21902 to specify the context. */
c24bdb02 21903 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21904 }
21905 else
21906 {
e37fd15a 21907 list_to_add = cu->list_in_scope;
c906108c
SS
21908 }
21909 break;
edb3359d
DJ
21910 case DW_TAG_inlined_subroutine:
21911 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21912 finish_block. */
f1e6e072 21913 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21914 SYMBOL_INLINED (sym) = 1;
481860b3 21915 list_to_add = cu->list_in_scope;
edb3359d 21916 break;
34eaf542
TT
21917 case DW_TAG_template_value_param:
21918 suppress_add = 1;
21919 /* Fall through. */
72929c62 21920 case DW_TAG_constant:
c906108c 21921 case DW_TAG_variable:
254e6b9e 21922 case DW_TAG_member:
0963b4bd
MS
21923 /* Compilation with minimal debug info may result in
21924 variables with missing type entries. Change the
21925 misleading `void' type to something sensible. */
c906108c 21926 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21927 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21928
e142c38c 21929 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21930 /* In the case of DW_TAG_member, we should only be called for
21931 static const members. */
21932 if (die->tag == DW_TAG_member)
21933 {
3863f96c
DE
21934 /* dwarf2_add_field uses die_is_declaration,
21935 so we do the same. */
254e6b9e
DE
21936 gdb_assert (die_is_declaration (die, cu));
21937 gdb_assert (attr);
21938 }
435d3d88 21939 if (attr != nullptr)
c906108c 21940 {
e7c27a73 21941 dwarf2_const_value (attr, sym, cu);
e142c38c 21942 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21943 if (!suppress_add)
34eaf542
TT
21944 {
21945 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 21946 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21947 else
e37fd15a 21948 list_to_add = cu->list_in_scope;
34eaf542 21949 }
c906108c
SS
21950 break;
21951 }
e142c38c 21952 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21953 if (attr != nullptr)
c906108c 21954 {
e7c27a73 21955 var_decode_location (attr, sym, cu);
e142c38c 21956 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21957
21958 /* Fortran explicitly imports any global symbols to the local
21959 scope by DW_TAG_common_block. */
21960 if (cu->language == language_fortran && die->parent
21961 && die->parent->tag == DW_TAG_common_block)
21962 attr2 = NULL;
21963
caac4577
JG
21964 if (SYMBOL_CLASS (sym) == LOC_STATIC
21965 && SYMBOL_VALUE_ADDRESS (sym) == 0
21966 && !dwarf2_per_objfile->has_section_at_zero)
21967 {
21968 /* When a static variable is eliminated by the linker,
21969 the corresponding debug information is not stripped
21970 out, but the variable address is set to null;
21971 do not add such variables into symbol table. */
21972 }
21973 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21974 {
4b610737
TT
21975 if (SYMBOL_CLASS (sym) == LOC_STATIC
21976 && (objfile->flags & OBJF_MAINLINE) == 0
21977 && dwarf2_per_objfile->can_copy)
21978 {
21979 /* A global static variable might be subject to
21980 copy relocation. We first check for a local
21981 minsym, though, because maybe the symbol was
21982 marked hidden, in which case this would not
21983 apply. */
21984 bound_minimal_symbol found
21985 = (lookup_minimal_symbol_linkage
987012b8 21986 (sym->linkage_name (), objfile));
4b610737
TT
21987 if (found.minsym != nullptr)
21988 sym->maybe_copied = 1;
21989 }
f55ee35c 21990
1c809c68
TT
21991 /* A variable with DW_AT_external is never static,
21992 but it may be block-scoped. */
804d2729 21993 list_to_add
c24bdb02
KS
21994 = ((cu->list_in_scope
21995 == cu->get_builder ()->get_file_symbols ())
21996 ? cu->get_builder ()->get_global_symbols ()
804d2729 21997 : cu->list_in_scope);
1c809c68 21998 }
c906108c 21999 else
e37fd15a 22000 list_to_add = cu->list_in_scope;
c906108c
SS
22001 }
22002 else
22003 {
22004 /* We do not know the address of this symbol.
c5aa993b
JM
22005 If it is an external symbol and we have type information
22006 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22007 The address of the variable will then be determined from
22008 the minimal symbol table whenever the variable is
22009 referenced. */
e142c38c 22010 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
22011
22012 /* Fortran explicitly imports any global symbols to the local
22013 scope by DW_TAG_common_block. */
22014 if (cu->language == language_fortran && die->parent
22015 && die->parent->tag == DW_TAG_common_block)
22016 {
22017 /* SYMBOL_CLASS doesn't matter here because
22018 read_common_block is going to reset it. */
22019 if (!suppress_add)
22020 list_to_add = cu->list_in_scope;
22021 }
22022 else if (attr2 && (DW_UNSND (attr2) != 0)
22023 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 22024 {
0fe7935b
DJ
22025 /* A variable with DW_AT_external is never static, but it
22026 may be block-scoped. */
804d2729 22027 list_to_add
c24bdb02
KS
22028 = ((cu->list_in_scope
22029 == cu->get_builder ()->get_file_symbols ())
22030 ? cu->get_builder ()->get_global_symbols ()
804d2729 22031 : cu->list_in_scope);
0fe7935b 22032
f1e6e072 22033 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 22034 }
442ddf59
JK
22035 else if (!die_is_declaration (die, cu))
22036 {
22037 /* Use the default LOC_OPTIMIZED_OUT class. */
22038 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
22039 if (!suppress_add)
22040 list_to_add = cu->list_in_scope;
442ddf59 22041 }
c906108c
SS
22042 }
22043 break;
22044 case DW_TAG_formal_parameter:
a60f3166
TT
22045 {
22046 /* If we are inside a function, mark this as an argument. If
22047 not, we might be looking at an argument to an inlined function
22048 when we do not have enough information to show inlined frames;
22049 pretend it's a local variable in that case so that the user can
22050 still see it. */
804d2729 22051 struct context_stack *curr
c24bdb02 22052 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
22053 if (curr != nullptr && curr->name != nullptr)
22054 SYMBOL_IS_ARGUMENT (sym) = 1;
22055 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22056 if (attr != nullptr)
a60f3166
TT
22057 {
22058 var_decode_location (attr, sym, cu);
22059 }
22060 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22061 if (attr != nullptr)
a60f3166
TT
22062 {
22063 dwarf2_const_value (attr, sym, cu);
22064 }
f346a30d 22065
a60f3166
TT
22066 list_to_add = cu->list_in_scope;
22067 }
c906108c
SS
22068 break;
22069 case DW_TAG_unspecified_parameters:
22070 /* From varargs functions; gdb doesn't seem to have any
22071 interest in this information, so just ignore it for now.
22072 (FIXME?) */
22073 break;
34eaf542
TT
22074 case DW_TAG_template_type_param:
22075 suppress_add = 1;
22076 /* Fall through. */
c906108c 22077 case DW_TAG_class_type:
680b30c7 22078 case DW_TAG_interface_type:
c906108c
SS
22079 case DW_TAG_structure_type:
22080 case DW_TAG_union_type:
72019c9c 22081 case DW_TAG_set_type:
c906108c 22082 case DW_TAG_enumeration_type:
f1e6e072 22083 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22084 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 22085
63d06c5c 22086 {
9c37b5ae 22087 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
22088 really ever be static objects: otherwise, if you try
22089 to, say, break of a class's method and you're in a file
22090 which doesn't mention that class, it won't work unless
22091 the check for all static symbols in lookup_symbol_aux
22092 saves you. See the OtherFileClass tests in
22093 gdb.c++/namespace.exp. */
22094
e37fd15a 22095 if (!suppress_add)
34eaf542 22096 {
c24bdb02 22097 buildsym_compunit *builder = cu->get_builder ();
804d2729 22098 list_to_add
c24bdb02 22099 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 22100 && cu->language == language_cplus
c24bdb02 22101 ? builder->get_global_symbols ()
804d2729 22102 : cu->list_in_scope);
63d06c5c 22103
64382290 22104 /* The semantics of C++ state that "struct foo {
9c37b5ae 22105 ... }" also defines a typedef for "foo". */
64382290 22106 if (cu->language == language_cplus
45280282 22107 || cu->language == language_ada
c44af4eb
TT
22108 || cu->language == language_d
22109 || cu->language == language_rust)
64382290
TT
22110 {
22111 /* The symbol's name is already allocated along
22112 with this objfile, so we don't need to
22113 duplicate it for the type. */
22114 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
987012b8 22115 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
64382290 22116 }
63d06c5c
DC
22117 }
22118 }
c906108c
SS
22119 break;
22120 case DW_TAG_typedef:
f1e6e072 22121 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 22122 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22123 list_to_add = cu->list_in_scope;
63d06c5c 22124 break;
c906108c 22125 case DW_TAG_base_type:
a02abb62 22126 case DW_TAG_subrange_type:
f1e6e072 22127 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22128 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22129 list_to_add = cu->list_in_scope;
c906108c
SS
22130 break;
22131 case DW_TAG_enumerator:
e142c38c 22132 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22133 if (attr != nullptr)
c906108c 22134 {
e7c27a73 22135 dwarf2_const_value (attr, sym, cu);
c906108c 22136 }
63d06c5c
DC
22137 {
22138 /* NOTE: carlton/2003-11-10: See comment above in the
22139 DW_TAG_class_type, etc. block. */
22140
804d2729 22141 list_to_add
c24bdb02 22142 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 22143 && cu->language == language_cplus
c24bdb02 22144 ? cu->get_builder ()->get_global_symbols ()
804d2729 22145 : cu->list_in_scope);
63d06c5c 22146 }
c906108c 22147 break;
74921315 22148 case DW_TAG_imported_declaration:
5c4e30ca 22149 case DW_TAG_namespace:
f1e6e072 22150 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 22151 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 22152 break;
530e8392
KB
22153 case DW_TAG_module:
22154 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22155 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 22156 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 22157 break;
4357ac6c 22158 case DW_TAG_common_block:
f1e6e072 22159 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 22160 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 22161 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 22162 break;
c906108c
SS
22163 default:
22164 /* Not a tag we recognize. Hopefully we aren't processing
22165 trash data, but since we must specifically ignore things
22166 we don't recognize, there is nothing else we should do at
0963b4bd 22167 this point. */
b98664d3 22168 complaint (_("unsupported tag: '%s'"),
4d3c2250 22169 dwarf_tag_name (die->tag));
c906108c
SS
22170 break;
22171 }
df8a16a1 22172
e37fd15a
SW
22173 if (suppress_add)
22174 {
22175 sym->hash_next = objfile->template_symbols;
22176 objfile->template_symbols = sym;
22177 list_to_add = NULL;
22178 }
22179
22180 if (list_to_add != NULL)
d3cb6808 22181 add_symbol_to_list (sym, list_to_add);
e37fd15a 22182
df8a16a1
DJ
22183 /* For the benefit of old versions of GCC, check for anonymous
22184 namespaces based on the demangled name. */
4d4ec4e5 22185 if (!cu->processing_has_namespace_info
94af9270 22186 && cu->language == language_cplus)
c24bdb02 22187 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
22188 }
22189 return (sym);
22190}
22191
98bfdba5
PA
22192/* Given an attr with a DW_FORM_dataN value in host byte order,
22193 zero-extend it as appropriate for the symbol's type. The DWARF
22194 standard (v4) is not entirely clear about the meaning of using
22195 DW_FORM_dataN for a constant with a signed type, where the type is
22196 wider than the data. The conclusion of a discussion on the DWARF
22197 list was that this is unspecified. We choose to always zero-extend
22198 because that is the interpretation long in use by GCC. */
c906108c 22199
98bfdba5 22200static gdb_byte *
ff39bb5e 22201dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 22202 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 22203{
518817b3 22204 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
22205 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22206 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
22207 LONGEST l = DW_UNSND (attr);
22208
22209 if (bits < sizeof (*value) * 8)
22210 {
22211 l &= ((LONGEST) 1 << bits) - 1;
22212 *value = l;
22213 }
22214 else if (bits == sizeof (*value) * 8)
22215 *value = l;
22216 else
22217 {
224c3ddb 22218 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22219 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22220 return bytes;
22221 }
22222
22223 return NULL;
22224}
22225
22226/* Read a constant value from an attribute. Either set *VALUE, or if
22227 the value does not fit in *VALUE, set *BYTES - either already
22228 allocated on the objfile obstack, or newly allocated on OBSTACK,
22229 or, set *BATON, if we translated the constant to a location
22230 expression. */
22231
22232static void
ff39bb5e 22233dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22234 const char *name, struct obstack *obstack,
22235 struct dwarf2_cu *cu,
d521ce57 22236 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22237 struct dwarf2_locexpr_baton **baton)
22238{
518817b3 22239 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 22240 struct comp_unit_head *cu_header = &cu->header;
c906108c 22241 struct dwarf_block *blk;
98bfdba5
PA
22242 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22243 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22244
22245 *value = 0;
22246 *bytes = NULL;
22247 *baton = NULL;
c906108c
SS
22248
22249 switch (attr->form)
22250 {
22251 case DW_FORM_addr:
336d760d 22252 case DW_FORM_addrx:
3019eac3 22253 case DW_FORM_GNU_addr_index:
ac56253d 22254 {
ac56253d
TT
22255 gdb_byte *data;
22256
98bfdba5
PA
22257 if (TYPE_LENGTH (type) != cu_header->addr_size)
22258 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22259 cu_header->addr_size,
98bfdba5 22260 TYPE_LENGTH (type));
ac56253d
TT
22261 /* Symbols of this form are reasonably rare, so we just
22262 piggyback on the existing location code rather than writing
22263 a new implementation of symbol_computed_ops. */
8d749320 22264 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
22265 (*baton)->per_cu = cu->per_cu;
22266 gdb_assert ((*baton)->per_cu);
ac56253d 22267
98bfdba5 22268 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22269 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22270 (*baton)->data = data;
ac56253d
TT
22271
22272 data[0] = DW_OP_addr;
22273 store_unsigned_integer (&data[1], cu_header->addr_size,
22274 byte_order, DW_ADDR (attr));
22275 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22276 }
c906108c 22277 break;
4ac36638 22278 case DW_FORM_string:
93b5768b 22279 case DW_FORM_strp:
cf532bd1 22280 case DW_FORM_strx:
3019eac3 22281 case DW_FORM_GNU_str_index:
36586728 22282 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
22283 /* DW_STRING is already allocated on the objfile obstack, point
22284 directly to it. */
d521ce57 22285 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 22286 break;
c906108c
SS
22287 case DW_FORM_block1:
22288 case DW_FORM_block2:
22289 case DW_FORM_block4:
22290 case DW_FORM_block:
2dc7f7b3 22291 case DW_FORM_exprloc:
0224619f 22292 case DW_FORM_data16:
c906108c 22293 blk = DW_BLOCK (attr);
98bfdba5
PA
22294 if (TYPE_LENGTH (type) != blk->size)
22295 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22296 TYPE_LENGTH (type));
22297 *bytes = blk->data;
c906108c 22298 break;
2df3850c
JM
22299
22300 /* The DW_AT_const_value attributes are supposed to carry the
22301 symbol's value "represented as it would be on the target
22302 architecture." By the time we get here, it's already been
22303 converted to host endianness, so we just need to sign- or
22304 zero-extend it as appropriate. */
22305 case DW_FORM_data1:
3aef2284 22306 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22307 break;
c906108c 22308 case DW_FORM_data2:
3aef2284 22309 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22310 break;
c906108c 22311 case DW_FORM_data4:
3aef2284 22312 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22313 break;
c906108c 22314 case DW_FORM_data8:
3aef2284 22315 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22316 break;
22317
c906108c 22318 case DW_FORM_sdata:
663c44ac 22319 case DW_FORM_implicit_const:
98bfdba5 22320 *value = DW_SND (attr);
2df3850c
JM
22321 break;
22322
c906108c 22323 case DW_FORM_udata:
98bfdba5 22324 *value = DW_UNSND (attr);
c906108c 22325 break;
2df3850c 22326
c906108c 22327 default:
b98664d3 22328 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22329 dwarf_form_name (attr->form));
98bfdba5 22330 *value = 0;
c906108c
SS
22331 break;
22332 }
22333}
22334
2df3850c 22335
98bfdba5
PA
22336/* Copy constant value from an attribute to a symbol. */
22337
2df3850c 22338static void
ff39bb5e 22339dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22340 struct dwarf2_cu *cu)
2df3850c 22341{
518817b3 22342 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 22343 LONGEST value;
d521ce57 22344 const gdb_byte *bytes;
98bfdba5 22345 struct dwarf2_locexpr_baton *baton;
2df3850c 22346
98bfdba5 22347 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22348 sym->print_name (),
98bfdba5
PA
22349 &objfile->objfile_obstack, cu,
22350 &value, &bytes, &baton);
2df3850c 22351
98bfdba5
PA
22352 if (baton != NULL)
22353 {
98bfdba5 22354 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22355 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22356 }
22357 else if (bytes != NULL)
22358 {
22359 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22360 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22361 }
22362 else
22363 {
22364 SYMBOL_VALUE (sym) = value;
f1e6e072 22365 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22366 }
2df3850c
JM
22367}
22368
c906108c
SS
22369/* Return the type of the die in question using its DW_AT_type attribute. */
22370
22371static struct type *
e7c27a73 22372die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22373{
c906108c 22374 struct attribute *type_attr;
c906108c 22375
e142c38c 22376 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22377 if (!type_attr)
22378 {
518817b3 22379 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22380 /* A missing DW_AT_type represents a void type. */
518817b3 22381 return objfile_type (objfile)->builtin_void;
c906108c 22382 }
348e048f 22383
673bfd45 22384 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22385}
22386
b4ba55a1
JB
22387/* True iff CU's producer generates GNAT Ada auxiliary information
22388 that allows to find parallel types through that information instead
22389 of having to do expensive parallel lookups by type name. */
22390
22391static int
22392need_gnat_info (struct dwarf2_cu *cu)
22393{
de4cb04a
JB
22394 /* Assume that the Ada compiler was GNAT, which always produces
22395 the auxiliary information. */
22396 return (cu->language == language_ada);
b4ba55a1
JB
22397}
22398
b4ba55a1
JB
22399/* Return the auxiliary type of the die in question using its
22400 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22401 attribute is not present. */
22402
22403static struct type *
22404die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22405{
b4ba55a1 22406 struct attribute *type_attr;
b4ba55a1
JB
22407
22408 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22409 if (!type_attr)
22410 return NULL;
22411
673bfd45 22412 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22413}
22414
22415/* If DIE has a descriptive_type attribute, then set the TYPE's
22416 descriptive type accordingly. */
22417
22418static void
22419set_descriptive_type (struct type *type, struct die_info *die,
22420 struct dwarf2_cu *cu)
22421{
22422 struct type *descriptive_type = die_descriptive_type (die, cu);
22423
22424 if (descriptive_type)
22425 {
22426 ALLOCATE_GNAT_AUX_TYPE (type);
22427 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22428 }
22429}
22430
c906108c
SS
22431/* Return the containing type of the die in question using its
22432 DW_AT_containing_type attribute. */
22433
22434static struct type *
e7c27a73 22435die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22436{
c906108c 22437 struct attribute *type_attr;
518817b3 22438 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22439
e142c38c 22440 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22441 if (!type_attr)
22442 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22443 "[in module %s]"), objfile_name (objfile));
33ac96f0 22444
673bfd45 22445 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22446}
22447
ac9ec31b
DE
22448/* Return an error marker type to use for the ill formed type in DIE/CU. */
22449
22450static struct type *
22451build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22452{
518817b3
SM
22453 struct dwarf2_per_objfile *dwarf2_per_objfile
22454 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 22455 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 22456 char *saved;
ac9ec31b 22457
528e1572
SM
22458 std::string message
22459 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22460 objfile_name (objfile),
22461 sect_offset_str (cu->header.sect_off),
22462 sect_offset_str (die->sect_off));
efba19b0 22463 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22464
19f392bc 22465 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22466}
22467
673bfd45 22468/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22469 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22470 DW_AT_containing_type.
673bfd45
DE
22471 If there is no type substitute an error marker. */
22472
c906108c 22473static struct type *
ff39bb5e 22474lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22475 struct dwarf2_cu *cu)
c906108c 22476{
518817b3
SM
22477 struct dwarf2_per_objfile *dwarf2_per_objfile
22478 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22479 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
22480 struct type *this_type;
22481
ac9ec31b
DE
22482 gdb_assert (attr->name == DW_AT_type
22483 || attr->name == DW_AT_GNAT_descriptive_type
22484 || attr->name == DW_AT_containing_type);
22485
673bfd45
DE
22486 /* First see if we have it cached. */
22487
36586728
TT
22488 if (attr->form == DW_FORM_GNU_ref_alt)
22489 {
22490 struct dwarf2_per_cu_data *per_cu;
9c541725 22491 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 22492
ed2dc618
SM
22493 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22494 dwarf2_per_objfile);
9c541725 22495 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 22496 }
7771576e 22497 else if (attr_form_is_ref (attr))
673bfd45 22498 {
9c541725 22499 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 22500
9c541725 22501 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 22502 }
55f1336d 22503 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22504 {
ac9ec31b 22505 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 22506
ac9ec31b 22507 return get_signatured_type (die, signature, cu);
673bfd45
DE
22508 }
22509 else
22510 {
b98664d3 22511 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22512 " at %s [in module %s]"),
22513 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22514 objfile_name (objfile));
ac9ec31b 22515 return build_error_marker_type (cu, die);
673bfd45
DE
22516 }
22517
22518 /* If not cached we need to read it in. */
22519
22520 if (this_type == NULL)
22521 {
ac9ec31b 22522 struct die_info *type_die = NULL;
673bfd45
DE
22523 struct dwarf2_cu *type_cu = cu;
22524
7771576e 22525 if (attr_form_is_ref (attr))
ac9ec31b
DE
22526 type_die = follow_die_ref (die, attr, &type_cu);
22527 if (type_die == NULL)
22528 return build_error_marker_type (cu, die);
22529 /* If we find the type now, it's probably because the type came
3019eac3
DE
22530 from an inter-CU reference and the type's CU got expanded before
22531 ours. */
ac9ec31b 22532 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22533 }
22534
22535 /* If we still don't have a type use an error marker. */
22536
22537 if (this_type == NULL)
ac9ec31b 22538 return build_error_marker_type (cu, die);
673bfd45 22539
f792889a 22540 return this_type;
c906108c
SS
22541}
22542
673bfd45
DE
22543/* Return the type in DIE, CU.
22544 Returns NULL for invalid types.
22545
02142a6c 22546 This first does a lookup in die_type_hash,
673bfd45
DE
22547 and only reads the die in if necessary.
22548
22549 NOTE: This can be called when reading in partial or full symbols. */
22550
f792889a 22551static struct type *
e7c27a73 22552read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22553{
f792889a
DJ
22554 struct type *this_type;
22555
22556 this_type = get_die_type (die, cu);
22557 if (this_type)
22558 return this_type;
22559
673bfd45
DE
22560 return read_type_die_1 (die, cu);
22561}
22562
22563/* Read the type in DIE, CU.
22564 Returns NULL for invalid types. */
22565
22566static struct type *
22567read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22568{
22569 struct type *this_type = NULL;
22570
c906108c
SS
22571 switch (die->tag)
22572 {
22573 case DW_TAG_class_type:
680b30c7 22574 case DW_TAG_interface_type:
c906108c
SS
22575 case DW_TAG_structure_type:
22576 case DW_TAG_union_type:
f792889a 22577 this_type = read_structure_type (die, cu);
c906108c
SS
22578 break;
22579 case DW_TAG_enumeration_type:
f792889a 22580 this_type = read_enumeration_type (die, cu);
c906108c
SS
22581 break;
22582 case DW_TAG_subprogram:
22583 case DW_TAG_subroutine_type:
edb3359d 22584 case DW_TAG_inlined_subroutine:
f792889a 22585 this_type = read_subroutine_type (die, cu);
c906108c
SS
22586 break;
22587 case DW_TAG_array_type:
f792889a 22588 this_type = read_array_type (die, cu);
c906108c 22589 break;
72019c9c 22590 case DW_TAG_set_type:
f792889a 22591 this_type = read_set_type (die, cu);
72019c9c 22592 break;
c906108c 22593 case DW_TAG_pointer_type:
f792889a 22594 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22595 break;
22596 case DW_TAG_ptr_to_member_type:
f792889a 22597 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22598 break;
22599 case DW_TAG_reference_type:
4297a3f0
AV
22600 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22601 break;
22602 case DW_TAG_rvalue_reference_type:
22603 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22604 break;
22605 case DW_TAG_const_type:
f792889a 22606 this_type = read_tag_const_type (die, cu);
c906108c
SS
22607 break;
22608 case DW_TAG_volatile_type:
f792889a 22609 this_type = read_tag_volatile_type (die, cu);
c906108c 22610 break;
06d66ee9
TT
22611 case DW_TAG_restrict_type:
22612 this_type = read_tag_restrict_type (die, cu);
22613 break;
c906108c 22614 case DW_TAG_string_type:
f792889a 22615 this_type = read_tag_string_type (die, cu);
c906108c
SS
22616 break;
22617 case DW_TAG_typedef:
f792889a 22618 this_type = read_typedef (die, cu);
c906108c 22619 break;
a02abb62 22620 case DW_TAG_subrange_type:
f792889a 22621 this_type = read_subrange_type (die, cu);
a02abb62 22622 break;
c906108c 22623 case DW_TAG_base_type:
f792889a 22624 this_type = read_base_type (die, cu);
c906108c 22625 break;
81a17f79 22626 case DW_TAG_unspecified_type:
f792889a 22627 this_type = read_unspecified_type (die, cu);
81a17f79 22628 break;
0114d602
DJ
22629 case DW_TAG_namespace:
22630 this_type = read_namespace_type (die, cu);
22631 break;
f55ee35c
JK
22632 case DW_TAG_module:
22633 this_type = read_module_type (die, cu);
22634 break;
a2c2acaf
MW
22635 case DW_TAG_atomic_type:
22636 this_type = read_tag_atomic_type (die, cu);
22637 break;
c906108c 22638 default:
b98664d3 22639 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22640 dwarf_tag_name (die->tag));
c906108c
SS
22641 break;
22642 }
63d06c5c 22643
f792889a 22644 return this_type;
63d06c5c
DC
22645}
22646
abc72ce4
DE
22647/* See if we can figure out if the class lives in a namespace. We do
22648 this by looking for a member function; its demangled name will
22649 contain namespace info, if there is any.
22650 Return the computed name or NULL.
22651 Space for the result is allocated on the objfile's obstack.
22652 This is the full-die version of guess_partial_die_structure_name.
22653 In this case we know DIE has no useful parent. */
22654
43816ebc 22655static const char *
abc72ce4
DE
22656guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22657{
22658 struct die_info *spec_die;
22659 struct dwarf2_cu *spec_cu;
22660 struct die_info *child;
518817b3 22661 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22662
22663 spec_cu = cu;
22664 spec_die = die_specification (die, &spec_cu);
22665 if (spec_die != NULL)
22666 {
22667 die = spec_die;
22668 cu = spec_cu;
22669 }
22670
22671 for (child = die->child;
22672 child != NULL;
22673 child = child->sibling)
22674 {
22675 if (child->tag == DW_TAG_subprogram)
22676 {
73b9be8b 22677 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22678
7d45c7c3 22679 if (linkage_name != NULL)
abc72ce4 22680 {
43816ebc
TT
22681 gdb::unique_xmalloc_ptr<char> actual_name
22682 (language_class_name_from_physname (cu->language_defn,
22683 linkage_name));
22684 const char *name = NULL;
abc72ce4
DE
22685
22686 if (actual_name != NULL)
22687 {
15d034d0 22688 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22689
22690 if (die_name != NULL
43816ebc 22691 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
22692 {
22693 /* Strip off the class name from the full name.
22694 We want the prefix. */
22695 int die_name_len = strlen (die_name);
43816ebc
TT
22696 int actual_name_len = strlen (actual_name.get ());
22697 const char *ptr = actual_name.get ();
abc72ce4
DE
22698
22699 /* Test for '::' as a sanity check. */
22700 if (actual_name_len > die_name_len + 2
43816ebc 22701 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 22702 name = obstack_strndup (
e3b94546 22703 &objfile->per_bfd->storage_obstack,
43816ebc 22704 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
22705 }
22706 }
abc72ce4
DE
22707 return name;
22708 }
22709 }
22710 }
22711
22712 return NULL;
22713}
22714
96408a79
SA
22715/* GCC might emit a nameless typedef that has a linkage name. Determine the
22716 prefix part in such case. See
22717 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22718
a121b7c1 22719static const char *
96408a79
SA
22720anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22721{
22722 struct attribute *attr;
e6a959d6 22723 const char *base;
96408a79
SA
22724
22725 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22726 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22727 return NULL;
22728
7d45c7c3 22729 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22730 return NULL;
22731
73b9be8b 22732 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22733 if (attr == NULL || DW_STRING (attr) == NULL)
22734 return NULL;
22735
22736 /* dwarf2_name had to be already called. */
22737 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22738
22739 /* Strip the base name, keep any leading namespaces/classes. */
22740 base = strrchr (DW_STRING (attr), ':');
22741 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22742 return "";
22743
518817b3 22744 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
22745 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22746 DW_STRING (attr),
22747 &base[-1] - DW_STRING (attr));
96408a79
SA
22748}
22749
fdde2d81 22750/* Return the name of the namespace/class that DIE is defined within,
0114d602 22751 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22752
0114d602
DJ
22753 For example, if we're within the method foo() in the following
22754 code:
22755
22756 namespace N {
22757 class C {
22758 void foo () {
22759 }
22760 };
22761 }
22762
22763 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22764
0d5cff50 22765static const char *
e142c38c 22766determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22767{
518817b3
SM
22768 struct dwarf2_per_objfile *dwarf2_per_objfile
22769 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22770 struct die_info *parent, *spec_die;
22771 struct dwarf2_cu *spec_cu;
22772 struct type *parent_type;
a121b7c1 22773 const char *retval;
63d06c5c 22774
9c37b5ae 22775 if (cu->language != language_cplus
c44af4eb
TT
22776 && cu->language != language_fortran && cu->language != language_d
22777 && cu->language != language_rust)
0114d602
DJ
22778 return "";
22779
96408a79
SA
22780 retval = anonymous_struct_prefix (die, cu);
22781 if (retval)
22782 return retval;
22783
0114d602
DJ
22784 /* We have to be careful in the presence of DW_AT_specification.
22785 For example, with GCC 3.4, given the code
22786
22787 namespace N {
22788 void foo() {
22789 // Definition of N::foo.
22790 }
22791 }
22792
22793 then we'll have a tree of DIEs like this:
22794
22795 1: DW_TAG_compile_unit
22796 2: DW_TAG_namespace // N
22797 3: DW_TAG_subprogram // declaration of N::foo
22798 4: DW_TAG_subprogram // definition of N::foo
22799 DW_AT_specification // refers to die #3
22800
22801 Thus, when processing die #4, we have to pretend that we're in
22802 the context of its DW_AT_specification, namely the contex of die
22803 #3. */
22804 spec_cu = cu;
22805 spec_die = die_specification (die, &spec_cu);
22806 if (spec_die == NULL)
22807 parent = die->parent;
22808 else
63d06c5c 22809 {
0114d602
DJ
22810 parent = spec_die->parent;
22811 cu = spec_cu;
63d06c5c 22812 }
0114d602
DJ
22813
22814 if (parent == NULL)
22815 return "";
98bfdba5
PA
22816 else if (parent->building_fullname)
22817 {
22818 const char *name;
22819 const char *parent_name;
22820
22821 /* It has been seen on RealView 2.2 built binaries,
22822 DW_TAG_template_type_param types actually _defined_ as
22823 children of the parent class:
22824
22825 enum E {};
22826 template class <class Enum> Class{};
22827 Class<enum E> class_e;
22828
22829 1: DW_TAG_class_type (Class)
22830 2: DW_TAG_enumeration_type (E)
22831 3: DW_TAG_enumerator (enum1:0)
22832 3: DW_TAG_enumerator (enum2:1)
22833 ...
22834 2: DW_TAG_template_type_param
22835 DW_AT_type DW_FORM_ref_udata (E)
22836
22837 Besides being broken debug info, it can put GDB into an
22838 infinite loop. Consider:
22839
22840 When we're building the full name for Class<E>, we'll start
22841 at Class, and go look over its template type parameters,
22842 finding E. We'll then try to build the full name of E, and
22843 reach here. We're now trying to build the full name of E,
22844 and look over the parent DIE for containing scope. In the
22845 broken case, if we followed the parent DIE of E, we'd again
22846 find Class, and once again go look at its template type
22847 arguments, etc., etc. Simply don't consider such parent die
22848 as source-level parent of this die (it can't be, the language
22849 doesn't allow it), and break the loop here. */
22850 name = dwarf2_name (die, cu);
22851 parent_name = dwarf2_name (parent, cu);
b98664d3 22852 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22853 name ? name : "<unknown>",
22854 parent_name ? parent_name : "<unknown>");
22855 return "";
22856 }
63d06c5c 22857 else
0114d602
DJ
22858 switch (parent->tag)
22859 {
63d06c5c 22860 case DW_TAG_namespace:
0114d602 22861 parent_type = read_type_die (parent, cu);
acebe513
UW
22862 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22863 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22864 Work around this problem here. */
22865 if (cu->language == language_cplus
e86ca25f 22866 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 22867 return "";
0114d602 22868 /* We give a name to even anonymous namespaces. */
e86ca25f 22869 return TYPE_NAME (parent_type);
63d06c5c 22870 case DW_TAG_class_type:
680b30c7 22871 case DW_TAG_interface_type:
63d06c5c 22872 case DW_TAG_structure_type:
0114d602 22873 case DW_TAG_union_type:
f55ee35c 22874 case DW_TAG_module:
0114d602 22875 parent_type = read_type_die (parent, cu);
e86ca25f
TT
22876 if (TYPE_NAME (parent_type) != NULL)
22877 return TYPE_NAME (parent_type);
0114d602
DJ
22878 else
22879 /* An anonymous structure is only allowed non-static data
22880 members; no typedefs, no member functions, et cetera.
22881 So it does not need a prefix. */
22882 return "";
abc72ce4 22883 case DW_TAG_compile_unit:
95554aad 22884 case DW_TAG_partial_unit:
abc72ce4
DE
22885 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22886 if (cu->language == language_cplus
fd5866f6 22887 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
22888 && die->child != NULL
22889 && (die->tag == DW_TAG_class_type
22890 || die->tag == DW_TAG_structure_type
22891 || die->tag == DW_TAG_union_type))
22892 {
43816ebc 22893 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
22894 if (name != NULL)
22895 return name;
22896 }
22897 return "";
0a4b0913
AB
22898 case DW_TAG_subprogram:
22899 /* Nested subroutines in Fortran get a prefix with the name
22900 of the parent's subroutine. */
22901 if (cu->language == language_fortran)
22902 {
22903 if ((die->tag == DW_TAG_subprogram)
22904 && (dwarf2_name (parent, cu) != NULL))
22905 return dwarf2_name (parent, cu);
22906 }
22907 return determine_prefix (parent, cu);
3d567982
TT
22908 case DW_TAG_enumeration_type:
22909 parent_type = read_type_die (parent, cu);
22910 if (TYPE_DECLARED_CLASS (parent_type))
22911 {
e86ca25f
TT
22912 if (TYPE_NAME (parent_type) != NULL)
22913 return TYPE_NAME (parent_type);
3d567982
TT
22914 return "";
22915 }
22916 /* Fall through. */
63d06c5c 22917 default:
8176b9b8 22918 return determine_prefix (parent, cu);
63d06c5c 22919 }
63d06c5c
DC
22920}
22921
3e43a32a
MS
22922/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22923 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22924 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22925 an obconcat, otherwise allocate storage for the result. The CU argument is
22926 used to determine the language and hence, the appropriate separator. */
987504bb 22927
f55ee35c 22928#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22929
22930static char *
f55ee35c
JK
22931typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22932 int physname, struct dwarf2_cu *cu)
63d06c5c 22933{
f55ee35c 22934 const char *lead = "";
5c315b68 22935 const char *sep;
63d06c5c 22936
3e43a32a
MS
22937 if (suffix == NULL || suffix[0] == '\0'
22938 || prefix == NULL || prefix[0] == '\0')
987504bb 22939 sep = "";
45280282
IB
22940 else if (cu->language == language_d)
22941 {
22942 /* For D, the 'main' function could be defined in any module, but it
22943 should never be prefixed. */
22944 if (strcmp (suffix, "D main") == 0)
22945 {
22946 prefix = "";
22947 sep = "";
22948 }
22949 else
22950 sep = ".";
22951 }
f55ee35c
JK
22952 else if (cu->language == language_fortran && physname)
22953 {
22954 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22955 DW_AT_MIPS_linkage_name is preferred and used instead. */
22956
22957 lead = "__";
22958 sep = "_MOD_";
22959 }
987504bb
JJ
22960 else
22961 sep = "::";
63d06c5c 22962
6dd47d34
DE
22963 if (prefix == NULL)
22964 prefix = "";
22965 if (suffix == NULL)
22966 suffix = "";
22967
987504bb
JJ
22968 if (obs == NULL)
22969 {
3e43a32a 22970 char *retval
224c3ddb
SM
22971 = ((char *)
22972 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22973
f55ee35c
JK
22974 strcpy (retval, lead);
22975 strcat (retval, prefix);
6dd47d34
DE
22976 strcat (retval, sep);
22977 strcat (retval, suffix);
63d06c5c
DC
22978 return retval;
22979 }
987504bb
JJ
22980 else
22981 {
22982 /* We have an obstack. */
f55ee35c 22983 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22984 }
63d06c5c
DC
22985}
22986
c906108c
SS
22987/* Return sibling of die, NULL if no sibling. */
22988
f9aca02d 22989static struct die_info *
fba45db2 22990sibling_die (struct die_info *die)
c906108c 22991{
639d11d3 22992 return die->sibling;
c906108c
SS
22993}
22994
71c25dea
TT
22995/* Get name of a die, return NULL if not found. */
22996
15d034d0
TT
22997static const char *
22998dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22999 struct obstack *obstack)
23000{
23001 if (name && cu->language == language_cplus)
23002 {
2f408ecb 23003 std::string canon_name = cp_canonicalize_string (name);
71c25dea 23004
2f408ecb 23005 if (!canon_name.empty ())
71c25dea 23006 {
2f408ecb 23007 if (canon_name != name)
efba19b0 23008 name = obstack_strdup (obstack, canon_name);
71c25dea
TT
23009 }
23010 }
23011
23012 return name;
c906108c
SS
23013}
23014
96553a0c
DE
23015/* Get name of a die, return NULL if not found.
23016 Anonymous namespaces are converted to their magic string. */
9219021c 23017
15d034d0 23018static const char *
e142c38c 23019dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
23020{
23021 struct attribute *attr;
518817b3 23022 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 23023
e142c38c 23024 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 23025 if ((!attr || !DW_STRING (attr))
96553a0c 23026 && die->tag != DW_TAG_namespace
53832f31
TT
23027 && die->tag != DW_TAG_class_type
23028 && die->tag != DW_TAG_interface_type
23029 && die->tag != DW_TAG_structure_type
23030 && die->tag != DW_TAG_union_type)
71c25dea
TT
23031 return NULL;
23032
23033 switch (die->tag)
23034 {
23035 case DW_TAG_compile_unit:
95554aad 23036 case DW_TAG_partial_unit:
71c25dea
TT
23037 /* Compilation units have a DW_AT_name that is a filename, not
23038 a source language identifier. */
23039 case DW_TAG_enumeration_type:
23040 case DW_TAG_enumerator:
23041 /* These tags always have simple identifiers already; no need
23042 to canonicalize them. */
23043 return DW_STRING (attr);
907af001 23044
96553a0c
DE
23045 case DW_TAG_namespace:
23046 if (attr != NULL && DW_STRING (attr) != NULL)
23047 return DW_STRING (attr);
23048 return CP_ANONYMOUS_NAMESPACE_STR;
23049
907af001
UW
23050 case DW_TAG_class_type:
23051 case DW_TAG_interface_type:
23052 case DW_TAG_structure_type:
23053 case DW_TAG_union_type:
23054 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23055 structures or unions. These were of the form "._%d" in GCC 4.1,
23056 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23057 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 23058 if (attr && DW_STRING (attr)
61012eef
GB
23059 && (startswith (DW_STRING (attr), "._")
23060 || startswith (DW_STRING (attr), "<anonymous")))
907af001 23061 return NULL;
53832f31
TT
23062
23063 /* GCC might emit a nameless typedef that has a linkage name. See
23064 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23065 if (!attr || DW_STRING (attr) == NULL)
23066 {
73b9be8b 23067 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
23068 if (attr == NULL || DW_STRING (attr) == NULL)
23069 return NULL;
23070
df5c6c50
JK
23071 /* Avoid demangling DW_STRING (attr) the second time on a second
23072 call for the same DIE. */
23073 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 23074 {
43816ebc
TT
23075 gdb::unique_xmalloc_ptr<char> demangled
23076 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
23077
e6a959d6 23078 const char *base;
96408a79 23079
53832f31 23080 /* FIXME: we already did this for the partial symbol... */
34a68019 23081 DW_STRING (attr)
021887d8 23082 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 23083 demangled.get ());
53832f31 23084 DW_STRING_IS_CANONICAL (attr) = 1;
96408a79
SA
23085
23086 /* Strip any leading namespaces/classes, keep only the base name.
23087 DW_AT_name for named DIEs does not contain the prefixes. */
23088 base = strrchr (DW_STRING (attr), ':');
23089 if (base && base > DW_STRING (attr) && base[-1] == ':')
23090 return &base[1];
23091 else
23092 return DW_STRING (attr);
53832f31
TT
23093 }
23094 }
907af001
UW
23095 break;
23096
71c25dea 23097 default:
907af001
UW
23098 break;
23099 }
23100
23101 if (!DW_STRING_IS_CANONICAL (attr))
23102 {
23103 DW_STRING (attr)
23104 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 23105 &objfile->per_bfd->storage_obstack);
907af001 23106 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 23107 }
907af001 23108 return DW_STRING (attr);
9219021c
DC
23109}
23110
23111/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
23112 is none. *EXT_CU is the CU containing DIE on input, and the CU
23113 containing the return value on output. */
9219021c
DC
23114
23115static struct die_info *
f2f0e013 23116dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
23117{
23118 struct attribute *attr;
9219021c 23119
f2f0e013 23120 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
23121 if (attr == NULL)
23122 return NULL;
23123
f2f0e013 23124 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
23125}
23126
fa9c3fa0
TT
23127/* A convenience function that returns an "unknown" DWARF name,
23128 including the value of V. STR is the name of the entity being
23129 printed, e.g., "TAG". */
23130
23131static const char *
23132dwarf_unknown (const char *str, unsigned v)
23133{
23134 char *cell = get_print_cell ();
23135 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
23136 return cell;
23137}
23138
c906108c
SS
23139/* Convert a DIE tag into its string name. */
23140
f39c6ffd 23141static const char *
aa1ee363 23142dwarf_tag_name (unsigned tag)
c906108c 23143{
f39c6ffd
TT
23144 const char *name = get_DW_TAG_name (tag);
23145
23146 if (name == NULL)
fa9c3fa0 23147 return dwarf_unknown ("TAG", tag);
f39c6ffd
TT
23148
23149 return name;
c906108c
SS
23150}
23151
23152/* Convert a DWARF attribute code into its string name. */
23153
f39c6ffd 23154static const char *
aa1ee363 23155dwarf_attr_name (unsigned attr)
c906108c 23156{
f39c6ffd
TT
23157 const char *name;
23158
c764a876 23159#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
23160 if (attr == DW_AT_MIPS_fde)
23161 return "DW_AT_MIPS_fde";
23162#else
23163 if (attr == DW_AT_HP_block_index)
23164 return "DW_AT_HP_block_index";
c764a876 23165#endif
f39c6ffd
TT
23166
23167 name = get_DW_AT_name (attr);
23168
23169 if (name == NULL)
fa9c3fa0 23170 return dwarf_unknown ("AT", attr);
f39c6ffd
TT
23171
23172 return name;
c906108c
SS
23173}
23174
a084a2a6
AT
23175/* Convert a unit type to corresponding DW_UT name. */
23176
23177static const char *
23178dwarf_unit_type_name (int unit_type) {
23179 switch (unit_type)
23180 {
23181 case 0x01:
23182 return "DW_UT_compile (0x01)";
23183 case 0x02:
23184 return "DW_UT_type (0x02)";
23185 case 0x03:
23186 return "DW_UT_partial (0x03)";
23187 case 0x04:
23188 return "DW_UT_skeleton (0x04)";
23189 case 0x05:
23190 return "DW_UT_split_compile (0x05)";
23191 case 0x06:
23192 return "DW_UT_split_type (0x06)";
23193 case 0x80:
23194 return "DW_UT_lo_user (0x80)";
23195 case 0xff:
23196 return "DW_UT_hi_user (0xff)";
23197 default:
23198 return nullptr;
23199 }
23200}
23201
c906108c
SS
23202/* Convert a DWARF value form code into its string name. */
23203
f39c6ffd 23204static const char *
aa1ee363 23205dwarf_form_name (unsigned form)
c906108c 23206{
f39c6ffd
TT
23207 const char *name = get_DW_FORM_name (form);
23208
23209 if (name == NULL)
fa9c3fa0 23210 return dwarf_unknown ("FORM", form);
f39c6ffd
TT
23211
23212 return name;
c906108c
SS
23213}
23214
a121b7c1 23215static const char *
fba45db2 23216dwarf_bool_name (unsigned mybool)
c906108c
SS
23217{
23218 if (mybool)
23219 return "TRUE";
23220 else
23221 return "FALSE";
23222}
23223
23224/* Convert a DWARF type code into its string name. */
23225
f39c6ffd 23226static const char *
aa1ee363 23227dwarf_type_encoding_name (unsigned enc)
c906108c 23228{
f39c6ffd 23229 const char *name = get_DW_ATE_name (enc);
c906108c 23230
f39c6ffd 23231 if (name == NULL)
fa9c3fa0 23232 return dwarf_unknown ("ATE", enc);
c906108c 23233
f39c6ffd 23234 return name;
c906108c 23235}
c906108c 23236
f9aca02d 23237static void
d97bc12b 23238dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23239{
23240 unsigned int i;
23241
d97bc12b 23242 print_spaces (indent, f);
9d8780f0 23243 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23244 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23245 sect_offset_str (die->sect_off));
d97bc12b
DE
23246
23247 if (die->parent != NULL)
23248 {
23249 print_spaces (indent, f);
9d8780f0
SM
23250 fprintf_unfiltered (f, " parent at offset: %s\n",
23251 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23252 }
23253
23254 print_spaces (indent, f);
23255 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23256 dwarf_bool_name (die->child != NULL));
c906108c 23257
d97bc12b
DE
23258 print_spaces (indent, f);
23259 fprintf_unfiltered (f, " attributes:\n");
23260
c906108c
SS
23261 for (i = 0; i < die->num_attrs; ++i)
23262 {
d97bc12b
DE
23263 print_spaces (indent, f);
23264 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23265 dwarf_attr_name (die->attrs[i].name),
23266 dwarf_form_name (die->attrs[i].form));
d97bc12b 23267
c906108c
SS
23268 switch (die->attrs[i].form)
23269 {
c906108c 23270 case DW_FORM_addr:
336d760d 23271 case DW_FORM_addrx:
3019eac3 23272 case DW_FORM_GNU_addr_index:
d97bc12b 23273 fprintf_unfiltered (f, "address: ");
5af949e3 23274 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
23275 break;
23276 case DW_FORM_block2:
23277 case DW_FORM_block4:
23278 case DW_FORM_block:
23279 case DW_FORM_block1:
56eb65bd
SP
23280 fprintf_unfiltered (f, "block: size %s",
23281 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 23282 break;
2dc7f7b3 23283 case DW_FORM_exprloc:
56eb65bd
SP
23284 fprintf_unfiltered (f, "expression: size %s",
23285 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 23286 break;
0224619f
JK
23287 case DW_FORM_data16:
23288 fprintf_unfiltered (f, "constant of 16 bytes");
23289 break;
4568ecf9
DE
23290 case DW_FORM_ref_addr:
23291 fprintf_unfiltered (f, "ref address: ");
23292 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23293 break;
36586728
TT
23294 case DW_FORM_GNU_ref_alt:
23295 fprintf_unfiltered (f, "alt ref address: ");
23296 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23297 break;
10b3939b
DJ
23298 case DW_FORM_ref1:
23299 case DW_FORM_ref2:
23300 case DW_FORM_ref4:
4568ecf9
DE
23301 case DW_FORM_ref8:
23302 case DW_FORM_ref_udata:
d97bc12b 23303 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 23304 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 23305 break;
c906108c
SS
23306 case DW_FORM_data1:
23307 case DW_FORM_data2:
23308 case DW_FORM_data4:
ce5d95e1 23309 case DW_FORM_data8:
c906108c
SS
23310 case DW_FORM_udata:
23311 case DW_FORM_sdata:
43bbcdc2
PH
23312 fprintf_unfiltered (f, "constant: %s",
23313 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 23314 break;
2dc7f7b3
TT
23315 case DW_FORM_sec_offset:
23316 fprintf_unfiltered (f, "section offset: %s",
23317 pulongest (DW_UNSND (&die->attrs[i])));
23318 break;
55f1336d 23319 case DW_FORM_ref_sig8:
ac9ec31b
DE
23320 fprintf_unfiltered (f, "signature: %s",
23321 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 23322 break;
c906108c 23323 case DW_FORM_string:
4bdf3d34 23324 case DW_FORM_strp:
43988095 23325 case DW_FORM_line_strp:
cf532bd1 23326 case DW_FORM_strx:
3019eac3 23327 case DW_FORM_GNU_str_index:
36586728 23328 case DW_FORM_GNU_strp_alt:
8285870a 23329 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 23330 DW_STRING (&die->attrs[i])
8285870a
JK
23331 ? DW_STRING (&die->attrs[i]) : "",
23332 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
23333 break;
23334 case DW_FORM_flag:
23335 if (DW_UNSND (&die->attrs[i]))
d97bc12b 23336 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23337 else
d97bc12b 23338 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23339 break;
2dc7f7b3
TT
23340 case DW_FORM_flag_present:
23341 fprintf_unfiltered (f, "flag: TRUE");
23342 break;
a8329558 23343 case DW_FORM_indirect:
0963b4bd
MS
23344 /* The reader will have reduced the indirect form to
23345 the "base form" so this form should not occur. */
5f48f8f3 23346 fprintf_unfiltered (f,
3e43a32a 23347 "unexpected attribute form: DW_FORM_indirect");
a8329558 23348 break;
663c44ac
JK
23349 case DW_FORM_implicit_const:
23350 fprintf_unfiltered (f, "constant: %s",
23351 plongest (DW_SND (&die->attrs[i])));
23352 break;
c906108c 23353 default:
d97bc12b 23354 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23355 die->attrs[i].form);
d97bc12b 23356 break;
c906108c 23357 }
d97bc12b 23358 fprintf_unfiltered (f, "\n");
c906108c
SS
23359 }
23360}
23361
f9aca02d 23362static void
d97bc12b 23363dump_die_for_error (struct die_info *die)
c906108c 23364{
d97bc12b
DE
23365 dump_die_shallow (gdb_stderr, 0, die);
23366}
23367
23368static void
23369dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23370{
23371 int indent = level * 4;
23372
23373 gdb_assert (die != NULL);
23374
23375 if (level >= max_level)
23376 return;
23377
23378 dump_die_shallow (f, indent, die);
23379
23380 if (die->child != NULL)
c906108c 23381 {
d97bc12b
DE
23382 print_spaces (indent, f);
23383 fprintf_unfiltered (f, " Children:");
23384 if (level + 1 < max_level)
23385 {
23386 fprintf_unfiltered (f, "\n");
23387 dump_die_1 (f, level + 1, max_level, die->child);
23388 }
23389 else
23390 {
3e43a32a
MS
23391 fprintf_unfiltered (f,
23392 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23393 }
23394 }
23395
23396 if (die->sibling != NULL && level > 0)
23397 {
23398 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23399 }
23400}
23401
d97bc12b
DE
23402/* This is called from the pdie macro in gdbinit.in.
23403 It's not static so gcc will keep a copy callable from gdb. */
23404
23405void
23406dump_die (struct die_info *die, int max_level)
23407{
23408 dump_die_1 (gdb_stdlog, 0, max_level, die);
23409}
23410
f9aca02d 23411static void
51545339 23412store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23413{
51545339 23414 void **slot;
c906108c 23415
9c541725
PA
23416 slot = htab_find_slot_with_hash (cu->die_hash, die,
23417 to_underlying (die->sect_off),
b64f50a1 23418 INSERT);
51545339
DJ
23419
23420 *slot = die;
c906108c
SS
23421}
23422
b64f50a1
JK
23423/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23424 required kind. */
23425
23426static sect_offset
ff39bb5e 23427dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 23428{
7771576e 23429 if (attr_form_is_ref (attr))
9c541725 23430 return (sect_offset) DW_UNSND (attr);
93311388 23431
b98664d3 23432 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 23433 dwarf_form_name (attr->form));
9c541725 23434 return {};
c906108c
SS
23435}
23436
43bbcdc2
PH
23437/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23438 * the value held by the attribute is not constant. */
a02abb62 23439
43bbcdc2 23440static LONGEST
ff39bb5e 23441dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 23442{
663c44ac 23443 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
23444 return DW_SND (attr);
23445 else if (attr->form == DW_FORM_udata
23446 || attr->form == DW_FORM_data1
23447 || attr->form == DW_FORM_data2
23448 || attr->form == DW_FORM_data4
23449 || attr->form == DW_FORM_data8)
23450 return DW_UNSND (attr);
23451 else
23452 {
0224619f 23453 /* For DW_FORM_data16 see attr_form_is_constant. */
b98664d3 23454 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
23455 dwarf_form_name (attr->form));
23456 return default_value;
23457 }
23458}
23459
348e048f
DE
23460/* Follow reference or signature attribute ATTR of SRC_DIE.
23461 On entry *REF_CU is the CU of SRC_DIE.
23462 On exit *REF_CU is the CU of the result. */
23463
23464static struct die_info *
ff39bb5e 23465follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23466 struct dwarf2_cu **ref_cu)
23467{
23468 struct die_info *die;
23469
7771576e 23470 if (attr_form_is_ref (attr))
348e048f 23471 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23472 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23473 die = follow_die_sig (src_die, attr, ref_cu);
23474 else
23475 {
23476 dump_die_for_error (src_die);
23477 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 23478 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
23479 }
23480
23481 return die;
03dd20cc
DJ
23482}
23483
5c631832 23484/* Follow reference OFFSET.
673bfd45
DE
23485 On entry *REF_CU is the CU of the source die referencing OFFSET.
23486 On exit *REF_CU is the CU of the result.
23487 Returns NULL if OFFSET is invalid. */
f504f079 23488
f9aca02d 23489static struct die_info *
9c541725 23490follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23491 struct dwarf2_cu **ref_cu)
c906108c 23492{
10b3939b 23493 struct die_info temp_die;
f2f0e013 23494 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
23495 struct dwarf2_per_objfile *dwarf2_per_objfile
23496 = cu->per_cu->dwarf2_per_objfile;
10b3939b 23497
348e048f
DE
23498 gdb_assert (cu->per_cu != NULL);
23499
98bfdba5
PA
23500 target_cu = cu;
23501
3019eac3 23502 if (cu->per_cu->is_debug_types)
348e048f
DE
23503 {
23504 /* .debug_types CUs cannot reference anything outside their CU.
23505 If they need to, they have to reference a signatured type via
55f1336d 23506 DW_FORM_ref_sig8. */
9c541725 23507 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 23508 return NULL;
348e048f 23509 }
36586728 23510 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 23511 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
23512 {
23513 struct dwarf2_per_cu_data *per_cu;
9a619af0 23514
9c541725 23515 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 23516 dwarf2_per_objfile);
03dd20cc
DJ
23517
23518 /* If necessary, add it to the queue and load its DIEs. */
95554aad 23519 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 23520 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 23521
10b3939b
DJ
23522 target_cu = per_cu->cu;
23523 }
98bfdba5
PA
23524 else if (cu->dies == NULL)
23525 {
23526 /* We're loading full DIEs during partial symbol reading. */
23527 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 23528 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 23529 }
c906108c 23530
f2f0e013 23531 *ref_cu = target_cu;
9c541725 23532 temp_die.sect_off = sect_off;
c24bdb02
KS
23533
23534 if (target_cu != cu)
23535 target_cu->ancestor = cu;
23536
9a3c8263 23537 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23538 &temp_die,
23539 to_underlying (sect_off));
5c631832 23540}
10b3939b 23541
5c631832
JK
23542/* Follow reference attribute ATTR of SRC_DIE.
23543 On entry *REF_CU is the CU of SRC_DIE.
23544 On exit *REF_CU is the CU of the result. */
23545
23546static struct die_info *
ff39bb5e 23547follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23548 struct dwarf2_cu **ref_cu)
23549{
9c541725 23550 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
23551 struct dwarf2_cu *cu = *ref_cu;
23552 struct die_info *die;
23553
9c541725 23554 die = follow_die_offset (sect_off,
36586728
TT
23555 (attr->form == DW_FORM_GNU_ref_alt
23556 || cu->per_cu->is_dwz),
23557 ref_cu);
5c631832 23558 if (!die)
9d8780f0
SM
23559 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23560 "at %s [in module %s]"),
23561 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 23562 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 23563
5c631832
JK
23564 return die;
23565}
23566
9c541725 23567/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 23568 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
23569 dwarf2_locexpr_baton->data has lifetime of
23570 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
23571
23572struct dwarf2_locexpr_baton
9c541725 23573dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
23574 struct dwarf2_per_cu_data *per_cu,
23575 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 23576 void *baton, bool resolve_abstract_p)
5c631832 23577{
918dd910 23578 struct dwarf2_cu *cu;
5c631832
JK
23579 struct die_info *die;
23580 struct attribute *attr;
23581 struct dwarf2_locexpr_baton retval;
12359b5e
SM
23582 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23583 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 23584
918dd910 23585 if (per_cu->cu == NULL)
58f0c718 23586 load_cu (per_cu, false);
918dd910 23587 cu = per_cu->cu;
cc12ce38
DE
23588 if (cu == NULL)
23589 {
23590 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23591 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23592 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23593 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23594 }
918dd910 23595
9c541725 23596 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23597 if (!die)
9d8780f0
SM
23598 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23599 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23600
23601 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23602 if (!attr && resolve_abstract_p
3360b6e7 23603 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
23604 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23605 {
23606 CORE_ADDR pc = (*get_frame_pc) (baton);
6a053cb1 23607 CORE_ADDR baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
eba4caf2 23608 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 23609
3360b6e7
TV
23610 for (const auto &cand_off
23611 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 23612 {
3360b6e7
TV
23613 struct dwarf2_cu *cand_cu = cu;
23614 struct die_info *cand
23615 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23616 if (!cand
23617 || !cand->parent
e4a62c65
TV
23618 || cand->parent->tag != DW_TAG_subprogram)
23619 continue;
23620
23621 CORE_ADDR pc_low, pc_high;
23622 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23623 if (pc_low == ((CORE_ADDR) -1))
23624 continue;
23625 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23626 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23627 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23628 continue;
23629
23630 die = cand;
23631 attr = dwarf2_attr (die, DW_AT_location, cu);
23632 break;
23633 }
23634 }
23635
5c631832
JK
23636 if (!attr)
23637 {
e103e986
JK
23638 /* DWARF: "If there is no such attribute, then there is no effect.".
23639 DATA is ignored if SIZE is 0. */
5c631832 23640
e103e986 23641 retval.data = NULL;
5c631832
JK
23642 retval.size = 0;
23643 }
8cf6f0b1
TT
23644 else if (attr_form_is_section_offset (attr))
23645 {
23646 struct dwarf2_loclist_baton loclist_baton;
23647 CORE_ADDR pc = (*get_frame_pc) (baton);
23648 size_t size;
23649
23650 fill_in_loclist_baton (cu, &loclist_baton, attr);
23651
23652 retval.data = dwarf2_find_location_expression (&loclist_baton,
23653 &size, pc);
23654 retval.size = size;
23655 }
5c631832
JK
23656 else
23657 {
23658 if (!attr_form_is_block (attr))
9d8780f0 23659 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23660 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23661 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23662
23663 retval.data = DW_BLOCK (attr)->data;
23664 retval.size = DW_BLOCK (attr)->size;
23665 }
23666 retval.per_cu = cu->per_cu;
918dd910 23667
ed2dc618 23668 age_cached_comp_units (dwarf2_per_objfile);
918dd910 23669
5c631832 23670 return retval;
348e048f
DE
23671}
23672
8b9737bf
TT
23673/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23674 offset. */
23675
23676struct dwarf2_locexpr_baton
23677dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23678 struct dwarf2_per_cu_data *per_cu,
23679 CORE_ADDR (*get_frame_pc) (void *baton),
23680 void *baton)
23681{
9c541725 23682 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23683
9c541725 23684 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
23685}
23686
b6807d98
TT
23687/* Write a constant of a given type as target-ordered bytes into
23688 OBSTACK. */
23689
23690static const gdb_byte *
23691write_constant_as_bytes (struct obstack *obstack,
23692 enum bfd_endian byte_order,
23693 struct type *type,
23694 ULONGEST value,
23695 LONGEST *len)
23696{
23697 gdb_byte *result;
23698
23699 *len = TYPE_LENGTH (type);
224c3ddb 23700 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23701 store_unsigned_integer (result, *len, byte_order, value);
23702
23703 return result;
23704}
23705
23706/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23707 pointer to the constant bytes and set LEN to the length of the
23708 data. If memory is needed, allocate it on OBSTACK. If the DIE
23709 does not have a DW_AT_const_value, return NULL. */
23710
23711const gdb_byte *
9c541725 23712dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23713 struct dwarf2_per_cu_data *per_cu,
23714 struct obstack *obstack,
23715 LONGEST *len)
23716{
23717 struct dwarf2_cu *cu;
23718 struct die_info *die;
23719 struct attribute *attr;
23720 const gdb_byte *result = NULL;
23721 struct type *type;
23722 LONGEST value;
23723 enum bfd_endian byte_order;
e3b94546 23724 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23725
b6807d98 23726 if (per_cu->cu == NULL)
58f0c718 23727 load_cu (per_cu, false);
b6807d98 23728 cu = per_cu->cu;
cc12ce38
DE
23729 if (cu == NULL)
23730 {
23731 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23732 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23733 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23734 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23735 }
b6807d98 23736
9c541725 23737 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23738 if (!die)
9d8780f0
SM
23739 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23740 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23741
23742 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23743 if (attr == NULL)
23744 return NULL;
23745
e3b94546 23746 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23747 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23748
23749 switch (attr->form)
23750 {
23751 case DW_FORM_addr:
336d760d 23752 case DW_FORM_addrx:
b6807d98
TT
23753 case DW_FORM_GNU_addr_index:
23754 {
23755 gdb_byte *tem;
23756
23757 *len = cu->header.addr_size;
224c3ddb 23758 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23759 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23760 result = tem;
23761 }
23762 break;
23763 case DW_FORM_string:
23764 case DW_FORM_strp:
cf532bd1 23765 case DW_FORM_strx:
b6807d98
TT
23766 case DW_FORM_GNU_str_index:
23767 case DW_FORM_GNU_strp_alt:
23768 /* DW_STRING is already allocated on the objfile obstack, point
23769 directly to it. */
23770 result = (const gdb_byte *) DW_STRING (attr);
23771 *len = strlen (DW_STRING (attr));
23772 break;
23773 case DW_FORM_block1:
23774 case DW_FORM_block2:
23775 case DW_FORM_block4:
23776 case DW_FORM_block:
23777 case DW_FORM_exprloc:
0224619f 23778 case DW_FORM_data16:
b6807d98
TT
23779 result = DW_BLOCK (attr)->data;
23780 *len = DW_BLOCK (attr)->size;
23781 break;
23782
23783 /* The DW_AT_const_value attributes are supposed to carry the
23784 symbol's value "represented as it would be on the target
23785 architecture." By the time we get here, it's already been
23786 converted to host endianness, so we just need to sign- or
23787 zero-extend it as appropriate. */
23788 case DW_FORM_data1:
23789 type = die_type (die, cu);
23790 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23791 if (result == NULL)
23792 result = write_constant_as_bytes (obstack, byte_order,
23793 type, value, len);
23794 break;
23795 case DW_FORM_data2:
23796 type = die_type (die, cu);
23797 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23798 if (result == NULL)
23799 result = write_constant_as_bytes (obstack, byte_order,
23800 type, value, len);
23801 break;
23802 case DW_FORM_data4:
23803 type = die_type (die, cu);
23804 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23805 if (result == NULL)
23806 result = write_constant_as_bytes (obstack, byte_order,
23807 type, value, len);
23808 break;
23809 case DW_FORM_data8:
23810 type = die_type (die, cu);
23811 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23812 if (result == NULL)
23813 result = write_constant_as_bytes (obstack, byte_order,
23814 type, value, len);
23815 break;
23816
23817 case DW_FORM_sdata:
663c44ac 23818 case DW_FORM_implicit_const:
b6807d98
TT
23819 type = die_type (die, cu);
23820 result = write_constant_as_bytes (obstack, byte_order,
23821 type, DW_SND (attr), len);
23822 break;
23823
23824 case DW_FORM_udata:
23825 type = die_type (die, cu);
23826 result = write_constant_as_bytes (obstack, byte_order,
23827 type, DW_UNSND (attr), len);
23828 break;
23829
23830 default:
b98664d3 23831 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23832 dwarf_form_name (attr->form));
23833 break;
23834 }
23835
23836 return result;
23837}
23838
7942e96e
AA
23839/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23840 valid type for this die is found. */
23841
23842struct type *
9c541725 23843dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23844 struct dwarf2_per_cu_data *per_cu)
23845{
23846 struct dwarf2_cu *cu;
23847 struct die_info *die;
23848
7942e96e 23849 if (per_cu->cu == NULL)
58f0c718 23850 load_cu (per_cu, false);
7942e96e
AA
23851 cu = per_cu->cu;
23852 if (!cu)
23853 return NULL;
23854
9c541725 23855 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23856 if (!die)
23857 return NULL;
23858
23859 return die_type (die, cu);
23860}
23861
8a9b8146
TT
23862/* Return the type of the DIE at DIE_OFFSET in the CU named by
23863 PER_CU. */
23864
23865struct type *
b64f50a1 23866dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23867 struct dwarf2_per_cu_data *per_cu)
23868{
9c541725 23869 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23870 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23871}
23872
ac9ec31b 23873/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23874 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23875 On exit *REF_CU is the CU of the result.
23876 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23877
23878static struct die_info *
ac9ec31b
DE
23879follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23880 struct dwarf2_cu **ref_cu)
348e048f 23881{
348e048f 23882 struct die_info temp_die;
c24bdb02 23883 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
23884 struct die_info *die;
23885
ac9ec31b
DE
23886 /* While it might be nice to assert sig_type->type == NULL here,
23887 we can get here for DW_AT_imported_declaration where we need
23888 the DIE not the type. */
348e048f
DE
23889
23890 /* If necessary, add it to the queue and load its DIEs. */
23891
95554aad 23892 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23893 read_signatured_type (sig_type);
348e048f 23894
348e048f 23895 sig_cu = sig_type->per_cu.cu;
69d751e3 23896 gdb_assert (sig_cu != NULL);
9c541725
PA
23897 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23898 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23899 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23900 to_underlying (temp_die.sect_off));
348e048f
DE
23901 if (die)
23902 {
ed2dc618 23903 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23904 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23905
796a7ff8
DE
23906 /* For .gdb_index version 7 keep track of included TUs.
23907 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23908 if (dwarf2_per_objfile->index_table != NULL
23909 && dwarf2_per_objfile->index_table->version <= 7)
23910 {
ae640021 23911 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23912 }
23913
348e048f 23914 *ref_cu = sig_cu;
c24bdb02
KS
23915 if (sig_cu != cu)
23916 sig_cu->ancestor = cu;
23917
348e048f
DE
23918 return die;
23919 }
23920
ac9ec31b
DE
23921 return NULL;
23922}
23923
23924/* Follow signatured type referenced by ATTR in SRC_DIE.
23925 On entry *REF_CU is the CU of SRC_DIE.
23926 On exit *REF_CU is the CU of the result.
23927 The result is the DIE of the type.
23928 If the referenced type cannot be found an error is thrown. */
23929
23930static struct die_info *
ff39bb5e 23931follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23932 struct dwarf2_cu **ref_cu)
23933{
23934 ULONGEST signature = DW_SIGNATURE (attr);
23935 struct signatured_type *sig_type;
23936 struct die_info *die;
23937
23938 gdb_assert (attr->form == DW_FORM_ref_sig8);
23939
a2ce51a0 23940 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23941 /* sig_type will be NULL if the signatured type is missing from
23942 the debug info. */
23943 if (sig_type == NULL)
23944 {
23945 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23946 " from DIE at %s [in module %s]"),
23947 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23948 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23949 }
23950
23951 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23952 if (die == NULL)
23953 {
23954 dump_die_for_error (src_die);
23955 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23956 " from DIE at %s [in module %s]"),
23957 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23958 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23959 }
23960
23961 return die;
23962}
23963
23964/* Get the type specified by SIGNATURE referenced in DIE/CU,
23965 reading in and processing the type unit if necessary. */
23966
23967static struct type *
23968get_signatured_type (struct die_info *die, ULONGEST signature,
23969 struct dwarf2_cu *cu)
23970{
518817b3
SM
23971 struct dwarf2_per_objfile *dwarf2_per_objfile
23972 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23973 struct signatured_type *sig_type;
23974 struct dwarf2_cu *type_cu;
23975 struct die_info *type_die;
23976 struct type *type;
23977
a2ce51a0 23978 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23979 /* sig_type will be NULL if the signatured type is missing from
23980 the debug info. */
23981 if (sig_type == NULL)
23982 {
b98664d3 23983 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23984 " from DIE at %s [in module %s]"),
23985 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23986 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23987 return build_error_marker_type (cu, die);
23988 }
23989
23990 /* If we already know the type we're done. */
23991 if (sig_type->type != NULL)
23992 return sig_type->type;
23993
23994 type_cu = cu;
23995 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23996 if (type_die != NULL)
23997 {
23998 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23999 is created. This is important, for example, because for c++ classes
24000 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24001 type = read_type_die (type_die, type_cu);
24002 if (type == NULL)
24003 {
b98664d3 24004 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
24005 " referenced from DIE at %s [in module %s]"),
24006 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 24007 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
24008 type = build_error_marker_type (cu, die);
24009 }
24010 }
24011 else
24012 {
b98664d3 24013 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
24014 " from DIE at %s [in module %s]"),
24015 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 24016 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
24017 type = build_error_marker_type (cu, die);
24018 }
24019 sig_type->type = type;
24020
24021 return type;
24022}
24023
24024/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24025 reading in and processing the type unit if necessary. */
24026
24027static struct type *
ff39bb5e 24028get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 24029 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
24030{
24031 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 24032 if (attr_form_is_ref (attr))
ac9ec31b
DE
24033 {
24034 struct dwarf2_cu *type_cu = cu;
24035 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24036
24037 return read_type_die (type_die, type_cu);
24038 }
24039 else if (attr->form == DW_FORM_ref_sig8)
24040 {
24041 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
24042 }
24043 else
24044 {
518817b3
SM
24045 struct dwarf2_per_objfile *dwarf2_per_objfile
24046 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 24047
b98664d3 24048 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
24049 " at %s [in module %s]"),
24050 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 24051 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
24052 return build_error_marker_type (cu, die);
24053 }
348e048f
DE
24054}
24055
e5fe5e75 24056/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
24057
24058static void
e5fe5e75 24059load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 24060{
52dc124a 24061 struct signatured_type *sig_type;
348e048f 24062
f4dc4d17
DE
24063 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24064 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
24065
6721b2ec
DE
24066 /* We have the per_cu, but we need the signatured_type.
24067 Fortunately this is an easy translation. */
24068 gdb_assert (per_cu->is_debug_types);
24069 sig_type = (struct signatured_type *) per_cu;
348e048f 24070
6721b2ec 24071 gdb_assert (per_cu->cu == NULL);
348e048f 24072
52dc124a 24073 read_signatured_type (sig_type);
348e048f 24074
6721b2ec 24075 gdb_assert (per_cu->cu != NULL);
348e048f
DE
24076}
24077
dee91e82
DE
24078/* die_reader_func for read_signatured_type.
24079 This is identical to load_full_comp_unit_reader,
24080 but is kept separate for now. */
348e048f
DE
24081
24082static void
dee91e82 24083read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 24084 const gdb_byte *info_ptr,
dee91e82
DE
24085 struct die_info *comp_unit_die,
24086 int has_children,
24087 void *data)
348e048f 24088{
dee91e82 24089 struct dwarf2_cu *cu = reader->cu;
348e048f 24090
dee91e82
DE
24091 gdb_assert (cu->die_hash == NULL);
24092 cu->die_hash =
24093 htab_create_alloc_ex (cu->header.length / 12,
24094 die_hash,
24095 die_eq,
24096 NULL,
24097 &cu->comp_unit_obstack,
24098 hashtab_obstack_allocate,
24099 dummy_obstack_deallocate);
348e048f 24100
dee91e82
DE
24101 if (has_children)
24102 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
24103 &info_ptr, comp_unit_die);
24104 cu->dies = comp_unit_die;
24105 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
24106
24107 /* We try not to read any attributes in this function, because not
9cdd5dbd 24108 all CUs needed for references have been loaded yet, and symbol
348e048f 24109 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
24110 or we won't be able to build types correctly.
24111 Similarly, if we do not read the producer, we can not apply
24112 producer-specific interpretation. */
95554aad 24113 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 24114}
348e048f 24115
3019eac3
DE
24116/* Read in a signatured type and build its CU and DIEs.
24117 If the type is a stub for the real type in a DWO file,
24118 read in the real type from the DWO file as well. */
dee91e82
DE
24119
24120static void
24121read_signatured_type (struct signatured_type *sig_type)
24122{
24123 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 24124
3019eac3 24125 gdb_assert (per_cu->is_debug_types);
dee91e82 24126 gdb_assert (per_cu->cu == NULL);
348e048f 24127
58f0c718 24128 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
f4dc4d17 24129 read_signatured_type_reader, NULL);
7ee85ab1 24130 sig_type->per_cu.tu_read = 1;
c906108c
SS
24131}
24132
c906108c
SS
24133/* Decode simple location descriptions.
24134 Given a pointer to a dwarf block that defines a location, compute
24135 the location and return the value.
24136
4cecd739
DJ
24137 NOTE drow/2003-11-18: This function is called in two situations
24138 now: for the address of static or global variables (partial symbols
24139 only) and for offsets into structures which are expected to be
24140 (more or less) constant. The partial symbol case should go away,
24141 and only the constant case should remain. That will let this
24142 function complain more accurately. A few special modes are allowed
24143 without complaint for global variables (for instance, global
24144 register values and thread-local values).
c906108c
SS
24145
24146 A location description containing no operations indicates that the
4cecd739 24147 object is optimized out. The return value is 0 for that case.
6b992462
DJ
24148 FIXME drow/2003-11-16: No callers check for this case any more; soon all
24149 callers will only want a very basic result and this can become a
21ae7a4d
JK
24150 complaint.
24151
24152 Note that stack[0] is unused except as a default error return. */
c906108c
SS
24153
24154static CORE_ADDR
e7c27a73 24155decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 24156{
518817b3 24157 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
24158 size_t i;
24159 size_t size = blk->size;
d521ce57 24160 const gdb_byte *data = blk->data;
21ae7a4d
JK
24161 CORE_ADDR stack[64];
24162 int stacki;
24163 unsigned int bytes_read, unsnd;
24164 gdb_byte op;
c906108c 24165
21ae7a4d
JK
24166 i = 0;
24167 stacki = 0;
24168 stack[stacki] = 0;
24169 stack[++stacki] = 0;
24170
24171 while (i < size)
24172 {
24173 op = data[i++];
24174 switch (op)
24175 {
24176 case DW_OP_lit0:
24177 case DW_OP_lit1:
24178 case DW_OP_lit2:
24179 case DW_OP_lit3:
24180 case DW_OP_lit4:
24181 case DW_OP_lit5:
24182 case DW_OP_lit6:
24183 case DW_OP_lit7:
24184 case DW_OP_lit8:
24185 case DW_OP_lit9:
24186 case DW_OP_lit10:
24187 case DW_OP_lit11:
24188 case DW_OP_lit12:
24189 case DW_OP_lit13:
24190 case DW_OP_lit14:
24191 case DW_OP_lit15:
24192 case DW_OP_lit16:
24193 case DW_OP_lit17:
24194 case DW_OP_lit18:
24195 case DW_OP_lit19:
24196 case DW_OP_lit20:
24197 case DW_OP_lit21:
24198 case DW_OP_lit22:
24199 case DW_OP_lit23:
24200 case DW_OP_lit24:
24201 case DW_OP_lit25:
24202 case DW_OP_lit26:
24203 case DW_OP_lit27:
24204 case DW_OP_lit28:
24205 case DW_OP_lit29:
24206 case DW_OP_lit30:
24207 case DW_OP_lit31:
24208 stack[++stacki] = op - DW_OP_lit0;
24209 break;
f1bea926 24210
21ae7a4d
JK
24211 case DW_OP_reg0:
24212 case DW_OP_reg1:
24213 case DW_OP_reg2:
24214 case DW_OP_reg3:
24215 case DW_OP_reg4:
24216 case DW_OP_reg5:
24217 case DW_OP_reg6:
24218 case DW_OP_reg7:
24219 case DW_OP_reg8:
24220 case DW_OP_reg9:
24221 case DW_OP_reg10:
24222 case DW_OP_reg11:
24223 case DW_OP_reg12:
24224 case DW_OP_reg13:
24225 case DW_OP_reg14:
24226 case DW_OP_reg15:
24227 case DW_OP_reg16:
24228 case DW_OP_reg17:
24229 case DW_OP_reg18:
24230 case DW_OP_reg19:
24231 case DW_OP_reg20:
24232 case DW_OP_reg21:
24233 case DW_OP_reg22:
24234 case DW_OP_reg23:
24235 case DW_OP_reg24:
24236 case DW_OP_reg25:
24237 case DW_OP_reg26:
24238 case DW_OP_reg27:
24239 case DW_OP_reg28:
24240 case DW_OP_reg29:
24241 case DW_OP_reg30:
24242 case DW_OP_reg31:
24243 stack[++stacki] = op - DW_OP_reg0;
24244 if (i < size)
24245 dwarf2_complex_location_expr_complaint ();
24246 break;
c906108c 24247
21ae7a4d
JK
24248 case DW_OP_regx:
24249 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24250 i += bytes_read;
24251 stack[++stacki] = unsnd;
24252 if (i < size)
24253 dwarf2_complex_location_expr_complaint ();
24254 break;
c906108c 24255
21ae7a4d
JK
24256 case DW_OP_addr:
24257 stack[++stacki] = read_address (objfile->obfd, &data[i],
24258 cu, &bytes_read);
24259 i += bytes_read;
24260 break;
d53d4ac5 24261
21ae7a4d
JK
24262 case DW_OP_const1u:
24263 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24264 i += 1;
24265 break;
24266
24267 case DW_OP_const1s:
24268 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24269 i += 1;
24270 break;
24271
24272 case DW_OP_const2u:
24273 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24274 i += 2;
24275 break;
24276
24277 case DW_OP_const2s:
24278 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24279 i += 2;
24280 break;
d53d4ac5 24281
21ae7a4d
JK
24282 case DW_OP_const4u:
24283 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24284 i += 4;
24285 break;
24286
24287 case DW_OP_const4s:
24288 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24289 i += 4;
24290 break;
24291
585861ea
JK
24292 case DW_OP_const8u:
24293 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24294 i += 8;
24295 break;
24296
21ae7a4d
JK
24297 case DW_OP_constu:
24298 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24299 &bytes_read);
24300 i += bytes_read;
24301 break;
24302
24303 case DW_OP_consts:
24304 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24305 i += bytes_read;
24306 break;
24307
24308 case DW_OP_dup:
24309 stack[stacki + 1] = stack[stacki];
24310 stacki++;
24311 break;
24312
24313 case DW_OP_plus:
24314 stack[stacki - 1] += stack[stacki];
24315 stacki--;
24316 break;
24317
24318 case DW_OP_plus_uconst:
24319 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24320 &bytes_read);
24321 i += bytes_read;
24322 break;
24323
24324 case DW_OP_minus:
24325 stack[stacki - 1] -= stack[stacki];
24326 stacki--;
24327 break;
24328
24329 case DW_OP_deref:
24330 /* If we're not the last op, then we definitely can't encode
24331 this using GDB's address_class enum. This is valid for partial
24332 global symbols, although the variable's address will be bogus
24333 in the psymtab. */
24334 if (i < size)
24335 dwarf2_complex_location_expr_complaint ();
24336 break;
24337
24338 case DW_OP_GNU_push_tls_address:
4aa4e28b 24339 case DW_OP_form_tls_address:
21ae7a4d
JK
24340 /* The top of the stack has the offset from the beginning
24341 of the thread control block at which the variable is located. */
24342 /* Nothing should follow this operator, so the top of stack would
24343 be returned. */
24344 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24345 address will be bogus in the psymtab. Make it always at least
24346 non-zero to not look as a variable garbage collected by linker
24347 which have DW_OP_addr 0. */
21ae7a4d
JK
24348 if (i < size)
24349 dwarf2_complex_location_expr_complaint ();
585861ea 24350 stack[stacki]++;
21ae7a4d
JK
24351 break;
24352
24353 case DW_OP_GNU_uninit:
24354 break;
24355
336d760d 24356 case DW_OP_addrx:
3019eac3 24357 case DW_OP_GNU_addr_index:
49f6c839 24358 case DW_OP_GNU_const_index:
3019eac3
DE
24359 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24360 &bytes_read);
24361 i += bytes_read;
24362 break;
24363
21ae7a4d
JK
24364 default:
24365 {
f39c6ffd 24366 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
24367
24368 if (name)
b98664d3 24369 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
24370 name);
24371 else
b98664d3 24372 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
24373 op);
24374 }
24375
24376 return (stack[stacki]);
d53d4ac5 24377 }
3c6e0cb3 24378
21ae7a4d
JK
24379 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24380 outside of the allocated space. Also enforce minimum>0. */
24381 if (stacki >= ARRAY_SIZE (stack) - 1)
24382 {
b98664d3 24383 complaint (_("location description stack overflow"));
21ae7a4d
JK
24384 return 0;
24385 }
24386
24387 if (stacki <= 0)
24388 {
b98664d3 24389 complaint (_("location description stack underflow"));
21ae7a4d
JK
24390 return 0;
24391 }
24392 }
24393 return (stack[stacki]);
c906108c
SS
24394}
24395
24396/* memory allocation interface */
24397
c906108c 24398static struct dwarf_block *
7b5a2f43 24399dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24400{
8d749320 24401 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24402}
24403
c906108c 24404static struct die_info *
b60c80d6 24405dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24406{
24407 struct die_info *die;
b60c80d6
DJ
24408 size_t size = sizeof (struct die_info);
24409
24410 if (num_attrs > 1)
24411 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24412
b60c80d6 24413 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24414 memset (die, 0, sizeof (struct die_info));
24415 return (die);
24416}
2e276125
JB
24417
24418\f
24419/* Macro support. */
24420
233d95b5
JK
24421/* Return file name relative to the compilation directory of file number I in
24422 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 24423 responsible for freeing it. */
233d95b5 24424
2e276125 24425static char *
233d95b5 24426file_file_name (int file, struct line_header *lh)
2e276125 24427{
6a83a1e6
EZ
24428 /* Is the file number a valid index into the line header's file name
24429 table? Remember that file numbers start with one, not zero. */
7ba99d21 24430 if (lh->is_valid_file_index (file))
6a83a1e6 24431 {
7ba99d21 24432 const file_entry *fe = lh->file_name_at (file);
6e70227d 24433
7ba99d21 24434 if (!IS_ABSOLUTE_PATH (fe->name))
8c43009f 24435 {
7ba99d21 24436 const char *dir = fe->include_dir (lh);
8c43009f 24437 if (dir != NULL)
7ba99d21 24438 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
8c43009f 24439 }
7ba99d21 24440 return xstrdup (fe->name);
6a83a1e6 24441 }
2e276125
JB
24442 else
24443 {
6a83a1e6
EZ
24444 /* The compiler produced a bogus file number. We can at least
24445 record the macro definitions made in the file, even if we
24446 won't be able to find the file by name. */
24447 char fake_name[80];
9a619af0 24448
8c042590
PM
24449 xsnprintf (fake_name, sizeof (fake_name),
24450 "<bad macro file number %d>", file);
2e276125 24451
b98664d3 24452 complaint (_("bad file number in macro information (%d)"),
6a83a1e6 24453 file);
2e276125 24454
6a83a1e6 24455 return xstrdup (fake_name);
2e276125
JB
24456 }
24457}
24458
233d95b5
JK
24459/* Return the full name of file number I in *LH's file name table.
24460 Use COMP_DIR as the name of the current directory of the
24461 compilation. The result is allocated using xmalloc; the caller is
24462 responsible for freeing it. */
24463static char *
24464file_full_name (int file, struct line_header *lh, const char *comp_dir)
24465{
24466 /* Is the file number a valid index into the line header's file name
24467 table? Remember that file numbers start with one, not zero. */
7ba99d21 24468 if (lh->is_valid_file_index (file))
233d95b5
JK
24469 {
24470 char *relative = file_file_name (file, lh);
24471
24472 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24473 return relative;
b36cec19
PA
24474 return reconcat (relative, comp_dir, SLASH_STRING,
24475 relative, (char *) NULL);
233d95b5
JK
24476 }
24477 else
24478 return file_file_name (file, lh);
24479}
24480
2e276125
JB
24481
24482static struct macro_source_file *
804d2729
TT
24483macro_start_file (struct dwarf2_cu *cu,
24484 int file, int line,
2e276125 24485 struct macro_source_file *current_file,
43f3e411 24486 struct line_header *lh)
2e276125 24487{
233d95b5
JK
24488 /* File name relative to the compilation directory of this source file. */
24489 char *file_name = file_file_name (file, lh);
2e276125 24490
2e276125 24491 if (! current_file)
abc9d0dc 24492 {
fc474241
DE
24493 /* Note: We don't create a macro table for this compilation unit
24494 at all until we actually get a filename. */
c24bdb02 24495 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
fc474241 24496
abc9d0dc
TT
24497 /* If we have no current file, then this must be the start_file
24498 directive for the compilation unit's main source file. */
fc474241
DE
24499 current_file = macro_set_main (macro_table, file_name);
24500 macro_define_special (macro_table);
abc9d0dc 24501 }
2e276125 24502 else
233d95b5 24503 current_file = macro_include (current_file, line, file_name);
2e276125 24504
233d95b5 24505 xfree (file_name);
6e70227d 24506
2e276125
JB
24507 return current_file;
24508}
24509
2e276125
JB
24510static const char *
24511consume_improper_spaces (const char *p, const char *body)
24512{
24513 if (*p == ' ')
24514 {
b98664d3 24515 complaint (_("macro definition contains spaces "
3e43a32a 24516 "in formal argument list:\n`%s'"),
4d3c2250 24517 body);
2e276125
JB
24518
24519 while (*p == ' ')
24520 p++;
24521 }
24522
24523 return p;
24524}
24525
24526
24527static void
24528parse_macro_definition (struct macro_source_file *file, int line,
24529 const char *body)
24530{
24531 const char *p;
24532
24533 /* The body string takes one of two forms. For object-like macro
24534 definitions, it should be:
24535
24536 <macro name> " " <definition>
24537
24538 For function-like macro definitions, it should be:
24539
24540 <macro name> "() " <definition>
24541 or
24542 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24543
24544 Spaces may appear only where explicitly indicated, and in the
24545 <definition>.
24546
24547 The Dwarf 2 spec says that an object-like macro's name is always
24548 followed by a space, but versions of GCC around March 2002 omit
6e70227d 24549 the space when the macro's definition is the empty string.
2e276125
JB
24550
24551 The Dwarf 2 spec says that there should be no spaces between the
24552 formal arguments in a function-like macro's formal argument list,
24553 but versions of GCC around March 2002 include spaces after the
24554 commas. */
24555
24556
24557 /* Find the extent of the macro name. The macro name is terminated
24558 by either a space or null character (for an object-like macro) or
24559 an opening paren (for a function-like macro). */
24560 for (p = body; *p; p++)
24561 if (*p == ' ' || *p == '(')
24562 break;
24563
24564 if (*p == ' ' || *p == '\0')
24565 {
24566 /* It's an object-like macro. */
24567 int name_len = p - body;
456e800a 24568 std::string name (body, name_len);
2e276125
JB
24569 const char *replacement;
24570
24571 if (*p == ' ')
24572 replacement = body + name_len + 1;
24573 else
24574 {
4d3c2250 24575 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24576 replacement = body + name_len;
24577 }
6e70227d 24578
456e800a 24579 macro_define_object (file, line, name.c_str (), replacement);
2e276125
JB
24580 }
24581 else if (*p == '(')
24582 {
24583 /* It's a function-like macro. */
456e800a 24584 std::string name (body, p - body);
2e276125
JB
24585 int argc = 0;
24586 int argv_size = 1;
8d749320 24587 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
24588
24589 p++;
24590
24591 p = consume_improper_spaces (p, body);
24592
24593 /* Parse the formal argument list. */
24594 while (*p && *p != ')')
24595 {
24596 /* Find the extent of the current argument name. */
24597 const char *arg_start = p;
24598
24599 while (*p && *p != ',' && *p != ')' && *p != ' ')
24600 p++;
24601
24602 if (! *p || p == arg_start)
4d3c2250 24603 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24604 else
24605 {
24606 /* Make sure argv has room for the new argument. */
24607 if (argc >= argv_size)
24608 {
24609 argv_size *= 2;
224c3ddb 24610 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
24611 }
24612
3f8a7804 24613 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
24614 }
24615
24616 p = consume_improper_spaces (p, body);
24617
24618 /* Consume the comma, if present. */
24619 if (*p == ',')
24620 {
24621 p++;
24622
24623 p = consume_improper_spaces (p, body);
24624 }
24625 }
24626
24627 if (*p == ')')
24628 {
24629 p++;
24630
24631 if (*p == ' ')
24632 /* Perfectly formed definition, no complaints. */
456e800a 24633 macro_define_function (file, line, name.c_str (),
6e70227d 24634 argc, (const char **) argv,
2e276125
JB
24635 p + 1);
24636 else if (*p == '\0')
24637 {
24638 /* Complain, but do define it. */
4d3c2250 24639 dwarf2_macro_malformed_definition_complaint (body);
456e800a 24640 macro_define_function (file, line, name.c_str (),
6e70227d 24641 argc, (const char **) argv,
2e276125
JB
24642 p);
24643 }
24644 else
24645 /* Just complain. */
4d3c2250 24646 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24647 }
24648 else
24649 /* Just complain. */
4d3c2250 24650 dwarf2_macro_malformed_definition_complaint (body);
2e276125 24651
2e276125
JB
24652 {
24653 int i;
24654
24655 for (i = 0; i < argc; i++)
24656 xfree (argv[i]);
24657 }
24658 xfree (argv);
24659 }
24660 else
4d3c2250 24661 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24662}
24663
cf2c3c16
TT
24664/* Skip some bytes from BYTES according to the form given in FORM.
24665 Returns the new pointer. */
2e276125 24666
d521ce57
TT
24667static const gdb_byte *
24668skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
24669 enum dwarf_form form,
24670 unsigned int offset_size,
24671 struct dwarf2_section_info *section)
2e276125 24672{
cf2c3c16 24673 unsigned int bytes_read;
2e276125 24674
cf2c3c16 24675 switch (form)
2e276125 24676 {
cf2c3c16
TT
24677 case DW_FORM_data1:
24678 case DW_FORM_flag:
24679 ++bytes;
24680 break;
24681
24682 case DW_FORM_data2:
24683 bytes += 2;
24684 break;
24685
24686 case DW_FORM_data4:
24687 bytes += 4;
24688 break;
24689
24690 case DW_FORM_data8:
24691 bytes += 8;
24692 break;
24693
0224619f
JK
24694 case DW_FORM_data16:
24695 bytes += 16;
24696 break;
24697
cf2c3c16
TT
24698 case DW_FORM_string:
24699 read_direct_string (abfd, bytes, &bytes_read);
24700 bytes += bytes_read;
24701 break;
24702
24703 case DW_FORM_sec_offset:
24704 case DW_FORM_strp:
36586728 24705 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24706 bytes += offset_size;
24707 break;
24708
24709 case DW_FORM_block:
24710 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24711 bytes += bytes_read;
24712 break;
24713
24714 case DW_FORM_block1:
24715 bytes += 1 + read_1_byte (abfd, bytes);
24716 break;
24717 case DW_FORM_block2:
24718 bytes += 2 + read_2_bytes (abfd, bytes);
24719 break;
24720 case DW_FORM_block4:
24721 bytes += 4 + read_4_bytes (abfd, bytes);
24722 break;
24723
336d760d 24724 case DW_FORM_addrx:
cf2c3c16 24725 case DW_FORM_sdata:
cf532bd1 24726 case DW_FORM_strx:
cf2c3c16 24727 case DW_FORM_udata:
3019eac3
DE
24728 case DW_FORM_GNU_addr_index:
24729 case DW_FORM_GNU_str_index:
d521ce57 24730 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24731 if (bytes == NULL)
24732 {
24733 dwarf2_section_buffer_overflow_complaint (section);
24734 return NULL;
24735 }
cf2c3c16
TT
24736 break;
24737
663c44ac
JK
24738 case DW_FORM_implicit_const:
24739 break;
24740
cf2c3c16
TT
24741 default:
24742 {
b98664d3 24743 complaint (_("invalid form 0x%x in `%s'"),
a32a8923 24744 form, get_section_name (section));
cf2c3c16
TT
24745 return NULL;
24746 }
2e276125
JB
24747 }
24748
cf2c3c16
TT
24749 return bytes;
24750}
757a13d0 24751
cf2c3c16
TT
24752/* A helper for dwarf_decode_macros that handles skipping an unknown
24753 opcode. Returns an updated pointer to the macro data buffer; or,
24754 on error, issues a complaint and returns NULL. */
757a13d0 24755
d521ce57 24756static const gdb_byte *
cf2c3c16 24757skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24758 const gdb_byte **opcode_definitions,
24759 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24760 bfd *abfd,
24761 unsigned int offset_size,
24762 struct dwarf2_section_info *section)
24763{
24764 unsigned int bytes_read, i;
24765 unsigned long arg;
d521ce57 24766 const gdb_byte *defn;
2e276125 24767
cf2c3c16 24768 if (opcode_definitions[opcode] == NULL)
2e276125 24769 {
b98664d3 24770 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
24771 opcode);
24772 return NULL;
24773 }
2e276125 24774
cf2c3c16
TT
24775 defn = opcode_definitions[opcode];
24776 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24777 defn += bytes_read;
2e276125 24778
cf2c3c16
TT
24779 for (i = 0; i < arg; ++i)
24780 {
aead7601
SM
24781 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24782 (enum dwarf_form) defn[i], offset_size,
f664829e 24783 section);
cf2c3c16
TT
24784 if (mac_ptr == NULL)
24785 {
24786 /* skip_form_bytes already issued the complaint. */
24787 return NULL;
24788 }
24789 }
757a13d0 24790
cf2c3c16
TT
24791 return mac_ptr;
24792}
757a13d0 24793
cf2c3c16
TT
24794/* A helper function which parses the header of a macro section.
24795 If the macro section is the extended (for now called "GNU") type,
24796 then this updates *OFFSET_SIZE. Returns a pointer to just after
24797 the header, or issues a complaint and returns NULL on error. */
757a13d0 24798
d521ce57
TT
24799static const gdb_byte *
24800dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24801 bfd *abfd,
d521ce57 24802 const gdb_byte *mac_ptr,
cf2c3c16
TT
24803 unsigned int *offset_size,
24804 int section_is_gnu)
24805{
24806 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24807
cf2c3c16
TT
24808 if (section_is_gnu)
24809 {
24810 unsigned int version, flags;
757a13d0 24811
cf2c3c16 24812 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24813 if (version != 4 && version != 5)
cf2c3c16 24814 {
b98664d3 24815 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
24816 version);
24817 return NULL;
24818 }
24819 mac_ptr += 2;
757a13d0 24820
cf2c3c16
TT
24821 flags = read_1_byte (abfd, mac_ptr);
24822 ++mac_ptr;
24823 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24824
cf2c3c16
TT
24825 if ((flags & 2) != 0)
24826 /* We don't need the line table offset. */
24827 mac_ptr += *offset_size;
757a13d0 24828
cf2c3c16
TT
24829 /* Vendor opcode descriptions. */
24830 if ((flags & 4) != 0)
24831 {
24832 unsigned int i, count;
757a13d0 24833
cf2c3c16
TT
24834 count = read_1_byte (abfd, mac_ptr);
24835 ++mac_ptr;
24836 for (i = 0; i < count; ++i)
24837 {
24838 unsigned int opcode, bytes_read;
24839 unsigned long arg;
24840
24841 opcode = read_1_byte (abfd, mac_ptr);
24842 ++mac_ptr;
24843 opcode_definitions[opcode] = mac_ptr;
24844 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24845 mac_ptr += bytes_read;
24846 mac_ptr += arg;
24847 }
757a13d0 24848 }
cf2c3c16 24849 }
757a13d0 24850
cf2c3c16
TT
24851 return mac_ptr;
24852}
757a13d0 24853
cf2c3c16 24854/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24855 including DW_MACRO_import. */
cf2c3c16
TT
24856
24857static void
804d2729 24858dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
ed2dc618 24859 bfd *abfd,
d521ce57 24860 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24861 struct macro_source_file *current_file,
43f3e411 24862 struct line_header *lh,
cf2c3c16 24863 struct dwarf2_section_info *section,
36586728 24864 int section_is_gnu, int section_is_dwz,
cf2c3c16 24865 unsigned int offset_size,
8fc3fc34 24866 htab_t include_hash)
cf2c3c16 24867{
804d2729
TT
24868 struct dwarf2_per_objfile *dwarf2_per_objfile
24869 = cu->per_cu->dwarf2_per_objfile;
4d663531 24870 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24871 enum dwarf_macro_record_type macinfo_type;
24872 int at_commandline;
d521ce57 24873 const gdb_byte *opcode_definitions[256];
757a13d0 24874
cf2c3c16
TT
24875 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24876 &offset_size, section_is_gnu);
24877 if (mac_ptr == NULL)
24878 {
24879 /* We already issued a complaint. */
24880 return;
24881 }
757a13d0
JK
24882
24883 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24884 GDB is still reading the definitions from command line. First
24885 DW_MACINFO_start_file will need to be ignored as it was already executed
24886 to create CURRENT_FILE for the main source holding also the command line
24887 definitions. On first met DW_MACINFO_start_file this flag is reset to
24888 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24889
24890 at_commandline = 1;
24891
24892 do
24893 {
24894 /* Do we at least have room for a macinfo type byte? */
24895 if (mac_ptr >= mac_end)
24896 {
f664829e 24897 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24898 break;
24899 }
24900
aead7601 24901 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24902 mac_ptr++;
24903
cf2c3c16
TT
24904 /* Note that we rely on the fact that the corresponding GNU and
24905 DWARF constants are the same. */
132448f8
SM
24906 DIAGNOSTIC_PUSH
24907 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24908 switch (macinfo_type)
24909 {
24910 /* A zero macinfo type indicates the end of the macro
24911 information. */
24912 case 0:
24913 break;
2e276125 24914
0af92d60
JK
24915 case DW_MACRO_define:
24916 case DW_MACRO_undef:
24917 case DW_MACRO_define_strp:
24918 case DW_MACRO_undef_strp:
24919 case DW_MACRO_define_sup:
24920 case DW_MACRO_undef_sup:
2e276125 24921 {
891d2f0b 24922 unsigned int bytes_read;
2e276125 24923 int line;
d521ce57 24924 const char *body;
cf2c3c16 24925 int is_define;
2e276125 24926
cf2c3c16
TT
24927 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24928 mac_ptr += bytes_read;
24929
0af92d60
JK
24930 if (macinfo_type == DW_MACRO_define
24931 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24932 {
24933 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24934 mac_ptr += bytes_read;
24935 }
24936 else
24937 {
24938 LONGEST str_offset;
24939
24940 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24941 mac_ptr += offset_size;
2e276125 24942
0af92d60
JK
24943 if (macinfo_type == DW_MACRO_define_sup
24944 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24945 || section_is_dwz)
36586728 24946 {
ed2dc618
SM
24947 struct dwz_file *dwz
24948 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24949
ed2dc618
SM
24950 body = read_indirect_string_from_dwz (objfile,
24951 dwz, str_offset);
36586728
TT
24952 }
24953 else
ed2dc618
SM
24954 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24955 abfd, str_offset);
cf2c3c16
TT
24956 }
24957
0af92d60
JK
24958 is_define = (macinfo_type == DW_MACRO_define
24959 || macinfo_type == DW_MACRO_define_strp
24960 || macinfo_type == DW_MACRO_define_sup);
2e276125 24961 if (! current_file)
757a13d0
JK
24962 {
24963 /* DWARF violation as no main source is present. */
b98664d3 24964 complaint (_("debug info with no main source gives macro %s "
757a13d0 24965 "on line %d: %s"),
cf2c3c16
TT
24966 is_define ? _("definition") : _("undefinition"),
24967 line, body);
757a13d0
JK
24968 break;
24969 }
3e43a32a
MS
24970 if ((line == 0 && !at_commandline)
24971 || (line != 0 && at_commandline))
b98664d3 24972 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 24973 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24974 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24975 line == 0 ? _("zero") : _("non-zero"), line, body);
24976
955b06fa 24977 if (body == NULL)
7bede828 24978 {
955b06fa
SDJ
24979 /* Fedora's rpm-build's "debugedit" binary
24980 corrupted .debug_macro sections.
24981
24982 For more info, see
24983 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24984 complaint (_("debug info gives %s invalid macro %s "
24985 "without body (corrupted?) at line %d "
24986 "on file %s"),
24987 at_commandline ? _("command-line") : _("in-file"),
24988 is_define ? _("definition") : _("undefinition"),
24989 line, current_file->filename);
7bede828 24990 }
955b06fa
SDJ
24991 else if (is_define)
24992 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24993 else
24994 {
0af92d60
JK
24995 gdb_assert (macinfo_type == DW_MACRO_undef
24996 || macinfo_type == DW_MACRO_undef_strp
24997 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24998 macro_undef (current_file, line, body);
24999 }
2e276125
JB
25000 }
25001 break;
25002
0af92d60 25003 case DW_MACRO_start_file:
2e276125 25004 {
891d2f0b 25005 unsigned int bytes_read;
2e276125
JB
25006 int line, file;
25007
25008 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25009 mac_ptr += bytes_read;
25010 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25011 mac_ptr += bytes_read;
25012
3e43a32a
MS
25013 if ((line == 0 && !at_commandline)
25014 || (line != 0 && at_commandline))
b98664d3 25015 complaint (_("debug info gives source %d included "
757a13d0
JK
25016 "from %s at %s line %d"),
25017 file, at_commandline ? _("command-line") : _("file"),
25018 line == 0 ? _("zero") : _("non-zero"), line);
25019
25020 if (at_commandline)
25021 {
0af92d60 25022 /* This DW_MACRO_start_file was executed in the
cf2c3c16 25023 pass one. */
757a13d0
JK
25024 at_commandline = 0;
25025 }
25026 else
804d2729
TT
25027 current_file = macro_start_file (cu, file, line, current_file,
25028 lh);
2e276125
JB
25029 }
25030 break;
25031
0af92d60 25032 case DW_MACRO_end_file:
2e276125 25033 if (! current_file)
b98664d3 25034 complaint (_("macro debug info has an unmatched "
3e43a32a 25035 "`close_file' directive"));
2e276125
JB
25036 else
25037 {
25038 current_file = current_file->included_by;
25039 if (! current_file)
25040 {
cf2c3c16 25041 enum dwarf_macro_record_type next_type;
2e276125
JB
25042
25043 /* GCC circa March 2002 doesn't produce the zero
25044 type byte marking the end of the compilation
25045 unit. Complain if it's not there, but exit no
25046 matter what. */
25047
25048 /* Do we at least have room for a macinfo type byte? */
25049 if (mac_ptr >= mac_end)
25050 {
f664829e 25051 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
25052 return;
25053 }
25054
25055 /* We don't increment mac_ptr here, so this is just
25056 a look-ahead. */
aead7601
SM
25057 next_type
25058 = (enum dwarf_macro_record_type) read_1_byte (abfd,
25059 mac_ptr);
2e276125 25060 if (next_type != 0)
b98664d3 25061 complaint (_("no terminating 0-type entry for "
3e43a32a 25062 "macros in `.debug_macinfo' section"));
2e276125
JB
25063
25064 return;
25065 }
25066 }
25067 break;
25068
0af92d60
JK
25069 case DW_MACRO_import:
25070 case DW_MACRO_import_sup:
cf2c3c16
TT
25071 {
25072 LONGEST offset;
8fc3fc34 25073 void **slot;
a036ba48
TT
25074 bfd *include_bfd = abfd;
25075 struct dwarf2_section_info *include_section = section;
d521ce57 25076 const gdb_byte *include_mac_end = mac_end;
a036ba48 25077 int is_dwz = section_is_dwz;
d521ce57 25078 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
25079
25080 offset = read_offset_1 (abfd, mac_ptr, offset_size);
25081 mac_ptr += offset_size;
25082
0af92d60 25083 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 25084 {
ed2dc618 25085 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 25086
4d663531 25087 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 25088
a036ba48 25089 include_section = &dwz->macro;
a32a8923 25090 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
25091 include_mac_end = dwz->macro.buffer + dwz->macro.size;
25092 is_dwz = 1;
25093 }
25094
25095 new_mac_ptr = include_section->buffer + offset;
25096 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
25097
8fc3fc34
TT
25098 if (*slot != NULL)
25099 {
25100 /* This has actually happened; see
25101 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 25102 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
25103 ".debug_macro section"));
25104 }
25105 else
25106 {
d521ce57 25107 *slot = (void *) new_mac_ptr;
36586728 25108
804d2729 25109 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
43f3e411 25110 include_mac_end, current_file, lh,
36586728 25111 section, section_is_gnu, is_dwz,
4d663531 25112 offset_size, include_hash);
8fc3fc34 25113
d521ce57 25114 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 25115 }
cf2c3c16
TT
25116 }
25117 break;
25118
2e276125 25119 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
25120 if (!section_is_gnu)
25121 {
25122 unsigned int bytes_read;
2e276125 25123
ac298888
TT
25124 /* This reads the constant, but since we don't recognize
25125 any vendor extensions, we ignore it. */
25126 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
25127 mac_ptr += bytes_read;
25128 read_direct_string (abfd, mac_ptr, &bytes_read);
25129 mac_ptr += bytes_read;
2e276125 25130
cf2c3c16
TT
25131 /* We don't recognize any vendor extensions. */
25132 break;
25133 }
25134 /* FALLTHROUGH */
25135
25136 default:
25137 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25138 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25139 section);
25140 if (mac_ptr == NULL)
25141 return;
25142 break;
2e276125 25143 }
132448f8 25144 DIAGNOSTIC_POP
757a13d0 25145 } while (macinfo_type != 0);
2e276125 25146}
8e19ed76 25147
cf2c3c16 25148static void
09262596 25149dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 25150 int section_is_gnu)
cf2c3c16 25151{
518817b3
SM
25152 struct dwarf2_per_objfile *dwarf2_per_objfile
25153 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25154 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
25155 struct line_header *lh = cu->line_header;
25156 bfd *abfd;
d521ce57 25157 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
25158 struct macro_source_file *current_file = 0;
25159 enum dwarf_macro_record_type macinfo_type;
25160 unsigned int offset_size = cu->header.offset_size;
d521ce57 25161 const gdb_byte *opcode_definitions[256];
8fc3fc34 25162 void **slot;
09262596
DE
25163 struct dwarf2_section_info *section;
25164 const char *section_name;
25165
25166 if (cu->dwo_unit != NULL)
25167 {
25168 if (section_is_gnu)
25169 {
25170 section = &cu->dwo_unit->dwo_file->sections.macro;
25171 section_name = ".debug_macro.dwo";
25172 }
25173 else
25174 {
25175 section = &cu->dwo_unit->dwo_file->sections.macinfo;
25176 section_name = ".debug_macinfo.dwo";
25177 }
25178 }
25179 else
25180 {
25181 if (section_is_gnu)
25182 {
25183 section = &dwarf2_per_objfile->macro;
25184 section_name = ".debug_macro";
25185 }
25186 else
25187 {
25188 section = &dwarf2_per_objfile->macinfo;
25189 section_name = ".debug_macinfo";
25190 }
25191 }
cf2c3c16 25192
bb5ed363 25193 dwarf2_read_section (objfile, section);
cf2c3c16
TT
25194 if (section->buffer == NULL)
25195 {
b98664d3 25196 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
25197 return;
25198 }
a32a8923 25199 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
25200
25201 /* First pass: Find the name of the base filename.
25202 This filename is needed in order to process all macros whose definition
25203 (or undefinition) comes from the command line. These macros are defined
25204 before the first DW_MACINFO_start_file entry, and yet still need to be
25205 associated to the base file.
25206
25207 To determine the base file name, we scan the macro definitions until we
25208 reach the first DW_MACINFO_start_file entry. We then initialize
25209 CURRENT_FILE accordingly so that any macro definition found before the
25210 first DW_MACINFO_start_file can still be associated to the base file. */
25211
25212 mac_ptr = section->buffer + offset;
25213 mac_end = section->buffer + section->size;
25214
25215 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
25216 &offset_size, section_is_gnu);
25217 if (mac_ptr == NULL)
25218 {
25219 /* We already issued a complaint. */
25220 return;
25221 }
25222
25223 do
25224 {
25225 /* Do we at least have room for a macinfo type byte? */
25226 if (mac_ptr >= mac_end)
25227 {
25228 /* Complaint is printed during the second pass as GDB will probably
25229 stop the first pass earlier upon finding
25230 DW_MACINFO_start_file. */
25231 break;
25232 }
25233
aead7601 25234 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
25235 mac_ptr++;
25236
25237 /* Note that we rely on the fact that the corresponding GNU and
25238 DWARF constants are the same. */
132448f8
SM
25239 DIAGNOSTIC_PUSH
25240 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
25241 switch (macinfo_type)
25242 {
25243 /* A zero macinfo type indicates the end of the macro
25244 information. */
25245 case 0:
25246 break;
25247
0af92d60
JK
25248 case DW_MACRO_define:
25249 case DW_MACRO_undef:
cf2c3c16
TT
25250 /* Only skip the data by MAC_PTR. */
25251 {
25252 unsigned int bytes_read;
25253
25254 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25255 mac_ptr += bytes_read;
25256 read_direct_string (abfd, mac_ptr, &bytes_read);
25257 mac_ptr += bytes_read;
25258 }
25259 break;
25260
0af92d60 25261 case DW_MACRO_start_file:
cf2c3c16
TT
25262 {
25263 unsigned int bytes_read;
25264 int line, file;
25265
25266 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25267 mac_ptr += bytes_read;
25268 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25269 mac_ptr += bytes_read;
25270
804d2729 25271 current_file = macro_start_file (cu, file, line, current_file, lh);
cf2c3c16
TT
25272 }
25273 break;
25274
0af92d60 25275 case DW_MACRO_end_file:
cf2c3c16
TT
25276 /* No data to skip by MAC_PTR. */
25277 break;
25278
0af92d60
JK
25279 case DW_MACRO_define_strp:
25280 case DW_MACRO_undef_strp:
25281 case DW_MACRO_define_sup:
25282 case DW_MACRO_undef_sup:
cf2c3c16
TT
25283 {
25284 unsigned int bytes_read;
25285
25286 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25287 mac_ptr += bytes_read;
25288 mac_ptr += offset_size;
25289 }
25290 break;
25291
0af92d60
JK
25292 case DW_MACRO_import:
25293 case DW_MACRO_import_sup:
cf2c3c16 25294 /* Note that, according to the spec, a transparent include
0af92d60 25295 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
25296 skip this opcode. */
25297 mac_ptr += offset_size;
25298 break;
25299
25300 case DW_MACINFO_vendor_ext:
25301 /* Only skip the data by MAC_PTR. */
25302 if (!section_is_gnu)
25303 {
25304 unsigned int bytes_read;
25305
25306 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25307 mac_ptr += bytes_read;
25308 read_direct_string (abfd, mac_ptr, &bytes_read);
25309 mac_ptr += bytes_read;
25310 }
25311 /* FALLTHROUGH */
25312
25313 default:
25314 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25315 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25316 section);
25317 if (mac_ptr == NULL)
25318 return;
25319 break;
25320 }
132448f8 25321 DIAGNOSTIC_POP
cf2c3c16
TT
25322 } while (macinfo_type != 0 && current_file == NULL);
25323
25324 /* Second pass: Process all entries.
25325
25326 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25327 command-line macro definitions/undefinitions. This flag is unset when we
25328 reach the first DW_MACINFO_start_file entry. */
25329
fc4007c9
TT
25330 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25331 htab_eq_pointer,
25332 NULL, xcalloc, xfree));
8fc3fc34 25333 mac_ptr = section->buffer + offset;
fc4007c9 25334 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 25335 *slot = (void *) mac_ptr;
804d2729 25336 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
43f3e411 25337 current_file, lh, section,
fc4007c9
TT
25338 section_is_gnu, 0, offset_size,
25339 include_hash.get ());
cf2c3c16
TT
25340}
25341
8e19ed76 25342/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 25343 if so return true else false. */
380bca97 25344
8e19ed76 25345static int
6e5a29e1 25346attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
25347{
25348 return (attr == NULL ? 0 :
25349 attr->form == DW_FORM_block1
25350 || attr->form == DW_FORM_block2
25351 || attr->form == DW_FORM_block4
2dc7f7b3
TT
25352 || attr->form == DW_FORM_block
25353 || attr->form == DW_FORM_exprloc);
8e19ed76 25354}
4c2df51b 25355
c6a0999f
JB
25356/* Return non-zero if ATTR's value is a section offset --- classes
25357 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25358 You may use DW_UNSND (attr) to retrieve such offsets.
25359
25360 Section 7.5.4, "Attribute Encodings", explains that no attribute
25361 may have a value that belongs to more than one of these classes; it
25362 would be ambiguous if we did, because we use the same forms for all
25363 of them. */
380bca97 25364
3690dd37 25365static int
6e5a29e1 25366attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
25367{
25368 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
25369 || attr->form == DW_FORM_data8
25370 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
25371}
25372
3690dd37
JB
25373/* Return non-zero if ATTR's value falls in the 'constant' class, or
25374 zero otherwise. When this function returns true, you can apply
25375 dwarf2_get_attr_constant_value to it.
25376
25377 However, note that for some attributes you must check
25378 attr_form_is_section_offset before using this test. DW_FORM_data4
25379 and DW_FORM_data8 are members of both the constant class, and of
25380 the classes that contain offsets into other debug sections
25381 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25382 that, if an attribute's can be either a constant or one of the
25383 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
25384 taken as section offsets, not constants.
25385
25386 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25387 cannot handle that. */
380bca97 25388
3690dd37 25389static int
6e5a29e1 25390attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
25391{
25392 switch (attr->form)
25393 {
25394 case DW_FORM_sdata:
25395 case DW_FORM_udata:
25396 case DW_FORM_data1:
25397 case DW_FORM_data2:
25398 case DW_FORM_data4:
25399 case DW_FORM_data8:
663c44ac 25400 case DW_FORM_implicit_const:
3690dd37
JB
25401 return 1;
25402 default:
25403 return 0;
25404 }
25405}
25406
7771576e
SA
25407
25408/* DW_ADDR is always stored already as sect_offset; despite for the forms
25409 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25410
25411static int
6e5a29e1 25412attr_form_is_ref (const struct attribute *attr)
7771576e
SA
25413{
25414 switch (attr->form)
25415 {
25416 case DW_FORM_ref_addr:
25417 case DW_FORM_ref1:
25418 case DW_FORM_ref2:
25419 case DW_FORM_ref4:
25420 case DW_FORM_ref8:
25421 case DW_FORM_ref_udata:
25422 case DW_FORM_GNU_ref_alt:
25423 return 1;
25424 default:
25425 return 0;
25426 }
25427}
25428
3019eac3
DE
25429/* Return the .debug_loc section to use for CU.
25430 For DWO files use .debug_loc.dwo. */
25431
25432static struct dwarf2_section_info *
25433cu_debug_loc_section (struct dwarf2_cu *cu)
25434{
518817b3
SM
25435 struct dwarf2_per_objfile *dwarf2_per_objfile
25436 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 25437
3019eac3 25438 if (cu->dwo_unit)
43988095
JK
25439 {
25440 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 25441
43988095
JK
25442 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25443 }
25444 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25445 : &dwarf2_per_objfile->loc);
3019eac3
DE
25446}
25447
8cf6f0b1
TT
25448/* A helper function that fills in a dwarf2_loclist_baton. */
25449
25450static void
25451fill_in_loclist_baton (struct dwarf2_cu *cu,
25452 struct dwarf2_loclist_baton *baton,
ff39bb5e 25453 const struct attribute *attr)
8cf6f0b1 25454{
518817b3
SM
25455 struct dwarf2_per_objfile *dwarf2_per_objfile
25456 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
25457 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25458
25459 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
25460
25461 baton->per_cu = cu->per_cu;
25462 gdb_assert (baton->per_cu);
25463 /* We don't know how long the location list is, but make sure we
25464 don't run off the edge of the section. */
3019eac3
DE
25465 baton->size = section->size - DW_UNSND (attr);
25466 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 25467 baton->base_address = cu->base_address;
f664829e 25468 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
25469}
25470
4c2df51b 25471static void
ff39bb5e 25472dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 25473 struct dwarf2_cu *cu, int is_block)
4c2df51b 25474{
518817b3
SM
25475 struct dwarf2_per_objfile *dwarf2_per_objfile
25476 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25477 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 25478 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 25479
3690dd37 25480 if (attr_form_is_section_offset (attr)
3019eac3 25481 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
25482 the section. If so, fall through to the complaint in the
25483 other branch. */
3019eac3 25484 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 25485 {
0d53c4c4 25486 struct dwarf2_loclist_baton *baton;
4c2df51b 25487
8d749320 25488 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 25489
8cf6f0b1 25490 fill_in_loclist_baton (cu, baton, attr);
be391dca 25491
d00adf39 25492 if (cu->base_known == 0)
b98664d3 25493 complaint (_("Location list used without "
3e43a32a 25494 "specifying the CU base address."));
4c2df51b 25495
f1e6e072
TT
25496 SYMBOL_ACLASS_INDEX (sym) = (is_block
25497 ? dwarf2_loclist_block_index
25498 : dwarf2_loclist_index);
0d53c4c4
DJ
25499 SYMBOL_LOCATION_BATON (sym) = baton;
25500 }
25501 else
25502 {
25503 struct dwarf2_locexpr_baton *baton;
25504
8d749320 25505 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
25506 baton->per_cu = cu->per_cu;
25507 gdb_assert (baton->per_cu);
0d53c4c4
DJ
25508
25509 if (attr_form_is_block (attr))
25510 {
25511 /* Note that we're just copying the block's data pointer
25512 here, not the actual data. We're still pointing into the
6502dd73
DJ
25513 info_buffer for SYM's objfile; right now we never release
25514 that buffer, but when we do clean up properly this may
25515 need to change. */
0d53c4c4
DJ
25516 baton->size = DW_BLOCK (attr)->size;
25517 baton->data = DW_BLOCK (attr)->data;
25518 }
25519 else
25520 {
25521 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 25522 sym->natural_name ());
0d53c4c4 25523 baton->size = 0;
0d53c4c4 25524 }
6e70227d 25525
f1e6e072
TT
25526 SYMBOL_ACLASS_INDEX (sym) = (is_block
25527 ? dwarf2_locexpr_block_index
25528 : dwarf2_locexpr_index);
0d53c4c4
DJ
25529 SYMBOL_LOCATION_BATON (sym) = baton;
25530 }
4c2df51b 25531}
6502dd73 25532
9aa1f1e3
TT
25533/* Return the OBJFILE associated with the compilation unit CU. If CU
25534 came from a separate debuginfo file, then the master objfile is
25535 returned. */
ae0d2f24
UW
25536
25537struct objfile *
25538dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25539{
e3b94546 25540 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
25541
25542 /* Return the master objfile, so that we can report and look up the
25543 correct file containing this variable. */
25544 if (objfile->separate_debug_objfile_backlink)
25545 objfile = objfile->separate_debug_objfile_backlink;
25546
25547 return objfile;
25548}
25549
96408a79
SA
25550/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25551 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25552 CU_HEADERP first. */
25553
25554static const struct comp_unit_head *
25555per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25556 struct dwarf2_per_cu_data *per_cu)
25557{
d521ce57 25558 const gdb_byte *info_ptr;
96408a79
SA
25559
25560 if (per_cu->cu)
25561 return &per_cu->cu->header;
25562
9c541725 25563 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
25564
25565 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
25566 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25567 rcuh_kind::COMPILE);
96408a79
SA
25568
25569 return cu_headerp;
25570}
25571
ae0d2f24
UW
25572/* Return the address size given in the compilation unit header for CU. */
25573
98714339 25574int
ae0d2f24
UW
25575dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25576{
96408a79
SA
25577 struct comp_unit_head cu_header_local;
25578 const struct comp_unit_head *cu_headerp;
c471e790 25579
96408a79
SA
25580 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25581
25582 return cu_headerp->addr_size;
ae0d2f24
UW
25583}
25584
9eae7c52
TT
25585/* Return the offset size given in the compilation unit header for CU. */
25586
25587int
25588dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25589{
96408a79
SA
25590 struct comp_unit_head cu_header_local;
25591 const struct comp_unit_head *cu_headerp;
9c6c53f7 25592
96408a79
SA
25593 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25594
25595 return cu_headerp->offset_size;
25596}
25597
25598/* See its dwarf2loc.h declaration. */
25599
25600int
25601dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25602{
25603 struct comp_unit_head cu_header_local;
25604 const struct comp_unit_head *cu_headerp;
25605
25606 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25607
25608 if (cu_headerp->version == 2)
25609 return cu_headerp->addr_size;
25610 else
25611 return cu_headerp->offset_size;
181cebd4
JK
25612}
25613
9aa1f1e3
TT
25614/* Return the text offset of the CU. The returned offset comes from
25615 this CU's objfile. If this objfile came from a separate debuginfo
25616 file, then the offset may be different from the corresponding
25617 offset in the parent objfile. */
25618
25619CORE_ADDR
25620dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25621{
e3b94546 25622 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9aa1f1e3 25623
6a053cb1 25624 return objfile->section_offsets[SECT_OFF_TEXT (objfile)];
9aa1f1e3
TT
25625}
25626
9a49df9d
AB
25627/* Return a type that is a generic pointer type, the size of which matches
25628 the address size given in the compilation unit header for PER_CU. */
25629static struct type *
25630dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25631{
25632 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25633 struct type *void_type = objfile_type (objfile)->builtin_void;
25634 struct type *addr_type = lookup_pointer_type (void_type);
25635 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25636
25637 if (TYPE_LENGTH (addr_type) == addr_size)
25638 return addr_type;
25639
25640 addr_type
25641 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25642 return addr_type;
25643}
25644
43988095
JK
25645/* Return DWARF version number of PER_CU. */
25646
25647short
25648dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25649{
25650 return per_cu->dwarf_version;
25651}
25652
348e048f
DE
25653/* Locate the .debug_info compilation unit from CU's objfile which contains
25654 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
25655
25656static struct dwarf2_per_cu_data *
9c541725 25657dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 25658 unsigned int offset_in_dwz,
ed2dc618 25659 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25660{
25661 struct dwarf2_per_cu_data *this_cu;
25662 int low, high;
25663
ae038cb0 25664 low = 0;
b76e467d 25665 high = dwarf2_per_objfile->all_comp_units.size () - 1;
ae038cb0
DJ
25666 while (high > low)
25667 {
36586728 25668 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 25669 int mid = low + (high - low) / 2;
9a619af0 25670
36586728 25671 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
36586728 25672 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 25673 || (mid_cu->is_dwz == offset_in_dwz
45b8ae0c 25674 && mid_cu->sect_off + mid_cu->length >= sect_off))
ae038cb0
DJ
25675 high = mid;
25676 else
25677 low = mid + 1;
25678 }
25679 gdb_assert (low == high);
36586728 25680 this_cu = dwarf2_per_objfile->all_comp_units[low];
45b8ae0c 25681 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 25682 {
36586728 25683 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 25684 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
25685 "offset %s [in module %s]"),
25686 sect_offset_str (sect_off),
ed2dc618 25687 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 25688
9c541725
PA
25689 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25690 <= sect_off);
ae038cb0
DJ
25691 return dwarf2_per_objfile->all_comp_units[low-1];
25692 }
25693 else
25694 {
b76e467d 25695 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 25696 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 25697 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 25698 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25699 return this_cu;
25700 }
25701}
25702
23745b47 25703/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 25704
fcd3b13d
SM
25705dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25706 : per_cu (per_cu_),
9068261f
AB
25707 mark (false),
25708 has_loclist (false),
25709 checked_producer (false),
25710 producer_is_gxx_lt_4_6 (false),
25711 producer_is_gcc_lt_4_3 (false),
eb77c9df 25712 producer_is_icc (false),
9068261f 25713 producer_is_icc_lt_14 (false),
c258c396 25714 producer_is_codewarrior (false),
9068261f 25715 processing_has_namespace_info (false)
93311388 25716{
fcd3b13d
SM
25717 per_cu->cu = this;
25718}
25719
25720/* Destroy a dwarf2_cu. */
25721
25722dwarf2_cu::~dwarf2_cu ()
25723{
25724 per_cu->cu = NULL;
9816fde3
JK
25725}
25726
25727/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25728
25729static void
95554aad
TT
25730prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25731 enum language pretend_language)
9816fde3
JK
25732{
25733 struct attribute *attr;
25734
25735 /* Set the language we're debugging. */
25736 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 25737 if (attr != nullptr)
9816fde3
JK
25738 set_cu_language (DW_UNSND (attr), cu);
25739 else
9cded63f 25740 {
95554aad 25741 cu->language = pretend_language;
9cded63f
TT
25742 cu->language_defn = language_def (cu->language);
25743 }
dee91e82 25744
7d45c7c3 25745 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25746}
25747
ae038cb0
DJ
25748/* Increase the age counter on each cached compilation unit, and free
25749 any that are too old. */
25750
25751static void
ed2dc618 25752age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25753{
25754 struct dwarf2_per_cu_data *per_cu, **last_chain;
25755
25756 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25757 per_cu = dwarf2_per_objfile->read_in_chain;
25758 while (per_cu != NULL)
25759 {
25760 per_cu->cu->last_used ++;
b4f54984 25761 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25762 dwarf2_mark (per_cu->cu);
25763 per_cu = per_cu->cu->read_in_chain;
25764 }
25765
25766 per_cu = dwarf2_per_objfile->read_in_chain;
25767 last_chain = &dwarf2_per_objfile->read_in_chain;
25768 while (per_cu != NULL)
25769 {
25770 struct dwarf2_per_cu_data *next_cu;
25771
25772 next_cu = per_cu->cu->read_in_chain;
25773
25774 if (!per_cu->cu->mark)
25775 {
fcd3b13d 25776 delete per_cu->cu;
ae038cb0
DJ
25777 *last_chain = next_cu;
25778 }
25779 else
25780 last_chain = &per_cu->cu->read_in_chain;
25781
25782 per_cu = next_cu;
25783 }
25784}
25785
25786/* Remove a single compilation unit from the cache. */
25787
25788static void
dee91e82 25789free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25790{
25791 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25792 struct dwarf2_per_objfile *dwarf2_per_objfile
25793 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25794
25795 per_cu = dwarf2_per_objfile->read_in_chain;
25796 last_chain = &dwarf2_per_objfile->read_in_chain;
25797 while (per_cu != NULL)
25798 {
25799 struct dwarf2_per_cu_data *next_cu;
25800
25801 next_cu = per_cu->cu->read_in_chain;
25802
dee91e82 25803 if (per_cu == target_per_cu)
ae038cb0 25804 {
fcd3b13d 25805 delete per_cu->cu;
dee91e82 25806 per_cu->cu = NULL;
ae038cb0
DJ
25807 *last_chain = next_cu;
25808 break;
25809 }
25810 else
25811 last_chain = &per_cu->cu->read_in_chain;
25812
25813 per_cu = next_cu;
25814 }
25815}
25816
dee91e82
DE
25817/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25818 We store these in a hash table separate from the DIEs, and preserve them
25819 when the DIEs are flushed out of cache.
25820
25821 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25822 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25823 or the type may come from a DWO file. Furthermore, while it's more logical
25824 to use per_cu->section+offset, with Fission the section with the data is in
25825 the DWO file but we don't know that section at the point we need it.
25826 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25827 because we can enter the lookup routine, get_die_type_at_offset, from
25828 outside this file, and thus won't necessarily have PER_CU->cu.
25829 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25830
dee91e82 25831struct dwarf2_per_cu_offset_and_type
1c379e20 25832{
dee91e82 25833 const struct dwarf2_per_cu_data *per_cu;
9c541725 25834 sect_offset sect_off;
1c379e20
DJ
25835 struct type *type;
25836};
25837
dee91e82 25838/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25839
25840static hashval_t
dee91e82 25841per_cu_offset_and_type_hash (const void *item)
1c379e20 25842{
9a3c8263
SM
25843 const struct dwarf2_per_cu_offset_and_type *ofs
25844 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25845
9c541725 25846 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25847}
25848
dee91e82 25849/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25850
25851static int
dee91e82 25852per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25853{
9a3c8263
SM
25854 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25855 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25856 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25857 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25858
dee91e82 25859 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25860 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25861}
25862
25863/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25864 table if necessary. For convenience, return TYPE.
25865
25866 The DIEs reading must have careful ordering to:
85102364 25867 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
25868 reading current DIE.
25869 * Not trying to dereference contents of still incompletely read in types
25870 while reading in other DIEs.
25871 * Enable referencing still incompletely read in types just by a pointer to
25872 the type without accessing its fields.
25873
25874 Therefore caller should follow these rules:
25875 * Try to fetch any prerequisite types we may need to build this DIE type
25876 before building the type and calling set_die_type.
e71ec853 25877 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25878 possible before fetching more types to complete the current type.
25879 * Make the type as complete as possible before fetching more types. */
1c379e20 25880
f792889a 25881static struct type *
1c379e20
DJ
25882set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25883{
518817b3
SM
25884 struct dwarf2_per_objfile *dwarf2_per_objfile
25885 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25886 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25887 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25888 struct attribute *attr;
25889 struct dynamic_prop prop;
1c379e20 25890
b4ba55a1
JB
25891 /* For Ada types, make sure that the gnat-specific data is always
25892 initialized (if not already set). There are a few types where
25893 we should not be doing so, because the type-specific area is
25894 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25895 where the type-specific area is used to store the floatformat).
25896 But this is not a problem, because the gnat-specific information
25897 is actually not needed for these types. */
25898 if (need_gnat_info (cu)
25899 && TYPE_CODE (type) != TYPE_CODE_FUNC
25900 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25901 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25902 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25903 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25904 && !HAVE_GNAT_AUX_INFO (type))
25905 INIT_GNAT_SPECIFIC (type);
25906
3f2f83dd
KB
25907 /* Read DW_AT_allocated and set in type. */
25908 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25909 if (attr_form_is_block (attr))
25910 {
9a49df9d
AB
25911 struct type *prop_type
25912 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25913 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25914 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25915 }
25916 else if (attr != NULL)
25917 {
b98664d3 25918 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25919 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25920 sect_offset_str (die->sect_off));
3f2f83dd
KB
25921 }
25922
25923 /* Read DW_AT_associated and set in type. */
25924 attr = dwarf2_attr (die, DW_AT_associated, cu);
25925 if (attr_form_is_block (attr))
25926 {
9a49df9d
AB
25927 struct type *prop_type
25928 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25929 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25930 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25931 }
25932 else if (attr != NULL)
25933 {
b98664d3 25934 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25935 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25936 sect_offset_str (die->sect_off));
3f2f83dd
KB
25937 }
25938
3cdcd0ce
JB
25939 /* Read DW_AT_data_location and set in type. */
25940 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d
AB
25941 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25942 dwarf2_per_cu_addr_type (cu->per_cu)))
50a82047 25943 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25944
dee91e82 25945 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25946 {
dee91e82
DE
25947 dwarf2_per_objfile->die_type_hash =
25948 htab_create_alloc_ex (127,
25949 per_cu_offset_and_type_hash,
25950 per_cu_offset_and_type_eq,
25951 NULL,
25952 &objfile->objfile_obstack,
25953 hashtab_obstack_allocate,
25954 dummy_obstack_deallocate);
f792889a 25955 }
1c379e20 25956
dee91e82 25957 ofs.per_cu = cu->per_cu;
9c541725 25958 ofs.sect_off = die->sect_off;
1c379e20 25959 ofs.type = type;
dee91e82
DE
25960 slot = (struct dwarf2_per_cu_offset_and_type **)
25961 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57 25962 if (*slot)
b98664d3 25963 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25964 sect_offset_str (die->sect_off));
8d749320
SM
25965 *slot = XOBNEW (&objfile->objfile_obstack,
25966 struct dwarf2_per_cu_offset_and_type);
1c379e20 25967 **slot = ofs;
f792889a 25968 return type;
1c379e20
DJ
25969}
25970
9c541725 25971/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25972 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25973
25974static struct type *
9c541725 25975get_die_type_at_offset (sect_offset sect_off,
673bfd45 25976 struct dwarf2_per_cu_data *per_cu)
1c379e20 25977{
dee91e82 25978 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25979 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25980
dee91e82 25981 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25982 return NULL;
1c379e20 25983
dee91e82 25984 ofs.per_cu = per_cu;
9c541725 25985 ofs.sect_off = sect_off;
9a3c8263
SM
25986 slot = ((struct dwarf2_per_cu_offset_and_type *)
25987 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25988 if (slot)
25989 return slot->type;
25990 else
25991 return NULL;
25992}
25993
02142a6c 25994/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25995 or return NULL if DIE does not have a saved type. */
25996
25997static struct type *
25998get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25999{
9c541725 26000 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
26001}
26002
10b3939b
DJ
26003/* Add a dependence relationship from CU to REF_PER_CU. */
26004
26005static void
26006dwarf2_add_dependence (struct dwarf2_cu *cu,
26007 struct dwarf2_per_cu_data *ref_per_cu)
26008{
26009 void **slot;
26010
26011 if (cu->dependencies == NULL)
26012 cu->dependencies
26013 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
26014 NULL, &cu->comp_unit_obstack,
26015 hashtab_obstack_allocate,
26016 dummy_obstack_deallocate);
26017
26018 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
26019 if (*slot == NULL)
26020 *slot = ref_per_cu;
26021}
1c379e20 26022
f504f079
DE
26023/* Subroutine of dwarf2_mark to pass to htab_traverse.
26024 Set the mark field in every compilation unit in the
ae038cb0
DJ
26025 cache that we must keep because we are keeping CU. */
26026
10b3939b
DJ
26027static int
26028dwarf2_mark_helper (void **slot, void *data)
26029{
26030 struct dwarf2_per_cu_data *per_cu;
26031
26032 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
26033
26034 /* cu->dependencies references may not yet have been ever read if QUIT aborts
26035 reading of the chain. As such dependencies remain valid it is not much
26036 useful to track and undo them during QUIT cleanups. */
26037 if (per_cu->cu == NULL)
26038 return 1;
26039
10b3939b
DJ
26040 if (per_cu->cu->mark)
26041 return 1;
9068261f 26042 per_cu->cu->mark = true;
10b3939b
DJ
26043
26044 if (per_cu->cu->dependencies != NULL)
26045 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
26046
26047 return 1;
26048}
26049
f504f079
DE
26050/* Set the mark field in CU and in every other compilation unit in the
26051 cache that we must keep because we are keeping CU. */
26052
ae038cb0
DJ
26053static void
26054dwarf2_mark (struct dwarf2_cu *cu)
26055{
26056 if (cu->mark)
26057 return;
9068261f 26058 cu->mark = true;
10b3939b
DJ
26059 if (cu->dependencies != NULL)
26060 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
26061}
26062
26063static void
26064dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
26065{
26066 while (per_cu)
26067 {
9068261f 26068 per_cu->cu->mark = false;
ae038cb0
DJ
26069 per_cu = per_cu->cu->read_in_chain;
26070 }
72bf9492
DJ
26071}
26072
72bf9492
DJ
26073/* Trivial hash function for partial_die_info: the hash value of a DIE
26074 is its offset in .debug_info for this objfile. */
26075
26076static hashval_t
26077partial_die_hash (const void *item)
26078{
9a3c8263
SM
26079 const struct partial_die_info *part_die
26080 = (const struct partial_die_info *) item;
9a619af0 26081
9c541725 26082 return to_underlying (part_die->sect_off);
72bf9492
DJ
26083}
26084
26085/* Trivial comparison function for partial_die_info structures: two DIEs
26086 are equal if they have the same offset. */
26087
26088static int
26089partial_die_eq (const void *item_lhs, const void *item_rhs)
26090{
9a3c8263
SM
26091 const struct partial_die_info *part_die_lhs
26092 = (const struct partial_die_info *) item_lhs;
26093 const struct partial_die_info *part_die_rhs
26094 = (const struct partial_die_info *) item_rhs;
9a619af0 26095
9c541725 26096 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
26097}
26098
3c3bb058
AB
26099struct cmd_list_element *set_dwarf_cmdlist;
26100struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
26101
26102static void
981a3fb3 26103set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 26104{
b4f54984 26105 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 26106 gdb_stdout);
ae038cb0
DJ
26107}
26108
26109static void
981a3fb3 26110show_dwarf_cmd (const char *args, int from_tty)
6e70227d 26111{
b4f54984 26112 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
26113}
26114
491144b5 26115bool dwarf_always_disassemble;
437afbb8 26116
437afbb8 26117static void
cd4fb1b2
SM
26118show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
26119 struct cmd_list_element *c, const char *value)
9291a0cd 26120{
cd4fb1b2
SM
26121 fprintf_filtered (file,
26122 _("Whether to always disassemble "
26123 "DWARF expressions is %s.\n"),
26124 value);
9291a0cd
TT
26125}
26126
9291a0cd 26127static void
cd4fb1b2
SM
26128show_check_physname (struct ui_file *file, int from_tty,
26129 struct cmd_list_element *c, const char *value)
9291a0cd 26130{
cd4fb1b2
SM
26131 fprintf_filtered (file,
26132 _("Whether to check \"physname\" is %s.\n"),
26133 value);
9291a0cd
TT
26134}
26135
cd4fb1b2
SM
26136void
26137_initialize_dwarf2_read (void)
9291a0cd 26138{
cd4fb1b2
SM
26139 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
26140Set DWARF specific variables.\n\
590042fc 26141Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
26142 &set_dwarf_cmdlist, "maintenance set dwarf ",
26143 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 26144
cd4fb1b2 26145 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
26146Show DWARF specific variables.\n\
26147Show DWARF variables such as the cache size."),
cd4fb1b2
SM
26148 &show_dwarf_cmdlist, "maintenance show dwarf ",
26149 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 26150
cd4fb1b2
SM
26151 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
26152 &dwarf_max_cache_age, _("\
26153Set the upper bound on the age of cached DWARF compilation units."), _("\
26154Show the upper bound on the age of cached DWARF compilation units."), _("\
26155A higher limit means that cached compilation units will be stored\n\
26156in memory longer, and more total memory will be used. Zero disables\n\
26157caching, which can slow down startup."),
26158 NULL,
26159 show_dwarf_max_cache_age,
26160 &set_dwarf_cmdlist,
26161 &show_dwarf_cmdlist);
156942c7 26162
cd4fb1b2
SM
26163 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
26164 &dwarf_always_disassemble, _("\
26165Set whether `info address' always disassembles DWARF expressions."), _("\
26166Show whether `info address' always disassembles DWARF expressions."), _("\
26167When enabled, DWARF expressions are always printed in an assembly-like\n\
26168syntax. When disabled, expressions will be printed in a more\n\
26169conversational style, when possible."),
26170 NULL,
26171 show_dwarf_always_disassemble,
26172 &set_dwarf_cmdlist,
26173 &show_dwarf_cmdlist);
9291a0cd 26174
cd4fb1b2
SM
26175 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
26176Set debugging of the DWARF reader."), _("\
26177Show debugging of the DWARF reader."), _("\
26178When enabled (non-zero), debugging messages are printed during DWARF\n\
26179reading and symtab expansion. A value of 1 (one) provides basic\n\
26180information. A value greater than 1 provides more verbose information."),
26181 NULL,
26182 NULL,
26183 &setdebuglist, &showdebuglist);
9291a0cd 26184
cd4fb1b2
SM
26185 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
26186Set debugging of the DWARF DIE reader."), _("\
26187Show debugging of the DWARF DIE reader."), _("\
26188When enabled (non-zero), DIEs are dumped after they are read in.\n\
26189The value is the maximum depth to print."),
26190 NULL,
26191 NULL,
26192 &setdebuglist, &showdebuglist);
9291a0cd 26193
cd4fb1b2
SM
26194 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
26195Set debugging of the dwarf line reader."), _("\
26196Show debugging of the dwarf line reader."), _("\
26197When enabled (non-zero), line number entries are dumped as they are read in.\n\
26198A value of 1 (one) provides basic information.\n\
26199A value greater than 1 provides more verbose information."),
26200 NULL,
26201 NULL,
26202 &setdebuglist, &showdebuglist);
437afbb8 26203
cd4fb1b2
SM
26204 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
26205Set cross-checking of \"physname\" code against demangler."), _("\
26206Show cross-checking of \"physname\" code against demangler."), _("\
26207When enabled, GDB's internal \"physname\" code is checked against\n\
26208the demangler."),
26209 NULL, show_check_physname,
26210 &setdebuglist, &showdebuglist);
900e11f9 26211
e615022a
DE
26212 add_setshow_boolean_cmd ("use-deprecated-index-sections",
26213 no_class, &use_deprecated_index_sections, _("\
26214Set whether to use deprecated gdb_index sections."), _("\
26215Show whether to use deprecated gdb_index sections."), _("\
26216When enabled, deprecated .gdb_index sections are used anyway.\n\
26217Normally they are ignored either because of a missing feature or\n\
26218performance issue.\n\
26219Warning: This option must be enabled before gdb reads the file."),
26220 NULL,
26221 NULL,
26222 &setlist, &showlist);
26223
f1e6e072
TT
26224 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
26225 &dwarf2_locexpr_funcs);
26226 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
26227 &dwarf2_loclist_funcs);
26228
26229 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
26230 &dwarf2_block_frame_base_locexpr_funcs);
26231 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
26232 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
26233
26234#if GDB_SELF_TEST
26235 selftests::register_test ("dw2_expand_symtabs_matching",
26236 selftests::dw2_expand_symtabs_matching::run_test);
26237#endif
6502dd73 26238}
This page took 5.035201 seconds and 4 git commands to generate.